2 Copyright (C) 2000, 2001, 2002, 2003 Free Software Foundation, Inc.
3 Written by Mark Mitchell <mark@codesourcery.com>.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 GCC is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
24 #include "coretypes.h"
26 #include "dyn-string.h"
34 #include "diagnostic.h"
44 A cp_lexer represents a stream of cp_tokens. It allows arbitrary
50 We use a circular buffer to store incoming tokens.
52 Some artifacts of the C++ language (such as the
53 expression/declaration ambiguity) require arbitrary look-ahead.
54 The strategy we adopt for dealing with these problems is to attempt
55 to parse one construct (e.g., the declaration) and fall back to the
56 other (e.g., the expression) if that attempt does not succeed.
57 Therefore, we must sometimes store an arbitrary number of tokens.
59 The parser routinely peeks at the next token, and then consumes it
60 later. That also requires a buffer in which to store the tokens.
62 In order to easily permit adding tokens to the end of the buffer,
63 while removing them from the beginning of the buffer, we use a
68 typedef struct cp_token GTY (())
70 /* The kind of token. */
71 enum cpp_ttype type : 8;
72 /* If this token is a keyword, this value indicates which keyword.
73 Otherwise, this value is RID_MAX. */
75 /* The value associated with this token, if any. */
77 /* The location at which this token was found. */
81 /* The number of tokens in a single token block.
82 Computed so that cp_token_block fits in a 512B allocation unit. */
84 #define CP_TOKEN_BLOCK_NUM_TOKENS ((512 - 3*sizeof (char*))/sizeof (cp_token))
86 /* A group of tokens. These groups are chained together to store
87 large numbers of tokens. (For example, a token block is created
88 when the body of an inline member function is first encountered;
89 the tokens are processed later after the class definition is
92 This somewhat ungainly data structure (as opposed to, say, a
93 variable-length array), is used due to constraints imposed by the
94 current garbage-collection methodology. If it is made more
95 flexible, we could perhaps simplify the data structures involved. */
97 typedef struct cp_token_block GTY (())
100 cp_token tokens[CP_TOKEN_BLOCK_NUM_TOKENS];
101 /* The number of tokens in this block. */
103 /* The next token block in the chain. */
104 struct cp_token_block *next;
105 /* The previous block in the chain. */
106 struct cp_token_block *prev;
109 typedef struct cp_token_cache GTY (())
111 /* The first block in the cache. NULL if there are no tokens in the
113 cp_token_block *first;
114 /* The last block in the cache. NULL If there are no tokens in the
116 cp_token_block *last;
121 static cp_token_cache *cp_token_cache_new
123 static void cp_token_cache_push_token
124 (cp_token_cache *, cp_token *);
126 /* Create a new cp_token_cache. */
128 static cp_token_cache *
129 cp_token_cache_new ()
131 return ggc_alloc_cleared (sizeof (cp_token_cache));
134 /* Add *TOKEN to *CACHE. */
137 cp_token_cache_push_token (cp_token_cache *cache,
140 cp_token_block *b = cache->last;
142 /* See if we need to allocate a new token block. */
143 if (!b || b->num_tokens == CP_TOKEN_BLOCK_NUM_TOKENS)
145 b = ggc_alloc_cleared (sizeof (cp_token_block));
146 b->prev = cache->last;
149 cache->last->next = b;
153 cache->first = cache->last = b;
155 /* Add this token to the current token block. */
156 b->tokens[b->num_tokens++] = *token;
159 /* The cp_lexer structure represents the C++ lexer. It is responsible
160 for managing the token stream from the preprocessor and supplying
163 typedef struct cp_lexer GTY (())
165 /* The memory allocated for the buffer. Never NULL. */
166 cp_token * GTY ((length ("(%h.buffer_end - %h.buffer)"))) buffer;
167 /* A pointer just past the end of the memory allocated for the buffer. */
168 cp_token * GTY ((skip (""))) buffer_end;
169 /* The first valid token in the buffer, or NULL if none. */
170 cp_token * GTY ((skip (""))) first_token;
171 /* The next available token. If NEXT_TOKEN is NULL, then there are
172 no more available tokens. */
173 cp_token * GTY ((skip (""))) next_token;
174 /* A pointer just past the last available token. If FIRST_TOKEN is
175 NULL, however, there are no available tokens, and then this
176 location is simply the place in which the next token read will be
177 placed. If LAST_TOKEN == FIRST_TOKEN, then the buffer is full.
178 When the LAST_TOKEN == BUFFER, then the last token is at the
179 highest memory address in the BUFFER. */
180 cp_token * GTY ((skip (""))) last_token;
182 /* A stack indicating positions at which cp_lexer_save_tokens was
183 called. The top entry is the most recent position at which we
184 began saving tokens. The entries are differences in token
185 position between FIRST_TOKEN and the first saved token.
187 If the stack is non-empty, we are saving tokens. When a token is
188 consumed, the NEXT_TOKEN pointer will move, but the FIRST_TOKEN
189 pointer will not. The token stream will be preserved so that it
190 can be reexamined later.
192 If the stack is empty, then we are not saving tokens. Whenever a
193 token is consumed, the FIRST_TOKEN pointer will be moved, and the
194 consumed token will be gone forever. */
195 varray_type saved_tokens;
197 /* The STRING_CST tokens encountered while processing the current
199 varray_type string_tokens;
201 /* True if we should obtain more tokens from the preprocessor; false
202 if we are processing a saved token cache. */
205 /* True if we should output debugging information. */
208 /* The next lexer in a linked list of lexers. */
209 struct cp_lexer *next;
214 static cp_lexer *cp_lexer_new_main
216 static cp_lexer *cp_lexer_new_from_tokens
217 (struct cp_token_cache *);
218 static int cp_lexer_saving_tokens
220 static cp_token *cp_lexer_next_token
221 (cp_lexer *, cp_token *);
222 static ptrdiff_t cp_lexer_token_difference
223 (cp_lexer *, cp_token *, cp_token *);
224 static cp_token *cp_lexer_read_token
226 static void cp_lexer_maybe_grow_buffer
228 static void cp_lexer_get_preprocessor_token
229 (cp_lexer *, cp_token *);
230 static cp_token *cp_lexer_peek_token
232 static cp_token *cp_lexer_peek_nth_token
233 (cp_lexer *, size_t);
234 static inline bool cp_lexer_next_token_is
235 (cp_lexer *, enum cpp_ttype);
236 static bool cp_lexer_next_token_is_not
237 (cp_lexer *, enum cpp_ttype);
238 static bool cp_lexer_next_token_is_keyword
239 (cp_lexer *, enum rid);
240 static cp_token *cp_lexer_consume_token
242 static void cp_lexer_purge_token
244 static void cp_lexer_purge_tokens_after
245 (cp_lexer *, cp_token *);
246 static void cp_lexer_save_tokens
248 static void cp_lexer_commit_tokens
250 static void cp_lexer_rollback_tokens
252 static inline void cp_lexer_set_source_position_from_token
253 (cp_lexer *, const cp_token *);
254 static void cp_lexer_print_token
255 (FILE *, cp_token *);
256 static inline bool cp_lexer_debugging_p
258 static void cp_lexer_start_debugging
259 (cp_lexer *) ATTRIBUTE_UNUSED;
260 static void cp_lexer_stop_debugging
261 (cp_lexer *) ATTRIBUTE_UNUSED;
263 /* Manifest constants. */
265 #define CP_TOKEN_BUFFER_SIZE 5
266 #define CP_SAVED_TOKENS_SIZE 5
268 /* A token type for keywords, as opposed to ordinary identifiers. */
269 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
271 /* A token type for template-ids. If a template-id is processed while
272 parsing tentatively, it is replaced with a CPP_TEMPLATE_ID token;
273 the value of the CPP_TEMPLATE_ID is whatever was returned by
274 cp_parser_template_id. */
275 #define CPP_TEMPLATE_ID ((enum cpp_ttype) (CPP_KEYWORD + 1))
277 /* A token type for nested-name-specifiers. If a
278 nested-name-specifier is processed while parsing tentatively, it is
279 replaced with a CPP_NESTED_NAME_SPECIFIER token; the value of the
280 CPP_NESTED_NAME_SPECIFIER is whatever was returned by
281 cp_parser_nested_name_specifier_opt. */
282 #define CPP_NESTED_NAME_SPECIFIER ((enum cpp_ttype) (CPP_TEMPLATE_ID + 1))
284 /* A token type for tokens that are not tokens at all; these are used
285 to mark the end of a token block. */
286 #define CPP_NONE (CPP_NESTED_NAME_SPECIFIER + 1)
290 /* The stream to which debugging output should be written. */
291 static FILE *cp_lexer_debug_stream;
293 /* Create a new main C++ lexer, the lexer that gets tokens from the
297 cp_lexer_new_main (void)
300 cp_token first_token;
302 /* It's possible that lexing the first token will load a PCH file,
303 which is a GC collection point. So we have to grab the first
304 token before allocating any memory. */
305 cp_lexer_get_preprocessor_token (NULL, &first_token);
306 c_common_no_more_pch ();
308 /* Allocate the memory. */
309 lexer = ggc_alloc_cleared (sizeof (cp_lexer));
311 /* Create the circular buffer. */
312 lexer->buffer = ggc_calloc (CP_TOKEN_BUFFER_SIZE, sizeof (cp_token));
313 lexer->buffer_end = lexer->buffer + CP_TOKEN_BUFFER_SIZE;
315 /* There is one token in the buffer. */
316 lexer->last_token = lexer->buffer + 1;
317 lexer->first_token = lexer->buffer;
318 lexer->next_token = lexer->buffer;
319 memcpy (lexer->buffer, &first_token, sizeof (cp_token));
321 /* This lexer obtains more tokens by calling c_lex. */
322 lexer->main_lexer_p = true;
324 /* Create the SAVED_TOKENS stack. */
325 VARRAY_INT_INIT (lexer->saved_tokens, CP_SAVED_TOKENS_SIZE, "saved_tokens");
327 /* Create the STRINGS array. */
328 VARRAY_TREE_INIT (lexer->string_tokens, 32, "strings");
330 /* Assume we are not debugging. */
331 lexer->debugging_p = false;
336 /* Create a new lexer whose token stream is primed with the TOKENS.
337 When these tokens are exhausted, no new tokens will be read. */
340 cp_lexer_new_from_tokens (cp_token_cache *tokens)
344 cp_token_block *block;
345 ptrdiff_t num_tokens;
347 /* Allocate the memory. */
348 lexer = ggc_alloc_cleared (sizeof (cp_lexer));
350 /* Create a new buffer, appropriately sized. */
352 for (block = tokens->first; block != NULL; block = block->next)
353 num_tokens += block->num_tokens;
354 lexer->buffer = ggc_alloc (num_tokens * sizeof (cp_token));
355 lexer->buffer_end = lexer->buffer + num_tokens;
357 /* Install the tokens. */
358 token = lexer->buffer;
359 for (block = tokens->first; block != NULL; block = block->next)
361 memcpy (token, block->tokens, block->num_tokens * sizeof (cp_token));
362 token += block->num_tokens;
365 /* The FIRST_TOKEN is the beginning of the buffer. */
366 lexer->first_token = lexer->buffer;
367 /* The next available token is also at the beginning of the buffer. */
368 lexer->next_token = lexer->buffer;
369 /* The buffer is full. */
370 lexer->last_token = lexer->first_token;
372 /* This lexer doesn't obtain more tokens. */
373 lexer->main_lexer_p = false;
375 /* Create the SAVED_TOKENS stack. */
376 VARRAY_INT_INIT (lexer->saved_tokens, CP_SAVED_TOKENS_SIZE, "saved_tokens");
378 /* Create the STRINGS array. */
379 VARRAY_TREE_INIT (lexer->string_tokens, 32, "strings");
381 /* Assume we are not debugging. */
382 lexer->debugging_p = false;
387 /* Returns nonzero if debugging information should be output. */
390 cp_lexer_debugging_p (cp_lexer *lexer)
392 return lexer->debugging_p;
395 /* Set the current source position from the information stored in
399 cp_lexer_set_source_position_from_token (cp_lexer *lexer ATTRIBUTE_UNUSED ,
400 const cp_token *token)
402 /* Ideally, the source position information would not be a global
403 variable, but it is. */
405 /* Update the line number. */
406 if (token->type != CPP_EOF)
407 input_location = token->location;
410 /* TOKEN points into the circular token buffer. Return a pointer to
411 the next token in the buffer. */
413 static inline cp_token *
414 cp_lexer_next_token (cp_lexer* lexer, cp_token* token)
417 if (token == lexer->buffer_end)
418 token = lexer->buffer;
422 /* nonzero if we are presently saving tokens. */
425 cp_lexer_saving_tokens (const cp_lexer* lexer)
427 return VARRAY_ACTIVE_SIZE (lexer->saved_tokens) != 0;
430 /* Return a pointer to the token that is N tokens beyond TOKEN in the
434 cp_lexer_advance_token (cp_lexer *lexer, cp_token *token, ptrdiff_t n)
437 if (token >= lexer->buffer_end)
438 token = lexer->buffer + (token - lexer->buffer_end);
442 /* Returns the number of times that START would have to be incremented
443 to reach FINISH. If START and FINISH are the same, returns zero. */
446 cp_lexer_token_difference (cp_lexer* lexer, cp_token* start, cp_token* finish)
449 return finish - start;
451 return ((lexer->buffer_end - lexer->buffer)
455 /* Obtain another token from the C preprocessor and add it to the
456 token buffer. Returns the newly read token. */
459 cp_lexer_read_token (cp_lexer* lexer)
463 /* Make sure there is room in the buffer. */
464 cp_lexer_maybe_grow_buffer (lexer);
466 /* If there weren't any tokens, then this one will be the first. */
467 if (!lexer->first_token)
468 lexer->first_token = lexer->last_token;
469 /* Similarly, if there were no available tokens, there is one now. */
470 if (!lexer->next_token)
471 lexer->next_token = lexer->last_token;
473 /* Figure out where we're going to store the new token. */
474 token = lexer->last_token;
476 /* Get a new token from the preprocessor. */
477 cp_lexer_get_preprocessor_token (lexer, token);
479 /* Increment LAST_TOKEN. */
480 lexer->last_token = cp_lexer_next_token (lexer, token);
482 /* Strings should have type `const char []'. Right now, we will
483 have an ARRAY_TYPE that is constant rather than an array of
485 FIXME: Make fix_string_type get this right in the first place. */
486 if ((token->type == CPP_STRING || token->type == CPP_WSTRING)
487 && flag_const_strings)
491 /* Get the current type. It will be an ARRAY_TYPE. */
492 type = TREE_TYPE (token->value);
493 /* Use build_cplus_array_type to rebuild the array, thereby
494 getting the right type. */
495 type = build_cplus_array_type (TREE_TYPE (type), TYPE_DOMAIN (type));
496 /* Reset the type of the token. */
497 TREE_TYPE (token->value) = type;
503 /* If the circular buffer is full, make it bigger. */
506 cp_lexer_maybe_grow_buffer (cp_lexer* lexer)
508 /* If the buffer is full, enlarge it. */
509 if (lexer->last_token == lexer->first_token)
511 cp_token *new_buffer;
512 cp_token *old_buffer;
513 cp_token *new_first_token;
514 ptrdiff_t buffer_length;
515 size_t num_tokens_to_copy;
517 /* Remember the current buffer pointer. It will become invalid,
518 but we will need to do pointer arithmetic involving this
520 old_buffer = lexer->buffer;
521 /* Compute the current buffer size. */
522 buffer_length = lexer->buffer_end - lexer->buffer;
523 /* Allocate a buffer twice as big. */
524 new_buffer = ggc_realloc (lexer->buffer,
525 2 * buffer_length * sizeof (cp_token));
527 /* Because the buffer is circular, logically consecutive tokens
528 are not necessarily placed consecutively in memory.
529 Therefore, we must keep move the tokens that were before
530 FIRST_TOKEN to the second half of the newly allocated
532 num_tokens_to_copy = (lexer->first_token - old_buffer);
533 memcpy (new_buffer + buffer_length,
535 num_tokens_to_copy * sizeof (cp_token));
536 /* Clear the rest of the buffer. We never look at this storage,
537 but the garbage collector may. */
538 memset (new_buffer + buffer_length + num_tokens_to_copy, 0,
539 (buffer_length - num_tokens_to_copy) * sizeof (cp_token));
541 /* Now recompute all of the buffer pointers. */
543 = new_buffer + (lexer->first_token - old_buffer);
544 if (lexer->next_token != NULL)
546 ptrdiff_t next_token_delta;
548 if (lexer->next_token > lexer->first_token)
549 next_token_delta = lexer->next_token - lexer->first_token;
552 buffer_length - (lexer->first_token - lexer->next_token);
553 lexer->next_token = new_first_token + next_token_delta;
555 lexer->last_token = new_first_token + buffer_length;
556 lexer->buffer = new_buffer;
557 lexer->buffer_end = new_buffer + buffer_length * 2;
558 lexer->first_token = new_first_token;
562 /* Store the next token from the preprocessor in *TOKEN. */
565 cp_lexer_get_preprocessor_token (cp_lexer *lexer ATTRIBUTE_UNUSED ,
570 /* If this not the main lexer, return a terminating CPP_EOF token. */
571 if (lexer != NULL && !lexer->main_lexer_p)
573 token->type = CPP_EOF;
574 token->location.line = 0;
575 token->location.file = NULL;
576 token->value = NULL_TREE;
577 token->keyword = RID_MAX;
583 /* Keep going until we get a token we like. */
586 /* Get a new token from the preprocessor. */
587 token->type = c_lex (&token->value);
588 /* Issue messages about tokens we cannot process. */
594 error ("invalid token");
598 /* This is a good token, so we exit the loop. */
603 /* Now we've got our token. */
604 token->location = input_location;
606 /* Check to see if this token is a keyword. */
607 if (token->type == CPP_NAME
608 && C_IS_RESERVED_WORD (token->value))
610 /* Mark this token as a keyword. */
611 token->type = CPP_KEYWORD;
612 /* Record which keyword. */
613 token->keyword = C_RID_CODE (token->value);
614 /* Update the value. Some keywords are mapped to particular
615 entities, rather than simply having the value of the
616 corresponding IDENTIFIER_NODE. For example, `__const' is
617 mapped to `const'. */
618 token->value = ridpointers[token->keyword];
621 token->keyword = RID_MAX;
624 /* Return a pointer to the next token in the token stream, but do not
628 cp_lexer_peek_token (cp_lexer* lexer)
632 /* If there are no tokens, read one now. */
633 if (!lexer->next_token)
634 cp_lexer_read_token (lexer);
636 /* Provide debugging output. */
637 if (cp_lexer_debugging_p (lexer))
639 fprintf (cp_lexer_debug_stream, "cp_lexer: peeking at token: ");
640 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
641 fprintf (cp_lexer_debug_stream, "\n");
644 token = lexer->next_token;
645 cp_lexer_set_source_position_from_token (lexer, token);
649 /* Return true if the next token has the indicated TYPE. */
652 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
656 /* Peek at the next token. */
657 token = cp_lexer_peek_token (lexer);
658 /* Check to see if it has the indicated TYPE. */
659 return token->type == type;
662 /* Return true if the next token does not have the indicated TYPE. */
665 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
667 return !cp_lexer_next_token_is (lexer, type);
670 /* Return true if the next token is the indicated KEYWORD. */
673 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
677 /* Peek at the next token. */
678 token = cp_lexer_peek_token (lexer);
679 /* Check to see if it is the indicated keyword. */
680 return token->keyword == keyword;
683 /* Return a pointer to the Nth token in the token stream. If N is 1,
684 then this is precisely equivalent to cp_lexer_peek_token. */
687 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
691 /* N is 1-based, not zero-based. */
692 my_friendly_assert (n > 0, 20000224);
694 /* Skip ahead from NEXT_TOKEN, reading more tokens as necessary. */
695 token = lexer->next_token;
696 /* If there are no tokens in the buffer, get one now. */
699 cp_lexer_read_token (lexer);
700 token = lexer->next_token;
703 /* Now, read tokens until we have enough. */
706 /* Advance to the next token. */
707 token = cp_lexer_next_token (lexer, token);
708 /* If that's all the tokens we have, read a new one. */
709 if (token == lexer->last_token)
710 token = cp_lexer_read_token (lexer);
716 /* Consume the next token. The pointer returned is valid only until
717 another token is read. Callers should preserve copy the token
718 explicitly if they will need its value for a longer period of
722 cp_lexer_consume_token (cp_lexer* lexer)
726 /* If there are no tokens, read one now. */
727 if (!lexer->next_token)
728 cp_lexer_read_token (lexer);
730 /* Remember the token we'll be returning. */
731 token = lexer->next_token;
733 /* Increment NEXT_TOKEN. */
734 lexer->next_token = cp_lexer_next_token (lexer,
736 /* Check to see if we're all out of tokens. */
737 if (lexer->next_token == lexer->last_token)
738 lexer->next_token = NULL;
740 /* If we're not saving tokens, then move FIRST_TOKEN too. */
741 if (!cp_lexer_saving_tokens (lexer))
743 /* If there are no tokens available, set FIRST_TOKEN to NULL. */
744 if (!lexer->next_token)
745 lexer->first_token = NULL;
747 lexer->first_token = lexer->next_token;
750 /* Provide debugging output. */
751 if (cp_lexer_debugging_p (lexer))
753 fprintf (cp_lexer_debug_stream, "cp_lexer: consuming token: ");
754 cp_lexer_print_token (cp_lexer_debug_stream, token);
755 fprintf (cp_lexer_debug_stream, "\n");
761 /* Permanently remove the next token from the token stream. There
762 must be a valid next token already; this token never reads
763 additional tokens from the preprocessor. */
766 cp_lexer_purge_token (cp_lexer *lexer)
769 cp_token *next_token;
771 token = lexer->next_token;
774 next_token = cp_lexer_next_token (lexer, token);
775 if (next_token == lexer->last_token)
777 *token = *next_token;
781 lexer->last_token = token;
782 /* The token purged may have been the only token remaining; if so,
784 if (lexer->next_token == token)
785 lexer->next_token = NULL;
788 /* Permanently remove all tokens after TOKEN, up to, but not
789 including, the token that will be returned next by
790 cp_lexer_peek_token. */
793 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *token)
799 if (lexer->next_token)
801 /* Copy the tokens that have not yet been read to the location
802 immediately following TOKEN. */
803 t1 = cp_lexer_next_token (lexer, token);
804 t2 = peek = cp_lexer_peek_token (lexer);
805 /* Move tokens into the vacant area between TOKEN and PEEK. */
806 while (t2 != lexer->last_token)
809 t1 = cp_lexer_next_token (lexer, t1);
810 t2 = cp_lexer_next_token (lexer, t2);
812 /* Now, the next available token is right after TOKEN. */
813 lexer->next_token = cp_lexer_next_token (lexer, token);
814 /* And the last token is wherever we ended up. */
815 lexer->last_token = t1;
819 /* There are no tokens in the buffer, so there is nothing to
820 copy. The last token in the buffer is TOKEN itself. */
821 lexer->last_token = cp_lexer_next_token (lexer, token);
825 /* Begin saving tokens. All tokens consumed after this point will be
829 cp_lexer_save_tokens (cp_lexer* lexer)
831 /* Provide debugging output. */
832 if (cp_lexer_debugging_p (lexer))
833 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
835 /* Make sure that LEXER->NEXT_TOKEN is non-NULL so that we can
836 restore the tokens if required. */
837 if (!lexer->next_token)
838 cp_lexer_read_token (lexer);
840 VARRAY_PUSH_INT (lexer->saved_tokens,
841 cp_lexer_token_difference (lexer,
846 /* Commit to the portion of the token stream most recently saved. */
849 cp_lexer_commit_tokens (cp_lexer* lexer)
851 /* Provide debugging output. */
852 if (cp_lexer_debugging_p (lexer))
853 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
855 VARRAY_POP (lexer->saved_tokens);
858 /* Return all tokens saved since the last call to cp_lexer_save_tokens
859 to the token stream. Stop saving tokens. */
862 cp_lexer_rollback_tokens (cp_lexer* lexer)
866 /* Provide debugging output. */
867 if (cp_lexer_debugging_p (lexer))
868 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
870 /* Find the token that was the NEXT_TOKEN when we started saving
872 delta = VARRAY_TOP_INT(lexer->saved_tokens);
873 /* Make it the next token again now. */
874 lexer->next_token = cp_lexer_advance_token (lexer,
877 /* It might be the case that there were no tokens when we started
878 saving tokens, but that there are some tokens now. */
879 if (!lexer->next_token && lexer->first_token)
880 lexer->next_token = lexer->first_token;
882 /* Stop saving tokens. */
883 VARRAY_POP (lexer->saved_tokens);
886 /* Print a representation of the TOKEN on the STREAM. */
889 cp_lexer_print_token (FILE * stream, cp_token* token)
891 const char *token_type = NULL;
893 /* Figure out what kind of token this is. */
901 token_type = "COMMA";
905 token_type = "OPEN_PAREN";
908 case CPP_CLOSE_PAREN:
909 token_type = "CLOSE_PAREN";
913 token_type = "OPEN_BRACE";
916 case CPP_CLOSE_BRACE:
917 token_type = "CLOSE_BRACE";
921 token_type = "SEMICOLON";
933 token_type = "keyword";
936 /* This is not a token that we know how to handle yet. */
941 /* If we have a name for the token, print it out. Otherwise, we
942 simply give the numeric code. */
944 fprintf (stream, "%s", token_type);
946 fprintf (stream, "%d", token->type);
947 /* And, for an identifier, print the identifier name. */
948 if (token->type == CPP_NAME
949 /* Some keywords have a value that is not an IDENTIFIER_NODE.
950 For example, `struct' is mapped to an INTEGER_CST. */
951 || (token->type == CPP_KEYWORD
952 && TREE_CODE (token->value) == IDENTIFIER_NODE))
953 fprintf (stream, " %s", IDENTIFIER_POINTER (token->value));
956 /* Start emitting debugging information. */
959 cp_lexer_start_debugging (cp_lexer* lexer)
961 ++lexer->debugging_p;
964 /* Stop emitting debugging information. */
967 cp_lexer_stop_debugging (cp_lexer* lexer)
969 --lexer->debugging_p;
978 A cp_parser parses the token stream as specified by the C++
979 grammar. Its job is purely parsing, not semantic analysis. For
980 example, the parser breaks the token stream into declarators,
981 expressions, statements, and other similar syntactic constructs.
982 It does not check that the types of the expressions on either side
983 of an assignment-statement are compatible, or that a function is
984 not declared with a parameter of type `void'.
986 The parser invokes routines elsewhere in the compiler to perform
987 semantic analysis and to build up the abstract syntax tree for the
990 The parser (and the template instantiation code, which is, in a
991 way, a close relative of parsing) are the only parts of the
992 compiler that should be calling push_scope and pop_scope, or
993 related functions. The parser (and template instantiation code)
994 keeps track of what scope is presently active; everything else
995 should simply honor that. (The code that generates static
996 initializers may also need to set the scope, in order to check
997 access control correctly when emitting the initializers.)
1002 The parser is of the standard recursive-descent variety. Upcoming
1003 tokens in the token stream are examined in order to determine which
1004 production to use when parsing a non-terminal. Some C++ constructs
1005 require arbitrary look ahead to disambiguate. For example, it is
1006 impossible, in the general case, to tell whether a statement is an
1007 expression or declaration without scanning the entire statement.
1008 Therefore, the parser is capable of "parsing tentatively." When the
1009 parser is not sure what construct comes next, it enters this mode.
1010 Then, while we attempt to parse the construct, the parser queues up
1011 error messages, rather than issuing them immediately, and saves the
1012 tokens it consumes. If the construct is parsed successfully, the
1013 parser "commits", i.e., it issues any queued error messages and
1014 the tokens that were being preserved are permanently discarded.
1015 If, however, the construct is not parsed successfully, the parser
1016 rolls back its state completely so that it can resume parsing using
1017 a different alternative.
1022 The performance of the parser could probably be improved
1023 substantially. Some possible improvements include:
1025 - The expression parser recurses through the various levels of
1026 precedence as specified in the grammar, rather than using an
1027 operator-precedence technique. Therefore, parsing a simple
1028 identifier requires multiple recursive calls.
1030 - We could often eliminate the need to parse tentatively by
1031 looking ahead a little bit. In some places, this approach
1032 might not entirely eliminate the need to parse tentatively, but
1033 it might still speed up the average case. */
1035 /* Flags that are passed to some parsing functions. These values can
1036 be bitwise-ored together. */
1038 typedef enum cp_parser_flags
1041 CP_PARSER_FLAGS_NONE = 0x0,
1042 /* The construct is optional. If it is not present, then no error
1043 should be issued. */
1044 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1045 /* When parsing a type-specifier, do not allow user-defined types. */
1046 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2
1049 /* The different kinds of declarators we want to parse. */
1051 typedef enum cp_parser_declarator_kind
1053 /* We want an abstract declartor. */
1054 CP_PARSER_DECLARATOR_ABSTRACT,
1055 /* We want a named declarator. */
1056 CP_PARSER_DECLARATOR_NAMED,
1057 /* We don't mind, but the name must be an unqualified-id */
1058 CP_PARSER_DECLARATOR_EITHER
1059 } cp_parser_declarator_kind;
1061 /* A mapping from a token type to a corresponding tree node type. */
1063 typedef struct cp_parser_token_tree_map_node
1065 /* The token type. */
1066 enum cpp_ttype token_type : 8;
1067 /* The corresponding tree code. */
1068 enum tree_code tree_type : 8;
1069 } cp_parser_token_tree_map_node;
1071 /* A complete map consists of several ordinary entries, followed by a
1072 terminator. The terminating entry has a token_type of CPP_EOF. */
1074 typedef cp_parser_token_tree_map_node cp_parser_token_tree_map[];
1076 /* The status of a tentative parse. */
1078 typedef enum cp_parser_status_kind
1080 /* No errors have occurred. */
1081 CP_PARSER_STATUS_KIND_NO_ERROR,
1082 /* An error has occurred. */
1083 CP_PARSER_STATUS_KIND_ERROR,
1084 /* We are committed to this tentative parse, whether or not an error
1086 CP_PARSER_STATUS_KIND_COMMITTED
1087 } cp_parser_status_kind;
1089 /* Context that is saved and restored when parsing tentatively. */
1091 typedef struct cp_parser_context GTY (())
1093 /* If this is a tentative parsing context, the status of the
1095 enum cp_parser_status_kind status;
1096 /* If non-NULL, we have just seen a `x->' or `x.' expression. Names
1097 that are looked up in this context must be looked up both in the
1098 scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1099 the context of the containing expression. */
1101 /* The next parsing context in the stack. */
1102 struct cp_parser_context *next;
1103 } cp_parser_context;
1107 /* Constructors and destructors. */
1109 static cp_parser_context *cp_parser_context_new
1110 (cp_parser_context *);
1112 /* Class variables. */
1114 static GTY((deletable (""))) cp_parser_context* cp_parser_context_free_list;
1116 /* Constructors and destructors. */
1118 /* Construct a new context. The context below this one on the stack
1119 is given by NEXT. */
1121 static cp_parser_context *
1122 cp_parser_context_new (cp_parser_context* next)
1124 cp_parser_context *context;
1126 /* Allocate the storage. */
1127 if (cp_parser_context_free_list != NULL)
1129 /* Pull the first entry from the free list. */
1130 context = cp_parser_context_free_list;
1131 cp_parser_context_free_list = context->next;
1132 memset (context, 0, sizeof (*context));
1135 context = ggc_alloc_cleared (sizeof (cp_parser_context));
1136 /* No errors have occurred yet in this context. */
1137 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1138 /* If this is not the bottomost context, copy information that we
1139 need from the previous context. */
1142 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1143 expression, then we are parsing one in this context, too. */
1144 context->object_type = next->object_type;
1145 /* Thread the stack. */
1146 context->next = next;
1152 /* The cp_parser structure represents the C++ parser. */
1154 typedef struct cp_parser GTY(())
1156 /* The lexer from which we are obtaining tokens. */
1159 /* The scope in which names should be looked up. If NULL_TREE, then
1160 we look up names in the scope that is currently open in the
1161 source program. If non-NULL, this is either a TYPE or
1162 NAMESPACE_DECL for the scope in which we should look.
1164 This value is not cleared automatically after a name is looked
1165 up, so we must be careful to clear it before starting a new look
1166 up sequence. (If it is not cleared, then `X::Y' followed by `Z'
1167 will look up `Z' in the scope of `X', rather than the current
1168 scope.) Unfortunately, it is difficult to tell when name lookup
1169 is complete, because we sometimes peek at a token, look it up,
1170 and then decide not to consume it. */
1173 /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1174 last lookup took place. OBJECT_SCOPE is used if an expression
1175 like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1176 respectively. QUALIFYING_SCOPE is used for an expression of the
1177 form "X::Y"; it refers to X. */
1179 tree qualifying_scope;
1181 /* A stack of parsing contexts. All but the bottom entry on the
1182 stack will be tentative contexts.
1184 We parse tentatively in order to determine which construct is in
1185 use in some situations. For example, in order to determine
1186 whether a statement is an expression-statement or a
1187 declaration-statement we parse it tentatively as a
1188 declaration-statement. If that fails, we then reparse the same
1189 token stream as an expression-statement. */
1190 cp_parser_context *context;
1192 /* True if we are parsing GNU C++. If this flag is not set, then
1193 GNU extensions are not recognized. */
1194 bool allow_gnu_extensions_p;
1196 /* TRUE if the `>' token should be interpreted as the greater-than
1197 operator. FALSE if it is the end of a template-id or
1198 template-parameter-list. */
1199 bool greater_than_is_operator_p;
1201 /* TRUE if default arguments are allowed within a parameter list
1202 that starts at this point. FALSE if only a gnu extension makes
1203 them permissable. */
1204 bool default_arg_ok_p;
1206 /* TRUE if we are parsing an integral constant-expression. See
1207 [expr.const] for a precise definition. */
1208 bool constant_expression_p;
1210 /* TRUE if we are parsing an integral constant-expression -- but a
1211 non-constant expression should be permitted as well. This flag
1212 is used when parsing an array bound so that GNU variable-length
1213 arrays are tolerated. */
1214 bool allow_non_constant_expression_p;
1216 /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1217 been seen that makes the expression non-constant. */
1218 bool non_constant_expression_p;
1220 /* TRUE if local variable names and `this' are forbidden in the
1222 bool local_variables_forbidden_p;
1224 /* TRUE if the declaration we are parsing is part of a
1225 linkage-specification of the form `extern string-literal
1227 bool in_unbraced_linkage_specification_p;
1229 /* TRUE if we are presently parsing a declarator, after the
1230 direct-declarator. */
1231 bool in_declarator_p;
1233 /* If non-NULL, then we are parsing a construct where new type
1234 definitions are not permitted. The string stored here will be
1235 issued as an error message if a type is defined. */
1236 const char *type_definition_forbidden_message;
1238 /* A list of lists. The outer list is a stack, used for member
1239 functions of local classes. At each level there are two sub-list,
1240 one on TREE_VALUE and one on TREE_PURPOSE. Each of those
1241 sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their
1242 TREE_VALUE's. The functions are chained in reverse declaration
1245 The TREE_PURPOSE sublist contains those functions with default
1246 arguments that need post processing, and the TREE_VALUE sublist
1247 contains those functions with definitions that need post
1250 These lists can only be processed once the outermost class being
1251 defined is complete. */
1252 tree unparsed_functions_queues;
1254 /* The number of classes whose definitions are currently in
1256 unsigned num_classes_being_defined;
1258 /* The number of template parameter lists that apply directly to the
1259 current declaration. */
1260 unsigned num_template_parameter_lists;
1263 /* The type of a function that parses some kind of expression */
1264 typedef tree (*cp_parser_expression_fn) (cp_parser *);
1268 /* Constructors and destructors. */
1270 static cp_parser *cp_parser_new
1273 /* Routines to parse various constructs.
1275 Those that return `tree' will return the error_mark_node (rather
1276 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1277 Sometimes, they will return an ordinary node if error-recovery was
1278 attempted, even though a parse error occurred. So, to check
1279 whether or not a parse error occurred, you should always use
1280 cp_parser_error_occurred. If the construct is optional (indicated
1281 either by an `_opt' in the name of the function that does the
1282 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1283 the construct is not present. */
1285 /* Lexical conventions [gram.lex] */
1287 static tree cp_parser_identifier
1290 /* Basic concepts [gram.basic] */
1292 static bool cp_parser_translation_unit
1295 /* Expressions [gram.expr] */
1297 static tree cp_parser_primary_expression
1298 (cp_parser *, cp_id_kind *, tree *);
1299 static tree cp_parser_id_expression
1300 (cp_parser *, bool, bool, bool *, bool);
1301 static tree cp_parser_unqualified_id
1302 (cp_parser *, bool, bool, bool);
1303 static tree cp_parser_nested_name_specifier_opt
1304 (cp_parser *, bool, bool, bool);
1305 static tree cp_parser_nested_name_specifier
1306 (cp_parser *, bool, bool, bool);
1307 static tree cp_parser_class_or_namespace_name
1308 (cp_parser *, bool, bool, bool, bool);
1309 static tree cp_parser_postfix_expression
1310 (cp_parser *, bool);
1311 static tree cp_parser_parenthesized_expression_list
1312 (cp_parser *, bool, bool *);
1313 static void cp_parser_pseudo_destructor_name
1314 (cp_parser *, tree *, tree *);
1315 static tree cp_parser_unary_expression
1316 (cp_parser *, bool);
1317 static enum tree_code cp_parser_unary_operator
1319 static tree cp_parser_new_expression
1321 static tree cp_parser_new_placement
1323 static tree cp_parser_new_type_id
1325 static tree cp_parser_new_declarator_opt
1327 static tree cp_parser_direct_new_declarator
1329 static tree cp_parser_new_initializer
1331 static tree cp_parser_delete_expression
1333 static tree cp_parser_cast_expression
1334 (cp_parser *, bool);
1335 static tree cp_parser_pm_expression
1337 static tree cp_parser_multiplicative_expression
1339 static tree cp_parser_additive_expression
1341 static tree cp_parser_shift_expression
1343 static tree cp_parser_relational_expression
1345 static tree cp_parser_equality_expression
1347 static tree cp_parser_and_expression
1349 static tree cp_parser_exclusive_or_expression
1351 static tree cp_parser_inclusive_or_expression
1353 static tree cp_parser_logical_and_expression
1355 static tree cp_parser_logical_or_expression
1357 static tree cp_parser_question_colon_clause
1358 (cp_parser *, tree);
1359 static tree cp_parser_assignment_expression
1361 static enum tree_code cp_parser_assignment_operator_opt
1363 static tree cp_parser_expression
1365 static tree cp_parser_constant_expression
1366 (cp_parser *, bool, bool *);
1368 /* Statements [gram.stmt.stmt] */
1370 static void cp_parser_statement
1371 (cp_parser *, bool);
1372 static tree cp_parser_labeled_statement
1373 (cp_parser *, bool);
1374 static tree cp_parser_expression_statement
1375 (cp_parser *, bool);
1376 static tree cp_parser_compound_statement
1377 (cp_parser *, bool);
1378 static void cp_parser_statement_seq_opt
1379 (cp_parser *, bool);
1380 static tree cp_parser_selection_statement
1382 static tree cp_parser_condition
1384 static tree cp_parser_iteration_statement
1386 static void cp_parser_for_init_statement
1388 static tree cp_parser_jump_statement
1390 static void cp_parser_declaration_statement
1393 static tree cp_parser_implicitly_scoped_statement
1395 static void cp_parser_already_scoped_statement
1398 /* Declarations [gram.dcl.dcl] */
1400 static void cp_parser_declaration_seq_opt
1402 static void cp_parser_declaration
1404 static void cp_parser_block_declaration
1405 (cp_parser *, bool);
1406 static void cp_parser_simple_declaration
1407 (cp_parser *, bool);
1408 static tree cp_parser_decl_specifier_seq
1409 (cp_parser *, cp_parser_flags, tree *, int *);
1410 static tree cp_parser_storage_class_specifier_opt
1412 static tree cp_parser_function_specifier_opt
1414 static tree cp_parser_type_specifier
1415 (cp_parser *, cp_parser_flags, bool, bool, int *, bool *);
1416 static tree cp_parser_simple_type_specifier
1417 (cp_parser *, cp_parser_flags, bool);
1418 static tree cp_parser_type_name
1420 static tree cp_parser_elaborated_type_specifier
1421 (cp_parser *, bool, bool);
1422 static tree cp_parser_enum_specifier
1424 static void cp_parser_enumerator_list
1425 (cp_parser *, tree);
1426 static void cp_parser_enumerator_definition
1427 (cp_parser *, tree);
1428 static tree cp_parser_namespace_name
1430 static void cp_parser_namespace_definition
1432 static void cp_parser_namespace_body
1434 static tree cp_parser_qualified_namespace_specifier
1436 static void cp_parser_namespace_alias_definition
1438 static void cp_parser_using_declaration
1440 static void cp_parser_using_directive
1442 static void cp_parser_asm_definition
1444 static void cp_parser_linkage_specification
1447 /* Declarators [gram.dcl.decl] */
1449 static tree cp_parser_init_declarator
1450 (cp_parser *, tree, tree, bool, bool, int, bool *);
1451 static tree cp_parser_declarator
1452 (cp_parser *, cp_parser_declarator_kind, int *);
1453 static tree cp_parser_direct_declarator
1454 (cp_parser *, cp_parser_declarator_kind, int *);
1455 static enum tree_code cp_parser_ptr_operator
1456 (cp_parser *, tree *, tree *);
1457 static tree cp_parser_cv_qualifier_seq_opt
1459 static tree cp_parser_cv_qualifier_opt
1461 static tree cp_parser_declarator_id
1463 static tree cp_parser_type_id
1465 static tree cp_parser_type_specifier_seq
1467 static tree cp_parser_parameter_declaration_clause
1469 static tree cp_parser_parameter_declaration_list
1471 static tree cp_parser_parameter_declaration
1472 (cp_parser *, bool);
1473 static tree cp_parser_function_definition
1474 (cp_parser *, bool *);
1475 static void cp_parser_function_body
1477 static tree cp_parser_initializer
1478 (cp_parser *, bool *, bool *);
1479 static tree cp_parser_initializer_clause
1480 (cp_parser *, bool *);
1481 static tree cp_parser_initializer_list
1482 (cp_parser *, bool *);
1484 static bool cp_parser_ctor_initializer_opt_and_function_body
1487 /* Classes [gram.class] */
1489 static tree cp_parser_class_name
1490 (cp_parser *, bool, bool, bool, bool, bool);
1491 static tree cp_parser_class_specifier
1493 static tree cp_parser_class_head
1494 (cp_parser *, bool *);
1495 static enum tag_types cp_parser_class_key
1497 static void cp_parser_member_specification_opt
1499 static void cp_parser_member_declaration
1501 static tree cp_parser_pure_specifier
1503 static tree cp_parser_constant_initializer
1506 /* Derived classes [gram.class.derived] */
1508 static tree cp_parser_base_clause
1510 static tree cp_parser_base_specifier
1513 /* Special member functions [gram.special] */
1515 static tree cp_parser_conversion_function_id
1517 static tree cp_parser_conversion_type_id
1519 static tree cp_parser_conversion_declarator_opt
1521 static bool cp_parser_ctor_initializer_opt
1523 static void cp_parser_mem_initializer_list
1525 static tree cp_parser_mem_initializer
1527 static tree cp_parser_mem_initializer_id
1530 /* Overloading [gram.over] */
1532 static tree cp_parser_operator_function_id
1534 static tree cp_parser_operator
1537 /* Templates [gram.temp] */
1539 static void cp_parser_template_declaration
1540 (cp_parser *, bool);
1541 static tree cp_parser_template_parameter_list
1543 static tree cp_parser_template_parameter
1545 static tree cp_parser_type_parameter
1547 static tree cp_parser_template_id
1548 (cp_parser *, bool, bool);
1549 static tree cp_parser_template_name
1550 (cp_parser *, bool, bool);
1551 static tree cp_parser_template_argument_list
1553 static tree cp_parser_template_argument
1555 static void cp_parser_explicit_instantiation
1557 static void cp_parser_explicit_specialization
1560 /* Exception handling [gram.exception] */
1562 static tree cp_parser_try_block
1564 static bool cp_parser_function_try_block
1566 static void cp_parser_handler_seq
1568 static void cp_parser_handler
1570 static tree cp_parser_exception_declaration
1572 static tree cp_parser_throw_expression
1574 static tree cp_parser_exception_specification_opt
1576 static tree cp_parser_type_id_list
1579 /* GNU Extensions */
1581 static tree cp_parser_asm_specification_opt
1583 static tree cp_parser_asm_operand_list
1585 static tree cp_parser_asm_clobber_list
1587 static tree cp_parser_attributes_opt
1589 static tree cp_parser_attribute_list
1591 static bool cp_parser_extension_opt
1592 (cp_parser *, int *);
1593 static void cp_parser_label_declaration
1596 /* Utility Routines */
1598 static tree cp_parser_lookup_name
1599 (cp_parser *, tree, bool, bool, bool);
1600 static tree cp_parser_lookup_name_simple
1601 (cp_parser *, tree);
1602 static tree cp_parser_maybe_treat_template_as_class
1604 static bool cp_parser_check_declarator_template_parameters
1605 (cp_parser *, tree);
1606 static bool cp_parser_check_template_parameters
1607 (cp_parser *, unsigned);
1608 static tree cp_parser_simple_cast_expression
1610 static tree cp_parser_binary_expression
1611 (cp_parser *, const cp_parser_token_tree_map, cp_parser_expression_fn);
1612 static tree cp_parser_global_scope_opt
1613 (cp_parser *, bool);
1614 static bool cp_parser_constructor_declarator_p
1615 (cp_parser *, bool);
1616 static tree cp_parser_function_definition_from_specifiers_and_declarator
1617 (cp_parser *, tree, tree, tree);
1618 static tree cp_parser_function_definition_after_declarator
1619 (cp_parser *, bool);
1620 static void cp_parser_template_declaration_after_export
1621 (cp_parser *, bool);
1622 static tree cp_parser_single_declaration
1623 (cp_parser *, bool, bool *);
1624 static tree cp_parser_functional_cast
1625 (cp_parser *, tree);
1626 static void cp_parser_save_default_args
1627 (cp_parser *, tree);
1628 static void cp_parser_late_parsing_for_member
1629 (cp_parser *, tree);
1630 static void cp_parser_late_parsing_default_args
1631 (cp_parser *, tree);
1632 static tree cp_parser_sizeof_operand
1633 (cp_parser *, enum rid);
1634 static bool cp_parser_declares_only_class_p
1636 static tree cp_parser_fold_non_dependent_expr
1638 static bool cp_parser_friend_p
1640 static cp_token *cp_parser_require
1641 (cp_parser *, enum cpp_ttype, const char *);
1642 static cp_token *cp_parser_require_keyword
1643 (cp_parser *, enum rid, const char *);
1644 static bool cp_parser_token_starts_function_definition_p
1646 static bool cp_parser_next_token_starts_class_definition_p
1648 static bool cp_parser_next_token_ends_template_argument_p
1650 static enum tag_types cp_parser_token_is_class_key
1652 static void cp_parser_check_class_key
1653 (enum tag_types, tree type);
1654 static bool cp_parser_optional_template_keyword
1656 static void cp_parser_pre_parsed_nested_name_specifier
1658 static void cp_parser_cache_group
1659 (cp_parser *, cp_token_cache *, enum cpp_ttype, unsigned);
1660 static void cp_parser_parse_tentatively
1662 static void cp_parser_commit_to_tentative_parse
1664 static void cp_parser_abort_tentative_parse
1666 static bool cp_parser_parse_definitely
1668 static inline bool cp_parser_parsing_tentatively
1670 static bool cp_parser_committed_to_tentative_parse
1672 static void cp_parser_error
1673 (cp_parser *, const char *);
1674 static bool cp_parser_simulate_error
1676 static void cp_parser_check_type_definition
1678 static void cp_parser_check_for_definition_in_return_type
1680 static tree cp_parser_non_constant_expression
1682 static bool cp_parser_diagnose_invalid_type_name
1684 static int cp_parser_skip_to_closing_parenthesis
1685 (cp_parser *, bool, bool);
1686 static void cp_parser_skip_to_end_of_statement
1688 static void cp_parser_consume_semicolon_at_end_of_statement
1690 static void cp_parser_skip_to_end_of_block_or_statement
1692 static void cp_parser_skip_to_closing_brace
1694 static void cp_parser_skip_until_found
1695 (cp_parser *, enum cpp_ttype, const char *);
1696 static bool cp_parser_error_occurred
1698 static bool cp_parser_allow_gnu_extensions_p
1700 static bool cp_parser_is_string_literal
1702 static bool cp_parser_is_keyword
1703 (cp_token *, enum rid);
1705 /* Returns nonzero if we are parsing tentatively. */
1708 cp_parser_parsing_tentatively (cp_parser* parser)
1710 return parser->context->next != NULL;
1713 /* Returns nonzero if TOKEN is a string literal. */
1716 cp_parser_is_string_literal (cp_token* token)
1718 return (token->type == CPP_STRING || token->type == CPP_WSTRING);
1721 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
1724 cp_parser_is_keyword (cp_token* token, enum rid keyword)
1726 return token->keyword == keyword;
1729 /* Issue the indicated error MESSAGE. */
1732 cp_parser_error (cp_parser* parser, const char* message)
1734 /* Output the MESSAGE -- unless we're parsing tentatively. */
1735 if (!cp_parser_simulate_error (parser))
1739 /* If we are parsing tentatively, remember that an error has occurred
1740 during this tentative parse. Returns true if the error was
1741 simulated; false if a messgae should be issued by the caller. */
1744 cp_parser_simulate_error (cp_parser* parser)
1746 if (cp_parser_parsing_tentatively (parser)
1747 && !cp_parser_committed_to_tentative_parse (parser))
1749 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
1755 /* This function is called when a type is defined. If type
1756 definitions are forbidden at this point, an error message is
1760 cp_parser_check_type_definition (cp_parser* parser)
1762 /* If types are forbidden here, issue a message. */
1763 if (parser->type_definition_forbidden_message)
1764 /* Use `%s' to print the string in case there are any escape
1765 characters in the message. */
1766 error ("%s", parser->type_definition_forbidden_message);
1769 /* This function is called when a declaration is parsed. If
1770 DECLARATOR is a function declarator and DECLARES_CLASS_OR_ENUM
1771 indicates that a type was defined in the decl-specifiers for DECL,
1772 then an error is issued. */
1775 cp_parser_check_for_definition_in_return_type (tree declarator,
1776 int declares_class_or_enum)
1778 /* [dcl.fct] forbids type definitions in return types.
1779 Unfortunately, it's not easy to know whether or not we are
1780 processing a return type until after the fact. */
1782 && (TREE_CODE (declarator) == INDIRECT_REF
1783 || TREE_CODE (declarator) == ADDR_EXPR))
1784 declarator = TREE_OPERAND (declarator, 0);
1786 && TREE_CODE (declarator) == CALL_EXPR
1787 && declares_class_or_enum & 2)
1788 error ("new types may not be defined in a return type");
1791 /* Issue an eror message about the fact that THING appeared in a
1792 constant-expression. Returns ERROR_MARK_NODE. */
1795 cp_parser_non_constant_expression (const char *thing)
1797 error ("%s cannot appear in a constant-expression", thing);
1798 return error_mark_node;
1801 /* Check for a common situation where a type-name should be present,
1802 but is not, and issue a sensible error message. Returns true if an
1803 invalid type-name was detected. */
1806 cp_parser_diagnose_invalid_type_name (cp_parser *parser)
1808 /* If the next two tokens are both identifiers, the code is
1809 erroneous. The usual cause of this situation is code like:
1813 where "T" should name a type -- but does not. */
1814 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
1815 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
1819 /* If parsing tentatively, we should commit; we really are
1820 looking at a declaration. */
1821 /* Consume the first identifier. */
1822 name = cp_lexer_consume_token (parser->lexer)->value;
1823 /* Issue an error message. */
1824 error ("`%s' does not name a type", IDENTIFIER_POINTER (name));
1825 /* If we're in a template class, it's possible that the user was
1826 referring to a type from a base class. For example:
1828 template <typename T> struct A { typedef T X; };
1829 template <typename T> struct B : public A<T> { X x; };
1831 The user should have said "typename A<T>::X". */
1832 if (processing_template_decl && current_class_type)
1836 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
1840 tree base_type = BINFO_TYPE (b);
1841 if (CLASS_TYPE_P (base_type)
1842 && dependent_type_p (base_type))
1845 /* Go from a particular instantiation of the
1846 template (which will have an empty TYPE_FIELDs),
1847 to the main version. */
1848 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
1849 for (field = TYPE_FIELDS (base_type);
1851 field = TREE_CHAIN (field))
1852 if (TREE_CODE (field) == TYPE_DECL
1853 && DECL_NAME (field) == name)
1855 error ("(perhaps `typename %T::%s' was intended)",
1856 BINFO_TYPE (b), IDENTIFIER_POINTER (name));
1864 /* Skip to the end of the declaration; there's no point in
1865 trying to process it. */
1866 cp_parser_skip_to_end_of_statement (parser);
1874 /* Consume tokens up to, and including, the next non-nested closing `)'.
1875 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
1876 are doing error recovery. Returns -1 if OR_COMMA is true and we
1877 found an unnested comma. */
1880 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
1881 bool recovering, bool or_comma)
1883 unsigned paren_depth = 0;
1884 unsigned brace_depth = 0;
1886 if (recovering && !or_comma && cp_parser_parsing_tentatively (parser)
1887 && !cp_parser_committed_to_tentative_parse (parser))
1894 /* If we've run out of tokens, then there is no closing `)'. */
1895 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
1900 token = cp_lexer_peek_token (parser->lexer);
1902 /* This matches the processing in skip_to_end_of_statement */
1903 if (token->type == CPP_SEMICOLON && !brace_depth)
1905 if (token->type == CPP_OPEN_BRACE)
1907 if (token->type == CPP_CLOSE_BRACE)
1912 if (or_comma && token->type == CPP_COMMA
1913 && !brace_depth && !paren_depth)
1917 /* Consume the token. */
1918 token = cp_lexer_consume_token (parser->lexer);
1922 /* If it is an `(', we have entered another level of nesting. */
1923 if (token->type == CPP_OPEN_PAREN)
1925 /* If it is a `)', then we might be done. */
1926 else if (token->type == CPP_CLOSE_PAREN && !paren_depth--)
1932 /* Consume tokens until we reach the end of the current statement.
1933 Normally, that will be just before consuming a `;'. However, if a
1934 non-nested `}' comes first, then we stop before consuming that. */
1937 cp_parser_skip_to_end_of_statement (cp_parser* parser)
1939 unsigned nesting_depth = 0;
1945 /* Peek at the next token. */
1946 token = cp_lexer_peek_token (parser->lexer);
1947 /* If we've run out of tokens, stop. */
1948 if (token->type == CPP_EOF)
1950 /* If the next token is a `;', we have reached the end of the
1952 if (token->type == CPP_SEMICOLON && !nesting_depth)
1954 /* If the next token is a non-nested `}', then we have reached
1955 the end of the current block. */
1956 if (token->type == CPP_CLOSE_BRACE)
1958 /* If this is a non-nested `}', stop before consuming it.
1959 That way, when confronted with something like:
1963 we stop before consuming the closing `}', even though we
1964 have not yet reached a `;'. */
1965 if (nesting_depth == 0)
1967 /* If it is the closing `}' for a block that we have
1968 scanned, stop -- but only after consuming the token.
1974 we will stop after the body of the erroneously declared
1975 function, but before consuming the following `typedef'
1977 if (--nesting_depth == 0)
1979 cp_lexer_consume_token (parser->lexer);
1983 /* If it the next token is a `{', then we are entering a new
1984 block. Consume the entire block. */
1985 else if (token->type == CPP_OPEN_BRACE)
1987 /* Consume the token. */
1988 cp_lexer_consume_token (parser->lexer);
1992 /* This function is called at the end of a statement or declaration.
1993 If the next token is a semicolon, it is consumed; otherwise, error
1994 recovery is attempted. */
1997 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
1999 /* Look for the trailing `;'. */
2000 if (!cp_parser_require (parser, CPP_SEMICOLON, "`;'"))
2002 /* If there is additional (erroneous) input, skip to the end of
2004 cp_parser_skip_to_end_of_statement (parser);
2005 /* If the next token is now a `;', consume it. */
2006 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2007 cp_lexer_consume_token (parser->lexer);
2011 /* Skip tokens until we have consumed an entire block, or until we
2012 have consumed a non-nested `;'. */
2015 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2017 unsigned nesting_depth = 0;
2023 /* Peek at the next token. */
2024 token = cp_lexer_peek_token (parser->lexer);
2025 /* If we've run out of tokens, stop. */
2026 if (token->type == CPP_EOF)
2028 /* If the next token is a `;', we have reached the end of the
2030 if (token->type == CPP_SEMICOLON && !nesting_depth)
2032 /* Consume the `;'. */
2033 cp_lexer_consume_token (parser->lexer);
2036 /* Consume the token. */
2037 token = cp_lexer_consume_token (parser->lexer);
2038 /* If the next token is a non-nested `}', then we have reached
2039 the end of the current block. */
2040 if (token->type == CPP_CLOSE_BRACE
2041 && (nesting_depth == 0 || --nesting_depth == 0))
2043 /* If it the next token is a `{', then we are entering a new
2044 block. Consume the entire block. */
2045 if (token->type == CPP_OPEN_BRACE)
2050 /* Skip tokens until a non-nested closing curly brace is the next
2054 cp_parser_skip_to_closing_brace (cp_parser *parser)
2056 unsigned nesting_depth = 0;
2062 /* Peek at the next token. */
2063 token = cp_lexer_peek_token (parser->lexer);
2064 /* If we've run out of tokens, stop. */
2065 if (token->type == CPP_EOF)
2067 /* If the next token is a non-nested `}', then we have reached
2068 the end of the current block. */
2069 if (token->type == CPP_CLOSE_BRACE && nesting_depth-- == 0)
2071 /* If it the next token is a `{', then we are entering a new
2072 block. Consume the entire block. */
2073 else if (token->type == CPP_OPEN_BRACE)
2075 /* Consume the token. */
2076 cp_lexer_consume_token (parser->lexer);
2080 /* Create a new C++ parser. */
2083 cp_parser_new (void)
2088 /* cp_lexer_new_main is called before calling ggc_alloc because
2089 cp_lexer_new_main might load a PCH file. */
2090 lexer = cp_lexer_new_main ();
2092 parser = ggc_alloc_cleared (sizeof (cp_parser));
2093 parser->lexer = lexer;
2094 parser->context = cp_parser_context_new (NULL);
2096 /* For now, we always accept GNU extensions. */
2097 parser->allow_gnu_extensions_p = 1;
2099 /* The `>' token is a greater-than operator, not the end of a
2101 parser->greater_than_is_operator_p = true;
2103 parser->default_arg_ok_p = true;
2105 /* We are not parsing a constant-expression. */
2106 parser->constant_expression_p = false;
2107 parser->allow_non_constant_expression_p = false;
2108 parser->non_constant_expression_p = false;
2110 /* Local variable names are not forbidden. */
2111 parser->local_variables_forbidden_p = false;
2113 /* We are not processing an `extern "C"' declaration. */
2114 parser->in_unbraced_linkage_specification_p = false;
2116 /* We are not processing a declarator. */
2117 parser->in_declarator_p = false;
2119 /* The unparsed function queue is empty. */
2120 parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2122 /* There are no classes being defined. */
2123 parser->num_classes_being_defined = 0;
2125 /* No template parameters apply. */
2126 parser->num_template_parameter_lists = 0;
2131 /* Lexical conventions [gram.lex] */
2133 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
2137 cp_parser_identifier (cp_parser* parser)
2141 /* Look for the identifier. */
2142 token = cp_parser_require (parser, CPP_NAME, "identifier");
2143 /* Return the value. */
2144 return token ? token->value : error_mark_node;
2147 /* Basic concepts [gram.basic] */
2149 /* Parse a translation-unit.
2152 declaration-seq [opt]
2154 Returns TRUE if all went well. */
2157 cp_parser_translation_unit (cp_parser* parser)
2161 cp_parser_declaration_seq_opt (parser);
2163 /* If there are no tokens left then all went well. */
2164 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2167 /* Otherwise, issue an error message. */
2168 cp_parser_error (parser, "expected declaration");
2172 /* Consume the EOF token. */
2173 cp_parser_require (parser, CPP_EOF, "end-of-file");
2176 finish_translation_unit ();
2178 /* All went well. */
2182 /* Expressions [gram.expr] */
2184 /* Parse a primary-expression.
2195 ( compound-statement )
2196 __builtin_va_arg ( assignment-expression , type-id )
2201 Returns a representation of the expression.
2203 *IDK indicates what kind of id-expression (if any) was present.
2205 *QUALIFYING_CLASS is set to a non-NULL value if the id-expression can be
2206 used as the operand of a pointer-to-member. In that case,
2207 *QUALIFYING_CLASS gives the class that is used as the qualifying
2208 class in the pointer-to-member. */
2211 cp_parser_primary_expression (cp_parser *parser,
2213 tree *qualifying_class)
2217 /* Assume the primary expression is not an id-expression. */
2218 *idk = CP_ID_KIND_NONE;
2219 /* And that it cannot be used as pointer-to-member. */
2220 *qualifying_class = NULL_TREE;
2222 /* Peek at the next token. */
2223 token = cp_lexer_peek_token (parser->lexer);
2224 switch (token->type)
2237 token = cp_lexer_consume_token (parser->lexer);
2238 return token->value;
2240 case CPP_OPEN_PAREN:
2243 bool saved_greater_than_is_operator_p;
2245 /* Consume the `('. */
2246 cp_lexer_consume_token (parser->lexer);
2247 /* Within a parenthesized expression, a `>' token is always
2248 the greater-than operator. */
2249 saved_greater_than_is_operator_p
2250 = parser->greater_than_is_operator_p;
2251 parser->greater_than_is_operator_p = true;
2252 /* If we see `( { ' then we are looking at the beginning of
2253 a GNU statement-expression. */
2254 if (cp_parser_allow_gnu_extensions_p (parser)
2255 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
2257 /* Statement-expressions are not allowed by the standard. */
2259 pedwarn ("ISO C++ forbids braced-groups within expressions");
2261 /* And they're not allowed outside of a function-body; you
2262 cannot, for example, write:
2264 int i = ({ int j = 3; j + 1; });
2266 at class or namespace scope. */
2267 if (!at_function_scope_p ())
2268 error ("statement-expressions are allowed only inside functions");
2269 /* Start the statement-expression. */
2270 expr = begin_stmt_expr ();
2271 /* Parse the compound-statement. */
2272 cp_parser_compound_statement (parser, true);
2274 expr = finish_stmt_expr (expr, false);
2278 /* Parse the parenthesized expression. */
2279 expr = cp_parser_expression (parser);
2280 /* Let the front end know that this expression was
2281 enclosed in parentheses. This matters in case, for
2282 example, the expression is of the form `A::B', since
2283 `&A::B' might be a pointer-to-member, but `&(A::B)' is
2285 finish_parenthesized_expr (expr);
2287 /* The `>' token might be the end of a template-id or
2288 template-parameter-list now. */
2289 parser->greater_than_is_operator_p
2290 = saved_greater_than_is_operator_p;
2291 /* Consume the `)'. */
2292 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
2293 cp_parser_skip_to_end_of_statement (parser);
2299 switch (token->keyword)
2301 /* These two are the boolean literals. */
2303 cp_lexer_consume_token (parser->lexer);
2304 return boolean_true_node;
2306 cp_lexer_consume_token (parser->lexer);
2307 return boolean_false_node;
2309 /* The `__null' literal. */
2311 cp_lexer_consume_token (parser->lexer);
2314 /* Recognize the `this' keyword. */
2316 cp_lexer_consume_token (parser->lexer);
2317 if (parser->local_variables_forbidden_p)
2319 error ("`this' may not be used in this context");
2320 return error_mark_node;
2322 /* Pointers cannot appear in constant-expressions. */
2323 if (parser->constant_expression_p)
2325 if (!parser->allow_non_constant_expression_p)
2326 return cp_parser_non_constant_expression ("`this'");
2327 parser->non_constant_expression_p = true;
2329 return finish_this_expr ();
2331 /* The `operator' keyword can be the beginning of an
2336 case RID_FUNCTION_NAME:
2337 case RID_PRETTY_FUNCTION_NAME:
2338 case RID_C99_FUNCTION_NAME:
2339 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
2340 __func__ are the names of variables -- but they are
2341 treated specially. Therefore, they are handled here,
2342 rather than relying on the generic id-expression logic
2343 below. Grammatically, these names are id-expressions.
2345 Consume the token. */
2346 token = cp_lexer_consume_token (parser->lexer);
2347 /* Look up the name. */
2348 return finish_fname (token->value);
2355 /* The `__builtin_va_arg' construct is used to handle
2356 `va_arg'. Consume the `__builtin_va_arg' token. */
2357 cp_lexer_consume_token (parser->lexer);
2358 /* Look for the opening `('. */
2359 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
2360 /* Now, parse the assignment-expression. */
2361 expression = cp_parser_assignment_expression (parser);
2362 /* Look for the `,'. */
2363 cp_parser_require (parser, CPP_COMMA, "`,'");
2364 /* Parse the type-id. */
2365 type = cp_parser_type_id (parser);
2366 /* Look for the closing `)'. */
2367 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
2368 /* Using `va_arg' in a constant-expression is not
2370 if (parser->constant_expression_p)
2372 if (!parser->allow_non_constant_expression_p)
2373 return cp_parser_non_constant_expression ("`va_arg'");
2374 parser->non_constant_expression_p = true;
2376 return build_x_va_arg (expression, type);
2380 cp_parser_error (parser, "expected primary-expression");
2381 return error_mark_node;
2384 /* An id-expression can start with either an identifier, a
2385 `::' as the beginning of a qualified-id, or the "operator"
2389 case CPP_TEMPLATE_ID:
2390 case CPP_NESTED_NAME_SPECIFIER:
2394 const char *error_msg;
2397 /* Parse the id-expression. */
2399 = cp_parser_id_expression (parser,
2400 /*template_keyword_p=*/false,
2401 /*check_dependency_p=*/true,
2402 /*template_p=*/NULL,
2403 /*declarator_p=*/false);
2404 if (id_expression == error_mark_node)
2405 return error_mark_node;
2406 /* If we have a template-id, then no further lookup is
2407 required. If the template-id was for a template-class, we
2408 will sometimes have a TYPE_DECL at this point. */
2409 else if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
2410 || TREE_CODE (id_expression) == TYPE_DECL)
2411 decl = id_expression;
2412 /* Look up the name. */
2415 decl = cp_parser_lookup_name_simple (parser, id_expression);
2416 /* If name lookup gives us a SCOPE_REF, then the
2417 qualifying scope was dependent. Just propagate the
2419 if (TREE_CODE (decl) == SCOPE_REF)
2421 if (TYPE_P (TREE_OPERAND (decl, 0)))
2422 *qualifying_class = TREE_OPERAND (decl, 0);
2425 /* Check to see if DECL is a local variable in a context
2426 where that is forbidden. */
2427 if (parser->local_variables_forbidden_p
2428 && local_variable_p (decl))
2430 /* It might be that we only found DECL because we are
2431 trying to be generous with pre-ISO scoping rules.
2432 For example, consider:
2436 for (int i = 0; i < 10; ++i) {}
2437 extern void f(int j = i);
2440 Here, name look up will originally find the out
2441 of scope `i'. We need to issue a warning message,
2442 but then use the global `i'. */
2443 decl = check_for_out_of_scope_variable (decl);
2444 if (local_variable_p (decl))
2446 error ("local variable `%D' may not appear in this context",
2448 return error_mark_node;
2453 decl = finish_id_expression (id_expression, decl, parser->scope,
2454 idk, qualifying_class,
2455 parser->constant_expression_p,
2456 parser->allow_non_constant_expression_p,
2457 &parser->non_constant_expression_p,
2460 cp_parser_error (parser, error_msg);
2464 /* Anything else is an error. */
2466 cp_parser_error (parser, "expected primary-expression");
2467 return error_mark_node;
2471 /* Parse an id-expression.
2478 :: [opt] nested-name-specifier template [opt] unqualified-id
2480 :: operator-function-id
2483 Return a representation of the unqualified portion of the
2484 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
2485 a `::' or nested-name-specifier.
2487 Often, if the id-expression was a qualified-id, the caller will
2488 want to make a SCOPE_REF to represent the qualified-id. This
2489 function does not do this in order to avoid wastefully creating
2490 SCOPE_REFs when they are not required.
2492 If TEMPLATE_KEYWORD_P is true, then we have just seen the
2495 If CHECK_DEPENDENCY_P is false, then names are looked up inside
2496 uninstantiated templates.
2498 If *TEMPLATE_P is non-NULL, it is set to true iff the
2499 `template' keyword is used to explicitly indicate that the entity
2500 named is a template.
2502 If DECLARATOR_P is true, the id-expression is appearing as part of
2503 a declarator, rather than as part of an exprsesion. */
2506 cp_parser_id_expression (cp_parser *parser,
2507 bool template_keyword_p,
2508 bool check_dependency_p,
2512 bool global_scope_p;
2513 bool nested_name_specifier_p;
2515 /* Assume the `template' keyword was not used. */
2517 *template_p = false;
2519 /* Look for the optional `::' operator. */
2521 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
2523 /* Look for the optional nested-name-specifier. */
2524 nested_name_specifier_p
2525 = (cp_parser_nested_name_specifier_opt (parser,
2526 /*typename_keyword_p=*/false,
2530 /* If there is a nested-name-specifier, then we are looking at
2531 the first qualified-id production. */
2532 if (nested_name_specifier_p)
2535 tree saved_object_scope;
2536 tree saved_qualifying_scope;
2537 tree unqualified_id;
2540 /* See if the next token is the `template' keyword. */
2542 template_p = &is_template;
2543 *template_p = cp_parser_optional_template_keyword (parser);
2544 /* Name lookup we do during the processing of the
2545 unqualified-id might obliterate SCOPE. */
2546 saved_scope = parser->scope;
2547 saved_object_scope = parser->object_scope;
2548 saved_qualifying_scope = parser->qualifying_scope;
2549 /* Process the final unqualified-id. */
2550 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
2553 /* Restore the SAVED_SCOPE for our caller. */
2554 parser->scope = saved_scope;
2555 parser->object_scope = saved_object_scope;
2556 parser->qualifying_scope = saved_qualifying_scope;
2558 return unqualified_id;
2560 /* Otherwise, if we are in global scope, then we are looking at one
2561 of the other qualified-id productions. */
2562 else if (global_scope_p)
2567 /* Peek at the next token. */
2568 token = cp_lexer_peek_token (parser->lexer);
2570 /* If it's an identifier, and the next token is not a "<", then
2571 we can avoid the template-id case. This is an optimization
2572 for this common case. */
2573 if (token->type == CPP_NAME
2574 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_LESS)
2575 return cp_parser_identifier (parser);
2577 cp_parser_parse_tentatively (parser);
2578 /* Try a template-id. */
2579 id = cp_parser_template_id (parser,
2580 /*template_keyword_p=*/false,
2581 /*check_dependency_p=*/true);
2582 /* If that worked, we're done. */
2583 if (cp_parser_parse_definitely (parser))
2586 /* Peek at the next token. (Changes in the token buffer may
2587 have invalidated the pointer obtained above.) */
2588 token = cp_lexer_peek_token (parser->lexer);
2590 switch (token->type)
2593 return cp_parser_identifier (parser);
2596 if (token->keyword == RID_OPERATOR)
2597 return cp_parser_operator_function_id (parser);
2601 cp_parser_error (parser, "expected id-expression");
2602 return error_mark_node;
2606 return cp_parser_unqualified_id (parser, template_keyword_p,
2607 /*check_dependency_p=*/true,
2611 /* Parse an unqualified-id.
2615 operator-function-id
2616 conversion-function-id
2620 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
2621 keyword, in a construct like `A::template ...'.
2623 Returns a representation of unqualified-id. For the `identifier'
2624 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
2625 production a BIT_NOT_EXPR is returned; the operand of the
2626 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
2627 other productions, see the documentation accompanying the
2628 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
2629 names are looked up in uninstantiated templates. If DECLARATOR_P
2630 is true, the unqualified-id is appearing as part of a declarator,
2631 rather than as part of an expression. */
2634 cp_parser_unqualified_id (cp_parser* parser,
2635 bool template_keyword_p,
2636 bool check_dependency_p,
2641 /* Peek at the next token. */
2642 token = cp_lexer_peek_token (parser->lexer);
2644 switch (token->type)
2650 /* We don't know yet whether or not this will be a
2652 cp_parser_parse_tentatively (parser);
2653 /* Try a template-id. */
2654 id = cp_parser_template_id (parser, template_keyword_p,
2655 check_dependency_p);
2656 /* If it worked, we're done. */
2657 if (cp_parser_parse_definitely (parser))
2659 /* Otherwise, it's an ordinary identifier. */
2660 return cp_parser_identifier (parser);
2663 case CPP_TEMPLATE_ID:
2664 return cp_parser_template_id (parser, template_keyword_p,
2665 check_dependency_p);
2670 tree qualifying_scope;
2674 /* Consume the `~' token. */
2675 cp_lexer_consume_token (parser->lexer);
2676 /* Parse the class-name. The standard, as written, seems to
2679 template <typename T> struct S { ~S (); };
2680 template <typename T> S<T>::~S() {}
2682 is invalid, since `~' must be followed by a class-name, but
2683 `S<T>' is dependent, and so not known to be a class.
2684 That's not right; we need to look in uninstantiated
2685 templates. A further complication arises from:
2687 template <typename T> void f(T t) {
2691 Here, it is not possible to look up `T' in the scope of `T'
2692 itself. We must look in both the current scope, and the
2693 scope of the containing complete expression.
2695 Yet another issue is:
2704 The standard does not seem to say that the `S' in `~S'
2705 should refer to the type `S' and not the data member
2708 /* DR 244 says that we look up the name after the "~" in the
2709 same scope as we looked up the qualifying name. That idea
2710 isn't fully worked out; it's more complicated than that. */
2711 scope = parser->scope;
2712 object_scope = parser->object_scope;
2713 qualifying_scope = parser->qualifying_scope;
2715 /* If the name is of the form "X::~X" it's OK. */
2716 if (scope && TYPE_P (scope)
2717 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
2718 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
2720 && (cp_lexer_peek_token (parser->lexer)->value
2721 == TYPE_IDENTIFIER (scope)))
2723 cp_lexer_consume_token (parser->lexer);
2724 return build_nt (BIT_NOT_EXPR, scope);
2727 /* If there was an explicit qualification (S::~T), first look
2728 in the scope given by the qualification (i.e., S). */
2731 cp_parser_parse_tentatively (parser);
2732 type_decl = cp_parser_class_name (parser,
2733 /*typename_keyword_p=*/false,
2734 /*template_keyword_p=*/false,
2736 /*check_dependency=*/false,
2737 /*class_head_p=*/false);
2738 if (cp_parser_parse_definitely (parser))
2739 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
2741 /* In "N::S::~S", look in "N" as well. */
2742 if (scope && qualifying_scope)
2744 cp_parser_parse_tentatively (parser);
2745 parser->scope = qualifying_scope;
2746 parser->object_scope = NULL_TREE;
2747 parser->qualifying_scope = NULL_TREE;
2749 = cp_parser_class_name (parser,
2750 /*typename_keyword_p=*/false,
2751 /*template_keyword_p=*/false,
2753 /*check_dependency=*/false,
2754 /*class_head_p=*/false);
2755 if (cp_parser_parse_definitely (parser))
2756 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
2758 /* In "p->S::~T", look in the scope given by "*p" as well. */
2759 else if (object_scope)
2761 cp_parser_parse_tentatively (parser);
2762 parser->scope = object_scope;
2763 parser->object_scope = NULL_TREE;
2764 parser->qualifying_scope = NULL_TREE;
2766 = cp_parser_class_name (parser,
2767 /*typename_keyword_p=*/false,
2768 /*template_keyword_p=*/false,
2770 /*check_dependency=*/false,
2771 /*class_head_p=*/false);
2772 if (cp_parser_parse_definitely (parser))
2773 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
2775 /* Look in the surrounding context. */
2776 parser->scope = NULL_TREE;
2777 parser->object_scope = NULL_TREE;
2778 parser->qualifying_scope = NULL_TREE;
2780 = cp_parser_class_name (parser,
2781 /*typename_keyword_p=*/false,
2782 /*template_keyword_p=*/false,
2784 /*check_dependency=*/false,
2785 /*class_head_p=*/false);
2786 /* If an error occurred, assume that the name of the
2787 destructor is the same as the name of the qualifying
2788 class. That allows us to keep parsing after running
2789 into ill-formed destructor names. */
2790 if (type_decl == error_mark_node && scope && TYPE_P (scope))
2791 return build_nt (BIT_NOT_EXPR, scope);
2792 else if (type_decl == error_mark_node)
2793 return error_mark_node;
2797 A typedef-name that names a class shall not be used as the
2798 identifier in the declarator for a destructor declaration. */
2800 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
2801 && !DECL_SELF_REFERENCE_P (type_decl))
2802 error ("typedef-name `%D' used as destructor declarator",
2805 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
2809 if (token->keyword == RID_OPERATOR)
2813 /* This could be a template-id, so we try that first. */
2814 cp_parser_parse_tentatively (parser);
2815 /* Try a template-id. */
2816 id = cp_parser_template_id (parser, template_keyword_p,
2817 /*check_dependency_p=*/true);
2818 /* If that worked, we're done. */
2819 if (cp_parser_parse_definitely (parser))
2821 /* We still don't know whether we're looking at an
2822 operator-function-id or a conversion-function-id. */
2823 cp_parser_parse_tentatively (parser);
2824 /* Try an operator-function-id. */
2825 id = cp_parser_operator_function_id (parser);
2826 /* If that didn't work, try a conversion-function-id. */
2827 if (!cp_parser_parse_definitely (parser))
2828 id = cp_parser_conversion_function_id (parser);
2835 cp_parser_error (parser, "expected unqualified-id");
2836 return error_mark_node;
2840 /* Parse an (optional) nested-name-specifier.
2842 nested-name-specifier:
2843 class-or-namespace-name :: nested-name-specifier [opt]
2844 class-or-namespace-name :: template nested-name-specifier [opt]
2846 PARSER->SCOPE should be set appropriately before this function is
2847 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
2848 effect. TYPE_P is TRUE if we non-type bindings should be ignored
2851 Sets PARSER->SCOPE to the class (TYPE) or namespace
2852 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
2853 it unchanged if there is no nested-name-specifier. Returns the new
2854 scope iff there is a nested-name-specifier, or NULL_TREE otherwise. */
2857 cp_parser_nested_name_specifier_opt (cp_parser *parser,
2858 bool typename_keyword_p,
2859 bool check_dependency_p,
2862 bool success = false;
2863 tree access_check = NULL_TREE;
2867 /* If the next token corresponds to a nested name specifier, there
2868 is no need to reparse it. However, if CHECK_DEPENDENCY_P is
2869 false, it may have been true before, in which case something
2870 like `A<X>::B<Y>::C' may have resulted in a nested-name-specifier
2871 of `A<X>::', where it should now be `A<X>::B<Y>::'. So, when
2872 CHECK_DEPENDENCY_P is false, we have to fall through into the
2874 if (check_dependency_p
2875 && cp_lexer_next_token_is (parser->lexer, CPP_NESTED_NAME_SPECIFIER))
2877 cp_parser_pre_parsed_nested_name_specifier (parser);
2878 return parser->scope;
2881 /* Remember where the nested-name-specifier starts. */
2882 if (cp_parser_parsing_tentatively (parser)
2883 && !cp_parser_committed_to_tentative_parse (parser))
2885 token = cp_lexer_peek_token (parser->lexer);
2886 start = cp_lexer_token_difference (parser->lexer,
2887 parser->lexer->first_token,
2893 push_deferring_access_checks (dk_deferred);
2899 tree saved_qualifying_scope;
2900 bool template_keyword_p;
2902 /* Spot cases that cannot be the beginning of a
2903 nested-name-specifier. */
2904 token = cp_lexer_peek_token (parser->lexer);
2906 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
2907 the already parsed nested-name-specifier. */
2908 if (token->type == CPP_NESTED_NAME_SPECIFIER)
2910 /* Grab the nested-name-specifier and continue the loop. */
2911 cp_parser_pre_parsed_nested_name_specifier (parser);
2916 /* Spot cases that cannot be the beginning of a
2917 nested-name-specifier. On the second and subsequent times
2918 through the loop, we look for the `template' keyword. */
2919 if (success && token->keyword == RID_TEMPLATE)
2921 /* A template-id can start a nested-name-specifier. */
2922 else if (token->type == CPP_TEMPLATE_ID)
2926 /* If the next token is not an identifier, then it is
2927 definitely not a class-or-namespace-name. */
2928 if (token->type != CPP_NAME)
2930 /* If the following token is neither a `<' (to begin a
2931 template-id), nor a `::', then we are not looking at a
2932 nested-name-specifier. */
2933 token = cp_lexer_peek_nth_token (parser->lexer, 2);
2934 if (token->type != CPP_LESS && token->type != CPP_SCOPE)
2938 /* The nested-name-specifier is optional, so we parse
2940 cp_parser_parse_tentatively (parser);
2942 /* Look for the optional `template' keyword, if this isn't the
2943 first time through the loop. */
2945 template_keyword_p = cp_parser_optional_template_keyword (parser);
2947 template_keyword_p = false;
2949 /* Save the old scope since the name lookup we are about to do
2950 might destroy it. */
2951 old_scope = parser->scope;
2952 saved_qualifying_scope = parser->qualifying_scope;
2953 /* Parse the qualifying entity. */
2955 = cp_parser_class_or_namespace_name (parser,
2960 /* Look for the `::' token. */
2961 cp_parser_require (parser, CPP_SCOPE, "`::'");
2963 /* If we found what we wanted, we keep going; otherwise, we're
2965 if (!cp_parser_parse_definitely (parser))
2967 bool error_p = false;
2969 /* Restore the OLD_SCOPE since it was valid before the
2970 failed attempt at finding the last
2971 class-or-namespace-name. */
2972 parser->scope = old_scope;
2973 parser->qualifying_scope = saved_qualifying_scope;
2974 /* If the next token is an identifier, and the one after
2975 that is a `::', then any valid interpretation would have
2976 found a class-or-namespace-name. */
2977 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
2978 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
2980 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
2983 token = cp_lexer_consume_token (parser->lexer);
2988 decl = cp_parser_lookup_name_simple (parser, token->value);
2989 if (TREE_CODE (decl) == TEMPLATE_DECL)
2990 error ("`%D' used without template parameters",
2992 else if (parser->scope)
2994 if (TYPE_P (parser->scope))
2995 error ("`%T::%D' is not a class-name or "
2997 parser->scope, token->value);
2999 error ("`%D::%D' is not a class-name or "
3001 parser->scope, token->value);
3004 error ("`%D' is not a class-name or namespace-name",
3006 parser->scope = NULL_TREE;
3008 /* Treat this as a successful nested-name-specifier
3013 If the name found is not a class-name (clause
3014 _class_) or namespace-name (_namespace.def_), the
3015 program is ill-formed. */
3018 cp_lexer_consume_token (parser->lexer);
3023 /* We've found one valid nested-name-specifier. */
3025 /* Make sure we look in the right scope the next time through
3027 parser->scope = (TREE_CODE (new_scope) == TYPE_DECL
3028 ? TREE_TYPE (new_scope)
3030 /* If it is a class scope, try to complete it; we are about to
3031 be looking up names inside the class. */
3032 if (TYPE_P (parser->scope)
3033 /* Since checking types for dependency can be expensive,
3034 avoid doing it if the type is already complete. */
3035 && !COMPLETE_TYPE_P (parser->scope)
3036 /* Do not try to complete dependent types. */
3037 && !dependent_type_p (parser->scope))
3038 complete_type (parser->scope);
3041 /* Retrieve any deferred checks. Do not pop this access checks yet
3042 so the memory will not be reclaimed during token replacing below. */
3043 access_check = get_deferred_access_checks ();
3045 /* If parsing tentatively, replace the sequence of tokens that makes
3046 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
3047 token. That way, should we re-parse the token stream, we will
3048 not have to repeat the effort required to do the parse, nor will
3049 we issue duplicate error messages. */
3050 if (success && start >= 0)
3052 /* Find the token that corresponds to the start of the
3054 token = cp_lexer_advance_token (parser->lexer,
3055 parser->lexer->first_token,
3058 /* Reset the contents of the START token. */
3059 token->type = CPP_NESTED_NAME_SPECIFIER;
3060 token->value = build_tree_list (access_check, parser->scope);
3061 TREE_TYPE (token->value) = parser->qualifying_scope;
3062 token->keyword = RID_MAX;
3063 /* Purge all subsequent tokens. */
3064 cp_lexer_purge_tokens_after (parser->lexer, token);
3067 pop_deferring_access_checks ();
3068 return success ? parser->scope : NULL_TREE;
3071 /* Parse a nested-name-specifier. See
3072 cp_parser_nested_name_specifier_opt for details. This function
3073 behaves identically, except that it will an issue an error if no
3074 nested-name-specifier is present, and it will return
3075 ERROR_MARK_NODE, rather than NULL_TREE, if no nested-name-specifier
3079 cp_parser_nested_name_specifier (cp_parser *parser,
3080 bool typename_keyword_p,
3081 bool check_dependency_p,
3086 /* Look for the nested-name-specifier. */
3087 scope = cp_parser_nested_name_specifier_opt (parser,
3091 /* If it was not present, issue an error message. */
3094 cp_parser_error (parser, "expected nested-name-specifier");
3095 parser->scope = NULL_TREE;
3096 return error_mark_node;
3102 /* Parse a class-or-namespace-name.
3104 class-or-namespace-name:
3108 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
3109 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
3110 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
3111 TYPE_P is TRUE iff the next name should be taken as a class-name,
3112 even the same name is declared to be another entity in the same
3115 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
3116 specified by the class-or-namespace-name. If neither is found the
3117 ERROR_MARK_NODE is returned. */
3120 cp_parser_class_or_namespace_name (cp_parser *parser,
3121 bool typename_keyword_p,
3122 bool template_keyword_p,
3123 bool check_dependency_p,
3127 tree saved_qualifying_scope;
3128 tree saved_object_scope;
3132 /* Before we try to parse the class-name, we must save away the
3133 current PARSER->SCOPE since cp_parser_class_name will destroy
3135 saved_scope = parser->scope;
3136 saved_qualifying_scope = parser->qualifying_scope;
3137 saved_object_scope = parser->object_scope;
3138 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
3139 there is no need to look for a namespace-name. */
3140 only_class_p = template_keyword_p || (saved_scope && TYPE_P (saved_scope));
3142 cp_parser_parse_tentatively (parser);
3143 scope = cp_parser_class_name (parser,
3148 /*class_head_p=*/false);
3149 /* If that didn't work, try for a namespace-name. */
3150 if (!only_class_p && !cp_parser_parse_definitely (parser))
3152 /* Restore the saved scope. */
3153 parser->scope = saved_scope;
3154 parser->qualifying_scope = saved_qualifying_scope;
3155 parser->object_scope = saved_object_scope;
3156 /* If we are not looking at an identifier followed by the scope
3157 resolution operator, then this is not part of a
3158 nested-name-specifier. (Note that this function is only used
3159 to parse the components of a nested-name-specifier.) */
3160 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
3161 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
3162 return error_mark_node;
3163 scope = cp_parser_namespace_name (parser);
3169 /* Parse a postfix-expression.
3173 postfix-expression [ expression ]
3174 postfix-expression ( expression-list [opt] )
3175 simple-type-specifier ( expression-list [opt] )
3176 typename :: [opt] nested-name-specifier identifier
3177 ( expression-list [opt] )
3178 typename :: [opt] nested-name-specifier template [opt] template-id
3179 ( expression-list [opt] )
3180 postfix-expression . template [opt] id-expression
3181 postfix-expression -> template [opt] id-expression
3182 postfix-expression . pseudo-destructor-name
3183 postfix-expression -> pseudo-destructor-name
3184 postfix-expression ++
3185 postfix-expression --
3186 dynamic_cast < type-id > ( expression )
3187 static_cast < type-id > ( expression )
3188 reinterpret_cast < type-id > ( expression )
3189 const_cast < type-id > ( expression )
3190 typeid ( expression )
3196 ( type-id ) { initializer-list , [opt] }
3198 This extension is a GNU version of the C99 compound-literal
3199 construct. (The C99 grammar uses `type-name' instead of `type-id',
3200 but they are essentially the same concept.)
3202 If ADDRESS_P is true, the postfix expression is the operand of the
3205 Returns a representation of the expression. */
3208 cp_parser_postfix_expression (cp_parser *parser, bool address_p)
3212 cp_id_kind idk = CP_ID_KIND_NONE;
3213 tree postfix_expression = NULL_TREE;
3214 /* Non-NULL only if the current postfix-expression can be used to
3215 form a pointer-to-member. In that case, QUALIFYING_CLASS is the
3216 class used to qualify the member. */
3217 tree qualifying_class = NULL_TREE;
3219 /* Peek at the next token. */
3220 token = cp_lexer_peek_token (parser->lexer);
3221 /* Some of the productions are determined by keywords. */
3222 keyword = token->keyword;
3232 const char *saved_message;
3234 /* All of these can be handled in the same way from the point
3235 of view of parsing. Begin by consuming the token
3236 identifying the cast. */
3237 cp_lexer_consume_token (parser->lexer);
3239 /* New types cannot be defined in the cast. */
3240 saved_message = parser->type_definition_forbidden_message;
3241 parser->type_definition_forbidden_message
3242 = "types may not be defined in casts";
3244 /* Look for the opening `<'. */
3245 cp_parser_require (parser, CPP_LESS, "`<'");
3246 /* Parse the type to which we are casting. */
3247 type = cp_parser_type_id (parser);
3248 /* Look for the closing `>'. */
3249 cp_parser_require (parser, CPP_GREATER, "`>'");
3250 /* Restore the old message. */
3251 parser->type_definition_forbidden_message = saved_message;
3253 /* And the expression which is being cast. */
3254 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3255 expression = cp_parser_expression (parser);
3256 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3258 /* Only type conversions to integral or enumeration types
3259 can be used in constant-expressions. */
3260 if (parser->constant_expression_p
3261 && !dependent_type_p (type)
3262 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type))
3264 if (!parser->allow_non_constant_expression_p)
3265 return (cp_parser_non_constant_expression
3266 ("a cast to a type other than an integral or "
3267 "enumeration type"));
3268 parser->non_constant_expression_p = true;
3275 = build_dynamic_cast (type, expression);
3279 = build_static_cast (type, expression);
3283 = build_reinterpret_cast (type, expression);
3287 = build_const_cast (type, expression);
3298 const char *saved_message;
3300 /* Consume the `typeid' token. */
3301 cp_lexer_consume_token (parser->lexer);
3302 /* Look for the `(' token. */
3303 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3304 /* Types cannot be defined in a `typeid' expression. */
3305 saved_message = parser->type_definition_forbidden_message;
3306 parser->type_definition_forbidden_message
3307 = "types may not be defined in a `typeid\' expression";
3308 /* We can't be sure yet whether we're looking at a type-id or an
3310 cp_parser_parse_tentatively (parser);
3311 /* Try a type-id first. */
3312 type = cp_parser_type_id (parser);
3313 /* Look for the `)' token. Otherwise, we can't be sure that
3314 we're not looking at an expression: consider `typeid (int
3315 (3))', for example. */
3316 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3317 /* If all went well, simply lookup the type-id. */
3318 if (cp_parser_parse_definitely (parser))
3319 postfix_expression = get_typeid (type);
3320 /* Otherwise, fall back to the expression variant. */
3325 /* Look for an expression. */
3326 expression = cp_parser_expression (parser);
3327 /* Compute its typeid. */
3328 postfix_expression = build_typeid (expression);
3329 /* Look for the `)' token. */
3330 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3333 /* Restore the saved message. */
3334 parser->type_definition_forbidden_message = saved_message;
3340 bool template_p = false;
3344 /* Consume the `typename' token. */
3345 cp_lexer_consume_token (parser->lexer);
3346 /* Look for the optional `::' operator. */
3347 cp_parser_global_scope_opt (parser,
3348 /*current_scope_valid_p=*/false);
3349 /* Look for the nested-name-specifier. */
3350 cp_parser_nested_name_specifier (parser,
3351 /*typename_keyword_p=*/true,
3352 /*check_dependency_p=*/true,
3354 /* Look for the optional `template' keyword. */
3355 template_p = cp_parser_optional_template_keyword (parser);
3356 /* We don't know whether we're looking at a template-id or an
3358 cp_parser_parse_tentatively (parser);
3359 /* Try a template-id. */
3360 id = cp_parser_template_id (parser, template_p,
3361 /*check_dependency_p=*/true);
3362 /* If that didn't work, try an identifier. */
3363 if (!cp_parser_parse_definitely (parser))
3364 id = cp_parser_identifier (parser);
3365 /* Create a TYPENAME_TYPE to represent the type to which the
3366 functional cast is being performed. */
3367 type = make_typename_type (parser->scope, id,
3370 postfix_expression = cp_parser_functional_cast (parser, type);
3378 /* If the next thing is a simple-type-specifier, we may be
3379 looking at a functional cast. We could also be looking at
3380 an id-expression. So, we try the functional cast, and if
3381 that doesn't work we fall back to the primary-expression. */
3382 cp_parser_parse_tentatively (parser);
3383 /* Look for the simple-type-specifier. */
3384 type = cp_parser_simple_type_specifier (parser,
3385 CP_PARSER_FLAGS_NONE,
3386 /*identifier_p=*/false);
3387 /* Parse the cast itself. */
3388 if (!cp_parser_error_occurred (parser))
3390 = cp_parser_functional_cast (parser, type);
3391 /* If that worked, we're done. */
3392 if (cp_parser_parse_definitely (parser))
3395 /* If the functional-cast didn't work out, try a
3396 compound-literal. */
3397 if (cp_parser_allow_gnu_extensions_p (parser)
3398 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
3400 tree initializer_list = NULL_TREE;
3402 cp_parser_parse_tentatively (parser);
3403 /* Consume the `('. */
3404 cp_lexer_consume_token (parser->lexer);
3405 /* Parse the type. */
3406 type = cp_parser_type_id (parser);
3407 /* Look for the `)'. */
3408 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3409 /* Look for the `{'. */
3410 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
3411 /* If things aren't going well, there's no need to
3413 if (!cp_parser_error_occurred (parser))
3415 bool non_constant_p;
3416 /* Parse the initializer-list. */
3418 = cp_parser_initializer_list (parser, &non_constant_p);
3419 /* Allow a trailing `,'. */
3420 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
3421 cp_lexer_consume_token (parser->lexer);
3422 /* Look for the final `}'. */
3423 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
3425 /* If that worked, we're definitely looking at a
3426 compound-literal expression. */
3427 if (cp_parser_parse_definitely (parser))
3429 /* Warn the user that a compound literal is not
3430 allowed in standard C++. */
3432 pedwarn ("ISO C++ forbids compound-literals");
3433 /* Form the representation of the compound-literal. */
3435 = finish_compound_literal (type, initializer_list);
3440 /* It must be a primary-expression. */
3441 postfix_expression = cp_parser_primary_expression (parser,
3448 /* If we were avoiding committing to the processing of a
3449 qualified-id until we knew whether or not we had a
3450 pointer-to-member, we now know. */
3451 if (qualifying_class)
3455 /* Peek at the next token. */
3456 token = cp_lexer_peek_token (parser->lexer);
3457 done = (token->type != CPP_OPEN_SQUARE
3458 && token->type != CPP_OPEN_PAREN
3459 && token->type != CPP_DOT
3460 && token->type != CPP_DEREF
3461 && token->type != CPP_PLUS_PLUS
3462 && token->type != CPP_MINUS_MINUS);
3464 postfix_expression = finish_qualified_id_expr (qualifying_class,
3469 return postfix_expression;
3472 /* Keep looping until the postfix-expression is complete. */
3475 if (idk == CP_ID_KIND_UNQUALIFIED
3476 && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
3477 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
3478 /* It is not a Koenig lookup function call. */
3480 = unqualified_name_lookup_error (postfix_expression);
3482 /* Peek at the next token. */
3483 token = cp_lexer_peek_token (parser->lexer);
3485 switch (token->type)
3487 case CPP_OPEN_SQUARE:
3488 /* postfix-expression [ expression ] */
3492 /* Consume the `[' token. */
3493 cp_lexer_consume_token (parser->lexer);
3494 /* Parse the index expression. */
3495 index = cp_parser_expression (parser);
3496 /* Look for the closing `]'. */
3497 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
3499 /* Build the ARRAY_REF. */
3501 = grok_array_decl (postfix_expression, index);
3502 idk = CP_ID_KIND_NONE;
3506 case CPP_OPEN_PAREN:
3507 /* postfix-expression ( expression-list [opt] ) */
3510 tree args = (cp_parser_parenthesized_expression_list
3511 (parser, false, /*non_constant_p=*/NULL));
3513 if (args == error_mark_node)
3515 postfix_expression = error_mark_node;
3519 /* Function calls are not permitted in
3520 constant-expressions. */
3521 if (parser->constant_expression_p)
3523 if (!parser->allow_non_constant_expression_p)
3524 return cp_parser_non_constant_expression ("a function call");
3525 parser->non_constant_expression_p = true;
3529 if (idk == CP_ID_KIND_UNQUALIFIED)
3532 && (is_overloaded_fn (postfix_expression)
3533 || DECL_P (postfix_expression)
3534 || TREE_CODE (postfix_expression) == IDENTIFIER_NODE))
3538 = perform_koenig_lookup (postfix_expression, args);
3540 else if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
3542 = unqualified_fn_lookup_error (postfix_expression);
3545 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
3547 tree instance = TREE_OPERAND (postfix_expression, 0);
3548 tree fn = TREE_OPERAND (postfix_expression, 1);
3550 if (processing_template_decl
3551 && (type_dependent_expression_p (instance)
3552 || (!BASELINK_P (fn)
3553 && TREE_CODE (fn) != FIELD_DECL)
3554 || type_dependent_expression_p (fn)
3555 || any_type_dependent_arguments_p (args)))
3558 = build_min_nt (CALL_EXPR, postfix_expression, args);
3563 = (build_new_method_call
3564 (instance, fn, args, NULL_TREE,
3565 (idk == CP_ID_KIND_QUALIFIED
3566 ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL)));
3568 else if (TREE_CODE (postfix_expression) == OFFSET_REF
3569 || TREE_CODE (postfix_expression) == MEMBER_REF
3570 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
3571 postfix_expression = (build_offset_ref_call_from_tree
3572 (postfix_expression, args));
3573 else if (idk == CP_ID_KIND_QUALIFIED)
3574 /* A call to a static class member, or a namespace-scope
3577 = finish_call_expr (postfix_expression, args,
3578 /*disallow_virtual=*/true,
3581 /* All other function calls. */
3583 = finish_call_expr (postfix_expression, args,
3584 /*disallow_virtual=*/false,
3587 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
3588 idk = CP_ID_KIND_NONE;
3594 /* postfix-expression . template [opt] id-expression
3595 postfix-expression . pseudo-destructor-name
3596 postfix-expression -> template [opt] id-expression
3597 postfix-expression -> pseudo-destructor-name */
3602 tree scope = NULL_TREE;
3604 /* If this is a `->' operator, dereference the pointer. */
3605 if (token->type == CPP_DEREF)
3606 postfix_expression = build_x_arrow (postfix_expression);
3607 /* Check to see whether or not the expression is
3609 dependent_p = type_dependent_expression_p (postfix_expression);
3610 /* The identifier following the `->' or `.' is not
3612 parser->scope = NULL_TREE;
3613 parser->qualifying_scope = NULL_TREE;
3614 parser->object_scope = NULL_TREE;
3615 idk = CP_ID_KIND_NONE;
3616 /* Enter the scope corresponding to the type of the object
3617 given by the POSTFIX_EXPRESSION. */
3619 && TREE_TYPE (postfix_expression) != NULL_TREE)
3621 scope = TREE_TYPE (postfix_expression);
3622 /* According to the standard, no expression should
3623 ever have reference type. Unfortunately, we do not
3624 currently match the standard in this respect in
3625 that our internal representation of an expression
3626 may have reference type even when the standard says
3627 it does not. Therefore, we have to manually obtain
3628 the underlying type here. */
3629 scope = non_reference (scope);
3630 /* The type of the POSTFIX_EXPRESSION must be
3632 scope = complete_type_or_else (scope, NULL_TREE);
3633 /* Let the name lookup machinery know that we are
3634 processing a class member access expression. */
3635 parser->context->object_type = scope;
3636 /* If something went wrong, we want to be able to
3637 discern that case, as opposed to the case where
3638 there was no SCOPE due to the type of expression
3641 scope = error_mark_node;
3644 /* Consume the `.' or `->' operator. */
3645 cp_lexer_consume_token (parser->lexer);
3646 /* If the SCOPE is not a scalar type, we are looking at an
3647 ordinary class member access expression, rather than a
3648 pseudo-destructor-name. */
3649 if (!scope || !SCALAR_TYPE_P (scope))
3651 template_p = cp_parser_optional_template_keyword (parser);
3652 /* Parse the id-expression. */
3653 name = cp_parser_id_expression (parser,
3655 /*check_dependency_p=*/true,
3656 /*template_p=*/NULL,
3657 /*declarator_p=*/false);
3658 /* In general, build a SCOPE_REF if the member name is
3659 qualified. However, if the name was not dependent
3660 and has already been resolved; there is no need to
3661 build the SCOPE_REF. For example;
3663 struct X { void f(); };
3664 template <typename T> void f(T* t) { t->X::f(); }
3666 Even though "t" is dependent, "X::f" is not and has
3667 been resolved to a BASELINK; there is no need to
3668 include scope information. */
3670 /* But we do need to remember that there was an explicit
3671 scope for virtual function calls. */
3673 idk = CP_ID_KIND_QUALIFIED;
3675 if (name != error_mark_node
3676 && !BASELINK_P (name)
3679 name = build_nt (SCOPE_REF, parser->scope, name);
3680 parser->scope = NULL_TREE;
3681 parser->qualifying_scope = NULL_TREE;
3682 parser->object_scope = NULL_TREE;
3685 = finish_class_member_access_expr (postfix_expression, name);
3687 /* Otherwise, try the pseudo-destructor-name production. */
3693 /* Parse the pseudo-destructor-name. */
3694 cp_parser_pseudo_destructor_name (parser, &s, &type);
3695 /* Form the call. */
3697 = finish_pseudo_destructor_expr (postfix_expression,
3698 s, TREE_TYPE (type));
3701 /* We no longer need to look up names in the scope of the
3702 object on the left-hand side of the `.' or `->'
3704 parser->context->object_type = NULL_TREE;
3709 /* postfix-expression ++ */
3710 /* Consume the `++' token. */
3711 cp_lexer_consume_token (parser->lexer);
3712 /* Increments may not appear in constant-expressions. */
3713 if (parser->constant_expression_p)
3715 if (!parser->allow_non_constant_expression_p)
3716 return cp_parser_non_constant_expression ("an increment");
3717 parser->non_constant_expression_p = true;
3719 /* Generate a representation for the complete expression. */
3721 = finish_increment_expr (postfix_expression,
3722 POSTINCREMENT_EXPR);
3723 idk = CP_ID_KIND_NONE;
3726 case CPP_MINUS_MINUS:
3727 /* postfix-expression -- */
3728 /* Consume the `--' token. */
3729 cp_lexer_consume_token (parser->lexer);
3730 /* Decrements may not appear in constant-expressions. */
3731 if (parser->constant_expression_p)
3733 if (!parser->allow_non_constant_expression_p)
3734 return cp_parser_non_constant_expression ("a decrement");
3735 parser->non_constant_expression_p = true;
3737 /* Generate a representation for the complete expression. */
3739 = finish_increment_expr (postfix_expression,
3740 POSTDECREMENT_EXPR);
3741 idk = CP_ID_KIND_NONE;
3745 return postfix_expression;
3749 /* We should never get here. */
3751 return error_mark_node;
3754 /* Parse a parenthesized expression-list.
3757 assignment-expression
3758 expression-list, assignment-expression
3763 identifier, expression-list
3765 Returns a TREE_LIST. The TREE_VALUE of each node is a
3766 representation of an assignment-expression. Note that a TREE_LIST
3767 is returned even if there is only a single expression in the list.
3768 error_mark_node is returned if the ( and or ) are
3769 missing. NULL_TREE is returned on no expressions. The parentheses
3770 are eaten. IS_ATTRIBUTE_LIST is true if this is really an attribute
3771 list being parsed. If NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P
3772 indicates whether or not all of the expressions in the list were
3776 cp_parser_parenthesized_expression_list (cp_parser* parser,
3777 bool is_attribute_list,
3778 bool *non_constant_p)
3780 tree expression_list = NULL_TREE;
3781 tree identifier = NULL_TREE;
3783 /* Assume all the expressions will be constant. */
3785 *non_constant_p = false;
3787 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
3788 return error_mark_node;
3790 /* Consume expressions until there are no more. */
3791 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
3796 /* At the beginning of attribute lists, check to see if the
3797 next token is an identifier. */
3798 if (is_attribute_list
3799 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
3803 /* Consume the identifier. */
3804 token = cp_lexer_consume_token (parser->lexer);
3805 /* Save the identifier. */
3806 identifier = token->value;
3810 /* Parse the next assignment-expression. */
3813 bool expr_non_constant_p;
3814 expr = (cp_parser_constant_expression
3815 (parser, /*allow_non_constant_p=*/true,
3816 &expr_non_constant_p));
3817 if (expr_non_constant_p)
3818 *non_constant_p = true;
3821 expr = cp_parser_assignment_expression (parser);
3823 /* Add it to the list. We add error_mark_node
3824 expressions to the list, so that we can still tell if
3825 the correct form for a parenthesized expression-list
3826 is found. That gives better errors. */
3827 expression_list = tree_cons (NULL_TREE, expr, expression_list);
3829 if (expr == error_mark_node)
3833 /* After the first item, attribute lists look the same as
3834 expression lists. */
3835 is_attribute_list = false;
3838 /* If the next token isn't a `,', then we are done. */
3839 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
3842 /* Otherwise, consume the `,' and keep going. */
3843 cp_lexer_consume_token (parser->lexer);
3846 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
3851 /* We try and resync to an unnested comma, as that will give the
3852 user better diagnostics. */
3853 ending = cp_parser_skip_to_closing_parenthesis (parser, true, true);
3857 return error_mark_node;
3860 /* We built up the list in reverse order so we must reverse it now. */
3861 expression_list = nreverse (expression_list);
3863 expression_list = tree_cons (NULL_TREE, identifier, expression_list);
3865 return expression_list;
3868 /* Parse a pseudo-destructor-name.
3870 pseudo-destructor-name:
3871 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
3872 :: [opt] nested-name-specifier template template-id :: ~ type-name
3873 :: [opt] nested-name-specifier [opt] ~ type-name
3875 If either of the first two productions is used, sets *SCOPE to the
3876 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
3877 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
3878 or ERROR_MARK_NODE if no type-name is present. */
3881 cp_parser_pseudo_destructor_name (cp_parser* parser,
3885 bool nested_name_specifier_p;
3887 /* Look for the optional `::' operator. */
3888 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
3889 /* Look for the optional nested-name-specifier. */
3890 nested_name_specifier_p
3891 = (cp_parser_nested_name_specifier_opt (parser,
3892 /*typename_keyword_p=*/false,
3893 /*check_dependency_p=*/true,
3896 /* Now, if we saw a nested-name-specifier, we might be doing the
3897 second production. */
3898 if (nested_name_specifier_p
3899 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
3901 /* Consume the `template' keyword. */
3902 cp_lexer_consume_token (parser->lexer);
3903 /* Parse the template-id. */
3904 cp_parser_template_id (parser,
3905 /*template_keyword_p=*/true,
3906 /*check_dependency_p=*/false);
3907 /* Look for the `::' token. */
3908 cp_parser_require (parser, CPP_SCOPE, "`::'");
3910 /* If the next token is not a `~', then there might be some
3911 additional qualification. */
3912 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
3914 /* Look for the type-name. */
3915 *scope = TREE_TYPE (cp_parser_type_name (parser));
3916 /* Look for the `::' token. */
3917 cp_parser_require (parser, CPP_SCOPE, "`::'");
3922 /* Look for the `~'. */
3923 cp_parser_require (parser, CPP_COMPL, "`~'");
3924 /* Look for the type-name again. We are not responsible for
3925 checking that it matches the first type-name. */
3926 *type = cp_parser_type_name (parser);
3929 /* Parse a unary-expression.
3935 unary-operator cast-expression
3936 sizeof unary-expression
3944 __extension__ cast-expression
3945 __alignof__ unary-expression
3946 __alignof__ ( type-id )
3947 __real__ cast-expression
3948 __imag__ cast-expression
3951 ADDRESS_P is true iff the unary-expression is appearing as the
3952 operand of the `&' operator.
3954 Returns a representation of the expression. */
3957 cp_parser_unary_expression (cp_parser *parser, bool address_p)
3960 enum tree_code unary_operator;
3962 /* Peek at the next token. */
3963 token = cp_lexer_peek_token (parser->lexer);
3964 /* Some keywords give away the kind of expression. */
3965 if (token->type == CPP_KEYWORD)
3967 enum rid keyword = token->keyword;
3977 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
3978 /* Consume the token. */
3979 cp_lexer_consume_token (parser->lexer);
3980 /* Parse the operand. */
3981 operand = cp_parser_sizeof_operand (parser, keyword);
3983 if (TYPE_P (operand))
3984 return cxx_sizeof_or_alignof_type (operand, op, true);
3986 return cxx_sizeof_or_alignof_expr (operand, op);
3990 return cp_parser_new_expression (parser);
3993 return cp_parser_delete_expression (parser);
3997 /* The saved value of the PEDANTIC flag. */
4001 /* Save away the PEDANTIC flag. */
4002 cp_parser_extension_opt (parser, &saved_pedantic);
4003 /* Parse the cast-expression. */
4004 expr = cp_parser_simple_cast_expression (parser);
4005 /* Restore the PEDANTIC flag. */
4006 pedantic = saved_pedantic;
4016 /* Consume the `__real__' or `__imag__' token. */
4017 cp_lexer_consume_token (parser->lexer);
4018 /* Parse the cast-expression. */
4019 expression = cp_parser_simple_cast_expression (parser);
4020 /* Create the complete representation. */
4021 return build_x_unary_op ((keyword == RID_REALPART
4022 ? REALPART_EXPR : IMAGPART_EXPR),
4032 /* Look for the `:: new' and `:: delete', which also signal the
4033 beginning of a new-expression, or delete-expression,
4034 respectively. If the next token is `::', then it might be one of
4036 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
4040 /* See if the token after the `::' is one of the keywords in
4041 which we're interested. */
4042 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
4043 /* If it's `new', we have a new-expression. */
4044 if (keyword == RID_NEW)
4045 return cp_parser_new_expression (parser);
4046 /* Similarly, for `delete'. */
4047 else if (keyword == RID_DELETE)
4048 return cp_parser_delete_expression (parser);
4051 /* Look for a unary operator. */
4052 unary_operator = cp_parser_unary_operator (token);
4053 /* The `++' and `--' operators can be handled similarly, even though
4054 they are not technically unary-operators in the grammar. */
4055 if (unary_operator == ERROR_MARK)
4057 if (token->type == CPP_PLUS_PLUS)
4058 unary_operator = PREINCREMENT_EXPR;
4059 else if (token->type == CPP_MINUS_MINUS)
4060 unary_operator = PREDECREMENT_EXPR;
4061 /* Handle the GNU address-of-label extension. */
4062 else if (cp_parser_allow_gnu_extensions_p (parser)
4063 && token->type == CPP_AND_AND)
4067 /* Consume the '&&' token. */
4068 cp_lexer_consume_token (parser->lexer);
4069 /* Look for the identifier. */
4070 identifier = cp_parser_identifier (parser);
4071 /* Create an expression representing the address. */
4072 return finish_label_address_expr (identifier);
4075 if (unary_operator != ERROR_MARK)
4077 tree cast_expression;
4079 /* Consume the operator token. */
4080 token = cp_lexer_consume_token (parser->lexer);
4081 /* Parse the cast-expression. */
4083 = cp_parser_cast_expression (parser, unary_operator == ADDR_EXPR);
4084 /* Now, build an appropriate representation. */
4085 switch (unary_operator)
4088 return build_x_indirect_ref (cast_expression, "unary *");
4092 return build_x_unary_op (unary_operator, cast_expression);
4094 case PREINCREMENT_EXPR:
4095 case PREDECREMENT_EXPR:
4096 if (parser->constant_expression_p)
4098 if (!parser->allow_non_constant_expression_p)
4099 return cp_parser_non_constant_expression (PREINCREMENT_EXPR
4102 parser->non_constant_expression_p = true;
4107 case TRUTH_NOT_EXPR:
4108 return finish_unary_op_expr (unary_operator, cast_expression);
4112 return error_mark_node;
4116 return cp_parser_postfix_expression (parser, address_p);
4119 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
4120 unary-operator, the corresponding tree code is returned. */
4122 static enum tree_code
4123 cp_parser_unary_operator (cp_token* token)
4125 switch (token->type)
4128 return INDIRECT_REF;
4134 return CONVERT_EXPR;
4140 return TRUTH_NOT_EXPR;
4143 return BIT_NOT_EXPR;
4150 /* Parse a new-expression.
4153 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
4154 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
4156 Returns a representation of the expression. */
4159 cp_parser_new_expression (cp_parser* parser)
4161 bool global_scope_p;
4166 /* Look for the optional `::' operator. */
4168 = (cp_parser_global_scope_opt (parser,
4169 /*current_scope_valid_p=*/false)
4171 /* Look for the `new' operator. */
4172 cp_parser_require_keyword (parser, RID_NEW, "`new'");
4173 /* There's no easy way to tell a new-placement from the
4174 `( type-id )' construct. */
4175 cp_parser_parse_tentatively (parser);
4176 /* Look for a new-placement. */
4177 placement = cp_parser_new_placement (parser);
4178 /* If that didn't work out, there's no new-placement. */
4179 if (!cp_parser_parse_definitely (parser))
4180 placement = NULL_TREE;
4182 /* If the next token is a `(', then we have a parenthesized
4184 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4186 /* Consume the `('. */
4187 cp_lexer_consume_token (parser->lexer);
4188 /* Parse the type-id. */
4189 type = cp_parser_type_id (parser);
4190 /* Look for the closing `)'. */
4191 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4193 /* Otherwise, there must be a new-type-id. */
4195 type = cp_parser_new_type_id (parser);
4197 /* If the next token is a `(', then we have a new-initializer. */
4198 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4199 initializer = cp_parser_new_initializer (parser);
4201 initializer = NULL_TREE;
4203 /* Create a representation of the new-expression. */
4204 return build_new (placement, type, initializer, global_scope_p);
4207 /* Parse a new-placement.
4212 Returns the same representation as for an expression-list. */
4215 cp_parser_new_placement (cp_parser* parser)
4217 tree expression_list;
4219 /* Parse the expression-list. */
4220 expression_list = (cp_parser_parenthesized_expression_list
4221 (parser, false, /*non_constant_p=*/NULL));
4223 return expression_list;
4226 /* Parse a new-type-id.
4229 type-specifier-seq new-declarator [opt]
4231 Returns a TREE_LIST whose TREE_PURPOSE is the type-specifier-seq,
4232 and whose TREE_VALUE is the new-declarator. */
4235 cp_parser_new_type_id (cp_parser* parser)
4237 tree type_specifier_seq;
4239 const char *saved_message;
4241 /* The type-specifier sequence must not contain type definitions.
4242 (It cannot contain declarations of new types either, but if they
4243 are not definitions we will catch that because they are not
4245 saved_message = parser->type_definition_forbidden_message;
4246 parser->type_definition_forbidden_message
4247 = "types may not be defined in a new-type-id";
4248 /* Parse the type-specifier-seq. */
4249 type_specifier_seq = cp_parser_type_specifier_seq (parser);
4250 /* Restore the old message. */
4251 parser->type_definition_forbidden_message = saved_message;
4252 /* Parse the new-declarator. */
4253 declarator = cp_parser_new_declarator_opt (parser);
4255 return build_tree_list (type_specifier_seq, declarator);
4258 /* Parse an (optional) new-declarator.
4261 ptr-operator new-declarator [opt]
4262 direct-new-declarator
4264 Returns a representation of the declarator. See
4265 cp_parser_declarator for the representations used. */
4268 cp_parser_new_declarator_opt (cp_parser* parser)
4270 enum tree_code code;
4272 tree cv_qualifier_seq;
4274 /* We don't know if there's a ptr-operator next, or not. */
4275 cp_parser_parse_tentatively (parser);
4276 /* Look for a ptr-operator. */
4277 code = cp_parser_ptr_operator (parser, &type, &cv_qualifier_seq);
4278 /* If that worked, look for more new-declarators. */
4279 if (cp_parser_parse_definitely (parser))
4283 /* Parse another optional declarator. */
4284 declarator = cp_parser_new_declarator_opt (parser);
4286 /* Create the representation of the declarator. */
4287 if (code == INDIRECT_REF)
4288 declarator = make_pointer_declarator (cv_qualifier_seq,
4291 declarator = make_reference_declarator (cv_qualifier_seq,
4294 /* Handle the pointer-to-member case. */
4296 declarator = build_nt (SCOPE_REF, type, declarator);
4301 /* If the next token is a `[', there is a direct-new-declarator. */
4302 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
4303 return cp_parser_direct_new_declarator (parser);
4308 /* Parse a direct-new-declarator.
4310 direct-new-declarator:
4312 direct-new-declarator [constant-expression]
4314 Returns an ARRAY_REF, following the same conventions as are
4315 documented for cp_parser_direct_declarator. */
4318 cp_parser_direct_new_declarator (cp_parser* parser)
4320 tree declarator = NULL_TREE;
4326 /* Look for the opening `['. */
4327 cp_parser_require (parser, CPP_OPEN_SQUARE, "`['");
4328 /* The first expression is not required to be constant. */
4331 expression = cp_parser_expression (parser);
4332 /* The standard requires that the expression have integral
4333 type. DR 74 adds enumeration types. We believe that the
4334 real intent is that these expressions be handled like the
4335 expression in a `switch' condition, which also allows
4336 classes with a single conversion to integral or
4337 enumeration type. */
4338 if (!processing_template_decl)
4341 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
4346 error ("expression in new-declarator must have integral or enumeration type");
4347 expression = error_mark_node;
4351 /* But all the other expressions must be. */
4354 = cp_parser_constant_expression (parser,
4355 /*allow_non_constant=*/false,
4357 /* Look for the closing `]'. */
4358 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4360 /* Add this bound to the declarator. */
4361 declarator = build_nt (ARRAY_REF, declarator, expression);
4363 /* If the next token is not a `[', then there are no more
4365 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
4372 /* Parse a new-initializer.
4375 ( expression-list [opt] )
4377 Returns a representation of the expression-list. If there is no
4378 expression-list, VOID_ZERO_NODE is returned. */
4381 cp_parser_new_initializer (cp_parser* parser)
4383 tree expression_list;
4385 expression_list = (cp_parser_parenthesized_expression_list
4386 (parser, false, /*non_constant_p=*/NULL));
4387 if (!expression_list)
4388 expression_list = void_zero_node;
4390 return expression_list;
4393 /* Parse a delete-expression.
4396 :: [opt] delete cast-expression
4397 :: [opt] delete [ ] cast-expression
4399 Returns a representation of the expression. */
4402 cp_parser_delete_expression (cp_parser* parser)
4404 bool global_scope_p;
4408 /* Look for the optional `::' operator. */
4410 = (cp_parser_global_scope_opt (parser,
4411 /*current_scope_valid_p=*/false)
4413 /* Look for the `delete' keyword. */
4414 cp_parser_require_keyword (parser, RID_DELETE, "`delete'");
4415 /* See if the array syntax is in use. */
4416 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
4418 /* Consume the `[' token. */
4419 cp_lexer_consume_token (parser->lexer);
4420 /* Look for the `]' token. */
4421 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4422 /* Remember that this is the `[]' construct. */
4428 /* Parse the cast-expression. */
4429 expression = cp_parser_simple_cast_expression (parser);
4431 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
4434 /* Parse a cast-expression.
4438 ( type-id ) cast-expression
4440 Returns a representation of the expression. */
4443 cp_parser_cast_expression (cp_parser *parser, bool address_p)
4445 /* If it's a `(', then we might be looking at a cast. */
4446 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4448 tree type = NULL_TREE;
4449 tree expr = NULL_TREE;
4450 bool compound_literal_p;
4451 const char *saved_message;
4453 /* There's no way to know yet whether or not this is a cast.
4454 For example, `(int (3))' is a unary-expression, while `(int)
4455 3' is a cast. So, we resort to parsing tentatively. */
4456 cp_parser_parse_tentatively (parser);
4457 /* Types may not be defined in a cast. */
4458 saved_message = parser->type_definition_forbidden_message;
4459 parser->type_definition_forbidden_message
4460 = "types may not be defined in casts";
4461 /* Consume the `('. */
4462 cp_lexer_consume_token (parser->lexer);
4463 /* A very tricky bit is that `(struct S) { 3 }' is a
4464 compound-literal (which we permit in C++ as an extension).
4465 But, that construct is not a cast-expression -- it is a
4466 postfix-expression. (The reason is that `(struct S) { 3 }.i'
4467 is legal; if the compound-literal were a cast-expression,
4468 you'd need an extra set of parentheses.) But, if we parse
4469 the type-id, and it happens to be a class-specifier, then we
4470 will commit to the parse at that point, because we cannot
4471 undo the action that is done when creating a new class. So,
4472 then we cannot back up and do a postfix-expression.
4474 Therefore, we scan ahead to the closing `)', and check to see
4475 if the token after the `)' is a `{'. If so, we are not
4476 looking at a cast-expression.
4478 Save tokens so that we can put them back. */
4479 cp_lexer_save_tokens (parser->lexer);
4480 /* Skip tokens until the next token is a closing parenthesis.
4481 If we find the closing `)', and the next token is a `{', then
4482 we are looking at a compound-literal. */
4484 = (cp_parser_skip_to_closing_parenthesis (parser, false, false)
4485 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
4486 /* Roll back the tokens we skipped. */
4487 cp_lexer_rollback_tokens (parser->lexer);
4488 /* If we were looking at a compound-literal, simulate an error
4489 so that the call to cp_parser_parse_definitely below will
4491 if (compound_literal_p)
4492 cp_parser_simulate_error (parser);
4495 /* Look for the type-id. */
4496 type = cp_parser_type_id (parser);
4497 /* Look for the closing `)'. */
4498 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4501 /* Restore the saved message. */
4502 parser->type_definition_forbidden_message = saved_message;
4504 /* If ok so far, parse the dependent expression. We cannot be
4505 sure it is a cast. Consider `(T ())'. It is a parenthesized
4506 ctor of T, but looks like a cast to function returning T
4507 without a dependent expression. */
4508 if (!cp_parser_error_occurred (parser))
4509 expr = cp_parser_simple_cast_expression (parser);
4511 if (cp_parser_parse_definitely (parser))
4513 /* Warn about old-style casts, if so requested. */
4514 if (warn_old_style_cast
4515 && !in_system_header
4516 && !VOID_TYPE_P (type)
4517 && current_lang_name != lang_name_c)
4518 warning ("use of old-style cast");
4520 /* Only type conversions to integral or enumeration types
4521 can be used in constant-expressions. */
4522 if (parser->constant_expression_p
4523 && !dependent_type_p (type)
4524 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type))
4526 if (!parser->allow_non_constant_expression_p)
4527 return (cp_parser_non_constant_expression
4528 ("a casts to a type other than an integral or "
4529 "enumeration type"));
4530 parser->non_constant_expression_p = true;
4532 /* Perform the cast. */
4533 expr = build_c_cast (type, expr);
4538 /* If we get here, then it's not a cast, so it must be a
4539 unary-expression. */
4540 return cp_parser_unary_expression (parser, address_p);
4543 /* Parse a pm-expression.
4547 pm-expression .* cast-expression
4548 pm-expression ->* cast-expression
4550 Returns a representation of the expression. */
4553 cp_parser_pm_expression (cp_parser* parser)
4555 static const cp_parser_token_tree_map map = {
4556 { CPP_DEREF_STAR, MEMBER_REF },
4557 { CPP_DOT_STAR, DOTSTAR_EXPR },
4558 { CPP_EOF, ERROR_MARK }
4561 return cp_parser_binary_expression (parser, map,
4562 cp_parser_simple_cast_expression);
4565 /* Parse a multiplicative-expression.
4567 mulitplicative-expression:
4569 multiplicative-expression * pm-expression
4570 multiplicative-expression / pm-expression
4571 multiplicative-expression % pm-expression
4573 Returns a representation of the expression. */
4576 cp_parser_multiplicative_expression (cp_parser* parser)
4578 static const cp_parser_token_tree_map map = {
4579 { CPP_MULT, MULT_EXPR },
4580 { CPP_DIV, TRUNC_DIV_EXPR },
4581 { CPP_MOD, TRUNC_MOD_EXPR },
4582 { CPP_EOF, ERROR_MARK }
4585 return cp_parser_binary_expression (parser,
4587 cp_parser_pm_expression);
4590 /* Parse an additive-expression.
4592 additive-expression:
4593 multiplicative-expression
4594 additive-expression + multiplicative-expression
4595 additive-expression - multiplicative-expression
4597 Returns a representation of the expression. */
4600 cp_parser_additive_expression (cp_parser* parser)
4602 static const cp_parser_token_tree_map map = {
4603 { CPP_PLUS, PLUS_EXPR },
4604 { CPP_MINUS, MINUS_EXPR },
4605 { CPP_EOF, ERROR_MARK }
4608 return cp_parser_binary_expression (parser,
4610 cp_parser_multiplicative_expression);
4613 /* Parse a shift-expression.
4617 shift-expression << additive-expression
4618 shift-expression >> additive-expression
4620 Returns a representation of the expression. */
4623 cp_parser_shift_expression (cp_parser* parser)
4625 static const cp_parser_token_tree_map map = {
4626 { CPP_LSHIFT, LSHIFT_EXPR },
4627 { CPP_RSHIFT, RSHIFT_EXPR },
4628 { CPP_EOF, ERROR_MARK }
4631 return cp_parser_binary_expression (parser,
4633 cp_parser_additive_expression);
4636 /* Parse a relational-expression.
4638 relational-expression:
4640 relational-expression < shift-expression
4641 relational-expression > shift-expression
4642 relational-expression <= shift-expression
4643 relational-expression >= shift-expression
4647 relational-expression:
4648 relational-expression <? shift-expression
4649 relational-expression >? shift-expression
4651 Returns a representation of the expression. */
4654 cp_parser_relational_expression (cp_parser* parser)
4656 static const cp_parser_token_tree_map map = {
4657 { CPP_LESS, LT_EXPR },
4658 { CPP_GREATER, GT_EXPR },
4659 { CPP_LESS_EQ, LE_EXPR },
4660 { CPP_GREATER_EQ, GE_EXPR },
4661 { CPP_MIN, MIN_EXPR },
4662 { CPP_MAX, MAX_EXPR },
4663 { CPP_EOF, ERROR_MARK }
4666 return cp_parser_binary_expression (parser,
4668 cp_parser_shift_expression);
4671 /* Parse an equality-expression.
4673 equality-expression:
4674 relational-expression
4675 equality-expression == relational-expression
4676 equality-expression != relational-expression
4678 Returns a representation of the expression. */
4681 cp_parser_equality_expression (cp_parser* parser)
4683 static const cp_parser_token_tree_map map = {
4684 { CPP_EQ_EQ, EQ_EXPR },
4685 { CPP_NOT_EQ, NE_EXPR },
4686 { CPP_EOF, ERROR_MARK }
4689 return cp_parser_binary_expression (parser,
4691 cp_parser_relational_expression);
4694 /* Parse an and-expression.
4698 and-expression & equality-expression
4700 Returns a representation of the expression. */
4703 cp_parser_and_expression (cp_parser* parser)
4705 static const cp_parser_token_tree_map map = {
4706 { CPP_AND, BIT_AND_EXPR },
4707 { CPP_EOF, ERROR_MARK }
4710 return cp_parser_binary_expression (parser,
4712 cp_parser_equality_expression);
4715 /* Parse an exclusive-or-expression.
4717 exclusive-or-expression:
4719 exclusive-or-expression ^ and-expression
4721 Returns a representation of the expression. */
4724 cp_parser_exclusive_or_expression (cp_parser* parser)
4726 static const cp_parser_token_tree_map map = {
4727 { CPP_XOR, BIT_XOR_EXPR },
4728 { CPP_EOF, ERROR_MARK }
4731 return cp_parser_binary_expression (parser,
4733 cp_parser_and_expression);
4737 /* Parse an inclusive-or-expression.
4739 inclusive-or-expression:
4740 exclusive-or-expression
4741 inclusive-or-expression | exclusive-or-expression
4743 Returns a representation of the expression. */
4746 cp_parser_inclusive_or_expression (cp_parser* parser)
4748 static const cp_parser_token_tree_map map = {
4749 { CPP_OR, BIT_IOR_EXPR },
4750 { CPP_EOF, ERROR_MARK }
4753 return cp_parser_binary_expression (parser,
4755 cp_parser_exclusive_or_expression);
4758 /* Parse a logical-and-expression.
4760 logical-and-expression:
4761 inclusive-or-expression
4762 logical-and-expression && inclusive-or-expression
4764 Returns a representation of the expression. */
4767 cp_parser_logical_and_expression (cp_parser* parser)
4769 static const cp_parser_token_tree_map map = {
4770 { CPP_AND_AND, TRUTH_ANDIF_EXPR },
4771 { CPP_EOF, ERROR_MARK }
4774 return cp_parser_binary_expression (parser,
4776 cp_parser_inclusive_or_expression);
4779 /* Parse a logical-or-expression.
4781 logical-or-expression:
4782 logical-and-expression
4783 logical-or-expression || logical-and-expression
4785 Returns a representation of the expression. */
4788 cp_parser_logical_or_expression (cp_parser* parser)
4790 static const cp_parser_token_tree_map map = {
4791 { CPP_OR_OR, TRUTH_ORIF_EXPR },
4792 { CPP_EOF, ERROR_MARK }
4795 return cp_parser_binary_expression (parser,
4797 cp_parser_logical_and_expression);
4800 /* Parse the `? expression : assignment-expression' part of a
4801 conditional-expression. The LOGICAL_OR_EXPR is the
4802 logical-or-expression that started the conditional-expression.
4803 Returns a representation of the entire conditional-expression.
4805 This routine is used by cp_parser_assignment_expression.
4807 ? expression : assignment-expression
4811 ? : assignment-expression */
4814 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
4817 tree assignment_expr;
4819 /* Consume the `?' token. */
4820 cp_lexer_consume_token (parser->lexer);
4821 if (cp_parser_allow_gnu_extensions_p (parser)
4822 && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
4823 /* Implicit true clause. */
4826 /* Parse the expression. */
4827 expr = cp_parser_expression (parser);
4829 /* The next token should be a `:'. */
4830 cp_parser_require (parser, CPP_COLON, "`:'");
4831 /* Parse the assignment-expression. */
4832 assignment_expr = cp_parser_assignment_expression (parser);
4834 /* Build the conditional-expression. */
4835 return build_x_conditional_expr (logical_or_expr,
4840 /* Parse an assignment-expression.
4842 assignment-expression:
4843 conditional-expression
4844 logical-or-expression assignment-operator assignment_expression
4847 Returns a representation for the expression. */
4850 cp_parser_assignment_expression (cp_parser* parser)
4854 /* If the next token is the `throw' keyword, then we're looking at
4855 a throw-expression. */
4856 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
4857 expr = cp_parser_throw_expression (parser);
4858 /* Otherwise, it must be that we are looking at a
4859 logical-or-expression. */
4862 /* Parse the logical-or-expression. */
4863 expr = cp_parser_logical_or_expression (parser);
4864 /* If the next token is a `?' then we're actually looking at a
4865 conditional-expression. */
4866 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
4867 return cp_parser_question_colon_clause (parser, expr);
4870 enum tree_code assignment_operator;
4872 /* If it's an assignment-operator, we're using the second
4875 = cp_parser_assignment_operator_opt (parser);
4876 if (assignment_operator != ERROR_MARK)
4880 /* Parse the right-hand side of the assignment. */
4881 rhs = cp_parser_assignment_expression (parser);
4882 /* An assignment may not appear in a
4883 constant-expression. */
4884 if (parser->constant_expression_p)
4886 if (!parser->allow_non_constant_expression_p)
4887 return cp_parser_non_constant_expression ("an assignment");
4888 parser->non_constant_expression_p = true;
4890 /* Build the assignment expression. */
4891 expr = build_x_modify_expr (expr,
4892 assignment_operator,
4901 /* Parse an (optional) assignment-operator.
4903 assignment-operator: one of
4904 = *= /= %= += -= >>= <<= &= ^= |=
4908 assignment-operator: one of
4911 If the next token is an assignment operator, the corresponding tree
4912 code is returned, and the token is consumed. For example, for
4913 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
4914 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
4915 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
4916 operator, ERROR_MARK is returned. */
4918 static enum tree_code
4919 cp_parser_assignment_operator_opt (cp_parser* parser)
4924 /* Peek at the next toen. */
4925 token = cp_lexer_peek_token (parser->lexer);
4927 switch (token->type)
4938 op = TRUNC_DIV_EXPR;
4942 op = TRUNC_MOD_EXPR;
4982 /* Nothing else is an assignment operator. */
4986 /* If it was an assignment operator, consume it. */
4987 if (op != ERROR_MARK)
4988 cp_lexer_consume_token (parser->lexer);
4993 /* Parse an expression.
4996 assignment-expression
4997 expression , assignment-expression
4999 Returns a representation of the expression. */
5002 cp_parser_expression (cp_parser* parser)
5004 tree expression = NULL_TREE;
5008 tree assignment_expression;
5010 /* Parse the next assignment-expression. */
5011 assignment_expression
5012 = cp_parser_assignment_expression (parser);
5013 /* If this is the first assignment-expression, we can just
5016 expression = assignment_expression;
5018 expression = build_x_compound_expr (expression,
5019 assignment_expression);
5020 /* If the next token is not a comma, then we are done with the
5022 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5024 /* Consume the `,'. */
5025 cp_lexer_consume_token (parser->lexer);
5026 /* A comma operator cannot appear in a constant-expression. */
5027 if (parser->constant_expression_p)
5029 if (!parser->allow_non_constant_expression_p)
5031 = cp_parser_non_constant_expression ("a comma operator");
5032 parser->non_constant_expression_p = true;
5039 /* Parse a constant-expression.
5041 constant-expression:
5042 conditional-expression
5044 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
5045 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
5046 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
5047 is false, NON_CONSTANT_P should be NULL. */
5050 cp_parser_constant_expression (cp_parser* parser,
5051 bool allow_non_constant_p,
5052 bool *non_constant_p)
5054 bool saved_constant_expression_p;
5055 bool saved_allow_non_constant_expression_p;
5056 bool saved_non_constant_expression_p;
5059 /* It might seem that we could simply parse the
5060 conditional-expression, and then check to see if it were
5061 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
5062 one that the compiler can figure out is constant, possibly after
5063 doing some simplifications or optimizations. The standard has a
5064 precise definition of constant-expression, and we must honor
5065 that, even though it is somewhat more restrictive.
5071 is not a legal declaration, because `(2, 3)' is not a
5072 constant-expression. The `,' operator is forbidden in a
5073 constant-expression. However, GCC's constant-folding machinery
5074 will fold this operation to an INTEGER_CST for `3'. */
5076 /* Save the old settings. */
5077 saved_constant_expression_p = parser->constant_expression_p;
5078 saved_allow_non_constant_expression_p
5079 = parser->allow_non_constant_expression_p;
5080 saved_non_constant_expression_p = parser->non_constant_expression_p;
5081 /* We are now parsing a constant-expression. */
5082 parser->constant_expression_p = true;
5083 parser->allow_non_constant_expression_p = allow_non_constant_p;
5084 parser->non_constant_expression_p = false;
5085 /* Although the grammar says "conditional-expression", we parse an
5086 "assignment-expression", which also permits "throw-expression"
5087 and the use of assignment operators. In the case that
5088 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
5089 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
5090 actually essential that we look for an assignment-expression.
5091 For example, cp_parser_initializer_clauses uses this function to
5092 determine whether a particular assignment-expression is in fact
5094 expression = cp_parser_assignment_expression (parser);
5095 /* Restore the old settings. */
5096 parser->constant_expression_p = saved_constant_expression_p;
5097 parser->allow_non_constant_expression_p
5098 = saved_allow_non_constant_expression_p;
5099 if (allow_non_constant_p)
5100 *non_constant_p = parser->non_constant_expression_p;
5101 parser->non_constant_expression_p = saved_non_constant_expression_p;
5106 /* Statements [gram.stmt.stmt] */
5108 /* Parse a statement.
5112 expression-statement
5117 declaration-statement
5121 cp_parser_statement (cp_parser* parser, bool in_statement_expr_p)
5125 int statement_line_number;
5127 /* There is no statement yet. */
5128 statement = NULL_TREE;
5129 /* Peek at the next token. */
5130 token = cp_lexer_peek_token (parser->lexer);
5131 /* Remember the line number of the first token in the statement. */
5132 statement_line_number = token->location.line;
5133 /* If this is a keyword, then that will often determine what kind of
5134 statement we have. */
5135 if (token->type == CPP_KEYWORD)
5137 enum rid keyword = token->keyword;
5143 statement = cp_parser_labeled_statement (parser,
5144 in_statement_expr_p);
5149 statement = cp_parser_selection_statement (parser);
5155 statement = cp_parser_iteration_statement (parser);
5162 statement = cp_parser_jump_statement (parser);
5166 statement = cp_parser_try_block (parser);
5170 /* It might be a keyword like `int' that can start a
5171 declaration-statement. */
5175 else if (token->type == CPP_NAME)
5177 /* If the next token is a `:', then we are looking at a
5178 labeled-statement. */
5179 token = cp_lexer_peek_nth_token (parser->lexer, 2);
5180 if (token->type == CPP_COLON)
5181 statement = cp_parser_labeled_statement (parser, in_statement_expr_p);
5183 /* Anything that starts with a `{' must be a compound-statement. */
5184 else if (token->type == CPP_OPEN_BRACE)
5185 statement = cp_parser_compound_statement (parser, false);
5187 /* Everything else must be a declaration-statement or an
5188 expression-statement. Try for the declaration-statement
5189 first, unless we are looking at a `;', in which case we know that
5190 we have an expression-statement. */
5193 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
5195 cp_parser_parse_tentatively (parser);
5196 /* Try to parse the declaration-statement. */
5197 cp_parser_declaration_statement (parser);
5198 /* If that worked, we're done. */
5199 if (cp_parser_parse_definitely (parser))
5202 /* Look for an expression-statement instead. */
5203 statement = cp_parser_expression_statement (parser, in_statement_expr_p);
5206 /* Set the line number for the statement. */
5207 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
5208 STMT_LINENO (statement) = statement_line_number;
5211 /* Parse a labeled-statement.
5214 identifier : statement
5215 case constant-expression : statement
5218 Returns the new CASE_LABEL, for a `case' or `default' label. For
5219 an ordinary label, returns a LABEL_STMT. */
5222 cp_parser_labeled_statement (cp_parser* parser, bool in_statement_expr_p)
5225 tree statement = NULL_TREE;
5227 /* The next token should be an identifier. */
5228 token = cp_lexer_peek_token (parser->lexer);
5229 if (token->type != CPP_NAME
5230 && token->type != CPP_KEYWORD)
5232 cp_parser_error (parser, "expected labeled-statement");
5233 return error_mark_node;
5236 switch (token->keyword)
5242 /* Consume the `case' token. */
5243 cp_lexer_consume_token (parser->lexer);
5244 /* Parse the constant-expression. */
5245 expr = cp_parser_constant_expression (parser,
5246 /*allow_non_constant_p=*/false,
5248 /* Create the label. */
5249 statement = finish_case_label (expr, NULL_TREE);
5254 /* Consume the `default' token. */
5255 cp_lexer_consume_token (parser->lexer);
5256 /* Create the label. */
5257 statement = finish_case_label (NULL_TREE, NULL_TREE);
5261 /* Anything else must be an ordinary label. */
5262 statement = finish_label_stmt (cp_parser_identifier (parser));
5266 /* Require the `:' token. */
5267 cp_parser_require (parser, CPP_COLON, "`:'");
5268 /* Parse the labeled statement. */
5269 cp_parser_statement (parser, in_statement_expr_p);
5271 /* Return the label, in the case of a `case' or `default' label. */
5275 /* Parse an expression-statement.
5277 expression-statement:
5280 Returns the new EXPR_STMT -- or NULL_TREE if the expression
5281 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
5282 indicates whether this expression-statement is part of an
5283 expression statement. */
5286 cp_parser_expression_statement (cp_parser* parser, bool in_statement_expr_p)
5288 tree statement = NULL_TREE;
5290 /* If the next token is a ';', then there is no expression
5292 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
5293 statement = cp_parser_expression (parser);
5295 /* Consume the final `;'. */
5296 cp_parser_consume_semicolon_at_end_of_statement (parser);
5298 if (in_statement_expr_p
5299 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
5301 /* This is the final expression statement of a statement
5303 statement = finish_stmt_expr_expr (statement);
5306 statement = finish_expr_stmt (statement);
5313 /* Parse a compound-statement.
5316 { statement-seq [opt] }
5318 Returns a COMPOUND_STMT representing the statement. */
5321 cp_parser_compound_statement (cp_parser *parser, bool in_statement_expr_p)
5325 /* Consume the `{'. */
5326 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
5327 return error_mark_node;
5328 /* Begin the compound-statement. */
5329 compound_stmt = begin_compound_stmt (/*has_no_scope=*/false);
5330 /* Parse an (optional) statement-seq. */
5331 cp_parser_statement_seq_opt (parser, in_statement_expr_p);
5332 /* Finish the compound-statement. */
5333 finish_compound_stmt (compound_stmt);
5334 /* Consume the `}'. */
5335 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
5337 return compound_stmt;
5340 /* Parse an (optional) statement-seq.
5344 statement-seq [opt] statement */
5347 cp_parser_statement_seq_opt (cp_parser* parser, bool in_statement_expr_p)
5349 /* Scan statements until there aren't any more. */
5352 /* If we're looking at a `}', then we've run out of statements. */
5353 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE)
5354 || cp_lexer_next_token_is (parser->lexer, CPP_EOF))
5357 /* Parse the statement. */
5358 cp_parser_statement (parser, in_statement_expr_p);
5362 /* Parse a selection-statement.
5364 selection-statement:
5365 if ( condition ) statement
5366 if ( condition ) statement else statement
5367 switch ( condition ) statement
5369 Returns the new IF_STMT or SWITCH_STMT. */
5372 cp_parser_selection_statement (cp_parser* parser)
5377 /* Peek at the next token. */
5378 token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
5380 /* See what kind of keyword it is. */
5381 keyword = token->keyword;
5390 /* Look for the `('. */
5391 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
5393 cp_parser_skip_to_end_of_statement (parser);
5394 return error_mark_node;
5397 /* Begin the selection-statement. */
5398 if (keyword == RID_IF)
5399 statement = begin_if_stmt ();
5401 statement = begin_switch_stmt ();
5403 /* Parse the condition. */
5404 condition = cp_parser_condition (parser);
5405 /* Look for the `)'. */
5406 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
5407 cp_parser_skip_to_closing_parenthesis (parser, true, false);
5409 if (keyword == RID_IF)
5413 /* Add the condition. */
5414 finish_if_stmt_cond (condition, statement);
5416 /* Parse the then-clause. */
5417 then_stmt = cp_parser_implicitly_scoped_statement (parser);
5418 finish_then_clause (statement);
5420 /* If the next token is `else', parse the else-clause. */
5421 if (cp_lexer_next_token_is_keyword (parser->lexer,
5426 /* Consume the `else' keyword. */
5427 cp_lexer_consume_token (parser->lexer);
5428 /* Parse the else-clause. */
5430 = cp_parser_implicitly_scoped_statement (parser);
5431 finish_else_clause (statement);
5434 /* Now we're all done with the if-statement. */
5441 /* Add the condition. */
5442 finish_switch_cond (condition, statement);
5444 /* Parse the body of the switch-statement. */
5445 body = cp_parser_implicitly_scoped_statement (parser);
5447 /* Now we're all done with the switch-statement. */
5448 finish_switch_stmt (statement);
5456 cp_parser_error (parser, "expected selection-statement");
5457 return error_mark_node;
5461 /* Parse a condition.
5465 type-specifier-seq declarator = assignment-expression
5470 type-specifier-seq declarator asm-specification [opt]
5471 attributes [opt] = assignment-expression
5473 Returns the expression that should be tested. */
5476 cp_parser_condition (cp_parser* parser)
5478 tree type_specifiers;
5479 const char *saved_message;
5481 /* Try the declaration first. */
5482 cp_parser_parse_tentatively (parser);
5483 /* New types are not allowed in the type-specifier-seq for a
5485 saved_message = parser->type_definition_forbidden_message;
5486 parser->type_definition_forbidden_message
5487 = "types may not be defined in conditions";
5488 /* Parse the type-specifier-seq. */
5489 type_specifiers = cp_parser_type_specifier_seq (parser);
5490 /* Restore the saved message. */
5491 parser->type_definition_forbidden_message = saved_message;
5492 /* If all is well, we might be looking at a declaration. */
5493 if (!cp_parser_error_occurred (parser))
5496 tree asm_specification;
5499 tree initializer = NULL_TREE;
5501 /* Parse the declarator. */
5502 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
5503 /*ctor_dtor_or_conv_p=*/NULL);
5504 /* Parse the attributes. */
5505 attributes = cp_parser_attributes_opt (parser);
5506 /* Parse the asm-specification. */
5507 asm_specification = cp_parser_asm_specification_opt (parser);
5508 /* If the next token is not an `=', then we might still be
5509 looking at an expression. For example:
5513 looks like a decl-specifier-seq and a declarator -- but then
5514 there is no `=', so this is an expression. */
5515 cp_parser_require (parser, CPP_EQ, "`='");
5516 /* If we did see an `=', then we are looking at a declaration
5518 if (cp_parser_parse_definitely (parser))
5520 /* Create the declaration. */
5521 decl = start_decl (declarator, type_specifiers,
5522 /*initialized_p=*/true,
5523 attributes, /*prefix_attributes=*/NULL_TREE);
5524 /* Parse the assignment-expression. */
5525 initializer = cp_parser_assignment_expression (parser);
5527 /* Process the initializer. */
5528 cp_finish_decl (decl,
5531 LOOKUP_ONLYCONVERTING);
5533 return convert_from_reference (decl);
5536 /* If we didn't even get past the declarator successfully, we are
5537 definitely not looking at a declaration. */
5539 cp_parser_abort_tentative_parse (parser);
5541 /* Otherwise, we are looking at an expression. */
5542 return cp_parser_expression (parser);
5545 /* Parse an iteration-statement.
5547 iteration-statement:
5548 while ( condition ) statement
5549 do statement while ( expression ) ;
5550 for ( for-init-statement condition [opt] ; expression [opt] )
5553 Returns the new WHILE_STMT, DO_STMT, or FOR_STMT. */
5556 cp_parser_iteration_statement (cp_parser* parser)
5562 /* Peek at the next token. */
5563 token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
5565 return error_mark_node;
5567 /* See what kind of keyword it is. */
5568 keyword = token->keyword;
5575 /* Begin the while-statement. */
5576 statement = begin_while_stmt ();
5577 /* Look for the `('. */
5578 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
5579 /* Parse the condition. */
5580 condition = cp_parser_condition (parser);
5581 finish_while_stmt_cond (condition, statement);
5582 /* Look for the `)'. */
5583 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5584 /* Parse the dependent statement. */
5585 cp_parser_already_scoped_statement (parser);
5586 /* We're done with the while-statement. */
5587 finish_while_stmt (statement);
5595 /* Begin the do-statement. */
5596 statement = begin_do_stmt ();
5597 /* Parse the body of the do-statement. */
5598 cp_parser_implicitly_scoped_statement (parser);
5599 finish_do_body (statement);
5600 /* Look for the `while' keyword. */
5601 cp_parser_require_keyword (parser, RID_WHILE, "`while'");
5602 /* Look for the `('. */
5603 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
5604 /* Parse the expression. */
5605 expression = cp_parser_expression (parser);
5606 /* We're done with the do-statement. */
5607 finish_do_stmt (expression, statement);
5608 /* Look for the `)'. */
5609 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5610 /* Look for the `;'. */
5611 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
5617 tree condition = NULL_TREE;
5618 tree expression = NULL_TREE;
5620 /* Begin the for-statement. */
5621 statement = begin_for_stmt ();
5622 /* Look for the `('. */
5623 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
5624 /* Parse the initialization. */
5625 cp_parser_for_init_statement (parser);
5626 finish_for_init_stmt (statement);
5628 /* If there's a condition, process it. */
5629 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
5630 condition = cp_parser_condition (parser);
5631 finish_for_cond (condition, statement);
5632 /* Look for the `;'. */
5633 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
5635 /* If there's an expression, process it. */
5636 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
5637 expression = cp_parser_expression (parser);
5638 finish_for_expr (expression, statement);
5639 /* Look for the `)'. */
5640 cp_parser_require (parser, CPP_CLOSE_PAREN, "`;'");
5642 /* Parse the body of the for-statement. */
5643 cp_parser_already_scoped_statement (parser);
5645 /* We're done with the for-statement. */
5646 finish_for_stmt (statement);
5651 cp_parser_error (parser, "expected iteration-statement");
5652 statement = error_mark_node;
5659 /* Parse a for-init-statement.
5662 expression-statement
5663 simple-declaration */
5666 cp_parser_for_init_statement (cp_parser* parser)
5668 /* If the next token is a `;', then we have an empty
5669 expression-statement. Grammatically, this is also a
5670 simple-declaration, but an invalid one, because it does not
5671 declare anything. Therefore, if we did not handle this case
5672 specially, we would issue an error message about an invalid
5674 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
5676 /* We're going to speculatively look for a declaration, falling back
5677 to an expression, if necessary. */
5678 cp_parser_parse_tentatively (parser);
5679 /* Parse the declaration. */
5680 cp_parser_simple_declaration (parser,
5681 /*function_definition_allowed_p=*/false);
5682 /* If the tentative parse failed, then we shall need to look for an
5683 expression-statement. */
5684 if (cp_parser_parse_definitely (parser))
5688 cp_parser_expression_statement (parser, false);
5691 /* Parse a jump-statement.
5696 return expression [opt] ;
5704 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_STMT, or
5708 cp_parser_jump_statement (cp_parser* parser)
5710 tree statement = error_mark_node;
5714 /* Peek at the next token. */
5715 token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
5717 return error_mark_node;
5719 /* See what kind of keyword it is. */
5720 keyword = token->keyword;
5724 statement = finish_break_stmt ();
5725 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
5729 statement = finish_continue_stmt ();
5730 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
5737 /* If the next token is a `;', then there is no
5739 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
5740 expr = cp_parser_expression (parser);
5743 /* Build the return-statement. */
5744 statement = finish_return_stmt (expr);
5745 /* Look for the final `;'. */
5746 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
5751 /* Create the goto-statement. */
5752 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
5754 /* Issue a warning about this use of a GNU extension. */
5756 pedwarn ("ISO C++ forbids computed gotos");
5757 /* Consume the '*' token. */
5758 cp_lexer_consume_token (parser->lexer);
5759 /* Parse the dependent expression. */
5760 finish_goto_stmt (cp_parser_expression (parser));
5763 finish_goto_stmt (cp_parser_identifier (parser));
5764 /* Look for the final `;'. */
5765 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
5769 cp_parser_error (parser, "expected jump-statement");
5776 /* Parse a declaration-statement.
5778 declaration-statement:
5779 block-declaration */
5782 cp_parser_declaration_statement (cp_parser* parser)
5784 /* Parse the block-declaration. */
5785 cp_parser_block_declaration (parser, /*statement_p=*/true);
5787 /* Finish off the statement. */
5791 /* Some dependent statements (like `if (cond) statement'), are
5792 implicitly in their own scope. In other words, if the statement is
5793 a single statement (as opposed to a compound-statement), it is
5794 none-the-less treated as if it were enclosed in braces. Any
5795 declarations appearing in the dependent statement are out of scope
5796 after control passes that point. This function parses a statement,
5797 but ensures that is in its own scope, even if it is not a
5800 Returns the new statement. */
5803 cp_parser_implicitly_scoped_statement (cp_parser* parser)
5807 /* If the token is not a `{', then we must take special action. */
5808 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
5810 /* Create a compound-statement. */
5811 statement = begin_compound_stmt (/*has_no_scope=*/false);
5812 /* Parse the dependent-statement. */
5813 cp_parser_statement (parser, false);
5814 /* Finish the dummy compound-statement. */
5815 finish_compound_stmt (statement);
5817 /* Otherwise, we simply parse the statement directly. */
5819 statement = cp_parser_compound_statement (parser, false);
5821 /* Return the statement. */
5825 /* For some dependent statements (like `while (cond) statement'), we
5826 have already created a scope. Therefore, even if the dependent
5827 statement is a compound-statement, we do not want to create another
5831 cp_parser_already_scoped_statement (cp_parser* parser)
5833 /* If the token is not a `{', then we must take special action. */
5834 if (cp_lexer_next_token_is_not(parser->lexer, CPP_OPEN_BRACE))
5838 /* Create a compound-statement. */
5839 statement = begin_compound_stmt (/*has_no_scope=*/true);
5840 /* Parse the dependent-statement. */
5841 cp_parser_statement (parser, false);
5842 /* Finish the dummy compound-statement. */
5843 finish_compound_stmt (statement);
5845 /* Otherwise, we simply parse the statement directly. */
5847 cp_parser_statement (parser, false);
5850 /* Declarations [gram.dcl.dcl] */
5852 /* Parse an optional declaration-sequence.
5856 declaration-seq declaration */
5859 cp_parser_declaration_seq_opt (cp_parser* parser)
5865 token = cp_lexer_peek_token (parser->lexer);
5867 if (token->type == CPP_CLOSE_BRACE
5868 || token->type == CPP_EOF)
5871 if (token->type == CPP_SEMICOLON)
5873 /* A declaration consisting of a single semicolon is
5874 invalid. Allow it unless we're being pedantic. */
5876 pedwarn ("extra `;'");
5877 cp_lexer_consume_token (parser->lexer);
5881 /* The C lexer modifies PENDING_LANG_CHANGE when it wants the
5882 parser to enter or exit implicit `extern "C"' blocks. */
5883 while (pending_lang_change > 0)
5885 push_lang_context (lang_name_c);
5886 --pending_lang_change;
5888 while (pending_lang_change < 0)
5890 pop_lang_context ();
5891 ++pending_lang_change;
5894 /* Parse the declaration itself. */
5895 cp_parser_declaration (parser);
5899 /* Parse a declaration.
5904 template-declaration
5905 explicit-instantiation
5906 explicit-specialization
5907 linkage-specification
5908 namespace-definition
5913 __extension__ declaration */
5916 cp_parser_declaration (cp_parser* parser)
5922 /* Check for the `__extension__' keyword. */
5923 if (cp_parser_extension_opt (parser, &saved_pedantic))
5925 /* Parse the qualified declaration. */
5926 cp_parser_declaration (parser);
5927 /* Restore the PEDANTIC flag. */
5928 pedantic = saved_pedantic;
5933 /* Try to figure out what kind of declaration is present. */
5934 token1 = *cp_lexer_peek_token (parser->lexer);
5935 if (token1.type != CPP_EOF)
5936 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
5938 /* If the next token is `extern' and the following token is a string
5939 literal, then we have a linkage specification. */
5940 if (token1.keyword == RID_EXTERN
5941 && cp_parser_is_string_literal (&token2))
5942 cp_parser_linkage_specification (parser);
5943 /* If the next token is `template', then we have either a template
5944 declaration, an explicit instantiation, or an explicit
5946 else if (token1.keyword == RID_TEMPLATE)
5948 /* `template <>' indicates a template specialization. */
5949 if (token2.type == CPP_LESS
5950 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
5951 cp_parser_explicit_specialization (parser);
5952 /* `template <' indicates a template declaration. */
5953 else if (token2.type == CPP_LESS)
5954 cp_parser_template_declaration (parser, /*member_p=*/false);
5955 /* Anything else must be an explicit instantiation. */
5957 cp_parser_explicit_instantiation (parser);
5959 /* If the next token is `export', then we have a template
5961 else if (token1.keyword == RID_EXPORT)
5962 cp_parser_template_declaration (parser, /*member_p=*/false);
5963 /* If the next token is `extern', 'static' or 'inline' and the one
5964 after that is `template', we have a GNU extended explicit
5965 instantiation directive. */
5966 else if (cp_parser_allow_gnu_extensions_p (parser)
5967 && (token1.keyword == RID_EXTERN
5968 || token1.keyword == RID_STATIC
5969 || token1.keyword == RID_INLINE)
5970 && token2.keyword == RID_TEMPLATE)
5971 cp_parser_explicit_instantiation (parser);
5972 /* If the next token is `namespace', check for a named or unnamed
5973 namespace definition. */
5974 else if (token1.keyword == RID_NAMESPACE
5975 && (/* A named namespace definition. */
5976 (token2.type == CPP_NAME
5977 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
5979 /* An unnamed namespace definition. */
5980 || token2.type == CPP_OPEN_BRACE))
5981 cp_parser_namespace_definition (parser);
5982 /* We must have either a block declaration or a function
5985 /* Try to parse a block-declaration, or a function-definition. */
5986 cp_parser_block_declaration (parser, /*statement_p=*/false);
5989 /* Parse a block-declaration.
5994 namespace-alias-definition
6001 __extension__ block-declaration
6004 If STATEMENT_P is TRUE, then this block-declaration is occurring as
6005 part of a declaration-statement. */
6008 cp_parser_block_declaration (cp_parser *parser,
6014 /* Check for the `__extension__' keyword. */
6015 if (cp_parser_extension_opt (parser, &saved_pedantic))
6017 /* Parse the qualified declaration. */
6018 cp_parser_block_declaration (parser, statement_p);
6019 /* Restore the PEDANTIC flag. */
6020 pedantic = saved_pedantic;
6025 /* Peek at the next token to figure out which kind of declaration is
6027 token1 = cp_lexer_peek_token (parser->lexer);
6029 /* If the next keyword is `asm', we have an asm-definition. */
6030 if (token1->keyword == RID_ASM)
6033 cp_parser_commit_to_tentative_parse (parser);
6034 cp_parser_asm_definition (parser);
6036 /* If the next keyword is `namespace', we have a
6037 namespace-alias-definition. */
6038 else if (token1->keyword == RID_NAMESPACE)
6039 cp_parser_namespace_alias_definition (parser);
6040 /* If the next keyword is `using', we have either a
6041 using-declaration or a using-directive. */
6042 else if (token1->keyword == RID_USING)
6047 cp_parser_commit_to_tentative_parse (parser);
6048 /* If the token after `using' is `namespace', then we have a
6050 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
6051 if (token2->keyword == RID_NAMESPACE)
6052 cp_parser_using_directive (parser);
6053 /* Otherwise, it's a using-declaration. */
6055 cp_parser_using_declaration (parser);
6057 /* If the next keyword is `__label__' we have a label declaration. */
6058 else if (token1->keyword == RID_LABEL)
6061 cp_parser_commit_to_tentative_parse (parser);
6062 cp_parser_label_declaration (parser);
6064 /* Anything else must be a simple-declaration. */
6066 cp_parser_simple_declaration (parser, !statement_p);
6069 /* Parse a simple-declaration.
6072 decl-specifier-seq [opt] init-declarator-list [opt] ;
6074 init-declarator-list:
6076 init-declarator-list , init-declarator
6078 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
6079 function-definition as a simple-declaration. */
6082 cp_parser_simple_declaration (cp_parser* parser,
6083 bool function_definition_allowed_p)
6085 tree decl_specifiers;
6087 int declares_class_or_enum;
6088 bool saw_declarator;
6090 /* Defer access checks until we know what is being declared; the
6091 checks for names appearing in the decl-specifier-seq should be
6092 done as if we were in the scope of the thing being declared. */
6093 push_deferring_access_checks (dk_deferred);
6095 /* Parse the decl-specifier-seq. We have to keep track of whether
6096 or not the decl-specifier-seq declares a named class or
6097 enumeration type, since that is the only case in which the
6098 init-declarator-list is allowed to be empty.
6102 In a simple-declaration, the optional init-declarator-list can be
6103 omitted only when declaring a class or enumeration, that is when
6104 the decl-specifier-seq contains either a class-specifier, an
6105 elaborated-type-specifier, or an enum-specifier. */
6107 = cp_parser_decl_specifier_seq (parser,
6108 CP_PARSER_FLAGS_OPTIONAL,
6110 &declares_class_or_enum);
6111 /* We no longer need to defer access checks. */
6112 stop_deferring_access_checks ();
6114 /* In a block scope, a valid declaration must always have a
6115 decl-specifier-seq. By not trying to parse declarators, we can
6116 resolve the declaration/expression ambiguity more quickly. */
6117 if (!function_definition_allowed_p && !decl_specifiers)
6119 cp_parser_error (parser, "expected declaration");
6123 /* If the next two tokens are both identifiers, the code is
6124 erroneous. The usual cause of this situation is code like:
6128 where "T" should name a type -- but does not. */
6129 if (cp_parser_diagnose_invalid_type_name (parser))
6131 /* If parsing tentatively, we should commit; we really are
6132 looking at a declaration. */
6133 cp_parser_commit_to_tentative_parse (parser);
6138 /* Keep going until we hit the `;' at the end of the simple
6140 saw_declarator = false;
6141 while (cp_lexer_next_token_is_not (parser->lexer,
6145 bool function_definition_p;
6148 saw_declarator = true;
6149 /* Parse the init-declarator. */
6150 decl = cp_parser_init_declarator (parser, decl_specifiers, attributes,
6151 function_definition_allowed_p,
6153 declares_class_or_enum,
6154 &function_definition_p);
6155 /* If an error occurred while parsing tentatively, exit quickly.
6156 (That usually happens when in the body of a function; each
6157 statement is treated as a declaration-statement until proven
6159 if (cp_parser_error_occurred (parser))
6161 /* Handle function definitions specially. */
6162 if (function_definition_p)
6164 /* If the next token is a `,', then we are probably
6165 processing something like:
6169 which is erroneous. */
6170 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
6171 error ("mixing declarations and function-definitions is forbidden");
6172 /* Otherwise, we're done with the list of declarators. */
6175 pop_deferring_access_checks ();
6179 /* The next token should be either a `,' or a `;'. */
6180 token = cp_lexer_peek_token (parser->lexer);
6181 /* If it's a `,', there are more declarators to come. */
6182 if (token->type == CPP_COMMA)
6183 cp_lexer_consume_token (parser->lexer);
6184 /* If it's a `;', we are done. */
6185 else if (token->type == CPP_SEMICOLON)
6187 /* Anything else is an error. */
6190 cp_parser_error (parser, "expected `,' or `;'");
6191 /* Skip tokens until we reach the end of the statement. */
6192 cp_parser_skip_to_end_of_statement (parser);
6195 /* After the first time around, a function-definition is not
6196 allowed -- even if it was OK at first. For example:
6201 function_definition_allowed_p = false;
6204 /* Issue an error message if no declarators are present, and the
6205 decl-specifier-seq does not itself declare a class or
6207 if (!saw_declarator)
6209 if (cp_parser_declares_only_class_p (parser))
6210 shadow_tag (decl_specifiers);
6211 /* Perform any deferred access checks. */
6212 perform_deferred_access_checks ();
6215 /* Consume the `;'. */
6216 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6219 pop_deferring_access_checks ();
6222 /* Parse a decl-specifier-seq.
6225 decl-specifier-seq [opt] decl-specifier
6228 storage-class-specifier
6237 decl-specifier-seq [opt] attributes
6239 Returns a TREE_LIST, giving the decl-specifiers in the order they
6240 appear in the source code. The TREE_VALUE of each node is the
6241 decl-specifier. For a keyword (such as `auto' or `friend'), the
6242 TREE_VALUE is simply the corresponding TREE_IDENTIFIER. For the
6243 representation of a type-specifier, see cp_parser_type_specifier.
6245 If there are attributes, they will be stored in *ATTRIBUTES,
6246 represented as described above cp_parser_attributes.
6248 If FRIEND_IS_NOT_CLASS_P is non-NULL, and the `friend' specifier
6249 appears, and the entity that will be a friend is not going to be a
6250 class, then *FRIEND_IS_NOT_CLASS_P will be set to TRUE. Note that
6251 even if *FRIEND_IS_NOT_CLASS_P is FALSE, the entity to which
6252 friendship is granted might not be a class.
6254 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
6257 1: one of the decl-specifiers is an elaborated-type-specifier
6258 2: one of the decl-specifiers is an enum-specifier or a
6264 cp_parser_decl_specifier_seq (cp_parser* parser,
6265 cp_parser_flags flags,
6267 int* declares_class_or_enum)
6269 tree decl_specs = NULL_TREE;
6270 bool friend_p = false;
6271 bool constructor_possible_p = !parser->in_declarator_p;
6273 /* Assume no class or enumeration type is declared. */
6274 *declares_class_or_enum = 0;
6276 /* Assume there are no attributes. */
6277 *attributes = NULL_TREE;
6279 /* Keep reading specifiers until there are no more to read. */
6282 tree decl_spec = NULL_TREE;
6286 /* Peek at the next token. */
6287 token = cp_lexer_peek_token (parser->lexer);
6288 /* Handle attributes. */
6289 if (token->keyword == RID_ATTRIBUTE)
6291 /* Parse the attributes. */
6292 decl_spec = cp_parser_attributes_opt (parser);
6293 /* Add them to the list. */
6294 *attributes = chainon (*attributes, decl_spec);
6297 /* If the next token is an appropriate keyword, we can simply
6298 add it to the list. */
6299 switch (token->keyword)
6305 error ("duplicate `friend'");
6308 /* The representation of the specifier is simply the
6309 appropriate TREE_IDENTIFIER node. */
6310 decl_spec = token->value;
6311 /* Consume the token. */
6312 cp_lexer_consume_token (parser->lexer);
6315 /* function-specifier:
6322 decl_spec = cp_parser_function_specifier_opt (parser);
6328 /* The representation of the specifier is simply the
6329 appropriate TREE_IDENTIFIER node. */
6330 decl_spec = token->value;
6331 /* Consume the token. */
6332 cp_lexer_consume_token (parser->lexer);
6333 /* A constructor declarator cannot appear in a typedef. */
6334 constructor_possible_p = false;
6335 /* The "typedef" keyword can only occur in a declaration; we
6336 may as well commit at this point. */
6337 cp_parser_commit_to_tentative_parse (parser);
6340 /* storage-class-specifier:
6355 decl_spec = cp_parser_storage_class_specifier_opt (parser);
6362 /* Constructors are a special case. The `S' in `S()' is not a
6363 decl-specifier; it is the beginning of the declarator. */
6364 constructor_p = (!decl_spec
6365 && constructor_possible_p
6366 && cp_parser_constructor_declarator_p (parser,
6369 /* If we don't have a DECL_SPEC yet, then we must be looking at
6370 a type-specifier. */
6371 if (!decl_spec && !constructor_p)
6373 int decl_spec_declares_class_or_enum;
6374 bool is_cv_qualifier;
6377 = cp_parser_type_specifier (parser, flags,
6379 /*is_declaration=*/true,
6380 &decl_spec_declares_class_or_enum,
6383 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
6385 /* If this type-specifier referenced a user-defined type
6386 (a typedef, class-name, etc.), then we can't allow any
6387 more such type-specifiers henceforth.
6391 The longest sequence of decl-specifiers that could
6392 possibly be a type name is taken as the
6393 decl-specifier-seq of a declaration. The sequence shall
6394 be self-consistent as described below.
6398 As a general rule, at most one type-specifier is allowed
6399 in the complete decl-specifier-seq of a declaration. The
6400 only exceptions are the following:
6402 -- const or volatile can be combined with any other
6405 -- signed or unsigned can be combined with char, long,
6413 void g (const int Pc);
6415 Here, Pc is *not* part of the decl-specifier seq; it's
6416 the declarator. Therefore, once we see a type-specifier
6417 (other than a cv-qualifier), we forbid any additional
6418 user-defined types. We *do* still allow things like `int
6419 int' to be considered a decl-specifier-seq, and issue the
6420 error message later. */
6421 if (decl_spec && !is_cv_qualifier)
6422 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
6423 /* A constructor declarator cannot follow a type-specifier. */
6425 constructor_possible_p = false;
6428 /* If we still do not have a DECL_SPEC, then there are no more
6432 /* Issue an error message, unless the entire construct was
6434 if (!(flags & CP_PARSER_FLAGS_OPTIONAL))
6436 cp_parser_error (parser, "expected decl specifier");
6437 return error_mark_node;
6443 /* Add the DECL_SPEC to the list of specifiers. */
6444 decl_specs = tree_cons (NULL_TREE, decl_spec, decl_specs);
6446 /* After we see one decl-specifier, further decl-specifiers are
6448 flags |= CP_PARSER_FLAGS_OPTIONAL;
6451 /* We have built up the DECL_SPECS in reverse order. Return them in
6452 the correct order. */
6453 return nreverse (decl_specs);
6456 /* Parse an (optional) storage-class-specifier.
6458 storage-class-specifier:
6467 storage-class-specifier:
6470 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
6473 cp_parser_storage_class_specifier_opt (cp_parser* parser)
6475 switch (cp_lexer_peek_token (parser->lexer)->keyword)
6483 /* Consume the token. */
6484 return cp_lexer_consume_token (parser->lexer)->value;
6491 /* Parse an (optional) function-specifier.
6498 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
6501 cp_parser_function_specifier_opt (cp_parser* parser)
6503 switch (cp_lexer_peek_token (parser->lexer)->keyword)
6508 /* Consume the token. */
6509 return cp_lexer_consume_token (parser->lexer)->value;
6516 /* Parse a linkage-specification.
6518 linkage-specification:
6519 extern string-literal { declaration-seq [opt] }
6520 extern string-literal declaration */
6523 cp_parser_linkage_specification (cp_parser* parser)
6528 /* Look for the `extern' keyword. */
6529 cp_parser_require_keyword (parser, RID_EXTERN, "`extern'");
6531 /* Peek at the next token. */
6532 token = cp_lexer_peek_token (parser->lexer);
6533 /* If it's not a string-literal, then there's a problem. */
6534 if (!cp_parser_is_string_literal (token))
6536 cp_parser_error (parser, "expected language-name");
6539 /* Consume the token. */
6540 cp_lexer_consume_token (parser->lexer);
6542 /* Transform the literal into an identifier. If the literal is a
6543 wide-character string, or contains embedded NULs, then we can't
6544 handle it as the user wants. */
6545 if (token->type == CPP_WSTRING
6546 || (strlen (TREE_STRING_POINTER (token->value))
6547 != (size_t) (TREE_STRING_LENGTH (token->value) - 1)))
6549 cp_parser_error (parser, "invalid linkage-specification");
6550 /* Assume C++ linkage. */
6551 linkage = get_identifier ("c++");
6553 /* If it's a simple string constant, things are easier. */
6555 linkage = get_identifier (TREE_STRING_POINTER (token->value));
6557 /* We're now using the new linkage. */
6558 push_lang_context (linkage);
6560 /* If the next token is a `{', then we're using the first
6562 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6564 /* Consume the `{' token. */
6565 cp_lexer_consume_token (parser->lexer);
6566 /* Parse the declarations. */
6567 cp_parser_declaration_seq_opt (parser);
6568 /* Look for the closing `}'. */
6569 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
6571 /* Otherwise, there's just one declaration. */
6574 bool saved_in_unbraced_linkage_specification_p;
6576 saved_in_unbraced_linkage_specification_p
6577 = parser->in_unbraced_linkage_specification_p;
6578 parser->in_unbraced_linkage_specification_p = true;
6579 have_extern_spec = true;
6580 cp_parser_declaration (parser);
6581 have_extern_spec = false;
6582 parser->in_unbraced_linkage_specification_p
6583 = saved_in_unbraced_linkage_specification_p;
6586 /* We're done with the linkage-specification. */
6587 pop_lang_context ();
6590 /* Special member functions [gram.special] */
6592 /* Parse a conversion-function-id.
6594 conversion-function-id:
6595 operator conversion-type-id
6597 Returns an IDENTIFIER_NODE representing the operator. */
6600 cp_parser_conversion_function_id (cp_parser* parser)
6604 tree saved_qualifying_scope;
6605 tree saved_object_scope;
6607 /* Look for the `operator' token. */
6608 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
6609 return error_mark_node;
6610 /* When we parse the conversion-type-id, the current scope will be
6611 reset. However, we need that information in able to look up the
6612 conversion function later, so we save it here. */
6613 saved_scope = parser->scope;
6614 saved_qualifying_scope = parser->qualifying_scope;
6615 saved_object_scope = parser->object_scope;
6616 /* We must enter the scope of the class so that the names of
6617 entities declared within the class are available in the
6618 conversion-type-id. For example, consider:
6625 S::operator I() { ... }
6627 In order to see that `I' is a type-name in the definition, we
6628 must be in the scope of `S'. */
6630 push_scope (saved_scope);
6631 /* Parse the conversion-type-id. */
6632 type = cp_parser_conversion_type_id (parser);
6633 /* Leave the scope of the class, if any. */
6635 pop_scope (saved_scope);
6636 /* Restore the saved scope. */
6637 parser->scope = saved_scope;
6638 parser->qualifying_scope = saved_qualifying_scope;
6639 parser->object_scope = saved_object_scope;
6640 /* If the TYPE is invalid, indicate failure. */
6641 if (type == error_mark_node)
6642 return error_mark_node;
6643 return mangle_conv_op_name_for_type (type);
6646 /* Parse a conversion-type-id:
6649 type-specifier-seq conversion-declarator [opt]
6651 Returns the TYPE specified. */
6654 cp_parser_conversion_type_id (cp_parser* parser)
6657 tree type_specifiers;
6660 /* Parse the attributes. */
6661 attributes = cp_parser_attributes_opt (parser);
6662 /* Parse the type-specifiers. */
6663 type_specifiers = cp_parser_type_specifier_seq (parser);
6664 /* If that didn't work, stop. */
6665 if (type_specifiers == error_mark_node)
6666 return error_mark_node;
6667 /* Parse the conversion-declarator. */
6668 declarator = cp_parser_conversion_declarator_opt (parser);
6670 return grokdeclarator (declarator, type_specifiers, TYPENAME,
6671 /*initialized=*/0, &attributes);
6674 /* Parse an (optional) conversion-declarator.
6676 conversion-declarator:
6677 ptr-operator conversion-declarator [opt]
6679 Returns a representation of the declarator. See
6680 cp_parser_declarator for details. */
6683 cp_parser_conversion_declarator_opt (cp_parser* parser)
6685 enum tree_code code;
6687 tree cv_qualifier_seq;
6689 /* We don't know if there's a ptr-operator next, or not. */
6690 cp_parser_parse_tentatively (parser);
6691 /* Try the ptr-operator. */
6692 code = cp_parser_ptr_operator (parser, &class_type,
6694 /* If it worked, look for more conversion-declarators. */
6695 if (cp_parser_parse_definitely (parser))
6699 /* Parse another optional declarator. */
6700 declarator = cp_parser_conversion_declarator_opt (parser);
6702 /* Create the representation of the declarator. */
6703 if (code == INDIRECT_REF)
6704 declarator = make_pointer_declarator (cv_qualifier_seq,
6707 declarator = make_reference_declarator (cv_qualifier_seq,
6710 /* Handle the pointer-to-member case. */
6712 declarator = build_nt (SCOPE_REF, class_type, declarator);
6720 /* Parse an (optional) ctor-initializer.
6723 : mem-initializer-list
6725 Returns TRUE iff the ctor-initializer was actually present. */
6728 cp_parser_ctor_initializer_opt (cp_parser* parser)
6730 /* If the next token is not a `:', then there is no
6731 ctor-initializer. */
6732 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
6734 /* Do default initialization of any bases and members. */
6735 if (DECL_CONSTRUCTOR_P (current_function_decl))
6736 finish_mem_initializers (NULL_TREE);
6741 /* Consume the `:' token. */
6742 cp_lexer_consume_token (parser->lexer);
6743 /* And the mem-initializer-list. */
6744 cp_parser_mem_initializer_list (parser);
6749 /* Parse a mem-initializer-list.
6751 mem-initializer-list:
6753 mem-initializer , mem-initializer-list */
6756 cp_parser_mem_initializer_list (cp_parser* parser)
6758 tree mem_initializer_list = NULL_TREE;
6760 /* Let the semantic analysis code know that we are starting the
6761 mem-initializer-list. */
6762 if (!DECL_CONSTRUCTOR_P (current_function_decl))
6763 error ("only constructors take base initializers");
6765 /* Loop through the list. */
6768 tree mem_initializer;
6770 /* Parse the mem-initializer. */
6771 mem_initializer = cp_parser_mem_initializer (parser);
6772 /* Add it to the list, unless it was erroneous. */
6773 if (mem_initializer)
6775 TREE_CHAIN (mem_initializer) = mem_initializer_list;
6776 mem_initializer_list = mem_initializer;
6778 /* If the next token is not a `,', we're done. */
6779 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
6781 /* Consume the `,' token. */
6782 cp_lexer_consume_token (parser->lexer);
6785 /* Perform semantic analysis. */
6786 if (DECL_CONSTRUCTOR_P (current_function_decl))
6787 finish_mem_initializers (mem_initializer_list);
6790 /* Parse a mem-initializer.
6793 mem-initializer-id ( expression-list [opt] )
6798 ( expression-list [opt] )
6800 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
6801 class) or FIELD_DECL (for a non-static data member) to initialize;
6802 the TREE_VALUE is the expression-list. */
6805 cp_parser_mem_initializer (cp_parser* parser)
6807 tree mem_initializer_id;
6808 tree expression_list;
6811 /* Find out what is being initialized. */
6812 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6814 pedwarn ("anachronistic old-style base class initializer");
6815 mem_initializer_id = NULL_TREE;
6818 mem_initializer_id = cp_parser_mem_initializer_id (parser);
6819 member = expand_member_init (mem_initializer_id);
6820 if (member && !DECL_P (member))
6821 in_base_initializer = 1;
6824 = cp_parser_parenthesized_expression_list (parser, false,
6825 /*non_constant_p=*/NULL);
6826 if (!expression_list)
6827 expression_list = void_type_node;
6829 in_base_initializer = 0;
6831 return member ? build_tree_list (member, expression_list) : NULL_TREE;
6834 /* Parse a mem-initializer-id.
6837 :: [opt] nested-name-specifier [opt] class-name
6840 Returns a TYPE indicating the class to be initializer for the first
6841 production. Returns an IDENTIFIER_NODE indicating the data member
6842 to be initialized for the second production. */
6845 cp_parser_mem_initializer_id (cp_parser* parser)
6847 bool global_scope_p;
6848 bool nested_name_specifier_p;
6851 /* Look for the optional `::' operator. */
6853 = (cp_parser_global_scope_opt (parser,
6854 /*current_scope_valid_p=*/false)
6856 /* Look for the optional nested-name-specifier. The simplest way to
6861 The keyword `typename' is not permitted in a base-specifier or
6862 mem-initializer; in these contexts a qualified name that
6863 depends on a template-parameter is implicitly assumed to be a
6866 is to assume that we have seen the `typename' keyword at this
6868 nested_name_specifier_p
6869 = (cp_parser_nested_name_specifier_opt (parser,
6870 /*typename_keyword_p=*/true,
6871 /*check_dependency_p=*/true,
6874 /* If there is a `::' operator or a nested-name-specifier, then we
6875 are definitely looking for a class-name. */
6876 if (global_scope_p || nested_name_specifier_p)
6877 return cp_parser_class_name (parser,
6878 /*typename_keyword_p=*/true,
6879 /*template_keyword_p=*/false,
6881 /*check_dependency_p=*/true,
6882 /*class_head_p=*/false);
6883 /* Otherwise, we could also be looking for an ordinary identifier. */
6884 cp_parser_parse_tentatively (parser);
6885 /* Try a class-name. */
6886 id = cp_parser_class_name (parser,
6887 /*typename_keyword_p=*/true,
6888 /*template_keyword_p=*/false,
6890 /*check_dependency_p=*/true,
6891 /*class_head_p=*/false);
6892 /* If we found one, we're done. */
6893 if (cp_parser_parse_definitely (parser))
6895 /* Otherwise, look for an ordinary identifier. */
6896 return cp_parser_identifier (parser);
6899 /* Overloading [gram.over] */
6901 /* Parse an operator-function-id.
6903 operator-function-id:
6906 Returns an IDENTIFIER_NODE for the operator which is a
6907 human-readable spelling of the identifier, e.g., `operator +'. */
6910 cp_parser_operator_function_id (cp_parser* parser)
6912 /* Look for the `operator' keyword. */
6913 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
6914 return error_mark_node;
6915 /* And then the name of the operator itself. */
6916 return cp_parser_operator (parser);
6919 /* Parse an operator.
6922 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
6923 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
6924 || ++ -- , ->* -> () []
6931 Returns an IDENTIFIER_NODE for the operator which is a
6932 human-readable spelling of the identifier, e.g., `operator +'. */
6935 cp_parser_operator (cp_parser* parser)
6937 tree id = NULL_TREE;
6940 /* Peek at the next token. */
6941 token = cp_lexer_peek_token (parser->lexer);
6942 /* Figure out which operator we have. */
6943 switch (token->type)
6949 /* The keyword should be either `new' or `delete'. */
6950 if (token->keyword == RID_NEW)
6952 else if (token->keyword == RID_DELETE)
6957 /* Consume the `new' or `delete' token. */
6958 cp_lexer_consume_token (parser->lexer);
6960 /* Peek at the next token. */
6961 token = cp_lexer_peek_token (parser->lexer);
6962 /* If it's a `[' token then this is the array variant of the
6964 if (token->type == CPP_OPEN_SQUARE)
6966 /* Consume the `[' token. */
6967 cp_lexer_consume_token (parser->lexer);
6968 /* Look for the `]' token. */
6969 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
6970 id = ansi_opname (op == NEW_EXPR
6971 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
6973 /* Otherwise, we have the non-array variant. */
6975 id = ansi_opname (op);
6981 id = ansi_opname (PLUS_EXPR);
6985 id = ansi_opname (MINUS_EXPR);
6989 id = ansi_opname (MULT_EXPR);
6993 id = ansi_opname (TRUNC_DIV_EXPR);
6997 id = ansi_opname (TRUNC_MOD_EXPR);
7001 id = ansi_opname (BIT_XOR_EXPR);
7005 id = ansi_opname (BIT_AND_EXPR);
7009 id = ansi_opname (BIT_IOR_EXPR);
7013 id = ansi_opname (BIT_NOT_EXPR);
7017 id = ansi_opname (TRUTH_NOT_EXPR);
7021 id = ansi_assopname (NOP_EXPR);
7025 id = ansi_opname (LT_EXPR);
7029 id = ansi_opname (GT_EXPR);
7033 id = ansi_assopname (PLUS_EXPR);
7037 id = ansi_assopname (MINUS_EXPR);
7041 id = ansi_assopname (MULT_EXPR);
7045 id = ansi_assopname (TRUNC_DIV_EXPR);
7049 id = ansi_assopname (TRUNC_MOD_EXPR);
7053 id = ansi_assopname (BIT_XOR_EXPR);
7057 id = ansi_assopname (BIT_AND_EXPR);
7061 id = ansi_assopname (BIT_IOR_EXPR);
7065 id = ansi_opname (LSHIFT_EXPR);
7069 id = ansi_opname (RSHIFT_EXPR);
7073 id = ansi_assopname (LSHIFT_EXPR);
7077 id = ansi_assopname (RSHIFT_EXPR);
7081 id = ansi_opname (EQ_EXPR);
7085 id = ansi_opname (NE_EXPR);
7089 id = ansi_opname (LE_EXPR);
7092 case CPP_GREATER_EQ:
7093 id = ansi_opname (GE_EXPR);
7097 id = ansi_opname (TRUTH_ANDIF_EXPR);
7101 id = ansi_opname (TRUTH_ORIF_EXPR);
7105 id = ansi_opname (POSTINCREMENT_EXPR);
7108 case CPP_MINUS_MINUS:
7109 id = ansi_opname (PREDECREMENT_EXPR);
7113 id = ansi_opname (COMPOUND_EXPR);
7116 case CPP_DEREF_STAR:
7117 id = ansi_opname (MEMBER_REF);
7121 id = ansi_opname (COMPONENT_REF);
7124 case CPP_OPEN_PAREN:
7125 /* Consume the `('. */
7126 cp_lexer_consume_token (parser->lexer);
7127 /* Look for the matching `)'. */
7128 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
7129 return ansi_opname (CALL_EXPR);
7131 case CPP_OPEN_SQUARE:
7132 /* Consume the `['. */
7133 cp_lexer_consume_token (parser->lexer);
7134 /* Look for the matching `]'. */
7135 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
7136 return ansi_opname (ARRAY_REF);
7140 id = ansi_opname (MIN_EXPR);
7144 id = ansi_opname (MAX_EXPR);
7148 id = ansi_assopname (MIN_EXPR);
7152 id = ansi_assopname (MAX_EXPR);
7156 /* Anything else is an error. */
7160 /* If we have selected an identifier, we need to consume the
7163 cp_lexer_consume_token (parser->lexer);
7164 /* Otherwise, no valid operator name was present. */
7167 cp_parser_error (parser, "expected operator");
7168 id = error_mark_node;
7174 /* Parse a template-declaration.
7176 template-declaration:
7177 export [opt] template < template-parameter-list > declaration
7179 If MEMBER_P is TRUE, this template-declaration occurs within a
7182 The grammar rule given by the standard isn't correct. What
7185 template-declaration:
7186 export [opt] template-parameter-list-seq
7187 decl-specifier-seq [opt] init-declarator [opt] ;
7188 export [opt] template-parameter-list-seq
7191 template-parameter-list-seq:
7192 template-parameter-list-seq [opt]
7193 template < template-parameter-list > */
7196 cp_parser_template_declaration (cp_parser* parser, bool member_p)
7198 /* Check for `export'. */
7199 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
7201 /* Consume the `export' token. */
7202 cp_lexer_consume_token (parser->lexer);
7203 /* Warn that we do not support `export'. */
7204 warning ("keyword `export' not implemented, and will be ignored");
7207 cp_parser_template_declaration_after_export (parser, member_p);
7210 /* Parse a template-parameter-list.
7212 template-parameter-list:
7214 template-parameter-list , template-parameter
7216 Returns a TREE_LIST. Each node represents a template parameter.
7217 The nodes are connected via their TREE_CHAINs. */
7220 cp_parser_template_parameter_list (cp_parser* parser)
7222 tree parameter_list = NULL_TREE;
7229 /* Parse the template-parameter. */
7230 parameter = cp_parser_template_parameter (parser);
7231 /* Add it to the list. */
7232 parameter_list = process_template_parm (parameter_list,
7235 /* Peek at the next token. */
7236 token = cp_lexer_peek_token (parser->lexer);
7237 /* If it's not a `,', we're done. */
7238 if (token->type != CPP_COMMA)
7240 /* Otherwise, consume the `,' token. */
7241 cp_lexer_consume_token (parser->lexer);
7244 return parameter_list;
7247 /* Parse a template-parameter.
7251 parameter-declaration
7253 Returns a TREE_LIST. The TREE_VALUE represents the parameter. The
7254 TREE_PURPOSE is the default value, if any. */
7257 cp_parser_template_parameter (cp_parser* parser)
7261 /* Peek at the next token. */
7262 token = cp_lexer_peek_token (parser->lexer);
7263 /* If it is `class' or `template', we have a type-parameter. */
7264 if (token->keyword == RID_TEMPLATE)
7265 return cp_parser_type_parameter (parser);
7266 /* If it is `class' or `typename' we do not know yet whether it is a
7267 type parameter or a non-type parameter. Consider:
7269 template <typename T, typename T::X X> ...
7273 template <class C, class D*> ...
7275 Here, the first parameter is a type parameter, and the second is
7276 a non-type parameter. We can tell by looking at the token after
7277 the identifier -- if it is a `,', `=', or `>' then we have a type
7279 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
7281 /* Peek at the token after `class' or `typename'. */
7282 token = cp_lexer_peek_nth_token (parser->lexer, 2);
7283 /* If it's an identifier, skip it. */
7284 if (token->type == CPP_NAME)
7285 token = cp_lexer_peek_nth_token (parser->lexer, 3);
7286 /* Now, see if the token looks like the end of a template
7288 if (token->type == CPP_COMMA
7289 || token->type == CPP_EQ
7290 || token->type == CPP_GREATER)
7291 return cp_parser_type_parameter (parser);
7294 /* Otherwise, it is a non-type parameter.
7298 When parsing a default template-argument for a non-type
7299 template-parameter, the first non-nested `>' is taken as the end
7300 of the template parameter-list rather than a greater-than
7303 cp_parser_parameter_declaration (parser, /*template_parm_p=*/true);
7306 /* Parse a type-parameter.
7309 class identifier [opt]
7310 class identifier [opt] = type-id
7311 typename identifier [opt]
7312 typename identifier [opt] = type-id
7313 template < template-parameter-list > class identifier [opt]
7314 template < template-parameter-list > class identifier [opt]
7317 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
7318 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
7319 the declaration of the parameter. */
7322 cp_parser_type_parameter (cp_parser* parser)
7327 /* Look for a keyword to tell us what kind of parameter this is. */
7328 token = cp_parser_require (parser, CPP_KEYWORD,
7329 "`class', `typename', or `template'");
7331 return error_mark_node;
7333 switch (token->keyword)
7339 tree default_argument;
7341 /* If the next token is an identifier, then it names the
7343 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
7344 identifier = cp_parser_identifier (parser);
7346 identifier = NULL_TREE;
7348 /* Create the parameter. */
7349 parameter = finish_template_type_parm (class_type_node, identifier);
7351 /* If the next token is an `=', we have a default argument. */
7352 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7354 /* Consume the `=' token. */
7355 cp_lexer_consume_token (parser->lexer);
7356 /* Parse the default-argument. */
7357 default_argument = cp_parser_type_id (parser);
7360 default_argument = NULL_TREE;
7362 /* Create the combined representation of the parameter and the
7363 default argument. */
7364 parameter = build_tree_list (default_argument, parameter);
7370 tree parameter_list;
7372 tree default_argument;
7374 /* Look for the `<'. */
7375 cp_parser_require (parser, CPP_LESS, "`<'");
7376 /* Parse the template-parameter-list. */
7377 begin_template_parm_list ();
7379 = cp_parser_template_parameter_list (parser);
7380 parameter_list = end_template_parm_list (parameter_list);
7381 /* Look for the `>'. */
7382 cp_parser_require (parser, CPP_GREATER, "`>'");
7383 /* Look for the `class' keyword. */
7384 cp_parser_require_keyword (parser, RID_CLASS, "`class'");
7385 /* If the next token is an `=', then there is a
7386 default-argument. If the next token is a `>', we are at
7387 the end of the parameter-list. If the next token is a `,',
7388 then we are at the end of this parameter. */
7389 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
7390 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
7391 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7392 identifier = cp_parser_identifier (parser);
7394 identifier = NULL_TREE;
7395 /* Create the template parameter. */
7396 parameter = finish_template_template_parm (class_type_node,
7399 /* If the next token is an `=', then there is a
7400 default-argument. */
7401 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7403 /* Consume the `='. */
7404 cp_lexer_consume_token (parser->lexer);
7405 /* Parse the id-expression. */
7407 = cp_parser_id_expression (parser,
7408 /*template_keyword_p=*/false,
7409 /*check_dependency_p=*/true,
7410 /*template_p=*/NULL,
7411 /*declarator_p=*/false);
7412 /* Look up the name. */
7414 = cp_parser_lookup_name_simple (parser, default_argument);
7415 /* See if the default argument is valid. */
7417 = check_template_template_default_arg (default_argument);
7420 default_argument = NULL_TREE;
7422 /* Create the combined representation of the parameter and the
7423 default argument. */
7424 parameter = build_tree_list (default_argument, parameter);
7429 /* Anything else is an error. */
7430 cp_parser_error (parser,
7431 "expected `class', `typename', or `template'");
7432 parameter = error_mark_node;
7438 /* Parse a template-id.
7441 template-name < template-argument-list [opt] >
7443 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
7444 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
7445 returned. Otherwise, if the template-name names a function, or set
7446 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
7447 names a class, returns a TYPE_DECL for the specialization.
7449 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
7450 uninstantiated templates. */
7453 cp_parser_template_id (cp_parser *parser,
7454 bool template_keyword_p,
7455 bool check_dependency_p)
7460 tree saved_qualifying_scope;
7461 tree saved_object_scope;
7463 bool saved_greater_than_is_operator_p;
7464 ptrdiff_t start_of_id;
7465 tree access_check = NULL_TREE;
7466 cp_token *next_token;
7468 /* If the next token corresponds to a template-id, there is no need
7470 next_token = cp_lexer_peek_token (parser->lexer);
7471 if (next_token->type == CPP_TEMPLATE_ID)
7476 /* Get the stored value. */
7477 value = cp_lexer_consume_token (parser->lexer)->value;
7478 /* Perform any access checks that were deferred. */
7479 for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
7480 perform_or_defer_access_check (TREE_PURPOSE (check),
7481 TREE_VALUE (check));
7482 /* Return the stored value. */
7483 return TREE_VALUE (value);
7486 /* Avoid performing name lookup if there is no possibility of
7487 finding a template-id. */
7488 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
7489 || (next_token->type == CPP_NAME
7490 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_LESS))
7492 cp_parser_error (parser, "expected template-id");
7493 return error_mark_node;
7496 /* Remember where the template-id starts. */
7497 if (cp_parser_parsing_tentatively (parser)
7498 && !cp_parser_committed_to_tentative_parse (parser))
7500 next_token = cp_lexer_peek_token (parser->lexer);
7501 start_of_id = cp_lexer_token_difference (parser->lexer,
7502 parser->lexer->first_token,
7508 push_deferring_access_checks (dk_deferred);
7510 /* Parse the template-name. */
7511 template = cp_parser_template_name (parser, template_keyword_p,
7512 check_dependency_p);
7513 if (template == error_mark_node)
7515 pop_deferring_access_checks ();
7516 return error_mark_node;
7519 /* Look for the `<' that starts the template-argument-list. */
7520 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
7522 pop_deferring_access_checks ();
7523 return error_mark_node;
7528 When parsing a template-id, the first non-nested `>' is taken as
7529 the end of the template-argument-list rather than a greater-than
7531 saved_greater_than_is_operator_p
7532 = parser->greater_than_is_operator_p;
7533 parser->greater_than_is_operator_p = false;
7534 /* Parsing the argument list may modify SCOPE, so we save it
7536 saved_scope = parser->scope;
7537 saved_qualifying_scope = parser->qualifying_scope;
7538 saved_object_scope = parser->object_scope;
7539 /* Parse the template-argument-list itself. */
7540 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
7541 arguments = NULL_TREE;
7543 arguments = cp_parser_template_argument_list (parser);
7544 /* Look for the `>' that ends the template-argument-list. */
7545 cp_parser_require (parser, CPP_GREATER, "`>'");
7546 /* The `>' token might be a greater-than operator again now. */
7547 parser->greater_than_is_operator_p
7548 = saved_greater_than_is_operator_p;
7549 /* Restore the SAVED_SCOPE. */
7550 parser->scope = saved_scope;
7551 parser->qualifying_scope = saved_qualifying_scope;
7552 parser->object_scope = saved_object_scope;
7554 /* Build a representation of the specialization. */
7555 if (TREE_CODE (template) == IDENTIFIER_NODE)
7556 template_id = build_min_nt (TEMPLATE_ID_EXPR, template, arguments);
7557 else if (DECL_CLASS_TEMPLATE_P (template)
7558 || DECL_TEMPLATE_TEMPLATE_PARM_P (template))
7560 = finish_template_type (template, arguments,
7561 cp_lexer_next_token_is (parser->lexer,
7565 /* If it's not a class-template or a template-template, it should be
7566 a function-template. */
7567 my_friendly_assert ((DECL_FUNCTION_TEMPLATE_P (template)
7568 || TREE_CODE (template) == OVERLOAD
7569 || BASELINK_P (template)),
7572 template_id = lookup_template_function (template, arguments);
7575 /* Retrieve any deferred checks. Do not pop this access checks yet
7576 so the memory will not be reclaimed during token replacing below. */
7577 access_check = get_deferred_access_checks ();
7579 /* If parsing tentatively, replace the sequence of tokens that makes
7580 up the template-id with a CPP_TEMPLATE_ID token. That way,
7581 should we re-parse the token stream, we will not have to repeat
7582 the effort required to do the parse, nor will we issue duplicate
7583 error messages about problems during instantiation of the
7585 if (start_of_id >= 0)
7589 /* Find the token that corresponds to the start of the
7591 token = cp_lexer_advance_token (parser->lexer,
7592 parser->lexer->first_token,
7595 /* Reset the contents of the START_OF_ID token. */
7596 token->type = CPP_TEMPLATE_ID;
7597 token->value = build_tree_list (access_check, template_id);
7598 token->keyword = RID_MAX;
7599 /* Purge all subsequent tokens. */
7600 cp_lexer_purge_tokens_after (parser->lexer, token);
7603 pop_deferring_access_checks ();
7607 /* Parse a template-name.
7612 The standard should actually say:
7616 operator-function-id
7617 conversion-function-id
7619 A defect report has been filed about this issue.
7621 If TEMPLATE_KEYWORD_P is true, then we have just seen the
7622 `template' keyword, in a construction like:
7626 In that case `f' is taken to be a template-name, even though there
7627 is no way of knowing for sure.
7629 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
7630 name refers to a set of overloaded functions, at least one of which
7631 is a template, or an IDENTIFIER_NODE with the name of the template,
7632 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
7633 names are looked up inside uninstantiated templates. */
7636 cp_parser_template_name (cp_parser* parser,
7637 bool template_keyword_p,
7638 bool check_dependency_p)
7644 /* If the next token is `operator', then we have either an
7645 operator-function-id or a conversion-function-id. */
7646 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
7648 /* We don't know whether we're looking at an
7649 operator-function-id or a conversion-function-id. */
7650 cp_parser_parse_tentatively (parser);
7651 /* Try an operator-function-id. */
7652 identifier = cp_parser_operator_function_id (parser);
7653 /* If that didn't work, try a conversion-function-id. */
7654 if (!cp_parser_parse_definitely (parser))
7655 identifier = cp_parser_conversion_function_id (parser);
7657 /* Look for the identifier. */
7659 identifier = cp_parser_identifier (parser);
7661 /* If we didn't find an identifier, we don't have a template-id. */
7662 if (identifier == error_mark_node)
7663 return error_mark_node;
7665 /* If the name immediately followed the `template' keyword, then it
7666 is a template-name. However, if the next token is not `<', then
7667 we do not treat it as a template-name, since it is not being used
7668 as part of a template-id. This enables us to handle constructs
7671 template <typename T> struct S { S(); };
7672 template <typename T> S<T>::S();
7674 correctly. We would treat `S' as a template -- if it were `S<T>'
7675 -- but we do not if there is no `<'. */
7676 if (template_keyword_p && processing_template_decl
7677 && cp_lexer_next_token_is (parser->lexer, CPP_LESS))
7680 /* Look up the name. */
7681 decl = cp_parser_lookup_name (parser, identifier,
7683 /*is_namespace=*/false,
7684 check_dependency_p);
7685 decl = maybe_get_template_decl_from_type_decl (decl);
7687 /* If DECL is a template, then the name was a template-name. */
7688 if (TREE_CODE (decl) == TEMPLATE_DECL)
7692 /* The standard does not explicitly indicate whether a name that
7693 names a set of overloaded declarations, some of which are
7694 templates, is a template-name. However, such a name should
7695 be a template-name; otherwise, there is no way to form a
7696 template-id for the overloaded templates. */
7697 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
7698 if (TREE_CODE (fns) == OVERLOAD)
7702 for (fn = fns; fn; fn = OVL_NEXT (fn))
7703 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
7708 /* Otherwise, the name does not name a template. */
7709 cp_parser_error (parser, "expected template-name");
7710 return error_mark_node;
7714 /* If DECL is dependent, and refers to a function, then just return
7715 its name; we will look it up again during template instantiation. */
7716 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
7718 tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
7719 if (TYPE_P (scope) && dependent_type_p (scope))
7726 /* Parse a template-argument-list.
7728 template-argument-list:
7730 template-argument-list , template-argument
7732 Returns a TREE_VEC containing the arguments. */
7735 cp_parser_template_argument_list (cp_parser* parser)
7737 tree fixed_args[10];
7738 unsigned n_args = 0;
7739 unsigned alloced = 10;
7740 tree *arg_ary = fixed_args;
7748 /* Consume the comma. */
7749 cp_lexer_consume_token (parser->lexer);
7751 /* Parse the template-argument. */
7752 argument = cp_parser_template_argument (parser);
7753 if (n_args == alloced)
7757 if (arg_ary == fixed_args)
7759 arg_ary = xmalloc (sizeof (tree) * alloced);
7760 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
7763 arg_ary = xrealloc (arg_ary, sizeof (tree) * alloced);
7765 arg_ary[n_args++] = argument;
7767 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
7769 vec = make_tree_vec (n_args);
7772 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
7774 if (arg_ary != fixed_args)
7779 /* Parse a template-argument.
7782 assignment-expression
7786 The representation is that of an assignment-expression, type-id, or
7787 id-expression -- except that the qualified id-expression is
7788 evaluated, so that the value returned is either a DECL or an
7791 Although the standard says "assignment-expression", it forbids
7792 throw-expressions or assignments in the template argument.
7793 Therefore, we use "conditional-expression" instead. */
7796 cp_parser_template_argument (cp_parser* parser)
7803 tree qualifying_class;
7805 /* There's really no way to know what we're looking at, so we just
7806 try each alternative in order.
7810 In a template-argument, an ambiguity between a type-id and an
7811 expression is resolved to a type-id, regardless of the form of
7812 the corresponding template-parameter.
7814 Therefore, we try a type-id first. */
7815 cp_parser_parse_tentatively (parser);
7816 argument = cp_parser_type_id (parser);
7817 /* If the next token isn't a `,' or a `>', then this argument wasn't
7819 if (!cp_parser_next_token_ends_template_argument_p (parser))
7820 cp_parser_error (parser, "expected template-argument");
7821 /* If that worked, we're done. */
7822 if (cp_parser_parse_definitely (parser))
7824 /* We're still not sure what the argument will be. */
7825 cp_parser_parse_tentatively (parser);
7826 /* Try a template. */
7827 argument = cp_parser_id_expression (parser,
7828 /*template_keyword_p=*/false,
7829 /*check_dependency_p=*/true,
7831 /*declarator_p=*/false);
7832 /* If the next token isn't a `,' or a `>', then this argument wasn't
7834 if (!cp_parser_next_token_ends_template_argument_p (parser))
7835 cp_parser_error (parser, "expected template-argument");
7836 if (!cp_parser_error_occurred (parser))
7838 /* Figure out what is being referred to. */
7839 argument = cp_parser_lookup_name_simple (parser, argument);
7841 argument = make_unbound_class_template (TREE_OPERAND (argument, 0),
7842 TREE_OPERAND (argument, 1),
7844 else if (TREE_CODE (argument) != TEMPLATE_DECL)
7845 cp_parser_error (parser, "expected template-name");
7847 if (cp_parser_parse_definitely (parser))
7849 /* It must be a non-type argument. There permitted cases are given
7850 in [temp.arg.nontype]:
7852 -- an integral constant-expression of integral or enumeration
7855 -- the name of a non-type template-parameter; or
7857 -- the name of an object or function with external linkage...
7859 -- the address of an object or function with external linkage...
7861 -- a pointer to member... */
7862 /* Look for a non-type template parameter. */
7863 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
7865 cp_parser_parse_tentatively (parser);
7866 argument = cp_parser_primary_expression (parser,
7869 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
7870 || !cp_parser_next_token_ends_template_argument_p (parser))
7871 cp_parser_simulate_error (parser);
7872 if (cp_parser_parse_definitely (parser))
7875 /* If the next token is "&", the argument must be the address of an
7876 object or function with external linkage. */
7877 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
7879 cp_lexer_consume_token (parser->lexer);
7880 /* See if we might have an id-expression. */
7881 token = cp_lexer_peek_token (parser->lexer);
7882 if (token->type == CPP_NAME
7883 || token->keyword == RID_OPERATOR
7884 || token->type == CPP_SCOPE
7885 || token->type == CPP_TEMPLATE_ID
7886 || token->type == CPP_NESTED_NAME_SPECIFIER)
7888 cp_parser_parse_tentatively (parser);
7889 argument = cp_parser_primary_expression (parser,
7892 if (cp_parser_error_occurred (parser)
7893 || !cp_parser_next_token_ends_template_argument_p (parser))
7894 cp_parser_abort_tentative_parse (parser);
7897 if (qualifying_class)
7898 argument = finish_qualified_id_expr (qualifying_class,
7902 if (TREE_CODE (argument) == VAR_DECL)
7904 /* A variable without external linkage might still be a
7905 valid constant-expression, so no error is issued here
7906 if the external-linkage check fails. */
7907 if (!DECL_EXTERNAL_LINKAGE_P (argument))
7908 cp_parser_simulate_error (parser);
7910 else if (is_overloaded_fn (argument))
7911 /* All overloaded functions are allowed; if the external
7912 linkage test does not pass, an error will be issued
7916 && (TREE_CODE (argument) == OFFSET_REF
7917 || TREE_CODE (argument) == SCOPE_REF))
7918 /* A pointer-to-member. */
7921 cp_parser_simulate_error (parser);
7923 if (cp_parser_parse_definitely (parser))
7926 argument = build_x_unary_op (ADDR_EXPR, argument);
7931 /* If the argument started with "&", there are no other valid
7932 alternatives at this point. */
7935 cp_parser_error (parser, "invalid non-type template argument");
7936 return error_mark_node;
7938 /* The argument must be a constant-expression. */
7939 argument = cp_parser_constant_expression (parser,
7940 /*allow_non_constant_p=*/false,
7941 /*non_constant_p=*/NULL);
7942 /* If it's non-dependent, simplify it. */
7943 return cp_parser_fold_non_dependent_expr (argument);
7946 /* Parse an explicit-instantiation.
7948 explicit-instantiation:
7949 template declaration
7951 Although the standard says `declaration', what it really means is:
7953 explicit-instantiation:
7954 template decl-specifier-seq [opt] declarator [opt] ;
7956 Things like `template int S<int>::i = 5, int S<double>::j;' are not
7957 supposed to be allowed. A defect report has been filed about this
7962 explicit-instantiation:
7963 storage-class-specifier template
7964 decl-specifier-seq [opt] declarator [opt] ;
7965 function-specifier template
7966 decl-specifier-seq [opt] declarator [opt] ; */
7969 cp_parser_explicit_instantiation (cp_parser* parser)
7971 int declares_class_or_enum;
7972 tree decl_specifiers;
7974 tree extension_specifier = NULL_TREE;
7976 /* Look for an (optional) storage-class-specifier or
7977 function-specifier. */
7978 if (cp_parser_allow_gnu_extensions_p (parser))
7981 = cp_parser_storage_class_specifier_opt (parser);
7982 if (!extension_specifier)
7983 extension_specifier = cp_parser_function_specifier_opt (parser);
7986 /* Look for the `template' keyword. */
7987 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
7988 /* Let the front end know that we are processing an explicit
7990 begin_explicit_instantiation ();
7991 /* [temp.explicit] says that we are supposed to ignore access
7992 control while processing explicit instantiation directives. */
7993 push_deferring_access_checks (dk_no_check);
7994 /* Parse a decl-specifier-seq. */
7996 = cp_parser_decl_specifier_seq (parser,
7997 CP_PARSER_FLAGS_OPTIONAL,
7999 &declares_class_or_enum);
8000 /* If there was exactly one decl-specifier, and it declared a class,
8001 and there's no declarator, then we have an explicit type
8003 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
8007 type = check_tag_decl (decl_specifiers);
8008 /* Turn access control back on for names used during
8009 template instantiation. */
8010 pop_deferring_access_checks ();
8012 do_type_instantiation (type, extension_specifier, /*complain=*/1);
8019 /* Parse the declarator. */
8021 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
8022 /*ctor_dtor_or_conv_p=*/NULL);
8023 cp_parser_check_for_definition_in_return_type (declarator,
8024 declares_class_or_enum);
8025 decl = grokdeclarator (declarator, decl_specifiers,
8027 /* Turn access control back on for names used during
8028 template instantiation. */
8029 pop_deferring_access_checks ();
8030 /* Do the explicit instantiation. */
8031 do_decl_instantiation (decl, extension_specifier);
8033 /* We're done with the instantiation. */
8034 end_explicit_instantiation ();
8036 cp_parser_consume_semicolon_at_end_of_statement (parser);
8039 /* Parse an explicit-specialization.
8041 explicit-specialization:
8042 template < > declaration
8044 Although the standard says `declaration', what it really means is:
8046 explicit-specialization:
8047 template <> decl-specifier [opt] init-declarator [opt] ;
8048 template <> function-definition
8049 template <> explicit-specialization
8050 template <> template-declaration */
8053 cp_parser_explicit_specialization (cp_parser* parser)
8055 /* Look for the `template' keyword. */
8056 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
8057 /* Look for the `<'. */
8058 cp_parser_require (parser, CPP_LESS, "`<'");
8059 /* Look for the `>'. */
8060 cp_parser_require (parser, CPP_GREATER, "`>'");
8061 /* We have processed another parameter list. */
8062 ++parser->num_template_parameter_lists;
8063 /* Let the front end know that we are beginning a specialization. */
8064 begin_specialization ();
8066 /* If the next keyword is `template', we need to figure out whether
8067 or not we're looking a template-declaration. */
8068 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
8070 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
8071 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
8072 cp_parser_template_declaration_after_export (parser,
8073 /*member_p=*/false);
8075 cp_parser_explicit_specialization (parser);
8078 /* Parse the dependent declaration. */
8079 cp_parser_single_declaration (parser,
8083 /* We're done with the specialization. */
8084 end_specialization ();
8085 /* We're done with this parameter list. */
8086 --parser->num_template_parameter_lists;
8089 /* Parse a type-specifier.
8092 simple-type-specifier
8095 elaborated-type-specifier
8103 Returns a representation of the type-specifier. If the
8104 type-specifier is a keyword (like `int' or `const', or
8105 `__complex__') then the corresponding IDENTIFIER_NODE is returned.
8106 For a class-specifier, enum-specifier, or elaborated-type-specifier
8107 a TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
8109 If IS_FRIEND is TRUE then this type-specifier is being declared a
8110 `friend'. If IS_DECLARATION is TRUE, then this type-specifier is
8111 appearing in a decl-specifier-seq.
8113 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
8114 class-specifier, enum-specifier, or elaborated-type-specifier, then
8115 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
8116 if a type is declared; 2 if it is defined. Otherwise, it is set to
8119 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
8120 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
8124 cp_parser_type_specifier (cp_parser* parser,
8125 cp_parser_flags flags,
8127 bool is_declaration,
8128 int* declares_class_or_enum,
8129 bool* is_cv_qualifier)
8131 tree type_spec = NULL_TREE;
8135 /* Assume this type-specifier does not declare a new type. */
8136 if (declares_class_or_enum)
8137 *declares_class_or_enum = false;
8138 /* And that it does not specify a cv-qualifier. */
8139 if (is_cv_qualifier)
8140 *is_cv_qualifier = false;
8141 /* Peek at the next token. */
8142 token = cp_lexer_peek_token (parser->lexer);
8144 /* If we're looking at a keyword, we can use that to guide the
8145 production we choose. */
8146 keyword = token->keyword;
8149 /* Any of these indicate either a class-specifier, or an
8150 elaborated-type-specifier. */
8155 /* Parse tentatively so that we can back up if we don't find a
8156 class-specifier or enum-specifier. */
8157 cp_parser_parse_tentatively (parser);
8158 /* Look for the class-specifier or enum-specifier. */
8159 if (keyword == RID_ENUM)
8160 type_spec = cp_parser_enum_specifier (parser);
8162 type_spec = cp_parser_class_specifier (parser);
8164 /* If that worked, we're done. */
8165 if (cp_parser_parse_definitely (parser))
8167 if (declares_class_or_enum)
8168 *declares_class_or_enum = 2;
8175 /* Look for an elaborated-type-specifier. */
8176 type_spec = cp_parser_elaborated_type_specifier (parser,
8179 /* We're declaring a class or enum -- unless we're using
8181 if (declares_class_or_enum && keyword != RID_TYPENAME)
8182 *declares_class_or_enum = 1;
8188 type_spec = cp_parser_cv_qualifier_opt (parser);
8189 /* Even though we call a routine that looks for an optional
8190 qualifier, we know that there should be one. */
8191 my_friendly_assert (type_spec != NULL, 20000328);
8192 /* This type-specifier was a cv-qualified. */
8193 if (is_cv_qualifier)
8194 *is_cv_qualifier = true;
8199 /* The `__complex__' keyword is a GNU extension. */
8200 return cp_lexer_consume_token (parser->lexer)->value;
8206 /* If we do not already have a type-specifier, assume we are looking
8207 at a simple-type-specifier. */
8208 type_spec = cp_parser_simple_type_specifier (parser, flags,
8209 /*identifier_p=*/true);
8211 /* If we didn't find a type-specifier, and a type-specifier was not
8212 optional in this context, issue an error message. */
8213 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
8215 cp_parser_error (parser, "expected type specifier");
8216 return error_mark_node;
8222 /* Parse a simple-type-specifier.
8224 simple-type-specifier:
8225 :: [opt] nested-name-specifier [opt] type-name
8226 :: [opt] nested-name-specifier template template-id
8241 simple-type-specifier:
8242 __typeof__ unary-expression
8243 __typeof__ ( type-id )
8245 For the various keywords, the value returned is simply the
8246 TREE_IDENTIFIER representing the keyword if IDENTIFIER_P is true.
8247 For the first two productions, and if IDENTIFIER_P is false, the
8248 value returned is the indicated TYPE_DECL. */
8251 cp_parser_simple_type_specifier (cp_parser* parser, cp_parser_flags flags,
8254 tree type = NULL_TREE;
8257 /* Peek at the next token. */
8258 token = cp_lexer_peek_token (parser->lexer);
8260 /* If we're looking at a keyword, things are easy. */
8261 switch (token->keyword)
8264 type = char_type_node;
8267 type = wchar_type_node;
8270 type = boolean_type_node;
8273 type = short_integer_type_node;
8276 type = integer_type_node;
8279 type = long_integer_type_node;
8282 type = integer_type_node;
8285 type = unsigned_type_node;
8288 type = float_type_node;
8291 type = double_type_node;
8294 type = void_type_node;
8301 /* Consume the `typeof' token. */
8302 cp_lexer_consume_token (parser->lexer);
8303 /* Parse the operand to `typeof' */
8304 operand = cp_parser_sizeof_operand (parser, RID_TYPEOF);
8305 /* If it is not already a TYPE, take its type. */
8306 if (!TYPE_P (operand))
8307 operand = finish_typeof (operand);
8316 /* If the type-specifier was for a built-in type, we're done. */
8321 /* Consume the token. */
8322 id = cp_lexer_consume_token (parser->lexer)->value;
8323 return identifier_p ? id : TYPE_NAME (type);
8326 /* The type-specifier must be a user-defined type. */
8327 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
8329 /* Don't gobble tokens or issue error messages if this is an
8330 optional type-specifier. */
8331 if (flags & CP_PARSER_FLAGS_OPTIONAL)
8332 cp_parser_parse_tentatively (parser);
8334 /* Look for the optional `::' operator. */
8335 cp_parser_global_scope_opt (parser,
8336 /*current_scope_valid_p=*/false);
8337 /* Look for the nested-name specifier. */
8338 cp_parser_nested_name_specifier_opt (parser,
8339 /*typename_keyword_p=*/false,
8340 /*check_dependency_p=*/true,
8342 /* If we have seen a nested-name-specifier, and the next token
8343 is `template', then we are using the template-id production. */
8345 && cp_parser_optional_template_keyword (parser))
8347 /* Look for the template-id. */
8348 type = cp_parser_template_id (parser,
8349 /*template_keyword_p=*/true,
8350 /*check_dependency_p=*/true);
8351 /* If the template-id did not name a type, we are out of
8353 if (TREE_CODE (type) != TYPE_DECL)
8355 cp_parser_error (parser, "expected template-id for type");
8359 /* Otherwise, look for a type-name. */
8362 type = cp_parser_type_name (parser);
8363 if (type == error_mark_node)
8367 /* If it didn't work out, we don't have a TYPE. */
8368 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
8369 && !cp_parser_parse_definitely (parser))
8373 /* If we didn't get a type-name, issue an error message. */
8374 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
8376 cp_parser_error (parser, "expected type-name");
8377 return error_mark_node;
8383 /* Parse a type-name.
8396 Returns a TYPE_DECL for the the type. */
8399 cp_parser_type_name (cp_parser* parser)
8404 /* We can't know yet whether it is a class-name or not. */
8405 cp_parser_parse_tentatively (parser);
8406 /* Try a class-name. */
8407 type_decl = cp_parser_class_name (parser,
8408 /*typename_keyword_p=*/false,
8409 /*template_keyword_p=*/false,
8411 /*check_dependency_p=*/true,
8412 /*class_head_p=*/false);
8413 /* If it's not a class-name, keep looking. */
8414 if (!cp_parser_parse_definitely (parser))
8416 /* It must be a typedef-name or an enum-name. */
8417 identifier = cp_parser_identifier (parser);
8418 if (identifier == error_mark_node)
8419 return error_mark_node;
8421 /* Look up the type-name. */
8422 type_decl = cp_parser_lookup_name_simple (parser, identifier);
8423 /* Issue an error if we did not find a type-name. */
8424 if (TREE_CODE (type_decl) != TYPE_DECL)
8426 cp_parser_error (parser, "expected type-name");
8427 type_decl = error_mark_node;
8429 /* Remember that the name was used in the definition of the
8430 current class so that we can check later to see if the
8431 meaning would have been different after the class was
8432 entirely defined. */
8433 else if (type_decl != error_mark_node
8435 maybe_note_name_used_in_class (identifier, type_decl);
8442 /* Parse an elaborated-type-specifier. Note that the grammar given
8443 here incorporates the resolution to DR68.
8445 elaborated-type-specifier:
8446 class-key :: [opt] nested-name-specifier [opt] identifier
8447 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
8448 enum :: [opt] nested-name-specifier [opt] identifier
8449 typename :: [opt] nested-name-specifier identifier
8450 typename :: [opt] nested-name-specifier template [opt]
8453 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
8454 declared `friend'. If IS_DECLARATION is TRUE, then this
8455 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
8456 something is being declared.
8458 Returns the TYPE specified. */
8461 cp_parser_elaborated_type_specifier (cp_parser* parser,
8463 bool is_declaration)
8465 enum tag_types tag_type;
8467 tree type = NULL_TREE;
8469 /* See if we're looking at the `enum' keyword. */
8470 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
8472 /* Consume the `enum' token. */
8473 cp_lexer_consume_token (parser->lexer);
8474 /* Remember that it's an enumeration type. */
8475 tag_type = enum_type;
8477 /* Or, it might be `typename'. */
8478 else if (cp_lexer_next_token_is_keyword (parser->lexer,
8481 /* Consume the `typename' token. */
8482 cp_lexer_consume_token (parser->lexer);
8483 /* Remember that it's a `typename' type. */
8484 tag_type = typename_type;
8485 /* The `typename' keyword is only allowed in templates. */
8486 if (!processing_template_decl)
8487 pedwarn ("using `typename' outside of template");
8489 /* Otherwise it must be a class-key. */
8492 tag_type = cp_parser_class_key (parser);
8493 if (tag_type == none_type)
8494 return error_mark_node;
8497 /* Look for the `::' operator. */
8498 cp_parser_global_scope_opt (parser,
8499 /*current_scope_valid_p=*/false);
8500 /* Look for the nested-name-specifier. */
8501 if (tag_type == typename_type)
8503 if (cp_parser_nested_name_specifier (parser,
8504 /*typename_keyword_p=*/true,
8505 /*check_dependency_p=*/true,
8508 return error_mark_node;
8511 /* Even though `typename' is not present, the proposed resolution
8512 to Core Issue 180 says that in `class A<T>::B', `B' should be
8513 considered a type-name, even if `A<T>' is dependent. */
8514 cp_parser_nested_name_specifier_opt (parser,
8515 /*typename_keyword_p=*/true,
8516 /*check_dependency_p=*/true,
8518 /* For everything but enumeration types, consider a template-id. */
8519 if (tag_type != enum_type)
8521 bool template_p = false;
8524 /* Allow the `template' keyword. */
8525 template_p = cp_parser_optional_template_keyword (parser);
8526 /* If we didn't see `template', we don't know if there's a
8527 template-id or not. */
8529 cp_parser_parse_tentatively (parser);
8530 /* Parse the template-id. */
8531 decl = cp_parser_template_id (parser, template_p,
8532 /*check_dependency_p=*/true);
8533 /* If we didn't find a template-id, look for an ordinary
8535 if (!template_p && !cp_parser_parse_definitely (parser))
8537 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
8538 in effect, then we must assume that, upon instantiation, the
8539 template will correspond to a class. */
8540 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
8541 && tag_type == typename_type)
8542 type = make_typename_type (parser->scope, decl,
8545 type = TREE_TYPE (decl);
8548 /* For an enumeration type, consider only a plain identifier. */
8551 identifier = cp_parser_identifier (parser);
8553 if (identifier == error_mark_node)
8555 parser->scope = NULL_TREE;
8556 return error_mark_node;
8559 /* For a `typename', we needn't call xref_tag. */
8560 if (tag_type == typename_type)
8561 return make_typename_type (parser->scope, identifier,
8563 /* Look up a qualified name in the usual way. */
8568 /* In an elaborated-type-specifier, names are assumed to name
8569 types, so we set IS_TYPE to TRUE when calling
8570 cp_parser_lookup_name. */
8571 decl = cp_parser_lookup_name (parser, identifier,
8573 /*is_namespace=*/false,
8574 /*check_dependency=*/true);
8576 /* If we are parsing friend declaration, DECL may be a
8577 TEMPLATE_DECL tree node here. However, we need to check
8578 whether this TEMPLATE_DECL results in valid code. Consider
8579 the following example:
8582 template <class T> class C {};
8585 template <class T> friend class N::C; // #1, valid code
8587 template <class T> class Y {
8588 friend class N::C; // #2, invalid code
8591 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
8592 name lookup of `N::C'. We see that friend declaration must
8593 be template for the code to be valid. Note that
8594 processing_template_decl does not work here since it is
8595 always 1 for the above two cases. */
8597 decl = (cp_parser_maybe_treat_template_as_class
8598 (decl, /*tag_name_p=*/is_friend
8599 && parser->num_template_parameter_lists));
8601 if (TREE_CODE (decl) != TYPE_DECL)
8603 error ("expected type-name");
8604 return error_mark_node;
8607 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
8608 check_elaborated_type_specifier
8610 (parser->num_template_parameter_lists
8611 || DECL_SELF_REFERENCE_P (decl)));
8613 type = TREE_TYPE (decl);
8617 /* An elaborated-type-specifier sometimes introduces a new type and
8618 sometimes names an existing type. Normally, the rule is that it
8619 introduces a new type only if there is not an existing type of
8620 the same name already in scope. For example, given:
8623 void f() { struct S s; }
8625 the `struct S' in the body of `f' is the same `struct S' as in
8626 the global scope; the existing definition is used. However, if
8627 there were no global declaration, this would introduce a new
8628 local class named `S'.
8630 An exception to this rule applies to the following code:
8632 namespace N { struct S; }
8634 Here, the elaborated-type-specifier names a new type
8635 unconditionally; even if there is already an `S' in the
8636 containing scope this declaration names a new type.
8637 This exception only applies if the elaborated-type-specifier
8638 forms the complete declaration:
8642 A declaration consisting solely of `class-key identifier ;' is
8643 either a redeclaration of the name in the current scope or a
8644 forward declaration of the identifier as a class name. It
8645 introduces the name into the current scope.
8647 We are in this situation precisely when the next token is a `;'.
8649 An exception to the exception is that a `friend' declaration does
8650 *not* name a new type; i.e., given:
8652 struct S { friend struct T; };
8654 `T' is not a new type in the scope of `S'.
8656 Also, `new struct S' or `sizeof (struct S)' never results in the
8657 definition of a new type; a new type can only be declared in a
8658 declaration context. */
8660 type = xref_tag (tag_type, identifier,
8661 /*attributes=*/NULL_TREE,
8664 || cp_lexer_next_token_is_not (parser->lexer,
8666 parser->num_template_parameter_lists);
8669 if (tag_type != enum_type)
8670 cp_parser_check_class_key (tag_type, type);
8674 /* Parse an enum-specifier.
8677 enum identifier [opt] { enumerator-list [opt] }
8679 Returns an ENUM_TYPE representing the enumeration. */
8682 cp_parser_enum_specifier (cp_parser* parser)
8685 tree identifier = NULL_TREE;
8688 /* Look for the `enum' keyword. */
8689 if (!cp_parser_require_keyword (parser, RID_ENUM, "`enum'"))
8690 return error_mark_node;
8691 /* Peek at the next token. */
8692 token = cp_lexer_peek_token (parser->lexer);
8694 /* See if it is an identifier. */
8695 if (token->type == CPP_NAME)
8696 identifier = cp_parser_identifier (parser);
8698 /* Look for the `{'. */
8699 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
8700 return error_mark_node;
8702 /* At this point, we're going ahead with the enum-specifier, even
8703 if some other problem occurs. */
8704 cp_parser_commit_to_tentative_parse (parser);
8706 /* Issue an error message if type-definitions are forbidden here. */
8707 cp_parser_check_type_definition (parser);
8709 /* Create the new type. */
8710 type = start_enum (identifier ? identifier : make_anon_name ());
8712 /* Peek at the next token. */
8713 token = cp_lexer_peek_token (parser->lexer);
8714 /* If it's not a `}', then there are some enumerators. */
8715 if (token->type != CPP_CLOSE_BRACE)
8716 cp_parser_enumerator_list (parser, type);
8717 /* Look for the `}'. */
8718 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
8720 /* Finish up the enumeration. */
8726 /* Parse an enumerator-list. The enumerators all have the indicated
8730 enumerator-definition
8731 enumerator-list , enumerator-definition */
8734 cp_parser_enumerator_list (cp_parser* parser, tree type)
8740 /* Parse an enumerator-definition. */
8741 cp_parser_enumerator_definition (parser, type);
8742 /* Peek at the next token. */
8743 token = cp_lexer_peek_token (parser->lexer);
8744 /* If it's not a `,', then we've reached the end of the
8746 if (token->type != CPP_COMMA)
8748 /* Otherwise, consume the `,' and keep going. */
8749 cp_lexer_consume_token (parser->lexer);
8750 /* If the next token is a `}', there is a trailing comma. */
8751 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
8753 if (pedantic && !in_system_header)
8754 pedwarn ("comma at end of enumerator list");
8760 /* Parse an enumerator-definition. The enumerator has the indicated
8763 enumerator-definition:
8765 enumerator = constant-expression
8771 cp_parser_enumerator_definition (cp_parser* parser, tree type)
8777 /* Look for the identifier. */
8778 identifier = cp_parser_identifier (parser);
8779 if (identifier == error_mark_node)
8782 /* Peek at the next token. */
8783 token = cp_lexer_peek_token (parser->lexer);
8784 /* If it's an `=', then there's an explicit value. */
8785 if (token->type == CPP_EQ)
8787 /* Consume the `=' token. */
8788 cp_lexer_consume_token (parser->lexer);
8789 /* Parse the value. */
8790 value = cp_parser_constant_expression (parser,
8791 /*allow_non_constant_p=*/false,
8797 /* Create the enumerator. */
8798 build_enumerator (identifier, value, type);
8801 /* Parse a namespace-name.
8804 original-namespace-name
8807 Returns the NAMESPACE_DECL for the namespace. */
8810 cp_parser_namespace_name (cp_parser* parser)
8813 tree namespace_decl;
8815 /* Get the name of the namespace. */
8816 identifier = cp_parser_identifier (parser);
8817 if (identifier == error_mark_node)
8818 return error_mark_node;
8820 /* Look up the identifier in the currently active scope. Look only
8821 for namespaces, due to:
8825 When looking up a namespace-name in a using-directive or alias
8826 definition, only namespace names are considered.
8832 During the lookup of a name preceding the :: scope resolution
8833 operator, object, function, and enumerator names are ignored.
8835 (Note that cp_parser_class_or_namespace_name only calls this
8836 function if the token after the name is the scope resolution
8838 namespace_decl = cp_parser_lookup_name (parser, identifier,
8840 /*is_namespace=*/true,
8841 /*check_dependency=*/true);
8842 /* If it's not a namespace, issue an error. */
8843 if (namespace_decl == error_mark_node
8844 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
8846 cp_parser_error (parser, "expected namespace-name");
8847 namespace_decl = error_mark_node;
8850 return namespace_decl;
8853 /* Parse a namespace-definition.
8855 namespace-definition:
8856 named-namespace-definition
8857 unnamed-namespace-definition
8859 named-namespace-definition:
8860 original-namespace-definition
8861 extension-namespace-definition
8863 original-namespace-definition:
8864 namespace identifier { namespace-body }
8866 extension-namespace-definition:
8867 namespace original-namespace-name { namespace-body }
8869 unnamed-namespace-definition:
8870 namespace { namespace-body } */
8873 cp_parser_namespace_definition (cp_parser* parser)
8877 /* Look for the `namespace' keyword. */
8878 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
8880 /* Get the name of the namespace. We do not attempt to distinguish
8881 between an original-namespace-definition and an
8882 extension-namespace-definition at this point. The semantic
8883 analysis routines are responsible for that. */
8884 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
8885 identifier = cp_parser_identifier (parser);
8887 identifier = NULL_TREE;
8889 /* Look for the `{' to start the namespace. */
8890 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
8891 /* Start the namespace. */
8892 push_namespace (identifier);
8893 /* Parse the body of the namespace. */
8894 cp_parser_namespace_body (parser);
8895 /* Finish the namespace. */
8897 /* Look for the final `}'. */
8898 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
8901 /* Parse a namespace-body.
8904 declaration-seq [opt] */
8907 cp_parser_namespace_body (cp_parser* parser)
8909 cp_parser_declaration_seq_opt (parser);
8912 /* Parse a namespace-alias-definition.
8914 namespace-alias-definition:
8915 namespace identifier = qualified-namespace-specifier ; */
8918 cp_parser_namespace_alias_definition (cp_parser* parser)
8921 tree namespace_specifier;
8923 /* Look for the `namespace' keyword. */
8924 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
8925 /* Look for the identifier. */
8926 identifier = cp_parser_identifier (parser);
8927 if (identifier == error_mark_node)
8929 /* Look for the `=' token. */
8930 cp_parser_require (parser, CPP_EQ, "`='");
8931 /* Look for the qualified-namespace-specifier. */
8933 = cp_parser_qualified_namespace_specifier (parser);
8934 /* Look for the `;' token. */
8935 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
8937 /* Register the alias in the symbol table. */
8938 do_namespace_alias (identifier, namespace_specifier);
8941 /* Parse a qualified-namespace-specifier.
8943 qualified-namespace-specifier:
8944 :: [opt] nested-name-specifier [opt] namespace-name
8946 Returns a NAMESPACE_DECL corresponding to the specified
8950 cp_parser_qualified_namespace_specifier (cp_parser* parser)
8952 /* Look for the optional `::'. */
8953 cp_parser_global_scope_opt (parser,
8954 /*current_scope_valid_p=*/false);
8956 /* Look for the optional nested-name-specifier. */
8957 cp_parser_nested_name_specifier_opt (parser,
8958 /*typename_keyword_p=*/false,
8959 /*check_dependency_p=*/true,
8962 return cp_parser_namespace_name (parser);
8965 /* Parse a using-declaration.
8968 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
8969 using :: unqualified-id ; */
8972 cp_parser_using_declaration (cp_parser* parser)
8975 bool typename_p = false;
8976 bool global_scope_p;
8981 /* Look for the `using' keyword. */
8982 cp_parser_require_keyword (parser, RID_USING, "`using'");
8984 /* Peek at the next token. */
8985 token = cp_lexer_peek_token (parser->lexer);
8986 /* See if it's `typename'. */
8987 if (token->keyword == RID_TYPENAME)
8989 /* Remember that we've seen it. */
8991 /* Consume the `typename' token. */
8992 cp_lexer_consume_token (parser->lexer);
8995 /* Look for the optional global scope qualification. */
8997 = (cp_parser_global_scope_opt (parser,
8998 /*current_scope_valid_p=*/false)
9001 /* If we saw `typename', or didn't see `::', then there must be a
9002 nested-name-specifier present. */
9003 if (typename_p || !global_scope_p)
9004 cp_parser_nested_name_specifier (parser, typename_p,
9005 /*check_dependency_p=*/true,
9007 /* Otherwise, we could be in either of the two productions. In that
9008 case, treat the nested-name-specifier as optional. */
9010 cp_parser_nested_name_specifier_opt (parser,
9011 /*typename_keyword_p=*/false,
9012 /*check_dependency_p=*/true,
9015 /* Parse the unqualified-id. */
9016 identifier = cp_parser_unqualified_id (parser,
9017 /*template_keyword_p=*/false,
9018 /*check_dependency_p=*/true,
9019 /*declarator_p=*/true);
9021 /* The function we call to handle a using-declaration is different
9022 depending on what scope we are in. */
9023 if (identifier == error_mark_node)
9025 else if (TREE_CODE (identifier) != IDENTIFIER_NODE
9026 && TREE_CODE (identifier) != BIT_NOT_EXPR)
9027 /* [namespace.udecl]
9029 A using declaration shall not name a template-id. */
9030 error ("a template-id may not appear in a using-declaration");
9033 scope = current_scope ();
9034 if (scope && TYPE_P (scope))
9036 /* Create the USING_DECL. */
9037 decl = do_class_using_decl (build_nt (SCOPE_REF,
9040 /* Add it to the list of members in this class. */
9041 finish_member_declaration (decl);
9045 decl = cp_parser_lookup_name_simple (parser, identifier);
9046 if (decl == error_mark_node)
9048 if (parser->scope && parser->scope != global_namespace)
9049 error ("`%D::%D' has not been declared",
9050 parser->scope, identifier);
9052 error ("`::%D' has not been declared", identifier);
9055 do_local_using_decl (decl);
9057 do_toplevel_using_decl (decl);
9061 /* Look for the final `;'. */
9062 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
9065 /* Parse a using-directive.
9068 using namespace :: [opt] nested-name-specifier [opt]
9072 cp_parser_using_directive (cp_parser* parser)
9074 tree namespace_decl;
9076 /* Look for the `using' keyword. */
9077 cp_parser_require_keyword (parser, RID_USING, "`using'");
9078 /* And the `namespace' keyword. */
9079 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
9080 /* Look for the optional `::' operator. */
9081 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
9082 /* And the optional nested-name-specifier. */
9083 cp_parser_nested_name_specifier_opt (parser,
9084 /*typename_keyword_p=*/false,
9085 /*check_dependency_p=*/true,
9087 /* Get the namespace being used. */
9088 namespace_decl = cp_parser_namespace_name (parser);
9089 /* Update the symbol table. */
9090 do_using_directive (namespace_decl);
9091 /* Look for the final `;'. */
9092 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
9095 /* Parse an asm-definition.
9098 asm ( string-literal ) ;
9103 asm volatile [opt] ( string-literal ) ;
9104 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
9105 asm volatile [opt] ( string-literal : asm-operand-list [opt]
9106 : asm-operand-list [opt] ) ;
9107 asm volatile [opt] ( string-literal : asm-operand-list [opt]
9108 : asm-operand-list [opt]
9109 : asm-operand-list [opt] ) ; */
9112 cp_parser_asm_definition (cp_parser* parser)
9116 tree outputs = NULL_TREE;
9117 tree inputs = NULL_TREE;
9118 tree clobbers = NULL_TREE;
9120 bool volatile_p = false;
9121 bool extended_p = false;
9123 /* Look for the `asm' keyword. */
9124 cp_parser_require_keyword (parser, RID_ASM, "`asm'");
9125 /* See if the next token is `volatile'. */
9126 if (cp_parser_allow_gnu_extensions_p (parser)
9127 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
9129 /* Remember that we saw the `volatile' keyword. */
9131 /* Consume the token. */
9132 cp_lexer_consume_token (parser->lexer);
9134 /* Look for the opening `('. */
9135 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
9136 /* Look for the string. */
9137 token = cp_parser_require (parser, CPP_STRING, "asm body");
9140 string = token->value;
9141 /* If we're allowing GNU extensions, check for the extended assembly
9142 syntax. Unfortunately, the `:' tokens need not be separated by
9143 a space in C, and so, for compatibility, we tolerate that here
9144 too. Doing that means that we have to treat the `::' operator as
9146 if (cp_parser_allow_gnu_extensions_p (parser)
9147 && at_function_scope_p ()
9148 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
9149 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
9151 bool inputs_p = false;
9152 bool clobbers_p = false;
9154 /* The extended syntax was used. */
9157 /* Look for outputs. */
9158 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
9160 /* Consume the `:'. */
9161 cp_lexer_consume_token (parser->lexer);
9162 /* Parse the output-operands. */
9163 if (cp_lexer_next_token_is_not (parser->lexer,
9165 && cp_lexer_next_token_is_not (parser->lexer,
9167 && cp_lexer_next_token_is_not (parser->lexer,
9169 outputs = cp_parser_asm_operand_list (parser);
9171 /* If the next token is `::', there are no outputs, and the
9172 next token is the beginning of the inputs. */
9173 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
9175 /* Consume the `::' token. */
9176 cp_lexer_consume_token (parser->lexer);
9177 /* The inputs are coming next. */
9181 /* Look for inputs. */
9183 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
9186 /* Consume the `:'. */
9187 cp_lexer_consume_token (parser->lexer);
9188 /* Parse the output-operands. */
9189 if (cp_lexer_next_token_is_not (parser->lexer,
9191 && cp_lexer_next_token_is_not (parser->lexer,
9193 && cp_lexer_next_token_is_not (parser->lexer,
9195 inputs = cp_parser_asm_operand_list (parser);
9197 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
9198 /* The clobbers are coming next. */
9201 /* Look for clobbers. */
9203 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
9206 /* Consume the `:'. */
9207 cp_lexer_consume_token (parser->lexer);
9208 /* Parse the clobbers. */
9209 if (cp_lexer_next_token_is_not (parser->lexer,
9211 clobbers = cp_parser_asm_clobber_list (parser);
9214 /* Look for the closing `)'. */
9215 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
9216 cp_parser_skip_to_closing_parenthesis (parser, true, false);
9217 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
9219 /* Create the ASM_STMT. */
9220 if (at_function_scope_p ())
9223 finish_asm_stmt (volatile_p
9224 ? ridpointers[(int) RID_VOLATILE] : NULL_TREE,
9225 string, outputs, inputs, clobbers);
9226 /* If the extended syntax was not used, mark the ASM_STMT. */
9228 ASM_INPUT_P (asm_stmt) = 1;
9231 assemble_asm (string);
9234 /* Declarators [gram.dcl.decl] */
9236 /* Parse an init-declarator.
9239 declarator initializer [opt]
9244 declarator asm-specification [opt] attributes [opt] initializer [opt]
9246 The DECL_SPECIFIERS and PREFIX_ATTRIBUTES apply to this declarator.
9247 Returns a representation of the entity declared. If MEMBER_P is TRUE,
9248 then this declarator appears in a class scope. The new DECL created
9249 by this declarator is returned.
9251 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
9252 for a function-definition here as well. If the declarator is a
9253 declarator for a function-definition, *FUNCTION_DEFINITION_P will
9254 be TRUE upon return. By that point, the function-definition will
9255 have been completely parsed.
9257 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
9261 cp_parser_init_declarator (cp_parser* parser,
9262 tree decl_specifiers,
9263 tree prefix_attributes,
9264 bool function_definition_allowed_p,
9266 int declares_class_or_enum,
9267 bool* function_definition_p)
9272 tree asm_specification;
9274 tree decl = NULL_TREE;
9276 bool is_initialized;
9277 bool is_parenthesized_init;
9278 bool is_non_constant_init;
9279 int ctor_dtor_or_conv_p;
9282 /* Assume that this is not the declarator for a function
9284 if (function_definition_p)
9285 *function_definition_p = false;
9287 /* Defer access checks while parsing the declarator; we cannot know
9288 what names are accessible until we know what is being
9290 resume_deferring_access_checks ();
9292 /* Parse the declarator. */
9294 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
9295 &ctor_dtor_or_conv_p);
9296 /* Gather up the deferred checks. */
9297 stop_deferring_access_checks ();
9299 /* If the DECLARATOR was erroneous, there's no need to go
9301 if (declarator == error_mark_node)
9302 return error_mark_node;
9304 cp_parser_check_for_definition_in_return_type (declarator,
9305 declares_class_or_enum);
9307 /* Figure out what scope the entity declared by the DECLARATOR is
9308 located in. `grokdeclarator' sometimes changes the scope, so
9309 we compute it now. */
9310 scope = get_scope_of_declarator (declarator);
9312 /* If we're allowing GNU extensions, look for an asm-specification
9314 if (cp_parser_allow_gnu_extensions_p (parser))
9316 /* Look for an asm-specification. */
9317 asm_specification = cp_parser_asm_specification_opt (parser);
9318 /* And attributes. */
9319 attributes = cp_parser_attributes_opt (parser);
9323 asm_specification = NULL_TREE;
9324 attributes = NULL_TREE;
9327 /* Peek at the next token. */
9328 token = cp_lexer_peek_token (parser->lexer);
9329 /* Check to see if the token indicates the start of a
9330 function-definition. */
9331 if (cp_parser_token_starts_function_definition_p (token))
9333 if (!function_definition_allowed_p)
9335 /* If a function-definition should not appear here, issue an
9337 cp_parser_error (parser,
9338 "a function-definition is not allowed here");
9339 return error_mark_node;
9343 /* Neither attributes nor an asm-specification are allowed
9344 on a function-definition. */
9345 if (asm_specification)
9346 error ("an asm-specification is not allowed on a function-definition");
9348 error ("attributes are not allowed on a function-definition");
9349 /* This is a function-definition. */
9350 *function_definition_p = true;
9352 /* Parse the function definition. */
9353 decl = (cp_parser_function_definition_from_specifiers_and_declarator
9354 (parser, decl_specifiers, prefix_attributes, declarator));
9362 Only in function declarations for constructors, destructors, and
9363 type conversions can the decl-specifier-seq be omitted.
9365 We explicitly postpone this check past the point where we handle
9366 function-definitions because we tolerate function-definitions
9367 that are missing their return types in some modes. */
9368 if (!decl_specifiers && ctor_dtor_or_conv_p <= 0)
9370 cp_parser_error (parser,
9371 "expected constructor, destructor, or type conversion");
9372 return error_mark_node;
9375 /* An `=' or an `(' indicates an initializer. */
9376 is_initialized = (token->type == CPP_EQ
9377 || token->type == CPP_OPEN_PAREN);
9378 /* If the init-declarator isn't initialized and isn't followed by a
9379 `,' or `;', it's not a valid init-declarator. */
9381 && token->type != CPP_COMMA
9382 && token->type != CPP_SEMICOLON)
9384 cp_parser_error (parser, "expected init-declarator");
9385 return error_mark_node;
9388 /* Because start_decl has side-effects, we should only call it if we
9389 know we're going ahead. By this point, we know that we cannot
9390 possibly be looking at any other construct. */
9391 cp_parser_commit_to_tentative_parse (parser);
9393 /* Check to see whether or not this declaration is a friend. */
9394 friend_p = cp_parser_friend_p (decl_specifiers);
9396 /* Check that the number of template-parameter-lists is OK. */
9397 if (!cp_parser_check_declarator_template_parameters (parser, declarator))
9398 return error_mark_node;
9400 /* Enter the newly declared entry in the symbol table. If we're
9401 processing a declaration in a class-specifier, we wait until
9402 after processing the initializer. */
9405 if (parser->in_unbraced_linkage_specification_p)
9407 decl_specifiers = tree_cons (error_mark_node,
9408 get_identifier ("extern"),
9410 have_extern_spec = false;
9412 decl = start_decl (declarator, decl_specifiers,
9413 is_initialized, attributes, prefix_attributes);
9416 /* Enter the SCOPE. That way unqualified names appearing in the
9417 initializer will be looked up in SCOPE. */
9421 /* Perform deferred access control checks, now that we know in which
9422 SCOPE the declared entity resides. */
9423 if (!member_p && decl)
9425 tree saved_current_function_decl = NULL_TREE;
9427 /* If the entity being declared is a function, pretend that we
9428 are in its scope. If it is a `friend', it may have access to
9429 things that would not otherwise be accessible. */
9430 if (TREE_CODE (decl) == FUNCTION_DECL)
9432 saved_current_function_decl = current_function_decl;
9433 current_function_decl = decl;
9436 /* Perform the access control checks for the declarator and the
9437 the decl-specifiers. */
9438 perform_deferred_access_checks ();
9440 /* Restore the saved value. */
9441 if (TREE_CODE (decl) == FUNCTION_DECL)
9442 current_function_decl = saved_current_function_decl;
9445 /* Parse the initializer. */
9447 initializer = cp_parser_initializer (parser,
9448 &is_parenthesized_init,
9449 &is_non_constant_init);
9452 initializer = NULL_TREE;
9453 is_parenthesized_init = false;
9454 is_non_constant_init = true;
9457 /* The old parser allows attributes to appear after a parenthesized
9458 initializer. Mark Mitchell proposed removing this functionality
9459 on the GCC mailing lists on 2002-08-13. This parser accepts the
9460 attributes -- but ignores them. */
9461 if (cp_parser_allow_gnu_extensions_p (parser) && is_parenthesized_init)
9462 if (cp_parser_attributes_opt (parser))
9463 warning ("attributes after parenthesized initializer ignored");
9465 /* Leave the SCOPE, now that we have processed the initializer. It
9466 is important to do this before calling cp_finish_decl because it
9467 makes decisions about whether to create DECL_STMTs or not based
9468 on the current scope. */
9472 /* For an in-class declaration, use `grokfield' to create the
9476 decl = grokfield (declarator, decl_specifiers,
9477 initializer, /*asmspec=*/NULL_TREE,
9478 /*attributes=*/NULL_TREE);
9479 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
9480 cp_parser_save_default_args (parser, decl);
9483 /* Finish processing the declaration. But, skip friend
9485 if (!friend_p && decl)
9486 cp_finish_decl (decl,
9489 /* If the initializer is in parentheses, then this is
9490 a direct-initialization, which means that an
9491 `explicit' constructor is OK. Otherwise, an
9492 `explicit' constructor cannot be used. */
9493 ((is_parenthesized_init || !is_initialized)
9494 ? 0 : LOOKUP_ONLYCONVERTING));
9496 /* Remember whether or not variables were initialized by
9497 constant-expressions. */
9498 if (decl && TREE_CODE (decl) == VAR_DECL
9499 && is_initialized && !is_non_constant_init)
9500 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = true;
9505 /* Parse a declarator.
9509 ptr-operator declarator
9511 abstract-declarator:
9512 ptr-operator abstract-declarator [opt]
9513 direct-abstract-declarator
9518 attributes [opt] direct-declarator
9519 attributes [opt] ptr-operator declarator
9521 abstract-declarator:
9522 attributes [opt] ptr-operator abstract-declarator [opt]
9523 attributes [opt] direct-abstract-declarator
9525 Returns a representation of the declarator. If the declarator has
9526 the form `* declarator', then an INDIRECT_REF is returned, whose
9527 only operand is the sub-declarator. Analogously, `& declarator' is
9528 represented as an ADDR_EXPR. For `X::* declarator', a SCOPE_REF is
9529 used. The first operand is the TYPE for `X'. The second operand
9530 is an INDIRECT_REF whose operand is the sub-declarator.
9532 Otherwise, the representation is as for a direct-declarator.
9534 (It would be better to define a structure type to represent
9535 declarators, rather than abusing `tree' nodes to represent
9536 declarators. That would be much clearer and save some memory.
9537 There is no reason for declarators to be garbage-collected, for
9538 example; they are created during parser and no longer needed after
9539 `grokdeclarator' has been called.)
9541 For a ptr-operator that has the optional cv-qualifier-seq,
9542 cv-qualifiers will be stored in the TREE_TYPE of the INDIRECT_REF
9545 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
9546 detect constructor, destructor or conversion operators. It is set
9547 to -1 if the declarator is a name, and +1 if it is a
9548 function. Otherwise it is set to zero. Usually you just want to
9549 test for >0, but internally the negative value is used.
9551 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
9552 a decl-specifier-seq unless it declares a constructor, destructor,
9553 or conversion. It might seem that we could check this condition in
9554 semantic analysis, rather than parsing, but that makes it difficult
9555 to handle something like `f()'. We want to notice that there are
9556 no decl-specifiers, and therefore realize that this is an
9557 expression, not a declaration.) */
9560 cp_parser_declarator (cp_parser* parser,
9561 cp_parser_declarator_kind dcl_kind,
9562 int* ctor_dtor_or_conv_p)
9566 enum tree_code code;
9567 tree cv_qualifier_seq;
9569 tree attributes = NULL_TREE;
9571 /* Assume this is not a constructor, destructor, or type-conversion
9573 if (ctor_dtor_or_conv_p)
9574 *ctor_dtor_or_conv_p = 0;
9576 if (cp_parser_allow_gnu_extensions_p (parser))
9577 attributes = cp_parser_attributes_opt (parser);
9579 /* Peek at the next token. */
9580 token = cp_lexer_peek_token (parser->lexer);
9582 /* Check for the ptr-operator production. */
9583 cp_parser_parse_tentatively (parser);
9584 /* Parse the ptr-operator. */
9585 code = cp_parser_ptr_operator (parser,
9588 /* If that worked, then we have a ptr-operator. */
9589 if (cp_parser_parse_definitely (parser))
9591 /* The dependent declarator is optional if we are parsing an
9592 abstract-declarator. */
9593 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
9594 cp_parser_parse_tentatively (parser);
9596 /* Parse the dependent declarator. */
9597 declarator = cp_parser_declarator (parser, dcl_kind,
9598 /*ctor_dtor_or_conv_p=*/NULL);
9600 /* If we are parsing an abstract-declarator, we must handle the
9601 case where the dependent declarator is absent. */
9602 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
9603 && !cp_parser_parse_definitely (parser))
9604 declarator = NULL_TREE;
9606 /* Build the representation of the ptr-operator. */
9607 if (code == INDIRECT_REF)
9608 declarator = make_pointer_declarator (cv_qualifier_seq,
9611 declarator = make_reference_declarator (cv_qualifier_seq,
9613 /* Handle the pointer-to-member case. */
9615 declarator = build_nt (SCOPE_REF, class_type, declarator);
9617 /* Everything else is a direct-declarator. */
9619 declarator = cp_parser_direct_declarator (parser, dcl_kind,
9620 ctor_dtor_or_conv_p);
9622 if (attributes && declarator != error_mark_node)
9623 declarator = tree_cons (attributes, declarator, NULL_TREE);
9628 /* Parse a direct-declarator or direct-abstract-declarator.
9632 direct-declarator ( parameter-declaration-clause )
9633 cv-qualifier-seq [opt]
9634 exception-specification [opt]
9635 direct-declarator [ constant-expression [opt] ]
9638 direct-abstract-declarator:
9639 direct-abstract-declarator [opt]
9640 ( parameter-declaration-clause )
9641 cv-qualifier-seq [opt]
9642 exception-specification [opt]
9643 direct-abstract-declarator [opt] [ constant-expression [opt] ]
9644 ( abstract-declarator )
9646 Returns a representation of the declarator. DCL_KIND is
9647 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
9648 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
9649 we are parsing a direct-declarator. It is
9650 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
9651 of ambiguity we prefer an abstract declarator, as per
9652 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P is as for
9653 cp_parser_declarator.
9655 For the declarator-id production, the representation is as for an
9656 id-expression, except that a qualified name is represented as a
9657 SCOPE_REF. A function-declarator is represented as a CALL_EXPR;
9658 see the documentation of the FUNCTION_DECLARATOR_* macros for
9659 information about how to find the various declarator components.
9660 An array-declarator is represented as an ARRAY_REF. The
9661 direct-declarator is the first operand; the constant-expression
9662 indicating the size of the array is the second operand. */
9665 cp_parser_direct_declarator (cp_parser* parser,
9666 cp_parser_declarator_kind dcl_kind,
9667 int* ctor_dtor_or_conv_p)
9670 tree declarator = NULL_TREE;
9671 tree scope = NULL_TREE;
9672 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
9673 bool saved_in_declarator_p = parser->in_declarator_p;
9678 /* Peek at the next token. */
9679 token = cp_lexer_peek_token (parser->lexer);
9680 if (token->type == CPP_OPEN_PAREN)
9682 /* This is either a parameter-declaration-clause, or a
9683 parenthesized declarator. When we know we are parsing a
9684 named declarator, it must be a parenthesized declarator
9685 if FIRST is true. For instance, `(int)' is a
9686 parameter-declaration-clause, with an omitted
9687 direct-abstract-declarator. But `((*))', is a
9688 parenthesized abstract declarator. Finally, when T is a
9689 template parameter `(T)' is a
9690 parameter-declaration-clause, and not a parenthesized
9693 We first try and parse a parameter-declaration-clause,
9694 and then try a nested declarator (if FIRST is true).
9696 It is not an error for it not to be a
9697 parameter-declaration-clause, even when FIRST is
9703 The first is the declaration of a function while the
9704 second is a the definition of a variable, including its
9707 Having seen only the parenthesis, we cannot know which of
9708 these two alternatives should be selected. Even more
9709 complex are examples like:
9714 The former is a function-declaration; the latter is a
9715 variable initialization.
9717 Thus again, we try a parameter-declaration-clause, and if
9718 that fails, we back out and return. */
9720 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
9724 cp_parser_parse_tentatively (parser);
9726 /* Consume the `('. */
9727 cp_lexer_consume_token (parser->lexer);
9730 /* If this is going to be an abstract declarator, we're
9731 in a declarator and we can't have default args. */
9732 parser->default_arg_ok_p = false;
9733 parser->in_declarator_p = true;
9736 /* Parse the parameter-declaration-clause. */
9737 params = cp_parser_parameter_declaration_clause (parser);
9739 /* If all went well, parse the cv-qualifier-seq and the
9740 exception-specification. */
9741 if (cp_parser_parse_definitely (parser))
9744 tree exception_specification;
9746 if (ctor_dtor_or_conv_p)
9747 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
9749 /* Consume the `)'. */
9750 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
9752 /* Parse the cv-qualifier-seq. */
9753 cv_qualifiers = cp_parser_cv_qualifier_seq_opt (parser);
9754 /* And the exception-specification. */
9755 exception_specification
9756 = cp_parser_exception_specification_opt (parser);
9758 /* Create the function-declarator. */
9759 declarator = make_call_declarator (declarator,
9762 exception_specification);
9763 /* Any subsequent parameter lists are to do with
9764 return type, so are not those of the declared
9766 parser->default_arg_ok_p = false;
9768 /* Repeat the main loop. */
9773 /* If this is the first, we can try a parenthesized
9777 parser->default_arg_ok_p = saved_default_arg_ok_p;
9778 parser->in_declarator_p = saved_in_declarator_p;
9780 /* Consume the `('. */
9781 cp_lexer_consume_token (parser->lexer);
9782 /* Parse the nested declarator. */
9784 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p);
9787 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
9788 declarator = error_mark_node;
9789 if (declarator == error_mark_node)
9792 goto handle_declarator;
9794 /* Otherwise, we must be done. */
9798 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
9799 && token->type == CPP_OPEN_SQUARE)
9801 /* Parse an array-declarator. */
9804 if (ctor_dtor_or_conv_p)
9805 *ctor_dtor_or_conv_p = 0;
9808 parser->default_arg_ok_p = false;
9809 parser->in_declarator_p = true;
9810 /* Consume the `['. */
9811 cp_lexer_consume_token (parser->lexer);
9812 /* Peek at the next token. */
9813 token = cp_lexer_peek_token (parser->lexer);
9814 /* If the next token is `]', then there is no
9815 constant-expression. */
9816 if (token->type != CPP_CLOSE_SQUARE)
9818 bool non_constant_p;
9821 = cp_parser_constant_expression (parser,
9822 /*allow_non_constant=*/true,
9824 if (!non_constant_p)
9825 bounds = cp_parser_fold_non_dependent_expr (bounds);
9829 /* Look for the closing `]'. */
9830 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'"))
9832 declarator = error_mark_node;
9836 declarator = build_nt (ARRAY_REF, declarator, bounds);
9838 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
9840 /* Parse a declarator_id */
9841 if (dcl_kind == CP_PARSER_DECLARATOR_EITHER)
9842 cp_parser_parse_tentatively (parser);
9843 declarator = cp_parser_declarator_id (parser);
9844 if (dcl_kind == CP_PARSER_DECLARATOR_EITHER)
9846 if (!cp_parser_parse_definitely (parser))
9847 declarator = error_mark_node;
9848 else if (TREE_CODE (declarator) != IDENTIFIER_NODE)
9850 cp_parser_error (parser, "expected unqualified-id");
9851 declarator = error_mark_node;
9855 if (declarator == error_mark_node)
9858 if (TREE_CODE (declarator) == SCOPE_REF)
9860 tree scope = TREE_OPERAND (declarator, 0);
9862 /* In the declaration of a member of a template class
9863 outside of the class itself, the SCOPE will sometimes
9864 be a TYPENAME_TYPE. For example, given:
9866 template <typename T>
9869 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
9870 this context, we must resolve S<T>::R to an ordinary
9871 type, rather than a typename type.
9873 The reason we normally avoid resolving TYPENAME_TYPEs
9874 is that a specialization of `S' might render
9875 `S<T>::R' not a type. However, if `S' is
9876 specialized, then this `i' will not be used, so there
9877 is no harm in resolving the types here. */
9878 if (TREE_CODE (scope) == TYPENAME_TYPE)
9882 /* Resolve the TYPENAME_TYPE. */
9883 type = resolve_typename_type (scope,
9884 /*only_current_p=*/false);
9885 /* If that failed, the declarator is invalid. */
9886 if (type != error_mark_node)
9888 /* Build a new DECLARATOR. */
9889 declarator = build_nt (SCOPE_REF,
9891 TREE_OPERAND (declarator, 1));
9895 /* Check to see whether the declarator-id names a constructor,
9896 destructor, or conversion. */
9897 if (declarator && ctor_dtor_or_conv_p
9898 && ((TREE_CODE (declarator) == SCOPE_REF
9899 && CLASS_TYPE_P (TREE_OPERAND (declarator, 0)))
9900 || (TREE_CODE (declarator) != SCOPE_REF
9901 && at_class_scope_p ())))
9903 tree unqualified_name;
9906 /* Get the unqualified part of the name. */
9907 if (TREE_CODE (declarator) == SCOPE_REF)
9909 class_type = TREE_OPERAND (declarator, 0);
9910 unqualified_name = TREE_OPERAND (declarator, 1);
9914 class_type = current_class_type;
9915 unqualified_name = declarator;
9918 /* See if it names ctor, dtor or conv. */
9919 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR
9920 || IDENTIFIER_TYPENAME_P (unqualified_name)
9921 || constructor_name_p (unqualified_name, class_type))
9922 *ctor_dtor_or_conv_p = -1;
9926 scope = get_scope_of_declarator (declarator);
9928 /* Any names that appear after the declarator-id for a member
9929 are looked up in the containing scope. */
9931 parser->in_declarator_p = true;
9932 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
9934 && (TREE_CODE (declarator) == SCOPE_REF
9935 || TREE_CODE (declarator) == IDENTIFIER_NODE)))
9936 /* Default args are only allowed on function
9938 parser->default_arg_ok_p = saved_default_arg_ok_p;
9940 parser->default_arg_ok_p = false;
9949 /* For an abstract declarator, we might wind up with nothing at this
9950 point. That's an error; the declarator is not optional. */
9952 cp_parser_error (parser, "expected declarator");
9954 /* If we entered a scope, we must exit it now. */
9958 parser->default_arg_ok_p = saved_default_arg_ok_p;
9959 parser->in_declarator_p = saved_in_declarator_p;
9964 /* Parse a ptr-operator.
9967 * cv-qualifier-seq [opt]
9969 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
9974 & cv-qualifier-seq [opt]
9976 Returns INDIRECT_REF if a pointer, or pointer-to-member, was
9977 used. Returns ADDR_EXPR if a reference was used. In the
9978 case of a pointer-to-member, *TYPE is filled in with the
9979 TYPE containing the member. *CV_QUALIFIER_SEQ is filled in
9980 with the cv-qualifier-seq, or NULL_TREE, if there are no
9981 cv-qualifiers. Returns ERROR_MARK if an error occurred. */
9983 static enum tree_code
9984 cp_parser_ptr_operator (cp_parser* parser,
9986 tree* cv_qualifier_seq)
9988 enum tree_code code = ERROR_MARK;
9991 /* Assume that it's not a pointer-to-member. */
9993 /* And that there are no cv-qualifiers. */
9994 *cv_qualifier_seq = NULL_TREE;
9996 /* Peek at the next token. */
9997 token = cp_lexer_peek_token (parser->lexer);
9998 /* If it's a `*' or `&' we have a pointer or reference. */
9999 if (token->type == CPP_MULT || token->type == CPP_AND)
10001 /* Remember which ptr-operator we were processing. */
10002 code = (token->type == CPP_AND ? ADDR_EXPR : INDIRECT_REF);
10004 /* Consume the `*' or `&'. */
10005 cp_lexer_consume_token (parser->lexer);
10007 /* A `*' can be followed by a cv-qualifier-seq, and so can a
10008 `&', if we are allowing GNU extensions. (The only qualifier
10009 that can legally appear after `&' is `restrict', but that is
10010 enforced during semantic analysis. */
10011 if (code == INDIRECT_REF
10012 || cp_parser_allow_gnu_extensions_p (parser))
10013 *cv_qualifier_seq = cp_parser_cv_qualifier_seq_opt (parser);
10017 /* Try the pointer-to-member case. */
10018 cp_parser_parse_tentatively (parser);
10019 /* Look for the optional `::' operator. */
10020 cp_parser_global_scope_opt (parser,
10021 /*current_scope_valid_p=*/false);
10022 /* Look for the nested-name specifier. */
10023 cp_parser_nested_name_specifier (parser,
10024 /*typename_keyword_p=*/false,
10025 /*check_dependency_p=*/true,
10027 /* If we found it, and the next token is a `*', then we are
10028 indeed looking at a pointer-to-member operator. */
10029 if (!cp_parser_error_occurred (parser)
10030 && cp_parser_require (parser, CPP_MULT, "`*'"))
10032 /* The type of which the member is a member is given by the
10034 *type = parser->scope;
10035 /* The next name will not be qualified. */
10036 parser->scope = NULL_TREE;
10037 parser->qualifying_scope = NULL_TREE;
10038 parser->object_scope = NULL_TREE;
10039 /* Indicate that the `*' operator was used. */
10040 code = INDIRECT_REF;
10041 /* Look for the optional cv-qualifier-seq. */
10042 *cv_qualifier_seq = cp_parser_cv_qualifier_seq_opt (parser);
10044 /* If that didn't work we don't have a ptr-operator. */
10045 if (!cp_parser_parse_definitely (parser))
10046 cp_parser_error (parser, "expected ptr-operator");
10052 /* Parse an (optional) cv-qualifier-seq.
10055 cv-qualifier cv-qualifier-seq [opt]
10057 Returns a TREE_LIST. The TREE_VALUE of each node is the
10058 representation of a cv-qualifier. */
10061 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
10063 tree cv_qualifiers = NULL_TREE;
10069 /* Look for the next cv-qualifier. */
10070 cv_qualifier = cp_parser_cv_qualifier_opt (parser);
10071 /* If we didn't find one, we're done. */
10075 /* Add this cv-qualifier to the list. */
10077 = tree_cons (NULL_TREE, cv_qualifier, cv_qualifiers);
10080 /* We built up the list in reverse order. */
10081 return nreverse (cv_qualifiers);
10084 /* Parse an (optional) cv-qualifier.
10096 cp_parser_cv_qualifier_opt (cp_parser* parser)
10099 tree cv_qualifier = NULL_TREE;
10101 /* Peek at the next token. */
10102 token = cp_lexer_peek_token (parser->lexer);
10103 /* See if it's a cv-qualifier. */
10104 switch (token->keyword)
10109 /* Save the value of the token. */
10110 cv_qualifier = token->value;
10111 /* Consume the token. */
10112 cp_lexer_consume_token (parser->lexer);
10119 return cv_qualifier;
10122 /* Parse a declarator-id.
10126 :: [opt] nested-name-specifier [opt] type-name
10128 In the `id-expression' case, the value returned is as for
10129 cp_parser_id_expression if the id-expression was an unqualified-id.
10130 If the id-expression was a qualified-id, then a SCOPE_REF is
10131 returned. The first operand is the scope (either a NAMESPACE_DECL
10132 or TREE_TYPE), but the second is still just a representation of an
10136 cp_parser_declarator_id (cp_parser* parser)
10138 tree id_expression;
10140 /* The expression must be an id-expression. Assume that qualified
10141 names are the names of types so that:
10144 int S<T>::R::i = 3;
10146 will work; we must treat `S<T>::R' as the name of a type.
10147 Similarly, assume that qualified names are templates, where
10151 int S<T>::R<T>::i = 3;
10154 id_expression = cp_parser_id_expression (parser,
10155 /*template_keyword_p=*/false,
10156 /*check_dependency_p=*/false,
10157 /*template_p=*/NULL,
10158 /*declarator_p=*/true);
10159 /* If the name was qualified, create a SCOPE_REF to represent
10163 id_expression = build_nt (SCOPE_REF, parser->scope, id_expression);
10164 parser->scope = NULL_TREE;
10167 return id_expression;
10170 /* Parse a type-id.
10173 type-specifier-seq abstract-declarator [opt]
10175 Returns the TYPE specified. */
10178 cp_parser_type_id (cp_parser* parser)
10180 tree type_specifier_seq;
10181 tree abstract_declarator;
10183 /* Parse the type-specifier-seq. */
10185 = cp_parser_type_specifier_seq (parser);
10186 if (type_specifier_seq == error_mark_node)
10187 return error_mark_node;
10189 /* There might or might not be an abstract declarator. */
10190 cp_parser_parse_tentatively (parser);
10191 /* Look for the declarator. */
10192 abstract_declarator
10193 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL);
10194 /* Check to see if there really was a declarator. */
10195 if (!cp_parser_parse_definitely (parser))
10196 abstract_declarator = NULL_TREE;
10198 return groktypename (build_tree_list (type_specifier_seq,
10199 abstract_declarator));
10202 /* Parse a type-specifier-seq.
10204 type-specifier-seq:
10205 type-specifier type-specifier-seq [opt]
10209 type-specifier-seq:
10210 attributes type-specifier-seq [opt]
10212 Returns a TREE_LIST. Either the TREE_VALUE of each node is a
10213 type-specifier, or the TREE_PURPOSE is a list of attributes. */
10216 cp_parser_type_specifier_seq (cp_parser* parser)
10218 bool seen_type_specifier = false;
10219 tree type_specifier_seq = NULL_TREE;
10221 /* Parse the type-specifiers and attributes. */
10224 tree type_specifier;
10226 /* Check for attributes first. */
10227 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
10229 type_specifier_seq = tree_cons (cp_parser_attributes_opt (parser),
10231 type_specifier_seq);
10235 /* After the first type-specifier, others are optional. */
10236 if (seen_type_specifier)
10237 cp_parser_parse_tentatively (parser);
10238 /* Look for the type-specifier. */
10239 type_specifier = cp_parser_type_specifier (parser,
10240 CP_PARSER_FLAGS_NONE,
10241 /*is_friend=*/false,
10242 /*is_declaration=*/false,
10245 /* If the first type-specifier could not be found, this is not a
10246 type-specifier-seq at all. */
10247 if (!seen_type_specifier && type_specifier == error_mark_node)
10248 return error_mark_node;
10249 /* If subsequent type-specifiers could not be found, the
10250 type-specifier-seq is complete. */
10251 else if (seen_type_specifier && !cp_parser_parse_definitely (parser))
10254 /* Add the new type-specifier to the list. */
10256 = tree_cons (NULL_TREE, type_specifier, type_specifier_seq);
10257 seen_type_specifier = true;
10260 /* We built up the list in reverse order. */
10261 return nreverse (type_specifier_seq);
10264 /* Parse a parameter-declaration-clause.
10266 parameter-declaration-clause:
10267 parameter-declaration-list [opt] ... [opt]
10268 parameter-declaration-list , ...
10270 Returns a representation for the parameter declarations. Each node
10271 is a TREE_LIST. (See cp_parser_parameter_declaration for the exact
10272 representation.) If the parameter-declaration-clause ends with an
10273 ellipsis, PARMLIST_ELLIPSIS_P will hold of the first node in the
10274 list. A return value of NULL_TREE indicates a
10275 parameter-declaration-clause consisting only of an ellipsis. */
10278 cp_parser_parameter_declaration_clause (cp_parser* parser)
10284 /* Peek at the next token. */
10285 token = cp_lexer_peek_token (parser->lexer);
10286 /* Check for trivial parameter-declaration-clauses. */
10287 if (token->type == CPP_ELLIPSIS)
10289 /* Consume the `...' token. */
10290 cp_lexer_consume_token (parser->lexer);
10293 else if (token->type == CPP_CLOSE_PAREN)
10294 /* There are no parameters. */
10296 #ifndef NO_IMPLICIT_EXTERN_C
10297 if (in_system_header && current_class_type == NULL
10298 && current_lang_name == lang_name_c)
10302 return void_list_node;
10304 /* Check for `(void)', too, which is a special case. */
10305 else if (token->keyword == RID_VOID
10306 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
10307 == CPP_CLOSE_PAREN))
10309 /* Consume the `void' token. */
10310 cp_lexer_consume_token (parser->lexer);
10311 /* There are no parameters. */
10312 return void_list_node;
10315 /* Parse the parameter-declaration-list. */
10316 parameters = cp_parser_parameter_declaration_list (parser);
10317 /* If a parse error occurred while parsing the
10318 parameter-declaration-list, then the entire
10319 parameter-declaration-clause is erroneous. */
10320 if (parameters == error_mark_node)
10321 return error_mark_node;
10323 /* Peek at the next token. */
10324 token = cp_lexer_peek_token (parser->lexer);
10325 /* If it's a `,', the clause should terminate with an ellipsis. */
10326 if (token->type == CPP_COMMA)
10328 /* Consume the `,'. */
10329 cp_lexer_consume_token (parser->lexer);
10330 /* Expect an ellipsis. */
10332 = (cp_parser_require (parser, CPP_ELLIPSIS, "`...'") != NULL);
10334 /* It might also be `...' if the optional trailing `,' was
10336 else if (token->type == CPP_ELLIPSIS)
10338 /* Consume the `...' token. */
10339 cp_lexer_consume_token (parser->lexer);
10340 /* And remember that we saw it. */
10344 ellipsis_p = false;
10346 /* Finish the parameter list. */
10347 return finish_parmlist (parameters, ellipsis_p);
10350 /* Parse a parameter-declaration-list.
10352 parameter-declaration-list:
10353 parameter-declaration
10354 parameter-declaration-list , parameter-declaration
10356 Returns a representation of the parameter-declaration-list, as for
10357 cp_parser_parameter_declaration_clause. However, the
10358 `void_list_node' is never appended to the list. */
10361 cp_parser_parameter_declaration_list (cp_parser* parser)
10363 tree parameters = NULL_TREE;
10365 /* Look for more parameters. */
10369 /* Parse the parameter. */
10371 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/false);
10373 /* If a parse error occurred parsing the parameter declaration,
10374 then the entire parameter-declaration-list is erroneous. */
10375 if (parameter == error_mark_node)
10377 parameters = error_mark_node;
10380 /* Add the new parameter to the list. */
10381 TREE_CHAIN (parameter) = parameters;
10382 parameters = parameter;
10384 /* Peek at the next token. */
10385 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
10386 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10387 /* The parameter-declaration-list is complete. */
10389 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
10393 /* Peek at the next token. */
10394 token = cp_lexer_peek_nth_token (parser->lexer, 2);
10395 /* If it's an ellipsis, then the list is complete. */
10396 if (token->type == CPP_ELLIPSIS)
10398 /* Otherwise, there must be more parameters. Consume the
10400 cp_lexer_consume_token (parser->lexer);
10404 cp_parser_error (parser, "expected `,' or `...'");
10409 /* We built up the list in reverse order; straighten it out now. */
10410 return nreverse (parameters);
10413 /* Parse a parameter declaration.
10415 parameter-declaration:
10416 decl-specifier-seq declarator
10417 decl-specifier-seq declarator = assignment-expression
10418 decl-specifier-seq abstract-declarator [opt]
10419 decl-specifier-seq abstract-declarator [opt] = assignment-expression
10421 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
10422 declares a template parameter. (In that case, a non-nested `>'
10423 token encountered during the parsing of the assignment-expression
10424 is not interpreted as a greater-than operator.)
10426 Returns a TREE_LIST representing the parameter-declaration. The
10427 TREE_VALUE is a representation of the decl-specifier-seq and
10428 declarator. In particular, the TREE_VALUE will be a TREE_LIST
10429 whose TREE_PURPOSE represents the decl-specifier-seq and whose
10430 TREE_VALUE represents the declarator. */
10433 cp_parser_parameter_declaration (cp_parser *parser,
10434 bool template_parm_p)
10436 int declares_class_or_enum;
10437 bool greater_than_is_operator_p;
10438 tree decl_specifiers;
10441 tree default_argument;
10444 const char *saved_message;
10446 /* In a template parameter, `>' is not an operator.
10450 When parsing a default template-argument for a non-type
10451 template-parameter, the first non-nested `>' is taken as the end
10452 of the template parameter-list rather than a greater-than
10454 greater_than_is_operator_p = !template_parm_p;
10456 /* Type definitions may not appear in parameter types. */
10457 saved_message = parser->type_definition_forbidden_message;
10458 parser->type_definition_forbidden_message
10459 = "types may not be defined in parameter types";
10461 /* Parse the declaration-specifiers. */
10463 = cp_parser_decl_specifier_seq (parser,
10464 CP_PARSER_FLAGS_NONE,
10466 &declares_class_or_enum);
10467 /* If an error occurred, there's no reason to attempt to parse the
10468 rest of the declaration. */
10469 if (cp_parser_error_occurred (parser))
10471 parser->type_definition_forbidden_message = saved_message;
10472 return error_mark_node;
10475 /* Peek at the next token. */
10476 token = cp_lexer_peek_token (parser->lexer);
10477 /* If the next token is a `)', `,', `=', `>', or `...', then there
10478 is no declarator. */
10479 if (token->type == CPP_CLOSE_PAREN
10480 || token->type == CPP_COMMA
10481 || token->type == CPP_EQ
10482 || token->type == CPP_ELLIPSIS
10483 || token->type == CPP_GREATER)
10484 declarator = NULL_TREE;
10485 /* Otherwise, there should be a declarator. */
10488 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
10489 parser->default_arg_ok_p = false;
10491 declarator = cp_parser_declarator (parser,
10492 CP_PARSER_DECLARATOR_EITHER,
10493 /*ctor_dtor_or_conv_p=*/NULL);
10494 parser->default_arg_ok_p = saved_default_arg_ok_p;
10495 /* After the declarator, allow more attributes. */
10496 attributes = chainon (attributes, cp_parser_attributes_opt (parser));
10499 /* The restriction on defining new types applies only to the type
10500 of the parameter, not to the default argument. */
10501 parser->type_definition_forbidden_message = saved_message;
10503 /* If the next token is `=', then process a default argument. */
10504 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10506 bool saved_greater_than_is_operator_p;
10507 /* Consume the `='. */
10508 cp_lexer_consume_token (parser->lexer);
10510 /* If we are defining a class, then the tokens that make up the
10511 default argument must be saved and processed later. */
10512 if (!template_parm_p && at_class_scope_p ()
10513 && TYPE_BEING_DEFINED (current_class_type))
10515 unsigned depth = 0;
10517 /* Create a DEFAULT_ARG to represented the unparsed default
10519 default_argument = make_node (DEFAULT_ARG);
10520 DEFARG_TOKENS (default_argument) = cp_token_cache_new ();
10522 /* Add tokens until we have processed the entire default
10529 /* Peek at the next token. */
10530 token = cp_lexer_peek_token (parser->lexer);
10531 /* What we do depends on what token we have. */
10532 switch (token->type)
10534 /* In valid code, a default argument must be
10535 immediately followed by a `,' `)', or `...'. */
10537 case CPP_CLOSE_PAREN:
10539 /* If we run into a non-nested `;', `}', or `]',
10540 then the code is invalid -- but the default
10541 argument is certainly over. */
10542 case CPP_SEMICOLON:
10543 case CPP_CLOSE_BRACE:
10544 case CPP_CLOSE_SQUARE:
10547 /* Update DEPTH, if necessary. */
10548 else if (token->type == CPP_CLOSE_PAREN
10549 || token->type == CPP_CLOSE_BRACE
10550 || token->type == CPP_CLOSE_SQUARE)
10554 case CPP_OPEN_PAREN:
10555 case CPP_OPEN_SQUARE:
10556 case CPP_OPEN_BRACE:
10561 /* If we see a non-nested `>', and `>' is not an
10562 operator, then it marks the end of the default
10564 if (!depth && !greater_than_is_operator_p)
10568 /* If we run out of tokens, issue an error message. */
10570 error ("file ends in default argument");
10576 /* In these cases, we should look for template-ids.
10577 For example, if the default argument is
10578 `X<int, double>()', we need to do name lookup to
10579 figure out whether or not `X' is a template; if
10580 so, the `,' does not end the default argument.
10582 That is not yet done. */
10589 /* If we've reached the end, stop. */
10593 /* Add the token to the token block. */
10594 token = cp_lexer_consume_token (parser->lexer);
10595 cp_token_cache_push_token (DEFARG_TOKENS (default_argument),
10599 /* Outside of a class definition, we can just parse the
10600 assignment-expression. */
10603 bool saved_local_variables_forbidden_p;
10605 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
10607 saved_greater_than_is_operator_p
10608 = parser->greater_than_is_operator_p;
10609 parser->greater_than_is_operator_p = greater_than_is_operator_p;
10610 /* Local variable names (and the `this' keyword) may not
10611 appear in a default argument. */
10612 saved_local_variables_forbidden_p
10613 = parser->local_variables_forbidden_p;
10614 parser->local_variables_forbidden_p = true;
10615 /* Parse the assignment-expression. */
10616 default_argument = cp_parser_assignment_expression (parser);
10617 /* Restore saved state. */
10618 parser->greater_than_is_operator_p
10619 = saved_greater_than_is_operator_p;
10620 parser->local_variables_forbidden_p
10621 = saved_local_variables_forbidden_p;
10623 if (!parser->default_arg_ok_p)
10625 if (!flag_pedantic_errors)
10626 warning ("deprecated use of default argument for parameter of non-function");
10629 error ("default arguments are only permitted for function parameters");
10630 default_argument = NULL_TREE;
10635 default_argument = NULL_TREE;
10637 /* Create the representation of the parameter. */
10639 decl_specifiers = tree_cons (attributes, NULL_TREE, decl_specifiers);
10640 parameter = build_tree_list (default_argument,
10641 build_tree_list (decl_specifiers,
10647 /* Parse a function-definition.
10649 function-definition:
10650 decl-specifier-seq [opt] declarator ctor-initializer [opt]
10652 decl-specifier-seq [opt] declarator function-try-block
10656 function-definition:
10657 __extension__ function-definition
10659 Returns the FUNCTION_DECL for the function. If FRIEND_P is
10660 non-NULL, *FRIEND_P is set to TRUE iff the function was declared to
10664 cp_parser_function_definition (cp_parser* parser, bool* friend_p)
10666 tree decl_specifiers;
10671 int declares_class_or_enum;
10673 /* The saved value of the PEDANTIC flag. */
10674 int saved_pedantic;
10676 /* Any pending qualification must be cleared by our caller. It is
10677 more robust to force the callers to clear PARSER->SCOPE than to
10678 do it here since if the qualification is in effect here, it might
10679 also end up in effect elsewhere that it is not intended. */
10680 my_friendly_assert (!parser->scope, 20010821);
10682 /* Handle `__extension__'. */
10683 if (cp_parser_extension_opt (parser, &saved_pedantic))
10685 /* Parse the function-definition. */
10686 fn = cp_parser_function_definition (parser, friend_p);
10687 /* Restore the PEDANTIC flag. */
10688 pedantic = saved_pedantic;
10693 /* Check to see if this definition appears in a class-specifier. */
10694 member_p = (at_class_scope_p ()
10695 && TYPE_BEING_DEFINED (current_class_type));
10696 /* Defer access checks in the decl-specifier-seq until we know what
10697 function is being defined. There is no need to do this for the
10698 definition of member functions; we cannot be defining a member
10699 from another class. */
10700 push_deferring_access_checks (member_p ? dk_no_check: dk_deferred);
10702 /* Parse the decl-specifier-seq. */
10704 = cp_parser_decl_specifier_seq (parser,
10705 CP_PARSER_FLAGS_OPTIONAL,
10707 &declares_class_or_enum);
10708 /* Figure out whether this declaration is a `friend'. */
10710 *friend_p = cp_parser_friend_p (decl_specifiers);
10712 /* Parse the declarator. */
10713 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
10714 /*ctor_dtor_or_conv_p=*/NULL);
10716 /* Gather up any access checks that occurred. */
10717 stop_deferring_access_checks ();
10719 /* If something has already gone wrong, we may as well stop now. */
10720 if (declarator == error_mark_node)
10722 /* Skip to the end of the function, or if this wasn't anything
10723 like a function-definition, to a `;' in the hopes of finding
10724 a sensible place from which to continue parsing. */
10725 cp_parser_skip_to_end_of_block_or_statement (parser);
10726 pop_deferring_access_checks ();
10727 return error_mark_node;
10730 /* The next character should be a `{' (for a simple function
10731 definition), a `:' (for a ctor-initializer), or `try' (for a
10732 function-try block). */
10733 token = cp_lexer_peek_token (parser->lexer);
10734 if (!cp_parser_token_starts_function_definition_p (token))
10736 /* Issue the error-message. */
10737 cp_parser_error (parser, "expected function-definition");
10738 /* Skip to the next `;'. */
10739 cp_parser_skip_to_end_of_block_or_statement (parser);
10741 pop_deferring_access_checks ();
10742 return error_mark_node;
10745 cp_parser_check_for_definition_in_return_type (declarator,
10746 declares_class_or_enum);
10748 /* If we are in a class scope, then we must handle
10749 function-definitions specially. In particular, we save away the
10750 tokens that make up the function body, and parse them again
10751 later, in order to handle code like:
10754 int f () { return i; }
10758 Here, we cannot parse the body of `f' until after we have seen
10759 the declaration of `i'. */
10762 cp_token_cache *cache;
10764 /* Create the function-declaration. */
10765 fn = start_method (decl_specifiers, declarator, attributes);
10766 /* If something went badly wrong, bail out now. */
10767 if (fn == error_mark_node)
10769 /* If there's a function-body, skip it. */
10770 if (cp_parser_token_starts_function_definition_p
10771 (cp_lexer_peek_token (parser->lexer)))
10772 cp_parser_skip_to_end_of_block_or_statement (parser);
10773 pop_deferring_access_checks ();
10774 return error_mark_node;
10777 /* Remember it, if there default args to post process. */
10778 cp_parser_save_default_args (parser, fn);
10780 /* Create a token cache. */
10781 cache = cp_token_cache_new ();
10782 /* Save away the tokens that make up the body of the
10784 cp_parser_cache_group (parser, cache, CPP_CLOSE_BRACE, /*depth=*/0);
10785 /* Handle function try blocks. */
10786 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
10787 cp_parser_cache_group (parser, cache, CPP_CLOSE_BRACE, /*depth=*/0);
10789 /* Save away the inline definition; we will process it when the
10790 class is complete. */
10791 DECL_PENDING_INLINE_INFO (fn) = cache;
10792 DECL_PENDING_INLINE_P (fn) = 1;
10794 /* We need to know that this was defined in the class, so that
10795 friend templates are handled correctly. */
10796 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
10798 /* We're done with the inline definition. */
10799 finish_method (fn);
10801 /* Add FN to the queue of functions to be parsed later. */
10802 TREE_VALUE (parser->unparsed_functions_queues)
10803 = tree_cons (NULL_TREE, fn,
10804 TREE_VALUE (parser->unparsed_functions_queues));
10806 pop_deferring_access_checks ();
10810 /* Check that the number of template-parameter-lists is OK. */
10811 if (!cp_parser_check_declarator_template_parameters (parser,
10814 cp_parser_skip_to_end_of_block_or_statement (parser);
10815 pop_deferring_access_checks ();
10816 return error_mark_node;
10819 fn = cp_parser_function_definition_from_specifiers_and_declarator
10820 (parser, decl_specifiers, attributes, declarator);
10821 pop_deferring_access_checks ();
10825 /* Parse a function-body.
10828 compound_statement */
10831 cp_parser_function_body (cp_parser *parser)
10833 cp_parser_compound_statement (parser, false);
10836 /* Parse a ctor-initializer-opt followed by a function-body. Return
10837 true if a ctor-initializer was present. */
10840 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
10843 bool ctor_initializer_p;
10845 /* Begin the function body. */
10846 body = begin_function_body ();
10847 /* Parse the optional ctor-initializer. */
10848 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
10849 /* Parse the function-body. */
10850 cp_parser_function_body (parser);
10851 /* Finish the function body. */
10852 finish_function_body (body);
10854 return ctor_initializer_p;
10857 /* Parse an initializer.
10860 = initializer-clause
10861 ( expression-list )
10863 Returns a expression representing the initializer. If no
10864 initializer is present, NULL_TREE is returned.
10866 *IS_PARENTHESIZED_INIT is set to TRUE if the `( expression-list )'
10867 production is used, and zero otherwise. *IS_PARENTHESIZED_INIT is
10868 set to FALSE if there is no initializer present. If there is an
10869 initializer, and it is not a constant-expression, *NON_CONSTANT_P
10870 is set to true; otherwise it is set to false. */
10873 cp_parser_initializer (cp_parser* parser, bool* is_parenthesized_init,
10874 bool* non_constant_p)
10879 /* Peek at the next token. */
10880 token = cp_lexer_peek_token (parser->lexer);
10882 /* Let our caller know whether or not this initializer was
10884 *is_parenthesized_init = (token->type == CPP_OPEN_PAREN);
10885 /* Assume that the initializer is constant. */
10886 *non_constant_p = false;
10888 if (token->type == CPP_EQ)
10890 /* Consume the `='. */
10891 cp_lexer_consume_token (parser->lexer);
10892 /* Parse the initializer-clause. */
10893 init = cp_parser_initializer_clause (parser, non_constant_p);
10895 else if (token->type == CPP_OPEN_PAREN)
10896 init = cp_parser_parenthesized_expression_list (parser, false,
10900 /* Anything else is an error. */
10901 cp_parser_error (parser, "expected initializer");
10902 init = error_mark_node;
10908 /* Parse an initializer-clause.
10910 initializer-clause:
10911 assignment-expression
10912 { initializer-list , [opt] }
10915 Returns an expression representing the initializer.
10917 If the `assignment-expression' production is used the value
10918 returned is simply a representation for the expression.
10920 Otherwise, a CONSTRUCTOR is returned. The CONSTRUCTOR_ELTS will be
10921 the elements of the initializer-list (or NULL_TREE, if the last
10922 production is used). The TREE_TYPE for the CONSTRUCTOR will be
10923 NULL_TREE. There is no way to detect whether or not the optional
10924 trailing `,' was provided. NON_CONSTANT_P is as for
10925 cp_parser_initializer. */
10928 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
10932 /* If it is not a `{', then we are looking at an
10933 assignment-expression. */
10934 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
10936 = cp_parser_constant_expression (parser,
10937 /*allow_non_constant_p=*/true,
10941 /* Consume the `{' token. */
10942 cp_lexer_consume_token (parser->lexer);
10943 /* Create a CONSTRUCTOR to represent the braced-initializer. */
10944 initializer = make_node (CONSTRUCTOR);
10945 /* Mark it with TREE_HAS_CONSTRUCTOR. This should not be
10946 necessary, but check_initializer depends upon it, for
10948 TREE_HAS_CONSTRUCTOR (initializer) = 1;
10949 /* If it's not a `}', then there is a non-trivial initializer. */
10950 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
10952 /* Parse the initializer list. */
10953 CONSTRUCTOR_ELTS (initializer)
10954 = cp_parser_initializer_list (parser, non_constant_p);
10955 /* A trailing `,' token is allowed. */
10956 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
10957 cp_lexer_consume_token (parser->lexer);
10959 /* Now, there should be a trailing `}'. */
10960 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
10963 return initializer;
10966 /* Parse an initializer-list.
10970 initializer-list , initializer-clause
10975 identifier : initializer-clause
10976 initializer-list, identifier : initializer-clause
10978 Returns a TREE_LIST. The TREE_VALUE of each node is an expression
10979 for the initializer. If the TREE_PURPOSE is non-NULL, it is the
10980 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
10981 as for cp_parser_initializer. */
10984 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
10986 tree initializers = NULL_TREE;
10988 /* Assume all of the expressions are constant. */
10989 *non_constant_p = false;
10991 /* Parse the rest of the list. */
10997 bool clause_non_constant_p;
10999 /* If the next token is an identifier and the following one is a
11000 colon, we are looking at the GNU designated-initializer
11002 if (cp_parser_allow_gnu_extensions_p (parser)
11003 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
11004 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
11006 /* Consume the identifier. */
11007 identifier = cp_lexer_consume_token (parser->lexer)->value;
11008 /* Consume the `:'. */
11009 cp_lexer_consume_token (parser->lexer);
11012 identifier = NULL_TREE;
11014 /* Parse the initializer. */
11015 initializer = cp_parser_initializer_clause (parser,
11016 &clause_non_constant_p);
11017 /* If any clause is non-constant, so is the entire initializer. */
11018 if (clause_non_constant_p)
11019 *non_constant_p = true;
11020 /* Add it to the list. */
11021 initializers = tree_cons (identifier, initializer, initializers);
11023 /* If the next token is not a comma, we have reached the end of
11025 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11028 /* Peek at the next token. */
11029 token = cp_lexer_peek_nth_token (parser->lexer, 2);
11030 /* If the next token is a `}', then we're still done. An
11031 initializer-clause can have a trailing `,' after the
11032 initializer-list and before the closing `}'. */
11033 if (token->type == CPP_CLOSE_BRACE)
11036 /* Consume the `,' token. */
11037 cp_lexer_consume_token (parser->lexer);
11040 /* The initializers were built up in reverse order, so we need to
11041 reverse them now. */
11042 return nreverse (initializers);
11045 /* Classes [gram.class] */
11047 /* Parse a class-name.
11053 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
11054 to indicate that names looked up in dependent types should be
11055 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
11056 keyword has been used to indicate that the name that appears next
11057 is a template. TYPE_P is true iff the next name should be treated
11058 as class-name, even if it is declared to be some other kind of name
11059 as well. If CHECK_DEPENDENCY_P is FALSE, names are looked up in
11060 dependent scopes. If CLASS_HEAD_P is TRUE, this class is the class
11061 being defined in a class-head.
11063 Returns the TYPE_DECL representing the class. */
11066 cp_parser_class_name (cp_parser *parser,
11067 bool typename_keyword_p,
11068 bool template_keyword_p,
11070 bool check_dependency_p,
11078 /* All class-names start with an identifier. */
11079 token = cp_lexer_peek_token (parser->lexer);
11080 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
11082 cp_parser_error (parser, "expected class-name");
11083 return error_mark_node;
11086 /* PARSER->SCOPE can be cleared when parsing the template-arguments
11087 to a template-id, so we save it here. */
11088 scope = parser->scope;
11089 if (scope == error_mark_node)
11090 return error_mark_node;
11092 /* Any name names a type if we're following the `typename' keyword
11093 in a qualified name where the enclosing scope is type-dependent. */
11094 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
11095 && dependent_type_p (scope));
11096 /* Handle the common case (an identifier, but not a template-id)
11098 if (token->type == CPP_NAME
11099 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_LESS)
11103 /* Look for the identifier. */
11104 identifier = cp_parser_identifier (parser);
11105 /* If the next token isn't an identifier, we are certainly not
11106 looking at a class-name. */
11107 if (identifier == error_mark_node)
11108 decl = error_mark_node;
11109 /* If we know this is a type-name, there's no need to look it
11111 else if (typename_p)
11115 /* If the next token is a `::', then the name must be a type
11118 [basic.lookup.qual]
11120 During the lookup for a name preceding the :: scope
11121 resolution operator, object, function, and enumerator
11122 names are ignored. */
11123 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
11125 /* Look up the name. */
11126 decl = cp_parser_lookup_name (parser, identifier,
11128 /*is_namespace=*/false,
11129 check_dependency_p);
11134 /* Try a template-id. */
11135 decl = cp_parser_template_id (parser, template_keyword_p,
11136 check_dependency_p);
11137 if (decl == error_mark_node)
11138 return error_mark_node;
11141 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
11143 /* If this is a typename, create a TYPENAME_TYPE. */
11144 if (typename_p && decl != error_mark_node)
11145 decl = TYPE_NAME (make_typename_type (scope, decl,
11148 /* Check to see that it is really the name of a class. */
11149 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
11150 && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
11151 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
11152 /* Situations like this:
11154 template <typename T> struct A {
11155 typename T::template X<int>::I i;
11158 are problematic. Is `T::template X<int>' a class-name? The
11159 standard does not seem to be definitive, but there is no other
11160 valid interpretation of the following `::'. Therefore, those
11161 names are considered class-names. */
11162 decl = TYPE_NAME (make_typename_type (scope, decl, tf_error));
11163 else if (decl == error_mark_node
11164 || TREE_CODE (decl) != TYPE_DECL
11165 || !IS_AGGR_TYPE (TREE_TYPE (decl)))
11167 cp_parser_error (parser, "expected class-name");
11168 return error_mark_node;
11174 /* Parse a class-specifier.
11177 class-head { member-specification [opt] }
11179 Returns the TREE_TYPE representing the class. */
11182 cp_parser_class_specifier (cp_parser* parser)
11186 tree attributes = NULL_TREE;
11187 int has_trailing_semicolon;
11188 bool nested_name_specifier_p;
11189 unsigned saved_num_template_parameter_lists;
11191 push_deferring_access_checks (dk_no_deferred);
11193 /* Parse the class-head. */
11194 type = cp_parser_class_head (parser,
11195 &nested_name_specifier_p);
11196 /* If the class-head was a semantic disaster, skip the entire body
11200 cp_parser_skip_to_end_of_block_or_statement (parser);
11201 pop_deferring_access_checks ();
11202 return error_mark_node;
11205 /* Look for the `{'. */
11206 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
11208 pop_deferring_access_checks ();
11209 return error_mark_node;
11212 /* Issue an error message if type-definitions are forbidden here. */
11213 cp_parser_check_type_definition (parser);
11214 /* Remember that we are defining one more class. */
11215 ++parser->num_classes_being_defined;
11216 /* Inside the class, surrounding template-parameter-lists do not
11218 saved_num_template_parameter_lists
11219 = parser->num_template_parameter_lists;
11220 parser->num_template_parameter_lists = 0;
11222 /* Start the class. */
11223 type = begin_class_definition (type);
11224 if (type == error_mark_node)
11225 /* If the type is erroneous, skip the entire body of the class. */
11226 cp_parser_skip_to_closing_brace (parser);
11228 /* Parse the member-specification. */
11229 cp_parser_member_specification_opt (parser);
11230 /* Look for the trailing `}'. */
11231 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
11232 /* We get better error messages by noticing a common problem: a
11233 missing trailing `;'. */
11234 token = cp_lexer_peek_token (parser->lexer);
11235 has_trailing_semicolon = (token->type == CPP_SEMICOLON);
11236 /* Look for attributes to apply to this class. */
11237 if (cp_parser_allow_gnu_extensions_p (parser))
11238 attributes = cp_parser_attributes_opt (parser);
11239 /* If we got any attributes in class_head, xref_tag will stick them in
11240 TREE_TYPE of the type. Grab them now. */
11241 if (type != error_mark_node)
11243 attributes = chainon (TYPE_ATTRIBUTES (type), attributes);
11244 TYPE_ATTRIBUTES (type) = NULL_TREE;
11245 type = finish_struct (type, attributes);
11247 if (nested_name_specifier_p)
11248 pop_scope (CP_DECL_CONTEXT (TYPE_MAIN_DECL (type)));
11249 /* If this class is not itself within the scope of another class,
11250 then we need to parse the bodies of all of the queued function
11251 definitions. Note that the queued functions defined in a class
11252 are not always processed immediately following the
11253 class-specifier for that class. Consider:
11256 struct B { void f() { sizeof (A); } };
11259 If `f' were processed before the processing of `A' were
11260 completed, there would be no way to compute the size of `A'.
11261 Note that the nesting we are interested in here is lexical --
11262 not the semantic nesting given by TYPE_CONTEXT. In particular,
11265 struct A { struct B; };
11266 struct A::B { void f() { } };
11268 there is no need to delay the parsing of `A::B::f'. */
11269 if (--parser->num_classes_being_defined == 0)
11274 /* In a first pass, parse default arguments to the functions.
11275 Then, in a second pass, parse the bodies of the functions.
11276 This two-phased approach handles cases like:
11284 for (TREE_PURPOSE (parser->unparsed_functions_queues)
11285 = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
11286 (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
11287 TREE_PURPOSE (parser->unparsed_functions_queues)
11288 = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
11290 fn = TREE_VALUE (queue_entry);
11291 /* Make sure that any template parameters are in scope. */
11292 maybe_begin_member_template_processing (fn);
11293 /* If there are default arguments that have not yet been processed,
11294 take care of them now. */
11295 cp_parser_late_parsing_default_args (parser, fn);
11296 /* Remove any template parameters from the symbol table. */
11297 maybe_end_member_template_processing ();
11299 /* Now parse the body of the functions. */
11300 for (TREE_VALUE (parser->unparsed_functions_queues)
11301 = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
11302 (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
11303 TREE_VALUE (parser->unparsed_functions_queues)
11304 = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
11306 /* Figure out which function we need to process. */
11307 fn = TREE_VALUE (queue_entry);
11309 /* Parse the function. */
11310 cp_parser_late_parsing_for_member (parser, fn);
11315 /* Put back any saved access checks. */
11316 pop_deferring_access_checks ();
11318 /* Restore the count of active template-parameter-lists. */
11319 parser->num_template_parameter_lists
11320 = saved_num_template_parameter_lists;
11325 /* Parse a class-head.
11328 class-key identifier [opt] base-clause [opt]
11329 class-key nested-name-specifier identifier base-clause [opt]
11330 class-key nested-name-specifier [opt] template-id
11334 class-key attributes identifier [opt] base-clause [opt]
11335 class-key attributes nested-name-specifier identifier base-clause [opt]
11336 class-key attributes nested-name-specifier [opt] template-id
11339 Returns the TYPE of the indicated class. Sets
11340 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
11341 involving a nested-name-specifier was used, and FALSE otherwise.
11343 Returns NULL_TREE if the class-head is syntactically valid, but
11344 semantically invalid in a way that means we should skip the entire
11345 body of the class. */
11348 cp_parser_class_head (cp_parser* parser,
11349 bool* nested_name_specifier_p)
11352 tree nested_name_specifier;
11353 enum tag_types class_key;
11354 tree id = NULL_TREE;
11355 tree type = NULL_TREE;
11357 bool template_id_p = false;
11358 bool qualified_p = false;
11359 bool invalid_nested_name_p = false;
11360 unsigned num_templates;
11362 /* Assume no nested-name-specifier will be present. */
11363 *nested_name_specifier_p = false;
11364 /* Assume no template parameter lists will be used in defining the
11368 /* Look for the class-key. */
11369 class_key = cp_parser_class_key (parser);
11370 if (class_key == none_type)
11371 return error_mark_node;
11373 /* Parse the attributes. */
11374 attributes = cp_parser_attributes_opt (parser);
11376 /* If the next token is `::', that is invalid -- but sometimes
11377 people do try to write:
11381 Handle this gracefully by accepting the extra qualifier, and then
11382 issuing an error about it later if this really is a
11383 class-head. If it turns out just to be an elaborated type
11384 specifier, remain silent. */
11385 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
11386 qualified_p = true;
11388 push_deferring_access_checks (dk_no_check);
11390 /* Determine the name of the class. Begin by looking for an
11391 optional nested-name-specifier. */
11392 nested_name_specifier
11393 = cp_parser_nested_name_specifier_opt (parser,
11394 /*typename_keyword_p=*/false,
11395 /*check_dependency_p=*/false,
11397 /* If there was a nested-name-specifier, then there *must* be an
11399 if (nested_name_specifier)
11401 /* Although the grammar says `identifier', it really means
11402 `class-name' or `template-name'. You are only allowed to
11403 define a class that has already been declared with this
11406 The proposed resolution for Core Issue 180 says that whever
11407 you see `class T::X' you should treat `X' as a type-name.
11409 It is OK to define an inaccessible class; for example:
11411 class A { class B; };
11414 We do not know if we will see a class-name, or a
11415 template-name. We look for a class-name first, in case the
11416 class-name is a template-id; if we looked for the
11417 template-name first we would stop after the template-name. */
11418 cp_parser_parse_tentatively (parser);
11419 type = cp_parser_class_name (parser,
11420 /*typename_keyword_p=*/false,
11421 /*template_keyword_p=*/false,
11423 /*check_dependency_p=*/false,
11424 /*class_head_p=*/true);
11425 /* If that didn't work, ignore the nested-name-specifier. */
11426 if (!cp_parser_parse_definitely (parser))
11428 invalid_nested_name_p = true;
11429 id = cp_parser_identifier (parser);
11430 if (id == error_mark_node)
11433 /* If we could not find a corresponding TYPE, treat this
11434 declaration like an unqualified declaration. */
11435 if (type == error_mark_node)
11436 nested_name_specifier = NULL_TREE;
11437 /* Otherwise, count the number of templates used in TYPE and its
11438 containing scopes. */
11443 for (scope = TREE_TYPE (type);
11444 scope && TREE_CODE (scope) != NAMESPACE_DECL;
11445 scope = (TYPE_P (scope)
11446 ? TYPE_CONTEXT (scope)
11447 : DECL_CONTEXT (scope)))
11449 && CLASS_TYPE_P (scope)
11450 && CLASSTYPE_TEMPLATE_INFO (scope)
11451 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
11452 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
11456 /* Otherwise, the identifier is optional. */
11459 /* We don't know whether what comes next is a template-id,
11460 an identifier, or nothing at all. */
11461 cp_parser_parse_tentatively (parser);
11462 /* Check for a template-id. */
11463 id = cp_parser_template_id (parser,
11464 /*template_keyword_p=*/false,
11465 /*check_dependency_p=*/true);
11466 /* If that didn't work, it could still be an identifier. */
11467 if (!cp_parser_parse_definitely (parser))
11469 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
11470 id = cp_parser_identifier (parser);
11476 template_id_p = true;
11481 pop_deferring_access_checks ();
11483 /* If it's not a `:' or a `{' then we can't really be looking at a
11484 class-head, since a class-head only appears as part of a
11485 class-specifier. We have to detect this situation before calling
11486 xref_tag, since that has irreversible side-effects. */
11487 if (!cp_parser_next_token_starts_class_definition_p (parser))
11489 cp_parser_error (parser, "expected `{' or `:'");
11490 return error_mark_node;
11493 /* At this point, we're going ahead with the class-specifier, even
11494 if some other problem occurs. */
11495 cp_parser_commit_to_tentative_parse (parser);
11496 /* Issue the error about the overly-qualified name now. */
11498 cp_parser_error (parser,
11499 "global qualification of class name is invalid");
11500 else if (invalid_nested_name_p)
11501 cp_parser_error (parser,
11502 "qualified name does not name a class");
11503 /* Make sure that the right number of template parameters were
11505 if (!cp_parser_check_template_parameters (parser, num_templates))
11506 /* If something went wrong, there is no point in even trying to
11507 process the class-definition. */
11510 /* Look up the type. */
11513 type = TREE_TYPE (id);
11514 maybe_process_partial_specialization (type);
11516 else if (!nested_name_specifier)
11518 /* If the class was unnamed, create a dummy name. */
11520 id = make_anon_name ();
11521 type = xref_tag (class_key, id, attributes, /*globalize=*/false,
11522 parser->num_template_parameter_lists);
11531 template <typename T> struct S { struct T };
11532 template <typename T> struct S<T>::T { };
11534 we will get a TYPENAME_TYPE when processing the definition of
11535 `S::T'. We need to resolve it to the actual type before we
11536 try to define it. */
11537 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
11539 class_type = resolve_typename_type (TREE_TYPE (type),
11540 /*only_current_p=*/false);
11541 if (class_type != error_mark_node)
11542 type = TYPE_NAME (class_type);
11545 cp_parser_error (parser, "could not resolve typename type");
11546 type = error_mark_node;
11550 /* Figure out in what scope the declaration is being placed. */
11551 scope = current_scope ();
11553 scope = current_namespace;
11554 /* If that scope does not contain the scope in which the
11555 class was originally declared, the program is invalid. */
11556 if (scope && !is_ancestor (scope, CP_DECL_CONTEXT (type)))
11558 error ("declaration of `%D' in `%D' which does not "
11559 "enclose `%D'", type, scope, nested_name_specifier);
11564 A declarator-id shall not be qualified exception of the
11565 definition of a ... nested class outside of its class
11566 ... [or] a the definition or explicit instantiation of a
11567 class member of a namespace outside of its namespace. */
11568 if (scope == CP_DECL_CONTEXT (type))
11570 pedwarn ("extra qualification ignored");
11571 nested_name_specifier = NULL_TREE;
11574 maybe_process_partial_specialization (TREE_TYPE (type));
11575 class_type = current_class_type;
11576 /* Enter the scope indicated by the nested-name-specifier. */
11577 if (nested_name_specifier)
11578 push_scope (nested_name_specifier);
11579 /* Get the canonical version of this type. */
11580 type = TYPE_MAIN_DECL (TREE_TYPE (type));
11581 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
11582 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
11583 type = push_template_decl (type);
11584 type = TREE_TYPE (type);
11585 if (nested_name_specifier)
11586 *nested_name_specifier_p = true;
11588 /* Indicate whether this class was declared as a `class' or as a
11590 if (TREE_CODE (type) == RECORD_TYPE)
11591 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
11592 cp_parser_check_class_key (class_key, type);
11594 /* Enter the scope containing the class; the names of base classes
11595 should be looked up in that context. For example, given:
11597 struct A { struct B {}; struct C; };
11598 struct A::C : B {};
11601 if (nested_name_specifier)
11602 push_scope (nested_name_specifier);
11603 /* Now, look for the base-clause. */
11604 token = cp_lexer_peek_token (parser->lexer);
11605 if (token->type == CPP_COLON)
11609 /* Get the list of base-classes. */
11610 bases = cp_parser_base_clause (parser);
11611 /* Process them. */
11612 xref_basetypes (type, bases);
11614 /* Leave the scope given by the nested-name-specifier. We will
11615 enter the class scope itself while processing the members. */
11616 if (nested_name_specifier)
11617 pop_scope (nested_name_specifier);
11622 /* Parse a class-key.
11629 Returns the kind of class-key specified, or none_type to indicate
11632 static enum tag_types
11633 cp_parser_class_key (cp_parser* parser)
11636 enum tag_types tag_type;
11638 /* Look for the class-key. */
11639 token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
11643 /* Check to see if the TOKEN is a class-key. */
11644 tag_type = cp_parser_token_is_class_key (token);
11646 cp_parser_error (parser, "expected class-key");
11650 /* Parse an (optional) member-specification.
11652 member-specification:
11653 member-declaration member-specification [opt]
11654 access-specifier : member-specification [opt] */
11657 cp_parser_member_specification_opt (cp_parser* parser)
11664 /* Peek at the next token. */
11665 token = cp_lexer_peek_token (parser->lexer);
11666 /* If it's a `}', or EOF then we've seen all the members. */
11667 if (token->type == CPP_CLOSE_BRACE || token->type == CPP_EOF)
11670 /* See if this token is a keyword. */
11671 keyword = token->keyword;
11675 case RID_PROTECTED:
11677 /* Consume the access-specifier. */
11678 cp_lexer_consume_token (parser->lexer);
11679 /* Remember which access-specifier is active. */
11680 current_access_specifier = token->value;
11681 /* Look for the `:'. */
11682 cp_parser_require (parser, CPP_COLON, "`:'");
11686 /* Otherwise, the next construction must be a
11687 member-declaration. */
11688 cp_parser_member_declaration (parser);
11693 /* Parse a member-declaration.
11695 member-declaration:
11696 decl-specifier-seq [opt] member-declarator-list [opt] ;
11697 function-definition ; [opt]
11698 :: [opt] nested-name-specifier template [opt] unqualified-id ;
11700 template-declaration
11702 member-declarator-list:
11704 member-declarator-list , member-declarator
11707 declarator pure-specifier [opt]
11708 declarator constant-initializer [opt]
11709 identifier [opt] : constant-expression
11713 member-declaration:
11714 __extension__ member-declaration
11717 declarator attributes [opt] pure-specifier [opt]
11718 declarator attributes [opt] constant-initializer [opt]
11719 identifier [opt] attributes [opt] : constant-expression */
11722 cp_parser_member_declaration (cp_parser* parser)
11724 tree decl_specifiers;
11725 tree prefix_attributes;
11727 int declares_class_or_enum;
11730 int saved_pedantic;
11732 /* Check for the `__extension__' keyword. */
11733 if (cp_parser_extension_opt (parser, &saved_pedantic))
11736 cp_parser_member_declaration (parser);
11737 /* Restore the old value of the PEDANTIC flag. */
11738 pedantic = saved_pedantic;
11743 /* Check for a template-declaration. */
11744 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
11746 /* Parse the template-declaration. */
11747 cp_parser_template_declaration (parser, /*member_p=*/true);
11752 /* Check for a using-declaration. */
11753 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
11755 /* Parse the using-declaration. */
11756 cp_parser_using_declaration (parser);
11761 /* We can't tell whether we're looking at a declaration or a
11762 function-definition. */
11763 cp_parser_parse_tentatively (parser);
11765 /* Parse the decl-specifier-seq. */
11767 = cp_parser_decl_specifier_seq (parser,
11768 CP_PARSER_FLAGS_OPTIONAL,
11769 &prefix_attributes,
11770 &declares_class_or_enum);
11771 /* Check for an invalid type-name. */
11772 if (cp_parser_diagnose_invalid_type_name (parser))
11774 /* If there is no declarator, then the decl-specifier-seq should
11776 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
11778 /* If there was no decl-specifier-seq, and the next token is a
11779 `;', then we have something like:
11785 Each member-declaration shall declare at least one member
11786 name of the class. */
11787 if (!decl_specifiers)
11790 pedwarn ("extra semicolon");
11796 /* See if this declaration is a friend. */
11797 friend_p = cp_parser_friend_p (decl_specifiers);
11798 /* If there were decl-specifiers, check to see if there was
11799 a class-declaration. */
11800 type = check_tag_decl (decl_specifiers);
11801 /* Nested classes have already been added to the class, but
11802 a `friend' needs to be explicitly registered. */
11805 /* If the `friend' keyword was present, the friend must
11806 be introduced with a class-key. */
11807 if (!declares_class_or_enum)
11808 error ("a class-key must be used when declaring a friend");
11811 template <typename T> struct A {
11812 friend struct A<T>::B;
11815 A<T>::B will be represented by a TYPENAME_TYPE, and
11816 therefore not recognized by check_tag_decl. */
11821 for (specifier = decl_specifiers;
11823 specifier = TREE_CHAIN (specifier))
11825 tree s = TREE_VALUE (specifier);
11827 if (TREE_CODE (s) == IDENTIFIER_NODE
11828 && IDENTIFIER_GLOBAL_VALUE (s))
11829 type = IDENTIFIER_GLOBAL_VALUE (s);
11830 if (TREE_CODE (s) == TYPE_DECL)
11840 error ("friend declaration does not name a class or "
11843 make_friend_class (current_class_type, type,
11844 /*complain=*/true);
11846 /* If there is no TYPE, an error message will already have
11850 /* An anonymous aggregate has to be handled specially; such
11851 a declaration really declares a data member (with a
11852 particular type), as opposed to a nested class. */
11853 else if (ANON_AGGR_TYPE_P (type))
11855 /* Remove constructors and such from TYPE, now that we
11856 know it is an anonymous aggregate. */
11857 fixup_anonymous_aggr (type);
11858 /* And make the corresponding data member. */
11859 decl = build_decl (FIELD_DECL, NULL_TREE, type);
11860 /* Add it to the class. */
11861 finish_member_declaration (decl);
11867 /* See if these declarations will be friends. */
11868 friend_p = cp_parser_friend_p (decl_specifiers);
11870 /* Keep going until we hit the `;' at the end of the
11872 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11874 tree attributes = NULL_TREE;
11875 tree first_attribute;
11877 /* Peek at the next token. */
11878 token = cp_lexer_peek_token (parser->lexer);
11880 /* Check for a bitfield declaration. */
11881 if (token->type == CPP_COLON
11882 || (token->type == CPP_NAME
11883 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
11889 /* Get the name of the bitfield. Note that we cannot just
11890 check TOKEN here because it may have been invalidated by
11891 the call to cp_lexer_peek_nth_token above. */
11892 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
11893 identifier = cp_parser_identifier (parser);
11895 identifier = NULL_TREE;
11897 /* Consume the `:' token. */
11898 cp_lexer_consume_token (parser->lexer);
11899 /* Get the width of the bitfield. */
11901 = cp_parser_constant_expression (parser,
11902 /*allow_non_constant=*/false,
11905 /* Look for attributes that apply to the bitfield. */
11906 attributes = cp_parser_attributes_opt (parser);
11907 /* Remember which attributes are prefix attributes and
11909 first_attribute = attributes;
11910 /* Combine the attributes. */
11911 attributes = chainon (prefix_attributes, attributes);
11913 /* Create the bitfield declaration. */
11914 decl = grokbitfield (identifier,
11917 /* Apply the attributes. */
11918 cplus_decl_attributes (&decl, attributes, /*flags=*/0);
11924 tree asm_specification;
11925 int ctor_dtor_or_conv_p;
11927 /* Parse the declarator. */
11929 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
11930 &ctor_dtor_or_conv_p);
11932 /* If something went wrong parsing the declarator, make sure
11933 that we at least consume some tokens. */
11934 if (declarator == error_mark_node)
11936 /* Skip to the end of the statement. */
11937 cp_parser_skip_to_end_of_statement (parser);
11941 cp_parser_check_for_definition_in_return_type
11942 (declarator, declares_class_or_enum);
11944 /* Look for an asm-specification. */
11945 asm_specification = cp_parser_asm_specification_opt (parser);
11946 /* Look for attributes that apply to the declaration. */
11947 attributes = cp_parser_attributes_opt (parser);
11948 /* Remember which attributes are prefix attributes and
11950 first_attribute = attributes;
11951 /* Combine the attributes. */
11952 attributes = chainon (prefix_attributes, attributes);
11954 /* If it's an `=', then we have a constant-initializer or a
11955 pure-specifier. It is not correct to parse the
11956 initializer before registering the member declaration
11957 since the member declaration should be in scope while
11958 its initializer is processed. However, the rest of the
11959 front end does not yet provide an interface that allows
11960 us to handle this correctly. */
11961 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11965 A pure-specifier shall be used only in the declaration of
11966 a virtual function.
11968 A member-declarator can contain a constant-initializer
11969 only if it declares a static member of integral or
11972 Therefore, if the DECLARATOR is for a function, we look
11973 for a pure-specifier; otherwise, we look for a
11974 constant-initializer. When we call `grokfield', it will
11975 perform more stringent semantics checks. */
11976 if (TREE_CODE (declarator) == CALL_EXPR)
11977 initializer = cp_parser_pure_specifier (parser);
11980 /* This declaration cannot be a function
11982 cp_parser_commit_to_tentative_parse (parser);
11983 /* Parse the initializer. */
11984 initializer = cp_parser_constant_initializer (parser);
11987 /* Otherwise, there is no initializer. */
11989 initializer = NULL_TREE;
11991 /* See if we are probably looking at a function
11992 definition. We are certainly not looking at at a
11993 member-declarator. Calling `grokfield' has
11994 side-effects, so we must not do it unless we are sure
11995 that we are looking at a member-declarator. */
11996 if (cp_parser_token_starts_function_definition_p
11997 (cp_lexer_peek_token (parser->lexer)))
11998 decl = error_mark_node;
12001 /* Create the declaration. */
12002 decl = grokfield (declarator, decl_specifiers,
12003 initializer, asm_specification,
12005 /* Any initialization must have been from a
12006 constant-expression. */
12007 if (decl && TREE_CODE (decl) == VAR_DECL && initializer)
12008 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = 1;
12012 /* Reset PREFIX_ATTRIBUTES. */
12013 while (attributes && TREE_CHAIN (attributes) != first_attribute)
12014 attributes = TREE_CHAIN (attributes);
12016 TREE_CHAIN (attributes) = NULL_TREE;
12018 /* If there is any qualification still in effect, clear it
12019 now; we will be starting fresh with the next declarator. */
12020 parser->scope = NULL_TREE;
12021 parser->qualifying_scope = NULL_TREE;
12022 parser->object_scope = NULL_TREE;
12023 /* If it's a `,', then there are more declarators. */
12024 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
12025 cp_lexer_consume_token (parser->lexer);
12026 /* If the next token isn't a `;', then we have a parse error. */
12027 else if (cp_lexer_next_token_is_not (parser->lexer,
12030 cp_parser_error (parser, "expected `;'");
12031 /* Skip tokens until we find a `;' */
12032 cp_parser_skip_to_end_of_statement (parser);
12039 /* Add DECL to the list of members. */
12041 finish_member_declaration (decl);
12043 if (TREE_CODE (decl) == FUNCTION_DECL)
12044 cp_parser_save_default_args (parser, decl);
12049 /* If everything went well, look for the `;'. */
12050 if (cp_parser_parse_definitely (parser))
12052 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
12056 /* Parse the function-definition. */
12057 decl = cp_parser_function_definition (parser, &friend_p);
12058 /* If the member was not a friend, declare it here. */
12060 finish_member_declaration (decl);
12061 /* Peek at the next token. */
12062 token = cp_lexer_peek_token (parser->lexer);
12063 /* If the next token is a semicolon, consume it. */
12064 if (token->type == CPP_SEMICOLON)
12065 cp_lexer_consume_token (parser->lexer);
12068 /* Parse a pure-specifier.
12073 Returns INTEGER_ZERO_NODE if a pure specifier is found.
12074 Otherwiser, ERROR_MARK_NODE is returned. */
12077 cp_parser_pure_specifier (cp_parser* parser)
12081 /* Look for the `=' token. */
12082 if (!cp_parser_require (parser, CPP_EQ, "`='"))
12083 return error_mark_node;
12084 /* Look for the `0' token. */
12085 token = cp_parser_require (parser, CPP_NUMBER, "`0'");
12086 /* Unfortunately, this will accept `0L' and `0x00' as well. We need
12087 to get information from the lexer about how the number was
12088 spelled in order to fix this problem. */
12089 if (!token || !integer_zerop (token->value))
12090 return error_mark_node;
12092 return integer_zero_node;
12095 /* Parse a constant-initializer.
12097 constant-initializer:
12098 = constant-expression
12100 Returns a representation of the constant-expression. */
12103 cp_parser_constant_initializer (cp_parser* parser)
12105 /* Look for the `=' token. */
12106 if (!cp_parser_require (parser, CPP_EQ, "`='"))
12107 return error_mark_node;
12109 /* It is invalid to write:
12111 struct S { static const int i = { 7 }; };
12114 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12116 cp_parser_error (parser,
12117 "a brace-enclosed initializer is not allowed here");
12118 /* Consume the opening brace. */
12119 cp_lexer_consume_token (parser->lexer);
12120 /* Skip the initializer. */
12121 cp_parser_skip_to_closing_brace (parser);
12122 /* Look for the trailing `}'. */
12123 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
12125 return error_mark_node;
12128 return cp_parser_constant_expression (parser,
12129 /*allow_non_constant=*/false,
12133 /* Derived classes [gram.class.derived] */
12135 /* Parse a base-clause.
12138 : base-specifier-list
12140 base-specifier-list:
12142 base-specifier-list , base-specifier
12144 Returns a TREE_LIST representing the base-classes, in the order in
12145 which they were declared. The representation of each node is as
12146 described by cp_parser_base_specifier.
12148 In the case that no bases are specified, this function will return
12149 NULL_TREE, not ERROR_MARK_NODE. */
12152 cp_parser_base_clause (cp_parser* parser)
12154 tree bases = NULL_TREE;
12156 /* Look for the `:' that begins the list. */
12157 cp_parser_require (parser, CPP_COLON, "`:'");
12159 /* Scan the base-specifier-list. */
12165 /* Look for the base-specifier. */
12166 base = cp_parser_base_specifier (parser);
12167 /* Add BASE to the front of the list. */
12168 if (base != error_mark_node)
12170 TREE_CHAIN (base) = bases;
12173 /* Peek at the next token. */
12174 token = cp_lexer_peek_token (parser->lexer);
12175 /* If it's not a comma, then the list is complete. */
12176 if (token->type != CPP_COMMA)
12178 /* Consume the `,'. */
12179 cp_lexer_consume_token (parser->lexer);
12182 /* PARSER->SCOPE may still be non-NULL at this point, if the last
12183 base class had a qualified name. However, the next name that
12184 appears is certainly not qualified. */
12185 parser->scope = NULL_TREE;
12186 parser->qualifying_scope = NULL_TREE;
12187 parser->object_scope = NULL_TREE;
12189 return nreverse (bases);
12192 /* Parse a base-specifier.
12195 :: [opt] nested-name-specifier [opt] class-name
12196 virtual access-specifier [opt] :: [opt] nested-name-specifier
12198 access-specifier virtual [opt] :: [opt] nested-name-specifier
12201 Returns a TREE_LIST. The TREE_PURPOSE will be one of
12202 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
12203 indicate the specifiers provided. The TREE_VALUE will be a TYPE
12204 (or the ERROR_MARK_NODE) indicating the type that was specified. */
12207 cp_parser_base_specifier (cp_parser* parser)
12211 bool virtual_p = false;
12212 bool duplicate_virtual_error_issued_p = false;
12213 bool duplicate_access_error_issued_p = false;
12214 bool class_scope_p, template_p;
12215 tree access = access_default_node;
12218 /* Process the optional `virtual' and `access-specifier'. */
12221 /* Peek at the next token. */
12222 token = cp_lexer_peek_token (parser->lexer);
12223 /* Process `virtual'. */
12224 switch (token->keyword)
12227 /* If `virtual' appears more than once, issue an error. */
12228 if (virtual_p && !duplicate_virtual_error_issued_p)
12230 cp_parser_error (parser,
12231 "`virtual' specified more than once in base-specified");
12232 duplicate_virtual_error_issued_p = true;
12237 /* Consume the `virtual' token. */
12238 cp_lexer_consume_token (parser->lexer);
12243 case RID_PROTECTED:
12245 /* If more than one access specifier appears, issue an
12247 if (access != access_default_node
12248 && !duplicate_access_error_issued_p)
12250 cp_parser_error (parser,
12251 "more than one access specifier in base-specified");
12252 duplicate_access_error_issued_p = true;
12255 access = ridpointers[(int) token->keyword];
12257 /* Consume the access-specifier. */
12258 cp_lexer_consume_token (parser->lexer);
12268 /* Look for the optional `::' operator. */
12269 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
12270 /* Look for the nested-name-specifier. The simplest way to
12275 The keyword `typename' is not permitted in a base-specifier or
12276 mem-initializer; in these contexts a qualified name that
12277 depends on a template-parameter is implicitly assumed to be a
12280 is to pretend that we have seen the `typename' keyword at this
12282 cp_parser_nested_name_specifier_opt (parser,
12283 /*typename_keyword_p=*/true,
12284 /*check_dependency_p=*/true,
12286 /* If the base class is given by a qualified name, assume that names
12287 we see are type names or templates, as appropriate. */
12288 class_scope_p = (parser->scope && TYPE_P (parser->scope));
12289 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
12291 /* Finally, look for the class-name. */
12292 type = cp_parser_class_name (parser,
12296 /*check_dependency_p=*/true,
12297 /*class_head_p=*/false);
12299 if (type == error_mark_node)
12300 return error_mark_node;
12302 return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
12305 /* Exception handling [gram.exception] */
12307 /* Parse an (optional) exception-specification.
12309 exception-specification:
12310 throw ( type-id-list [opt] )
12312 Returns a TREE_LIST representing the exception-specification. The
12313 TREE_VALUE of each node is a type. */
12316 cp_parser_exception_specification_opt (cp_parser* parser)
12321 /* Peek at the next token. */
12322 token = cp_lexer_peek_token (parser->lexer);
12323 /* If it's not `throw', then there's no exception-specification. */
12324 if (!cp_parser_is_keyword (token, RID_THROW))
12327 /* Consume the `throw'. */
12328 cp_lexer_consume_token (parser->lexer);
12330 /* Look for the `('. */
12331 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
12333 /* Peek at the next token. */
12334 token = cp_lexer_peek_token (parser->lexer);
12335 /* If it's not a `)', then there is a type-id-list. */
12336 if (token->type != CPP_CLOSE_PAREN)
12338 const char *saved_message;
12340 /* Types may not be defined in an exception-specification. */
12341 saved_message = parser->type_definition_forbidden_message;
12342 parser->type_definition_forbidden_message
12343 = "types may not be defined in an exception-specification";
12344 /* Parse the type-id-list. */
12345 type_id_list = cp_parser_type_id_list (parser);
12346 /* Restore the saved message. */
12347 parser->type_definition_forbidden_message = saved_message;
12350 type_id_list = empty_except_spec;
12352 /* Look for the `)'. */
12353 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
12355 return type_id_list;
12358 /* Parse an (optional) type-id-list.
12362 type-id-list , type-id
12364 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
12365 in the order that the types were presented. */
12368 cp_parser_type_id_list (cp_parser* parser)
12370 tree types = NULL_TREE;
12377 /* Get the next type-id. */
12378 type = cp_parser_type_id (parser);
12379 /* Add it to the list. */
12380 types = add_exception_specifier (types, type, /*complain=*/1);
12381 /* Peek at the next token. */
12382 token = cp_lexer_peek_token (parser->lexer);
12383 /* If it is not a `,', we are done. */
12384 if (token->type != CPP_COMMA)
12386 /* Consume the `,'. */
12387 cp_lexer_consume_token (parser->lexer);
12390 return nreverse (types);
12393 /* Parse a try-block.
12396 try compound-statement handler-seq */
12399 cp_parser_try_block (cp_parser* parser)
12403 cp_parser_require_keyword (parser, RID_TRY, "`try'");
12404 try_block = begin_try_block ();
12405 cp_parser_compound_statement (parser, false);
12406 finish_try_block (try_block);
12407 cp_parser_handler_seq (parser);
12408 finish_handler_sequence (try_block);
12413 /* Parse a function-try-block.
12415 function-try-block:
12416 try ctor-initializer [opt] function-body handler-seq */
12419 cp_parser_function_try_block (cp_parser* parser)
12422 bool ctor_initializer_p;
12424 /* Look for the `try' keyword. */
12425 if (!cp_parser_require_keyword (parser, RID_TRY, "`try'"))
12427 /* Let the rest of the front-end know where we are. */
12428 try_block = begin_function_try_block ();
12429 /* Parse the function-body. */
12431 = cp_parser_ctor_initializer_opt_and_function_body (parser);
12432 /* We're done with the `try' part. */
12433 finish_function_try_block (try_block);
12434 /* Parse the handlers. */
12435 cp_parser_handler_seq (parser);
12436 /* We're done with the handlers. */
12437 finish_function_handler_sequence (try_block);
12439 return ctor_initializer_p;
12442 /* Parse a handler-seq.
12445 handler handler-seq [opt] */
12448 cp_parser_handler_seq (cp_parser* parser)
12454 /* Parse the handler. */
12455 cp_parser_handler (parser);
12456 /* Peek at the next token. */
12457 token = cp_lexer_peek_token (parser->lexer);
12458 /* If it's not `catch' then there are no more handlers. */
12459 if (!cp_parser_is_keyword (token, RID_CATCH))
12464 /* Parse a handler.
12467 catch ( exception-declaration ) compound-statement */
12470 cp_parser_handler (cp_parser* parser)
12475 cp_parser_require_keyword (parser, RID_CATCH, "`catch'");
12476 handler = begin_handler ();
12477 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
12478 declaration = cp_parser_exception_declaration (parser);
12479 finish_handler_parms (declaration, handler);
12480 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
12481 cp_parser_compound_statement (parser, false);
12482 finish_handler (handler);
12485 /* Parse an exception-declaration.
12487 exception-declaration:
12488 type-specifier-seq declarator
12489 type-specifier-seq abstract-declarator
12493 Returns a VAR_DECL for the declaration, or NULL_TREE if the
12494 ellipsis variant is used. */
12497 cp_parser_exception_declaration (cp_parser* parser)
12499 tree type_specifiers;
12501 const char *saved_message;
12503 /* If it's an ellipsis, it's easy to handle. */
12504 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
12506 /* Consume the `...' token. */
12507 cp_lexer_consume_token (parser->lexer);
12511 /* Types may not be defined in exception-declarations. */
12512 saved_message = parser->type_definition_forbidden_message;
12513 parser->type_definition_forbidden_message
12514 = "types may not be defined in exception-declarations";
12516 /* Parse the type-specifier-seq. */
12517 type_specifiers = cp_parser_type_specifier_seq (parser);
12518 /* If it's a `)', then there is no declarator. */
12519 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
12520 declarator = NULL_TREE;
12522 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
12523 /*ctor_dtor_or_conv_p=*/NULL);
12525 /* Restore the saved message. */
12526 parser->type_definition_forbidden_message = saved_message;
12528 return start_handler_parms (type_specifiers, declarator);
12531 /* Parse a throw-expression.
12534 throw assignment-expression [opt]
12536 Returns a THROW_EXPR representing the throw-expression. */
12539 cp_parser_throw_expression (cp_parser* parser)
12543 cp_parser_require_keyword (parser, RID_THROW, "`throw'");
12544 /* We can't be sure if there is an assignment-expression or not. */
12545 cp_parser_parse_tentatively (parser);
12547 expression = cp_parser_assignment_expression (parser);
12548 /* If it didn't work, this is just a rethrow. */
12549 if (!cp_parser_parse_definitely (parser))
12550 expression = NULL_TREE;
12552 return build_throw (expression);
12555 /* GNU Extensions */
12557 /* Parse an (optional) asm-specification.
12560 asm ( string-literal )
12562 If the asm-specification is present, returns a STRING_CST
12563 corresponding to the string-literal. Otherwise, returns
12567 cp_parser_asm_specification_opt (cp_parser* parser)
12570 tree asm_specification;
12572 /* Peek at the next token. */
12573 token = cp_lexer_peek_token (parser->lexer);
12574 /* If the next token isn't the `asm' keyword, then there's no
12575 asm-specification. */
12576 if (!cp_parser_is_keyword (token, RID_ASM))
12579 /* Consume the `asm' token. */
12580 cp_lexer_consume_token (parser->lexer);
12581 /* Look for the `('. */
12582 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
12584 /* Look for the string-literal. */
12585 token = cp_parser_require (parser, CPP_STRING, "string-literal");
12587 asm_specification = token->value;
12589 asm_specification = NULL_TREE;
12591 /* Look for the `)'. */
12592 cp_parser_require (parser, CPP_CLOSE_PAREN, "`('");
12594 return asm_specification;
12597 /* Parse an asm-operand-list.
12601 asm-operand-list , asm-operand
12604 string-literal ( expression )
12605 [ string-literal ] string-literal ( expression )
12607 Returns a TREE_LIST representing the operands. The TREE_VALUE of
12608 each node is the expression. The TREE_PURPOSE is itself a
12609 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
12610 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
12611 is a STRING_CST for the string literal before the parenthesis. */
12614 cp_parser_asm_operand_list (cp_parser* parser)
12616 tree asm_operands = NULL_TREE;
12620 tree string_literal;
12625 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
12627 /* Consume the `[' token. */
12628 cp_lexer_consume_token (parser->lexer);
12629 /* Read the operand name. */
12630 name = cp_parser_identifier (parser);
12631 if (name != error_mark_node)
12632 name = build_string (IDENTIFIER_LENGTH (name),
12633 IDENTIFIER_POINTER (name));
12634 /* Look for the closing `]'. */
12635 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
12639 /* Look for the string-literal. */
12640 token = cp_parser_require (parser, CPP_STRING, "string-literal");
12641 string_literal = token ? token->value : error_mark_node;
12642 /* Look for the `('. */
12643 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
12644 /* Parse the expression. */
12645 expression = cp_parser_expression (parser);
12646 /* Look for the `)'. */
12647 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
12648 /* Add this operand to the list. */
12649 asm_operands = tree_cons (build_tree_list (name, string_literal),
12652 /* If the next token is not a `,', there are no more
12654 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12656 /* Consume the `,'. */
12657 cp_lexer_consume_token (parser->lexer);
12660 return nreverse (asm_operands);
12663 /* Parse an asm-clobber-list.
12667 asm-clobber-list , string-literal
12669 Returns a TREE_LIST, indicating the clobbers in the order that they
12670 appeared. The TREE_VALUE of each node is a STRING_CST. */
12673 cp_parser_asm_clobber_list (cp_parser* parser)
12675 tree clobbers = NULL_TREE;
12680 tree string_literal;
12682 /* Look for the string literal. */
12683 token = cp_parser_require (parser, CPP_STRING, "string-literal");
12684 string_literal = token ? token->value : error_mark_node;
12685 /* Add it to the list. */
12686 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
12687 /* If the next token is not a `,', then the list is
12689 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12691 /* Consume the `,' token. */
12692 cp_lexer_consume_token (parser->lexer);
12698 /* Parse an (optional) series of attributes.
12701 attributes attribute
12704 __attribute__ (( attribute-list [opt] ))
12706 The return value is as for cp_parser_attribute_list. */
12709 cp_parser_attributes_opt (cp_parser* parser)
12711 tree attributes = NULL_TREE;
12716 tree attribute_list;
12718 /* Peek at the next token. */
12719 token = cp_lexer_peek_token (parser->lexer);
12720 /* If it's not `__attribute__', then we're done. */
12721 if (token->keyword != RID_ATTRIBUTE)
12724 /* Consume the `__attribute__' keyword. */
12725 cp_lexer_consume_token (parser->lexer);
12726 /* Look for the two `(' tokens. */
12727 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
12728 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
12730 /* Peek at the next token. */
12731 token = cp_lexer_peek_token (parser->lexer);
12732 if (token->type != CPP_CLOSE_PAREN)
12733 /* Parse the attribute-list. */
12734 attribute_list = cp_parser_attribute_list (parser);
12736 /* If the next token is a `)', then there is no attribute
12738 attribute_list = NULL;
12740 /* Look for the two `)' tokens. */
12741 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
12742 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
12744 /* Add these new attributes to the list. */
12745 attributes = chainon (attributes, attribute_list);
12751 /* Parse an attribute-list.
12755 attribute-list , attribute
12759 identifier ( identifier )
12760 identifier ( identifier , expression-list )
12761 identifier ( expression-list )
12763 Returns a TREE_LIST. Each node corresponds to an attribute. THe
12764 TREE_PURPOSE of each node is the identifier indicating which
12765 attribute is in use. The TREE_VALUE represents the arguments, if
12769 cp_parser_attribute_list (cp_parser* parser)
12771 tree attribute_list = NULL_TREE;
12779 /* Look for the identifier. We also allow keywords here; for
12780 example `__attribute__ ((const))' is legal. */
12781 token = cp_lexer_peek_token (parser->lexer);
12782 if (token->type != CPP_NAME
12783 && token->type != CPP_KEYWORD)
12784 return error_mark_node;
12785 /* Consume the token. */
12786 token = cp_lexer_consume_token (parser->lexer);
12788 /* Save away the identifier that indicates which attribute this is. */
12789 identifier = token->value;
12790 attribute = build_tree_list (identifier, NULL_TREE);
12792 /* Peek at the next token. */
12793 token = cp_lexer_peek_token (parser->lexer);
12794 /* If it's an `(', then parse the attribute arguments. */
12795 if (token->type == CPP_OPEN_PAREN)
12799 arguments = (cp_parser_parenthesized_expression_list
12800 (parser, true, /*non_constant_p=*/NULL));
12801 /* Save the identifier and arguments away. */
12802 TREE_VALUE (attribute) = arguments;
12805 /* Add this attribute to the list. */
12806 TREE_CHAIN (attribute) = attribute_list;
12807 attribute_list = attribute;
12809 /* Now, look for more attributes. */
12810 token = cp_lexer_peek_token (parser->lexer);
12811 /* If the next token isn't a `,', we're done. */
12812 if (token->type != CPP_COMMA)
12815 /* Consume the commma and keep going. */
12816 cp_lexer_consume_token (parser->lexer);
12819 /* We built up the list in reverse order. */
12820 return nreverse (attribute_list);
12823 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
12824 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
12825 current value of the PEDANTIC flag, regardless of whether or not
12826 the `__extension__' keyword is present. The caller is responsible
12827 for restoring the value of the PEDANTIC flag. */
12830 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
12832 /* Save the old value of the PEDANTIC flag. */
12833 *saved_pedantic = pedantic;
12835 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
12837 /* Consume the `__extension__' token. */
12838 cp_lexer_consume_token (parser->lexer);
12839 /* We're not being pedantic while the `__extension__' keyword is
12849 /* Parse a label declaration.
12852 __label__ label-declarator-seq ;
12854 label-declarator-seq:
12855 identifier , label-declarator-seq
12859 cp_parser_label_declaration (cp_parser* parser)
12861 /* Look for the `__label__' keyword. */
12862 cp_parser_require_keyword (parser, RID_LABEL, "`__label__'");
12868 /* Look for an identifier. */
12869 identifier = cp_parser_identifier (parser);
12870 /* Declare it as a lobel. */
12871 finish_label_decl (identifier);
12872 /* If the next token is a `;', stop. */
12873 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12875 /* Look for the `,' separating the label declarations. */
12876 cp_parser_require (parser, CPP_COMMA, "`,'");
12879 /* Look for the final `;'. */
12880 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
12883 /* Support Functions */
12885 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
12886 NAME should have one of the representations used for an
12887 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
12888 is returned. If PARSER->SCOPE is a dependent type, then a
12889 SCOPE_REF is returned.
12891 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
12892 returned; the name was already resolved when the TEMPLATE_ID_EXPR
12893 was formed. Abstractly, such entities should not be passed to this
12894 function, because they do not need to be looked up, but it is
12895 simpler to check for this special case here, rather than at the
12898 In cases not explicitly covered above, this function returns a
12899 DECL, OVERLOAD, or baselink representing the result of the lookup.
12900 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
12903 If IS_TYPE is TRUE, bindings that do not refer to types are
12906 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
12909 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
12913 cp_parser_lookup_name (cp_parser *parser, tree name,
12914 bool is_type, bool is_namespace, bool check_dependency)
12917 tree object_type = parser->context->object_type;
12919 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
12920 no longer valid. Note that if we are parsing tentatively, and
12921 the parse fails, OBJECT_TYPE will be automatically restored. */
12922 parser->context->object_type = NULL_TREE;
12924 if (name == error_mark_node)
12925 return error_mark_node;
12927 /* A template-id has already been resolved; there is no lookup to
12929 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
12931 if (BASELINK_P (name))
12933 my_friendly_assert ((TREE_CODE (BASELINK_FUNCTIONS (name))
12934 == TEMPLATE_ID_EXPR),
12939 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
12940 it should already have been checked to make sure that the name
12941 used matches the type being destroyed. */
12942 if (TREE_CODE (name) == BIT_NOT_EXPR)
12946 /* Figure out to which type this destructor applies. */
12948 type = parser->scope;
12949 else if (object_type)
12950 type = object_type;
12952 type = current_class_type;
12953 /* If that's not a class type, there is no destructor. */
12954 if (!type || !CLASS_TYPE_P (type))
12955 return error_mark_node;
12956 /* If it was a class type, return the destructor. */
12957 return CLASSTYPE_DESTRUCTORS (type);
12960 /* By this point, the NAME should be an ordinary identifier. If
12961 the id-expression was a qualified name, the qualifying scope is
12962 stored in PARSER->SCOPE at this point. */
12963 my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE,
12966 /* Perform the lookup. */
12971 if (parser->scope == error_mark_node)
12972 return error_mark_node;
12974 /* If the SCOPE is dependent, the lookup must be deferred until
12975 the template is instantiated -- unless we are explicitly
12976 looking up names in uninstantiated templates. Even then, we
12977 cannot look up the name if the scope is not a class type; it
12978 might, for example, be a template type parameter. */
12979 dependent_p = (TYPE_P (parser->scope)
12980 && !(parser->in_declarator_p
12981 && currently_open_class (parser->scope))
12982 && dependent_type_p (parser->scope));
12983 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
12987 decl = build_nt (SCOPE_REF, parser->scope, name);
12989 /* The resolution to Core Issue 180 says that `struct A::B'
12990 should be considered a type-name, even if `A' is
12992 decl = TYPE_NAME (make_typename_type (parser->scope,
12998 /* If PARSER->SCOPE is a dependent type, then it must be a
12999 class type, and we must not be checking dependencies;
13000 otherwise, we would have processed this lookup above. So
13001 that PARSER->SCOPE is not considered a dependent base by
13002 lookup_member, we must enter the scope here. */
13004 push_scope (parser->scope);
13005 /* If the PARSER->SCOPE is a a template specialization, it
13006 may be instantiated during name lookup. In that case,
13007 errors may be issued. Even if we rollback the current
13008 tentative parse, those errors are valid. */
13009 decl = lookup_qualified_name (parser->scope, name, is_type,
13010 /*complain=*/true);
13012 pop_scope (parser->scope);
13014 parser->qualifying_scope = parser->scope;
13015 parser->object_scope = NULL_TREE;
13017 else if (object_type)
13019 tree object_decl = NULL_TREE;
13020 /* Look up the name in the scope of the OBJECT_TYPE, unless the
13021 OBJECT_TYPE is not a class. */
13022 if (CLASS_TYPE_P (object_type))
13023 /* If the OBJECT_TYPE is a template specialization, it may
13024 be instantiated during name lookup. In that case, errors
13025 may be issued. Even if we rollback the current tentative
13026 parse, those errors are valid. */
13027 object_decl = lookup_member (object_type,
13029 /*protect=*/0, is_type);
13030 /* Look it up in the enclosing context, too. */
13031 decl = lookup_name_real (name, is_type, /*nonclass=*/0,
13034 parser->object_scope = object_type;
13035 parser->qualifying_scope = NULL_TREE;
13037 decl = object_decl;
13041 decl = lookup_name_real (name, is_type, /*nonclass=*/0,
13044 parser->qualifying_scope = NULL_TREE;
13045 parser->object_scope = NULL_TREE;
13048 /* If the lookup failed, let our caller know. */
13050 || decl == error_mark_node
13051 || (TREE_CODE (decl) == FUNCTION_DECL
13052 && DECL_ANTICIPATED (decl)))
13053 return error_mark_node;
13055 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
13056 if (TREE_CODE (decl) == TREE_LIST)
13058 /* The error message we have to print is too complicated for
13059 cp_parser_error, so we incorporate its actions directly. */
13060 if (!cp_parser_simulate_error (parser))
13062 error ("reference to `%D' is ambiguous", name);
13063 print_candidates (decl);
13065 return error_mark_node;
13068 my_friendly_assert (DECL_P (decl)
13069 || TREE_CODE (decl) == OVERLOAD
13070 || TREE_CODE (decl) == SCOPE_REF
13071 || BASELINK_P (decl),
13074 /* If we have resolved the name of a member declaration, check to
13075 see if the declaration is accessible. When the name resolves to
13076 set of overloaded functions, accessibility is checked when
13077 overload resolution is done.
13079 During an explicit instantiation, access is not checked at all,
13080 as per [temp.explicit]. */
13082 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
13087 /* Like cp_parser_lookup_name, but for use in the typical case where
13088 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, and CHECK_DEPENDENCY is
13092 cp_parser_lookup_name_simple (cp_parser* parser, tree name)
13094 return cp_parser_lookup_name (parser, name,
13096 /*is_namespace=*/false,
13097 /*check_dependency=*/true);
13100 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
13101 the current context, return the TYPE_DECL. If TAG_NAME_P is
13102 true, the DECL indicates the class being defined in a class-head,
13103 or declared in an elaborated-type-specifier.
13105 Otherwise, return DECL. */
13108 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
13110 /* If the TEMPLATE_DECL is being declared as part of a class-head,
13111 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
13114 template <typename T> struct B;
13117 template <typename T> struct A::B {};
13119 Similarly, in a elaborated-type-specifier:
13121 namespace N { struct X{}; }
13124 template <typename T> friend struct N::X;
13127 However, if the DECL refers to a class type, and we are in
13128 the scope of the class, then the name lookup automatically
13129 finds the TYPE_DECL created by build_self_reference rather
13130 than a TEMPLATE_DECL. For example, in:
13132 template <class T> struct S {
13136 there is no need to handle such case. */
13138 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
13139 return DECL_TEMPLATE_RESULT (decl);
13144 /* If too many, or too few, template-parameter lists apply to the
13145 declarator, issue an error message. Returns TRUE if all went well,
13146 and FALSE otherwise. */
13149 cp_parser_check_declarator_template_parameters (cp_parser* parser,
13152 unsigned num_templates;
13154 /* We haven't seen any classes that involve template parameters yet. */
13157 switch (TREE_CODE (declarator))
13164 tree main_declarator = TREE_OPERAND (declarator, 0);
13166 cp_parser_check_declarator_template_parameters (parser,
13175 scope = TREE_OPERAND (declarator, 0);
13176 member = TREE_OPERAND (declarator, 1);
13178 /* If this is a pointer-to-member, then we are not interested
13179 in the SCOPE, because it does not qualify the thing that is
13181 if (TREE_CODE (member) == INDIRECT_REF)
13182 return (cp_parser_check_declarator_template_parameters
13185 while (scope && CLASS_TYPE_P (scope))
13187 /* You're supposed to have one `template <...>'
13188 for every template class, but you don't need one
13189 for a full specialization. For example:
13191 template <class T> struct S{};
13192 template <> struct S<int> { void f(); };
13193 void S<int>::f () {}
13195 is correct; there shouldn't be a `template <>' for
13196 the definition of `S<int>::f'. */
13197 if (CLASSTYPE_TEMPLATE_INFO (scope)
13198 && (CLASSTYPE_TEMPLATE_INSTANTIATION (scope)
13199 || uses_template_parms (CLASSTYPE_TI_ARGS (scope)))
13200 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
13203 scope = TYPE_CONTEXT (scope);
13207 /* Fall through. */
13210 /* If the DECLARATOR has the form `X<y>' then it uses one
13211 additional level of template parameters. */
13212 if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
13215 return cp_parser_check_template_parameters (parser,
13220 /* NUM_TEMPLATES were used in the current declaration. If that is
13221 invalid, return FALSE and issue an error messages. Otherwise,
13225 cp_parser_check_template_parameters (cp_parser* parser,
13226 unsigned num_templates)
13228 /* If there are more template classes than parameter lists, we have
13231 template <class T> void S<T>::R<T>::f (); */
13232 if (parser->num_template_parameter_lists < num_templates)
13234 error ("too few template-parameter-lists");
13237 /* If there are the same number of template classes and parameter
13238 lists, that's OK. */
13239 if (parser->num_template_parameter_lists == num_templates)
13241 /* If there are more, but only one more, then we are referring to a
13242 member template. That's OK too. */
13243 if (parser->num_template_parameter_lists == num_templates + 1)
13245 /* Otherwise, there are too many template parameter lists. We have
13248 template <class T> template <class U> void S::f(); */
13249 error ("too many template-parameter-lists");
13253 /* Parse a binary-expression of the general form:
13257 binary-expression <token> <expr>
13259 The TOKEN_TREE_MAP maps <token> types to <expr> codes. FN is used
13260 to parser the <expr>s. If the first production is used, then the
13261 value returned by FN is returned directly. Otherwise, a node with
13262 the indicated EXPR_TYPE is returned, with operands corresponding to
13263 the two sub-expressions. */
13266 cp_parser_binary_expression (cp_parser* parser,
13267 const cp_parser_token_tree_map token_tree_map,
13268 cp_parser_expression_fn fn)
13272 /* Parse the first expression. */
13273 lhs = (*fn) (parser);
13274 /* Now, look for more expressions. */
13278 const cp_parser_token_tree_map_node *map_node;
13281 /* Peek at the next token. */
13282 token = cp_lexer_peek_token (parser->lexer);
13283 /* If the token is `>', and that's not an operator at the
13284 moment, then we're done. */
13285 if (token->type == CPP_GREATER
13286 && !parser->greater_than_is_operator_p)
13288 /* If we find one of the tokens we want, build the corresponding
13289 tree representation. */
13290 for (map_node = token_tree_map;
13291 map_node->token_type != CPP_EOF;
13293 if (map_node->token_type == token->type)
13295 /* Consume the operator token. */
13296 cp_lexer_consume_token (parser->lexer);
13297 /* Parse the right-hand side of the expression. */
13298 rhs = (*fn) (parser);
13299 /* Build the binary tree node. */
13300 lhs = build_x_binary_op (map_node->tree_type, lhs, rhs);
13304 /* If the token wasn't one of the ones we want, we're done. */
13305 if (map_node->token_type == CPP_EOF)
13312 /* Parse an optional `::' token indicating that the following name is
13313 from the global namespace. If so, PARSER->SCOPE is set to the
13314 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
13315 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
13316 Returns the new value of PARSER->SCOPE, if the `::' token is
13317 present, and NULL_TREE otherwise. */
13320 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
13324 /* Peek at the next token. */
13325 token = cp_lexer_peek_token (parser->lexer);
13326 /* If we're looking at a `::' token then we're starting from the
13327 global namespace, not our current location. */
13328 if (token->type == CPP_SCOPE)
13330 /* Consume the `::' token. */
13331 cp_lexer_consume_token (parser->lexer);
13332 /* Set the SCOPE so that we know where to start the lookup. */
13333 parser->scope = global_namespace;
13334 parser->qualifying_scope = global_namespace;
13335 parser->object_scope = NULL_TREE;
13337 return parser->scope;
13339 else if (!current_scope_valid_p)
13341 parser->scope = NULL_TREE;
13342 parser->qualifying_scope = NULL_TREE;
13343 parser->object_scope = NULL_TREE;
13349 /* Returns TRUE if the upcoming token sequence is the start of a
13350 constructor declarator. If FRIEND_P is true, the declarator is
13351 preceded by the `friend' specifier. */
13354 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
13356 bool constructor_p;
13357 tree type_decl = NULL_TREE;
13358 bool nested_name_p;
13359 cp_token *next_token;
13361 /* The common case is that this is not a constructor declarator, so
13362 try to avoid doing lots of work if at all possible. It's not
13363 valid declare a constructor at function scope. */
13364 if (at_function_scope_p ())
13366 /* And only certain tokens can begin a constructor declarator. */
13367 next_token = cp_lexer_peek_token (parser->lexer);
13368 if (next_token->type != CPP_NAME
13369 && next_token->type != CPP_SCOPE
13370 && next_token->type != CPP_NESTED_NAME_SPECIFIER
13371 && next_token->type != CPP_TEMPLATE_ID)
13374 /* Parse tentatively; we are going to roll back all of the tokens
13376 cp_parser_parse_tentatively (parser);
13377 /* Assume that we are looking at a constructor declarator. */
13378 constructor_p = true;
13380 /* Look for the optional `::' operator. */
13381 cp_parser_global_scope_opt (parser,
13382 /*current_scope_valid_p=*/false);
13383 /* Look for the nested-name-specifier. */
13385 = (cp_parser_nested_name_specifier_opt (parser,
13386 /*typename_keyword_p=*/false,
13387 /*check_dependency_p=*/false,
13390 /* Outside of a class-specifier, there must be a
13391 nested-name-specifier. */
13392 if (!nested_name_p &&
13393 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
13395 constructor_p = false;
13396 /* If we still think that this might be a constructor-declarator,
13397 look for a class-name. */
13402 template <typename T> struct S { S(); };
13403 template <typename T> S<T>::S ();
13405 we must recognize that the nested `S' names a class.
13408 template <typename T> S<T>::S<T> ();
13410 we must recognize that the nested `S' names a template. */
13411 type_decl = cp_parser_class_name (parser,
13412 /*typename_keyword_p=*/false,
13413 /*template_keyword_p=*/false,
13415 /*check_dependency_p=*/false,
13416 /*class_head_p=*/false);
13417 /* If there was no class-name, then this is not a constructor. */
13418 constructor_p = !cp_parser_error_occurred (parser);
13421 /* If we're still considering a constructor, we have to see a `(',
13422 to begin the parameter-declaration-clause, followed by either a
13423 `)', an `...', or a decl-specifier. We need to check for a
13424 type-specifier to avoid being fooled into thinking that:
13428 is a constructor. (It is actually a function named `f' that
13429 takes one parameter (of type `int') and returns a value of type
13432 && cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
13434 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
13435 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
13436 && !cp_parser_storage_class_specifier_opt (parser))
13440 /* Names appearing in the type-specifier should be looked up
13441 in the scope of the class. */
13442 if (current_class_type)
13446 type = TREE_TYPE (type_decl);
13447 if (TREE_CODE (type) == TYPENAME_TYPE)
13449 type = resolve_typename_type (type,
13450 /*only_current_p=*/false);
13451 if (type == error_mark_node)
13453 cp_parser_abort_tentative_parse (parser);
13459 /* Look for the type-specifier. */
13460 cp_parser_type_specifier (parser,
13461 CP_PARSER_FLAGS_NONE,
13462 /*is_friend=*/false,
13463 /*is_declarator=*/true,
13464 /*declares_class_or_enum=*/NULL,
13465 /*is_cv_qualifier=*/NULL);
13466 /* Leave the scope of the class. */
13470 constructor_p = !cp_parser_error_occurred (parser);
13474 constructor_p = false;
13475 /* We did not really want to consume any tokens. */
13476 cp_parser_abort_tentative_parse (parser);
13478 return constructor_p;
13481 /* Parse the definition of the function given by the DECL_SPECIFIERS,
13482 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
13483 they must be performed once we are in the scope of the function.
13485 Returns the function defined. */
13488 cp_parser_function_definition_from_specifiers_and_declarator
13489 (cp_parser* parser,
13490 tree decl_specifiers,
13497 /* Begin the function-definition. */
13498 success_p = begin_function_definition (decl_specifiers,
13502 /* If there were names looked up in the decl-specifier-seq that we
13503 did not check, check them now. We must wait until we are in the
13504 scope of the function to perform the checks, since the function
13505 might be a friend. */
13506 perform_deferred_access_checks ();
13510 /* If begin_function_definition didn't like the definition, skip
13511 the entire function. */
13512 error ("invalid function declaration");
13513 cp_parser_skip_to_end_of_block_or_statement (parser);
13514 fn = error_mark_node;
13517 fn = cp_parser_function_definition_after_declarator (parser,
13518 /*inline_p=*/false);
13523 /* Parse the part of a function-definition that follows the
13524 declarator. INLINE_P is TRUE iff this function is an inline
13525 function defined with a class-specifier.
13527 Returns the function defined. */
13530 cp_parser_function_definition_after_declarator (cp_parser* parser,
13534 bool ctor_initializer_p = false;
13535 bool saved_in_unbraced_linkage_specification_p;
13536 unsigned saved_num_template_parameter_lists;
13538 /* If the next token is `return', then the code may be trying to
13539 make use of the "named return value" extension that G++ used to
13541 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
13543 /* Consume the `return' keyword. */
13544 cp_lexer_consume_token (parser->lexer);
13545 /* Look for the identifier that indicates what value is to be
13547 cp_parser_identifier (parser);
13548 /* Issue an error message. */
13549 error ("named return values are no longer supported");
13550 /* Skip tokens until we reach the start of the function body. */
13551 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
13552 cp_lexer_consume_token (parser->lexer);
13554 /* The `extern' in `extern "C" void f () { ... }' does not apply to
13555 anything declared inside `f'. */
13556 saved_in_unbraced_linkage_specification_p
13557 = parser->in_unbraced_linkage_specification_p;
13558 parser->in_unbraced_linkage_specification_p = false;
13559 /* Inside the function, surrounding template-parameter-lists do not
13561 saved_num_template_parameter_lists
13562 = parser->num_template_parameter_lists;
13563 parser->num_template_parameter_lists = 0;
13564 /* If the next token is `try', then we are looking at a
13565 function-try-block. */
13566 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
13567 ctor_initializer_p = cp_parser_function_try_block (parser);
13568 /* A function-try-block includes the function-body, so we only do
13569 this next part if we're not processing a function-try-block. */
13572 = cp_parser_ctor_initializer_opt_and_function_body (parser);
13574 /* Finish the function. */
13575 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
13576 (inline_p ? 2 : 0));
13577 /* Generate code for it, if necessary. */
13578 expand_or_defer_fn (fn);
13579 /* Restore the saved values. */
13580 parser->in_unbraced_linkage_specification_p
13581 = saved_in_unbraced_linkage_specification_p;
13582 parser->num_template_parameter_lists
13583 = saved_num_template_parameter_lists;
13588 /* Parse a template-declaration, assuming that the `export' (and
13589 `extern') keywords, if present, has already been scanned. MEMBER_P
13590 is as for cp_parser_template_declaration. */
13593 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
13595 tree decl = NULL_TREE;
13596 tree parameter_list;
13597 bool friend_p = false;
13599 /* Look for the `template' keyword. */
13600 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'"))
13604 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
13607 /* Parse the template parameters. */
13608 begin_template_parm_list ();
13609 /* If the next token is `>', then we have an invalid
13610 specialization. Rather than complain about an invalid template
13611 parameter, issue an error message here. */
13612 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
13614 cp_parser_error (parser, "invalid explicit specialization");
13615 parameter_list = NULL_TREE;
13618 parameter_list = cp_parser_template_parameter_list (parser);
13619 parameter_list = end_template_parm_list (parameter_list);
13620 /* Look for the `>'. */
13621 cp_parser_skip_until_found (parser, CPP_GREATER, "`>'");
13622 /* We just processed one more parameter list. */
13623 ++parser->num_template_parameter_lists;
13624 /* If the next token is `template', there are more template
13626 if (cp_lexer_next_token_is_keyword (parser->lexer,
13628 cp_parser_template_declaration_after_export (parser, member_p);
13631 decl = cp_parser_single_declaration (parser,
13635 /* If this is a member template declaration, let the front
13637 if (member_p && !friend_p && decl)
13638 decl = finish_member_template_decl (decl);
13639 else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
13640 make_friend_class (current_class_type, TREE_TYPE (decl),
13641 /*complain=*/true);
13643 /* We are done with the current parameter list. */
13644 --parser->num_template_parameter_lists;
13647 finish_template_decl (parameter_list);
13649 /* Register member declarations. */
13650 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
13651 finish_member_declaration (decl);
13653 /* If DECL is a function template, we must return to parse it later.
13654 (Even though there is no definition, there might be default
13655 arguments that need handling.) */
13656 if (member_p && decl
13657 && (TREE_CODE (decl) == FUNCTION_DECL
13658 || DECL_FUNCTION_TEMPLATE_P (decl)))
13659 TREE_VALUE (parser->unparsed_functions_queues)
13660 = tree_cons (NULL_TREE, decl,
13661 TREE_VALUE (parser->unparsed_functions_queues));
13664 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
13665 `function-definition' sequence. MEMBER_P is true, this declaration
13666 appears in a class scope.
13668 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
13669 *FRIEND_P is set to TRUE iff the declaration is a friend. */
13672 cp_parser_single_declaration (cp_parser* parser,
13676 int declares_class_or_enum;
13677 tree decl = NULL_TREE;
13678 tree decl_specifiers;
13681 /* Parse the dependent declaration. We don't know yet
13682 whether it will be a function-definition. */
13683 cp_parser_parse_tentatively (parser);
13684 /* Defer access checks until we know what is being declared. */
13685 push_deferring_access_checks (dk_deferred);
13687 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
13690 = cp_parser_decl_specifier_seq (parser,
13691 CP_PARSER_FLAGS_OPTIONAL,
13693 &declares_class_or_enum);
13694 /* Gather up the access checks that occurred the
13695 decl-specifier-seq. */
13696 stop_deferring_access_checks ();
13698 /* Check for the declaration of a template class. */
13699 if (declares_class_or_enum)
13701 if (cp_parser_declares_only_class_p (parser))
13703 decl = shadow_tag (decl_specifiers);
13705 decl = TYPE_NAME (decl);
13707 decl = error_mark_node;
13712 /* If it's not a template class, try for a template function. If
13713 the next token is a `;', then this declaration does not declare
13714 anything. But, if there were errors in the decl-specifiers, then
13715 the error might well have come from an attempted class-specifier.
13716 In that case, there's no need to warn about a missing declarator. */
13718 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
13719 || !value_member (error_mark_node, decl_specifiers)))
13720 decl = cp_parser_init_declarator (parser,
13723 /*function_definition_allowed_p=*/false,
13725 declares_class_or_enum,
13726 /*function_definition_p=*/NULL);
13728 pop_deferring_access_checks ();
13730 /* Clear any current qualification; whatever comes next is the start
13731 of something new. */
13732 parser->scope = NULL_TREE;
13733 parser->qualifying_scope = NULL_TREE;
13734 parser->object_scope = NULL_TREE;
13735 /* Look for a trailing `;' after the declaration. */
13736 if (!cp_parser_require (parser, CPP_SEMICOLON, "`;'")
13737 && cp_parser_committed_to_tentative_parse (parser))
13738 cp_parser_skip_to_end_of_block_or_statement (parser);
13739 /* If it worked, set *FRIEND_P based on the DECL_SPECIFIERS. */
13740 if (cp_parser_parse_definitely (parser))
13743 *friend_p = cp_parser_friend_p (decl_specifiers);
13745 /* Otherwise, try a function-definition. */
13747 decl = cp_parser_function_definition (parser, friend_p);
13752 /* Parse a cast-expression that is not the operand of a unary "&". */
13755 cp_parser_simple_cast_expression (cp_parser *parser)
13757 return cp_parser_cast_expression (parser, /*address_p=*/false);
13760 /* Parse a functional cast to TYPE. Returns an expression
13761 representing the cast. */
13764 cp_parser_functional_cast (cp_parser* parser, tree type)
13766 tree expression_list;
13769 = cp_parser_parenthesized_expression_list (parser, false,
13770 /*non_constant_p=*/NULL);
13772 return build_functional_cast (type, expression_list);
13775 /* MEMBER_FUNCTION is a member function, or a friend. If default
13776 arguments, or the body of the function have not yet been parsed,
13780 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
13782 cp_lexer *saved_lexer;
13784 /* If this member is a template, get the underlying
13786 if (DECL_FUNCTION_TEMPLATE_P (member_function))
13787 member_function = DECL_TEMPLATE_RESULT (member_function);
13789 /* There should not be any class definitions in progress at this
13790 point; the bodies of members are only parsed outside of all class
13792 my_friendly_assert (parser->num_classes_being_defined == 0, 20010816);
13793 /* While we're parsing the member functions we might encounter more
13794 classes. We want to handle them right away, but we don't want
13795 them getting mixed up with functions that are currently in the
13797 parser->unparsed_functions_queues
13798 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
13800 /* Make sure that any template parameters are in scope. */
13801 maybe_begin_member_template_processing (member_function);
13803 /* If the body of the function has not yet been parsed, parse it
13805 if (DECL_PENDING_INLINE_P (member_function))
13807 tree function_scope;
13808 cp_token_cache *tokens;
13810 /* The function is no longer pending; we are processing it. */
13811 tokens = DECL_PENDING_INLINE_INFO (member_function);
13812 DECL_PENDING_INLINE_INFO (member_function) = NULL;
13813 DECL_PENDING_INLINE_P (member_function) = 0;
13814 /* If this was an inline function in a local class, enter the scope
13815 of the containing function. */
13816 function_scope = decl_function_context (member_function);
13817 if (function_scope)
13818 push_function_context_to (function_scope);
13820 /* Save away the current lexer. */
13821 saved_lexer = parser->lexer;
13822 /* Make a new lexer to feed us the tokens saved for this function. */
13823 parser->lexer = cp_lexer_new_from_tokens (tokens);
13824 parser->lexer->next = saved_lexer;
13826 /* Set the current source position to be the location of the first
13827 token in the saved inline body. */
13828 cp_lexer_peek_token (parser->lexer);
13830 /* Let the front end know that we going to be defining this
13832 start_function (NULL_TREE, member_function, NULL_TREE,
13833 SF_PRE_PARSED | SF_INCLASS_INLINE);
13835 /* Now, parse the body of the function. */
13836 cp_parser_function_definition_after_declarator (parser,
13837 /*inline_p=*/true);
13839 /* Leave the scope of the containing function. */
13840 if (function_scope)
13841 pop_function_context_from (function_scope);
13842 /* Restore the lexer. */
13843 parser->lexer = saved_lexer;
13846 /* Remove any template parameters from the symbol table. */
13847 maybe_end_member_template_processing ();
13849 /* Restore the queue. */
13850 parser->unparsed_functions_queues
13851 = TREE_CHAIN (parser->unparsed_functions_queues);
13854 /* If DECL contains any default args, remeber it on the unparsed
13855 functions queue. */
13858 cp_parser_save_default_args (cp_parser* parser, tree decl)
13862 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
13864 probe = TREE_CHAIN (probe))
13865 if (TREE_PURPOSE (probe))
13867 TREE_PURPOSE (parser->unparsed_functions_queues)
13868 = tree_cons (NULL_TREE, decl,
13869 TREE_PURPOSE (parser->unparsed_functions_queues));
13875 /* FN is a FUNCTION_DECL which may contains a parameter with an
13876 unparsed DEFAULT_ARG. Parse the default args now. */
13879 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
13881 cp_lexer *saved_lexer;
13882 cp_token_cache *tokens;
13883 bool saved_local_variables_forbidden_p;
13886 /* While we're parsing the default args, we might (due to the
13887 statement expression extension) encounter more classes. We want
13888 to handle them right away, but we don't want them getting mixed
13889 up with default args that are currently in the queue. */
13890 parser->unparsed_functions_queues
13891 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
13893 for (parameters = TYPE_ARG_TYPES (TREE_TYPE (fn));
13895 parameters = TREE_CHAIN (parameters))
13897 if (!TREE_PURPOSE (parameters)
13898 || TREE_CODE (TREE_PURPOSE (parameters)) != DEFAULT_ARG)
13901 /* Save away the current lexer. */
13902 saved_lexer = parser->lexer;
13903 /* Create a new one, using the tokens we have saved. */
13904 tokens = DEFARG_TOKENS (TREE_PURPOSE (parameters));
13905 parser->lexer = cp_lexer_new_from_tokens (tokens);
13907 /* Set the current source position to be the location of the
13908 first token in the default argument. */
13909 cp_lexer_peek_token (parser->lexer);
13911 /* Local variable names (and the `this' keyword) may not appear
13912 in a default argument. */
13913 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
13914 parser->local_variables_forbidden_p = true;
13915 /* Parse the assignment-expression. */
13916 if (DECL_CONTEXT (fn))
13917 push_nested_class (DECL_CONTEXT (fn));
13918 TREE_PURPOSE (parameters) = cp_parser_assignment_expression (parser);
13919 if (DECL_CONTEXT (fn))
13920 pop_nested_class ();
13922 /* Restore saved state. */
13923 parser->lexer = saved_lexer;
13924 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
13927 /* Restore the queue. */
13928 parser->unparsed_functions_queues
13929 = TREE_CHAIN (parser->unparsed_functions_queues);
13932 /* Parse the operand of `sizeof' (or a similar operator). Returns
13933 either a TYPE or an expression, depending on the form of the
13934 input. The KEYWORD indicates which kind of expression we have
13938 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
13940 static const char *format;
13941 tree expr = NULL_TREE;
13942 const char *saved_message;
13943 bool saved_constant_expression_p;
13945 /* Initialize FORMAT the first time we get here. */
13947 format = "types may not be defined in `%s' expressions";
13949 /* Types cannot be defined in a `sizeof' expression. Save away the
13951 saved_message = parser->type_definition_forbidden_message;
13952 /* And create the new one. */
13953 parser->type_definition_forbidden_message
13954 = xmalloc (strlen (format)
13955 + strlen (IDENTIFIER_POINTER (ridpointers[keyword]))
13957 sprintf ((char *) parser->type_definition_forbidden_message,
13958 format, IDENTIFIER_POINTER (ridpointers[keyword]));
13960 /* The restrictions on constant-expressions do not apply inside
13961 sizeof expressions. */
13962 saved_constant_expression_p = parser->constant_expression_p;
13963 parser->constant_expression_p = false;
13965 /* Do not actually evaluate the expression. */
13967 /* If it's a `(', then we might be looking at the type-id
13969 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
13973 /* We can't be sure yet whether we're looking at a type-id or an
13975 cp_parser_parse_tentatively (parser);
13976 /* Consume the `('. */
13977 cp_lexer_consume_token (parser->lexer);
13978 /* Parse the type-id. */
13979 type = cp_parser_type_id (parser);
13980 /* Now, look for the trailing `)'. */
13981 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13982 /* If all went well, then we're done. */
13983 if (cp_parser_parse_definitely (parser))
13985 /* Build a list of decl-specifiers; right now, we have only
13986 a single type-specifier. */
13987 type = build_tree_list (NULL_TREE,
13990 /* Call grokdeclarator to figure out what type this is. */
13991 expr = grokdeclarator (NULL_TREE,
13995 /*attrlist=*/NULL);
13999 /* If the type-id production did not work out, then we must be
14000 looking at the unary-expression production. */
14002 expr = cp_parser_unary_expression (parser, /*address_p=*/false);
14003 /* Go back to evaluating expressions. */
14006 /* Free the message we created. */
14007 free ((char *) parser->type_definition_forbidden_message);
14008 /* And restore the old one. */
14009 parser->type_definition_forbidden_message = saved_message;
14010 parser->constant_expression_p = saved_constant_expression_p;
14015 /* If the current declaration has no declarator, return true. */
14018 cp_parser_declares_only_class_p (cp_parser *parser)
14020 /* If the next token is a `;' or a `,' then there is no
14022 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
14023 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
14026 /* Simplify EXPR if it is a non-dependent expression. Returns the
14027 (possibly simplified) expression. */
14030 cp_parser_fold_non_dependent_expr (tree expr)
14032 /* If we're in a template, but EXPR isn't value dependent, simplify
14033 it. We're supposed to treat:
14035 template <typename T> void f(T[1 + 1]);
14036 template <typename T> void f(T[2]);
14038 as two declarations of the same function, for example. */
14039 if (processing_template_decl
14040 && !type_dependent_expression_p (expr)
14041 && !value_dependent_expression_p (expr))
14043 HOST_WIDE_INT saved_processing_template_decl;
14045 saved_processing_template_decl = processing_template_decl;
14046 processing_template_decl = 0;
14047 expr = tsubst_copy_and_build (expr,
14048 /*args=*/NULL_TREE,
14050 /*in_decl=*/NULL_TREE,
14051 /*function_p=*/false);
14052 processing_template_decl = saved_processing_template_decl;
14057 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
14058 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
14061 cp_parser_friend_p (tree decl_specifiers)
14063 while (decl_specifiers)
14065 /* See if this decl-specifier is `friend'. */
14066 if (TREE_CODE (TREE_VALUE (decl_specifiers)) == IDENTIFIER_NODE
14067 && C_RID_CODE (TREE_VALUE (decl_specifiers)) == RID_FRIEND)
14070 /* Go on to the next decl-specifier. */
14071 decl_specifiers = TREE_CHAIN (decl_specifiers);
14077 /* If the next token is of the indicated TYPE, consume it. Otherwise,
14078 issue an error message indicating that TOKEN_DESC was expected.
14080 Returns the token consumed, if the token had the appropriate type.
14081 Otherwise, returns NULL. */
14084 cp_parser_require (cp_parser* parser,
14085 enum cpp_ttype type,
14086 const char* token_desc)
14088 if (cp_lexer_next_token_is (parser->lexer, type))
14089 return cp_lexer_consume_token (parser->lexer);
14092 /* Output the MESSAGE -- unless we're parsing tentatively. */
14093 if (!cp_parser_simulate_error (parser))
14094 error ("expected %s", token_desc);
14099 /* Like cp_parser_require, except that tokens will be skipped until
14100 the desired token is found. An error message is still produced if
14101 the next token is not as expected. */
14104 cp_parser_skip_until_found (cp_parser* parser,
14105 enum cpp_ttype type,
14106 const char* token_desc)
14109 unsigned nesting_depth = 0;
14111 if (cp_parser_require (parser, type, token_desc))
14114 /* Skip tokens until the desired token is found. */
14117 /* Peek at the next token. */
14118 token = cp_lexer_peek_token (parser->lexer);
14119 /* If we've reached the token we want, consume it and
14121 if (token->type == type && !nesting_depth)
14123 cp_lexer_consume_token (parser->lexer);
14126 /* If we've run out of tokens, stop. */
14127 if (token->type == CPP_EOF)
14129 if (token->type == CPP_OPEN_BRACE
14130 || token->type == CPP_OPEN_PAREN
14131 || token->type == CPP_OPEN_SQUARE)
14133 else if (token->type == CPP_CLOSE_BRACE
14134 || token->type == CPP_CLOSE_PAREN
14135 || token->type == CPP_CLOSE_SQUARE)
14137 if (nesting_depth-- == 0)
14140 /* Consume this token. */
14141 cp_lexer_consume_token (parser->lexer);
14145 /* If the next token is the indicated keyword, consume it. Otherwise,
14146 issue an error message indicating that TOKEN_DESC was expected.
14148 Returns the token consumed, if the token had the appropriate type.
14149 Otherwise, returns NULL. */
14152 cp_parser_require_keyword (cp_parser* parser,
14154 const char* token_desc)
14156 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
14158 if (token && token->keyword != keyword)
14160 dyn_string_t error_msg;
14162 /* Format the error message. */
14163 error_msg = dyn_string_new (0);
14164 dyn_string_append_cstr (error_msg, "expected ");
14165 dyn_string_append_cstr (error_msg, token_desc);
14166 cp_parser_error (parser, error_msg->s);
14167 dyn_string_delete (error_msg);
14174 /* Returns TRUE iff TOKEN is a token that can begin the body of a
14175 function-definition. */
14178 cp_parser_token_starts_function_definition_p (cp_token* token)
14180 return (/* An ordinary function-body begins with an `{'. */
14181 token->type == CPP_OPEN_BRACE
14182 /* A ctor-initializer begins with a `:'. */
14183 || token->type == CPP_COLON
14184 /* A function-try-block begins with `try'. */
14185 || token->keyword == RID_TRY
14186 /* The named return value extension begins with `return'. */
14187 || token->keyword == RID_RETURN);
14190 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
14194 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
14198 token = cp_lexer_peek_token (parser->lexer);
14199 return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
14202 /* Returns TRUE iff the next token is the "," or ">" ending a
14203 template-argument. */
14206 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
14210 token = cp_lexer_peek_token (parser->lexer);
14211 return (token->type == CPP_COMMA || token->type == CPP_GREATER);
14214 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
14215 or none_type otherwise. */
14217 static enum tag_types
14218 cp_parser_token_is_class_key (cp_token* token)
14220 switch (token->keyword)
14225 return record_type;
14234 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
14237 cp_parser_check_class_key (enum tag_types class_key, tree type)
14239 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
14240 pedwarn ("`%s' tag used in naming `%#T'",
14241 class_key == union_type ? "union"
14242 : class_key == record_type ? "struct" : "class",
14246 /* Look for the `template' keyword, as a syntactic disambiguator.
14247 Return TRUE iff it is present, in which case it will be
14251 cp_parser_optional_template_keyword (cp_parser *parser)
14253 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
14255 /* The `template' keyword can only be used within templates;
14256 outside templates the parser can always figure out what is a
14257 template and what is not. */
14258 if (!processing_template_decl)
14260 error ("`template' (as a disambiguator) is only allowed "
14261 "within templates");
14262 /* If this part of the token stream is rescanned, the same
14263 error message would be generated. So, we purge the token
14264 from the stream. */
14265 cp_lexer_purge_token (parser->lexer);
14270 /* Consume the `template' keyword. */
14271 cp_lexer_consume_token (parser->lexer);
14279 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
14280 set PARSER->SCOPE, and perform other related actions. */
14283 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
14288 /* Get the stored value. */
14289 value = cp_lexer_consume_token (parser->lexer)->value;
14290 /* Perform any access checks that were deferred. */
14291 for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
14292 perform_or_defer_access_check (TREE_PURPOSE (check), TREE_VALUE (check));
14293 /* Set the scope from the stored value. */
14294 parser->scope = TREE_VALUE (value);
14295 parser->qualifying_scope = TREE_TYPE (value);
14296 parser->object_scope = NULL_TREE;
14299 /* Add tokens to CACHE until an non-nested END token appears. */
14302 cp_parser_cache_group (cp_parser *parser,
14303 cp_token_cache *cache,
14304 enum cpp_ttype end,
14311 /* Abort a parenthesized expression if we encounter a brace. */
14312 if ((end == CPP_CLOSE_PAREN || depth == 0)
14313 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
14315 /* Consume the next token. */
14316 token = cp_lexer_consume_token (parser->lexer);
14317 /* If we've reached the end of the file, stop. */
14318 if (token->type == CPP_EOF)
14320 /* Add this token to the tokens we are saving. */
14321 cp_token_cache_push_token (cache, token);
14322 /* See if it starts a new group. */
14323 if (token->type == CPP_OPEN_BRACE)
14325 cp_parser_cache_group (parser, cache, CPP_CLOSE_BRACE, depth + 1);
14329 else if (token->type == CPP_OPEN_PAREN)
14330 cp_parser_cache_group (parser, cache, CPP_CLOSE_PAREN, depth + 1);
14331 else if (token->type == end)
14336 /* Begin parsing tentatively. We always save tokens while parsing
14337 tentatively so that if the tentative parsing fails we can restore the
14341 cp_parser_parse_tentatively (cp_parser* parser)
14343 /* Enter a new parsing context. */
14344 parser->context = cp_parser_context_new (parser->context);
14345 /* Begin saving tokens. */
14346 cp_lexer_save_tokens (parser->lexer);
14347 /* In order to avoid repetitive access control error messages,
14348 access checks are queued up until we are no longer parsing
14350 push_deferring_access_checks (dk_deferred);
14353 /* Commit to the currently active tentative parse. */
14356 cp_parser_commit_to_tentative_parse (cp_parser* parser)
14358 cp_parser_context *context;
14361 /* Mark all of the levels as committed. */
14362 lexer = parser->lexer;
14363 for (context = parser->context; context->next; context = context->next)
14365 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
14367 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
14368 while (!cp_lexer_saving_tokens (lexer))
14369 lexer = lexer->next;
14370 cp_lexer_commit_tokens (lexer);
14374 /* Abort the currently active tentative parse. All consumed tokens
14375 will be rolled back, and no diagnostics will be issued. */
14378 cp_parser_abort_tentative_parse (cp_parser* parser)
14380 cp_parser_simulate_error (parser);
14381 /* Now, pretend that we want to see if the construct was
14382 successfully parsed. */
14383 cp_parser_parse_definitely (parser);
14386 /* Stop parsing tentatively. If a parse error has occurred, restore the
14387 token stream. Otherwise, commit to the tokens we have consumed.
14388 Returns true if no error occurred; false otherwise. */
14391 cp_parser_parse_definitely (cp_parser* parser)
14393 bool error_occurred;
14394 cp_parser_context *context;
14396 /* Remember whether or not an error occurred, since we are about to
14397 destroy that information. */
14398 error_occurred = cp_parser_error_occurred (parser);
14399 /* Remove the topmost context from the stack. */
14400 context = parser->context;
14401 parser->context = context->next;
14402 /* If no parse errors occurred, commit to the tentative parse. */
14403 if (!error_occurred)
14405 /* Commit to the tokens read tentatively, unless that was
14407 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
14408 cp_lexer_commit_tokens (parser->lexer);
14410 pop_to_parent_deferring_access_checks ();
14412 /* Otherwise, if errors occurred, roll back our state so that things
14413 are just as they were before we began the tentative parse. */
14416 cp_lexer_rollback_tokens (parser->lexer);
14417 pop_deferring_access_checks ();
14419 /* Add the context to the front of the free list. */
14420 context->next = cp_parser_context_free_list;
14421 cp_parser_context_free_list = context;
14423 return !error_occurred;
14426 /* Returns true if we are parsing tentatively -- but have decided that
14427 we will stick with this tentative parse, even if errors occur. */
14430 cp_parser_committed_to_tentative_parse (cp_parser* parser)
14432 return (cp_parser_parsing_tentatively (parser)
14433 && parser->context->status == CP_PARSER_STATUS_KIND_COMMITTED);
14436 /* Returns nonzero iff an error has occurred during the most recent
14437 tentative parse. */
14440 cp_parser_error_occurred (cp_parser* parser)
14442 return (cp_parser_parsing_tentatively (parser)
14443 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
14446 /* Returns nonzero if GNU extensions are allowed. */
14449 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
14451 return parser->allow_gnu_extensions_p;
14458 static GTY (()) cp_parser *the_parser;
14460 /* External interface. */
14462 /* Parse one entire translation unit. */
14465 c_parse_file (void)
14467 bool error_occurred;
14469 the_parser = cp_parser_new ();
14470 push_deferring_access_checks (flag_access_control
14471 ? dk_no_deferred : dk_no_check);
14472 error_occurred = cp_parser_translation_unit (the_parser);
14476 /* Clean up after parsing the entire translation unit. */
14479 free_parser_stacks (void)
14481 /* Nothing to do. */
14484 /* This variable must be provided by every front end. */
14488 #include "gt-cp-parser.h"