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. */
72 /* The value associated with this token, if any. */
74 /* If this token is a keyword, this value indicates which keyword.
75 Otherwise, this value is RID_MAX. */
77 /* The location at which this token was found. */
81 /* The number of tokens in a single token block. */
83 #define CP_TOKEN_BLOCK_NUM_TOKENS 32
85 /* A group of tokens. These groups are chained together to store
86 large numbers of tokens. (For example, a token block is created
87 when the body of an inline member function is first encountered;
88 the tokens are processed later after the class definition is
91 This somewhat ungainly data structure (as opposed to, say, a
92 variable-length array), is used due to constraints imposed by the
93 current garbage-collection methodology. If it is made more
94 flexible, we could perhaps simplify the data structures involved. */
96 typedef struct cp_token_block GTY (())
99 cp_token tokens[CP_TOKEN_BLOCK_NUM_TOKENS];
100 /* The number of tokens in this block. */
102 /* The next token block in the chain. */
103 struct cp_token_block *next;
104 /* The previous block in the chain. */
105 struct cp_token_block *prev;
108 typedef struct cp_token_cache GTY (())
110 /* The first block in the cache. NULL if there are no tokens in the
112 cp_token_block *first;
113 /* The last block in the cache. NULL If there are no tokens in the
115 cp_token_block *last;
120 static cp_token_cache *cp_token_cache_new
122 static void cp_token_cache_push_token
123 (cp_token_cache *, cp_token *);
125 /* Create a new cp_token_cache. */
127 static cp_token_cache *
128 cp_token_cache_new ()
130 return ggc_alloc_cleared (sizeof (cp_token_cache));
133 /* Add *TOKEN to *CACHE. */
136 cp_token_cache_push_token (cp_token_cache *cache,
139 cp_token_block *b = cache->last;
141 /* See if we need to allocate a new token block. */
142 if (!b || b->num_tokens == CP_TOKEN_BLOCK_NUM_TOKENS)
144 b = ggc_alloc_cleared (sizeof (cp_token_block));
145 b->prev = cache->last;
148 cache->last->next = b;
152 cache->first = cache->last = b;
154 /* Add this token to the current token block. */
155 b->tokens[b->num_tokens++] = *token;
158 /* The cp_lexer structure represents the C++ lexer. It is responsible
159 for managing the token stream from the preprocessor and supplying
162 typedef struct cp_lexer GTY (())
164 /* The memory allocated for the buffer. Never NULL. */
165 cp_token * GTY ((length ("(%h.buffer_end - %h.buffer)"))) buffer;
166 /* A pointer just past the end of the memory allocated for the buffer. */
167 cp_token * GTY ((skip (""))) buffer_end;
168 /* The first valid token in the buffer, or NULL if none. */
169 cp_token * GTY ((skip (""))) first_token;
170 /* The next available token. If NEXT_TOKEN is NULL, then there are
171 no more available tokens. */
172 cp_token * GTY ((skip (""))) next_token;
173 /* A pointer just past the last available token. If FIRST_TOKEN is
174 NULL, however, there are no available tokens, and then this
175 location is simply the place in which the next token read will be
176 placed. If LAST_TOKEN == FIRST_TOKEN, then the buffer is full.
177 When the LAST_TOKEN == BUFFER, then the last token is at the
178 highest memory address in the BUFFER. */
179 cp_token * GTY ((skip (""))) last_token;
181 /* A stack indicating positions at which cp_lexer_save_tokens was
182 called. The top entry is the most recent position at which we
183 began saving tokens. The entries are differences in token
184 position between FIRST_TOKEN and the first saved token.
186 If the stack is non-empty, we are saving tokens. When a token is
187 consumed, the NEXT_TOKEN pointer will move, but the FIRST_TOKEN
188 pointer will not. The token stream will be preserved so that it
189 can be reexamined later.
191 If the stack is empty, then we are not saving tokens. Whenever a
192 token is consumed, the FIRST_TOKEN pointer will be moved, and the
193 consumed token will be gone forever. */
194 varray_type saved_tokens;
196 /* The STRING_CST tokens encountered while processing the current
198 varray_type string_tokens;
200 /* True if we should obtain more tokens from the preprocessor; false
201 if we are processing a saved token cache. */
204 /* True if we should output debugging information. */
207 /* The next lexer in a linked list of lexers. */
208 struct cp_lexer *next;
213 static cp_lexer *cp_lexer_new_main
215 static cp_lexer *cp_lexer_new_from_tokens
216 (struct cp_token_cache *);
217 static int cp_lexer_saving_tokens
219 static cp_token *cp_lexer_next_token
220 (cp_lexer *, cp_token *);
221 static ptrdiff_t cp_lexer_token_difference
222 (cp_lexer *, cp_token *, cp_token *);
223 static cp_token *cp_lexer_read_token
225 static void cp_lexer_maybe_grow_buffer
227 static void cp_lexer_get_preprocessor_token
228 (cp_lexer *, cp_token *);
229 static cp_token *cp_lexer_peek_token
231 static cp_token *cp_lexer_peek_nth_token
232 (cp_lexer *, size_t);
233 static inline bool cp_lexer_next_token_is
234 (cp_lexer *, enum cpp_ttype);
235 static bool cp_lexer_next_token_is_not
236 (cp_lexer *, enum cpp_ttype);
237 static bool cp_lexer_next_token_is_keyword
238 (cp_lexer *, enum rid);
239 static cp_token *cp_lexer_consume_token
241 static void cp_lexer_purge_token
243 static void cp_lexer_purge_tokens_after
244 (cp_lexer *, cp_token *);
245 static void cp_lexer_save_tokens
247 static void cp_lexer_commit_tokens
249 static void cp_lexer_rollback_tokens
251 static inline void cp_lexer_set_source_position_from_token
252 (cp_lexer *, const cp_token *);
253 static void cp_lexer_print_token
254 (FILE *, cp_token *);
255 static inline bool cp_lexer_debugging_p
257 static void cp_lexer_start_debugging
258 (cp_lexer *) ATTRIBUTE_UNUSED;
259 static void cp_lexer_stop_debugging
260 (cp_lexer *) ATTRIBUTE_UNUSED;
262 /* Manifest constants. */
264 #define CP_TOKEN_BUFFER_SIZE 5
265 #define CP_SAVED_TOKENS_SIZE 5
267 /* A token type for keywords, as opposed to ordinary identifiers. */
268 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
270 /* A token type for template-ids. If a template-id is processed while
271 parsing tentatively, it is replaced with a CPP_TEMPLATE_ID token;
272 the value of the CPP_TEMPLATE_ID is whatever was returned by
273 cp_parser_template_id. */
274 #define CPP_TEMPLATE_ID ((enum cpp_ttype) (CPP_KEYWORD + 1))
276 /* A token type for nested-name-specifiers. If a
277 nested-name-specifier is processed while parsing tentatively, it is
278 replaced with a CPP_NESTED_NAME_SPECIFIER token; the value of the
279 CPP_NESTED_NAME_SPECIFIER is whatever was returned by
280 cp_parser_nested_name_specifier_opt. */
281 #define CPP_NESTED_NAME_SPECIFIER ((enum cpp_ttype) (CPP_TEMPLATE_ID + 1))
283 /* A token type for tokens that are not tokens at all; these are used
284 to mark the end of a token block. */
285 #define CPP_NONE (CPP_NESTED_NAME_SPECIFIER + 1)
289 /* The stream to which debugging output should be written. */
290 static FILE *cp_lexer_debug_stream;
292 /* Create a new main C++ lexer, the lexer that gets tokens from the
296 cp_lexer_new_main (void)
299 cp_token first_token;
301 /* It's possible that lexing the first token will load a PCH file,
302 which is a GC collection point. So we have to grab the first
303 token before allocating any memory. */
304 cp_lexer_get_preprocessor_token (NULL, &first_token);
305 c_common_no_more_pch ();
307 /* Allocate the memory. */
308 lexer = ggc_alloc_cleared (sizeof (cp_lexer));
310 /* Create the circular buffer. */
311 lexer->buffer = ggc_calloc (CP_TOKEN_BUFFER_SIZE, sizeof (cp_token));
312 lexer->buffer_end = lexer->buffer + CP_TOKEN_BUFFER_SIZE;
314 /* There is one token in the buffer. */
315 lexer->last_token = lexer->buffer + 1;
316 lexer->first_token = lexer->buffer;
317 lexer->next_token = lexer->buffer;
318 memcpy (lexer->buffer, &first_token, sizeof (cp_token));
320 /* This lexer obtains more tokens by calling c_lex. */
321 lexer->main_lexer_p = true;
323 /* Create the SAVED_TOKENS stack. */
324 VARRAY_INT_INIT (lexer->saved_tokens, CP_SAVED_TOKENS_SIZE, "saved_tokens");
326 /* Create the STRINGS array. */
327 VARRAY_TREE_INIT (lexer->string_tokens, 32, "strings");
329 /* Assume we are not debugging. */
330 lexer->debugging_p = false;
335 /* Create a new lexer whose token stream is primed with the TOKENS.
336 When these tokens are exhausted, no new tokens will be read. */
339 cp_lexer_new_from_tokens (cp_token_cache *tokens)
343 cp_token_block *block;
344 ptrdiff_t num_tokens;
346 /* Allocate the memory. */
347 lexer = ggc_alloc_cleared (sizeof (cp_lexer));
349 /* Create a new buffer, appropriately sized. */
351 for (block = tokens->first; block != NULL; block = block->next)
352 num_tokens += block->num_tokens;
353 lexer->buffer = ggc_alloc (num_tokens * sizeof (cp_token));
354 lexer->buffer_end = lexer->buffer + num_tokens;
356 /* Install the tokens. */
357 token = lexer->buffer;
358 for (block = tokens->first; block != NULL; block = block->next)
360 memcpy (token, block->tokens, block->num_tokens * sizeof (cp_token));
361 token += block->num_tokens;
364 /* The FIRST_TOKEN is the beginning of the buffer. */
365 lexer->first_token = lexer->buffer;
366 /* The next available token is also at the beginning of the buffer. */
367 lexer->next_token = lexer->buffer;
368 /* The buffer is full. */
369 lexer->last_token = lexer->first_token;
371 /* This lexer doesn't obtain more tokens. */
372 lexer->main_lexer_p = false;
374 /* Create the SAVED_TOKENS stack. */
375 VARRAY_INT_INIT (lexer->saved_tokens, CP_SAVED_TOKENS_SIZE, "saved_tokens");
377 /* Create the STRINGS array. */
378 VARRAY_TREE_INIT (lexer->string_tokens, 32, "strings");
380 /* Assume we are not debugging. */
381 lexer->debugging_p = false;
386 /* Returns nonzero if debugging information should be output. */
389 cp_lexer_debugging_p (cp_lexer *lexer)
391 return lexer->debugging_p;
394 /* Set the current source position from the information stored in
398 cp_lexer_set_source_position_from_token (cp_lexer *lexer ATTRIBUTE_UNUSED ,
399 const cp_token *token)
401 /* Ideally, the source position information would not be a global
402 variable, but it is. */
404 /* Update the line number. */
405 if (token->type != CPP_EOF)
406 input_location = token->location;
409 /* TOKEN points into the circular token buffer. Return a pointer to
410 the next token in the buffer. */
412 static inline cp_token *
413 cp_lexer_next_token (cp_lexer* lexer, cp_token* token)
416 if (token == lexer->buffer_end)
417 token = lexer->buffer;
421 /* nonzero if we are presently saving tokens. */
424 cp_lexer_saving_tokens (const cp_lexer* lexer)
426 return VARRAY_ACTIVE_SIZE (lexer->saved_tokens) != 0;
429 /* Return a pointer to the token that is N tokens beyond TOKEN in the
433 cp_lexer_advance_token (cp_lexer *lexer, cp_token *token, ptrdiff_t n)
436 if (token >= lexer->buffer_end)
437 token = lexer->buffer + (token - lexer->buffer_end);
441 /* Returns the number of times that START would have to be incremented
442 to reach FINISH. If START and FINISH are the same, returns zero. */
445 cp_lexer_token_difference (cp_lexer* lexer, cp_token* start, cp_token* finish)
448 return finish - start;
450 return ((lexer->buffer_end - lexer->buffer)
454 /* Obtain another token from the C preprocessor and add it to the
455 token buffer. Returns the newly read token. */
458 cp_lexer_read_token (cp_lexer* lexer)
462 /* Make sure there is room in the buffer. */
463 cp_lexer_maybe_grow_buffer (lexer);
465 /* If there weren't any tokens, then this one will be the first. */
466 if (!lexer->first_token)
467 lexer->first_token = lexer->last_token;
468 /* Similarly, if there were no available tokens, there is one now. */
469 if (!lexer->next_token)
470 lexer->next_token = lexer->last_token;
472 /* Figure out where we're going to store the new token. */
473 token = lexer->last_token;
475 /* Get a new token from the preprocessor. */
476 cp_lexer_get_preprocessor_token (lexer, token);
478 /* Increment LAST_TOKEN. */
479 lexer->last_token = cp_lexer_next_token (lexer, token);
481 /* Strings should have type `const char []'. Right now, we will
482 have an ARRAY_TYPE that is constant rather than an array of
484 FIXME: Make fix_string_type get this right in the first place. */
485 if ((token->type == CPP_STRING || token->type == CPP_WSTRING)
486 && flag_const_strings)
490 /* Get the current type. It will be an ARRAY_TYPE. */
491 type = TREE_TYPE (token->value);
492 /* Use build_cplus_array_type to rebuild the array, thereby
493 getting the right type. */
494 type = build_cplus_array_type (TREE_TYPE (type), TYPE_DOMAIN (type));
495 /* Reset the type of the token. */
496 TREE_TYPE (token->value) = type;
502 /* If the circular buffer is full, make it bigger. */
505 cp_lexer_maybe_grow_buffer (cp_lexer* lexer)
507 /* If the buffer is full, enlarge it. */
508 if (lexer->last_token == lexer->first_token)
510 cp_token *new_buffer;
511 cp_token *old_buffer;
512 cp_token *new_first_token;
513 ptrdiff_t buffer_length;
514 size_t num_tokens_to_copy;
516 /* Remember the current buffer pointer. It will become invalid,
517 but we will need to do pointer arithmetic involving this
519 old_buffer = lexer->buffer;
520 /* Compute the current buffer size. */
521 buffer_length = lexer->buffer_end - lexer->buffer;
522 /* Allocate a buffer twice as big. */
523 new_buffer = ggc_realloc (lexer->buffer,
524 2 * buffer_length * sizeof (cp_token));
526 /* Because the buffer is circular, logically consecutive tokens
527 are not necessarily placed consecutively in memory.
528 Therefore, we must keep move the tokens that were before
529 FIRST_TOKEN to the second half of the newly allocated
531 num_tokens_to_copy = (lexer->first_token - old_buffer);
532 memcpy (new_buffer + buffer_length,
534 num_tokens_to_copy * sizeof (cp_token));
535 /* Clear the rest of the buffer. We never look at this storage,
536 but the garbage collector may. */
537 memset (new_buffer + buffer_length + num_tokens_to_copy, 0,
538 (buffer_length - num_tokens_to_copy) * sizeof (cp_token));
540 /* Now recompute all of the buffer pointers. */
542 = new_buffer + (lexer->first_token - old_buffer);
543 if (lexer->next_token != NULL)
545 ptrdiff_t next_token_delta;
547 if (lexer->next_token > lexer->first_token)
548 next_token_delta = lexer->next_token - lexer->first_token;
551 buffer_length - (lexer->first_token - lexer->next_token);
552 lexer->next_token = new_first_token + next_token_delta;
554 lexer->last_token = new_first_token + buffer_length;
555 lexer->buffer = new_buffer;
556 lexer->buffer_end = new_buffer + buffer_length * 2;
557 lexer->first_token = new_first_token;
561 /* Store the next token from the preprocessor in *TOKEN. */
564 cp_lexer_get_preprocessor_token (cp_lexer *lexer ATTRIBUTE_UNUSED ,
569 /* If this not the main lexer, return a terminating CPP_EOF token. */
570 if (lexer != NULL && !lexer->main_lexer_p)
572 token->type = CPP_EOF;
573 token->location.line = 0;
574 token->location.file = NULL;
575 token->value = NULL_TREE;
576 token->keyword = RID_MAX;
582 /* Keep going until we get a token we like. */
585 /* Get a new token from the preprocessor. */
586 token->type = c_lex (&token->value);
587 /* Issue messages about tokens we cannot process. */
593 error ("invalid token");
597 /* This is a good token, so we exit the loop. */
602 /* Now we've got our token. */
603 token->location = input_location;
605 /* Check to see if this token is a keyword. */
606 if (token->type == CPP_NAME
607 && C_IS_RESERVED_WORD (token->value))
609 /* Mark this token as a keyword. */
610 token->type = CPP_KEYWORD;
611 /* Record which keyword. */
612 token->keyword = C_RID_CODE (token->value);
613 /* Update the value. Some keywords are mapped to particular
614 entities, rather than simply having the value of the
615 corresponding IDENTIFIER_NODE. For example, `__const' is
616 mapped to `const'. */
617 token->value = ridpointers[token->keyword];
620 token->keyword = RID_MAX;
623 /* Return a pointer to the next token in the token stream, but do not
627 cp_lexer_peek_token (cp_lexer* lexer)
631 /* If there are no tokens, read one now. */
632 if (!lexer->next_token)
633 cp_lexer_read_token (lexer);
635 /* Provide debugging output. */
636 if (cp_lexer_debugging_p (lexer))
638 fprintf (cp_lexer_debug_stream, "cp_lexer: peeking at token: ");
639 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
640 fprintf (cp_lexer_debug_stream, "\n");
643 token = lexer->next_token;
644 cp_lexer_set_source_position_from_token (lexer, token);
648 /* Return true if the next token has the indicated TYPE. */
651 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
655 /* Peek at the next token. */
656 token = cp_lexer_peek_token (lexer);
657 /* Check to see if it has the indicated TYPE. */
658 return token->type == type;
661 /* Return true if the next token does not have the indicated TYPE. */
664 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
666 return !cp_lexer_next_token_is (lexer, type);
669 /* Return true if the next token is the indicated KEYWORD. */
672 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
676 /* Peek at the next token. */
677 token = cp_lexer_peek_token (lexer);
678 /* Check to see if it is the indicated keyword. */
679 return token->keyword == keyword;
682 /* Return a pointer to the Nth token in the token stream. If N is 1,
683 then this is precisely equivalent to cp_lexer_peek_token. */
686 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
690 /* N is 1-based, not zero-based. */
691 my_friendly_assert (n > 0, 20000224);
693 /* Skip ahead from NEXT_TOKEN, reading more tokens as necessary. */
694 token = lexer->next_token;
695 /* If there are no tokens in the buffer, get one now. */
698 cp_lexer_read_token (lexer);
699 token = lexer->next_token;
702 /* Now, read tokens until we have enough. */
705 /* Advance to the next token. */
706 token = cp_lexer_next_token (lexer, token);
707 /* If that's all the tokens we have, read a new one. */
708 if (token == lexer->last_token)
709 token = cp_lexer_read_token (lexer);
715 /* Consume the next token. The pointer returned is valid only until
716 another token is read. Callers should preserve copy the token
717 explicitly if they will need its value for a longer period of
721 cp_lexer_consume_token (cp_lexer* lexer)
725 /* If there are no tokens, read one now. */
726 if (!lexer->next_token)
727 cp_lexer_read_token (lexer);
729 /* Remember the token we'll be returning. */
730 token = lexer->next_token;
732 /* Increment NEXT_TOKEN. */
733 lexer->next_token = cp_lexer_next_token (lexer,
735 /* Check to see if we're all out of tokens. */
736 if (lexer->next_token == lexer->last_token)
737 lexer->next_token = NULL;
739 /* If we're not saving tokens, then move FIRST_TOKEN too. */
740 if (!cp_lexer_saving_tokens (lexer))
742 /* If there are no tokens available, set FIRST_TOKEN to NULL. */
743 if (!lexer->next_token)
744 lexer->first_token = NULL;
746 lexer->first_token = lexer->next_token;
749 /* Provide debugging output. */
750 if (cp_lexer_debugging_p (lexer))
752 fprintf (cp_lexer_debug_stream, "cp_lexer: consuming token: ");
753 cp_lexer_print_token (cp_lexer_debug_stream, token);
754 fprintf (cp_lexer_debug_stream, "\n");
760 /* Permanently remove the next token from the token stream. There
761 must be a valid next token already; this token never reads
762 additional tokens from the preprocessor. */
765 cp_lexer_purge_token (cp_lexer *lexer)
768 cp_token *next_token;
770 token = lexer->next_token;
773 next_token = cp_lexer_next_token (lexer, token);
774 if (next_token == lexer->last_token)
776 *token = *next_token;
780 lexer->last_token = token;
781 /* The token purged may have been the only token remaining; if so,
783 if (lexer->next_token == token)
784 lexer->next_token = NULL;
787 /* Permanently remove all tokens after TOKEN, up to, but not
788 including, the token that will be returned next by
789 cp_lexer_peek_token. */
792 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *token)
798 if (lexer->next_token)
800 /* Copy the tokens that have not yet been read to the location
801 immediately following TOKEN. */
802 t1 = cp_lexer_next_token (lexer, token);
803 t2 = peek = cp_lexer_peek_token (lexer);
804 /* Move tokens into the vacant area between TOKEN and PEEK. */
805 while (t2 != lexer->last_token)
808 t1 = cp_lexer_next_token (lexer, t1);
809 t2 = cp_lexer_next_token (lexer, t2);
811 /* Now, the next available token is right after TOKEN. */
812 lexer->next_token = cp_lexer_next_token (lexer, token);
813 /* And the last token is wherever we ended up. */
814 lexer->last_token = t1;
818 /* There are no tokens in the buffer, so there is nothing to
819 copy. The last token in the buffer is TOKEN itself. */
820 lexer->last_token = cp_lexer_next_token (lexer, token);
824 /* Begin saving tokens. All tokens consumed after this point will be
828 cp_lexer_save_tokens (cp_lexer* lexer)
830 /* Provide debugging output. */
831 if (cp_lexer_debugging_p (lexer))
832 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
834 /* Make sure that LEXER->NEXT_TOKEN is non-NULL so that we can
835 restore the tokens if required. */
836 if (!lexer->next_token)
837 cp_lexer_read_token (lexer);
839 VARRAY_PUSH_INT (lexer->saved_tokens,
840 cp_lexer_token_difference (lexer,
845 /* Commit to the portion of the token stream most recently saved. */
848 cp_lexer_commit_tokens (cp_lexer* lexer)
850 /* Provide debugging output. */
851 if (cp_lexer_debugging_p (lexer))
852 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
854 VARRAY_POP (lexer->saved_tokens);
857 /* Return all tokens saved since the last call to cp_lexer_save_tokens
858 to the token stream. Stop saving tokens. */
861 cp_lexer_rollback_tokens (cp_lexer* lexer)
865 /* Provide debugging output. */
866 if (cp_lexer_debugging_p (lexer))
867 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
869 /* Find the token that was the NEXT_TOKEN when we started saving
871 delta = VARRAY_TOP_INT(lexer->saved_tokens);
872 /* Make it the next token again now. */
873 lexer->next_token = cp_lexer_advance_token (lexer,
876 /* It might be the case that there were no tokens when we started
877 saving tokens, but that there are some tokens now. */
878 if (!lexer->next_token && lexer->first_token)
879 lexer->next_token = lexer->first_token;
881 /* Stop saving tokens. */
882 VARRAY_POP (lexer->saved_tokens);
885 /* Print a representation of the TOKEN on the STREAM. */
888 cp_lexer_print_token (FILE * stream, cp_token* token)
890 const char *token_type = NULL;
892 /* Figure out what kind of token this is. */
900 token_type = "COMMA";
904 token_type = "OPEN_PAREN";
907 case CPP_CLOSE_PAREN:
908 token_type = "CLOSE_PAREN";
912 token_type = "OPEN_BRACE";
915 case CPP_CLOSE_BRACE:
916 token_type = "CLOSE_BRACE";
920 token_type = "SEMICOLON";
932 token_type = "keyword";
935 /* This is not a token that we know how to handle yet. */
940 /* If we have a name for the token, print it out. Otherwise, we
941 simply give the numeric code. */
943 fprintf (stream, "%s", token_type);
945 fprintf (stream, "%d", token->type);
946 /* And, for an identifier, print the identifier name. */
947 if (token->type == CPP_NAME
948 /* Some keywords have a value that is not an IDENTIFIER_NODE.
949 For example, `struct' is mapped to an INTEGER_CST. */
950 || (token->type == CPP_KEYWORD
951 && TREE_CODE (token->value) == IDENTIFIER_NODE))
952 fprintf (stream, " %s", IDENTIFIER_POINTER (token->value));
955 /* Start emitting debugging information. */
958 cp_lexer_start_debugging (cp_lexer* lexer)
960 ++lexer->debugging_p;
963 /* Stop emitting debugging information. */
966 cp_lexer_stop_debugging (cp_lexer* lexer)
968 --lexer->debugging_p;
977 A cp_parser parses the token stream as specified by the C++
978 grammar. Its job is purely parsing, not semantic analysis. For
979 example, the parser breaks the token stream into declarators,
980 expressions, statements, and other similar syntactic constructs.
981 It does not check that the types of the expressions on either side
982 of an assignment-statement are compatible, or that a function is
983 not declared with a parameter of type `void'.
985 The parser invokes routines elsewhere in the compiler to perform
986 semantic analysis and to build up the abstract syntax tree for the
989 The parser (and the template instantiation code, which is, in a
990 way, a close relative of parsing) are the only parts of the
991 compiler that should be calling push_scope and pop_scope, or
992 related functions. The parser (and template instantiation code)
993 keeps track of what scope is presently active; everything else
994 should simply honor that. (The code that generates static
995 initializers may also need to set the scope, in order to check
996 access control correctly when emitting the initializers.)
1001 The parser is of the standard recursive-descent variety. Upcoming
1002 tokens in the token stream are examined in order to determine which
1003 production to use when parsing a non-terminal. Some C++ constructs
1004 require arbitrary look ahead to disambiguate. For example, it is
1005 impossible, in the general case, to tell whether a statement is an
1006 expression or declaration without scanning the entire statement.
1007 Therefore, the parser is capable of "parsing tentatively." When the
1008 parser is not sure what construct comes next, it enters this mode.
1009 Then, while we attempt to parse the construct, the parser queues up
1010 error messages, rather than issuing them immediately, and saves the
1011 tokens it consumes. If the construct is parsed successfully, the
1012 parser "commits", i.e., it issues any queued error messages and
1013 the tokens that were being preserved are permanently discarded.
1014 If, however, the construct is not parsed successfully, the parser
1015 rolls back its state completely so that it can resume parsing using
1016 a different alternative.
1021 The performance of the parser could probably be improved
1022 substantially. Some possible improvements include:
1024 - The expression parser recurses through the various levels of
1025 precedence as specified in the grammar, rather than using an
1026 operator-precedence technique. Therefore, parsing a simple
1027 identifier requires multiple recursive calls.
1029 - We could often eliminate the need to parse tentatively by
1030 looking ahead a little bit. In some places, this approach
1031 might not entirely eliminate the need to parse tentatively, but
1032 it might still speed up the average case. */
1034 /* Flags that are passed to some parsing functions. These values can
1035 be bitwise-ored together. */
1037 typedef enum cp_parser_flags
1040 CP_PARSER_FLAGS_NONE = 0x0,
1041 /* The construct is optional. If it is not present, then no error
1042 should be issued. */
1043 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1044 /* When parsing a type-specifier, do not allow user-defined types. */
1045 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2
1048 /* The different kinds of declarators we want to parse. */
1050 typedef enum cp_parser_declarator_kind
1052 /* We want an abstract declartor. */
1053 CP_PARSER_DECLARATOR_ABSTRACT,
1054 /* We want a named declarator. */
1055 CP_PARSER_DECLARATOR_NAMED,
1056 /* We don't mind, but the name must be an unqualified-id */
1057 CP_PARSER_DECLARATOR_EITHER
1058 } cp_parser_declarator_kind;
1060 /* A mapping from a token type to a corresponding tree node type. */
1062 typedef struct cp_parser_token_tree_map_node
1064 /* The token type. */
1065 enum cpp_ttype token_type;
1066 /* The corresponding tree code. */
1067 enum tree_code tree_type;
1068 } cp_parser_token_tree_map_node;
1070 /* A complete map consists of several ordinary entries, followed by a
1071 terminator. The terminating entry has a token_type of CPP_EOF. */
1073 typedef cp_parser_token_tree_map_node cp_parser_token_tree_map[];
1075 /* The status of a tentative parse. */
1077 typedef enum cp_parser_status_kind
1079 /* No errors have occurred. */
1080 CP_PARSER_STATUS_KIND_NO_ERROR,
1081 /* An error has occurred. */
1082 CP_PARSER_STATUS_KIND_ERROR,
1083 /* We are committed to this tentative parse, whether or not an error
1085 CP_PARSER_STATUS_KIND_COMMITTED
1086 } cp_parser_status_kind;
1088 /* Context that is saved and restored when parsing tentatively. */
1090 typedef struct cp_parser_context GTY (())
1092 /* If this is a tentative parsing context, the status of the
1094 enum cp_parser_status_kind status;
1095 /* If non-NULL, we have just seen a `x->' or `x.' expression. Names
1096 that are looked up in this context must be looked up both in the
1097 scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1098 the context of the containing expression. */
1100 /* The next parsing context in the stack. */
1101 struct cp_parser_context *next;
1102 } cp_parser_context;
1106 /* Constructors and destructors. */
1108 static cp_parser_context *cp_parser_context_new
1109 (cp_parser_context *);
1111 /* Class variables. */
1113 static GTY((deletable (""))) cp_parser_context* cp_parser_context_free_list;
1115 /* Constructors and destructors. */
1117 /* Construct a new context. The context below this one on the stack
1118 is given by NEXT. */
1120 static cp_parser_context *
1121 cp_parser_context_new (cp_parser_context* next)
1123 cp_parser_context *context;
1125 /* Allocate the storage. */
1126 if (cp_parser_context_free_list != NULL)
1128 /* Pull the first entry from the free list. */
1129 context = cp_parser_context_free_list;
1130 cp_parser_context_free_list = context->next;
1131 memset (context, 0, sizeof (*context));
1134 context = ggc_alloc_cleared (sizeof (cp_parser_context));
1135 /* No errors have occurred yet in this context. */
1136 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1137 /* If this is not the bottomost context, copy information that we
1138 need from the previous context. */
1141 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1142 expression, then we are parsing one in this context, too. */
1143 context->object_type = next->object_type;
1144 /* Thread the stack. */
1145 context->next = next;
1151 /* The cp_parser structure represents the C++ parser. */
1153 typedef struct cp_parser GTY(())
1155 /* The lexer from which we are obtaining tokens. */
1158 /* The scope in which names should be looked up. If NULL_TREE, then
1159 we look up names in the scope that is currently open in the
1160 source program. If non-NULL, this is either a TYPE or
1161 NAMESPACE_DECL for the scope in which we should look.
1163 This value is not cleared automatically after a name is looked
1164 up, so we must be careful to clear it before starting a new look
1165 up sequence. (If it is not cleared, then `X::Y' followed by `Z'
1166 will look up `Z' in the scope of `X', rather than the current
1167 scope.) Unfortunately, it is difficult to tell when name lookup
1168 is complete, because we sometimes peek at a token, look it up,
1169 and then decide not to consume it. */
1172 /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1173 last lookup took place. OBJECT_SCOPE is used if an expression
1174 like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1175 respectively. QUALIFYING_SCOPE is used for an expression of the
1176 form "X::Y"; it refers to X. */
1178 tree qualifying_scope;
1180 /* A stack of parsing contexts. All but the bottom entry on the
1181 stack will be tentative contexts.
1183 We parse tentatively in order to determine which construct is in
1184 use in some situations. For example, in order to determine
1185 whether a statement is an expression-statement or a
1186 declaration-statement we parse it tentatively as a
1187 declaration-statement. If that fails, we then reparse the same
1188 token stream as an expression-statement. */
1189 cp_parser_context *context;
1191 /* True if we are parsing GNU C++. If this flag is not set, then
1192 GNU extensions are not recognized. */
1193 bool allow_gnu_extensions_p;
1195 /* TRUE if the `>' token should be interpreted as the greater-than
1196 operator. FALSE if it is the end of a template-id or
1197 template-parameter-list. */
1198 bool greater_than_is_operator_p;
1200 /* TRUE if default arguments are allowed within a parameter list
1201 that starts at this point. FALSE if only a gnu extension makes
1202 them permissable. */
1203 bool default_arg_ok_p;
1205 /* TRUE if we are parsing an integral constant-expression. See
1206 [expr.const] for a precise definition. */
1207 bool constant_expression_p;
1209 /* TRUE if we are parsing an integral constant-expression -- but a
1210 non-constant expression should be permitted as well. This flag
1211 is used when parsing an array bound so that GNU variable-length
1212 arrays are tolerated. */
1213 bool allow_non_constant_expression_p;
1215 /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1216 been seen that makes the expression non-constant. */
1217 bool non_constant_expression_p;
1219 /* TRUE if local variable names and `this' are forbidden in the
1221 bool local_variables_forbidden_p;
1223 /* TRUE if the declaration we are parsing is part of a
1224 linkage-specification of the form `extern string-literal
1226 bool in_unbraced_linkage_specification_p;
1228 /* TRUE if we are presently parsing a declarator, after the
1229 direct-declarator. */
1230 bool in_declarator_p;
1232 /* If non-NULL, then we are parsing a construct where new type
1233 definitions are not permitted. The string stored here will be
1234 issued as an error message if a type is defined. */
1235 const char *type_definition_forbidden_message;
1237 /* A list of lists. The outer list is a stack, used for member
1238 functions of local classes. At each level there are two sub-list,
1239 one on TREE_VALUE and one on TREE_PURPOSE. Each of those
1240 sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their
1241 TREE_VALUE's. The functions are chained in reverse declaration
1244 The TREE_PURPOSE sublist contains those functions with default
1245 arguments that need post processing, and the TREE_VALUE sublist
1246 contains those functions with definitions that need post
1249 These lists can only be processed once the outermost class being
1250 defined is complete. */
1251 tree unparsed_functions_queues;
1253 /* The number of classes whose definitions are currently in
1255 unsigned num_classes_being_defined;
1257 /* The number of template parameter lists that apply directly to the
1258 current declaration. */
1259 unsigned num_template_parameter_lists;
1262 /* The type of a function that parses some kind of expression */
1263 typedef tree (*cp_parser_expression_fn) (cp_parser *);
1267 /* Constructors and destructors. */
1269 static cp_parser *cp_parser_new
1272 /* Routines to parse various constructs.
1274 Those that return `tree' will return the error_mark_node (rather
1275 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1276 Sometimes, they will return an ordinary node if error-recovery was
1277 attempted, even though a parse error occurred. So, to check
1278 whether or not a parse error occurred, you should always use
1279 cp_parser_error_occurred. If the construct is optional (indicated
1280 either by an `_opt' in the name of the function that does the
1281 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1282 the construct is not present. */
1284 /* Lexical conventions [gram.lex] */
1286 static tree cp_parser_identifier
1289 /* Basic concepts [gram.basic] */
1291 static bool cp_parser_translation_unit
1294 /* Expressions [gram.expr] */
1296 static tree cp_parser_primary_expression
1297 (cp_parser *, cp_id_kind *, tree *);
1298 static tree cp_parser_id_expression
1299 (cp_parser *, bool, bool, bool *);
1300 static tree cp_parser_unqualified_id
1301 (cp_parser *, bool, bool);
1302 static tree cp_parser_nested_name_specifier_opt
1303 (cp_parser *, bool, bool, bool);
1304 static tree cp_parser_nested_name_specifier
1305 (cp_parser *, bool, bool, bool);
1306 static tree cp_parser_class_or_namespace_name
1307 (cp_parser *, bool, bool, bool, bool);
1308 static tree cp_parser_postfix_expression
1309 (cp_parser *, bool);
1310 static tree cp_parser_parenthesized_expression_list
1311 (cp_parser *, bool, bool *);
1312 static void cp_parser_pseudo_destructor_name
1313 (cp_parser *, tree *, tree *);
1314 static tree cp_parser_unary_expression
1315 (cp_parser *, bool);
1316 static enum tree_code cp_parser_unary_operator
1318 static tree cp_parser_new_expression
1320 static tree cp_parser_new_placement
1322 static tree cp_parser_new_type_id
1324 static tree cp_parser_new_declarator_opt
1326 static tree cp_parser_direct_new_declarator
1328 static tree cp_parser_new_initializer
1330 static tree cp_parser_delete_expression
1332 static tree cp_parser_cast_expression
1333 (cp_parser *, bool);
1334 static tree cp_parser_pm_expression
1336 static tree cp_parser_multiplicative_expression
1338 static tree cp_parser_additive_expression
1340 static tree cp_parser_shift_expression
1342 static tree cp_parser_relational_expression
1344 static tree cp_parser_equality_expression
1346 static tree cp_parser_and_expression
1348 static tree cp_parser_exclusive_or_expression
1350 static tree cp_parser_inclusive_or_expression
1352 static tree cp_parser_logical_and_expression
1354 static tree cp_parser_logical_or_expression
1356 static tree cp_parser_question_colon_clause
1357 (cp_parser *, tree);
1358 static tree cp_parser_assignment_expression
1360 static enum tree_code cp_parser_assignment_operator_opt
1362 static tree cp_parser_expression
1364 static tree cp_parser_constant_expression
1365 (cp_parser *, bool, bool *);
1367 /* Statements [gram.stmt.stmt] */
1369 static void cp_parser_statement
1370 (cp_parser *, bool);
1371 static tree cp_parser_labeled_statement
1372 (cp_parser *, bool);
1373 static tree cp_parser_expression_statement
1374 (cp_parser *, bool);
1375 static tree cp_parser_compound_statement
1376 (cp_parser *, bool);
1377 static void cp_parser_statement_seq_opt
1378 (cp_parser *, bool);
1379 static tree cp_parser_selection_statement
1381 static tree cp_parser_condition
1383 static tree cp_parser_iteration_statement
1385 static void cp_parser_for_init_statement
1387 static tree cp_parser_jump_statement
1389 static void cp_parser_declaration_statement
1392 static tree cp_parser_implicitly_scoped_statement
1394 static void cp_parser_already_scoped_statement
1397 /* Declarations [gram.dcl.dcl] */
1399 static void cp_parser_declaration_seq_opt
1401 static void cp_parser_declaration
1403 static void cp_parser_block_declaration
1404 (cp_parser *, bool);
1405 static void cp_parser_simple_declaration
1406 (cp_parser *, bool);
1407 static tree cp_parser_decl_specifier_seq
1408 (cp_parser *, cp_parser_flags, tree *, int *);
1409 static tree cp_parser_storage_class_specifier_opt
1411 static tree cp_parser_function_specifier_opt
1413 static tree cp_parser_type_specifier
1414 (cp_parser *, cp_parser_flags, bool, bool, int *, bool *);
1415 static tree cp_parser_simple_type_specifier
1416 (cp_parser *, cp_parser_flags, bool);
1417 static tree cp_parser_type_name
1419 static tree cp_parser_elaborated_type_specifier
1420 (cp_parser *, bool, bool);
1421 static tree cp_parser_enum_specifier
1423 static void cp_parser_enumerator_list
1424 (cp_parser *, tree);
1425 static void cp_parser_enumerator_definition
1426 (cp_parser *, tree);
1427 static tree cp_parser_namespace_name
1429 static void cp_parser_namespace_definition
1431 static void cp_parser_namespace_body
1433 static tree cp_parser_qualified_namespace_specifier
1435 static void cp_parser_namespace_alias_definition
1437 static void cp_parser_using_declaration
1439 static void cp_parser_using_directive
1441 static void cp_parser_asm_definition
1443 static void cp_parser_linkage_specification
1446 /* Declarators [gram.dcl.decl] */
1448 static tree cp_parser_init_declarator
1449 (cp_parser *, tree, tree, bool, bool, int, bool *);
1450 static tree cp_parser_declarator
1451 (cp_parser *, cp_parser_declarator_kind, int *);
1452 static tree cp_parser_direct_declarator
1453 (cp_parser *, cp_parser_declarator_kind, int *);
1454 static enum tree_code cp_parser_ptr_operator
1455 (cp_parser *, tree *, tree *);
1456 static tree cp_parser_cv_qualifier_seq_opt
1458 static tree cp_parser_cv_qualifier_opt
1460 static tree cp_parser_declarator_id
1462 static tree cp_parser_type_id
1464 static tree cp_parser_type_specifier_seq
1466 static tree cp_parser_parameter_declaration_clause
1468 static tree cp_parser_parameter_declaration_list
1470 static tree cp_parser_parameter_declaration
1471 (cp_parser *, bool);
1472 static tree cp_parser_function_definition
1473 (cp_parser *, bool *);
1474 static void cp_parser_function_body
1476 static tree cp_parser_initializer
1477 (cp_parser *, bool *, bool *);
1478 static tree cp_parser_initializer_clause
1479 (cp_parser *, bool *);
1480 static tree cp_parser_initializer_list
1481 (cp_parser *, bool *);
1483 static bool cp_parser_ctor_initializer_opt_and_function_body
1486 /* Classes [gram.class] */
1488 static tree cp_parser_class_name
1489 (cp_parser *, bool, bool, bool, bool, bool);
1490 static tree cp_parser_class_specifier
1492 static tree cp_parser_class_head
1493 (cp_parser *, bool *);
1494 static enum tag_types cp_parser_class_key
1496 static void cp_parser_member_specification_opt
1498 static void cp_parser_member_declaration
1500 static tree cp_parser_pure_specifier
1502 static tree cp_parser_constant_initializer
1505 /* Derived classes [gram.class.derived] */
1507 static tree cp_parser_base_clause
1509 static tree cp_parser_base_specifier
1512 /* Special member functions [gram.special] */
1514 static tree cp_parser_conversion_function_id
1516 static tree cp_parser_conversion_type_id
1518 static tree cp_parser_conversion_declarator_opt
1520 static bool cp_parser_ctor_initializer_opt
1522 static void cp_parser_mem_initializer_list
1524 static tree cp_parser_mem_initializer
1526 static tree cp_parser_mem_initializer_id
1529 /* Overloading [gram.over] */
1531 static tree cp_parser_operator_function_id
1533 static tree cp_parser_operator
1536 /* Templates [gram.temp] */
1538 static void cp_parser_template_declaration
1539 (cp_parser *, bool);
1540 static tree cp_parser_template_parameter_list
1542 static tree cp_parser_template_parameter
1544 static tree cp_parser_type_parameter
1546 static tree cp_parser_template_id
1547 (cp_parser *, bool, bool);
1548 static tree cp_parser_template_name
1549 (cp_parser *, bool, bool);
1550 static tree cp_parser_template_argument_list
1552 static tree cp_parser_template_argument
1554 static void cp_parser_explicit_instantiation
1556 static void cp_parser_explicit_specialization
1559 /* Exception handling [gram.exception] */
1561 static tree cp_parser_try_block
1563 static bool cp_parser_function_try_block
1565 static void cp_parser_handler_seq
1567 static void cp_parser_handler
1569 static tree cp_parser_exception_declaration
1571 static tree cp_parser_throw_expression
1573 static tree cp_parser_exception_specification_opt
1575 static tree cp_parser_type_id_list
1578 /* GNU Extensions */
1580 static tree cp_parser_asm_specification_opt
1582 static tree cp_parser_asm_operand_list
1584 static tree cp_parser_asm_clobber_list
1586 static tree cp_parser_attributes_opt
1588 static tree cp_parser_attribute_list
1590 static bool cp_parser_extension_opt
1591 (cp_parser *, int *);
1592 static void cp_parser_label_declaration
1595 /* Utility Routines */
1597 static tree cp_parser_lookup_name
1598 (cp_parser *, tree, bool, bool, bool);
1599 static tree cp_parser_lookup_name_simple
1600 (cp_parser *, tree);
1601 static tree cp_parser_maybe_treat_template_as_class
1603 static bool cp_parser_check_declarator_template_parameters
1604 (cp_parser *, tree);
1605 static bool cp_parser_check_template_parameters
1606 (cp_parser *, unsigned);
1607 static tree cp_parser_simple_cast_expression
1609 static tree cp_parser_binary_expression
1610 (cp_parser *, const cp_parser_token_tree_map, cp_parser_expression_fn);
1611 static tree cp_parser_global_scope_opt
1612 (cp_parser *, bool);
1613 static bool cp_parser_constructor_declarator_p
1614 (cp_parser *, bool);
1615 static tree cp_parser_function_definition_from_specifiers_and_declarator
1616 (cp_parser *, tree, tree, tree);
1617 static tree cp_parser_function_definition_after_declarator
1618 (cp_parser *, bool);
1619 static void cp_parser_template_declaration_after_export
1620 (cp_parser *, bool);
1621 static tree cp_parser_single_declaration
1622 (cp_parser *, bool, bool *);
1623 static tree cp_parser_functional_cast
1624 (cp_parser *, tree);
1625 static void cp_parser_save_default_args
1626 (cp_parser *, tree);
1627 static void cp_parser_late_parsing_for_member
1628 (cp_parser *, tree);
1629 static void cp_parser_late_parsing_default_args
1630 (cp_parser *, tree);
1631 static tree cp_parser_sizeof_operand
1632 (cp_parser *, enum rid);
1633 static bool cp_parser_declares_only_class_p
1635 static tree cp_parser_fold_non_dependent_expr
1637 static bool cp_parser_friend_p
1639 static cp_token *cp_parser_require
1640 (cp_parser *, enum cpp_ttype, const char *);
1641 static cp_token *cp_parser_require_keyword
1642 (cp_parser *, enum rid, const char *);
1643 static bool cp_parser_token_starts_function_definition_p
1645 static bool cp_parser_next_token_starts_class_definition_p
1647 static bool cp_parser_next_token_ends_template_argument_p
1649 static enum tag_types cp_parser_token_is_class_key
1651 static void cp_parser_check_class_key
1652 (enum tag_types, tree type);
1653 static bool cp_parser_optional_template_keyword
1655 static void cp_parser_pre_parsed_nested_name_specifier
1657 static void cp_parser_cache_group
1658 (cp_parser *, cp_token_cache *, enum cpp_ttype, unsigned);
1659 static void cp_parser_parse_tentatively
1661 static void cp_parser_commit_to_tentative_parse
1663 static void cp_parser_abort_tentative_parse
1665 static bool cp_parser_parse_definitely
1667 static inline bool cp_parser_parsing_tentatively
1669 static bool cp_parser_committed_to_tentative_parse
1671 static void cp_parser_error
1672 (cp_parser *, const char *);
1673 static bool cp_parser_simulate_error
1675 static void cp_parser_check_type_definition
1677 static void cp_parser_check_for_definition_in_return_type
1679 static tree cp_parser_non_constant_expression
1681 static bool cp_parser_diagnose_invalid_type_name
1683 static int cp_parser_skip_to_closing_parenthesis
1684 (cp_parser *, bool, bool);
1685 static void cp_parser_skip_to_end_of_statement
1687 static void cp_parser_consume_semicolon_at_end_of_statement
1689 static void cp_parser_skip_to_end_of_block_or_statement
1691 static void cp_parser_skip_to_closing_brace
1693 static void cp_parser_skip_until_found
1694 (cp_parser *, enum cpp_ttype, const char *);
1695 static bool cp_parser_error_occurred
1697 static bool cp_parser_allow_gnu_extensions_p
1699 static bool cp_parser_is_string_literal
1701 static bool cp_parser_is_keyword
1702 (cp_token *, enum rid);
1704 /* Returns nonzero if we are parsing tentatively. */
1707 cp_parser_parsing_tentatively (cp_parser* parser)
1709 return parser->context->next != NULL;
1712 /* Returns nonzero if TOKEN is a string literal. */
1715 cp_parser_is_string_literal (cp_token* token)
1717 return (token->type == CPP_STRING || token->type == CPP_WSTRING);
1720 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
1723 cp_parser_is_keyword (cp_token* token, enum rid keyword)
1725 return token->keyword == keyword;
1728 /* Issue the indicated error MESSAGE. */
1731 cp_parser_error (cp_parser* parser, const char* message)
1733 /* Output the MESSAGE -- unless we're parsing tentatively. */
1734 if (!cp_parser_simulate_error (parser))
1738 /* If we are parsing tentatively, remember that an error has occurred
1739 during this tentative parse. Returns true if the error was
1740 simulated; false if a messgae should be issued by the caller. */
1743 cp_parser_simulate_error (cp_parser* parser)
1745 if (cp_parser_parsing_tentatively (parser)
1746 && !cp_parser_committed_to_tentative_parse (parser))
1748 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
1754 /* This function is called when a type is defined. If type
1755 definitions are forbidden at this point, an error message is
1759 cp_parser_check_type_definition (cp_parser* parser)
1761 /* If types are forbidden here, issue a message. */
1762 if (parser->type_definition_forbidden_message)
1763 /* Use `%s' to print the string in case there are any escape
1764 characters in the message. */
1765 error ("%s", parser->type_definition_forbidden_message);
1768 /* This function is called when a declaration is parsed. If
1769 DECLARATOR is a function declarator and DECLARES_CLASS_OR_ENUM
1770 indicates that a type was defined in the decl-specifiers for DECL,
1771 then an error is issued. */
1774 cp_parser_check_for_definition_in_return_type (tree declarator,
1775 int declares_class_or_enum)
1777 /* [dcl.fct] forbids type definitions in return types.
1778 Unfortunately, it's not easy to know whether or not we are
1779 processing a return type until after the fact. */
1781 && (TREE_CODE (declarator) == INDIRECT_REF
1782 || TREE_CODE (declarator) == ADDR_EXPR))
1783 declarator = TREE_OPERAND (declarator, 0);
1785 && TREE_CODE (declarator) == CALL_EXPR
1786 && declares_class_or_enum & 2)
1787 error ("new types may not be defined in a return type");
1790 /* Issue an eror message about the fact that THING appeared in a
1791 constant-expression. Returns ERROR_MARK_NODE. */
1794 cp_parser_non_constant_expression (const char *thing)
1796 error ("%s cannot appear in a constant-expression", thing);
1797 return error_mark_node;
1800 /* Check for a common situation where a type-name should be present,
1801 but is not, and issue a sensible error message. Returns true if an
1802 invalid type-name was detected. */
1805 cp_parser_diagnose_invalid_type_name (cp_parser *parser)
1807 /* If the next two tokens are both identifiers, the code is
1808 erroneous. The usual cause of this situation is code like:
1812 where "T" should name a type -- but does not. */
1813 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
1814 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
1818 /* If parsing tentatively, we should commit; we really are
1819 looking at a declaration. */
1820 /* Consume the first identifier. */
1821 name = cp_lexer_consume_token (parser->lexer)->value;
1822 /* Issue an error message. */
1823 error ("`%s' does not name a type", IDENTIFIER_POINTER (name));
1824 /* If we're in a template class, it's possible that the user was
1825 referring to a type from a base class. For example:
1827 template <typename T> struct A { typedef T X; };
1828 template <typename T> struct B : public A<T> { X x; };
1830 The user should have said "typename A<T>::X". */
1831 if (processing_template_decl && current_class_type)
1835 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
1839 tree base_type = BINFO_TYPE (b);
1840 if (CLASS_TYPE_P (base_type)
1841 && dependent_type_p (base_type))
1844 /* Go from a particular instantiation of the
1845 template (which will have an empty TYPE_FIELDs),
1846 to the main version. */
1847 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
1848 for (field = TYPE_FIELDS (base_type);
1850 field = TREE_CHAIN (field))
1851 if (TREE_CODE (field) == TYPE_DECL
1852 && DECL_NAME (field) == name)
1854 error ("(perhaps `typename %T::%s' was intended)",
1855 BINFO_TYPE (b), IDENTIFIER_POINTER (name));
1863 /* Skip to the end of the declaration; there's no point in
1864 trying to process it. */
1865 cp_parser_skip_to_end_of_statement (parser);
1873 /* Consume tokens up to, and including, the next non-nested closing `)'.
1874 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
1875 are doing error recovery. Returns -1 if OR_COMMA is true and we
1876 found an unnested comma. */
1879 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
1880 bool recovering, bool or_comma)
1882 unsigned paren_depth = 0;
1883 unsigned brace_depth = 0;
1885 if (recovering && !or_comma && cp_parser_parsing_tentatively (parser)
1886 && !cp_parser_committed_to_tentative_parse (parser))
1893 /* If we've run out of tokens, then there is no closing `)'. */
1894 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
1899 token = cp_lexer_peek_token (parser->lexer);
1901 /* This matches the processing in skip_to_end_of_statement */
1902 if (token->type == CPP_SEMICOLON && !brace_depth)
1904 if (token->type == CPP_OPEN_BRACE)
1906 if (token->type == CPP_CLOSE_BRACE)
1911 if (or_comma && token->type == CPP_COMMA
1912 && !brace_depth && !paren_depth)
1916 /* Consume the token. */
1917 token = cp_lexer_consume_token (parser->lexer);
1921 /* If it is an `(', we have entered another level of nesting. */
1922 if (token->type == CPP_OPEN_PAREN)
1924 /* If it is a `)', then we might be done. */
1925 else if (token->type == CPP_CLOSE_PAREN && !paren_depth--)
1931 /* Consume tokens until we reach the end of the current statement.
1932 Normally, that will be just before consuming a `;'. However, if a
1933 non-nested `}' comes first, then we stop before consuming that. */
1936 cp_parser_skip_to_end_of_statement (cp_parser* parser)
1938 unsigned nesting_depth = 0;
1944 /* Peek at the next token. */
1945 token = cp_lexer_peek_token (parser->lexer);
1946 /* If we've run out of tokens, stop. */
1947 if (token->type == CPP_EOF)
1949 /* If the next token is a `;', we have reached the end of the
1951 if (token->type == CPP_SEMICOLON && !nesting_depth)
1953 /* If the next token is a non-nested `}', then we have reached
1954 the end of the current block. */
1955 if (token->type == CPP_CLOSE_BRACE)
1957 /* If this is a non-nested `}', stop before consuming it.
1958 That way, when confronted with something like:
1962 we stop before consuming the closing `}', even though we
1963 have not yet reached a `;'. */
1964 if (nesting_depth == 0)
1966 /* If it is the closing `}' for a block that we have
1967 scanned, stop -- but only after consuming the token.
1973 we will stop after the body of the erroneously declared
1974 function, but before consuming the following `typedef'
1976 if (--nesting_depth == 0)
1978 cp_lexer_consume_token (parser->lexer);
1982 /* If it the next token is a `{', then we are entering a new
1983 block. Consume the entire block. */
1984 else if (token->type == CPP_OPEN_BRACE)
1986 /* Consume the token. */
1987 cp_lexer_consume_token (parser->lexer);
1991 /* This function is called at the end of a statement or declaration.
1992 If the next token is a semicolon, it is consumed; otherwise, error
1993 recovery is attempted. */
1996 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
1998 /* Look for the trailing `;'. */
1999 if (!cp_parser_require (parser, CPP_SEMICOLON, "`;'"))
2001 /* If there is additional (erroneous) input, skip to the end of
2003 cp_parser_skip_to_end_of_statement (parser);
2004 /* If the next token is now a `;', consume it. */
2005 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2006 cp_lexer_consume_token (parser->lexer);
2010 /* Skip tokens until we have consumed an entire block, or until we
2011 have consumed a non-nested `;'. */
2014 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2016 unsigned nesting_depth = 0;
2022 /* Peek at the next token. */
2023 token = cp_lexer_peek_token (parser->lexer);
2024 /* If we've run out of tokens, stop. */
2025 if (token->type == CPP_EOF)
2027 /* If the next token is a `;', we have reached the end of the
2029 if (token->type == CPP_SEMICOLON && !nesting_depth)
2031 /* Consume the `;'. */
2032 cp_lexer_consume_token (parser->lexer);
2035 /* Consume the token. */
2036 token = cp_lexer_consume_token (parser->lexer);
2037 /* If the next token is a non-nested `}', then we have reached
2038 the end of the current block. */
2039 if (token->type == CPP_CLOSE_BRACE
2040 && (nesting_depth == 0 || --nesting_depth == 0))
2042 /* If it the next token is a `{', then we are entering a new
2043 block. Consume the entire block. */
2044 if (token->type == CPP_OPEN_BRACE)
2049 /* Skip tokens until a non-nested closing curly brace is the next
2053 cp_parser_skip_to_closing_brace (cp_parser *parser)
2055 unsigned nesting_depth = 0;
2061 /* Peek at the next token. */
2062 token = cp_lexer_peek_token (parser->lexer);
2063 /* If we've run out of tokens, stop. */
2064 if (token->type == CPP_EOF)
2066 /* If the next token is a non-nested `}', then we have reached
2067 the end of the current block. */
2068 if (token->type == CPP_CLOSE_BRACE && nesting_depth-- == 0)
2070 /* If it the next token is a `{', then we are entering a new
2071 block. Consume the entire block. */
2072 else if (token->type == CPP_OPEN_BRACE)
2074 /* Consume the token. */
2075 cp_lexer_consume_token (parser->lexer);
2079 /* Create a new C++ parser. */
2082 cp_parser_new (void)
2087 /* cp_lexer_new_main is called before calling ggc_alloc because
2088 cp_lexer_new_main might load a PCH file. */
2089 lexer = cp_lexer_new_main ();
2091 parser = ggc_alloc_cleared (sizeof (cp_parser));
2092 parser->lexer = lexer;
2093 parser->context = cp_parser_context_new (NULL);
2095 /* For now, we always accept GNU extensions. */
2096 parser->allow_gnu_extensions_p = 1;
2098 /* The `>' token is a greater-than operator, not the end of a
2100 parser->greater_than_is_operator_p = true;
2102 parser->default_arg_ok_p = true;
2104 /* We are not parsing a constant-expression. */
2105 parser->constant_expression_p = false;
2106 parser->allow_non_constant_expression_p = false;
2107 parser->non_constant_expression_p = false;
2109 /* Local variable names are not forbidden. */
2110 parser->local_variables_forbidden_p = false;
2112 /* We are not processing an `extern "C"' declaration. */
2113 parser->in_unbraced_linkage_specification_p = false;
2115 /* We are not processing a declarator. */
2116 parser->in_declarator_p = false;
2118 /* The unparsed function queue is empty. */
2119 parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2121 /* There are no classes being defined. */
2122 parser->num_classes_being_defined = 0;
2124 /* No template parameters apply. */
2125 parser->num_template_parameter_lists = 0;
2130 /* Lexical conventions [gram.lex] */
2132 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
2136 cp_parser_identifier (cp_parser* parser)
2140 /* Look for the identifier. */
2141 token = cp_parser_require (parser, CPP_NAME, "identifier");
2142 /* Return the value. */
2143 return token ? token->value : error_mark_node;
2146 /* Basic concepts [gram.basic] */
2148 /* Parse a translation-unit.
2151 declaration-seq [opt]
2153 Returns TRUE if all went well. */
2156 cp_parser_translation_unit (cp_parser* parser)
2160 cp_parser_declaration_seq_opt (parser);
2162 /* If there are no tokens left then all went well. */
2163 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2166 /* Otherwise, issue an error message. */
2167 cp_parser_error (parser, "expected declaration");
2171 /* Consume the EOF token. */
2172 cp_parser_require (parser, CPP_EOF, "end-of-file");
2175 finish_translation_unit ();
2177 /* All went well. */
2181 /* Expressions [gram.expr] */
2183 /* Parse a primary-expression.
2194 ( compound-statement )
2195 __builtin_va_arg ( assignment-expression , type-id )
2200 Returns a representation of the expression.
2202 *IDK indicates what kind of id-expression (if any) was present.
2204 *QUALIFYING_CLASS is set to a non-NULL value if the id-expression can be
2205 used as the operand of a pointer-to-member. In that case,
2206 *QUALIFYING_CLASS gives the class that is used as the qualifying
2207 class in the pointer-to-member. */
2210 cp_parser_primary_expression (cp_parser *parser,
2212 tree *qualifying_class)
2216 /* Assume the primary expression is not an id-expression. */
2217 *idk = CP_ID_KIND_NONE;
2218 /* And that it cannot be used as pointer-to-member. */
2219 *qualifying_class = NULL_TREE;
2221 /* Peek at the next token. */
2222 token = cp_lexer_peek_token (parser->lexer);
2223 switch (token->type)
2236 token = cp_lexer_consume_token (parser->lexer);
2237 return token->value;
2239 case CPP_OPEN_PAREN:
2242 bool saved_greater_than_is_operator_p;
2244 /* Consume the `('. */
2245 cp_lexer_consume_token (parser->lexer);
2246 /* Within a parenthesized expression, a `>' token is always
2247 the greater-than operator. */
2248 saved_greater_than_is_operator_p
2249 = parser->greater_than_is_operator_p;
2250 parser->greater_than_is_operator_p = true;
2251 /* If we see `( { ' then we are looking at the beginning of
2252 a GNU statement-expression. */
2253 if (cp_parser_allow_gnu_extensions_p (parser)
2254 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
2256 /* Statement-expressions are not allowed by the standard. */
2258 pedwarn ("ISO C++ forbids braced-groups within expressions");
2260 /* And they're not allowed outside of a function-body; you
2261 cannot, for example, write:
2263 int i = ({ int j = 3; j + 1; });
2265 at class or namespace scope. */
2266 if (!at_function_scope_p ())
2267 error ("statement-expressions are allowed only inside functions");
2268 /* Start the statement-expression. */
2269 expr = begin_stmt_expr ();
2270 /* Parse the compound-statement. */
2271 cp_parser_compound_statement (parser, true);
2273 expr = finish_stmt_expr (expr, false);
2277 /* Parse the parenthesized expression. */
2278 expr = cp_parser_expression (parser);
2279 /* Let the front end know that this expression was
2280 enclosed in parentheses. This matters in case, for
2281 example, the expression is of the form `A::B', since
2282 `&A::B' might be a pointer-to-member, but `&(A::B)' is
2284 finish_parenthesized_expr (expr);
2286 /* The `>' token might be the end of a template-id or
2287 template-parameter-list now. */
2288 parser->greater_than_is_operator_p
2289 = saved_greater_than_is_operator_p;
2290 /* Consume the `)'. */
2291 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
2292 cp_parser_skip_to_end_of_statement (parser);
2298 switch (token->keyword)
2300 /* These two are the boolean literals. */
2302 cp_lexer_consume_token (parser->lexer);
2303 return boolean_true_node;
2305 cp_lexer_consume_token (parser->lexer);
2306 return boolean_false_node;
2308 /* The `__null' literal. */
2310 cp_lexer_consume_token (parser->lexer);
2313 /* Recognize the `this' keyword. */
2315 cp_lexer_consume_token (parser->lexer);
2316 if (parser->local_variables_forbidden_p)
2318 error ("`this' may not be used in this context");
2319 return error_mark_node;
2321 /* Pointers cannot appear in constant-expressions. */
2322 if (parser->constant_expression_p)
2324 if (!parser->allow_non_constant_expression_p)
2325 return cp_parser_non_constant_expression ("`this'");
2326 parser->non_constant_expression_p = true;
2328 return finish_this_expr ();
2330 /* The `operator' keyword can be the beginning of an
2335 case RID_FUNCTION_NAME:
2336 case RID_PRETTY_FUNCTION_NAME:
2337 case RID_C99_FUNCTION_NAME:
2338 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
2339 __func__ are the names of variables -- but they are
2340 treated specially. Therefore, they are handled here,
2341 rather than relying on the generic id-expression logic
2342 below. Grammatically, these names are id-expressions.
2344 Consume the token. */
2345 token = cp_lexer_consume_token (parser->lexer);
2346 /* Look up the name. */
2347 return finish_fname (token->value);
2354 /* The `__builtin_va_arg' construct is used to handle
2355 `va_arg'. Consume the `__builtin_va_arg' token. */
2356 cp_lexer_consume_token (parser->lexer);
2357 /* Look for the opening `('. */
2358 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
2359 /* Now, parse the assignment-expression. */
2360 expression = cp_parser_assignment_expression (parser);
2361 /* Look for the `,'. */
2362 cp_parser_require (parser, CPP_COMMA, "`,'");
2363 /* Parse the type-id. */
2364 type = cp_parser_type_id (parser);
2365 /* Look for the closing `)'. */
2366 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
2367 /* Using `va_arg' in a constant-expression is not
2369 if (parser->constant_expression_p)
2371 if (!parser->allow_non_constant_expression_p)
2372 return cp_parser_non_constant_expression ("`va_arg'");
2373 parser->non_constant_expression_p = true;
2375 return build_x_va_arg (expression, type);
2379 cp_parser_error (parser, "expected primary-expression");
2380 return error_mark_node;
2383 /* An id-expression can start with either an identifier, a
2384 `::' as the beginning of a qualified-id, or the "operator"
2388 case CPP_TEMPLATE_ID:
2389 case CPP_NESTED_NAME_SPECIFIER:
2393 const char *error_msg;
2396 /* Parse the id-expression. */
2398 = cp_parser_id_expression (parser,
2399 /*template_keyword_p=*/false,
2400 /*check_dependency_p=*/true,
2401 /*template_p=*/NULL);
2402 if (id_expression == error_mark_node)
2403 return error_mark_node;
2404 /* If we have a template-id, then no further lookup is
2405 required. If the template-id was for a template-class, we
2406 will sometimes have a TYPE_DECL at this point. */
2407 else if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
2408 || TREE_CODE (id_expression) == TYPE_DECL)
2409 decl = id_expression;
2410 /* Look up the name. */
2413 decl = cp_parser_lookup_name_simple (parser, id_expression);
2414 /* If name lookup gives us a SCOPE_REF, then the
2415 qualifying scope was dependent. Just propagate the
2417 if (TREE_CODE (decl) == SCOPE_REF)
2419 if (TYPE_P (TREE_OPERAND (decl, 0)))
2420 *qualifying_class = TREE_OPERAND (decl, 0);
2423 /* Check to see if DECL is a local variable in a context
2424 where that is forbidden. */
2425 if (parser->local_variables_forbidden_p
2426 && local_variable_p (decl))
2428 /* It might be that we only found DECL because we are
2429 trying to be generous with pre-ISO scoping rules.
2430 For example, consider:
2434 for (int i = 0; i < 10; ++i) {}
2435 extern void f(int j = i);
2438 Here, name look up will originally find the out
2439 of scope `i'. We need to issue a warning message,
2440 but then use the global `i'. */
2441 decl = check_for_out_of_scope_variable (decl);
2442 if (local_variable_p (decl))
2444 error ("local variable `%D' may not appear in this context",
2446 return error_mark_node;
2451 decl = finish_id_expression (id_expression, decl, parser->scope,
2452 idk, qualifying_class,
2453 parser->constant_expression_p,
2454 parser->allow_non_constant_expression_p,
2455 &parser->non_constant_expression_p,
2458 cp_parser_error (parser, error_msg);
2462 /* Anything else is an error. */
2464 cp_parser_error (parser, "expected primary-expression");
2465 return error_mark_node;
2469 /* Parse an id-expression.
2476 :: [opt] nested-name-specifier template [opt] unqualified-id
2478 :: operator-function-id
2481 Return a representation of the unqualified portion of the
2482 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
2483 a `::' or nested-name-specifier.
2485 Often, if the id-expression was a qualified-id, the caller will
2486 want to make a SCOPE_REF to represent the qualified-id. This
2487 function does not do this in order to avoid wastefully creating
2488 SCOPE_REFs when they are not required.
2490 If TEMPLATE_KEYWORD_P is true, then we have just seen the
2493 If CHECK_DEPENDENCY_P is false, then names are looked up inside
2494 uninstantiated templates.
2496 If *TEMPLATE_P is non-NULL, it is set to true iff the
2497 `template' keyword is used to explicitly indicate that the entity
2498 named is a template. */
2501 cp_parser_id_expression (cp_parser *parser,
2502 bool template_keyword_p,
2503 bool check_dependency_p,
2506 bool global_scope_p;
2507 bool nested_name_specifier_p;
2509 /* Assume the `template' keyword was not used. */
2511 *template_p = false;
2513 /* Look for the optional `::' operator. */
2515 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
2517 /* Look for the optional nested-name-specifier. */
2518 nested_name_specifier_p
2519 = (cp_parser_nested_name_specifier_opt (parser,
2520 /*typename_keyword_p=*/false,
2524 /* If there is a nested-name-specifier, then we are looking at
2525 the first qualified-id production. */
2526 if (nested_name_specifier_p)
2529 tree saved_object_scope;
2530 tree saved_qualifying_scope;
2531 tree unqualified_id;
2534 /* See if the next token is the `template' keyword. */
2536 template_p = &is_template;
2537 *template_p = cp_parser_optional_template_keyword (parser);
2538 /* Name lookup we do during the processing of the
2539 unqualified-id might obliterate SCOPE. */
2540 saved_scope = parser->scope;
2541 saved_object_scope = parser->object_scope;
2542 saved_qualifying_scope = parser->qualifying_scope;
2543 /* Process the final unqualified-id. */
2544 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
2545 check_dependency_p);
2546 /* Restore the SAVED_SCOPE for our caller. */
2547 parser->scope = saved_scope;
2548 parser->object_scope = saved_object_scope;
2549 parser->qualifying_scope = saved_qualifying_scope;
2551 return unqualified_id;
2553 /* Otherwise, if we are in global scope, then we are looking at one
2554 of the other qualified-id productions. */
2555 else if (global_scope_p)
2560 /* Peek at the next token. */
2561 token = cp_lexer_peek_token (parser->lexer);
2563 /* If it's an identifier, and the next token is not a "<", then
2564 we can avoid the template-id case. This is an optimization
2565 for this common case. */
2566 if (token->type == CPP_NAME
2567 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_LESS)
2568 return cp_parser_identifier (parser);
2570 cp_parser_parse_tentatively (parser);
2571 /* Try a template-id. */
2572 id = cp_parser_template_id (parser,
2573 /*template_keyword_p=*/false,
2574 /*check_dependency_p=*/true);
2575 /* If that worked, we're done. */
2576 if (cp_parser_parse_definitely (parser))
2579 /* Peek at the next token. (Changes in the token buffer may
2580 have invalidated the pointer obtained above.) */
2581 token = cp_lexer_peek_token (parser->lexer);
2583 switch (token->type)
2586 return cp_parser_identifier (parser);
2589 if (token->keyword == RID_OPERATOR)
2590 return cp_parser_operator_function_id (parser);
2594 cp_parser_error (parser, "expected id-expression");
2595 return error_mark_node;
2599 return cp_parser_unqualified_id (parser, template_keyword_p,
2600 /*check_dependency_p=*/true);
2603 /* Parse an unqualified-id.
2607 operator-function-id
2608 conversion-function-id
2612 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
2613 keyword, in a construct like `A::template ...'.
2615 Returns a representation of unqualified-id. For the `identifier'
2616 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
2617 production a BIT_NOT_EXPR is returned; the operand of the
2618 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
2619 other productions, see the documentation accompanying the
2620 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
2621 names are looked up in uninstantiated templates. */
2624 cp_parser_unqualified_id (cp_parser* parser,
2625 bool template_keyword_p,
2626 bool check_dependency_p)
2630 /* Peek at the next token. */
2631 token = cp_lexer_peek_token (parser->lexer);
2633 switch (token->type)
2639 /* We don't know yet whether or not this will be a
2641 cp_parser_parse_tentatively (parser);
2642 /* Try a template-id. */
2643 id = cp_parser_template_id (parser, template_keyword_p,
2644 check_dependency_p);
2645 /* If it worked, we're done. */
2646 if (cp_parser_parse_definitely (parser))
2648 /* Otherwise, it's an ordinary identifier. */
2649 return cp_parser_identifier (parser);
2652 case CPP_TEMPLATE_ID:
2653 return cp_parser_template_id (parser, template_keyword_p,
2654 check_dependency_p);
2659 tree qualifying_scope;
2663 /* Consume the `~' token. */
2664 cp_lexer_consume_token (parser->lexer);
2665 /* Parse the class-name. The standard, as written, seems to
2668 template <typename T> struct S { ~S (); };
2669 template <typename T> S<T>::~S() {}
2671 is invalid, since `~' must be followed by a class-name, but
2672 `S<T>' is dependent, and so not known to be a class.
2673 That's not right; we need to look in uninstantiated
2674 templates. A further complication arises from:
2676 template <typename T> void f(T t) {
2680 Here, it is not possible to look up `T' in the scope of `T'
2681 itself. We must look in both the current scope, and the
2682 scope of the containing complete expression.
2684 Yet another issue is:
2693 The standard does not seem to say that the `S' in `~S'
2694 should refer to the type `S' and not the data member
2697 /* DR 244 says that we look up the name after the "~" in the
2698 same scope as we looked up the qualifying name. That idea
2699 isn't fully worked out; it's more complicated than that. */
2700 scope = parser->scope;
2701 object_scope = parser->object_scope;
2702 qualifying_scope = parser->qualifying_scope;
2704 /* If the name is of the form "X::~X" it's OK. */
2705 if (scope && TYPE_P (scope)
2706 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
2707 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
2709 && (cp_lexer_peek_token (parser->lexer)->value
2710 == TYPE_IDENTIFIER (scope)))
2712 cp_lexer_consume_token (parser->lexer);
2713 return build_nt (BIT_NOT_EXPR, scope);
2716 /* If there was an explicit qualification (S::~T), first look
2717 in the scope given by the qualification (i.e., S). */
2720 cp_parser_parse_tentatively (parser);
2721 type_decl = cp_parser_class_name (parser,
2722 /*typename_keyword_p=*/false,
2723 /*template_keyword_p=*/false,
2725 /*check_dependency=*/false,
2726 /*class_head_p=*/false);
2727 if (cp_parser_parse_definitely (parser))
2728 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
2730 /* In "N::S::~S", look in "N" as well. */
2731 if (scope && qualifying_scope)
2733 cp_parser_parse_tentatively (parser);
2734 parser->scope = qualifying_scope;
2735 parser->object_scope = NULL_TREE;
2736 parser->qualifying_scope = NULL_TREE;
2738 = cp_parser_class_name (parser,
2739 /*typename_keyword_p=*/false,
2740 /*template_keyword_p=*/false,
2742 /*check_dependency=*/false,
2743 /*class_head_p=*/false);
2744 if (cp_parser_parse_definitely (parser))
2745 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
2747 /* In "p->S::~T", look in the scope given by "*p" as well. */
2748 else if (object_scope)
2750 cp_parser_parse_tentatively (parser);
2751 parser->scope = object_scope;
2752 parser->object_scope = NULL_TREE;
2753 parser->qualifying_scope = NULL_TREE;
2755 = cp_parser_class_name (parser,
2756 /*typename_keyword_p=*/false,
2757 /*template_keyword_p=*/false,
2759 /*check_dependency=*/false,
2760 /*class_head_p=*/false);
2761 if (cp_parser_parse_definitely (parser))
2762 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
2764 /* Look in the surrounding context. */
2765 parser->scope = NULL_TREE;
2766 parser->object_scope = NULL_TREE;
2767 parser->qualifying_scope = NULL_TREE;
2769 = cp_parser_class_name (parser,
2770 /*typename_keyword_p=*/false,
2771 /*template_keyword_p=*/false,
2773 /*check_dependency=*/false,
2774 /*class_head_p=*/false);
2775 /* If an error occurred, assume that the name of the
2776 destructor is the same as the name of the qualifying
2777 class. That allows us to keep parsing after running
2778 into ill-formed destructor names. */
2779 if (type_decl == error_mark_node && scope && TYPE_P (scope))
2780 return build_nt (BIT_NOT_EXPR, scope);
2781 else if (type_decl == error_mark_node)
2782 return error_mark_node;
2784 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
2788 if (token->keyword == RID_OPERATOR)
2792 /* This could be a template-id, so we try that first. */
2793 cp_parser_parse_tentatively (parser);
2794 /* Try a template-id. */
2795 id = cp_parser_template_id (parser, template_keyword_p,
2796 /*check_dependency_p=*/true);
2797 /* If that worked, we're done. */
2798 if (cp_parser_parse_definitely (parser))
2800 /* We still don't know whether we're looking at an
2801 operator-function-id or a conversion-function-id. */
2802 cp_parser_parse_tentatively (parser);
2803 /* Try an operator-function-id. */
2804 id = cp_parser_operator_function_id (parser);
2805 /* If that didn't work, try a conversion-function-id. */
2806 if (!cp_parser_parse_definitely (parser))
2807 id = cp_parser_conversion_function_id (parser);
2814 cp_parser_error (parser, "expected unqualified-id");
2815 return error_mark_node;
2819 /* Parse an (optional) nested-name-specifier.
2821 nested-name-specifier:
2822 class-or-namespace-name :: nested-name-specifier [opt]
2823 class-or-namespace-name :: template nested-name-specifier [opt]
2825 PARSER->SCOPE should be set appropriately before this function is
2826 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
2827 effect. TYPE_P is TRUE if we non-type bindings should be ignored
2830 Sets PARSER->SCOPE to the class (TYPE) or namespace
2831 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
2832 it unchanged if there is no nested-name-specifier. Returns the new
2833 scope iff there is a nested-name-specifier, or NULL_TREE otherwise. */
2836 cp_parser_nested_name_specifier_opt (cp_parser *parser,
2837 bool typename_keyword_p,
2838 bool check_dependency_p,
2841 bool success = false;
2842 tree access_check = NULL_TREE;
2846 /* If the next token corresponds to a nested name specifier, there
2847 is no need to reparse it. However, if CHECK_DEPENDENCY_P is
2848 false, it may have been true before, in which case something
2849 like `A<X>::B<Y>::C' may have resulted in a nested-name-specifier
2850 of `A<X>::', where it should now be `A<X>::B<Y>::'. So, when
2851 CHECK_DEPENDENCY_P is false, we have to fall through into the
2853 if (check_dependency_p
2854 && cp_lexer_next_token_is (parser->lexer, CPP_NESTED_NAME_SPECIFIER))
2856 cp_parser_pre_parsed_nested_name_specifier (parser);
2857 return parser->scope;
2860 /* Remember where the nested-name-specifier starts. */
2861 if (cp_parser_parsing_tentatively (parser)
2862 && !cp_parser_committed_to_tentative_parse (parser))
2864 token = cp_lexer_peek_token (parser->lexer);
2865 start = cp_lexer_token_difference (parser->lexer,
2866 parser->lexer->first_token,
2872 push_deferring_access_checks (dk_deferred);
2878 tree saved_qualifying_scope;
2879 bool template_keyword_p;
2881 /* Spot cases that cannot be the beginning of a
2882 nested-name-specifier. */
2883 token = cp_lexer_peek_token (parser->lexer);
2885 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
2886 the already parsed nested-name-specifier. */
2887 if (token->type == CPP_NESTED_NAME_SPECIFIER)
2889 /* Grab the nested-name-specifier and continue the loop. */
2890 cp_parser_pre_parsed_nested_name_specifier (parser);
2895 /* Spot cases that cannot be the beginning of a
2896 nested-name-specifier. On the second and subsequent times
2897 through the loop, we look for the `template' keyword. */
2898 if (success && token->keyword == RID_TEMPLATE)
2900 /* A template-id can start a nested-name-specifier. */
2901 else if (token->type == CPP_TEMPLATE_ID)
2905 /* If the next token is not an identifier, then it is
2906 definitely not a class-or-namespace-name. */
2907 if (token->type != CPP_NAME)
2909 /* If the following token is neither a `<' (to begin a
2910 template-id), nor a `::', then we are not looking at a
2911 nested-name-specifier. */
2912 token = cp_lexer_peek_nth_token (parser->lexer, 2);
2913 if (token->type != CPP_LESS && token->type != CPP_SCOPE)
2917 /* The nested-name-specifier is optional, so we parse
2919 cp_parser_parse_tentatively (parser);
2921 /* Look for the optional `template' keyword, if this isn't the
2922 first time through the loop. */
2924 template_keyword_p = cp_parser_optional_template_keyword (parser);
2926 template_keyword_p = false;
2928 /* Save the old scope since the name lookup we are about to do
2929 might destroy it. */
2930 old_scope = parser->scope;
2931 saved_qualifying_scope = parser->qualifying_scope;
2932 /* Parse the qualifying entity. */
2934 = cp_parser_class_or_namespace_name (parser,
2939 /* Look for the `::' token. */
2940 cp_parser_require (parser, CPP_SCOPE, "`::'");
2942 /* If we found what we wanted, we keep going; otherwise, we're
2944 if (!cp_parser_parse_definitely (parser))
2946 bool error_p = false;
2948 /* Restore the OLD_SCOPE since it was valid before the
2949 failed attempt at finding the last
2950 class-or-namespace-name. */
2951 parser->scope = old_scope;
2952 parser->qualifying_scope = saved_qualifying_scope;
2953 /* If the next token is an identifier, and the one after
2954 that is a `::', then any valid interpretation would have
2955 found a class-or-namespace-name. */
2956 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
2957 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
2959 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
2962 token = cp_lexer_consume_token (parser->lexer);
2967 decl = cp_parser_lookup_name_simple (parser, token->value);
2968 if (TREE_CODE (decl) == TEMPLATE_DECL)
2969 error ("`%D' used without template parameters",
2971 else if (parser->scope)
2973 if (TYPE_P (parser->scope))
2974 error ("`%T::%D' is not a class-name or "
2976 parser->scope, token->value);
2978 error ("`%D::%D' is not a class-name or "
2980 parser->scope, token->value);
2983 error ("`%D' is not a class-name or namespace-name",
2985 parser->scope = NULL_TREE;
2987 /* Treat this as a successful nested-name-specifier
2992 If the name found is not a class-name (clause
2993 _class_) or namespace-name (_namespace.def_), the
2994 program is ill-formed. */
2997 cp_lexer_consume_token (parser->lexer);
3002 /* We've found one valid nested-name-specifier. */
3004 /* Make sure we look in the right scope the next time through
3006 parser->scope = (TREE_CODE (new_scope) == TYPE_DECL
3007 ? TREE_TYPE (new_scope)
3009 /* If it is a class scope, try to complete it; we are about to
3010 be looking up names inside the class. */
3011 if (TYPE_P (parser->scope)
3012 /* Since checking types for dependency can be expensive,
3013 avoid doing it if the type is already complete. */
3014 && !COMPLETE_TYPE_P (parser->scope)
3015 /* Do not try to complete dependent types. */
3016 && !dependent_type_p (parser->scope))
3017 complete_type (parser->scope);
3020 /* Retrieve any deferred checks. Do not pop this access checks yet
3021 so the memory will not be reclaimed during token replacing below. */
3022 access_check = get_deferred_access_checks ();
3024 /* If parsing tentatively, replace the sequence of tokens that makes
3025 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
3026 token. That way, should we re-parse the token stream, we will
3027 not have to repeat the effort required to do the parse, nor will
3028 we issue duplicate error messages. */
3029 if (success && start >= 0)
3031 /* Find the token that corresponds to the start of the
3033 token = cp_lexer_advance_token (parser->lexer,
3034 parser->lexer->first_token,
3037 /* Reset the contents of the START token. */
3038 token->type = CPP_NESTED_NAME_SPECIFIER;
3039 token->value = build_tree_list (access_check, parser->scope);
3040 TREE_TYPE (token->value) = parser->qualifying_scope;
3041 token->keyword = RID_MAX;
3042 /* Purge all subsequent tokens. */
3043 cp_lexer_purge_tokens_after (parser->lexer, token);
3046 pop_deferring_access_checks ();
3047 return success ? parser->scope : NULL_TREE;
3050 /* Parse a nested-name-specifier. See
3051 cp_parser_nested_name_specifier_opt for details. This function
3052 behaves identically, except that it will an issue an error if no
3053 nested-name-specifier is present, and it will return
3054 ERROR_MARK_NODE, rather than NULL_TREE, if no nested-name-specifier
3058 cp_parser_nested_name_specifier (cp_parser *parser,
3059 bool typename_keyword_p,
3060 bool check_dependency_p,
3065 /* Look for the nested-name-specifier. */
3066 scope = cp_parser_nested_name_specifier_opt (parser,
3070 /* If it was not present, issue an error message. */
3073 cp_parser_error (parser, "expected nested-name-specifier");
3074 parser->scope = NULL_TREE;
3075 return error_mark_node;
3081 /* Parse a class-or-namespace-name.
3083 class-or-namespace-name:
3087 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
3088 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
3089 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
3090 TYPE_P is TRUE iff the next name should be taken as a class-name,
3091 even the same name is declared to be another entity in the same
3094 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
3095 specified by the class-or-namespace-name. If neither is found the
3096 ERROR_MARK_NODE is returned. */
3099 cp_parser_class_or_namespace_name (cp_parser *parser,
3100 bool typename_keyword_p,
3101 bool template_keyword_p,
3102 bool check_dependency_p,
3106 tree saved_qualifying_scope;
3107 tree saved_object_scope;
3111 /* Before we try to parse the class-name, we must save away the
3112 current PARSER->SCOPE since cp_parser_class_name will destroy
3114 saved_scope = parser->scope;
3115 saved_qualifying_scope = parser->qualifying_scope;
3116 saved_object_scope = parser->object_scope;
3117 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
3118 there is no need to look for a namespace-name. */
3119 only_class_p = template_keyword_p || (saved_scope && TYPE_P (saved_scope));
3121 cp_parser_parse_tentatively (parser);
3122 scope = cp_parser_class_name (parser,
3127 /*class_head_p=*/false);
3128 /* If that didn't work, try for a namespace-name. */
3129 if (!only_class_p && !cp_parser_parse_definitely (parser))
3131 /* Restore the saved scope. */
3132 parser->scope = saved_scope;
3133 parser->qualifying_scope = saved_qualifying_scope;
3134 parser->object_scope = saved_object_scope;
3135 /* If we are not looking at an identifier followed by the scope
3136 resolution operator, then this is not part of a
3137 nested-name-specifier. (Note that this function is only used
3138 to parse the components of a nested-name-specifier.) */
3139 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
3140 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
3141 return error_mark_node;
3142 scope = cp_parser_namespace_name (parser);
3148 /* Parse a postfix-expression.
3152 postfix-expression [ expression ]
3153 postfix-expression ( expression-list [opt] )
3154 simple-type-specifier ( expression-list [opt] )
3155 typename :: [opt] nested-name-specifier identifier
3156 ( expression-list [opt] )
3157 typename :: [opt] nested-name-specifier template [opt] template-id
3158 ( expression-list [opt] )
3159 postfix-expression . template [opt] id-expression
3160 postfix-expression -> template [opt] id-expression
3161 postfix-expression . pseudo-destructor-name
3162 postfix-expression -> pseudo-destructor-name
3163 postfix-expression ++
3164 postfix-expression --
3165 dynamic_cast < type-id > ( expression )
3166 static_cast < type-id > ( expression )
3167 reinterpret_cast < type-id > ( expression )
3168 const_cast < type-id > ( expression )
3169 typeid ( expression )
3175 ( type-id ) { initializer-list , [opt] }
3177 This extension is a GNU version of the C99 compound-literal
3178 construct. (The C99 grammar uses `type-name' instead of `type-id',
3179 but they are essentially the same concept.)
3181 If ADDRESS_P is true, the postfix expression is the operand of the
3184 Returns a representation of the expression. */
3187 cp_parser_postfix_expression (cp_parser *parser, bool address_p)
3191 cp_id_kind idk = CP_ID_KIND_NONE;
3192 tree postfix_expression = NULL_TREE;
3193 /* Non-NULL only if the current postfix-expression can be used to
3194 form a pointer-to-member. In that case, QUALIFYING_CLASS is the
3195 class used to qualify the member. */
3196 tree qualifying_class = NULL_TREE;
3198 /* Peek at the next token. */
3199 token = cp_lexer_peek_token (parser->lexer);
3200 /* Some of the productions are determined by keywords. */
3201 keyword = token->keyword;
3211 const char *saved_message;
3213 /* All of these can be handled in the same way from the point
3214 of view of parsing. Begin by consuming the token
3215 identifying the cast. */
3216 cp_lexer_consume_token (parser->lexer);
3218 /* New types cannot be defined in the cast. */
3219 saved_message = parser->type_definition_forbidden_message;
3220 parser->type_definition_forbidden_message
3221 = "types may not be defined in casts";
3223 /* Look for the opening `<'. */
3224 cp_parser_require (parser, CPP_LESS, "`<'");
3225 /* Parse the type to which we are casting. */
3226 type = cp_parser_type_id (parser);
3227 /* Look for the closing `>'. */
3228 cp_parser_require (parser, CPP_GREATER, "`>'");
3229 /* Restore the old message. */
3230 parser->type_definition_forbidden_message = saved_message;
3232 /* And the expression which is being cast. */
3233 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3234 expression = cp_parser_expression (parser);
3235 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3237 /* Only type conversions to integral or enumeration types
3238 can be used in constant-expressions. */
3239 if (parser->constant_expression_p
3240 && !dependent_type_p (type)
3241 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type))
3243 if (!parser->allow_non_constant_expression_p)
3244 return (cp_parser_non_constant_expression
3245 ("a cast to a type other than an integral or "
3246 "enumeration type"));
3247 parser->non_constant_expression_p = true;
3254 = build_dynamic_cast (type, expression);
3258 = build_static_cast (type, expression);
3262 = build_reinterpret_cast (type, expression);
3266 = build_const_cast (type, expression);
3277 const char *saved_message;
3279 /* Consume the `typeid' token. */
3280 cp_lexer_consume_token (parser->lexer);
3281 /* Look for the `(' token. */
3282 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3283 /* Types cannot be defined in a `typeid' expression. */
3284 saved_message = parser->type_definition_forbidden_message;
3285 parser->type_definition_forbidden_message
3286 = "types may not be defined in a `typeid\' expression";
3287 /* We can't be sure yet whether we're looking at a type-id or an
3289 cp_parser_parse_tentatively (parser);
3290 /* Try a type-id first. */
3291 type = cp_parser_type_id (parser);
3292 /* Look for the `)' token. Otherwise, we can't be sure that
3293 we're not looking at an expression: consider `typeid (int
3294 (3))', for example. */
3295 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3296 /* If all went well, simply lookup the type-id. */
3297 if (cp_parser_parse_definitely (parser))
3298 postfix_expression = get_typeid (type);
3299 /* Otherwise, fall back to the expression variant. */
3304 /* Look for an expression. */
3305 expression = cp_parser_expression (parser);
3306 /* Compute its typeid. */
3307 postfix_expression = build_typeid (expression);
3308 /* Look for the `)' token. */
3309 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3312 /* Restore the saved message. */
3313 parser->type_definition_forbidden_message = saved_message;
3319 bool template_p = false;
3323 /* Consume the `typename' token. */
3324 cp_lexer_consume_token (parser->lexer);
3325 /* Look for the optional `::' operator. */
3326 cp_parser_global_scope_opt (parser,
3327 /*current_scope_valid_p=*/false);
3328 /* Look for the nested-name-specifier. */
3329 cp_parser_nested_name_specifier (parser,
3330 /*typename_keyword_p=*/true,
3331 /*check_dependency_p=*/true,
3333 /* Look for the optional `template' keyword. */
3334 template_p = cp_parser_optional_template_keyword (parser);
3335 /* We don't know whether we're looking at a template-id or an
3337 cp_parser_parse_tentatively (parser);
3338 /* Try a template-id. */
3339 id = cp_parser_template_id (parser, template_p,
3340 /*check_dependency_p=*/true);
3341 /* If that didn't work, try an identifier. */
3342 if (!cp_parser_parse_definitely (parser))
3343 id = cp_parser_identifier (parser);
3344 /* Create a TYPENAME_TYPE to represent the type to which the
3345 functional cast is being performed. */
3346 type = make_typename_type (parser->scope, id,
3349 postfix_expression = cp_parser_functional_cast (parser, type);
3357 /* If the next thing is a simple-type-specifier, we may be
3358 looking at a functional cast. We could also be looking at
3359 an id-expression. So, we try the functional cast, and if
3360 that doesn't work we fall back to the primary-expression. */
3361 cp_parser_parse_tentatively (parser);
3362 /* Look for the simple-type-specifier. */
3363 type = cp_parser_simple_type_specifier (parser,
3364 CP_PARSER_FLAGS_NONE,
3365 /*identifier_p=*/false);
3366 /* Parse the cast itself. */
3367 if (!cp_parser_error_occurred (parser))
3369 = cp_parser_functional_cast (parser, type);
3370 /* If that worked, we're done. */
3371 if (cp_parser_parse_definitely (parser))
3374 /* If the functional-cast didn't work out, try a
3375 compound-literal. */
3376 if (cp_parser_allow_gnu_extensions_p (parser)
3377 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
3379 tree initializer_list = NULL_TREE;
3381 cp_parser_parse_tentatively (parser);
3382 /* Consume the `('. */
3383 cp_lexer_consume_token (parser->lexer);
3384 /* Parse the type. */
3385 type = cp_parser_type_id (parser);
3386 /* Look for the `)'. */
3387 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3388 /* Look for the `{'. */
3389 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
3390 /* If things aren't going well, there's no need to
3392 if (!cp_parser_error_occurred (parser))
3394 bool non_constant_p;
3395 /* Parse the initializer-list. */
3397 = cp_parser_initializer_list (parser, &non_constant_p);
3398 /* Allow a trailing `,'. */
3399 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
3400 cp_lexer_consume_token (parser->lexer);
3401 /* Look for the final `}'. */
3402 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
3404 /* If that worked, we're definitely looking at a
3405 compound-literal expression. */
3406 if (cp_parser_parse_definitely (parser))
3408 /* Warn the user that a compound literal is not
3409 allowed in standard C++. */
3411 pedwarn ("ISO C++ forbids compound-literals");
3412 /* Form the representation of the compound-literal. */
3414 = finish_compound_literal (type, initializer_list);
3419 /* It must be a primary-expression. */
3420 postfix_expression = cp_parser_primary_expression (parser,
3427 /* If we were avoiding committing to the processing of a
3428 qualified-id until we knew whether or not we had a
3429 pointer-to-member, we now know. */
3430 if (qualifying_class)
3434 /* Peek at the next token. */
3435 token = cp_lexer_peek_token (parser->lexer);
3436 done = (token->type != CPP_OPEN_SQUARE
3437 && token->type != CPP_OPEN_PAREN
3438 && token->type != CPP_DOT
3439 && token->type != CPP_DEREF
3440 && token->type != CPP_PLUS_PLUS
3441 && token->type != CPP_MINUS_MINUS);
3443 postfix_expression = finish_qualified_id_expr (qualifying_class,
3448 return postfix_expression;
3451 /* Keep looping until the postfix-expression is complete. */
3454 if (idk == CP_ID_KIND_UNQUALIFIED
3455 && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
3456 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
3457 /* It is not a Koenig lookup function call. */
3459 = unqualified_name_lookup_error (postfix_expression);
3461 /* Peek at the next token. */
3462 token = cp_lexer_peek_token (parser->lexer);
3464 switch (token->type)
3466 case CPP_OPEN_SQUARE:
3467 /* postfix-expression [ expression ] */
3471 /* Consume the `[' token. */
3472 cp_lexer_consume_token (parser->lexer);
3473 /* Parse the index expression. */
3474 index = cp_parser_expression (parser);
3475 /* Look for the closing `]'. */
3476 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
3478 /* Build the ARRAY_REF. */
3480 = grok_array_decl (postfix_expression, index);
3481 idk = CP_ID_KIND_NONE;
3485 case CPP_OPEN_PAREN:
3486 /* postfix-expression ( expression-list [opt] ) */
3488 tree args = (cp_parser_parenthesized_expression_list
3489 (parser, false, /*non_constant_p=*/NULL));
3491 if (args == error_mark_node)
3493 postfix_expression = error_mark_node;
3497 /* Function calls are not permitted in
3498 constant-expressions. */
3499 if (parser->constant_expression_p)
3501 if (!parser->allow_non_constant_expression_p)
3502 return cp_parser_non_constant_expression ("a function call");
3503 parser->non_constant_expression_p = true;
3506 if (idk == CP_ID_KIND_UNQUALIFIED)
3509 && (is_overloaded_fn (postfix_expression)
3510 || DECL_P (postfix_expression)
3511 || TREE_CODE (postfix_expression) == IDENTIFIER_NODE))
3513 = perform_koenig_lookup (postfix_expression, args);
3514 else if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
3516 = unqualified_fn_lookup_error (postfix_expression);
3519 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
3521 tree instance = TREE_OPERAND (postfix_expression, 0);
3522 tree fn = TREE_OPERAND (postfix_expression, 1);
3524 if (processing_template_decl
3525 && (type_dependent_expression_p (instance)
3526 || (!BASELINK_P (fn)
3527 && TREE_CODE (fn) != FIELD_DECL)
3528 || type_dependent_expression_p (fn)
3529 || any_type_dependent_arguments_p (args)))
3532 = build_min_nt (CALL_EXPR, postfix_expression, args);
3537 = (build_new_method_call
3538 (instance, fn, args, NULL_TREE,
3539 (idk == CP_ID_KIND_QUALIFIED
3540 ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL)));
3542 else if (TREE_CODE (postfix_expression) == OFFSET_REF
3543 || TREE_CODE (postfix_expression) == MEMBER_REF
3544 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
3545 postfix_expression = (build_offset_ref_call_from_tree
3546 (postfix_expression, args));
3547 else if (idk == CP_ID_KIND_QUALIFIED)
3548 /* A call to a static class member, or a namespace-scope
3551 = finish_call_expr (postfix_expression, args,
3552 /*disallow_virtual=*/true);
3554 /* All other function calls. */
3556 = finish_call_expr (postfix_expression, args,
3557 /*disallow_virtual=*/false);
3559 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
3560 idk = CP_ID_KIND_NONE;
3566 /* postfix-expression . template [opt] id-expression
3567 postfix-expression . pseudo-destructor-name
3568 postfix-expression -> template [opt] id-expression
3569 postfix-expression -> pseudo-destructor-name */
3574 tree scope = NULL_TREE;
3576 /* If this is a `->' operator, dereference the pointer. */
3577 if (token->type == CPP_DEREF)
3578 postfix_expression = build_x_arrow (postfix_expression);
3579 /* Check to see whether or not the expression is
3581 dependent_p = type_dependent_expression_p (postfix_expression);
3582 /* The identifier following the `->' or `.' is not
3584 parser->scope = NULL_TREE;
3585 parser->qualifying_scope = NULL_TREE;
3586 parser->object_scope = NULL_TREE;
3587 idk = CP_ID_KIND_NONE;
3588 /* Enter the scope corresponding to the type of the object
3589 given by the POSTFIX_EXPRESSION. */
3591 && TREE_TYPE (postfix_expression) != NULL_TREE)
3593 scope = TREE_TYPE (postfix_expression);
3594 /* According to the standard, no expression should
3595 ever have reference type. Unfortunately, we do not
3596 currently match the standard in this respect in
3597 that our internal representation of an expression
3598 may have reference type even when the standard says
3599 it does not. Therefore, we have to manually obtain
3600 the underlying type here. */
3601 scope = non_reference (scope);
3602 /* The type of the POSTFIX_EXPRESSION must be
3604 scope = complete_type_or_else (scope, NULL_TREE);
3605 /* Let the name lookup machinery know that we are
3606 processing a class member access expression. */
3607 parser->context->object_type = scope;
3608 /* If something went wrong, we want to be able to
3609 discern that case, as opposed to the case where
3610 there was no SCOPE due to the type of expression
3613 scope = error_mark_node;
3616 /* Consume the `.' or `->' operator. */
3617 cp_lexer_consume_token (parser->lexer);
3618 /* If the SCOPE is not a scalar type, we are looking at an
3619 ordinary class member access expression, rather than a
3620 pseudo-destructor-name. */
3621 if (!scope || !SCALAR_TYPE_P (scope))
3623 template_p = cp_parser_optional_template_keyword (parser);
3624 /* Parse the id-expression. */
3625 name = cp_parser_id_expression (parser,
3627 /*check_dependency_p=*/true,
3628 /*template_p=*/NULL);
3629 /* In general, build a SCOPE_REF if the member name is
3630 qualified. However, if the name was not dependent
3631 and has already been resolved; there is no need to
3632 build the SCOPE_REF. For example;
3634 struct X { void f(); };
3635 template <typename T> void f(T* t) { t->X::f(); }
3637 Even though "t" is dependent, "X::f" is not and has
3638 been resolved to a BASELINK; there is no need to
3639 include scope information. */
3641 /* But we do need to remember that there was an explicit
3642 scope for virtual function calls. */
3644 idk = CP_ID_KIND_QUALIFIED;
3646 if (name != error_mark_node
3647 && !BASELINK_P (name)
3650 name = build_nt (SCOPE_REF, parser->scope, name);
3651 parser->scope = NULL_TREE;
3652 parser->qualifying_scope = NULL_TREE;
3653 parser->object_scope = NULL_TREE;
3656 = finish_class_member_access_expr (postfix_expression, name);
3658 /* Otherwise, try the pseudo-destructor-name production. */
3664 /* Parse the pseudo-destructor-name. */
3665 cp_parser_pseudo_destructor_name (parser, &s, &type);
3666 /* Form the call. */
3668 = finish_pseudo_destructor_expr (postfix_expression,
3669 s, TREE_TYPE (type));
3672 /* We no longer need to look up names in the scope of the
3673 object on the left-hand side of the `.' or `->'
3675 parser->context->object_type = NULL_TREE;
3680 /* postfix-expression ++ */
3681 /* Consume the `++' token. */
3682 cp_lexer_consume_token (parser->lexer);
3683 /* Increments may not appear in constant-expressions. */
3684 if (parser->constant_expression_p)
3686 if (!parser->allow_non_constant_expression_p)
3687 return cp_parser_non_constant_expression ("an increment");
3688 parser->non_constant_expression_p = true;
3690 /* Generate a representation for the complete expression. */
3692 = finish_increment_expr (postfix_expression,
3693 POSTINCREMENT_EXPR);
3694 idk = CP_ID_KIND_NONE;
3697 case CPP_MINUS_MINUS:
3698 /* postfix-expression -- */
3699 /* Consume the `--' token. */
3700 cp_lexer_consume_token (parser->lexer);
3701 /* Decrements may not appear in constant-expressions. */
3702 if (parser->constant_expression_p)
3704 if (!parser->allow_non_constant_expression_p)
3705 return cp_parser_non_constant_expression ("a decrement");
3706 parser->non_constant_expression_p = true;
3708 /* Generate a representation for the complete expression. */
3710 = finish_increment_expr (postfix_expression,
3711 POSTDECREMENT_EXPR);
3712 idk = CP_ID_KIND_NONE;
3716 return postfix_expression;
3720 /* We should never get here. */
3722 return error_mark_node;
3725 /* Parse a parenthesized expression-list.
3728 assignment-expression
3729 expression-list, assignment-expression
3734 identifier, expression-list
3736 Returns a TREE_LIST. The TREE_VALUE of each node is a
3737 representation of an assignment-expression. Note that a TREE_LIST
3738 is returned even if there is only a single expression in the list.
3739 error_mark_node is returned if the ( and or ) are
3740 missing. NULL_TREE is returned on no expressions. The parentheses
3741 are eaten. IS_ATTRIBUTE_LIST is true if this is really an attribute
3742 list being parsed. If NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P
3743 indicates whether or not all of the expressions in the list were
3747 cp_parser_parenthesized_expression_list (cp_parser* parser,
3748 bool is_attribute_list,
3749 bool *non_constant_p)
3751 tree expression_list = NULL_TREE;
3752 tree identifier = NULL_TREE;
3754 /* Assume all the expressions will be constant. */
3756 *non_constant_p = false;
3758 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
3759 return error_mark_node;
3761 /* Consume expressions until there are no more. */
3762 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
3767 /* At the beginning of attribute lists, check to see if the
3768 next token is an identifier. */
3769 if (is_attribute_list
3770 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
3774 /* Consume the identifier. */
3775 token = cp_lexer_consume_token (parser->lexer);
3776 /* Save the identifier. */
3777 identifier = token->value;
3781 /* Parse the next assignment-expression. */
3784 bool expr_non_constant_p;
3785 expr = (cp_parser_constant_expression
3786 (parser, /*allow_non_constant_p=*/true,
3787 &expr_non_constant_p));
3788 if (expr_non_constant_p)
3789 *non_constant_p = true;
3792 expr = cp_parser_assignment_expression (parser);
3794 /* Add it to the list. We add error_mark_node
3795 expressions to the list, so that we can still tell if
3796 the correct form for a parenthesized expression-list
3797 is found. That gives better errors. */
3798 expression_list = tree_cons (NULL_TREE, expr, expression_list);
3800 if (expr == error_mark_node)
3804 /* After the first item, attribute lists look the same as
3805 expression lists. */
3806 is_attribute_list = false;
3809 /* If the next token isn't a `,', then we are done. */
3810 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
3813 /* Otherwise, consume the `,' and keep going. */
3814 cp_lexer_consume_token (parser->lexer);
3817 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
3822 /* We try and resync to an unnested comma, as that will give the
3823 user better diagnostics. */
3824 ending = cp_parser_skip_to_closing_parenthesis (parser, true, true);
3828 return error_mark_node;
3831 /* We built up the list in reverse order so we must reverse it now. */
3832 expression_list = nreverse (expression_list);
3834 expression_list = tree_cons (NULL_TREE, identifier, expression_list);
3836 return expression_list;
3839 /* Parse a pseudo-destructor-name.
3841 pseudo-destructor-name:
3842 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
3843 :: [opt] nested-name-specifier template template-id :: ~ type-name
3844 :: [opt] nested-name-specifier [opt] ~ type-name
3846 If either of the first two productions is used, sets *SCOPE to the
3847 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
3848 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
3849 or ERROR_MARK_NODE if no type-name is present. */
3852 cp_parser_pseudo_destructor_name (cp_parser* parser,
3856 bool nested_name_specifier_p;
3858 /* Look for the optional `::' operator. */
3859 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
3860 /* Look for the optional nested-name-specifier. */
3861 nested_name_specifier_p
3862 = (cp_parser_nested_name_specifier_opt (parser,
3863 /*typename_keyword_p=*/false,
3864 /*check_dependency_p=*/true,
3867 /* Now, if we saw a nested-name-specifier, we might be doing the
3868 second production. */
3869 if (nested_name_specifier_p
3870 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
3872 /* Consume the `template' keyword. */
3873 cp_lexer_consume_token (parser->lexer);
3874 /* Parse the template-id. */
3875 cp_parser_template_id (parser,
3876 /*template_keyword_p=*/true,
3877 /*check_dependency_p=*/false);
3878 /* Look for the `::' token. */
3879 cp_parser_require (parser, CPP_SCOPE, "`::'");
3881 /* If the next token is not a `~', then there might be some
3882 additional qualification. */
3883 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
3885 /* Look for the type-name. */
3886 *scope = TREE_TYPE (cp_parser_type_name (parser));
3887 /* Look for the `::' token. */
3888 cp_parser_require (parser, CPP_SCOPE, "`::'");
3893 /* Look for the `~'. */
3894 cp_parser_require (parser, CPP_COMPL, "`~'");
3895 /* Look for the type-name again. We are not responsible for
3896 checking that it matches the first type-name. */
3897 *type = cp_parser_type_name (parser);
3900 /* Parse a unary-expression.
3906 unary-operator cast-expression
3907 sizeof unary-expression
3915 __extension__ cast-expression
3916 __alignof__ unary-expression
3917 __alignof__ ( type-id )
3918 __real__ cast-expression
3919 __imag__ cast-expression
3922 ADDRESS_P is true iff the unary-expression is appearing as the
3923 operand of the `&' operator.
3925 Returns a representation of the expression. */
3928 cp_parser_unary_expression (cp_parser *parser, bool address_p)
3931 enum tree_code unary_operator;
3933 /* Peek at the next token. */
3934 token = cp_lexer_peek_token (parser->lexer);
3935 /* Some keywords give away the kind of expression. */
3936 if (token->type == CPP_KEYWORD)
3938 enum rid keyword = token->keyword;
3944 /* Consume the `alignof' token. */
3945 cp_lexer_consume_token (parser->lexer);
3946 /* Parse the operand. */
3947 return finish_alignof (cp_parser_sizeof_operand
3955 /* Consume the `sizeof' token. */
3956 cp_lexer_consume_token (parser->lexer);
3957 /* Parse the operand. */
3958 operand = cp_parser_sizeof_operand (parser, keyword);
3960 /* If the type of the operand cannot be determined build a
3962 if (TYPE_P (operand)
3963 ? dependent_type_p (operand)
3964 : type_dependent_expression_p (operand))
3965 return build_min (SIZEOF_EXPR, size_type_node, operand);
3966 /* Otherwise, compute the constant value. */
3968 return finish_sizeof (operand);
3972 return cp_parser_new_expression (parser);
3975 return cp_parser_delete_expression (parser);
3979 /* The saved value of the PEDANTIC flag. */
3983 /* Save away the PEDANTIC flag. */
3984 cp_parser_extension_opt (parser, &saved_pedantic);
3985 /* Parse the cast-expression. */
3986 expr = cp_parser_simple_cast_expression (parser);
3987 /* Restore the PEDANTIC flag. */
3988 pedantic = saved_pedantic;
3998 /* Consume the `__real__' or `__imag__' token. */
3999 cp_lexer_consume_token (parser->lexer);
4000 /* Parse the cast-expression. */
4001 expression = cp_parser_simple_cast_expression (parser);
4002 /* Create the complete representation. */
4003 return build_x_unary_op ((keyword == RID_REALPART
4004 ? REALPART_EXPR : IMAGPART_EXPR),
4014 /* Look for the `:: new' and `:: delete', which also signal the
4015 beginning of a new-expression, or delete-expression,
4016 respectively. If the next token is `::', then it might be one of
4018 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
4022 /* See if the token after the `::' is one of the keywords in
4023 which we're interested. */
4024 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
4025 /* If it's `new', we have a new-expression. */
4026 if (keyword == RID_NEW)
4027 return cp_parser_new_expression (parser);
4028 /* Similarly, for `delete'. */
4029 else if (keyword == RID_DELETE)
4030 return cp_parser_delete_expression (parser);
4033 /* Look for a unary operator. */
4034 unary_operator = cp_parser_unary_operator (token);
4035 /* The `++' and `--' operators can be handled similarly, even though
4036 they are not technically unary-operators in the grammar. */
4037 if (unary_operator == ERROR_MARK)
4039 if (token->type == CPP_PLUS_PLUS)
4040 unary_operator = PREINCREMENT_EXPR;
4041 else if (token->type == CPP_MINUS_MINUS)
4042 unary_operator = PREDECREMENT_EXPR;
4043 /* Handle the GNU address-of-label extension. */
4044 else if (cp_parser_allow_gnu_extensions_p (parser)
4045 && token->type == CPP_AND_AND)
4049 /* Consume the '&&' token. */
4050 cp_lexer_consume_token (parser->lexer);
4051 /* Look for the identifier. */
4052 identifier = cp_parser_identifier (parser);
4053 /* Create an expression representing the address. */
4054 return finish_label_address_expr (identifier);
4057 if (unary_operator != ERROR_MARK)
4059 tree cast_expression;
4061 /* Consume the operator token. */
4062 token = cp_lexer_consume_token (parser->lexer);
4063 /* Parse the cast-expression. */
4065 = cp_parser_cast_expression (parser, unary_operator == ADDR_EXPR);
4066 /* Now, build an appropriate representation. */
4067 switch (unary_operator)
4070 return build_x_indirect_ref (cast_expression, "unary *");
4074 return build_x_unary_op (unary_operator, cast_expression);
4076 case PREINCREMENT_EXPR:
4077 case PREDECREMENT_EXPR:
4078 if (parser->constant_expression_p)
4080 if (!parser->allow_non_constant_expression_p)
4081 return cp_parser_non_constant_expression (PREINCREMENT_EXPR
4084 parser->non_constant_expression_p = true;
4089 case TRUTH_NOT_EXPR:
4090 return finish_unary_op_expr (unary_operator, cast_expression);
4094 return error_mark_node;
4098 return cp_parser_postfix_expression (parser, address_p);
4101 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
4102 unary-operator, the corresponding tree code is returned. */
4104 static enum tree_code
4105 cp_parser_unary_operator (cp_token* token)
4107 switch (token->type)
4110 return INDIRECT_REF;
4116 return CONVERT_EXPR;
4122 return TRUTH_NOT_EXPR;
4125 return BIT_NOT_EXPR;
4132 /* Parse a new-expression.
4135 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
4136 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
4138 Returns a representation of the expression. */
4141 cp_parser_new_expression (cp_parser* parser)
4143 bool global_scope_p;
4148 /* Look for the optional `::' operator. */
4150 = (cp_parser_global_scope_opt (parser,
4151 /*current_scope_valid_p=*/false)
4153 /* Look for the `new' operator. */
4154 cp_parser_require_keyword (parser, RID_NEW, "`new'");
4155 /* There's no easy way to tell a new-placement from the
4156 `( type-id )' construct. */
4157 cp_parser_parse_tentatively (parser);
4158 /* Look for a new-placement. */
4159 placement = cp_parser_new_placement (parser);
4160 /* If that didn't work out, there's no new-placement. */
4161 if (!cp_parser_parse_definitely (parser))
4162 placement = NULL_TREE;
4164 /* If the next token is a `(', then we have a parenthesized
4166 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4168 /* Consume the `('. */
4169 cp_lexer_consume_token (parser->lexer);
4170 /* Parse the type-id. */
4171 type = cp_parser_type_id (parser);
4172 /* Look for the closing `)'. */
4173 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4175 /* Otherwise, there must be a new-type-id. */
4177 type = cp_parser_new_type_id (parser);
4179 /* If the next token is a `(', then we have a new-initializer. */
4180 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4181 initializer = cp_parser_new_initializer (parser);
4183 initializer = NULL_TREE;
4185 /* Create a representation of the new-expression. */
4186 return build_new (placement, type, initializer, global_scope_p);
4189 /* Parse a new-placement.
4194 Returns the same representation as for an expression-list. */
4197 cp_parser_new_placement (cp_parser* parser)
4199 tree expression_list;
4201 /* Parse the expression-list. */
4202 expression_list = (cp_parser_parenthesized_expression_list
4203 (parser, false, /*non_constant_p=*/NULL));
4205 return expression_list;
4208 /* Parse a new-type-id.
4211 type-specifier-seq new-declarator [opt]
4213 Returns a TREE_LIST whose TREE_PURPOSE is the type-specifier-seq,
4214 and whose TREE_VALUE is the new-declarator. */
4217 cp_parser_new_type_id (cp_parser* parser)
4219 tree type_specifier_seq;
4221 const char *saved_message;
4223 /* The type-specifier sequence must not contain type definitions.
4224 (It cannot contain declarations of new types either, but if they
4225 are not definitions we will catch that because they are not
4227 saved_message = parser->type_definition_forbidden_message;
4228 parser->type_definition_forbidden_message
4229 = "types may not be defined in a new-type-id";
4230 /* Parse the type-specifier-seq. */
4231 type_specifier_seq = cp_parser_type_specifier_seq (parser);
4232 /* Restore the old message. */
4233 parser->type_definition_forbidden_message = saved_message;
4234 /* Parse the new-declarator. */
4235 declarator = cp_parser_new_declarator_opt (parser);
4237 return build_tree_list (type_specifier_seq, declarator);
4240 /* Parse an (optional) new-declarator.
4243 ptr-operator new-declarator [opt]
4244 direct-new-declarator
4246 Returns a representation of the declarator. See
4247 cp_parser_declarator for the representations used. */
4250 cp_parser_new_declarator_opt (cp_parser* parser)
4252 enum tree_code code;
4254 tree cv_qualifier_seq;
4256 /* We don't know if there's a ptr-operator next, or not. */
4257 cp_parser_parse_tentatively (parser);
4258 /* Look for a ptr-operator. */
4259 code = cp_parser_ptr_operator (parser, &type, &cv_qualifier_seq);
4260 /* If that worked, look for more new-declarators. */
4261 if (cp_parser_parse_definitely (parser))
4265 /* Parse another optional declarator. */
4266 declarator = cp_parser_new_declarator_opt (parser);
4268 /* Create the representation of the declarator. */
4269 if (code == INDIRECT_REF)
4270 declarator = make_pointer_declarator (cv_qualifier_seq,
4273 declarator = make_reference_declarator (cv_qualifier_seq,
4276 /* Handle the pointer-to-member case. */
4278 declarator = build_nt (SCOPE_REF, type, declarator);
4283 /* If the next token is a `[', there is a direct-new-declarator. */
4284 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
4285 return cp_parser_direct_new_declarator (parser);
4290 /* Parse a direct-new-declarator.
4292 direct-new-declarator:
4294 direct-new-declarator [constant-expression]
4296 Returns an ARRAY_REF, following the same conventions as are
4297 documented for cp_parser_direct_declarator. */
4300 cp_parser_direct_new_declarator (cp_parser* parser)
4302 tree declarator = NULL_TREE;
4308 /* Look for the opening `['. */
4309 cp_parser_require (parser, CPP_OPEN_SQUARE, "`['");
4310 /* The first expression is not required to be constant. */
4313 expression = cp_parser_expression (parser);
4314 /* The standard requires that the expression have integral
4315 type. DR 74 adds enumeration types. We believe that the
4316 real intent is that these expressions be handled like the
4317 expression in a `switch' condition, which also allows
4318 classes with a single conversion to integral or
4319 enumeration type. */
4320 if (!processing_template_decl)
4323 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
4328 error ("expression in new-declarator must have integral or enumeration type");
4329 expression = error_mark_node;
4333 /* But all the other expressions must be. */
4336 = cp_parser_constant_expression (parser,
4337 /*allow_non_constant=*/false,
4339 /* Look for the closing `]'. */
4340 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4342 /* Add this bound to the declarator. */
4343 declarator = build_nt (ARRAY_REF, declarator, expression);
4345 /* If the next token is not a `[', then there are no more
4347 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
4354 /* Parse a new-initializer.
4357 ( expression-list [opt] )
4359 Returns a representation of the expression-list. If there is no
4360 expression-list, VOID_ZERO_NODE is returned. */
4363 cp_parser_new_initializer (cp_parser* parser)
4365 tree expression_list;
4367 expression_list = (cp_parser_parenthesized_expression_list
4368 (parser, false, /*non_constant_p=*/NULL));
4369 if (!expression_list)
4370 expression_list = void_zero_node;
4372 return expression_list;
4375 /* Parse a delete-expression.
4378 :: [opt] delete cast-expression
4379 :: [opt] delete [ ] cast-expression
4381 Returns a representation of the expression. */
4384 cp_parser_delete_expression (cp_parser* parser)
4386 bool global_scope_p;
4390 /* Look for the optional `::' operator. */
4392 = (cp_parser_global_scope_opt (parser,
4393 /*current_scope_valid_p=*/false)
4395 /* Look for the `delete' keyword. */
4396 cp_parser_require_keyword (parser, RID_DELETE, "`delete'");
4397 /* See if the array syntax is in use. */
4398 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
4400 /* Consume the `[' token. */
4401 cp_lexer_consume_token (parser->lexer);
4402 /* Look for the `]' token. */
4403 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4404 /* Remember that this is the `[]' construct. */
4410 /* Parse the cast-expression. */
4411 expression = cp_parser_simple_cast_expression (parser);
4413 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
4416 /* Parse a cast-expression.
4420 ( type-id ) cast-expression
4422 Returns a representation of the expression. */
4425 cp_parser_cast_expression (cp_parser *parser, bool address_p)
4427 /* If it's a `(', then we might be looking at a cast. */
4428 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4430 tree type = NULL_TREE;
4431 tree expr = NULL_TREE;
4432 bool compound_literal_p;
4433 const char *saved_message;
4435 /* There's no way to know yet whether or not this is a cast.
4436 For example, `(int (3))' is a unary-expression, while `(int)
4437 3' is a cast. So, we resort to parsing tentatively. */
4438 cp_parser_parse_tentatively (parser);
4439 /* Types may not be defined in a cast. */
4440 saved_message = parser->type_definition_forbidden_message;
4441 parser->type_definition_forbidden_message
4442 = "types may not be defined in casts";
4443 /* Consume the `('. */
4444 cp_lexer_consume_token (parser->lexer);
4445 /* A very tricky bit is that `(struct S) { 3 }' is a
4446 compound-literal (which we permit in C++ as an extension).
4447 But, that construct is not a cast-expression -- it is a
4448 postfix-expression. (The reason is that `(struct S) { 3 }.i'
4449 is legal; if the compound-literal were a cast-expression,
4450 you'd need an extra set of parentheses.) But, if we parse
4451 the type-id, and it happens to be a class-specifier, then we
4452 will commit to the parse at that point, because we cannot
4453 undo the action that is done when creating a new class. So,
4454 then we cannot back up and do a postfix-expression.
4456 Therefore, we scan ahead to the closing `)', and check to see
4457 if the token after the `)' is a `{'. If so, we are not
4458 looking at a cast-expression.
4460 Save tokens so that we can put them back. */
4461 cp_lexer_save_tokens (parser->lexer);
4462 /* Skip tokens until the next token is a closing parenthesis.
4463 If we find the closing `)', and the next token is a `{', then
4464 we are looking at a compound-literal. */
4466 = (cp_parser_skip_to_closing_parenthesis (parser, false, false)
4467 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
4468 /* Roll back the tokens we skipped. */
4469 cp_lexer_rollback_tokens (parser->lexer);
4470 /* If we were looking at a compound-literal, simulate an error
4471 so that the call to cp_parser_parse_definitely below will
4473 if (compound_literal_p)
4474 cp_parser_simulate_error (parser);
4477 /* Look for the type-id. */
4478 type = cp_parser_type_id (parser);
4479 /* Look for the closing `)'. */
4480 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4483 /* Restore the saved message. */
4484 parser->type_definition_forbidden_message = saved_message;
4486 /* If ok so far, parse the dependent expression. We cannot be
4487 sure it is a cast. Consider `(T ())'. It is a parenthesized
4488 ctor of T, but looks like a cast to function returning T
4489 without a dependent expression. */
4490 if (!cp_parser_error_occurred (parser))
4491 expr = cp_parser_simple_cast_expression (parser);
4493 if (cp_parser_parse_definitely (parser))
4495 /* Warn about old-style casts, if so requested. */
4496 if (warn_old_style_cast
4497 && !in_system_header
4498 && !VOID_TYPE_P (type)
4499 && current_lang_name != lang_name_c)
4500 warning ("use of old-style cast");
4502 /* Only type conversions to integral or enumeration types
4503 can be used in constant-expressions. */
4504 if (parser->constant_expression_p
4505 && !dependent_type_p (type)
4506 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type))
4508 if (!parser->allow_non_constant_expression_p)
4509 return (cp_parser_non_constant_expression
4510 ("a casts to a type other than an integral or "
4511 "enumeration type"));
4512 parser->non_constant_expression_p = true;
4514 /* Perform the cast. */
4515 expr = build_c_cast (type, expr);
4520 /* If we get here, then it's not a cast, so it must be a
4521 unary-expression. */
4522 return cp_parser_unary_expression (parser, address_p);
4525 /* Parse a pm-expression.
4529 pm-expression .* cast-expression
4530 pm-expression ->* cast-expression
4532 Returns a representation of the expression. */
4535 cp_parser_pm_expression (cp_parser* parser)
4537 static const cp_parser_token_tree_map map = {
4538 { CPP_DEREF_STAR, MEMBER_REF },
4539 { CPP_DOT_STAR, DOTSTAR_EXPR },
4540 { CPP_EOF, ERROR_MARK }
4543 return cp_parser_binary_expression (parser, map,
4544 cp_parser_simple_cast_expression);
4547 /* Parse a multiplicative-expression.
4549 mulitplicative-expression:
4551 multiplicative-expression * pm-expression
4552 multiplicative-expression / pm-expression
4553 multiplicative-expression % pm-expression
4555 Returns a representation of the expression. */
4558 cp_parser_multiplicative_expression (cp_parser* parser)
4560 static const cp_parser_token_tree_map map = {
4561 { CPP_MULT, MULT_EXPR },
4562 { CPP_DIV, TRUNC_DIV_EXPR },
4563 { CPP_MOD, TRUNC_MOD_EXPR },
4564 { CPP_EOF, ERROR_MARK }
4567 return cp_parser_binary_expression (parser,
4569 cp_parser_pm_expression);
4572 /* Parse an additive-expression.
4574 additive-expression:
4575 multiplicative-expression
4576 additive-expression + multiplicative-expression
4577 additive-expression - multiplicative-expression
4579 Returns a representation of the expression. */
4582 cp_parser_additive_expression (cp_parser* parser)
4584 static const cp_parser_token_tree_map map = {
4585 { CPP_PLUS, PLUS_EXPR },
4586 { CPP_MINUS, MINUS_EXPR },
4587 { CPP_EOF, ERROR_MARK }
4590 return cp_parser_binary_expression (parser,
4592 cp_parser_multiplicative_expression);
4595 /* Parse a shift-expression.
4599 shift-expression << additive-expression
4600 shift-expression >> additive-expression
4602 Returns a representation of the expression. */
4605 cp_parser_shift_expression (cp_parser* parser)
4607 static const cp_parser_token_tree_map map = {
4608 { CPP_LSHIFT, LSHIFT_EXPR },
4609 { CPP_RSHIFT, RSHIFT_EXPR },
4610 { CPP_EOF, ERROR_MARK }
4613 return cp_parser_binary_expression (parser,
4615 cp_parser_additive_expression);
4618 /* Parse a relational-expression.
4620 relational-expression:
4622 relational-expression < shift-expression
4623 relational-expression > shift-expression
4624 relational-expression <= shift-expression
4625 relational-expression >= shift-expression
4629 relational-expression:
4630 relational-expression <? shift-expression
4631 relational-expression >? shift-expression
4633 Returns a representation of the expression. */
4636 cp_parser_relational_expression (cp_parser* parser)
4638 static const cp_parser_token_tree_map map = {
4639 { CPP_LESS, LT_EXPR },
4640 { CPP_GREATER, GT_EXPR },
4641 { CPP_LESS_EQ, LE_EXPR },
4642 { CPP_GREATER_EQ, GE_EXPR },
4643 { CPP_MIN, MIN_EXPR },
4644 { CPP_MAX, MAX_EXPR },
4645 { CPP_EOF, ERROR_MARK }
4648 return cp_parser_binary_expression (parser,
4650 cp_parser_shift_expression);
4653 /* Parse an equality-expression.
4655 equality-expression:
4656 relational-expression
4657 equality-expression == relational-expression
4658 equality-expression != relational-expression
4660 Returns a representation of the expression. */
4663 cp_parser_equality_expression (cp_parser* parser)
4665 static const cp_parser_token_tree_map map = {
4666 { CPP_EQ_EQ, EQ_EXPR },
4667 { CPP_NOT_EQ, NE_EXPR },
4668 { CPP_EOF, ERROR_MARK }
4671 return cp_parser_binary_expression (parser,
4673 cp_parser_relational_expression);
4676 /* Parse an and-expression.
4680 and-expression & equality-expression
4682 Returns a representation of the expression. */
4685 cp_parser_and_expression (cp_parser* parser)
4687 static const cp_parser_token_tree_map map = {
4688 { CPP_AND, BIT_AND_EXPR },
4689 { CPP_EOF, ERROR_MARK }
4692 return cp_parser_binary_expression (parser,
4694 cp_parser_equality_expression);
4697 /* Parse an exclusive-or-expression.
4699 exclusive-or-expression:
4701 exclusive-or-expression ^ and-expression
4703 Returns a representation of the expression. */
4706 cp_parser_exclusive_or_expression (cp_parser* parser)
4708 static const cp_parser_token_tree_map map = {
4709 { CPP_XOR, BIT_XOR_EXPR },
4710 { CPP_EOF, ERROR_MARK }
4713 return cp_parser_binary_expression (parser,
4715 cp_parser_and_expression);
4719 /* Parse an inclusive-or-expression.
4721 inclusive-or-expression:
4722 exclusive-or-expression
4723 inclusive-or-expression | exclusive-or-expression
4725 Returns a representation of the expression. */
4728 cp_parser_inclusive_or_expression (cp_parser* parser)
4730 static const cp_parser_token_tree_map map = {
4731 { CPP_OR, BIT_IOR_EXPR },
4732 { CPP_EOF, ERROR_MARK }
4735 return cp_parser_binary_expression (parser,
4737 cp_parser_exclusive_or_expression);
4740 /* Parse a logical-and-expression.
4742 logical-and-expression:
4743 inclusive-or-expression
4744 logical-and-expression && inclusive-or-expression
4746 Returns a representation of the expression. */
4749 cp_parser_logical_and_expression (cp_parser* parser)
4751 static const cp_parser_token_tree_map map = {
4752 { CPP_AND_AND, TRUTH_ANDIF_EXPR },
4753 { CPP_EOF, ERROR_MARK }
4756 return cp_parser_binary_expression (parser,
4758 cp_parser_inclusive_or_expression);
4761 /* Parse a logical-or-expression.
4763 logical-or-expression:
4764 logical-and-expression
4765 logical-or-expression || logical-and-expression
4767 Returns a representation of the expression. */
4770 cp_parser_logical_or_expression (cp_parser* parser)
4772 static const cp_parser_token_tree_map map = {
4773 { CPP_OR_OR, TRUTH_ORIF_EXPR },
4774 { CPP_EOF, ERROR_MARK }
4777 return cp_parser_binary_expression (parser,
4779 cp_parser_logical_and_expression);
4782 /* Parse the `? expression : assignment-expression' part of a
4783 conditional-expression. The LOGICAL_OR_EXPR is the
4784 logical-or-expression that started the conditional-expression.
4785 Returns a representation of the entire conditional-expression.
4787 This routine is used by cp_parser_assignment_expression.
4789 ? expression : assignment-expression
4793 ? : assignment-expression */
4796 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
4799 tree assignment_expr;
4801 /* Consume the `?' token. */
4802 cp_lexer_consume_token (parser->lexer);
4803 if (cp_parser_allow_gnu_extensions_p (parser)
4804 && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
4805 /* Implicit true clause. */
4808 /* Parse the expression. */
4809 expr = cp_parser_expression (parser);
4811 /* The next token should be a `:'. */
4812 cp_parser_require (parser, CPP_COLON, "`:'");
4813 /* Parse the assignment-expression. */
4814 assignment_expr = cp_parser_assignment_expression (parser);
4816 /* Build the conditional-expression. */
4817 return build_x_conditional_expr (logical_or_expr,
4822 /* Parse an assignment-expression.
4824 assignment-expression:
4825 conditional-expression
4826 logical-or-expression assignment-operator assignment_expression
4829 Returns a representation for the expression. */
4832 cp_parser_assignment_expression (cp_parser* parser)
4836 /* If the next token is the `throw' keyword, then we're looking at
4837 a throw-expression. */
4838 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
4839 expr = cp_parser_throw_expression (parser);
4840 /* Otherwise, it must be that we are looking at a
4841 logical-or-expression. */
4844 /* Parse the logical-or-expression. */
4845 expr = cp_parser_logical_or_expression (parser);
4846 /* If the next token is a `?' then we're actually looking at a
4847 conditional-expression. */
4848 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
4849 return cp_parser_question_colon_clause (parser, expr);
4852 enum tree_code assignment_operator;
4854 /* If it's an assignment-operator, we're using the second
4857 = cp_parser_assignment_operator_opt (parser);
4858 if (assignment_operator != ERROR_MARK)
4862 /* Parse the right-hand side of the assignment. */
4863 rhs = cp_parser_assignment_expression (parser);
4864 /* An assignment may not appear in a
4865 constant-expression. */
4866 if (parser->constant_expression_p)
4868 if (!parser->allow_non_constant_expression_p)
4869 return cp_parser_non_constant_expression ("an assignment");
4870 parser->non_constant_expression_p = true;
4872 /* Build the assignment expression. */
4873 expr = build_x_modify_expr (expr,
4874 assignment_operator,
4883 /* Parse an (optional) assignment-operator.
4885 assignment-operator: one of
4886 = *= /= %= += -= >>= <<= &= ^= |=
4890 assignment-operator: one of
4893 If the next token is an assignment operator, the corresponding tree
4894 code is returned, and the token is consumed. For example, for
4895 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
4896 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
4897 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
4898 operator, ERROR_MARK is returned. */
4900 static enum tree_code
4901 cp_parser_assignment_operator_opt (cp_parser* parser)
4906 /* Peek at the next toen. */
4907 token = cp_lexer_peek_token (parser->lexer);
4909 switch (token->type)
4920 op = TRUNC_DIV_EXPR;
4924 op = TRUNC_MOD_EXPR;
4964 /* Nothing else is an assignment operator. */
4968 /* If it was an assignment operator, consume it. */
4969 if (op != ERROR_MARK)
4970 cp_lexer_consume_token (parser->lexer);
4975 /* Parse an expression.
4978 assignment-expression
4979 expression , assignment-expression
4981 Returns a representation of the expression. */
4984 cp_parser_expression (cp_parser* parser)
4986 tree expression = NULL_TREE;
4990 tree assignment_expression;
4992 /* Parse the next assignment-expression. */
4993 assignment_expression
4994 = cp_parser_assignment_expression (parser);
4995 /* If this is the first assignment-expression, we can just
4998 expression = assignment_expression;
5000 expression = build_x_compound_expr (expression,
5001 assignment_expression);
5002 /* If the next token is not a comma, then we are done with the
5004 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5006 /* Consume the `,'. */
5007 cp_lexer_consume_token (parser->lexer);
5008 /* A comma operator cannot appear in a constant-expression. */
5009 if (parser->constant_expression_p)
5011 if (!parser->allow_non_constant_expression_p)
5013 = cp_parser_non_constant_expression ("a comma operator");
5014 parser->non_constant_expression_p = true;
5021 /* Parse a constant-expression.
5023 constant-expression:
5024 conditional-expression
5026 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
5027 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
5028 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
5029 is false, NON_CONSTANT_P should be NULL. */
5032 cp_parser_constant_expression (cp_parser* parser,
5033 bool allow_non_constant_p,
5034 bool *non_constant_p)
5036 bool saved_constant_expression_p;
5037 bool saved_allow_non_constant_expression_p;
5038 bool saved_non_constant_expression_p;
5041 /* It might seem that we could simply parse the
5042 conditional-expression, and then check to see if it were
5043 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
5044 one that the compiler can figure out is constant, possibly after
5045 doing some simplifications or optimizations. The standard has a
5046 precise definition of constant-expression, and we must honor
5047 that, even though it is somewhat more restrictive.
5053 is not a legal declaration, because `(2, 3)' is not a
5054 constant-expression. The `,' operator is forbidden in a
5055 constant-expression. However, GCC's constant-folding machinery
5056 will fold this operation to an INTEGER_CST for `3'. */
5058 /* Save the old settings. */
5059 saved_constant_expression_p = parser->constant_expression_p;
5060 saved_allow_non_constant_expression_p
5061 = parser->allow_non_constant_expression_p;
5062 saved_non_constant_expression_p = parser->non_constant_expression_p;
5063 /* We are now parsing a constant-expression. */
5064 parser->constant_expression_p = true;
5065 parser->allow_non_constant_expression_p = allow_non_constant_p;
5066 parser->non_constant_expression_p = false;
5067 /* Although the grammar says "conditional-expression", we parse an
5068 "assignment-expression", which also permits "throw-expression"
5069 and the use of assignment operators. In the case that
5070 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
5071 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
5072 actually essential that we look for an assignment-expression.
5073 For example, cp_parser_initializer_clauses uses this function to
5074 determine whether a particular assignment-expression is in fact
5076 expression = cp_parser_assignment_expression (parser);
5077 /* Restore the old settings. */
5078 parser->constant_expression_p = saved_constant_expression_p;
5079 parser->allow_non_constant_expression_p
5080 = saved_allow_non_constant_expression_p;
5081 if (allow_non_constant_p)
5082 *non_constant_p = parser->non_constant_expression_p;
5083 parser->non_constant_expression_p = saved_non_constant_expression_p;
5088 /* Statements [gram.stmt.stmt] */
5090 /* Parse a statement.
5094 expression-statement
5099 declaration-statement
5103 cp_parser_statement (cp_parser* parser, bool in_statement_expr_p)
5107 int statement_line_number;
5109 /* There is no statement yet. */
5110 statement = NULL_TREE;
5111 /* Peek at the next token. */
5112 token = cp_lexer_peek_token (parser->lexer);
5113 /* Remember the line number of the first token in the statement. */
5114 statement_line_number = token->location.line;
5115 /* If this is a keyword, then that will often determine what kind of
5116 statement we have. */
5117 if (token->type == CPP_KEYWORD)
5119 enum rid keyword = token->keyword;
5125 statement = cp_parser_labeled_statement (parser,
5126 in_statement_expr_p);
5131 statement = cp_parser_selection_statement (parser);
5137 statement = cp_parser_iteration_statement (parser);
5144 statement = cp_parser_jump_statement (parser);
5148 statement = cp_parser_try_block (parser);
5152 /* It might be a keyword like `int' that can start a
5153 declaration-statement. */
5157 else if (token->type == CPP_NAME)
5159 /* If the next token is a `:', then we are looking at a
5160 labeled-statement. */
5161 token = cp_lexer_peek_nth_token (parser->lexer, 2);
5162 if (token->type == CPP_COLON)
5163 statement = cp_parser_labeled_statement (parser, in_statement_expr_p);
5165 /* Anything that starts with a `{' must be a compound-statement. */
5166 else if (token->type == CPP_OPEN_BRACE)
5167 statement = cp_parser_compound_statement (parser, false);
5169 /* Everything else must be a declaration-statement or an
5170 expression-statement. Try for the declaration-statement
5171 first, unless we are looking at a `;', in which case we know that
5172 we have an expression-statement. */
5175 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
5177 cp_parser_parse_tentatively (parser);
5178 /* Try to parse the declaration-statement. */
5179 cp_parser_declaration_statement (parser);
5180 /* If that worked, we're done. */
5181 if (cp_parser_parse_definitely (parser))
5184 /* Look for an expression-statement instead. */
5185 statement = cp_parser_expression_statement (parser, in_statement_expr_p);
5188 /* Set the line number for the statement. */
5189 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
5190 STMT_LINENO (statement) = statement_line_number;
5193 /* Parse a labeled-statement.
5196 identifier : statement
5197 case constant-expression : statement
5200 Returns the new CASE_LABEL, for a `case' or `default' label. For
5201 an ordinary label, returns a LABEL_STMT. */
5204 cp_parser_labeled_statement (cp_parser* parser, bool in_statement_expr_p)
5207 tree statement = NULL_TREE;
5209 /* The next token should be an identifier. */
5210 token = cp_lexer_peek_token (parser->lexer);
5211 if (token->type != CPP_NAME
5212 && token->type != CPP_KEYWORD)
5214 cp_parser_error (parser, "expected labeled-statement");
5215 return error_mark_node;
5218 switch (token->keyword)
5224 /* Consume the `case' token. */
5225 cp_lexer_consume_token (parser->lexer);
5226 /* Parse the constant-expression. */
5227 expr = cp_parser_constant_expression (parser,
5228 /*allow_non_constant_p=*/false,
5230 /* Create the label. */
5231 statement = finish_case_label (expr, NULL_TREE);
5236 /* Consume the `default' token. */
5237 cp_lexer_consume_token (parser->lexer);
5238 /* Create the label. */
5239 statement = finish_case_label (NULL_TREE, NULL_TREE);
5243 /* Anything else must be an ordinary label. */
5244 statement = finish_label_stmt (cp_parser_identifier (parser));
5248 /* Require the `:' token. */
5249 cp_parser_require (parser, CPP_COLON, "`:'");
5250 /* Parse the labeled statement. */
5251 cp_parser_statement (parser, in_statement_expr_p);
5253 /* Return the label, in the case of a `case' or `default' label. */
5257 /* Parse an expression-statement.
5259 expression-statement:
5262 Returns the new EXPR_STMT -- or NULL_TREE if the expression
5263 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
5264 indicates whether this expression-statement is part of an
5265 expression statement. */
5268 cp_parser_expression_statement (cp_parser* parser, bool in_statement_expr_p)
5270 tree statement = NULL_TREE;
5272 /* If the next token is a ';', then there is no expression
5274 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
5275 statement = cp_parser_expression (parser);
5277 /* Consume the final `;'. */
5278 cp_parser_consume_semicolon_at_end_of_statement (parser);
5280 if (in_statement_expr_p
5281 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
5283 /* This is the final expression statement of a statement
5285 statement = finish_stmt_expr_expr (statement);
5288 statement = finish_expr_stmt (statement);
5295 /* Parse a compound-statement.
5298 { statement-seq [opt] }
5300 Returns a COMPOUND_STMT representing the statement. */
5303 cp_parser_compound_statement (cp_parser *parser, bool in_statement_expr_p)
5307 /* Consume the `{'. */
5308 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
5309 return error_mark_node;
5310 /* Begin the compound-statement. */
5311 compound_stmt = begin_compound_stmt (/*has_no_scope=*/false);
5312 /* Parse an (optional) statement-seq. */
5313 cp_parser_statement_seq_opt (parser, in_statement_expr_p);
5314 /* Finish the compound-statement. */
5315 finish_compound_stmt (compound_stmt);
5316 /* Consume the `}'. */
5317 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
5319 return compound_stmt;
5322 /* Parse an (optional) statement-seq.
5326 statement-seq [opt] statement */
5329 cp_parser_statement_seq_opt (cp_parser* parser, bool in_statement_expr_p)
5331 /* Scan statements until there aren't any more. */
5334 /* If we're looking at a `}', then we've run out of statements. */
5335 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE)
5336 || cp_lexer_next_token_is (parser->lexer, CPP_EOF))
5339 /* Parse the statement. */
5340 cp_parser_statement (parser, in_statement_expr_p);
5344 /* Parse a selection-statement.
5346 selection-statement:
5347 if ( condition ) statement
5348 if ( condition ) statement else statement
5349 switch ( condition ) statement
5351 Returns the new IF_STMT or SWITCH_STMT. */
5354 cp_parser_selection_statement (cp_parser* parser)
5359 /* Peek at the next token. */
5360 token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
5362 /* See what kind of keyword it is. */
5363 keyword = token->keyword;
5372 /* Look for the `('. */
5373 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
5375 cp_parser_skip_to_end_of_statement (parser);
5376 return error_mark_node;
5379 /* Begin the selection-statement. */
5380 if (keyword == RID_IF)
5381 statement = begin_if_stmt ();
5383 statement = begin_switch_stmt ();
5385 /* Parse the condition. */
5386 condition = cp_parser_condition (parser);
5387 /* Look for the `)'. */
5388 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
5389 cp_parser_skip_to_closing_parenthesis (parser, true, false);
5391 if (keyword == RID_IF)
5395 /* Add the condition. */
5396 finish_if_stmt_cond (condition, statement);
5398 /* Parse the then-clause. */
5399 then_stmt = cp_parser_implicitly_scoped_statement (parser);
5400 finish_then_clause (statement);
5402 /* If the next token is `else', parse the else-clause. */
5403 if (cp_lexer_next_token_is_keyword (parser->lexer,
5408 /* Consume the `else' keyword. */
5409 cp_lexer_consume_token (parser->lexer);
5410 /* Parse the else-clause. */
5412 = cp_parser_implicitly_scoped_statement (parser);
5413 finish_else_clause (statement);
5416 /* Now we're all done with the if-statement. */
5423 /* Add the condition. */
5424 finish_switch_cond (condition, statement);
5426 /* Parse the body of the switch-statement. */
5427 body = cp_parser_implicitly_scoped_statement (parser);
5429 /* Now we're all done with the switch-statement. */
5430 finish_switch_stmt (statement);
5438 cp_parser_error (parser, "expected selection-statement");
5439 return error_mark_node;
5443 /* Parse a condition.
5447 type-specifier-seq declarator = assignment-expression
5452 type-specifier-seq declarator asm-specification [opt]
5453 attributes [opt] = assignment-expression
5455 Returns the expression that should be tested. */
5458 cp_parser_condition (cp_parser* parser)
5460 tree type_specifiers;
5461 const char *saved_message;
5463 /* Try the declaration first. */
5464 cp_parser_parse_tentatively (parser);
5465 /* New types are not allowed in the type-specifier-seq for a
5467 saved_message = parser->type_definition_forbidden_message;
5468 parser->type_definition_forbidden_message
5469 = "types may not be defined in conditions";
5470 /* Parse the type-specifier-seq. */
5471 type_specifiers = cp_parser_type_specifier_seq (parser);
5472 /* Restore the saved message. */
5473 parser->type_definition_forbidden_message = saved_message;
5474 /* If all is well, we might be looking at a declaration. */
5475 if (!cp_parser_error_occurred (parser))
5478 tree asm_specification;
5481 tree initializer = NULL_TREE;
5483 /* Parse the declarator. */
5484 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
5485 /*ctor_dtor_or_conv_p=*/NULL);
5486 /* Parse the attributes. */
5487 attributes = cp_parser_attributes_opt (parser);
5488 /* Parse the asm-specification. */
5489 asm_specification = cp_parser_asm_specification_opt (parser);
5490 /* If the next token is not an `=', then we might still be
5491 looking at an expression. For example:
5495 looks like a decl-specifier-seq and a declarator -- but then
5496 there is no `=', so this is an expression. */
5497 cp_parser_require (parser, CPP_EQ, "`='");
5498 /* If we did see an `=', then we are looking at a declaration
5500 if (cp_parser_parse_definitely (parser))
5502 /* Create the declaration. */
5503 decl = start_decl (declarator, type_specifiers,
5504 /*initialized_p=*/true,
5505 attributes, /*prefix_attributes=*/NULL_TREE);
5506 /* Parse the assignment-expression. */
5507 initializer = cp_parser_assignment_expression (parser);
5509 /* Process the initializer. */
5510 cp_finish_decl (decl,
5513 LOOKUP_ONLYCONVERTING);
5515 return convert_from_reference (decl);
5518 /* If we didn't even get past the declarator successfully, we are
5519 definitely not looking at a declaration. */
5521 cp_parser_abort_tentative_parse (parser);
5523 /* Otherwise, we are looking at an expression. */
5524 return cp_parser_expression (parser);
5527 /* Parse an iteration-statement.
5529 iteration-statement:
5530 while ( condition ) statement
5531 do statement while ( expression ) ;
5532 for ( for-init-statement condition [opt] ; expression [opt] )
5535 Returns the new WHILE_STMT, DO_STMT, or FOR_STMT. */
5538 cp_parser_iteration_statement (cp_parser* parser)
5544 /* Peek at the next token. */
5545 token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
5547 return error_mark_node;
5549 /* See what kind of keyword it is. */
5550 keyword = token->keyword;
5557 /* Begin the while-statement. */
5558 statement = begin_while_stmt ();
5559 /* Look for the `('. */
5560 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
5561 /* Parse the condition. */
5562 condition = cp_parser_condition (parser);
5563 finish_while_stmt_cond (condition, statement);
5564 /* Look for the `)'. */
5565 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5566 /* Parse the dependent statement. */
5567 cp_parser_already_scoped_statement (parser);
5568 /* We're done with the while-statement. */
5569 finish_while_stmt (statement);
5577 /* Begin the do-statement. */
5578 statement = begin_do_stmt ();
5579 /* Parse the body of the do-statement. */
5580 cp_parser_implicitly_scoped_statement (parser);
5581 finish_do_body (statement);
5582 /* Look for the `while' keyword. */
5583 cp_parser_require_keyword (parser, RID_WHILE, "`while'");
5584 /* Look for the `('. */
5585 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
5586 /* Parse the expression. */
5587 expression = cp_parser_expression (parser);
5588 /* We're done with the do-statement. */
5589 finish_do_stmt (expression, statement);
5590 /* Look for the `)'. */
5591 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5592 /* Look for the `;'. */
5593 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
5599 tree condition = NULL_TREE;
5600 tree expression = NULL_TREE;
5602 /* Begin the for-statement. */
5603 statement = begin_for_stmt ();
5604 /* Look for the `('. */
5605 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
5606 /* Parse the initialization. */
5607 cp_parser_for_init_statement (parser);
5608 finish_for_init_stmt (statement);
5610 /* If there's a condition, process it. */
5611 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
5612 condition = cp_parser_condition (parser);
5613 finish_for_cond (condition, statement);
5614 /* Look for the `;'. */
5615 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
5617 /* If there's an expression, process it. */
5618 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
5619 expression = cp_parser_expression (parser);
5620 finish_for_expr (expression, statement);
5621 /* Look for the `)'. */
5622 cp_parser_require (parser, CPP_CLOSE_PAREN, "`;'");
5624 /* Parse the body of the for-statement. */
5625 cp_parser_already_scoped_statement (parser);
5627 /* We're done with the for-statement. */
5628 finish_for_stmt (statement);
5633 cp_parser_error (parser, "expected iteration-statement");
5634 statement = error_mark_node;
5641 /* Parse a for-init-statement.
5644 expression-statement
5645 simple-declaration */
5648 cp_parser_for_init_statement (cp_parser* parser)
5650 /* If the next token is a `;', then we have an empty
5651 expression-statement. Grammatically, this is also a
5652 simple-declaration, but an invalid one, because it does not
5653 declare anything. Therefore, if we did not handle this case
5654 specially, we would issue an error message about an invalid
5656 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
5658 /* We're going to speculatively look for a declaration, falling back
5659 to an expression, if necessary. */
5660 cp_parser_parse_tentatively (parser);
5661 /* Parse the declaration. */
5662 cp_parser_simple_declaration (parser,
5663 /*function_definition_allowed_p=*/false);
5664 /* If the tentative parse failed, then we shall need to look for an
5665 expression-statement. */
5666 if (cp_parser_parse_definitely (parser))
5670 cp_parser_expression_statement (parser, false);
5673 /* Parse a jump-statement.
5678 return expression [opt] ;
5686 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_STMT, or
5690 cp_parser_jump_statement (cp_parser* parser)
5692 tree statement = error_mark_node;
5696 /* Peek at the next token. */
5697 token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
5699 return error_mark_node;
5701 /* See what kind of keyword it is. */
5702 keyword = token->keyword;
5706 statement = finish_break_stmt ();
5707 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
5711 statement = finish_continue_stmt ();
5712 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
5719 /* If the next token is a `;', then there is no
5721 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
5722 expr = cp_parser_expression (parser);
5725 /* Build the return-statement. */
5726 statement = finish_return_stmt (expr);
5727 /* Look for the final `;'. */
5728 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
5733 /* Create the goto-statement. */
5734 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
5736 /* Issue a warning about this use of a GNU extension. */
5738 pedwarn ("ISO C++ forbids computed gotos");
5739 /* Consume the '*' token. */
5740 cp_lexer_consume_token (parser->lexer);
5741 /* Parse the dependent expression. */
5742 finish_goto_stmt (cp_parser_expression (parser));
5745 finish_goto_stmt (cp_parser_identifier (parser));
5746 /* Look for the final `;'. */
5747 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
5751 cp_parser_error (parser, "expected jump-statement");
5758 /* Parse a declaration-statement.
5760 declaration-statement:
5761 block-declaration */
5764 cp_parser_declaration_statement (cp_parser* parser)
5766 /* Parse the block-declaration. */
5767 cp_parser_block_declaration (parser, /*statement_p=*/true);
5769 /* Finish off the statement. */
5773 /* Some dependent statements (like `if (cond) statement'), are
5774 implicitly in their own scope. In other words, if the statement is
5775 a single statement (as opposed to a compound-statement), it is
5776 none-the-less treated as if it were enclosed in braces. Any
5777 declarations appearing in the dependent statement are out of scope
5778 after control passes that point. This function parses a statement,
5779 but ensures that is in its own scope, even if it is not a
5782 Returns the new statement. */
5785 cp_parser_implicitly_scoped_statement (cp_parser* parser)
5789 /* If the token is not a `{', then we must take special action. */
5790 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
5792 /* Create a compound-statement. */
5793 statement = begin_compound_stmt (/*has_no_scope=*/false);
5794 /* Parse the dependent-statement. */
5795 cp_parser_statement (parser, false);
5796 /* Finish the dummy compound-statement. */
5797 finish_compound_stmt (statement);
5799 /* Otherwise, we simply parse the statement directly. */
5801 statement = cp_parser_compound_statement (parser, false);
5803 /* Return the statement. */
5807 /* For some dependent statements (like `while (cond) statement'), we
5808 have already created a scope. Therefore, even if the dependent
5809 statement is a compound-statement, we do not want to create another
5813 cp_parser_already_scoped_statement (cp_parser* parser)
5815 /* If the token is not a `{', then we must take special action. */
5816 if (cp_lexer_next_token_is_not(parser->lexer, CPP_OPEN_BRACE))
5820 /* Create a compound-statement. */
5821 statement = begin_compound_stmt (/*has_no_scope=*/true);
5822 /* Parse the dependent-statement. */
5823 cp_parser_statement (parser, false);
5824 /* Finish the dummy compound-statement. */
5825 finish_compound_stmt (statement);
5827 /* Otherwise, we simply parse the statement directly. */
5829 cp_parser_statement (parser, false);
5832 /* Declarations [gram.dcl.dcl] */
5834 /* Parse an optional declaration-sequence.
5838 declaration-seq declaration */
5841 cp_parser_declaration_seq_opt (cp_parser* parser)
5847 token = cp_lexer_peek_token (parser->lexer);
5849 if (token->type == CPP_CLOSE_BRACE
5850 || token->type == CPP_EOF)
5853 if (token->type == CPP_SEMICOLON)
5855 /* A declaration consisting of a single semicolon is
5856 invalid. Allow it unless we're being pedantic. */
5858 pedwarn ("extra `;'");
5859 cp_lexer_consume_token (parser->lexer);
5863 /* The C lexer modifies PENDING_LANG_CHANGE when it wants the
5864 parser to enter or exit implicit `extern "C"' blocks. */
5865 while (pending_lang_change > 0)
5867 push_lang_context (lang_name_c);
5868 --pending_lang_change;
5870 while (pending_lang_change < 0)
5872 pop_lang_context ();
5873 ++pending_lang_change;
5876 /* Parse the declaration itself. */
5877 cp_parser_declaration (parser);
5881 /* Parse a declaration.
5886 template-declaration
5887 explicit-instantiation
5888 explicit-specialization
5889 linkage-specification
5890 namespace-definition
5895 __extension__ declaration */
5898 cp_parser_declaration (cp_parser* parser)
5904 /* Check for the `__extension__' keyword. */
5905 if (cp_parser_extension_opt (parser, &saved_pedantic))
5907 /* Parse the qualified declaration. */
5908 cp_parser_declaration (parser);
5909 /* Restore the PEDANTIC flag. */
5910 pedantic = saved_pedantic;
5915 /* Try to figure out what kind of declaration is present. */
5916 token1 = *cp_lexer_peek_token (parser->lexer);
5917 if (token1.type != CPP_EOF)
5918 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
5920 /* If the next token is `extern' and the following token is a string
5921 literal, then we have a linkage specification. */
5922 if (token1.keyword == RID_EXTERN
5923 && cp_parser_is_string_literal (&token2))
5924 cp_parser_linkage_specification (parser);
5925 /* If the next token is `template', then we have either a template
5926 declaration, an explicit instantiation, or an explicit
5928 else if (token1.keyword == RID_TEMPLATE)
5930 /* `template <>' indicates a template specialization. */
5931 if (token2.type == CPP_LESS
5932 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
5933 cp_parser_explicit_specialization (parser);
5934 /* `template <' indicates a template declaration. */
5935 else if (token2.type == CPP_LESS)
5936 cp_parser_template_declaration (parser, /*member_p=*/false);
5937 /* Anything else must be an explicit instantiation. */
5939 cp_parser_explicit_instantiation (parser);
5941 /* If the next token is `export', then we have a template
5943 else if (token1.keyword == RID_EXPORT)
5944 cp_parser_template_declaration (parser, /*member_p=*/false);
5945 /* If the next token is `extern', 'static' or 'inline' and the one
5946 after that is `template', we have a GNU extended explicit
5947 instantiation directive. */
5948 else if (cp_parser_allow_gnu_extensions_p (parser)
5949 && (token1.keyword == RID_EXTERN
5950 || token1.keyword == RID_STATIC
5951 || token1.keyword == RID_INLINE)
5952 && token2.keyword == RID_TEMPLATE)
5953 cp_parser_explicit_instantiation (parser);
5954 /* If the next token is `namespace', check for a named or unnamed
5955 namespace definition. */
5956 else if (token1.keyword == RID_NAMESPACE
5957 && (/* A named namespace definition. */
5958 (token2.type == CPP_NAME
5959 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
5961 /* An unnamed namespace definition. */
5962 || token2.type == CPP_OPEN_BRACE))
5963 cp_parser_namespace_definition (parser);
5964 /* We must have either a block declaration or a function
5967 /* Try to parse a block-declaration, or a function-definition. */
5968 cp_parser_block_declaration (parser, /*statement_p=*/false);
5971 /* Parse a block-declaration.
5976 namespace-alias-definition
5983 __extension__ block-declaration
5986 If STATEMENT_P is TRUE, then this block-declaration is occurring as
5987 part of a declaration-statement. */
5990 cp_parser_block_declaration (cp_parser *parser,
5996 /* Check for the `__extension__' keyword. */
5997 if (cp_parser_extension_opt (parser, &saved_pedantic))
5999 /* Parse the qualified declaration. */
6000 cp_parser_block_declaration (parser, statement_p);
6001 /* Restore the PEDANTIC flag. */
6002 pedantic = saved_pedantic;
6007 /* Peek at the next token to figure out which kind of declaration is
6009 token1 = cp_lexer_peek_token (parser->lexer);
6011 /* If the next keyword is `asm', we have an asm-definition. */
6012 if (token1->keyword == RID_ASM)
6015 cp_parser_commit_to_tentative_parse (parser);
6016 cp_parser_asm_definition (parser);
6018 /* If the next keyword is `namespace', we have a
6019 namespace-alias-definition. */
6020 else if (token1->keyword == RID_NAMESPACE)
6021 cp_parser_namespace_alias_definition (parser);
6022 /* If the next keyword is `using', we have either a
6023 using-declaration or a using-directive. */
6024 else if (token1->keyword == RID_USING)
6029 cp_parser_commit_to_tentative_parse (parser);
6030 /* If the token after `using' is `namespace', then we have a
6032 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
6033 if (token2->keyword == RID_NAMESPACE)
6034 cp_parser_using_directive (parser);
6035 /* Otherwise, it's a using-declaration. */
6037 cp_parser_using_declaration (parser);
6039 /* If the next keyword is `__label__' we have a label declaration. */
6040 else if (token1->keyword == RID_LABEL)
6043 cp_parser_commit_to_tentative_parse (parser);
6044 cp_parser_label_declaration (parser);
6046 /* Anything else must be a simple-declaration. */
6048 cp_parser_simple_declaration (parser, !statement_p);
6051 /* Parse a simple-declaration.
6054 decl-specifier-seq [opt] init-declarator-list [opt] ;
6056 init-declarator-list:
6058 init-declarator-list , init-declarator
6060 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
6061 function-definition as a simple-declaration. */
6064 cp_parser_simple_declaration (cp_parser* parser,
6065 bool function_definition_allowed_p)
6067 tree decl_specifiers;
6069 int declares_class_or_enum;
6070 bool saw_declarator;
6072 /* Defer access checks until we know what is being declared; the
6073 checks for names appearing in the decl-specifier-seq should be
6074 done as if we were in the scope of the thing being declared. */
6075 push_deferring_access_checks (dk_deferred);
6077 /* Parse the decl-specifier-seq. We have to keep track of whether
6078 or not the decl-specifier-seq declares a named class or
6079 enumeration type, since that is the only case in which the
6080 init-declarator-list is allowed to be empty.
6084 In a simple-declaration, the optional init-declarator-list can be
6085 omitted only when declaring a class or enumeration, that is when
6086 the decl-specifier-seq contains either a class-specifier, an
6087 elaborated-type-specifier, or an enum-specifier. */
6089 = cp_parser_decl_specifier_seq (parser,
6090 CP_PARSER_FLAGS_OPTIONAL,
6092 &declares_class_or_enum);
6093 /* We no longer need to defer access checks. */
6094 stop_deferring_access_checks ();
6096 /* In a block scope, a valid declaration must always have a
6097 decl-specifier-seq. By not trying to parse declarators, we can
6098 resolve the declaration/expression ambiguity more quickly. */
6099 if (!function_definition_allowed_p && !decl_specifiers)
6101 cp_parser_error (parser, "expected declaration");
6105 /* If the next two tokens are both identifiers, the code is
6106 erroneous. The usual cause of this situation is code like:
6110 where "T" should name a type -- but does not. */
6111 if (cp_parser_diagnose_invalid_type_name (parser))
6113 /* If parsing tentatively, we should commit; we really are
6114 looking at a declaration. */
6115 cp_parser_commit_to_tentative_parse (parser);
6120 /* Keep going until we hit the `;' at the end of the simple
6122 saw_declarator = false;
6123 while (cp_lexer_next_token_is_not (parser->lexer,
6127 bool function_definition_p;
6130 saw_declarator = true;
6131 /* Parse the init-declarator. */
6132 decl = cp_parser_init_declarator (parser, decl_specifiers, attributes,
6133 function_definition_allowed_p,
6135 declares_class_or_enum,
6136 &function_definition_p);
6137 /* If an error occurred while parsing tentatively, exit quickly.
6138 (That usually happens when in the body of a function; each
6139 statement is treated as a declaration-statement until proven
6141 if (cp_parser_error_occurred (parser))
6143 /* Handle function definitions specially. */
6144 if (function_definition_p)
6146 /* If the next token is a `,', then we are probably
6147 processing something like:
6151 which is erroneous. */
6152 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
6153 error ("mixing declarations and function-definitions is forbidden");
6154 /* Otherwise, we're done with the list of declarators. */
6157 pop_deferring_access_checks ();
6161 /* The next token should be either a `,' or a `;'. */
6162 token = cp_lexer_peek_token (parser->lexer);
6163 /* If it's a `,', there are more declarators to come. */
6164 if (token->type == CPP_COMMA)
6165 cp_lexer_consume_token (parser->lexer);
6166 /* If it's a `;', we are done. */
6167 else if (token->type == CPP_SEMICOLON)
6169 /* Anything else is an error. */
6172 cp_parser_error (parser, "expected `,' or `;'");
6173 /* Skip tokens until we reach the end of the statement. */
6174 cp_parser_skip_to_end_of_statement (parser);
6177 /* After the first time around, a function-definition is not
6178 allowed -- even if it was OK at first. For example:
6183 function_definition_allowed_p = false;
6186 /* Issue an error message if no declarators are present, and the
6187 decl-specifier-seq does not itself declare a class or
6189 if (!saw_declarator)
6191 if (cp_parser_declares_only_class_p (parser))
6192 shadow_tag (decl_specifiers);
6193 /* Perform any deferred access checks. */
6194 perform_deferred_access_checks ();
6197 /* Consume the `;'. */
6198 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6201 pop_deferring_access_checks ();
6204 /* Parse a decl-specifier-seq.
6207 decl-specifier-seq [opt] decl-specifier
6210 storage-class-specifier
6219 decl-specifier-seq [opt] attributes
6221 Returns a TREE_LIST, giving the decl-specifiers in the order they
6222 appear in the source code. The TREE_VALUE of each node is the
6223 decl-specifier. For a keyword (such as `auto' or `friend'), the
6224 TREE_VALUE is simply the corresponding TREE_IDENTIFIER. For the
6225 representation of a type-specifier, see cp_parser_type_specifier.
6227 If there are attributes, they will be stored in *ATTRIBUTES,
6228 represented as described above cp_parser_attributes.
6230 If FRIEND_IS_NOT_CLASS_P is non-NULL, and the `friend' specifier
6231 appears, and the entity that will be a friend is not going to be a
6232 class, then *FRIEND_IS_NOT_CLASS_P will be set to TRUE. Note that
6233 even if *FRIEND_IS_NOT_CLASS_P is FALSE, the entity to which
6234 friendship is granted might not be a class.
6236 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
6239 1: one of the decl-specifiers is an elaborated-type-specifier
6240 2: one of the decl-specifiers is an enum-specifier or a
6246 cp_parser_decl_specifier_seq (cp_parser* parser,
6247 cp_parser_flags flags,
6249 int* declares_class_or_enum)
6251 tree decl_specs = NULL_TREE;
6252 bool friend_p = false;
6253 bool constructor_possible_p = !parser->in_declarator_p;
6255 /* Assume no class or enumeration type is declared. */
6256 *declares_class_or_enum = 0;
6258 /* Assume there are no attributes. */
6259 *attributes = NULL_TREE;
6261 /* Keep reading specifiers until there are no more to read. */
6264 tree decl_spec = NULL_TREE;
6268 /* Peek at the next token. */
6269 token = cp_lexer_peek_token (parser->lexer);
6270 /* Handle attributes. */
6271 if (token->keyword == RID_ATTRIBUTE)
6273 /* Parse the attributes. */
6274 decl_spec = cp_parser_attributes_opt (parser);
6275 /* Add them to the list. */
6276 *attributes = chainon (*attributes, decl_spec);
6279 /* If the next token is an appropriate keyword, we can simply
6280 add it to the list. */
6281 switch (token->keyword)
6287 /* The representation of the specifier is simply the
6288 appropriate TREE_IDENTIFIER node. */
6289 decl_spec = token->value;
6290 /* Consume the token. */
6291 cp_lexer_consume_token (parser->lexer);
6294 /* function-specifier:
6301 decl_spec = cp_parser_function_specifier_opt (parser);
6307 /* The representation of the specifier is simply the
6308 appropriate TREE_IDENTIFIER node. */
6309 decl_spec = token->value;
6310 /* Consume the token. */
6311 cp_lexer_consume_token (parser->lexer);
6312 /* A constructor declarator cannot appear in a typedef. */
6313 constructor_possible_p = false;
6314 /* The "typedef" keyword can only occur in a declaration; we
6315 may as well commit at this point. */
6316 cp_parser_commit_to_tentative_parse (parser);
6319 /* storage-class-specifier:
6334 decl_spec = cp_parser_storage_class_specifier_opt (parser);
6341 /* Constructors are a special case. The `S' in `S()' is not a
6342 decl-specifier; it is the beginning of the declarator. */
6343 constructor_p = (!decl_spec
6344 && constructor_possible_p
6345 && cp_parser_constructor_declarator_p (parser,
6348 /* If we don't have a DECL_SPEC yet, then we must be looking at
6349 a type-specifier. */
6350 if (!decl_spec && !constructor_p)
6352 int decl_spec_declares_class_or_enum;
6353 bool is_cv_qualifier;
6356 = cp_parser_type_specifier (parser, flags,
6358 /*is_declaration=*/true,
6359 &decl_spec_declares_class_or_enum,
6362 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
6364 /* If this type-specifier referenced a user-defined type
6365 (a typedef, class-name, etc.), then we can't allow any
6366 more such type-specifiers henceforth.
6370 The longest sequence of decl-specifiers that could
6371 possibly be a type name is taken as the
6372 decl-specifier-seq of a declaration. The sequence shall
6373 be self-consistent as described below.
6377 As a general rule, at most one type-specifier is allowed
6378 in the complete decl-specifier-seq of a declaration. The
6379 only exceptions are the following:
6381 -- const or volatile can be combined with any other
6384 -- signed or unsigned can be combined with char, long,
6392 void g (const int Pc);
6394 Here, Pc is *not* part of the decl-specifier seq; it's
6395 the declarator. Therefore, once we see a type-specifier
6396 (other than a cv-qualifier), we forbid any additional
6397 user-defined types. We *do* still allow things like `int
6398 int' to be considered a decl-specifier-seq, and issue the
6399 error message later. */
6400 if (decl_spec && !is_cv_qualifier)
6401 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
6402 /* A constructor declarator cannot follow a type-specifier. */
6404 constructor_possible_p = false;
6407 /* If we still do not have a DECL_SPEC, then there are no more
6411 /* Issue an error message, unless the entire construct was
6413 if (!(flags & CP_PARSER_FLAGS_OPTIONAL))
6415 cp_parser_error (parser, "expected decl specifier");
6416 return error_mark_node;
6422 /* Add the DECL_SPEC to the list of specifiers. */
6423 decl_specs = tree_cons (NULL_TREE, decl_spec, decl_specs);
6425 /* After we see one decl-specifier, further decl-specifiers are
6427 flags |= CP_PARSER_FLAGS_OPTIONAL;
6430 /* We have built up the DECL_SPECS in reverse order. Return them in
6431 the correct order. */
6432 return nreverse (decl_specs);
6435 /* Parse an (optional) storage-class-specifier.
6437 storage-class-specifier:
6446 storage-class-specifier:
6449 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
6452 cp_parser_storage_class_specifier_opt (cp_parser* parser)
6454 switch (cp_lexer_peek_token (parser->lexer)->keyword)
6462 /* Consume the token. */
6463 return cp_lexer_consume_token (parser->lexer)->value;
6470 /* Parse an (optional) function-specifier.
6477 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
6480 cp_parser_function_specifier_opt (cp_parser* parser)
6482 switch (cp_lexer_peek_token (parser->lexer)->keyword)
6487 /* Consume the token. */
6488 return cp_lexer_consume_token (parser->lexer)->value;
6495 /* Parse a linkage-specification.
6497 linkage-specification:
6498 extern string-literal { declaration-seq [opt] }
6499 extern string-literal declaration */
6502 cp_parser_linkage_specification (cp_parser* parser)
6507 /* Look for the `extern' keyword. */
6508 cp_parser_require_keyword (parser, RID_EXTERN, "`extern'");
6510 /* Peek at the next token. */
6511 token = cp_lexer_peek_token (parser->lexer);
6512 /* If it's not a string-literal, then there's a problem. */
6513 if (!cp_parser_is_string_literal (token))
6515 cp_parser_error (parser, "expected language-name");
6518 /* Consume the token. */
6519 cp_lexer_consume_token (parser->lexer);
6521 /* Transform the literal into an identifier. If the literal is a
6522 wide-character string, or contains embedded NULs, then we can't
6523 handle it as the user wants. */
6524 if (token->type == CPP_WSTRING
6525 || (strlen (TREE_STRING_POINTER (token->value))
6526 != (size_t) (TREE_STRING_LENGTH (token->value) - 1)))
6528 cp_parser_error (parser, "invalid linkage-specification");
6529 /* Assume C++ linkage. */
6530 linkage = get_identifier ("c++");
6532 /* If it's a simple string constant, things are easier. */
6534 linkage = get_identifier (TREE_STRING_POINTER (token->value));
6536 /* We're now using the new linkage. */
6537 push_lang_context (linkage);
6539 /* If the next token is a `{', then we're using the first
6541 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6543 /* Consume the `{' token. */
6544 cp_lexer_consume_token (parser->lexer);
6545 /* Parse the declarations. */
6546 cp_parser_declaration_seq_opt (parser);
6547 /* Look for the closing `}'. */
6548 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
6550 /* Otherwise, there's just one declaration. */
6553 bool saved_in_unbraced_linkage_specification_p;
6555 saved_in_unbraced_linkage_specification_p
6556 = parser->in_unbraced_linkage_specification_p;
6557 parser->in_unbraced_linkage_specification_p = true;
6558 have_extern_spec = true;
6559 cp_parser_declaration (parser);
6560 have_extern_spec = false;
6561 parser->in_unbraced_linkage_specification_p
6562 = saved_in_unbraced_linkage_specification_p;
6565 /* We're done with the linkage-specification. */
6566 pop_lang_context ();
6569 /* Special member functions [gram.special] */
6571 /* Parse a conversion-function-id.
6573 conversion-function-id:
6574 operator conversion-type-id
6576 Returns an IDENTIFIER_NODE representing the operator. */
6579 cp_parser_conversion_function_id (cp_parser* parser)
6583 tree saved_qualifying_scope;
6584 tree saved_object_scope;
6586 /* Look for the `operator' token. */
6587 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
6588 return error_mark_node;
6589 /* When we parse the conversion-type-id, the current scope will be
6590 reset. However, we need that information in able to look up the
6591 conversion function later, so we save it here. */
6592 saved_scope = parser->scope;
6593 saved_qualifying_scope = parser->qualifying_scope;
6594 saved_object_scope = parser->object_scope;
6595 /* We must enter the scope of the class so that the names of
6596 entities declared within the class are available in the
6597 conversion-type-id. For example, consider:
6604 S::operator I() { ... }
6606 In order to see that `I' is a type-name in the definition, we
6607 must be in the scope of `S'. */
6609 push_scope (saved_scope);
6610 /* Parse the conversion-type-id. */
6611 type = cp_parser_conversion_type_id (parser);
6612 /* Leave the scope of the class, if any. */
6614 pop_scope (saved_scope);
6615 /* Restore the saved scope. */
6616 parser->scope = saved_scope;
6617 parser->qualifying_scope = saved_qualifying_scope;
6618 parser->object_scope = saved_object_scope;
6619 /* If the TYPE is invalid, indicate failure. */
6620 if (type == error_mark_node)
6621 return error_mark_node;
6622 return mangle_conv_op_name_for_type (type);
6625 /* Parse a conversion-type-id:
6628 type-specifier-seq conversion-declarator [opt]
6630 Returns the TYPE specified. */
6633 cp_parser_conversion_type_id (cp_parser* parser)
6636 tree type_specifiers;
6639 /* Parse the attributes. */
6640 attributes = cp_parser_attributes_opt (parser);
6641 /* Parse the type-specifiers. */
6642 type_specifiers = cp_parser_type_specifier_seq (parser);
6643 /* If that didn't work, stop. */
6644 if (type_specifiers == error_mark_node)
6645 return error_mark_node;
6646 /* Parse the conversion-declarator. */
6647 declarator = cp_parser_conversion_declarator_opt (parser);
6649 return grokdeclarator (declarator, type_specifiers, TYPENAME,
6650 /*initialized=*/0, &attributes);
6653 /* Parse an (optional) conversion-declarator.
6655 conversion-declarator:
6656 ptr-operator conversion-declarator [opt]
6658 Returns a representation of the declarator. See
6659 cp_parser_declarator for details. */
6662 cp_parser_conversion_declarator_opt (cp_parser* parser)
6664 enum tree_code code;
6666 tree cv_qualifier_seq;
6668 /* We don't know if there's a ptr-operator next, or not. */
6669 cp_parser_parse_tentatively (parser);
6670 /* Try the ptr-operator. */
6671 code = cp_parser_ptr_operator (parser, &class_type,
6673 /* If it worked, look for more conversion-declarators. */
6674 if (cp_parser_parse_definitely (parser))
6678 /* Parse another optional declarator. */
6679 declarator = cp_parser_conversion_declarator_opt (parser);
6681 /* Create the representation of the declarator. */
6682 if (code == INDIRECT_REF)
6683 declarator = make_pointer_declarator (cv_qualifier_seq,
6686 declarator = make_reference_declarator (cv_qualifier_seq,
6689 /* Handle the pointer-to-member case. */
6691 declarator = build_nt (SCOPE_REF, class_type, declarator);
6699 /* Parse an (optional) ctor-initializer.
6702 : mem-initializer-list
6704 Returns TRUE iff the ctor-initializer was actually present. */
6707 cp_parser_ctor_initializer_opt (cp_parser* parser)
6709 /* If the next token is not a `:', then there is no
6710 ctor-initializer. */
6711 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
6713 /* Do default initialization of any bases and members. */
6714 if (DECL_CONSTRUCTOR_P (current_function_decl))
6715 finish_mem_initializers (NULL_TREE);
6720 /* Consume the `:' token. */
6721 cp_lexer_consume_token (parser->lexer);
6722 /* And the mem-initializer-list. */
6723 cp_parser_mem_initializer_list (parser);
6728 /* Parse a mem-initializer-list.
6730 mem-initializer-list:
6732 mem-initializer , mem-initializer-list */
6735 cp_parser_mem_initializer_list (cp_parser* parser)
6737 tree mem_initializer_list = NULL_TREE;
6739 /* Let the semantic analysis code know that we are starting the
6740 mem-initializer-list. */
6741 if (!DECL_CONSTRUCTOR_P (current_function_decl))
6742 error ("only constructors take base initializers");
6744 /* Loop through the list. */
6747 tree mem_initializer;
6749 /* Parse the mem-initializer. */
6750 mem_initializer = cp_parser_mem_initializer (parser);
6751 /* Add it to the list, unless it was erroneous. */
6752 if (mem_initializer)
6754 TREE_CHAIN (mem_initializer) = mem_initializer_list;
6755 mem_initializer_list = mem_initializer;
6757 /* If the next token is not a `,', we're done. */
6758 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
6760 /* Consume the `,' token. */
6761 cp_lexer_consume_token (parser->lexer);
6764 /* Perform semantic analysis. */
6765 if (DECL_CONSTRUCTOR_P (current_function_decl))
6766 finish_mem_initializers (mem_initializer_list);
6769 /* Parse a mem-initializer.
6772 mem-initializer-id ( expression-list [opt] )
6777 ( expression-list [opt] )
6779 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
6780 class) or FIELD_DECL (for a non-static data member) to initialize;
6781 the TREE_VALUE is the expression-list. */
6784 cp_parser_mem_initializer (cp_parser* parser)
6786 tree mem_initializer_id;
6787 tree expression_list;
6790 /* Find out what is being initialized. */
6791 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6793 pedwarn ("anachronistic old-style base class initializer");
6794 mem_initializer_id = NULL_TREE;
6797 mem_initializer_id = cp_parser_mem_initializer_id (parser);
6798 member = expand_member_init (mem_initializer_id);
6799 if (member && !DECL_P (member))
6800 in_base_initializer = 1;
6803 = cp_parser_parenthesized_expression_list (parser, false,
6804 /*non_constant_p=*/NULL);
6805 if (!expression_list)
6806 expression_list = void_type_node;
6808 in_base_initializer = 0;
6810 return member ? build_tree_list (member, expression_list) : NULL_TREE;
6813 /* Parse a mem-initializer-id.
6816 :: [opt] nested-name-specifier [opt] class-name
6819 Returns a TYPE indicating the class to be initializer for the first
6820 production. Returns an IDENTIFIER_NODE indicating the data member
6821 to be initialized for the second production. */
6824 cp_parser_mem_initializer_id (cp_parser* parser)
6826 bool global_scope_p;
6827 bool nested_name_specifier_p;
6830 /* Look for the optional `::' operator. */
6832 = (cp_parser_global_scope_opt (parser,
6833 /*current_scope_valid_p=*/false)
6835 /* Look for the optional nested-name-specifier. The simplest way to
6840 The keyword `typename' is not permitted in a base-specifier or
6841 mem-initializer; in these contexts a qualified name that
6842 depends on a template-parameter is implicitly assumed to be a
6845 is to assume that we have seen the `typename' keyword at this
6847 nested_name_specifier_p
6848 = (cp_parser_nested_name_specifier_opt (parser,
6849 /*typename_keyword_p=*/true,
6850 /*check_dependency_p=*/true,
6853 /* If there is a `::' operator or a nested-name-specifier, then we
6854 are definitely looking for a class-name. */
6855 if (global_scope_p || nested_name_specifier_p)
6856 return cp_parser_class_name (parser,
6857 /*typename_keyword_p=*/true,
6858 /*template_keyword_p=*/false,
6860 /*check_dependency_p=*/true,
6861 /*class_head_p=*/false);
6862 /* Otherwise, we could also be looking for an ordinary identifier. */
6863 cp_parser_parse_tentatively (parser);
6864 /* Try a class-name. */
6865 id = cp_parser_class_name (parser,
6866 /*typename_keyword_p=*/true,
6867 /*template_keyword_p=*/false,
6869 /*check_dependency_p=*/true,
6870 /*class_head_p=*/false);
6871 /* If we found one, we're done. */
6872 if (cp_parser_parse_definitely (parser))
6874 /* Otherwise, look for an ordinary identifier. */
6875 return cp_parser_identifier (parser);
6878 /* Overloading [gram.over] */
6880 /* Parse an operator-function-id.
6882 operator-function-id:
6885 Returns an IDENTIFIER_NODE for the operator which is a
6886 human-readable spelling of the identifier, e.g., `operator +'. */
6889 cp_parser_operator_function_id (cp_parser* parser)
6891 /* Look for the `operator' keyword. */
6892 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
6893 return error_mark_node;
6894 /* And then the name of the operator itself. */
6895 return cp_parser_operator (parser);
6898 /* Parse an operator.
6901 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
6902 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
6903 || ++ -- , ->* -> () []
6910 Returns an IDENTIFIER_NODE for the operator which is a
6911 human-readable spelling of the identifier, e.g., `operator +'. */
6914 cp_parser_operator (cp_parser* parser)
6916 tree id = NULL_TREE;
6919 /* Peek at the next token. */
6920 token = cp_lexer_peek_token (parser->lexer);
6921 /* Figure out which operator we have. */
6922 switch (token->type)
6928 /* The keyword should be either `new' or `delete'. */
6929 if (token->keyword == RID_NEW)
6931 else if (token->keyword == RID_DELETE)
6936 /* Consume the `new' or `delete' token. */
6937 cp_lexer_consume_token (parser->lexer);
6939 /* Peek at the next token. */
6940 token = cp_lexer_peek_token (parser->lexer);
6941 /* If it's a `[' token then this is the array variant of the
6943 if (token->type == CPP_OPEN_SQUARE)
6945 /* Consume the `[' token. */
6946 cp_lexer_consume_token (parser->lexer);
6947 /* Look for the `]' token. */
6948 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
6949 id = ansi_opname (op == NEW_EXPR
6950 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
6952 /* Otherwise, we have the non-array variant. */
6954 id = ansi_opname (op);
6960 id = ansi_opname (PLUS_EXPR);
6964 id = ansi_opname (MINUS_EXPR);
6968 id = ansi_opname (MULT_EXPR);
6972 id = ansi_opname (TRUNC_DIV_EXPR);
6976 id = ansi_opname (TRUNC_MOD_EXPR);
6980 id = ansi_opname (BIT_XOR_EXPR);
6984 id = ansi_opname (BIT_AND_EXPR);
6988 id = ansi_opname (BIT_IOR_EXPR);
6992 id = ansi_opname (BIT_NOT_EXPR);
6996 id = ansi_opname (TRUTH_NOT_EXPR);
7000 id = ansi_assopname (NOP_EXPR);
7004 id = ansi_opname (LT_EXPR);
7008 id = ansi_opname (GT_EXPR);
7012 id = ansi_assopname (PLUS_EXPR);
7016 id = ansi_assopname (MINUS_EXPR);
7020 id = ansi_assopname (MULT_EXPR);
7024 id = ansi_assopname (TRUNC_DIV_EXPR);
7028 id = ansi_assopname (TRUNC_MOD_EXPR);
7032 id = ansi_assopname (BIT_XOR_EXPR);
7036 id = ansi_assopname (BIT_AND_EXPR);
7040 id = ansi_assopname (BIT_IOR_EXPR);
7044 id = ansi_opname (LSHIFT_EXPR);
7048 id = ansi_opname (RSHIFT_EXPR);
7052 id = ansi_assopname (LSHIFT_EXPR);
7056 id = ansi_assopname (RSHIFT_EXPR);
7060 id = ansi_opname (EQ_EXPR);
7064 id = ansi_opname (NE_EXPR);
7068 id = ansi_opname (LE_EXPR);
7071 case CPP_GREATER_EQ:
7072 id = ansi_opname (GE_EXPR);
7076 id = ansi_opname (TRUTH_ANDIF_EXPR);
7080 id = ansi_opname (TRUTH_ORIF_EXPR);
7084 id = ansi_opname (POSTINCREMENT_EXPR);
7087 case CPP_MINUS_MINUS:
7088 id = ansi_opname (PREDECREMENT_EXPR);
7092 id = ansi_opname (COMPOUND_EXPR);
7095 case CPP_DEREF_STAR:
7096 id = ansi_opname (MEMBER_REF);
7100 id = ansi_opname (COMPONENT_REF);
7103 case CPP_OPEN_PAREN:
7104 /* Consume the `('. */
7105 cp_lexer_consume_token (parser->lexer);
7106 /* Look for the matching `)'. */
7107 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
7108 return ansi_opname (CALL_EXPR);
7110 case CPP_OPEN_SQUARE:
7111 /* Consume the `['. */
7112 cp_lexer_consume_token (parser->lexer);
7113 /* Look for the matching `]'. */
7114 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
7115 return ansi_opname (ARRAY_REF);
7119 id = ansi_opname (MIN_EXPR);
7123 id = ansi_opname (MAX_EXPR);
7127 id = ansi_assopname (MIN_EXPR);
7131 id = ansi_assopname (MAX_EXPR);
7135 /* Anything else is an error. */
7139 /* If we have selected an identifier, we need to consume the
7142 cp_lexer_consume_token (parser->lexer);
7143 /* Otherwise, no valid operator name was present. */
7146 cp_parser_error (parser, "expected operator");
7147 id = error_mark_node;
7153 /* Parse a template-declaration.
7155 template-declaration:
7156 export [opt] template < template-parameter-list > declaration
7158 If MEMBER_P is TRUE, this template-declaration occurs within a
7161 The grammar rule given by the standard isn't correct. What
7164 template-declaration:
7165 export [opt] template-parameter-list-seq
7166 decl-specifier-seq [opt] init-declarator [opt] ;
7167 export [opt] template-parameter-list-seq
7170 template-parameter-list-seq:
7171 template-parameter-list-seq [opt]
7172 template < template-parameter-list > */
7175 cp_parser_template_declaration (cp_parser* parser, bool member_p)
7177 /* Check for `export'. */
7178 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
7180 /* Consume the `export' token. */
7181 cp_lexer_consume_token (parser->lexer);
7182 /* Warn that we do not support `export'. */
7183 warning ("keyword `export' not implemented, and will be ignored");
7186 cp_parser_template_declaration_after_export (parser, member_p);
7189 /* Parse a template-parameter-list.
7191 template-parameter-list:
7193 template-parameter-list , template-parameter
7195 Returns a TREE_LIST. Each node represents a template parameter.
7196 The nodes are connected via their TREE_CHAINs. */
7199 cp_parser_template_parameter_list (cp_parser* parser)
7201 tree parameter_list = NULL_TREE;
7208 /* Parse the template-parameter. */
7209 parameter = cp_parser_template_parameter (parser);
7210 /* Add it to the list. */
7211 parameter_list = process_template_parm (parameter_list,
7214 /* Peek at the next token. */
7215 token = cp_lexer_peek_token (parser->lexer);
7216 /* If it's not a `,', we're done. */
7217 if (token->type != CPP_COMMA)
7219 /* Otherwise, consume the `,' token. */
7220 cp_lexer_consume_token (parser->lexer);
7223 return parameter_list;
7226 /* Parse a template-parameter.
7230 parameter-declaration
7232 Returns a TREE_LIST. The TREE_VALUE represents the parameter. The
7233 TREE_PURPOSE is the default value, if any. */
7236 cp_parser_template_parameter (cp_parser* parser)
7240 /* Peek at the next token. */
7241 token = cp_lexer_peek_token (parser->lexer);
7242 /* If it is `class' or `template', we have a type-parameter. */
7243 if (token->keyword == RID_TEMPLATE)
7244 return cp_parser_type_parameter (parser);
7245 /* If it is `class' or `typename' we do not know yet whether it is a
7246 type parameter or a non-type parameter. Consider:
7248 template <typename T, typename T::X X> ...
7252 template <class C, class D*> ...
7254 Here, the first parameter is a type parameter, and the second is
7255 a non-type parameter. We can tell by looking at the token after
7256 the identifier -- if it is a `,', `=', or `>' then we have a type
7258 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
7260 /* Peek at the token after `class' or `typename'. */
7261 token = cp_lexer_peek_nth_token (parser->lexer, 2);
7262 /* If it's an identifier, skip it. */
7263 if (token->type == CPP_NAME)
7264 token = cp_lexer_peek_nth_token (parser->lexer, 3);
7265 /* Now, see if the token looks like the end of a template
7267 if (token->type == CPP_COMMA
7268 || token->type == CPP_EQ
7269 || token->type == CPP_GREATER)
7270 return cp_parser_type_parameter (parser);
7273 /* Otherwise, it is a non-type parameter.
7277 When parsing a default template-argument for a non-type
7278 template-parameter, the first non-nested `>' is taken as the end
7279 of the template parameter-list rather than a greater-than
7282 cp_parser_parameter_declaration (parser, /*template_parm_p=*/true);
7285 /* Parse a type-parameter.
7288 class identifier [opt]
7289 class identifier [opt] = type-id
7290 typename identifier [opt]
7291 typename identifier [opt] = type-id
7292 template < template-parameter-list > class identifier [opt]
7293 template < template-parameter-list > class identifier [opt]
7296 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
7297 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
7298 the declaration of the parameter. */
7301 cp_parser_type_parameter (cp_parser* parser)
7306 /* Look for a keyword to tell us what kind of parameter this is. */
7307 token = cp_parser_require (parser, CPP_KEYWORD,
7308 "`class', `typename', or `template'");
7310 return error_mark_node;
7312 switch (token->keyword)
7318 tree default_argument;
7320 /* If the next token is an identifier, then it names the
7322 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
7323 identifier = cp_parser_identifier (parser);
7325 identifier = NULL_TREE;
7327 /* Create the parameter. */
7328 parameter = finish_template_type_parm (class_type_node, identifier);
7330 /* If the next token is an `=', we have a default argument. */
7331 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7333 /* Consume the `=' token. */
7334 cp_lexer_consume_token (parser->lexer);
7335 /* Parse the default-argument. */
7336 default_argument = cp_parser_type_id (parser);
7339 default_argument = NULL_TREE;
7341 /* Create the combined representation of the parameter and the
7342 default argument. */
7343 parameter = build_tree_list (default_argument, parameter);
7349 tree parameter_list;
7351 tree default_argument;
7353 /* Look for the `<'. */
7354 cp_parser_require (parser, CPP_LESS, "`<'");
7355 /* Parse the template-parameter-list. */
7356 begin_template_parm_list ();
7358 = cp_parser_template_parameter_list (parser);
7359 parameter_list = end_template_parm_list (parameter_list);
7360 /* Look for the `>'. */
7361 cp_parser_require (parser, CPP_GREATER, "`>'");
7362 /* Look for the `class' keyword. */
7363 cp_parser_require_keyword (parser, RID_CLASS, "`class'");
7364 /* If the next token is an `=', then there is a
7365 default-argument. If the next token is a `>', we are at
7366 the end of the parameter-list. If the next token is a `,',
7367 then we are at the end of this parameter. */
7368 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
7369 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
7370 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7371 identifier = cp_parser_identifier (parser);
7373 identifier = NULL_TREE;
7374 /* Create the template parameter. */
7375 parameter = finish_template_template_parm (class_type_node,
7378 /* If the next token is an `=', then there is a
7379 default-argument. */
7380 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7382 /* Consume the `='. */
7383 cp_lexer_consume_token (parser->lexer);
7384 /* Parse the id-expression. */
7386 = cp_parser_id_expression (parser,
7387 /*template_keyword_p=*/false,
7388 /*check_dependency_p=*/true,
7389 /*template_p=*/NULL);
7390 /* Look up the name. */
7392 = cp_parser_lookup_name_simple (parser, default_argument);
7393 /* See if the default argument is valid. */
7395 = check_template_template_default_arg (default_argument);
7398 default_argument = NULL_TREE;
7400 /* Create the combined representation of the parameter and the
7401 default argument. */
7402 parameter = build_tree_list (default_argument, parameter);
7407 /* Anything else is an error. */
7408 cp_parser_error (parser,
7409 "expected `class', `typename', or `template'");
7410 parameter = error_mark_node;
7416 /* Parse a template-id.
7419 template-name < template-argument-list [opt] >
7421 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
7422 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
7423 returned. Otherwise, if the template-name names a function, or set
7424 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
7425 names a class, returns a TYPE_DECL for the specialization.
7427 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
7428 uninstantiated templates. */
7431 cp_parser_template_id (cp_parser *parser,
7432 bool template_keyword_p,
7433 bool check_dependency_p)
7438 tree saved_qualifying_scope;
7439 tree saved_object_scope;
7441 bool saved_greater_than_is_operator_p;
7442 ptrdiff_t start_of_id;
7443 tree access_check = NULL_TREE;
7444 cp_token *next_token;
7446 /* If the next token corresponds to a template-id, there is no need
7448 next_token = cp_lexer_peek_token (parser->lexer);
7449 if (next_token->type == CPP_TEMPLATE_ID)
7454 /* Get the stored value. */
7455 value = cp_lexer_consume_token (parser->lexer)->value;
7456 /* Perform any access checks that were deferred. */
7457 for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
7458 perform_or_defer_access_check (TREE_PURPOSE (check),
7459 TREE_VALUE (check));
7460 /* Return the stored value. */
7461 return TREE_VALUE (value);
7464 /* Avoid performing name lookup if there is no possibility of
7465 finding a template-id. */
7466 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
7467 || (next_token->type == CPP_NAME
7468 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_LESS))
7470 cp_parser_error (parser, "expected template-id");
7471 return error_mark_node;
7474 /* Remember where the template-id starts. */
7475 if (cp_parser_parsing_tentatively (parser)
7476 && !cp_parser_committed_to_tentative_parse (parser))
7478 next_token = cp_lexer_peek_token (parser->lexer);
7479 start_of_id = cp_lexer_token_difference (parser->lexer,
7480 parser->lexer->first_token,
7486 push_deferring_access_checks (dk_deferred);
7488 /* Parse the template-name. */
7489 template = cp_parser_template_name (parser, template_keyword_p,
7490 check_dependency_p);
7491 if (template == error_mark_node)
7493 pop_deferring_access_checks ();
7494 return error_mark_node;
7497 /* Look for the `<' that starts the template-argument-list. */
7498 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
7500 pop_deferring_access_checks ();
7501 return error_mark_node;
7506 When parsing a template-id, the first non-nested `>' is taken as
7507 the end of the template-argument-list rather than a greater-than
7509 saved_greater_than_is_operator_p
7510 = parser->greater_than_is_operator_p;
7511 parser->greater_than_is_operator_p = false;
7512 /* Parsing the argument list may modify SCOPE, so we save it
7514 saved_scope = parser->scope;
7515 saved_qualifying_scope = parser->qualifying_scope;
7516 saved_object_scope = parser->object_scope;
7517 /* Parse the template-argument-list itself. */
7518 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
7519 arguments = NULL_TREE;
7521 arguments = cp_parser_template_argument_list (parser);
7522 /* Look for the `>' that ends the template-argument-list. */
7523 cp_parser_require (parser, CPP_GREATER, "`>'");
7524 /* The `>' token might be a greater-than operator again now. */
7525 parser->greater_than_is_operator_p
7526 = saved_greater_than_is_operator_p;
7527 /* Restore the SAVED_SCOPE. */
7528 parser->scope = saved_scope;
7529 parser->qualifying_scope = saved_qualifying_scope;
7530 parser->object_scope = saved_object_scope;
7532 /* Build a representation of the specialization. */
7533 if (TREE_CODE (template) == IDENTIFIER_NODE)
7534 template_id = build_min_nt (TEMPLATE_ID_EXPR, template, arguments);
7535 else if (DECL_CLASS_TEMPLATE_P (template)
7536 || DECL_TEMPLATE_TEMPLATE_PARM_P (template))
7538 = finish_template_type (template, arguments,
7539 cp_lexer_next_token_is (parser->lexer,
7543 /* If it's not a class-template or a template-template, it should be
7544 a function-template. */
7545 my_friendly_assert ((DECL_FUNCTION_TEMPLATE_P (template)
7546 || TREE_CODE (template) == OVERLOAD
7547 || BASELINK_P (template)),
7550 template_id = lookup_template_function (template, arguments);
7553 /* Retrieve any deferred checks. Do not pop this access checks yet
7554 so the memory will not be reclaimed during token replacing below. */
7555 access_check = get_deferred_access_checks ();
7557 /* If parsing tentatively, replace the sequence of tokens that makes
7558 up the template-id with a CPP_TEMPLATE_ID token. That way,
7559 should we re-parse the token stream, we will not have to repeat
7560 the effort required to do the parse, nor will we issue duplicate
7561 error messages about problems during instantiation of the
7563 if (start_of_id >= 0)
7567 /* Find the token that corresponds to the start of the
7569 token = cp_lexer_advance_token (parser->lexer,
7570 parser->lexer->first_token,
7573 /* Reset the contents of the START_OF_ID token. */
7574 token->type = CPP_TEMPLATE_ID;
7575 token->value = build_tree_list (access_check, template_id);
7576 token->keyword = RID_MAX;
7577 /* Purge all subsequent tokens. */
7578 cp_lexer_purge_tokens_after (parser->lexer, token);
7581 pop_deferring_access_checks ();
7585 /* Parse a template-name.
7590 The standard should actually say:
7594 operator-function-id
7595 conversion-function-id
7597 A defect report has been filed about this issue.
7599 If TEMPLATE_KEYWORD_P is true, then we have just seen the
7600 `template' keyword, in a construction like:
7604 In that case `f' is taken to be a template-name, even though there
7605 is no way of knowing for sure.
7607 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
7608 name refers to a set of overloaded functions, at least one of which
7609 is a template, or an IDENTIFIER_NODE with the name of the template,
7610 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
7611 names are looked up inside uninstantiated templates. */
7614 cp_parser_template_name (cp_parser* parser,
7615 bool template_keyword_p,
7616 bool check_dependency_p)
7622 /* If the next token is `operator', then we have either an
7623 operator-function-id or a conversion-function-id. */
7624 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
7626 /* We don't know whether we're looking at an
7627 operator-function-id or a conversion-function-id. */
7628 cp_parser_parse_tentatively (parser);
7629 /* Try an operator-function-id. */
7630 identifier = cp_parser_operator_function_id (parser);
7631 /* If that didn't work, try a conversion-function-id. */
7632 if (!cp_parser_parse_definitely (parser))
7633 identifier = cp_parser_conversion_function_id (parser);
7635 /* Look for the identifier. */
7637 identifier = cp_parser_identifier (parser);
7639 /* If we didn't find an identifier, we don't have a template-id. */
7640 if (identifier == error_mark_node)
7641 return error_mark_node;
7643 /* If the name immediately followed the `template' keyword, then it
7644 is a template-name. However, if the next token is not `<', then
7645 we do not treat it as a template-name, since it is not being used
7646 as part of a template-id. This enables us to handle constructs
7649 template <typename T> struct S { S(); };
7650 template <typename T> S<T>::S();
7652 correctly. We would treat `S' as a template -- if it were `S<T>'
7653 -- but we do not if there is no `<'. */
7654 if (template_keyword_p && processing_template_decl
7655 && cp_lexer_next_token_is (parser->lexer, CPP_LESS))
7658 /* Look up the name. */
7659 decl = cp_parser_lookup_name (parser, identifier,
7661 /*is_namespace=*/false,
7662 check_dependency_p);
7663 decl = maybe_get_template_decl_from_type_decl (decl);
7665 /* If DECL is a template, then the name was a template-name. */
7666 if (TREE_CODE (decl) == TEMPLATE_DECL)
7670 /* The standard does not explicitly indicate whether a name that
7671 names a set of overloaded declarations, some of which are
7672 templates, is a template-name. However, such a name should
7673 be a template-name; otherwise, there is no way to form a
7674 template-id for the overloaded templates. */
7675 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
7676 if (TREE_CODE (fns) == OVERLOAD)
7680 for (fn = fns; fn; fn = OVL_NEXT (fn))
7681 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
7686 /* Otherwise, the name does not name a template. */
7687 cp_parser_error (parser, "expected template-name");
7688 return error_mark_node;
7692 /* If DECL is dependent, and refers to a function, then just return
7693 its name; we will look it up again during template instantiation. */
7694 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
7696 tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
7697 if (TYPE_P (scope) && dependent_type_p (scope))
7704 /* Parse a template-argument-list.
7706 template-argument-list:
7708 template-argument-list , template-argument
7710 Returns a TREE_VEC containing the arguments. */
7713 cp_parser_template_argument_list (cp_parser* parser)
7715 tree fixed_args[10];
7716 unsigned n_args = 0;
7717 unsigned alloced = 10;
7718 tree *arg_ary = fixed_args;
7726 /* Consume the comma. */
7727 cp_lexer_consume_token (parser->lexer);
7729 /* Parse the template-argument. */
7730 argument = cp_parser_template_argument (parser);
7731 if (n_args == alloced)
7735 if (arg_ary == fixed_args)
7737 arg_ary = xmalloc (sizeof (tree) * alloced);
7738 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
7741 arg_ary = xrealloc (arg_ary, sizeof (tree) * alloced);
7743 arg_ary[n_args++] = argument;
7745 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
7747 vec = make_tree_vec (n_args);
7750 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
7752 if (arg_ary != fixed_args)
7757 /* Parse a template-argument.
7760 assignment-expression
7764 The representation is that of an assignment-expression, type-id, or
7765 id-expression -- except that the qualified id-expression is
7766 evaluated, so that the value returned is either a DECL or an
7769 Although the standard says "assignment-expression", it forbids
7770 throw-expressions or assignments in the template argument.
7771 Therefore, we use "conditional-expression" instead. */
7774 cp_parser_template_argument (cp_parser* parser)
7781 tree qualifying_class;
7783 /* There's really no way to know what we're looking at, so we just
7784 try each alternative in order.
7788 In a template-argument, an ambiguity between a type-id and an
7789 expression is resolved to a type-id, regardless of the form of
7790 the corresponding template-parameter.
7792 Therefore, we try a type-id first. */
7793 cp_parser_parse_tentatively (parser);
7794 argument = cp_parser_type_id (parser);
7795 /* If the next token isn't a `,' or a `>', then this argument wasn't
7797 if (!cp_parser_next_token_ends_template_argument_p (parser))
7798 cp_parser_error (parser, "expected template-argument");
7799 /* If that worked, we're done. */
7800 if (cp_parser_parse_definitely (parser))
7802 /* We're still not sure what the argument will be. */
7803 cp_parser_parse_tentatively (parser);
7804 /* Try a template. */
7805 argument = cp_parser_id_expression (parser,
7806 /*template_keyword_p=*/false,
7807 /*check_dependency_p=*/true,
7809 /* If the next token isn't a `,' or a `>', then this argument wasn't
7811 if (!cp_parser_next_token_ends_template_argument_p (parser))
7812 cp_parser_error (parser, "expected template-argument");
7813 if (!cp_parser_error_occurred (parser))
7815 /* Figure out what is being referred to. */
7816 argument = cp_parser_lookup_name_simple (parser, argument);
7818 argument = make_unbound_class_template (TREE_OPERAND (argument, 0),
7819 TREE_OPERAND (argument, 1),
7821 else if (TREE_CODE (argument) != TEMPLATE_DECL)
7822 cp_parser_error (parser, "expected template-name");
7824 if (cp_parser_parse_definitely (parser))
7826 /* It must be a non-type argument. There permitted cases are given
7827 in [temp.arg.nontype]:
7829 -- an integral constant-expression of integral or enumeration
7832 -- the name of a non-type template-parameter; or
7834 -- the name of an object or function with external linkage...
7836 -- the address of an object or function with external linkage...
7838 -- a pointer to member... */
7839 /* Look for a non-type template parameter. */
7840 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
7842 cp_parser_parse_tentatively (parser);
7843 argument = cp_parser_primary_expression (parser,
7846 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
7847 || !cp_parser_next_token_ends_template_argument_p (parser))
7848 cp_parser_simulate_error (parser);
7849 if (cp_parser_parse_definitely (parser))
7852 /* If the next token is "&", the argument must be the address of an
7853 object or function with external linkage. */
7854 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
7856 cp_lexer_consume_token (parser->lexer);
7857 /* See if we might have an id-expression. */
7858 token = cp_lexer_peek_token (parser->lexer);
7859 if (token->type == CPP_NAME
7860 || token->keyword == RID_OPERATOR
7861 || token->type == CPP_SCOPE
7862 || token->type == CPP_TEMPLATE_ID
7863 || token->type == CPP_NESTED_NAME_SPECIFIER)
7865 cp_parser_parse_tentatively (parser);
7866 argument = cp_parser_primary_expression (parser,
7869 if (cp_parser_error_occurred (parser)
7870 || !cp_parser_next_token_ends_template_argument_p (parser))
7871 cp_parser_abort_tentative_parse (parser);
7874 if (qualifying_class)
7875 argument = finish_qualified_id_expr (qualifying_class,
7879 if (TREE_CODE (argument) == VAR_DECL)
7881 /* A variable without external linkage might still be a
7882 valid constant-expression, so no error is issued here
7883 if the external-linkage check fails. */
7884 if (!DECL_EXTERNAL_LINKAGE_P (argument))
7885 cp_parser_simulate_error (parser);
7887 else if (is_overloaded_fn (argument))
7888 /* All overloaded functions are allowed; if the external
7889 linkage test does not pass, an error will be issued
7893 && (TREE_CODE (argument) == OFFSET_REF
7894 || TREE_CODE (argument) == SCOPE_REF))
7895 /* A pointer-to-member. */
7898 cp_parser_simulate_error (parser);
7900 if (cp_parser_parse_definitely (parser))
7903 argument = build_x_unary_op (ADDR_EXPR, argument);
7908 /* If the argument started with "&", there are no other valid
7909 alternatives at this point. */
7912 cp_parser_error (parser, "invalid non-type template argument");
7913 return error_mark_node;
7915 /* The argument must be a constant-expression. */
7916 argument = cp_parser_constant_expression (parser,
7917 /*allow_non_constant_p=*/false,
7918 /*non_constant_p=*/NULL);
7919 /* If it's non-dependent, simplify it. */
7920 return cp_parser_fold_non_dependent_expr (argument);
7923 /* Parse an explicit-instantiation.
7925 explicit-instantiation:
7926 template declaration
7928 Although the standard says `declaration', what it really means is:
7930 explicit-instantiation:
7931 template decl-specifier-seq [opt] declarator [opt] ;
7933 Things like `template int S<int>::i = 5, int S<double>::j;' are not
7934 supposed to be allowed. A defect report has been filed about this
7939 explicit-instantiation:
7940 storage-class-specifier template
7941 decl-specifier-seq [opt] declarator [opt] ;
7942 function-specifier template
7943 decl-specifier-seq [opt] declarator [opt] ; */
7946 cp_parser_explicit_instantiation (cp_parser* parser)
7948 int declares_class_or_enum;
7949 tree decl_specifiers;
7951 tree extension_specifier = NULL_TREE;
7953 /* Look for an (optional) storage-class-specifier or
7954 function-specifier. */
7955 if (cp_parser_allow_gnu_extensions_p (parser))
7958 = cp_parser_storage_class_specifier_opt (parser);
7959 if (!extension_specifier)
7960 extension_specifier = cp_parser_function_specifier_opt (parser);
7963 /* Look for the `template' keyword. */
7964 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
7965 /* Let the front end know that we are processing an explicit
7967 begin_explicit_instantiation ();
7968 /* [temp.explicit] says that we are supposed to ignore access
7969 control while processing explicit instantiation directives. */
7970 push_deferring_access_checks (dk_no_check);
7971 /* Parse a decl-specifier-seq. */
7973 = cp_parser_decl_specifier_seq (parser,
7974 CP_PARSER_FLAGS_OPTIONAL,
7976 &declares_class_or_enum);
7977 /* If there was exactly one decl-specifier, and it declared a class,
7978 and there's no declarator, then we have an explicit type
7980 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
7984 type = check_tag_decl (decl_specifiers);
7985 /* Turn access control back on for names used during
7986 template instantiation. */
7987 pop_deferring_access_checks ();
7989 do_type_instantiation (type, extension_specifier, /*complain=*/1);
7996 /* Parse the declarator. */
7998 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
7999 /*ctor_dtor_or_conv_p=*/NULL);
8000 cp_parser_check_for_definition_in_return_type (declarator,
8001 declares_class_or_enum);
8002 decl = grokdeclarator (declarator, decl_specifiers,
8004 /* Turn access control back on for names used during
8005 template instantiation. */
8006 pop_deferring_access_checks ();
8007 /* Do the explicit instantiation. */
8008 do_decl_instantiation (decl, extension_specifier);
8010 /* We're done with the instantiation. */
8011 end_explicit_instantiation ();
8013 cp_parser_consume_semicolon_at_end_of_statement (parser);
8016 /* Parse an explicit-specialization.
8018 explicit-specialization:
8019 template < > declaration
8021 Although the standard says `declaration', what it really means is:
8023 explicit-specialization:
8024 template <> decl-specifier [opt] init-declarator [opt] ;
8025 template <> function-definition
8026 template <> explicit-specialization
8027 template <> template-declaration */
8030 cp_parser_explicit_specialization (cp_parser* parser)
8032 /* Look for the `template' keyword. */
8033 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
8034 /* Look for the `<'. */
8035 cp_parser_require (parser, CPP_LESS, "`<'");
8036 /* Look for the `>'. */
8037 cp_parser_require (parser, CPP_GREATER, "`>'");
8038 /* We have processed another parameter list. */
8039 ++parser->num_template_parameter_lists;
8040 /* Let the front end know that we are beginning a specialization. */
8041 begin_specialization ();
8043 /* If the next keyword is `template', we need to figure out whether
8044 or not we're looking a template-declaration. */
8045 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
8047 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
8048 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
8049 cp_parser_template_declaration_after_export (parser,
8050 /*member_p=*/false);
8052 cp_parser_explicit_specialization (parser);
8055 /* Parse the dependent declaration. */
8056 cp_parser_single_declaration (parser,
8060 /* We're done with the specialization. */
8061 end_specialization ();
8062 /* We're done with this parameter list. */
8063 --parser->num_template_parameter_lists;
8066 /* Parse a type-specifier.
8069 simple-type-specifier
8072 elaborated-type-specifier
8080 Returns a representation of the type-specifier. If the
8081 type-specifier is a keyword (like `int' or `const', or
8082 `__complex__') then the corresponding IDENTIFIER_NODE is returned.
8083 For a class-specifier, enum-specifier, or elaborated-type-specifier
8084 a TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
8086 If IS_FRIEND is TRUE then this type-specifier is being declared a
8087 `friend'. If IS_DECLARATION is TRUE, then this type-specifier is
8088 appearing in a decl-specifier-seq.
8090 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
8091 class-specifier, enum-specifier, or elaborated-type-specifier, then
8092 *DECLARES_CLASS_OR_ENUM is set to a non-zero value. The value is 1
8093 if a type is declared; 2 if it is defined. Otherwise, it is set to
8096 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
8097 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
8101 cp_parser_type_specifier (cp_parser* parser,
8102 cp_parser_flags flags,
8104 bool is_declaration,
8105 int* declares_class_or_enum,
8106 bool* is_cv_qualifier)
8108 tree type_spec = NULL_TREE;
8112 /* Assume this type-specifier does not declare a new type. */
8113 if (declares_class_or_enum)
8114 *declares_class_or_enum = false;
8115 /* And that it does not specify a cv-qualifier. */
8116 if (is_cv_qualifier)
8117 *is_cv_qualifier = false;
8118 /* Peek at the next token. */
8119 token = cp_lexer_peek_token (parser->lexer);
8121 /* If we're looking at a keyword, we can use that to guide the
8122 production we choose. */
8123 keyword = token->keyword;
8126 /* Any of these indicate either a class-specifier, or an
8127 elaborated-type-specifier. */
8132 /* Parse tentatively so that we can back up if we don't find a
8133 class-specifier or enum-specifier. */
8134 cp_parser_parse_tentatively (parser);
8135 /* Look for the class-specifier or enum-specifier. */
8136 if (keyword == RID_ENUM)
8137 type_spec = cp_parser_enum_specifier (parser);
8139 type_spec = cp_parser_class_specifier (parser);
8141 /* If that worked, we're done. */
8142 if (cp_parser_parse_definitely (parser))
8144 if (declares_class_or_enum)
8145 *declares_class_or_enum = 2;
8152 /* Look for an elaborated-type-specifier. */
8153 type_spec = cp_parser_elaborated_type_specifier (parser,
8156 /* We're declaring a class or enum -- unless we're using
8158 if (declares_class_or_enum && keyword != RID_TYPENAME)
8159 *declares_class_or_enum = 1;
8165 type_spec = cp_parser_cv_qualifier_opt (parser);
8166 /* Even though we call a routine that looks for an optional
8167 qualifier, we know that there should be one. */
8168 my_friendly_assert (type_spec != NULL, 20000328);
8169 /* This type-specifier was a cv-qualified. */
8170 if (is_cv_qualifier)
8171 *is_cv_qualifier = true;
8176 /* The `__complex__' keyword is a GNU extension. */
8177 return cp_lexer_consume_token (parser->lexer)->value;
8183 /* If we do not already have a type-specifier, assume we are looking
8184 at a simple-type-specifier. */
8185 type_spec = cp_parser_simple_type_specifier (parser, flags,
8186 /*identifier_p=*/true);
8188 /* If we didn't find a type-specifier, and a type-specifier was not
8189 optional in this context, issue an error message. */
8190 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
8192 cp_parser_error (parser, "expected type specifier");
8193 return error_mark_node;
8199 /* Parse a simple-type-specifier.
8201 simple-type-specifier:
8202 :: [opt] nested-name-specifier [opt] type-name
8203 :: [opt] nested-name-specifier template template-id
8218 simple-type-specifier:
8219 __typeof__ unary-expression
8220 __typeof__ ( type-id )
8222 For the various keywords, the value returned is simply the
8223 TREE_IDENTIFIER representing the keyword if IDENTIFIER_P is true.
8224 For the first two productions, and if IDENTIFIER_P is false, the
8225 value returned is the indicated TYPE_DECL. */
8228 cp_parser_simple_type_specifier (cp_parser* parser, cp_parser_flags flags,
8231 tree type = NULL_TREE;
8234 /* Peek at the next token. */
8235 token = cp_lexer_peek_token (parser->lexer);
8237 /* If we're looking at a keyword, things are easy. */
8238 switch (token->keyword)
8241 type = char_type_node;
8244 type = wchar_type_node;
8247 type = boolean_type_node;
8250 type = short_integer_type_node;
8253 type = integer_type_node;
8256 type = long_integer_type_node;
8259 type = integer_type_node;
8262 type = unsigned_type_node;
8265 type = float_type_node;
8268 type = double_type_node;
8271 type = void_type_node;
8278 /* Consume the `typeof' token. */
8279 cp_lexer_consume_token (parser->lexer);
8280 /* Parse the operand to `typeof' */
8281 operand = cp_parser_sizeof_operand (parser, RID_TYPEOF);
8282 /* If it is not already a TYPE, take its type. */
8283 if (!TYPE_P (operand))
8284 operand = finish_typeof (operand);
8293 /* If the type-specifier was for a built-in type, we're done. */
8298 /* Consume the token. */
8299 id = cp_lexer_consume_token (parser->lexer)->value;
8300 return identifier_p ? id : TYPE_NAME (type);
8303 /* The type-specifier must be a user-defined type. */
8304 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
8306 /* Don't gobble tokens or issue error messages if this is an
8307 optional type-specifier. */
8308 if (flags & CP_PARSER_FLAGS_OPTIONAL)
8309 cp_parser_parse_tentatively (parser);
8311 /* Look for the optional `::' operator. */
8312 cp_parser_global_scope_opt (parser,
8313 /*current_scope_valid_p=*/false);
8314 /* Look for the nested-name specifier. */
8315 cp_parser_nested_name_specifier_opt (parser,
8316 /*typename_keyword_p=*/false,
8317 /*check_dependency_p=*/true,
8319 /* If we have seen a nested-name-specifier, and the next token
8320 is `template', then we are using the template-id production. */
8322 && cp_parser_optional_template_keyword (parser))
8324 /* Look for the template-id. */
8325 type = cp_parser_template_id (parser,
8326 /*template_keyword_p=*/true,
8327 /*check_dependency_p=*/true);
8328 /* If the template-id did not name a type, we are out of
8330 if (TREE_CODE (type) != TYPE_DECL)
8332 cp_parser_error (parser, "expected template-id for type");
8336 /* Otherwise, look for a type-name. */
8339 type = cp_parser_type_name (parser);
8340 if (type == error_mark_node)
8344 /* If it didn't work out, we don't have a TYPE. */
8345 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
8346 && !cp_parser_parse_definitely (parser))
8350 /* If we didn't get a type-name, issue an error message. */
8351 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
8353 cp_parser_error (parser, "expected type-name");
8354 return error_mark_node;
8360 /* Parse a type-name.
8373 Returns a TYPE_DECL for the the type. */
8376 cp_parser_type_name (cp_parser* parser)
8381 /* We can't know yet whether it is a class-name or not. */
8382 cp_parser_parse_tentatively (parser);
8383 /* Try a class-name. */
8384 type_decl = cp_parser_class_name (parser,
8385 /*typename_keyword_p=*/false,
8386 /*template_keyword_p=*/false,
8388 /*check_dependency_p=*/true,
8389 /*class_head_p=*/false);
8390 /* If it's not a class-name, keep looking. */
8391 if (!cp_parser_parse_definitely (parser))
8393 /* It must be a typedef-name or an enum-name. */
8394 identifier = cp_parser_identifier (parser);
8395 if (identifier == error_mark_node)
8396 return error_mark_node;
8398 /* Look up the type-name. */
8399 type_decl = cp_parser_lookup_name_simple (parser, identifier);
8400 /* Issue an error if we did not find a type-name. */
8401 if (TREE_CODE (type_decl) != TYPE_DECL)
8403 cp_parser_error (parser, "expected type-name");
8404 type_decl = error_mark_node;
8406 /* Remember that the name was used in the definition of the
8407 current class so that we can check later to see if the
8408 meaning would have been different after the class was
8409 entirely defined. */
8410 else if (type_decl != error_mark_node
8412 maybe_note_name_used_in_class (identifier, type_decl);
8419 /* Parse an elaborated-type-specifier. Note that the grammar given
8420 here incorporates the resolution to DR68.
8422 elaborated-type-specifier:
8423 class-key :: [opt] nested-name-specifier [opt] identifier
8424 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
8425 enum :: [opt] nested-name-specifier [opt] identifier
8426 typename :: [opt] nested-name-specifier identifier
8427 typename :: [opt] nested-name-specifier template [opt]
8430 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
8431 declared `friend'. If IS_DECLARATION is TRUE, then this
8432 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
8433 something is being declared.
8435 Returns the TYPE specified. */
8438 cp_parser_elaborated_type_specifier (cp_parser* parser,
8440 bool is_declaration)
8442 enum tag_types tag_type;
8444 tree type = NULL_TREE;
8446 /* See if we're looking at the `enum' keyword. */
8447 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
8449 /* Consume the `enum' token. */
8450 cp_lexer_consume_token (parser->lexer);
8451 /* Remember that it's an enumeration type. */
8452 tag_type = enum_type;
8454 /* Or, it might be `typename'. */
8455 else if (cp_lexer_next_token_is_keyword (parser->lexer,
8458 /* Consume the `typename' token. */
8459 cp_lexer_consume_token (parser->lexer);
8460 /* Remember that it's a `typename' type. */
8461 tag_type = typename_type;
8462 /* The `typename' keyword is only allowed in templates. */
8463 if (!processing_template_decl)
8464 pedwarn ("using `typename' outside of template");
8466 /* Otherwise it must be a class-key. */
8469 tag_type = cp_parser_class_key (parser);
8470 if (tag_type == none_type)
8471 return error_mark_node;
8474 /* Look for the `::' operator. */
8475 cp_parser_global_scope_opt (parser,
8476 /*current_scope_valid_p=*/false);
8477 /* Look for the nested-name-specifier. */
8478 if (tag_type == typename_type)
8480 if (cp_parser_nested_name_specifier (parser,
8481 /*typename_keyword_p=*/true,
8482 /*check_dependency_p=*/true,
8485 return error_mark_node;
8488 /* Even though `typename' is not present, the proposed resolution
8489 to Core Issue 180 says that in `class A<T>::B', `B' should be
8490 considered a type-name, even if `A<T>' is dependent. */
8491 cp_parser_nested_name_specifier_opt (parser,
8492 /*typename_keyword_p=*/true,
8493 /*check_dependency_p=*/true,
8495 /* For everything but enumeration types, consider a template-id. */
8496 if (tag_type != enum_type)
8498 bool template_p = false;
8501 /* Allow the `template' keyword. */
8502 template_p = cp_parser_optional_template_keyword (parser);
8503 /* If we didn't see `template', we don't know if there's a
8504 template-id or not. */
8506 cp_parser_parse_tentatively (parser);
8507 /* Parse the template-id. */
8508 decl = cp_parser_template_id (parser, template_p,
8509 /*check_dependency_p=*/true);
8510 /* If we didn't find a template-id, look for an ordinary
8512 if (!template_p && !cp_parser_parse_definitely (parser))
8514 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
8515 in effect, then we must assume that, upon instantiation, the
8516 template will correspond to a class. */
8517 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
8518 && tag_type == typename_type)
8519 type = make_typename_type (parser->scope, decl,
8522 type = TREE_TYPE (decl);
8525 /* For an enumeration type, consider only a plain identifier. */
8528 identifier = cp_parser_identifier (parser);
8530 if (identifier == error_mark_node)
8532 parser->scope = NULL_TREE;
8533 return error_mark_node;
8536 /* For a `typename', we needn't call xref_tag. */
8537 if (tag_type == typename_type)
8538 return make_typename_type (parser->scope, identifier,
8540 /* Look up a qualified name in the usual way. */
8545 /* In an elaborated-type-specifier, names are assumed to name
8546 types, so we set IS_TYPE to TRUE when calling
8547 cp_parser_lookup_name. */
8548 decl = cp_parser_lookup_name (parser, identifier,
8550 /*is_namespace=*/false,
8551 /*check_dependency=*/true);
8553 /* If we are parsing friend declaration, DECL may be a
8554 TEMPLATE_DECL tree node here. However, we need to check
8555 whether this TEMPLATE_DECL results in valid code. Consider
8556 the following example:
8559 template <class T> class C {};
8562 template <class T> friend class N::C; // #1, valid code
8564 template <class T> class Y {
8565 friend class N::C; // #2, invalid code
8568 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
8569 name lookup of `N::C'. We see that friend declaration must
8570 be template for the code to be valid. Note that
8571 processing_template_decl does not work here since it is
8572 always 1 for the above two cases. */
8574 decl = (cp_parser_maybe_treat_template_as_class
8575 (decl, /*tag_name_p=*/is_friend
8576 && parser->num_template_parameter_lists));
8578 if (TREE_CODE (decl) != TYPE_DECL)
8580 error ("expected type-name");
8581 return error_mark_node;
8584 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
8585 check_elaborated_type_specifier
8587 (parser->num_template_parameter_lists
8588 || DECL_SELF_REFERENCE_P (decl)));
8590 type = TREE_TYPE (decl);
8594 /* An elaborated-type-specifier sometimes introduces a new type and
8595 sometimes names an existing type. Normally, the rule is that it
8596 introduces a new type only if there is not an existing type of
8597 the same name already in scope. For example, given:
8600 void f() { struct S s; }
8602 the `struct S' in the body of `f' is the same `struct S' as in
8603 the global scope; the existing definition is used. However, if
8604 there were no global declaration, this would introduce a new
8605 local class named `S'.
8607 An exception to this rule applies to the following code:
8609 namespace N { struct S; }
8611 Here, the elaborated-type-specifier names a new type
8612 unconditionally; even if there is already an `S' in the
8613 containing scope this declaration names a new type.
8614 This exception only applies if the elaborated-type-specifier
8615 forms the complete declaration:
8619 A declaration consisting solely of `class-key identifier ;' is
8620 either a redeclaration of the name in the current scope or a
8621 forward declaration of the identifier as a class name. It
8622 introduces the name into the current scope.
8624 We are in this situation precisely when the next token is a `;'.
8626 An exception to the exception is that a `friend' declaration does
8627 *not* name a new type; i.e., given:
8629 struct S { friend struct T; };
8631 `T' is not a new type in the scope of `S'.
8633 Also, `new struct S' or `sizeof (struct S)' never results in the
8634 definition of a new type; a new type can only be declared in a
8635 declaration context. */
8637 type = xref_tag (tag_type, identifier,
8638 /*attributes=*/NULL_TREE,
8641 || cp_lexer_next_token_is_not (parser->lexer,
8643 parser->num_template_parameter_lists);
8646 if (tag_type != enum_type)
8647 cp_parser_check_class_key (tag_type, type);
8651 /* Parse an enum-specifier.
8654 enum identifier [opt] { enumerator-list [opt] }
8656 Returns an ENUM_TYPE representing the enumeration. */
8659 cp_parser_enum_specifier (cp_parser* parser)
8662 tree identifier = NULL_TREE;
8665 /* Look for the `enum' keyword. */
8666 if (!cp_parser_require_keyword (parser, RID_ENUM, "`enum'"))
8667 return error_mark_node;
8668 /* Peek at the next token. */
8669 token = cp_lexer_peek_token (parser->lexer);
8671 /* See if it is an identifier. */
8672 if (token->type == CPP_NAME)
8673 identifier = cp_parser_identifier (parser);
8675 /* Look for the `{'. */
8676 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
8677 return error_mark_node;
8679 /* At this point, we're going ahead with the enum-specifier, even
8680 if some other problem occurs. */
8681 cp_parser_commit_to_tentative_parse (parser);
8683 /* Issue an error message if type-definitions are forbidden here. */
8684 cp_parser_check_type_definition (parser);
8686 /* Create the new type. */
8687 type = start_enum (identifier ? identifier : make_anon_name ());
8689 /* Peek at the next token. */
8690 token = cp_lexer_peek_token (parser->lexer);
8691 /* If it's not a `}', then there are some enumerators. */
8692 if (token->type != CPP_CLOSE_BRACE)
8693 cp_parser_enumerator_list (parser, type);
8694 /* Look for the `}'. */
8695 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
8697 /* Finish up the enumeration. */
8703 /* Parse an enumerator-list. The enumerators all have the indicated
8707 enumerator-definition
8708 enumerator-list , enumerator-definition */
8711 cp_parser_enumerator_list (cp_parser* parser, tree type)
8717 /* Parse an enumerator-definition. */
8718 cp_parser_enumerator_definition (parser, type);
8719 /* Peek at the next token. */
8720 token = cp_lexer_peek_token (parser->lexer);
8721 /* If it's not a `,', then we've reached the end of the
8723 if (token->type != CPP_COMMA)
8725 /* Otherwise, consume the `,' and keep going. */
8726 cp_lexer_consume_token (parser->lexer);
8727 /* If the next token is a `}', there is a trailing comma. */
8728 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
8730 if (pedantic && !in_system_header)
8731 pedwarn ("comma at end of enumerator list");
8737 /* Parse an enumerator-definition. The enumerator has the indicated
8740 enumerator-definition:
8742 enumerator = constant-expression
8748 cp_parser_enumerator_definition (cp_parser* parser, tree type)
8754 /* Look for the identifier. */
8755 identifier = cp_parser_identifier (parser);
8756 if (identifier == error_mark_node)
8759 /* Peek at the next token. */
8760 token = cp_lexer_peek_token (parser->lexer);
8761 /* If it's an `=', then there's an explicit value. */
8762 if (token->type == CPP_EQ)
8764 /* Consume the `=' token. */
8765 cp_lexer_consume_token (parser->lexer);
8766 /* Parse the value. */
8767 value = cp_parser_constant_expression (parser,
8768 /*allow_non_constant_p=*/false,
8774 /* Create the enumerator. */
8775 build_enumerator (identifier, value, type);
8778 /* Parse a namespace-name.
8781 original-namespace-name
8784 Returns the NAMESPACE_DECL for the namespace. */
8787 cp_parser_namespace_name (cp_parser* parser)
8790 tree namespace_decl;
8792 /* Get the name of the namespace. */
8793 identifier = cp_parser_identifier (parser);
8794 if (identifier == error_mark_node)
8795 return error_mark_node;
8797 /* Look up the identifier in the currently active scope. Look only
8798 for namespaces, due to:
8802 When looking up a namespace-name in a using-directive or alias
8803 definition, only namespace names are considered.
8809 During the lookup of a name preceding the :: scope resolution
8810 operator, object, function, and enumerator names are ignored.
8812 (Note that cp_parser_class_or_namespace_name only calls this
8813 function if the token after the name is the scope resolution
8815 namespace_decl = cp_parser_lookup_name (parser, identifier,
8817 /*is_namespace=*/true,
8818 /*check_dependency=*/true);
8819 /* If it's not a namespace, issue an error. */
8820 if (namespace_decl == error_mark_node
8821 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
8823 cp_parser_error (parser, "expected namespace-name");
8824 namespace_decl = error_mark_node;
8827 return namespace_decl;
8830 /* Parse a namespace-definition.
8832 namespace-definition:
8833 named-namespace-definition
8834 unnamed-namespace-definition
8836 named-namespace-definition:
8837 original-namespace-definition
8838 extension-namespace-definition
8840 original-namespace-definition:
8841 namespace identifier { namespace-body }
8843 extension-namespace-definition:
8844 namespace original-namespace-name { namespace-body }
8846 unnamed-namespace-definition:
8847 namespace { namespace-body } */
8850 cp_parser_namespace_definition (cp_parser* parser)
8854 /* Look for the `namespace' keyword. */
8855 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
8857 /* Get the name of the namespace. We do not attempt to distinguish
8858 between an original-namespace-definition and an
8859 extension-namespace-definition at this point. The semantic
8860 analysis routines are responsible for that. */
8861 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
8862 identifier = cp_parser_identifier (parser);
8864 identifier = NULL_TREE;
8866 /* Look for the `{' to start the namespace. */
8867 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
8868 /* Start the namespace. */
8869 push_namespace (identifier);
8870 /* Parse the body of the namespace. */
8871 cp_parser_namespace_body (parser);
8872 /* Finish the namespace. */
8874 /* Look for the final `}'. */
8875 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
8878 /* Parse a namespace-body.
8881 declaration-seq [opt] */
8884 cp_parser_namespace_body (cp_parser* parser)
8886 cp_parser_declaration_seq_opt (parser);
8889 /* Parse a namespace-alias-definition.
8891 namespace-alias-definition:
8892 namespace identifier = qualified-namespace-specifier ; */
8895 cp_parser_namespace_alias_definition (cp_parser* parser)
8898 tree namespace_specifier;
8900 /* Look for the `namespace' keyword. */
8901 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
8902 /* Look for the identifier. */
8903 identifier = cp_parser_identifier (parser);
8904 if (identifier == error_mark_node)
8906 /* Look for the `=' token. */
8907 cp_parser_require (parser, CPP_EQ, "`='");
8908 /* Look for the qualified-namespace-specifier. */
8910 = cp_parser_qualified_namespace_specifier (parser);
8911 /* Look for the `;' token. */
8912 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
8914 /* Register the alias in the symbol table. */
8915 do_namespace_alias (identifier, namespace_specifier);
8918 /* Parse a qualified-namespace-specifier.
8920 qualified-namespace-specifier:
8921 :: [opt] nested-name-specifier [opt] namespace-name
8923 Returns a NAMESPACE_DECL corresponding to the specified
8927 cp_parser_qualified_namespace_specifier (cp_parser* parser)
8929 /* Look for the optional `::'. */
8930 cp_parser_global_scope_opt (parser,
8931 /*current_scope_valid_p=*/false);
8933 /* Look for the optional nested-name-specifier. */
8934 cp_parser_nested_name_specifier_opt (parser,
8935 /*typename_keyword_p=*/false,
8936 /*check_dependency_p=*/true,
8939 return cp_parser_namespace_name (parser);
8942 /* Parse a using-declaration.
8945 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
8946 using :: unqualified-id ; */
8949 cp_parser_using_declaration (cp_parser* parser)
8952 bool typename_p = false;
8953 bool global_scope_p;
8958 /* Look for the `using' keyword. */
8959 cp_parser_require_keyword (parser, RID_USING, "`using'");
8961 /* Peek at the next token. */
8962 token = cp_lexer_peek_token (parser->lexer);
8963 /* See if it's `typename'. */
8964 if (token->keyword == RID_TYPENAME)
8966 /* Remember that we've seen it. */
8968 /* Consume the `typename' token. */
8969 cp_lexer_consume_token (parser->lexer);
8972 /* Look for the optional global scope qualification. */
8974 = (cp_parser_global_scope_opt (parser,
8975 /*current_scope_valid_p=*/false)
8978 /* If we saw `typename', or didn't see `::', then there must be a
8979 nested-name-specifier present. */
8980 if (typename_p || !global_scope_p)
8981 cp_parser_nested_name_specifier (parser, typename_p,
8982 /*check_dependency_p=*/true,
8984 /* Otherwise, we could be in either of the two productions. In that
8985 case, treat the nested-name-specifier as optional. */
8987 cp_parser_nested_name_specifier_opt (parser,
8988 /*typename_keyword_p=*/false,
8989 /*check_dependency_p=*/true,
8992 /* Parse the unqualified-id. */
8993 identifier = cp_parser_unqualified_id (parser,
8994 /*template_keyword_p=*/false,
8995 /*check_dependency_p=*/true);
8997 /* The function we call to handle a using-declaration is different
8998 depending on what scope we are in. */
8999 scope = current_scope ();
9000 if (scope && TYPE_P (scope))
9002 /* Create the USING_DECL. */
9003 decl = do_class_using_decl (build_nt (SCOPE_REF,
9006 /* Add it to the list of members in this class. */
9007 finish_member_declaration (decl);
9011 decl = cp_parser_lookup_name_simple (parser, identifier);
9012 if (decl == error_mark_node)
9014 if (parser->scope && parser->scope != global_namespace)
9015 error ("`%D::%D' has not been declared",
9016 parser->scope, identifier);
9018 error ("`::%D' has not been declared", identifier);
9021 do_local_using_decl (decl);
9023 do_toplevel_using_decl (decl);
9026 /* Look for the final `;'. */
9027 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
9030 /* Parse a using-directive.
9033 using namespace :: [opt] nested-name-specifier [opt]
9037 cp_parser_using_directive (cp_parser* parser)
9039 tree namespace_decl;
9041 /* Look for the `using' keyword. */
9042 cp_parser_require_keyword (parser, RID_USING, "`using'");
9043 /* And the `namespace' keyword. */
9044 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
9045 /* Look for the optional `::' operator. */
9046 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
9047 /* And the optional nested-name-specifier. */
9048 cp_parser_nested_name_specifier_opt (parser,
9049 /*typename_keyword_p=*/false,
9050 /*check_dependency_p=*/true,
9052 /* Get the namespace being used. */
9053 namespace_decl = cp_parser_namespace_name (parser);
9054 /* Update the symbol table. */
9055 do_using_directive (namespace_decl);
9056 /* Look for the final `;'. */
9057 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
9060 /* Parse an asm-definition.
9063 asm ( string-literal ) ;
9068 asm volatile [opt] ( string-literal ) ;
9069 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
9070 asm volatile [opt] ( string-literal : asm-operand-list [opt]
9071 : asm-operand-list [opt] ) ;
9072 asm volatile [opt] ( string-literal : asm-operand-list [opt]
9073 : asm-operand-list [opt]
9074 : asm-operand-list [opt] ) ; */
9077 cp_parser_asm_definition (cp_parser* parser)
9081 tree outputs = NULL_TREE;
9082 tree inputs = NULL_TREE;
9083 tree clobbers = NULL_TREE;
9085 bool volatile_p = false;
9086 bool extended_p = false;
9088 /* Look for the `asm' keyword. */
9089 cp_parser_require_keyword (parser, RID_ASM, "`asm'");
9090 /* See if the next token is `volatile'. */
9091 if (cp_parser_allow_gnu_extensions_p (parser)
9092 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
9094 /* Remember that we saw the `volatile' keyword. */
9096 /* Consume the token. */
9097 cp_lexer_consume_token (parser->lexer);
9099 /* Look for the opening `('. */
9100 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
9101 /* Look for the string. */
9102 token = cp_parser_require (parser, CPP_STRING, "asm body");
9105 string = token->value;
9106 /* If we're allowing GNU extensions, check for the extended assembly
9107 syntax. Unfortunately, the `:' tokens need not be separated by
9108 a space in C, and so, for compatibility, we tolerate that here
9109 too. Doing that means that we have to treat the `::' operator as
9111 if (cp_parser_allow_gnu_extensions_p (parser)
9112 && at_function_scope_p ()
9113 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
9114 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
9116 bool inputs_p = false;
9117 bool clobbers_p = false;
9119 /* The extended syntax was used. */
9122 /* Look for outputs. */
9123 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
9125 /* Consume the `:'. */
9126 cp_lexer_consume_token (parser->lexer);
9127 /* Parse the output-operands. */
9128 if (cp_lexer_next_token_is_not (parser->lexer,
9130 && cp_lexer_next_token_is_not (parser->lexer,
9132 && cp_lexer_next_token_is_not (parser->lexer,
9134 outputs = cp_parser_asm_operand_list (parser);
9136 /* If the next token is `::', there are no outputs, and the
9137 next token is the beginning of the inputs. */
9138 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
9140 /* Consume the `::' token. */
9141 cp_lexer_consume_token (parser->lexer);
9142 /* The inputs are coming next. */
9146 /* Look for inputs. */
9148 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
9151 /* Consume the `:'. */
9152 cp_lexer_consume_token (parser->lexer);
9153 /* Parse the output-operands. */
9154 if (cp_lexer_next_token_is_not (parser->lexer,
9156 && cp_lexer_next_token_is_not (parser->lexer,
9158 && cp_lexer_next_token_is_not (parser->lexer,
9160 inputs = cp_parser_asm_operand_list (parser);
9162 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
9163 /* The clobbers are coming next. */
9166 /* Look for clobbers. */
9168 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
9171 /* Consume the `:'. */
9172 cp_lexer_consume_token (parser->lexer);
9173 /* Parse the clobbers. */
9174 if (cp_lexer_next_token_is_not (parser->lexer,
9176 clobbers = cp_parser_asm_clobber_list (parser);
9179 /* Look for the closing `)'. */
9180 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
9181 cp_parser_skip_to_closing_parenthesis (parser, true, false);
9182 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
9184 /* Create the ASM_STMT. */
9185 if (at_function_scope_p ())
9188 finish_asm_stmt (volatile_p
9189 ? ridpointers[(int) RID_VOLATILE] : NULL_TREE,
9190 string, outputs, inputs, clobbers);
9191 /* If the extended syntax was not used, mark the ASM_STMT. */
9193 ASM_INPUT_P (asm_stmt) = 1;
9196 assemble_asm (string);
9199 /* Declarators [gram.dcl.decl] */
9201 /* Parse an init-declarator.
9204 declarator initializer [opt]
9209 declarator asm-specification [opt] attributes [opt] initializer [opt]
9211 The DECL_SPECIFIERS and PREFIX_ATTRIBUTES apply to this declarator.
9212 Returns a representation of the entity declared. If MEMBER_P is TRUE,
9213 then this declarator appears in a class scope. The new DECL created
9214 by this declarator is returned.
9216 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
9217 for a function-definition here as well. If the declarator is a
9218 declarator for a function-definition, *FUNCTION_DEFINITION_P will
9219 be TRUE upon return. By that point, the function-definition will
9220 have been completely parsed.
9222 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
9226 cp_parser_init_declarator (cp_parser* parser,
9227 tree decl_specifiers,
9228 tree prefix_attributes,
9229 bool function_definition_allowed_p,
9231 int declares_class_or_enum,
9232 bool* function_definition_p)
9237 tree asm_specification;
9239 tree decl = NULL_TREE;
9241 bool is_initialized;
9242 bool is_parenthesized_init;
9243 bool is_non_constant_init;
9244 int ctor_dtor_or_conv_p;
9247 /* Assume that this is not the declarator for a function
9249 if (function_definition_p)
9250 *function_definition_p = false;
9252 /* Defer access checks while parsing the declarator; we cannot know
9253 what names are accessible until we know what is being
9255 resume_deferring_access_checks ();
9257 /* Parse the declarator. */
9259 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
9260 &ctor_dtor_or_conv_p);
9261 /* Gather up the deferred checks. */
9262 stop_deferring_access_checks ();
9264 /* If the DECLARATOR was erroneous, there's no need to go
9266 if (declarator == error_mark_node)
9267 return error_mark_node;
9269 cp_parser_check_for_definition_in_return_type (declarator,
9270 declares_class_or_enum);
9272 /* Figure out what scope the entity declared by the DECLARATOR is
9273 located in. `grokdeclarator' sometimes changes the scope, so
9274 we compute it now. */
9275 scope = get_scope_of_declarator (declarator);
9277 /* If we're allowing GNU extensions, look for an asm-specification
9279 if (cp_parser_allow_gnu_extensions_p (parser))
9281 /* Look for an asm-specification. */
9282 asm_specification = cp_parser_asm_specification_opt (parser);
9283 /* And attributes. */
9284 attributes = cp_parser_attributes_opt (parser);
9288 asm_specification = NULL_TREE;
9289 attributes = NULL_TREE;
9292 /* Peek at the next token. */
9293 token = cp_lexer_peek_token (parser->lexer);
9294 /* Check to see if the token indicates the start of a
9295 function-definition. */
9296 if (cp_parser_token_starts_function_definition_p (token))
9298 if (!function_definition_allowed_p)
9300 /* If a function-definition should not appear here, issue an
9302 cp_parser_error (parser,
9303 "a function-definition is not allowed here");
9304 return error_mark_node;
9308 /* Neither attributes nor an asm-specification are allowed
9309 on a function-definition. */
9310 if (asm_specification)
9311 error ("an asm-specification is not allowed on a function-definition");
9313 error ("attributes are not allowed on a function-definition");
9314 /* This is a function-definition. */
9315 *function_definition_p = true;
9317 /* Parse the function definition. */
9318 decl = (cp_parser_function_definition_from_specifiers_and_declarator
9319 (parser, decl_specifiers, prefix_attributes, declarator));
9327 Only in function declarations for constructors, destructors, and
9328 type conversions can the decl-specifier-seq be omitted.
9330 We explicitly postpone this check past the point where we handle
9331 function-definitions because we tolerate function-definitions
9332 that are missing their return types in some modes. */
9333 if (!decl_specifiers && ctor_dtor_or_conv_p <= 0)
9335 cp_parser_error (parser,
9336 "expected constructor, destructor, or type conversion");
9337 return error_mark_node;
9340 /* An `=' or an `(' indicates an initializer. */
9341 is_initialized = (token->type == CPP_EQ
9342 || token->type == CPP_OPEN_PAREN);
9343 /* If the init-declarator isn't initialized and isn't followed by a
9344 `,' or `;', it's not a valid init-declarator. */
9346 && token->type != CPP_COMMA
9347 && token->type != CPP_SEMICOLON)
9349 cp_parser_error (parser, "expected init-declarator");
9350 return error_mark_node;
9353 /* Because start_decl has side-effects, we should only call it if we
9354 know we're going ahead. By this point, we know that we cannot
9355 possibly be looking at any other construct. */
9356 cp_parser_commit_to_tentative_parse (parser);
9358 /* Check to see whether or not this declaration is a friend. */
9359 friend_p = cp_parser_friend_p (decl_specifiers);
9361 /* Check that the number of template-parameter-lists is OK. */
9362 if (!cp_parser_check_declarator_template_parameters (parser, declarator))
9363 return error_mark_node;
9365 /* Enter the newly declared entry in the symbol table. If we're
9366 processing a declaration in a class-specifier, we wait until
9367 after processing the initializer. */
9370 if (parser->in_unbraced_linkage_specification_p)
9372 decl_specifiers = tree_cons (error_mark_node,
9373 get_identifier ("extern"),
9375 have_extern_spec = false;
9377 decl = start_decl (declarator, decl_specifiers,
9378 is_initialized, attributes, prefix_attributes);
9381 /* Enter the SCOPE. That way unqualified names appearing in the
9382 initializer will be looked up in SCOPE. */
9386 /* Perform deferred access control checks, now that we know in which
9387 SCOPE the declared entity resides. */
9388 if (!member_p && decl)
9390 tree saved_current_function_decl = NULL_TREE;
9392 /* If the entity being declared is a function, pretend that we
9393 are in its scope. If it is a `friend', it may have access to
9394 things that would not otherwise be accessible. */
9395 if (TREE_CODE (decl) == FUNCTION_DECL)
9397 saved_current_function_decl = current_function_decl;
9398 current_function_decl = decl;
9401 /* Perform the access control checks for the declarator and the
9402 the decl-specifiers. */
9403 perform_deferred_access_checks ();
9405 /* Restore the saved value. */
9406 if (TREE_CODE (decl) == FUNCTION_DECL)
9407 current_function_decl = saved_current_function_decl;
9410 /* Parse the initializer. */
9412 initializer = cp_parser_initializer (parser,
9413 &is_parenthesized_init,
9414 &is_non_constant_init);
9417 initializer = NULL_TREE;
9418 is_parenthesized_init = false;
9419 is_non_constant_init = true;
9422 /* The old parser allows attributes to appear after a parenthesized
9423 initializer. Mark Mitchell proposed removing this functionality
9424 on the GCC mailing lists on 2002-08-13. This parser accepts the
9425 attributes -- but ignores them. */
9426 if (cp_parser_allow_gnu_extensions_p (parser) && is_parenthesized_init)
9427 if (cp_parser_attributes_opt (parser))
9428 warning ("attributes after parenthesized initializer ignored");
9430 /* Leave the SCOPE, now that we have processed the initializer. It
9431 is important to do this before calling cp_finish_decl because it
9432 makes decisions about whether to create DECL_STMTs or not based
9433 on the current scope. */
9437 /* For an in-class declaration, use `grokfield' to create the
9441 decl = grokfield (declarator, decl_specifiers,
9442 initializer, /*asmspec=*/NULL_TREE,
9443 /*attributes=*/NULL_TREE);
9444 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
9445 cp_parser_save_default_args (parser, decl);
9448 /* Finish processing the declaration. But, skip friend
9450 if (!friend_p && decl)
9451 cp_finish_decl (decl,
9454 /* If the initializer is in parentheses, then this is
9455 a direct-initialization, which means that an
9456 `explicit' constructor is OK. Otherwise, an
9457 `explicit' constructor cannot be used. */
9458 ((is_parenthesized_init || !is_initialized)
9459 ? 0 : LOOKUP_ONLYCONVERTING));
9461 /* Remember whether or not variables were initialized by
9462 constant-expressions. */
9463 if (decl && TREE_CODE (decl) == VAR_DECL
9464 && is_initialized && !is_non_constant_init)
9465 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = true;
9470 /* Parse a declarator.
9474 ptr-operator declarator
9476 abstract-declarator:
9477 ptr-operator abstract-declarator [opt]
9478 direct-abstract-declarator
9483 attributes [opt] direct-declarator
9484 attributes [opt] ptr-operator declarator
9486 abstract-declarator:
9487 attributes [opt] ptr-operator abstract-declarator [opt]
9488 attributes [opt] direct-abstract-declarator
9490 Returns a representation of the declarator. If the declarator has
9491 the form `* declarator', then an INDIRECT_REF is returned, whose
9492 only operand is the sub-declarator. Analogously, `& declarator' is
9493 represented as an ADDR_EXPR. For `X::* declarator', a SCOPE_REF is
9494 used. The first operand is the TYPE for `X'. The second operand
9495 is an INDIRECT_REF whose operand is the sub-declarator.
9497 Otherwise, the representation is as for a direct-declarator.
9499 (It would be better to define a structure type to represent
9500 declarators, rather than abusing `tree' nodes to represent
9501 declarators. That would be much clearer and save some memory.
9502 There is no reason for declarators to be garbage-collected, for
9503 example; they are created during parser and no longer needed after
9504 `grokdeclarator' has been called.)
9506 For a ptr-operator that has the optional cv-qualifier-seq,
9507 cv-qualifiers will be stored in the TREE_TYPE of the INDIRECT_REF
9510 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
9511 detect constructor, destructor or conversion operators. It is set
9512 to -1 if the declarator is a name, and +1 if it is a
9513 function. Otherwise it is set to zero. Usually you just want to
9514 test for >0, but internally the negative value is used.
9516 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
9517 a decl-specifier-seq unless it declares a constructor, destructor,
9518 or conversion. It might seem that we could check this condition in
9519 semantic analysis, rather than parsing, but that makes it difficult
9520 to handle something like `f()'. We want to notice that there are
9521 no decl-specifiers, and therefore realize that this is an
9522 expression, not a declaration.) */
9525 cp_parser_declarator (cp_parser* parser,
9526 cp_parser_declarator_kind dcl_kind,
9527 int* ctor_dtor_or_conv_p)
9531 enum tree_code code;
9532 tree cv_qualifier_seq;
9534 tree attributes = NULL_TREE;
9536 /* Assume this is not a constructor, destructor, or type-conversion
9538 if (ctor_dtor_or_conv_p)
9539 *ctor_dtor_or_conv_p = 0;
9541 if (cp_parser_allow_gnu_extensions_p (parser))
9542 attributes = cp_parser_attributes_opt (parser);
9544 /* Peek at the next token. */
9545 token = cp_lexer_peek_token (parser->lexer);
9547 /* Check for the ptr-operator production. */
9548 cp_parser_parse_tentatively (parser);
9549 /* Parse the ptr-operator. */
9550 code = cp_parser_ptr_operator (parser,
9553 /* If that worked, then we have a ptr-operator. */
9554 if (cp_parser_parse_definitely (parser))
9556 /* The dependent declarator is optional if we are parsing an
9557 abstract-declarator. */
9558 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
9559 cp_parser_parse_tentatively (parser);
9561 /* Parse the dependent declarator. */
9562 declarator = cp_parser_declarator (parser, dcl_kind,
9563 /*ctor_dtor_or_conv_p=*/NULL);
9565 /* If we are parsing an abstract-declarator, we must handle the
9566 case where the dependent declarator is absent. */
9567 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
9568 && !cp_parser_parse_definitely (parser))
9569 declarator = NULL_TREE;
9571 /* Build the representation of the ptr-operator. */
9572 if (code == INDIRECT_REF)
9573 declarator = make_pointer_declarator (cv_qualifier_seq,
9576 declarator = make_reference_declarator (cv_qualifier_seq,
9578 /* Handle the pointer-to-member case. */
9580 declarator = build_nt (SCOPE_REF, class_type, declarator);
9582 /* Everything else is a direct-declarator. */
9584 declarator = cp_parser_direct_declarator (parser, dcl_kind,
9585 ctor_dtor_or_conv_p);
9587 if (attributes && declarator != error_mark_node)
9588 declarator = tree_cons (attributes, declarator, NULL_TREE);
9593 /* Parse a direct-declarator or direct-abstract-declarator.
9597 direct-declarator ( parameter-declaration-clause )
9598 cv-qualifier-seq [opt]
9599 exception-specification [opt]
9600 direct-declarator [ constant-expression [opt] ]
9603 direct-abstract-declarator:
9604 direct-abstract-declarator [opt]
9605 ( parameter-declaration-clause )
9606 cv-qualifier-seq [opt]
9607 exception-specification [opt]
9608 direct-abstract-declarator [opt] [ constant-expression [opt] ]
9609 ( abstract-declarator )
9611 Returns a representation of the declarator. DCL_KIND is
9612 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
9613 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
9614 we are parsing a direct-declarator. It is
9615 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
9616 of ambiguity we prefer an abstract declarator, as per
9617 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P is as for
9618 cp_parser_declarator.
9620 For the declarator-id production, the representation is as for an
9621 id-expression, except that a qualified name is represented as a
9622 SCOPE_REF. A function-declarator is represented as a CALL_EXPR;
9623 see the documentation of the FUNCTION_DECLARATOR_* macros for
9624 information about how to find the various declarator components.
9625 An array-declarator is represented as an ARRAY_REF. The
9626 direct-declarator is the first operand; the constant-expression
9627 indicating the size of the array is the second operand. */
9630 cp_parser_direct_declarator (cp_parser* parser,
9631 cp_parser_declarator_kind dcl_kind,
9632 int* ctor_dtor_or_conv_p)
9635 tree declarator = NULL_TREE;
9636 tree scope = NULL_TREE;
9637 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
9638 bool saved_in_declarator_p = parser->in_declarator_p;
9643 /* Peek at the next token. */
9644 token = cp_lexer_peek_token (parser->lexer);
9645 if (token->type == CPP_OPEN_PAREN)
9647 /* This is either a parameter-declaration-clause, or a
9648 parenthesized declarator. When we know we are parsing a
9649 named declarator, it must be a parenthesized declarator
9650 if FIRST is true. For instance, `(int)' is a
9651 parameter-declaration-clause, with an omitted
9652 direct-abstract-declarator. But `((*))', is a
9653 parenthesized abstract declarator. Finally, when T is a
9654 template parameter `(T)' is a
9655 parameter-declaration-clause, and not a parenthesized
9658 We first try and parse a parameter-declaration-clause,
9659 and then try a nested declarator (if FIRST is true).
9661 It is not an error for it not to be a
9662 parameter-declaration-clause, even when FIRST is
9668 The first is the declaration of a function while the
9669 second is a the definition of a variable, including its
9672 Having seen only the parenthesis, we cannot know which of
9673 these two alternatives should be selected. Even more
9674 complex are examples like:
9679 The former is a function-declaration; the latter is a
9680 variable initialization.
9682 Thus again, we try a parameter-declaration-clause, and if
9683 that fails, we back out and return. */
9685 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
9689 cp_parser_parse_tentatively (parser);
9691 /* Consume the `('. */
9692 cp_lexer_consume_token (parser->lexer);
9695 /* If this is going to be an abstract declarator, we're
9696 in a declarator and we can't have default args. */
9697 parser->default_arg_ok_p = false;
9698 parser->in_declarator_p = true;
9701 /* Parse the parameter-declaration-clause. */
9702 params = cp_parser_parameter_declaration_clause (parser);
9704 /* If all went well, parse the cv-qualifier-seq and the
9705 exception-specification. */
9706 if (cp_parser_parse_definitely (parser))
9709 tree exception_specification;
9711 if (ctor_dtor_or_conv_p)
9712 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
9714 /* Consume the `)'. */
9715 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
9717 /* Parse the cv-qualifier-seq. */
9718 cv_qualifiers = cp_parser_cv_qualifier_seq_opt (parser);
9719 /* And the exception-specification. */
9720 exception_specification
9721 = cp_parser_exception_specification_opt (parser);
9723 /* Create the function-declarator. */
9724 declarator = make_call_declarator (declarator,
9727 exception_specification);
9728 /* Any subsequent parameter lists are to do with
9729 return type, so are not those of the declared
9731 parser->default_arg_ok_p = false;
9733 /* Repeat the main loop. */
9738 /* If this is the first, we can try a parenthesized
9742 parser->default_arg_ok_p = saved_default_arg_ok_p;
9743 parser->in_declarator_p = saved_in_declarator_p;
9745 /* Consume the `('. */
9746 cp_lexer_consume_token (parser->lexer);
9747 /* Parse the nested declarator. */
9749 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p);
9752 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
9753 declarator = error_mark_node;
9754 if (declarator == error_mark_node)
9757 goto handle_declarator;
9759 /* Otherwise, we must be done. */
9763 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
9764 && token->type == CPP_OPEN_SQUARE)
9766 /* Parse an array-declarator. */
9769 if (ctor_dtor_or_conv_p)
9770 *ctor_dtor_or_conv_p = 0;
9773 parser->default_arg_ok_p = false;
9774 parser->in_declarator_p = true;
9775 /* Consume the `['. */
9776 cp_lexer_consume_token (parser->lexer);
9777 /* Peek at the next token. */
9778 token = cp_lexer_peek_token (parser->lexer);
9779 /* If the next token is `]', then there is no
9780 constant-expression. */
9781 if (token->type != CPP_CLOSE_SQUARE)
9783 bool non_constant_p;
9786 = cp_parser_constant_expression (parser,
9787 /*allow_non_constant=*/true,
9789 if (!non_constant_p)
9790 bounds = cp_parser_fold_non_dependent_expr (bounds);
9794 /* Look for the closing `]'. */
9795 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'"))
9797 declarator = error_mark_node;
9801 declarator = build_nt (ARRAY_REF, declarator, bounds);
9803 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
9805 /* Parse a declarator_id */
9806 if (dcl_kind == CP_PARSER_DECLARATOR_EITHER)
9807 cp_parser_parse_tentatively (parser);
9808 declarator = cp_parser_declarator_id (parser);
9809 if (dcl_kind == CP_PARSER_DECLARATOR_EITHER)
9811 if (!cp_parser_parse_definitely (parser))
9812 declarator = error_mark_node;
9813 else if (TREE_CODE (declarator) != IDENTIFIER_NODE)
9815 cp_parser_error (parser, "expected unqualified-id");
9816 declarator = error_mark_node;
9820 if (declarator == error_mark_node)
9823 if (TREE_CODE (declarator) == SCOPE_REF)
9825 tree scope = TREE_OPERAND (declarator, 0);
9827 /* In the declaration of a member of a template class
9828 outside of the class itself, the SCOPE will sometimes
9829 be a TYPENAME_TYPE. For example, given:
9831 template <typename T>
9834 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
9835 this context, we must resolve S<T>::R to an ordinary
9836 type, rather than a typename type.
9838 The reason we normally avoid resolving TYPENAME_TYPEs
9839 is that a specialization of `S' might render
9840 `S<T>::R' not a type. However, if `S' is
9841 specialized, then this `i' will not be used, so there
9842 is no harm in resolving the types here. */
9843 if (TREE_CODE (scope) == TYPENAME_TYPE)
9847 /* Resolve the TYPENAME_TYPE. */
9848 type = resolve_typename_type (scope,
9849 /*only_current_p=*/false);
9850 /* If that failed, the declarator is invalid. */
9851 if (type != error_mark_node)
9853 /* Build a new DECLARATOR. */
9854 declarator = build_nt (SCOPE_REF,
9856 TREE_OPERAND (declarator, 1));
9860 /* Check to see whether the declarator-id names a constructor,
9861 destructor, or conversion. */
9862 if (declarator && ctor_dtor_or_conv_p
9863 && ((TREE_CODE (declarator) == SCOPE_REF
9864 && CLASS_TYPE_P (TREE_OPERAND (declarator, 0)))
9865 || (TREE_CODE (declarator) != SCOPE_REF
9866 && at_class_scope_p ())))
9868 tree unqualified_name;
9871 /* Get the unqualified part of the name. */
9872 if (TREE_CODE (declarator) == SCOPE_REF)
9874 class_type = TREE_OPERAND (declarator, 0);
9875 unqualified_name = TREE_OPERAND (declarator, 1);
9879 class_type = current_class_type;
9880 unqualified_name = declarator;
9883 /* See if it names ctor, dtor or conv. */
9884 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR
9885 || IDENTIFIER_TYPENAME_P (unqualified_name)
9886 || constructor_name_p (unqualified_name, class_type))
9887 *ctor_dtor_or_conv_p = -1;
9891 scope = get_scope_of_declarator (declarator);
9893 /* Any names that appear after the declarator-id for a member
9894 are looked up in the containing scope. */
9896 parser->in_declarator_p = true;
9897 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
9899 && (TREE_CODE (declarator) == SCOPE_REF
9900 || TREE_CODE (declarator) == IDENTIFIER_NODE)))
9901 /* Default args are only allowed on function
9903 parser->default_arg_ok_p = saved_default_arg_ok_p;
9905 parser->default_arg_ok_p = false;
9914 /* For an abstract declarator, we might wind up with nothing at this
9915 point. That's an error; the declarator is not optional. */
9917 cp_parser_error (parser, "expected declarator");
9919 /* If we entered a scope, we must exit it now. */
9923 parser->default_arg_ok_p = saved_default_arg_ok_p;
9924 parser->in_declarator_p = saved_in_declarator_p;
9929 /* Parse a ptr-operator.
9932 * cv-qualifier-seq [opt]
9934 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
9939 & cv-qualifier-seq [opt]
9941 Returns INDIRECT_REF if a pointer, or pointer-to-member, was
9942 used. Returns ADDR_EXPR if a reference was used. In the
9943 case of a pointer-to-member, *TYPE is filled in with the
9944 TYPE containing the member. *CV_QUALIFIER_SEQ is filled in
9945 with the cv-qualifier-seq, or NULL_TREE, if there are no
9946 cv-qualifiers. Returns ERROR_MARK if an error occurred. */
9948 static enum tree_code
9949 cp_parser_ptr_operator (cp_parser* parser,
9951 tree* cv_qualifier_seq)
9953 enum tree_code code = ERROR_MARK;
9956 /* Assume that it's not a pointer-to-member. */
9958 /* And that there are no cv-qualifiers. */
9959 *cv_qualifier_seq = NULL_TREE;
9961 /* Peek at the next token. */
9962 token = cp_lexer_peek_token (parser->lexer);
9963 /* If it's a `*' or `&' we have a pointer or reference. */
9964 if (token->type == CPP_MULT || token->type == CPP_AND)
9966 /* Remember which ptr-operator we were processing. */
9967 code = (token->type == CPP_AND ? ADDR_EXPR : INDIRECT_REF);
9969 /* Consume the `*' or `&'. */
9970 cp_lexer_consume_token (parser->lexer);
9972 /* A `*' can be followed by a cv-qualifier-seq, and so can a
9973 `&', if we are allowing GNU extensions. (The only qualifier
9974 that can legally appear after `&' is `restrict', but that is
9975 enforced during semantic analysis. */
9976 if (code == INDIRECT_REF
9977 || cp_parser_allow_gnu_extensions_p (parser))
9978 *cv_qualifier_seq = cp_parser_cv_qualifier_seq_opt (parser);
9982 /* Try the pointer-to-member case. */
9983 cp_parser_parse_tentatively (parser);
9984 /* Look for the optional `::' operator. */
9985 cp_parser_global_scope_opt (parser,
9986 /*current_scope_valid_p=*/false);
9987 /* Look for the nested-name specifier. */
9988 cp_parser_nested_name_specifier (parser,
9989 /*typename_keyword_p=*/false,
9990 /*check_dependency_p=*/true,
9992 /* If we found it, and the next token is a `*', then we are
9993 indeed looking at a pointer-to-member operator. */
9994 if (!cp_parser_error_occurred (parser)
9995 && cp_parser_require (parser, CPP_MULT, "`*'"))
9997 /* The type of which the member is a member is given by the
9999 *type = parser->scope;
10000 /* The next name will not be qualified. */
10001 parser->scope = NULL_TREE;
10002 parser->qualifying_scope = NULL_TREE;
10003 parser->object_scope = NULL_TREE;
10004 /* Indicate that the `*' operator was used. */
10005 code = INDIRECT_REF;
10006 /* Look for the optional cv-qualifier-seq. */
10007 *cv_qualifier_seq = cp_parser_cv_qualifier_seq_opt (parser);
10009 /* If that didn't work we don't have a ptr-operator. */
10010 if (!cp_parser_parse_definitely (parser))
10011 cp_parser_error (parser, "expected ptr-operator");
10017 /* Parse an (optional) cv-qualifier-seq.
10020 cv-qualifier cv-qualifier-seq [opt]
10022 Returns a TREE_LIST. The TREE_VALUE of each node is the
10023 representation of a cv-qualifier. */
10026 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
10028 tree cv_qualifiers = NULL_TREE;
10034 /* Look for the next cv-qualifier. */
10035 cv_qualifier = cp_parser_cv_qualifier_opt (parser);
10036 /* If we didn't find one, we're done. */
10040 /* Add this cv-qualifier to the list. */
10042 = tree_cons (NULL_TREE, cv_qualifier, cv_qualifiers);
10045 /* We built up the list in reverse order. */
10046 return nreverse (cv_qualifiers);
10049 /* Parse an (optional) cv-qualifier.
10061 cp_parser_cv_qualifier_opt (cp_parser* parser)
10064 tree cv_qualifier = NULL_TREE;
10066 /* Peek at the next token. */
10067 token = cp_lexer_peek_token (parser->lexer);
10068 /* See if it's a cv-qualifier. */
10069 switch (token->keyword)
10074 /* Save the value of the token. */
10075 cv_qualifier = token->value;
10076 /* Consume the token. */
10077 cp_lexer_consume_token (parser->lexer);
10084 return cv_qualifier;
10087 /* Parse a declarator-id.
10091 :: [opt] nested-name-specifier [opt] type-name
10093 In the `id-expression' case, the value returned is as for
10094 cp_parser_id_expression if the id-expression was an unqualified-id.
10095 If the id-expression was a qualified-id, then a SCOPE_REF is
10096 returned. The first operand is the scope (either a NAMESPACE_DECL
10097 or TREE_TYPE), but the second is still just a representation of an
10101 cp_parser_declarator_id (cp_parser* parser)
10103 tree id_expression;
10105 /* The expression must be an id-expression. Assume that qualified
10106 names are the names of types so that:
10109 int S<T>::R::i = 3;
10111 will work; we must treat `S<T>::R' as the name of a type.
10112 Similarly, assume that qualified names are templates, where
10116 int S<T>::R<T>::i = 3;
10119 id_expression = cp_parser_id_expression (parser,
10120 /*template_keyword_p=*/false,
10121 /*check_dependency_p=*/false,
10122 /*template_p=*/NULL);
10123 /* If the name was qualified, create a SCOPE_REF to represent
10127 id_expression = build_nt (SCOPE_REF, parser->scope, id_expression);
10128 parser->scope = NULL_TREE;
10131 return id_expression;
10134 /* Parse a type-id.
10137 type-specifier-seq abstract-declarator [opt]
10139 Returns the TYPE specified. */
10142 cp_parser_type_id (cp_parser* parser)
10144 tree type_specifier_seq;
10145 tree abstract_declarator;
10147 /* Parse the type-specifier-seq. */
10149 = cp_parser_type_specifier_seq (parser);
10150 if (type_specifier_seq == error_mark_node)
10151 return error_mark_node;
10153 /* There might or might not be an abstract declarator. */
10154 cp_parser_parse_tentatively (parser);
10155 /* Look for the declarator. */
10156 abstract_declarator
10157 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL);
10158 /* Check to see if there really was a declarator. */
10159 if (!cp_parser_parse_definitely (parser))
10160 abstract_declarator = NULL_TREE;
10162 return groktypename (build_tree_list (type_specifier_seq,
10163 abstract_declarator));
10166 /* Parse a type-specifier-seq.
10168 type-specifier-seq:
10169 type-specifier type-specifier-seq [opt]
10173 type-specifier-seq:
10174 attributes type-specifier-seq [opt]
10176 Returns a TREE_LIST. Either the TREE_VALUE of each node is a
10177 type-specifier, or the TREE_PURPOSE is a list of attributes. */
10180 cp_parser_type_specifier_seq (cp_parser* parser)
10182 bool seen_type_specifier = false;
10183 tree type_specifier_seq = NULL_TREE;
10185 /* Parse the type-specifiers and attributes. */
10188 tree type_specifier;
10190 /* Check for attributes first. */
10191 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
10193 type_specifier_seq = tree_cons (cp_parser_attributes_opt (parser),
10195 type_specifier_seq);
10199 /* After the first type-specifier, others are optional. */
10200 if (seen_type_specifier)
10201 cp_parser_parse_tentatively (parser);
10202 /* Look for the type-specifier. */
10203 type_specifier = cp_parser_type_specifier (parser,
10204 CP_PARSER_FLAGS_NONE,
10205 /*is_friend=*/false,
10206 /*is_declaration=*/false,
10209 /* If the first type-specifier could not be found, this is not a
10210 type-specifier-seq at all. */
10211 if (!seen_type_specifier && type_specifier == error_mark_node)
10212 return error_mark_node;
10213 /* If subsequent type-specifiers could not be found, the
10214 type-specifier-seq is complete. */
10215 else if (seen_type_specifier && !cp_parser_parse_definitely (parser))
10218 /* Add the new type-specifier to the list. */
10220 = tree_cons (NULL_TREE, type_specifier, type_specifier_seq);
10221 seen_type_specifier = true;
10224 /* We built up the list in reverse order. */
10225 return nreverse (type_specifier_seq);
10228 /* Parse a parameter-declaration-clause.
10230 parameter-declaration-clause:
10231 parameter-declaration-list [opt] ... [opt]
10232 parameter-declaration-list , ...
10234 Returns a representation for the parameter declarations. Each node
10235 is a TREE_LIST. (See cp_parser_parameter_declaration for the exact
10236 representation.) If the parameter-declaration-clause ends with an
10237 ellipsis, PARMLIST_ELLIPSIS_P will hold of the first node in the
10238 list. A return value of NULL_TREE indicates a
10239 parameter-declaration-clause consisting only of an ellipsis. */
10242 cp_parser_parameter_declaration_clause (cp_parser* parser)
10248 /* Peek at the next token. */
10249 token = cp_lexer_peek_token (parser->lexer);
10250 /* Check for trivial parameter-declaration-clauses. */
10251 if (token->type == CPP_ELLIPSIS)
10253 /* Consume the `...' token. */
10254 cp_lexer_consume_token (parser->lexer);
10257 else if (token->type == CPP_CLOSE_PAREN)
10258 /* There are no parameters. */
10260 #ifndef NO_IMPLICIT_EXTERN_C
10261 if (in_system_header && current_class_type == NULL
10262 && current_lang_name == lang_name_c)
10266 return void_list_node;
10268 /* Check for `(void)', too, which is a special case. */
10269 else if (token->keyword == RID_VOID
10270 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
10271 == CPP_CLOSE_PAREN))
10273 /* Consume the `void' token. */
10274 cp_lexer_consume_token (parser->lexer);
10275 /* There are no parameters. */
10276 return void_list_node;
10279 /* Parse the parameter-declaration-list. */
10280 parameters = cp_parser_parameter_declaration_list (parser);
10281 /* If a parse error occurred while parsing the
10282 parameter-declaration-list, then the entire
10283 parameter-declaration-clause is erroneous. */
10284 if (parameters == error_mark_node)
10285 return error_mark_node;
10287 /* Peek at the next token. */
10288 token = cp_lexer_peek_token (parser->lexer);
10289 /* If it's a `,', the clause should terminate with an ellipsis. */
10290 if (token->type == CPP_COMMA)
10292 /* Consume the `,'. */
10293 cp_lexer_consume_token (parser->lexer);
10294 /* Expect an ellipsis. */
10296 = (cp_parser_require (parser, CPP_ELLIPSIS, "`...'") != NULL);
10298 /* It might also be `...' if the optional trailing `,' was
10300 else if (token->type == CPP_ELLIPSIS)
10302 /* Consume the `...' token. */
10303 cp_lexer_consume_token (parser->lexer);
10304 /* And remember that we saw it. */
10308 ellipsis_p = false;
10310 /* Finish the parameter list. */
10311 return finish_parmlist (parameters, ellipsis_p);
10314 /* Parse a parameter-declaration-list.
10316 parameter-declaration-list:
10317 parameter-declaration
10318 parameter-declaration-list , parameter-declaration
10320 Returns a representation of the parameter-declaration-list, as for
10321 cp_parser_parameter_declaration_clause. However, the
10322 `void_list_node' is never appended to the list. */
10325 cp_parser_parameter_declaration_list (cp_parser* parser)
10327 tree parameters = NULL_TREE;
10329 /* Look for more parameters. */
10333 /* Parse the parameter. */
10335 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/false);
10337 /* If a parse error occurred parsing the parameter declaration,
10338 then the entire parameter-declaration-list is erroneous. */
10339 if (parameter == error_mark_node)
10341 parameters = error_mark_node;
10344 /* Add the new parameter to the list. */
10345 TREE_CHAIN (parameter) = parameters;
10346 parameters = parameter;
10348 /* Peek at the next token. */
10349 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
10350 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10351 /* The parameter-declaration-list is complete. */
10353 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
10357 /* Peek at the next token. */
10358 token = cp_lexer_peek_nth_token (parser->lexer, 2);
10359 /* If it's an ellipsis, then the list is complete. */
10360 if (token->type == CPP_ELLIPSIS)
10362 /* Otherwise, there must be more parameters. Consume the
10364 cp_lexer_consume_token (parser->lexer);
10368 cp_parser_error (parser, "expected `,' or `...'");
10373 /* We built up the list in reverse order; straighten it out now. */
10374 return nreverse (parameters);
10377 /* Parse a parameter declaration.
10379 parameter-declaration:
10380 decl-specifier-seq declarator
10381 decl-specifier-seq declarator = assignment-expression
10382 decl-specifier-seq abstract-declarator [opt]
10383 decl-specifier-seq abstract-declarator [opt] = assignment-expression
10385 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
10386 declares a template parameter. (In that case, a non-nested `>'
10387 token encountered during the parsing of the assignment-expression
10388 is not interpreted as a greater-than operator.)
10390 Returns a TREE_LIST representing the parameter-declaration. The
10391 TREE_VALUE is a representation of the decl-specifier-seq and
10392 declarator. In particular, the TREE_VALUE will be a TREE_LIST
10393 whose TREE_PURPOSE represents the decl-specifier-seq and whose
10394 TREE_VALUE represents the declarator. */
10397 cp_parser_parameter_declaration (cp_parser *parser,
10398 bool template_parm_p)
10400 int declares_class_or_enum;
10401 bool greater_than_is_operator_p;
10402 tree decl_specifiers;
10405 tree default_argument;
10408 const char *saved_message;
10410 /* In a template parameter, `>' is not an operator.
10414 When parsing a default template-argument for a non-type
10415 template-parameter, the first non-nested `>' is taken as the end
10416 of the template parameter-list rather than a greater-than
10418 greater_than_is_operator_p = !template_parm_p;
10420 /* Type definitions may not appear in parameter types. */
10421 saved_message = parser->type_definition_forbidden_message;
10422 parser->type_definition_forbidden_message
10423 = "types may not be defined in parameter types";
10425 /* Parse the declaration-specifiers. */
10427 = cp_parser_decl_specifier_seq (parser,
10428 CP_PARSER_FLAGS_NONE,
10430 &declares_class_or_enum);
10431 /* If an error occurred, there's no reason to attempt to parse the
10432 rest of the declaration. */
10433 if (cp_parser_error_occurred (parser))
10435 parser->type_definition_forbidden_message = saved_message;
10436 return error_mark_node;
10439 /* Peek at the next token. */
10440 token = cp_lexer_peek_token (parser->lexer);
10441 /* If the next token is a `)', `,', `=', `>', or `...', then there
10442 is no declarator. */
10443 if (token->type == CPP_CLOSE_PAREN
10444 || token->type == CPP_COMMA
10445 || token->type == CPP_EQ
10446 || token->type == CPP_ELLIPSIS
10447 || token->type == CPP_GREATER)
10448 declarator = NULL_TREE;
10449 /* Otherwise, there should be a declarator. */
10452 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
10453 parser->default_arg_ok_p = false;
10455 declarator = cp_parser_declarator (parser,
10456 CP_PARSER_DECLARATOR_EITHER,
10457 /*ctor_dtor_or_conv_p=*/NULL);
10458 parser->default_arg_ok_p = saved_default_arg_ok_p;
10459 /* After the declarator, allow more attributes. */
10460 attributes = chainon (attributes, cp_parser_attributes_opt (parser));
10463 /* The restriction on defining new types applies only to the type
10464 of the parameter, not to the default argument. */
10465 parser->type_definition_forbidden_message = saved_message;
10467 /* If the next token is `=', then process a default argument. */
10468 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10470 bool saved_greater_than_is_operator_p;
10471 /* Consume the `='. */
10472 cp_lexer_consume_token (parser->lexer);
10474 /* If we are defining a class, then the tokens that make up the
10475 default argument must be saved and processed later. */
10476 if (!template_parm_p && at_class_scope_p ()
10477 && TYPE_BEING_DEFINED (current_class_type))
10479 unsigned depth = 0;
10481 /* Create a DEFAULT_ARG to represented the unparsed default
10483 default_argument = make_node (DEFAULT_ARG);
10484 DEFARG_TOKENS (default_argument) = cp_token_cache_new ();
10486 /* Add tokens until we have processed the entire default
10493 /* Peek at the next token. */
10494 token = cp_lexer_peek_token (parser->lexer);
10495 /* What we do depends on what token we have. */
10496 switch (token->type)
10498 /* In valid code, a default argument must be
10499 immediately followed by a `,' `)', or `...'. */
10501 case CPP_CLOSE_PAREN:
10503 /* If we run into a non-nested `;', `}', or `]',
10504 then the code is invalid -- but the default
10505 argument is certainly over. */
10506 case CPP_SEMICOLON:
10507 case CPP_CLOSE_BRACE:
10508 case CPP_CLOSE_SQUARE:
10511 /* Update DEPTH, if necessary. */
10512 else if (token->type == CPP_CLOSE_PAREN
10513 || token->type == CPP_CLOSE_BRACE
10514 || token->type == CPP_CLOSE_SQUARE)
10518 case CPP_OPEN_PAREN:
10519 case CPP_OPEN_SQUARE:
10520 case CPP_OPEN_BRACE:
10525 /* If we see a non-nested `>', and `>' is not an
10526 operator, then it marks the end of the default
10528 if (!depth && !greater_than_is_operator_p)
10532 /* If we run out of tokens, issue an error message. */
10534 error ("file ends in default argument");
10540 /* In these cases, we should look for template-ids.
10541 For example, if the default argument is
10542 `X<int, double>()', we need to do name lookup to
10543 figure out whether or not `X' is a template; if
10544 so, the `,' does not end the default argument.
10546 That is not yet done. */
10553 /* If we've reached the end, stop. */
10557 /* Add the token to the token block. */
10558 token = cp_lexer_consume_token (parser->lexer);
10559 cp_token_cache_push_token (DEFARG_TOKENS (default_argument),
10563 /* Outside of a class definition, we can just parse the
10564 assignment-expression. */
10567 bool saved_local_variables_forbidden_p;
10569 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
10571 saved_greater_than_is_operator_p
10572 = parser->greater_than_is_operator_p;
10573 parser->greater_than_is_operator_p = greater_than_is_operator_p;
10574 /* Local variable names (and the `this' keyword) may not
10575 appear in a default argument. */
10576 saved_local_variables_forbidden_p
10577 = parser->local_variables_forbidden_p;
10578 parser->local_variables_forbidden_p = true;
10579 /* Parse the assignment-expression. */
10580 default_argument = cp_parser_assignment_expression (parser);
10581 /* Restore saved state. */
10582 parser->greater_than_is_operator_p
10583 = saved_greater_than_is_operator_p;
10584 parser->local_variables_forbidden_p
10585 = saved_local_variables_forbidden_p;
10587 if (!parser->default_arg_ok_p)
10589 if (!flag_pedantic_errors)
10590 warning ("deprecated use of default argument for parameter of non-function");
10593 error ("default arguments are only permitted for function parameters");
10594 default_argument = NULL_TREE;
10599 default_argument = NULL_TREE;
10601 /* Create the representation of the parameter. */
10603 decl_specifiers = tree_cons (attributes, NULL_TREE, decl_specifiers);
10604 parameter = build_tree_list (default_argument,
10605 build_tree_list (decl_specifiers,
10611 /* Parse a function-definition.
10613 function-definition:
10614 decl-specifier-seq [opt] declarator ctor-initializer [opt]
10616 decl-specifier-seq [opt] declarator function-try-block
10620 function-definition:
10621 __extension__ function-definition
10623 Returns the FUNCTION_DECL for the function. If FRIEND_P is
10624 non-NULL, *FRIEND_P is set to TRUE iff the function was declared to
10628 cp_parser_function_definition (cp_parser* parser, bool* friend_p)
10630 tree decl_specifiers;
10635 int declares_class_or_enum;
10637 /* The saved value of the PEDANTIC flag. */
10638 int saved_pedantic;
10640 /* Any pending qualification must be cleared by our caller. It is
10641 more robust to force the callers to clear PARSER->SCOPE than to
10642 do it here since if the qualification is in effect here, it might
10643 also end up in effect elsewhere that it is not intended. */
10644 my_friendly_assert (!parser->scope, 20010821);
10646 /* Handle `__extension__'. */
10647 if (cp_parser_extension_opt (parser, &saved_pedantic))
10649 /* Parse the function-definition. */
10650 fn = cp_parser_function_definition (parser, friend_p);
10651 /* Restore the PEDANTIC flag. */
10652 pedantic = saved_pedantic;
10657 /* Check to see if this definition appears in a class-specifier. */
10658 member_p = (at_class_scope_p ()
10659 && TYPE_BEING_DEFINED (current_class_type));
10660 /* Defer access checks in the decl-specifier-seq until we know what
10661 function is being defined. There is no need to do this for the
10662 definition of member functions; we cannot be defining a member
10663 from another class. */
10664 push_deferring_access_checks (member_p ? dk_no_check: dk_deferred);
10666 /* Parse the decl-specifier-seq. */
10668 = cp_parser_decl_specifier_seq (parser,
10669 CP_PARSER_FLAGS_OPTIONAL,
10671 &declares_class_or_enum);
10672 /* Figure out whether this declaration is a `friend'. */
10674 *friend_p = cp_parser_friend_p (decl_specifiers);
10676 /* Parse the declarator. */
10677 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
10678 /*ctor_dtor_or_conv_p=*/NULL);
10680 /* Gather up any access checks that occurred. */
10681 stop_deferring_access_checks ();
10683 /* If something has already gone wrong, we may as well stop now. */
10684 if (declarator == error_mark_node)
10686 /* Skip to the end of the function, or if this wasn't anything
10687 like a function-definition, to a `;' in the hopes of finding
10688 a sensible place from which to continue parsing. */
10689 cp_parser_skip_to_end_of_block_or_statement (parser);
10690 pop_deferring_access_checks ();
10691 return error_mark_node;
10694 /* The next character should be a `{' (for a simple function
10695 definition), a `:' (for a ctor-initializer), or `try' (for a
10696 function-try block). */
10697 token = cp_lexer_peek_token (parser->lexer);
10698 if (!cp_parser_token_starts_function_definition_p (token))
10700 /* Issue the error-message. */
10701 cp_parser_error (parser, "expected function-definition");
10702 /* Skip to the next `;'. */
10703 cp_parser_skip_to_end_of_block_or_statement (parser);
10705 pop_deferring_access_checks ();
10706 return error_mark_node;
10709 cp_parser_check_for_definition_in_return_type (declarator,
10710 declares_class_or_enum);
10712 /* If we are in a class scope, then we must handle
10713 function-definitions specially. In particular, we save away the
10714 tokens that make up the function body, and parse them again
10715 later, in order to handle code like:
10718 int f () { return i; }
10722 Here, we cannot parse the body of `f' until after we have seen
10723 the declaration of `i'. */
10726 cp_token_cache *cache;
10728 /* Create the function-declaration. */
10729 fn = start_method (decl_specifiers, declarator, attributes);
10730 /* If something went badly wrong, bail out now. */
10731 if (fn == error_mark_node)
10733 /* If there's a function-body, skip it. */
10734 if (cp_parser_token_starts_function_definition_p
10735 (cp_lexer_peek_token (parser->lexer)))
10736 cp_parser_skip_to_end_of_block_or_statement (parser);
10737 pop_deferring_access_checks ();
10738 return error_mark_node;
10741 /* Remember it, if there default args to post process. */
10742 cp_parser_save_default_args (parser, fn);
10744 /* Create a token cache. */
10745 cache = cp_token_cache_new ();
10746 /* Save away the tokens that make up the body of the
10748 cp_parser_cache_group (parser, cache, CPP_CLOSE_BRACE, /*depth=*/0);
10749 /* Handle function try blocks. */
10750 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
10751 cp_parser_cache_group (parser, cache, CPP_CLOSE_BRACE, /*depth=*/0);
10753 /* Save away the inline definition; we will process it when the
10754 class is complete. */
10755 DECL_PENDING_INLINE_INFO (fn) = cache;
10756 DECL_PENDING_INLINE_P (fn) = 1;
10758 /* We need to know that this was defined in the class, so that
10759 friend templates are handled correctly. */
10760 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
10762 /* We're done with the inline definition. */
10763 finish_method (fn);
10765 /* Add FN to the queue of functions to be parsed later. */
10766 TREE_VALUE (parser->unparsed_functions_queues)
10767 = tree_cons (NULL_TREE, fn,
10768 TREE_VALUE (parser->unparsed_functions_queues));
10770 pop_deferring_access_checks ();
10774 /* Check that the number of template-parameter-lists is OK. */
10775 if (!cp_parser_check_declarator_template_parameters (parser,
10778 cp_parser_skip_to_end_of_block_or_statement (parser);
10779 pop_deferring_access_checks ();
10780 return error_mark_node;
10783 fn = cp_parser_function_definition_from_specifiers_and_declarator
10784 (parser, decl_specifiers, attributes, declarator);
10785 pop_deferring_access_checks ();
10789 /* Parse a function-body.
10792 compound_statement */
10795 cp_parser_function_body (cp_parser *parser)
10797 cp_parser_compound_statement (parser, false);
10800 /* Parse a ctor-initializer-opt followed by a function-body. Return
10801 true if a ctor-initializer was present. */
10804 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
10807 bool ctor_initializer_p;
10809 /* Begin the function body. */
10810 body = begin_function_body ();
10811 /* Parse the optional ctor-initializer. */
10812 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
10813 /* Parse the function-body. */
10814 cp_parser_function_body (parser);
10815 /* Finish the function body. */
10816 finish_function_body (body);
10818 return ctor_initializer_p;
10821 /* Parse an initializer.
10824 = initializer-clause
10825 ( expression-list )
10827 Returns a expression representing the initializer. If no
10828 initializer is present, NULL_TREE is returned.
10830 *IS_PARENTHESIZED_INIT is set to TRUE if the `( expression-list )'
10831 production is used, and zero otherwise. *IS_PARENTHESIZED_INIT is
10832 set to FALSE if there is no initializer present. If there is an
10833 initializer, and it is not a constant-expression, *NON_CONSTANT_P
10834 is set to true; otherwise it is set to false. */
10837 cp_parser_initializer (cp_parser* parser, bool* is_parenthesized_init,
10838 bool* non_constant_p)
10843 /* Peek at the next token. */
10844 token = cp_lexer_peek_token (parser->lexer);
10846 /* Let our caller know whether or not this initializer was
10848 *is_parenthesized_init = (token->type == CPP_OPEN_PAREN);
10849 /* Assume that the initializer is constant. */
10850 *non_constant_p = false;
10852 if (token->type == CPP_EQ)
10854 /* Consume the `='. */
10855 cp_lexer_consume_token (parser->lexer);
10856 /* Parse the initializer-clause. */
10857 init = cp_parser_initializer_clause (parser, non_constant_p);
10859 else if (token->type == CPP_OPEN_PAREN)
10860 init = cp_parser_parenthesized_expression_list (parser, false,
10864 /* Anything else is an error. */
10865 cp_parser_error (parser, "expected initializer");
10866 init = error_mark_node;
10872 /* Parse an initializer-clause.
10874 initializer-clause:
10875 assignment-expression
10876 { initializer-list , [opt] }
10879 Returns an expression representing the initializer.
10881 If the `assignment-expression' production is used the value
10882 returned is simply a representation for the expression.
10884 Otherwise, a CONSTRUCTOR is returned. The CONSTRUCTOR_ELTS will be
10885 the elements of the initializer-list (or NULL_TREE, if the last
10886 production is used). The TREE_TYPE for the CONSTRUCTOR will be
10887 NULL_TREE. There is no way to detect whether or not the optional
10888 trailing `,' was provided. NON_CONSTANT_P is as for
10889 cp_parser_initializer. */
10892 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
10896 /* If it is not a `{', then we are looking at an
10897 assignment-expression. */
10898 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
10900 = cp_parser_constant_expression (parser,
10901 /*allow_non_constant_p=*/true,
10905 /* Consume the `{' token. */
10906 cp_lexer_consume_token (parser->lexer);
10907 /* Create a CONSTRUCTOR to represent the braced-initializer. */
10908 initializer = make_node (CONSTRUCTOR);
10909 /* Mark it with TREE_HAS_CONSTRUCTOR. This should not be
10910 necessary, but check_initializer depends upon it, for
10912 TREE_HAS_CONSTRUCTOR (initializer) = 1;
10913 /* If it's not a `}', then there is a non-trivial initializer. */
10914 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
10916 /* Parse the initializer list. */
10917 CONSTRUCTOR_ELTS (initializer)
10918 = cp_parser_initializer_list (parser, non_constant_p);
10919 /* A trailing `,' token is allowed. */
10920 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
10921 cp_lexer_consume_token (parser->lexer);
10923 /* Now, there should be a trailing `}'. */
10924 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
10927 return initializer;
10930 /* Parse an initializer-list.
10934 initializer-list , initializer-clause
10939 identifier : initializer-clause
10940 initializer-list, identifier : initializer-clause
10942 Returns a TREE_LIST. The TREE_VALUE of each node is an expression
10943 for the initializer. If the TREE_PURPOSE is non-NULL, it is the
10944 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
10945 as for cp_parser_initializer. */
10948 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
10950 tree initializers = NULL_TREE;
10952 /* Assume all of the expressions are constant. */
10953 *non_constant_p = false;
10955 /* Parse the rest of the list. */
10961 bool clause_non_constant_p;
10963 /* If the next token is an identifier and the following one is a
10964 colon, we are looking at the GNU designated-initializer
10966 if (cp_parser_allow_gnu_extensions_p (parser)
10967 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
10968 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
10970 /* Consume the identifier. */
10971 identifier = cp_lexer_consume_token (parser->lexer)->value;
10972 /* Consume the `:'. */
10973 cp_lexer_consume_token (parser->lexer);
10976 identifier = NULL_TREE;
10978 /* Parse the initializer. */
10979 initializer = cp_parser_initializer_clause (parser,
10980 &clause_non_constant_p);
10981 /* If any clause is non-constant, so is the entire initializer. */
10982 if (clause_non_constant_p)
10983 *non_constant_p = true;
10984 /* Add it to the list. */
10985 initializers = tree_cons (identifier, initializer, initializers);
10987 /* If the next token is not a comma, we have reached the end of
10989 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
10992 /* Peek at the next token. */
10993 token = cp_lexer_peek_nth_token (parser->lexer, 2);
10994 /* If the next token is a `}', then we're still done. An
10995 initializer-clause can have a trailing `,' after the
10996 initializer-list and before the closing `}'. */
10997 if (token->type == CPP_CLOSE_BRACE)
11000 /* Consume the `,' token. */
11001 cp_lexer_consume_token (parser->lexer);
11004 /* The initializers were built up in reverse order, so we need to
11005 reverse them now. */
11006 return nreverse (initializers);
11009 /* Classes [gram.class] */
11011 /* Parse a class-name.
11017 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
11018 to indicate that names looked up in dependent types should be
11019 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
11020 keyword has been used to indicate that the name that appears next
11021 is a template. TYPE_P is true iff the next name should be treated
11022 as class-name, even if it is declared to be some other kind of name
11023 as well. If CHECK_DEPENDENCY_P is FALSE, names are looked up in
11024 dependent scopes. If CLASS_HEAD_P is TRUE, this class is the class
11025 being defined in a class-head.
11027 Returns the TYPE_DECL representing the class. */
11030 cp_parser_class_name (cp_parser *parser,
11031 bool typename_keyword_p,
11032 bool template_keyword_p,
11034 bool check_dependency_p,
11042 /* All class-names start with an identifier. */
11043 token = cp_lexer_peek_token (parser->lexer);
11044 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
11046 cp_parser_error (parser, "expected class-name");
11047 return error_mark_node;
11050 /* PARSER->SCOPE can be cleared when parsing the template-arguments
11051 to a template-id, so we save it here. */
11052 scope = parser->scope;
11053 if (scope == error_mark_node)
11054 return error_mark_node;
11056 /* Any name names a type if we're following the `typename' keyword
11057 in a qualified name where the enclosing scope is type-dependent. */
11058 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
11059 && dependent_type_p (scope));
11060 /* Handle the common case (an identifier, but not a template-id)
11062 if (token->type == CPP_NAME
11063 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_LESS)
11067 /* Look for the identifier. */
11068 identifier = cp_parser_identifier (parser);
11069 /* If the next token isn't an identifier, we are certainly not
11070 looking at a class-name. */
11071 if (identifier == error_mark_node)
11072 decl = error_mark_node;
11073 /* If we know this is a type-name, there's no need to look it
11075 else if (typename_p)
11079 /* If the next token is a `::', then the name must be a type
11082 [basic.lookup.qual]
11084 During the lookup for a name preceding the :: scope
11085 resolution operator, object, function, and enumerator
11086 names are ignored. */
11087 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
11089 /* Look up the name. */
11090 decl = cp_parser_lookup_name (parser, identifier,
11092 /*is_namespace=*/false,
11093 check_dependency_p);
11098 /* Try a template-id. */
11099 decl = cp_parser_template_id (parser, template_keyword_p,
11100 check_dependency_p);
11101 if (decl == error_mark_node)
11102 return error_mark_node;
11105 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
11107 /* If this is a typename, create a TYPENAME_TYPE. */
11108 if (typename_p && decl != error_mark_node)
11109 decl = TYPE_NAME (make_typename_type (scope, decl,
11112 /* Check to see that it is really the name of a class. */
11113 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
11114 && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
11115 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
11116 /* Situations like this:
11118 template <typename T> struct A {
11119 typename T::template X<int>::I i;
11122 are problematic. Is `T::template X<int>' a class-name? The
11123 standard does not seem to be definitive, but there is no other
11124 valid interpretation of the following `::'. Therefore, those
11125 names are considered class-names. */
11126 decl = TYPE_NAME (make_typename_type (scope, decl, tf_error));
11127 else if (decl == error_mark_node
11128 || TREE_CODE (decl) != TYPE_DECL
11129 || !IS_AGGR_TYPE (TREE_TYPE (decl)))
11131 cp_parser_error (parser, "expected class-name");
11132 return error_mark_node;
11138 /* Parse a class-specifier.
11141 class-head { member-specification [opt] }
11143 Returns the TREE_TYPE representing the class. */
11146 cp_parser_class_specifier (cp_parser* parser)
11150 tree attributes = NULL_TREE;
11151 int has_trailing_semicolon;
11152 bool nested_name_specifier_p;
11153 unsigned saved_num_template_parameter_lists;
11155 push_deferring_access_checks (dk_no_deferred);
11157 /* Parse the class-head. */
11158 type = cp_parser_class_head (parser,
11159 &nested_name_specifier_p);
11160 /* If the class-head was a semantic disaster, skip the entire body
11164 cp_parser_skip_to_end_of_block_or_statement (parser);
11165 pop_deferring_access_checks ();
11166 return error_mark_node;
11169 /* Look for the `{'. */
11170 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
11172 pop_deferring_access_checks ();
11173 return error_mark_node;
11176 /* Issue an error message if type-definitions are forbidden here. */
11177 cp_parser_check_type_definition (parser);
11178 /* Remember that we are defining one more class. */
11179 ++parser->num_classes_being_defined;
11180 /* Inside the class, surrounding template-parameter-lists do not
11182 saved_num_template_parameter_lists
11183 = parser->num_template_parameter_lists;
11184 parser->num_template_parameter_lists = 0;
11186 /* Start the class. */
11187 type = begin_class_definition (type);
11188 if (type == error_mark_node)
11189 /* If the type is erroneous, skip the entire body of the class. */
11190 cp_parser_skip_to_closing_brace (parser);
11192 /* Parse the member-specification. */
11193 cp_parser_member_specification_opt (parser);
11194 /* Look for the trailing `}'. */
11195 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
11196 /* We get better error messages by noticing a common problem: a
11197 missing trailing `;'. */
11198 token = cp_lexer_peek_token (parser->lexer);
11199 has_trailing_semicolon = (token->type == CPP_SEMICOLON);
11200 /* Look for attributes to apply to this class. */
11201 if (cp_parser_allow_gnu_extensions_p (parser))
11202 attributes = cp_parser_attributes_opt (parser);
11203 /* If we got any attributes in class_head, xref_tag will stick them in
11204 TREE_TYPE of the type. Grab them now. */
11205 if (type != error_mark_node)
11207 attributes = chainon (TYPE_ATTRIBUTES (type), attributes);
11208 TYPE_ATTRIBUTES (type) = NULL_TREE;
11209 type = finish_struct (type, attributes);
11211 if (nested_name_specifier_p)
11212 pop_scope (CP_DECL_CONTEXT (TYPE_MAIN_DECL (type)));
11213 /* If this class is not itself within the scope of another class,
11214 then we need to parse the bodies of all of the queued function
11215 definitions. Note that the queued functions defined in a class
11216 are not always processed immediately following the
11217 class-specifier for that class. Consider:
11220 struct B { void f() { sizeof (A); } };
11223 If `f' were processed before the processing of `A' were
11224 completed, there would be no way to compute the size of `A'.
11225 Note that the nesting we are interested in here is lexical --
11226 not the semantic nesting given by TYPE_CONTEXT. In particular,
11229 struct A { struct B; };
11230 struct A::B { void f() { } };
11232 there is no need to delay the parsing of `A::B::f'. */
11233 if (--parser->num_classes_being_defined == 0)
11238 /* In a first pass, parse default arguments to the functions.
11239 Then, in a second pass, parse the bodies of the functions.
11240 This two-phased approach handles cases like:
11248 for (TREE_PURPOSE (parser->unparsed_functions_queues)
11249 = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
11250 (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
11251 TREE_PURPOSE (parser->unparsed_functions_queues)
11252 = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
11254 fn = TREE_VALUE (queue_entry);
11255 /* Make sure that any template parameters are in scope. */
11256 maybe_begin_member_template_processing (fn);
11257 /* If there are default arguments that have not yet been processed,
11258 take care of them now. */
11259 cp_parser_late_parsing_default_args (parser, fn);
11260 /* Remove any template parameters from the symbol table. */
11261 maybe_end_member_template_processing ();
11263 /* Now parse the body of the functions. */
11264 for (TREE_VALUE (parser->unparsed_functions_queues)
11265 = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
11266 (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
11267 TREE_VALUE (parser->unparsed_functions_queues)
11268 = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
11270 /* Figure out which function we need to process. */
11271 fn = TREE_VALUE (queue_entry);
11273 /* Parse the function. */
11274 cp_parser_late_parsing_for_member (parser, fn);
11279 /* Put back any saved access checks. */
11280 pop_deferring_access_checks ();
11282 /* Restore the count of active template-parameter-lists. */
11283 parser->num_template_parameter_lists
11284 = saved_num_template_parameter_lists;
11289 /* Parse a class-head.
11292 class-key identifier [opt] base-clause [opt]
11293 class-key nested-name-specifier identifier base-clause [opt]
11294 class-key nested-name-specifier [opt] template-id
11298 class-key attributes identifier [opt] base-clause [opt]
11299 class-key attributes nested-name-specifier identifier base-clause [opt]
11300 class-key attributes nested-name-specifier [opt] template-id
11303 Returns the TYPE of the indicated class. Sets
11304 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
11305 involving a nested-name-specifier was used, and FALSE otherwise.
11307 Returns NULL_TREE if the class-head is syntactically valid, but
11308 semantically invalid in a way that means we should skip the entire
11309 body of the class. */
11312 cp_parser_class_head (cp_parser* parser,
11313 bool* nested_name_specifier_p)
11316 tree nested_name_specifier;
11317 enum tag_types class_key;
11318 tree id = NULL_TREE;
11319 tree type = NULL_TREE;
11321 bool template_id_p = false;
11322 bool qualified_p = false;
11323 bool invalid_nested_name_p = false;
11324 unsigned num_templates;
11326 /* Assume no nested-name-specifier will be present. */
11327 *nested_name_specifier_p = false;
11328 /* Assume no template parameter lists will be used in defining the
11332 /* Look for the class-key. */
11333 class_key = cp_parser_class_key (parser);
11334 if (class_key == none_type)
11335 return error_mark_node;
11337 /* Parse the attributes. */
11338 attributes = cp_parser_attributes_opt (parser);
11340 /* If the next token is `::', that is invalid -- but sometimes
11341 people do try to write:
11345 Handle this gracefully by accepting the extra qualifier, and then
11346 issuing an error about it later if this really is a
11347 class-head. If it turns out just to be an elaborated type
11348 specifier, remain silent. */
11349 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
11350 qualified_p = true;
11352 push_deferring_access_checks (dk_no_check);
11354 /* Determine the name of the class. Begin by looking for an
11355 optional nested-name-specifier. */
11356 nested_name_specifier
11357 = cp_parser_nested_name_specifier_opt (parser,
11358 /*typename_keyword_p=*/false,
11359 /*check_dependency_p=*/false,
11361 /* If there was a nested-name-specifier, then there *must* be an
11363 if (nested_name_specifier)
11365 /* Although the grammar says `identifier', it really means
11366 `class-name' or `template-name'. You are only allowed to
11367 define a class that has already been declared with this
11370 The proposed resolution for Core Issue 180 says that whever
11371 you see `class T::X' you should treat `X' as a type-name.
11373 It is OK to define an inaccessible class; for example:
11375 class A { class B; };
11378 We do not know if we will see a class-name, or a
11379 template-name. We look for a class-name first, in case the
11380 class-name is a template-id; if we looked for the
11381 template-name first we would stop after the template-name. */
11382 cp_parser_parse_tentatively (parser);
11383 type = cp_parser_class_name (parser,
11384 /*typename_keyword_p=*/false,
11385 /*template_keyword_p=*/false,
11387 /*check_dependency_p=*/false,
11388 /*class_head_p=*/true);
11389 /* If that didn't work, ignore the nested-name-specifier. */
11390 if (!cp_parser_parse_definitely (parser))
11392 invalid_nested_name_p = true;
11393 id = cp_parser_identifier (parser);
11394 if (id == error_mark_node)
11397 /* If we could not find a corresponding TYPE, treat this
11398 declaration like an unqualified declaration. */
11399 if (type == error_mark_node)
11400 nested_name_specifier = NULL_TREE;
11401 /* Otherwise, count the number of templates used in TYPE and its
11402 containing scopes. */
11407 for (scope = TREE_TYPE (type);
11408 scope && TREE_CODE (scope) != NAMESPACE_DECL;
11409 scope = (TYPE_P (scope)
11410 ? TYPE_CONTEXT (scope)
11411 : DECL_CONTEXT (scope)))
11413 && CLASS_TYPE_P (scope)
11414 && CLASSTYPE_TEMPLATE_INFO (scope)
11415 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
11416 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
11420 /* Otherwise, the identifier is optional. */
11423 /* We don't know whether what comes next is a template-id,
11424 an identifier, or nothing at all. */
11425 cp_parser_parse_tentatively (parser);
11426 /* Check for a template-id. */
11427 id = cp_parser_template_id (parser,
11428 /*template_keyword_p=*/false,
11429 /*check_dependency_p=*/true);
11430 /* If that didn't work, it could still be an identifier. */
11431 if (!cp_parser_parse_definitely (parser))
11433 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
11434 id = cp_parser_identifier (parser);
11440 template_id_p = true;
11445 pop_deferring_access_checks ();
11447 /* If it's not a `:' or a `{' then we can't really be looking at a
11448 class-head, since a class-head only appears as part of a
11449 class-specifier. We have to detect this situation before calling
11450 xref_tag, since that has irreversible side-effects. */
11451 if (!cp_parser_next_token_starts_class_definition_p (parser))
11453 cp_parser_error (parser, "expected `{' or `:'");
11454 return error_mark_node;
11457 /* At this point, we're going ahead with the class-specifier, even
11458 if some other problem occurs. */
11459 cp_parser_commit_to_tentative_parse (parser);
11460 /* Issue the error about the overly-qualified name now. */
11462 cp_parser_error (parser,
11463 "global qualification of class name is invalid");
11464 else if (invalid_nested_name_p)
11465 cp_parser_error (parser,
11466 "qualified name does not name a class");
11467 /* Make sure that the right number of template parameters were
11469 if (!cp_parser_check_template_parameters (parser, num_templates))
11470 /* If something went wrong, there is no point in even trying to
11471 process the class-definition. */
11474 /* Look up the type. */
11477 type = TREE_TYPE (id);
11478 maybe_process_partial_specialization (type);
11480 else if (!nested_name_specifier)
11482 /* If the class was unnamed, create a dummy name. */
11484 id = make_anon_name ();
11485 type = xref_tag (class_key, id, attributes, /*globalize=*/false,
11486 parser->num_template_parameter_lists);
11495 template <typename T> struct S { struct T };
11496 template <typename T> struct S<T>::T { };
11498 we will get a TYPENAME_TYPE when processing the definition of
11499 `S::T'. We need to resolve it to the actual type before we
11500 try to define it. */
11501 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
11503 class_type = resolve_typename_type (TREE_TYPE (type),
11504 /*only_current_p=*/false);
11505 if (class_type != error_mark_node)
11506 type = TYPE_NAME (class_type);
11509 cp_parser_error (parser, "could not resolve typename type");
11510 type = error_mark_node;
11514 /* Figure out in what scope the declaration is being placed. */
11515 scope = current_scope ();
11517 scope = current_namespace;
11518 /* If that scope does not contain the scope in which the
11519 class was originally declared, the program is invalid. */
11520 if (scope && !is_ancestor (scope, CP_DECL_CONTEXT (type)))
11522 error ("declaration of `%D' in `%D' which does not "
11523 "enclose `%D'", type, scope, nested_name_specifier);
11528 A declarator-id shall not be qualified exception of the
11529 definition of a ... nested class outside of its class
11530 ... [or] a the definition or explicit instantiation of a
11531 class member of a namespace outside of its namespace. */
11532 if (scope == CP_DECL_CONTEXT (type))
11534 pedwarn ("extra qualification ignored");
11535 nested_name_specifier = NULL_TREE;
11538 maybe_process_partial_specialization (TREE_TYPE (type));
11539 class_type = current_class_type;
11540 /* Enter the scope indicated by the nested-name-specifier. */
11541 if (nested_name_specifier)
11542 push_scope (nested_name_specifier);
11543 /* Get the canonical version of this type. */
11544 type = TYPE_MAIN_DECL (TREE_TYPE (type));
11545 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
11546 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
11547 type = push_template_decl (type);
11548 type = TREE_TYPE (type);
11549 if (nested_name_specifier)
11550 *nested_name_specifier_p = true;
11552 /* Indicate whether this class was declared as a `class' or as a
11554 if (TREE_CODE (type) == RECORD_TYPE)
11555 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
11556 cp_parser_check_class_key (class_key, type);
11558 /* Enter the scope containing the class; the names of base classes
11559 should be looked up in that context. For example, given:
11561 struct A { struct B {}; struct C; };
11562 struct A::C : B {};
11565 if (nested_name_specifier)
11566 push_scope (nested_name_specifier);
11567 /* Now, look for the base-clause. */
11568 token = cp_lexer_peek_token (parser->lexer);
11569 if (token->type == CPP_COLON)
11573 /* Get the list of base-classes. */
11574 bases = cp_parser_base_clause (parser);
11575 /* Process them. */
11576 xref_basetypes (type, bases);
11578 /* Leave the scope given by the nested-name-specifier. We will
11579 enter the class scope itself while processing the members. */
11580 if (nested_name_specifier)
11581 pop_scope (nested_name_specifier);
11586 /* Parse a class-key.
11593 Returns the kind of class-key specified, or none_type to indicate
11596 static enum tag_types
11597 cp_parser_class_key (cp_parser* parser)
11600 enum tag_types tag_type;
11602 /* Look for the class-key. */
11603 token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
11607 /* Check to see if the TOKEN is a class-key. */
11608 tag_type = cp_parser_token_is_class_key (token);
11610 cp_parser_error (parser, "expected class-key");
11614 /* Parse an (optional) member-specification.
11616 member-specification:
11617 member-declaration member-specification [opt]
11618 access-specifier : member-specification [opt] */
11621 cp_parser_member_specification_opt (cp_parser* parser)
11628 /* Peek at the next token. */
11629 token = cp_lexer_peek_token (parser->lexer);
11630 /* If it's a `}', or EOF then we've seen all the members. */
11631 if (token->type == CPP_CLOSE_BRACE || token->type == CPP_EOF)
11634 /* See if this token is a keyword. */
11635 keyword = token->keyword;
11639 case RID_PROTECTED:
11641 /* Consume the access-specifier. */
11642 cp_lexer_consume_token (parser->lexer);
11643 /* Remember which access-specifier is active. */
11644 current_access_specifier = token->value;
11645 /* Look for the `:'. */
11646 cp_parser_require (parser, CPP_COLON, "`:'");
11650 /* Otherwise, the next construction must be a
11651 member-declaration. */
11652 cp_parser_member_declaration (parser);
11657 /* Parse a member-declaration.
11659 member-declaration:
11660 decl-specifier-seq [opt] member-declarator-list [opt] ;
11661 function-definition ; [opt]
11662 :: [opt] nested-name-specifier template [opt] unqualified-id ;
11664 template-declaration
11666 member-declarator-list:
11668 member-declarator-list , member-declarator
11671 declarator pure-specifier [opt]
11672 declarator constant-initializer [opt]
11673 identifier [opt] : constant-expression
11677 member-declaration:
11678 __extension__ member-declaration
11681 declarator attributes [opt] pure-specifier [opt]
11682 declarator attributes [opt] constant-initializer [opt]
11683 identifier [opt] attributes [opt] : constant-expression */
11686 cp_parser_member_declaration (cp_parser* parser)
11688 tree decl_specifiers;
11689 tree prefix_attributes;
11691 int declares_class_or_enum;
11694 int saved_pedantic;
11696 /* Check for the `__extension__' keyword. */
11697 if (cp_parser_extension_opt (parser, &saved_pedantic))
11700 cp_parser_member_declaration (parser);
11701 /* Restore the old value of the PEDANTIC flag. */
11702 pedantic = saved_pedantic;
11707 /* Check for a template-declaration. */
11708 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
11710 /* Parse the template-declaration. */
11711 cp_parser_template_declaration (parser, /*member_p=*/true);
11716 /* Check for a using-declaration. */
11717 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
11719 /* Parse the using-declaration. */
11720 cp_parser_using_declaration (parser);
11725 /* We can't tell whether we're looking at a declaration or a
11726 function-definition. */
11727 cp_parser_parse_tentatively (parser);
11729 /* Parse the decl-specifier-seq. */
11731 = cp_parser_decl_specifier_seq (parser,
11732 CP_PARSER_FLAGS_OPTIONAL,
11733 &prefix_attributes,
11734 &declares_class_or_enum);
11735 /* Check for an invalid type-name. */
11736 if (cp_parser_diagnose_invalid_type_name (parser))
11738 /* If there is no declarator, then the decl-specifier-seq should
11740 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
11742 /* If there was no decl-specifier-seq, and the next token is a
11743 `;', then we have something like:
11749 Each member-declaration shall declare at least one member
11750 name of the class. */
11751 if (!decl_specifiers)
11754 pedwarn ("extra semicolon");
11760 /* See if this declaration is a friend. */
11761 friend_p = cp_parser_friend_p (decl_specifiers);
11762 /* If there were decl-specifiers, check to see if there was
11763 a class-declaration. */
11764 type = check_tag_decl (decl_specifiers);
11765 /* Nested classes have already been added to the class, but
11766 a `friend' needs to be explicitly registered. */
11769 /* If the `friend' keyword was present, the friend must
11770 be introduced with a class-key. */
11771 if (!declares_class_or_enum)
11772 error ("a class-key must be used when declaring a friend");
11775 template <typename T> struct A {
11776 friend struct A<T>::B;
11779 A<T>::B will be represented by a TYPENAME_TYPE, and
11780 therefore not recognized by check_tag_decl. */
11785 for (specifier = decl_specifiers;
11787 specifier = TREE_CHAIN (specifier))
11789 tree s = TREE_VALUE (specifier);
11791 if (TREE_CODE (s) == IDENTIFIER_NODE
11792 && IDENTIFIER_GLOBAL_VALUE (s))
11793 type = IDENTIFIER_GLOBAL_VALUE (s);
11794 if (TREE_CODE (s) == TYPE_DECL)
11804 error ("friend declaration does not name a class or "
11807 make_friend_class (current_class_type, type);
11809 /* If there is no TYPE, an error message will already have
11813 /* An anonymous aggregate has to be handled specially; such
11814 a declaration really declares a data member (with a
11815 particular type), as opposed to a nested class. */
11816 else if (ANON_AGGR_TYPE_P (type))
11818 /* Remove constructors and such from TYPE, now that we
11819 know it is an anonymous aggregate. */
11820 fixup_anonymous_aggr (type);
11821 /* And make the corresponding data member. */
11822 decl = build_decl (FIELD_DECL, NULL_TREE, type);
11823 /* Add it to the class. */
11824 finish_member_declaration (decl);
11830 /* See if these declarations will be friends. */
11831 friend_p = cp_parser_friend_p (decl_specifiers);
11833 /* Keep going until we hit the `;' at the end of the
11835 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11837 tree attributes = NULL_TREE;
11838 tree first_attribute;
11840 /* Peek at the next token. */
11841 token = cp_lexer_peek_token (parser->lexer);
11843 /* Check for a bitfield declaration. */
11844 if (token->type == CPP_COLON
11845 || (token->type == CPP_NAME
11846 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
11852 /* Get the name of the bitfield. Note that we cannot just
11853 check TOKEN here because it may have been invalidated by
11854 the call to cp_lexer_peek_nth_token above. */
11855 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
11856 identifier = cp_parser_identifier (parser);
11858 identifier = NULL_TREE;
11860 /* Consume the `:' token. */
11861 cp_lexer_consume_token (parser->lexer);
11862 /* Get the width of the bitfield. */
11864 = cp_parser_constant_expression (parser,
11865 /*allow_non_constant=*/false,
11868 /* Look for attributes that apply to the bitfield. */
11869 attributes = cp_parser_attributes_opt (parser);
11870 /* Remember which attributes are prefix attributes and
11872 first_attribute = attributes;
11873 /* Combine the attributes. */
11874 attributes = chainon (prefix_attributes, attributes);
11876 /* Create the bitfield declaration. */
11877 decl = grokbitfield (identifier,
11880 /* Apply the attributes. */
11881 cplus_decl_attributes (&decl, attributes, /*flags=*/0);
11887 tree asm_specification;
11888 int ctor_dtor_or_conv_p;
11890 /* Parse the declarator. */
11892 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
11893 &ctor_dtor_or_conv_p);
11895 /* If something went wrong parsing the declarator, make sure
11896 that we at least consume some tokens. */
11897 if (declarator == error_mark_node)
11899 /* Skip to the end of the statement. */
11900 cp_parser_skip_to_end_of_statement (parser);
11904 cp_parser_check_for_definition_in_return_type
11905 (declarator, declares_class_or_enum);
11907 /* Look for an asm-specification. */
11908 asm_specification = cp_parser_asm_specification_opt (parser);
11909 /* Look for attributes that apply to the declaration. */
11910 attributes = cp_parser_attributes_opt (parser);
11911 /* Remember which attributes are prefix attributes and
11913 first_attribute = attributes;
11914 /* Combine the attributes. */
11915 attributes = chainon (prefix_attributes, attributes);
11917 /* If it's an `=', then we have a constant-initializer or a
11918 pure-specifier. It is not correct to parse the
11919 initializer before registering the member declaration
11920 since the member declaration should be in scope while
11921 its initializer is processed. However, the rest of the
11922 front end does not yet provide an interface that allows
11923 us to handle this correctly. */
11924 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11928 A pure-specifier shall be used only in the declaration of
11929 a virtual function.
11931 A member-declarator can contain a constant-initializer
11932 only if it declares a static member of integral or
11935 Therefore, if the DECLARATOR is for a function, we look
11936 for a pure-specifier; otherwise, we look for a
11937 constant-initializer. When we call `grokfield', it will
11938 perform more stringent semantics checks. */
11939 if (TREE_CODE (declarator) == CALL_EXPR)
11940 initializer = cp_parser_pure_specifier (parser);
11943 /* This declaration cannot be a function
11945 cp_parser_commit_to_tentative_parse (parser);
11946 /* Parse the initializer. */
11947 initializer = cp_parser_constant_initializer (parser);
11950 /* Otherwise, there is no initializer. */
11952 initializer = NULL_TREE;
11954 /* See if we are probably looking at a function
11955 definition. We are certainly not looking at at a
11956 member-declarator. Calling `grokfield' has
11957 side-effects, so we must not do it unless we are sure
11958 that we are looking at a member-declarator. */
11959 if (cp_parser_token_starts_function_definition_p
11960 (cp_lexer_peek_token (parser->lexer)))
11961 decl = error_mark_node;
11964 /* Create the declaration. */
11965 decl = grokfield (declarator, decl_specifiers,
11966 initializer, asm_specification,
11968 /* Any initialization must have been from a
11969 constant-expression. */
11970 if (decl && TREE_CODE (decl) == VAR_DECL && initializer)
11971 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = 1;
11975 /* Reset PREFIX_ATTRIBUTES. */
11976 while (attributes && TREE_CHAIN (attributes) != first_attribute)
11977 attributes = TREE_CHAIN (attributes);
11979 TREE_CHAIN (attributes) = NULL_TREE;
11981 /* If there is any qualification still in effect, clear it
11982 now; we will be starting fresh with the next declarator. */
11983 parser->scope = NULL_TREE;
11984 parser->qualifying_scope = NULL_TREE;
11985 parser->object_scope = NULL_TREE;
11986 /* If it's a `,', then there are more declarators. */
11987 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
11988 cp_lexer_consume_token (parser->lexer);
11989 /* If the next token isn't a `;', then we have a parse error. */
11990 else if (cp_lexer_next_token_is_not (parser->lexer,
11993 cp_parser_error (parser, "expected `;'");
11994 /* Skip tokens until we find a `;' */
11995 cp_parser_skip_to_end_of_statement (parser);
12002 /* Add DECL to the list of members. */
12004 finish_member_declaration (decl);
12006 if (TREE_CODE (decl) == FUNCTION_DECL)
12007 cp_parser_save_default_args (parser, decl);
12012 /* If everything went well, look for the `;'. */
12013 if (cp_parser_parse_definitely (parser))
12015 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
12019 /* Parse the function-definition. */
12020 decl = cp_parser_function_definition (parser, &friend_p);
12021 /* If the member was not a friend, declare it here. */
12023 finish_member_declaration (decl);
12024 /* Peek at the next token. */
12025 token = cp_lexer_peek_token (parser->lexer);
12026 /* If the next token is a semicolon, consume it. */
12027 if (token->type == CPP_SEMICOLON)
12028 cp_lexer_consume_token (parser->lexer);
12031 /* Parse a pure-specifier.
12036 Returns INTEGER_ZERO_NODE if a pure specifier is found.
12037 Otherwiser, ERROR_MARK_NODE is returned. */
12040 cp_parser_pure_specifier (cp_parser* parser)
12044 /* Look for the `=' token. */
12045 if (!cp_parser_require (parser, CPP_EQ, "`='"))
12046 return error_mark_node;
12047 /* Look for the `0' token. */
12048 token = cp_parser_require (parser, CPP_NUMBER, "`0'");
12049 /* Unfortunately, this will accept `0L' and `0x00' as well. We need
12050 to get information from the lexer about how the number was
12051 spelled in order to fix this problem. */
12052 if (!token || !integer_zerop (token->value))
12053 return error_mark_node;
12055 return integer_zero_node;
12058 /* Parse a constant-initializer.
12060 constant-initializer:
12061 = constant-expression
12063 Returns a representation of the constant-expression. */
12066 cp_parser_constant_initializer (cp_parser* parser)
12068 /* Look for the `=' token. */
12069 if (!cp_parser_require (parser, CPP_EQ, "`='"))
12070 return error_mark_node;
12072 /* It is invalid to write:
12074 struct S { static const int i = { 7 }; };
12077 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12079 cp_parser_error (parser,
12080 "a brace-enclosed initializer is not allowed here");
12081 /* Consume the opening brace. */
12082 cp_lexer_consume_token (parser->lexer);
12083 /* Skip the initializer. */
12084 cp_parser_skip_to_closing_brace (parser);
12085 /* Look for the trailing `}'. */
12086 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
12088 return error_mark_node;
12091 return cp_parser_constant_expression (parser,
12092 /*allow_non_constant=*/false,
12096 /* Derived classes [gram.class.derived] */
12098 /* Parse a base-clause.
12101 : base-specifier-list
12103 base-specifier-list:
12105 base-specifier-list , base-specifier
12107 Returns a TREE_LIST representing the base-classes, in the order in
12108 which they were declared. The representation of each node is as
12109 described by cp_parser_base_specifier.
12111 In the case that no bases are specified, this function will return
12112 NULL_TREE, not ERROR_MARK_NODE. */
12115 cp_parser_base_clause (cp_parser* parser)
12117 tree bases = NULL_TREE;
12119 /* Look for the `:' that begins the list. */
12120 cp_parser_require (parser, CPP_COLON, "`:'");
12122 /* Scan the base-specifier-list. */
12128 /* Look for the base-specifier. */
12129 base = cp_parser_base_specifier (parser);
12130 /* Add BASE to the front of the list. */
12131 if (base != error_mark_node)
12133 TREE_CHAIN (base) = bases;
12136 /* Peek at the next token. */
12137 token = cp_lexer_peek_token (parser->lexer);
12138 /* If it's not a comma, then the list is complete. */
12139 if (token->type != CPP_COMMA)
12141 /* Consume the `,'. */
12142 cp_lexer_consume_token (parser->lexer);
12145 /* PARSER->SCOPE may still be non-NULL at this point, if the last
12146 base class had a qualified name. However, the next name that
12147 appears is certainly not qualified. */
12148 parser->scope = NULL_TREE;
12149 parser->qualifying_scope = NULL_TREE;
12150 parser->object_scope = NULL_TREE;
12152 return nreverse (bases);
12155 /* Parse a base-specifier.
12158 :: [opt] nested-name-specifier [opt] class-name
12159 virtual access-specifier [opt] :: [opt] nested-name-specifier
12161 access-specifier virtual [opt] :: [opt] nested-name-specifier
12164 Returns a TREE_LIST. The TREE_PURPOSE will be one of
12165 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
12166 indicate the specifiers provided. The TREE_VALUE will be a TYPE
12167 (or the ERROR_MARK_NODE) indicating the type that was specified. */
12170 cp_parser_base_specifier (cp_parser* parser)
12174 bool virtual_p = false;
12175 bool duplicate_virtual_error_issued_p = false;
12176 bool duplicate_access_error_issued_p = false;
12177 bool class_scope_p, template_p;
12178 tree access = access_default_node;
12181 /* Process the optional `virtual' and `access-specifier'. */
12184 /* Peek at the next token. */
12185 token = cp_lexer_peek_token (parser->lexer);
12186 /* Process `virtual'. */
12187 switch (token->keyword)
12190 /* If `virtual' appears more than once, issue an error. */
12191 if (virtual_p && !duplicate_virtual_error_issued_p)
12193 cp_parser_error (parser,
12194 "`virtual' specified more than once in base-specified");
12195 duplicate_virtual_error_issued_p = true;
12200 /* Consume the `virtual' token. */
12201 cp_lexer_consume_token (parser->lexer);
12206 case RID_PROTECTED:
12208 /* If more than one access specifier appears, issue an
12210 if (access != access_default_node
12211 && !duplicate_access_error_issued_p)
12213 cp_parser_error (parser,
12214 "more than one access specifier in base-specified");
12215 duplicate_access_error_issued_p = true;
12218 access = ridpointers[(int) token->keyword];
12220 /* Consume the access-specifier. */
12221 cp_lexer_consume_token (parser->lexer);
12231 /* Look for the optional `::' operator. */
12232 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
12233 /* Look for the nested-name-specifier. The simplest way to
12238 The keyword `typename' is not permitted in a base-specifier or
12239 mem-initializer; in these contexts a qualified name that
12240 depends on a template-parameter is implicitly assumed to be a
12243 is to pretend that we have seen the `typename' keyword at this
12245 cp_parser_nested_name_specifier_opt (parser,
12246 /*typename_keyword_p=*/true,
12247 /*check_dependency_p=*/true,
12249 /* If the base class is given by a qualified name, assume that names
12250 we see are type names or templates, as appropriate. */
12251 class_scope_p = (parser->scope && TYPE_P (parser->scope));
12252 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
12254 /* Finally, look for the class-name. */
12255 type = cp_parser_class_name (parser,
12259 /*check_dependency_p=*/true,
12260 /*class_head_p=*/false);
12262 if (type == error_mark_node)
12263 return error_mark_node;
12265 return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
12268 /* Exception handling [gram.exception] */
12270 /* Parse an (optional) exception-specification.
12272 exception-specification:
12273 throw ( type-id-list [opt] )
12275 Returns a TREE_LIST representing the exception-specification. The
12276 TREE_VALUE of each node is a type. */
12279 cp_parser_exception_specification_opt (cp_parser* parser)
12284 /* Peek at the next token. */
12285 token = cp_lexer_peek_token (parser->lexer);
12286 /* If it's not `throw', then there's no exception-specification. */
12287 if (!cp_parser_is_keyword (token, RID_THROW))
12290 /* Consume the `throw'. */
12291 cp_lexer_consume_token (parser->lexer);
12293 /* Look for the `('. */
12294 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
12296 /* Peek at the next token. */
12297 token = cp_lexer_peek_token (parser->lexer);
12298 /* If it's not a `)', then there is a type-id-list. */
12299 if (token->type != CPP_CLOSE_PAREN)
12301 const char *saved_message;
12303 /* Types may not be defined in an exception-specification. */
12304 saved_message = parser->type_definition_forbidden_message;
12305 parser->type_definition_forbidden_message
12306 = "types may not be defined in an exception-specification";
12307 /* Parse the type-id-list. */
12308 type_id_list = cp_parser_type_id_list (parser);
12309 /* Restore the saved message. */
12310 parser->type_definition_forbidden_message = saved_message;
12313 type_id_list = empty_except_spec;
12315 /* Look for the `)'. */
12316 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
12318 return type_id_list;
12321 /* Parse an (optional) type-id-list.
12325 type-id-list , type-id
12327 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
12328 in the order that the types were presented. */
12331 cp_parser_type_id_list (cp_parser* parser)
12333 tree types = NULL_TREE;
12340 /* Get the next type-id. */
12341 type = cp_parser_type_id (parser);
12342 /* Add it to the list. */
12343 types = add_exception_specifier (types, type, /*complain=*/1);
12344 /* Peek at the next token. */
12345 token = cp_lexer_peek_token (parser->lexer);
12346 /* If it is not a `,', we are done. */
12347 if (token->type != CPP_COMMA)
12349 /* Consume the `,'. */
12350 cp_lexer_consume_token (parser->lexer);
12353 return nreverse (types);
12356 /* Parse a try-block.
12359 try compound-statement handler-seq */
12362 cp_parser_try_block (cp_parser* parser)
12366 cp_parser_require_keyword (parser, RID_TRY, "`try'");
12367 try_block = begin_try_block ();
12368 cp_parser_compound_statement (parser, false);
12369 finish_try_block (try_block);
12370 cp_parser_handler_seq (parser);
12371 finish_handler_sequence (try_block);
12376 /* Parse a function-try-block.
12378 function-try-block:
12379 try ctor-initializer [opt] function-body handler-seq */
12382 cp_parser_function_try_block (cp_parser* parser)
12385 bool ctor_initializer_p;
12387 /* Look for the `try' keyword. */
12388 if (!cp_parser_require_keyword (parser, RID_TRY, "`try'"))
12390 /* Let the rest of the front-end know where we are. */
12391 try_block = begin_function_try_block ();
12392 /* Parse the function-body. */
12394 = cp_parser_ctor_initializer_opt_and_function_body (parser);
12395 /* We're done with the `try' part. */
12396 finish_function_try_block (try_block);
12397 /* Parse the handlers. */
12398 cp_parser_handler_seq (parser);
12399 /* We're done with the handlers. */
12400 finish_function_handler_sequence (try_block);
12402 return ctor_initializer_p;
12405 /* Parse a handler-seq.
12408 handler handler-seq [opt] */
12411 cp_parser_handler_seq (cp_parser* parser)
12417 /* Parse the handler. */
12418 cp_parser_handler (parser);
12419 /* Peek at the next token. */
12420 token = cp_lexer_peek_token (parser->lexer);
12421 /* If it's not `catch' then there are no more handlers. */
12422 if (!cp_parser_is_keyword (token, RID_CATCH))
12427 /* Parse a handler.
12430 catch ( exception-declaration ) compound-statement */
12433 cp_parser_handler (cp_parser* parser)
12438 cp_parser_require_keyword (parser, RID_CATCH, "`catch'");
12439 handler = begin_handler ();
12440 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
12441 declaration = cp_parser_exception_declaration (parser);
12442 finish_handler_parms (declaration, handler);
12443 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
12444 cp_parser_compound_statement (parser, false);
12445 finish_handler (handler);
12448 /* Parse an exception-declaration.
12450 exception-declaration:
12451 type-specifier-seq declarator
12452 type-specifier-seq abstract-declarator
12456 Returns a VAR_DECL for the declaration, or NULL_TREE if the
12457 ellipsis variant is used. */
12460 cp_parser_exception_declaration (cp_parser* parser)
12462 tree type_specifiers;
12464 const char *saved_message;
12466 /* If it's an ellipsis, it's easy to handle. */
12467 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
12469 /* Consume the `...' token. */
12470 cp_lexer_consume_token (parser->lexer);
12474 /* Types may not be defined in exception-declarations. */
12475 saved_message = parser->type_definition_forbidden_message;
12476 parser->type_definition_forbidden_message
12477 = "types may not be defined in exception-declarations";
12479 /* Parse the type-specifier-seq. */
12480 type_specifiers = cp_parser_type_specifier_seq (parser);
12481 /* If it's a `)', then there is no declarator. */
12482 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
12483 declarator = NULL_TREE;
12485 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
12486 /*ctor_dtor_or_conv_p=*/NULL);
12488 /* Restore the saved message. */
12489 parser->type_definition_forbidden_message = saved_message;
12491 return start_handler_parms (type_specifiers, declarator);
12494 /* Parse a throw-expression.
12497 throw assignment-expression [opt]
12499 Returns a THROW_EXPR representing the throw-expression. */
12502 cp_parser_throw_expression (cp_parser* parser)
12506 cp_parser_require_keyword (parser, RID_THROW, "`throw'");
12507 /* We can't be sure if there is an assignment-expression or not. */
12508 cp_parser_parse_tentatively (parser);
12510 expression = cp_parser_assignment_expression (parser);
12511 /* If it didn't work, this is just a rethrow. */
12512 if (!cp_parser_parse_definitely (parser))
12513 expression = NULL_TREE;
12515 return build_throw (expression);
12518 /* GNU Extensions */
12520 /* Parse an (optional) asm-specification.
12523 asm ( string-literal )
12525 If the asm-specification is present, returns a STRING_CST
12526 corresponding to the string-literal. Otherwise, returns
12530 cp_parser_asm_specification_opt (cp_parser* parser)
12533 tree asm_specification;
12535 /* Peek at the next token. */
12536 token = cp_lexer_peek_token (parser->lexer);
12537 /* If the next token isn't the `asm' keyword, then there's no
12538 asm-specification. */
12539 if (!cp_parser_is_keyword (token, RID_ASM))
12542 /* Consume the `asm' token. */
12543 cp_lexer_consume_token (parser->lexer);
12544 /* Look for the `('. */
12545 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
12547 /* Look for the string-literal. */
12548 token = cp_parser_require (parser, CPP_STRING, "string-literal");
12550 asm_specification = token->value;
12552 asm_specification = NULL_TREE;
12554 /* Look for the `)'. */
12555 cp_parser_require (parser, CPP_CLOSE_PAREN, "`('");
12557 return asm_specification;
12560 /* Parse an asm-operand-list.
12564 asm-operand-list , asm-operand
12567 string-literal ( expression )
12568 [ string-literal ] string-literal ( expression )
12570 Returns a TREE_LIST representing the operands. The TREE_VALUE of
12571 each node is the expression. The TREE_PURPOSE is itself a
12572 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
12573 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
12574 is a STRING_CST for the string literal before the parenthesis. */
12577 cp_parser_asm_operand_list (cp_parser* parser)
12579 tree asm_operands = NULL_TREE;
12583 tree string_literal;
12588 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
12590 /* Consume the `[' token. */
12591 cp_lexer_consume_token (parser->lexer);
12592 /* Read the operand name. */
12593 name = cp_parser_identifier (parser);
12594 if (name != error_mark_node)
12595 name = build_string (IDENTIFIER_LENGTH (name),
12596 IDENTIFIER_POINTER (name));
12597 /* Look for the closing `]'. */
12598 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
12602 /* Look for the string-literal. */
12603 token = cp_parser_require (parser, CPP_STRING, "string-literal");
12604 string_literal = token ? token->value : error_mark_node;
12605 /* Look for the `('. */
12606 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
12607 /* Parse the expression. */
12608 expression = cp_parser_expression (parser);
12609 /* Look for the `)'. */
12610 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
12611 /* Add this operand to the list. */
12612 asm_operands = tree_cons (build_tree_list (name, string_literal),
12615 /* If the next token is not a `,', there are no more
12617 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12619 /* Consume the `,'. */
12620 cp_lexer_consume_token (parser->lexer);
12623 return nreverse (asm_operands);
12626 /* Parse an asm-clobber-list.
12630 asm-clobber-list , string-literal
12632 Returns a TREE_LIST, indicating the clobbers in the order that they
12633 appeared. The TREE_VALUE of each node is a STRING_CST. */
12636 cp_parser_asm_clobber_list (cp_parser* parser)
12638 tree clobbers = NULL_TREE;
12643 tree string_literal;
12645 /* Look for the string literal. */
12646 token = cp_parser_require (parser, CPP_STRING, "string-literal");
12647 string_literal = token ? token->value : error_mark_node;
12648 /* Add it to the list. */
12649 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
12650 /* If the next token is not a `,', then the list is
12652 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12654 /* Consume the `,' token. */
12655 cp_lexer_consume_token (parser->lexer);
12661 /* Parse an (optional) series of attributes.
12664 attributes attribute
12667 __attribute__ (( attribute-list [opt] ))
12669 The return value is as for cp_parser_attribute_list. */
12672 cp_parser_attributes_opt (cp_parser* parser)
12674 tree attributes = NULL_TREE;
12679 tree attribute_list;
12681 /* Peek at the next token. */
12682 token = cp_lexer_peek_token (parser->lexer);
12683 /* If it's not `__attribute__', then we're done. */
12684 if (token->keyword != RID_ATTRIBUTE)
12687 /* Consume the `__attribute__' keyword. */
12688 cp_lexer_consume_token (parser->lexer);
12689 /* Look for the two `(' tokens. */
12690 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
12691 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
12693 /* Peek at the next token. */
12694 token = cp_lexer_peek_token (parser->lexer);
12695 if (token->type != CPP_CLOSE_PAREN)
12696 /* Parse the attribute-list. */
12697 attribute_list = cp_parser_attribute_list (parser);
12699 /* If the next token is a `)', then there is no attribute
12701 attribute_list = NULL;
12703 /* Look for the two `)' tokens. */
12704 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
12705 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
12707 /* Add these new attributes to the list. */
12708 attributes = chainon (attributes, attribute_list);
12714 /* Parse an attribute-list.
12718 attribute-list , attribute
12722 identifier ( identifier )
12723 identifier ( identifier , expression-list )
12724 identifier ( expression-list )
12726 Returns a TREE_LIST. Each node corresponds to an attribute. THe
12727 TREE_PURPOSE of each node is the identifier indicating which
12728 attribute is in use. The TREE_VALUE represents the arguments, if
12732 cp_parser_attribute_list (cp_parser* parser)
12734 tree attribute_list = NULL_TREE;
12742 /* Look for the identifier. We also allow keywords here; for
12743 example `__attribute__ ((const))' is legal. */
12744 token = cp_lexer_peek_token (parser->lexer);
12745 if (token->type != CPP_NAME
12746 && token->type != CPP_KEYWORD)
12747 return error_mark_node;
12748 /* Consume the token. */
12749 token = cp_lexer_consume_token (parser->lexer);
12751 /* Save away the identifier that indicates which attribute this is. */
12752 identifier = token->value;
12753 attribute = build_tree_list (identifier, NULL_TREE);
12755 /* Peek at the next token. */
12756 token = cp_lexer_peek_token (parser->lexer);
12757 /* If it's an `(', then parse the attribute arguments. */
12758 if (token->type == CPP_OPEN_PAREN)
12762 arguments = (cp_parser_parenthesized_expression_list
12763 (parser, true, /*non_constant_p=*/NULL));
12764 /* Save the identifier and arguments away. */
12765 TREE_VALUE (attribute) = arguments;
12768 /* Add this attribute to the list. */
12769 TREE_CHAIN (attribute) = attribute_list;
12770 attribute_list = attribute;
12772 /* Now, look for more attributes. */
12773 token = cp_lexer_peek_token (parser->lexer);
12774 /* If the next token isn't a `,', we're done. */
12775 if (token->type != CPP_COMMA)
12778 /* Consume the commma and keep going. */
12779 cp_lexer_consume_token (parser->lexer);
12782 /* We built up the list in reverse order. */
12783 return nreverse (attribute_list);
12786 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
12787 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
12788 current value of the PEDANTIC flag, regardless of whether or not
12789 the `__extension__' keyword is present. The caller is responsible
12790 for restoring the value of the PEDANTIC flag. */
12793 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
12795 /* Save the old value of the PEDANTIC flag. */
12796 *saved_pedantic = pedantic;
12798 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
12800 /* Consume the `__extension__' token. */
12801 cp_lexer_consume_token (parser->lexer);
12802 /* We're not being pedantic while the `__extension__' keyword is
12812 /* Parse a label declaration.
12815 __label__ label-declarator-seq ;
12817 label-declarator-seq:
12818 identifier , label-declarator-seq
12822 cp_parser_label_declaration (cp_parser* parser)
12824 /* Look for the `__label__' keyword. */
12825 cp_parser_require_keyword (parser, RID_LABEL, "`__label__'");
12831 /* Look for an identifier. */
12832 identifier = cp_parser_identifier (parser);
12833 /* Declare it as a lobel. */
12834 finish_label_decl (identifier);
12835 /* If the next token is a `;', stop. */
12836 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12838 /* Look for the `,' separating the label declarations. */
12839 cp_parser_require (parser, CPP_COMMA, "`,'");
12842 /* Look for the final `;'. */
12843 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
12846 /* Support Functions */
12848 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
12849 NAME should have one of the representations used for an
12850 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
12851 is returned. If PARSER->SCOPE is a dependent type, then a
12852 SCOPE_REF is returned.
12854 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
12855 returned; the name was already resolved when the TEMPLATE_ID_EXPR
12856 was formed. Abstractly, such entities should not be passed to this
12857 function, because they do not need to be looked up, but it is
12858 simpler to check for this special case here, rather than at the
12861 In cases not explicitly covered above, this function returns a
12862 DECL, OVERLOAD, or baselink representing the result of the lookup.
12863 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
12866 If IS_TYPE is TRUE, bindings that do not refer to types are
12869 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
12872 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
12876 cp_parser_lookup_name (cp_parser *parser, tree name,
12877 bool is_type, bool is_namespace, bool check_dependency)
12880 tree object_type = parser->context->object_type;
12882 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
12883 no longer valid. Note that if we are parsing tentatively, and
12884 the parse fails, OBJECT_TYPE will be automatically restored. */
12885 parser->context->object_type = NULL_TREE;
12887 if (name == error_mark_node)
12888 return error_mark_node;
12890 /* A template-id has already been resolved; there is no lookup to
12892 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
12894 if (BASELINK_P (name))
12896 my_friendly_assert ((TREE_CODE (BASELINK_FUNCTIONS (name))
12897 == TEMPLATE_ID_EXPR),
12902 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
12903 it should already have been checked to make sure that the name
12904 used matches the type being destroyed. */
12905 if (TREE_CODE (name) == BIT_NOT_EXPR)
12909 /* Figure out to which type this destructor applies. */
12911 type = parser->scope;
12912 else if (object_type)
12913 type = object_type;
12915 type = current_class_type;
12916 /* If that's not a class type, there is no destructor. */
12917 if (!type || !CLASS_TYPE_P (type))
12918 return error_mark_node;
12919 /* If it was a class type, return the destructor. */
12920 return CLASSTYPE_DESTRUCTORS (type);
12923 /* By this point, the NAME should be an ordinary identifier. If
12924 the id-expression was a qualified name, the qualifying scope is
12925 stored in PARSER->SCOPE at this point. */
12926 my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE,
12929 /* Perform the lookup. */
12934 if (parser->scope == error_mark_node)
12935 return error_mark_node;
12937 /* If the SCOPE is dependent, the lookup must be deferred until
12938 the template is instantiated -- unless we are explicitly
12939 looking up names in uninstantiated templates. Even then, we
12940 cannot look up the name if the scope is not a class type; it
12941 might, for example, be a template type parameter. */
12942 dependent_p = (TYPE_P (parser->scope)
12943 && !(parser->in_declarator_p
12944 && currently_open_class (parser->scope))
12945 && dependent_type_p (parser->scope));
12946 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
12950 decl = build_nt (SCOPE_REF, parser->scope, name);
12952 /* The resolution to Core Issue 180 says that `struct A::B'
12953 should be considered a type-name, even if `A' is
12955 decl = TYPE_NAME (make_typename_type (parser->scope,
12961 /* If PARSER->SCOPE is a dependent type, then it must be a
12962 class type, and we must not be checking dependencies;
12963 otherwise, we would have processed this lookup above. So
12964 that PARSER->SCOPE is not considered a dependent base by
12965 lookup_member, we must enter the scope here. */
12967 push_scope (parser->scope);
12968 /* If the PARSER->SCOPE is a a template specialization, it
12969 may be instantiated during name lookup. In that case,
12970 errors may be issued. Even if we rollback the current
12971 tentative parse, those errors are valid. */
12972 decl = lookup_qualified_name (parser->scope, name, is_type,
12973 /*complain=*/true);
12975 pop_scope (parser->scope);
12977 parser->qualifying_scope = parser->scope;
12978 parser->object_scope = NULL_TREE;
12980 else if (object_type)
12982 tree object_decl = NULL_TREE;
12983 /* Look up the name in the scope of the OBJECT_TYPE, unless the
12984 OBJECT_TYPE is not a class. */
12985 if (CLASS_TYPE_P (object_type))
12986 /* If the OBJECT_TYPE is a template specialization, it may
12987 be instantiated during name lookup. In that case, errors
12988 may be issued. Even if we rollback the current tentative
12989 parse, those errors are valid. */
12990 object_decl = lookup_member (object_type,
12992 /*protect=*/0, is_type);
12993 /* Look it up in the enclosing context, too. */
12994 decl = lookup_name_real (name, is_type, /*nonclass=*/0,
12997 parser->object_scope = object_type;
12998 parser->qualifying_scope = NULL_TREE;
13000 decl = object_decl;
13004 decl = lookup_name_real (name, is_type, /*nonclass=*/0,
13007 parser->qualifying_scope = NULL_TREE;
13008 parser->object_scope = NULL_TREE;
13011 /* If the lookup failed, let our caller know. */
13013 || decl == error_mark_node
13014 || (TREE_CODE (decl) == FUNCTION_DECL
13015 && DECL_ANTICIPATED (decl)))
13016 return error_mark_node;
13018 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
13019 if (TREE_CODE (decl) == TREE_LIST)
13021 /* The error message we have to print is too complicated for
13022 cp_parser_error, so we incorporate its actions directly. */
13023 if (!cp_parser_simulate_error (parser))
13025 error ("reference to `%D' is ambiguous", name);
13026 print_candidates (decl);
13028 return error_mark_node;
13031 my_friendly_assert (DECL_P (decl)
13032 || TREE_CODE (decl) == OVERLOAD
13033 || TREE_CODE (decl) == SCOPE_REF
13034 || BASELINK_P (decl),
13037 /* If we have resolved the name of a member declaration, check to
13038 see if the declaration is accessible. When the name resolves to
13039 set of overloaded functions, accessibility is checked when
13040 overload resolution is done.
13042 During an explicit instantiation, access is not checked at all,
13043 as per [temp.explicit]. */
13045 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
13050 /* Like cp_parser_lookup_name, but for use in the typical case where
13051 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, and CHECK_DEPENDENCY is
13055 cp_parser_lookup_name_simple (cp_parser* parser, tree name)
13057 return cp_parser_lookup_name (parser, name,
13059 /*is_namespace=*/false,
13060 /*check_dependency=*/true);
13063 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
13064 the current context, return the TYPE_DECL. If TAG_NAME_P is
13065 true, the DECL indicates the class being defined in a class-head,
13066 or declared in an elaborated-type-specifier.
13068 Otherwise, return DECL. */
13071 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
13073 /* If the TEMPLATE_DECL is being declared as part of a class-head,
13074 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
13077 template <typename T> struct B;
13080 template <typename T> struct A::B {};
13082 Similarly, in a elaborated-type-specifier:
13084 namespace N { struct X{}; }
13087 template <typename T> friend struct N::X;
13090 However, if the DECL refers to a class type, and we are in
13091 the scope of the class, then the name lookup automatically
13092 finds the TYPE_DECL created by build_self_reference rather
13093 than a TEMPLATE_DECL. For example, in:
13095 template <class T> struct S {
13099 there is no need to handle such case. */
13101 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
13102 return DECL_TEMPLATE_RESULT (decl);
13107 /* If too many, or too few, template-parameter lists apply to the
13108 declarator, issue an error message. Returns TRUE if all went well,
13109 and FALSE otherwise. */
13112 cp_parser_check_declarator_template_parameters (cp_parser* parser,
13115 unsigned num_templates;
13117 /* We haven't seen any classes that involve template parameters yet. */
13120 switch (TREE_CODE (declarator))
13127 tree main_declarator = TREE_OPERAND (declarator, 0);
13129 cp_parser_check_declarator_template_parameters (parser,
13138 scope = TREE_OPERAND (declarator, 0);
13139 member = TREE_OPERAND (declarator, 1);
13141 /* If this is a pointer-to-member, then we are not interested
13142 in the SCOPE, because it does not qualify the thing that is
13144 if (TREE_CODE (member) == INDIRECT_REF)
13145 return (cp_parser_check_declarator_template_parameters
13148 while (scope && CLASS_TYPE_P (scope))
13150 /* You're supposed to have one `template <...>'
13151 for every template class, but you don't need one
13152 for a full specialization. For example:
13154 template <class T> struct S{};
13155 template <> struct S<int> { void f(); };
13156 void S<int>::f () {}
13158 is correct; there shouldn't be a `template <>' for
13159 the definition of `S<int>::f'. */
13160 if (CLASSTYPE_TEMPLATE_INFO (scope)
13161 && (CLASSTYPE_TEMPLATE_INSTANTIATION (scope)
13162 || uses_template_parms (CLASSTYPE_TI_ARGS (scope)))
13163 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
13166 scope = TYPE_CONTEXT (scope);
13170 /* Fall through. */
13173 /* If the DECLARATOR has the form `X<y>' then it uses one
13174 additional level of template parameters. */
13175 if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
13178 return cp_parser_check_template_parameters (parser,
13183 /* NUM_TEMPLATES were used in the current declaration. If that is
13184 invalid, return FALSE and issue an error messages. Otherwise,
13188 cp_parser_check_template_parameters (cp_parser* parser,
13189 unsigned num_templates)
13191 /* If there are more template classes than parameter lists, we have
13194 template <class T> void S<T>::R<T>::f (); */
13195 if (parser->num_template_parameter_lists < num_templates)
13197 error ("too few template-parameter-lists");
13200 /* If there are the same number of template classes and parameter
13201 lists, that's OK. */
13202 if (parser->num_template_parameter_lists == num_templates)
13204 /* If there are more, but only one more, then we are referring to a
13205 member template. That's OK too. */
13206 if (parser->num_template_parameter_lists == num_templates + 1)
13208 /* Otherwise, there are too many template parameter lists. We have
13211 template <class T> template <class U> void S::f(); */
13212 error ("too many template-parameter-lists");
13216 /* Parse a binary-expression of the general form:
13220 binary-expression <token> <expr>
13222 The TOKEN_TREE_MAP maps <token> types to <expr> codes. FN is used
13223 to parser the <expr>s. If the first production is used, then the
13224 value returned by FN is returned directly. Otherwise, a node with
13225 the indicated EXPR_TYPE is returned, with operands corresponding to
13226 the two sub-expressions. */
13229 cp_parser_binary_expression (cp_parser* parser,
13230 const cp_parser_token_tree_map token_tree_map,
13231 cp_parser_expression_fn fn)
13235 /* Parse the first expression. */
13236 lhs = (*fn) (parser);
13237 /* Now, look for more expressions. */
13241 const cp_parser_token_tree_map_node *map_node;
13244 /* Peek at the next token. */
13245 token = cp_lexer_peek_token (parser->lexer);
13246 /* If the token is `>', and that's not an operator at the
13247 moment, then we're done. */
13248 if (token->type == CPP_GREATER
13249 && !parser->greater_than_is_operator_p)
13251 /* If we find one of the tokens we want, build the corresponding
13252 tree representation. */
13253 for (map_node = token_tree_map;
13254 map_node->token_type != CPP_EOF;
13256 if (map_node->token_type == token->type)
13258 /* Consume the operator token. */
13259 cp_lexer_consume_token (parser->lexer);
13260 /* Parse the right-hand side of the expression. */
13261 rhs = (*fn) (parser);
13262 /* Build the binary tree node. */
13263 lhs = build_x_binary_op (map_node->tree_type, lhs, rhs);
13267 /* If the token wasn't one of the ones we want, we're done. */
13268 if (map_node->token_type == CPP_EOF)
13275 /* Parse an optional `::' token indicating that the following name is
13276 from the global namespace. If so, PARSER->SCOPE is set to the
13277 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
13278 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
13279 Returns the new value of PARSER->SCOPE, if the `::' token is
13280 present, and NULL_TREE otherwise. */
13283 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
13287 /* Peek at the next token. */
13288 token = cp_lexer_peek_token (parser->lexer);
13289 /* If we're looking at a `::' token then we're starting from the
13290 global namespace, not our current location. */
13291 if (token->type == CPP_SCOPE)
13293 /* Consume the `::' token. */
13294 cp_lexer_consume_token (parser->lexer);
13295 /* Set the SCOPE so that we know where to start the lookup. */
13296 parser->scope = global_namespace;
13297 parser->qualifying_scope = global_namespace;
13298 parser->object_scope = NULL_TREE;
13300 return parser->scope;
13302 else if (!current_scope_valid_p)
13304 parser->scope = NULL_TREE;
13305 parser->qualifying_scope = NULL_TREE;
13306 parser->object_scope = NULL_TREE;
13312 /* Returns TRUE if the upcoming token sequence is the start of a
13313 constructor declarator. If FRIEND_P is true, the declarator is
13314 preceded by the `friend' specifier. */
13317 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
13319 bool constructor_p;
13320 tree type_decl = NULL_TREE;
13321 bool nested_name_p;
13322 cp_token *next_token;
13324 /* The common case is that this is not a constructor declarator, so
13325 try to avoid doing lots of work if at all possible. It's not
13326 valid declare a constructor at function scope. */
13327 if (at_function_scope_p ())
13329 /* And only certain tokens can begin a constructor declarator. */
13330 next_token = cp_lexer_peek_token (parser->lexer);
13331 if (next_token->type != CPP_NAME
13332 && next_token->type != CPP_SCOPE
13333 && next_token->type != CPP_NESTED_NAME_SPECIFIER
13334 && next_token->type != CPP_TEMPLATE_ID)
13337 /* Parse tentatively; we are going to roll back all of the tokens
13339 cp_parser_parse_tentatively (parser);
13340 /* Assume that we are looking at a constructor declarator. */
13341 constructor_p = true;
13343 /* Look for the optional `::' operator. */
13344 cp_parser_global_scope_opt (parser,
13345 /*current_scope_valid_p=*/false);
13346 /* Look for the nested-name-specifier. */
13348 = (cp_parser_nested_name_specifier_opt (parser,
13349 /*typename_keyword_p=*/false,
13350 /*check_dependency_p=*/false,
13353 /* Outside of a class-specifier, there must be a
13354 nested-name-specifier. */
13355 if (!nested_name_p &&
13356 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
13358 constructor_p = false;
13359 /* If we still think that this might be a constructor-declarator,
13360 look for a class-name. */
13365 template <typename T> struct S { S(); };
13366 template <typename T> S<T>::S ();
13368 we must recognize that the nested `S' names a class.
13371 template <typename T> S<T>::S<T> ();
13373 we must recognize that the nested `S' names a template. */
13374 type_decl = cp_parser_class_name (parser,
13375 /*typename_keyword_p=*/false,
13376 /*template_keyword_p=*/false,
13378 /*check_dependency_p=*/false,
13379 /*class_head_p=*/false);
13380 /* If there was no class-name, then this is not a constructor. */
13381 constructor_p = !cp_parser_error_occurred (parser);
13384 /* If we're still considering a constructor, we have to see a `(',
13385 to begin the parameter-declaration-clause, followed by either a
13386 `)', an `...', or a decl-specifier. We need to check for a
13387 type-specifier to avoid being fooled into thinking that:
13391 is a constructor. (It is actually a function named `f' that
13392 takes one parameter (of type `int') and returns a value of type
13395 && cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
13397 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
13398 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
13399 && !cp_parser_storage_class_specifier_opt (parser))
13403 /* Names appearing in the type-specifier should be looked up
13404 in the scope of the class. */
13405 if (current_class_type)
13409 type = TREE_TYPE (type_decl);
13410 if (TREE_CODE (type) == TYPENAME_TYPE)
13412 type = resolve_typename_type (type,
13413 /*only_current_p=*/false);
13414 if (type == error_mark_node)
13416 cp_parser_abort_tentative_parse (parser);
13422 /* Look for the type-specifier. */
13423 cp_parser_type_specifier (parser,
13424 CP_PARSER_FLAGS_NONE,
13425 /*is_friend=*/false,
13426 /*is_declarator=*/true,
13427 /*declares_class_or_enum=*/NULL,
13428 /*is_cv_qualifier=*/NULL);
13429 /* Leave the scope of the class. */
13433 constructor_p = !cp_parser_error_occurred (parser);
13437 constructor_p = false;
13438 /* We did not really want to consume any tokens. */
13439 cp_parser_abort_tentative_parse (parser);
13441 return constructor_p;
13444 /* Parse the definition of the function given by the DECL_SPECIFIERS,
13445 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
13446 they must be performed once we are in the scope of the function.
13448 Returns the function defined. */
13451 cp_parser_function_definition_from_specifiers_and_declarator
13452 (cp_parser* parser,
13453 tree decl_specifiers,
13460 /* Begin the function-definition. */
13461 success_p = begin_function_definition (decl_specifiers,
13465 /* If there were names looked up in the decl-specifier-seq that we
13466 did not check, check them now. We must wait until we are in the
13467 scope of the function to perform the checks, since the function
13468 might be a friend. */
13469 perform_deferred_access_checks ();
13473 /* If begin_function_definition didn't like the definition, skip
13474 the entire function. */
13475 error ("invalid function declaration");
13476 cp_parser_skip_to_end_of_block_or_statement (parser);
13477 fn = error_mark_node;
13480 fn = cp_parser_function_definition_after_declarator (parser,
13481 /*inline_p=*/false);
13486 /* Parse the part of a function-definition that follows the
13487 declarator. INLINE_P is TRUE iff this function is an inline
13488 function defined with a class-specifier.
13490 Returns the function defined. */
13493 cp_parser_function_definition_after_declarator (cp_parser* parser,
13497 bool ctor_initializer_p = false;
13498 bool saved_in_unbraced_linkage_specification_p;
13499 unsigned saved_num_template_parameter_lists;
13501 /* If the next token is `return', then the code may be trying to
13502 make use of the "named return value" extension that G++ used to
13504 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
13506 /* Consume the `return' keyword. */
13507 cp_lexer_consume_token (parser->lexer);
13508 /* Look for the identifier that indicates what value is to be
13510 cp_parser_identifier (parser);
13511 /* Issue an error message. */
13512 error ("named return values are no longer supported");
13513 /* Skip tokens until we reach the start of the function body. */
13514 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
13515 cp_lexer_consume_token (parser->lexer);
13517 /* The `extern' in `extern "C" void f () { ... }' does not apply to
13518 anything declared inside `f'. */
13519 saved_in_unbraced_linkage_specification_p
13520 = parser->in_unbraced_linkage_specification_p;
13521 parser->in_unbraced_linkage_specification_p = false;
13522 /* Inside the function, surrounding template-parameter-lists do not
13524 saved_num_template_parameter_lists
13525 = parser->num_template_parameter_lists;
13526 parser->num_template_parameter_lists = 0;
13527 /* If the next token is `try', then we are looking at a
13528 function-try-block. */
13529 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
13530 ctor_initializer_p = cp_parser_function_try_block (parser);
13531 /* A function-try-block includes the function-body, so we only do
13532 this next part if we're not processing a function-try-block. */
13535 = cp_parser_ctor_initializer_opt_and_function_body (parser);
13537 /* Finish the function. */
13538 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
13539 (inline_p ? 2 : 0));
13540 /* Generate code for it, if necessary. */
13541 expand_or_defer_fn (fn);
13542 /* Restore the saved values. */
13543 parser->in_unbraced_linkage_specification_p
13544 = saved_in_unbraced_linkage_specification_p;
13545 parser->num_template_parameter_lists
13546 = saved_num_template_parameter_lists;
13551 /* Parse a template-declaration, assuming that the `export' (and
13552 `extern') keywords, if present, has already been scanned. MEMBER_P
13553 is as for cp_parser_template_declaration. */
13556 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
13558 tree decl = NULL_TREE;
13559 tree parameter_list;
13560 bool friend_p = false;
13562 /* Look for the `template' keyword. */
13563 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'"))
13567 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
13570 /* Parse the template parameters. */
13571 begin_template_parm_list ();
13572 /* If the next token is `>', then we have an invalid
13573 specialization. Rather than complain about an invalid template
13574 parameter, issue an error message here. */
13575 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
13577 cp_parser_error (parser, "invalid explicit specialization");
13578 parameter_list = NULL_TREE;
13581 parameter_list = cp_parser_template_parameter_list (parser);
13582 parameter_list = end_template_parm_list (parameter_list);
13583 /* Look for the `>'. */
13584 cp_parser_skip_until_found (parser, CPP_GREATER, "`>'");
13585 /* We just processed one more parameter list. */
13586 ++parser->num_template_parameter_lists;
13587 /* If the next token is `template', there are more template
13589 if (cp_lexer_next_token_is_keyword (parser->lexer,
13591 cp_parser_template_declaration_after_export (parser, member_p);
13594 decl = cp_parser_single_declaration (parser,
13598 /* If this is a member template declaration, let the front
13600 if (member_p && !friend_p && decl)
13601 decl = finish_member_template_decl (decl);
13602 else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
13603 make_friend_class (current_class_type, TREE_TYPE (decl));
13605 /* We are done with the current parameter list. */
13606 --parser->num_template_parameter_lists;
13609 finish_template_decl (parameter_list);
13611 /* Register member declarations. */
13612 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
13613 finish_member_declaration (decl);
13615 /* If DECL is a function template, we must return to parse it later.
13616 (Even though there is no definition, there might be default
13617 arguments that need handling.) */
13618 if (member_p && decl
13619 && (TREE_CODE (decl) == FUNCTION_DECL
13620 || DECL_FUNCTION_TEMPLATE_P (decl)))
13621 TREE_VALUE (parser->unparsed_functions_queues)
13622 = tree_cons (NULL_TREE, decl,
13623 TREE_VALUE (parser->unparsed_functions_queues));
13626 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
13627 `function-definition' sequence. MEMBER_P is true, this declaration
13628 appears in a class scope.
13630 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
13631 *FRIEND_P is set to TRUE iff the declaration is a friend. */
13634 cp_parser_single_declaration (cp_parser* parser,
13638 int declares_class_or_enum;
13639 tree decl = NULL_TREE;
13640 tree decl_specifiers;
13643 /* Parse the dependent declaration. We don't know yet
13644 whether it will be a function-definition. */
13645 cp_parser_parse_tentatively (parser);
13646 /* Defer access checks until we know what is being declared. */
13647 push_deferring_access_checks (dk_deferred);
13649 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
13652 = cp_parser_decl_specifier_seq (parser,
13653 CP_PARSER_FLAGS_OPTIONAL,
13655 &declares_class_or_enum);
13656 /* Gather up the access checks that occurred the
13657 decl-specifier-seq. */
13658 stop_deferring_access_checks ();
13660 /* Check for the declaration of a template class. */
13661 if (declares_class_or_enum)
13663 if (cp_parser_declares_only_class_p (parser))
13665 decl = shadow_tag (decl_specifiers);
13667 decl = TYPE_NAME (decl);
13669 decl = error_mark_node;
13674 /* If it's not a template class, try for a template function. If
13675 the next token is a `;', then this declaration does not declare
13676 anything. But, if there were errors in the decl-specifiers, then
13677 the error might well have come from an attempted class-specifier.
13678 In that case, there's no need to warn about a missing declarator. */
13680 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
13681 || !value_member (error_mark_node, decl_specifiers)))
13682 decl = cp_parser_init_declarator (parser,
13685 /*function_definition_allowed_p=*/false,
13687 declares_class_or_enum,
13688 /*function_definition_p=*/NULL);
13690 pop_deferring_access_checks ();
13692 /* Clear any current qualification; whatever comes next is the start
13693 of something new. */
13694 parser->scope = NULL_TREE;
13695 parser->qualifying_scope = NULL_TREE;
13696 parser->object_scope = NULL_TREE;
13697 /* Look for a trailing `;' after the declaration. */
13698 if (!cp_parser_require (parser, CPP_SEMICOLON, "`;'")
13699 && cp_parser_committed_to_tentative_parse (parser))
13700 cp_parser_skip_to_end_of_block_or_statement (parser);
13701 /* If it worked, set *FRIEND_P based on the DECL_SPECIFIERS. */
13702 if (cp_parser_parse_definitely (parser))
13705 *friend_p = cp_parser_friend_p (decl_specifiers);
13707 /* Otherwise, try a function-definition. */
13709 decl = cp_parser_function_definition (parser, friend_p);
13714 /* Parse a cast-expression that is not the operand of a unary "&". */
13717 cp_parser_simple_cast_expression (cp_parser *parser)
13719 return cp_parser_cast_expression (parser, /*address_p=*/false);
13722 /* Parse a functional cast to TYPE. Returns an expression
13723 representing the cast. */
13726 cp_parser_functional_cast (cp_parser* parser, tree type)
13728 tree expression_list;
13731 = cp_parser_parenthesized_expression_list (parser, false,
13732 /*non_constant_p=*/NULL);
13734 return build_functional_cast (type, expression_list);
13737 /* MEMBER_FUNCTION is a member function, or a friend. If default
13738 arguments, or the body of the function have not yet been parsed,
13742 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
13744 cp_lexer *saved_lexer;
13746 /* If this member is a template, get the underlying
13748 if (DECL_FUNCTION_TEMPLATE_P (member_function))
13749 member_function = DECL_TEMPLATE_RESULT (member_function);
13751 /* There should not be any class definitions in progress at this
13752 point; the bodies of members are only parsed outside of all class
13754 my_friendly_assert (parser->num_classes_being_defined == 0, 20010816);
13755 /* While we're parsing the member functions we might encounter more
13756 classes. We want to handle them right away, but we don't want
13757 them getting mixed up with functions that are currently in the
13759 parser->unparsed_functions_queues
13760 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
13762 /* Make sure that any template parameters are in scope. */
13763 maybe_begin_member_template_processing (member_function);
13765 /* If the body of the function has not yet been parsed, parse it
13767 if (DECL_PENDING_INLINE_P (member_function))
13769 tree function_scope;
13770 cp_token_cache *tokens;
13772 /* The function is no longer pending; we are processing it. */
13773 tokens = DECL_PENDING_INLINE_INFO (member_function);
13774 DECL_PENDING_INLINE_INFO (member_function) = NULL;
13775 DECL_PENDING_INLINE_P (member_function) = 0;
13776 /* If this was an inline function in a local class, enter the scope
13777 of the containing function. */
13778 function_scope = decl_function_context (member_function);
13779 if (function_scope)
13780 push_function_context_to (function_scope);
13782 /* Save away the current lexer. */
13783 saved_lexer = parser->lexer;
13784 /* Make a new lexer to feed us the tokens saved for this function. */
13785 parser->lexer = cp_lexer_new_from_tokens (tokens);
13786 parser->lexer->next = saved_lexer;
13788 /* Set the current source position to be the location of the first
13789 token in the saved inline body. */
13790 cp_lexer_peek_token (parser->lexer);
13792 /* Let the front end know that we going to be defining this
13794 start_function (NULL_TREE, member_function, NULL_TREE,
13795 SF_PRE_PARSED | SF_INCLASS_INLINE);
13797 /* Now, parse the body of the function. */
13798 cp_parser_function_definition_after_declarator (parser,
13799 /*inline_p=*/true);
13801 /* Leave the scope of the containing function. */
13802 if (function_scope)
13803 pop_function_context_from (function_scope);
13804 /* Restore the lexer. */
13805 parser->lexer = saved_lexer;
13808 /* Remove any template parameters from the symbol table. */
13809 maybe_end_member_template_processing ();
13811 /* Restore the queue. */
13812 parser->unparsed_functions_queues
13813 = TREE_CHAIN (parser->unparsed_functions_queues);
13816 /* If DECL contains any default args, remeber it on the unparsed
13817 functions queue. */
13820 cp_parser_save_default_args (cp_parser* parser, tree decl)
13824 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
13826 probe = TREE_CHAIN (probe))
13827 if (TREE_PURPOSE (probe))
13829 TREE_PURPOSE (parser->unparsed_functions_queues)
13830 = tree_cons (NULL_TREE, decl,
13831 TREE_PURPOSE (parser->unparsed_functions_queues));
13837 /* FN is a FUNCTION_DECL which may contains a parameter with an
13838 unparsed DEFAULT_ARG. Parse the default args now. */
13841 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
13843 cp_lexer *saved_lexer;
13844 cp_token_cache *tokens;
13845 bool saved_local_variables_forbidden_p;
13848 for (parameters = TYPE_ARG_TYPES (TREE_TYPE (fn));
13850 parameters = TREE_CHAIN (parameters))
13852 if (!TREE_PURPOSE (parameters)
13853 || TREE_CODE (TREE_PURPOSE (parameters)) != DEFAULT_ARG)
13856 /* Save away the current lexer. */
13857 saved_lexer = parser->lexer;
13858 /* Create a new one, using the tokens we have saved. */
13859 tokens = DEFARG_TOKENS (TREE_PURPOSE (parameters));
13860 parser->lexer = cp_lexer_new_from_tokens (tokens);
13862 /* Set the current source position to be the location of the
13863 first token in the default argument. */
13864 cp_lexer_peek_token (parser->lexer);
13866 /* Local variable names (and the `this' keyword) may not appear
13867 in a default argument. */
13868 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
13869 parser->local_variables_forbidden_p = true;
13870 /* Parse the assignment-expression. */
13871 if (DECL_CONTEXT (fn))
13872 push_nested_class (DECL_CONTEXT (fn));
13873 TREE_PURPOSE (parameters) = cp_parser_assignment_expression (parser);
13874 if (DECL_CONTEXT (fn))
13875 pop_nested_class ();
13877 /* Restore saved state. */
13878 parser->lexer = saved_lexer;
13879 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
13883 /* Parse the operand of `sizeof' (or a similar operator). Returns
13884 either a TYPE or an expression, depending on the form of the
13885 input. The KEYWORD indicates which kind of expression we have
13889 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
13891 static const char *format;
13892 tree expr = NULL_TREE;
13893 const char *saved_message;
13894 bool saved_constant_expression_p;
13896 /* Initialize FORMAT the first time we get here. */
13898 format = "types may not be defined in `%s' expressions";
13900 /* Types cannot be defined in a `sizeof' expression. Save away the
13902 saved_message = parser->type_definition_forbidden_message;
13903 /* And create the new one. */
13904 parser->type_definition_forbidden_message
13905 = xmalloc (strlen (format)
13906 + strlen (IDENTIFIER_POINTER (ridpointers[keyword]))
13908 sprintf ((char *) parser->type_definition_forbidden_message,
13909 format, IDENTIFIER_POINTER (ridpointers[keyword]));
13911 /* The restrictions on constant-expressions do not apply inside
13912 sizeof expressions. */
13913 saved_constant_expression_p = parser->constant_expression_p;
13914 parser->constant_expression_p = false;
13916 /* Do not actually evaluate the expression. */
13918 /* If it's a `(', then we might be looking at the type-id
13920 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
13924 /* We can't be sure yet whether we're looking at a type-id or an
13926 cp_parser_parse_tentatively (parser);
13927 /* Consume the `('. */
13928 cp_lexer_consume_token (parser->lexer);
13929 /* Parse the type-id. */
13930 type = cp_parser_type_id (parser);
13931 /* Now, look for the trailing `)'. */
13932 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13933 /* If all went well, then we're done. */
13934 if (cp_parser_parse_definitely (parser))
13936 /* Build a list of decl-specifiers; right now, we have only
13937 a single type-specifier. */
13938 type = build_tree_list (NULL_TREE,
13941 /* Call grokdeclarator to figure out what type this is. */
13942 expr = grokdeclarator (NULL_TREE,
13946 /*attrlist=*/NULL);
13950 /* If the type-id production did not work out, then we must be
13951 looking at the unary-expression production. */
13953 expr = cp_parser_unary_expression (parser, /*address_p=*/false);
13954 /* Go back to evaluating expressions. */
13957 /* Free the message we created. */
13958 free ((char *) parser->type_definition_forbidden_message);
13959 /* And restore the old one. */
13960 parser->type_definition_forbidden_message = saved_message;
13961 parser->constant_expression_p = saved_constant_expression_p;
13966 /* If the current declaration has no declarator, return true. */
13969 cp_parser_declares_only_class_p (cp_parser *parser)
13971 /* If the next token is a `;' or a `,' then there is no
13973 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
13974 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
13977 /* Simplify EXPR if it is a non-dependent expression. Returns the
13978 (possibly simplified) expression. */
13981 cp_parser_fold_non_dependent_expr (tree expr)
13983 /* If we're in a template, but EXPR isn't value dependent, simplify
13984 it. We're supposed to treat:
13986 template <typename T> void f(T[1 + 1]);
13987 template <typename T> void f(T[2]);
13989 as two declarations of the same function, for example. */
13990 if (processing_template_decl
13991 && !type_dependent_expression_p (expr)
13992 && !value_dependent_expression_p (expr))
13994 HOST_WIDE_INT saved_processing_template_decl;
13996 saved_processing_template_decl = processing_template_decl;
13997 processing_template_decl = 0;
13998 expr = tsubst_copy_and_build (expr,
13999 /*args=*/NULL_TREE,
14001 /*in_decl=*/NULL_TREE,
14002 /*function_p=*/false);
14003 processing_template_decl = saved_processing_template_decl;
14008 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
14009 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
14012 cp_parser_friend_p (tree decl_specifiers)
14014 while (decl_specifiers)
14016 /* See if this decl-specifier is `friend'. */
14017 if (TREE_CODE (TREE_VALUE (decl_specifiers)) == IDENTIFIER_NODE
14018 && C_RID_CODE (TREE_VALUE (decl_specifiers)) == RID_FRIEND)
14021 /* Go on to the next decl-specifier. */
14022 decl_specifiers = TREE_CHAIN (decl_specifiers);
14028 /* If the next token is of the indicated TYPE, consume it. Otherwise,
14029 issue an error message indicating that TOKEN_DESC was expected.
14031 Returns the token consumed, if the token had the appropriate type.
14032 Otherwise, returns NULL. */
14035 cp_parser_require (cp_parser* parser,
14036 enum cpp_ttype type,
14037 const char* token_desc)
14039 if (cp_lexer_next_token_is (parser->lexer, type))
14040 return cp_lexer_consume_token (parser->lexer);
14043 /* Output the MESSAGE -- unless we're parsing tentatively. */
14044 if (!cp_parser_simulate_error (parser))
14045 error ("expected %s", token_desc);
14050 /* Like cp_parser_require, except that tokens will be skipped until
14051 the desired token is found. An error message is still produced if
14052 the next token is not as expected. */
14055 cp_parser_skip_until_found (cp_parser* parser,
14056 enum cpp_ttype type,
14057 const char* token_desc)
14060 unsigned nesting_depth = 0;
14062 if (cp_parser_require (parser, type, token_desc))
14065 /* Skip tokens until the desired token is found. */
14068 /* Peek at the next token. */
14069 token = cp_lexer_peek_token (parser->lexer);
14070 /* If we've reached the token we want, consume it and
14072 if (token->type == type && !nesting_depth)
14074 cp_lexer_consume_token (parser->lexer);
14077 /* If we've run out of tokens, stop. */
14078 if (token->type == CPP_EOF)
14080 if (token->type == CPP_OPEN_BRACE
14081 || token->type == CPP_OPEN_PAREN
14082 || token->type == CPP_OPEN_SQUARE)
14084 else if (token->type == CPP_CLOSE_BRACE
14085 || token->type == CPP_CLOSE_PAREN
14086 || token->type == CPP_CLOSE_SQUARE)
14088 if (nesting_depth-- == 0)
14091 /* Consume this token. */
14092 cp_lexer_consume_token (parser->lexer);
14096 /* If the next token is the indicated keyword, consume it. Otherwise,
14097 issue an error message indicating that TOKEN_DESC was expected.
14099 Returns the token consumed, if the token had the appropriate type.
14100 Otherwise, returns NULL. */
14103 cp_parser_require_keyword (cp_parser* parser,
14105 const char* token_desc)
14107 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
14109 if (token && token->keyword != keyword)
14111 dyn_string_t error_msg;
14113 /* Format the error message. */
14114 error_msg = dyn_string_new (0);
14115 dyn_string_append_cstr (error_msg, "expected ");
14116 dyn_string_append_cstr (error_msg, token_desc);
14117 cp_parser_error (parser, error_msg->s);
14118 dyn_string_delete (error_msg);
14125 /* Returns TRUE iff TOKEN is a token that can begin the body of a
14126 function-definition. */
14129 cp_parser_token_starts_function_definition_p (cp_token* token)
14131 return (/* An ordinary function-body begins with an `{'. */
14132 token->type == CPP_OPEN_BRACE
14133 /* A ctor-initializer begins with a `:'. */
14134 || token->type == CPP_COLON
14135 /* A function-try-block begins with `try'. */
14136 || token->keyword == RID_TRY
14137 /* The named return value extension begins with `return'. */
14138 || token->keyword == RID_RETURN);
14141 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
14145 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
14149 token = cp_lexer_peek_token (parser->lexer);
14150 return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
14153 /* Returns TRUE iff the next token is the "," or ">" ending a
14154 template-argument. */
14157 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
14161 token = cp_lexer_peek_token (parser->lexer);
14162 return (token->type == CPP_COMMA || token->type == CPP_GREATER);
14165 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
14166 or none_type otherwise. */
14168 static enum tag_types
14169 cp_parser_token_is_class_key (cp_token* token)
14171 switch (token->keyword)
14176 return record_type;
14185 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
14188 cp_parser_check_class_key (enum tag_types class_key, tree type)
14190 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
14191 pedwarn ("`%s' tag used in naming `%#T'",
14192 class_key == union_type ? "union"
14193 : class_key == record_type ? "struct" : "class",
14197 /* Look for the `template' keyword, as a syntactic disambiguator.
14198 Return TRUE iff it is present, in which case it will be
14202 cp_parser_optional_template_keyword (cp_parser *parser)
14204 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
14206 /* The `template' keyword can only be used within templates;
14207 outside templates the parser can always figure out what is a
14208 template and what is not. */
14209 if (!processing_template_decl)
14211 error ("`template' (as a disambiguator) is only allowed "
14212 "within templates");
14213 /* If this part of the token stream is rescanned, the same
14214 error message would be generated. So, we purge the token
14215 from the stream. */
14216 cp_lexer_purge_token (parser->lexer);
14221 /* Consume the `template' keyword. */
14222 cp_lexer_consume_token (parser->lexer);
14230 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
14231 set PARSER->SCOPE, and perform other related actions. */
14234 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
14239 /* Get the stored value. */
14240 value = cp_lexer_consume_token (parser->lexer)->value;
14241 /* Perform any access checks that were deferred. */
14242 for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
14243 perform_or_defer_access_check (TREE_PURPOSE (check), TREE_VALUE (check));
14244 /* Set the scope from the stored value. */
14245 parser->scope = TREE_VALUE (value);
14246 parser->qualifying_scope = TREE_TYPE (value);
14247 parser->object_scope = NULL_TREE;
14250 /* Add tokens to CACHE until an non-nested END token appears. */
14253 cp_parser_cache_group (cp_parser *parser,
14254 cp_token_cache *cache,
14255 enum cpp_ttype end,
14262 /* Abort a parenthesized expression if we encounter a brace. */
14263 if ((end == CPP_CLOSE_PAREN || depth == 0)
14264 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
14266 /* Consume the next token. */
14267 token = cp_lexer_consume_token (parser->lexer);
14268 /* If we've reached the end of the file, stop. */
14269 if (token->type == CPP_EOF)
14271 /* Add this token to the tokens we are saving. */
14272 cp_token_cache_push_token (cache, token);
14273 /* See if it starts a new group. */
14274 if (token->type == CPP_OPEN_BRACE)
14276 cp_parser_cache_group (parser, cache, CPP_CLOSE_BRACE, depth + 1);
14280 else if (token->type == CPP_OPEN_PAREN)
14281 cp_parser_cache_group (parser, cache, CPP_CLOSE_PAREN, depth + 1);
14282 else if (token->type == end)
14287 /* Begin parsing tentatively. We always save tokens while parsing
14288 tentatively so that if the tentative parsing fails we can restore the
14292 cp_parser_parse_tentatively (cp_parser* parser)
14294 /* Enter a new parsing context. */
14295 parser->context = cp_parser_context_new (parser->context);
14296 /* Begin saving tokens. */
14297 cp_lexer_save_tokens (parser->lexer);
14298 /* In order to avoid repetitive access control error messages,
14299 access checks are queued up until we are no longer parsing
14301 push_deferring_access_checks (dk_deferred);
14304 /* Commit to the currently active tentative parse. */
14307 cp_parser_commit_to_tentative_parse (cp_parser* parser)
14309 cp_parser_context *context;
14312 /* Mark all of the levels as committed. */
14313 lexer = parser->lexer;
14314 for (context = parser->context; context->next; context = context->next)
14316 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
14318 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
14319 while (!cp_lexer_saving_tokens (lexer))
14320 lexer = lexer->next;
14321 cp_lexer_commit_tokens (lexer);
14325 /* Abort the currently active tentative parse. All consumed tokens
14326 will be rolled back, and no diagnostics will be issued. */
14329 cp_parser_abort_tentative_parse (cp_parser* parser)
14331 cp_parser_simulate_error (parser);
14332 /* Now, pretend that we want to see if the construct was
14333 successfully parsed. */
14334 cp_parser_parse_definitely (parser);
14337 /* Stop parsing tentatively. If a parse error has occurred, restore the
14338 token stream. Otherwise, commit to the tokens we have consumed.
14339 Returns true if no error occurred; false otherwise. */
14342 cp_parser_parse_definitely (cp_parser* parser)
14344 bool error_occurred;
14345 cp_parser_context *context;
14347 /* Remember whether or not an error occurred, since we are about to
14348 destroy that information. */
14349 error_occurred = cp_parser_error_occurred (parser);
14350 /* Remove the topmost context from the stack. */
14351 context = parser->context;
14352 parser->context = context->next;
14353 /* If no parse errors occurred, commit to the tentative parse. */
14354 if (!error_occurred)
14356 /* Commit to the tokens read tentatively, unless that was
14358 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
14359 cp_lexer_commit_tokens (parser->lexer);
14361 pop_to_parent_deferring_access_checks ();
14363 /* Otherwise, if errors occurred, roll back our state so that things
14364 are just as they were before we began the tentative parse. */
14367 cp_lexer_rollback_tokens (parser->lexer);
14368 pop_deferring_access_checks ();
14370 /* Add the context to the front of the free list. */
14371 context->next = cp_parser_context_free_list;
14372 cp_parser_context_free_list = context;
14374 return !error_occurred;
14377 /* Returns true if we are parsing tentatively -- but have decided that
14378 we will stick with this tentative parse, even if errors occur. */
14381 cp_parser_committed_to_tentative_parse (cp_parser* parser)
14383 return (cp_parser_parsing_tentatively (parser)
14384 && parser->context->status == CP_PARSER_STATUS_KIND_COMMITTED);
14387 /* Returns nonzero iff an error has occurred during the most recent
14388 tentative parse. */
14391 cp_parser_error_occurred (cp_parser* parser)
14393 return (cp_parser_parsing_tentatively (parser)
14394 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
14397 /* Returns nonzero if GNU extensions are allowed. */
14400 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
14402 return parser->allow_gnu_extensions_p;
14409 static GTY (()) cp_parser *the_parser;
14411 /* External interface. */
14413 /* Parse one entire translation unit. */
14416 c_parse_file (void)
14418 bool error_occurred;
14420 the_parser = cp_parser_new ();
14421 push_deferring_access_checks (flag_access_control
14422 ? dk_no_deferred : dk_no_check);
14423 error_occurred = cp_parser_translation_unit (the_parser);
14427 /* Clean up after parsing the entire translation unit. */
14430 free_parser_stacks (void)
14432 /* Nothing to do. */
14435 /* This variable must be provided by every front end. */
14439 #include "gt-cp-parser.h"