2 Copyright (C) 2000, 2001, 2002, 2003, 2004 Free Software Foundation, Inc.
3 Written by Mark Mitchell <mark@codesourcery.com>.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 GCC is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
24 #include "coretypes.h"
26 #include "dyn-string.h"
34 #include "diagnostic.h"
44 A cp_lexer represents a stream of cp_tokens. It allows arbitrary
50 We use a circular buffer to store incoming tokens.
52 Some artifacts of the C++ language (such as the
53 expression/declaration ambiguity) require arbitrary look-ahead.
54 The strategy we adopt for dealing with these problems is to attempt
55 to parse one construct (e.g., the declaration) and fall back to the
56 other (e.g., the expression) if that attempt does not succeed.
57 Therefore, we must sometimes store an arbitrary number of tokens.
59 The parser routinely peeks at the next token, and then consumes it
60 later. That also requires a buffer in which to store the tokens.
62 In order to easily permit adding tokens to the end of the buffer,
63 while removing them from the beginning of the buffer, we use a
68 typedef struct cp_token GTY (())
70 /* The kind of token. */
71 ENUM_BITFIELD (cpp_ttype) type : 8;
72 /* If this token is a keyword, this value indicates which keyword.
73 Otherwise, this value is RID_MAX. */
74 ENUM_BITFIELD (rid) keyword : 8;
77 /* The value associated with this token, if any. */
79 /* The location at which this token was found. */
83 /* The number of tokens in a single token block.
84 Computed so that cp_token_block fits in a 512B allocation unit. */
86 #define CP_TOKEN_BLOCK_NUM_TOKENS ((512 - 3*sizeof (char*))/sizeof (cp_token))
88 /* A group of tokens. These groups are chained together to store
89 large numbers of tokens. (For example, a token block is created
90 when the body of an inline member function is first encountered;
91 the tokens are processed later after the class definition is
94 This somewhat ungainly data structure (as opposed to, say, a
95 variable-length array), is used due to constraints imposed by the
96 current garbage-collection methodology. If it is made more
97 flexible, we could perhaps simplify the data structures involved. */
99 typedef struct cp_token_block GTY (())
102 cp_token tokens[CP_TOKEN_BLOCK_NUM_TOKENS];
103 /* The number of tokens in this block. */
105 /* The next token block in the chain. */
106 struct cp_token_block *next;
107 /* The previous block in the chain. */
108 struct cp_token_block *prev;
111 typedef struct cp_token_cache GTY (())
113 /* The first block in the cache. NULL if there are no tokens in the
115 cp_token_block *first;
116 /* The last block in the cache. NULL If there are no tokens in the
118 cp_token_block *last;
123 static cp_token_cache *cp_token_cache_new
125 static void cp_token_cache_push_token
126 (cp_token_cache *, cp_token *);
128 /* Create a new cp_token_cache. */
130 static cp_token_cache *
131 cp_token_cache_new (void)
133 return ggc_alloc_cleared (sizeof (cp_token_cache));
136 /* Add *TOKEN to *CACHE. */
139 cp_token_cache_push_token (cp_token_cache *cache,
142 cp_token_block *b = cache->last;
144 /* See if we need to allocate a new token block. */
145 if (!b || b->num_tokens == CP_TOKEN_BLOCK_NUM_TOKENS)
147 b = ggc_alloc_cleared (sizeof (cp_token_block));
148 b->prev = cache->last;
151 cache->last->next = b;
155 cache->first = cache->last = b;
157 /* Add this token to the current token block. */
158 b->tokens[b->num_tokens++] = *token;
161 /* The cp_lexer structure represents the C++ lexer. It is responsible
162 for managing the token stream from the preprocessor and supplying
165 typedef struct cp_lexer GTY (())
167 /* The memory allocated for the buffer. Never NULL. */
168 cp_token * GTY ((length ("(%h.buffer_end - %h.buffer)"))) buffer;
169 /* A pointer just past the end of the memory allocated for the buffer. */
170 cp_token * GTY ((skip)) buffer_end;
171 /* The first valid token in the buffer, or NULL if none. */
172 cp_token * GTY ((skip)) first_token;
173 /* The next available token. If NEXT_TOKEN is NULL, then there are
174 no more available tokens. */
175 cp_token * GTY ((skip)) next_token;
176 /* A pointer just past the last available token. If FIRST_TOKEN is
177 NULL, however, there are no available tokens, and then this
178 location is simply the place in which the next token read will be
179 placed. If LAST_TOKEN == FIRST_TOKEN, then the buffer is full.
180 When the LAST_TOKEN == BUFFER, then the last token is at the
181 highest memory address in the BUFFER. */
182 cp_token * GTY ((skip)) last_token;
184 /* A stack indicating positions at which cp_lexer_save_tokens was
185 called. The top entry is the most recent position at which we
186 began saving tokens. The entries are differences in token
187 position between FIRST_TOKEN and the first saved token.
189 If the stack is non-empty, we are saving tokens. When a token is
190 consumed, the NEXT_TOKEN pointer will move, but the FIRST_TOKEN
191 pointer will not. The token stream will be preserved so that it
192 can be reexamined later.
194 If the stack is empty, then we are not saving tokens. Whenever a
195 token is consumed, the FIRST_TOKEN pointer will be moved, and the
196 consumed token will be gone forever. */
197 varray_type saved_tokens;
199 /* The STRING_CST tokens encountered while processing the current
201 varray_type string_tokens;
203 /* True if we should obtain more tokens from the preprocessor; false
204 if we are processing a saved token cache. */
207 /* True if we should output debugging information. */
210 /* The next lexer in a linked list of lexers. */
211 struct cp_lexer *next;
216 static cp_lexer *cp_lexer_new_main
218 static cp_lexer *cp_lexer_new_from_tokens
219 (struct cp_token_cache *);
220 static int cp_lexer_saving_tokens
222 static cp_token *cp_lexer_next_token
223 (cp_lexer *, cp_token *);
224 static cp_token *cp_lexer_prev_token
225 (cp_lexer *, cp_token *);
226 static ptrdiff_t cp_lexer_token_difference
227 (cp_lexer *, cp_token *, cp_token *);
228 static cp_token *cp_lexer_read_token
230 static void cp_lexer_maybe_grow_buffer
232 static void cp_lexer_get_preprocessor_token
233 (cp_lexer *, cp_token *);
234 static cp_token *cp_lexer_peek_token
236 static cp_token *cp_lexer_peek_nth_token
237 (cp_lexer *, size_t);
238 static inline bool cp_lexer_next_token_is
239 (cp_lexer *, enum cpp_ttype);
240 static bool cp_lexer_next_token_is_not
241 (cp_lexer *, enum cpp_ttype);
242 static bool cp_lexer_next_token_is_keyword
243 (cp_lexer *, enum rid);
244 static cp_token *cp_lexer_consume_token
246 static void cp_lexer_purge_token
248 static void cp_lexer_purge_tokens_after
249 (cp_lexer *, cp_token *);
250 static void cp_lexer_save_tokens
252 static void cp_lexer_commit_tokens
254 static void cp_lexer_rollback_tokens
256 static inline void cp_lexer_set_source_position_from_token
257 (cp_lexer *, const cp_token *);
258 static void cp_lexer_print_token
259 (FILE *, cp_token *);
260 static inline bool cp_lexer_debugging_p
262 static void cp_lexer_start_debugging
263 (cp_lexer *) ATTRIBUTE_UNUSED;
264 static void cp_lexer_stop_debugging
265 (cp_lexer *) ATTRIBUTE_UNUSED;
267 /* Manifest constants. */
269 #define CP_TOKEN_BUFFER_SIZE 5
270 #define CP_SAVED_TOKENS_SIZE 5
272 /* A token type for keywords, as opposed to ordinary identifiers. */
273 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
275 /* A token type for template-ids. If a template-id is processed while
276 parsing tentatively, it is replaced with a CPP_TEMPLATE_ID token;
277 the value of the CPP_TEMPLATE_ID is whatever was returned by
278 cp_parser_template_id. */
279 #define CPP_TEMPLATE_ID ((enum cpp_ttype) (CPP_KEYWORD + 1))
281 /* A token type for nested-name-specifiers. If a
282 nested-name-specifier is processed while parsing tentatively, it is
283 replaced with a CPP_NESTED_NAME_SPECIFIER token; the value of the
284 CPP_NESTED_NAME_SPECIFIER is whatever was returned by
285 cp_parser_nested_name_specifier_opt. */
286 #define CPP_NESTED_NAME_SPECIFIER ((enum cpp_ttype) (CPP_TEMPLATE_ID + 1))
288 /* A token type for tokens that are not tokens at all; these are used
289 to mark the end of a token block. */
290 #define CPP_NONE (CPP_NESTED_NAME_SPECIFIER + 1)
294 /* The stream to which debugging output should be written. */
295 static FILE *cp_lexer_debug_stream;
297 /* Create a new main C++ lexer, the lexer that gets tokens from the
301 cp_lexer_new_main (void)
304 cp_token first_token;
306 /* It's possible that lexing the first token will load a PCH file,
307 which is a GC collection point. So we have to grab the first
308 token before allocating any memory. */
309 cp_lexer_get_preprocessor_token (NULL, &first_token);
310 c_common_no_more_pch ();
312 /* Allocate the memory. */
313 lexer = ggc_alloc_cleared (sizeof (cp_lexer));
315 /* Create the circular buffer. */
316 lexer->buffer = ggc_calloc (CP_TOKEN_BUFFER_SIZE, sizeof (cp_token));
317 lexer->buffer_end = lexer->buffer + CP_TOKEN_BUFFER_SIZE;
319 /* There is one token in the buffer. */
320 lexer->last_token = lexer->buffer + 1;
321 lexer->first_token = lexer->buffer;
322 lexer->next_token = lexer->buffer;
323 memcpy (lexer->buffer, &first_token, sizeof (cp_token));
325 /* This lexer obtains more tokens by calling c_lex. */
326 lexer->main_lexer_p = true;
328 /* Create the SAVED_TOKENS stack. */
329 VARRAY_INT_INIT (lexer->saved_tokens, CP_SAVED_TOKENS_SIZE, "saved_tokens");
331 /* Create the STRINGS array. */
332 VARRAY_TREE_INIT (lexer->string_tokens, 32, "strings");
334 /* Assume we are not debugging. */
335 lexer->debugging_p = false;
340 /* Create a new lexer whose token stream is primed with the TOKENS.
341 When these tokens are exhausted, no new tokens will be read. */
344 cp_lexer_new_from_tokens (cp_token_cache *tokens)
348 cp_token_block *block;
349 ptrdiff_t num_tokens;
351 /* Allocate the memory. */
352 lexer = ggc_alloc_cleared (sizeof (cp_lexer));
354 /* Create a new buffer, appropriately sized. */
356 for (block = tokens->first; block != NULL; block = block->next)
357 num_tokens += block->num_tokens;
358 lexer->buffer = ggc_alloc (num_tokens * sizeof (cp_token));
359 lexer->buffer_end = lexer->buffer + num_tokens;
361 /* Install the tokens. */
362 token = lexer->buffer;
363 for (block = tokens->first; block != NULL; block = block->next)
365 memcpy (token, block->tokens, block->num_tokens * sizeof (cp_token));
366 token += block->num_tokens;
369 /* The FIRST_TOKEN is the beginning of the buffer. */
370 lexer->first_token = lexer->buffer;
371 /* The next available token is also at the beginning of the buffer. */
372 lexer->next_token = lexer->buffer;
373 /* The buffer is full. */
374 lexer->last_token = lexer->first_token;
376 /* This lexer doesn't obtain more tokens. */
377 lexer->main_lexer_p = false;
379 /* Create the SAVED_TOKENS stack. */
380 VARRAY_INT_INIT (lexer->saved_tokens, CP_SAVED_TOKENS_SIZE, "saved_tokens");
382 /* Create the STRINGS array. */
383 VARRAY_TREE_INIT (lexer->string_tokens, 32, "strings");
385 /* Assume we are not debugging. */
386 lexer->debugging_p = false;
391 /* Returns nonzero if debugging information should be output. */
394 cp_lexer_debugging_p (cp_lexer *lexer)
396 return lexer->debugging_p;
399 /* Set the current source position from the information stored in
403 cp_lexer_set_source_position_from_token (cp_lexer *lexer ATTRIBUTE_UNUSED ,
404 const cp_token *token)
406 /* Ideally, the source position information would not be a global
407 variable, but it is. */
409 /* Update the line number. */
410 if (token->type != CPP_EOF)
411 input_location = token->location;
414 /* TOKEN points into the circular token buffer. Return a pointer to
415 the next token in the buffer. */
417 static inline cp_token *
418 cp_lexer_next_token (cp_lexer* lexer, cp_token* token)
421 if (token == lexer->buffer_end)
422 token = lexer->buffer;
426 /* TOKEN points into the circular token buffer. Return a pointer to
427 the previous token in the buffer. */
429 static inline cp_token *
430 cp_lexer_prev_token (cp_lexer* lexer, cp_token* token)
432 if (token == lexer->buffer)
433 token = lexer->buffer_end;
437 /* nonzero if we are presently saving tokens. */
440 cp_lexer_saving_tokens (const cp_lexer* lexer)
442 return VARRAY_ACTIVE_SIZE (lexer->saved_tokens) != 0;
445 /* Return a pointer to the token that is N tokens beyond TOKEN in the
449 cp_lexer_advance_token (cp_lexer *lexer, cp_token *token, ptrdiff_t n)
452 if (token >= lexer->buffer_end)
453 token = lexer->buffer + (token - lexer->buffer_end);
457 /* Returns the number of times that START would have to be incremented
458 to reach FINISH. If START and FINISH are the same, returns zero. */
461 cp_lexer_token_difference (cp_lexer* lexer, cp_token* start, cp_token* finish)
464 return finish - start;
466 return ((lexer->buffer_end - lexer->buffer)
470 /* Obtain another token from the C preprocessor and add it to the
471 token buffer. Returns the newly read token. */
474 cp_lexer_read_token (cp_lexer* lexer)
478 /* Make sure there is room in the buffer. */
479 cp_lexer_maybe_grow_buffer (lexer);
481 /* If there weren't any tokens, then this one will be the first. */
482 if (!lexer->first_token)
483 lexer->first_token = lexer->last_token;
484 /* Similarly, if there were no available tokens, there is one now. */
485 if (!lexer->next_token)
486 lexer->next_token = lexer->last_token;
488 /* Figure out where we're going to store the new token. */
489 token = lexer->last_token;
491 /* Get a new token from the preprocessor. */
492 cp_lexer_get_preprocessor_token (lexer, token);
494 /* Increment LAST_TOKEN. */
495 lexer->last_token = cp_lexer_next_token (lexer, token);
497 /* Strings should have type `const char []'. Right now, we will
498 have an ARRAY_TYPE that is constant rather than an array of
500 FIXME: Make fix_string_type get this right in the first place. */
501 if ((token->type == CPP_STRING || token->type == CPP_WSTRING)
502 && flag_const_strings)
506 /* Get the current type. It will be an ARRAY_TYPE. */
507 type = TREE_TYPE (token->value);
508 /* Use build_cplus_array_type to rebuild the array, thereby
509 getting the right type. */
510 type = build_cplus_array_type (TREE_TYPE (type), TYPE_DOMAIN (type));
511 /* Reset the type of the token. */
512 TREE_TYPE (token->value) = type;
518 /* If the circular buffer is full, make it bigger. */
521 cp_lexer_maybe_grow_buffer (cp_lexer* lexer)
523 /* If the buffer is full, enlarge it. */
524 if (lexer->last_token == lexer->first_token)
526 cp_token *new_buffer;
527 cp_token *old_buffer;
528 cp_token *new_first_token;
529 ptrdiff_t buffer_length;
530 size_t num_tokens_to_copy;
532 /* Remember the current buffer pointer. It will become invalid,
533 but we will need to do pointer arithmetic involving this
535 old_buffer = lexer->buffer;
536 /* Compute the current buffer size. */
537 buffer_length = lexer->buffer_end - lexer->buffer;
538 /* Allocate a buffer twice as big. */
539 new_buffer = ggc_realloc (lexer->buffer,
540 2 * buffer_length * sizeof (cp_token));
542 /* Because the buffer is circular, logically consecutive tokens
543 are not necessarily placed consecutively in memory.
544 Therefore, we must keep move the tokens that were before
545 FIRST_TOKEN to the second half of the newly allocated
547 num_tokens_to_copy = (lexer->first_token - old_buffer);
548 memcpy (new_buffer + buffer_length,
550 num_tokens_to_copy * sizeof (cp_token));
551 /* Clear the rest of the buffer. We never look at this storage,
552 but the garbage collector may. */
553 memset (new_buffer + buffer_length + num_tokens_to_copy, 0,
554 (buffer_length - num_tokens_to_copy) * sizeof (cp_token));
556 /* Now recompute all of the buffer pointers. */
558 = new_buffer + (lexer->first_token - old_buffer);
559 if (lexer->next_token != NULL)
561 ptrdiff_t next_token_delta;
563 if (lexer->next_token > lexer->first_token)
564 next_token_delta = lexer->next_token - lexer->first_token;
567 buffer_length - (lexer->first_token - lexer->next_token);
568 lexer->next_token = new_first_token + next_token_delta;
570 lexer->last_token = new_first_token + buffer_length;
571 lexer->buffer = new_buffer;
572 lexer->buffer_end = new_buffer + buffer_length * 2;
573 lexer->first_token = new_first_token;
577 /* Store the next token from the preprocessor in *TOKEN. */
580 cp_lexer_get_preprocessor_token (cp_lexer *lexer ATTRIBUTE_UNUSED ,
585 /* If this not the main lexer, return a terminating CPP_EOF token. */
586 if (lexer != NULL && !lexer->main_lexer_p)
588 token->type = CPP_EOF;
589 token->location.line = 0;
590 token->location.file = NULL;
591 token->value = NULL_TREE;
592 token->keyword = RID_MAX;
598 /* Keep going until we get a token we like. */
601 /* Get a new token from the preprocessor. */
602 token->type = c_lex_with_flags (&token->value, &token->flags);
603 /* Issue messages about tokens we cannot process. */
609 error ("invalid token");
613 /* This is a good token, so we exit the loop. */
618 /* Now we've got our token. */
619 token->location = input_location;
621 /* Check to see if this token is a keyword. */
622 if (token->type == CPP_NAME
623 && C_IS_RESERVED_WORD (token->value))
625 /* Mark this token as a keyword. */
626 token->type = CPP_KEYWORD;
627 /* Record which keyword. */
628 token->keyword = C_RID_CODE (token->value);
629 /* Update the value. Some keywords are mapped to particular
630 entities, rather than simply having the value of the
631 corresponding IDENTIFIER_NODE. For example, `__const' is
632 mapped to `const'. */
633 token->value = ridpointers[token->keyword];
636 token->keyword = RID_MAX;
639 /* Return a pointer to the next token in the token stream, but do not
643 cp_lexer_peek_token (cp_lexer* lexer)
647 /* If there are no tokens, read one now. */
648 if (!lexer->next_token)
649 cp_lexer_read_token (lexer);
651 /* Provide debugging output. */
652 if (cp_lexer_debugging_p (lexer))
654 fprintf (cp_lexer_debug_stream, "cp_lexer: peeking at token: ");
655 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
656 fprintf (cp_lexer_debug_stream, "\n");
659 token = lexer->next_token;
660 cp_lexer_set_source_position_from_token (lexer, token);
664 /* Return true if the next token has the indicated TYPE. */
667 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
671 /* Peek at the next token. */
672 token = cp_lexer_peek_token (lexer);
673 /* Check to see if it has the indicated TYPE. */
674 return token->type == type;
677 /* Return true if the next token does not have the indicated TYPE. */
680 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
682 return !cp_lexer_next_token_is (lexer, type);
685 /* Return true if the next token is the indicated KEYWORD. */
688 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
692 /* Peek at the next token. */
693 token = cp_lexer_peek_token (lexer);
694 /* Check to see if it is the indicated keyword. */
695 return token->keyword == keyword;
698 /* Return a pointer to the Nth token in the token stream. If N is 1,
699 then this is precisely equivalent to cp_lexer_peek_token. */
702 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
706 /* N is 1-based, not zero-based. */
707 my_friendly_assert (n > 0, 20000224);
709 /* Skip ahead from NEXT_TOKEN, reading more tokens as necessary. */
710 token = lexer->next_token;
711 /* If there are no tokens in the buffer, get one now. */
714 cp_lexer_read_token (lexer);
715 token = lexer->next_token;
718 /* Now, read tokens until we have enough. */
721 /* Advance to the next token. */
722 token = cp_lexer_next_token (lexer, token);
723 /* If that's all the tokens we have, read a new one. */
724 if (token == lexer->last_token)
725 token = cp_lexer_read_token (lexer);
731 /* Consume the next token. The pointer returned is valid only until
732 another token is read. Callers should preserve copy the token
733 explicitly if they will need its value for a longer period of
737 cp_lexer_consume_token (cp_lexer* lexer)
741 /* If there are no tokens, read one now. */
742 if (!lexer->next_token)
743 cp_lexer_read_token (lexer);
745 /* Remember the token we'll be returning. */
746 token = lexer->next_token;
748 /* Increment NEXT_TOKEN. */
749 lexer->next_token = cp_lexer_next_token (lexer,
751 /* Check to see if we're all out of tokens. */
752 if (lexer->next_token == lexer->last_token)
753 lexer->next_token = NULL;
755 /* If we're not saving tokens, then move FIRST_TOKEN too. */
756 if (!cp_lexer_saving_tokens (lexer))
758 /* If there are no tokens available, set FIRST_TOKEN to NULL. */
759 if (!lexer->next_token)
760 lexer->first_token = NULL;
762 lexer->first_token = lexer->next_token;
765 /* Provide debugging output. */
766 if (cp_lexer_debugging_p (lexer))
768 fprintf (cp_lexer_debug_stream, "cp_lexer: consuming token: ");
769 cp_lexer_print_token (cp_lexer_debug_stream, token);
770 fprintf (cp_lexer_debug_stream, "\n");
776 /* Permanently remove the next token from the token stream. There
777 must be a valid next token already; this token never reads
778 additional tokens from the preprocessor. */
781 cp_lexer_purge_token (cp_lexer *lexer)
784 cp_token *next_token;
786 token = lexer->next_token;
789 next_token = cp_lexer_next_token (lexer, token);
790 if (next_token == lexer->last_token)
792 *token = *next_token;
796 lexer->last_token = token;
797 /* The token purged may have been the only token remaining; if so,
799 if (lexer->next_token == token)
800 lexer->next_token = NULL;
803 /* Permanently remove all tokens after TOKEN, up to, but not
804 including, the token that will be returned next by
805 cp_lexer_peek_token. */
808 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *token)
814 if (lexer->next_token)
816 /* Copy the tokens that have not yet been read to the location
817 immediately following TOKEN. */
818 t1 = cp_lexer_next_token (lexer, token);
819 t2 = peek = cp_lexer_peek_token (lexer);
820 /* Move tokens into the vacant area between TOKEN and PEEK. */
821 while (t2 != lexer->last_token)
824 t1 = cp_lexer_next_token (lexer, t1);
825 t2 = cp_lexer_next_token (lexer, t2);
827 /* Now, the next available token is right after TOKEN. */
828 lexer->next_token = cp_lexer_next_token (lexer, token);
829 /* And the last token is wherever we ended up. */
830 lexer->last_token = t1;
834 /* There are no tokens in the buffer, so there is nothing to
835 copy. The last token in the buffer is TOKEN itself. */
836 lexer->last_token = cp_lexer_next_token (lexer, token);
840 /* Begin saving tokens. All tokens consumed after this point will be
844 cp_lexer_save_tokens (cp_lexer* lexer)
846 /* Provide debugging output. */
847 if (cp_lexer_debugging_p (lexer))
848 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
850 /* Make sure that LEXER->NEXT_TOKEN is non-NULL so that we can
851 restore the tokens if required. */
852 if (!lexer->next_token)
853 cp_lexer_read_token (lexer);
855 VARRAY_PUSH_INT (lexer->saved_tokens,
856 cp_lexer_token_difference (lexer,
861 /* Commit to the portion of the token stream most recently saved. */
864 cp_lexer_commit_tokens (cp_lexer* lexer)
866 /* Provide debugging output. */
867 if (cp_lexer_debugging_p (lexer))
868 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
870 VARRAY_POP (lexer->saved_tokens);
873 /* Return all tokens saved since the last call to cp_lexer_save_tokens
874 to the token stream. Stop saving tokens. */
877 cp_lexer_rollback_tokens (cp_lexer* lexer)
881 /* Provide debugging output. */
882 if (cp_lexer_debugging_p (lexer))
883 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
885 /* Find the token that was the NEXT_TOKEN when we started saving
887 delta = VARRAY_TOP_INT(lexer->saved_tokens);
888 /* Make it the next token again now. */
889 lexer->next_token = cp_lexer_advance_token (lexer,
892 /* It might be the case that there were no tokens when we started
893 saving tokens, but that there are some tokens now. */
894 if (!lexer->next_token && lexer->first_token)
895 lexer->next_token = lexer->first_token;
897 /* Stop saving tokens. */
898 VARRAY_POP (lexer->saved_tokens);
901 /* Print a representation of the TOKEN on the STREAM. */
904 cp_lexer_print_token (FILE * stream, cp_token* token)
906 const char *token_type = NULL;
908 /* Figure out what kind of token this is. */
916 token_type = "COMMA";
920 token_type = "OPEN_PAREN";
923 case CPP_CLOSE_PAREN:
924 token_type = "CLOSE_PAREN";
928 token_type = "OPEN_BRACE";
931 case CPP_CLOSE_BRACE:
932 token_type = "CLOSE_BRACE";
936 token_type = "SEMICOLON";
948 token_type = "keyword";
951 /* This is not a token that we know how to handle yet. */
956 /* If we have a name for the token, print it out. Otherwise, we
957 simply give the numeric code. */
959 fprintf (stream, "%s", token_type);
961 fprintf (stream, "%d", token->type);
962 /* And, for an identifier, print the identifier name. */
963 if (token->type == CPP_NAME
964 /* Some keywords have a value that is not an IDENTIFIER_NODE.
965 For example, `struct' is mapped to an INTEGER_CST. */
966 || (token->type == CPP_KEYWORD
967 && TREE_CODE (token->value) == IDENTIFIER_NODE))
968 fprintf (stream, " %s", IDENTIFIER_POINTER (token->value));
971 /* Start emitting debugging information. */
974 cp_lexer_start_debugging (cp_lexer* lexer)
976 ++lexer->debugging_p;
979 /* Stop emitting debugging information. */
982 cp_lexer_stop_debugging (cp_lexer* lexer)
984 --lexer->debugging_p;
993 A cp_parser parses the token stream as specified by the C++
994 grammar. Its job is purely parsing, not semantic analysis. For
995 example, the parser breaks the token stream into declarators,
996 expressions, statements, and other similar syntactic constructs.
997 It does not check that the types of the expressions on either side
998 of an assignment-statement are compatible, or that a function is
999 not declared with a parameter of type `void'.
1001 The parser invokes routines elsewhere in the compiler to perform
1002 semantic analysis and to build up the abstract syntax tree for the
1005 The parser (and the template instantiation code, which is, in a
1006 way, a close relative of parsing) are the only parts of the
1007 compiler that should be calling push_scope and pop_scope, or
1008 related functions. The parser (and template instantiation code)
1009 keeps track of what scope is presently active; everything else
1010 should simply honor that. (The code that generates static
1011 initializers may also need to set the scope, in order to check
1012 access control correctly when emitting the initializers.)
1017 The parser is of the standard recursive-descent variety. Upcoming
1018 tokens in the token stream are examined in order to determine which
1019 production to use when parsing a non-terminal. Some C++ constructs
1020 require arbitrary look ahead to disambiguate. For example, it is
1021 impossible, in the general case, to tell whether a statement is an
1022 expression or declaration without scanning the entire statement.
1023 Therefore, the parser is capable of "parsing tentatively." When the
1024 parser is not sure what construct comes next, it enters this mode.
1025 Then, while we attempt to parse the construct, the parser queues up
1026 error messages, rather than issuing them immediately, and saves the
1027 tokens it consumes. If the construct is parsed successfully, the
1028 parser "commits", i.e., it issues any queued error messages and
1029 the tokens that were being preserved are permanently discarded.
1030 If, however, the construct is not parsed successfully, the parser
1031 rolls back its state completely so that it can resume parsing using
1032 a different alternative.
1037 The performance of the parser could probably be improved
1038 substantially. Some possible improvements include:
1040 - The expression parser recurses through the various levels of
1041 precedence as specified in the grammar, rather than using an
1042 operator-precedence technique. Therefore, parsing a simple
1043 identifier requires multiple recursive calls.
1045 - We could often eliminate the need to parse tentatively by
1046 looking ahead a little bit. In some places, this approach
1047 might not entirely eliminate the need to parse tentatively, but
1048 it might still speed up the average case. */
1050 /* Flags that are passed to some parsing functions. These values can
1051 be bitwise-ored together. */
1053 typedef enum cp_parser_flags
1056 CP_PARSER_FLAGS_NONE = 0x0,
1057 /* The construct is optional. If it is not present, then no error
1058 should be issued. */
1059 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1060 /* When parsing a type-specifier, do not allow user-defined types. */
1061 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2
1064 /* The different kinds of declarators we want to parse. */
1066 typedef enum cp_parser_declarator_kind
1068 /* We want an abstract declarator. */
1069 CP_PARSER_DECLARATOR_ABSTRACT,
1070 /* We want a named declarator. */
1071 CP_PARSER_DECLARATOR_NAMED,
1072 /* We don't mind, but the name must be an unqualified-id. */
1073 CP_PARSER_DECLARATOR_EITHER
1074 } cp_parser_declarator_kind;
1076 /* A mapping from a token type to a corresponding tree node type. */
1078 typedef struct cp_parser_token_tree_map_node
1080 /* The token type. */
1081 ENUM_BITFIELD (cpp_ttype) token_type : 8;
1082 /* The corresponding tree code. */
1083 ENUM_BITFIELD (tree_code) tree_type : 8;
1084 } cp_parser_token_tree_map_node;
1086 /* A complete map consists of several ordinary entries, followed by a
1087 terminator. The terminating entry has a token_type of CPP_EOF. */
1089 typedef cp_parser_token_tree_map_node cp_parser_token_tree_map[];
1091 /* The status of a tentative parse. */
1093 typedef enum cp_parser_status_kind
1095 /* No errors have occurred. */
1096 CP_PARSER_STATUS_KIND_NO_ERROR,
1097 /* An error has occurred. */
1098 CP_PARSER_STATUS_KIND_ERROR,
1099 /* We are committed to this tentative parse, whether or not an error
1101 CP_PARSER_STATUS_KIND_COMMITTED
1102 } cp_parser_status_kind;
1104 /* Context that is saved and restored when parsing tentatively. */
1106 typedef struct cp_parser_context GTY (())
1108 /* If this is a tentative parsing context, the status of the
1110 enum cp_parser_status_kind status;
1111 /* If non-NULL, we have just seen a `x->' or `x.' expression. Names
1112 that are looked up in this context must be looked up both in the
1113 scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1114 the context of the containing expression. */
1116 /* The next parsing context in the stack. */
1117 struct cp_parser_context *next;
1118 } cp_parser_context;
1122 /* Constructors and destructors. */
1124 static cp_parser_context *cp_parser_context_new
1125 (cp_parser_context *);
1127 /* Class variables. */
1129 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1131 /* Constructors and destructors. */
1133 /* Construct a new context. The context below this one on the stack
1134 is given by NEXT. */
1136 static cp_parser_context *
1137 cp_parser_context_new (cp_parser_context* next)
1139 cp_parser_context *context;
1141 /* Allocate the storage. */
1142 if (cp_parser_context_free_list != NULL)
1144 /* Pull the first entry from the free list. */
1145 context = cp_parser_context_free_list;
1146 cp_parser_context_free_list = context->next;
1147 memset (context, 0, sizeof (*context));
1150 context = ggc_alloc_cleared (sizeof (cp_parser_context));
1151 /* No errors have occurred yet in this context. */
1152 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1153 /* If this is not the bottomost context, copy information that we
1154 need from the previous context. */
1157 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1158 expression, then we are parsing one in this context, too. */
1159 context->object_type = next->object_type;
1160 /* Thread the stack. */
1161 context->next = next;
1167 /* The cp_parser structure represents the C++ parser. */
1169 typedef struct cp_parser GTY(())
1171 /* The lexer from which we are obtaining tokens. */
1174 /* The scope in which names should be looked up. If NULL_TREE, then
1175 we look up names in the scope that is currently open in the
1176 source program. If non-NULL, this is either a TYPE or
1177 NAMESPACE_DECL for the scope in which we should look.
1179 This value is not cleared automatically after a name is looked
1180 up, so we must be careful to clear it before starting a new look
1181 up sequence. (If it is not cleared, then `X::Y' followed by `Z'
1182 will look up `Z' in the scope of `X', rather than the current
1183 scope.) Unfortunately, it is difficult to tell when name lookup
1184 is complete, because we sometimes peek at a token, look it up,
1185 and then decide not to consume it. */
1188 /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1189 last lookup took place. OBJECT_SCOPE is used if an expression
1190 like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1191 respectively. QUALIFYING_SCOPE is used for an expression of the
1192 form "X::Y"; it refers to X. */
1194 tree qualifying_scope;
1196 /* A stack of parsing contexts. All but the bottom entry on the
1197 stack will be tentative contexts.
1199 We parse tentatively in order to determine which construct is in
1200 use in some situations. For example, in order to determine
1201 whether a statement is an expression-statement or a
1202 declaration-statement we parse it tentatively as a
1203 declaration-statement. If that fails, we then reparse the same
1204 token stream as an expression-statement. */
1205 cp_parser_context *context;
1207 /* True if we are parsing GNU C++. If this flag is not set, then
1208 GNU extensions are not recognized. */
1209 bool allow_gnu_extensions_p;
1211 /* TRUE if the `>' token should be interpreted as the greater-than
1212 operator. FALSE if it is the end of a template-id or
1213 template-parameter-list. */
1214 bool greater_than_is_operator_p;
1216 /* TRUE if default arguments are allowed within a parameter list
1217 that starts at this point. FALSE if only a gnu extension makes
1218 them permissible. */
1219 bool default_arg_ok_p;
1221 /* TRUE if we are parsing an integral constant-expression. See
1222 [expr.const] for a precise definition. */
1223 bool integral_constant_expression_p;
1225 /* TRUE if we are parsing an integral constant-expression -- but a
1226 non-constant expression should be permitted as well. This flag
1227 is used when parsing an array bound so that GNU variable-length
1228 arrays are tolerated. */
1229 bool allow_non_integral_constant_expression_p;
1231 /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1232 been seen that makes the expression non-constant. */
1233 bool non_integral_constant_expression_p;
1235 /* TRUE if we are parsing the argument to "__offsetof__". */
1238 /* TRUE if local variable names and `this' are forbidden in the
1240 bool local_variables_forbidden_p;
1242 /* TRUE if the declaration we are parsing is part of a
1243 linkage-specification of the form `extern string-literal
1245 bool in_unbraced_linkage_specification_p;
1247 /* TRUE if we are presently parsing a declarator, after the
1248 direct-declarator. */
1249 bool in_declarator_p;
1251 /* TRUE if we are presently parsing a template-argument-list. */
1252 bool in_template_argument_list_p;
1254 /* TRUE if we are presently parsing the body of an
1255 iteration-statement. */
1256 bool in_iteration_statement_p;
1258 /* TRUE if we are presently parsing the body of a switch
1260 bool in_switch_statement_p;
1262 /* TRUE if we are parsing a type-id in an expression context. In
1263 such a situation, both "type (expr)" and "type (type)" are valid
1265 bool in_type_id_in_expr_p;
1267 /* If non-NULL, then we are parsing a construct where new type
1268 definitions are not permitted. The string stored here will be
1269 issued as an error message if a type is defined. */
1270 const char *type_definition_forbidden_message;
1272 /* A list of lists. The outer list is a stack, used for member
1273 functions of local classes. At each level there are two sub-list,
1274 one on TREE_VALUE and one on TREE_PURPOSE. Each of those
1275 sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their
1276 TREE_VALUE's. The functions are chained in reverse declaration
1279 The TREE_PURPOSE sublist contains those functions with default
1280 arguments that need post processing, and the TREE_VALUE sublist
1281 contains those functions with definitions that need post
1284 These lists can only be processed once the outermost class being
1285 defined is complete. */
1286 tree unparsed_functions_queues;
1288 /* The number of classes whose definitions are currently in
1290 unsigned num_classes_being_defined;
1292 /* The number of template parameter lists that apply directly to the
1293 current declaration. */
1294 unsigned num_template_parameter_lists;
1297 /* The type of a function that parses some kind of expression. */
1298 typedef tree (*cp_parser_expression_fn) (cp_parser *);
1302 /* Constructors and destructors. */
1304 static cp_parser *cp_parser_new
1307 /* Routines to parse various constructs.
1309 Those that return `tree' will return the error_mark_node (rather
1310 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1311 Sometimes, they will return an ordinary node if error-recovery was
1312 attempted, even though a parse error occurred. So, to check
1313 whether or not a parse error occurred, you should always use
1314 cp_parser_error_occurred. If the construct is optional (indicated
1315 either by an `_opt' in the name of the function that does the
1316 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1317 the construct is not present. */
1319 /* Lexical conventions [gram.lex] */
1321 static tree cp_parser_identifier
1324 /* Basic concepts [gram.basic] */
1326 static bool cp_parser_translation_unit
1329 /* Expressions [gram.expr] */
1331 static tree cp_parser_primary_expression
1332 (cp_parser *, cp_id_kind *, tree *);
1333 static tree cp_parser_id_expression
1334 (cp_parser *, bool, bool, bool *, bool);
1335 static tree cp_parser_unqualified_id
1336 (cp_parser *, bool, bool, bool);
1337 static tree cp_parser_nested_name_specifier_opt
1338 (cp_parser *, bool, bool, bool, bool);
1339 static tree cp_parser_nested_name_specifier
1340 (cp_parser *, bool, bool, bool, bool);
1341 static tree cp_parser_class_or_namespace_name
1342 (cp_parser *, bool, bool, bool, bool, bool);
1343 static tree cp_parser_postfix_expression
1344 (cp_parser *, bool);
1345 static tree cp_parser_parenthesized_expression_list
1346 (cp_parser *, bool, bool *);
1347 static void cp_parser_pseudo_destructor_name
1348 (cp_parser *, tree *, tree *);
1349 static tree cp_parser_unary_expression
1350 (cp_parser *, bool);
1351 static enum tree_code cp_parser_unary_operator
1353 static tree cp_parser_new_expression
1355 static tree cp_parser_new_placement
1357 static tree cp_parser_new_type_id
1359 static tree cp_parser_new_declarator_opt
1361 static tree cp_parser_direct_new_declarator
1363 static tree cp_parser_new_initializer
1365 static tree cp_parser_delete_expression
1367 static tree cp_parser_cast_expression
1368 (cp_parser *, bool);
1369 static tree cp_parser_pm_expression
1371 static tree cp_parser_multiplicative_expression
1373 static tree cp_parser_additive_expression
1375 static tree cp_parser_shift_expression
1377 static tree cp_parser_relational_expression
1379 static tree cp_parser_equality_expression
1381 static tree cp_parser_and_expression
1383 static tree cp_parser_exclusive_or_expression
1385 static tree cp_parser_inclusive_or_expression
1387 static tree cp_parser_logical_and_expression
1389 static tree cp_parser_logical_or_expression
1391 static tree cp_parser_question_colon_clause
1392 (cp_parser *, tree);
1393 static tree cp_parser_assignment_expression
1395 static enum tree_code cp_parser_assignment_operator_opt
1397 static tree cp_parser_expression
1399 static tree cp_parser_constant_expression
1400 (cp_parser *, bool, bool *);
1402 /* Statements [gram.stmt.stmt] */
1404 static void cp_parser_statement
1405 (cp_parser *, bool);
1406 static tree cp_parser_labeled_statement
1407 (cp_parser *, bool);
1408 static tree cp_parser_expression_statement
1409 (cp_parser *, bool);
1410 static tree cp_parser_compound_statement
1411 (cp_parser *, bool);
1412 static void cp_parser_statement_seq_opt
1413 (cp_parser *, bool);
1414 static tree cp_parser_selection_statement
1416 static tree cp_parser_condition
1418 static tree cp_parser_iteration_statement
1420 static void cp_parser_for_init_statement
1422 static tree cp_parser_jump_statement
1424 static void cp_parser_declaration_statement
1427 static tree cp_parser_implicitly_scoped_statement
1429 static void cp_parser_already_scoped_statement
1432 /* Declarations [gram.dcl.dcl] */
1434 static void cp_parser_declaration_seq_opt
1436 static void cp_parser_declaration
1438 static void cp_parser_block_declaration
1439 (cp_parser *, bool);
1440 static void cp_parser_simple_declaration
1441 (cp_parser *, bool);
1442 static tree cp_parser_decl_specifier_seq
1443 (cp_parser *, cp_parser_flags, tree *, int *);
1444 static tree cp_parser_storage_class_specifier_opt
1446 static tree cp_parser_function_specifier_opt
1448 static tree cp_parser_type_specifier
1449 (cp_parser *, cp_parser_flags, bool, bool, int *, bool *);
1450 static tree cp_parser_simple_type_specifier
1451 (cp_parser *, cp_parser_flags, bool);
1452 static tree cp_parser_type_name
1454 static tree cp_parser_elaborated_type_specifier
1455 (cp_parser *, bool, bool);
1456 static tree cp_parser_enum_specifier
1458 static void cp_parser_enumerator_list
1459 (cp_parser *, tree);
1460 static void cp_parser_enumerator_definition
1461 (cp_parser *, tree);
1462 static tree cp_parser_namespace_name
1464 static void cp_parser_namespace_definition
1466 static void cp_parser_namespace_body
1468 static tree cp_parser_qualified_namespace_specifier
1470 static void cp_parser_namespace_alias_definition
1472 static void cp_parser_using_declaration
1474 static void cp_parser_using_directive
1476 static void cp_parser_asm_definition
1478 static void cp_parser_linkage_specification
1481 /* Declarators [gram.dcl.decl] */
1483 static tree cp_parser_init_declarator
1484 (cp_parser *, tree, tree, bool, bool, int, bool *);
1485 static tree cp_parser_declarator
1486 (cp_parser *, cp_parser_declarator_kind, int *, bool *);
1487 static tree cp_parser_direct_declarator
1488 (cp_parser *, cp_parser_declarator_kind, int *);
1489 static enum tree_code cp_parser_ptr_operator
1490 (cp_parser *, tree *, tree *);
1491 static tree cp_parser_cv_qualifier_seq_opt
1493 static tree cp_parser_cv_qualifier_opt
1495 static tree cp_parser_declarator_id
1497 static tree cp_parser_type_id
1499 static tree cp_parser_type_specifier_seq
1501 static tree cp_parser_parameter_declaration_clause
1503 static tree cp_parser_parameter_declaration_list
1505 static tree cp_parser_parameter_declaration
1506 (cp_parser *, bool, bool *);
1507 static void cp_parser_function_body
1509 static tree cp_parser_initializer
1510 (cp_parser *, bool *, bool *);
1511 static tree cp_parser_initializer_clause
1512 (cp_parser *, bool *);
1513 static tree cp_parser_initializer_list
1514 (cp_parser *, bool *);
1516 static bool cp_parser_ctor_initializer_opt_and_function_body
1519 /* Classes [gram.class] */
1521 static tree cp_parser_class_name
1522 (cp_parser *, bool, bool, bool, bool, bool, bool);
1523 static tree cp_parser_class_specifier
1525 static tree cp_parser_class_head
1526 (cp_parser *, bool *, tree *);
1527 static enum tag_types cp_parser_class_key
1529 static void cp_parser_member_specification_opt
1531 static void cp_parser_member_declaration
1533 static tree cp_parser_pure_specifier
1535 static tree cp_parser_constant_initializer
1538 /* Derived classes [gram.class.derived] */
1540 static tree cp_parser_base_clause
1542 static tree cp_parser_base_specifier
1545 /* Special member functions [gram.special] */
1547 static tree cp_parser_conversion_function_id
1549 static tree cp_parser_conversion_type_id
1551 static tree cp_parser_conversion_declarator_opt
1553 static bool cp_parser_ctor_initializer_opt
1555 static void cp_parser_mem_initializer_list
1557 static tree cp_parser_mem_initializer
1559 static tree cp_parser_mem_initializer_id
1562 /* Overloading [gram.over] */
1564 static tree cp_parser_operator_function_id
1566 static tree cp_parser_operator
1569 /* Templates [gram.temp] */
1571 static void cp_parser_template_declaration
1572 (cp_parser *, bool);
1573 static tree cp_parser_template_parameter_list
1575 static tree cp_parser_template_parameter
1577 static tree cp_parser_type_parameter
1579 static tree cp_parser_template_id
1580 (cp_parser *, bool, bool, bool);
1581 static tree cp_parser_template_name
1582 (cp_parser *, bool, bool, bool, bool *);
1583 static tree cp_parser_template_argument_list
1585 static tree cp_parser_template_argument
1587 static void cp_parser_explicit_instantiation
1589 static void cp_parser_explicit_specialization
1592 /* Exception handling [gram.exception] */
1594 static tree cp_parser_try_block
1596 static bool cp_parser_function_try_block
1598 static void cp_parser_handler_seq
1600 static void cp_parser_handler
1602 static tree cp_parser_exception_declaration
1604 static tree cp_parser_throw_expression
1606 static tree cp_parser_exception_specification_opt
1608 static tree cp_parser_type_id_list
1611 /* GNU Extensions */
1613 static tree cp_parser_asm_specification_opt
1615 static tree cp_parser_asm_operand_list
1617 static tree cp_parser_asm_clobber_list
1619 static tree cp_parser_attributes_opt
1621 static tree cp_parser_attribute_list
1623 static bool cp_parser_extension_opt
1624 (cp_parser *, int *);
1625 static void cp_parser_label_declaration
1628 /* Utility Routines */
1630 static tree cp_parser_lookup_name
1631 (cp_parser *, tree, bool, bool, bool, bool);
1632 static tree cp_parser_lookup_name_simple
1633 (cp_parser *, tree);
1634 static tree cp_parser_maybe_treat_template_as_class
1636 static bool cp_parser_check_declarator_template_parameters
1637 (cp_parser *, tree);
1638 static bool cp_parser_check_template_parameters
1639 (cp_parser *, unsigned);
1640 static tree cp_parser_simple_cast_expression
1642 static tree cp_parser_binary_expression
1643 (cp_parser *, const cp_parser_token_tree_map, cp_parser_expression_fn);
1644 static tree cp_parser_global_scope_opt
1645 (cp_parser *, bool);
1646 static bool cp_parser_constructor_declarator_p
1647 (cp_parser *, bool);
1648 static tree cp_parser_function_definition_from_specifiers_and_declarator
1649 (cp_parser *, tree, tree, tree);
1650 static tree cp_parser_function_definition_after_declarator
1651 (cp_parser *, bool);
1652 static void cp_parser_template_declaration_after_export
1653 (cp_parser *, bool);
1654 static tree cp_parser_single_declaration
1655 (cp_parser *, bool, bool *);
1656 static tree cp_parser_functional_cast
1657 (cp_parser *, tree);
1658 static tree cp_parser_save_member_function_body
1659 (cp_parser *, tree, tree, tree);
1660 static tree cp_parser_enclosed_template_argument_list
1662 static void cp_parser_save_default_args
1663 (cp_parser *, tree);
1664 static void cp_parser_late_parsing_for_member
1665 (cp_parser *, tree);
1666 static void cp_parser_late_parsing_default_args
1667 (cp_parser *, tree);
1668 static tree cp_parser_sizeof_operand
1669 (cp_parser *, enum rid);
1670 static bool cp_parser_declares_only_class_p
1672 static bool cp_parser_friend_p
1674 static cp_token *cp_parser_require
1675 (cp_parser *, enum cpp_ttype, const char *);
1676 static cp_token *cp_parser_require_keyword
1677 (cp_parser *, enum rid, const char *);
1678 static bool cp_parser_token_starts_function_definition_p
1680 static bool cp_parser_next_token_starts_class_definition_p
1682 static bool cp_parser_next_token_ends_template_argument_p
1684 static bool cp_parser_nth_token_starts_template_argument_list_p
1685 (cp_parser *, size_t);
1686 static enum tag_types cp_parser_token_is_class_key
1688 static void cp_parser_check_class_key
1689 (enum tag_types, tree type);
1690 static void cp_parser_check_access_in_redeclaration
1692 static bool cp_parser_optional_template_keyword
1694 static void cp_parser_pre_parsed_nested_name_specifier
1696 static void cp_parser_cache_group
1697 (cp_parser *, cp_token_cache *, enum cpp_ttype, unsigned);
1698 static void cp_parser_parse_tentatively
1700 static void cp_parser_commit_to_tentative_parse
1702 static void cp_parser_abort_tentative_parse
1704 static bool cp_parser_parse_definitely
1706 static inline bool cp_parser_parsing_tentatively
1708 static bool cp_parser_committed_to_tentative_parse
1710 static void cp_parser_error
1711 (cp_parser *, const char *);
1712 static void cp_parser_name_lookup_error
1713 (cp_parser *, tree, tree, const char *);
1714 static bool cp_parser_simulate_error
1716 static void cp_parser_check_type_definition
1718 static void cp_parser_check_for_definition_in_return_type
1720 static void cp_parser_check_for_invalid_template_id
1721 (cp_parser *, tree);
1722 static bool cp_parser_non_integral_constant_expression
1723 (cp_parser *, const char *);
1724 static void cp_parser_diagnose_invalid_type_name
1725 (cp_parser *, tree, tree);
1726 static bool cp_parser_parse_and_diagnose_invalid_type_name
1728 static int cp_parser_skip_to_closing_parenthesis
1729 (cp_parser *, bool, bool, bool);
1730 static void cp_parser_skip_to_end_of_statement
1732 static void cp_parser_consume_semicolon_at_end_of_statement
1734 static void cp_parser_skip_to_end_of_block_or_statement
1736 static void cp_parser_skip_to_closing_brace
1738 static void cp_parser_skip_until_found
1739 (cp_parser *, enum cpp_ttype, const char *);
1740 static bool cp_parser_error_occurred
1742 static bool cp_parser_allow_gnu_extensions_p
1744 static bool cp_parser_is_string_literal
1746 static bool cp_parser_is_keyword
1747 (cp_token *, enum rid);
1748 static tree cp_parser_make_typename_type
1749 (cp_parser *, tree, tree);
1751 /* Returns nonzero if we are parsing tentatively. */
1754 cp_parser_parsing_tentatively (cp_parser* parser)
1756 return parser->context->next != NULL;
1759 /* Returns nonzero if TOKEN is a string literal. */
1762 cp_parser_is_string_literal (cp_token* token)
1764 return (token->type == CPP_STRING || token->type == CPP_WSTRING);
1767 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
1770 cp_parser_is_keyword (cp_token* token, enum rid keyword)
1772 return token->keyword == keyword;
1775 /* Issue the indicated error MESSAGE. */
1778 cp_parser_error (cp_parser* parser, const char* message)
1780 /* Output the MESSAGE -- unless we're parsing tentatively. */
1781 if (!cp_parser_simulate_error (parser))
1784 token = cp_lexer_peek_token (parser->lexer);
1785 c_parse_error (message,
1786 /* Because c_parser_error does not understand
1787 CPP_KEYWORD, keywords are treated like
1789 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
1794 /* Issue an error about name-lookup failing. NAME is the
1795 IDENTIFIER_NODE DECL is the result of
1796 the lookup (as returned from cp_parser_lookup_name). DESIRED is
1797 the thing that we hoped to find. */
1800 cp_parser_name_lookup_error (cp_parser* parser,
1803 const char* desired)
1805 /* If name lookup completely failed, tell the user that NAME was not
1807 if (decl == error_mark_node)
1809 if (parser->scope && parser->scope != global_namespace)
1810 error ("`%D::%D' has not been declared",
1811 parser->scope, name);
1812 else if (parser->scope == global_namespace)
1813 error ("`::%D' has not been declared", name);
1815 error ("`%D' has not been declared", name);
1817 else if (parser->scope && parser->scope != global_namespace)
1818 error ("`%D::%D' %s", parser->scope, name, desired);
1819 else if (parser->scope == global_namespace)
1820 error ("`::%D' %s", name, desired);
1822 error ("`%D' %s", name, desired);
1825 /* If we are parsing tentatively, remember that an error has occurred
1826 during this tentative parse. Returns true if the error was
1827 simulated; false if a message should be issued by the caller. */
1830 cp_parser_simulate_error (cp_parser* parser)
1832 if (cp_parser_parsing_tentatively (parser)
1833 && !cp_parser_committed_to_tentative_parse (parser))
1835 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
1841 /* This function is called when a type is defined. If type
1842 definitions are forbidden at this point, an error message is
1846 cp_parser_check_type_definition (cp_parser* parser)
1848 /* If types are forbidden here, issue a message. */
1849 if (parser->type_definition_forbidden_message)
1850 /* Use `%s' to print the string in case there are any escape
1851 characters in the message. */
1852 error ("%s", parser->type_definition_forbidden_message);
1855 /* This function is called when a declaration is parsed. If
1856 DECLARATOR is a function declarator and DECLARES_CLASS_OR_ENUM
1857 indicates that a type was defined in the decl-specifiers for DECL,
1858 then an error is issued. */
1861 cp_parser_check_for_definition_in_return_type (tree declarator,
1862 int declares_class_or_enum)
1864 /* [dcl.fct] forbids type definitions in return types.
1865 Unfortunately, it's not easy to know whether or not we are
1866 processing a return type until after the fact. */
1868 && (TREE_CODE (declarator) == INDIRECT_REF
1869 || TREE_CODE (declarator) == ADDR_EXPR))
1870 declarator = TREE_OPERAND (declarator, 0);
1872 && TREE_CODE (declarator) == CALL_EXPR
1873 && declares_class_or_enum & 2)
1874 error ("new types may not be defined in a return type");
1877 /* A type-specifier (TYPE) has been parsed which cannot be followed by
1878 "<" in any valid C++ program. If the next token is indeed "<",
1879 issue a message warning the user about what appears to be an
1880 invalid attempt to form a template-id. */
1883 cp_parser_check_for_invalid_template_id (cp_parser* parser,
1889 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
1892 error ("`%T' is not a template", type);
1893 else if (TREE_CODE (type) == IDENTIFIER_NODE)
1894 error ("`%s' is not a template", IDENTIFIER_POINTER (type));
1896 error ("invalid template-id");
1897 /* Remember the location of the invalid "<". */
1898 if (cp_parser_parsing_tentatively (parser)
1899 && !cp_parser_committed_to_tentative_parse (parser))
1901 token = cp_lexer_peek_token (parser->lexer);
1902 token = cp_lexer_prev_token (parser->lexer, token);
1903 start = cp_lexer_token_difference (parser->lexer,
1904 parser->lexer->first_token,
1909 /* Consume the "<". */
1910 cp_lexer_consume_token (parser->lexer);
1911 /* Parse the template arguments. */
1912 cp_parser_enclosed_template_argument_list (parser);
1913 /* Permanently remove the invalid template arguments so that
1914 this error message is not issued again. */
1917 token = cp_lexer_advance_token (parser->lexer,
1918 parser->lexer->first_token,
1920 cp_lexer_purge_tokens_after (parser->lexer, token);
1925 /* If parsing an integral constant-expression, issue an error message
1926 about the fact that THING appeared and return true. Otherwise,
1927 return false, marking the current expression as non-constant. */
1930 cp_parser_non_integral_constant_expression (cp_parser *parser,
1933 if (parser->integral_constant_expression_p)
1935 if (!parser->allow_non_integral_constant_expression_p)
1937 error ("%s cannot appear in a constant-expression", thing);
1940 parser->non_integral_constant_expression_p = true;
1945 /* Emit a diagnostic for an invalid type name. Consider also if it is
1946 qualified or not and the result of a lookup, to provide a better
1950 cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree scope, tree id)
1952 tree decl, old_scope;
1953 /* Try to lookup the identifier. */
1954 old_scope = parser->scope;
1955 parser->scope = scope;
1956 decl = cp_parser_lookup_name_simple (parser, id);
1957 parser->scope = old_scope;
1958 /* If the lookup found a template-name, it means that the user forgot
1959 to specify an argument list. Emit an useful error message. */
1960 if (TREE_CODE (decl) == TEMPLATE_DECL)
1961 error ("invalid use of template-name `%E' without an argument list",
1963 else if (!parser->scope)
1965 /* Issue an error message. */
1966 error ("`%E' does not name a type", id);
1967 /* If we're in a template class, it's possible that the user was
1968 referring to a type from a base class. For example:
1970 template <typename T> struct A { typedef T X; };
1971 template <typename T> struct B : public A<T> { X x; };
1973 The user should have said "typename A<T>::X". */
1974 if (processing_template_decl && current_class_type)
1978 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
1982 tree base_type = BINFO_TYPE (b);
1983 if (CLASS_TYPE_P (base_type)
1984 && dependent_type_p (base_type))
1987 /* Go from a particular instantiation of the
1988 template (which will have an empty TYPE_FIELDs),
1989 to the main version. */
1990 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
1991 for (field = TYPE_FIELDS (base_type);
1993 field = TREE_CHAIN (field))
1994 if (TREE_CODE (field) == TYPE_DECL
1995 && DECL_NAME (field) == id)
1997 inform ("(perhaps `typename %T::%E' was intended)",
1998 BINFO_TYPE (b), id);
2007 /* Here we diagnose qualified-ids where the scope is actually correct,
2008 but the identifier does not resolve to a valid type name. */
2011 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2012 error ("`%E' in namespace `%E' does not name a type",
2014 else if (TYPE_P (parser->scope))
2015 error ("`%E' in class `%T' does not name a type",
2022 /* Check for a common situation where a type-name should be present,
2023 but is not, and issue a sensible error message. Returns true if an
2024 invalid type-name was detected.
2026 The situation handled by this function are variable declarations of the
2027 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2028 Usually, `ID' should name a type, but if we got here it means that it
2029 does not. We try to emit the best possible error message depending on
2030 how exactly the id-expression looks like.
2034 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2038 cp_parser_parse_tentatively (parser);
2039 id = cp_parser_id_expression (parser,
2040 /*template_keyword_p=*/false,
2041 /*check_dependency_p=*/true,
2042 /*template_p=*/NULL,
2043 /*declarator_p=*/true);
2044 /* After the id-expression, there should be a plain identifier,
2045 otherwise this is not a simple variable declaration. Also, if
2046 the scope is dependent, we cannot do much. */
2047 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME)
2048 || (parser->scope && TYPE_P (parser->scope)
2049 && dependent_type_p (parser->scope)))
2051 cp_parser_abort_tentative_parse (parser);
2054 if (!cp_parser_parse_definitely (parser))
2057 /* If we got here, this cannot be a valid variable declaration, thus
2058 the cp_parser_id_expression must have resolved to a plain identifier
2059 node (not a TYPE_DECL or TEMPLATE_ID_EXPR). */
2060 my_friendly_assert (TREE_CODE (id) == IDENTIFIER_NODE, 20030203);
2061 /* Emit a diagnostic for the invalid type. */
2062 cp_parser_diagnose_invalid_type_name (parser, parser->scope, id);
2063 /* Skip to the end of the declaration; there's no point in
2064 trying to process it. */
2065 cp_parser_skip_to_end_of_block_or_statement (parser);
2069 /* Consume tokens up to, and including, the next non-nested closing `)'.
2070 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
2071 are doing error recovery. Returns -1 if OR_COMMA is true and we
2072 found an unnested comma. */
2075 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2080 unsigned paren_depth = 0;
2081 unsigned brace_depth = 0;
2083 if (recovering && !or_comma && cp_parser_parsing_tentatively (parser)
2084 && !cp_parser_committed_to_tentative_parse (parser))
2091 /* If we've run out of tokens, then there is no closing `)'. */
2092 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2095 token = cp_lexer_peek_token (parser->lexer);
2097 /* This matches the processing in skip_to_end_of_statement. */
2098 if (token->type == CPP_SEMICOLON && !brace_depth)
2100 if (token->type == CPP_OPEN_BRACE)
2102 if (token->type == CPP_CLOSE_BRACE)
2107 if (recovering && or_comma && token->type == CPP_COMMA
2108 && !brace_depth && !paren_depth)
2113 /* If it is an `(', we have entered another level of nesting. */
2114 if (token->type == CPP_OPEN_PAREN)
2116 /* If it is a `)', then we might be done. */
2117 else if (token->type == CPP_CLOSE_PAREN && !paren_depth--)
2120 cp_lexer_consume_token (parser->lexer);
2125 /* Consume the token. */
2126 cp_lexer_consume_token (parser->lexer);
2130 /* Consume tokens until we reach the end of the current statement.
2131 Normally, that will be just before consuming a `;'. However, if a
2132 non-nested `}' comes first, then we stop before consuming that. */
2135 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2137 unsigned nesting_depth = 0;
2143 /* Peek at the next token. */
2144 token = cp_lexer_peek_token (parser->lexer);
2145 /* If we've run out of tokens, stop. */
2146 if (token->type == CPP_EOF)
2148 /* If the next token is a `;', we have reached the end of the
2150 if (token->type == CPP_SEMICOLON && !nesting_depth)
2152 /* If the next token is a non-nested `}', then we have reached
2153 the end of the current block. */
2154 if (token->type == CPP_CLOSE_BRACE)
2156 /* If this is a non-nested `}', stop before consuming it.
2157 That way, when confronted with something like:
2161 we stop before consuming the closing `}', even though we
2162 have not yet reached a `;'. */
2163 if (nesting_depth == 0)
2165 /* If it is the closing `}' for a block that we have
2166 scanned, stop -- but only after consuming the token.
2172 we will stop after the body of the erroneously declared
2173 function, but before consuming the following `typedef'
2175 if (--nesting_depth == 0)
2177 cp_lexer_consume_token (parser->lexer);
2181 /* If it the next token is a `{', then we are entering a new
2182 block. Consume the entire block. */
2183 else if (token->type == CPP_OPEN_BRACE)
2185 /* Consume the token. */
2186 cp_lexer_consume_token (parser->lexer);
2190 /* This function is called at the end of a statement or declaration.
2191 If the next token is a semicolon, it is consumed; otherwise, error
2192 recovery is attempted. */
2195 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2197 /* Look for the trailing `;'. */
2198 if (!cp_parser_require (parser, CPP_SEMICOLON, "`;'"))
2200 /* If there is additional (erroneous) input, skip to the end of
2202 cp_parser_skip_to_end_of_statement (parser);
2203 /* If the next token is now a `;', consume it. */
2204 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2205 cp_lexer_consume_token (parser->lexer);
2209 /* Skip tokens until we have consumed an entire block, or until we
2210 have consumed a non-nested `;'. */
2213 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2215 unsigned nesting_depth = 0;
2221 /* Peek at the next token. */
2222 token = cp_lexer_peek_token (parser->lexer);
2223 /* If we've run out of tokens, stop. */
2224 if (token->type == CPP_EOF)
2226 /* If the next token is a `;', we have reached the end of the
2228 if (token->type == CPP_SEMICOLON && !nesting_depth)
2230 /* Consume the `;'. */
2231 cp_lexer_consume_token (parser->lexer);
2234 /* Consume the token. */
2235 token = cp_lexer_consume_token (parser->lexer);
2236 /* If the next token is a non-nested `}', then we have reached
2237 the end of the current block. */
2238 if (token->type == CPP_CLOSE_BRACE
2239 && (nesting_depth == 0 || --nesting_depth == 0))
2241 /* If it the next token is a `{', then we are entering a new
2242 block. Consume the entire block. */
2243 if (token->type == CPP_OPEN_BRACE)
2248 /* Skip tokens until a non-nested closing curly brace is the next
2252 cp_parser_skip_to_closing_brace (cp_parser *parser)
2254 unsigned nesting_depth = 0;
2260 /* Peek at the next token. */
2261 token = cp_lexer_peek_token (parser->lexer);
2262 /* If we've run out of tokens, stop. */
2263 if (token->type == CPP_EOF)
2265 /* If the next token is a non-nested `}', then we have reached
2266 the end of the current block. */
2267 if (token->type == CPP_CLOSE_BRACE && nesting_depth-- == 0)
2269 /* If it the next token is a `{', then we are entering a new
2270 block. Consume the entire block. */
2271 else if (token->type == CPP_OPEN_BRACE)
2273 /* Consume the token. */
2274 cp_lexer_consume_token (parser->lexer);
2278 /* This is a simple wrapper around make_typename_type. When the id is
2279 an unresolved identifier node, we can provide a superior diagnostic
2280 using cp_parser_diagnose_invalid_type_name. */
2283 cp_parser_make_typename_type (cp_parser *parser, tree scope, tree id)
2286 if (TREE_CODE (id) == IDENTIFIER_NODE)
2288 result = make_typename_type (scope, id, /*complain=*/0);
2289 if (result == error_mark_node)
2290 cp_parser_diagnose_invalid_type_name (parser, scope, id);
2293 return make_typename_type (scope, id, tf_error);
2297 /* Create a new C++ parser. */
2300 cp_parser_new (void)
2305 /* cp_lexer_new_main is called before calling ggc_alloc because
2306 cp_lexer_new_main might load a PCH file. */
2307 lexer = cp_lexer_new_main ();
2309 parser = ggc_alloc_cleared (sizeof (cp_parser));
2310 parser->lexer = lexer;
2311 parser->context = cp_parser_context_new (NULL);
2313 /* For now, we always accept GNU extensions. */
2314 parser->allow_gnu_extensions_p = 1;
2316 /* The `>' token is a greater-than operator, not the end of a
2318 parser->greater_than_is_operator_p = true;
2320 parser->default_arg_ok_p = true;
2322 /* We are not parsing a constant-expression. */
2323 parser->integral_constant_expression_p = false;
2324 parser->allow_non_integral_constant_expression_p = false;
2325 parser->non_integral_constant_expression_p = false;
2327 /* We are not parsing offsetof. */
2328 parser->in_offsetof_p = false;
2330 /* Local variable names are not forbidden. */
2331 parser->local_variables_forbidden_p = false;
2333 /* We are not processing an `extern "C"' declaration. */
2334 parser->in_unbraced_linkage_specification_p = false;
2336 /* We are not processing a declarator. */
2337 parser->in_declarator_p = false;
2339 /* We are not processing a template-argument-list. */
2340 parser->in_template_argument_list_p = false;
2342 /* We are not in an iteration statement. */
2343 parser->in_iteration_statement_p = false;
2345 /* We are not in a switch statement. */
2346 parser->in_switch_statement_p = false;
2348 /* We are not parsing a type-id inside an expression. */
2349 parser->in_type_id_in_expr_p = false;
2351 /* The unparsed function queue is empty. */
2352 parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2354 /* There are no classes being defined. */
2355 parser->num_classes_being_defined = 0;
2357 /* No template parameters apply. */
2358 parser->num_template_parameter_lists = 0;
2363 /* Lexical conventions [gram.lex] */
2365 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
2369 cp_parser_identifier (cp_parser* parser)
2373 /* Look for the identifier. */
2374 token = cp_parser_require (parser, CPP_NAME, "identifier");
2375 /* Return the value. */
2376 return token ? token->value : error_mark_node;
2379 /* Basic concepts [gram.basic] */
2381 /* Parse a translation-unit.
2384 declaration-seq [opt]
2386 Returns TRUE if all went well. */
2389 cp_parser_translation_unit (cp_parser* parser)
2393 cp_parser_declaration_seq_opt (parser);
2395 /* If there are no tokens left then all went well. */
2396 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2399 /* Otherwise, issue an error message. */
2400 cp_parser_error (parser, "expected declaration");
2404 /* Consume the EOF token. */
2405 cp_parser_require (parser, CPP_EOF, "end-of-file");
2408 finish_translation_unit ();
2410 /* All went well. */
2414 /* Expressions [gram.expr] */
2416 /* Parse a primary-expression.
2427 ( compound-statement )
2428 __builtin_va_arg ( assignment-expression , type-id )
2433 Returns a representation of the expression.
2435 *IDK indicates what kind of id-expression (if any) was present.
2437 *QUALIFYING_CLASS is set to a non-NULL value if the id-expression can be
2438 used as the operand of a pointer-to-member. In that case,
2439 *QUALIFYING_CLASS gives the class that is used as the qualifying
2440 class in the pointer-to-member. */
2443 cp_parser_primary_expression (cp_parser *parser,
2445 tree *qualifying_class)
2449 /* Assume the primary expression is not an id-expression. */
2450 *idk = CP_ID_KIND_NONE;
2451 /* And that it cannot be used as pointer-to-member. */
2452 *qualifying_class = NULL_TREE;
2454 /* Peek at the next token. */
2455 token = cp_lexer_peek_token (parser->lexer);
2456 switch (token->type)
2469 token = cp_lexer_consume_token (parser->lexer);
2470 return token->value;
2472 case CPP_OPEN_PAREN:
2475 bool saved_greater_than_is_operator_p;
2477 /* Consume the `('. */
2478 cp_lexer_consume_token (parser->lexer);
2479 /* Within a parenthesized expression, a `>' token is always
2480 the greater-than operator. */
2481 saved_greater_than_is_operator_p
2482 = parser->greater_than_is_operator_p;
2483 parser->greater_than_is_operator_p = true;
2484 /* If we see `( { ' then we are looking at the beginning of
2485 a GNU statement-expression. */
2486 if (cp_parser_allow_gnu_extensions_p (parser)
2487 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
2489 /* Statement-expressions are not allowed by the standard. */
2491 pedwarn ("ISO C++ forbids braced-groups within expressions");
2493 /* And they're not allowed outside of a function-body; you
2494 cannot, for example, write:
2496 int i = ({ int j = 3; j + 1; });
2498 at class or namespace scope. */
2499 if (!at_function_scope_p ())
2500 error ("statement-expressions are allowed only inside functions");
2501 /* Start the statement-expression. */
2502 expr = begin_stmt_expr ();
2503 /* Parse the compound-statement. */
2504 cp_parser_compound_statement (parser, true);
2506 expr = finish_stmt_expr (expr, false);
2510 /* Parse the parenthesized expression. */
2511 expr = cp_parser_expression (parser);
2512 /* Let the front end know that this expression was
2513 enclosed in parentheses. This matters in case, for
2514 example, the expression is of the form `A::B', since
2515 `&A::B' might be a pointer-to-member, but `&(A::B)' is
2517 finish_parenthesized_expr (expr);
2519 /* The `>' token might be the end of a template-id or
2520 template-parameter-list now. */
2521 parser->greater_than_is_operator_p
2522 = saved_greater_than_is_operator_p;
2523 /* Consume the `)'. */
2524 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
2525 cp_parser_skip_to_end_of_statement (parser);
2531 switch (token->keyword)
2533 /* These two are the boolean literals. */
2535 cp_lexer_consume_token (parser->lexer);
2536 return boolean_true_node;
2538 cp_lexer_consume_token (parser->lexer);
2539 return boolean_false_node;
2541 /* The `__null' literal. */
2543 cp_lexer_consume_token (parser->lexer);
2546 /* Recognize the `this' keyword. */
2548 cp_lexer_consume_token (parser->lexer);
2549 if (parser->local_variables_forbidden_p)
2551 error ("`this' may not be used in this context");
2552 return error_mark_node;
2554 /* Pointers cannot appear in constant-expressions. */
2555 if (cp_parser_non_integral_constant_expression (parser,
2557 return error_mark_node;
2558 return finish_this_expr ();
2560 /* The `operator' keyword can be the beginning of an
2565 case RID_FUNCTION_NAME:
2566 case RID_PRETTY_FUNCTION_NAME:
2567 case RID_C99_FUNCTION_NAME:
2568 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
2569 __func__ are the names of variables -- but they are
2570 treated specially. Therefore, they are handled here,
2571 rather than relying on the generic id-expression logic
2572 below. Grammatically, these names are id-expressions.
2574 Consume the token. */
2575 token = cp_lexer_consume_token (parser->lexer);
2576 /* Look up the name. */
2577 return finish_fname (token->value);
2584 /* The `__builtin_va_arg' construct is used to handle
2585 `va_arg'. Consume the `__builtin_va_arg' token. */
2586 cp_lexer_consume_token (parser->lexer);
2587 /* Look for the opening `('. */
2588 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
2589 /* Now, parse the assignment-expression. */
2590 expression = cp_parser_assignment_expression (parser);
2591 /* Look for the `,'. */
2592 cp_parser_require (parser, CPP_COMMA, "`,'");
2593 /* Parse the type-id. */
2594 type = cp_parser_type_id (parser);
2595 /* Look for the closing `)'. */
2596 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
2597 /* Using `va_arg' in a constant-expression is not
2599 if (cp_parser_non_integral_constant_expression (parser,
2601 return error_mark_node;
2602 return build_x_va_arg (expression, type);
2608 bool saved_in_offsetof_p;
2610 /* Consume the "__offsetof__" token. */
2611 cp_lexer_consume_token (parser->lexer);
2612 /* Consume the opening `('. */
2613 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
2614 /* Parse the parenthesized (almost) constant-expression. */
2615 saved_in_offsetof_p = parser->in_offsetof_p;
2616 parser->in_offsetof_p = true;
2618 = cp_parser_constant_expression (parser,
2619 /*allow_non_constant_p=*/false,
2620 /*non_constant_p=*/NULL);
2621 parser->in_offsetof_p = saved_in_offsetof_p;
2622 /* Consume the closing ')'. */
2623 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
2629 cp_parser_error (parser, "expected primary-expression");
2630 return error_mark_node;
2633 /* An id-expression can start with either an identifier, a
2634 `::' as the beginning of a qualified-id, or the "operator"
2638 case CPP_TEMPLATE_ID:
2639 case CPP_NESTED_NAME_SPECIFIER:
2643 const char *error_msg;
2646 /* Parse the id-expression. */
2648 = cp_parser_id_expression (parser,
2649 /*template_keyword_p=*/false,
2650 /*check_dependency_p=*/true,
2651 /*template_p=*/NULL,
2652 /*declarator_p=*/false);
2653 if (id_expression == error_mark_node)
2654 return error_mark_node;
2655 /* If we have a template-id, then no further lookup is
2656 required. If the template-id was for a template-class, we
2657 will sometimes have a TYPE_DECL at this point. */
2658 else if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
2659 || TREE_CODE (id_expression) == TYPE_DECL)
2660 decl = id_expression;
2661 /* Look up the name. */
2664 decl = cp_parser_lookup_name_simple (parser, id_expression);
2665 /* If name lookup gives us a SCOPE_REF, then the
2666 qualifying scope was dependent. Just propagate the
2668 if (TREE_CODE (decl) == SCOPE_REF)
2670 if (TYPE_P (TREE_OPERAND (decl, 0)))
2671 *qualifying_class = TREE_OPERAND (decl, 0);
2674 /* Check to see if DECL is a local variable in a context
2675 where that is forbidden. */
2676 if (parser->local_variables_forbidden_p
2677 && local_variable_p (decl))
2679 /* It might be that we only found DECL because we are
2680 trying to be generous with pre-ISO scoping rules.
2681 For example, consider:
2685 for (int i = 0; i < 10; ++i) {}
2686 extern void f(int j = i);
2689 Here, name look up will originally find the out
2690 of scope `i'. We need to issue a warning message,
2691 but then use the global `i'. */
2692 decl = check_for_out_of_scope_variable (decl);
2693 if (local_variable_p (decl))
2695 error ("local variable `%D' may not appear in this context",
2697 return error_mark_node;
2702 decl = finish_id_expression (id_expression, decl, parser->scope,
2703 idk, qualifying_class,
2704 parser->integral_constant_expression_p,
2705 parser->allow_non_integral_constant_expression_p,
2706 &parser->non_integral_constant_expression_p,
2709 cp_parser_error (parser, error_msg);
2713 /* Anything else is an error. */
2715 cp_parser_error (parser, "expected primary-expression");
2716 return error_mark_node;
2720 /* Parse an id-expression.
2727 :: [opt] nested-name-specifier template [opt] unqualified-id
2729 :: operator-function-id
2732 Return a representation of the unqualified portion of the
2733 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
2734 a `::' or nested-name-specifier.
2736 Often, if the id-expression was a qualified-id, the caller will
2737 want to make a SCOPE_REF to represent the qualified-id. This
2738 function does not do this in order to avoid wastefully creating
2739 SCOPE_REFs when they are not required.
2741 If TEMPLATE_KEYWORD_P is true, then we have just seen the
2744 If CHECK_DEPENDENCY_P is false, then names are looked up inside
2745 uninstantiated templates.
2747 If *TEMPLATE_P is non-NULL, it is set to true iff the
2748 `template' keyword is used to explicitly indicate that the entity
2749 named is a template.
2751 If DECLARATOR_P is true, the id-expression is appearing as part of
2752 a declarator, rather than as part of an expression. */
2755 cp_parser_id_expression (cp_parser *parser,
2756 bool template_keyword_p,
2757 bool check_dependency_p,
2761 bool global_scope_p;
2762 bool nested_name_specifier_p;
2764 /* Assume the `template' keyword was not used. */
2766 *template_p = false;
2768 /* Look for the optional `::' operator. */
2770 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
2772 /* Look for the optional nested-name-specifier. */
2773 nested_name_specifier_p
2774 = (cp_parser_nested_name_specifier_opt (parser,
2775 /*typename_keyword_p=*/false,
2778 /*is_declarator=*/false)
2780 /* If there is a nested-name-specifier, then we are looking at
2781 the first qualified-id production. */
2782 if (nested_name_specifier_p)
2785 tree saved_object_scope;
2786 tree saved_qualifying_scope;
2787 tree unqualified_id;
2790 /* See if the next token is the `template' keyword. */
2792 template_p = &is_template;
2793 *template_p = cp_parser_optional_template_keyword (parser);
2794 /* Name lookup we do during the processing of the
2795 unqualified-id might obliterate SCOPE. */
2796 saved_scope = parser->scope;
2797 saved_object_scope = parser->object_scope;
2798 saved_qualifying_scope = parser->qualifying_scope;
2799 /* Process the final unqualified-id. */
2800 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
2803 /* Restore the SAVED_SCOPE for our caller. */
2804 parser->scope = saved_scope;
2805 parser->object_scope = saved_object_scope;
2806 parser->qualifying_scope = saved_qualifying_scope;
2808 return unqualified_id;
2810 /* Otherwise, if we are in global scope, then we are looking at one
2811 of the other qualified-id productions. */
2812 else if (global_scope_p)
2817 /* Peek at the next token. */
2818 token = cp_lexer_peek_token (parser->lexer);
2820 /* If it's an identifier, and the next token is not a "<", then
2821 we can avoid the template-id case. This is an optimization
2822 for this common case. */
2823 if (token->type == CPP_NAME
2824 && !cp_parser_nth_token_starts_template_argument_list_p
2826 return cp_parser_identifier (parser);
2828 cp_parser_parse_tentatively (parser);
2829 /* Try a template-id. */
2830 id = cp_parser_template_id (parser,
2831 /*template_keyword_p=*/false,
2832 /*check_dependency_p=*/true,
2834 /* If that worked, we're done. */
2835 if (cp_parser_parse_definitely (parser))
2838 /* Peek at the next token. (Changes in the token buffer may
2839 have invalidated the pointer obtained above.) */
2840 token = cp_lexer_peek_token (parser->lexer);
2842 switch (token->type)
2845 return cp_parser_identifier (parser);
2848 if (token->keyword == RID_OPERATOR)
2849 return cp_parser_operator_function_id (parser);
2853 cp_parser_error (parser, "expected id-expression");
2854 return error_mark_node;
2858 return cp_parser_unqualified_id (parser, template_keyword_p,
2859 /*check_dependency_p=*/true,
2863 /* Parse an unqualified-id.
2867 operator-function-id
2868 conversion-function-id
2872 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
2873 keyword, in a construct like `A::template ...'.
2875 Returns a representation of unqualified-id. For the `identifier'
2876 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
2877 production a BIT_NOT_EXPR is returned; the operand of the
2878 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
2879 other productions, see the documentation accompanying the
2880 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
2881 names are looked up in uninstantiated templates. If DECLARATOR_P
2882 is true, the unqualified-id is appearing as part of a declarator,
2883 rather than as part of an expression. */
2886 cp_parser_unqualified_id (cp_parser* parser,
2887 bool template_keyword_p,
2888 bool check_dependency_p,
2893 /* Peek at the next token. */
2894 token = cp_lexer_peek_token (parser->lexer);
2896 switch (token->type)
2902 /* We don't know yet whether or not this will be a
2904 cp_parser_parse_tentatively (parser);
2905 /* Try a template-id. */
2906 id = cp_parser_template_id (parser, template_keyword_p,
2909 /* If it worked, we're done. */
2910 if (cp_parser_parse_definitely (parser))
2912 /* Otherwise, it's an ordinary identifier. */
2913 return cp_parser_identifier (parser);
2916 case CPP_TEMPLATE_ID:
2917 return cp_parser_template_id (parser, template_keyword_p,
2924 tree qualifying_scope;
2928 /* Consume the `~' token. */
2929 cp_lexer_consume_token (parser->lexer);
2930 /* Parse the class-name. The standard, as written, seems to
2933 template <typename T> struct S { ~S (); };
2934 template <typename T> S<T>::~S() {}
2936 is invalid, since `~' must be followed by a class-name, but
2937 `S<T>' is dependent, and so not known to be a class.
2938 That's not right; we need to look in uninstantiated
2939 templates. A further complication arises from:
2941 template <typename T> void f(T t) {
2945 Here, it is not possible to look up `T' in the scope of `T'
2946 itself. We must look in both the current scope, and the
2947 scope of the containing complete expression.
2949 Yet another issue is:
2958 The standard does not seem to say that the `S' in `~S'
2959 should refer to the type `S' and not the data member
2962 /* DR 244 says that we look up the name after the "~" in the
2963 same scope as we looked up the qualifying name. That idea
2964 isn't fully worked out; it's more complicated than that. */
2965 scope = parser->scope;
2966 object_scope = parser->object_scope;
2967 qualifying_scope = parser->qualifying_scope;
2969 /* If the name is of the form "X::~X" it's OK. */
2970 if (scope && TYPE_P (scope)
2971 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
2972 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
2974 && (cp_lexer_peek_token (parser->lexer)->value
2975 == TYPE_IDENTIFIER (scope)))
2977 cp_lexer_consume_token (parser->lexer);
2978 return build_nt (BIT_NOT_EXPR, scope);
2981 /* If there was an explicit qualification (S::~T), first look
2982 in the scope given by the qualification (i.e., S). */
2985 cp_parser_parse_tentatively (parser);
2986 type_decl = cp_parser_class_name (parser,
2987 /*typename_keyword_p=*/false,
2988 /*template_keyword_p=*/false,
2990 /*check_dependency=*/false,
2991 /*class_head_p=*/false,
2993 if (cp_parser_parse_definitely (parser))
2994 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
2996 /* In "N::S::~S", look in "N" as well. */
2997 if (scope && qualifying_scope)
2999 cp_parser_parse_tentatively (parser);
3000 parser->scope = qualifying_scope;
3001 parser->object_scope = NULL_TREE;
3002 parser->qualifying_scope = NULL_TREE;
3004 = cp_parser_class_name (parser,
3005 /*typename_keyword_p=*/false,
3006 /*template_keyword_p=*/false,
3008 /*check_dependency=*/false,
3009 /*class_head_p=*/false,
3011 if (cp_parser_parse_definitely (parser))
3012 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3014 /* In "p->S::~T", look in the scope given by "*p" as well. */
3015 else if (object_scope)
3017 cp_parser_parse_tentatively (parser);
3018 parser->scope = object_scope;
3019 parser->object_scope = NULL_TREE;
3020 parser->qualifying_scope = NULL_TREE;
3022 = cp_parser_class_name (parser,
3023 /*typename_keyword_p=*/false,
3024 /*template_keyword_p=*/false,
3026 /*check_dependency=*/false,
3027 /*class_head_p=*/false,
3029 if (cp_parser_parse_definitely (parser))
3030 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3032 /* Look in the surrounding context. */
3033 parser->scope = NULL_TREE;
3034 parser->object_scope = NULL_TREE;
3035 parser->qualifying_scope = NULL_TREE;
3037 = cp_parser_class_name (parser,
3038 /*typename_keyword_p=*/false,
3039 /*template_keyword_p=*/false,
3041 /*check_dependency=*/false,
3042 /*class_head_p=*/false,
3044 /* If an error occurred, assume that the name of the
3045 destructor is the same as the name of the qualifying
3046 class. That allows us to keep parsing after running
3047 into ill-formed destructor names. */
3048 if (type_decl == error_mark_node && scope && TYPE_P (scope))
3049 return build_nt (BIT_NOT_EXPR, scope);
3050 else if (type_decl == error_mark_node)
3051 return error_mark_node;
3055 A typedef-name that names a class shall not be used as the
3056 identifier in the declarator for a destructor declaration. */
3058 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
3059 && !DECL_SELF_REFERENCE_P (type_decl))
3060 error ("typedef-name `%D' used as destructor declarator",
3063 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3067 if (token->keyword == RID_OPERATOR)
3071 /* This could be a template-id, so we try that first. */
3072 cp_parser_parse_tentatively (parser);
3073 /* Try a template-id. */
3074 id = cp_parser_template_id (parser, template_keyword_p,
3075 /*check_dependency_p=*/true,
3077 /* If that worked, we're done. */
3078 if (cp_parser_parse_definitely (parser))
3080 /* We still don't know whether we're looking at an
3081 operator-function-id or a conversion-function-id. */
3082 cp_parser_parse_tentatively (parser);
3083 /* Try an operator-function-id. */
3084 id = cp_parser_operator_function_id (parser);
3085 /* If that didn't work, try a conversion-function-id. */
3086 if (!cp_parser_parse_definitely (parser))
3087 id = cp_parser_conversion_function_id (parser);
3094 cp_parser_error (parser, "expected unqualified-id");
3095 return error_mark_node;
3099 /* Parse an (optional) nested-name-specifier.
3101 nested-name-specifier:
3102 class-or-namespace-name :: nested-name-specifier [opt]
3103 class-or-namespace-name :: template nested-name-specifier [opt]
3105 PARSER->SCOPE should be set appropriately before this function is
3106 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
3107 effect. TYPE_P is TRUE if we non-type bindings should be ignored
3110 Sets PARSER->SCOPE to the class (TYPE) or namespace
3111 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
3112 it unchanged if there is no nested-name-specifier. Returns the new
3113 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
3115 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
3116 part of a declaration and/or decl-specifier. */
3119 cp_parser_nested_name_specifier_opt (cp_parser *parser,
3120 bool typename_keyword_p,
3121 bool check_dependency_p,
3123 bool is_declaration)
3125 bool success = false;
3126 tree access_check = NULL_TREE;
3130 /* If the next token corresponds to a nested name specifier, there
3131 is no need to reparse it. However, if CHECK_DEPENDENCY_P is
3132 false, it may have been true before, in which case something
3133 like `A<X>::B<Y>::C' may have resulted in a nested-name-specifier
3134 of `A<X>::', where it should now be `A<X>::B<Y>::'. So, when
3135 CHECK_DEPENDENCY_P is false, we have to fall through into the
3137 if (check_dependency_p
3138 && cp_lexer_next_token_is (parser->lexer, CPP_NESTED_NAME_SPECIFIER))
3140 cp_parser_pre_parsed_nested_name_specifier (parser);
3141 return parser->scope;
3144 /* Remember where the nested-name-specifier starts. */
3145 if (cp_parser_parsing_tentatively (parser)
3146 && !cp_parser_committed_to_tentative_parse (parser))
3148 token = cp_lexer_peek_token (parser->lexer);
3149 start = cp_lexer_token_difference (parser->lexer,
3150 parser->lexer->first_token,
3156 push_deferring_access_checks (dk_deferred);
3162 tree saved_qualifying_scope;
3163 bool template_keyword_p;
3165 /* Spot cases that cannot be the beginning of a
3166 nested-name-specifier. */
3167 token = cp_lexer_peek_token (parser->lexer);
3169 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
3170 the already parsed nested-name-specifier. */
3171 if (token->type == CPP_NESTED_NAME_SPECIFIER)
3173 /* Grab the nested-name-specifier and continue the loop. */
3174 cp_parser_pre_parsed_nested_name_specifier (parser);
3179 /* Spot cases that cannot be the beginning of a
3180 nested-name-specifier. On the second and subsequent times
3181 through the loop, we look for the `template' keyword. */
3182 if (success && token->keyword == RID_TEMPLATE)
3184 /* A template-id can start a nested-name-specifier. */
3185 else if (token->type == CPP_TEMPLATE_ID)
3189 /* If the next token is not an identifier, then it is
3190 definitely not a class-or-namespace-name. */
3191 if (token->type != CPP_NAME)
3193 /* If the following token is neither a `<' (to begin a
3194 template-id), nor a `::', then we are not looking at a
3195 nested-name-specifier. */
3196 token = cp_lexer_peek_nth_token (parser->lexer, 2);
3197 if (token->type != CPP_SCOPE
3198 && !cp_parser_nth_token_starts_template_argument_list_p
3203 /* The nested-name-specifier is optional, so we parse
3205 cp_parser_parse_tentatively (parser);
3207 /* Look for the optional `template' keyword, if this isn't the
3208 first time through the loop. */
3210 template_keyword_p = cp_parser_optional_template_keyword (parser);
3212 template_keyword_p = false;
3214 /* Save the old scope since the name lookup we are about to do
3215 might destroy it. */
3216 old_scope = parser->scope;
3217 saved_qualifying_scope = parser->qualifying_scope;
3218 /* Parse the qualifying entity. */
3220 = cp_parser_class_or_namespace_name (parser,
3226 /* Look for the `::' token. */
3227 cp_parser_require (parser, CPP_SCOPE, "`::'");
3229 /* If we found what we wanted, we keep going; otherwise, we're
3231 if (!cp_parser_parse_definitely (parser))
3233 bool error_p = false;
3235 /* Restore the OLD_SCOPE since it was valid before the
3236 failed attempt at finding the last
3237 class-or-namespace-name. */
3238 parser->scope = old_scope;
3239 parser->qualifying_scope = saved_qualifying_scope;
3240 /* If the next token is an identifier, and the one after
3241 that is a `::', then any valid interpretation would have
3242 found a class-or-namespace-name. */
3243 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
3244 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3246 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
3249 token = cp_lexer_consume_token (parser->lexer);
3254 decl = cp_parser_lookup_name_simple (parser, token->value);
3255 if (TREE_CODE (decl) == TEMPLATE_DECL)
3256 error ("`%D' used without template parameters",
3259 cp_parser_name_lookup_error
3260 (parser, token->value, decl,
3261 "is not a class or namespace");
3262 parser->scope = NULL_TREE;
3264 /* Treat this as a successful nested-name-specifier
3269 If the name found is not a class-name (clause
3270 _class_) or namespace-name (_namespace.def_), the
3271 program is ill-formed. */
3274 cp_lexer_consume_token (parser->lexer);
3279 /* We've found one valid nested-name-specifier. */
3281 /* Make sure we look in the right scope the next time through
3283 parser->scope = (TREE_CODE (new_scope) == TYPE_DECL
3284 ? TREE_TYPE (new_scope)
3286 /* If it is a class scope, try to complete it; we are about to
3287 be looking up names inside the class. */
3288 if (TYPE_P (parser->scope)
3289 /* Since checking types for dependency can be expensive,
3290 avoid doing it if the type is already complete. */
3291 && !COMPLETE_TYPE_P (parser->scope)
3292 /* Do not try to complete dependent types. */
3293 && !dependent_type_p (parser->scope))
3294 complete_type (parser->scope);
3297 /* Retrieve any deferred checks. Do not pop this access checks yet
3298 so the memory will not be reclaimed during token replacing below. */
3299 access_check = get_deferred_access_checks ();
3301 /* If parsing tentatively, replace the sequence of tokens that makes
3302 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
3303 token. That way, should we re-parse the token stream, we will
3304 not have to repeat the effort required to do the parse, nor will
3305 we issue duplicate error messages. */
3306 if (success && start >= 0)
3308 /* Find the token that corresponds to the start of the
3310 token = cp_lexer_advance_token (parser->lexer,
3311 parser->lexer->first_token,
3314 /* Reset the contents of the START token. */
3315 token->type = CPP_NESTED_NAME_SPECIFIER;
3316 token->value = build_tree_list (access_check, parser->scope);
3317 TREE_TYPE (token->value) = parser->qualifying_scope;
3318 token->keyword = RID_MAX;
3319 /* Purge all subsequent tokens. */
3320 cp_lexer_purge_tokens_after (parser->lexer, token);
3323 pop_deferring_access_checks ();
3324 return success ? parser->scope : NULL_TREE;
3327 /* Parse a nested-name-specifier. See
3328 cp_parser_nested_name_specifier_opt for details. This function
3329 behaves identically, except that it will an issue an error if no
3330 nested-name-specifier is present, and it will return
3331 ERROR_MARK_NODE, rather than NULL_TREE, if no nested-name-specifier
3335 cp_parser_nested_name_specifier (cp_parser *parser,
3336 bool typename_keyword_p,
3337 bool check_dependency_p,
3339 bool is_declaration)
3343 /* Look for the nested-name-specifier. */
3344 scope = cp_parser_nested_name_specifier_opt (parser,
3349 /* If it was not present, issue an error message. */
3352 cp_parser_error (parser, "expected nested-name-specifier");
3353 parser->scope = NULL_TREE;
3354 return error_mark_node;
3360 /* Parse a class-or-namespace-name.
3362 class-or-namespace-name:
3366 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
3367 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
3368 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
3369 TYPE_P is TRUE iff the next name should be taken as a class-name,
3370 even the same name is declared to be another entity in the same
3373 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
3374 specified by the class-or-namespace-name. If neither is found the
3375 ERROR_MARK_NODE is returned. */
3378 cp_parser_class_or_namespace_name (cp_parser *parser,
3379 bool typename_keyword_p,
3380 bool template_keyword_p,
3381 bool check_dependency_p,
3383 bool is_declaration)
3386 tree saved_qualifying_scope;
3387 tree saved_object_scope;
3391 /* Before we try to parse the class-name, we must save away the
3392 current PARSER->SCOPE since cp_parser_class_name will destroy
3394 saved_scope = parser->scope;
3395 saved_qualifying_scope = parser->qualifying_scope;
3396 saved_object_scope = parser->object_scope;
3397 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
3398 there is no need to look for a namespace-name. */
3399 only_class_p = template_keyword_p || (saved_scope && TYPE_P (saved_scope));
3401 cp_parser_parse_tentatively (parser);
3402 scope = cp_parser_class_name (parser,
3407 /*class_head_p=*/false,
3409 /* If that didn't work, try for a namespace-name. */
3410 if (!only_class_p && !cp_parser_parse_definitely (parser))
3412 /* Restore the saved scope. */
3413 parser->scope = saved_scope;
3414 parser->qualifying_scope = saved_qualifying_scope;
3415 parser->object_scope = saved_object_scope;
3416 /* If we are not looking at an identifier followed by the scope
3417 resolution operator, then this is not part of a
3418 nested-name-specifier. (Note that this function is only used
3419 to parse the components of a nested-name-specifier.) */
3420 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
3421 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
3422 return error_mark_node;
3423 scope = cp_parser_namespace_name (parser);
3429 /* Parse a postfix-expression.
3433 postfix-expression [ expression ]
3434 postfix-expression ( expression-list [opt] )
3435 simple-type-specifier ( expression-list [opt] )
3436 typename :: [opt] nested-name-specifier identifier
3437 ( expression-list [opt] )
3438 typename :: [opt] nested-name-specifier template [opt] template-id
3439 ( expression-list [opt] )
3440 postfix-expression . template [opt] id-expression
3441 postfix-expression -> template [opt] id-expression
3442 postfix-expression . pseudo-destructor-name
3443 postfix-expression -> pseudo-destructor-name
3444 postfix-expression ++
3445 postfix-expression --
3446 dynamic_cast < type-id > ( expression )
3447 static_cast < type-id > ( expression )
3448 reinterpret_cast < type-id > ( expression )
3449 const_cast < type-id > ( expression )
3450 typeid ( expression )
3456 ( type-id ) { initializer-list , [opt] }
3458 This extension is a GNU version of the C99 compound-literal
3459 construct. (The C99 grammar uses `type-name' instead of `type-id',
3460 but they are essentially the same concept.)
3462 If ADDRESS_P is true, the postfix expression is the operand of the
3465 Returns a representation of the expression. */
3468 cp_parser_postfix_expression (cp_parser *parser, bool address_p)
3472 cp_id_kind idk = CP_ID_KIND_NONE;
3473 tree postfix_expression = NULL_TREE;
3474 /* Non-NULL only if the current postfix-expression can be used to
3475 form a pointer-to-member. In that case, QUALIFYING_CLASS is the
3476 class used to qualify the member. */
3477 tree qualifying_class = NULL_TREE;
3479 /* Peek at the next token. */
3480 token = cp_lexer_peek_token (parser->lexer);
3481 /* Some of the productions are determined by keywords. */
3482 keyword = token->keyword;
3492 const char *saved_message;
3494 /* All of these can be handled in the same way from the point
3495 of view of parsing. Begin by consuming the token
3496 identifying the cast. */
3497 cp_lexer_consume_token (parser->lexer);
3499 /* New types cannot be defined in the cast. */
3500 saved_message = parser->type_definition_forbidden_message;
3501 parser->type_definition_forbidden_message
3502 = "types may not be defined in casts";
3504 /* Look for the opening `<'. */
3505 cp_parser_require (parser, CPP_LESS, "`<'");
3506 /* Parse the type to which we are casting. */
3507 type = cp_parser_type_id (parser);
3508 /* Look for the closing `>'. */
3509 cp_parser_require (parser, CPP_GREATER, "`>'");
3510 /* Restore the old message. */
3511 parser->type_definition_forbidden_message = saved_message;
3513 /* And the expression which is being cast. */
3514 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3515 expression = cp_parser_expression (parser);
3516 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3518 /* Only type conversions to integral or enumeration types
3519 can be used in constant-expressions. */
3520 if (parser->integral_constant_expression_p
3521 && !dependent_type_p (type)
3522 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type)
3523 /* A cast to pointer or reference type is allowed in the
3524 implementation of "offsetof". */
3525 && !(parser->in_offsetof_p && POINTER_TYPE_P (type))
3526 && (cp_parser_non_integral_constant_expression
3528 "a cast to a type other than an integral or "
3529 "enumeration type")))
3530 return error_mark_node;
3536 = build_dynamic_cast (type, expression);
3540 = build_static_cast (type, expression);
3544 = build_reinterpret_cast (type, expression);
3548 = build_const_cast (type, expression);
3559 const char *saved_message;
3560 bool saved_in_type_id_in_expr_p;
3562 /* Consume the `typeid' token. */
3563 cp_lexer_consume_token (parser->lexer);
3564 /* Look for the `(' token. */
3565 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3566 /* Types cannot be defined in a `typeid' expression. */
3567 saved_message = parser->type_definition_forbidden_message;
3568 parser->type_definition_forbidden_message
3569 = "types may not be defined in a `typeid\' expression";
3570 /* We can't be sure yet whether we're looking at a type-id or an
3572 cp_parser_parse_tentatively (parser);
3573 /* Try a type-id first. */
3574 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
3575 parser->in_type_id_in_expr_p = true;
3576 type = cp_parser_type_id (parser);
3577 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
3578 /* Look for the `)' token. Otherwise, we can't be sure that
3579 we're not looking at an expression: consider `typeid (int
3580 (3))', for example. */
3581 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3582 /* If all went well, simply lookup the type-id. */
3583 if (cp_parser_parse_definitely (parser))
3584 postfix_expression = get_typeid (type);
3585 /* Otherwise, fall back to the expression variant. */
3590 /* Look for an expression. */
3591 expression = cp_parser_expression (parser);
3592 /* Compute its typeid. */
3593 postfix_expression = build_typeid (expression);
3594 /* Look for the `)' token. */
3595 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3598 /* Restore the saved message. */
3599 parser->type_definition_forbidden_message = saved_message;
3605 bool template_p = false;
3609 /* Consume the `typename' token. */
3610 cp_lexer_consume_token (parser->lexer);
3611 /* Look for the optional `::' operator. */
3612 cp_parser_global_scope_opt (parser,
3613 /*current_scope_valid_p=*/false);
3614 /* Look for the nested-name-specifier. */
3615 cp_parser_nested_name_specifier (parser,
3616 /*typename_keyword_p=*/true,
3617 /*check_dependency_p=*/true,
3619 /*is_declaration=*/true);
3620 /* Look for the optional `template' keyword. */
3621 template_p = cp_parser_optional_template_keyword (parser);
3622 /* We don't know whether we're looking at a template-id or an
3624 cp_parser_parse_tentatively (parser);
3625 /* Try a template-id. */
3626 id = cp_parser_template_id (parser, template_p,
3627 /*check_dependency_p=*/true,
3628 /*is_declaration=*/true);
3629 /* If that didn't work, try an identifier. */
3630 if (!cp_parser_parse_definitely (parser))
3631 id = cp_parser_identifier (parser);
3632 /* If we look up a template-id in a non-dependent qualifying
3633 scope, there's no need to create a dependent type. */
3634 if (TREE_CODE (id) == TYPE_DECL
3635 && !dependent_type_p (parser->scope))
3636 type = TREE_TYPE (id);
3637 /* Create a TYPENAME_TYPE to represent the type to which the
3638 functional cast is being performed. */
3640 type = make_typename_type (parser->scope, id,
3643 postfix_expression = cp_parser_functional_cast (parser, type);
3651 /* If the next thing is a simple-type-specifier, we may be
3652 looking at a functional cast. We could also be looking at
3653 an id-expression. So, we try the functional cast, and if
3654 that doesn't work we fall back to the primary-expression. */
3655 cp_parser_parse_tentatively (parser);
3656 /* Look for the simple-type-specifier. */
3657 type = cp_parser_simple_type_specifier (parser,
3658 CP_PARSER_FLAGS_NONE,
3659 /*identifier_p=*/false);
3660 /* Parse the cast itself. */
3661 if (!cp_parser_error_occurred (parser))
3663 = cp_parser_functional_cast (parser, type);
3664 /* If that worked, we're done. */
3665 if (cp_parser_parse_definitely (parser))
3668 /* If the functional-cast didn't work out, try a
3669 compound-literal. */
3670 if (cp_parser_allow_gnu_extensions_p (parser)
3671 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
3673 tree initializer_list = NULL_TREE;
3674 bool saved_in_type_id_in_expr_p;
3676 cp_parser_parse_tentatively (parser);
3677 /* Consume the `('. */
3678 cp_lexer_consume_token (parser->lexer);
3679 /* Parse the type. */
3680 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
3681 parser->in_type_id_in_expr_p = true;
3682 type = cp_parser_type_id (parser);
3683 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
3684 /* Look for the `)'. */
3685 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3686 /* Look for the `{'. */
3687 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
3688 /* If things aren't going well, there's no need to
3690 if (!cp_parser_error_occurred (parser))
3692 bool non_constant_p;
3693 /* Parse the initializer-list. */
3695 = cp_parser_initializer_list (parser, &non_constant_p);
3696 /* Allow a trailing `,'. */
3697 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
3698 cp_lexer_consume_token (parser->lexer);
3699 /* Look for the final `}'. */
3700 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
3702 /* If that worked, we're definitely looking at a
3703 compound-literal expression. */
3704 if (cp_parser_parse_definitely (parser))
3706 /* Warn the user that a compound literal is not
3707 allowed in standard C++. */
3709 pedwarn ("ISO C++ forbids compound-literals");
3710 /* Form the representation of the compound-literal. */
3712 = finish_compound_literal (type, initializer_list);
3717 /* It must be a primary-expression. */
3718 postfix_expression = cp_parser_primary_expression (parser,
3725 /* If we were avoiding committing to the processing of a
3726 qualified-id until we knew whether or not we had a
3727 pointer-to-member, we now know. */
3728 if (qualifying_class)
3732 /* Peek at the next token. */
3733 token = cp_lexer_peek_token (parser->lexer);
3734 done = (token->type != CPP_OPEN_SQUARE
3735 && token->type != CPP_OPEN_PAREN
3736 && token->type != CPP_DOT
3737 && token->type != CPP_DEREF
3738 && token->type != CPP_PLUS_PLUS
3739 && token->type != CPP_MINUS_MINUS);
3741 postfix_expression = finish_qualified_id_expr (qualifying_class,
3746 return postfix_expression;
3749 /* Keep looping until the postfix-expression is complete. */
3752 if (idk == CP_ID_KIND_UNQUALIFIED
3753 && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
3754 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
3755 /* It is not a Koenig lookup function call. */
3757 = unqualified_name_lookup_error (postfix_expression);
3759 /* Peek at the next token. */
3760 token = cp_lexer_peek_token (parser->lexer);
3762 switch (token->type)
3764 case CPP_OPEN_SQUARE:
3765 /* postfix-expression [ expression ] */
3769 /* Consume the `[' token. */
3770 cp_lexer_consume_token (parser->lexer);
3771 /* Parse the index expression. */
3772 index = cp_parser_expression (parser);
3773 /* Look for the closing `]'. */
3774 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
3776 /* Build the ARRAY_REF. */
3778 = grok_array_decl (postfix_expression, index);
3779 idk = CP_ID_KIND_NONE;
3780 /* Array references are not permitted in
3781 constant-expressions. */
3782 if (cp_parser_non_integral_constant_expression
3783 (parser, "an array reference"))
3784 postfix_expression = error_mark_node;
3788 case CPP_OPEN_PAREN:
3789 /* postfix-expression ( expression-list [opt] ) */
3792 tree args = (cp_parser_parenthesized_expression_list
3793 (parser, false, /*non_constant_p=*/NULL));
3795 if (args == error_mark_node)
3797 postfix_expression = error_mark_node;
3801 /* Function calls are not permitted in
3802 constant-expressions. */
3803 if (cp_parser_non_integral_constant_expression (parser,
3806 postfix_expression = error_mark_node;
3811 if (idk == CP_ID_KIND_UNQUALIFIED)
3813 /* We do not perform argument-dependent lookup if
3814 normal lookup finds a non-function, in accordance
3815 with the expected resolution of DR 218. */
3817 && (is_overloaded_fn (postfix_expression)
3818 || TREE_CODE (postfix_expression) == IDENTIFIER_NODE))
3822 = perform_koenig_lookup (postfix_expression, args);
3824 else if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
3826 = unqualified_fn_lookup_error (postfix_expression);
3829 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
3831 tree instance = TREE_OPERAND (postfix_expression, 0);
3832 tree fn = TREE_OPERAND (postfix_expression, 1);
3834 if (processing_template_decl
3835 && (type_dependent_expression_p (instance)
3836 || (!BASELINK_P (fn)
3837 && TREE_CODE (fn) != FIELD_DECL)
3838 || type_dependent_expression_p (fn)
3839 || any_type_dependent_arguments_p (args)))
3842 = build_min_nt (CALL_EXPR, postfix_expression, args);
3846 if (BASELINK_P (fn))
3848 = (build_new_method_call
3849 (instance, fn, args, NULL_TREE,
3850 (idk == CP_ID_KIND_QUALIFIED
3851 ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL)));
3854 = finish_call_expr (postfix_expression, args,
3855 /*disallow_virtual=*/false,
3856 /*koenig_p=*/false);
3858 else if (TREE_CODE (postfix_expression) == OFFSET_REF
3859 || TREE_CODE (postfix_expression) == MEMBER_REF
3860 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
3861 postfix_expression = (build_offset_ref_call_from_tree
3862 (postfix_expression, args));
3863 else if (idk == CP_ID_KIND_QUALIFIED)
3864 /* A call to a static class member, or a namespace-scope
3867 = finish_call_expr (postfix_expression, args,
3868 /*disallow_virtual=*/true,
3871 /* All other function calls. */
3873 = finish_call_expr (postfix_expression, args,
3874 /*disallow_virtual=*/false,
3877 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
3878 idk = CP_ID_KIND_NONE;
3884 /* postfix-expression . template [opt] id-expression
3885 postfix-expression . pseudo-destructor-name
3886 postfix-expression -> template [opt] id-expression
3887 postfix-expression -> pseudo-destructor-name */
3892 tree scope = NULL_TREE;
3893 enum cpp_ttype token_type = token->type;
3895 /* If this is a `->' operator, dereference the pointer. */
3896 if (token->type == CPP_DEREF)
3897 postfix_expression = build_x_arrow (postfix_expression);
3898 /* Check to see whether or not the expression is
3900 dependent_p = type_dependent_expression_p (postfix_expression);
3901 /* The identifier following the `->' or `.' is not
3903 parser->scope = NULL_TREE;
3904 parser->qualifying_scope = NULL_TREE;
3905 parser->object_scope = NULL_TREE;
3906 idk = CP_ID_KIND_NONE;
3907 /* Enter the scope corresponding to the type of the object
3908 given by the POSTFIX_EXPRESSION. */
3910 && TREE_TYPE (postfix_expression) != NULL_TREE)
3912 scope = TREE_TYPE (postfix_expression);
3913 /* According to the standard, no expression should
3914 ever have reference type. Unfortunately, we do not
3915 currently match the standard in this respect in
3916 that our internal representation of an expression
3917 may have reference type even when the standard says
3918 it does not. Therefore, we have to manually obtain
3919 the underlying type here. */
3920 scope = non_reference (scope);
3921 /* The type of the POSTFIX_EXPRESSION must be
3923 scope = complete_type_or_else (scope, NULL_TREE);
3924 /* Let the name lookup machinery know that we are
3925 processing a class member access expression. */
3926 parser->context->object_type = scope;
3927 /* If something went wrong, we want to be able to
3928 discern that case, as opposed to the case where
3929 there was no SCOPE due to the type of expression
3932 scope = error_mark_node;
3933 /* If the SCOPE was erroneous, make the various
3934 semantic analysis functions exit quickly -- and
3935 without issuing additional error messages. */
3936 if (scope == error_mark_node)
3937 postfix_expression = error_mark_node;
3940 /* Consume the `.' or `->' operator. */
3941 cp_lexer_consume_token (parser->lexer);
3942 /* If the SCOPE is not a scalar type, we are looking at an
3943 ordinary class member access expression, rather than a
3944 pseudo-destructor-name. */
3945 if (!scope || !SCALAR_TYPE_P (scope))
3947 template_p = cp_parser_optional_template_keyword (parser);
3948 /* Parse the id-expression. */
3949 name = cp_parser_id_expression (parser,
3951 /*check_dependency_p=*/true,
3952 /*template_p=*/NULL,
3953 /*declarator_p=*/false);
3954 /* In general, build a SCOPE_REF if the member name is
3955 qualified. However, if the name was not dependent
3956 and has already been resolved; there is no need to
3957 build the SCOPE_REF. For example;
3959 struct X { void f(); };
3960 template <typename T> void f(T* t) { t->X::f(); }
3962 Even though "t" is dependent, "X::f" is not and has
3963 been resolved to a BASELINK; there is no need to
3964 include scope information. */
3966 /* But we do need to remember that there was an explicit
3967 scope for virtual function calls. */
3969 idk = CP_ID_KIND_QUALIFIED;
3971 if (name != error_mark_node
3972 && !BASELINK_P (name)
3975 name = build_nt (SCOPE_REF, parser->scope, name);
3976 parser->scope = NULL_TREE;
3977 parser->qualifying_scope = NULL_TREE;
3978 parser->object_scope = NULL_TREE;
3980 if (scope && name && BASELINK_P (name))
3981 adjust_result_of_qualified_name_lookup
3982 (name, BINFO_TYPE (BASELINK_BINFO (name)), scope);
3984 = finish_class_member_access_expr (postfix_expression, name);
3986 /* Otherwise, try the pseudo-destructor-name production. */
3992 /* Parse the pseudo-destructor-name. */
3993 cp_parser_pseudo_destructor_name (parser, &s, &type);
3994 /* Form the call. */
3996 = finish_pseudo_destructor_expr (postfix_expression,
3997 s, TREE_TYPE (type));
4000 /* We no longer need to look up names in the scope of the
4001 object on the left-hand side of the `.' or `->'
4003 parser->context->object_type = NULL_TREE;
4004 /* These operators may not appear in constant-expressions. */
4005 if (/* The "->" operator is allowed in the implementation
4006 of "offsetof". The "." operator may appear in the
4007 name of the member. */
4008 !parser->in_offsetof_p
4009 && (cp_parser_non_integral_constant_expression
4011 token_type == CPP_DEREF ? "'->'" : "`.'")))
4012 postfix_expression = error_mark_node;
4017 /* postfix-expression ++ */
4018 /* Consume the `++' token. */
4019 cp_lexer_consume_token (parser->lexer);
4020 /* Generate a representation for the complete expression. */
4022 = finish_increment_expr (postfix_expression,
4023 POSTINCREMENT_EXPR);
4024 /* Increments may not appear in constant-expressions. */
4025 if (cp_parser_non_integral_constant_expression (parser,
4027 postfix_expression = error_mark_node;
4028 idk = CP_ID_KIND_NONE;
4031 case CPP_MINUS_MINUS:
4032 /* postfix-expression -- */
4033 /* Consume the `--' token. */
4034 cp_lexer_consume_token (parser->lexer);
4035 /* Generate a representation for the complete expression. */
4037 = finish_increment_expr (postfix_expression,
4038 POSTDECREMENT_EXPR);
4039 /* Decrements may not appear in constant-expressions. */
4040 if (cp_parser_non_integral_constant_expression (parser,
4042 postfix_expression = error_mark_node;
4043 idk = CP_ID_KIND_NONE;
4047 return postfix_expression;
4051 /* We should never get here. */
4053 return error_mark_node;
4056 /* Parse a parenthesized expression-list.
4059 assignment-expression
4060 expression-list, assignment-expression
4065 identifier, expression-list
4067 Returns a TREE_LIST. The TREE_VALUE of each node is a
4068 representation of an assignment-expression. Note that a TREE_LIST
4069 is returned even if there is only a single expression in the list.
4070 error_mark_node is returned if the ( and or ) are
4071 missing. NULL_TREE is returned on no expressions. The parentheses
4072 are eaten. IS_ATTRIBUTE_LIST is true if this is really an attribute
4073 list being parsed. If NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P
4074 indicates whether or not all of the expressions in the list were
4078 cp_parser_parenthesized_expression_list (cp_parser* parser,
4079 bool is_attribute_list,
4080 bool *non_constant_p)
4082 tree expression_list = NULL_TREE;
4083 tree identifier = NULL_TREE;
4085 /* Assume all the expressions will be constant. */
4087 *non_constant_p = false;
4089 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
4090 return error_mark_node;
4092 /* Consume expressions until there are no more. */
4093 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
4098 /* At the beginning of attribute lists, check to see if the
4099 next token is an identifier. */
4100 if (is_attribute_list
4101 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
4105 /* Consume the identifier. */
4106 token = cp_lexer_consume_token (parser->lexer);
4107 /* Save the identifier. */
4108 identifier = token->value;
4112 /* Parse the next assignment-expression. */
4115 bool expr_non_constant_p;
4116 expr = (cp_parser_constant_expression
4117 (parser, /*allow_non_constant_p=*/true,
4118 &expr_non_constant_p));
4119 if (expr_non_constant_p)
4120 *non_constant_p = true;
4123 expr = cp_parser_assignment_expression (parser);
4125 /* Add it to the list. We add error_mark_node
4126 expressions to the list, so that we can still tell if
4127 the correct form for a parenthesized expression-list
4128 is found. That gives better errors. */
4129 expression_list = tree_cons (NULL_TREE, expr, expression_list);
4131 if (expr == error_mark_node)
4135 /* After the first item, attribute lists look the same as
4136 expression lists. */
4137 is_attribute_list = false;
4140 /* If the next token isn't a `,', then we are done. */
4141 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
4144 /* Otherwise, consume the `,' and keep going. */
4145 cp_lexer_consume_token (parser->lexer);
4148 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
4153 /* We try and resync to an unnested comma, as that will give the
4154 user better diagnostics. */
4155 ending = cp_parser_skip_to_closing_parenthesis (parser,
4156 /*recovering=*/true,
4158 /*consume_paren=*/true);
4162 return error_mark_node;
4165 /* We built up the list in reverse order so we must reverse it now. */
4166 expression_list = nreverse (expression_list);
4168 expression_list = tree_cons (NULL_TREE, identifier, expression_list);
4170 return expression_list;
4173 /* Parse a pseudo-destructor-name.
4175 pseudo-destructor-name:
4176 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
4177 :: [opt] nested-name-specifier template template-id :: ~ type-name
4178 :: [opt] nested-name-specifier [opt] ~ type-name
4180 If either of the first two productions is used, sets *SCOPE to the
4181 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
4182 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
4183 or ERROR_MARK_NODE if the parse fails. */
4186 cp_parser_pseudo_destructor_name (cp_parser* parser,
4190 bool nested_name_specifier_p;
4192 /* Look for the optional `::' operator. */
4193 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
4194 /* Look for the optional nested-name-specifier. */
4195 nested_name_specifier_p
4196 = (cp_parser_nested_name_specifier_opt (parser,
4197 /*typename_keyword_p=*/false,
4198 /*check_dependency_p=*/true,
4200 /*is_declaration=*/true)
4202 /* Now, if we saw a nested-name-specifier, we might be doing the
4203 second production. */
4204 if (nested_name_specifier_p
4205 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
4207 /* Consume the `template' keyword. */
4208 cp_lexer_consume_token (parser->lexer);
4209 /* Parse the template-id. */
4210 cp_parser_template_id (parser,
4211 /*template_keyword_p=*/true,
4212 /*check_dependency_p=*/false,
4213 /*is_declaration=*/true);
4214 /* Look for the `::' token. */
4215 cp_parser_require (parser, CPP_SCOPE, "`::'");
4217 /* If the next token is not a `~', then there might be some
4218 additional qualification. */
4219 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
4221 /* Look for the type-name. */
4222 *scope = TREE_TYPE (cp_parser_type_name (parser));
4224 /* If we didn't get an aggregate type, or we don't have ::~,
4225 then something has gone wrong. Since the only caller of this
4226 function is looking for something after `.' or `->' after a
4227 scalar type, most likely the program is trying to get a
4228 member of a non-aggregate type. */
4229 if (*scope == error_mark_node
4230 || cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE)
4231 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_COMPL)
4233 cp_parser_error (parser, "request for member of non-aggregate type");
4234 *type = error_mark_node;
4238 /* Look for the `::' token. */
4239 cp_parser_require (parser, CPP_SCOPE, "`::'");
4244 /* Look for the `~'. */
4245 cp_parser_require (parser, CPP_COMPL, "`~'");
4246 /* Look for the type-name again. We are not responsible for
4247 checking that it matches the first type-name. */
4248 *type = cp_parser_type_name (parser);
4251 /* Parse a unary-expression.
4257 unary-operator cast-expression
4258 sizeof unary-expression
4266 __extension__ cast-expression
4267 __alignof__ unary-expression
4268 __alignof__ ( type-id )
4269 __real__ cast-expression
4270 __imag__ cast-expression
4273 ADDRESS_P is true iff the unary-expression is appearing as the
4274 operand of the `&' operator.
4276 Returns a representation of the expression. */
4279 cp_parser_unary_expression (cp_parser *parser, bool address_p)
4282 enum tree_code unary_operator;
4284 /* Peek at the next token. */
4285 token = cp_lexer_peek_token (parser->lexer);
4286 /* Some keywords give away the kind of expression. */
4287 if (token->type == CPP_KEYWORD)
4289 enum rid keyword = token->keyword;
4299 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
4300 /* Consume the token. */
4301 cp_lexer_consume_token (parser->lexer);
4302 /* Parse the operand. */
4303 operand = cp_parser_sizeof_operand (parser, keyword);
4305 if (TYPE_P (operand))
4306 return cxx_sizeof_or_alignof_type (operand, op, true);
4308 return cxx_sizeof_or_alignof_expr (operand, op);
4312 return cp_parser_new_expression (parser);
4315 return cp_parser_delete_expression (parser);
4319 /* The saved value of the PEDANTIC flag. */
4323 /* Save away the PEDANTIC flag. */
4324 cp_parser_extension_opt (parser, &saved_pedantic);
4325 /* Parse the cast-expression. */
4326 expr = cp_parser_simple_cast_expression (parser);
4327 /* Restore the PEDANTIC flag. */
4328 pedantic = saved_pedantic;
4338 /* Consume the `__real__' or `__imag__' token. */
4339 cp_lexer_consume_token (parser->lexer);
4340 /* Parse the cast-expression. */
4341 expression = cp_parser_simple_cast_expression (parser);
4342 /* Create the complete representation. */
4343 return build_x_unary_op ((keyword == RID_REALPART
4344 ? REALPART_EXPR : IMAGPART_EXPR),
4354 /* Look for the `:: new' and `:: delete', which also signal the
4355 beginning of a new-expression, or delete-expression,
4356 respectively. If the next token is `::', then it might be one of
4358 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
4362 /* See if the token after the `::' is one of the keywords in
4363 which we're interested. */
4364 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
4365 /* If it's `new', we have a new-expression. */
4366 if (keyword == RID_NEW)
4367 return cp_parser_new_expression (parser);
4368 /* Similarly, for `delete'. */
4369 else if (keyword == RID_DELETE)
4370 return cp_parser_delete_expression (parser);
4373 /* Look for a unary operator. */
4374 unary_operator = cp_parser_unary_operator (token);
4375 /* The `++' and `--' operators can be handled similarly, even though
4376 they are not technically unary-operators in the grammar. */
4377 if (unary_operator == ERROR_MARK)
4379 if (token->type == CPP_PLUS_PLUS)
4380 unary_operator = PREINCREMENT_EXPR;
4381 else if (token->type == CPP_MINUS_MINUS)
4382 unary_operator = PREDECREMENT_EXPR;
4383 /* Handle the GNU address-of-label extension. */
4384 else if (cp_parser_allow_gnu_extensions_p (parser)
4385 && token->type == CPP_AND_AND)
4389 /* Consume the '&&' token. */
4390 cp_lexer_consume_token (parser->lexer);
4391 /* Look for the identifier. */
4392 identifier = cp_parser_identifier (parser);
4393 /* Create an expression representing the address. */
4394 return finish_label_address_expr (identifier);
4397 if (unary_operator != ERROR_MARK)
4399 tree cast_expression;
4400 tree expression = error_mark_node;
4401 const char *non_constant_p = NULL;
4403 /* Consume the operator token. */
4404 token = cp_lexer_consume_token (parser->lexer);
4405 /* Parse the cast-expression. */
4407 = cp_parser_cast_expression (parser, unary_operator == ADDR_EXPR);
4408 /* Now, build an appropriate representation. */
4409 switch (unary_operator)
4412 non_constant_p = "`*'";
4413 expression = build_x_indirect_ref (cast_expression, "unary *");
4417 /* The "&" operator is allowed in the implementation of
4419 if (!parser->in_offsetof_p)
4420 non_constant_p = "`&'";
4423 expression = build_x_unary_op (unary_operator, cast_expression);
4426 case PREINCREMENT_EXPR:
4427 case PREDECREMENT_EXPR:
4428 non_constant_p = (unary_operator == PREINCREMENT_EXPR
4433 case TRUTH_NOT_EXPR:
4434 expression = finish_unary_op_expr (unary_operator, cast_expression);
4442 && cp_parser_non_integral_constant_expression (parser,
4444 expression = error_mark_node;
4449 return cp_parser_postfix_expression (parser, address_p);
4452 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
4453 unary-operator, the corresponding tree code is returned. */
4455 static enum tree_code
4456 cp_parser_unary_operator (cp_token* token)
4458 switch (token->type)
4461 return INDIRECT_REF;
4467 return CONVERT_EXPR;
4473 return TRUTH_NOT_EXPR;
4476 return BIT_NOT_EXPR;
4483 /* Parse a new-expression.
4486 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
4487 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
4489 Returns a representation of the expression. */
4492 cp_parser_new_expression (cp_parser* parser)
4494 bool global_scope_p;
4499 /* Look for the optional `::' operator. */
4501 = (cp_parser_global_scope_opt (parser,
4502 /*current_scope_valid_p=*/false)
4504 /* Look for the `new' operator. */
4505 cp_parser_require_keyword (parser, RID_NEW, "`new'");
4506 /* There's no easy way to tell a new-placement from the
4507 `( type-id )' construct. */
4508 cp_parser_parse_tentatively (parser);
4509 /* Look for a new-placement. */
4510 placement = cp_parser_new_placement (parser);
4511 /* If that didn't work out, there's no new-placement. */
4512 if (!cp_parser_parse_definitely (parser))
4513 placement = NULL_TREE;
4515 /* If the next token is a `(', then we have a parenthesized
4517 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4519 /* Consume the `('. */
4520 cp_lexer_consume_token (parser->lexer);
4521 /* Parse the type-id. */
4522 type = cp_parser_type_id (parser);
4523 /* Look for the closing `)'. */
4524 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4525 /* There should not be a direct-new-declarator in this production,
4526 but GCC used to allowed this, so we check and emit a sensible error
4527 message for this case. */
4528 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
4530 error ("array bound forbidden after parenthesized type-id");
4531 inform ("try removing the parentheses around the type-id");
4532 cp_parser_direct_new_declarator (parser);
4535 /* Otherwise, there must be a new-type-id. */
4537 type = cp_parser_new_type_id (parser);
4539 /* If the next token is a `(', then we have a new-initializer. */
4540 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4541 initializer = cp_parser_new_initializer (parser);
4543 initializer = NULL_TREE;
4545 /* A new-expression may not appear in an integral constant
4547 if (cp_parser_non_integral_constant_expression (parser, "`new'"))
4548 return error_mark_node;
4550 /* Create a representation of the new-expression. */
4551 return build_new (placement, type, initializer, global_scope_p);
4554 /* Parse a new-placement.
4559 Returns the same representation as for an expression-list. */
4562 cp_parser_new_placement (cp_parser* parser)
4564 tree expression_list;
4566 /* Parse the expression-list. */
4567 expression_list = (cp_parser_parenthesized_expression_list
4568 (parser, false, /*non_constant_p=*/NULL));
4570 return expression_list;
4573 /* Parse a new-type-id.
4576 type-specifier-seq new-declarator [opt]
4578 Returns a TREE_LIST whose TREE_PURPOSE is the type-specifier-seq,
4579 and whose TREE_VALUE is the new-declarator. */
4582 cp_parser_new_type_id (cp_parser* parser)
4584 tree type_specifier_seq;
4586 const char *saved_message;
4588 /* The type-specifier sequence must not contain type definitions.
4589 (It cannot contain declarations of new types either, but if they
4590 are not definitions we will catch that because they are not
4592 saved_message = parser->type_definition_forbidden_message;
4593 parser->type_definition_forbidden_message
4594 = "types may not be defined in a new-type-id";
4595 /* Parse the type-specifier-seq. */
4596 type_specifier_seq = cp_parser_type_specifier_seq (parser);
4597 /* Restore the old message. */
4598 parser->type_definition_forbidden_message = saved_message;
4599 /* Parse the new-declarator. */
4600 declarator = cp_parser_new_declarator_opt (parser);
4602 return build_tree_list (type_specifier_seq, declarator);
4605 /* Parse an (optional) new-declarator.
4608 ptr-operator new-declarator [opt]
4609 direct-new-declarator
4611 Returns a representation of the declarator. See
4612 cp_parser_declarator for the representations used. */
4615 cp_parser_new_declarator_opt (cp_parser* parser)
4617 enum tree_code code;
4619 tree cv_qualifier_seq;
4621 /* We don't know if there's a ptr-operator next, or not. */
4622 cp_parser_parse_tentatively (parser);
4623 /* Look for a ptr-operator. */
4624 code = cp_parser_ptr_operator (parser, &type, &cv_qualifier_seq);
4625 /* If that worked, look for more new-declarators. */
4626 if (cp_parser_parse_definitely (parser))
4630 /* Parse another optional declarator. */
4631 declarator = cp_parser_new_declarator_opt (parser);
4633 /* Create the representation of the declarator. */
4634 if (code == INDIRECT_REF)
4635 declarator = make_pointer_declarator (cv_qualifier_seq,
4638 declarator = make_reference_declarator (cv_qualifier_seq,
4641 /* Handle the pointer-to-member case. */
4643 declarator = build_nt (SCOPE_REF, type, declarator);
4648 /* If the next token is a `[', there is a direct-new-declarator. */
4649 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
4650 return cp_parser_direct_new_declarator (parser);
4655 /* Parse a direct-new-declarator.
4657 direct-new-declarator:
4659 direct-new-declarator [constant-expression]
4661 Returns an ARRAY_REF, following the same conventions as are
4662 documented for cp_parser_direct_declarator. */
4665 cp_parser_direct_new_declarator (cp_parser* parser)
4667 tree declarator = NULL_TREE;
4673 /* Look for the opening `['. */
4674 cp_parser_require (parser, CPP_OPEN_SQUARE, "`['");
4675 /* The first expression is not required to be constant. */
4678 expression = cp_parser_expression (parser);
4679 /* The standard requires that the expression have integral
4680 type. DR 74 adds enumeration types. We believe that the
4681 real intent is that these expressions be handled like the
4682 expression in a `switch' condition, which also allows
4683 classes with a single conversion to integral or
4684 enumeration type. */
4685 if (!processing_template_decl)
4688 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
4693 error ("expression in new-declarator must have integral or enumeration type");
4694 expression = error_mark_node;
4698 /* But all the other expressions must be. */
4701 = cp_parser_constant_expression (parser,
4702 /*allow_non_constant=*/false,
4704 /* Look for the closing `]'. */
4705 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4707 /* Add this bound to the declarator. */
4708 declarator = build_nt (ARRAY_REF, declarator, expression);
4710 /* If the next token is not a `[', then there are no more
4712 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
4719 /* Parse a new-initializer.
4722 ( expression-list [opt] )
4724 Returns a representation of the expression-list. If there is no
4725 expression-list, VOID_ZERO_NODE is returned. */
4728 cp_parser_new_initializer (cp_parser* parser)
4730 tree expression_list;
4732 expression_list = (cp_parser_parenthesized_expression_list
4733 (parser, false, /*non_constant_p=*/NULL));
4734 if (!expression_list)
4735 expression_list = void_zero_node;
4737 return expression_list;
4740 /* Parse a delete-expression.
4743 :: [opt] delete cast-expression
4744 :: [opt] delete [ ] cast-expression
4746 Returns a representation of the expression. */
4749 cp_parser_delete_expression (cp_parser* parser)
4751 bool global_scope_p;
4755 /* Look for the optional `::' operator. */
4757 = (cp_parser_global_scope_opt (parser,
4758 /*current_scope_valid_p=*/false)
4760 /* Look for the `delete' keyword. */
4761 cp_parser_require_keyword (parser, RID_DELETE, "`delete'");
4762 /* See if the array syntax is in use. */
4763 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
4765 /* Consume the `[' token. */
4766 cp_lexer_consume_token (parser->lexer);
4767 /* Look for the `]' token. */
4768 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4769 /* Remember that this is the `[]' construct. */
4775 /* Parse the cast-expression. */
4776 expression = cp_parser_simple_cast_expression (parser);
4778 /* A delete-expression may not appear in an integral constant
4780 if (cp_parser_non_integral_constant_expression (parser, "`delete'"))
4781 return error_mark_node;
4783 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
4786 /* Parse a cast-expression.
4790 ( type-id ) cast-expression
4792 Returns a representation of the expression. */
4795 cp_parser_cast_expression (cp_parser *parser, bool address_p)
4797 /* If it's a `(', then we might be looking at a cast. */
4798 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4800 tree type = NULL_TREE;
4801 tree expr = NULL_TREE;
4802 bool compound_literal_p;
4803 const char *saved_message;
4805 /* There's no way to know yet whether or not this is a cast.
4806 For example, `(int (3))' is a unary-expression, while `(int)
4807 3' is a cast. So, we resort to parsing tentatively. */
4808 cp_parser_parse_tentatively (parser);
4809 /* Types may not be defined in a cast. */
4810 saved_message = parser->type_definition_forbidden_message;
4811 parser->type_definition_forbidden_message
4812 = "types may not be defined in casts";
4813 /* Consume the `('. */
4814 cp_lexer_consume_token (parser->lexer);
4815 /* A very tricky bit is that `(struct S) { 3 }' is a
4816 compound-literal (which we permit in C++ as an extension).
4817 But, that construct is not a cast-expression -- it is a
4818 postfix-expression. (The reason is that `(struct S) { 3 }.i'
4819 is legal; if the compound-literal were a cast-expression,
4820 you'd need an extra set of parentheses.) But, if we parse
4821 the type-id, and it happens to be a class-specifier, then we
4822 will commit to the parse at that point, because we cannot
4823 undo the action that is done when creating a new class. So,
4824 then we cannot back up and do a postfix-expression.
4826 Therefore, we scan ahead to the closing `)', and check to see
4827 if the token after the `)' is a `{'. If so, we are not
4828 looking at a cast-expression.
4830 Save tokens so that we can put them back. */
4831 cp_lexer_save_tokens (parser->lexer);
4832 /* Skip tokens until the next token is a closing parenthesis.
4833 If we find the closing `)', and the next token is a `{', then
4834 we are looking at a compound-literal. */
4836 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
4837 /*consume_paren=*/true)
4838 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
4839 /* Roll back the tokens we skipped. */
4840 cp_lexer_rollback_tokens (parser->lexer);
4841 /* If we were looking at a compound-literal, simulate an error
4842 so that the call to cp_parser_parse_definitely below will
4844 if (compound_literal_p)
4845 cp_parser_simulate_error (parser);
4848 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4849 parser->in_type_id_in_expr_p = true;
4850 /* Look for the type-id. */
4851 type = cp_parser_type_id (parser);
4852 /* Look for the closing `)'. */
4853 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4854 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4857 /* Restore the saved message. */
4858 parser->type_definition_forbidden_message = saved_message;
4860 /* If ok so far, parse the dependent expression. We cannot be
4861 sure it is a cast. Consider `(T ())'. It is a parenthesized
4862 ctor of T, but looks like a cast to function returning T
4863 without a dependent expression. */
4864 if (!cp_parser_error_occurred (parser))
4865 expr = cp_parser_simple_cast_expression (parser);
4867 if (cp_parser_parse_definitely (parser))
4869 /* Warn about old-style casts, if so requested. */
4870 if (warn_old_style_cast
4871 && !in_system_header
4872 && !VOID_TYPE_P (type)
4873 && current_lang_name != lang_name_c)
4874 warning ("use of old-style cast");
4876 /* Only type conversions to integral or enumeration types
4877 can be used in constant-expressions. */
4878 if (parser->integral_constant_expression_p
4879 && !dependent_type_p (type)
4880 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type)
4881 && (cp_parser_non_integral_constant_expression
4883 "a cast to a type other than an integral or "
4884 "enumeration type")))
4885 return error_mark_node;
4887 /* Perform the cast. */
4888 expr = build_c_cast (type, expr);
4893 /* If we get here, then it's not a cast, so it must be a
4894 unary-expression. */
4895 return cp_parser_unary_expression (parser, address_p);
4898 /* Parse a pm-expression.
4902 pm-expression .* cast-expression
4903 pm-expression ->* cast-expression
4905 Returns a representation of the expression. */
4908 cp_parser_pm_expression (cp_parser* parser)
4910 static const cp_parser_token_tree_map map = {
4911 { CPP_DEREF_STAR, MEMBER_REF },
4912 { CPP_DOT_STAR, DOTSTAR_EXPR },
4913 { CPP_EOF, ERROR_MARK }
4916 return cp_parser_binary_expression (parser, map,
4917 cp_parser_simple_cast_expression);
4920 /* Parse a multiplicative-expression.
4922 multiplicative-expression:
4924 multiplicative-expression * pm-expression
4925 multiplicative-expression / pm-expression
4926 multiplicative-expression % pm-expression
4928 Returns a representation of the expression. */
4931 cp_parser_multiplicative_expression (cp_parser* parser)
4933 static const cp_parser_token_tree_map map = {
4934 { CPP_MULT, MULT_EXPR },
4935 { CPP_DIV, TRUNC_DIV_EXPR },
4936 { CPP_MOD, TRUNC_MOD_EXPR },
4937 { CPP_EOF, ERROR_MARK }
4940 return cp_parser_binary_expression (parser,
4942 cp_parser_pm_expression);
4945 /* Parse an additive-expression.
4947 additive-expression:
4948 multiplicative-expression
4949 additive-expression + multiplicative-expression
4950 additive-expression - multiplicative-expression
4952 Returns a representation of the expression. */
4955 cp_parser_additive_expression (cp_parser* parser)
4957 static const cp_parser_token_tree_map map = {
4958 { CPP_PLUS, PLUS_EXPR },
4959 { CPP_MINUS, MINUS_EXPR },
4960 { CPP_EOF, ERROR_MARK }
4963 return cp_parser_binary_expression (parser,
4965 cp_parser_multiplicative_expression);
4968 /* Parse a shift-expression.
4972 shift-expression << additive-expression
4973 shift-expression >> additive-expression
4975 Returns a representation of the expression. */
4978 cp_parser_shift_expression (cp_parser* parser)
4980 static const cp_parser_token_tree_map map = {
4981 { CPP_LSHIFT, LSHIFT_EXPR },
4982 { CPP_RSHIFT, RSHIFT_EXPR },
4983 { CPP_EOF, ERROR_MARK }
4986 return cp_parser_binary_expression (parser,
4988 cp_parser_additive_expression);
4991 /* Parse a relational-expression.
4993 relational-expression:
4995 relational-expression < shift-expression
4996 relational-expression > shift-expression
4997 relational-expression <= shift-expression
4998 relational-expression >= shift-expression
5002 relational-expression:
5003 relational-expression <? shift-expression
5004 relational-expression >? shift-expression
5006 Returns a representation of the expression. */
5009 cp_parser_relational_expression (cp_parser* parser)
5011 static const cp_parser_token_tree_map map = {
5012 { CPP_LESS, LT_EXPR },
5013 { CPP_GREATER, GT_EXPR },
5014 { CPP_LESS_EQ, LE_EXPR },
5015 { CPP_GREATER_EQ, GE_EXPR },
5016 { CPP_MIN, MIN_EXPR },
5017 { CPP_MAX, MAX_EXPR },
5018 { CPP_EOF, ERROR_MARK }
5021 return cp_parser_binary_expression (parser,
5023 cp_parser_shift_expression);
5026 /* Parse an equality-expression.
5028 equality-expression:
5029 relational-expression
5030 equality-expression == relational-expression
5031 equality-expression != relational-expression
5033 Returns a representation of the expression. */
5036 cp_parser_equality_expression (cp_parser* parser)
5038 static const cp_parser_token_tree_map map = {
5039 { CPP_EQ_EQ, EQ_EXPR },
5040 { CPP_NOT_EQ, NE_EXPR },
5041 { CPP_EOF, ERROR_MARK }
5044 return cp_parser_binary_expression (parser,
5046 cp_parser_relational_expression);
5049 /* Parse an and-expression.
5053 and-expression & equality-expression
5055 Returns a representation of the expression. */
5058 cp_parser_and_expression (cp_parser* parser)
5060 static const cp_parser_token_tree_map map = {
5061 { CPP_AND, BIT_AND_EXPR },
5062 { CPP_EOF, ERROR_MARK }
5065 return cp_parser_binary_expression (parser,
5067 cp_parser_equality_expression);
5070 /* Parse an exclusive-or-expression.
5072 exclusive-or-expression:
5074 exclusive-or-expression ^ and-expression
5076 Returns a representation of the expression. */
5079 cp_parser_exclusive_or_expression (cp_parser* parser)
5081 static const cp_parser_token_tree_map map = {
5082 { CPP_XOR, BIT_XOR_EXPR },
5083 { CPP_EOF, ERROR_MARK }
5086 return cp_parser_binary_expression (parser,
5088 cp_parser_and_expression);
5092 /* Parse an inclusive-or-expression.
5094 inclusive-or-expression:
5095 exclusive-or-expression
5096 inclusive-or-expression | exclusive-or-expression
5098 Returns a representation of the expression. */
5101 cp_parser_inclusive_or_expression (cp_parser* parser)
5103 static const cp_parser_token_tree_map map = {
5104 { CPP_OR, BIT_IOR_EXPR },
5105 { CPP_EOF, ERROR_MARK }
5108 return cp_parser_binary_expression (parser,
5110 cp_parser_exclusive_or_expression);
5113 /* Parse a logical-and-expression.
5115 logical-and-expression:
5116 inclusive-or-expression
5117 logical-and-expression && inclusive-or-expression
5119 Returns a representation of the expression. */
5122 cp_parser_logical_and_expression (cp_parser* parser)
5124 static const cp_parser_token_tree_map map = {
5125 { CPP_AND_AND, TRUTH_ANDIF_EXPR },
5126 { CPP_EOF, ERROR_MARK }
5129 return cp_parser_binary_expression (parser,
5131 cp_parser_inclusive_or_expression);
5134 /* Parse a logical-or-expression.
5136 logical-or-expression:
5137 logical-and-expression
5138 logical-or-expression || logical-and-expression
5140 Returns a representation of the expression. */
5143 cp_parser_logical_or_expression (cp_parser* parser)
5145 static const cp_parser_token_tree_map map = {
5146 { CPP_OR_OR, TRUTH_ORIF_EXPR },
5147 { CPP_EOF, ERROR_MARK }
5150 return cp_parser_binary_expression (parser,
5152 cp_parser_logical_and_expression);
5155 /* Parse the `? expression : assignment-expression' part of a
5156 conditional-expression. The LOGICAL_OR_EXPR is the
5157 logical-or-expression that started the conditional-expression.
5158 Returns a representation of the entire conditional-expression.
5160 This routine is used by cp_parser_assignment_expression.
5162 ? expression : assignment-expression
5166 ? : assignment-expression */
5169 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
5172 tree assignment_expr;
5174 /* Consume the `?' token. */
5175 cp_lexer_consume_token (parser->lexer);
5176 if (cp_parser_allow_gnu_extensions_p (parser)
5177 && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
5178 /* Implicit true clause. */
5181 /* Parse the expression. */
5182 expr = cp_parser_expression (parser);
5184 /* The next token should be a `:'. */
5185 cp_parser_require (parser, CPP_COLON, "`:'");
5186 /* Parse the assignment-expression. */
5187 assignment_expr = cp_parser_assignment_expression (parser);
5189 /* Build the conditional-expression. */
5190 return build_x_conditional_expr (logical_or_expr,
5195 /* Parse an assignment-expression.
5197 assignment-expression:
5198 conditional-expression
5199 logical-or-expression assignment-operator assignment_expression
5202 Returns a representation for the expression. */
5205 cp_parser_assignment_expression (cp_parser* parser)
5209 /* If the next token is the `throw' keyword, then we're looking at
5210 a throw-expression. */
5211 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
5212 expr = cp_parser_throw_expression (parser);
5213 /* Otherwise, it must be that we are looking at a
5214 logical-or-expression. */
5217 /* Parse the logical-or-expression. */
5218 expr = cp_parser_logical_or_expression (parser);
5219 /* If the next token is a `?' then we're actually looking at a
5220 conditional-expression. */
5221 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
5222 return cp_parser_question_colon_clause (parser, expr);
5225 enum tree_code assignment_operator;
5227 /* If it's an assignment-operator, we're using the second
5230 = cp_parser_assignment_operator_opt (parser);
5231 if (assignment_operator != ERROR_MARK)
5235 /* Parse the right-hand side of the assignment. */
5236 rhs = cp_parser_assignment_expression (parser);
5237 /* An assignment may not appear in a
5238 constant-expression. */
5239 if (cp_parser_non_integral_constant_expression (parser,
5241 return error_mark_node;
5242 /* Build the assignment expression. */
5243 expr = build_x_modify_expr (expr,
5244 assignment_operator,
5253 /* Parse an (optional) assignment-operator.
5255 assignment-operator: one of
5256 = *= /= %= += -= >>= <<= &= ^= |=
5260 assignment-operator: one of
5263 If the next token is an assignment operator, the corresponding tree
5264 code is returned, and the token is consumed. For example, for
5265 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
5266 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
5267 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
5268 operator, ERROR_MARK is returned. */
5270 static enum tree_code
5271 cp_parser_assignment_operator_opt (cp_parser* parser)
5276 /* Peek at the next toen. */
5277 token = cp_lexer_peek_token (parser->lexer);
5279 switch (token->type)
5290 op = TRUNC_DIV_EXPR;
5294 op = TRUNC_MOD_EXPR;
5334 /* Nothing else is an assignment operator. */
5338 /* If it was an assignment operator, consume it. */
5339 if (op != ERROR_MARK)
5340 cp_lexer_consume_token (parser->lexer);
5345 /* Parse an expression.
5348 assignment-expression
5349 expression , assignment-expression
5351 Returns a representation of the expression. */
5354 cp_parser_expression (cp_parser* parser)
5356 tree expression = NULL_TREE;
5360 tree assignment_expression;
5362 /* Parse the next assignment-expression. */
5363 assignment_expression
5364 = cp_parser_assignment_expression (parser);
5365 /* If this is the first assignment-expression, we can just
5368 expression = assignment_expression;
5370 expression = build_x_compound_expr (expression,
5371 assignment_expression);
5372 /* If the next token is not a comma, then we are done with the
5374 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5376 /* Consume the `,'. */
5377 cp_lexer_consume_token (parser->lexer);
5378 /* A comma operator cannot appear in a constant-expression. */
5379 if (cp_parser_non_integral_constant_expression (parser,
5380 "a comma operator"))
5381 expression = error_mark_node;
5387 /* Parse a constant-expression.
5389 constant-expression:
5390 conditional-expression
5392 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
5393 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
5394 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
5395 is false, NON_CONSTANT_P should be NULL. */
5398 cp_parser_constant_expression (cp_parser* parser,
5399 bool allow_non_constant_p,
5400 bool *non_constant_p)
5402 bool saved_integral_constant_expression_p;
5403 bool saved_allow_non_integral_constant_expression_p;
5404 bool saved_non_integral_constant_expression_p;
5407 /* It might seem that we could simply parse the
5408 conditional-expression, and then check to see if it were
5409 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
5410 one that the compiler can figure out is constant, possibly after
5411 doing some simplifications or optimizations. The standard has a
5412 precise definition of constant-expression, and we must honor
5413 that, even though it is somewhat more restrictive.
5419 is not a legal declaration, because `(2, 3)' is not a
5420 constant-expression. The `,' operator is forbidden in a
5421 constant-expression. However, GCC's constant-folding machinery
5422 will fold this operation to an INTEGER_CST for `3'. */
5424 /* Save the old settings. */
5425 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
5426 saved_allow_non_integral_constant_expression_p
5427 = parser->allow_non_integral_constant_expression_p;
5428 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
5429 /* We are now parsing a constant-expression. */
5430 parser->integral_constant_expression_p = true;
5431 parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
5432 parser->non_integral_constant_expression_p = false;
5433 /* Although the grammar says "conditional-expression", we parse an
5434 "assignment-expression", which also permits "throw-expression"
5435 and the use of assignment operators. In the case that
5436 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
5437 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
5438 actually essential that we look for an assignment-expression.
5439 For example, cp_parser_initializer_clauses uses this function to
5440 determine whether a particular assignment-expression is in fact
5442 expression = cp_parser_assignment_expression (parser);
5443 /* Restore the old settings. */
5444 parser->integral_constant_expression_p = saved_integral_constant_expression_p;
5445 parser->allow_non_integral_constant_expression_p
5446 = saved_allow_non_integral_constant_expression_p;
5447 if (allow_non_constant_p)
5448 *non_constant_p = parser->non_integral_constant_expression_p;
5449 parser->non_integral_constant_expression_p = saved_non_integral_constant_expression_p;
5454 /* Statements [gram.stmt.stmt] */
5456 /* Parse a statement.
5460 expression-statement
5465 declaration-statement
5469 cp_parser_statement (cp_parser* parser, bool in_statement_expr_p)
5473 int statement_line_number;
5475 /* There is no statement yet. */
5476 statement = NULL_TREE;
5477 /* Peek at the next token. */
5478 token = cp_lexer_peek_token (parser->lexer);
5479 /* Remember the line number of the first token in the statement. */
5480 statement_line_number = token->location.line;
5481 /* If this is a keyword, then that will often determine what kind of
5482 statement we have. */
5483 if (token->type == CPP_KEYWORD)
5485 enum rid keyword = token->keyword;
5491 statement = cp_parser_labeled_statement (parser,
5492 in_statement_expr_p);
5497 statement = cp_parser_selection_statement (parser);
5503 statement = cp_parser_iteration_statement (parser);
5510 statement = cp_parser_jump_statement (parser);
5514 statement = cp_parser_try_block (parser);
5518 /* It might be a keyword like `int' that can start a
5519 declaration-statement. */
5523 else if (token->type == CPP_NAME)
5525 /* If the next token is a `:', then we are looking at a
5526 labeled-statement. */
5527 token = cp_lexer_peek_nth_token (parser->lexer, 2);
5528 if (token->type == CPP_COLON)
5529 statement = cp_parser_labeled_statement (parser, in_statement_expr_p);
5531 /* Anything that starts with a `{' must be a compound-statement. */
5532 else if (token->type == CPP_OPEN_BRACE)
5533 statement = cp_parser_compound_statement (parser, false);
5535 /* Everything else must be a declaration-statement or an
5536 expression-statement. Try for the declaration-statement
5537 first, unless we are looking at a `;', in which case we know that
5538 we have an expression-statement. */
5541 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
5543 cp_parser_parse_tentatively (parser);
5544 /* Try to parse the declaration-statement. */
5545 cp_parser_declaration_statement (parser);
5546 /* If that worked, we're done. */
5547 if (cp_parser_parse_definitely (parser))
5550 /* Look for an expression-statement instead. */
5551 statement = cp_parser_expression_statement (parser, in_statement_expr_p);
5554 /* Set the line number for the statement. */
5555 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
5556 STMT_LINENO (statement) = statement_line_number;
5559 /* Parse a labeled-statement.
5562 identifier : statement
5563 case constant-expression : statement
5569 case constant-expression ... constant-expression : statement
5571 Returns the new CASE_LABEL, for a `case' or `default' label. For
5572 an ordinary label, returns a LABEL_STMT. */
5575 cp_parser_labeled_statement (cp_parser* parser, bool in_statement_expr_p)
5578 tree statement = error_mark_node;
5580 /* The next token should be an identifier. */
5581 token = cp_lexer_peek_token (parser->lexer);
5582 if (token->type != CPP_NAME
5583 && token->type != CPP_KEYWORD)
5585 cp_parser_error (parser, "expected labeled-statement");
5586 return error_mark_node;
5589 switch (token->keyword)
5596 /* Consume the `case' token. */
5597 cp_lexer_consume_token (parser->lexer);
5598 /* Parse the constant-expression. */
5599 expr = cp_parser_constant_expression (parser,
5600 /*allow_non_constant_p=*/false,
5603 ellipsis = cp_lexer_peek_token (parser->lexer);
5604 if (ellipsis->type == CPP_ELLIPSIS)
5606 /* Consume the `...' token. */
5607 cp_lexer_consume_token (parser->lexer);
5609 cp_parser_constant_expression (parser,
5610 /*allow_non_constant_p=*/false,
5612 /* We don't need to emit warnings here, as the common code
5613 will do this for us. */
5616 expr_hi = NULL_TREE;
5618 if (!parser->in_switch_statement_p)
5619 error ("case label `%E' not within a switch statement", expr);
5621 statement = finish_case_label (expr, expr_hi);
5626 /* Consume the `default' token. */
5627 cp_lexer_consume_token (parser->lexer);
5628 if (!parser->in_switch_statement_p)
5629 error ("case label not within a switch statement");
5631 statement = finish_case_label (NULL_TREE, NULL_TREE);
5635 /* Anything else must be an ordinary label. */
5636 statement = finish_label_stmt (cp_parser_identifier (parser));
5640 /* Require the `:' token. */
5641 cp_parser_require (parser, CPP_COLON, "`:'");
5642 /* Parse the labeled statement. */
5643 cp_parser_statement (parser, in_statement_expr_p);
5645 /* Return the label, in the case of a `case' or `default' label. */
5649 /* Parse an expression-statement.
5651 expression-statement:
5654 Returns the new EXPR_STMT -- or NULL_TREE if the expression
5655 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
5656 indicates whether this expression-statement is part of an
5657 expression statement. */
5660 cp_parser_expression_statement (cp_parser* parser, bool in_statement_expr_p)
5662 tree statement = NULL_TREE;
5664 /* If the next token is a ';', then there is no expression
5666 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
5667 statement = cp_parser_expression (parser);
5669 /* Consume the final `;'. */
5670 cp_parser_consume_semicolon_at_end_of_statement (parser);
5672 if (in_statement_expr_p
5673 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
5675 /* This is the final expression statement of a statement
5677 statement = finish_stmt_expr_expr (statement);
5680 statement = finish_expr_stmt (statement);
5687 /* Parse a compound-statement.
5690 { statement-seq [opt] }
5692 Returns a COMPOUND_STMT representing the statement. */
5695 cp_parser_compound_statement (cp_parser *parser, bool in_statement_expr_p)
5699 /* Consume the `{'. */
5700 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
5701 return error_mark_node;
5702 /* Begin the compound-statement. */
5703 compound_stmt = begin_compound_stmt (/*has_no_scope=*/false);
5704 /* Parse an (optional) statement-seq. */
5705 cp_parser_statement_seq_opt (parser, in_statement_expr_p);
5706 /* Finish the compound-statement. */
5707 finish_compound_stmt (compound_stmt);
5708 /* Consume the `}'. */
5709 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
5711 return compound_stmt;
5714 /* Parse an (optional) statement-seq.
5718 statement-seq [opt] statement */
5721 cp_parser_statement_seq_opt (cp_parser* parser, bool in_statement_expr_p)
5723 /* Scan statements until there aren't any more. */
5726 /* If we're looking at a `}', then we've run out of statements. */
5727 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE)
5728 || cp_lexer_next_token_is (parser->lexer, CPP_EOF))
5731 /* Parse the statement. */
5732 cp_parser_statement (parser, in_statement_expr_p);
5736 /* Parse a selection-statement.
5738 selection-statement:
5739 if ( condition ) statement
5740 if ( condition ) statement else statement
5741 switch ( condition ) statement
5743 Returns the new IF_STMT or SWITCH_STMT. */
5746 cp_parser_selection_statement (cp_parser* parser)
5751 /* Peek at the next token. */
5752 token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
5754 /* See what kind of keyword it is. */
5755 keyword = token->keyword;
5764 /* Look for the `('. */
5765 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
5767 cp_parser_skip_to_end_of_statement (parser);
5768 return error_mark_node;
5771 /* Begin the selection-statement. */
5772 if (keyword == RID_IF)
5773 statement = begin_if_stmt ();
5775 statement = begin_switch_stmt ();
5777 /* Parse the condition. */
5778 condition = cp_parser_condition (parser);
5779 /* Look for the `)'. */
5780 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
5781 cp_parser_skip_to_closing_parenthesis (parser, true, false,
5782 /*consume_paren=*/true);
5784 if (keyword == RID_IF)
5788 /* Add the condition. */
5789 finish_if_stmt_cond (condition, statement);
5791 /* Parse the then-clause. */
5792 then_stmt = cp_parser_implicitly_scoped_statement (parser);
5793 finish_then_clause (statement);
5795 /* If the next token is `else', parse the else-clause. */
5796 if (cp_lexer_next_token_is_keyword (parser->lexer,
5801 /* Consume the `else' keyword. */
5802 cp_lexer_consume_token (parser->lexer);
5803 /* Parse the else-clause. */
5805 = cp_parser_implicitly_scoped_statement (parser);
5806 finish_else_clause (statement);
5809 /* Now we're all done with the if-statement. */
5815 bool in_switch_statement_p;
5817 /* Add the condition. */
5818 finish_switch_cond (condition, statement);
5820 /* Parse the body of the switch-statement. */
5821 in_switch_statement_p = parser->in_switch_statement_p;
5822 parser->in_switch_statement_p = true;
5823 body = cp_parser_implicitly_scoped_statement (parser);
5824 parser->in_switch_statement_p = in_switch_statement_p;
5826 /* Now we're all done with the switch-statement. */
5827 finish_switch_stmt (statement);
5835 cp_parser_error (parser, "expected selection-statement");
5836 return error_mark_node;
5840 /* Parse a condition.
5844 type-specifier-seq declarator = assignment-expression
5849 type-specifier-seq declarator asm-specification [opt]
5850 attributes [opt] = assignment-expression
5852 Returns the expression that should be tested. */
5855 cp_parser_condition (cp_parser* parser)
5857 tree type_specifiers;
5858 const char *saved_message;
5860 /* Try the declaration first. */
5861 cp_parser_parse_tentatively (parser);
5862 /* New types are not allowed in the type-specifier-seq for a
5864 saved_message = parser->type_definition_forbidden_message;
5865 parser->type_definition_forbidden_message
5866 = "types may not be defined in conditions";
5867 /* Parse the type-specifier-seq. */
5868 type_specifiers = cp_parser_type_specifier_seq (parser);
5869 /* Restore the saved message. */
5870 parser->type_definition_forbidden_message = saved_message;
5871 /* If all is well, we might be looking at a declaration. */
5872 if (!cp_parser_error_occurred (parser))
5875 tree asm_specification;
5878 tree initializer = NULL_TREE;
5880 /* Parse the declarator. */
5881 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
5882 /*ctor_dtor_or_conv_p=*/NULL,
5883 /*parenthesized_p=*/NULL);
5884 /* Parse the attributes. */
5885 attributes = cp_parser_attributes_opt (parser);
5886 /* Parse the asm-specification. */
5887 asm_specification = cp_parser_asm_specification_opt (parser);
5888 /* If the next token is not an `=', then we might still be
5889 looking at an expression. For example:
5893 looks like a decl-specifier-seq and a declarator -- but then
5894 there is no `=', so this is an expression. */
5895 cp_parser_require (parser, CPP_EQ, "`='");
5896 /* If we did see an `=', then we are looking at a declaration
5898 if (cp_parser_parse_definitely (parser))
5900 /* Create the declaration. */
5901 decl = start_decl (declarator, type_specifiers,
5902 /*initialized_p=*/true,
5903 attributes, /*prefix_attributes=*/NULL_TREE);
5904 /* Parse the assignment-expression. */
5905 initializer = cp_parser_assignment_expression (parser);
5907 /* Process the initializer. */
5908 cp_finish_decl (decl,
5911 LOOKUP_ONLYCONVERTING);
5913 return convert_from_reference (decl);
5916 /* If we didn't even get past the declarator successfully, we are
5917 definitely not looking at a declaration. */
5919 cp_parser_abort_tentative_parse (parser);
5921 /* Otherwise, we are looking at an expression. */
5922 return cp_parser_expression (parser);
5925 /* Parse an iteration-statement.
5927 iteration-statement:
5928 while ( condition ) statement
5929 do statement while ( expression ) ;
5930 for ( for-init-statement condition [opt] ; expression [opt] )
5933 Returns the new WHILE_STMT, DO_STMT, or FOR_STMT. */
5936 cp_parser_iteration_statement (cp_parser* parser)
5941 bool in_iteration_statement_p;
5944 /* Peek at the next token. */
5945 token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
5947 return error_mark_node;
5949 /* Remember whether or not we are already within an iteration
5951 in_iteration_statement_p = parser->in_iteration_statement_p;
5953 /* See what kind of keyword it is. */
5954 keyword = token->keyword;
5961 /* Begin the while-statement. */
5962 statement = begin_while_stmt ();
5963 /* Look for the `('. */
5964 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
5965 /* Parse the condition. */
5966 condition = cp_parser_condition (parser);
5967 finish_while_stmt_cond (condition, statement);
5968 /* Look for the `)'. */
5969 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5970 /* Parse the dependent statement. */
5971 parser->in_iteration_statement_p = true;
5972 cp_parser_already_scoped_statement (parser);
5973 parser->in_iteration_statement_p = in_iteration_statement_p;
5974 /* We're done with the while-statement. */
5975 finish_while_stmt (statement);
5983 /* Begin the do-statement. */
5984 statement = begin_do_stmt ();
5985 /* Parse the body of the do-statement. */
5986 parser->in_iteration_statement_p = true;
5987 cp_parser_implicitly_scoped_statement (parser);
5988 parser->in_iteration_statement_p = in_iteration_statement_p;
5989 finish_do_body (statement);
5990 /* Look for the `while' keyword. */
5991 cp_parser_require_keyword (parser, RID_WHILE, "`while'");
5992 /* Look for the `('. */
5993 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
5994 /* Parse the expression. */
5995 expression = cp_parser_expression (parser);
5996 /* We're done with the do-statement. */
5997 finish_do_stmt (expression, statement);
5998 /* Look for the `)'. */
5999 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6000 /* Look for the `;'. */
6001 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6007 tree condition = NULL_TREE;
6008 tree expression = NULL_TREE;
6010 /* Begin the for-statement. */
6011 statement = begin_for_stmt ();
6012 /* Look for the `('. */
6013 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6014 /* Parse the initialization. */
6015 cp_parser_for_init_statement (parser);
6016 finish_for_init_stmt (statement);
6018 /* If there's a condition, process it. */
6019 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6020 condition = cp_parser_condition (parser);
6021 finish_for_cond (condition, statement);
6022 /* Look for the `;'. */
6023 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6025 /* If there's an expression, process it. */
6026 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
6027 expression = cp_parser_expression (parser);
6028 finish_for_expr (expression, statement);
6029 /* Look for the `)'. */
6030 cp_parser_require (parser, CPP_CLOSE_PAREN, "`;'");
6032 /* Parse the body of the for-statement. */
6033 parser->in_iteration_statement_p = true;
6034 cp_parser_already_scoped_statement (parser);
6035 parser->in_iteration_statement_p = in_iteration_statement_p;
6037 /* We're done with the for-statement. */
6038 finish_for_stmt (statement);
6043 cp_parser_error (parser, "expected iteration-statement");
6044 statement = error_mark_node;
6051 /* Parse a for-init-statement.
6054 expression-statement
6055 simple-declaration */
6058 cp_parser_for_init_statement (cp_parser* parser)
6060 /* If the next token is a `;', then we have an empty
6061 expression-statement. Grammatically, this is also a
6062 simple-declaration, but an invalid one, because it does not
6063 declare anything. Therefore, if we did not handle this case
6064 specially, we would issue an error message about an invalid
6066 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6068 /* We're going to speculatively look for a declaration, falling back
6069 to an expression, if necessary. */
6070 cp_parser_parse_tentatively (parser);
6071 /* Parse the declaration. */
6072 cp_parser_simple_declaration (parser,
6073 /*function_definition_allowed_p=*/false);
6074 /* If the tentative parse failed, then we shall need to look for an
6075 expression-statement. */
6076 if (cp_parser_parse_definitely (parser))
6080 cp_parser_expression_statement (parser, false);
6083 /* Parse a jump-statement.
6088 return expression [opt] ;
6096 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_STMT, or
6100 cp_parser_jump_statement (cp_parser* parser)
6102 tree statement = error_mark_node;
6106 /* Peek at the next token. */
6107 token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
6109 return error_mark_node;
6111 /* See what kind of keyword it is. */
6112 keyword = token->keyword;
6116 if (!parser->in_switch_statement_p
6117 && !parser->in_iteration_statement_p)
6119 error ("break statement not within loop or switch");
6120 statement = error_mark_node;
6123 statement = finish_break_stmt ();
6124 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6128 if (!parser->in_iteration_statement_p)
6130 error ("continue statement not within a loop");
6131 statement = error_mark_node;
6134 statement = finish_continue_stmt ();
6135 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6142 /* If the next token is a `;', then there is no
6144 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6145 expr = cp_parser_expression (parser);
6148 /* Build the return-statement. */
6149 statement = finish_return_stmt (expr);
6150 /* Look for the final `;'. */
6151 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6156 /* Create the goto-statement. */
6157 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
6159 /* Issue a warning about this use of a GNU extension. */
6161 pedwarn ("ISO C++ forbids computed gotos");
6162 /* Consume the '*' token. */
6163 cp_lexer_consume_token (parser->lexer);
6164 /* Parse the dependent expression. */
6165 finish_goto_stmt (cp_parser_expression (parser));
6168 finish_goto_stmt (cp_parser_identifier (parser));
6169 /* Look for the final `;'. */
6170 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6174 cp_parser_error (parser, "expected jump-statement");
6181 /* Parse a declaration-statement.
6183 declaration-statement:
6184 block-declaration */
6187 cp_parser_declaration_statement (cp_parser* parser)
6189 /* Parse the block-declaration. */
6190 cp_parser_block_declaration (parser, /*statement_p=*/true);
6192 /* Finish off the statement. */
6196 /* Some dependent statements (like `if (cond) statement'), are
6197 implicitly in their own scope. In other words, if the statement is
6198 a single statement (as opposed to a compound-statement), it is
6199 none-the-less treated as if it were enclosed in braces. Any
6200 declarations appearing in the dependent statement are out of scope
6201 after control passes that point. This function parses a statement,
6202 but ensures that is in its own scope, even if it is not a
6205 Returns the new statement. */
6208 cp_parser_implicitly_scoped_statement (cp_parser* parser)
6212 /* If the token is not a `{', then we must take special action. */
6213 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
6215 /* Create a compound-statement. */
6216 statement = begin_compound_stmt (/*has_no_scope=*/false);
6217 /* Parse the dependent-statement. */
6218 cp_parser_statement (parser, false);
6219 /* Finish the dummy compound-statement. */
6220 finish_compound_stmt (statement);
6222 /* Otherwise, we simply parse the statement directly. */
6224 statement = cp_parser_compound_statement (parser, false);
6226 /* Return the statement. */
6230 /* For some dependent statements (like `while (cond) statement'), we
6231 have already created a scope. Therefore, even if the dependent
6232 statement is a compound-statement, we do not want to create another
6236 cp_parser_already_scoped_statement (cp_parser* parser)
6238 /* If the token is not a `{', then we must take special action. */
6239 if (cp_lexer_next_token_is_not(parser->lexer, CPP_OPEN_BRACE))
6243 /* Create a compound-statement. */
6244 statement = begin_compound_stmt (/*has_no_scope=*/true);
6245 /* Parse the dependent-statement. */
6246 cp_parser_statement (parser, false);
6247 /* Finish the dummy compound-statement. */
6248 finish_compound_stmt (statement);
6250 /* Otherwise, we simply parse the statement directly. */
6252 cp_parser_statement (parser, false);
6255 /* Declarations [gram.dcl.dcl] */
6257 /* Parse an optional declaration-sequence.
6261 declaration-seq declaration */
6264 cp_parser_declaration_seq_opt (cp_parser* parser)
6270 token = cp_lexer_peek_token (parser->lexer);
6272 if (token->type == CPP_CLOSE_BRACE
6273 || token->type == CPP_EOF)
6276 if (token->type == CPP_SEMICOLON)
6278 /* A declaration consisting of a single semicolon is
6279 invalid. Allow it unless we're being pedantic. */
6280 if (pedantic && !in_system_header)
6281 pedwarn ("extra `;'");
6282 cp_lexer_consume_token (parser->lexer);
6286 /* The C lexer modifies PENDING_LANG_CHANGE when it wants the
6287 parser to enter or exit implicit `extern "C"' blocks. */
6288 while (pending_lang_change > 0)
6290 push_lang_context (lang_name_c);
6291 --pending_lang_change;
6293 while (pending_lang_change < 0)
6295 pop_lang_context ();
6296 ++pending_lang_change;
6299 /* Parse the declaration itself. */
6300 cp_parser_declaration (parser);
6304 /* Parse a declaration.
6309 template-declaration
6310 explicit-instantiation
6311 explicit-specialization
6312 linkage-specification
6313 namespace-definition
6318 __extension__ declaration */
6321 cp_parser_declaration (cp_parser* parser)
6327 /* Set this here since we can be called after
6328 pushing the linkage specification. */
6329 c_lex_string_translate = true;
6331 /* Check for the `__extension__' keyword. */
6332 if (cp_parser_extension_opt (parser, &saved_pedantic))
6334 /* Parse the qualified declaration. */
6335 cp_parser_declaration (parser);
6336 /* Restore the PEDANTIC flag. */
6337 pedantic = saved_pedantic;
6342 /* Try to figure out what kind of declaration is present. */
6343 token1 = *cp_lexer_peek_token (parser->lexer);
6345 /* Don't translate the CPP_STRING in extern "C". */
6346 if (token1.keyword == RID_EXTERN)
6347 c_lex_string_translate = false;
6349 if (token1.type != CPP_EOF)
6350 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
6352 /* If the next token is `extern' and the following token is a string
6353 literal, then we have a linkage specification. */
6354 if (token1.keyword == RID_EXTERN
6355 && cp_parser_is_string_literal (&token2))
6356 cp_parser_linkage_specification (parser);
6357 /* If the next token is `template', then we have either a template
6358 declaration, an explicit instantiation, or an explicit
6360 else if (token1.keyword == RID_TEMPLATE)
6362 /* `template <>' indicates a template specialization. */
6363 if (token2.type == CPP_LESS
6364 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
6365 cp_parser_explicit_specialization (parser);
6366 /* `template <' indicates a template declaration. */
6367 else if (token2.type == CPP_LESS)
6368 cp_parser_template_declaration (parser, /*member_p=*/false);
6369 /* Anything else must be an explicit instantiation. */
6371 cp_parser_explicit_instantiation (parser);
6373 /* If the next token is `export', then we have a template
6375 else if (token1.keyword == RID_EXPORT)
6376 cp_parser_template_declaration (parser, /*member_p=*/false);
6377 /* If the next token is `extern', 'static' or 'inline' and the one
6378 after that is `template', we have a GNU extended explicit
6379 instantiation directive. */
6380 else if (cp_parser_allow_gnu_extensions_p (parser)
6381 && (token1.keyword == RID_EXTERN
6382 || token1.keyword == RID_STATIC
6383 || token1.keyword == RID_INLINE)
6384 && token2.keyword == RID_TEMPLATE)
6385 cp_parser_explicit_instantiation (parser);
6386 /* If the next token is `namespace', check for a named or unnamed
6387 namespace definition. */
6388 else if (token1.keyword == RID_NAMESPACE
6389 && (/* A named namespace definition. */
6390 (token2.type == CPP_NAME
6391 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
6393 /* An unnamed namespace definition. */
6394 || token2.type == CPP_OPEN_BRACE))
6395 cp_parser_namespace_definition (parser);
6396 /* We must have either a block declaration or a function
6399 /* Try to parse a block-declaration, or a function-definition. */
6400 cp_parser_block_declaration (parser, /*statement_p=*/false);
6402 c_lex_string_translate = true;
6405 /* Parse a block-declaration.
6410 namespace-alias-definition
6417 __extension__ block-declaration
6420 If STATEMENT_P is TRUE, then this block-declaration is occurring as
6421 part of a declaration-statement. */
6424 cp_parser_block_declaration (cp_parser *parser,
6430 /* Check for the `__extension__' keyword. */
6431 if (cp_parser_extension_opt (parser, &saved_pedantic))
6433 /* Parse the qualified declaration. */
6434 cp_parser_block_declaration (parser, statement_p);
6435 /* Restore the PEDANTIC flag. */
6436 pedantic = saved_pedantic;
6441 /* Peek at the next token to figure out which kind of declaration is
6443 token1 = cp_lexer_peek_token (parser->lexer);
6445 /* If the next keyword is `asm', we have an asm-definition. */
6446 if (token1->keyword == RID_ASM)
6449 cp_parser_commit_to_tentative_parse (parser);
6450 cp_parser_asm_definition (parser);
6452 /* If the next keyword is `namespace', we have a
6453 namespace-alias-definition. */
6454 else if (token1->keyword == RID_NAMESPACE)
6455 cp_parser_namespace_alias_definition (parser);
6456 /* If the next keyword is `using', we have either a
6457 using-declaration or a using-directive. */
6458 else if (token1->keyword == RID_USING)
6463 cp_parser_commit_to_tentative_parse (parser);
6464 /* If the token after `using' is `namespace', then we have a
6466 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
6467 if (token2->keyword == RID_NAMESPACE)
6468 cp_parser_using_directive (parser);
6469 /* Otherwise, it's a using-declaration. */
6471 cp_parser_using_declaration (parser);
6473 /* If the next keyword is `__label__' we have a label declaration. */
6474 else if (token1->keyword == RID_LABEL)
6477 cp_parser_commit_to_tentative_parse (parser);
6478 cp_parser_label_declaration (parser);
6480 /* Anything else must be a simple-declaration. */
6482 cp_parser_simple_declaration (parser, !statement_p);
6485 /* Parse a simple-declaration.
6488 decl-specifier-seq [opt] init-declarator-list [opt] ;
6490 init-declarator-list:
6492 init-declarator-list , init-declarator
6494 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
6495 function-definition as a simple-declaration. */
6498 cp_parser_simple_declaration (cp_parser* parser,
6499 bool function_definition_allowed_p)
6501 tree decl_specifiers;
6503 int declares_class_or_enum;
6504 bool saw_declarator;
6506 /* Defer access checks until we know what is being declared; the
6507 checks for names appearing in the decl-specifier-seq should be
6508 done as if we were in the scope of the thing being declared. */
6509 push_deferring_access_checks (dk_deferred);
6511 /* Parse the decl-specifier-seq. We have to keep track of whether
6512 or not the decl-specifier-seq declares a named class or
6513 enumeration type, since that is the only case in which the
6514 init-declarator-list is allowed to be empty.
6518 In a simple-declaration, the optional init-declarator-list can be
6519 omitted only when declaring a class or enumeration, that is when
6520 the decl-specifier-seq contains either a class-specifier, an
6521 elaborated-type-specifier, or an enum-specifier. */
6523 = cp_parser_decl_specifier_seq (parser,
6524 CP_PARSER_FLAGS_OPTIONAL,
6526 &declares_class_or_enum);
6527 /* We no longer need to defer access checks. */
6528 stop_deferring_access_checks ();
6530 /* In a block scope, a valid declaration must always have a
6531 decl-specifier-seq. By not trying to parse declarators, we can
6532 resolve the declaration/expression ambiguity more quickly. */
6533 if (!function_definition_allowed_p && !decl_specifiers)
6535 cp_parser_error (parser, "expected declaration");
6539 /* If the next two tokens are both identifiers, the code is
6540 erroneous. The usual cause of this situation is code like:
6544 where "T" should name a type -- but does not. */
6545 if (cp_parser_parse_and_diagnose_invalid_type_name (parser))
6547 /* If parsing tentatively, we should commit; we really are
6548 looking at a declaration. */
6549 cp_parser_commit_to_tentative_parse (parser);
6554 /* Keep going until we hit the `;' at the end of the simple
6556 saw_declarator = false;
6557 while (cp_lexer_next_token_is_not (parser->lexer,
6561 bool function_definition_p;
6564 saw_declarator = true;
6565 /* Parse the init-declarator. */
6566 decl = cp_parser_init_declarator (parser, decl_specifiers, attributes,
6567 function_definition_allowed_p,
6569 declares_class_or_enum,
6570 &function_definition_p);
6571 /* If an error occurred while parsing tentatively, exit quickly.
6572 (That usually happens when in the body of a function; each
6573 statement is treated as a declaration-statement until proven
6575 if (cp_parser_error_occurred (parser))
6577 /* Handle function definitions specially. */
6578 if (function_definition_p)
6580 /* If the next token is a `,', then we are probably
6581 processing something like:
6585 which is erroneous. */
6586 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
6587 error ("mixing declarations and function-definitions is forbidden");
6588 /* Otherwise, we're done with the list of declarators. */
6591 pop_deferring_access_checks ();
6595 /* The next token should be either a `,' or a `;'. */
6596 token = cp_lexer_peek_token (parser->lexer);
6597 /* If it's a `,', there are more declarators to come. */
6598 if (token->type == CPP_COMMA)
6599 cp_lexer_consume_token (parser->lexer);
6600 /* If it's a `;', we are done. */
6601 else if (token->type == CPP_SEMICOLON)
6603 /* Anything else is an error. */
6606 cp_parser_error (parser, "expected `,' or `;'");
6607 /* Skip tokens until we reach the end of the statement. */
6608 cp_parser_skip_to_end_of_statement (parser);
6609 /* If the next token is now a `;', consume it. */
6610 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
6611 cp_lexer_consume_token (parser->lexer);
6614 /* After the first time around, a function-definition is not
6615 allowed -- even if it was OK at first. For example:
6620 function_definition_allowed_p = false;
6623 /* Issue an error message if no declarators are present, and the
6624 decl-specifier-seq does not itself declare a class or
6626 if (!saw_declarator)
6628 if (cp_parser_declares_only_class_p (parser))
6629 shadow_tag (decl_specifiers);
6630 /* Perform any deferred access checks. */
6631 perform_deferred_access_checks ();
6634 /* Consume the `;'. */
6635 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6638 pop_deferring_access_checks ();
6641 /* Parse a decl-specifier-seq.
6644 decl-specifier-seq [opt] decl-specifier
6647 storage-class-specifier
6656 decl-specifier-seq [opt] attributes
6658 Returns a TREE_LIST, giving the decl-specifiers in the order they
6659 appear in the source code. The TREE_VALUE of each node is the
6660 decl-specifier. For a keyword (such as `auto' or `friend'), the
6661 TREE_VALUE is simply the corresponding TREE_IDENTIFIER. For the
6662 representation of a type-specifier, see cp_parser_type_specifier.
6664 If there are attributes, they will be stored in *ATTRIBUTES,
6665 represented as described above cp_parser_attributes.
6667 If FRIEND_IS_NOT_CLASS_P is non-NULL, and the `friend' specifier
6668 appears, and the entity that will be a friend is not going to be a
6669 class, then *FRIEND_IS_NOT_CLASS_P will be set to TRUE. Note that
6670 even if *FRIEND_IS_NOT_CLASS_P is FALSE, the entity to which
6671 friendship is granted might not be a class.
6673 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
6676 1: one of the decl-specifiers is an elaborated-type-specifier
6677 (i.e., a type declaration)
6678 2: one of the decl-specifiers is an enum-specifier or a
6679 class-specifier (i.e., a type definition)
6684 cp_parser_decl_specifier_seq (cp_parser* parser,
6685 cp_parser_flags flags,
6687 int* declares_class_or_enum)
6689 tree decl_specs = NULL_TREE;
6690 bool friend_p = false;
6691 bool constructor_possible_p = !parser->in_declarator_p;
6693 /* Assume no class or enumeration type is declared. */
6694 *declares_class_or_enum = 0;
6696 /* Assume there are no attributes. */
6697 *attributes = NULL_TREE;
6699 /* Keep reading specifiers until there are no more to read. */
6702 tree decl_spec = NULL_TREE;
6706 /* Peek at the next token. */
6707 token = cp_lexer_peek_token (parser->lexer);
6708 /* Handle attributes. */
6709 if (token->keyword == RID_ATTRIBUTE)
6711 /* Parse the attributes. */
6712 decl_spec = cp_parser_attributes_opt (parser);
6713 /* Add them to the list. */
6714 *attributes = chainon (*attributes, decl_spec);
6717 /* If the next token is an appropriate keyword, we can simply
6718 add it to the list. */
6719 switch (token->keyword)
6725 error ("duplicate `friend'");
6728 /* The representation of the specifier is simply the
6729 appropriate TREE_IDENTIFIER node. */
6730 decl_spec = token->value;
6731 /* Consume the token. */
6732 cp_lexer_consume_token (parser->lexer);
6735 /* function-specifier:
6742 decl_spec = cp_parser_function_specifier_opt (parser);
6748 /* The representation of the specifier is simply the
6749 appropriate TREE_IDENTIFIER node. */
6750 decl_spec = token->value;
6751 /* Consume the token. */
6752 cp_lexer_consume_token (parser->lexer);
6753 /* A constructor declarator cannot appear in a typedef. */
6754 constructor_possible_p = false;
6755 /* The "typedef" keyword can only occur in a declaration; we
6756 may as well commit at this point. */
6757 cp_parser_commit_to_tentative_parse (parser);
6760 /* storage-class-specifier:
6775 decl_spec = cp_parser_storage_class_specifier_opt (parser);
6782 /* Constructors are a special case. The `S' in `S()' is not a
6783 decl-specifier; it is the beginning of the declarator. */
6784 constructor_p = (!decl_spec
6785 && constructor_possible_p
6786 && cp_parser_constructor_declarator_p (parser,
6789 /* If we don't have a DECL_SPEC yet, then we must be looking at
6790 a type-specifier. */
6791 if (!decl_spec && !constructor_p)
6793 int decl_spec_declares_class_or_enum;
6794 bool is_cv_qualifier;
6797 = cp_parser_type_specifier (parser, flags,
6799 /*is_declaration=*/true,
6800 &decl_spec_declares_class_or_enum,
6803 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
6805 /* If this type-specifier referenced a user-defined type
6806 (a typedef, class-name, etc.), then we can't allow any
6807 more such type-specifiers henceforth.
6811 The longest sequence of decl-specifiers that could
6812 possibly be a type name is taken as the
6813 decl-specifier-seq of a declaration. The sequence shall
6814 be self-consistent as described below.
6818 As a general rule, at most one type-specifier is allowed
6819 in the complete decl-specifier-seq of a declaration. The
6820 only exceptions are the following:
6822 -- const or volatile can be combined with any other
6825 -- signed or unsigned can be combined with char, long,
6833 void g (const int Pc);
6835 Here, Pc is *not* part of the decl-specifier seq; it's
6836 the declarator. Therefore, once we see a type-specifier
6837 (other than a cv-qualifier), we forbid any additional
6838 user-defined types. We *do* still allow things like `int
6839 int' to be considered a decl-specifier-seq, and issue the
6840 error message later. */
6841 if (decl_spec && !is_cv_qualifier)
6842 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
6843 /* A constructor declarator cannot follow a type-specifier. */
6845 constructor_possible_p = false;
6848 /* If we still do not have a DECL_SPEC, then there are no more
6852 /* Issue an error message, unless the entire construct was
6854 if (!(flags & CP_PARSER_FLAGS_OPTIONAL))
6856 cp_parser_error (parser, "expected decl specifier");
6857 return error_mark_node;
6863 /* Add the DECL_SPEC to the list of specifiers. */
6864 if (decl_specs == NULL || TREE_VALUE (decl_specs) != error_mark_node)
6865 decl_specs = tree_cons (NULL_TREE, decl_spec, decl_specs);
6867 /* After we see one decl-specifier, further decl-specifiers are
6869 flags |= CP_PARSER_FLAGS_OPTIONAL;
6872 /* Don't allow a friend specifier with a class definition. */
6873 if (friend_p && (*declares_class_or_enum & 2))
6874 error ("class definition may not be declared a friend");
6876 /* We have built up the DECL_SPECS in reverse order. Return them in
6877 the correct order. */
6878 return nreverse (decl_specs);
6881 /* Parse an (optional) storage-class-specifier.
6883 storage-class-specifier:
6892 storage-class-specifier:
6895 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
6898 cp_parser_storage_class_specifier_opt (cp_parser* parser)
6900 switch (cp_lexer_peek_token (parser->lexer)->keyword)
6908 /* Consume the token. */
6909 return cp_lexer_consume_token (parser->lexer)->value;
6916 /* Parse an (optional) function-specifier.
6923 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
6926 cp_parser_function_specifier_opt (cp_parser* parser)
6928 switch (cp_lexer_peek_token (parser->lexer)->keyword)
6933 /* Consume the token. */
6934 return cp_lexer_consume_token (parser->lexer)->value;
6941 /* Parse a linkage-specification.
6943 linkage-specification:
6944 extern string-literal { declaration-seq [opt] }
6945 extern string-literal declaration */
6948 cp_parser_linkage_specification (cp_parser* parser)
6953 /* Look for the `extern' keyword. */
6954 cp_parser_require_keyword (parser, RID_EXTERN, "`extern'");
6956 /* Peek at the next token. */
6957 token = cp_lexer_peek_token (parser->lexer);
6958 /* If it's not a string-literal, then there's a problem. */
6959 if (!cp_parser_is_string_literal (token))
6961 cp_parser_error (parser, "expected language-name");
6964 /* Consume the token. */
6965 cp_lexer_consume_token (parser->lexer);
6967 /* Transform the literal into an identifier. If the literal is a
6968 wide-character string, or contains embedded NULs, then we can't
6969 handle it as the user wants. */
6970 if (token->type == CPP_WSTRING
6971 || (strlen (TREE_STRING_POINTER (token->value))
6972 != (size_t) (TREE_STRING_LENGTH (token->value) - 1)))
6974 cp_parser_error (parser, "invalid linkage-specification");
6975 /* Assume C++ linkage. */
6976 linkage = get_identifier ("c++");
6978 /* If it's a simple string constant, things are easier. */
6980 linkage = get_identifier (TREE_STRING_POINTER (token->value));
6982 /* We're now using the new linkage. */
6983 push_lang_context (linkage);
6985 /* If the next token is a `{', then we're using the first
6987 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6989 /* Consume the `{' token. */
6990 cp_lexer_consume_token (parser->lexer);
6991 /* Parse the declarations. */
6992 cp_parser_declaration_seq_opt (parser);
6993 /* Look for the closing `}'. */
6994 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
6996 /* Otherwise, there's just one declaration. */
6999 bool saved_in_unbraced_linkage_specification_p;
7001 saved_in_unbraced_linkage_specification_p
7002 = parser->in_unbraced_linkage_specification_p;
7003 parser->in_unbraced_linkage_specification_p = true;
7004 have_extern_spec = true;
7005 cp_parser_declaration (parser);
7006 have_extern_spec = false;
7007 parser->in_unbraced_linkage_specification_p
7008 = saved_in_unbraced_linkage_specification_p;
7011 /* We're done with the linkage-specification. */
7012 pop_lang_context ();
7015 /* Special member functions [gram.special] */
7017 /* Parse a conversion-function-id.
7019 conversion-function-id:
7020 operator conversion-type-id
7022 Returns an IDENTIFIER_NODE representing the operator. */
7025 cp_parser_conversion_function_id (cp_parser* parser)
7029 tree saved_qualifying_scope;
7030 tree saved_object_scope;
7033 /* Look for the `operator' token. */
7034 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
7035 return error_mark_node;
7036 /* When we parse the conversion-type-id, the current scope will be
7037 reset. However, we need that information in able to look up the
7038 conversion function later, so we save it here. */
7039 saved_scope = parser->scope;
7040 saved_qualifying_scope = parser->qualifying_scope;
7041 saved_object_scope = parser->object_scope;
7042 /* We must enter the scope of the class so that the names of
7043 entities declared within the class are available in the
7044 conversion-type-id. For example, consider:
7051 S::operator I() { ... }
7053 In order to see that `I' is a type-name in the definition, we
7054 must be in the scope of `S'. */
7056 pop_p = push_scope (saved_scope);
7057 /* Parse the conversion-type-id. */
7058 type = cp_parser_conversion_type_id (parser);
7059 /* Leave the scope of the class, if any. */
7061 pop_scope (saved_scope);
7062 /* Restore the saved scope. */
7063 parser->scope = saved_scope;
7064 parser->qualifying_scope = saved_qualifying_scope;
7065 parser->object_scope = saved_object_scope;
7066 /* If the TYPE is invalid, indicate failure. */
7067 if (type == error_mark_node)
7068 return error_mark_node;
7069 return mangle_conv_op_name_for_type (type);
7072 /* Parse a conversion-type-id:
7075 type-specifier-seq conversion-declarator [opt]
7077 Returns the TYPE specified. */
7080 cp_parser_conversion_type_id (cp_parser* parser)
7083 tree type_specifiers;
7086 /* Parse the attributes. */
7087 attributes = cp_parser_attributes_opt (parser);
7088 /* Parse the type-specifiers. */
7089 type_specifiers = cp_parser_type_specifier_seq (parser);
7090 /* If that didn't work, stop. */
7091 if (type_specifiers == error_mark_node)
7092 return error_mark_node;
7093 /* Parse the conversion-declarator. */
7094 declarator = cp_parser_conversion_declarator_opt (parser);
7096 return grokdeclarator (declarator, type_specifiers, TYPENAME,
7097 /*initialized=*/0, &attributes);
7100 /* Parse an (optional) conversion-declarator.
7102 conversion-declarator:
7103 ptr-operator conversion-declarator [opt]
7105 Returns a representation of the declarator. See
7106 cp_parser_declarator for details. */
7109 cp_parser_conversion_declarator_opt (cp_parser* parser)
7111 enum tree_code code;
7113 tree cv_qualifier_seq;
7115 /* We don't know if there's a ptr-operator next, or not. */
7116 cp_parser_parse_tentatively (parser);
7117 /* Try the ptr-operator. */
7118 code = cp_parser_ptr_operator (parser, &class_type,
7120 /* If it worked, look for more conversion-declarators. */
7121 if (cp_parser_parse_definitely (parser))
7125 /* Parse another optional declarator. */
7126 declarator = cp_parser_conversion_declarator_opt (parser);
7128 /* Create the representation of the declarator. */
7129 if (code == INDIRECT_REF)
7130 declarator = make_pointer_declarator (cv_qualifier_seq,
7133 declarator = make_reference_declarator (cv_qualifier_seq,
7136 /* Handle the pointer-to-member case. */
7138 declarator = build_nt (SCOPE_REF, class_type, declarator);
7146 /* Parse an (optional) ctor-initializer.
7149 : mem-initializer-list
7151 Returns TRUE iff the ctor-initializer was actually present. */
7154 cp_parser_ctor_initializer_opt (cp_parser* parser)
7156 /* If the next token is not a `:', then there is no
7157 ctor-initializer. */
7158 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
7160 /* Do default initialization of any bases and members. */
7161 if (DECL_CONSTRUCTOR_P (current_function_decl))
7162 finish_mem_initializers (NULL_TREE);
7167 /* Consume the `:' token. */
7168 cp_lexer_consume_token (parser->lexer);
7169 /* And the mem-initializer-list. */
7170 cp_parser_mem_initializer_list (parser);
7175 /* Parse a mem-initializer-list.
7177 mem-initializer-list:
7179 mem-initializer , mem-initializer-list */
7182 cp_parser_mem_initializer_list (cp_parser* parser)
7184 tree mem_initializer_list = NULL_TREE;
7186 /* Let the semantic analysis code know that we are starting the
7187 mem-initializer-list. */
7188 if (!DECL_CONSTRUCTOR_P (current_function_decl))
7189 error ("only constructors take base initializers");
7191 /* Loop through the list. */
7194 tree mem_initializer;
7196 /* Parse the mem-initializer. */
7197 mem_initializer = cp_parser_mem_initializer (parser);
7198 /* Add it to the list, unless it was erroneous. */
7199 if (mem_initializer)
7201 TREE_CHAIN (mem_initializer) = mem_initializer_list;
7202 mem_initializer_list = mem_initializer;
7204 /* If the next token is not a `,', we're done. */
7205 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7207 /* Consume the `,' token. */
7208 cp_lexer_consume_token (parser->lexer);
7211 /* Perform semantic analysis. */
7212 if (DECL_CONSTRUCTOR_P (current_function_decl))
7213 finish_mem_initializers (mem_initializer_list);
7216 /* Parse a mem-initializer.
7219 mem-initializer-id ( expression-list [opt] )
7224 ( expression-list [opt] )
7226 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
7227 class) or FIELD_DECL (for a non-static data member) to initialize;
7228 the TREE_VALUE is the expression-list. */
7231 cp_parser_mem_initializer (cp_parser* parser)
7233 tree mem_initializer_id;
7234 tree expression_list;
7237 /* Find out what is being initialized. */
7238 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7240 pedwarn ("anachronistic old-style base class initializer");
7241 mem_initializer_id = NULL_TREE;
7244 mem_initializer_id = cp_parser_mem_initializer_id (parser);
7245 member = expand_member_init (mem_initializer_id);
7246 if (member && !DECL_P (member))
7247 in_base_initializer = 1;
7250 = cp_parser_parenthesized_expression_list (parser, false,
7251 /*non_constant_p=*/NULL);
7252 if (!expression_list)
7253 expression_list = void_type_node;
7255 in_base_initializer = 0;
7257 return member ? build_tree_list (member, expression_list) : NULL_TREE;
7260 /* Parse a mem-initializer-id.
7263 :: [opt] nested-name-specifier [opt] class-name
7266 Returns a TYPE indicating the class to be initializer for the first
7267 production. Returns an IDENTIFIER_NODE indicating the data member
7268 to be initialized for the second production. */
7271 cp_parser_mem_initializer_id (cp_parser* parser)
7273 bool global_scope_p;
7274 bool nested_name_specifier_p;
7277 /* Look for the optional `::' operator. */
7279 = (cp_parser_global_scope_opt (parser,
7280 /*current_scope_valid_p=*/false)
7282 /* Look for the optional nested-name-specifier. The simplest way to
7287 The keyword `typename' is not permitted in a base-specifier or
7288 mem-initializer; in these contexts a qualified name that
7289 depends on a template-parameter is implicitly assumed to be a
7292 is to assume that we have seen the `typename' keyword at this
7294 nested_name_specifier_p
7295 = (cp_parser_nested_name_specifier_opt (parser,
7296 /*typename_keyword_p=*/true,
7297 /*check_dependency_p=*/true,
7299 /*is_declaration=*/true)
7301 /* If there is a `::' operator or a nested-name-specifier, then we
7302 are definitely looking for a class-name. */
7303 if (global_scope_p || nested_name_specifier_p)
7304 return cp_parser_class_name (parser,
7305 /*typename_keyword_p=*/true,
7306 /*template_keyword_p=*/false,
7308 /*check_dependency_p=*/true,
7309 /*class_head_p=*/false,
7310 /*is_declaration=*/true);
7311 /* Otherwise, we could also be looking for an ordinary identifier. */
7312 cp_parser_parse_tentatively (parser);
7313 /* Try a class-name. */
7314 id = cp_parser_class_name (parser,
7315 /*typename_keyword_p=*/true,
7316 /*template_keyword_p=*/false,
7318 /*check_dependency_p=*/true,
7319 /*class_head_p=*/false,
7320 /*is_declaration=*/true);
7321 /* If we found one, we're done. */
7322 if (cp_parser_parse_definitely (parser))
7324 /* Otherwise, look for an ordinary identifier. */
7325 return cp_parser_identifier (parser);
7328 /* Overloading [gram.over] */
7330 /* Parse an operator-function-id.
7332 operator-function-id:
7335 Returns an IDENTIFIER_NODE for the operator which is a
7336 human-readable spelling of the identifier, e.g., `operator +'. */
7339 cp_parser_operator_function_id (cp_parser* parser)
7341 /* Look for the `operator' keyword. */
7342 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
7343 return error_mark_node;
7344 /* And then the name of the operator itself. */
7345 return cp_parser_operator (parser);
7348 /* Parse an operator.
7351 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
7352 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
7353 || ++ -- , ->* -> () []
7360 Returns an IDENTIFIER_NODE for the operator which is a
7361 human-readable spelling of the identifier, e.g., `operator +'. */
7364 cp_parser_operator (cp_parser* parser)
7366 tree id = NULL_TREE;
7369 /* Peek at the next token. */
7370 token = cp_lexer_peek_token (parser->lexer);
7371 /* Figure out which operator we have. */
7372 switch (token->type)
7378 /* The keyword should be either `new' or `delete'. */
7379 if (token->keyword == RID_NEW)
7381 else if (token->keyword == RID_DELETE)
7386 /* Consume the `new' or `delete' token. */
7387 cp_lexer_consume_token (parser->lexer);
7389 /* Peek at the next token. */
7390 token = cp_lexer_peek_token (parser->lexer);
7391 /* If it's a `[' token then this is the array variant of the
7393 if (token->type == CPP_OPEN_SQUARE)
7395 /* Consume the `[' token. */
7396 cp_lexer_consume_token (parser->lexer);
7397 /* Look for the `]' token. */
7398 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
7399 id = ansi_opname (op == NEW_EXPR
7400 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
7402 /* Otherwise, we have the non-array variant. */
7404 id = ansi_opname (op);
7410 id = ansi_opname (PLUS_EXPR);
7414 id = ansi_opname (MINUS_EXPR);
7418 id = ansi_opname (MULT_EXPR);
7422 id = ansi_opname (TRUNC_DIV_EXPR);
7426 id = ansi_opname (TRUNC_MOD_EXPR);
7430 id = ansi_opname (BIT_XOR_EXPR);
7434 id = ansi_opname (BIT_AND_EXPR);
7438 id = ansi_opname (BIT_IOR_EXPR);
7442 id = ansi_opname (BIT_NOT_EXPR);
7446 id = ansi_opname (TRUTH_NOT_EXPR);
7450 id = ansi_assopname (NOP_EXPR);
7454 id = ansi_opname (LT_EXPR);
7458 id = ansi_opname (GT_EXPR);
7462 id = ansi_assopname (PLUS_EXPR);
7466 id = ansi_assopname (MINUS_EXPR);
7470 id = ansi_assopname (MULT_EXPR);
7474 id = ansi_assopname (TRUNC_DIV_EXPR);
7478 id = ansi_assopname (TRUNC_MOD_EXPR);
7482 id = ansi_assopname (BIT_XOR_EXPR);
7486 id = ansi_assopname (BIT_AND_EXPR);
7490 id = ansi_assopname (BIT_IOR_EXPR);
7494 id = ansi_opname (LSHIFT_EXPR);
7498 id = ansi_opname (RSHIFT_EXPR);
7502 id = ansi_assopname (LSHIFT_EXPR);
7506 id = ansi_assopname (RSHIFT_EXPR);
7510 id = ansi_opname (EQ_EXPR);
7514 id = ansi_opname (NE_EXPR);
7518 id = ansi_opname (LE_EXPR);
7521 case CPP_GREATER_EQ:
7522 id = ansi_opname (GE_EXPR);
7526 id = ansi_opname (TRUTH_ANDIF_EXPR);
7530 id = ansi_opname (TRUTH_ORIF_EXPR);
7534 id = ansi_opname (POSTINCREMENT_EXPR);
7537 case CPP_MINUS_MINUS:
7538 id = ansi_opname (PREDECREMENT_EXPR);
7542 id = ansi_opname (COMPOUND_EXPR);
7545 case CPP_DEREF_STAR:
7546 id = ansi_opname (MEMBER_REF);
7550 id = ansi_opname (COMPONENT_REF);
7553 case CPP_OPEN_PAREN:
7554 /* Consume the `('. */
7555 cp_lexer_consume_token (parser->lexer);
7556 /* Look for the matching `)'. */
7557 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
7558 return ansi_opname (CALL_EXPR);
7560 case CPP_OPEN_SQUARE:
7561 /* Consume the `['. */
7562 cp_lexer_consume_token (parser->lexer);
7563 /* Look for the matching `]'. */
7564 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
7565 return ansi_opname (ARRAY_REF);
7569 id = ansi_opname (MIN_EXPR);
7573 id = ansi_opname (MAX_EXPR);
7577 id = ansi_assopname (MIN_EXPR);
7581 id = ansi_assopname (MAX_EXPR);
7585 /* Anything else is an error. */
7589 /* If we have selected an identifier, we need to consume the
7592 cp_lexer_consume_token (parser->lexer);
7593 /* Otherwise, no valid operator name was present. */
7596 cp_parser_error (parser, "expected operator");
7597 id = error_mark_node;
7603 /* Parse a template-declaration.
7605 template-declaration:
7606 export [opt] template < template-parameter-list > declaration
7608 If MEMBER_P is TRUE, this template-declaration occurs within a
7611 The grammar rule given by the standard isn't correct. What
7614 template-declaration:
7615 export [opt] template-parameter-list-seq
7616 decl-specifier-seq [opt] init-declarator [opt] ;
7617 export [opt] template-parameter-list-seq
7620 template-parameter-list-seq:
7621 template-parameter-list-seq [opt]
7622 template < template-parameter-list > */
7625 cp_parser_template_declaration (cp_parser* parser, bool member_p)
7627 /* Check for `export'. */
7628 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
7630 /* Consume the `export' token. */
7631 cp_lexer_consume_token (parser->lexer);
7632 /* Warn that we do not support `export'. */
7633 warning ("keyword `export' not implemented, and will be ignored");
7636 cp_parser_template_declaration_after_export (parser, member_p);
7639 /* Parse a template-parameter-list.
7641 template-parameter-list:
7643 template-parameter-list , template-parameter
7645 Returns a TREE_LIST. Each node represents a template parameter.
7646 The nodes are connected via their TREE_CHAINs. */
7649 cp_parser_template_parameter_list (cp_parser* parser)
7651 tree parameter_list = NULL_TREE;
7658 /* Parse the template-parameter. */
7659 parameter = cp_parser_template_parameter (parser);
7660 /* Add it to the list. */
7661 parameter_list = process_template_parm (parameter_list,
7664 /* Peek at the next token. */
7665 token = cp_lexer_peek_token (parser->lexer);
7666 /* If it's not a `,', we're done. */
7667 if (token->type != CPP_COMMA)
7669 /* Otherwise, consume the `,' token. */
7670 cp_lexer_consume_token (parser->lexer);
7673 return parameter_list;
7676 /* Parse a template-parameter.
7680 parameter-declaration
7682 Returns a TREE_LIST. The TREE_VALUE represents the parameter. The
7683 TREE_PURPOSE is the default value, if any. */
7686 cp_parser_template_parameter (cp_parser* parser)
7690 /* Peek at the next token. */
7691 token = cp_lexer_peek_token (parser->lexer);
7692 /* If it is `class' or `template', we have a type-parameter. */
7693 if (token->keyword == RID_TEMPLATE)
7694 return cp_parser_type_parameter (parser);
7695 /* If it is `class' or `typename' we do not know yet whether it is a
7696 type parameter or a non-type parameter. Consider:
7698 template <typename T, typename T::X X> ...
7702 template <class C, class D*> ...
7704 Here, the first parameter is a type parameter, and the second is
7705 a non-type parameter. We can tell by looking at the token after
7706 the identifier -- if it is a `,', `=', or `>' then we have a type
7708 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
7710 /* Peek at the token after `class' or `typename'. */
7711 token = cp_lexer_peek_nth_token (parser->lexer, 2);
7712 /* If it's an identifier, skip it. */
7713 if (token->type == CPP_NAME)
7714 token = cp_lexer_peek_nth_token (parser->lexer, 3);
7715 /* Now, see if the token looks like the end of a template
7717 if (token->type == CPP_COMMA
7718 || token->type == CPP_EQ
7719 || token->type == CPP_GREATER)
7720 return cp_parser_type_parameter (parser);
7723 /* Otherwise, it is a non-type parameter.
7727 When parsing a default template-argument for a non-type
7728 template-parameter, the first non-nested `>' is taken as the end
7729 of the template parameter-list rather than a greater-than
7732 cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
7733 /*parenthesized_p=*/NULL);
7736 /* Parse a type-parameter.
7739 class identifier [opt]
7740 class identifier [opt] = type-id
7741 typename identifier [opt]
7742 typename identifier [opt] = type-id
7743 template < template-parameter-list > class identifier [opt]
7744 template < template-parameter-list > class identifier [opt]
7747 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
7748 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
7749 the declaration of the parameter. */
7752 cp_parser_type_parameter (cp_parser* parser)
7757 /* Look for a keyword to tell us what kind of parameter this is. */
7758 token = cp_parser_require (parser, CPP_KEYWORD,
7759 "`class', `typename', or `template'");
7761 return error_mark_node;
7763 switch (token->keyword)
7769 tree default_argument;
7771 /* If the next token is an identifier, then it names the
7773 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
7774 identifier = cp_parser_identifier (parser);
7776 identifier = NULL_TREE;
7778 /* Create the parameter. */
7779 parameter = finish_template_type_parm (class_type_node, identifier);
7781 /* If the next token is an `=', we have a default argument. */
7782 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7784 /* Consume the `=' token. */
7785 cp_lexer_consume_token (parser->lexer);
7786 /* Parse the default-argument. */
7787 default_argument = cp_parser_type_id (parser);
7790 default_argument = NULL_TREE;
7792 /* Create the combined representation of the parameter and the
7793 default argument. */
7794 parameter = build_tree_list (default_argument, parameter);
7800 tree parameter_list;
7802 tree default_argument;
7804 /* Look for the `<'. */
7805 cp_parser_require (parser, CPP_LESS, "`<'");
7806 /* Parse the template-parameter-list. */
7807 begin_template_parm_list ();
7809 = cp_parser_template_parameter_list (parser);
7810 parameter_list = end_template_parm_list (parameter_list);
7811 /* Look for the `>'. */
7812 cp_parser_require (parser, CPP_GREATER, "`>'");
7813 /* Look for the `class' keyword. */
7814 cp_parser_require_keyword (parser, RID_CLASS, "`class'");
7815 /* If the next token is an `=', then there is a
7816 default-argument. If the next token is a `>', we are at
7817 the end of the parameter-list. If the next token is a `,',
7818 then we are at the end of this parameter. */
7819 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
7820 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
7821 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7822 identifier = cp_parser_identifier (parser);
7824 identifier = NULL_TREE;
7825 /* Create the template parameter. */
7826 parameter = finish_template_template_parm (class_type_node,
7829 /* If the next token is an `=', then there is a
7830 default-argument. */
7831 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7835 /* Consume the `='. */
7836 cp_lexer_consume_token (parser->lexer);
7837 /* Parse the id-expression. */
7839 = cp_parser_id_expression (parser,
7840 /*template_keyword_p=*/false,
7841 /*check_dependency_p=*/true,
7842 /*template_p=*/&is_template,
7843 /*declarator_p=*/false);
7844 if (TREE_CODE (default_argument) == TYPE_DECL)
7845 /* If the id-expression was a template-id that refers to
7846 a template-class, we already have the declaration here,
7847 so no further lookup is needed. */
7850 /* Look up the name. */
7852 = cp_parser_lookup_name (parser, default_argument,
7854 /*is_template=*/is_template,
7855 /*is_namespace=*/false,
7856 /*check_dependency=*/true);
7857 /* See if the default argument is valid. */
7859 = check_template_template_default_arg (default_argument);
7862 default_argument = NULL_TREE;
7864 /* Create the combined representation of the parameter and the
7865 default argument. */
7866 parameter = build_tree_list (default_argument, parameter);
7871 /* Anything else is an error. */
7872 cp_parser_error (parser,
7873 "expected `class', `typename', or `template'");
7874 parameter = error_mark_node;
7880 /* Parse a template-id.
7883 template-name < template-argument-list [opt] >
7885 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
7886 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
7887 returned. Otherwise, if the template-name names a function, or set
7888 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
7889 names a class, returns a TYPE_DECL for the specialization.
7891 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
7892 uninstantiated templates. */
7895 cp_parser_template_id (cp_parser *parser,
7896 bool template_keyword_p,
7897 bool check_dependency_p,
7898 bool is_declaration)
7903 ptrdiff_t start_of_id;
7904 tree access_check = NULL_TREE;
7905 cp_token *next_token, *next_token_2;
7908 /* If the next token corresponds to a template-id, there is no need
7910 next_token = cp_lexer_peek_token (parser->lexer);
7911 if (next_token->type == CPP_TEMPLATE_ID)
7916 /* Get the stored value. */
7917 value = cp_lexer_consume_token (parser->lexer)->value;
7918 /* Perform any access checks that were deferred. */
7919 for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
7920 perform_or_defer_access_check (TREE_PURPOSE (check),
7921 TREE_VALUE (check));
7922 /* Return the stored value. */
7923 return TREE_VALUE (value);
7926 /* Avoid performing name lookup if there is no possibility of
7927 finding a template-id. */
7928 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
7929 || (next_token->type == CPP_NAME
7930 && !cp_parser_nth_token_starts_template_argument_list_p
7933 cp_parser_error (parser, "expected template-id");
7934 return error_mark_node;
7937 /* Remember where the template-id starts. */
7938 if (cp_parser_parsing_tentatively (parser)
7939 && !cp_parser_committed_to_tentative_parse (parser))
7941 next_token = cp_lexer_peek_token (parser->lexer);
7942 start_of_id = cp_lexer_token_difference (parser->lexer,
7943 parser->lexer->first_token,
7949 push_deferring_access_checks (dk_deferred);
7951 /* Parse the template-name. */
7952 is_identifier = false;
7953 template = cp_parser_template_name (parser, template_keyword_p,
7957 if (template == error_mark_node || is_identifier)
7959 pop_deferring_access_checks ();
7963 /* If we find the sequence `[:' after a template-name, it's probably
7964 a digraph-typo for `< ::'. Substitute the tokens and check if we can
7965 parse correctly the argument list. */
7966 next_token = cp_lexer_peek_nth_token (parser->lexer, 1);
7967 next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
7968 if (next_token->type == CPP_OPEN_SQUARE
7969 && next_token->flags & DIGRAPH
7970 && next_token_2->type == CPP_COLON
7971 && !(next_token_2->flags & PREV_WHITE))
7973 cp_parser_parse_tentatively (parser);
7974 /* Change `:' into `::'. */
7975 next_token_2->type = CPP_SCOPE;
7976 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
7978 cp_lexer_consume_token (parser->lexer);
7979 /* Parse the arguments. */
7980 arguments = cp_parser_enclosed_template_argument_list (parser);
7981 if (!cp_parser_parse_definitely (parser))
7983 /* If we couldn't parse an argument list, then we revert our changes
7984 and return simply an error. Maybe this is not a template-id
7986 next_token_2->type = CPP_COLON;
7987 cp_parser_error (parser, "expected `<'");
7988 pop_deferring_access_checks ();
7989 return error_mark_node;
7991 /* Otherwise, emit an error about the invalid digraph, but continue
7992 parsing because we got our argument list. */
7993 pedwarn ("`<::' cannot begin a template-argument list");
7994 inform ("`<:' is an alternate spelling for `['. Insert whitespace "
7995 "between `<' and `::'");
7996 if (!flag_permissive)
8001 inform ("(if you use `-fpermissive' G++ will accept your code)");
8008 /* Look for the `<' that starts the template-argument-list. */
8009 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
8011 pop_deferring_access_checks ();
8012 return error_mark_node;
8014 /* Parse the arguments. */
8015 arguments = cp_parser_enclosed_template_argument_list (parser);
8018 /* Build a representation of the specialization. */
8019 if (TREE_CODE (template) == IDENTIFIER_NODE)
8020 template_id = build_min_nt (TEMPLATE_ID_EXPR, template, arguments);
8021 else if (DECL_CLASS_TEMPLATE_P (template)
8022 || DECL_TEMPLATE_TEMPLATE_PARM_P (template))
8024 = finish_template_type (template, arguments,
8025 cp_lexer_next_token_is (parser->lexer,
8029 /* If it's not a class-template or a template-template, it should be
8030 a function-template. */
8031 my_friendly_assert ((DECL_FUNCTION_TEMPLATE_P (template)
8032 || TREE_CODE (template) == OVERLOAD
8033 || BASELINK_P (template)),
8036 template_id = lookup_template_function (template, arguments);
8039 /* Retrieve any deferred checks. Do not pop this access checks yet
8040 so the memory will not be reclaimed during token replacing below. */
8041 access_check = get_deferred_access_checks ();
8043 /* If parsing tentatively, replace the sequence of tokens that makes
8044 up the template-id with a CPP_TEMPLATE_ID token. That way,
8045 should we re-parse the token stream, we will not have to repeat
8046 the effort required to do the parse, nor will we issue duplicate
8047 error messages about problems during instantiation of the
8049 if (start_of_id >= 0)
8053 /* Find the token that corresponds to the start of the
8055 token = cp_lexer_advance_token (parser->lexer,
8056 parser->lexer->first_token,
8059 /* Reset the contents of the START_OF_ID token. */
8060 token->type = CPP_TEMPLATE_ID;
8061 token->value = build_tree_list (access_check, template_id);
8062 token->keyword = RID_MAX;
8063 /* Purge all subsequent tokens. */
8064 cp_lexer_purge_tokens_after (parser->lexer, token);
8067 pop_deferring_access_checks ();
8071 /* Parse a template-name.
8076 The standard should actually say:
8080 operator-function-id
8082 A defect report has been filed about this issue.
8084 A conversion-function-id cannot be a template name because they cannot
8085 be part of a template-id. In fact, looking at this code:
8089 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
8090 It is impossible to call a templated conversion-function-id with an
8091 explicit argument list, since the only allowed template parameter is
8092 the type to which it is converting.
8094 If TEMPLATE_KEYWORD_P is true, then we have just seen the
8095 `template' keyword, in a construction like:
8099 In that case `f' is taken to be a template-name, even though there
8100 is no way of knowing for sure.
8102 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
8103 name refers to a set of overloaded functions, at least one of which
8104 is a template, or an IDENTIFIER_NODE with the name of the template,
8105 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
8106 names are looked up inside uninstantiated templates. */
8109 cp_parser_template_name (cp_parser* parser,
8110 bool template_keyword_p,
8111 bool check_dependency_p,
8112 bool is_declaration,
8113 bool *is_identifier)
8119 /* If the next token is `operator', then we have either an
8120 operator-function-id or a conversion-function-id. */
8121 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
8123 /* We don't know whether we're looking at an
8124 operator-function-id or a conversion-function-id. */
8125 cp_parser_parse_tentatively (parser);
8126 /* Try an operator-function-id. */
8127 identifier = cp_parser_operator_function_id (parser);
8128 /* If that didn't work, try a conversion-function-id. */
8129 if (!cp_parser_parse_definitely (parser))
8131 cp_parser_error (parser, "expected template-name");
8132 return error_mark_node;
8135 /* Look for the identifier. */
8137 identifier = cp_parser_identifier (parser);
8139 /* If we didn't find an identifier, we don't have a template-id. */
8140 if (identifier == error_mark_node)
8141 return error_mark_node;
8143 /* If the name immediately followed the `template' keyword, then it
8144 is a template-name. However, if the next token is not `<', then
8145 we do not treat it as a template-name, since it is not being used
8146 as part of a template-id. This enables us to handle constructs
8149 template <typename T> struct S { S(); };
8150 template <typename T> S<T>::S();
8152 correctly. We would treat `S' as a template -- if it were `S<T>'
8153 -- but we do not if there is no `<'. */
8155 if (processing_template_decl
8156 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
8158 /* In a declaration, in a dependent context, we pretend that the
8159 "template" keyword was present in order to improve error
8160 recovery. For example, given:
8162 template <typename T> void f(T::X<int>);
8164 we want to treat "X<int>" as a template-id. */
8166 && !template_keyword_p
8167 && parser->scope && TYPE_P (parser->scope)
8168 && dependent_type_p (parser->scope))
8172 /* Explain what went wrong. */
8173 error ("non-template `%D' used as template", identifier);
8174 error ("(use `%T::template %D' to indicate that it is a template)",
8175 parser->scope, identifier);
8176 /* If parsing tentatively, find the location of the "<"
8178 if (cp_parser_parsing_tentatively (parser)
8179 && !cp_parser_committed_to_tentative_parse (parser))
8181 cp_parser_simulate_error (parser);
8182 token = cp_lexer_peek_token (parser->lexer);
8183 token = cp_lexer_prev_token (parser->lexer, token);
8184 start = cp_lexer_token_difference (parser->lexer,
8185 parser->lexer->first_token,
8190 /* Parse the template arguments so that we can issue error
8191 messages about them. */
8192 cp_lexer_consume_token (parser->lexer);
8193 cp_parser_enclosed_template_argument_list (parser);
8194 /* Skip tokens until we find a good place from which to
8195 continue parsing. */
8196 cp_parser_skip_to_closing_parenthesis (parser,
8197 /*recovering=*/true,
8199 /*consume_paren=*/false);
8200 /* If parsing tentatively, permanently remove the
8201 template argument list. That will prevent duplicate
8202 error messages from being issued about the missing
8203 "template" keyword. */
8206 token = cp_lexer_advance_token (parser->lexer,
8207 parser->lexer->first_token,
8209 cp_lexer_purge_tokens_after (parser->lexer, token);
8212 *is_identifier = true;
8216 /* If the "template" keyword is present, then there is generally
8217 no point in doing name-lookup, so we just return IDENTIFIER.
8218 But, if the qualifying scope is non-dependent then we can
8219 (and must) do name-lookup normally. */
8220 if (template_keyword_p
8222 || (TYPE_P (parser->scope)
8223 && dependent_type_p (parser->scope))))
8227 /* Look up the name. */
8228 decl = cp_parser_lookup_name (parser, identifier,
8230 /*is_template=*/false,
8231 /*is_namespace=*/false,
8232 check_dependency_p);
8233 decl = maybe_get_template_decl_from_type_decl (decl);
8235 /* If DECL is a template, then the name was a template-name. */
8236 if (TREE_CODE (decl) == TEMPLATE_DECL)
8240 /* The standard does not explicitly indicate whether a name that
8241 names a set of overloaded declarations, some of which are
8242 templates, is a template-name. However, such a name should
8243 be a template-name; otherwise, there is no way to form a
8244 template-id for the overloaded templates. */
8245 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
8246 if (TREE_CODE (fns) == OVERLOAD)
8250 for (fn = fns; fn; fn = OVL_NEXT (fn))
8251 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
8256 /* Otherwise, the name does not name a template. */
8257 cp_parser_error (parser, "expected template-name");
8258 return error_mark_node;
8262 /* If DECL is dependent, and refers to a function, then just return
8263 its name; we will look it up again during template instantiation. */
8264 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
8266 tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
8267 if (TYPE_P (scope) && dependent_type_p (scope))
8274 /* Parse a template-argument-list.
8276 template-argument-list:
8278 template-argument-list , template-argument
8280 Returns a TREE_VEC containing the arguments. */
8283 cp_parser_template_argument_list (cp_parser* parser)
8285 tree fixed_args[10];
8286 unsigned n_args = 0;
8287 unsigned alloced = 10;
8288 tree *arg_ary = fixed_args;
8290 bool saved_in_template_argument_list_p;
8292 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
8293 parser->in_template_argument_list_p = true;
8299 /* Consume the comma. */
8300 cp_lexer_consume_token (parser->lexer);
8302 /* Parse the template-argument. */
8303 argument = cp_parser_template_argument (parser);
8304 if (n_args == alloced)
8308 if (arg_ary == fixed_args)
8310 arg_ary = xmalloc (sizeof (tree) * alloced);
8311 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
8314 arg_ary = xrealloc (arg_ary, sizeof (tree) * alloced);
8316 arg_ary[n_args++] = argument;
8318 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
8320 vec = make_tree_vec (n_args);
8323 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
8325 if (arg_ary != fixed_args)
8327 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
8331 /* Parse a template-argument.
8334 assignment-expression
8338 The representation is that of an assignment-expression, type-id, or
8339 id-expression -- except that the qualified id-expression is
8340 evaluated, so that the value returned is either a DECL or an
8343 Although the standard says "assignment-expression", it forbids
8344 throw-expressions or assignments in the template argument.
8345 Therefore, we use "conditional-expression" instead. */
8348 cp_parser_template_argument (cp_parser* parser)
8353 bool maybe_type_id = false;
8356 tree qualifying_class;
8358 /* There's really no way to know what we're looking at, so we just
8359 try each alternative in order.
8363 In a template-argument, an ambiguity between a type-id and an
8364 expression is resolved to a type-id, regardless of the form of
8365 the corresponding template-parameter.
8367 Therefore, we try a type-id first. */
8368 cp_parser_parse_tentatively (parser);
8369 argument = cp_parser_type_id (parser);
8370 /* If there was no error parsing the type-id but the next token is a '>>',
8371 we probably found a typo for '> >'. But there are type-id which are
8372 also valid expressions. For instance:
8374 struct X { int operator >> (int); };
8375 template <int V> struct Foo {};
8378 Here 'X()' is a valid type-id of a function type, but the user just
8379 wanted to write the expression "X() >> 5". Thus, we remember that we
8380 found a valid type-id, but we still try to parse the argument as an
8381 expression to see what happens. */
8382 if (!cp_parser_error_occurred (parser)
8383 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
8385 maybe_type_id = true;
8386 cp_parser_abort_tentative_parse (parser);
8390 /* If the next token isn't a `,' or a `>', then this argument wasn't
8391 really finished. This means that the argument is not a valid
8393 if (!cp_parser_next_token_ends_template_argument_p (parser))
8394 cp_parser_error (parser, "expected template-argument");
8395 /* If that worked, we're done. */
8396 if (cp_parser_parse_definitely (parser))
8399 /* We're still not sure what the argument will be. */
8400 cp_parser_parse_tentatively (parser);
8401 /* Try a template. */
8402 argument = cp_parser_id_expression (parser,
8403 /*template_keyword_p=*/false,
8404 /*check_dependency_p=*/true,
8406 /*declarator_p=*/false);
8407 /* If the next token isn't a `,' or a `>', then this argument wasn't
8409 if (!cp_parser_next_token_ends_template_argument_p (parser))
8410 cp_parser_error (parser, "expected template-argument");
8411 if (!cp_parser_error_occurred (parser))
8413 /* Figure out what is being referred to. */
8414 argument = cp_parser_lookup_name (parser, argument,
8416 /*is_template=*/template_p,
8417 /*is_namespace=*/false,
8418 /*check_dependency=*/true);
8419 if (TREE_CODE (argument) != TEMPLATE_DECL
8420 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
8421 cp_parser_error (parser, "expected template-name");
8423 if (cp_parser_parse_definitely (parser))
8425 /* It must be a non-type argument. There permitted cases are given
8426 in [temp.arg.nontype]:
8428 -- an integral constant-expression of integral or enumeration
8431 -- the name of a non-type template-parameter; or
8433 -- the name of an object or function with external linkage...
8435 -- the address of an object or function with external linkage...
8437 -- a pointer to member... */
8438 /* Look for a non-type template parameter. */
8439 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
8441 cp_parser_parse_tentatively (parser);
8442 argument = cp_parser_primary_expression (parser,
8445 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
8446 || !cp_parser_next_token_ends_template_argument_p (parser))
8447 cp_parser_simulate_error (parser);
8448 if (cp_parser_parse_definitely (parser))
8451 /* If the next token is "&", the argument must be the address of an
8452 object or function with external linkage. */
8453 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
8455 cp_lexer_consume_token (parser->lexer);
8456 /* See if we might have an id-expression. */
8457 token = cp_lexer_peek_token (parser->lexer);
8458 if (token->type == CPP_NAME
8459 || token->keyword == RID_OPERATOR
8460 || token->type == CPP_SCOPE
8461 || token->type == CPP_TEMPLATE_ID
8462 || token->type == CPP_NESTED_NAME_SPECIFIER)
8464 cp_parser_parse_tentatively (parser);
8465 argument = cp_parser_primary_expression (parser,
8468 if (cp_parser_error_occurred (parser)
8469 || !cp_parser_next_token_ends_template_argument_p (parser))
8470 cp_parser_abort_tentative_parse (parser);
8473 if (qualifying_class)
8474 argument = finish_qualified_id_expr (qualifying_class,
8478 if (TREE_CODE (argument) == VAR_DECL)
8480 /* A variable without external linkage might still be a
8481 valid constant-expression, so no error is issued here
8482 if the external-linkage check fails. */
8483 if (!DECL_EXTERNAL_LINKAGE_P (argument))
8484 cp_parser_simulate_error (parser);
8486 else if (is_overloaded_fn (argument))
8487 /* All overloaded functions are allowed; if the external
8488 linkage test does not pass, an error will be issued
8492 && (TREE_CODE (argument) == OFFSET_REF
8493 || TREE_CODE (argument) == SCOPE_REF))
8494 /* A pointer-to-member. */
8497 cp_parser_simulate_error (parser);
8499 if (cp_parser_parse_definitely (parser))
8502 argument = build_x_unary_op (ADDR_EXPR, argument);
8507 /* If the argument started with "&", there are no other valid
8508 alternatives at this point. */
8511 cp_parser_error (parser, "invalid non-type template argument");
8512 return error_mark_node;
8514 /* If the argument wasn't successfully parsed as a type-id followed
8515 by '>>', the argument can only be a constant expression now.
8516 Otherwise, we try parsing the constant-expression tentatively,
8517 because the argument could really be a type-id. */
8519 cp_parser_parse_tentatively (parser);
8520 argument = cp_parser_constant_expression (parser,
8521 /*allow_non_constant_p=*/false,
8522 /*non_constant_p=*/NULL);
8523 argument = fold_non_dependent_expr (argument);
8526 if (!cp_parser_next_token_ends_template_argument_p (parser))
8527 cp_parser_error (parser, "expected template-argument");
8528 if (cp_parser_parse_definitely (parser))
8530 /* We did our best to parse the argument as a non type-id, but that
8531 was the only alternative that matched (albeit with a '>' after
8532 it). We can assume it's just a typo from the user, and a
8533 diagnostic will then be issued. */
8534 return cp_parser_type_id (parser);
8537 /* Parse an explicit-instantiation.
8539 explicit-instantiation:
8540 template declaration
8542 Although the standard says `declaration', what it really means is:
8544 explicit-instantiation:
8545 template decl-specifier-seq [opt] declarator [opt] ;
8547 Things like `template int S<int>::i = 5, int S<double>::j;' are not
8548 supposed to be allowed. A defect report has been filed about this
8553 explicit-instantiation:
8554 storage-class-specifier template
8555 decl-specifier-seq [opt] declarator [opt] ;
8556 function-specifier template
8557 decl-specifier-seq [opt] declarator [opt] ; */
8560 cp_parser_explicit_instantiation (cp_parser* parser)
8562 int declares_class_or_enum;
8563 tree decl_specifiers;
8565 tree extension_specifier = NULL_TREE;
8567 /* Look for an (optional) storage-class-specifier or
8568 function-specifier. */
8569 if (cp_parser_allow_gnu_extensions_p (parser))
8572 = cp_parser_storage_class_specifier_opt (parser);
8573 if (!extension_specifier)
8574 extension_specifier = cp_parser_function_specifier_opt (parser);
8577 /* Look for the `template' keyword. */
8578 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
8579 /* Let the front end know that we are processing an explicit
8581 begin_explicit_instantiation ();
8582 /* [temp.explicit] says that we are supposed to ignore access
8583 control while processing explicit instantiation directives. */
8584 push_deferring_access_checks (dk_no_check);
8585 /* Parse a decl-specifier-seq. */
8587 = cp_parser_decl_specifier_seq (parser,
8588 CP_PARSER_FLAGS_OPTIONAL,
8590 &declares_class_or_enum);
8591 /* If there was exactly one decl-specifier, and it declared a class,
8592 and there's no declarator, then we have an explicit type
8594 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
8598 type = check_tag_decl (decl_specifiers);
8599 /* Turn access control back on for names used during
8600 template instantiation. */
8601 pop_deferring_access_checks ();
8603 do_type_instantiation (type, extension_specifier, /*complain=*/1);
8610 /* Parse the declarator. */
8612 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
8613 /*ctor_dtor_or_conv_p=*/NULL,
8614 /*parenthesized_p=*/NULL);
8615 cp_parser_check_for_definition_in_return_type (declarator,
8616 declares_class_or_enum);
8617 if (declarator != error_mark_node)
8619 decl = grokdeclarator (declarator, decl_specifiers,
8621 /* Turn access control back on for names used during
8622 template instantiation. */
8623 pop_deferring_access_checks ();
8624 /* Do the explicit instantiation. */
8625 do_decl_instantiation (decl, extension_specifier);
8629 pop_deferring_access_checks ();
8630 /* Skip the body of the explicit instantiation. */
8631 cp_parser_skip_to_end_of_statement (parser);
8634 /* We're done with the instantiation. */
8635 end_explicit_instantiation ();
8637 cp_parser_consume_semicolon_at_end_of_statement (parser);
8640 /* Parse an explicit-specialization.
8642 explicit-specialization:
8643 template < > declaration
8645 Although the standard says `declaration', what it really means is:
8647 explicit-specialization:
8648 template <> decl-specifier [opt] init-declarator [opt] ;
8649 template <> function-definition
8650 template <> explicit-specialization
8651 template <> template-declaration */
8654 cp_parser_explicit_specialization (cp_parser* parser)
8656 /* Look for the `template' keyword. */
8657 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
8658 /* Look for the `<'. */
8659 cp_parser_require (parser, CPP_LESS, "`<'");
8660 /* Look for the `>'. */
8661 cp_parser_require (parser, CPP_GREATER, "`>'");
8662 /* We have processed another parameter list. */
8663 ++parser->num_template_parameter_lists;
8664 /* Let the front end know that we are beginning a specialization. */
8665 begin_specialization ();
8667 /* If the next keyword is `template', we need to figure out whether
8668 or not we're looking a template-declaration. */
8669 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
8671 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
8672 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
8673 cp_parser_template_declaration_after_export (parser,
8674 /*member_p=*/false);
8676 cp_parser_explicit_specialization (parser);
8679 /* Parse the dependent declaration. */
8680 cp_parser_single_declaration (parser,
8684 /* We're done with the specialization. */
8685 end_specialization ();
8686 /* We're done with this parameter list. */
8687 --parser->num_template_parameter_lists;
8690 /* Parse a type-specifier.
8693 simple-type-specifier
8696 elaborated-type-specifier
8704 Returns a representation of the type-specifier. If the
8705 type-specifier is a keyword (like `int' or `const', or
8706 `__complex__') then the corresponding IDENTIFIER_NODE is returned.
8707 For a class-specifier, enum-specifier, or elaborated-type-specifier
8708 a TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
8710 If IS_FRIEND is TRUE then this type-specifier is being declared a
8711 `friend'. If IS_DECLARATION is TRUE, then this type-specifier is
8712 appearing in a decl-specifier-seq.
8714 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
8715 class-specifier, enum-specifier, or elaborated-type-specifier, then
8716 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
8717 if a type is declared; 2 if it is defined. Otherwise, it is set to
8720 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
8721 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
8725 cp_parser_type_specifier (cp_parser* parser,
8726 cp_parser_flags flags,
8728 bool is_declaration,
8729 int* declares_class_or_enum,
8730 bool* is_cv_qualifier)
8732 tree type_spec = NULL_TREE;
8736 /* Assume this type-specifier does not declare a new type. */
8737 if (declares_class_or_enum)
8738 *declares_class_or_enum = 0;
8739 /* And that it does not specify a cv-qualifier. */
8740 if (is_cv_qualifier)
8741 *is_cv_qualifier = false;
8742 /* Peek at the next token. */
8743 token = cp_lexer_peek_token (parser->lexer);
8745 /* If we're looking at a keyword, we can use that to guide the
8746 production we choose. */
8747 keyword = token->keyword;
8750 /* Any of these indicate either a class-specifier, or an
8751 elaborated-type-specifier. */
8756 /* Parse tentatively so that we can back up if we don't find a
8757 class-specifier or enum-specifier. */
8758 cp_parser_parse_tentatively (parser);
8759 /* Look for the class-specifier or enum-specifier. */
8760 if (keyword == RID_ENUM)
8761 type_spec = cp_parser_enum_specifier (parser);
8763 type_spec = cp_parser_class_specifier (parser);
8765 /* If that worked, we're done. */
8766 if (cp_parser_parse_definitely (parser))
8768 if (declares_class_or_enum)
8769 *declares_class_or_enum = 2;
8776 /* Look for an elaborated-type-specifier. */
8777 type_spec = cp_parser_elaborated_type_specifier (parser,
8780 /* We're declaring a class or enum -- unless we're using
8782 if (declares_class_or_enum && keyword != RID_TYPENAME)
8783 *declares_class_or_enum = 1;
8789 type_spec = cp_parser_cv_qualifier_opt (parser);
8790 /* Even though we call a routine that looks for an optional
8791 qualifier, we know that there should be one. */
8792 my_friendly_assert (type_spec != NULL, 20000328);
8793 /* This type-specifier was a cv-qualified. */
8794 if (is_cv_qualifier)
8795 *is_cv_qualifier = true;
8800 /* The `__complex__' keyword is a GNU extension. */
8801 return cp_lexer_consume_token (parser->lexer)->value;
8807 /* If we do not already have a type-specifier, assume we are looking
8808 at a simple-type-specifier. */
8809 type_spec = cp_parser_simple_type_specifier (parser, flags,
8810 /*identifier_p=*/true);
8812 /* If we didn't find a type-specifier, and a type-specifier was not
8813 optional in this context, issue an error message. */
8814 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
8816 cp_parser_error (parser, "expected type specifier");
8817 return error_mark_node;
8823 /* Parse a simple-type-specifier.
8825 simple-type-specifier:
8826 :: [opt] nested-name-specifier [opt] type-name
8827 :: [opt] nested-name-specifier template template-id
8842 simple-type-specifier:
8843 __typeof__ unary-expression
8844 __typeof__ ( type-id )
8846 For the various keywords, the value returned is simply the
8847 TREE_IDENTIFIER representing the keyword if IDENTIFIER_P is true.
8848 For the first two productions, and if IDENTIFIER_P is false, the
8849 value returned is the indicated TYPE_DECL. */
8852 cp_parser_simple_type_specifier (cp_parser* parser, cp_parser_flags flags,
8855 tree type = NULL_TREE;
8858 /* Peek at the next token. */
8859 token = cp_lexer_peek_token (parser->lexer);
8861 /* If we're looking at a keyword, things are easy. */
8862 switch (token->keyword)
8865 type = char_type_node;
8868 type = wchar_type_node;
8871 type = boolean_type_node;
8874 type = short_integer_type_node;
8877 type = integer_type_node;
8880 type = long_integer_type_node;
8883 type = integer_type_node;
8886 type = unsigned_type_node;
8889 type = float_type_node;
8892 type = double_type_node;
8895 type = void_type_node;
8902 /* Consume the `typeof' token. */
8903 cp_lexer_consume_token (parser->lexer);
8904 /* Parse the operand to `typeof'. */
8905 operand = cp_parser_sizeof_operand (parser, RID_TYPEOF);
8906 /* If it is not already a TYPE, take its type. */
8907 if (!TYPE_P (operand))
8908 operand = finish_typeof (operand);
8917 /* If the type-specifier was for a built-in type, we're done. */
8922 /* Consume the token. */
8923 id = cp_lexer_consume_token (parser->lexer)->value;
8925 /* There is no valid C++ program where a non-template type is
8926 followed by a "<". That usually indicates that the user thought
8927 that the type was a template. */
8928 cp_parser_check_for_invalid_template_id (parser, type);
8930 return identifier_p ? id : TYPE_NAME (type);
8933 /* The type-specifier must be a user-defined type. */
8934 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
8936 /* Don't gobble tokens or issue error messages if this is an
8937 optional type-specifier. */
8938 if (flags & CP_PARSER_FLAGS_OPTIONAL)
8939 cp_parser_parse_tentatively (parser);
8941 /* Look for the optional `::' operator. */
8942 cp_parser_global_scope_opt (parser,
8943 /*current_scope_valid_p=*/false);
8944 /* Look for the nested-name specifier. */
8945 cp_parser_nested_name_specifier_opt (parser,
8946 /*typename_keyword_p=*/false,
8947 /*check_dependency_p=*/true,
8949 /*is_declaration=*/false);
8950 /* If we have seen a nested-name-specifier, and the next token
8951 is `template', then we are using the template-id production. */
8953 && cp_parser_optional_template_keyword (parser))
8955 /* Look for the template-id. */
8956 type = cp_parser_template_id (parser,
8957 /*template_keyword_p=*/true,
8958 /*check_dependency_p=*/true,
8959 /*is_declaration=*/false);
8960 /* If the template-id did not name a type, we are out of
8962 if (TREE_CODE (type) != TYPE_DECL)
8964 cp_parser_error (parser, "expected template-id for type");
8968 /* Otherwise, look for a type-name. */
8970 type = cp_parser_type_name (parser);
8971 /* If it didn't work out, we don't have a TYPE. */
8972 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
8973 && !cp_parser_parse_definitely (parser))
8977 /* If we didn't get a type-name, issue an error message. */
8978 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
8980 cp_parser_error (parser, "expected type-name");
8981 return error_mark_node;
8984 /* There is no valid C++ program where a non-template type is
8985 followed by a "<". That usually indicates that the user thought
8986 that the type was a template. */
8987 if (type && type != error_mark_node)
8988 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type));
8993 /* Parse a type-name.
9006 Returns a TYPE_DECL for the the type. */
9009 cp_parser_type_name (cp_parser* parser)
9014 /* We can't know yet whether it is a class-name or not. */
9015 cp_parser_parse_tentatively (parser);
9016 /* Try a class-name. */
9017 type_decl = cp_parser_class_name (parser,
9018 /*typename_keyword_p=*/false,
9019 /*template_keyword_p=*/false,
9021 /*check_dependency_p=*/true,
9022 /*class_head_p=*/false,
9023 /*is_declaration=*/false);
9024 /* If it's not a class-name, keep looking. */
9025 if (!cp_parser_parse_definitely (parser))
9027 /* It must be a typedef-name or an enum-name. */
9028 identifier = cp_parser_identifier (parser);
9029 if (identifier == error_mark_node)
9030 return error_mark_node;
9032 /* Look up the type-name. */
9033 type_decl = cp_parser_lookup_name_simple (parser, identifier);
9034 /* Issue an error if we did not find a type-name. */
9035 if (TREE_CODE (type_decl) != TYPE_DECL)
9037 if (!cp_parser_simulate_error (parser))
9038 cp_parser_name_lookup_error (parser, identifier, type_decl,
9040 type_decl = error_mark_node;
9042 /* Remember that the name was used in the definition of the
9043 current class so that we can check later to see if the
9044 meaning would have been different after the class was
9045 entirely defined. */
9046 else if (type_decl != error_mark_node
9048 maybe_note_name_used_in_class (identifier, type_decl);
9055 /* Parse an elaborated-type-specifier. Note that the grammar given
9056 here incorporates the resolution to DR68.
9058 elaborated-type-specifier:
9059 class-key :: [opt] nested-name-specifier [opt] identifier
9060 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
9061 enum :: [opt] nested-name-specifier [opt] identifier
9062 typename :: [opt] nested-name-specifier identifier
9063 typename :: [opt] nested-name-specifier template [opt]
9068 elaborated-type-specifier:
9069 class-key attributes :: [opt] nested-name-specifier [opt] identifier
9070 class-key attributes :: [opt] nested-name-specifier [opt]
9071 template [opt] template-id
9072 enum attributes :: [opt] nested-name-specifier [opt] identifier
9074 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
9075 declared `friend'. If IS_DECLARATION is TRUE, then this
9076 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
9077 something is being declared.
9079 Returns the TYPE specified. */
9082 cp_parser_elaborated_type_specifier (cp_parser* parser,
9084 bool is_declaration)
9086 enum tag_types tag_type;
9088 tree type = NULL_TREE;
9089 tree attributes = NULL_TREE;
9091 /* See if we're looking at the `enum' keyword. */
9092 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
9094 /* Consume the `enum' token. */
9095 cp_lexer_consume_token (parser->lexer);
9096 /* Remember that it's an enumeration type. */
9097 tag_type = enum_type;
9098 /* Parse the attributes. */
9099 attributes = cp_parser_attributes_opt (parser);
9101 /* Or, it might be `typename'. */
9102 else if (cp_lexer_next_token_is_keyword (parser->lexer,
9105 /* Consume the `typename' token. */
9106 cp_lexer_consume_token (parser->lexer);
9107 /* Remember that it's a `typename' type. */
9108 tag_type = typename_type;
9109 /* The `typename' keyword is only allowed in templates. */
9110 if (!processing_template_decl)
9111 pedwarn ("using `typename' outside of template");
9113 /* Otherwise it must be a class-key. */
9116 tag_type = cp_parser_class_key (parser);
9117 if (tag_type == none_type)
9118 return error_mark_node;
9119 /* Parse the attributes. */
9120 attributes = cp_parser_attributes_opt (parser);
9123 /* Look for the `::' operator. */
9124 cp_parser_global_scope_opt (parser,
9125 /*current_scope_valid_p=*/false);
9126 /* Look for the nested-name-specifier. */
9127 if (tag_type == typename_type)
9129 if (cp_parser_nested_name_specifier (parser,
9130 /*typename_keyword_p=*/true,
9131 /*check_dependency_p=*/true,
9135 return error_mark_node;
9138 /* Even though `typename' is not present, the proposed resolution
9139 to Core Issue 180 says that in `class A<T>::B', `B' should be
9140 considered a type-name, even if `A<T>' is dependent. */
9141 cp_parser_nested_name_specifier_opt (parser,
9142 /*typename_keyword_p=*/true,
9143 /*check_dependency_p=*/true,
9146 /* For everything but enumeration types, consider a template-id. */
9147 if (tag_type != enum_type)
9149 bool template_p = false;
9152 /* Allow the `template' keyword. */
9153 template_p = cp_parser_optional_template_keyword (parser);
9154 /* If we didn't see `template', we don't know if there's a
9155 template-id or not. */
9157 cp_parser_parse_tentatively (parser);
9158 /* Parse the template-id. */
9159 decl = cp_parser_template_id (parser, template_p,
9160 /*check_dependency_p=*/true,
9162 /* If we didn't find a template-id, look for an ordinary
9164 if (!template_p && !cp_parser_parse_definitely (parser))
9166 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
9167 in effect, then we must assume that, upon instantiation, the
9168 template will correspond to a class. */
9169 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
9170 && tag_type == typename_type)
9171 type = make_typename_type (parser->scope, decl,
9174 type = TREE_TYPE (decl);
9177 /* For an enumeration type, consider only a plain identifier. */
9180 identifier = cp_parser_identifier (parser);
9182 if (identifier == error_mark_node)
9184 parser->scope = NULL_TREE;
9185 return error_mark_node;
9188 /* For a `typename', we needn't call xref_tag. */
9189 if (tag_type == typename_type)
9190 return cp_parser_make_typename_type (parser, parser->scope,
9192 /* Look up a qualified name in the usual way. */
9197 /* In an elaborated-type-specifier, names are assumed to name
9198 types, so we set IS_TYPE to TRUE when calling
9199 cp_parser_lookup_name. */
9200 decl = cp_parser_lookup_name (parser, identifier,
9202 /*is_template=*/false,
9203 /*is_namespace=*/false,
9204 /*check_dependency=*/true);
9206 /* If we are parsing friend declaration, DECL may be a
9207 TEMPLATE_DECL tree node here. However, we need to check
9208 whether this TEMPLATE_DECL results in valid code. Consider
9209 the following example:
9212 template <class T> class C {};
9215 template <class T> friend class N::C; // #1, valid code
9217 template <class T> class Y {
9218 friend class N::C; // #2, invalid code
9221 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
9222 name lookup of `N::C'. We see that friend declaration must
9223 be template for the code to be valid. Note that
9224 processing_template_decl does not work here since it is
9225 always 1 for the above two cases. */
9227 decl = (cp_parser_maybe_treat_template_as_class
9228 (decl, /*tag_name_p=*/is_friend
9229 && parser->num_template_parameter_lists));
9231 if (TREE_CODE (decl) != TYPE_DECL)
9233 error ("expected type-name");
9234 return error_mark_node;
9237 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
9238 check_elaborated_type_specifier
9240 (parser->num_template_parameter_lists
9241 || DECL_SELF_REFERENCE_P (decl)));
9243 type = TREE_TYPE (decl);
9247 /* An elaborated-type-specifier sometimes introduces a new type and
9248 sometimes names an existing type. Normally, the rule is that it
9249 introduces a new type only if there is not an existing type of
9250 the same name already in scope. For example, given:
9253 void f() { struct S s; }
9255 the `struct S' in the body of `f' is the same `struct S' as in
9256 the global scope; the existing definition is used. However, if
9257 there were no global declaration, this would introduce a new
9258 local class named `S'.
9260 An exception to this rule applies to the following code:
9262 namespace N { struct S; }
9264 Here, the elaborated-type-specifier names a new type
9265 unconditionally; even if there is already an `S' in the
9266 containing scope this declaration names a new type.
9267 This exception only applies if the elaborated-type-specifier
9268 forms the complete declaration:
9272 A declaration consisting solely of `class-key identifier ;' is
9273 either a redeclaration of the name in the current scope or a
9274 forward declaration of the identifier as a class name. It
9275 introduces the name into the current scope.
9277 We are in this situation precisely when the next token is a `;'.
9279 An exception to the exception is that a `friend' declaration does
9280 *not* name a new type; i.e., given:
9282 struct S { friend struct T; };
9284 `T' is not a new type in the scope of `S'.
9286 Also, `new struct S' or `sizeof (struct S)' never results in the
9287 definition of a new type; a new type can only be declared in a
9288 declaration context. */
9290 /* Warn about attributes. They are ignored. */
9292 warning ("type attributes are honored only at type definition");
9294 type = xref_tag (tag_type, identifier,
9297 || cp_lexer_next_token_is_not (parser->lexer,
9299 parser->num_template_parameter_lists);
9302 if (tag_type != enum_type)
9303 cp_parser_check_class_key (tag_type, type);
9305 /* A "<" cannot follow an elaborated type specifier. If that
9306 happens, the user was probably trying to form a template-id. */
9307 cp_parser_check_for_invalid_template_id (parser, type);
9312 /* Parse an enum-specifier.
9315 enum identifier [opt] { enumerator-list [opt] }
9317 Returns an ENUM_TYPE representing the enumeration. */
9320 cp_parser_enum_specifier (cp_parser* parser)
9323 tree identifier = NULL_TREE;
9326 /* Look for the `enum' keyword. */
9327 if (!cp_parser_require_keyword (parser, RID_ENUM, "`enum'"))
9328 return error_mark_node;
9329 /* Peek at the next token. */
9330 token = cp_lexer_peek_token (parser->lexer);
9332 /* See if it is an identifier. */
9333 if (token->type == CPP_NAME)
9334 identifier = cp_parser_identifier (parser);
9336 /* Look for the `{'. */
9337 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
9338 return error_mark_node;
9340 /* At this point, we're going ahead with the enum-specifier, even
9341 if some other problem occurs. */
9342 cp_parser_commit_to_tentative_parse (parser);
9344 /* Issue an error message if type-definitions are forbidden here. */
9345 cp_parser_check_type_definition (parser);
9347 /* Create the new type. */
9348 type = start_enum (identifier ? identifier : make_anon_name ());
9350 /* Peek at the next token. */
9351 token = cp_lexer_peek_token (parser->lexer);
9352 /* If it's not a `}', then there are some enumerators. */
9353 if (token->type != CPP_CLOSE_BRACE)
9354 cp_parser_enumerator_list (parser, type);
9355 /* Look for the `}'. */
9356 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
9358 /* Finish up the enumeration. */
9364 /* Parse an enumerator-list. The enumerators all have the indicated
9368 enumerator-definition
9369 enumerator-list , enumerator-definition */
9372 cp_parser_enumerator_list (cp_parser* parser, tree type)
9378 /* Parse an enumerator-definition. */
9379 cp_parser_enumerator_definition (parser, type);
9380 /* Peek at the next token. */
9381 token = cp_lexer_peek_token (parser->lexer);
9382 /* If it's not a `,', then we've reached the end of the
9384 if (token->type != CPP_COMMA)
9386 /* Otherwise, consume the `,' and keep going. */
9387 cp_lexer_consume_token (parser->lexer);
9388 /* If the next token is a `}', there is a trailing comma. */
9389 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
9391 if (pedantic && !in_system_header)
9392 pedwarn ("comma at end of enumerator list");
9398 /* Parse an enumerator-definition. The enumerator has the indicated
9401 enumerator-definition:
9403 enumerator = constant-expression
9409 cp_parser_enumerator_definition (cp_parser* parser, tree type)
9415 /* Look for the identifier. */
9416 identifier = cp_parser_identifier (parser);
9417 if (identifier == error_mark_node)
9420 /* Peek at the next token. */
9421 token = cp_lexer_peek_token (parser->lexer);
9422 /* If it's an `=', then there's an explicit value. */
9423 if (token->type == CPP_EQ)
9425 /* Consume the `=' token. */
9426 cp_lexer_consume_token (parser->lexer);
9427 /* Parse the value. */
9428 value = cp_parser_constant_expression (parser,
9429 /*allow_non_constant_p=*/false,
9435 /* Create the enumerator. */
9436 build_enumerator (identifier, value, type);
9439 /* Parse a namespace-name.
9442 original-namespace-name
9445 Returns the NAMESPACE_DECL for the namespace. */
9448 cp_parser_namespace_name (cp_parser* parser)
9451 tree namespace_decl;
9453 /* Get the name of the namespace. */
9454 identifier = cp_parser_identifier (parser);
9455 if (identifier == error_mark_node)
9456 return error_mark_node;
9458 /* Look up the identifier in the currently active scope. Look only
9459 for namespaces, due to:
9463 When looking up a namespace-name in a using-directive or alias
9464 definition, only namespace names are considered.
9470 During the lookup of a name preceding the :: scope resolution
9471 operator, object, function, and enumerator names are ignored.
9473 (Note that cp_parser_class_or_namespace_name only calls this
9474 function if the token after the name is the scope resolution
9476 namespace_decl = cp_parser_lookup_name (parser, identifier,
9478 /*is_template=*/false,
9479 /*is_namespace=*/true,
9480 /*check_dependency=*/true);
9481 /* If it's not a namespace, issue an error. */
9482 if (namespace_decl == error_mark_node
9483 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
9485 cp_parser_error (parser, "expected namespace-name");
9486 namespace_decl = error_mark_node;
9489 return namespace_decl;
9492 /* Parse a namespace-definition.
9494 namespace-definition:
9495 named-namespace-definition
9496 unnamed-namespace-definition
9498 named-namespace-definition:
9499 original-namespace-definition
9500 extension-namespace-definition
9502 original-namespace-definition:
9503 namespace identifier { namespace-body }
9505 extension-namespace-definition:
9506 namespace original-namespace-name { namespace-body }
9508 unnamed-namespace-definition:
9509 namespace { namespace-body } */
9512 cp_parser_namespace_definition (cp_parser* parser)
9516 /* Look for the `namespace' keyword. */
9517 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
9519 /* Get the name of the namespace. We do not attempt to distinguish
9520 between an original-namespace-definition and an
9521 extension-namespace-definition at this point. The semantic
9522 analysis routines are responsible for that. */
9523 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
9524 identifier = cp_parser_identifier (parser);
9526 identifier = NULL_TREE;
9528 /* Look for the `{' to start the namespace. */
9529 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
9530 /* Start the namespace. */
9531 push_namespace (identifier);
9532 /* Parse the body of the namespace. */
9533 cp_parser_namespace_body (parser);
9534 /* Finish the namespace. */
9536 /* Look for the final `}'. */
9537 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
9540 /* Parse a namespace-body.
9543 declaration-seq [opt] */
9546 cp_parser_namespace_body (cp_parser* parser)
9548 cp_parser_declaration_seq_opt (parser);
9551 /* Parse a namespace-alias-definition.
9553 namespace-alias-definition:
9554 namespace identifier = qualified-namespace-specifier ; */
9557 cp_parser_namespace_alias_definition (cp_parser* parser)
9560 tree namespace_specifier;
9562 /* Look for the `namespace' keyword. */
9563 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
9564 /* Look for the identifier. */
9565 identifier = cp_parser_identifier (parser);
9566 if (identifier == error_mark_node)
9568 /* Look for the `=' token. */
9569 cp_parser_require (parser, CPP_EQ, "`='");
9570 /* Look for the qualified-namespace-specifier. */
9572 = cp_parser_qualified_namespace_specifier (parser);
9573 /* Look for the `;' token. */
9574 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
9576 /* Register the alias in the symbol table. */
9577 do_namespace_alias (identifier, namespace_specifier);
9580 /* Parse a qualified-namespace-specifier.
9582 qualified-namespace-specifier:
9583 :: [opt] nested-name-specifier [opt] namespace-name
9585 Returns a NAMESPACE_DECL corresponding to the specified
9589 cp_parser_qualified_namespace_specifier (cp_parser* parser)
9591 /* Look for the optional `::'. */
9592 cp_parser_global_scope_opt (parser,
9593 /*current_scope_valid_p=*/false);
9595 /* Look for the optional nested-name-specifier. */
9596 cp_parser_nested_name_specifier_opt (parser,
9597 /*typename_keyword_p=*/false,
9598 /*check_dependency_p=*/true,
9600 /*is_declaration=*/true);
9602 return cp_parser_namespace_name (parser);
9605 /* Parse a using-declaration.
9608 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
9609 using :: unqualified-id ; */
9612 cp_parser_using_declaration (cp_parser* parser)
9615 bool typename_p = false;
9616 bool global_scope_p;
9622 /* Look for the `using' keyword. */
9623 cp_parser_require_keyword (parser, RID_USING, "`using'");
9625 /* Peek at the next token. */
9626 token = cp_lexer_peek_token (parser->lexer);
9627 /* See if it's `typename'. */
9628 if (token->keyword == RID_TYPENAME)
9630 /* Remember that we've seen it. */
9632 /* Consume the `typename' token. */
9633 cp_lexer_consume_token (parser->lexer);
9636 /* Look for the optional global scope qualification. */
9638 = (cp_parser_global_scope_opt (parser,
9639 /*current_scope_valid_p=*/false)
9642 /* If we saw `typename', or didn't see `::', then there must be a
9643 nested-name-specifier present. */
9644 if (typename_p || !global_scope_p)
9645 qscope = cp_parser_nested_name_specifier (parser, typename_p,
9646 /*check_dependency_p=*/true,
9648 /*is_declaration=*/true);
9649 /* Otherwise, we could be in either of the two productions. In that
9650 case, treat the nested-name-specifier as optional. */
9652 qscope = cp_parser_nested_name_specifier_opt (parser,
9653 /*typename_keyword_p=*/false,
9654 /*check_dependency_p=*/true,
9656 /*is_declaration=*/true);
9658 qscope = global_namespace;
9660 /* Parse the unqualified-id. */
9661 identifier = cp_parser_unqualified_id (parser,
9662 /*template_keyword_p=*/false,
9663 /*check_dependency_p=*/true,
9664 /*declarator_p=*/true);
9666 /* The function we call to handle a using-declaration is different
9667 depending on what scope we are in. */
9668 if (identifier == error_mark_node)
9670 else if (TREE_CODE (identifier) != IDENTIFIER_NODE
9671 && TREE_CODE (identifier) != BIT_NOT_EXPR)
9672 /* [namespace.udecl]
9674 A using declaration shall not name a template-id. */
9675 error ("a template-id may not appear in a using-declaration");
9678 scope = current_scope ();
9679 if (scope && TYPE_P (scope))
9681 /* Create the USING_DECL. */
9682 decl = do_class_using_decl (build_nt (SCOPE_REF,
9685 /* Add it to the list of members in this class. */
9686 finish_member_declaration (decl);
9690 decl = cp_parser_lookup_name_simple (parser, identifier);
9691 if (decl == error_mark_node)
9692 cp_parser_name_lookup_error (parser, identifier, decl, NULL);
9694 do_local_using_decl (decl, qscope, identifier);
9696 do_toplevel_using_decl (decl, qscope, identifier);
9700 /* Look for the final `;'. */
9701 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
9704 /* Parse a using-directive.
9707 using namespace :: [opt] nested-name-specifier [opt]
9711 cp_parser_using_directive (cp_parser* parser)
9713 tree namespace_decl;
9716 /* Look for the `using' keyword. */
9717 cp_parser_require_keyword (parser, RID_USING, "`using'");
9718 /* And the `namespace' keyword. */
9719 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
9720 /* Look for the optional `::' operator. */
9721 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
9722 /* And the optional nested-name-specifier. */
9723 cp_parser_nested_name_specifier_opt (parser,
9724 /*typename_keyword_p=*/false,
9725 /*check_dependency_p=*/true,
9727 /*is_declaration=*/true);
9728 /* Get the namespace being used. */
9729 namespace_decl = cp_parser_namespace_name (parser);
9730 /* And any specified attributes. */
9731 attribs = cp_parser_attributes_opt (parser);
9732 /* Update the symbol table. */
9733 parse_using_directive (namespace_decl, attribs);
9734 /* Look for the final `;'. */
9735 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
9738 /* Parse an asm-definition.
9741 asm ( string-literal ) ;
9746 asm volatile [opt] ( string-literal ) ;
9747 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
9748 asm volatile [opt] ( string-literal : asm-operand-list [opt]
9749 : asm-operand-list [opt] ) ;
9750 asm volatile [opt] ( string-literal : asm-operand-list [opt]
9751 : asm-operand-list [opt]
9752 : asm-operand-list [opt] ) ; */
9755 cp_parser_asm_definition (cp_parser* parser)
9759 tree outputs = NULL_TREE;
9760 tree inputs = NULL_TREE;
9761 tree clobbers = NULL_TREE;
9763 bool volatile_p = false;
9764 bool extended_p = false;
9766 /* Look for the `asm' keyword. */
9767 cp_parser_require_keyword (parser, RID_ASM, "`asm'");
9768 /* See if the next token is `volatile'. */
9769 if (cp_parser_allow_gnu_extensions_p (parser)
9770 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
9772 /* Remember that we saw the `volatile' keyword. */
9774 /* Consume the token. */
9775 cp_lexer_consume_token (parser->lexer);
9777 /* Look for the opening `('. */
9778 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
9779 /* Look for the string. */
9780 c_lex_string_translate = false;
9781 token = cp_parser_require (parser, CPP_STRING, "asm body");
9784 string = token->value;
9785 /* If we're allowing GNU extensions, check for the extended assembly
9786 syntax. Unfortunately, the `:' tokens need not be separated by
9787 a space in C, and so, for compatibility, we tolerate that here
9788 too. Doing that means that we have to treat the `::' operator as
9790 if (cp_parser_allow_gnu_extensions_p (parser)
9791 && at_function_scope_p ()
9792 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
9793 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
9795 bool inputs_p = false;
9796 bool clobbers_p = false;
9798 /* The extended syntax was used. */
9801 /* Look for outputs. */
9802 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
9804 /* Consume the `:'. */
9805 cp_lexer_consume_token (parser->lexer);
9806 /* Parse the output-operands. */
9807 if (cp_lexer_next_token_is_not (parser->lexer,
9809 && cp_lexer_next_token_is_not (parser->lexer,
9811 && cp_lexer_next_token_is_not (parser->lexer,
9813 outputs = cp_parser_asm_operand_list (parser);
9815 /* If the next token is `::', there are no outputs, and the
9816 next token is the beginning of the inputs. */
9817 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
9819 /* Consume the `::' token. */
9820 cp_lexer_consume_token (parser->lexer);
9821 /* The inputs are coming next. */
9825 /* Look for inputs. */
9827 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
9830 /* Consume the `:'. */
9831 cp_lexer_consume_token (parser->lexer);
9832 /* Parse the output-operands. */
9833 if (cp_lexer_next_token_is_not (parser->lexer,
9835 && cp_lexer_next_token_is_not (parser->lexer,
9837 && cp_lexer_next_token_is_not (parser->lexer,
9839 inputs = cp_parser_asm_operand_list (parser);
9841 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
9842 /* The clobbers are coming next. */
9845 /* Look for clobbers. */
9847 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
9850 /* Consume the `:'. */
9851 cp_lexer_consume_token (parser->lexer);
9852 /* Parse the clobbers. */
9853 if (cp_lexer_next_token_is_not (parser->lexer,
9855 clobbers = cp_parser_asm_clobber_list (parser);
9858 /* Look for the closing `)'. */
9859 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
9860 cp_parser_skip_to_closing_parenthesis (parser, true, false,
9861 /*consume_paren=*/true);
9862 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
9864 /* Create the ASM_STMT. */
9865 if (at_function_scope_p ())
9868 finish_asm_stmt (volatile_p
9869 ? ridpointers[(int) RID_VOLATILE] : NULL_TREE,
9870 string, outputs, inputs, clobbers);
9871 /* If the extended syntax was not used, mark the ASM_STMT. */
9873 ASM_INPUT_P (asm_stmt) = 1;
9876 assemble_asm (string);
9879 c_lex_string_translate = true;
9882 /* Declarators [gram.dcl.decl] */
9884 /* Parse an init-declarator.
9887 declarator initializer [opt]
9892 declarator asm-specification [opt] attributes [opt] initializer [opt]
9894 function-definition:
9895 decl-specifier-seq [opt] declarator ctor-initializer [opt]
9897 decl-specifier-seq [opt] declarator function-try-block
9901 function-definition:
9902 __extension__ function-definition
9904 The DECL_SPECIFIERS and PREFIX_ATTRIBUTES apply to this declarator.
9905 Returns a representation of the entity declared. If MEMBER_P is TRUE,
9906 then this declarator appears in a class scope. The new DECL created
9907 by this declarator is returned.
9909 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
9910 for a function-definition here as well. If the declarator is a
9911 declarator for a function-definition, *FUNCTION_DEFINITION_P will
9912 be TRUE upon return. By that point, the function-definition will
9913 have been completely parsed.
9915 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
9919 cp_parser_init_declarator (cp_parser* parser,
9920 tree decl_specifiers,
9921 tree prefix_attributes,
9922 bool function_definition_allowed_p,
9924 int declares_class_or_enum,
9925 bool* function_definition_p)
9930 tree asm_specification;
9932 tree decl = NULL_TREE;
9934 bool is_initialized;
9935 bool is_parenthesized_init;
9936 bool is_non_constant_init;
9937 int ctor_dtor_or_conv_p;
9941 /* Assume that this is not the declarator for a function
9943 if (function_definition_p)
9944 *function_definition_p = false;
9946 /* Defer access checks while parsing the declarator; we cannot know
9947 what names are accessible until we know what is being
9949 resume_deferring_access_checks ();
9951 /* Parse the declarator. */
9953 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
9954 &ctor_dtor_or_conv_p,
9955 /*parenthesized_p=*/NULL);
9956 /* Gather up the deferred checks. */
9957 stop_deferring_access_checks ();
9959 /* If the DECLARATOR was erroneous, there's no need to go
9961 if (declarator == error_mark_node)
9962 return error_mark_node;
9964 cp_parser_check_for_definition_in_return_type (declarator,
9965 declares_class_or_enum);
9967 /* Figure out what scope the entity declared by the DECLARATOR is
9968 located in. `grokdeclarator' sometimes changes the scope, so
9969 we compute it now. */
9970 scope = get_scope_of_declarator (declarator);
9972 /* If we're allowing GNU extensions, look for an asm-specification
9974 if (cp_parser_allow_gnu_extensions_p (parser))
9976 /* Look for an asm-specification. */
9977 asm_specification = cp_parser_asm_specification_opt (parser);
9978 /* And attributes. */
9979 attributes = cp_parser_attributes_opt (parser);
9983 asm_specification = NULL_TREE;
9984 attributes = NULL_TREE;
9987 /* Peek at the next token. */
9988 token = cp_lexer_peek_token (parser->lexer);
9989 /* Check to see if the token indicates the start of a
9990 function-definition. */
9991 if (cp_parser_token_starts_function_definition_p (token))
9993 if (!function_definition_allowed_p)
9995 /* If a function-definition should not appear here, issue an
9997 cp_parser_error (parser,
9998 "a function-definition is not allowed here");
9999 return error_mark_node;
10003 /* Neither attributes nor an asm-specification are allowed
10004 on a function-definition. */
10005 if (asm_specification)
10006 error ("an asm-specification is not allowed on a function-definition");
10008 error ("attributes are not allowed on a function-definition");
10009 /* This is a function-definition. */
10010 *function_definition_p = true;
10012 /* Parse the function definition. */
10014 decl = cp_parser_save_member_function_body (parser,
10017 prefix_attributes);
10020 = (cp_parser_function_definition_from_specifiers_and_declarator
10021 (parser, decl_specifiers, prefix_attributes, declarator));
10029 Only in function declarations for constructors, destructors, and
10030 type conversions can the decl-specifier-seq be omitted.
10032 We explicitly postpone this check past the point where we handle
10033 function-definitions because we tolerate function-definitions
10034 that are missing their return types in some modes. */
10035 if (!decl_specifiers && ctor_dtor_or_conv_p <= 0)
10037 cp_parser_error (parser,
10038 "expected constructor, destructor, or type conversion");
10039 return error_mark_node;
10042 /* An `=' or an `(' indicates an initializer. */
10043 is_initialized = (token->type == CPP_EQ
10044 || token->type == CPP_OPEN_PAREN);
10045 /* If the init-declarator isn't initialized and isn't followed by a
10046 `,' or `;', it's not a valid init-declarator. */
10047 if (!is_initialized
10048 && token->type != CPP_COMMA
10049 && token->type != CPP_SEMICOLON)
10051 cp_parser_error (parser, "expected init-declarator");
10052 return error_mark_node;
10055 /* Because start_decl has side-effects, we should only call it if we
10056 know we're going ahead. By this point, we know that we cannot
10057 possibly be looking at any other construct. */
10058 cp_parser_commit_to_tentative_parse (parser);
10060 /* If the decl specifiers were bad, issue an error now that we're
10061 sure this was intended to be a declarator. Then continue
10062 declaring the variable(s), as int, to try to cut down on further
10064 if (decl_specifiers != NULL
10065 && TREE_VALUE (decl_specifiers) == error_mark_node)
10067 cp_parser_error (parser, "invalid type in declaration");
10068 TREE_VALUE (decl_specifiers) = integer_type_node;
10071 /* Check to see whether or not this declaration is a friend. */
10072 friend_p = cp_parser_friend_p (decl_specifiers);
10074 /* Check that the number of template-parameter-lists is OK. */
10075 if (!cp_parser_check_declarator_template_parameters (parser, declarator))
10076 return error_mark_node;
10078 /* Enter the newly declared entry in the symbol table. If we're
10079 processing a declaration in a class-specifier, we wait until
10080 after processing the initializer. */
10083 if (parser->in_unbraced_linkage_specification_p)
10085 decl_specifiers = tree_cons (error_mark_node,
10086 get_identifier ("extern"),
10088 have_extern_spec = false;
10090 decl = start_decl (declarator, decl_specifiers,
10091 is_initialized, attributes, prefix_attributes);
10094 /* Enter the SCOPE. That way unqualified names appearing in the
10095 initializer will be looked up in SCOPE. */
10097 pop_p = push_scope (scope);
10099 /* Perform deferred access control checks, now that we know in which
10100 SCOPE the declared entity resides. */
10101 if (!member_p && decl)
10103 tree saved_current_function_decl = NULL_TREE;
10105 /* If the entity being declared is a function, pretend that we
10106 are in its scope. If it is a `friend', it may have access to
10107 things that would not otherwise be accessible. */
10108 if (TREE_CODE (decl) == FUNCTION_DECL)
10110 saved_current_function_decl = current_function_decl;
10111 current_function_decl = decl;
10114 /* Perform the access control checks for the declarator and the
10115 the decl-specifiers. */
10116 perform_deferred_access_checks ();
10118 /* Restore the saved value. */
10119 if (TREE_CODE (decl) == FUNCTION_DECL)
10120 current_function_decl = saved_current_function_decl;
10123 /* Parse the initializer. */
10124 if (is_initialized)
10125 initializer = cp_parser_initializer (parser,
10126 &is_parenthesized_init,
10127 &is_non_constant_init);
10130 initializer = NULL_TREE;
10131 is_parenthesized_init = false;
10132 is_non_constant_init = true;
10135 /* The old parser allows attributes to appear after a parenthesized
10136 initializer. Mark Mitchell proposed removing this functionality
10137 on the GCC mailing lists on 2002-08-13. This parser accepts the
10138 attributes -- but ignores them. */
10139 if (cp_parser_allow_gnu_extensions_p (parser) && is_parenthesized_init)
10140 if (cp_parser_attributes_opt (parser))
10141 warning ("attributes after parenthesized initializer ignored");
10143 /* Leave the SCOPE, now that we have processed the initializer. It
10144 is important to do this before calling cp_finish_decl because it
10145 makes decisions about whether to create DECL_STMTs or not based
10146 on the current scope. */
10150 /* For an in-class declaration, use `grokfield' to create the
10154 decl = grokfield (declarator, decl_specifiers,
10155 initializer, /*asmspec=*/NULL_TREE,
10156 /*attributes=*/NULL_TREE);
10157 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
10158 cp_parser_save_default_args (parser, decl);
10161 /* Finish processing the declaration. But, skip friend
10163 if (!friend_p && decl)
10164 cp_finish_decl (decl,
10167 /* If the initializer is in parentheses, then this is
10168 a direct-initialization, which means that an
10169 `explicit' constructor is OK. Otherwise, an
10170 `explicit' constructor cannot be used. */
10171 ((is_parenthesized_init || !is_initialized)
10172 ? 0 : LOOKUP_ONLYCONVERTING));
10174 /* Remember whether or not variables were initialized by
10175 constant-expressions. */
10176 if (decl && TREE_CODE (decl) == VAR_DECL
10177 && is_initialized && !is_non_constant_init)
10178 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = true;
10183 /* Parse a declarator.
10187 ptr-operator declarator
10189 abstract-declarator:
10190 ptr-operator abstract-declarator [opt]
10191 direct-abstract-declarator
10196 attributes [opt] direct-declarator
10197 attributes [opt] ptr-operator declarator
10199 abstract-declarator:
10200 attributes [opt] ptr-operator abstract-declarator [opt]
10201 attributes [opt] direct-abstract-declarator
10203 Returns a representation of the declarator. If the declarator has
10204 the form `* declarator', then an INDIRECT_REF is returned, whose
10205 only operand is the sub-declarator. Analogously, `& declarator' is
10206 represented as an ADDR_EXPR. For `X::* declarator', a SCOPE_REF is
10207 used. The first operand is the TYPE for `X'. The second operand
10208 is an INDIRECT_REF whose operand is the sub-declarator.
10210 Otherwise, the representation is as for a direct-declarator.
10212 (It would be better to define a structure type to represent
10213 declarators, rather than abusing `tree' nodes to represent
10214 declarators. That would be much clearer and save some memory.
10215 There is no reason for declarators to be garbage-collected, for
10216 example; they are created during parser and no longer needed after
10217 `grokdeclarator' has been called.)
10219 For a ptr-operator that has the optional cv-qualifier-seq,
10220 cv-qualifiers will be stored in the TREE_TYPE of the INDIRECT_REF
10223 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
10224 detect constructor, destructor or conversion operators. It is set
10225 to -1 if the declarator is a name, and +1 if it is a
10226 function. Otherwise it is set to zero. Usually you just want to
10227 test for >0, but internally the negative value is used.
10229 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
10230 a decl-specifier-seq unless it declares a constructor, destructor,
10231 or conversion. It might seem that we could check this condition in
10232 semantic analysis, rather than parsing, but that makes it difficult
10233 to handle something like `f()'. We want to notice that there are
10234 no decl-specifiers, and therefore realize that this is an
10235 expression, not a declaration.)
10237 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
10238 the declarator is a direct-declarator of the form "(...)". */
10241 cp_parser_declarator (cp_parser* parser,
10242 cp_parser_declarator_kind dcl_kind,
10243 int* ctor_dtor_or_conv_p,
10244 bool* parenthesized_p)
10248 enum tree_code code;
10249 tree cv_qualifier_seq;
10251 tree attributes = NULL_TREE;
10253 /* Assume this is not a constructor, destructor, or type-conversion
10255 if (ctor_dtor_or_conv_p)
10256 *ctor_dtor_or_conv_p = 0;
10258 if (cp_parser_allow_gnu_extensions_p (parser))
10259 attributes = cp_parser_attributes_opt (parser);
10261 /* Peek at the next token. */
10262 token = cp_lexer_peek_token (parser->lexer);
10264 /* Check for the ptr-operator production. */
10265 cp_parser_parse_tentatively (parser);
10266 /* Parse the ptr-operator. */
10267 code = cp_parser_ptr_operator (parser,
10269 &cv_qualifier_seq);
10270 /* If that worked, then we have a ptr-operator. */
10271 if (cp_parser_parse_definitely (parser))
10273 /* If a ptr-operator was found, then this declarator was not
10275 if (parenthesized_p)
10276 *parenthesized_p = true;
10277 /* The dependent declarator is optional if we are parsing an
10278 abstract-declarator. */
10279 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
10280 cp_parser_parse_tentatively (parser);
10282 /* Parse the dependent declarator. */
10283 declarator = cp_parser_declarator (parser, dcl_kind,
10284 /*ctor_dtor_or_conv_p=*/NULL,
10285 /*parenthesized_p=*/NULL);
10287 /* If we are parsing an abstract-declarator, we must handle the
10288 case where the dependent declarator is absent. */
10289 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
10290 && !cp_parser_parse_definitely (parser))
10291 declarator = NULL_TREE;
10293 /* Build the representation of the ptr-operator. */
10294 if (code == INDIRECT_REF)
10295 declarator = make_pointer_declarator (cv_qualifier_seq,
10298 declarator = make_reference_declarator (cv_qualifier_seq,
10300 /* Handle the pointer-to-member case. */
10302 declarator = build_nt (SCOPE_REF, class_type, declarator);
10304 /* Everything else is a direct-declarator. */
10307 if (parenthesized_p)
10308 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
10310 declarator = cp_parser_direct_declarator (parser, dcl_kind,
10311 ctor_dtor_or_conv_p);
10314 if (attributes && declarator != error_mark_node)
10315 declarator = tree_cons (attributes, declarator, NULL_TREE);
10320 /* Parse a direct-declarator or direct-abstract-declarator.
10324 direct-declarator ( parameter-declaration-clause )
10325 cv-qualifier-seq [opt]
10326 exception-specification [opt]
10327 direct-declarator [ constant-expression [opt] ]
10330 direct-abstract-declarator:
10331 direct-abstract-declarator [opt]
10332 ( parameter-declaration-clause )
10333 cv-qualifier-seq [opt]
10334 exception-specification [opt]
10335 direct-abstract-declarator [opt] [ constant-expression [opt] ]
10336 ( abstract-declarator )
10338 Returns a representation of the declarator. DCL_KIND is
10339 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
10340 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
10341 we are parsing a direct-declarator. It is
10342 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
10343 of ambiguity we prefer an abstract declarator, as per
10344 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P is as for
10345 cp_parser_declarator.
10347 For the declarator-id production, the representation is as for an
10348 id-expression, except that a qualified name is represented as a
10349 SCOPE_REF. A function-declarator is represented as a CALL_EXPR;
10350 see the documentation of the FUNCTION_DECLARATOR_* macros for
10351 information about how to find the various declarator components.
10352 An array-declarator is represented as an ARRAY_REF. The
10353 direct-declarator is the first operand; the constant-expression
10354 indicating the size of the array is the second operand. */
10357 cp_parser_direct_declarator (cp_parser* parser,
10358 cp_parser_declarator_kind dcl_kind,
10359 int* ctor_dtor_or_conv_p)
10362 tree declarator = NULL_TREE;
10363 tree scope = NULL_TREE;
10364 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
10365 bool saved_in_declarator_p = parser->in_declarator_p;
10367 bool pop_p = false;
10371 /* Peek at the next token. */
10372 token = cp_lexer_peek_token (parser->lexer);
10373 if (token->type == CPP_OPEN_PAREN)
10375 /* This is either a parameter-declaration-clause, or a
10376 parenthesized declarator. When we know we are parsing a
10377 named declarator, it must be a parenthesized declarator
10378 if FIRST is true. For instance, `(int)' is a
10379 parameter-declaration-clause, with an omitted
10380 direct-abstract-declarator. But `((*))', is a
10381 parenthesized abstract declarator. Finally, when T is a
10382 template parameter `(T)' is a
10383 parameter-declaration-clause, and not a parenthesized
10386 We first try and parse a parameter-declaration-clause,
10387 and then try a nested declarator (if FIRST is true).
10389 It is not an error for it not to be a
10390 parameter-declaration-clause, even when FIRST is
10396 The first is the declaration of a function while the
10397 second is a the definition of a variable, including its
10400 Having seen only the parenthesis, we cannot know which of
10401 these two alternatives should be selected. Even more
10402 complex are examples like:
10407 The former is a function-declaration; the latter is a
10408 variable initialization.
10410 Thus again, we try a parameter-declaration-clause, and if
10411 that fails, we back out and return. */
10413 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
10416 unsigned saved_num_template_parameter_lists;
10418 cp_parser_parse_tentatively (parser);
10420 /* Consume the `('. */
10421 cp_lexer_consume_token (parser->lexer);
10424 /* If this is going to be an abstract declarator, we're
10425 in a declarator and we can't have default args. */
10426 parser->default_arg_ok_p = false;
10427 parser->in_declarator_p = true;
10430 /* Inside the function parameter list, surrounding
10431 template-parameter-lists do not apply. */
10432 saved_num_template_parameter_lists
10433 = parser->num_template_parameter_lists;
10434 parser->num_template_parameter_lists = 0;
10436 /* Parse the parameter-declaration-clause. */
10437 params = cp_parser_parameter_declaration_clause (parser);
10439 parser->num_template_parameter_lists
10440 = saved_num_template_parameter_lists;
10442 /* If all went well, parse the cv-qualifier-seq and the
10443 exception-specification. */
10444 if (cp_parser_parse_definitely (parser))
10446 tree cv_qualifiers;
10447 tree exception_specification;
10449 if (ctor_dtor_or_conv_p)
10450 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
10452 /* Consume the `)'. */
10453 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
10455 /* Parse the cv-qualifier-seq. */
10456 cv_qualifiers = cp_parser_cv_qualifier_seq_opt (parser);
10457 /* And the exception-specification. */
10458 exception_specification
10459 = cp_parser_exception_specification_opt (parser);
10461 /* Create the function-declarator. */
10462 declarator = make_call_declarator (declarator,
10465 exception_specification);
10466 /* Any subsequent parameter lists are to do with
10467 return type, so are not those of the declared
10469 parser->default_arg_ok_p = false;
10471 /* Repeat the main loop. */
10476 /* If this is the first, we can try a parenthesized
10480 bool saved_in_type_id_in_expr_p;
10482 parser->default_arg_ok_p = saved_default_arg_ok_p;
10483 parser->in_declarator_p = saved_in_declarator_p;
10485 /* Consume the `('. */
10486 cp_lexer_consume_token (parser->lexer);
10487 /* Parse the nested declarator. */
10488 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
10489 parser->in_type_id_in_expr_p = true;
10491 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
10492 /*parenthesized_p=*/NULL);
10493 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
10495 /* Expect a `)'. */
10496 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
10497 declarator = error_mark_node;
10498 if (declarator == error_mark_node)
10501 goto handle_declarator;
10503 /* Otherwise, we must be done. */
10507 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
10508 && token->type == CPP_OPEN_SQUARE)
10510 /* Parse an array-declarator. */
10513 if (ctor_dtor_or_conv_p)
10514 *ctor_dtor_or_conv_p = 0;
10517 parser->default_arg_ok_p = false;
10518 parser->in_declarator_p = true;
10519 /* Consume the `['. */
10520 cp_lexer_consume_token (parser->lexer);
10521 /* Peek at the next token. */
10522 token = cp_lexer_peek_token (parser->lexer);
10523 /* If the next token is `]', then there is no
10524 constant-expression. */
10525 if (token->type != CPP_CLOSE_SQUARE)
10527 bool non_constant_p;
10530 = cp_parser_constant_expression (parser,
10531 /*allow_non_constant=*/true,
10533 if (!non_constant_p)
10534 bounds = fold_non_dependent_expr (bounds);
10537 bounds = NULL_TREE;
10538 /* Look for the closing `]'. */
10539 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'"))
10541 declarator = error_mark_node;
10545 declarator = build_nt (ARRAY_REF, declarator, bounds);
10547 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
10549 /* Parse a declarator-id */
10550 if (dcl_kind == CP_PARSER_DECLARATOR_EITHER)
10551 cp_parser_parse_tentatively (parser);
10552 declarator = cp_parser_declarator_id (parser);
10553 if (dcl_kind == CP_PARSER_DECLARATOR_EITHER)
10555 if (!cp_parser_parse_definitely (parser))
10556 declarator = error_mark_node;
10557 else if (TREE_CODE (declarator) != IDENTIFIER_NODE)
10559 cp_parser_error (parser, "expected unqualified-id");
10560 declarator = error_mark_node;
10564 if (declarator == error_mark_node)
10567 if (TREE_CODE (declarator) == SCOPE_REF
10568 && !current_scope ())
10570 tree scope = TREE_OPERAND (declarator, 0);
10572 /* In the declaration of a member of a template class
10573 outside of the class itself, the SCOPE will sometimes
10574 be a TYPENAME_TYPE. For example, given:
10576 template <typename T>
10577 int S<T>::R::i = 3;
10579 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
10580 this context, we must resolve S<T>::R to an ordinary
10581 type, rather than a typename type.
10583 The reason we normally avoid resolving TYPENAME_TYPEs
10584 is that a specialization of `S' might render
10585 `S<T>::R' not a type. However, if `S' is
10586 specialized, then this `i' will not be used, so there
10587 is no harm in resolving the types here. */
10588 if (TREE_CODE (scope) == TYPENAME_TYPE)
10592 /* Resolve the TYPENAME_TYPE. */
10593 type = resolve_typename_type (scope,
10594 /*only_current_p=*/false);
10595 /* If that failed, the declarator is invalid. */
10596 if (type != error_mark_node)
10598 /* Build a new DECLARATOR. */
10599 declarator = build_nt (SCOPE_REF,
10601 TREE_OPERAND (declarator, 1));
10605 /* Check to see whether the declarator-id names a constructor,
10606 destructor, or conversion. */
10607 if (declarator && ctor_dtor_or_conv_p
10608 && ((TREE_CODE (declarator) == SCOPE_REF
10609 && CLASS_TYPE_P (TREE_OPERAND (declarator, 0)))
10610 || (TREE_CODE (declarator) != SCOPE_REF
10611 && at_class_scope_p ())))
10613 tree unqualified_name;
10616 /* Get the unqualified part of the name. */
10617 if (TREE_CODE (declarator) == SCOPE_REF)
10619 class_type = TREE_OPERAND (declarator, 0);
10620 unqualified_name = TREE_OPERAND (declarator, 1);
10624 class_type = current_class_type;
10625 unqualified_name = declarator;
10628 /* See if it names ctor, dtor or conv. */
10629 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR
10630 || IDENTIFIER_TYPENAME_P (unqualified_name)
10631 || constructor_name_p (unqualified_name, class_type)
10632 || (TREE_CODE (unqualified_name) == TYPE_DECL
10633 && same_type_p (TREE_TYPE (unqualified_name),
10635 *ctor_dtor_or_conv_p = -1;
10638 handle_declarator:;
10639 scope = get_scope_of_declarator (declarator);
10641 /* Any names that appear after the declarator-id for a
10642 member are looked up in the containing scope. */
10643 pop_p = push_scope (scope);
10644 parser->in_declarator_p = true;
10645 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
10647 && (TREE_CODE (declarator) == SCOPE_REF
10648 || TREE_CODE (declarator) == IDENTIFIER_NODE)))
10649 /* Default args are only allowed on function
10651 parser->default_arg_ok_p = saved_default_arg_ok_p;
10653 parser->default_arg_ok_p = false;
10662 /* For an abstract declarator, we might wind up with nothing at this
10663 point. That's an error; the declarator is not optional. */
10665 cp_parser_error (parser, "expected declarator");
10667 /* If we entered a scope, we must exit it now. */
10671 parser->default_arg_ok_p = saved_default_arg_ok_p;
10672 parser->in_declarator_p = saved_in_declarator_p;
10677 /* Parse a ptr-operator.
10680 * cv-qualifier-seq [opt]
10682 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
10687 & cv-qualifier-seq [opt]
10689 Returns INDIRECT_REF if a pointer, or pointer-to-member, was
10690 used. Returns ADDR_EXPR if a reference was used. In the
10691 case of a pointer-to-member, *TYPE is filled in with the
10692 TYPE containing the member. *CV_QUALIFIER_SEQ is filled in
10693 with the cv-qualifier-seq, or NULL_TREE, if there are no
10694 cv-qualifiers. Returns ERROR_MARK if an error occurred. */
10696 static enum tree_code
10697 cp_parser_ptr_operator (cp_parser* parser,
10699 tree* cv_qualifier_seq)
10701 enum tree_code code = ERROR_MARK;
10704 /* Assume that it's not a pointer-to-member. */
10706 /* And that there are no cv-qualifiers. */
10707 *cv_qualifier_seq = NULL_TREE;
10709 /* Peek at the next token. */
10710 token = cp_lexer_peek_token (parser->lexer);
10711 /* If it's a `*' or `&' we have a pointer or reference. */
10712 if (token->type == CPP_MULT || token->type == CPP_AND)
10714 /* Remember which ptr-operator we were processing. */
10715 code = (token->type == CPP_AND ? ADDR_EXPR : INDIRECT_REF);
10717 /* Consume the `*' or `&'. */
10718 cp_lexer_consume_token (parser->lexer);
10720 /* A `*' can be followed by a cv-qualifier-seq, and so can a
10721 `&', if we are allowing GNU extensions. (The only qualifier
10722 that can legally appear after `&' is `restrict', but that is
10723 enforced during semantic analysis. */
10724 if (code == INDIRECT_REF
10725 || cp_parser_allow_gnu_extensions_p (parser))
10726 *cv_qualifier_seq = cp_parser_cv_qualifier_seq_opt (parser);
10730 /* Try the pointer-to-member case. */
10731 cp_parser_parse_tentatively (parser);
10732 /* Look for the optional `::' operator. */
10733 cp_parser_global_scope_opt (parser,
10734 /*current_scope_valid_p=*/false);
10735 /* Look for the nested-name specifier. */
10736 cp_parser_nested_name_specifier (parser,
10737 /*typename_keyword_p=*/false,
10738 /*check_dependency_p=*/true,
10740 /*is_declaration=*/false);
10741 /* If we found it, and the next token is a `*', then we are
10742 indeed looking at a pointer-to-member operator. */
10743 if (!cp_parser_error_occurred (parser)
10744 && cp_parser_require (parser, CPP_MULT, "`*'"))
10746 /* The type of which the member is a member is given by the
10748 *type = parser->scope;
10749 /* The next name will not be qualified. */
10750 parser->scope = NULL_TREE;
10751 parser->qualifying_scope = NULL_TREE;
10752 parser->object_scope = NULL_TREE;
10753 /* Indicate that the `*' operator was used. */
10754 code = INDIRECT_REF;
10755 /* Look for the optional cv-qualifier-seq. */
10756 *cv_qualifier_seq = cp_parser_cv_qualifier_seq_opt (parser);
10758 /* If that didn't work we don't have a ptr-operator. */
10759 if (!cp_parser_parse_definitely (parser))
10760 cp_parser_error (parser, "expected ptr-operator");
10766 /* Parse an (optional) cv-qualifier-seq.
10769 cv-qualifier cv-qualifier-seq [opt]
10771 Returns a TREE_LIST. The TREE_VALUE of each node is the
10772 representation of a cv-qualifier. */
10775 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
10777 tree cv_qualifiers = NULL_TREE;
10783 /* Look for the next cv-qualifier. */
10784 cv_qualifier = cp_parser_cv_qualifier_opt (parser);
10785 /* If we didn't find one, we're done. */
10789 /* Add this cv-qualifier to the list. */
10791 = tree_cons (NULL_TREE, cv_qualifier, cv_qualifiers);
10794 /* We built up the list in reverse order. */
10795 return nreverse (cv_qualifiers);
10798 /* Parse an (optional) cv-qualifier.
10810 cp_parser_cv_qualifier_opt (cp_parser* parser)
10813 tree cv_qualifier = NULL_TREE;
10815 /* Peek at the next token. */
10816 token = cp_lexer_peek_token (parser->lexer);
10817 /* See if it's a cv-qualifier. */
10818 switch (token->keyword)
10823 /* Save the value of the token. */
10824 cv_qualifier = token->value;
10825 /* Consume the token. */
10826 cp_lexer_consume_token (parser->lexer);
10833 return cv_qualifier;
10836 /* Parse a declarator-id.
10840 :: [opt] nested-name-specifier [opt] type-name
10842 In the `id-expression' case, the value returned is as for
10843 cp_parser_id_expression if the id-expression was an unqualified-id.
10844 If the id-expression was a qualified-id, then a SCOPE_REF is
10845 returned. The first operand is the scope (either a NAMESPACE_DECL
10846 or TREE_TYPE), but the second is still just a representation of an
10850 cp_parser_declarator_id (cp_parser* parser)
10852 tree id_expression;
10854 /* The expression must be an id-expression. Assume that qualified
10855 names are the names of types so that:
10858 int S<T>::R::i = 3;
10860 will work; we must treat `S<T>::R' as the name of a type.
10861 Similarly, assume that qualified names are templates, where
10865 int S<T>::R<T>::i = 3;
10868 id_expression = cp_parser_id_expression (parser,
10869 /*template_keyword_p=*/false,
10870 /*check_dependency_p=*/false,
10871 /*template_p=*/NULL,
10872 /*declarator_p=*/true);
10873 /* If the name was qualified, create a SCOPE_REF to represent
10877 id_expression = build_nt (SCOPE_REF, parser->scope, id_expression);
10878 parser->scope = NULL_TREE;
10881 return id_expression;
10884 /* Parse a type-id.
10887 type-specifier-seq abstract-declarator [opt]
10889 Returns the TYPE specified. */
10892 cp_parser_type_id (cp_parser* parser)
10894 tree type_specifier_seq;
10895 tree abstract_declarator;
10897 /* Parse the type-specifier-seq. */
10899 = cp_parser_type_specifier_seq (parser);
10900 if (type_specifier_seq == error_mark_node)
10901 return error_mark_node;
10903 /* There might or might not be an abstract declarator. */
10904 cp_parser_parse_tentatively (parser);
10905 /* Look for the declarator. */
10906 abstract_declarator
10907 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
10908 /*parenthesized_p=*/NULL);
10909 /* Check to see if there really was a declarator. */
10910 if (!cp_parser_parse_definitely (parser))
10911 abstract_declarator = NULL_TREE;
10913 return groktypename (build_tree_list (type_specifier_seq,
10914 abstract_declarator));
10917 /* Parse a type-specifier-seq.
10919 type-specifier-seq:
10920 type-specifier type-specifier-seq [opt]
10924 type-specifier-seq:
10925 attributes type-specifier-seq [opt]
10927 Returns a TREE_LIST. Either the TREE_VALUE of each node is a
10928 type-specifier, or the TREE_PURPOSE is a list of attributes. */
10931 cp_parser_type_specifier_seq (cp_parser* parser)
10933 bool seen_type_specifier = false;
10934 tree type_specifier_seq = NULL_TREE;
10936 /* Parse the type-specifiers and attributes. */
10939 tree type_specifier;
10941 /* Check for attributes first. */
10942 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
10944 type_specifier_seq = tree_cons (cp_parser_attributes_opt (parser),
10946 type_specifier_seq);
10950 /* After the first type-specifier, others are optional. */
10951 if (seen_type_specifier)
10952 cp_parser_parse_tentatively (parser);
10953 /* Look for the type-specifier. */
10954 type_specifier = cp_parser_type_specifier (parser,
10955 CP_PARSER_FLAGS_NONE,
10956 /*is_friend=*/false,
10957 /*is_declaration=*/false,
10960 /* If the first type-specifier could not be found, this is not a
10961 type-specifier-seq at all. */
10962 if (!seen_type_specifier && type_specifier == error_mark_node)
10963 return error_mark_node;
10964 /* If subsequent type-specifiers could not be found, the
10965 type-specifier-seq is complete. */
10966 else if (seen_type_specifier && !cp_parser_parse_definitely (parser))
10969 /* Add the new type-specifier to the list. */
10971 = tree_cons (NULL_TREE, type_specifier, type_specifier_seq);
10972 seen_type_specifier = true;
10975 /* We built up the list in reverse order. */
10976 return nreverse (type_specifier_seq);
10979 /* Parse a parameter-declaration-clause.
10981 parameter-declaration-clause:
10982 parameter-declaration-list [opt] ... [opt]
10983 parameter-declaration-list , ...
10985 Returns a representation for the parameter declarations. Each node
10986 is a TREE_LIST. (See cp_parser_parameter_declaration for the exact
10987 representation.) If the parameter-declaration-clause ends with an
10988 ellipsis, PARMLIST_ELLIPSIS_P will hold of the first node in the
10989 list. A return value of NULL_TREE indicates a
10990 parameter-declaration-clause consisting only of an ellipsis. */
10993 cp_parser_parameter_declaration_clause (cp_parser* parser)
10999 /* Peek at the next token. */
11000 token = cp_lexer_peek_token (parser->lexer);
11001 /* Check for trivial parameter-declaration-clauses. */
11002 if (token->type == CPP_ELLIPSIS)
11004 /* Consume the `...' token. */
11005 cp_lexer_consume_token (parser->lexer);
11008 else if (token->type == CPP_CLOSE_PAREN)
11009 /* There are no parameters. */
11011 #ifndef NO_IMPLICIT_EXTERN_C
11012 if (in_system_header && current_class_type == NULL
11013 && current_lang_name == lang_name_c)
11017 return void_list_node;
11019 /* Check for `(void)', too, which is a special case. */
11020 else if (token->keyword == RID_VOID
11021 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
11022 == CPP_CLOSE_PAREN))
11024 /* Consume the `void' token. */
11025 cp_lexer_consume_token (parser->lexer);
11026 /* There are no parameters. */
11027 return void_list_node;
11030 /* Parse the parameter-declaration-list. */
11031 parameters = cp_parser_parameter_declaration_list (parser);
11032 /* If a parse error occurred while parsing the
11033 parameter-declaration-list, then the entire
11034 parameter-declaration-clause is erroneous. */
11035 if (parameters == error_mark_node)
11036 return error_mark_node;
11038 /* Peek at the next token. */
11039 token = cp_lexer_peek_token (parser->lexer);
11040 /* If it's a `,', the clause should terminate with an ellipsis. */
11041 if (token->type == CPP_COMMA)
11043 /* Consume the `,'. */
11044 cp_lexer_consume_token (parser->lexer);
11045 /* Expect an ellipsis. */
11047 = (cp_parser_require (parser, CPP_ELLIPSIS, "`...'") != NULL);
11049 /* It might also be `...' if the optional trailing `,' was
11051 else if (token->type == CPP_ELLIPSIS)
11053 /* Consume the `...' token. */
11054 cp_lexer_consume_token (parser->lexer);
11055 /* And remember that we saw it. */
11059 ellipsis_p = false;
11061 /* Finish the parameter list. */
11062 return finish_parmlist (parameters, ellipsis_p);
11065 /* Parse a parameter-declaration-list.
11067 parameter-declaration-list:
11068 parameter-declaration
11069 parameter-declaration-list , parameter-declaration
11071 Returns a representation of the parameter-declaration-list, as for
11072 cp_parser_parameter_declaration_clause. However, the
11073 `void_list_node' is never appended to the list. */
11076 cp_parser_parameter_declaration_list (cp_parser* parser)
11078 tree parameters = NULL_TREE;
11080 /* Look for more parameters. */
11084 bool parenthesized_p;
11085 /* Parse the parameter. */
11087 = cp_parser_parameter_declaration (parser,
11088 /*template_parm_p=*/false,
11091 /* If a parse error occurred parsing the parameter declaration,
11092 then the entire parameter-declaration-list is erroneous. */
11093 if (parameter == error_mark_node)
11095 parameters = error_mark_node;
11098 /* Add the new parameter to the list. */
11099 TREE_CHAIN (parameter) = parameters;
11100 parameters = parameter;
11102 /* Peek at the next token. */
11103 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
11104 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11105 /* The parameter-declaration-list is complete. */
11107 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
11111 /* Peek at the next token. */
11112 token = cp_lexer_peek_nth_token (parser->lexer, 2);
11113 /* If it's an ellipsis, then the list is complete. */
11114 if (token->type == CPP_ELLIPSIS)
11116 /* Otherwise, there must be more parameters. Consume the
11118 cp_lexer_consume_token (parser->lexer);
11119 /* When parsing something like:
11121 int i(float f, double d)
11123 we can tell after seeing the declaration for "f" that we
11124 are not looking at an initialization of a variable "i",
11125 but rather at the declaration of a function "i".
11127 Due to the fact that the parsing of template arguments
11128 (as specified to a template-id) requires backtracking we
11129 cannot use this technique when inside a template argument
11131 if (!parser->in_template_argument_list_p
11132 && !parser->in_type_id_in_expr_p
11133 && cp_parser_parsing_tentatively (parser)
11134 && !cp_parser_committed_to_tentative_parse (parser)
11135 /* However, a parameter-declaration of the form
11136 "foat(f)" (which is a valid declaration of a
11137 parameter "f") can also be interpreted as an
11138 expression (the conversion of "f" to "float"). */
11139 && !parenthesized_p)
11140 cp_parser_commit_to_tentative_parse (parser);
11144 cp_parser_error (parser, "expected `,' or `...'");
11145 if (!cp_parser_parsing_tentatively (parser)
11146 || cp_parser_committed_to_tentative_parse (parser))
11147 cp_parser_skip_to_closing_parenthesis (parser,
11148 /*recovering=*/true,
11149 /*or_comma=*/false,
11150 /*consume_paren=*/false);
11155 /* We built up the list in reverse order; straighten it out now. */
11156 return nreverse (parameters);
11159 /* Parse a parameter declaration.
11161 parameter-declaration:
11162 decl-specifier-seq declarator
11163 decl-specifier-seq declarator = assignment-expression
11164 decl-specifier-seq abstract-declarator [opt]
11165 decl-specifier-seq abstract-declarator [opt] = assignment-expression
11167 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
11168 declares a template parameter. (In that case, a non-nested `>'
11169 token encountered during the parsing of the assignment-expression
11170 is not interpreted as a greater-than operator.)
11172 Returns a TREE_LIST representing the parameter-declaration. The
11173 TREE_PURPOSE is the default argument expression, or NULL_TREE if
11174 there is no default argument. The TREE_VALUE is a representation
11175 of the decl-specifier-seq and declarator. In particular, the
11176 TREE_VALUE will be a TREE_LIST whose TREE_PURPOSE represents the
11177 decl-specifier-seq and whose TREE_VALUE represents the declarator.
11178 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
11179 the declarator is of the form "(p)". */
11182 cp_parser_parameter_declaration (cp_parser *parser,
11183 bool template_parm_p,
11184 bool *parenthesized_p)
11186 int declares_class_or_enum;
11187 bool greater_than_is_operator_p;
11188 tree decl_specifiers;
11191 tree default_argument;
11194 const char *saved_message;
11196 /* In a template parameter, `>' is not an operator.
11200 When parsing a default template-argument for a non-type
11201 template-parameter, the first non-nested `>' is taken as the end
11202 of the template parameter-list rather than a greater-than
11204 greater_than_is_operator_p = !template_parm_p;
11206 /* Type definitions may not appear in parameter types. */
11207 saved_message = parser->type_definition_forbidden_message;
11208 parser->type_definition_forbidden_message
11209 = "types may not be defined in parameter types";
11211 /* Parse the declaration-specifiers. */
11213 = cp_parser_decl_specifier_seq (parser,
11214 CP_PARSER_FLAGS_NONE,
11216 &declares_class_or_enum);
11217 /* If an error occurred, there's no reason to attempt to parse the
11218 rest of the declaration. */
11219 if (cp_parser_error_occurred (parser))
11221 parser->type_definition_forbidden_message = saved_message;
11222 return error_mark_node;
11225 /* Peek at the next token. */
11226 token = cp_lexer_peek_token (parser->lexer);
11227 /* If the next token is a `)', `,', `=', `>', or `...', then there
11228 is no declarator. */
11229 if (token->type == CPP_CLOSE_PAREN
11230 || token->type == CPP_COMMA
11231 || token->type == CPP_EQ
11232 || token->type == CPP_ELLIPSIS
11233 || token->type == CPP_GREATER)
11235 declarator = NULL_TREE;
11236 if (parenthesized_p)
11237 *parenthesized_p = false;
11239 /* Otherwise, there should be a declarator. */
11242 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
11243 parser->default_arg_ok_p = false;
11245 /* After seeing a decl-specifier-seq, if the next token is not a
11246 "(", there is no possibility that the code is a valid
11247 expression. Therefore, if parsing tentatively, we commit at
11249 if (!parser->in_template_argument_list_p
11250 /* In an expression context, having seen:
11254 we cannot be sure whether we are looking at a
11255 function-type (taking a "char" as a parameter) or a cast
11256 of some object of type "char" to "int". */
11257 && !parser->in_type_id_in_expr_p
11258 && cp_parser_parsing_tentatively (parser)
11259 && !cp_parser_committed_to_tentative_parse (parser)
11260 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
11261 cp_parser_commit_to_tentative_parse (parser);
11262 /* Parse the declarator. */
11263 declarator = cp_parser_declarator (parser,
11264 CP_PARSER_DECLARATOR_EITHER,
11265 /*ctor_dtor_or_conv_p=*/NULL,
11267 parser->default_arg_ok_p = saved_default_arg_ok_p;
11268 /* After the declarator, allow more attributes. */
11269 attributes = chainon (attributes, cp_parser_attributes_opt (parser));
11272 /* The restriction on defining new types applies only to the type
11273 of the parameter, not to the default argument. */
11274 parser->type_definition_forbidden_message = saved_message;
11276 /* If the next token is `=', then process a default argument. */
11277 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11279 bool saved_greater_than_is_operator_p;
11280 /* Consume the `='. */
11281 cp_lexer_consume_token (parser->lexer);
11283 /* If we are defining a class, then the tokens that make up the
11284 default argument must be saved and processed later. */
11285 if (!template_parm_p && at_class_scope_p ()
11286 && TYPE_BEING_DEFINED (current_class_type))
11288 unsigned depth = 0;
11290 /* Create a DEFAULT_ARG to represented the unparsed default
11292 default_argument = make_node (DEFAULT_ARG);
11293 DEFARG_TOKENS (default_argument) = cp_token_cache_new ();
11295 /* Add tokens until we have processed the entire default
11302 /* Peek at the next token. */
11303 token = cp_lexer_peek_token (parser->lexer);
11304 /* What we do depends on what token we have. */
11305 switch (token->type)
11307 /* In valid code, a default argument must be
11308 immediately followed by a `,' `)', or `...'. */
11310 case CPP_CLOSE_PAREN:
11312 /* If we run into a non-nested `;', `}', or `]',
11313 then the code is invalid -- but the default
11314 argument is certainly over. */
11315 case CPP_SEMICOLON:
11316 case CPP_CLOSE_BRACE:
11317 case CPP_CLOSE_SQUARE:
11320 /* Update DEPTH, if necessary. */
11321 else if (token->type == CPP_CLOSE_PAREN
11322 || token->type == CPP_CLOSE_BRACE
11323 || token->type == CPP_CLOSE_SQUARE)
11327 case CPP_OPEN_PAREN:
11328 case CPP_OPEN_SQUARE:
11329 case CPP_OPEN_BRACE:
11334 /* If we see a non-nested `>', and `>' is not an
11335 operator, then it marks the end of the default
11337 if (!depth && !greater_than_is_operator_p)
11341 /* If we run out of tokens, issue an error message. */
11343 error ("file ends in default argument");
11349 /* In these cases, we should look for template-ids.
11350 For example, if the default argument is
11351 `X<int, double>()', we need to do name lookup to
11352 figure out whether or not `X' is a template; if
11353 so, the `,' does not end the default argument.
11355 That is not yet done. */
11362 /* If we've reached the end, stop. */
11366 /* Add the token to the token block. */
11367 token = cp_lexer_consume_token (parser->lexer);
11368 cp_token_cache_push_token (DEFARG_TOKENS (default_argument),
11372 /* Outside of a class definition, we can just parse the
11373 assignment-expression. */
11376 bool saved_local_variables_forbidden_p;
11378 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
11380 saved_greater_than_is_operator_p
11381 = parser->greater_than_is_operator_p;
11382 parser->greater_than_is_operator_p = greater_than_is_operator_p;
11383 /* Local variable names (and the `this' keyword) may not
11384 appear in a default argument. */
11385 saved_local_variables_forbidden_p
11386 = parser->local_variables_forbidden_p;
11387 parser->local_variables_forbidden_p = true;
11388 /* Parse the assignment-expression. */
11389 default_argument = cp_parser_assignment_expression (parser);
11390 /* Restore saved state. */
11391 parser->greater_than_is_operator_p
11392 = saved_greater_than_is_operator_p;
11393 parser->local_variables_forbidden_p
11394 = saved_local_variables_forbidden_p;
11396 if (!parser->default_arg_ok_p)
11398 if (!flag_pedantic_errors)
11399 warning ("deprecated use of default argument for parameter of non-function");
11402 error ("default arguments are only permitted for function parameters");
11403 default_argument = NULL_TREE;
11408 default_argument = NULL_TREE;
11410 /* Create the representation of the parameter. */
11412 decl_specifiers = tree_cons (attributes, NULL_TREE, decl_specifiers);
11413 parameter = build_tree_list (default_argument,
11414 build_tree_list (decl_specifiers,
11420 /* Parse a function-body.
11423 compound_statement */
11426 cp_parser_function_body (cp_parser *parser)
11428 cp_parser_compound_statement (parser, false);
11431 /* Parse a ctor-initializer-opt followed by a function-body. Return
11432 true if a ctor-initializer was present. */
11435 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
11438 bool ctor_initializer_p;
11440 /* Begin the function body. */
11441 body = begin_function_body ();
11442 /* Parse the optional ctor-initializer. */
11443 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
11444 /* Parse the function-body. */
11445 cp_parser_function_body (parser);
11446 /* Finish the function body. */
11447 finish_function_body (body);
11449 return ctor_initializer_p;
11452 /* Parse an initializer.
11455 = initializer-clause
11456 ( expression-list )
11458 Returns a expression representing the initializer. If no
11459 initializer is present, NULL_TREE is returned.
11461 *IS_PARENTHESIZED_INIT is set to TRUE if the `( expression-list )'
11462 production is used, and zero otherwise. *IS_PARENTHESIZED_INIT is
11463 set to FALSE if there is no initializer present. If there is an
11464 initializer, and it is not a constant-expression, *NON_CONSTANT_P
11465 is set to true; otherwise it is set to false. */
11468 cp_parser_initializer (cp_parser* parser, bool* is_parenthesized_init,
11469 bool* non_constant_p)
11474 /* Peek at the next token. */
11475 token = cp_lexer_peek_token (parser->lexer);
11477 /* Let our caller know whether or not this initializer was
11479 *is_parenthesized_init = (token->type == CPP_OPEN_PAREN);
11480 /* Assume that the initializer is constant. */
11481 *non_constant_p = false;
11483 if (token->type == CPP_EQ)
11485 /* Consume the `='. */
11486 cp_lexer_consume_token (parser->lexer);
11487 /* Parse the initializer-clause. */
11488 init = cp_parser_initializer_clause (parser, non_constant_p);
11490 else if (token->type == CPP_OPEN_PAREN)
11491 init = cp_parser_parenthesized_expression_list (parser, false,
11495 /* Anything else is an error. */
11496 cp_parser_error (parser, "expected initializer");
11497 init = error_mark_node;
11503 /* Parse an initializer-clause.
11505 initializer-clause:
11506 assignment-expression
11507 { initializer-list , [opt] }
11510 Returns an expression representing the initializer.
11512 If the `assignment-expression' production is used the value
11513 returned is simply a representation for the expression.
11515 Otherwise, a CONSTRUCTOR is returned. The CONSTRUCTOR_ELTS will be
11516 the elements of the initializer-list (or NULL_TREE, if the last
11517 production is used). The TREE_TYPE for the CONSTRUCTOR will be
11518 NULL_TREE. There is no way to detect whether or not the optional
11519 trailing `,' was provided. NON_CONSTANT_P is as for
11520 cp_parser_initializer. */
11523 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
11527 /* If it is not a `{', then we are looking at an
11528 assignment-expression. */
11529 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
11532 = cp_parser_constant_expression (parser,
11533 /*allow_non_constant_p=*/true,
11535 if (!*non_constant_p)
11536 initializer = fold_non_dependent_expr (initializer);
11540 /* Consume the `{' token. */
11541 cp_lexer_consume_token (parser->lexer);
11542 /* Create a CONSTRUCTOR to represent the braced-initializer. */
11543 initializer = make_node (CONSTRUCTOR);
11544 /* Mark it with TREE_HAS_CONSTRUCTOR. This should not be
11545 necessary, but check_initializer depends upon it, for
11547 TREE_HAS_CONSTRUCTOR (initializer) = 1;
11548 /* If it's not a `}', then there is a non-trivial initializer. */
11549 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
11551 /* Parse the initializer list. */
11552 CONSTRUCTOR_ELTS (initializer)
11553 = cp_parser_initializer_list (parser, non_constant_p);
11554 /* A trailing `,' token is allowed. */
11555 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
11556 cp_lexer_consume_token (parser->lexer);
11558 /* Now, there should be a trailing `}'. */
11559 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
11562 return initializer;
11565 /* Parse an initializer-list.
11569 initializer-list , initializer-clause
11574 identifier : initializer-clause
11575 initializer-list, identifier : initializer-clause
11577 Returns a TREE_LIST. The TREE_VALUE of each node is an expression
11578 for the initializer. If the TREE_PURPOSE is non-NULL, it is the
11579 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
11580 as for cp_parser_initializer. */
11583 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
11585 tree initializers = NULL_TREE;
11587 /* Assume all of the expressions are constant. */
11588 *non_constant_p = false;
11590 /* Parse the rest of the list. */
11596 bool clause_non_constant_p;
11598 /* If the next token is an identifier and the following one is a
11599 colon, we are looking at the GNU designated-initializer
11601 if (cp_parser_allow_gnu_extensions_p (parser)
11602 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
11603 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
11605 /* Consume the identifier. */
11606 identifier = cp_lexer_consume_token (parser->lexer)->value;
11607 /* Consume the `:'. */
11608 cp_lexer_consume_token (parser->lexer);
11611 identifier = NULL_TREE;
11613 /* Parse the initializer. */
11614 initializer = cp_parser_initializer_clause (parser,
11615 &clause_non_constant_p);
11616 /* If any clause is non-constant, so is the entire initializer. */
11617 if (clause_non_constant_p)
11618 *non_constant_p = true;
11619 /* Add it to the list. */
11620 initializers = tree_cons (identifier, initializer, initializers);
11622 /* If the next token is not a comma, we have reached the end of
11624 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11627 /* Peek at the next token. */
11628 token = cp_lexer_peek_nth_token (parser->lexer, 2);
11629 /* If the next token is a `}', then we're still done. An
11630 initializer-clause can have a trailing `,' after the
11631 initializer-list and before the closing `}'. */
11632 if (token->type == CPP_CLOSE_BRACE)
11635 /* Consume the `,' token. */
11636 cp_lexer_consume_token (parser->lexer);
11639 /* The initializers were built up in reverse order, so we need to
11640 reverse them now. */
11641 return nreverse (initializers);
11644 /* Classes [gram.class] */
11646 /* Parse a class-name.
11652 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
11653 to indicate that names looked up in dependent types should be
11654 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
11655 keyword has been used to indicate that the name that appears next
11656 is a template. TYPE_P is true iff the next name should be treated
11657 as class-name, even if it is declared to be some other kind of name
11658 as well. If CHECK_DEPENDENCY_P is FALSE, names are looked up in
11659 dependent scopes. If CLASS_HEAD_P is TRUE, this class is the class
11660 being defined in a class-head.
11662 Returns the TYPE_DECL representing the class. */
11665 cp_parser_class_name (cp_parser *parser,
11666 bool typename_keyword_p,
11667 bool template_keyword_p,
11669 bool check_dependency_p,
11671 bool is_declaration)
11678 /* All class-names start with an identifier. */
11679 token = cp_lexer_peek_token (parser->lexer);
11680 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
11682 cp_parser_error (parser, "expected class-name");
11683 return error_mark_node;
11686 /* PARSER->SCOPE can be cleared when parsing the template-arguments
11687 to a template-id, so we save it here. */
11688 scope = parser->scope;
11689 if (scope == error_mark_node)
11690 return error_mark_node;
11692 /* Any name names a type if we're following the `typename' keyword
11693 in a qualified name where the enclosing scope is type-dependent. */
11694 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
11695 && dependent_type_p (scope));
11696 /* Handle the common case (an identifier, but not a template-id)
11698 if (token->type == CPP_NAME
11699 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
11703 /* Look for the identifier. */
11704 identifier = cp_parser_identifier (parser);
11705 /* If the next token isn't an identifier, we are certainly not
11706 looking at a class-name. */
11707 if (identifier == error_mark_node)
11708 decl = error_mark_node;
11709 /* If we know this is a type-name, there's no need to look it
11711 else if (typename_p)
11715 /* If the next token is a `::', then the name must be a type
11718 [basic.lookup.qual]
11720 During the lookup for a name preceding the :: scope
11721 resolution operator, object, function, and enumerator
11722 names are ignored. */
11723 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
11725 /* Look up the name. */
11726 decl = cp_parser_lookup_name (parser, identifier,
11728 /*is_template=*/false,
11729 /*is_namespace=*/false,
11730 check_dependency_p);
11735 /* Try a template-id. */
11736 decl = cp_parser_template_id (parser, template_keyword_p,
11737 check_dependency_p,
11739 if (decl == error_mark_node)
11740 return error_mark_node;
11743 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
11745 /* If this is a typename, create a TYPENAME_TYPE. */
11746 if (typename_p && decl != error_mark_node)
11748 decl = make_typename_type (scope, decl, /*complain=*/1);
11749 if (decl != error_mark_node)
11750 decl = TYPE_NAME (decl);
11753 /* Check to see that it is really the name of a class. */
11754 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
11755 && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
11756 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
11757 /* Situations like this:
11759 template <typename T> struct A {
11760 typename T::template X<int>::I i;
11763 are problematic. Is `T::template X<int>' a class-name? The
11764 standard does not seem to be definitive, but there is no other
11765 valid interpretation of the following `::'. Therefore, those
11766 names are considered class-names. */
11767 decl = TYPE_NAME (make_typename_type (scope, decl, tf_error));
11768 else if (decl == error_mark_node
11769 || TREE_CODE (decl) != TYPE_DECL
11770 || !IS_AGGR_TYPE (TREE_TYPE (decl)))
11772 cp_parser_error (parser, "expected class-name");
11773 return error_mark_node;
11779 /* Parse a class-specifier.
11782 class-head { member-specification [opt] }
11784 Returns the TREE_TYPE representing the class. */
11787 cp_parser_class_specifier (cp_parser* parser)
11792 int has_trailing_semicolon;
11793 bool nested_name_specifier_p;
11794 unsigned saved_num_template_parameter_lists;
11795 bool pop_p = false;
11797 push_deferring_access_checks (dk_no_deferred);
11799 /* Parse the class-head. */
11800 type = cp_parser_class_head (parser,
11801 &nested_name_specifier_p,
11803 /* If the class-head was a semantic disaster, skip the entire body
11807 cp_parser_skip_to_end_of_block_or_statement (parser);
11808 pop_deferring_access_checks ();
11809 return error_mark_node;
11812 /* Look for the `{'. */
11813 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
11815 pop_deferring_access_checks ();
11816 return error_mark_node;
11819 /* Issue an error message if type-definitions are forbidden here. */
11820 cp_parser_check_type_definition (parser);
11821 /* Remember that we are defining one more class. */
11822 ++parser->num_classes_being_defined;
11823 /* Inside the class, surrounding template-parameter-lists do not
11825 saved_num_template_parameter_lists
11826 = parser->num_template_parameter_lists;
11827 parser->num_template_parameter_lists = 0;
11829 /* Start the class. */
11830 if (nested_name_specifier_p)
11831 pop_p = push_scope (CP_DECL_CONTEXT (TYPE_MAIN_DECL (type)));
11832 type = begin_class_definition (type);
11833 if (type == error_mark_node)
11834 /* If the type is erroneous, skip the entire body of the class. */
11835 cp_parser_skip_to_closing_brace (parser);
11837 /* Parse the member-specification. */
11838 cp_parser_member_specification_opt (parser);
11839 /* Look for the trailing `}'. */
11840 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
11841 /* We get better error messages by noticing a common problem: a
11842 missing trailing `;'. */
11843 token = cp_lexer_peek_token (parser->lexer);
11844 has_trailing_semicolon = (token->type == CPP_SEMICOLON);
11845 /* Look for trailing attributes to apply to this class. */
11846 if (cp_parser_allow_gnu_extensions_p (parser))
11848 tree sub_attr = cp_parser_attributes_opt (parser);
11849 attributes = chainon (attributes, sub_attr);
11851 if (type != error_mark_node)
11852 type = finish_struct (type, attributes);
11854 pop_scope (CP_DECL_CONTEXT (TYPE_MAIN_DECL (type)));
11855 /* If this class is not itself within the scope of another class,
11856 then we need to parse the bodies of all of the queued function
11857 definitions. Note that the queued functions defined in a class
11858 are not always processed immediately following the
11859 class-specifier for that class. Consider:
11862 struct B { void f() { sizeof (A); } };
11865 If `f' were processed before the processing of `A' were
11866 completed, there would be no way to compute the size of `A'.
11867 Note that the nesting we are interested in here is lexical --
11868 not the semantic nesting given by TYPE_CONTEXT. In particular,
11871 struct A { struct B; };
11872 struct A::B { void f() { } };
11874 there is no need to delay the parsing of `A::B::f'. */
11875 if (--parser->num_classes_being_defined == 0)
11880 /* In a first pass, parse default arguments to the functions.
11881 Then, in a second pass, parse the bodies of the functions.
11882 This two-phased approach handles cases like:
11890 for (TREE_PURPOSE (parser->unparsed_functions_queues)
11891 = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
11892 (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
11893 TREE_PURPOSE (parser->unparsed_functions_queues)
11894 = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
11896 fn = TREE_VALUE (queue_entry);
11897 /* Make sure that any template parameters are in scope. */
11898 maybe_begin_member_template_processing (fn);
11899 /* If there are default arguments that have not yet been processed,
11900 take care of them now. */
11901 cp_parser_late_parsing_default_args (parser, fn);
11902 /* Remove any template parameters from the symbol table. */
11903 maybe_end_member_template_processing ();
11905 /* Now parse the body of the functions. */
11906 for (TREE_VALUE (parser->unparsed_functions_queues)
11907 = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
11908 (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
11909 TREE_VALUE (parser->unparsed_functions_queues)
11910 = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
11912 /* Figure out which function we need to process. */
11913 fn = TREE_VALUE (queue_entry);
11915 /* A hack to prevent garbage collection. */
11918 /* Parse the function. */
11919 cp_parser_late_parsing_for_member (parser, fn);
11925 /* Put back any saved access checks. */
11926 pop_deferring_access_checks ();
11928 /* Restore the count of active template-parameter-lists. */
11929 parser->num_template_parameter_lists
11930 = saved_num_template_parameter_lists;
11935 /* Parse a class-head.
11938 class-key identifier [opt] base-clause [opt]
11939 class-key nested-name-specifier identifier base-clause [opt]
11940 class-key nested-name-specifier [opt] template-id
11944 class-key attributes identifier [opt] base-clause [opt]
11945 class-key attributes nested-name-specifier identifier base-clause [opt]
11946 class-key attributes nested-name-specifier [opt] template-id
11949 Returns the TYPE of the indicated class. Sets
11950 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
11951 involving a nested-name-specifier was used, and FALSE otherwise.
11953 Returns NULL_TREE if the class-head is syntactically valid, but
11954 semantically invalid in a way that means we should skip the entire
11955 body of the class. */
11958 cp_parser_class_head (cp_parser* parser,
11959 bool* nested_name_specifier_p,
11960 tree *attributes_p)
11963 tree nested_name_specifier;
11964 enum tag_types class_key;
11965 tree id = NULL_TREE;
11966 tree type = NULL_TREE;
11968 bool template_id_p = false;
11969 bool qualified_p = false;
11970 bool invalid_nested_name_p = false;
11971 bool invalid_explicit_specialization_p = false;
11972 bool pop_p = false;
11973 unsigned num_templates;
11975 /* Assume no nested-name-specifier will be present. */
11976 *nested_name_specifier_p = false;
11977 /* Assume no template parameter lists will be used in defining the
11981 /* Look for the class-key. */
11982 class_key = cp_parser_class_key (parser);
11983 if (class_key == none_type)
11984 return error_mark_node;
11986 /* Parse the attributes. */
11987 attributes = cp_parser_attributes_opt (parser);
11989 /* If the next token is `::', that is invalid -- but sometimes
11990 people do try to write:
11994 Handle this gracefully by accepting the extra qualifier, and then
11995 issuing an error about it later if this really is a
11996 class-head. If it turns out just to be an elaborated type
11997 specifier, remain silent. */
11998 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
11999 qualified_p = true;
12001 push_deferring_access_checks (dk_no_check);
12003 /* Determine the name of the class. Begin by looking for an
12004 optional nested-name-specifier. */
12005 nested_name_specifier
12006 = cp_parser_nested_name_specifier_opt (parser,
12007 /*typename_keyword_p=*/false,
12008 /*check_dependency_p=*/false,
12010 /*is_declaration=*/false);
12011 /* If there was a nested-name-specifier, then there *must* be an
12013 if (nested_name_specifier)
12015 /* Although the grammar says `identifier', it really means
12016 `class-name' or `template-name'. You are only allowed to
12017 define a class that has already been declared with this
12020 The proposed resolution for Core Issue 180 says that whever
12021 you see `class T::X' you should treat `X' as a type-name.
12023 It is OK to define an inaccessible class; for example:
12025 class A { class B; };
12028 We do not know if we will see a class-name, or a
12029 template-name. We look for a class-name first, in case the
12030 class-name is a template-id; if we looked for the
12031 template-name first we would stop after the template-name. */
12032 cp_parser_parse_tentatively (parser);
12033 type = cp_parser_class_name (parser,
12034 /*typename_keyword_p=*/false,
12035 /*template_keyword_p=*/false,
12037 /*check_dependency_p=*/false,
12038 /*class_head_p=*/true,
12039 /*is_declaration=*/false);
12040 /* If that didn't work, ignore the nested-name-specifier. */
12041 if (!cp_parser_parse_definitely (parser))
12043 invalid_nested_name_p = true;
12044 id = cp_parser_identifier (parser);
12045 if (id == error_mark_node)
12048 /* If we could not find a corresponding TYPE, treat this
12049 declaration like an unqualified declaration. */
12050 if (type == error_mark_node)
12051 nested_name_specifier = NULL_TREE;
12052 /* Otherwise, count the number of templates used in TYPE and its
12053 containing scopes. */
12058 for (scope = TREE_TYPE (type);
12059 scope && TREE_CODE (scope) != NAMESPACE_DECL;
12060 scope = (TYPE_P (scope)
12061 ? TYPE_CONTEXT (scope)
12062 : DECL_CONTEXT (scope)))
12064 && CLASS_TYPE_P (scope)
12065 && CLASSTYPE_TEMPLATE_INFO (scope)
12066 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
12067 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
12071 /* Otherwise, the identifier is optional. */
12074 /* We don't know whether what comes next is a template-id,
12075 an identifier, or nothing at all. */
12076 cp_parser_parse_tentatively (parser);
12077 /* Check for a template-id. */
12078 id = cp_parser_template_id (parser,
12079 /*template_keyword_p=*/false,
12080 /*check_dependency_p=*/true,
12081 /*is_declaration=*/true);
12082 /* If that didn't work, it could still be an identifier. */
12083 if (!cp_parser_parse_definitely (parser))
12085 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12086 id = cp_parser_identifier (parser);
12092 template_id_p = true;
12097 pop_deferring_access_checks ();
12099 cp_parser_check_for_invalid_template_id (parser, id);
12101 /* If it's not a `:' or a `{' then we can't really be looking at a
12102 class-head, since a class-head only appears as part of a
12103 class-specifier. We have to detect this situation before calling
12104 xref_tag, since that has irreversible side-effects. */
12105 if (!cp_parser_next_token_starts_class_definition_p (parser))
12107 cp_parser_error (parser, "expected `{' or `:'");
12108 return error_mark_node;
12111 /* At this point, we're going ahead with the class-specifier, even
12112 if some other problem occurs. */
12113 cp_parser_commit_to_tentative_parse (parser);
12114 /* Issue the error about the overly-qualified name now. */
12116 cp_parser_error (parser,
12117 "global qualification of class name is invalid");
12118 else if (invalid_nested_name_p)
12119 cp_parser_error (parser,
12120 "qualified name does not name a class");
12121 else if (nested_name_specifier)
12124 /* Figure out in what scope the declaration is being placed. */
12125 scope = current_scope ();
12127 scope = current_namespace;
12128 /* If that scope does not contain the scope in which the
12129 class was originally declared, the program is invalid. */
12130 if (scope && !is_ancestor (scope, nested_name_specifier))
12132 error ("declaration of `%D' in `%D' which does not "
12133 "enclose `%D'", type, scope, nested_name_specifier);
12139 A declarator-id shall not be qualified exception of the
12140 definition of a ... nested class outside of its class
12141 ... [or] a the definition or explicit instantiation of a
12142 class member of a namespace outside of its namespace. */
12143 if (scope == nested_name_specifier)
12145 pedwarn ("extra qualification ignored");
12146 nested_name_specifier = NULL_TREE;
12150 /* An explicit-specialization must be preceded by "template <>". If
12151 it is not, try to recover gracefully. */
12152 if (at_namespace_scope_p ()
12153 && parser->num_template_parameter_lists == 0
12156 error ("an explicit specialization must be preceded by 'template <>'");
12157 invalid_explicit_specialization_p = true;
12158 /* Take the same action that would have been taken by
12159 cp_parser_explicit_specialization. */
12160 ++parser->num_template_parameter_lists;
12161 begin_specialization ();
12163 /* There must be no "return" statements between this point and the
12164 end of this function; set "type "to the correct return value and
12165 use "goto done;" to return. */
12166 /* Make sure that the right number of template parameters were
12168 if (!cp_parser_check_template_parameters (parser, num_templates))
12170 /* If something went wrong, there is no point in even trying to
12171 process the class-definition. */
12176 /* Look up the type. */
12179 type = TREE_TYPE (id);
12180 maybe_process_partial_specialization (type);
12182 else if (!nested_name_specifier)
12184 /* If the class was unnamed, create a dummy name. */
12186 id = make_anon_name ();
12187 type = xref_tag (class_key, id, /*globalize=*/false,
12188 parser->num_template_parameter_lists);
12193 bool pop_p = false;
12197 template <typename T> struct S { struct T };
12198 template <typename T> struct S<T>::T { };
12200 we will get a TYPENAME_TYPE when processing the definition of
12201 `S::T'. We need to resolve it to the actual type before we
12202 try to define it. */
12203 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
12205 class_type = resolve_typename_type (TREE_TYPE (type),
12206 /*only_current_p=*/false);
12207 if (class_type != error_mark_node)
12208 type = TYPE_NAME (class_type);
12211 cp_parser_error (parser, "could not resolve typename type");
12212 type = error_mark_node;
12216 maybe_process_partial_specialization (TREE_TYPE (type));
12217 class_type = current_class_type;
12218 /* Enter the scope indicated by the nested-name-specifier. */
12219 if (nested_name_specifier)
12220 pop_p = push_scope (nested_name_specifier);
12221 /* Get the canonical version of this type. */
12222 type = TYPE_MAIN_DECL (TREE_TYPE (type));
12223 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
12224 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
12225 type = push_template_decl (type);
12226 type = TREE_TYPE (type);
12227 if (nested_name_specifier)
12229 *nested_name_specifier_p = true;
12231 pop_scope (nested_name_specifier);
12234 /* Indicate whether this class was declared as a `class' or as a
12236 if (TREE_CODE (type) == RECORD_TYPE)
12237 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
12238 cp_parser_check_class_key (class_key, type);
12240 /* Enter the scope containing the class; the names of base classes
12241 should be looked up in that context. For example, given:
12243 struct A { struct B {}; struct C; };
12244 struct A::C : B {};
12247 if (nested_name_specifier)
12248 pop_p = push_scope (nested_name_specifier);
12249 /* Now, look for the base-clause. */
12250 token = cp_lexer_peek_token (parser->lexer);
12251 if (token->type == CPP_COLON)
12255 /* Get the list of base-classes. */
12256 bases = cp_parser_base_clause (parser);
12257 /* Process them. */
12258 xref_basetypes (type, bases);
12260 /* Leave the scope given by the nested-name-specifier. We will
12261 enter the class scope itself while processing the members. */
12263 pop_scope (nested_name_specifier);
12266 if (invalid_explicit_specialization_p)
12268 end_specialization ();
12269 --parser->num_template_parameter_lists;
12271 *attributes_p = attributes;
12275 /* Parse a class-key.
12282 Returns the kind of class-key specified, or none_type to indicate
12285 static enum tag_types
12286 cp_parser_class_key (cp_parser* parser)
12289 enum tag_types tag_type;
12291 /* Look for the class-key. */
12292 token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
12296 /* Check to see if the TOKEN is a class-key. */
12297 tag_type = cp_parser_token_is_class_key (token);
12299 cp_parser_error (parser, "expected class-key");
12303 /* Parse an (optional) member-specification.
12305 member-specification:
12306 member-declaration member-specification [opt]
12307 access-specifier : member-specification [opt] */
12310 cp_parser_member_specification_opt (cp_parser* parser)
12317 /* Peek at the next token. */
12318 token = cp_lexer_peek_token (parser->lexer);
12319 /* If it's a `}', or EOF then we've seen all the members. */
12320 if (token->type == CPP_CLOSE_BRACE || token->type == CPP_EOF)
12323 /* See if this token is a keyword. */
12324 keyword = token->keyword;
12328 case RID_PROTECTED:
12330 /* Consume the access-specifier. */
12331 cp_lexer_consume_token (parser->lexer);
12332 /* Remember which access-specifier is active. */
12333 current_access_specifier = token->value;
12334 /* Look for the `:'. */
12335 cp_parser_require (parser, CPP_COLON, "`:'");
12339 /* Otherwise, the next construction must be a
12340 member-declaration. */
12341 cp_parser_member_declaration (parser);
12346 /* Parse a member-declaration.
12348 member-declaration:
12349 decl-specifier-seq [opt] member-declarator-list [opt] ;
12350 function-definition ; [opt]
12351 :: [opt] nested-name-specifier template [opt] unqualified-id ;
12353 template-declaration
12355 member-declarator-list:
12357 member-declarator-list , member-declarator
12360 declarator pure-specifier [opt]
12361 declarator constant-initializer [opt]
12362 identifier [opt] : constant-expression
12366 member-declaration:
12367 __extension__ member-declaration
12370 declarator attributes [opt] pure-specifier [opt]
12371 declarator attributes [opt] constant-initializer [opt]
12372 identifier [opt] attributes [opt] : constant-expression */
12375 cp_parser_member_declaration (cp_parser* parser)
12377 tree decl_specifiers;
12378 tree prefix_attributes;
12380 int declares_class_or_enum;
12383 int saved_pedantic;
12385 /* Check for the `__extension__' keyword. */
12386 if (cp_parser_extension_opt (parser, &saved_pedantic))
12389 cp_parser_member_declaration (parser);
12390 /* Restore the old value of the PEDANTIC flag. */
12391 pedantic = saved_pedantic;
12396 /* Check for a template-declaration. */
12397 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
12399 /* Parse the template-declaration. */
12400 cp_parser_template_declaration (parser, /*member_p=*/true);
12405 /* Check for a using-declaration. */
12406 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
12408 /* Parse the using-declaration. */
12409 cp_parser_using_declaration (parser);
12414 /* Parse the decl-specifier-seq. */
12416 = cp_parser_decl_specifier_seq (parser,
12417 CP_PARSER_FLAGS_OPTIONAL,
12418 &prefix_attributes,
12419 &declares_class_or_enum);
12420 /* Check for an invalid type-name. */
12421 if (cp_parser_parse_and_diagnose_invalid_type_name (parser))
12423 /* If there is no declarator, then the decl-specifier-seq should
12425 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12427 /* If there was no decl-specifier-seq, and the next token is a
12428 `;', then we have something like:
12434 Each member-declaration shall declare at least one member
12435 name of the class. */
12436 if (!decl_specifiers)
12439 pedwarn ("extra semicolon");
12445 /* See if this declaration is a friend. */
12446 friend_p = cp_parser_friend_p (decl_specifiers);
12447 /* If there were decl-specifiers, check to see if there was
12448 a class-declaration. */
12449 type = check_tag_decl (decl_specifiers);
12450 /* Nested classes have already been added to the class, but
12451 a `friend' needs to be explicitly registered. */
12454 /* If the `friend' keyword was present, the friend must
12455 be introduced with a class-key. */
12456 if (!declares_class_or_enum)
12457 error ("a class-key must be used when declaring a friend");
12460 template <typename T> struct A {
12461 friend struct A<T>::B;
12464 A<T>::B will be represented by a TYPENAME_TYPE, and
12465 therefore not recognized by check_tag_decl. */
12470 for (specifier = decl_specifiers;
12472 specifier = TREE_CHAIN (specifier))
12474 tree s = TREE_VALUE (specifier);
12476 if (TREE_CODE (s) == IDENTIFIER_NODE)
12477 get_global_value_if_present (s, &type);
12478 if (TREE_CODE (s) == TYPE_DECL)
12487 if (!type || !TYPE_P (type))
12488 error ("friend declaration does not name a class or "
12491 make_friend_class (current_class_type, type,
12492 /*complain=*/true);
12494 /* If there is no TYPE, an error message will already have
12498 /* An anonymous aggregate has to be handled specially; such
12499 a declaration really declares a data member (with a
12500 particular type), as opposed to a nested class. */
12501 else if (ANON_AGGR_TYPE_P (type))
12503 /* Remove constructors and such from TYPE, now that we
12504 know it is an anonymous aggregate. */
12505 fixup_anonymous_aggr (type);
12506 /* And make the corresponding data member. */
12507 decl = build_decl (FIELD_DECL, NULL_TREE, type);
12508 /* Add it to the class. */
12509 finish_member_declaration (decl);
12512 cp_parser_check_access_in_redeclaration (TYPE_NAME (type));
12517 /* See if these declarations will be friends. */
12518 friend_p = cp_parser_friend_p (decl_specifiers);
12520 /* Keep going until we hit the `;' at the end of the
12522 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12524 tree attributes = NULL_TREE;
12525 tree first_attribute;
12527 /* Peek at the next token. */
12528 token = cp_lexer_peek_token (parser->lexer);
12530 /* Check for a bitfield declaration. */
12531 if (token->type == CPP_COLON
12532 || (token->type == CPP_NAME
12533 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
12539 /* Get the name of the bitfield. Note that we cannot just
12540 check TOKEN here because it may have been invalidated by
12541 the call to cp_lexer_peek_nth_token above. */
12542 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
12543 identifier = cp_parser_identifier (parser);
12545 identifier = NULL_TREE;
12547 /* Consume the `:' token. */
12548 cp_lexer_consume_token (parser->lexer);
12549 /* Get the width of the bitfield. */
12551 = cp_parser_constant_expression (parser,
12552 /*allow_non_constant=*/false,
12555 /* Look for attributes that apply to the bitfield. */
12556 attributes = cp_parser_attributes_opt (parser);
12557 /* Remember which attributes are prefix attributes and
12559 first_attribute = attributes;
12560 /* Combine the attributes. */
12561 attributes = chainon (prefix_attributes, attributes);
12563 /* Create the bitfield declaration. */
12564 decl = grokbitfield (identifier,
12567 /* Apply the attributes. */
12568 cplus_decl_attributes (&decl, attributes, /*flags=*/0);
12574 tree asm_specification;
12575 int ctor_dtor_or_conv_p;
12577 /* Parse the declarator. */
12579 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
12580 &ctor_dtor_or_conv_p,
12581 /*parenthesized_p=*/NULL);
12583 /* If something went wrong parsing the declarator, make sure
12584 that we at least consume some tokens. */
12585 if (declarator == error_mark_node)
12587 /* Skip to the end of the statement. */
12588 cp_parser_skip_to_end_of_statement (parser);
12589 /* If the next token is not a semicolon, that is
12590 probably because we just skipped over the body of
12591 a function. So, we consume a semicolon if
12592 present, but do not issue an error message if it
12594 if (cp_lexer_next_token_is (parser->lexer,
12596 cp_lexer_consume_token (parser->lexer);
12600 cp_parser_check_for_definition_in_return_type
12601 (declarator, declares_class_or_enum);
12603 /* Look for an asm-specification. */
12604 asm_specification = cp_parser_asm_specification_opt (parser);
12605 /* Look for attributes that apply to the declaration. */
12606 attributes = cp_parser_attributes_opt (parser);
12607 /* Remember which attributes are prefix attributes and
12609 first_attribute = attributes;
12610 /* Combine the attributes. */
12611 attributes = chainon (prefix_attributes, attributes);
12613 /* If it's an `=', then we have a constant-initializer or a
12614 pure-specifier. It is not correct to parse the
12615 initializer before registering the member declaration
12616 since the member declaration should be in scope while
12617 its initializer is processed. However, the rest of the
12618 front end does not yet provide an interface that allows
12619 us to handle this correctly. */
12620 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12624 A pure-specifier shall be used only in the declaration of
12625 a virtual function.
12627 A member-declarator can contain a constant-initializer
12628 only if it declares a static member of integral or
12631 Therefore, if the DECLARATOR is for a function, we look
12632 for a pure-specifier; otherwise, we look for a
12633 constant-initializer. When we call `grokfield', it will
12634 perform more stringent semantics checks. */
12635 if (TREE_CODE (declarator) == CALL_EXPR)
12636 initializer = cp_parser_pure_specifier (parser);
12638 /* Parse the initializer. */
12639 initializer = cp_parser_constant_initializer (parser);
12641 /* Otherwise, there is no initializer. */
12643 initializer = NULL_TREE;
12645 /* See if we are probably looking at a function
12646 definition. We are certainly not looking at at a
12647 member-declarator. Calling `grokfield' has
12648 side-effects, so we must not do it unless we are sure
12649 that we are looking at a member-declarator. */
12650 if (cp_parser_token_starts_function_definition_p
12651 (cp_lexer_peek_token (parser->lexer)))
12653 /* The grammar does not allow a pure-specifier to be
12654 used when a member function is defined. (It is
12655 possible that this fact is an oversight in the
12656 standard, since a pure function may be defined
12657 outside of the class-specifier. */
12659 error ("pure-specifier on function-definition");
12660 decl = cp_parser_save_member_function_body (parser,
12664 /* If the member was not a friend, declare it here. */
12666 finish_member_declaration (decl);
12667 /* Peek at the next token. */
12668 token = cp_lexer_peek_token (parser->lexer);
12669 /* If the next token is a semicolon, consume it. */
12670 if (token->type == CPP_SEMICOLON)
12671 cp_lexer_consume_token (parser->lexer);
12676 /* Create the declaration. */
12677 decl = grokfield (declarator, decl_specifiers,
12678 initializer, asm_specification,
12680 /* Any initialization must have been from a
12681 constant-expression. */
12682 if (decl && TREE_CODE (decl) == VAR_DECL && initializer)
12683 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = 1;
12687 /* Reset PREFIX_ATTRIBUTES. */
12688 while (attributes && TREE_CHAIN (attributes) != first_attribute)
12689 attributes = TREE_CHAIN (attributes);
12691 TREE_CHAIN (attributes) = NULL_TREE;
12693 /* If there is any qualification still in effect, clear it
12694 now; we will be starting fresh with the next declarator. */
12695 parser->scope = NULL_TREE;
12696 parser->qualifying_scope = NULL_TREE;
12697 parser->object_scope = NULL_TREE;
12698 /* If it's a `,', then there are more declarators. */
12699 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
12700 cp_lexer_consume_token (parser->lexer);
12701 /* If the next token isn't a `;', then we have a parse error. */
12702 else if (cp_lexer_next_token_is_not (parser->lexer,
12705 cp_parser_error (parser, "expected `;'");
12706 /* Skip tokens until we find a `;'. */
12707 cp_parser_skip_to_end_of_statement (parser);
12714 /* Add DECL to the list of members. */
12716 finish_member_declaration (decl);
12718 if (TREE_CODE (decl) == FUNCTION_DECL)
12719 cp_parser_save_default_args (parser, decl);
12724 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
12727 /* Parse a pure-specifier.
12732 Returns INTEGER_ZERO_NODE if a pure specifier is found.
12733 Otherwise, ERROR_MARK_NODE is returned. */
12736 cp_parser_pure_specifier (cp_parser* parser)
12740 /* Look for the `=' token. */
12741 if (!cp_parser_require (parser, CPP_EQ, "`='"))
12742 return error_mark_node;
12743 /* Look for the `0' token. */
12744 token = cp_parser_require (parser, CPP_NUMBER, "`0'");
12745 /* Unfortunately, this will accept `0L' and `0x00' as well. We need
12746 to get information from the lexer about how the number was
12747 spelled in order to fix this problem. */
12748 if (!token || !integer_zerop (token->value))
12749 return error_mark_node;
12751 return integer_zero_node;
12754 /* Parse a constant-initializer.
12756 constant-initializer:
12757 = constant-expression
12759 Returns a representation of the constant-expression. */
12762 cp_parser_constant_initializer (cp_parser* parser)
12764 /* Look for the `=' token. */
12765 if (!cp_parser_require (parser, CPP_EQ, "`='"))
12766 return error_mark_node;
12768 /* It is invalid to write:
12770 struct S { static const int i = { 7 }; };
12773 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12775 cp_parser_error (parser,
12776 "a brace-enclosed initializer is not allowed here");
12777 /* Consume the opening brace. */
12778 cp_lexer_consume_token (parser->lexer);
12779 /* Skip the initializer. */
12780 cp_parser_skip_to_closing_brace (parser);
12781 /* Look for the trailing `}'. */
12782 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
12784 return error_mark_node;
12787 return cp_parser_constant_expression (parser,
12788 /*allow_non_constant=*/false,
12792 /* Derived classes [gram.class.derived] */
12794 /* Parse a base-clause.
12797 : base-specifier-list
12799 base-specifier-list:
12801 base-specifier-list , base-specifier
12803 Returns a TREE_LIST representing the base-classes, in the order in
12804 which they were declared. The representation of each node is as
12805 described by cp_parser_base_specifier.
12807 In the case that no bases are specified, this function will return
12808 NULL_TREE, not ERROR_MARK_NODE. */
12811 cp_parser_base_clause (cp_parser* parser)
12813 tree bases = NULL_TREE;
12815 /* Look for the `:' that begins the list. */
12816 cp_parser_require (parser, CPP_COLON, "`:'");
12818 /* Scan the base-specifier-list. */
12824 /* Look for the base-specifier. */
12825 base = cp_parser_base_specifier (parser);
12826 /* Add BASE to the front of the list. */
12827 if (base != error_mark_node)
12829 TREE_CHAIN (base) = bases;
12832 /* Peek at the next token. */
12833 token = cp_lexer_peek_token (parser->lexer);
12834 /* If it's not a comma, then the list is complete. */
12835 if (token->type != CPP_COMMA)
12837 /* Consume the `,'. */
12838 cp_lexer_consume_token (parser->lexer);
12841 /* PARSER->SCOPE may still be non-NULL at this point, if the last
12842 base class had a qualified name. However, the next name that
12843 appears is certainly not qualified. */
12844 parser->scope = NULL_TREE;
12845 parser->qualifying_scope = NULL_TREE;
12846 parser->object_scope = NULL_TREE;
12848 return nreverse (bases);
12851 /* Parse a base-specifier.
12854 :: [opt] nested-name-specifier [opt] class-name
12855 virtual access-specifier [opt] :: [opt] nested-name-specifier
12857 access-specifier virtual [opt] :: [opt] nested-name-specifier
12860 Returns a TREE_LIST. The TREE_PURPOSE will be one of
12861 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
12862 indicate the specifiers provided. The TREE_VALUE will be a TYPE
12863 (or the ERROR_MARK_NODE) indicating the type that was specified. */
12866 cp_parser_base_specifier (cp_parser* parser)
12870 bool virtual_p = false;
12871 bool duplicate_virtual_error_issued_p = false;
12872 bool duplicate_access_error_issued_p = false;
12873 bool class_scope_p, template_p;
12874 tree access = access_default_node;
12877 /* Process the optional `virtual' and `access-specifier'. */
12880 /* Peek at the next token. */
12881 token = cp_lexer_peek_token (parser->lexer);
12882 /* Process `virtual'. */
12883 switch (token->keyword)
12886 /* If `virtual' appears more than once, issue an error. */
12887 if (virtual_p && !duplicate_virtual_error_issued_p)
12889 cp_parser_error (parser,
12890 "`virtual' specified more than once in base-specified");
12891 duplicate_virtual_error_issued_p = true;
12896 /* Consume the `virtual' token. */
12897 cp_lexer_consume_token (parser->lexer);
12902 case RID_PROTECTED:
12904 /* If more than one access specifier appears, issue an
12906 if (access != access_default_node
12907 && !duplicate_access_error_issued_p)
12909 cp_parser_error (parser,
12910 "more than one access specifier in base-specified");
12911 duplicate_access_error_issued_p = true;
12914 access = ridpointers[(int) token->keyword];
12916 /* Consume the access-specifier. */
12917 cp_lexer_consume_token (parser->lexer);
12926 /* It is not uncommon to see programs mechanically, erroneously, use
12927 the 'typename' keyword to denote (dependent) qualified types
12928 as base classes. */
12929 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
12931 if (!processing_template_decl)
12932 error ("keyword `typename' not allowed outside of templates");
12934 error ("keyword `typename' not allowed in this context "
12935 "(the base class is implicitly a type)");
12936 cp_lexer_consume_token (parser->lexer);
12939 /* Look for the optional `::' operator. */
12940 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
12941 /* Look for the nested-name-specifier. The simplest way to
12946 The keyword `typename' is not permitted in a base-specifier or
12947 mem-initializer; in these contexts a qualified name that
12948 depends on a template-parameter is implicitly assumed to be a
12951 is to pretend that we have seen the `typename' keyword at this
12953 cp_parser_nested_name_specifier_opt (parser,
12954 /*typename_keyword_p=*/true,
12955 /*check_dependency_p=*/true,
12957 /*is_declaration=*/true);
12958 /* If the base class is given by a qualified name, assume that names
12959 we see are type names or templates, as appropriate. */
12960 class_scope_p = (parser->scope && TYPE_P (parser->scope));
12961 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
12963 /* Finally, look for the class-name. */
12964 type = cp_parser_class_name (parser,
12968 /*check_dependency_p=*/true,
12969 /*class_head_p=*/false,
12970 /*is_declaration=*/true);
12972 if (type == error_mark_node)
12973 return error_mark_node;
12975 return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
12978 /* Exception handling [gram.exception] */
12980 /* Parse an (optional) exception-specification.
12982 exception-specification:
12983 throw ( type-id-list [opt] )
12985 Returns a TREE_LIST representing the exception-specification. The
12986 TREE_VALUE of each node is a type. */
12989 cp_parser_exception_specification_opt (cp_parser* parser)
12994 /* Peek at the next token. */
12995 token = cp_lexer_peek_token (parser->lexer);
12996 /* If it's not `throw', then there's no exception-specification. */
12997 if (!cp_parser_is_keyword (token, RID_THROW))
13000 /* Consume the `throw'. */
13001 cp_lexer_consume_token (parser->lexer);
13003 /* Look for the `('. */
13004 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13006 /* Peek at the next token. */
13007 token = cp_lexer_peek_token (parser->lexer);
13008 /* If it's not a `)', then there is a type-id-list. */
13009 if (token->type != CPP_CLOSE_PAREN)
13011 const char *saved_message;
13013 /* Types may not be defined in an exception-specification. */
13014 saved_message = parser->type_definition_forbidden_message;
13015 parser->type_definition_forbidden_message
13016 = "types may not be defined in an exception-specification";
13017 /* Parse the type-id-list. */
13018 type_id_list = cp_parser_type_id_list (parser);
13019 /* Restore the saved message. */
13020 parser->type_definition_forbidden_message = saved_message;
13023 type_id_list = empty_except_spec;
13025 /* Look for the `)'. */
13026 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13028 return type_id_list;
13031 /* Parse an (optional) type-id-list.
13035 type-id-list , type-id
13037 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
13038 in the order that the types were presented. */
13041 cp_parser_type_id_list (cp_parser* parser)
13043 tree types = NULL_TREE;
13050 /* Get the next type-id. */
13051 type = cp_parser_type_id (parser);
13052 /* Add it to the list. */
13053 types = add_exception_specifier (types, type, /*complain=*/1);
13054 /* Peek at the next token. */
13055 token = cp_lexer_peek_token (parser->lexer);
13056 /* If it is not a `,', we are done. */
13057 if (token->type != CPP_COMMA)
13059 /* Consume the `,'. */
13060 cp_lexer_consume_token (parser->lexer);
13063 return nreverse (types);
13066 /* Parse a try-block.
13069 try compound-statement handler-seq */
13072 cp_parser_try_block (cp_parser* parser)
13076 cp_parser_require_keyword (parser, RID_TRY, "`try'");
13077 try_block = begin_try_block ();
13078 cp_parser_compound_statement (parser, false);
13079 finish_try_block (try_block);
13080 cp_parser_handler_seq (parser);
13081 finish_handler_sequence (try_block);
13086 /* Parse a function-try-block.
13088 function-try-block:
13089 try ctor-initializer [opt] function-body handler-seq */
13092 cp_parser_function_try_block (cp_parser* parser)
13095 bool ctor_initializer_p;
13097 /* Look for the `try' keyword. */
13098 if (!cp_parser_require_keyword (parser, RID_TRY, "`try'"))
13100 /* Let the rest of the front-end know where we are. */
13101 try_block = begin_function_try_block ();
13102 /* Parse the function-body. */
13104 = cp_parser_ctor_initializer_opt_and_function_body (parser);
13105 /* We're done with the `try' part. */
13106 finish_function_try_block (try_block);
13107 /* Parse the handlers. */
13108 cp_parser_handler_seq (parser);
13109 /* We're done with the handlers. */
13110 finish_function_handler_sequence (try_block);
13112 return ctor_initializer_p;
13115 /* Parse a handler-seq.
13118 handler handler-seq [opt] */
13121 cp_parser_handler_seq (cp_parser* parser)
13127 /* Parse the handler. */
13128 cp_parser_handler (parser);
13129 /* Peek at the next token. */
13130 token = cp_lexer_peek_token (parser->lexer);
13131 /* If it's not `catch' then there are no more handlers. */
13132 if (!cp_parser_is_keyword (token, RID_CATCH))
13137 /* Parse a handler.
13140 catch ( exception-declaration ) compound-statement */
13143 cp_parser_handler (cp_parser* parser)
13148 cp_parser_require_keyword (parser, RID_CATCH, "`catch'");
13149 handler = begin_handler ();
13150 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13151 declaration = cp_parser_exception_declaration (parser);
13152 finish_handler_parms (declaration, handler);
13153 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13154 cp_parser_compound_statement (parser, false);
13155 finish_handler (handler);
13158 /* Parse an exception-declaration.
13160 exception-declaration:
13161 type-specifier-seq declarator
13162 type-specifier-seq abstract-declarator
13166 Returns a VAR_DECL for the declaration, or NULL_TREE if the
13167 ellipsis variant is used. */
13170 cp_parser_exception_declaration (cp_parser* parser)
13172 tree type_specifiers;
13174 const char *saved_message;
13176 /* If it's an ellipsis, it's easy to handle. */
13177 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13179 /* Consume the `...' token. */
13180 cp_lexer_consume_token (parser->lexer);
13184 /* Types may not be defined in exception-declarations. */
13185 saved_message = parser->type_definition_forbidden_message;
13186 parser->type_definition_forbidden_message
13187 = "types may not be defined in exception-declarations";
13189 /* Parse the type-specifier-seq. */
13190 type_specifiers = cp_parser_type_specifier_seq (parser);
13191 /* If it's a `)', then there is no declarator. */
13192 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
13193 declarator = NULL_TREE;
13195 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
13196 /*ctor_dtor_or_conv_p=*/NULL,
13197 /*parenthesized_p=*/NULL);
13199 /* Restore the saved message. */
13200 parser->type_definition_forbidden_message = saved_message;
13202 return start_handler_parms (type_specifiers, declarator);
13205 /* Parse a throw-expression.
13208 throw assignment-expression [opt]
13210 Returns a THROW_EXPR representing the throw-expression. */
13213 cp_parser_throw_expression (cp_parser* parser)
13218 cp_parser_require_keyword (parser, RID_THROW, "`throw'");
13219 token = cp_lexer_peek_token (parser->lexer);
13220 /* Figure out whether or not there is an assignment-expression
13221 following the "throw" keyword. */
13222 if (token->type == CPP_COMMA
13223 || token->type == CPP_SEMICOLON
13224 || token->type == CPP_CLOSE_PAREN
13225 || token->type == CPP_CLOSE_SQUARE
13226 || token->type == CPP_CLOSE_BRACE
13227 || token->type == CPP_COLON)
13228 expression = NULL_TREE;
13230 expression = cp_parser_assignment_expression (parser);
13232 return build_throw (expression);
13235 /* GNU Extensions */
13237 /* Parse an (optional) asm-specification.
13240 asm ( string-literal )
13242 If the asm-specification is present, returns a STRING_CST
13243 corresponding to the string-literal. Otherwise, returns
13247 cp_parser_asm_specification_opt (cp_parser* parser)
13250 tree asm_specification;
13252 /* Peek at the next token. */
13253 token = cp_lexer_peek_token (parser->lexer);
13254 /* If the next token isn't the `asm' keyword, then there's no
13255 asm-specification. */
13256 if (!cp_parser_is_keyword (token, RID_ASM))
13259 /* Consume the `asm' token. */
13260 cp_lexer_consume_token (parser->lexer);
13261 /* Look for the `('. */
13262 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13264 /* Look for the string-literal. */
13265 token = cp_parser_require (parser, CPP_STRING, "string-literal");
13267 asm_specification = token->value;
13269 asm_specification = NULL_TREE;
13271 /* Look for the `)'. */
13272 cp_parser_require (parser, CPP_CLOSE_PAREN, "`('");
13274 return asm_specification;
13277 /* Parse an asm-operand-list.
13281 asm-operand-list , asm-operand
13284 string-literal ( expression )
13285 [ string-literal ] string-literal ( expression )
13287 Returns a TREE_LIST representing the operands. The TREE_VALUE of
13288 each node is the expression. The TREE_PURPOSE is itself a
13289 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
13290 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
13291 is a STRING_CST for the string literal before the parenthesis. */
13294 cp_parser_asm_operand_list (cp_parser* parser)
13296 tree asm_operands = NULL_TREE;
13300 tree string_literal;
13305 c_lex_string_translate = false;
13307 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
13309 /* Consume the `[' token. */
13310 cp_lexer_consume_token (parser->lexer);
13311 /* Read the operand name. */
13312 name = cp_parser_identifier (parser);
13313 if (name != error_mark_node)
13314 name = build_string (IDENTIFIER_LENGTH (name),
13315 IDENTIFIER_POINTER (name));
13316 /* Look for the closing `]'. */
13317 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
13321 /* Look for the string-literal. */
13322 token = cp_parser_require (parser, CPP_STRING, "string-literal");
13323 string_literal = token ? token->value : error_mark_node;
13324 c_lex_string_translate = true;
13325 /* Look for the `('. */
13326 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13327 /* Parse the expression. */
13328 expression = cp_parser_expression (parser);
13329 /* Look for the `)'. */
13330 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13331 c_lex_string_translate = false;
13332 /* Add this operand to the list. */
13333 asm_operands = tree_cons (build_tree_list (name, string_literal),
13336 /* If the next token is not a `,', there are no more
13338 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13340 /* Consume the `,'. */
13341 cp_lexer_consume_token (parser->lexer);
13344 return nreverse (asm_operands);
13347 /* Parse an asm-clobber-list.
13351 asm-clobber-list , string-literal
13353 Returns a TREE_LIST, indicating the clobbers in the order that they
13354 appeared. The TREE_VALUE of each node is a STRING_CST. */
13357 cp_parser_asm_clobber_list (cp_parser* parser)
13359 tree clobbers = NULL_TREE;
13364 tree string_literal;
13366 /* Look for the string literal. */
13367 token = cp_parser_require (parser, CPP_STRING, "string-literal");
13368 string_literal = token ? token->value : error_mark_node;
13369 /* Add it to the list. */
13370 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
13371 /* If the next token is not a `,', then the list is
13373 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13375 /* Consume the `,' token. */
13376 cp_lexer_consume_token (parser->lexer);
13382 /* Parse an (optional) series of attributes.
13385 attributes attribute
13388 __attribute__ (( attribute-list [opt] ))
13390 The return value is as for cp_parser_attribute_list. */
13393 cp_parser_attributes_opt (cp_parser* parser)
13395 tree attributes = NULL_TREE;
13400 tree attribute_list;
13402 /* Peek at the next token. */
13403 token = cp_lexer_peek_token (parser->lexer);
13404 /* If it's not `__attribute__', then we're done. */
13405 if (token->keyword != RID_ATTRIBUTE)
13408 /* Consume the `__attribute__' keyword. */
13409 cp_lexer_consume_token (parser->lexer);
13410 /* Look for the two `(' tokens. */
13411 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13412 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13414 /* Peek at the next token. */
13415 token = cp_lexer_peek_token (parser->lexer);
13416 if (token->type != CPP_CLOSE_PAREN)
13417 /* Parse the attribute-list. */
13418 attribute_list = cp_parser_attribute_list (parser);
13420 /* If the next token is a `)', then there is no attribute
13422 attribute_list = NULL;
13424 /* Look for the two `)' tokens. */
13425 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13426 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13428 /* Add these new attributes to the list. */
13429 attributes = chainon (attributes, attribute_list);
13435 /* Parse an attribute-list.
13439 attribute-list , attribute
13443 identifier ( identifier )
13444 identifier ( identifier , expression-list )
13445 identifier ( expression-list )
13447 Returns a TREE_LIST. Each node corresponds to an attribute. THe
13448 TREE_PURPOSE of each node is the identifier indicating which
13449 attribute is in use. The TREE_VALUE represents the arguments, if
13453 cp_parser_attribute_list (cp_parser* parser)
13455 tree attribute_list = NULL_TREE;
13457 c_lex_string_translate = false;
13464 /* Look for the identifier. We also allow keywords here; for
13465 example `__attribute__ ((const))' is legal. */
13466 token = cp_lexer_peek_token (parser->lexer);
13467 if (token->type != CPP_NAME
13468 && token->type != CPP_KEYWORD)
13469 return error_mark_node;
13470 /* Consume the token. */
13471 token = cp_lexer_consume_token (parser->lexer);
13473 /* Save away the identifier that indicates which attribute this is. */
13474 identifier = token->value;
13475 attribute = build_tree_list (identifier, NULL_TREE);
13477 /* Peek at the next token. */
13478 token = cp_lexer_peek_token (parser->lexer);
13479 /* If it's an `(', then parse the attribute arguments. */
13480 if (token->type == CPP_OPEN_PAREN)
13484 arguments = (cp_parser_parenthesized_expression_list
13485 (parser, true, /*non_constant_p=*/NULL));
13486 /* Save the identifier and arguments away. */
13487 TREE_VALUE (attribute) = arguments;
13490 /* Add this attribute to the list. */
13491 TREE_CHAIN (attribute) = attribute_list;
13492 attribute_list = attribute;
13494 /* Now, look for more attributes. */
13495 token = cp_lexer_peek_token (parser->lexer);
13496 /* If the next token isn't a `,', we're done. */
13497 if (token->type != CPP_COMMA)
13500 /* Consume the comma and keep going. */
13501 cp_lexer_consume_token (parser->lexer);
13503 c_lex_string_translate = true;
13505 /* We built up the list in reverse order. */
13506 return nreverse (attribute_list);
13509 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
13510 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
13511 current value of the PEDANTIC flag, regardless of whether or not
13512 the `__extension__' keyword is present. The caller is responsible
13513 for restoring the value of the PEDANTIC flag. */
13516 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
13518 /* Save the old value of the PEDANTIC flag. */
13519 *saved_pedantic = pedantic;
13521 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
13523 /* Consume the `__extension__' token. */
13524 cp_lexer_consume_token (parser->lexer);
13525 /* We're not being pedantic while the `__extension__' keyword is
13535 /* Parse a label declaration.
13538 __label__ label-declarator-seq ;
13540 label-declarator-seq:
13541 identifier , label-declarator-seq
13545 cp_parser_label_declaration (cp_parser* parser)
13547 /* Look for the `__label__' keyword. */
13548 cp_parser_require_keyword (parser, RID_LABEL, "`__label__'");
13554 /* Look for an identifier. */
13555 identifier = cp_parser_identifier (parser);
13556 /* Declare it as a lobel. */
13557 finish_label_decl (identifier);
13558 /* If the next token is a `;', stop. */
13559 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13561 /* Look for the `,' separating the label declarations. */
13562 cp_parser_require (parser, CPP_COMMA, "`,'");
13565 /* Look for the final `;'. */
13566 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
13569 /* Support Functions */
13571 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
13572 NAME should have one of the representations used for an
13573 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
13574 is returned. If PARSER->SCOPE is a dependent type, then a
13575 SCOPE_REF is returned.
13577 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
13578 returned; the name was already resolved when the TEMPLATE_ID_EXPR
13579 was formed. Abstractly, such entities should not be passed to this
13580 function, because they do not need to be looked up, but it is
13581 simpler to check for this special case here, rather than at the
13584 In cases not explicitly covered above, this function returns a
13585 DECL, OVERLOAD, or baselink representing the result of the lookup.
13586 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
13589 If IS_TYPE is TRUE, bindings that do not refer to types are
13592 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
13595 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
13598 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
13602 cp_parser_lookup_name (cp_parser *parser, tree name,
13603 bool is_type, bool is_template, bool is_namespace,
13604 bool check_dependency)
13607 tree object_type = parser->context->object_type;
13609 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
13610 no longer valid. Note that if we are parsing tentatively, and
13611 the parse fails, OBJECT_TYPE will be automatically restored. */
13612 parser->context->object_type = NULL_TREE;
13614 if (name == error_mark_node)
13615 return error_mark_node;
13617 /* A template-id has already been resolved; there is no lookup to
13619 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
13621 if (BASELINK_P (name))
13623 my_friendly_assert ((TREE_CODE (BASELINK_FUNCTIONS (name))
13624 == TEMPLATE_ID_EXPR),
13629 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
13630 it should already have been checked to make sure that the name
13631 used matches the type being destroyed. */
13632 if (TREE_CODE (name) == BIT_NOT_EXPR)
13636 /* Figure out to which type this destructor applies. */
13638 type = parser->scope;
13639 else if (object_type)
13640 type = object_type;
13642 type = current_class_type;
13643 /* If that's not a class type, there is no destructor. */
13644 if (!type || !CLASS_TYPE_P (type))
13645 return error_mark_node;
13646 if (!CLASSTYPE_DESTRUCTORS (type))
13647 return error_mark_node;
13648 /* If it was a class type, return the destructor. */
13649 return CLASSTYPE_DESTRUCTORS (type);
13652 /* By this point, the NAME should be an ordinary identifier. If
13653 the id-expression was a qualified name, the qualifying scope is
13654 stored in PARSER->SCOPE at this point. */
13655 my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE,
13658 /* Perform the lookup. */
13663 if (parser->scope == error_mark_node)
13664 return error_mark_node;
13666 /* If the SCOPE is dependent, the lookup must be deferred until
13667 the template is instantiated -- unless we are explicitly
13668 looking up names in uninstantiated templates. Even then, we
13669 cannot look up the name if the scope is not a class type; it
13670 might, for example, be a template type parameter. */
13671 dependent_p = (TYPE_P (parser->scope)
13672 && !(parser->in_declarator_p
13673 && currently_open_class (parser->scope))
13674 && dependent_type_p (parser->scope));
13675 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
13679 /* The resolution to Core Issue 180 says that `struct A::B'
13680 should be considered a type-name, even if `A' is
13682 decl = TYPE_NAME (make_typename_type (parser->scope,
13685 else if (is_template)
13686 decl = make_unbound_class_template (parser->scope,
13690 decl = build_nt (SCOPE_REF, parser->scope, name);
13694 bool pop_p = false;
13696 /* If PARSER->SCOPE is a dependent type, then it must be a
13697 class type, and we must not be checking dependencies;
13698 otherwise, we would have processed this lookup above. So
13699 that PARSER->SCOPE is not considered a dependent base by
13700 lookup_member, we must enter the scope here. */
13702 pop_p = push_scope (parser->scope);
13703 /* If the PARSER->SCOPE is a a template specialization, it
13704 may be instantiated during name lookup. In that case,
13705 errors may be issued. Even if we rollback the current
13706 tentative parse, those errors are valid. */
13707 decl = lookup_qualified_name (parser->scope, name, is_type,
13708 /*complain=*/true);
13710 pop_scope (parser->scope);
13712 parser->qualifying_scope = parser->scope;
13713 parser->object_scope = NULL_TREE;
13715 else if (object_type)
13717 tree object_decl = NULL_TREE;
13718 /* Look up the name in the scope of the OBJECT_TYPE, unless the
13719 OBJECT_TYPE is not a class. */
13720 if (CLASS_TYPE_P (object_type))
13721 /* If the OBJECT_TYPE is a template specialization, it may
13722 be instantiated during name lookup. In that case, errors
13723 may be issued. Even if we rollback the current tentative
13724 parse, those errors are valid. */
13725 object_decl = lookup_member (object_type,
13727 /*protect=*/0, is_type);
13728 /* Look it up in the enclosing context, too. */
13729 decl = lookup_name_real (name, is_type, /*nonclass=*/0,
13732 parser->object_scope = object_type;
13733 parser->qualifying_scope = NULL_TREE;
13735 decl = object_decl;
13739 decl = lookup_name_real (name, is_type, /*nonclass=*/0,
13742 parser->qualifying_scope = NULL_TREE;
13743 parser->object_scope = NULL_TREE;
13746 /* If the lookup failed, let our caller know. */
13748 || decl == error_mark_node
13749 || (TREE_CODE (decl) == FUNCTION_DECL
13750 && DECL_ANTICIPATED (decl)))
13751 return error_mark_node;
13753 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
13754 if (TREE_CODE (decl) == TREE_LIST)
13756 /* The error message we have to print is too complicated for
13757 cp_parser_error, so we incorporate its actions directly. */
13758 if (!cp_parser_simulate_error (parser))
13760 error ("reference to `%D' is ambiguous", name);
13761 print_candidates (decl);
13763 return error_mark_node;
13766 my_friendly_assert (DECL_P (decl)
13767 || TREE_CODE (decl) == OVERLOAD
13768 || TREE_CODE (decl) == SCOPE_REF
13769 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
13770 || BASELINK_P (decl),
13773 /* If we have resolved the name of a member declaration, check to
13774 see if the declaration is accessible. When the name resolves to
13775 set of overloaded functions, accessibility is checked when
13776 overload resolution is done.
13778 During an explicit instantiation, access is not checked at all,
13779 as per [temp.explicit]. */
13781 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
13786 /* Like cp_parser_lookup_name, but for use in the typical case where
13787 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
13788 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
13791 cp_parser_lookup_name_simple (cp_parser* parser, tree name)
13793 return cp_parser_lookup_name (parser, name,
13795 /*is_template=*/false,
13796 /*is_namespace=*/false,
13797 /*check_dependency=*/true);
13800 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
13801 the current context, return the TYPE_DECL. If TAG_NAME_P is
13802 true, the DECL indicates the class being defined in a class-head,
13803 or declared in an elaborated-type-specifier.
13805 Otherwise, return DECL. */
13808 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
13810 /* If the TEMPLATE_DECL is being declared as part of a class-head,
13811 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
13814 template <typename T> struct B;
13817 template <typename T> struct A::B {};
13819 Similarly, in a elaborated-type-specifier:
13821 namespace N { struct X{}; }
13824 template <typename T> friend struct N::X;
13827 However, if the DECL refers to a class type, and we are in
13828 the scope of the class, then the name lookup automatically
13829 finds the TYPE_DECL created by build_self_reference rather
13830 than a TEMPLATE_DECL. For example, in:
13832 template <class T> struct S {
13836 there is no need to handle such case. */
13838 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
13839 return DECL_TEMPLATE_RESULT (decl);
13844 /* If too many, or too few, template-parameter lists apply to the
13845 declarator, issue an error message. Returns TRUE if all went well,
13846 and FALSE otherwise. */
13849 cp_parser_check_declarator_template_parameters (cp_parser* parser,
13852 unsigned num_templates;
13854 /* We haven't seen any classes that involve template parameters yet. */
13857 switch (TREE_CODE (declarator))
13864 tree main_declarator = TREE_OPERAND (declarator, 0);
13866 cp_parser_check_declarator_template_parameters (parser,
13875 scope = TREE_OPERAND (declarator, 0);
13876 member = TREE_OPERAND (declarator, 1);
13878 /* If this is a pointer-to-member, then we are not interested
13879 in the SCOPE, because it does not qualify the thing that is
13881 if (TREE_CODE (member) == INDIRECT_REF)
13882 return (cp_parser_check_declarator_template_parameters
13885 while (scope && CLASS_TYPE_P (scope))
13887 /* You're supposed to have one `template <...>'
13888 for every template class, but you don't need one
13889 for a full specialization. For example:
13891 template <class T> struct S{};
13892 template <> struct S<int> { void f(); };
13893 void S<int>::f () {}
13895 is correct; there shouldn't be a `template <>' for
13896 the definition of `S<int>::f'. */
13897 if (CLASSTYPE_TEMPLATE_INFO (scope)
13898 && (CLASSTYPE_TEMPLATE_INSTANTIATION (scope)
13899 || uses_template_parms (CLASSTYPE_TI_ARGS (scope)))
13900 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
13903 scope = TYPE_CONTEXT (scope);
13907 /* Fall through. */
13910 /* If the DECLARATOR has the form `X<y>' then it uses one
13911 additional level of template parameters. */
13912 if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
13915 return cp_parser_check_template_parameters (parser,
13920 /* NUM_TEMPLATES were used in the current declaration. If that is
13921 invalid, return FALSE and issue an error messages. Otherwise,
13925 cp_parser_check_template_parameters (cp_parser* parser,
13926 unsigned num_templates)
13928 /* If there are more template classes than parameter lists, we have
13931 template <class T> void S<T>::R<T>::f (); */
13932 if (parser->num_template_parameter_lists < num_templates)
13934 error ("too few template-parameter-lists");
13937 /* If there are the same number of template classes and parameter
13938 lists, that's OK. */
13939 if (parser->num_template_parameter_lists == num_templates)
13941 /* If there are more, but only one more, then we are referring to a
13942 member template. That's OK too. */
13943 if (parser->num_template_parameter_lists == num_templates + 1)
13945 /* Otherwise, there are too many template parameter lists. We have
13948 template <class T> template <class U> void S::f(); */
13949 error ("too many template-parameter-lists");
13953 /* Parse a binary-expression of the general form:
13957 binary-expression <token> <expr>
13959 The TOKEN_TREE_MAP maps <token> types to <expr> codes. FN is used
13960 to parser the <expr>s. If the first production is used, then the
13961 value returned by FN is returned directly. Otherwise, a node with
13962 the indicated EXPR_TYPE is returned, with operands corresponding to
13963 the two sub-expressions. */
13966 cp_parser_binary_expression (cp_parser* parser,
13967 const cp_parser_token_tree_map token_tree_map,
13968 cp_parser_expression_fn fn)
13972 /* Parse the first expression. */
13973 lhs = (*fn) (parser);
13974 /* Now, look for more expressions. */
13978 const cp_parser_token_tree_map_node *map_node;
13981 /* Peek at the next token. */
13982 token = cp_lexer_peek_token (parser->lexer);
13983 /* If the token is `>', and that's not an operator at the
13984 moment, then we're done. */
13985 if (token->type == CPP_GREATER
13986 && !parser->greater_than_is_operator_p)
13988 /* If we find one of the tokens we want, build the corresponding
13989 tree representation. */
13990 for (map_node = token_tree_map;
13991 map_node->token_type != CPP_EOF;
13993 if (map_node->token_type == token->type)
13995 /* Assume that an overloaded operator will not be used. */
13996 bool overloaded_p = false;
13998 /* Consume the operator token. */
13999 cp_lexer_consume_token (parser->lexer);
14000 /* Parse the right-hand side of the expression. */
14001 rhs = (*fn) (parser);
14002 /* Build the binary tree node. */
14003 lhs = build_x_binary_op (map_node->tree_type, lhs, rhs,
14005 /* If the binary operator required the use of an
14006 overloaded operator, then this expression cannot be an
14007 integral constant-expression. An overloaded operator
14008 can be used even if both operands are otherwise
14009 permissible in an integral constant-expression if at
14010 least one of the operands is of enumeration type. */
14012 && (cp_parser_non_integral_constant_expression
14013 (parser, "calls to overloaded operators")))
14014 lhs = error_mark_node;
14018 /* If the token wasn't one of the ones we want, we're done. */
14019 if (map_node->token_type == CPP_EOF)
14026 /* Parse an optional `::' token indicating that the following name is
14027 from the global namespace. If so, PARSER->SCOPE is set to the
14028 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
14029 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
14030 Returns the new value of PARSER->SCOPE, if the `::' token is
14031 present, and NULL_TREE otherwise. */
14034 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
14038 /* Peek at the next token. */
14039 token = cp_lexer_peek_token (parser->lexer);
14040 /* If we're looking at a `::' token then we're starting from the
14041 global namespace, not our current location. */
14042 if (token->type == CPP_SCOPE)
14044 /* Consume the `::' token. */
14045 cp_lexer_consume_token (parser->lexer);
14046 /* Set the SCOPE so that we know where to start the lookup. */
14047 parser->scope = global_namespace;
14048 parser->qualifying_scope = global_namespace;
14049 parser->object_scope = NULL_TREE;
14051 return parser->scope;
14053 else if (!current_scope_valid_p)
14055 parser->scope = NULL_TREE;
14056 parser->qualifying_scope = NULL_TREE;
14057 parser->object_scope = NULL_TREE;
14063 /* Returns TRUE if the upcoming token sequence is the start of a
14064 constructor declarator. If FRIEND_P is true, the declarator is
14065 preceded by the `friend' specifier. */
14068 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
14070 bool constructor_p;
14071 tree type_decl = NULL_TREE;
14072 bool nested_name_p;
14073 cp_token *next_token;
14075 /* The common case is that this is not a constructor declarator, so
14076 try to avoid doing lots of work if at all possible. It's not
14077 valid declare a constructor at function scope. */
14078 if (at_function_scope_p ())
14080 /* And only certain tokens can begin a constructor declarator. */
14081 next_token = cp_lexer_peek_token (parser->lexer);
14082 if (next_token->type != CPP_NAME
14083 && next_token->type != CPP_SCOPE
14084 && next_token->type != CPP_NESTED_NAME_SPECIFIER
14085 && next_token->type != CPP_TEMPLATE_ID)
14088 /* Parse tentatively; we are going to roll back all of the tokens
14090 cp_parser_parse_tentatively (parser);
14091 /* Assume that we are looking at a constructor declarator. */
14092 constructor_p = true;
14094 /* Look for the optional `::' operator. */
14095 cp_parser_global_scope_opt (parser,
14096 /*current_scope_valid_p=*/false);
14097 /* Look for the nested-name-specifier. */
14099 = (cp_parser_nested_name_specifier_opt (parser,
14100 /*typename_keyword_p=*/false,
14101 /*check_dependency_p=*/false,
14103 /*is_declaration=*/false)
14105 /* Outside of a class-specifier, there must be a
14106 nested-name-specifier. */
14107 if (!nested_name_p &&
14108 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
14110 constructor_p = false;
14111 /* If we still think that this might be a constructor-declarator,
14112 look for a class-name. */
14117 template <typename T> struct S { S(); };
14118 template <typename T> S<T>::S ();
14120 we must recognize that the nested `S' names a class.
14123 template <typename T> S<T>::S<T> ();
14125 we must recognize that the nested `S' names a template. */
14126 type_decl = cp_parser_class_name (parser,
14127 /*typename_keyword_p=*/false,
14128 /*template_keyword_p=*/false,
14130 /*check_dependency_p=*/false,
14131 /*class_head_p=*/false,
14132 /*is_declaration=*/false);
14133 /* If there was no class-name, then this is not a constructor. */
14134 constructor_p = !cp_parser_error_occurred (parser);
14137 /* If we're still considering a constructor, we have to see a `(',
14138 to begin the parameter-declaration-clause, followed by either a
14139 `)', an `...', or a decl-specifier. We need to check for a
14140 type-specifier to avoid being fooled into thinking that:
14144 is a constructor. (It is actually a function named `f' that
14145 takes one parameter (of type `int') and returns a value of type
14148 && cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
14150 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
14151 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
14152 && !cp_parser_storage_class_specifier_opt (parser))
14155 bool pop_p = false;
14156 unsigned saved_num_template_parameter_lists;
14158 /* Names appearing in the type-specifier should be looked up
14159 in the scope of the class. */
14160 if (current_class_type)
14164 type = TREE_TYPE (type_decl);
14165 if (TREE_CODE (type) == TYPENAME_TYPE)
14167 type = resolve_typename_type (type,
14168 /*only_current_p=*/false);
14169 if (type == error_mark_node)
14171 cp_parser_abort_tentative_parse (parser);
14175 pop_p = push_scope (type);
14178 /* Inside the constructor parameter list, surrounding
14179 template-parameter-lists do not apply. */
14180 saved_num_template_parameter_lists
14181 = parser->num_template_parameter_lists;
14182 parser->num_template_parameter_lists = 0;
14184 /* Look for the type-specifier. */
14185 cp_parser_type_specifier (parser,
14186 CP_PARSER_FLAGS_NONE,
14187 /*is_friend=*/false,
14188 /*is_declarator=*/true,
14189 /*declares_class_or_enum=*/NULL,
14190 /*is_cv_qualifier=*/NULL);
14192 parser->num_template_parameter_lists
14193 = saved_num_template_parameter_lists;
14195 /* Leave the scope of the class. */
14199 constructor_p = !cp_parser_error_occurred (parser);
14203 constructor_p = false;
14204 /* We did not really want to consume any tokens. */
14205 cp_parser_abort_tentative_parse (parser);
14207 return constructor_p;
14210 /* Parse the definition of the function given by the DECL_SPECIFIERS,
14211 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
14212 they must be performed once we are in the scope of the function.
14214 Returns the function defined. */
14217 cp_parser_function_definition_from_specifiers_and_declarator
14218 (cp_parser* parser,
14219 tree decl_specifiers,
14226 /* Begin the function-definition. */
14227 success_p = begin_function_definition (decl_specifiers,
14231 /* If there were names looked up in the decl-specifier-seq that we
14232 did not check, check them now. We must wait until we are in the
14233 scope of the function to perform the checks, since the function
14234 might be a friend. */
14235 perform_deferred_access_checks ();
14239 /* If begin_function_definition didn't like the definition, skip
14240 the entire function. */
14241 error ("invalid function declaration");
14242 cp_parser_skip_to_end_of_block_or_statement (parser);
14243 fn = error_mark_node;
14246 fn = cp_parser_function_definition_after_declarator (parser,
14247 /*inline_p=*/false);
14252 /* Parse the part of a function-definition that follows the
14253 declarator. INLINE_P is TRUE iff this function is an inline
14254 function defined with a class-specifier.
14256 Returns the function defined. */
14259 cp_parser_function_definition_after_declarator (cp_parser* parser,
14263 bool ctor_initializer_p = false;
14264 bool saved_in_unbraced_linkage_specification_p;
14265 unsigned saved_num_template_parameter_lists;
14267 /* If the next token is `return', then the code may be trying to
14268 make use of the "named return value" extension that G++ used to
14270 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
14272 /* Consume the `return' keyword. */
14273 cp_lexer_consume_token (parser->lexer);
14274 /* Look for the identifier that indicates what value is to be
14276 cp_parser_identifier (parser);
14277 /* Issue an error message. */
14278 error ("named return values are no longer supported");
14279 /* Skip tokens until we reach the start of the function body. */
14280 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
14281 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
14282 cp_lexer_consume_token (parser->lexer);
14284 /* The `extern' in `extern "C" void f () { ... }' does not apply to
14285 anything declared inside `f'. */
14286 saved_in_unbraced_linkage_specification_p
14287 = parser->in_unbraced_linkage_specification_p;
14288 parser->in_unbraced_linkage_specification_p = false;
14289 /* Inside the function, surrounding template-parameter-lists do not
14291 saved_num_template_parameter_lists
14292 = parser->num_template_parameter_lists;
14293 parser->num_template_parameter_lists = 0;
14294 /* If the next token is `try', then we are looking at a
14295 function-try-block. */
14296 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
14297 ctor_initializer_p = cp_parser_function_try_block (parser);
14298 /* A function-try-block includes the function-body, so we only do
14299 this next part if we're not processing a function-try-block. */
14302 = cp_parser_ctor_initializer_opt_and_function_body (parser);
14304 /* Finish the function. */
14305 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
14306 (inline_p ? 2 : 0));
14307 /* Generate code for it, if necessary. */
14308 expand_or_defer_fn (fn);
14309 /* Restore the saved values. */
14310 parser->in_unbraced_linkage_specification_p
14311 = saved_in_unbraced_linkage_specification_p;
14312 parser->num_template_parameter_lists
14313 = saved_num_template_parameter_lists;
14318 /* Parse a template-declaration, assuming that the `export' (and
14319 `extern') keywords, if present, has already been scanned. MEMBER_P
14320 is as for cp_parser_template_declaration. */
14323 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
14325 tree decl = NULL_TREE;
14326 tree parameter_list;
14327 bool friend_p = false;
14329 /* Look for the `template' keyword. */
14330 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'"))
14334 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
14337 /* If the next token is `>', then we have an invalid
14338 specialization. Rather than complain about an invalid template
14339 parameter, issue an error message here. */
14340 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
14342 cp_parser_error (parser, "invalid explicit specialization");
14343 begin_specialization ();
14344 parameter_list = NULL_TREE;
14348 /* Parse the template parameters. */
14349 begin_template_parm_list ();
14350 parameter_list = cp_parser_template_parameter_list (parser);
14351 parameter_list = end_template_parm_list (parameter_list);
14354 /* Look for the `>'. */
14355 cp_parser_skip_until_found (parser, CPP_GREATER, "`>'");
14356 /* We just processed one more parameter list. */
14357 ++parser->num_template_parameter_lists;
14358 /* If the next token is `template', there are more template
14360 if (cp_lexer_next_token_is_keyword (parser->lexer,
14362 cp_parser_template_declaration_after_export (parser, member_p);
14365 decl = cp_parser_single_declaration (parser,
14369 /* If this is a member template declaration, let the front
14371 if (member_p && !friend_p && decl)
14373 if (TREE_CODE (decl) == TYPE_DECL)
14374 cp_parser_check_access_in_redeclaration (decl);
14376 decl = finish_member_template_decl (decl);
14378 else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
14379 make_friend_class (current_class_type, TREE_TYPE (decl),
14380 /*complain=*/true);
14382 /* We are done with the current parameter list. */
14383 --parser->num_template_parameter_lists;
14386 finish_template_decl (parameter_list);
14388 /* Register member declarations. */
14389 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
14390 finish_member_declaration (decl);
14392 /* If DECL is a function template, we must return to parse it later.
14393 (Even though there is no definition, there might be default
14394 arguments that need handling.) */
14395 if (member_p && decl
14396 && (TREE_CODE (decl) == FUNCTION_DECL
14397 || DECL_FUNCTION_TEMPLATE_P (decl)))
14398 TREE_VALUE (parser->unparsed_functions_queues)
14399 = tree_cons (NULL_TREE, decl,
14400 TREE_VALUE (parser->unparsed_functions_queues));
14403 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
14404 `function-definition' sequence. MEMBER_P is true, this declaration
14405 appears in a class scope.
14407 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
14408 *FRIEND_P is set to TRUE iff the declaration is a friend. */
14411 cp_parser_single_declaration (cp_parser* parser,
14415 int declares_class_or_enum;
14416 tree decl = NULL_TREE;
14417 tree decl_specifiers;
14419 bool function_definition_p = false;
14421 /* Defer access checks until we know what is being declared. */
14422 push_deferring_access_checks (dk_deferred);
14424 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
14427 = cp_parser_decl_specifier_seq (parser,
14428 CP_PARSER_FLAGS_OPTIONAL,
14430 &declares_class_or_enum);
14432 *friend_p = cp_parser_friend_p (decl_specifiers);
14433 /* Gather up the access checks that occurred the
14434 decl-specifier-seq. */
14435 stop_deferring_access_checks ();
14437 /* Check for the declaration of a template class. */
14438 if (declares_class_or_enum)
14440 if (cp_parser_declares_only_class_p (parser))
14442 decl = shadow_tag (decl_specifiers);
14444 decl = TYPE_NAME (decl);
14446 decl = error_mark_node;
14451 /* If it's not a template class, try for a template function. If
14452 the next token is a `;', then this declaration does not declare
14453 anything. But, if there were errors in the decl-specifiers, then
14454 the error might well have come from an attempted class-specifier.
14455 In that case, there's no need to warn about a missing declarator. */
14457 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
14458 || !value_member (error_mark_node, decl_specifiers)))
14459 decl = cp_parser_init_declarator (parser,
14462 /*function_definition_allowed_p=*/true,
14464 declares_class_or_enum,
14465 &function_definition_p);
14467 pop_deferring_access_checks ();
14469 /* Clear any current qualification; whatever comes next is the start
14470 of something new. */
14471 parser->scope = NULL_TREE;
14472 parser->qualifying_scope = NULL_TREE;
14473 parser->object_scope = NULL_TREE;
14474 /* Look for a trailing `;' after the declaration. */
14475 if (!function_definition_p
14476 && !cp_parser_require (parser, CPP_SEMICOLON, "`;'"))
14477 cp_parser_skip_to_end_of_block_or_statement (parser);
14482 /* Parse a cast-expression that is not the operand of a unary "&". */
14485 cp_parser_simple_cast_expression (cp_parser *parser)
14487 return cp_parser_cast_expression (parser, /*address_p=*/false);
14490 /* Parse a functional cast to TYPE. Returns an expression
14491 representing the cast. */
14494 cp_parser_functional_cast (cp_parser* parser, tree type)
14496 tree expression_list;
14500 = cp_parser_parenthesized_expression_list (parser, false,
14501 /*non_constant_p=*/NULL);
14503 cast = build_functional_cast (type, expression_list);
14504 /* [expr.const]/1: In an integral constant expression "only type
14505 conversions to integral or enumeration type can be used". */
14506 if (cast != error_mark_node && !type_dependent_expression_p (type)
14507 && !INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (type)))
14509 if (cp_parser_non_integral_constant_expression
14510 (parser, "a call to a constructor"))
14511 return error_mark_node;
14516 /* Save the tokens that make up the body of a member function defined
14517 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
14518 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
14519 specifiers applied to the declaration. Returns the FUNCTION_DECL
14520 for the member function. */
14523 cp_parser_save_member_function_body (cp_parser* parser,
14524 tree decl_specifiers,
14528 cp_token_cache *cache;
14531 /* Create the function-declaration. */
14532 fn = start_method (decl_specifiers, declarator, attributes);
14533 /* If something went badly wrong, bail out now. */
14534 if (fn == error_mark_node)
14536 /* If there's a function-body, skip it. */
14537 if (cp_parser_token_starts_function_definition_p
14538 (cp_lexer_peek_token (parser->lexer)))
14539 cp_parser_skip_to_end_of_block_or_statement (parser);
14540 return error_mark_node;
14543 /* Remember it, if there default args to post process. */
14544 cp_parser_save_default_args (parser, fn);
14546 /* Create a token cache. */
14547 cache = cp_token_cache_new ();
14548 /* Save away the tokens that make up the body of the
14550 cp_parser_cache_group (parser, cache, CPP_CLOSE_BRACE, /*depth=*/0);
14551 /* Handle function try blocks. */
14552 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
14553 cp_parser_cache_group (parser, cache, CPP_CLOSE_BRACE, /*depth=*/0);
14555 /* Save away the inline definition; we will process it when the
14556 class is complete. */
14557 DECL_PENDING_INLINE_INFO (fn) = cache;
14558 DECL_PENDING_INLINE_P (fn) = 1;
14560 /* We need to know that this was defined in the class, so that
14561 friend templates are handled correctly. */
14562 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
14564 /* We're done with the inline definition. */
14565 finish_method (fn);
14567 /* Add FN to the queue of functions to be parsed later. */
14568 TREE_VALUE (parser->unparsed_functions_queues)
14569 = tree_cons (NULL_TREE, fn,
14570 TREE_VALUE (parser->unparsed_functions_queues));
14575 /* Parse a template-argument-list, as well as the trailing ">" (but
14576 not the opening ">"). See cp_parser_template_argument_list for the
14580 cp_parser_enclosed_template_argument_list (cp_parser* parser)
14584 tree saved_qualifying_scope;
14585 tree saved_object_scope;
14586 bool saved_greater_than_is_operator_p;
14590 When parsing a template-id, the first non-nested `>' is taken as
14591 the end of the template-argument-list rather than a greater-than
14593 saved_greater_than_is_operator_p
14594 = parser->greater_than_is_operator_p;
14595 parser->greater_than_is_operator_p = false;
14596 /* Parsing the argument list may modify SCOPE, so we save it
14598 saved_scope = parser->scope;
14599 saved_qualifying_scope = parser->qualifying_scope;
14600 saved_object_scope = parser->object_scope;
14601 /* Parse the template-argument-list itself. */
14602 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
14603 arguments = NULL_TREE;
14605 arguments = cp_parser_template_argument_list (parser);
14606 /* Look for the `>' that ends the template-argument-list. If we find
14607 a '>>' instead, it's probably just a typo. */
14608 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
14610 if (!saved_greater_than_is_operator_p)
14612 /* If we're in a nested template argument list, the '>>' has to be
14613 a typo for '> >'. We emit the error message, but we continue
14614 parsing and we push a '>' as next token, so that the argument
14615 list will be parsed correctly.. */
14617 error ("`>>' should be `> >' within a nested template argument list");
14618 token = cp_lexer_peek_token (parser->lexer);
14619 token->type = CPP_GREATER;
14623 /* If this is not a nested template argument list, the '>>' is
14624 a typo for '>'. Emit an error message and continue. */
14625 error ("spurious `>>', use `>' to terminate a template argument list");
14626 cp_lexer_consume_token (parser->lexer);
14629 else if (!cp_parser_require (parser, CPP_GREATER, "`>'"))
14630 error ("missing `>' to terminate the template argument list");
14631 /* The `>' token might be a greater-than operator again now. */
14632 parser->greater_than_is_operator_p
14633 = saved_greater_than_is_operator_p;
14634 /* Restore the SAVED_SCOPE. */
14635 parser->scope = saved_scope;
14636 parser->qualifying_scope = saved_qualifying_scope;
14637 parser->object_scope = saved_object_scope;
14642 /* MEMBER_FUNCTION is a member function, or a friend. If default
14643 arguments, or the body of the function have not yet been parsed,
14647 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
14649 cp_lexer *saved_lexer;
14651 /* If this member is a template, get the underlying
14653 if (DECL_FUNCTION_TEMPLATE_P (member_function))
14654 member_function = DECL_TEMPLATE_RESULT (member_function);
14656 /* There should not be any class definitions in progress at this
14657 point; the bodies of members are only parsed outside of all class
14659 my_friendly_assert (parser->num_classes_being_defined == 0, 20010816);
14660 /* While we're parsing the member functions we might encounter more
14661 classes. We want to handle them right away, but we don't want
14662 them getting mixed up with functions that are currently in the
14664 parser->unparsed_functions_queues
14665 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
14667 /* Make sure that any template parameters are in scope. */
14668 maybe_begin_member_template_processing (member_function);
14670 /* If the body of the function has not yet been parsed, parse it
14672 if (DECL_PENDING_INLINE_P (member_function))
14674 tree function_scope;
14675 cp_token_cache *tokens;
14677 /* The function is no longer pending; we are processing it. */
14678 tokens = DECL_PENDING_INLINE_INFO (member_function);
14679 DECL_PENDING_INLINE_INFO (member_function) = NULL;
14680 DECL_PENDING_INLINE_P (member_function) = 0;
14681 /* If this was an inline function in a local class, enter the scope
14682 of the containing function. */
14683 function_scope = decl_function_context (member_function);
14684 if (function_scope)
14685 push_function_context_to (function_scope);
14687 /* Save away the current lexer. */
14688 saved_lexer = parser->lexer;
14689 /* Make a new lexer to feed us the tokens saved for this function. */
14690 parser->lexer = cp_lexer_new_from_tokens (tokens);
14691 parser->lexer->next = saved_lexer;
14693 /* Set the current source position to be the location of the first
14694 token in the saved inline body. */
14695 cp_lexer_peek_token (parser->lexer);
14697 /* Let the front end know that we going to be defining this
14699 start_function (NULL_TREE, member_function, NULL_TREE,
14700 SF_PRE_PARSED | SF_INCLASS_INLINE);
14702 /* Now, parse the body of the function. */
14703 cp_parser_function_definition_after_declarator (parser,
14704 /*inline_p=*/true);
14706 /* Leave the scope of the containing function. */
14707 if (function_scope)
14708 pop_function_context_from (function_scope);
14709 /* Restore the lexer. */
14710 parser->lexer = saved_lexer;
14713 /* Remove any template parameters from the symbol table. */
14714 maybe_end_member_template_processing ();
14716 /* Restore the queue. */
14717 parser->unparsed_functions_queues
14718 = TREE_CHAIN (parser->unparsed_functions_queues);
14721 /* If DECL contains any default args, remember it on the unparsed
14722 functions queue. */
14725 cp_parser_save_default_args (cp_parser* parser, tree decl)
14729 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
14731 probe = TREE_CHAIN (probe))
14732 if (TREE_PURPOSE (probe))
14734 TREE_PURPOSE (parser->unparsed_functions_queues)
14735 = tree_cons (NULL_TREE, decl,
14736 TREE_PURPOSE (parser->unparsed_functions_queues));
14742 /* FN is a FUNCTION_DECL which may contains a parameter with an
14743 unparsed DEFAULT_ARG. Parse the default args now. */
14746 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
14748 cp_lexer *saved_lexer;
14749 cp_token_cache *tokens;
14750 bool saved_local_variables_forbidden_p;
14753 /* While we're parsing the default args, we might (due to the
14754 statement expression extension) encounter more classes. We want
14755 to handle them right away, but we don't want them getting mixed
14756 up with default args that are currently in the queue. */
14757 parser->unparsed_functions_queues
14758 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
14760 for (parameters = TYPE_ARG_TYPES (TREE_TYPE (fn));
14762 parameters = TREE_CHAIN (parameters))
14764 if (!TREE_PURPOSE (parameters)
14765 || TREE_CODE (TREE_PURPOSE (parameters)) != DEFAULT_ARG)
14768 /* Save away the current lexer. */
14769 saved_lexer = parser->lexer;
14770 /* Create a new one, using the tokens we have saved. */
14771 tokens = DEFARG_TOKENS (TREE_PURPOSE (parameters));
14772 parser->lexer = cp_lexer_new_from_tokens (tokens);
14774 /* Set the current source position to be the location of the
14775 first token in the default argument. */
14776 cp_lexer_peek_token (parser->lexer);
14778 /* Local variable names (and the `this' keyword) may not appear
14779 in a default argument. */
14780 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
14781 parser->local_variables_forbidden_p = true;
14782 /* Parse the assignment-expression. */
14783 if (DECL_CLASS_SCOPE_P (fn))
14784 push_nested_class (DECL_CONTEXT (fn));
14785 TREE_PURPOSE (parameters) = cp_parser_assignment_expression (parser);
14786 if (DECL_CLASS_SCOPE_P (fn))
14787 pop_nested_class ();
14789 /* If the token stream has not been completely used up, then
14790 there was extra junk after the end of the default
14792 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
14793 cp_parser_error (parser, "expected `,'");
14795 /* Restore saved state. */
14796 parser->lexer = saved_lexer;
14797 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
14800 /* Restore the queue. */
14801 parser->unparsed_functions_queues
14802 = TREE_CHAIN (parser->unparsed_functions_queues);
14805 /* Parse the operand of `sizeof' (or a similar operator). Returns
14806 either a TYPE or an expression, depending on the form of the
14807 input. The KEYWORD indicates which kind of expression we have
14811 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
14813 static const char *format;
14814 tree expr = NULL_TREE;
14815 const char *saved_message;
14816 bool saved_integral_constant_expression_p;
14818 /* Initialize FORMAT the first time we get here. */
14820 format = "types may not be defined in `%s' expressions";
14822 /* Types cannot be defined in a `sizeof' expression. Save away the
14824 saved_message = parser->type_definition_forbidden_message;
14825 /* And create the new one. */
14826 parser->type_definition_forbidden_message
14827 = xmalloc (strlen (format)
14828 + strlen (IDENTIFIER_POINTER (ridpointers[keyword]))
14830 sprintf ((char *) parser->type_definition_forbidden_message,
14831 format, IDENTIFIER_POINTER (ridpointers[keyword]));
14833 /* The restrictions on constant-expressions do not apply inside
14834 sizeof expressions. */
14835 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
14836 parser->integral_constant_expression_p = false;
14838 /* Do not actually evaluate the expression. */
14840 /* If it's a `(', then we might be looking at the type-id
14842 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
14845 bool saved_in_type_id_in_expr_p;
14847 /* We can't be sure yet whether we're looking at a type-id or an
14849 cp_parser_parse_tentatively (parser);
14850 /* Consume the `('. */
14851 cp_lexer_consume_token (parser->lexer);
14852 /* Parse the type-id. */
14853 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
14854 parser->in_type_id_in_expr_p = true;
14855 type = cp_parser_type_id (parser);
14856 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
14857 /* Now, look for the trailing `)'. */
14858 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14859 /* If all went well, then we're done. */
14860 if (cp_parser_parse_definitely (parser))
14862 /* Build a list of decl-specifiers; right now, we have only
14863 a single type-specifier. */
14864 type = build_tree_list (NULL_TREE,
14867 /* Call grokdeclarator to figure out what type this is. */
14868 expr = grokdeclarator (NULL_TREE,
14872 /*attrlist=*/NULL);
14876 /* If the type-id production did not work out, then we must be
14877 looking at the unary-expression production. */
14879 expr = cp_parser_unary_expression (parser, /*address_p=*/false);
14880 /* Go back to evaluating expressions. */
14883 /* Free the message we created. */
14884 free ((char *) parser->type_definition_forbidden_message);
14885 /* And restore the old one. */
14886 parser->type_definition_forbidden_message = saved_message;
14887 parser->integral_constant_expression_p = saved_integral_constant_expression_p;
14892 /* If the current declaration has no declarator, return true. */
14895 cp_parser_declares_only_class_p (cp_parser *parser)
14897 /* If the next token is a `;' or a `,' then there is no
14899 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
14900 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
14903 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
14904 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
14907 cp_parser_friend_p (tree decl_specifiers)
14909 while (decl_specifiers)
14911 /* See if this decl-specifier is `friend'. */
14912 if (TREE_CODE (TREE_VALUE (decl_specifiers)) == IDENTIFIER_NODE
14913 && C_RID_CODE (TREE_VALUE (decl_specifiers)) == RID_FRIEND)
14916 /* Go on to the next decl-specifier. */
14917 decl_specifiers = TREE_CHAIN (decl_specifiers);
14923 /* If the next token is of the indicated TYPE, consume it. Otherwise,
14924 issue an error message indicating that TOKEN_DESC was expected.
14926 Returns the token consumed, if the token had the appropriate type.
14927 Otherwise, returns NULL. */
14930 cp_parser_require (cp_parser* parser,
14931 enum cpp_ttype type,
14932 const char* token_desc)
14934 if (cp_lexer_next_token_is (parser->lexer, type))
14935 return cp_lexer_consume_token (parser->lexer);
14938 /* Output the MESSAGE -- unless we're parsing tentatively. */
14939 if (!cp_parser_simulate_error (parser))
14941 char *message = concat ("expected ", token_desc, NULL);
14942 cp_parser_error (parser, message);
14949 /* Like cp_parser_require, except that tokens will be skipped until
14950 the desired token is found. An error message is still produced if
14951 the next token is not as expected. */
14954 cp_parser_skip_until_found (cp_parser* parser,
14955 enum cpp_ttype type,
14956 const char* token_desc)
14959 unsigned nesting_depth = 0;
14961 if (cp_parser_require (parser, type, token_desc))
14964 /* Skip tokens until the desired token is found. */
14967 /* Peek at the next token. */
14968 token = cp_lexer_peek_token (parser->lexer);
14969 /* If we've reached the token we want, consume it and
14971 if (token->type == type && !nesting_depth)
14973 cp_lexer_consume_token (parser->lexer);
14976 /* If we've run out of tokens, stop. */
14977 if (token->type == CPP_EOF)
14979 if (token->type == CPP_OPEN_BRACE
14980 || token->type == CPP_OPEN_PAREN
14981 || token->type == CPP_OPEN_SQUARE)
14983 else if (token->type == CPP_CLOSE_BRACE
14984 || token->type == CPP_CLOSE_PAREN
14985 || token->type == CPP_CLOSE_SQUARE)
14987 if (nesting_depth-- == 0)
14990 /* Consume this token. */
14991 cp_lexer_consume_token (parser->lexer);
14995 /* If the next token is the indicated keyword, consume it. Otherwise,
14996 issue an error message indicating that TOKEN_DESC was expected.
14998 Returns the token consumed, if the token had the appropriate type.
14999 Otherwise, returns NULL. */
15002 cp_parser_require_keyword (cp_parser* parser,
15004 const char* token_desc)
15006 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
15008 if (token && token->keyword != keyword)
15010 dyn_string_t error_msg;
15012 /* Format the error message. */
15013 error_msg = dyn_string_new (0);
15014 dyn_string_append_cstr (error_msg, "expected ");
15015 dyn_string_append_cstr (error_msg, token_desc);
15016 cp_parser_error (parser, error_msg->s);
15017 dyn_string_delete (error_msg);
15024 /* Returns TRUE iff TOKEN is a token that can begin the body of a
15025 function-definition. */
15028 cp_parser_token_starts_function_definition_p (cp_token* token)
15030 return (/* An ordinary function-body begins with an `{'. */
15031 token->type == CPP_OPEN_BRACE
15032 /* A ctor-initializer begins with a `:'. */
15033 || token->type == CPP_COLON
15034 /* A function-try-block begins with `try'. */
15035 || token->keyword == RID_TRY
15036 /* The named return value extension begins with `return'. */
15037 || token->keyword == RID_RETURN);
15040 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
15044 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
15048 token = cp_lexer_peek_token (parser->lexer);
15049 return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
15052 /* Returns TRUE iff the next token is the "," or ">" ending a
15053 template-argument. ">>" is also accepted (after the full
15054 argument was parsed) because it's probably a typo for "> >",
15055 and there is a specific diagnostic for this. */
15058 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
15062 token = cp_lexer_peek_token (parser->lexer);
15063 return (token->type == CPP_COMMA || token->type == CPP_GREATER
15064 || token->type == CPP_RSHIFT);
15067 /* Returns TRUE iff the n-th token is a ">", or the n-th is a "[" and the
15068 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
15071 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
15076 token = cp_lexer_peek_nth_token (parser->lexer, n);
15077 if (token->type == CPP_LESS)
15079 /* Check for the sequence `<::' in the original code. It would be lexed as
15080 `[:', where `[' is a digraph, and there is no whitespace before
15082 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
15085 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
15086 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
15092 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
15093 or none_type otherwise. */
15095 static enum tag_types
15096 cp_parser_token_is_class_key (cp_token* token)
15098 switch (token->keyword)
15103 return record_type;
15112 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
15115 cp_parser_check_class_key (enum tag_types class_key, tree type)
15117 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
15118 pedwarn ("`%s' tag used in naming `%#T'",
15119 class_key == union_type ? "union"
15120 : class_key == record_type ? "struct" : "class",
15124 /* Issue an error message if DECL is redeclared with different
15125 access than its original declaration [class.access.spec/3].
15126 This applies to nested classes and nested class templates.
15129 static void cp_parser_check_access_in_redeclaration (tree decl)
15131 if (!CLASS_TYPE_P (TREE_TYPE (decl)))
15134 if ((TREE_PRIVATE (decl)
15135 != (current_access_specifier == access_private_node))
15136 || (TREE_PROTECTED (decl)
15137 != (current_access_specifier == access_protected_node)))
15138 error ("%D redeclared with different access", decl);
15141 /* Look for the `template' keyword, as a syntactic disambiguator.
15142 Return TRUE iff it is present, in which case it will be
15146 cp_parser_optional_template_keyword (cp_parser *parser)
15148 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
15150 /* The `template' keyword can only be used within templates;
15151 outside templates the parser can always figure out what is a
15152 template and what is not. */
15153 if (!processing_template_decl)
15155 error ("`template' (as a disambiguator) is only allowed "
15156 "within templates");
15157 /* If this part of the token stream is rescanned, the same
15158 error message would be generated. So, we purge the token
15159 from the stream. */
15160 cp_lexer_purge_token (parser->lexer);
15165 /* Consume the `template' keyword. */
15166 cp_lexer_consume_token (parser->lexer);
15174 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
15175 set PARSER->SCOPE, and perform other related actions. */
15178 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
15183 /* Get the stored value. */
15184 value = cp_lexer_consume_token (parser->lexer)->value;
15185 /* Perform any access checks that were deferred. */
15186 for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
15187 perform_or_defer_access_check (TREE_PURPOSE (check), TREE_VALUE (check));
15188 /* Set the scope from the stored value. */
15189 parser->scope = TREE_VALUE (value);
15190 parser->qualifying_scope = TREE_TYPE (value);
15191 parser->object_scope = NULL_TREE;
15194 /* Add tokens to CACHE until a non-nested END token appears. */
15197 cp_parser_cache_group (cp_parser *parser,
15198 cp_token_cache *cache,
15199 enum cpp_ttype end,
15206 /* Abort a parenthesized expression if we encounter a brace. */
15207 if ((end == CPP_CLOSE_PAREN || depth == 0)
15208 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
15210 /* If we've reached the end of the file, stop. */
15211 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
15213 /* Consume the next token. */
15214 token = cp_lexer_consume_token (parser->lexer);
15215 /* Add this token to the tokens we are saving. */
15216 cp_token_cache_push_token (cache, token);
15217 /* See if it starts a new group. */
15218 if (token->type == CPP_OPEN_BRACE)
15220 cp_parser_cache_group (parser, cache, CPP_CLOSE_BRACE, depth + 1);
15224 else if (token->type == CPP_OPEN_PAREN)
15225 cp_parser_cache_group (parser, cache, CPP_CLOSE_PAREN, depth + 1);
15226 else if (token->type == end)
15231 /* Begin parsing tentatively. We always save tokens while parsing
15232 tentatively so that if the tentative parsing fails we can restore the
15236 cp_parser_parse_tentatively (cp_parser* parser)
15238 /* Enter a new parsing context. */
15239 parser->context = cp_parser_context_new (parser->context);
15240 /* Begin saving tokens. */
15241 cp_lexer_save_tokens (parser->lexer);
15242 /* In order to avoid repetitive access control error messages,
15243 access checks are queued up until we are no longer parsing
15245 push_deferring_access_checks (dk_deferred);
15248 /* Commit to the currently active tentative parse. */
15251 cp_parser_commit_to_tentative_parse (cp_parser* parser)
15253 cp_parser_context *context;
15256 /* Mark all of the levels as committed. */
15257 lexer = parser->lexer;
15258 for (context = parser->context; context->next; context = context->next)
15260 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
15262 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
15263 while (!cp_lexer_saving_tokens (lexer))
15264 lexer = lexer->next;
15265 cp_lexer_commit_tokens (lexer);
15269 /* Abort the currently active tentative parse. All consumed tokens
15270 will be rolled back, and no diagnostics will be issued. */
15273 cp_parser_abort_tentative_parse (cp_parser* parser)
15275 cp_parser_simulate_error (parser);
15276 /* Now, pretend that we want to see if the construct was
15277 successfully parsed. */
15278 cp_parser_parse_definitely (parser);
15281 /* Stop parsing tentatively. If a parse error has occurred, restore the
15282 token stream. Otherwise, commit to the tokens we have consumed.
15283 Returns true if no error occurred; false otherwise. */
15286 cp_parser_parse_definitely (cp_parser* parser)
15288 bool error_occurred;
15289 cp_parser_context *context;
15291 /* Remember whether or not an error occurred, since we are about to
15292 destroy that information. */
15293 error_occurred = cp_parser_error_occurred (parser);
15294 /* Remove the topmost context from the stack. */
15295 context = parser->context;
15296 parser->context = context->next;
15297 /* If no parse errors occurred, commit to the tentative parse. */
15298 if (!error_occurred)
15300 /* Commit to the tokens read tentatively, unless that was
15302 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
15303 cp_lexer_commit_tokens (parser->lexer);
15305 pop_to_parent_deferring_access_checks ();
15307 /* Otherwise, if errors occurred, roll back our state so that things
15308 are just as they were before we began the tentative parse. */
15311 cp_lexer_rollback_tokens (parser->lexer);
15312 pop_deferring_access_checks ();
15314 /* Add the context to the front of the free list. */
15315 context->next = cp_parser_context_free_list;
15316 cp_parser_context_free_list = context;
15318 return !error_occurred;
15321 /* Returns true if we are parsing tentatively -- but have decided that
15322 we will stick with this tentative parse, even if errors occur. */
15325 cp_parser_committed_to_tentative_parse (cp_parser* parser)
15327 return (cp_parser_parsing_tentatively (parser)
15328 && parser->context->status == CP_PARSER_STATUS_KIND_COMMITTED);
15331 /* Returns nonzero iff an error has occurred during the most recent
15332 tentative parse. */
15335 cp_parser_error_occurred (cp_parser* parser)
15337 return (cp_parser_parsing_tentatively (parser)
15338 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
15341 /* Returns nonzero if GNU extensions are allowed. */
15344 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
15346 return parser->allow_gnu_extensions_p;
15352 static GTY (()) cp_parser *the_parser;
15354 /* External interface. */
15356 /* Parse one entire translation unit. */
15359 c_parse_file (void)
15361 bool error_occurred;
15362 static bool already_called = false;
15364 if (already_called)
15366 sorry ("inter-module optimizations not implemented for C++");
15369 already_called = true;
15371 the_parser = cp_parser_new ();
15372 push_deferring_access_checks (flag_access_control
15373 ? dk_no_deferred : dk_no_check);
15374 error_occurred = cp_parser_translation_unit (the_parser);
15378 /* This variable must be provided by every front end. */
15382 #include "gt-cp-parser.h"