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 *);
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 tree cp_parser_non_integral_constant_expression
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 messgae 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 /* Issue an error message about the fact that THING appeared in a
1926 constant-expression. Returns ERROR_MARK_NODE. */
1929 cp_parser_non_integral_constant_expression (const char *thing)
1931 error ("%s cannot appear in a constant-expression", thing);
1932 return error_mark_node;
1935 /* Emit a diagnostic for an invalid type name. Consider also if it is
1936 qualified or not and the result of a lookup, to provide a better
1940 cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree scope, tree id)
1942 tree decl, old_scope;
1943 /* Try to lookup the identifier. */
1944 old_scope = parser->scope;
1945 parser->scope = scope;
1946 decl = cp_parser_lookup_name_simple (parser, id);
1947 parser->scope = old_scope;
1948 /* If the lookup found a template-name, it means that the user forgot
1949 to specify an argument list. Emit an useful error message. */
1950 if (TREE_CODE (decl) == TEMPLATE_DECL)
1951 error ("invalid use of template-name `%E' without an argument list",
1953 else if (!parser->scope)
1955 /* Issue an error message. */
1956 error ("`%E' does not name a type", id);
1957 /* If we're in a template class, it's possible that the user was
1958 referring to a type from a base class. For example:
1960 template <typename T> struct A { typedef T X; };
1961 template <typename T> struct B : public A<T> { X x; };
1963 The user should have said "typename A<T>::X". */
1964 if (processing_template_decl && current_class_type)
1968 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
1972 tree base_type = BINFO_TYPE (b);
1973 if (CLASS_TYPE_P (base_type)
1974 && dependent_type_p (base_type))
1977 /* Go from a particular instantiation of the
1978 template (which will have an empty TYPE_FIELDs),
1979 to the main version. */
1980 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
1981 for (field = TYPE_FIELDS (base_type);
1983 field = TREE_CHAIN (field))
1984 if (TREE_CODE (field) == TYPE_DECL
1985 && DECL_NAME (field) == id)
1987 inform ("(perhaps `typename %T::%E' was intended)",
1988 BINFO_TYPE (b), id);
1997 /* Here we diagnose qualified-ids where the scope is actually correct,
1998 but the identifier does not resolve to a valid type name. */
2001 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2002 error ("`%E' in namespace `%E' does not name a type",
2004 else if (TYPE_P (parser->scope))
2005 error ("`%E' in class `%T' does not name a type",
2012 /* Check for a common situation where a type-name should be present,
2013 but is not, and issue a sensible error message. Returns true if an
2014 invalid type-name was detected.
2016 The situation handled by this function are variable declarations of the
2017 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2018 Usually, `ID' should name a type, but if we got here it means that it
2019 does not. We try to emit the best possible error message depending on
2020 how exactly the id-expression looks like.
2024 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2028 cp_parser_parse_tentatively (parser);
2029 id = cp_parser_id_expression (parser,
2030 /*template_keyword_p=*/false,
2031 /*check_dependency_p=*/true,
2032 /*template_p=*/NULL,
2033 /*declarator_p=*/true);
2034 /* After the id-expression, there should be a plain identifier,
2035 otherwise this is not a simple variable declaration. Also, if
2036 the scope is dependent, we cannot do much. */
2037 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME)
2038 || (parser->scope && TYPE_P (parser->scope)
2039 && dependent_type_p (parser->scope)))
2041 cp_parser_abort_tentative_parse (parser);
2044 if (!cp_parser_parse_definitely (parser))
2047 /* If we got here, this cannot be a valid variable declaration, thus
2048 the cp_parser_id_expression must have resolved to a plain identifier
2049 node (not a TYPE_DECL or TEMPLATE_ID_EXPR). */
2050 my_friendly_assert (TREE_CODE (id) == IDENTIFIER_NODE, 20030203);
2051 /* Emit a diagnostic for the invalid type. */
2052 cp_parser_diagnose_invalid_type_name (parser, parser->scope, id);
2053 /* Skip to the end of the declaration; there's no point in
2054 trying to process it. */
2055 cp_parser_skip_to_end_of_block_or_statement (parser);
2059 /* Consume tokens up to, and including, the next non-nested closing `)'.
2060 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
2061 are doing error recovery. Returns -1 if OR_COMMA is true and we
2062 found an unnested comma. */
2065 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2070 unsigned paren_depth = 0;
2071 unsigned brace_depth = 0;
2073 if (recovering && !or_comma && cp_parser_parsing_tentatively (parser)
2074 && !cp_parser_committed_to_tentative_parse (parser))
2081 /* If we've run out of tokens, then there is no closing `)'. */
2082 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2085 token = cp_lexer_peek_token (parser->lexer);
2087 /* This matches the processing in skip_to_end_of_statement. */
2088 if (token->type == CPP_SEMICOLON && !brace_depth)
2090 if (token->type == CPP_OPEN_BRACE)
2092 if (token->type == CPP_CLOSE_BRACE)
2097 if (recovering && or_comma && token->type == CPP_COMMA
2098 && !brace_depth && !paren_depth)
2103 /* If it is an `(', we have entered another level of nesting. */
2104 if (token->type == CPP_OPEN_PAREN)
2106 /* If it is a `)', then we might be done. */
2107 else if (token->type == CPP_CLOSE_PAREN && !paren_depth--)
2110 cp_lexer_consume_token (parser->lexer);
2115 /* Consume the token. */
2116 cp_lexer_consume_token (parser->lexer);
2120 /* Consume tokens until we reach the end of the current statement.
2121 Normally, that will be just before consuming a `;'. However, if a
2122 non-nested `}' comes first, then we stop before consuming that. */
2125 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2127 unsigned nesting_depth = 0;
2133 /* Peek at the next token. */
2134 token = cp_lexer_peek_token (parser->lexer);
2135 /* If we've run out of tokens, stop. */
2136 if (token->type == CPP_EOF)
2138 /* If the next token is a `;', we have reached the end of the
2140 if (token->type == CPP_SEMICOLON && !nesting_depth)
2142 /* If the next token is a non-nested `}', then we have reached
2143 the end of the current block. */
2144 if (token->type == CPP_CLOSE_BRACE)
2146 /* If this is a non-nested `}', stop before consuming it.
2147 That way, when confronted with something like:
2151 we stop before consuming the closing `}', even though we
2152 have not yet reached a `;'. */
2153 if (nesting_depth == 0)
2155 /* If it is the closing `}' for a block that we have
2156 scanned, stop -- but only after consuming the token.
2162 we will stop after the body of the erroneously declared
2163 function, but before consuming the following `typedef'
2165 if (--nesting_depth == 0)
2167 cp_lexer_consume_token (parser->lexer);
2171 /* If it the next token is a `{', then we are entering a new
2172 block. Consume the entire block. */
2173 else if (token->type == CPP_OPEN_BRACE)
2175 /* Consume the token. */
2176 cp_lexer_consume_token (parser->lexer);
2180 /* This function is called at the end of a statement or declaration.
2181 If the next token is a semicolon, it is consumed; otherwise, error
2182 recovery is attempted. */
2185 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2187 /* Look for the trailing `;'. */
2188 if (!cp_parser_require (parser, CPP_SEMICOLON, "`;'"))
2190 /* If there is additional (erroneous) input, skip to the end of
2192 cp_parser_skip_to_end_of_statement (parser);
2193 /* If the next token is now a `;', consume it. */
2194 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2195 cp_lexer_consume_token (parser->lexer);
2199 /* Skip tokens until we have consumed an entire block, or until we
2200 have consumed a non-nested `;'. */
2203 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2205 unsigned nesting_depth = 0;
2211 /* Peek at the next token. */
2212 token = cp_lexer_peek_token (parser->lexer);
2213 /* If we've run out of tokens, stop. */
2214 if (token->type == CPP_EOF)
2216 /* If the next token is a `;', we have reached the end of the
2218 if (token->type == CPP_SEMICOLON && !nesting_depth)
2220 /* Consume the `;'. */
2221 cp_lexer_consume_token (parser->lexer);
2224 /* Consume the token. */
2225 token = cp_lexer_consume_token (parser->lexer);
2226 /* If the next token is a non-nested `}', then we have reached
2227 the end of the current block. */
2228 if (token->type == CPP_CLOSE_BRACE
2229 && (nesting_depth == 0 || --nesting_depth == 0))
2231 /* If it the next token is a `{', then we are entering a new
2232 block. Consume the entire block. */
2233 if (token->type == CPP_OPEN_BRACE)
2238 /* Skip tokens until a non-nested closing curly brace is the next
2242 cp_parser_skip_to_closing_brace (cp_parser *parser)
2244 unsigned nesting_depth = 0;
2250 /* Peek at the next token. */
2251 token = cp_lexer_peek_token (parser->lexer);
2252 /* If we've run out of tokens, stop. */
2253 if (token->type == CPP_EOF)
2255 /* If the next token is a non-nested `}', then we have reached
2256 the end of the current block. */
2257 if (token->type == CPP_CLOSE_BRACE && nesting_depth-- == 0)
2259 /* If it the next token is a `{', then we are entering a new
2260 block. Consume the entire block. */
2261 else if (token->type == CPP_OPEN_BRACE)
2263 /* Consume the token. */
2264 cp_lexer_consume_token (parser->lexer);
2268 /* This is a simple wrapper around make_typename_type. When the id is
2269 an unresolved identifier node, we can provide a superior diagnostic
2270 using cp_parser_diagnose_invalid_type_name. */
2273 cp_parser_make_typename_type (cp_parser *parser, tree scope, tree id)
2276 if (TREE_CODE (id) == IDENTIFIER_NODE)
2278 result = make_typename_type (scope, id, /*complain=*/0);
2279 if (result == error_mark_node)
2280 cp_parser_diagnose_invalid_type_name (parser, scope, id);
2283 return make_typename_type (scope, id, tf_error);
2287 /* Create a new C++ parser. */
2290 cp_parser_new (void)
2295 /* cp_lexer_new_main is called before calling ggc_alloc because
2296 cp_lexer_new_main might load a PCH file. */
2297 lexer = cp_lexer_new_main ();
2299 parser = ggc_alloc_cleared (sizeof (cp_parser));
2300 parser->lexer = lexer;
2301 parser->context = cp_parser_context_new (NULL);
2303 /* For now, we always accept GNU extensions. */
2304 parser->allow_gnu_extensions_p = 1;
2306 /* The `>' token is a greater-than operator, not the end of a
2308 parser->greater_than_is_operator_p = true;
2310 parser->default_arg_ok_p = true;
2312 /* We are not parsing a constant-expression. */
2313 parser->integral_constant_expression_p = false;
2314 parser->allow_non_integral_constant_expression_p = false;
2315 parser->non_integral_constant_expression_p = false;
2317 /* We are not parsing offsetof. */
2318 parser->in_offsetof_p = false;
2320 /* Local variable names are not forbidden. */
2321 parser->local_variables_forbidden_p = false;
2323 /* We are not processing an `extern "C"' declaration. */
2324 parser->in_unbraced_linkage_specification_p = false;
2326 /* We are not processing a declarator. */
2327 parser->in_declarator_p = false;
2329 /* We are not processing a template-argument-list. */
2330 parser->in_template_argument_list_p = false;
2332 /* We are not in an iteration statement. */
2333 parser->in_iteration_statement_p = false;
2335 /* We are not in a switch statement. */
2336 parser->in_switch_statement_p = false;
2338 /* We are not parsing a type-id inside an expression. */
2339 parser->in_type_id_in_expr_p = false;
2341 /* The unparsed function queue is empty. */
2342 parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2344 /* There are no classes being defined. */
2345 parser->num_classes_being_defined = 0;
2347 /* No template parameters apply. */
2348 parser->num_template_parameter_lists = 0;
2353 /* Lexical conventions [gram.lex] */
2355 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
2359 cp_parser_identifier (cp_parser* parser)
2363 /* Look for the identifier. */
2364 token = cp_parser_require (parser, CPP_NAME, "identifier");
2365 /* Return the value. */
2366 return token ? token->value : error_mark_node;
2369 /* Basic concepts [gram.basic] */
2371 /* Parse a translation-unit.
2374 declaration-seq [opt]
2376 Returns TRUE if all went well. */
2379 cp_parser_translation_unit (cp_parser* parser)
2383 cp_parser_declaration_seq_opt (parser);
2385 /* If there are no tokens left then all went well. */
2386 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2389 /* Otherwise, issue an error message. */
2390 cp_parser_error (parser, "expected declaration");
2394 /* Consume the EOF token. */
2395 cp_parser_require (parser, CPP_EOF, "end-of-file");
2398 finish_translation_unit ();
2400 /* All went well. */
2404 /* Expressions [gram.expr] */
2406 /* Parse a primary-expression.
2417 ( compound-statement )
2418 __builtin_va_arg ( assignment-expression , type-id )
2423 Returns a representation of the expression.
2425 *IDK indicates what kind of id-expression (if any) was present.
2427 *QUALIFYING_CLASS is set to a non-NULL value if the id-expression can be
2428 used as the operand of a pointer-to-member. In that case,
2429 *QUALIFYING_CLASS gives the class that is used as the qualifying
2430 class in the pointer-to-member. */
2433 cp_parser_primary_expression (cp_parser *parser,
2435 tree *qualifying_class)
2439 /* Assume the primary expression is not an id-expression. */
2440 *idk = CP_ID_KIND_NONE;
2441 /* And that it cannot be used as pointer-to-member. */
2442 *qualifying_class = NULL_TREE;
2444 /* Peek at the next token. */
2445 token = cp_lexer_peek_token (parser->lexer);
2446 switch (token->type)
2459 token = cp_lexer_consume_token (parser->lexer);
2460 return token->value;
2462 case CPP_OPEN_PAREN:
2465 bool saved_greater_than_is_operator_p;
2467 /* Consume the `('. */
2468 cp_lexer_consume_token (parser->lexer);
2469 /* Within a parenthesized expression, a `>' token is always
2470 the greater-than operator. */
2471 saved_greater_than_is_operator_p
2472 = parser->greater_than_is_operator_p;
2473 parser->greater_than_is_operator_p = true;
2474 /* If we see `( { ' then we are looking at the beginning of
2475 a GNU statement-expression. */
2476 if (cp_parser_allow_gnu_extensions_p (parser)
2477 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
2479 /* Statement-expressions are not allowed by the standard. */
2481 pedwarn ("ISO C++ forbids braced-groups within expressions");
2483 /* And they're not allowed outside of a function-body; you
2484 cannot, for example, write:
2486 int i = ({ int j = 3; j + 1; });
2488 at class or namespace scope. */
2489 if (!at_function_scope_p ())
2490 error ("statement-expressions are allowed only inside functions");
2491 /* Start the statement-expression. */
2492 expr = begin_stmt_expr ();
2493 /* Parse the compound-statement. */
2494 cp_parser_compound_statement (parser, true);
2496 expr = finish_stmt_expr (expr, false);
2500 /* Parse the parenthesized expression. */
2501 expr = cp_parser_expression (parser);
2502 /* Let the front end know that this expression was
2503 enclosed in parentheses. This matters in case, for
2504 example, the expression is of the form `A::B', since
2505 `&A::B' might be a pointer-to-member, but `&(A::B)' is
2507 finish_parenthesized_expr (expr);
2509 /* The `>' token might be the end of a template-id or
2510 template-parameter-list now. */
2511 parser->greater_than_is_operator_p
2512 = saved_greater_than_is_operator_p;
2513 /* Consume the `)'. */
2514 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
2515 cp_parser_skip_to_end_of_statement (parser);
2521 switch (token->keyword)
2523 /* These two are the boolean literals. */
2525 cp_lexer_consume_token (parser->lexer);
2526 return boolean_true_node;
2528 cp_lexer_consume_token (parser->lexer);
2529 return boolean_false_node;
2531 /* The `__null' literal. */
2533 cp_lexer_consume_token (parser->lexer);
2536 /* Recognize the `this' keyword. */
2538 cp_lexer_consume_token (parser->lexer);
2539 if (parser->local_variables_forbidden_p)
2541 error ("`this' may not be used in this context");
2542 return error_mark_node;
2544 /* Pointers cannot appear in constant-expressions. */
2545 if (parser->integral_constant_expression_p)
2547 if (!parser->allow_non_integral_constant_expression_p)
2548 return cp_parser_non_integral_constant_expression ("`this'");
2549 parser->non_integral_constant_expression_p = true;
2551 return finish_this_expr ();
2553 /* The `operator' keyword can be the beginning of an
2558 case RID_FUNCTION_NAME:
2559 case RID_PRETTY_FUNCTION_NAME:
2560 case RID_C99_FUNCTION_NAME:
2561 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
2562 __func__ are the names of variables -- but they are
2563 treated specially. Therefore, they are handled here,
2564 rather than relying on the generic id-expression logic
2565 below. Grammatically, these names are id-expressions.
2567 Consume the token. */
2568 token = cp_lexer_consume_token (parser->lexer);
2569 /* Look up the name. */
2570 return finish_fname (token->value);
2577 /* The `__builtin_va_arg' construct is used to handle
2578 `va_arg'. Consume the `__builtin_va_arg' token. */
2579 cp_lexer_consume_token (parser->lexer);
2580 /* Look for the opening `('. */
2581 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
2582 /* Now, parse the assignment-expression. */
2583 expression = cp_parser_assignment_expression (parser);
2584 /* Look for the `,'. */
2585 cp_parser_require (parser, CPP_COMMA, "`,'");
2586 /* Parse the type-id. */
2587 type = cp_parser_type_id (parser);
2588 /* Look for the closing `)'. */
2589 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
2590 /* Using `va_arg' in a constant-expression is not
2592 if (parser->integral_constant_expression_p)
2594 if (!parser->allow_non_integral_constant_expression_p)
2595 return cp_parser_non_integral_constant_expression ("`va_arg'");
2596 parser->non_integral_constant_expression_p = true;
2598 return build_x_va_arg (expression, type);
2604 bool saved_in_offsetof_p;
2606 /* Consume the "__offsetof__" token. */
2607 cp_lexer_consume_token (parser->lexer);
2608 /* Consume the opening `('. */
2609 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
2610 /* Parse the parenthesized (almost) constant-expression. */
2611 saved_in_offsetof_p = parser->in_offsetof_p;
2612 parser->in_offsetof_p = true;
2614 = cp_parser_constant_expression (parser,
2615 /*allow_non_constant_p=*/false,
2616 /*non_constant_p=*/NULL);
2617 parser->in_offsetof_p = saved_in_offsetof_p;
2618 /* Consume the closing ')'. */
2619 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
2625 cp_parser_error (parser, "expected primary-expression");
2626 return error_mark_node;
2629 /* An id-expression can start with either an identifier, a
2630 `::' as the beginning of a qualified-id, or the "operator"
2634 case CPP_TEMPLATE_ID:
2635 case CPP_NESTED_NAME_SPECIFIER:
2639 const char *error_msg;
2642 /* Parse the id-expression. */
2644 = cp_parser_id_expression (parser,
2645 /*template_keyword_p=*/false,
2646 /*check_dependency_p=*/true,
2647 /*template_p=*/NULL,
2648 /*declarator_p=*/false);
2649 if (id_expression == error_mark_node)
2650 return error_mark_node;
2651 /* If we have a template-id, then no further lookup is
2652 required. If the template-id was for a template-class, we
2653 will sometimes have a TYPE_DECL at this point. */
2654 else if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
2655 || TREE_CODE (id_expression) == TYPE_DECL)
2656 decl = id_expression;
2657 /* Look up the name. */
2660 decl = cp_parser_lookup_name_simple (parser, id_expression);
2661 /* If name lookup gives us a SCOPE_REF, then the
2662 qualifying scope was dependent. Just propagate the
2664 if (TREE_CODE (decl) == SCOPE_REF)
2666 if (TYPE_P (TREE_OPERAND (decl, 0)))
2667 *qualifying_class = TREE_OPERAND (decl, 0);
2670 /* Check to see if DECL is a local variable in a context
2671 where that is forbidden. */
2672 if (parser->local_variables_forbidden_p
2673 && local_variable_p (decl))
2675 /* It might be that we only found DECL because we are
2676 trying to be generous with pre-ISO scoping rules.
2677 For example, consider:
2681 for (int i = 0; i < 10; ++i) {}
2682 extern void f(int j = i);
2685 Here, name look up will originally find the out
2686 of scope `i'. We need to issue a warning message,
2687 but then use the global `i'. */
2688 decl = check_for_out_of_scope_variable (decl);
2689 if (local_variable_p (decl))
2691 error ("local variable `%D' may not appear in this context",
2693 return error_mark_node;
2698 decl = finish_id_expression (id_expression, decl, parser->scope,
2699 idk, qualifying_class,
2700 parser->integral_constant_expression_p,
2701 parser->allow_non_integral_constant_expression_p,
2702 &parser->non_integral_constant_expression_p,
2705 cp_parser_error (parser, error_msg);
2709 /* Anything else is an error. */
2711 cp_parser_error (parser, "expected primary-expression");
2712 return error_mark_node;
2716 /* Parse an id-expression.
2723 :: [opt] nested-name-specifier template [opt] unqualified-id
2725 :: operator-function-id
2728 Return a representation of the unqualified portion of the
2729 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
2730 a `::' or nested-name-specifier.
2732 Often, if the id-expression was a qualified-id, the caller will
2733 want to make a SCOPE_REF to represent the qualified-id. This
2734 function does not do this in order to avoid wastefully creating
2735 SCOPE_REFs when they are not required.
2737 If TEMPLATE_KEYWORD_P is true, then we have just seen the
2740 If CHECK_DEPENDENCY_P is false, then names are looked up inside
2741 uninstantiated templates.
2743 If *TEMPLATE_P is non-NULL, it is set to true iff the
2744 `template' keyword is used to explicitly indicate that the entity
2745 named is a template.
2747 If DECLARATOR_P is true, the id-expression is appearing as part of
2748 a declarator, rather than as part of an expression. */
2751 cp_parser_id_expression (cp_parser *parser,
2752 bool template_keyword_p,
2753 bool check_dependency_p,
2757 bool global_scope_p;
2758 bool nested_name_specifier_p;
2760 /* Assume the `template' keyword was not used. */
2762 *template_p = false;
2764 /* Look for the optional `::' operator. */
2766 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
2768 /* Look for the optional nested-name-specifier. */
2769 nested_name_specifier_p
2770 = (cp_parser_nested_name_specifier_opt (parser,
2771 /*typename_keyword_p=*/false,
2774 /*is_declarator=*/false)
2776 /* If there is a nested-name-specifier, then we are looking at
2777 the first qualified-id production. */
2778 if (nested_name_specifier_p)
2781 tree saved_object_scope;
2782 tree saved_qualifying_scope;
2783 tree unqualified_id;
2786 /* See if the next token is the `template' keyword. */
2788 template_p = &is_template;
2789 *template_p = cp_parser_optional_template_keyword (parser);
2790 /* Name lookup we do during the processing of the
2791 unqualified-id might obliterate SCOPE. */
2792 saved_scope = parser->scope;
2793 saved_object_scope = parser->object_scope;
2794 saved_qualifying_scope = parser->qualifying_scope;
2795 /* Process the final unqualified-id. */
2796 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
2799 /* Restore the SAVED_SCOPE for our caller. */
2800 parser->scope = saved_scope;
2801 parser->object_scope = saved_object_scope;
2802 parser->qualifying_scope = saved_qualifying_scope;
2804 return unqualified_id;
2806 /* Otherwise, if we are in global scope, then we are looking at one
2807 of the other qualified-id productions. */
2808 else if (global_scope_p)
2813 /* Peek at the next token. */
2814 token = cp_lexer_peek_token (parser->lexer);
2816 /* If it's an identifier, and the next token is not a "<", then
2817 we can avoid the template-id case. This is an optimization
2818 for this common case. */
2819 if (token->type == CPP_NAME
2820 && !cp_parser_nth_token_starts_template_argument_list_p
2822 return cp_parser_identifier (parser);
2824 cp_parser_parse_tentatively (parser);
2825 /* Try a template-id. */
2826 id = cp_parser_template_id (parser,
2827 /*template_keyword_p=*/false,
2828 /*check_dependency_p=*/true,
2830 /* If that worked, we're done. */
2831 if (cp_parser_parse_definitely (parser))
2834 /* Peek at the next token. (Changes in the token buffer may
2835 have invalidated the pointer obtained above.) */
2836 token = cp_lexer_peek_token (parser->lexer);
2838 switch (token->type)
2841 return cp_parser_identifier (parser);
2844 if (token->keyword == RID_OPERATOR)
2845 return cp_parser_operator_function_id (parser);
2849 cp_parser_error (parser, "expected id-expression");
2850 return error_mark_node;
2854 return cp_parser_unqualified_id (parser, template_keyword_p,
2855 /*check_dependency_p=*/true,
2859 /* Parse an unqualified-id.
2863 operator-function-id
2864 conversion-function-id
2868 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
2869 keyword, in a construct like `A::template ...'.
2871 Returns a representation of unqualified-id. For the `identifier'
2872 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
2873 production a BIT_NOT_EXPR is returned; the operand of the
2874 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
2875 other productions, see the documentation accompanying the
2876 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
2877 names are looked up in uninstantiated templates. If DECLARATOR_P
2878 is true, the unqualified-id is appearing as part of a declarator,
2879 rather than as part of an expression. */
2882 cp_parser_unqualified_id (cp_parser* parser,
2883 bool template_keyword_p,
2884 bool check_dependency_p,
2889 /* Peek at the next token. */
2890 token = cp_lexer_peek_token (parser->lexer);
2892 switch (token->type)
2898 /* We don't know yet whether or not this will be a
2900 cp_parser_parse_tentatively (parser);
2901 /* Try a template-id. */
2902 id = cp_parser_template_id (parser, template_keyword_p,
2905 /* If it worked, we're done. */
2906 if (cp_parser_parse_definitely (parser))
2908 /* Otherwise, it's an ordinary identifier. */
2909 return cp_parser_identifier (parser);
2912 case CPP_TEMPLATE_ID:
2913 return cp_parser_template_id (parser, template_keyword_p,
2920 tree qualifying_scope;
2924 /* Consume the `~' token. */
2925 cp_lexer_consume_token (parser->lexer);
2926 /* Parse the class-name. The standard, as written, seems to
2929 template <typename T> struct S { ~S (); };
2930 template <typename T> S<T>::~S() {}
2932 is invalid, since `~' must be followed by a class-name, but
2933 `S<T>' is dependent, and so not known to be a class.
2934 That's not right; we need to look in uninstantiated
2935 templates. A further complication arises from:
2937 template <typename T> void f(T t) {
2941 Here, it is not possible to look up `T' in the scope of `T'
2942 itself. We must look in both the current scope, and the
2943 scope of the containing complete expression.
2945 Yet another issue is:
2954 The standard does not seem to say that the `S' in `~S'
2955 should refer to the type `S' and not the data member
2958 /* DR 244 says that we look up the name after the "~" in the
2959 same scope as we looked up the qualifying name. That idea
2960 isn't fully worked out; it's more complicated than that. */
2961 scope = parser->scope;
2962 object_scope = parser->object_scope;
2963 qualifying_scope = parser->qualifying_scope;
2965 /* If the name is of the form "X::~X" it's OK. */
2966 if (scope && TYPE_P (scope)
2967 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
2968 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
2970 && (cp_lexer_peek_token (parser->lexer)->value
2971 == TYPE_IDENTIFIER (scope)))
2973 cp_lexer_consume_token (parser->lexer);
2974 return build_nt (BIT_NOT_EXPR, scope);
2977 /* If there was an explicit qualification (S::~T), first look
2978 in the scope given by the qualification (i.e., S). */
2981 cp_parser_parse_tentatively (parser);
2982 type_decl = cp_parser_class_name (parser,
2983 /*typename_keyword_p=*/false,
2984 /*template_keyword_p=*/false,
2986 /*check_dependency=*/false,
2987 /*class_head_p=*/false,
2989 if (cp_parser_parse_definitely (parser))
2990 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
2992 /* In "N::S::~S", look in "N" as well. */
2993 if (scope && qualifying_scope)
2995 cp_parser_parse_tentatively (parser);
2996 parser->scope = qualifying_scope;
2997 parser->object_scope = NULL_TREE;
2998 parser->qualifying_scope = NULL_TREE;
3000 = cp_parser_class_name (parser,
3001 /*typename_keyword_p=*/false,
3002 /*template_keyword_p=*/false,
3004 /*check_dependency=*/false,
3005 /*class_head_p=*/false,
3007 if (cp_parser_parse_definitely (parser))
3008 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3010 /* In "p->S::~T", look in the scope given by "*p" as well. */
3011 else if (object_scope)
3013 cp_parser_parse_tentatively (parser);
3014 parser->scope = object_scope;
3015 parser->object_scope = NULL_TREE;
3016 parser->qualifying_scope = NULL_TREE;
3018 = cp_parser_class_name (parser,
3019 /*typename_keyword_p=*/false,
3020 /*template_keyword_p=*/false,
3022 /*check_dependency=*/false,
3023 /*class_head_p=*/false,
3025 if (cp_parser_parse_definitely (parser))
3026 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3028 /* Look in the surrounding context. */
3029 parser->scope = NULL_TREE;
3030 parser->object_scope = NULL_TREE;
3031 parser->qualifying_scope = NULL_TREE;
3033 = cp_parser_class_name (parser,
3034 /*typename_keyword_p=*/false,
3035 /*template_keyword_p=*/false,
3037 /*check_dependency=*/false,
3038 /*class_head_p=*/false,
3040 /* If an error occurred, assume that the name of the
3041 destructor is the same as the name of the qualifying
3042 class. That allows us to keep parsing after running
3043 into ill-formed destructor names. */
3044 if (type_decl == error_mark_node && scope && TYPE_P (scope))
3045 return build_nt (BIT_NOT_EXPR, scope);
3046 else if (type_decl == error_mark_node)
3047 return error_mark_node;
3051 A typedef-name that names a class shall not be used as the
3052 identifier in the declarator for a destructor declaration. */
3054 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
3055 && !DECL_SELF_REFERENCE_P (type_decl))
3056 error ("typedef-name `%D' used as destructor declarator",
3059 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3063 if (token->keyword == RID_OPERATOR)
3067 /* This could be a template-id, so we try that first. */
3068 cp_parser_parse_tentatively (parser);
3069 /* Try a template-id. */
3070 id = cp_parser_template_id (parser, template_keyword_p,
3071 /*check_dependency_p=*/true,
3073 /* If that worked, we're done. */
3074 if (cp_parser_parse_definitely (parser))
3076 /* We still don't know whether we're looking at an
3077 operator-function-id or a conversion-function-id. */
3078 cp_parser_parse_tentatively (parser);
3079 /* Try an operator-function-id. */
3080 id = cp_parser_operator_function_id (parser);
3081 /* If that didn't work, try a conversion-function-id. */
3082 if (!cp_parser_parse_definitely (parser))
3083 id = cp_parser_conversion_function_id (parser);
3090 cp_parser_error (parser, "expected unqualified-id");
3091 return error_mark_node;
3095 /* Parse an (optional) nested-name-specifier.
3097 nested-name-specifier:
3098 class-or-namespace-name :: nested-name-specifier [opt]
3099 class-or-namespace-name :: template nested-name-specifier [opt]
3101 PARSER->SCOPE should be set appropriately before this function is
3102 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
3103 effect. TYPE_P is TRUE if we non-type bindings should be ignored
3106 Sets PARSER->SCOPE to the class (TYPE) or namespace
3107 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
3108 it unchanged if there is no nested-name-specifier. Returns the new
3109 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
3111 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
3112 part of a declaration and/or decl-specifier. */
3115 cp_parser_nested_name_specifier_opt (cp_parser *parser,
3116 bool typename_keyword_p,
3117 bool check_dependency_p,
3119 bool is_declaration)
3121 bool success = false;
3122 tree access_check = NULL_TREE;
3126 /* If the next token corresponds to a nested name specifier, there
3127 is no need to reparse it. However, if CHECK_DEPENDENCY_P is
3128 false, it may have been true before, in which case something
3129 like `A<X>::B<Y>::C' may have resulted in a nested-name-specifier
3130 of `A<X>::', where it should now be `A<X>::B<Y>::'. So, when
3131 CHECK_DEPENDENCY_P is false, we have to fall through into the
3133 if (check_dependency_p
3134 && cp_lexer_next_token_is (parser->lexer, CPP_NESTED_NAME_SPECIFIER))
3136 cp_parser_pre_parsed_nested_name_specifier (parser);
3137 return parser->scope;
3140 /* Remember where the nested-name-specifier starts. */
3141 if (cp_parser_parsing_tentatively (parser)
3142 && !cp_parser_committed_to_tentative_parse (parser))
3144 token = cp_lexer_peek_token (parser->lexer);
3145 start = cp_lexer_token_difference (parser->lexer,
3146 parser->lexer->first_token,
3152 push_deferring_access_checks (dk_deferred);
3158 tree saved_qualifying_scope;
3159 bool template_keyword_p;
3161 /* Spot cases that cannot be the beginning of a
3162 nested-name-specifier. */
3163 token = cp_lexer_peek_token (parser->lexer);
3165 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
3166 the already parsed nested-name-specifier. */
3167 if (token->type == CPP_NESTED_NAME_SPECIFIER)
3169 /* Grab the nested-name-specifier and continue the loop. */
3170 cp_parser_pre_parsed_nested_name_specifier (parser);
3175 /* Spot cases that cannot be the beginning of a
3176 nested-name-specifier. On the second and subsequent times
3177 through the loop, we look for the `template' keyword. */
3178 if (success && token->keyword == RID_TEMPLATE)
3180 /* A template-id can start a nested-name-specifier. */
3181 else if (token->type == CPP_TEMPLATE_ID)
3185 /* If the next token is not an identifier, then it is
3186 definitely not a class-or-namespace-name. */
3187 if (token->type != CPP_NAME)
3189 /* If the following token is neither a `<' (to begin a
3190 template-id), nor a `::', then we are not looking at a
3191 nested-name-specifier. */
3192 token = cp_lexer_peek_nth_token (parser->lexer, 2);
3193 if (token->type != CPP_SCOPE
3194 && !cp_parser_nth_token_starts_template_argument_list_p
3199 /* The nested-name-specifier is optional, so we parse
3201 cp_parser_parse_tentatively (parser);
3203 /* Look for the optional `template' keyword, if this isn't the
3204 first time through the loop. */
3206 template_keyword_p = cp_parser_optional_template_keyword (parser);
3208 template_keyword_p = false;
3210 /* Save the old scope since the name lookup we are about to do
3211 might destroy it. */
3212 old_scope = parser->scope;
3213 saved_qualifying_scope = parser->qualifying_scope;
3214 /* Parse the qualifying entity. */
3216 = cp_parser_class_or_namespace_name (parser,
3222 /* Look for the `::' token. */
3223 cp_parser_require (parser, CPP_SCOPE, "`::'");
3225 /* If we found what we wanted, we keep going; otherwise, we're
3227 if (!cp_parser_parse_definitely (parser))
3229 bool error_p = false;
3231 /* Restore the OLD_SCOPE since it was valid before the
3232 failed attempt at finding the last
3233 class-or-namespace-name. */
3234 parser->scope = old_scope;
3235 parser->qualifying_scope = saved_qualifying_scope;
3236 /* If the next token is an identifier, and the one after
3237 that is a `::', then any valid interpretation would have
3238 found a class-or-namespace-name. */
3239 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
3240 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3242 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
3245 token = cp_lexer_consume_token (parser->lexer);
3250 decl = cp_parser_lookup_name_simple (parser, token->value);
3251 if (TREE_CODE (decl) == TEMPLATE_DECL)
3252 error ("`%D' used without template parameters",
3255 cp_parser_name_lookup_error
3256 (parser, token->value, decl,
3257 "is not a class or namespace");
3258 parser->scope = NULL_TREE;
3260 /* Treat this as a successful nested-name-specifier
3265 If the name found is not a class-name (clause
3266 _class_) or namespace-name (_namespace.def_), the
3267 program is ill-formed. */
3270 cp_lexer_consume_token (parser->lexer);
3275 /* We've found one valid nested-name-specifier. */
3277 /* Make sure we look in the right scope the next time through
3279 parser->scope = (TREE_CODE (new_scope) == TYPE_DECL
3280 ? TREE_TYPE (new_scope)
3282 /* If it is a class scope, try to complete it; we are about to
3283 be looking up names inside the class. */
3284 if (TYPE_P (parser->scope)
3285 /* Since checking types for dependency can be expensive,
3286 avoid doing it if the type is already complete. */
3287 && !COMPLETE_TYPE_P (parser->scope)
3288 /* Do not try to complete dependent types. */
3289 && !dependent_type_p (parser->scope))
3290 complete_type (parser->scope);
3293 /* Retrieve any deferred checks. Do not pop this access checks yet
3294 so the memory will not be reclaimed during token replacing below. */
3295 access_check = get_deferred_access_checks ();
3297 /* If parsing tentatively, replace the sequence of tokens that makes
3298 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
3299 token. That way, should we re-parse the token stream, we will
3300 not have to repeat the effort required to do the parse, nor will
3301 we issue duplicate error messages. */
3302 if (success && start >= 0)
3304 /* Find the token that corresponds to the start of the
3306 token = cp_lexer_advance_token (parser->lexer,
3307 parser->lexer->first_token,
3310 /* Reset the contents of the START token. */
3311 token->type = CPP_NESTED_NAME_SPECIFIER;
3312 token->value = build_tree_list (access_check, parser->scope);
3313 TREE_TYPE (token->value) = parser->qualifying_scope;
3314 token->keyword = RID_MAX;
3315 /* Purge all subsequent tokens. */
3316 cp_lexer_purge_tokens_after (parser->lexer, token);
3319 pop_deferring_access_checks ();
3320 return success ? parser->scope : NULL_TREE;
3323 /* Parse a nested-name-specifier. See
3324 cp_parser_nested_name_specifier_opt for details. This function
3325 behaves identically, except that it will an issue an error if no
3326 nested-name-specifier is present, and it will return
3327 ERROR_MARK_NODE, rather than NULL_TREE, if no nested-name-specifier
3331 cp_parser_nested_name_specifier (cp_parser *parser,
3332 bool typename_keyword_p,
3333 bool check_dependency_p,
3335 bool is_declaration)
3339 /* Look for the nested-name-specifier. */
3340 scope = cp_parser_nested_name_specifier_opt (parser,
3345 /* If it was not present, issue an error message. */
3348 cp_parser_error (parser, "expected nested-name-specifier");
3349 parser->scope = NULL_TREE;
3350 return error_mark_node;
3356 /* Parse a class-or-namespace-name.
3358 class-or-namespace-name:
3362 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
3363 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
3364 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
3365 TYPE_P is TRUE iff the next name should be taken as a class-name,
3366 even the same name is declared to be another entity in the same
3369 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
3370 specified by the class-or-namespace-name. If neither is found the
3371 ERROR_MARK_NODE is returned. */
3374 cp_parser_class_or_namespace_name (cp_parser *parser,
3375 bool typename_keyword_p,
3376 bool template_keyword_p,
3377 bool check_dependency_p,
3379 bool is_declaration)
3382 tree saved_qualifying_scope;
3383 tree saved_object_scope;
3387 /* Before we try to parse the class-name, we must save away the
3388 current PARSER->SCOPE since cp_parser_class_name will destroy
3390 saved_scope = parser->scope;
3391 saved_qualifying_scope = parser->qualifying_scope;
3392 saved_object_scope = parser->object_scope;
3393 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
3394 there is no need to look for a namespace-name. */
3395 only_class_p = template_keyword_p || (saved_scope && TYPE_P (saved_scope));
3397 cp_parser_parse_tentatively (parser);
3398 scope = cp_parser_class_name (parser,
3403 /*class_head_p=*/false,
3405 /* If that didn't work, try for a namespace-name. */
3406 if (!only_class_p && !cp_parser_parse_definitely (parser))
3408 /* Restore the saved scope. */
3409 parser->scope = saved_scope;
3410 parser->qualifying_scope = saved_qualifying_scope;
3411 parser->object_scope = saved_object_scope;
3412 /* If we are not looking at an identifier followed by the scope
3413 resolution operator, then this is not part of a
3414 nested-name-specifier. (Note that this function is only used
3415 to parse the components of a nested-name-specifier.) */
3416 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
3417 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
3418 return error_mark_node;
3419 scope = cp_parser_namespace_name (parser);
3425 /* Parse a postfix-expression.
3429 postfix-expression [ expression ]
3430 postfix-expression ( expression-list [opt] )
3431 simple-type-specifier ( expression-list [opt] )
3432 typename :: [opt] nested-name-specifier identifier
3433 ( expression-list [opt] )
3434 typename :: [opt] nested-name-specifier template [opt] template-id
3435 ( expression-list [opt] )
3436 postfix-expression . template [opt] id-expression
3437 postfix-expression -> template [opt] id-expression
3438 postfix-expression . pseudo-destructor-name
3439 postfix-expression -> pseudo-destructor-name
3440 postfix-expression ++
3441 postfix-expression --
3442 dynamic_cast < type-id > ( expression )
3443 static_cast < type-id > ( expression )
3444 reinterpret_cast < type-id > ( expression )
3445 const_cast < type-id > ( expression )
3446 typeid ( expression )
3452 ( type-id ) { initializer-list , [opt] }
3454 This extension is a GNU version of the C99 compound-literal
3455 construct. (The C99 grammar uses `type-name' instead of `type-id',
3456 but they are essentially the same concept.)
3458 If ADDRESS_P is true, the postfix expression is the operand of the
3461 Returns a representation of the expression. */
3464 cp_parser_postfix_expression (cp_parser *parser, bool address_p)
3468 cp_id_kind idk = CP_ID_KIND_NONE;
3469 tree postfix_expression = NULL_TREE;
3470 /* Non-NULL only if the current postfix-expression can be used to
3471 form a pointer-to-member. In that case, QUALIFYING_CLASS is the
3472 class used to qualify the member. */
3473 tree qualifying_class = NULL_TREE;
3475 /* Peek at the next token. */
3476 token = cp_lexer_peek_token (parser->lexer);
3477 /* Some of the productions are determined by keywords. */
3478 keyword = token->keyword;
3488 const char *saved_message;
3490 /* All of these can be handled in the same way from the point
3491 of view of parsing. Begin by consuming the token
3492 identifying the cast. */
3493 cp_lexer_consume_token (parser->lexer);
3495 /* New types cannot be defined in the cast. */
3496 saved_message = parser->type_definition_forbidden_message;
3497 parser->type_definition_forbidden_message
3498 = "types may not be defined in casts";
3500 /* Look for the opening `<'. */
3501 cp_parser_require (parser, CPP_LESS, "`<'");
3502 /* Parse the type to which we are casting. */
3503 type = cp_parser_type_id (parser);
3504 /* Look for the closing `>'. */
3505 cp_parser_require (parser, CPP_GREATER, "`>'");
3506 /* Restore the old message. */
3507 parser->type_definition_forbidden_message = saved_message;
3509 /* And the expression which is being cast. */
3510 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3511 expression = cp_parser_expression (parser);
3512 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3514 /* Only type conversions to integral or enumeration types
3515 can be used in constant-expressions. */
3516 if (parser->integral_constant_expression_p
3517 && !dependent_type_p (type)
3518 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type)
3519 /* A cast to pointer or reference type is allowed in the
3520 implementation of "offsetof". */
3521 && !(parser->in_offsetof_p && POINTER_TYPE_P (type)))
3523 if (!parser->allow_non_integral_constant_expression_p)
3524 return (cp_parser_non_integral_constant_expression
3525 ("a cast to a type other than an integral or "
3526 "enumeration type"));
3527 parser->non_integral_constant_expression_p = true;
3534 = build_dynamic_cast (type, expression);
3538 = build_static_cast (type, expression);
3542 = build_reinterpret_cast (type, expression);
3546 = build_const_cast (type, expression);
3557 const char *saved_message;
3558 bool saved_in_type_id_in_expr_p;
3560 /* Consume the `typeid' token. */
3561 cp_lexer_consume_token (parser->lexer);
3562 /* Look for the `(' token. */
3563 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3564 /* Types cannot be defined in a `typeid' expression. */
3565 saved_message = parser->type_definition_forbidden_message;
3566 parser->type_definition_forbidden_message
3567 = "types may not be defined in a `typeid\' expression";
3568 /* We can't be sure yet whether we're looking at a type-id or an
3570 cp_parser_parse_tentatively (parser);
3571 /* Try a type-id first. */
3572 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
3573 parser->in_type_id_in_expr_p = true;
3574 type = cp_parser_type_id (parser);
3575 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
3576 /* Look for the `)' token. Otherwise, we can't be sure that
3577 we're not looking at an expression: consider `typeid (int
3578 (3))', for example. */
3579 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3580 /* If all went well, simply lookup the type-id. */
3581 if (cp_parser_parse_definitely (parser))
3582 postfix_expression = get_typeid (type);
3583 /* Otherwise, fall back to the expression variant. */
3588 /* Look for an expression. */
3589 expression = cp_parser_expression (parser);
3590 /* Compute its typeid. */
3591 postfix_expression = build_typeid (expression);
3592 /* Look for the `)' token. */
3593 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3596 /* Restore the saved message. */
3597 parser->type_definition_forbidden_message = saved_message;
3603 bool template_p = false;
3607 /* Consume the `typename' token. */
3608 cp_lexer_consume_token (parser->lexer);
3609 /* Look for the optional `::' operator. */
3610 cp_parser_global_scope_opt (parser,
3611 /*current_scope_valid_p=*/false);
3612 /* Look for the nested-name-specifier. */
3613 cp_parser_nested_name_specifier (parser,
3614 /*typename_keyword_p=*/true,
3615 /*check_dependency_p=*/true,
3617 /*is_declaration=*/true);
3618 /* Look for the optional `template' keyword. */
3619 template_p = cp_parser_optional_template_keyword (parser);
3620 /* We don't know whether we're looking at a template-id or an
3622 cp_parser_parse_tentatively (parser);
3623 /* Try a template-id. */
3624 id = cp_parser_template_id (parser, template_p,
3625 /*check_dependency_p=*/true,
3626 /*is_declaration=*/true);
3627 /* If that didn't work, try an identifier. */
3628 if (!cp_parser_parse_definitely (parser))
3629 id = cp_parser_identifier (parser);
3630 /* Create a TYPENAME_TYPE to represent the type to which the
3631 functional cast is being performed. */
3632 type = make_typename_type (parser->scope, id,
3635 postfix_expression = cp_parser_functional_cast (parser, type);
3643 /* If the next thing is a simple-type-specifier, we may be
3644 looking at a functional cast. We could also be looking at
3645 an id-expression. So, we try the functional cast, and if
3646 that doesn't work we fall back to the primary-expression. */
3647 cp_parser_parse_tentatively (parser);
3648 /* Look for the simple-type-specifier. */
3649 type = cp_parser_simple_type_specifier (parser,
3650 CP_PARSER_FLAGS_NONE,
3651 /*identifier_p=*/false);
3652 /* Parse the cast itself. */
3653 if (!cp_parser_error_occurred (parser))
3655 = cp_parser_functional_cast (parser, type);
3656 /* If that worked, we're done. */
3657 if (cp_parser_parse_definitely (parser))
3660 /* If the functional-cast didn't work out, try a
3661 compound-literal. */
3662 if (cp_parser_allow_gnu_extensions_p (parser)
3663 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
3665 tree initializer_list = NULL_TREE;
3666 bool saved_in_type_id_in_expr_p;
3668 cp_parser_parse_tentatively (parser);
3669 /* Consume the `('. */
3670 cp_lexer_consume_token (parser->lexer);
3671 /* Parse the type. */
3672 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
3673 parser->in_type_id_in_expr_p = true;
3674 type = cp_parser_type_id (parser);
3675 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
3676 /* Look for the `)'. */
3677 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3678 /* Look for the `{'. */
3679 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
3680 /* If things aren't going well, there's no need to
3682 if (!cp_parser_error_occurred (parser))
3684 bool non_constant_p;
3685 /* Parse the initializer-list. */
3687 = cp_parser_initializer_list (parser, &non_constant_p);
3688 /* Allow a trailing `,'. */
3689 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
3690 cp_lexer_consume_token (parser->lexer);
3691 /* Look for the final `}'. */
3692 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
3694 /* If that worked, we're definitely looking at a
3695 compound-literal expression. */
3696 if (cp_parser_parse_definitely (parser))
3698 /* Warn the user that a compound literal is not
3699 allowed in standard C++. */
3701 pedwarn ("ISO C++ forbids compound-literals");
3702 /* Form the representation of the compound-literal. */
3704 = finish_compound_literal (type, initializer_list);
3709 /* It must be a primary-expression. */
3710 postfix_expression = cp_parser_primary_expression (parser,
3717 /* If we were avoiding committing to the processing of a
3718 qualified-id until we knew whether or not we had a
3719 pointer-to-member, we now know. */
3720 if (qualifying_class)
3724 /* Peek at the next token. */
3725 token = cp_lexer_peek_token (parser->lexer);
3726 done = (token->type != CPP_OPEN_SQUARE
3727 && token->type != CPP_OPEN_PAREN
3728 && token->type != CPP_DOT
3729 && token->type != CPP_DEREF
3730 && token->type != CPP_PLUS_PLUS
3731 && token->type != CPP_MINUS_MINUS);
3733 postfix_expression = finish_qualified_id_expr (qualifying_class,
3738 return postfix_expression;
3741 /* Keep looping until the postfix-expression is complete. */
3744 if (idk == CP_ID_KIND_UNQUALIFIED
3745 && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
3746 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
3747 /* It is not a Koenig lookup function call. */
3749 = unqualified_name_lookup_error (postfix_expression);
3751 /* Peek at the next token. */
3752 token = cp_lexer_peek_token (parser->lexer);
3754 switch (token->type)
3756 case CPP_OPEN_SQUARE:
3757 /* postfix-expression [ expression ] */
3761 /* Consume the `[' token. */
3762 cp_lexer_consume_token (parser->lexer);
3763 /* Parse the index expression. */
3764 index = cp_parser_expression (parser);
3765 /* Look for the closing `]'. */
3766 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
3768 /* Build the ARRAY_REF. */
3770 = grok_array_decl (postfix_expression, index);
3771 idk = CP_ID_KIND_NONE;
3772 /* Array references are not permitted in
3773 constant-expressions. */
3774 if (parser->integral_constant_expression_p)
3776 if (!parser->allow_non_integral_constant_expression_p)
3778 = cp_parser_non_integral_constant_expression ("an array reference");
3779 parser->non_integral_constant_expression_p = true;
3784 case CPP_OPEN_PAREN:
3785 /* postfix-expression ( expression-list [opt] ) */
3788 tree args = (cp_parser_parenthesized_expression_list
3789 (parser, false, /*non_constant_p=*/NULL));
3791 if (args == error_mark_node)
3793 postfix_expression = error_mark_node;
3797 /* Function calls are not permitted in
3798 constant-expressions. */
3799 if (parser->integral_constant_expression_p)
3801 if (!parser->allow_non_integral_constant_expression_p)
3804 = cp_parser_non_integral_constant_expression ("a function call");
3807 parser->non_integral_constant_expression_p = true;
3811 if (idk == CP_ID_KIND_UNQUALIFIED)
3814 && (is_overloaded_fn (postfix_expression)
3815 || DECL_P (postfix_expression)
3816 || TREE_CODE (postfix_expression) == IDENTIFIER_NODE))
3820 = perform_koenig_lookup (postfix_expression, args);
3822 else if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
3824 = unqualified_fn_lookup_error (postfix_expression);
3827 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
3829 tree instance = TREE_OPERAND (postfix_expression, 0);
3830 tree fn = TREE_OPERAND (postfix_expression, 1);
3832 if (processing_template_decl
3833 && (type_dependent_expression_p (instance)
3834 || (!BASELINK_P (fn)
3835 && TREE_CODE (fn) != FIELD_DECL)
3836 || type_dependent_expression_p (fn)
3837 || any_type_dependent_arguments_p (args)))
3840 = build_min_nt (CALL_EXPR, postfix_expression, args);
3844 if (BASELINK_P (fn))
3846 = (build_new_method_call
3847 (instance, fn, args, NULL_TREE,
3848 (idk == CP_ID_KIND_QUALIFIED
3849 ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL)));
3852 = finish_call_expr (postfix_expression, args,
3853 /*disallow_virtual=*/false,
3854 /*koenig_p=*/false);
3856 else if (TREE_CODE (postfix_expression) == OFFSET_REF
3857 || TREE_CODE (postfix_expression) == MEMBER_REF
3858 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
3859 postfix_expression = (build_offset_ref_call_from_tree
3860 (postfix_expression, args));
3861 else if (idk == CP_ID_KIND_QUALIFIED)
3862 /* A call to a static class member, or a namespace-scope
3865 = finish_call_expr (postfix_expression, args,
3866 /*disallow_virtual=*/true,
3869 /* All other function calls. */
3871 = finish_call_expr (postfix_expression, args,
3872 /*disallow_virtual=*/false,
3875 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
3876 idk = CP_ID_KIND_NONE;
3882 /* postfix-expression . template [opt] id-expression
3883 postfix-expression . pseudo-destructor-name
3884 postfix-expression -> template [opt] id-expression
3885 postfix-expression -> pseudo-destructor-name */
3890 tree scope = NULL_TREE;
3891 enum cpp_ttype token_type = token->type;
3893 /* If this is a `->' operator, dereference the pointer. */
3894 if (token->type == CPP_DEREF)
3895 postfix_expression = build_x_arrow (postfix_expression);
3896 /* Check to see whether or not the expression is
3898 dependent_p = type_dependent_expression_p (postfix_expression);
3899 /* The identifier following the `->' or `.' is not
3901 parser->scope = NULL_TREE;
3902 parser->qualifying_scope = NULL_TREE;
3903 parser->object_scope = NULL_TREE;
3904 idk = CP_ID_KIND_NONE;
3905 /* Enter the scope corresponding to the type of the object
3906 given by the POSTFIX_EXPRESSION. */
3908 && TREE_TYPE (postfix_expression) != NULL_TREE)
3910 scope = TREE_TYPE (postfix_expression);
3911 /* According to the standard, no expression should
3912 ever have reference type. Unfortunately, we do not
3913 currently match the standard in this respect in
3914 that our internal representation of an expression
3915 may have reference type even when the standard says
3916 it does not. Therefore, we have to manually obtain
3917 the underlying type here. */
3918 scope = non_reference (scope);
3919 /* The type of the POSTFIX_EXPRESSION must be
3921 scope = complete_type_or_else (scope, NULL_TREE);
3922 /* Let the name lookup machinery know that we are
3923 processing a class member access expression. */
3924 parser->context->object_type = scope;
3925 /* If something went wrong, we want to be able to
3926 discern that case, as opposed to the case where
3927 there was no SCOPE due to the type of expression
3930 scope = error_mark_node;
3931 /* If the SCOPE was erroneous, make the various
3932 semantic analysis functions exit quickly -- and
3933 without issuing additional error messages. */
3934 if (scope == error_mark_node)
3935 postfix_expression = error_mark_node;
3938 /* Consume the `.' or `->' operator. */
3939 cp_lexer_consume_token (parser->lexer);
3940 /* If the SCOPE is not a scalar type, we are looking at an
3941 ordinary class member access expression, rather than a
3942 pseudo-destructor-name. */
3943 if (!scope || !SCALAR_TYPE_P (scope))
3945 template_p = cp_parser_optional_template_keyword (parser);
3946 /* Parse the id-expression. */
3947 name = cp_parser_id_expression (parser,
3949 /*check_dependency_p=*/true,
3950 /*template_p=*/NULL,
3951 /*declarator_p=*/false);
3952 /* In general, build a SCOPE_REF if the member name is
3953 qualified. However, if the name was not dependent
3954 and has already been resolved; there is no need to
3955 build the SCOPE_REF. For example;
3957 struct X { void f(); };
3958 template <typename T> void f(T* t) { t->X::f(); }
3960 Even though "t" is dependent, "X::f" is not and has
3961 been resolved to a BASELINK; there is no need to
3962 include scope information. */
3964 /* But we do need to remember that there was an explicit
3965 scope for virtual function calls. */
3967 idk = CP_ID_KIND_QUALIFIED;
3969 if (name != error_mark_node
3970 && !BASELINK_P (name)
3973 name = build_nt (SCOPE_REF, parser->scope, name);
3974 parser->scope = NULL_TREE;
3975 parser->qualifying_scope = NULL_TREE;
3976 parser->object_scope = NULL_TREE;
3979 = finish_class_member_access_expr (postfix_expression, name);
3981 /* Otherwise, try the pseudo-destructor-name production. */
3987 /* Parse the pseudo-destructor-name. */
3988 cp_parser_pseudo_destructor_name (parser, &s, &type);
3989 /* Form the call. */
3991 = finish_pseudo_destructor_expr (postfix_expression,
3992 s, TREE_TYPE (type));
3995 /* We no longer need to look up names in the scope of the
3996 object on the left-hand side of the `.' or `->'
3998 parser->context->object_type = NULL_TREE;
3999 /* These operators may not appear in constant-expressions. */
4000 if (parser->integral_constant_expression_p
4001 /* The "->" operator is allowed in the implementation
4002 of "offsetof". The "." operator may appear in the
4003 name of the member. */
4004 && !parser->in_offsetof_p)
4006 if (!parser->allow_non_integral_constant_expression_p)
4008 = (cp_parser_non_integral_constant_expression
4009 (token_type == CPP_DEREF ? "'->'" : "`.'"));
4010 parser->non_integral_constant_expression_p = true;
4016 /* postfix-expression ++ */
4017 /* Consume the `++' token. */
4018 cp_lexer_consume_token (parser->lexer);
4019 /* Generate a representation for the complete expression. */
4021 = finish_increment_expr (postfix_expression,
4022 POSTINCREMENT_EXPR);
4023 /* Increments may not appear in constant-expressions. */
4024 if (parser->integral_constant_expression_p)
4026 if (!parser->allow_non_integral_constant_expression_p)
4028 = cp_parser_non_integral_constant_expression ("an increment");
4029 parser->non_integral_constant_expression_p = true;
4031 idk = CP_ID_KIND_NONE;
4034 case CPP_MINUS_MINUS:
4035 /* postfix-expression -- */
4036 /* Consume the `--' token. */
4037 cp_lexer_consume_token (parser->lexer);
4038 /* Generate a representation for the complete expression. */
4040 = finish_increment_expr (postfix_expression,
4041 POSTDECREMENT_EXPR);
4042 /* Decrements may not appear in constant-expressions. */
4043 if (parser->integral_constant_expression_p)
4045 if (!parser->allow_non_integral_constant_expression_p)
4047 = cp_parser_non_integral_constant_expression ("a decrement");
4048 parser->non_integral_constant_expression_p = true;
4050 idk = CP_ID_KIND_NONE;
4054 return postfix_expression;
4058 /* We should never get here. */
4060 return error_mark_node;
4063 /* Parse a parenthesized expression-list.
4066 assignment-expression
4067 expression-list, assignment-expression
4072 identifier, expression-list
4074 Returns a TREE_LIST. The TREE_VALUE of each node is a
4075 representation of an assignment-expression. Note that a TREE_LIST
4076 is returned even if there is only a single expression in the list.
4077 error_mark_node is returned if the ( and or ) are
4078 missing. NULL_TREE is returned on no expressions. The parentheses
4079 are eaten. IS_ATTRIBUTE_LIST is true if this is really an attribute
4080 list being parsed. If NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P
4081 indicates whether or not all of the expressions in the list were
4085 cp_parser_parenthesized_expression_list (cp_parser* parser,
4086 bool is_attribute_list,
4087 bool *non_constant_p)
4089 tree expression_list = NULL_TREE;
4090 tree identifier = NULL_TREE;
4092 /* Assume all the expressions will be constant. */
4094 *non_constant_p = false;
4096 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
4097 return error_mark_node;
4099 /* Consume expressions until there are no more. */
4100 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
4105 /* At the beginning of attribute lists, check to see if the
4106 next token is an identifier. */
4107 if (is_attribute_list
4108 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
4112 /* Consume the identifier. */
4113 token = cp_lexer_consume_token (parser->lexer);
4114 /* Save the identifier. */
4115 identifier = token->value;
4119 /* Parse the next assignment-expression. */
4122 bool expr_non_constant_p;
4123 expr = (cp_parser_constant_expression
4124 (parser, /*allow_non_constant_p=*/true,
4125 &expr_non_constant_p));
4126 if (expr_non_constant_p)
4127 *non_constant_p = true;
4130 expr = cp_parser_assignment_expression (parser);
4132 /* Add it to the list. We add error_mark_node
4133 expressions to the list, so that we can still tell if
4134 the correct form for a parenthesized expression-list
4135 is found. That gives better errors. */
4136 expression_list = tree_cons (NULL_TREE, expr, expression_list);
4138 if (expr == error_mark_node)
4142 /* After the first item, attribute lists look the same as
4143 expression lists. */
4144 is_attribute_list = false;
4147 /* If the next token isn't a `,', then we are done. */
4148 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
4151 /* Otherwise, consume the `,' and keep going. */
4152 cp_lexer_consume_token (parser->lexer);
4155 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
4160 /* We try and resync to an unnested comma, as that will give the
4161 user better diagnostics. */
4162 ending = cp_parser_skip_to_closing_parenthesis (parser,
4163 /*recovering=*/true,
4165 /*consume_paren=*/true);
4169 return error_mark_node;
4172 /* We built up the list in reverse order so we must reverse it now. */
4173 expression_list = nreverse (expression_list);
4175 expression_list = tree_cons (NULL_TREE, identifier, expression_list);
4177 return expression_list;
4180 /* Parse a pseudo-destructor-name.
4182 pseudo-destructor-name:
4183 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
4184 :: [opt] nested-name-specifier template template-id :: ~ type-name
4185 :: [opt] nested-name-specifier [opt] ~ type-name
4187 If either of the first two productions is used, sets *SCOPE to the
4188 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
4189 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
4190 or ERROR_MARK_NODE if no type-name is present. */
4193 cp_parser_pseudo_destructor_name (cp_parser* parser,
4197 bool nested_name_specifier_p;
4199 /* Look for the optional `::' operator. */
4200 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
4201 /* Look for the optional nested-name-specifier. */
4202 nested_name_specifier_p
4203 = (cp_parser_nested_name_specifier_opt (parser,
4204 /*typename_keyword_p=*/false,
4205 /*check_dependency_p=*/true,
4207 /*is_declaration=*/true)
4209 /* Now, if we saw a nested-name-specifier, we might be doing the
4210 second production. */
4211 if (nested_name_specifier_p
4212 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
4214 /* Consume the `template' keyword. */
4215 cp_lexer_consume_token (parser->lexer);
4216 /* Parse the template-id. */
4217 cp_parser_template_id (parser,
4218 /*template_keyword_p=*/true,
4219 /*check_dependency_p=*/false,
4220 /*is_declaration=*/true);
4221 /* Look for the `::' token. */
4222 cp_parser_require (parser, CPP_SCOPE, "`::'");
4224 /* If the next token is not a `~', then there might be some
4225 additional qualification. */
4226 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
4228 /* Look for the type-name. */
4229 *scope = TREE_TYPE (cp_parser_type_name (parser));
4230 /* Look for the `::' token. */
4231 cp_parser_require (parser, CPP_SCOPE, "`::'");
4236 /* Look for the `~'. */
4237 cp_parser_require (parser, CPP_COMPL, "`~'");
4238 /* Look for the type-name again. We are not responsible for
4239 checking that it matches the first type-name. */
4240 *type = cp_parser_type_name (parser);
4243 /* Parse a unary-expression.
4249 unary-operator cast-expression
4250 sizeof unary-expression
4258 __extension__ cast-expression
4259 __alignof__ unary-expression
4260 __alignof__ ( type-id )
4261 __real__ cast-expression
4262 __imag__ cast-expression
4265 ADDRESS_P is true iff the unary-expression is appearing as the
4266 operand of the `&' operator.
4268 Returns a representation of the expression. */
4271 cp_parser_unary_expression (cp_parser *parser, bool address_p)
4274 enum tree_code unary_operator;
4276 /* Peek at the next token. */
4277 token = cp_lexer_peek_token (parser->lexer);
4278 /* Some keywords give away the kind of expression. */
4279 if (token->type == CPP_KEYWORD)
4281 enum rid keyword = token->keyword;
4291 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
4292 /* Consume the token. */
4293 cp_lexer_consume_token (parser->lexer);
4294 /* Parse the operand. */
4295 operand = cp_parser_sizeof_operand (parser, keyword);
4297 if (TYPE_P (operand))
4298 return cxx_sizeof_or_alignof_type (operand, op, true);
4300 return cxx_sizeof_or_alignof_expr (operand, op);
4304 return cp_parser_new_expression (parser);
4307 return cp_parser_delete_expression (parser);
4311 /* The saved value of the PEDANTIC flag. */
4315 /* Save away the PEDANTIC flag. */
4316 cp_parser_extension_opt (parser, &saved_pedantic);
4317 /* Parse the cast-expression. */
4318 expr = cp_parser_simple_cast_expression (parser);
4319 /* Restore the PEDANTIC flag. */
4320 pedantic = saved_pedantic;
4330 /* Consume the `__real__' or `__imag__' token. */
4331 cp_lexer_consume_token (parser->lexer);
4332 /* Parse the cast-expression. */
4333 expression = cp_parser_simple_cast_expression (parser);
4334 /* Create the complete representation. */
4335 return build_x_unary_op ((keyword == RID_REALPART
4336 ? REALPART_EXPR : IMAGPART_EXPR),
4346 /* Look for the `:: new' and `:: delete', which also signal the
4347 beginning of a new-expression, or delete-expression,
4348 respectively. If the next token is `::', then it might be one of
4350 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
4354 /* See if the token after the `::' is one of the keywords in
4355 which we're interested. */
4356 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
4357 /* If it's `new', we have a new-expression. */
4358 if (keyword == RID_NEW)
4359 return cp_parser_new_expression (parser);
4360 /* Similarly, for `delete'. */
4361 else if (keyword == RID_DELETE)
4362 return cp_parser_delete_expression (parser);
4365 /* Look for a unary operator. */
4366 unary_operator = cp_parser_unary_operator (token);
4367 /* The `++' and `--' operators can be handled similarly, even though
4368 they are not technically unary-operators in the grammar. */
4369 if (unary_operator == ERROR_MARK)
4371 if (token->type == CPP_PLUS_PLUS)
4372 unary_operator = PREINCREMENT_EXPR;
4373 else if (token->type == CPP_MINUS_MINUS)
4374 unary_operator = PREDECREMENT_EXPR;
4375 /* Handle the GNU address-of-label extension. */
4376 else if (cp_parser_allow_gnu_extensions_p (parser)
4377 && token->type == CPP_AND_AND)
4381 /* Consume the '&&' token. */
4382 cp_lexer_consume_token (parser->lexer);
4383 /* Look for the identifier. */
4384 identifier = cp_parser_identifier (parser);
4385 /* Create an expression representing the address. */
4386 return finish_label_address_expr (identifier);
4389 if (unary_operator != ERROR_MARK)
4391 tree cast_expression;
4392 tree expression = error_mark_node;
4393 const char *non_constant_p = NULL;
4395 /* Consume the operator token. */
4396 token = cp_lexer_consume_token (parser->lexer);
4397 /* Parse the cast-expression. */
4399 = cp_parser_cast_expression (parser, unary_operator == ADDR_EXPR);
4400 /* Now, build an appropriate representation. */
4401 switch (unary_operator)
4404 non_constant_p = "`*'";
4405 expression = build_x_indirect_ref (cast_expression, "unary *");
4409 /* The "&" operator is allowed in the implementation of
4411 if (!parser->in_offsetof_p)
4412 non_constant_p = "`&'";
4415 expression = build_x_unary_op (unary_operator, cast_expression);
4418 case PREINCREMENT_EXPR:
4419 case PREDECREMENT_EXPR:
4420 non_constant_p = (unary_operator == PREINCREMENT_EXPR
4425 case TRUTH_NOT_EXPR:
4426 expression = finish_unary_op_expr (unary_operator, cast_expression);
4433 if (non_constant_p && parser->integral_constant_expression_p)
4435 if (!parser->allow_non_integral_constant_expression_p)
4436 return cp_parser_non_integral_constant_expression (non_constant_p);
4437 parser->non_integral_constant_expression_p = true;
4443 return cp_parser_postfix_expression (parser, address_p);
4446 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
4447 unary-operator, the corresponding tree code is returned. */
4449 static enum tree_code
4450 cp_parser_unary_operator (cp_token* token)
4452 switch (token->type)
4455 return INDIRECT_REF;
4461 return CONVERT_EXPR;
4467 return TRUTH_NOT_EXPR;
4470 return BIT_NOT_EXPR;
4477 /* Parse a new-expression.
4480 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
4481 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
4483 Returns a representation of the expression. */
4486 cp_parser_new_expression (cp_parser* parser)
4488 bool global_scope_p;
4493 /* Look for the optional `::' operator. */
4495 = (cp_parser_global_scope_opt (parser,
4496 /*current_scope_valid_p=*/false)
4498 /* Look for the `new' operator. */
4499 cp_parser_require_keyword (parser, RID_NEW, "`new'");
4500 /* There's no easy way to tell a new-placement from the
4501 `( type-id )' construct. */
4502 cp_parser_parse_tentatively (parser);
4503 /* Look for a new-placement. */
4504 placement = cp_parser_new_placement (parser);
4505 /* If that didn't work out, there's no new-placement. */
4506 if (!cp_parser_parse_definitely (parser))
4507 placement = NULL_TREE;
4509 /* If the next token is a `(', then we have a parenthesized
4511 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4513 /* Consume the `('. */
4514 cp_lexer_consume_token (parser->lexer);
4515 /* Parse the type-id. */
4516 type = cp_parser_type_id (parser);
4517 /* Look for the closing `)'. */
4518 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4520 /* Otherwise, there must be a new-type-id. */
4522 type = cp_parser_new_type_id (parser);
4524 /* If the next token is a `(', then we have a new-initializer. */
4525 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4526 initializer = cp_parser_new_initializer (parser);
4528 initializer = NULL_TREE;
4530 /* Create a representation of the new-expression. */
4531 return build_new (placement, type, initializer, global_scope_p);
4534 /* Parse a new-placement.
4539 Returns the same representation as for an expression-list. */
4542 cp_parser_new_placement (cp_parser* parser)
4544 tree expression_list;
4546 /* Parse the expression-list. */
4547 expression_list = (cp_parser_parenthesized_expression_list
4548 (parser, false, /*non_constant_p=*/NULL));
4550 return expression_list;
4553 /* Parse a new-type-id.
4556 type-specifier-seq new-declarator [opt]
4558 Returns a TREE_LIST whose TREE_PURPOSE is the type-specifier-seq,
4559 and whose TREE_VALUE is the new-declarator. */
4562 cp_parser_new_type_id (cp_parser* parser)
4564 tree type_specifier_seq;
4566 const char *saved_message;
4568 /* The type-specifier sequence must not contain type definitions.
4569 (It cannot contain declarations of new types either, but if they
4570 are not definitions we will catch that because they are not
4572 saved_message = parser->type_definition_forbidden_message;
4573 parser->type_definition_forbidden_message
4574 = "types may not be defined in a new-type-id";
4575 /* Parse the type-specifier-seq. */
4576 type_specifier_seq = cp_parser_type_specifier_seq (parser);
4577 /* Restore the old message. */
4578 parser->type_definition_forbidden_message = saved_message;
4579 /* Parse the new-declarator. */
4580 declarator = cp_parser_new_declarator_opt (parser);
4582 return build_tree_list (type_specifier_seq, declarator);
4585 /* Parse an (optional) new-declarator.
4588 ptr-operator new-declarator [opt]
4589 direct-new-declarator
4591 Returns a representation of the declarator. See
4592 cp_parser_declarator for the representations used. */
4595 cp_parser_new_declarator_opt (cp_parser* parser)
4597 enum tree_code code;
4599 tree cv_qualifier_seq;
4601 /* We don't know if there's a ptr-operator next, or not. */
4602 cp_parser_parse_tentatively (parser);
4603 /* Look for a ptr-operator. */
4604 code = cp_parser_ptr_operator (parser, &type, &cv_qualifier_seq);
4605 /* If that worked, look for more new-declarators. */
4606 if (cp_parser_parse_definitely (parser))
4610 /* Parse another optional declarator. */
4611 declarator = cp_parser_new_declarator_opt (parser);
4613 /* Create the representation of the declarator. */
4614 if (code == INDIRECT_REF)
4615 declarator = make_pointer_declarator (cv_qualifier_seq,
4618 declarator = make_reference_declarator (cv_qualifier_seq,
4621 /* Handle the pointer-to-member case. */
4623 declarator = build_nt (SCOPE_REF, type, declarator);
4628 /* If the next token is a `[', there is a direct-new-declarator. */
4629 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
4630 return cp_parser_direct_new_declarator (parser);
4635 /* Parse a direct-new-declarator.
4637 direct-new-declarator:
4639 direct-new-declarator [constant-expression]
4641 Returns an ARRAY_REF, following the same conventions as are
4642 documented for cp_parser_direct_declarator. */
4645 cp_parser_direct_new_declarator (cp_parser* parser)
4647 tree declarator = NULL_TREE;
4653 /* Look for the opening `['. */
4654 cp_parser_require (parser, CPP_OPEN_SQUARE, "`['");
4655 /* The first expression is not required to be constant. */
4658 expression = cp_parser_expression (parser);
4659 /* The standard requires that the expression have integral
4660 type. DR 74 adds enumeration types. We believe that the
4661 real intent is that these expressions be handled like the
4662 expression in a `switch' condition, which also allows
4663 classes with a single conversion to integral or
4664 enumeration type. */
4665 if (!processing_template_decl)
4668 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
4673 error ("expression in new-declarator must have integral or enumeration type");
4674 expression = error_mark_node;
4678 /* But all the other expressions must be. */
4681 = cp_parser_constant_expression (parser,
4682 /*allow_non_constant=*/false,
4684 /* Look for the closing `]'. */
4685 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4687 /* Add this bound to the declarator. */
4688 declarator = build_nt (ARRAY_REF, declarator, expression);
4690 /* If the next token is not a `[', then there are no more
4692 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
4699 /* Parse a new-initializer.
4702 ( expression-list [opt] )
4704 Returns a representation of the expression-list. If there is no
4705 expression-list, VOID_ZERO_NODE is returned. */
4708 cp_parser_new_initializer (cp_parser* parser)
4710 tree expression_list;
4712 expression_list = (cp_parser_parenthesized_expression_list
4713 (parser, false, /*non_constant_p=*/NULL));
4714 if (!expression_list)
4715 expression_list = void_zero_node;
4717 return expression_list;
4720 /* Parse a delete-expression.
4723 :: [opt] delete cast-expression
4724 :: [opt] delete [ ] cast-expression
4726 Returns a representation of the expression. */
4729 cp_parser_delete_expression (cp_parser* parser)
4731 bool global_scope_p;
4735 /* Look for the optional `::' operator. */
4737 = (cp_parser_global_scope_opt (parser,
4738 /*current_scope_valid_p=*/false)
4740 /* Look for the `delete' keyword. */
4741 cp_parser_require_keyword (parser, RID_DELETE, "`delete'");
4742 /* See if the array syntax is in use. */
4743 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
4745 /* Consume the `[' token. */
4746 cp_lexer_consume_token (parser->lexer);
4747 /* Look for the `]' token. */
4748 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4749 /* Remember that this is the `[]' construct. */
4755 /* Parse the cast-expression. */
4756 expression = cp_parser_simple_cast_expression (parser);
4758 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
4761 /* Parse a cast-expression.
4765 ( type-id ) cast-expression
4767 Returns a representation of the expression. */
4770 cp_parser_cast_expression (cp_parser *parser, bool address_p)
4772 /* If it's a `(', then we might be looking at a cast. */
4773 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4775 tree type = NULL_TREE;
4776 tree expr = NULL_TREE;
4777 bool compound_literal_p;
4778 const char *saved_message;
4780 /* There's no way to know yet whether or not this is a cast.
4781 For example, `(int (3))' is a unary-expression, while `(int)
4782 3' is a cast. So, we resort to parsing tentatively. */
4783 cp_parser_parse_tentatively (parser);
4784 /* Types may not be defined in a cast. */
4785 saved_message = parser->type_definition_forbidden_message;
4786 parser->type_definition_forbidden_message
4787 = "types may not be defined in casts";
4788 /* Consume the `('. */
4789 cp_lexer_consume_token (parser->lexer);
4790 /* A very tricky bit is that `(struct S) { 3 }' is a
4791 compound-literal (which we permit in C++ as an extension).
4792 But, that construct is not a cast-expression -- it is a
4793 postfix-expression. (The reason is that `(struct S) { 3 }.i'
4794 is legal; if the compound-literal were a cast-expression,
4795 you'd need an extra set of parentheses.) But, if we parse
4796 the type-id, and it happens to be a class-specifier, then we
4797 will commit to the parse at that point, because we cannot
4798 undo the action that is done when creating a new class. So,
4799 then we cannot back up and do a postfix-expression.
4801 Therefore, we scan ahead to the closing `)', and check to see
4802 if the token after the `)' is a `{'. If so, we are not
4803 looking at a cast-expression.
4805 Save tokens so that we can put them back. */
4806 cp_lexer_save_tokens (parser->lexer);
4807 /* Skip tokens until the next token is a closing parenthesis.
4808 If we find the closing `)', and the next token is a `{', then
4809 we are looking at a compound-literal. */
4811 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
4812 /*consume_paren=*/true)
4813 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
4814 /* Roll back the tokens we skipped. */
4815 cp_lexer_rollback_tokens (parser->lexer);
4816 /* If we were looking at a compound-literal, simulate an error
4817 so that the call to cp_parser_parse_definitely below will
4819 if (compound_literal_p)
4820 cp_parser_simulate_error (parser);
4823 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4824 parser->in_type_id_in_expr_p = true;
4825 /* Look for the type-id. */
4826 type = cp_parser_type_id (parser);
4827 /* Look for the closing `)'. */
4828 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4829 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4832 /* Restore the saved message. */
4833 parser->type_definition_forbidden_message = saved_message;
4835 /* If ok so far, parse the dependent expression. We cannot be
4836 sure it is a cast. Consider `(T ())'. It is a parenthesized
4837 ctor of T, but looks like a cast to function returning T
4838 without a dependent expression. */
4839 if (!cp_parser_error_occurred (parser))
4840 expr = cp_parser_simple_cast_expression (parser);
4842 if (cp_parser_parse_definitely (parser))
4844 /* Warn about old-style casts, if so requested. */
4845 if (warn_old_style_cast
4846 && !in_system_header
4847 && !VOID_TYPE_P (type)
4848 && current_lang_name != lang_name_c)
4849 warning ("use of old-style cast");
4851 /* Only type conversions to integral or enumeration types
4852 can be used in constant-expressions. */
4853 if (parser->integral_constant_expression_p
4854 && !dependent_type_p (type)
4855 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type))
4857 if (!parser->allow_non_integral_constant_expression_p)
4858 return (cp_parser_non_integral_constant_expression
4859 ("a casts to a type other than an integral or "
4860 "enumeration type"));
4861 parser->non_integral_constant_expression_p = true;
4863 /* Perform the cast. */
4864 expr = build_c_cast (type, expr);
4869 /* If we get here, then it's not a cast, so it must be a
4870 unary-expression. */
4871 return cp_parser_unary_expression (parser, address_p);
4874 /* Parse a pm-expression.
4878 pm-expression .* cast-expression
4879 pm-expression ->* cast-expression
4881 Returns a representation of the expression. */
4884 cp_parser_pm_expression (cp_parser* parser)
4886 static const cp_parser_token_tree_map map = {
4887 { CPP_DEREF_STAR, MEMBER_REF },
4888 { CPP_DOT_STAR, DOTSTAR_EXPR },
4889 { CPP_EOF, ERROR_MARK }
4892 return cp_parser_binary_expression (parser, map,
4893 cp_parser_simple_cast_expression);
4896 /* Parse a multiplicative-expression.
4898 mulitplicative-expression:
4900 multiplicative-expression * pm-expression
4901 multiplicative-expression / pm-expression
4902 multiplicative-expression % pm-expression
4904 Returns a representation of the expression. */
4907 cp_parser_multiplicative_expression (cp_parser* parser)
4909 static const cp_parser_token_tree_map map = {
4910 { CPP_MULT, MULT_EXPR },
4911 { CPP_DIV, TRUNC_DIV_EXPR },
4912 { CPP_MOD, TRUNC_MOD_EXPR },
4913 { CPP_EOF, ERROR_MARK }
4916 return cp_parser_binary_expression (parser,
4918 cp_parser_pm_expression);
4921 /* Parse an additive-expression.
4923 additive-expression:
4924 multiplicative-expression
4925 additive-expression + multiplicative-expression
4926 additive-expression - multiplicative-expression
4928 Returns a representation of the expression. */
4931 cp_parser_additive_expression (cp_parser* parser)
4933 static const cp_parser_token_tree_map map = {
4934 { CPP_PLUS, PLUS_EXPR },
4935 { CPP_MINUS, MINUS_EXPR },
4936 { CPP_EOF, ERROR_MARK }
4939 return cp_parser_binary_expression (parser,
4941 cp_parser_multiplicative_expression);
4944 /* Parse a shift-expression.
4948 shift-expression << additive-expression
4949 shift-expression >> additive-expression
4951 Returns a representation of the expression. */
4954 cp_parser_shift_expression (cp_parser* parser)
4956 static const cp_parser_token_tree_map map = {
4957 { CPP_LSHIFT, LSHIFT_EXPR },
4958 { CPP_RSHIFT, RSHIFT_EXPR },
4959 { CPP_EOF, ERROR_MARK }
4962 return cp_parser_binary_expression (parser,
4964 cp_parser_additive_expression);
4967 /* Parse a relational-expression.
4969 relational-expression:
4971 relational-expression < shift-expression
4972 relational-expression > shift-expression
4973 relational-expression <= shift-expression
4974 relational-expression >= shift-expression
4978 relational-expression:
4979 relational-expression <? shift-expression
4980 relational-expression >? shift-expression
4982 Returns a representation of the expression. */
4985 cp_parser_relational_expression (cp_parser* parser)
4987 static const cp_parser_token_tree_map map = {
4988 { CPP_LESS, LT_EXPR },
4989 { CPP_GREATER, GT_EXPR },
4990 { CPP_LESS_EQ, LE_EXPR },
4991 { CPP_GREATER_EQ, GE_EXPR },
4992 { CPP_MIN, MIN_EXPR },
4993 { CPP_MAX, MAX_EXPR },
4994 { CPP_EOF, ERROR_MARK }
4997 return cp_parser_binary_expression (parser,
4999 cp_parser_shift_expression);
5002 /* Parse an equality-expression.
5004 equality-expression:
5005 relational-expression
5006 equality-expression == relational-expression
5007 equality-expression != relational-expression
5009 Returns a representation of the expression. */
5012 cp_parser_equality_expression (cp_parser* parser)
5014 static const cp_parser_token_tree_map map = {
5015 { CPP_EQ_EQ, EQ_EXPR },
5016 { CPP_NOT_EQ, NE_EXPR },
5017 { CPP_EOF, ERROR_MARK }
5020 return cp_parser_binary_expression (parser,
5022 cp_parser_relational_expression);
5025 /* Parse an and-expression.
5029 and-expression & equality-expression
5031 Returns a representation of the expression. */
5034 cp_parser_and_expression (cp_parser* parser)
5036 static const cp_parser_token_tree_map map = {
5037 { CPP_AND, BIT_AND_EXPR },
5038 { CPP_EOF, ERROR_MARK }
5041 return cp_parser_binary_expression (parser,
5043 cp_parser_equality_expression);
5046 /* Parse an exclusive-or-expression.
5048 exclusive-or-expression:
5050 exclusive-or-expression ^ and-expression
5052 Returns a representation of the expression. */
5055 cp_parser_exclusive_or_expression (cp_parser* parser)
5057 static const cp_parser_token_tree_map map = {
5058 { CPP_XOR, BIT_XOR_EXPR },
5059 { CPP_EOF, ERROR_MARK }
5062 return cp_parser_binary_expression (parser,
5064 cp_parser_and_expression);
5068 /* Parse an inclusive-or-expression.
5070 inclusive-or-expression:
5071 exclusive-or-expression
5072 inclusive-or-expression | exclusive-or-expression
5074 Returns a representation of the expression. */
5077 cp_parser_inclusive_or_expression (cp_parser* parser)
5079 static const cp_parser_token_tree_map map = {
5080 { CPP_OR, BIT_IOR_EXPR },
5081 { CPP_EOF, ERROR_MARK }
5084 return cp_parser_binary_expression (parser,
5086 cp_parser_exclusive_or_expression);
5089 /* Parse a logical-and-expression.
5091 logical-and-expression:
5092 inclusive-or-expression
5093 logical-and-expression && inclusive-or-expression
5095 Returns a representation of the expression. */
5098 cp_parser_logical_and_expression (cp_parser* parser)
5100 static const cp_parser_token_tree_map map = {
5101 { CPP_AND_AND, TRUTH_ANDIF_EXPR },
5102 { CPP_EOF, ERROR_MARK }
5105 return cp_parser_binary_expression (parser,
5107 cp_parser_inclusive_or_expression);
5110 /* Parse a logical-or-expression.
5112 logical-or-expression:
5113 logical-and-expression
5114 logical-or-expression || logical-and-expression
5116 Returns a representation of the expression. */
5119 cp_parser_logical_or_expression (cp_parser* parser)
5121 static const cp_parser_token_tree_map map = {
5122 { CPP_OR_OR, TRUTH_ORIF_EXPR },
5123 { CPP_EOF, ERROR_MARK }
5126 return cp_parser_binary_expression (parser,
5128 cp_parser_logical_and_expression);
5131 /* Parse the `? expression : assignment-expression' part of a
5132 conditional-expression. The LOGICAL_OR_EXPR is the
5133 logical-or-expression that started the conditional-expression.
5134 Returns a representation of the entire conditional-expression.
5136 This routine is used by cp_parser_assignment_expression.
5138 ? expression : assignment-expression
5142 ? : assignment-expression */
5145 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
5148 tree assignment_expr;
5150 /* Consume the `?' token. */
5151 cp_lexer_consume_token (parser->lexer);
5152 if (cp_parser_allow_gnu_extensions_p (parser)
5153 && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
5154 /* Implicit true clause. */
5157 /* Parse the expression. */
5158 expr = cp_parser_expression (parser);
5160 /* The next token should be a `:'. */
5161 cp_parser_require (parser, CPP_COLON, "`:'");
5162 /* Parse the assignment-expression. */
5163 assignment_expr = cp_parser_assignment_expression (parser);
5165 /* Build the conditional-expression. */
5166 return build_x_conditional_expr (logical_or_expr,
5171 /* Parse an assignment-expression.
5173 assignment-expression:
5174 conditional-expression
5175 logical-or-expression assignment-operator assignment_expression
5178 Returns a representation for the expression. */
5181 cp_parser_assignment_expression (cp_parser* parser)
5185 /* If the next token is the `throw' keyword, then we're looking at
5186 a throw-expression. */
5187 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
5188 expr = cp_parser_throw_expression (parser);
5189 /* Otherwise, it must be that we are looking at a
5190 logical-or-expression. */
5193 /* Parse the logical-or-expression. */
5194 expr = cp_parser_logical_or_expression (parser);
5195 /* If the next token is a `?' then we're actually looking at a
5196 conditional-expression. */
5197 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
5198 return cp_parser_question_colon_clause (parser, expr);
5201 enum tree_code assignment_operator;
5203 /* If it's an assignment-operator, we're using the second
5206 = cp_parser_assignment_operator_opt (parser);
5207 if (assignment_operator != ERROR_MARK)
5211 /* Parse the right-hand side of the assignment. */
5212 rhs = cp_parser_assignment_expression (parser);
5213 /* An assignment may not appear in a
5214 constant-expression. */
5215 if (parser->integral_constant_expression_p)
5217 if (!parser->allow_non_integral_constant_expression_p)
5218 return cp_parser_non_integral_constant_expression ("an assignment");
5219 parser->non_integral_constant_expression_p = true;
5221 /* Build the assignment expression. */
5222 expr = build_x_modify_expr (expr,
5223 assignment_operator,
5232 /* Parse an (optional) assignment-operator.
5234 assignment-operator: one of
5235 = *= /= %= += -= >>= <<= &= ^= |=
5239 assignment-operator: one of
5242 If the next token is an assignment operator, the corresponding tree
5243 code is returned, and the token is consumed. For example, for
5244 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
5245 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
5246 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
5247 operator, ERROR_MARK is returned. */
5249 static enum tree_code
5250 cp_parser_assignment_operator_opt (cp_parser* parser)
5255 /* Peek at the next toen. */
5256 token = cp_lexer_peek_token (parser->lexer);
5258 switch (token->type)
5269 op = TRUNC_DIV_EXPR;
5273 op = TRUNC_MOD_EXPR;
5313 /* Nothing else is an assignment operator. */
5317 /* If it was an assignment operator, consume it. */
5318 if (op != ERROR_MARK)
5319 cp_lexer_consume_token (parser->lexer);
5324 /* Parse an expression.
5327 assignment-expression
5328 expression , assignment-expression
5330 Returns a representation of the expression. */
5333 cp_parser_expression (cp_parser* parser)
5335 tree expression = NULL_TREE;
5339 tree assignment_expression;
5341 /* Parse the next assignment-expression. */
5342 assignment_expression
5343 = cp_parser_assignment_expression (parser);
5344 /* If this is the first assignment-expression, we can just
5347 expression = assignment_expression;
5349 expression = build_x_compound_expr (expression,
5350 assignment_expression);
5351 /* If the next token is not a comma, then we are done with the
5353 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5355 /* Consume the `,'. */
5356 cp_lexer_consume_token (parser->lexer);
5357 /* A comma operator cannot appear in a constant-expression. */
5358 if (parser->integral_constant_expression_p)
5360 if (!parser->allow_non_integral_constant_expression_p)
5362 = cp_parser_non_integral_constant_expression ("a comma operator");
5363 parser->non_integral_constant_expression_p = true;
5370 /* Parse a constant-expression.
5372 constant-expression:
5373 conditional-expression
5375 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
5376 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
5377 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
5378 is false, NON_CONSTANT_P should be NULL. */
5381 cp_parser_constant_expression (cp_parser* parser,
5382 bool allow_non_constant_p,
5383 bool *non_constant_p)
5385 bool saved_integral_constant_expression_p;
5386 bool saved_allow_non_integral_constant_expression_p;
5387 bool saved_non_integral_constant_expression_p;
5390 /* It might seem that we could simply parse the
5391 conditional-expression, and then check to see if it were
5392 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
5393 one that the compiler can figure out is constant, possibly after
5394 doing some simplifications or optimizations. The standard has a
5395 precise definition of constant-expression, and we must honor
5396 that, even though it is somewhat more restrictive.
5402 is not a legal declaration, because `(2, 3)' is not a
5403 constant-expression. The `,' operator is forbidden in a
5404 constant-expression. However, GCC's constant-folding machinery
5405 will fold this operation to an INTEGER_CST for `3'. */
5407 /* Save the old settings. */
5408 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
5409 saved_allow_non_integral_constant_expression_p
5410 = parser->allow_non_integral_constant_expression_p;
5411 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
5412 /* We are now parsing a constant-expression. */
5413 parser->integral_constant_expression_p = true;
5414 parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
5415 parser->non_integral_constant_expression_p = false;
5416 /* Although the grammar says "conditional-expression", we parse an
5417 "assignment-expression", which also permits "throw-expression"
5418 and the use of assignment operators. In the case that
5419 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
5420 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
5421 actually essential that we look for an assignment-expression.
5422 For example, cp_parser_initializer_clauses uses this function to
5423 determine whether a particular assignment-expression is in fact
5425 expression = cp_parser_assignment_expression (parser);
5426 /* Restore the old settings. */
5427 parser->integral_constant_expression_p = saved_integral_constant_expression_p;
5428 parser->allow_non_integral_constant_expression_p
5429 = saved_allow_non_integral_constant_expression_p;
5430 if (allow_non_constant_p)
5431 *non_constant_p = parser->non_integral_constant_expression_p;
5432 parser->non_integral_constant_expression_p = saved_non_integral_constant_expression_p;
5437 /* Statements [gram.stmt.stmt] */
5439 /* Parse a statement.
5443 expression-statement
5448 declaration-statement
5452 cp_parser_statement (cp_parser* parser, bool in_statement_expr_p)
5456 int statement_line_number;
5458 /* There is no statement yet. */
5459 statement = NULL_TREE;
5460 /* Peek at the next token. */
5461 token = cp_lexer_peek_token (parser->lexer);
5462 /* Remember the line number of the first token in the statement. */
5463 statement_line_number = token->location.line;
5464 /* If this is a keyword, then that will often determine what kind of
5465 statement we have. */
5466 if (token->type == CPP_KEYWORD)
5468 enum rid keyword = token->keyword;
5474 statement = cp_parser_labeled_statement (parser,
5475 in_statement_expr_p);
5480 statement = cp_parser_selection_statement (parser);
5486 statement = cp_parser_iteration_statement (parser);
5493 statement = cp_parser_jump_statement (parser);
5497 statement = cp_parser_try_block (parser);
5501 /* It might be a keyword like `int' that can start a
5502 declaration-statement. */
5506 else if (token->type == CPP_NAME)
5508 /* If the next token is a `:', then we are looking at a
5509 labeled-statement. */
5510 token = cp_lexer_peek_nth_token (parser->lexer, 2);
5511 if (token->type == CPP_COLON)
5512 statement = cp_parser_labeled_statement (parser, in_statement_expr_p);
5514 /* Anything that starts with a `{' must be a compound-statement. */
5515 else if (token->type == CPP_OPEN_BRACE)
5516 statement = cp_parser_compound_statement (parser, false);
5518 /* Everything else must be a declaration-statement or an
5519 expression-statement. Try for the declaration-statement
5520 first, unless we are looking at a `;', in which case we know that
5521 we have an expression-statement. */
5524 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
5526 cp_parser_parse_tentatively (parser);
5527 /* Try to parse the declaration-statement. */
5528 cp_parser_declaration_statement (parser);
5529 /* If that worked, we're done. */
5530 if (cp_parser_parse_definitely (parser))
5533 /* Look for an expression-statement instead. */
5534 statement = cp_parser_expression_statement (parser, in_statement_expr_p);
5537 /* Set the line number for the statement. */
5538 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
5539 STMT_LINENO (statement) = statement_line_number;
5542 /* Parse a labeled-statement.
5545 identifier : statement
5546 case constant-expression : statement
5552 case constant-expression ... constant-expression : statement
5554 Returns the new CASE_LABEL, for a `case' or `default' label. For
5555 an ordinary label, returns a LABEL_STMT. */
5558 cp_parser_labeled_statement (cp_parser* parser, bool in_statement_expr_p)
5561 tree statement = error_mark_node;
5563 /* The next token should be an identifier. */
5564 token = cp_lexer_peek_token (parser->lexer);
5565 if (token->type != CPP_NAME
5566 && token->type != CPP_KEYWORD)
5568 cp_parser_error (parser, "expected labeled-statement");
5569 return error_mark_node;
5572 switch (token->keyword)
5579 /* Consume the `case' token. */
5580 cp_lexer_consume_token (parser->lexer);
5581 /* Parse the constant-expression. */
5582 expr = cp_parser_constant_expression (parser,
5583 /*allow_non_constant_p=*/false,
5586 ellipsis = cp_lexer_peek_token (parser->lexer);
5587 if (ellipsis->type == CPP_ELLIPSIS)
5589 /* Consume the `...' token. */
5590 cp_lexer_consume_token (parser->lexer);
5592 cp_parser_constant_expression (parser,
5593 /*allow_non_constant_p=*/false,
5595 /* We don't need to emit warnings here, as the common code
5596 will do this for us. */
5599 expr_hi = NULL_TREE;
5601 if (!parser->in_switch_statement_p)
5602 error ("case label `%E' not within a switch statement", expr);
5604 statement = finish_case_label (expr, expr_hi);
5609 /* Consume the `default' token. */
5610 cp_lexer_consume_token (parser->lexer);
5611 if (!parser->in_switch_statement_p)
5612 error ("case label not within a switch statement");
5614 statement = finish_case_label (NULL_TREE, NULL_TREE);
5618 /* Anything else must be an ordinary label. */
5619 statement = finish_label_stmt (cp_parser_identifier (parser));
5623 /* Require the `:' token. */
5624 cp_parser_require (parser, CPP_COLON, "`:'");
5625 /* Parse the labeled statement. */
5626 cp_parser_statement (parser, in_statement_expr_p);
5628 /* Return the label, in the case of a `case' or `default' label. */
5632 /* Parse an expression-statement.
5634 expression-statement:
5637 Returns the new EXPR_STMT -- or NULL_TREE if the expression
5638 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
5639 indicates whether this expression-statement is part of an
5640 expression statement. */
5643 cp_parser_expression_statement (cp_parser* parser, bool in_statement_expr_p)
5645 tree statement = NULL_TREE;
5647 /* If the next token is a ';', then there is no expression
5649 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
5650 statement = cp_parser_expression (parser);
5652 /* Consume the final `;'. */
5653 cp_parser_consume_semicolon_at_end_of_statement (parser);
5655 if (in_statement_expr_p
5656 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
5658 /* This is the final expression statement of a statement
5660 statement = finish_stmt_expr_expr (statement);
5663 statement = finish_expr_stmt (statement);
5670 /* Parse a compound-statement.
5673 { statement-seq [opt] }
5675 Returns a COMPOUND_STMT representing the statement. */
5678 cp_parser_compound_statement (cp_parser *parser, bool in_statement_expr_p)
5682 /* Consume the `{'. */
5683 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
5684 return error_mark_node;
5685 /* Begin the compound-statement. */
5686 compound_stmt = begin_compound_stmt (/*has_no_scope=*/false);
5687 /* Parse an (optional) statement-seq. */
5688 cp_parser_statement_seq_opt (parser, in_statement_expr_p);
5689 /* Finish the compound-statement. */
5690 finish_compound_stmt (compound_stmt);
5691 /* Consume the `}'. */
5692 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
5694 return compound_stmt;
5697 /* Parse an (optional) statement-seq.
5701 statement-seq [opt] statement */
5704 cp_parser_statement_seq_opt (cp_parser* parser, bool in_statement_expr_p)
5706 /* Scan statements until there aren't any more. */
5709 /* If we're looking at a `}', then we've run out of statements. */
5710 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE)
5711 || cp_lexer_next_token_is (parser->lexer, CPP_EOF))
5714 /* Parse the statement. */
5715 cp_parser_statement (parser, in_statement_expr_p);
5719 /* Parse a selection-statement.
5721 selection-statement:
5722 if ( condition ) statement
5723 if ( condition ) statement else statement
5724 switch ( condition ) statement
5726 Returns the new IF_STMT or SWITCH_STMT. */
5729 cp_parser_selection_statement (cp_parser* parser)
5734 /* Peek at the next token. */
5735 token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
5737 /* See what kind of keyword it is. */
5738 keyword = token->keyword;
5747 /* Look for the `('. */
5748 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
5750 cp_parser_skip_to_end_of_statement (parser);
5751 return error_mark_node;
5754 /* Begin the selection-statement. */
5755 if (keyword == RID_IF)
5756 statement = begin_if_stmt ();
5758 statement = begin_switch_stmt ();
5760 /* Parse the condition. */
5761 condition = cp_parser_condition (parser);
5762 /* Look for the `)'. */
5763 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
5764 cp_parser_skip_to_closing_parenthesis (parser, true, false,
5765 /*consume_paren=*/true);
5767 if (keyword == RID_IF)
5771 /* Add the condition. */
5772 finish_if_stmt_cond (condition, statement);
5774 /* Parse the then-clause. */
5775 then_stmt = cp_parser_implicitly_scoped_statement (parser);
5776 finish_then_clause (statement);
5778 /* If the next token is `else', parse the else-clause. */
5779 if (cp_lexer_next_token_is_keyword (parser->lexer,
5784 /* Consume the `else' keyword. */
5785 cp_lexer_consume_token (parser->lexer);
5786 /* Parse the else-clause. */
5788 = cp_parser_implicitly_scoped_statement (parser);
5789 finish_else_clause (statement);
5792 /* Now we're all done with the if-statement. */
5798 bool in_switch_statement_p;
5800 /* Add the condition. */
5801 finish_switch_cond (condition, statement);
5803 /* Parse the body of the switch-statement. */
5804 in_switch_statement_p = parser->in_switch_statement_p;
5805 parser->in_switch_statement_p = true;
5806 body = cp_parser_implicitly_scoped_statement (parser);
5807 parser->in_switch_statement_p = in_switch_statement_p;
5809 /* Now we're all done with the switch-statement. */
5810 finish_switch_stmt (statement);
5818 cp_parser_error (parser, "expected selection-statement");
5819 return error_mark_node;
5823 /* Parse a condition.
5827 type-specifier-seq declarator = assignment-expression
5832 type-specifier-seq declarator asm-specification [opt]
5833 attributes [opt] = assignment-expression
5835 Returns the expression that should be tested. */
5838 cp_parser_condition (cp_parser* parser)
5840 tree type_specifiers;
5841 const char *saved_message;
5843 /* Try the declaration first. */
5844 cp_parser_parse_tentatively (parser);
5845 /* New types are not allowed in the type-specifier-seq for a
5847 saved_message = parser->type_definition_forbidden_message;
5848 parser->type_definition_forbidden_message
5849 = "types may not be defined in conditions";
5850 /* Parse the type-specifier-seq. */
5851 type_specifiers = cp_parser_type_specifier_seq (parser);
5852 /* Restore the saved message. */
5853 parser->type_definition_forbidden_message = saved_message;
5854 /* If all is well, we might be looking at a declaration. */
5855 if (!cp_parser_error_occurred (parser))
5858 tree asm_specification;
5861 tree initializer = NULL_TREE;
5863 /* Parse the declarator. */
5864 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
5865 /*ctor_dtor_or_conv_p=*/NULL,
5866 /*parenthesized_p=*/NULL);
5867 /* Parse the attributes. */
5868 attributes = cp_parser_attributes_opt (parser);
5869 /* Parse the asm-specification. */
5870 asm_specification = cp_parser_asm_specification_opt (parser);
5871 /* If the next token is not an `=', then we might still be
5872 looking at an expression. For example:
5876 looks like a decl-specifier-seq and a declarator -- but then
5877 there is no `=', so this is an expression. */
5878 cp_parser_require (parser, CPP_EQ, "`='");
5879 /* If we did see an `=', then we are looking at a declaration
5881 if (cp_parser_parse_definitely (parser))
5883 /* Create the declaration. */
5884 decl = start_decl (declarator, type_specifiers,
5885 /*initialized_p=*/true,
5886 attributes, /*prefix_attributes=*/NULL_TREE);
5887 /* Parse the assignment-expression. */
5888 initializer = cp_parser_assignment_expression (parser);
5890 /* Process the initializer. */
5891 cp_finish_decl (decl,
5894 LOOKUP_ONLYCONVERTING);
5896 return convert_from_reference (decl);
5899 /* If we didn't even get past the declarator successfully, we are
5900 definitely not looking at a declaration. */
5902 cp_parser_abort_tentative_parse (parser);
5904 /* Otherwise, we are looking at an expression. */
5905 return cp_parser_expression (parser);
5908 /* Parse an iteration-statement.
5910 iteration-statement:
5911 while ( condition ) statement
5912 do statement while ( expression ) ;
5913 for ( for-init-statement condition [opt] ; expression [opt] )
5916 Returns the new WHILE_STMT, DO_STMT, or FOR_STMT. */
5919 cp_parser_iteration_statement (cp_parser* parser)
5924 bool in_iteration_statement_p;
5927 /* Peek at the next token. */
5928 token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
5930 return error_mark_node;
5932 /* Remember whether or not we are already within an iteration
5934 in_iteration_statement_p = parser->in_iteration_statement_p;
5936 /* See what kind of keyword it is. */
5937 keyword = token->keyword;
5944 /* Begin the while-statement. */
5945 statement = begin_while_stmt ();
5946 /* Look for the `('. */
5947 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
5948 /* Parse the condition. */
5949 condition = cp_parser_condition (parser);
5950 finish_while_stmt_cond (condition, statement);
5951 /* Look for the `)'. */
5952 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5953 /* Parse the dependent statement. */
5954 parser->in_iteration_statement_p = true;
5955 cp_parser_already_scoped_statement (parser);
5956 parser->in_iteration_statement_p = in_iteration_statement_p;
5957 /* We're done with the while-statement. */
5958 finish_while_stmt (statement);
5966 /* Begin the do-statement. */
5967 statement = begin_do_stmt ();
5968 /* Parse the body of the do-statement. */
5969 parser->in_iteration_statement_p = true;
5970 cp_parser_implicitly_scoped_statement (parser);
5971 parser->in_iteration_statement_p = in_iteration_statement_p;
5972 finish_do_body (statement);
5973 /* Look for the `while' keyword. */
5974 cp_parser_require_keyword (parser, RID_WHILE, "`while'");
5975 /* Look for the `('. */
5976 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
5977 /* Parse the expression. */
5978 expression = cp_parser_expression (parser);
5979 /* We're done with the do-statement. */
5980 finish_do_stmt (expression, statement);
5981 /* Look for the `)'. */
5982 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5983 /* Look for the `;'. */
5984 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
5990 tree condition = NULL_TREE;
5991 tree expression = NULL_TREE;
5993 /* Begin the for-statement. */
5994 statement = begin_for_stmt ();
5995 /* Look for the `('. */
5996 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
5997 /* Parse the initialization. */
5998 cp_parser_for_init_statement (parser);
5999 finish_for_init_stmt (statement);
6001 /* If there's a condition, process it. */
6002 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6003 condition = cp_parser_condition (parser);
6004 finish_for_cond (condition, statement);
6005 /* Look for the `;'. */
6006 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6008 /* If there's an expression, process it. */
6009 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
6010 expression = cp_parser_expression (parser);
6011 finish_for_expr (expression, statement);
6012 /* Look for the `)'. */
6013 cp_parser_require (parser, CPP_CLOSE_PAREN, "`;'");
6015 /* Parse the body of the for-statement. */
6016 parser->in_iteration_statement_p = true;
6017 cp_parser_already_scoped_statement (parser);
6018 parser->in_iteration_statement_p = in_iteration_statement_p;
6020 /* We're done with the for-statement. */
6021 finish_for_stmt (statement);
6026 cp_parser_error (parser, "expected iteration-statement");
6027 statement = error_mark_node;
6034 /* Parse a for-init-statement.
6037 expression-statement
6038 simple-declaration */
6041 cp_parser_for_init_statement (cp_parser* parser)
6043 /* If the next token is a `;', then we have an empty
6044 expression-statement. Grammatically, this is also a
6045 simple-declaration, but an invalid one, because it does not
6046 declare anything. Therefore, if we did not handle this case
6047 specially, we would issue an error message about an invalid
6049 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6051 /* We're going to speculatively look for a declaration, falling back
6052 to an expression, if necessary. */
6053 cp_parser_parse_tentatively (parser);
6054 /* Parse the declaration. */
6055 cp_parser_simple_declaration (parser,
6056 /*function_definition_allowed_p=*/false);
6057 /* If the tentative parse failed, then we shall need to look for an
6058 expression-statement. */
6059 if (cp_parser_parse_definitely (parser))
6063 cp_parser_expression_statement (parser, false);
6066 /* Parse a jump-statement.
6071 return expression [opt] ;
6079 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_STMT, or
6083 cp_parser_jump_statement (cp_parser* parser)
6085 tree statement = error_mark_node;
6089 /* Peek at the next token. */
6090 token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
6092 return error_mark_node;
6094 /* See what kind of keyword it is. */
6095 keyword = token->keyword;
6099 if (!parser->in_switch_statement_p
6100 && !parser->in_iteration_statement_p)
6102 error ("break statement not within loop or switch");
6103 statement = error_mark_node;
6106 statement = finish_break_stmt ();
6107 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6111 if (!parser->in_iteration_statement_p)
6113 error ("continue statement not within a loop");
6114 statement = error_mark_node;
6117 statement = finish_continue_stmt ();
6118 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6125 /* If the next token is a `;', then there is no
6127 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6128 expr = cp_parser_expression (parser);
6131 /* Build the return-statement. */
6132 statement = finish_return_stmt (expr);
6133 /* Look for the final `;'. */
6134 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6139 /* Create the goto-statement. */
6140 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
6142 /* Issue a warning about this use of a GNU extension. */
6144 pedwarn ("ISO C++ forbids computed gotos");
6145 /* Consume the '*' token. */
6146 cp_lexer_consume_token (parser->lexer);
6147 /* Parse the dependent expression. */
6148 finish_goto_stmt (cp_parser_expression (parser));
6151 finish_goto_stmt (cp_parser_identifier (parser));
6152 /* Look for the final `;'. */
6153 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6157 cp_parser_error (parser, "expected jump-statement");
6164 /* Parse a declaration-statement.
6166 declaration-statement:
6167 block-declaration */
6170 cp_parser_declaration_statement (cp_parser* parser)
6172 /* Parse the block-declaration. */
6173 cp_parser_block_declaration (parser, /*statement_p=*/true);
6175 /* Finish off the statement. */
6179 /* Some dependent statements (like `if (cond) statement'), are
6180 implicitly in their own scope. In other words, if the statement is
6181 a single statement (as opposed to a compound-statement), it is
6182 none-the-less treated as if it were enclosed in braces. Any
6183 declarations appearing in the dependent statement are out of scope
6184 after control passes that point. This function parses a statement,
6185 but ensures that is in its own scope, even if it is not a
6188 Returns the new statement. */
6191 cp_parser_implicitly_scoped_statement (cp_parser* parser)
6195 /* If the token is not a `{', then we must take special action. */
6196 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
6198 /* Create a compound-statement. */
6199 statement = begin_compound_stmt (/*has_no_scope=*/false);
6200 /* Parse the dependent-statement. */
6201 cp_parser_statement (parser, false);
6202 /* Finish the dummy compound-statement. */
6203 finish_compound_stmt (statement);
6205 /* Otherwise, we simply parse the statement directly. */
6207 statement = cp_parser_compound_statement (parser, false);
6209 /* Return the statement. */
6213 /* For some dependent statements (like `while (cond) statement'), we
6214 have already created a scope. Therefore, even if the dependent
6215 statement is a compound-statement, we do not want to create another
6219 cp_parser_already_scoped_statement (cp_parser* parser)
6221 /* If the token is not a `{', then we must take special action. */
6222 if (cp_lexer_next_token_is_not(parser->lexer, CPP_OPEN_BRACE))
6226 /* Create a compound-statement. */
6227 statement = begin_compound_stmt (/*has_no_scope=*/true);
6228 /* Parse the dependent-statement. */
6229 cp_parser_statement (parser, false);
6230 /* Finish the dummy compound-statement. */
6231 finish_compound_stmt (statement);
6233 /* Otherwise, we simply parse the statement directly. */
6235 cp_parser_statement (parser, false);
6238 /* Declarations [gram.dcl.dcl] */
6240 /* Parse an optional declaration-sequence.
6244 declaration-seq declaration */
6247 cp_parser_declaration_seq_opt (cp_parser* parser)
6253 token = cp_lexer_peek_token (parser->lexer);
6255 if (token->type == CPP_CLOSE_BRACE
6256 || token->type == CPP_EOF)
6259 if (token->type == CPP_SEMICOLON)
6261 /* A declaration consisting of a single semicolon is
6262 invalid. Allow it unless we're being pedantic. */
6263 if (pedantic && !in_system_header)
6264 pedwarn ("extra `;'");
6265 cp_lexer_consume_token (parser->lexer);
6269 /* The C lexer modifies PENDING_LANG_CHANGE when it wants the
6270 parser to enter or exit implicit `extern "C"' blocks. */
6271 while (pending_lang_change > 0)
6273 push_lang_context (lang_name_c);
6274 --pending_lang_change;
6276 while (pending_lang_change < 0)
6278 pop_lang_context ();
6279 ++pending_lang_change;
6282 /* Parse the declaration itself. */
6283 cp_parser_declaration (parser);
6287 /* Parse a declaration.
6292 template-declaration
6293 explicit-instantiation
6294 explicit-specialization
6295 linkage-specification
6296 namespace-definition
6301 __extension__ declaration */
6304 cp_parser_declaration (cp_parser* parser)
6310 /* Check for the `__extension__' keyword. */
6311 if (cp_parser_extension_opt (parser, &saved_pedantic))
6313 /* Parse the qualified declaration. */
6314 cp_parser_declaration (parser);
6315 /* Restore the PEDANTIC flag. */
6316 pedantic = saved_pedantic;
6321 /* Try to figure out what kind of declaration is present. */
6322 token1 = *cp_lexer_peek_token (parser->lexer);
6323 if (token1.type != CPP_EOF)
6324 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
6326 /* If the next token is `extern' and the following token is a string
6327 literal, then we have a linkage specification. */
6328 if (token1.keyword == RID_EXTERN
6329 && cp_parser_is_string_literal (&token2))
6330 cp_parser_linkage_specification (parser);
6331 /* If the next token is `template', then we have either a template
6332 declaration, an explicit instantiation, or an explicit
6334 else if (token1.keyword == RID_TEMPLATE)
6336 /* `template <>' indicates a template specialization. */
6337 if (token2.type == CPP_LESS
6338 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
6339 cp_parser_explicit_specialization (parser);
6340 /* `template <' indicates a template declaration. */
6341 else if (token2.type == CPP_LESS)
6342 cp_parser_template_declaration (parser, /*member_p=*/false);
6343 /* Anything else must be an explicit instantiation. */
6345 cp_parser_explicit_instantiation (parser);
6347 /* If the next token is `export', then we have a template
6349 else if (token1.keyword == RID_EXPORT)
6350 cp_parser_template_declaration (parser, /*member_p=*/false);
6351 /* If the next token is `extern', 'static' or 'inline' and the one
6352 after that is `template', we have a GNU extended explicit
6353 instantiation directive. */
6354 else if (cp_parser_allow_gnu_extensions_p (parser)
6355 && (token1.keyword == RID_EXTERN
6356 || token1.keyword == RID_STATIC
6357 || token1.keyword == RID_INLINE)
6358 && token2.keyword == RID_TEMPLATE)
6359 cp_parser_explicit_instantiation (parser);
6360 /* If the next token is `namespace', check for a named or unnamed
6361 namespace definition. */
6362 else if (token1.keyword == RID_NAMESPACE
6363 && (/* A named namespace definition. */
6364 (token2.type == CPP_NAME
6365 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
6367 /* An unnamed namespace definition. */
6368 || token2.type == CPP_OPEN_BRACE))
6369 cp_parser_namespace_definition (parser);
6370 /* We must have either a block declaration or a function
6373 /* Try to parse a block-declaration, or a function-definition. */
6374 cp_parser_block_declaration (parser, /*statement_p=*/false);
6377 /* Parse a block-declaration.
6382 namespace-alias-definition
6389 __extension__ block-declaration
6392 If STATEMENT_P is TRUE, then this block-declaration is occurring as
6393 part of a declaration-statement. */
6396 cp_parser_block_declaration (cp_parser *parser,
6402 /* Check for the `__extension__' keyword. */
6403 if (cp_parser_extension_opt (parser, &saved_pedantic))
6405 /* Parse the qualified declaration. */
6406 cp_parser_block_declaration (parser, statement_p);
6407 /* Restore the PEDANTIC flag. */
6408 pedantic = saved_pedantic;
6413 /* Peek at the next token to figure out which kind of declaration is
6415 token1 = cp_lexer_peek_token (parser->lexer);
6417 /* If the next keyword is `asm', we have an asm-definition. */
6418 if (token1->keyword == RID_ASM)
6421 cp_parser_commit_to_tentative_parse (parser);
6422 cp_parser_asm_definition (parser);
6424 /* If the next keyword is `namespace', we have a
6425 namespace-alias-definition. */
6426 else if (token1->keyword == RID_NAMESPACE)
6427 cp_parser_namespace_alias_definition (parser);
6428 /* If the next keyword is `using', we have either a
6429 using-declaration or a using-directive. */
6430 else if (token1->keyword == RID_USING)
6435 cp_parser_commit_to_tentative_parse (parser);
6436 /* If the token after `using' is `namespace', then we have a
6438 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
6439 if (token2->keyword == RID_NAMESPACE)
6440 cp_parser_using_directive (parser);
6441 /* Otherwise, it's a using-declaration. */
6443 cp_parser_using_declaration (parser);
6445 /* If the next keyword is `__label__' we have a label declaration. */
6446 else if (token1->keyword == RID_LABEL)
6449 cp_parser_commit_to_tentative_parse (parser);
6450 cp_parser_label_declaration (parser);
6452 /* Anything else must be a simple-declaration. */
6454 cp_parser_simple_declaration (parser, !statement_p);
6457 /* Parse a simple-declaration.
6460 decl-specifier-seq [opt] init-declarator-list [opt] ;
6462 init-declarator-list:
6464 init-declarator-list , init-declarator
6466 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
6467 function-definition as a simple-declaration. */
6470 cp_parser_simple_declaration (cp_parser* parser,
6471 bool function_definition_allowed_p)
6473 tree decl_specifiers;
6475 int declares_class_or_enum;
6476 bool saw_declarator;
6478 /* Defer access checks until we know what is being declared; the
6479 checks for names appearing in the decl-specifier-seq should be
6480 done as if we were in the scope of the thing being declared. */
6481 push_deferring_access_checks (dk_deferred);
6483 /* Parse the decl-specifier-seq. We have to keep track of whether
6484 or not the decl-specifier-seq declares a named class or
6485 enumeration type, since that is the only case in which the
6486 init-declarator-list is allowed to be empty.
6490 In a simple-declaration, the optional init-declarator-list can be
6491 omitted only when declaring a class or enumeration, that is when
6492 the decl-specifier-seq contains either a class-specifier, an
6493 elaborated-type-specifier, or an enum-specifier. */
6495 = cp_parser_decl_specifier_seq (parser,
6496 CP_PARSER_FLAGS_OPTIONAL,
6498 &declares_class_or_enum);
6499 /* We no longer need to defer access checks. */
6500 stop_deferring_access_checks ();
6502 /* In a block scope, a valid declaration must always have a
6503 decl-specifier-seq. By not trying to parse declarators, we can
6504 resolve the declaration/expression ambiguity more quickly. */
6505 if (!function_definition_allowed_p && !decl_specifiers)
6507 cp_parser_error (parser, "expected declaration");
6511 /* If the next two tokens are both identifiers, the code is
6512 erroneous. The usual cause of this situation is code like:
6516 where "T" should name a type -- but does not. */
6517 if (cp_parser_parse_and_diagnose_invalid_type_name (parser))
6519 /* If parsing tentatively, we should commit; we really are
6520 looking at a declaration. */
6521 cp_parser_commit_to_tentative_parse (parser);
6526 /* Keep going until we hit the `;' at the end of the simple
6528 saw_declarator = false;
6529 while (cp_lexer_next_token_is_not (parser->lexer,
6533 bool function_definition_p;
6536 saw_declarator = true;
6537 /* Parse the init-declarator. */
6538 decl = cp_parser_init_declarator (parser, decl_specifiers, attributes,
6539 function_definition_allowed_p,
6541 declares_class_or_enum,
6542 &function_definition_p);
6543 /* If an error occurred while parsing tentatively, exit quickly.
6544 (That usually happens when in the body of a function; each
6545 statement is treated as a declaration-statement until proven
6547 if (cp_parser_error_occurred (parser))
6549 /* Handle function definitions specially. */
6550 if (function_definition_p)
6552 /* If the next token is a `,', then we are probably
6553 processing something like:
6557 which is erroneous. */
6558 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
6559 error ("mixing declarations and function-definitions is forbidden");
6560 /* Otherwise, we're done with the list of declarators. */
6563 pop_deferring_access_checks ();
6567 /* The next token should be either a `,' or a `;'. */
6568 token = cp_lexer_peek_token (parser->lexer);
6569 /* If it's a `,', there are more declarators to come. */
6570 if (token->type == CPP_COMMA)
6571 cp_lexer_consume_token (parser->lexer);
6572 /* If it's a `;', we are done. */
6573 else if (token->type == CPP_SEMICOLON)
6575 /* Anything else is an error. */
6578 cp_parser_error (parser, "expected `,' or `;'");
6579 /* Skip tokens until we reach the end of the statement. */
6580 cp_parser_skip_to_end_of_statement (parser);
6581 /* If the next token is now a `;', consume it. */
6582 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
6583 cp_lexer_consume_token (parser->lexer);
6586 /* After the first time around, a function-definition is not
6587 allowed -- even if it was OK at first. For example:
6592 function_definition_allowed_p = false;
6595 /* Issue an error message if no declarators are present, and the
6596 decl-specifier-seq does not itself declare a class or
6598 if (!saw_declarator)
6600 if (cp_parser_declares_only_class_p (parser))
6601 shadow_tag (decl_specifiers);
6602 /* Perform any deferred access checks. */
6603 perform_deferred_access_checks ();
6606 /* Consume the `;'. */
6607 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6610 pop_deferring_access_checks ();
6613 /* Parse a decl-specifier-seq.
6616 decl-specifier-seq [opt] decl-specifier
6619 storage-class-specifier
6628 decl-specifier-seq [opt] attributes
6630 Returns a TREE_LIST, giving the decl-specifiers in the order they
6631 appear in the source code. The TREE_VALUE of each node is the
6632 decl-specifier. For a keyword (such as `auto' or `friend'), the
6633 TREE_VALUE is simply the corresponding TREE_IDENTIFIER. For the
6634 representation of a type-specifier, see cp_parser_type_specifier.
6636 If there are attributes, they will be stored in *ATTRIBUTES,
6637 represented as described above cp_parser_attributes.
6639 If FRIEND_IS_NOT_CLASS_P is non-NULL, and the `friend' specifier
6640 appears, and the entity that will be a friend is not going to be a
6641 class, then *FRIEND_IS_NOT_CLASS_P will be set to TRUE. Note that
6642 even if *FRIEND_IS_NOT_CLASS_P is FALSE, the entity to which
6643 friendship is granted might not be a class.
6645 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
6648 1: one of the decl-specifiers is an elaborated-type-specifier
6649 (i.e., a type declaration)
6650 2: one of the decl-specifiers is an enum-specifier or a
6651 class-specifier (i.e., a type definition)
6656 cp_parser_decl_specifier_seq (cp_parser* parser,
6657 cp_parser_flags flags,
6659 int* declares_class_or_enum)
6661 tree decl_specs = NULL_TREE;
6662 bool friend_p = false;
6663 bool constructor_possible_p = !parser->in_declarator_p;
6665 /* Assume no class or enumeration type is declared. */
6666 *declares_class_or_enum = 0;
6668 /* Assume there are no attributes. */
6669 *attributes = NULL_TREE;
6671 /* Keep reading specifiers until there are no more to read. */
6674 tree decl_spec = NULL_TREE;
6678 /* Peek at the next token. */
6679 token = cp_lexer_peek_token (parser->lexer);
6680 /* Handle attributes. */
6681 if (token->keyword == RID_ATTRIBUTE)
6683 /* Parse the attributes. */
6684 decl_spec = cp_parser_attributes_opt (parser);
6685 /* Add them to the list. */
6686 *attributes = chainon (*attributes, decl_spec);
6689 /* If the next token is an appropriate keyword, we can simply
6690 add it to the list. */
6691 switch (token->keyword)
6697 error ("duplicate `friend'");
6700 /* The representation of the specifier is simply the
6701 appropriate TREE_IDENTIFIER node. */
6702 decl_spec = token->value;
6703 /* Consume the token. */
6704 cp_lexer_consume_token (parser->lexer);
6707 /* function-specifier:
6714 decl_spec = cp_parser_function_specifier_opt (parser);
6720 /* The representation of the specifier is simply the
6721 appropriate TREE_IDENTIFIER node. */
6722 decl_spec = token->value;
6723 /* Consume the token. */
6724 cp_lexer_consume_token (parser->lexer);
6725 /* A constructor declarator cannot appear in a typedef. */
6726 constructor_possible_p = false;
6727 /* The "typedef" keyword can only occur in a declaration; we
6728 may as well commit at this point. */
6729 cp_parser_commit_to_tentative_parse (parser);
6732 /* storage-class-specifier:
6747 decl_spec = cp_parser_storage_class_specifier_opt (parser);
6754 /* Constructors are a special case. The `S' in `S()' is not a
6755 decl-specifier; it is the beginning of the declarator. */
6756 constructor_p = (!decl_spec
6757 && constructor_possible_p
6758 && cp_parser_constructor_declarator_p (parser,
6761 /* If we don't have a DECL_SPEC yet, then we must be looking at
6762 a type-specifier. */
6763 if (!decl_spec && !constructor_p)
6765 int decl_spec_declares_class_or_enum;
6766 bool is_cv_qualifier;
6769 = cp_parser_type_specifier (parser, flags,
6771 /*is_declaration=*/true,
6772 &decl_spec_declares_class_or_enum,
6775 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
6777 /* If this type-specifier referenced a user-defined type
6778 (a typedef, class-name, etc.), then we can't allow any
6779 more such type-specifiers henceforth.
6783 The longest sequence of decl-specifiers that could
6784 possibly be a type name is taken as the
6785 decl-specifier-seq of a declaration. The sequence shall
6786 be self-consistent as described below.
6790 As a general rule, at most one type-specifier is allowed
6791 in the complete decl-specifier-seq of a declaration. The
6792 only exceptions are the following:
6794 -- const or volatile can be combined with any other
6797 -- signed or unsigned can be combined with char, long,
6805 void g (const int Pc);
6807 Here, Pc is *not* part of the decl-specifier seq; it's
6808 the declarator. Therefore, once we see a type-specifier
6809 (other than a cv-qualifier), we forbid any additional
6810 user-defined types. We *do* still allow things like `int
6811 int' to be considered a decl-specifier-seq, and issue the
6812 error message later. */
6813 if (decl_spec && !is_cv_qualifier)
6814 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
6815 /* A constructor declarator cannot follow a type-specifier. */
6817 constructor_possible_p = false;
6820 /* If we still do not have a DECL_SPEC, then there are no more
6824 /* Issue an error message, unless the entire construct was
6826 if (!(flags & CP_PARSER_FLAGS_OPTIONAL))
6828 cp_parser_error (parser, "expected decl specifier");
6829 return error_mark_node;
6835 /* Add the DECL_SPEC to the list of specifiers. */
6836 if (decl_specs == NULL || TREE_VALUE (decl_specs) != error_mark_node)
6837 decl_specs = tree_cons (NULL_TREE, decl_spec, decl_specs);
6839 /* After we see one decl-specifier, further decl-specifiers are
6841 flags |= CP_PARSER_FLAGS_OPTIONAL;
6844 /* Don't allow a friend specifier with a class definition. */
6845 if (friend_p && (*declares_class_or_enum & 2))
6846 error ("class definition may not be declared a friend");
6848 /* We have built up the DECL_SPECS in reverse order. Return them in
6849 the correct order. */
6850 return nreverse (decl_specs);
6853 /* Parse an (optional) storage-class-specifier.
6855 storage-class-specifier:
6864 storage-class-specifier:
6867 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
6870 cp_parser_storage_class_specifier_opt (cp_parser* parser)
6872 switch (cp_lexer_peek_token (parser->lexer)->keyword)
6880 /* Consume the token. */
6881 return cp_lexer_consume_token (parser->lexer)->value;
6888 /* Parse an (optional) function-specifier.
6895 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
6898 cp_parser_function_specifier_opt (cp_parser* parser)
6900 switch (cp_lexer_peek_token (parser->lexer)->keyword)
6905 /* Consume the token. */
6906 return cp_lexer_consume_token (parser->lexer)->value;
6913 /* Parse a linkage-specification.
6915 linkage-specification:
6916 extern string-literal { declaration-seq [opt] }
6917 extern string-literal declaration */
6920 cp_parser_linkage_specification (cp_parser* parser)
6925 /* Look for the `extern' keyword. */
6926 cp_parser_require_keyword (parser, RID_EXTERN, "`extern'");
6928 /* Peek at the next token. */
6929 token = cp_lexer_peek_token (parser->lexer);
6930 /* If it's not a string-literal, then there's a problem. */
6931 if (!cp_parser_is_string_literal (token))
6933 cp_parser_error (parser, "expected language-name");
6936 /* Consume the token. */
6937 cp_lexer_consume_token (parser->lexer);
6939 /* Transform the literal into an identifier. If the literal is a
6940 wide-character string, or contains embedded NULs, then we can't
6941 handle it as the user wants. */
6942 if (token->type == CPP_WSTRING
6943 || (strlen (TREE_STRING_POINTER (token->value))
6944 != (size_t) (TREE_STRING_LENGTH (token->value) - 1)))
6946 cp_parser_error (parser, "invalid linkage-specification");
6947 /* Assume C++ linkage. */
6948 linkage = get_identifier ("c++");
6950 /* If it's a simple string constant, things are easier. */
6952 linkage = get_identifier (TREE_STRING_POINTER (token->value));
6954 /* We're now using the new linkage. */
6955 push_lang_context (linkage);
6957 /* If the next token is a `{', then we're using the first
6959 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6961 /* Consume the `{' token. */
6962 cp_lexer_consume_token (parser->lexer);
6963 /* Parse the declarations. */
6964 cp_parser_declaration_seq_opt (parser);
6965 /* Look for the closing `}'. */
6966 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
6968 /* Otherwise, there's just one declaration. */
6971 bool saved_in_unbraced_linkage_specification_p;
6973 saved_in_unbraced_linkage_specification_p
6974 = parser->in_unbraced_linkage_specification_p;
6975 parser->in_unbraced_linkage_specification_p = true;
6976 have_extern_spec = true;
6977 cp_parser_declaration (parser);
6978 have_extern_spec = false;
6979 parser->in_unbraced_linkage_specification_p
6980 = saved_in_unbraced_linkage_specification_p;
6983 /* We're done with the linkage-specification. */
6984 pop_lang_context ();
6987 /* Special member functions [gram.special] */
6989 /* Parse a conversion-function-id.
6991 conversion-function-id:
6992 operator conversion-type-id
6994 Returns an IDENTIFIER_NODE representing the operator. */
6997 cp_parser_conversion_function_id (cp_parser* parser)
7001 tree saved_qualifying_scope;
7002 tree saved_object_scope;
7004 /* Look for the `operator' token. */
7005 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
7006 return error_mark_node;
7007 /* When we parse the conversion-type-id, the current scope will be
7008 reset. However, we need that information in able to look up the
7009 conversion function later, so we save it here. */
7010 saved_scope = parser->scope;
7011 saved_qualifying_scope = parser->qualifying_scope;
7012 saved_object_scope = parser->object_scope;
7013 /* We must enter the scope of the class so that the names of
7014 entities declared within the class are available in the
7015 conversion-type-id. For example, consider:
7022 S::operator I() { ... }
7024 In order to see that `I' is a type-name in the definition, we
7025 must be in the scope of `S'. */
7027 push_scope (saved_scope);
7028 /* Parse the conversion-type-id. */
7029 type = cp_parser_conversion_type_id (parser);
7030 /* Leave the scope of the class, if any. */
7032 pop_scope (saved_scope);
7033 /* Restore the saved scope. */
7034 parser->scope = saved_scope;
7035 parser->qualifying_scope = saved_qualifying_scope;
7036 parser->object_scope = saved_object_scope;
7037 /* If the TYPE is invalid, indicate failure. */
7038 if (type == error_mark_node)
7039 return error_mark_node;
7040 return mangle_conv_op_name_for_type (type);
7043 /* Parse a conversion-type-id:
7046 type-specifier-seq conversion-declarator [opt]
7048 Returns the TYPE specified. */
7051 cp_parser_conversion_type_id (cp_parser* parser)
7054 tree type_specifiers;
7057 /* Parse the attributes. */
7058 attributes = cp_parser_attributes_opt (parser);
7059 /* Parse the type-specifiers. */
7060 type_specifiers = cp_parser_type_specifier_seq (parser);
7061 /* If that didn't work, stop. */
7062 if (type_specifiers == error_mark_node)
7063 return error_mark_node;
7064 /* Parse the conversion-declarator. */
7065 declarator = cp_parser_conversion_declarator_opt (parser);
7067 return grokdeclarator (declarator, type_specifiers, TYPENAME,
7068 /*initialized=*/0, &attributes);
7071 /* Parse an (optional) conversion-declarator.
7073 conversion-declarator:
7074 ptr-operator conversion-declarator [opt]
7076 Returns a representation of the declarator. See
7077 cp_parser_declarator for details. */
7080 cp_parser_conversion_declarator_opt (cp_parser* parser)
7082 enum tree_code code;
7084 tree cv_qualifier_seq;
7086 /* We don't know if there's a ptr-operator next, or not. */
7087 cp_parser_parse_tentatively (parser);
7088 /* Try the ptr-operator. */
7089 code = cp_parser_ptr_operator (parser, &class_type,
7091 /* If it worked, look for more conversion-declarators. */
7092 if (cp_parser_parse_definitely (parser))
7096 /* Parse another optional declarator. */
7097 declarator = cp_parser_conversion_declarator_opt (parser);
7099 /* Create the representation of the declarator. */
7100 if (code == INDIRECT_REF)
7101 declarator = make_pointer_declarator (cv_qualifier_seq,
7104 declarator = make_reference_declarator (cv_qualifier_seq,
7107 /* Handle the pointer-to-member case. */
7109 declarator = build_nt (SCOPE_REF, class_type, declarator);
7117 /* Parse an (optional) ctor-initializer.
7120 : mem-initializer-list
7122 Returns TRUE iff the ctor-initializer was actually present. */
7125 cp_parser_ctor_initializer_opt (cp_parser* parser)
7127 /* If the next token is not a `:', then there is no
7128 ctor-initializer. */
7129 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
7131 /* Do default initialization of any bases and members. */
7132 if (DECL_CONSTRUCTOR_P (current_function_decl))
7133 finish_mem_initializers (NULL_TREE);
7138 /* Consume the `:' token. */
7139 cp_lexer_consume_token (parser->lexer);
7140 /* And the mem-initializer-list. */
7141 cp_parser_mem_initializer_list (parser);
7146 /* Parse a mem-initializer-list.
7148 mem-initializer-list:
7150 mem-initializer , mem-initializer-list */
7153 cp_parser_mem_initializer_list (cp_parser* parser)
7155 tree mem_initializer_list = NULL_TREE;
7157 /* Let the semantic analysis code know that we are starting the
7158 mem-initializer-list. */
7159 if (!DECL_CONSTRUCTOR_P (current_function_decl))
7160 error ("only constructors take base initializers");
7162 /* Loop through the list. */
7165 tree mem_initializer;
7167 /* Parse the mem-initializer. */
7168 mem_initializer = cp_parser_mem_initializer (parser);
7169 /* Add it to the list, unless it was erroneous. */
7170 if (mem_initializer)
7172 TREE_CHAIN (mem_initializer) = mem_initializer_list;
7173 mem_initializer_list = mem_initializer;
7175 /* If the next token is not a `,', we're done. */
7176 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7178 /* Consume the `,' token. */
7179 cp_lexer_consume_token (parser->lexer);
7182 /* Perform semantic analysis. */
7183 if (DECL_CONSTRUCTOR_P (current_function_decl))
7184 finish_mem_initializers (mem_initializer_list);
7187 /* Parse a mem-initializer.
7190 mem-initializer-id ( expression-list [opt] )
7195 ( expression-list [opt] )
7197 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
7198 class) or FIELD_DECL (for a non-static data member) to initialize;
7199 the TREE_VALUE is the expression-list. */
7202 cp_parser_mem_initializer (cp_parser* parser)
7204 tree mem_initializer_id;
7205 tree expression_list;
7208 /* Find out what is being initialized. */
7209 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7211 pedwarn ("anachronistic old-style base class initializer");
7212 mem_initializer_id = NULL_TREE;
7215 mem_initializer_id = cp_parser_mem_initializer_id (parser);
7216 member = expand_member_init (mem_initializer_id);
7217 if (member && !DECL_P (member))
7218 in_base_initializer = 1;
7221 = cp_parser_parenthesized_expression_list (parser, false,
7222 /*non_constant_p=*/NULL);
7223 if (!expression_list)
7224 expression_list = void_type_node;
7226 in_base_initializer = 0;
7228 return member ? build_tree_list (member, expression_list) : NULL_TREE;
7231 /* Parse a mem-initializer-id.
7234 :: [opt] nested-name-specifier [opt] class-name
7237 Returns a TYPE indicating the class to be initializer for the first
7238 production. Returns an IDENTIFIER_NODE indicating the data member
7239 to be initialized for the second production. */
7242 cp_parser_mem_initializer_id (cp_parser* parser)
7244 bool global_scope_p;
7245 bool nested_name_specifier_p;
7248 /* Look for the optional `::' operator. */
7250 = (cp_parser_global_scope_opt (parser,
7251 /*current_scope_valid_p=*/false)
7253 /* Look for the optional nested-name-specifier. The simplest way to
7258 The keyword `typename' is not permitted in a base-specifier or
7259 mem-initializer; in these contexts a qualified name that
7260 depends on a template-parameter is implicitly assumed to be a
7263 is to assume that we have seen the `typename' keyword at this
7265 nested_name_specifier_p
7266 = (cp_parser_nested_name_specifier_opt (parser,
7267 /*typename_keyword_p=*/true,
7268 /*check_dependency_p=*/true,
7270 /*is_declaration=*/true)
7272 /* If there is a `::' operator or a nested-name-specifier, then we
7273 are definitely looking for a class-name. */
7274 if (global_scope_p || nested_name_specifier_p)
7275 return cp_parser_class_name (parser,
7276 /*typename_keyword_p=*/true,
7277 /*template_keyword_p=*/false,
7279 /*check_dependency_p=*/true,
7280 /*class_head_p=*/false,
7281 /*is_declaration=*/true);
7282 /* Otherwise, we could also be looking for an ordinary identifier. */
7283 cp_parser_parse_tentatively (parser);
7284 /* Try a class-name. */
7285 id = cp_parser_class_name (parser,
7286 /*typename_keyword_p=*/true,
7287 /*template_keyword_p=*/false,
7289 /*check_dependency_p=*/true,
7290 /*class_head_p=*/false,
7291 /*is_declaration=*/true);
7292 /* If we found one, we're done. */
7293 if (cp_parser_parse_definitely (parser))
7295 /* Otherwise, look for an ordinary identifier. */
7296 return cp_parser_identifier (parser);
7299 /* Overloading [gram.over] */
7301 /* Parse an operator-function-id.
7303 operator-function-id:
7306 Returns an IDENTIFIER_NODE for the operator which is a
7307 human-readable spelling of the identifier, e.g., `operator +'. */
7310 cp_parser_operator_function_id (cp_parser* parser)
7312 /* Look for the `operator' keyword. */
7313 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
7314 return error_mark_node;
7315 /* And then the name of the operator itself. */
7316 return cp_parser_operator (parser);
7319 /* Parse an operator.
7322 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
7323 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
7324 || ++ -- , ->* -> () []
7331 Returns an IDENTIFIER_NODE for the operator which is a
7332 human-readable spelling of the identifier, e.g., `operator +'. */
7335 cp_parser_operator (cp_parser* parser)
7337 tree id = NULL_TREE;
7340 /* Peek at the next token. */
7341 token = cp_lexer_peek_token (parser->lexer);
7342 /* Figure out which operator we have. */
7343 switch (token->type)
7349 /* The keyword should be either `new' or `delete'. */
7350 if (token->keyword == RID_NEW)
7352 else if (token->keyword == RID_DELETE)
7357 /* Consume the `new' or `delete' token. */
7358 cp_lexer_consume_token (parser->lexer);
7360 /* Peek at the next token. */
7361 token = cp_lexer_peek_token (parser->lexer);
7362 /* If it's a `[' token then this is the array variant of the
7364 if (token->type == CPP_OPEN_SQUARE)
7366 /* Consume the `[' token. */
7367 cp_lexer_consume_token (parser->lexer);
7368 /* Look for the `]' token. */
7369 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
7370 id = ansi_opname (op == NEW_EXPR
7371 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
7373 /* Otherwise, we have the non-array variant. */
7375 id = ansi_opname (op);
7381 id = ansi_opname (PLUS_EXPR);
7385 id = ansi_opname (MINUS_EXPR);
7389 id = ansi_opname (MULT_EXPR);
7393 id = ansi_opname (TRUNC_DIV_EXPR);
7397 id = ansi_opname (TRUNC_MOD_EXPR);
7401 id = ansi_opname (BIT_XOR_EXPR);
7405 id = ansi_opname (BIT_AND_EXPR);
7409 id = ansi_opname (BIT_IOR_EXPR);
7413 id = ansi_opname (BIT_NOT_EXPR);
7417 id = ansi_opname (TRUTH_NOT_EXPR);
7421 id = ansi_assopname (NOP_EXPR);
7425 id = ansi_opname (LT_EXPR);
7429 id = ansi_opname (GT_EXPR);
7433 id = ansi_assopname (PLUS_EXPR);
7437 id = ansi_assopname (MINUS_EXPR);
7441 id = ansi_assopname (MULT_EXPR);
7445 id = ansi_assopname (TRUNC_DIV_EXPR);
7449 id = ansi_assopname (TRUNC_MOD_EXPR);
7453 id = ansi_assopname (BIT_XOR_EXPR);
7457 id = ansi_assopname (BIT_AND_EXPR);
7461 id = ansi_assopname (BIT_IOR_EXPR);
7465 id = ansi_opname (LSHIFT_EXPR);
7469 id = ansi_opname (RSHIFT_EXPR);
7473 id = ansi_assopname (LSHIFT_EXPR);
7477 id = ansi_assopname (RSHIFT_EXPR);
7481 id = ansi_opname (EQ_EXPR);
7485 id = ansi_opname (NE_EXPR);
7489 id = ansi_opname (LE_EXPR);
7492 case CPP_GREATER_EQ:
7493 id = ansi_opname (GE_EXPR);
7497 id = ansi_opname (TRUTH_ANDIF_EXPR);
7501 id = ansi_opname (TRUTH_ORIF_EXPR);
7505 id = ansi_opname (POSTINCREMENT_EXPR);
7508 case CPP_MINUS_MINUS:
7509 id = ansi_opname (PREDECREMENT_EXPR);
7513 id = ansi_opname (COMPOUND_EXPR);
7516 case CPP_DEREF_STAR:
7517 id = ansi_opname (MEMBER_REF);
7521 id = ansi_opname (COMPONENT_REF);
7524 case CPP_OPEN_PAREN:
7525 /* Consume the `('. */
7526 cp_lexer_consume_token (parser->lexer);
7527 /* Look for the matching `)'. */
7528 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
7529 return ansi_opname (CALL_EXPR);
7531 case CPP_OPEN_SQUARE:
7532 /* Consume the `['. */
7533 cp_lexer_consume_token (parser->lexer);
7534 /* Look for the matching `]'. */
7535 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
7536 return ansi_opname (ARRAY_REF);
7540 id = ansi_opname (MIN_EXPR);
7544 id = ansi_opname (MAX_EXPR);
7548 id = ansi_assopname (MIN_EXPR);
7552 id = ansi_assopname (MAX_EXPR);
7556 /* Anything else is an error. */
7560 /* If we have selected an identifier, we need to consume the
7563 cp_lexer_consume_token (parser->lexer);
7564 /* Otherwise, no valid operator name was present. */
7567 cp_parser_error (parser, "expected operator");
7568 id = error_mark_node;
7574 /* Parse a template-declaration.
7576 template-declaration:
7577 export [opt] template < template-parameter-list > declaration
7579 If MEMBER_P is TRUE, this template-declaration occurs within a
7582 The grammar rule given by the standard isn't correct. What
7585 template-declaration:
7586 export [opt] template-parameter-list-seq
7587 decl-specifier-seq [opt] init-declarator [opt] ;
7588 export [opt] template-parameter-list-seq
7591 template-parameter-list-seq:
7592 template-parameter-list-seq [opt]
7593 template < template-parameter-list > */
7596 cp_parser_template_declaration (cp_parser* parser, bool member_p)
7598 /* Check for `export'. */
7599 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
7601 /* Consume the `export' token. */
7602 cp_lexer_consume_token (parser->lexer);
7603 /* Warn that we do not support `export'. */
7604 warning ("keyword `export' not implemented, and will be ignored");
7607 cp_parser_template_declaration_after_export (parser, member_p);
7610 /* Parse a template-parameter-list.
7612 template-parameter-list:
7614 template-parameter-list , template-parameter
7616 Returns a TREE_LIST. Each node represents a template parameter.
7617 The nodes are connected via their TREE_CHAINs. */
7620 cp_parser_template_parameter_list (cp_parser* parser)
7622 tree parameter_list = NULL_TREE;
7629 /* Parse the template-parameter. */
7630 parameter = cp_parser_template_parameter (parser);
7631 /* Add it to the list. */
7632 parameter_list = process_template_parm (parameter_list,
7635 /* Peek at the next token. */
7636 token = cp_lexer_peek_token (parser->lexer);
7637 /* If it's not a `,', we're done. */
7638 if (token->type != CPP_COMMA)
7640 /* Otherwise, consume the `,' token. */
7641 cp_lexer_consume_token (parser->lexer);
7644 return parameter_list;
7647 /* Parse a template-parameter.
7651 parameter-declaration
7653 Returns a TREE_LIST. The TREE_VALUE represents the parameter. The
7654 TREE_PURPOSE is the default value, if any. */
7657 cp_parser_template_parameter (cp_parser* parser)
7661 /* Peek at the next token. */
7662 token = cp_lexer_peek_token (parser->lexer);
7663 /* If it is `class' or `template', we have a type-parameter. */
7664 if (token->keyword == RID_TEMPLATE)
7665 return cp_parser_type_parameter (parser);
7666 /* If it is `class' or `typename' we do not know yet whether it is a
7667 type parameter or a non-type parameter. Consider:
7669 template <typename T, typename T::X X> ...
7673 template <class C, class D*> ...
7675 Here, the first parameter is a type parameter, and the second is
7676 a non-type parameter. We can tell by looking at the token after
7677 the identifier -- if it is a `,', `=', or `>' then we have a type
7679 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
7681 /* Peek at the token after `class' or `typename'. */
7682 token = cp_lexer_peek_nth_token (parser->lexer, 2);
7683 /* If it's an identifier, skip it. */
7684 if (token->type == CPP_NAME)
7685 token = cp_lexer_peek_nth_token (parser->lexer, 3);
7686 /* Now, see if the token looks like the end of a template
7688 if (token->type == CPP_COMMA
7689 || token->type == CPP_EQ
7690 || token->type == CPP_GREATER)
7691 return cp_parser_type_parameter (parser);
7694 /* Otherwise, it is a non-type parameter.
7698 When parsing a default template-argument for a non-type
7699 template-parameter, the first non-nested `>' is taken as the end
7700 of the template parameter-list rather than a greater-than
7703 cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
7704 /*parenthesized_p=*/NULL);
7707 /* Parse a type-parameter.
7710 class identifier [opt]
7711 class identifier [opt] = type-id
7712 typename identifier [opt]
7713 typename identifier [opt] = type-id
7714 template < template-parameter-list > class identifier [opt]
7715 template < template-parameter-list > class identifier [opt]
7718 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
7719 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
7720 the declaration of the parameter. */
7723 cp_parser_type_parameter (cp_parser* parser)
7728 /* Look for a keyword to tell us what kind of parameter this is. */
7729 token = cp_parser_require (parser, CPP_KEYWORD,
7730 "`class', `typename', or `template'");
7732 return error_mark_node;
7734 switch (token->keyword)
7740 tree default_argument;
7742 /* If the next token is an identifier, then it names the
7744 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
7745 identifier = cp_parser_identifier (parser);
7747 identifier = NULL_TREE;
7749 /* Create the parameter. */
7750 parameter = finish_template_type_parm (class_type_node, identifier);
7752 /* If the next token is an `=', we have a default argument. */
7753 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7755 /* Consume the `=' token. */
7756 cp_lexer_consume_token (parser->lexer);
7757 /* Parse the default-argument. */
7758 default_argument = cp_parser_type_id (parser);
7761 default_argument = NULL_TREE;
7763 /* Create the combined representation of the parameter and the
7764 default argument. */
7765 parameter = build_tree_list (default_argument, parameter);
7771 tree parameter_list;
7773 tree default_argument;
7775 /* Look for the `<'. */
7776 cp_parser_require (parser, CPP_LESS, "`<'");
7777 /* Parse the template-parameter-list. */
7778 begin_template_parm_list ();
7780 = cp_parser_template_parameter_list (parser);
7781 parameter_list = end_template_parm_list (parameter_list);
7782 /* Look for the `>'. */
7783 cp_parser_require (parser, CPP_GREATER, "`>'");
7784 /* Look for the `class' keyword. */
7785 cp_parser_require_keyword (parser, RID_CLASS, "`class'");
7786 /* If the next token is an `=', then there is a
7787 default-argument. If the next token is a `>', we are at
7788 the end of the parameter-list. If the next token is a `,',
7789 then we are at the end of this parameter. */
7790 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
7791 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
7792 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7793 identifier = cp_parser_identifier (parser);
7795 identifier = NULL_TREE;
7796 /* Create the template parameter. */
7797 parameter = finish_template_template_parm (class_type_node,
7800 /* If the next token is an `=', then there is a
7801 default-argument. */
7802 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7806 /* Consume the `='. */
7807 cp_lexer_consume_token (parser->lexer);
7808 /* Parse the id-expression. */
7810 = cp_parser_id_expression (parser,
7811 /*template_keyword_p=*/false,
7812 /*check_dependency_p=*/true,
7813 /*template_p=*/&is_template,
7814 /*declarator_p=*/false);
7815 if (TREE_CODE (default_argument) == TYPE_DECL)
7816 /* If the id-expression was a template-id that refers to
7817 a template-class, we already have the declaration here,
7818 so no further lookup is needed. */
7821 /* Look up the name. */
7823 = cp_parser_lookup_name (parser, default_argument,
7825 /*is_template=*/is_template,
7826 /*is_namespace=*/false,
7827 /*check_dependency=*/true);
7828 /* See if the default argument is valid. */
7830 = check_template_template_default_arg (default_argument);
7833 default_argument = NULL_TREE;
7835 /* Create the combined representation of the parameter and the
7836 default argument. */
7837 parameter = build_tree_list (default_argument, parameter);
7842 /* Anything else is an error. */
7843 cp_parser_error (parser,
7844 "expected `class', `typename', or `template'");
7845 parameter = error_mark_node;
7851 /* Parse a template-id.
7854 template-name < template-argument-list [opt] >
7856 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
7857 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
7858 returned. Otherwise, if the template-name names a function, or set
7859 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
7860 names a class, returns a TYPE_DECL for the specialization.
7862 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
7863 uninstantiated templates. */
7866 cp_parser_template_id (cp_parser *parser,
7867 bool template_keyword_p,
7868 bool check_dependency_p,
7869 bool is_declaration)
7874 ptrdiff_t start_of_id;
7875 tree access_check = NULL_TREE;
7876 cp_token *next_token, *next_token_2;
7879 /* If the next token corresponds to a template-id, there is no need
7881 next_token = cp_lexer_peek_token (parser->lexer);
7882 if (next_token->type == CPP_TEMPLATE_ID)
7887 /* Get the stored value. */
7888 value = cp_lexer_consume_token (parser->lexer)->value;
7889 /* Perform any access checks that were deferred. */
7890 for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
7891 perform_or_defer_access_check (TREE_PURPOSE (check),
7892 TREE_VALUE (check));
7893 /* Return the stored value. */
7894 return TREE_VALUE (value);
7897 /* Avoid performing name lookup if there is no possibility of
7898 finding a template-id. */
7899 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
7900 || (next_token->type == CPP_NAME
7901 && !cp_parser_nth_token_starts_template_argument_list_p
7904 cp_parser_error (parser, "expected template-id");
7905 return error_mark_node;
7908 /* Remember where the template-id starts. */
7909 if (cp_parser_parsing_tentatively (parser)
7910 && !cp_parser_committed_to_tentative_parse (parser))
7912 next_token = cp_lexer_peek_token (parser->lexer);
7913 start_of_id = cp_lexer_token_difference (parser->lexer,
7914 parser->lexer->first_token,
7920 push_deferring_access_checks (dk_deferred);
7922 /* Parse the template-name. */
7923 is_identifier = false;
7924 template = cp_parser_template_name (parser, template_keyword_p,
7928 if (template == error_mark_node || is_identifier)
7930 pop_deferring_access_checks ();
7934 /* If we find the sequence `[:' after a template-name, it's probably
7935 a digraph-typo for `< ::'. Substitute the tokens and check if we can
7936 parse correctly the argument list. */
7937 next_token = cp_lexer_peek_nth_token (parser->lexer, 1);
7938 next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
7939 if (next_token->type == CPP_OPEN_SQUARE
7940 && next_token->flags & DIGRAPH
7941 && next_token_2->type == CPP_COLON
7942 && !(next_token_2->flags & PREV_WHITE))
7944 cp_parser_parse_tentatively (parser);
7945 /* Change `:' into `::'. */
7946 next_token_2->type = CPP_SCOPE;
7947 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
7949 cp_lexer_consume_token (parser->lexer);
7950 /* Parse the arguments. */
7951 arguments = cp_parser_enclosed_template_argument_list (parser);
7952 if (!cp_parser_parse_definitely (parser))
7954 /* If we couldn't parse an argument list, then we revert our changes
7955 and return simply an error. Maybe this is not a template-id
7957 next_token_2->type = CPP_COLON;
7958 cp_parser_error (parser, "expected `<'");
7959 pop_deferring_access_checks ();
7960 return error_mark_node;
7962 /* Otherwise, emit an error about the invalid digraph, but continue
7963 parsing because we got our argument list. */
7964 pedwarn ("`<::' cannot begin a template-argument list");
7965 inform ("`<:' is an alternate spelling for `['. Insert whitespace "
7966 "between `<' and `::'");
7967 if (!flag_permissive)
7972 inform ("(if you use `-fpermissive' G++ will accept your code)");
7979 /* Look for the `<' that starts the template-argument-list. */
7980 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
7982 pop_deferring_access_checks ();
7983 return error_mark_node;
7985 /* Parse the arguments. */
7986 arguments = cp_parser_enclosed_template_argument_list (parser);
7989 /* Build a representation of the specialization. */
7990 if (TREE_CODE (template) == IDENTIFIER_NODE)
7991 template_id = build_min_nt (TEMPLATE_ID_EXPR, template, arguments);
7992 else if (DECL_CLASS_TEMPLATE_P (template)
7993 || DECL_TEMPLATE_TEMPLATE_PARM_P (template))
7995 = finish_template_type (template, arguments,
7996 cp_lexer_next_token_is (parser->lexer,
8000 /* If it's not a class-template or a template-template, it should be
8001 a function-template. */
8002 my_friendly_assert ((DECL_FUNCTION_TEMPLATE_P (template)
8003 || TREE_CODE (template) == OVERLOAD
8004 || BASELINK_P (template)),
8007 template_id = lookup_template_function (template, arguments);
8010 /* Retrieve any deferred checks. Do not pop this access checks yet
8011 so the memory will not be reclaimed during token replacing below. */
8012 access_check = get_deferred_access_checks ();
8014 /* If parsing tentatively, replace the sequence of tokens that makes
8015 up the template-id with a CPP_TEMPLATE_ID token. That way,
8016 should we re-parse the token stream, we will not have to repeat
8017 the effort required to do the parse, nor will we issue duplicate
8018 error messages about problems during instantiation of the
8020 if (start_of_id >= 0)
8024 /* Find the token that corresponds to the start of the
8026 token = cp_lexer_advance_token (parser->lexer,
8027 parser->lexer->first_token,
8030 /* Reset the contents of the START_OF_ID token. */
8031 token->type = CPP_TEMPLATE_ID;
8032 token->value = build_tree_list (access_check, template_id);
8033 token->keyword = RID_MAX;
8034 /* Purge all subsequent tokens. */
8035 cp_lexer_purge_tokens_after (parser->lexer, token);
8038 pop_deferring_access_checks ();
8042 /* Parse a template-name.
8047 The standard should actually say:
8051 operator-function-id
8053 A defect report has been filed about this issue.
8055 A conversion-function-id cannot be a template name because they cannot
8056 be part of a template-id. In fact, looking at this code:
8060 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
8061 It is impossible to call a templated conversion-function-id with an
8062 explicit argument list, since the only allowed template parameter is
8063 the type to which it is converting.
8065 If TEMPLATE_KEYWORD_P is true, then we have just seen the
8066 `template' keyword, in a construction like:
8070 In that case `f' is taken to be a template-name, even though there
8071 is no way of knowing for sure.
8073 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
8074 name refers to a set of overloaded functions, at least one of which
8075 is a template, or an IDENTIFIER_NODE with the name of the template,
8076 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
8077 names are looked up inside uninstantiated templates. */
8080 cp_parser_template_name (cp_parser* parser,
8081 bool template_keyword_p,
8082 bool check_dependency_p,
8083 bool is_declaration,
8084 bool *is_identifier)
8090 /* If the next token is `operator', then we have either an
8091 operator-function-id or a conversion-function-id. */
8092 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
8094 /* We don't know whether we're looking at an
8095 operator-function-id or a conversion-function-id. */
8096 cp_parser_parse_tentatively (parser);
8097 /* Try an operator-function-id. */
8098 identifier = cp_parser_operator_function_id (parser);
8099 /* If that didn't work, try a conversion-function-id. */
8100 if (!cp_parser_parse_definitely (parser))
8102 cp_parser_error (parser, "expected template-name");
8103 return error_mark_node;
8106 /* Look for the identifier. */
8108 identifier = cp_parser_identifier (parser);
8110 /* If we didn't find an identifier, we don't have a template-id. */
8111 if (identifier == error_mark_node)
8112 return error_mark_node;
8114 /* If the name immediately followed the `template' keyword, then it
8115 is a template-name. However, if the next token is not `<', then
8116 we do not treat it as a template-name, since it is not being used
8117 as part of a template-id. This enables us to handle constructs
8120 template <typename T> struct S { S(); };
8121 template <typename T> S<T>::S();
8123 correctly. We would treat `S' as a template -- if it were `S<T>'
8124 -- but we do not if there is no `<'. */
8126 if (processing_template_decl
8127 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
8129 /* In a declaration, in a dependent context, we pretend that the
8130 "template" keyword was present in order to improve error
8131 recovery. For example, given:
8133 template <typename T> void f(T::X<int>);
8135 we want to treat "X<int>" as a template-id. */
8137 && !template_keyword_p
8138 && parser->scope && TYPE_P (parser->scope)
8139 && dependent_type_p (parser->scope))
8143 /* Explain what went wrong. */
8144 error ("non-template `%D' used as template", identifier);
8145 error ("(use `%T::template %D' to indicate that it is a template)",
8146 parser->scope, identifier);
8147 /* If parsing tentatively, find the location of the "<"
8149 if (cp_parser_parsing_tentatively (parser)
8150 && !cp_parser_committed_to_tentative_parse (parser))
8152 cp_parser_simulate_error (parser);
8153 token = cp_lexer_peek_token (parser->lexer);
8154 token = cp_lexer_prev_token (parser->lexer, token);
8155 start = cp_lexer_token_difference (parser->lexer,
8156 parser->lexer->first_token,
8161 /* Parse the template arguments so that we can issue error
8162 messages about them. */
8163 cp_lexer_consume_token (parser->lexer);
8164 cp_parser_enclosed_template_argument_list (parser);
8165 /* Skip tokens until we find a good place from which to
8166 continue parsing. */
8167 cp_parser_skip_to_closing_parenthesis (parser,
8168 /*recovering=*/true,
8170 /*consume_paren=*/false);
8171 /* If parsing tentatively, permanently remove the
8172 template argument list. That will prevent duplicate
8173 error messages from being issued about the missing
8174 "template" keyword. */
8177 token = cp_lexer_advance_token (parser->lexer,
8178 parser->lexer->first_token,
8180 cp_lexer_purge_tokens_after (parser->lexer, token);
8183 *is_identifier = true;
8186 if (template_keyword_p)
8190 /* Look up the name. */
8191 decl = cp_parser_lookup_name (parser, identifier,
8193 /*is_template=*/false,
8194 /*is_namespace=*/false,
8195 check_dependency_p);
8196 decl = maybe_get_template_decl_from_type_decl (decl);
8198 /* If DECL is a template, then the name was a template-name. */
8199 if (TREE_CODE (decl) == TEMPLATE_DECL)
8203 /* The standard does not explicitly indicate whether a name that
8204 names a set of overloaded declarations, some of which are
8205 templates, is a template-name. However, such a name should
8206 be a template-name; otherwise, there is no way to form a
8207 template-id for the overloaded templates. */
8208 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
8209 if (TREE_CODE (fns) == OVERLOAD)
8213 for (fn = fns; fn; fn = OVL_NEXT (fn))
8214 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
8219 /* Otherwise, the name does not name a template. */
8220 cp_parser_error (parser, "expected template-name");
8221 return error_mark_node;
8225 /* If DECL is dependent, and refers to a function, then just return
8226 its name; we will look it up again during template instantiation. */
8227 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
8229 tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
8230 if (TYPE_P (scope) && dependent_type_p (scope))
8237 /* Parse a template-argument-list.
8239 template-argument-list:
8241 template-argument-list , template-argument
8243 Returns a TREE_VEC containing the arguments. */
8246 cp_parser_template_argument_list (cp_parser* parser)
8248 tree fixed_args[10];
8249 unsigned n_args = 0;
8250 unsigned alloced = 10;
8251 tree *arg_ary = fixed_args;
8253 bool saved_in_template_argument_list_p;
8255 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
8256 parser->in_template_argument_list_p = true;
8262 /* Consume the comma. */
8263 cp_lexer_consume_token (parser->lexer);
8265 /* Parse the template-argument. */
8266 argument = cp_parser_template_argument (parser);
8267 if (n_args == alloced)
8271 if (arg_ary == fixed_args)
8273 arg_ary = xmalloc (sizeof (tree) * alloced);
8274 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
8277 arg_ary = xrealloc (arg_ary, sizeof (tree) * alloced);
8279 arg_ary[n_args++] = argument;
8281 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
8283 vec = make_tree_vec (n_args);
8286 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
8288 if (arg_ary != fixed_args)
8290 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
8294 /* Parse a template-argument.
8297 assignment-expression
8301 The representation is that of an assignment-expression, type-id, or
8302 id-expression -- except that the qualified id-expression is
8303 evaluated, so that the value returned is either a DECL or an
8306 Although the standard says "assignment-expression", it forbids
8307 throw-expressions or assignments in the template argument.
8308 Therefore, we use "conditional-expression" instead. */
8311 cp_parser_template_argument (cp_parser* parser)
8316 bool maybe_type_id = false;
8319 tree qualifying_class;
8321 /* There's really no way to know what we're looking at, so we just
8322 try each alternative in order.
8326 In a template-argument, an ambiguity between a type-id and an
8327 expression is resolved to a type-id, regardless of the form of
8328 the corresponding template-parameter.
8330 Therefore, we try a type-id first. */
8331 cp_parser_parse_tentatively (parser);
8332 argument = cp_parser_type_id (parser);
8333 /* If there was no error parsing the type-id but the next token is a '>>',
8334 we probably found a typo for '> >'. But there are type-id which are
8335 also valid expressions. For instance:
8337 struct X { int operator >> (int); };
8338 template <int V> struct Foo {};
8341 Here 'X()' is a valid type-id of a function type, but the user just
8342 wanted to write the expression "X() >> 5". Thus, we remember that we
8343 found a valid type-id, but we still try to parse the argument as an
8344 expression to see what happens. */
8345 if (!cp_parser_error_occurred (parser)
8346 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
8348 maybe_type_id = true;
8349 cp_parser_abort_tentative_parse (parser);
8353 /* If the next token isn't a `,' or a `>', then this argument wasn't
8354 really finished. This means that the argument is not a valid
8356 if (!cp_parser_next_token_ends_template_argument_p (parser))
8357 cp_parser_error (parser, "expected template-argument");
8358 /* If that worked, we're done. */
8359 if (cp_parser_parse_definitely (parser))
8362 /* We're still not sure what the argument will be. */
8363 cp_parser_parse_tentatively (parser);
8364 /* Try a template. */
8365 argument = cp_parser_id_expression (parser,
8366 /*template_keyword_p=*/false,
8367 /*check_dependency_p=*/true,
8369 /*declarator_p=*/false);
8370 /* If the next token isn't a `,' or a `>', then this argument wasn't
8372 if (!cp_parser_next_token_ends_template_argument_p (parser))
8373 cp_parser_error (parser, "expected template-argument");
8374 if (!cp_parser_error_occurred (parser))
8376 /* Figure out what is being referred to. */
8377 argument = cp_parser_lookup_name (parser, argument,
8379 /*is_template=*/template_p,
8380 /*is_namespace=*/false,
8381 /*check_dependency=*/true);
8382 if (TREE_CODE (argument) != TEMPLATE_DECL
8383 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
8384 cp_parser_error (parser, "expected template-name");
8386 if (cp_parser_parse_definitely (parser))
8388 /* It must be a non-type argument. There permitted cases are given
8389 in [temp.arg.nontype]:
8391 -- an integral constant-expression of integral or enumeration
8394 -- the name of a non-type template-parameter; or
8396 -- the name of an object or function with external linkage...
8398 -- the address of an object or function with external linkage...
8400 -- a pointer to member... */
8401 /* Look for a non-type template parameter. */
8402 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
8404 cp_parser_parse_tentatively (parser);
8405 argument = cp_parser_primary_expression (parser,
8408 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
8409 || !cp_parser_next_token_ends_template_argument_p (parser))
8410 cp_parser_simulate_error (parser);
8411 if (cp_parser_parse_definitely (parser))
8414 /* If the next token is "&", the argument must be the address of an
8415 object or function with external linkage. */
8416 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
8418 cp_lexer_consume_token (parser->lexer);
8419 /* See if we might have an id-expression. */
8420 token = cp_lexer_peek_token (parser->lexer);
8421 if (token->type == CPP_NAME
8422 || token->keyword == RID_OPERATOR
8423 || token->type == CPP_SCOPE
8424 || token->type == CPP_TEMPLATE_ID
8425 || token->type == CPP_NESTED_NAME_SPECIFIER)
8427 cp_parser_parse_tentatively (parser);
8428 argument = cp_parser_primary_expression (parser,
8431 if (cp_parser_error_occurred (parser)
8432 || !cp_parser_next_token_ends_template_argument_p (parser))
8433 cp_parser_abort_tentative_parse (parser);
8436 if (qualifying_class)
8437 argument = finish_qualified_id_expr (qualifying_class,
8441 if (TREE_CODE (argument) == VAR_DECL)
8443 /* A variable without external linkage might still be a
8444 valid constant-expression, so no error is issued here
8445 if the external-linkage check fails. */
8446 if (!DECL_EXTERNAL_LINKAGE_P (argument))
8447 cp_parser_simulate_error (parser);
8449 else if (is_overloaded_fn (argument))
8450 /* All overloaded functions are allowed; if the external
8451 linkage test does not pass, an error will be issued
8455 && (TREE_CODE (argument) == OFFSET_REF
8456 || TREE_CODE (argument) == SCOPE_REF))
8457 /* A pointer-to-member. */
8460 cp_parser_simulate_error (parser);
8462 if (cp_parser_parse_definitely (parser))
8465 argument = build_x_unary_op (ADDR_EXPR, argument);
8470 /* If the argument started with "&", there are no other valid
8471 alternatives at this point. */
8474 cp_parser_error (parser, "invalid non-type template argument");
8475 return error_mark_node;
8477 /* If the argument wasn't successfully parsed as a type-id followed
8478 by '>>', the argument can only be a constant expression now.
8479 Otherwise, we try parsing the constant-expression tentatively,
8480 because the argument could really be a type-id. */
8482 cp_parser_parse_tentatively (parser);
8483 argument = cp_parser_constant_expression (parser,
8484 /*allow_non_constant_p=*/false,
8485 /*non_constant_p=*/NULL);
8486 argument = fold_non_dependent_expr (argument);
8489 if (!cp_parser_next_token_ends_template_argument_p (parser))
8490 cp_parser_error (parser, "expected template-argument");
8491 if (cp_parser_parse_definitely (parser))
8493 /* We did our best to parse the argument as a non type-id, but that
8494 was the only alternative that matched (albeit with a '>' after
8495 it). We can assume it's just a typo from the user, and a
8496 diagnostic will then be issued. */
8497 return cp_parser_type_id (parser);
8500 /* Parse an explicit-instantiation.
8502 explicit-instantiation:
8503 template declaration
8505 Although the standard says `declaration', what it really means is:
8507 explicit-instantiation:
8508 template decl-specifier-seq [opt] declarator [opt] ;
8510 Things like `template int S<int>::i = 5, int S<double>::j;' are not
8511 supposed to be allowed. A defect report has been filed about this
8516 explicit-instantiation:
8517 storage-class-specifier template
8518 decl-specifier-seq [opt] declarator [opt] ;
8519 function-specifier template
8520 decl-specifier-seq [opt] declarator [opt] ; */
8523 cp_parser_explicit_instantiation (cp_parser* parser)
8525 int declares_class_or_enum;
8526 tree decl_specifiers;
8528 tree extension_specifier = NULL_TREE;
8530 /* Look for an (optional) storage-class-specifier or
8531 function-specifier. */
8532 if (cp_parser_allow_gnu_extensions_p (parser))
8535 = cp_parser_storage_class_specifier_opt (parser);
8536 if (!extension_specifier)
8537 extension_specifier = cp_parser_function_specifier_opt (parser);
8540 /* Look for the `template' keyword. */
8541 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
8542 /* Let the front end know that we are processing an explicit
8544 begin_explicit_instantiation ();
8545 /* [temp.explicit] says that we are supposed to ignore access
8546 control while processing explicit instantiation directives. */
8547 push_deferring_access_checks (dk_no_check);
8548 /* Parse a decl-specifier-seq. */
8550 = cp_parser_decl_specifier_seq (parser,
8551 CP_PARSER_FLAGS_OPTIONAL,
8553 &declares_class_or_enum);
8554 /* If there was exactly one decl-specifier, and it declared a class,
8555 and there's no declarator, then we have an explicit type
8557 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
8561 type = check_tag_decl (decl_specifiers);
8562 /* Turn access control back on for names used during
8563 template instantiation. */
8564 pop_deferring_access_checks ();
8566 do_type_instantiation (type, extension_specifier, /*complain=*/1);
8573 /* Parse the declarator. */
8575 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
8576 /*ctor_dtor_or_conv_p=*/NULL,
8577 /*parenthesized_p=*/NULL);
8578 cp_parser_check_for_definition_in_return_type (declarator,
8579 declares_class_or_enum);
8580 if (declarator != error_mark_node)
8582 decl = grokdeclarator (declarator, decl_specifiers,
8584 /* Turn access control back on for names used during
8585 template instantiation. */
8586 pop_deferring_access_checks ();
8587 /* Do the explicit instantiation. */
8588 do_decl_instantiation (decl, extension_specifier);
8592 pop_deferring_access_checks ();
8593 /* Skip the body of the explicit instantiation. */
8594 cp_parser_skip_to_end_of_statement (parser);
8597 /* We're done with the instantiation. */
8598 end_explicit_instantiation ();
8600 cp_parser_consume_semicolon_at_end_of_statement (parser);
8603 /* Parse an explicit-specialization.
8605 explicit-specialization:
8606 template < > declaration
8608 Although the standard says `declaration', what it really means is:
8610 explicit-specialization:
8611 template <> decl-specifier [opt] init-declarator [opt] ;
8612 template <> function-definition
8613 template <> explicit-specialization
8614 template <> template-declaration */
8617 cp_parser_explicit_specialization (cp_parser* parser)
8619 /* Look for the `template' keyword. */
8620 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
8621 /* Look for the `<'. */
8622 cp_parser_require (parser, CPP_LESS, "`<'");
8623 /* Look for the `>'. */
8624 cp_parser_require (parser, CPP_GREATER, "`>'");
8625 /* We have processed another parameter list. */
8626 ++parser->num_template_parameter_lists;
8627 /* Let the front end know that we are beginning a specialization. */
8628 begin_specialization ();
8630 /* If the next keyword is `template', we need to figure out whether
8631 or not we're looking a template-declaration. */
8632 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
8634 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
8635 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
8636 cp_parser_template_declaration_after_export (parser,
8637 /*member_p=*/false);
8639 cp_parser_explicit_specialization (parser);
8642 /* Parse the dependent declaration. */
8643 cp_parser_single_declaration (parser,
8647 /* We're done with the specialization. */
8648 end_specialization ();
8649 /* We're done with this parameter list. */
8650 --parser->num_template_parameter_lists;
8653 /* Parse a type-specifier.
8656 simple-type-specifier
8659 elaborated-type-specifier
8667 Returns a representation of the type-specifier. If the
8668 type-specifier is a keyword (like `int' or `const', or
8669 `__complex__') then the corresponding IDENTIFIER_NODE is returned.
8670 For a class-specifier, enum-specifier, or elaborated-type-specifier
8671 a TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
8673 If IS_FRIEND is TRUE then this type-specifier is being declared a
8674 `friend'. If IS_DECLARATION is TRUE, then this type-specifier is
8675 appearing in a decl-specifier-seq.
8677 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
8678 class-specifier, enum-specifier, or elaborated-type-specifier, then
8679 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
8680 if a type is declared; 2 if it is defined. Otherwise, it is set to
8683 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
8684 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
8688 cp_parser_type_specifier (cp_parser* parser,
8689 cp_parser_flags flags,
8691 bool is_declaration,
8692 int* declares_class_or_enum,
8693 bool* is_cv_qualifier)
8695 tree type_spec = NULL_TREE;
8699 /* Assume this type-specifier does not declare a new type. */
8700 if (declares_class_or_enum)
8701 *declares_class_or_enum = 0;
8702 /* And that it does not specify a cv-qualifier. */
8703 if (is_cv_qualifier)
8704 *is_cv_qualifier = false;
8705 /* Peek at the next token. */
8706 token = cp_lexer_peek_token (parser->lexer);
8708 /* If we're looking at a keyword, we can use that to guide the
8709 production we choose. */
8710 keyword = token->keyword;
8713 /* Any of these indicate either a class-specifier, or an
8714 elaborated-type-specifier. */
8719 /* Parse tentatively so that we can back up if we don't find a
8720 class-specifier or enum-specifier. */
8721 cp_parser_parse_tentatively (parser);
8722 /* Look for the class-specifier or enum-specifier. */
8723 if (keyword == RID_ENUM)
8724 type_spec = cp_parser_enum_specifier (parser);
8726 type_spec = cp_parser_class_specifier (parser);
8728 /* If that worked, we're done. */
8729 if (cp_parser_parse_definitely (parser))
8731 if (declares_class_or_enum)
8732 *declares_class_or_enum = 2;
8739 /* Look for an elaborated-type-specifier. */
8740 type_spec = cp_parser_elaborated_type_specifier (parser,
8743 /* We're declaring a class or enum -- unless we're using
8745 if (declares_class_or_enum && keyword != RID_TYPENAME)
8746 *declares_class_or_enum = 1;
8752 type_spec = cp_parser_cv_qualifier_opt (parser);
8753 /* Even though we call a routine that looks for an optional
8754 qualifier, we know that there should be one. */
8755 my_friendly_assert (type_spec != NULL, 20000328);
8756 /* This type-specifier was a cv-qualified. */
8757 if (is_cv_qualifier)
8758 *is_cv_qualifier = true;
8763 /* The `__complex__' keyword is a GNU extension. */
8764 return cp_lexer_consume_token (parser->lexer)->value;
8770 /* If we do not already have a type-specifier, assume we are looking
8771 at a simple-type-specifier. */
8772 type_spec = cp_parser_simple_type_specifier (parser, flags,
8773 /*identifier_p=*/true);
8775 /* If we didn't find a type-specifier, and a type-specifier was not
8776 optional in this context, issue an error message. */
8777 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
8779 cp_parser_error (parser, "expected type specifier");
8780 return error_mark_node;
8786 /* Parse a simple-type-specifier.
8788 simple-type-specifier:
8789 :: [opt] nested-name-specifier [opt] type-name
8790 :: [opt] nested-name-specifier template template-id
8805 simple-type-specifier:
8806 __typeof__ unary-expression
8807 __typeof__ ( type-id )
8809 For the various keywords, the value returned is simply the
8810 TREE_IDENTIFIER representing the keyword if IDENTIFIER_P is true.
8811 For the first two productions, and if IDENTIFIER_P is false, the
8812 value returned is the indicated TYPE_DECL. */
8815 cp_parser_simple_type_specifier (cp_parser* parser, cp_parser_flags flags,
8818 tree type = NULL_TREE;
8821 /* Peek at the next token. */
8822 token = cp_lexer_peek_token (parser->lexer);
8824 /* If we're looking at a keyword, things are easy. */
8825 switch (token->keyword)
8828 type = char_type_node;
8831 type = wchar_type_node;
8834 type = boolean_type_node;
8837 type = short_integer_type_node;
8840 type = integer_type_node;
8843 type = long_integer_type_node;
8846 type = integer_type_node;
8849 type = unsigned_type_node;
8852 type = float_type_node;
8855 type = double_type_node;
8858 type = void_type_node;
8865 /* Consume the `typeof' token. */
8866 cp_lexer_consume_token (parser->lexer);
8867 /* Parse the operand to `typeof'. */
8868 operand = cp_parser_sizeof_operand (parser, RID_TYPEOF);
8869 /* If it is not already a TYPE, take its type. */
8870 if (!TYPE_P (operand))
8871 operand = finish_typeof (operand);
8880 /* If the type-specifier was for a built-in type, we're done. */
8885 /* Consume the token. */
8886 id = cp_lexer_consume_token (parser->lexer)->value;
8888 /* There is no valid C++ program where a non-template type is
8889 followed by a "<". That usually indicates that the user thought
8890 that the type was a template. */
8891 cp_parser_check_for_invalid_template_id (parser, type);
8893 return identifier_p ? id : TYPE_NAME (type);
8896 /* The type-specifier must be a user-defined type. */
8897 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
8899 /* Don't gobble tokens or issue error messages if this is an
8900 optional type-specifier. */
8901 if (flags & CP_PARSER_FLAGS_OPTIONAL)
8902 cp_parser_parse_tentatively (parser);
8904 /* Look for the optional `::' operator. */
8905 cp_parser_global_scope_opt (parser,
8906 /*current_scope_valid_p=*/false);
8907 /* Look for the nested-name specifier. */
8908 cp_parser_nested_name_specifier_opt (parser,
8909 /*typename_keyword_p=*/false,
8910 /*check_dependency_p=*/true,
8912 /*is_declaration=*/false);
8913 /* If we have seen a nested-name-specifier, and the next token
8914 is `template', then we are using the template-id production. */
8916 && cp_parser_optional_template_keyword (parser))
8918 /* Look for the template-id. */
8919 type = cp_parser_template_id (parser,
8920 /*template_keyword_p=*/true,
8921 /*check_dependency_p=*/true,
8922 /*is_declaration=*/false);
8923 /* If the template-id did not name a type, we are out of
8925 if (TREE_CODE (type) != TYPE_DECL)
8927 cp_parser_error (parser, "expected template-id for type");
8931 /* Otherwise, look for a type-name. */
8933 type = cp_parser_type_name (parser);
8934 /* If it didn't work out, we don't have a TYPE. */
8935 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
8936 && !cp_parser_parse_definitely (parser))
8940 /* If we didn't get a type-name, issue an error message. */
8941 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
8943 cp_parser_error (parser, "expected type-name");
8944 return error_mark_node;
8947 /* There is no valid C++ program where a non-template type is
8948 followed by a "<". That usually indicates that the user thought
8949 that the type was a template. */
8950 if (type && type != error_mark_node)
8951 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type));
8956 /* Parse a type-name.
8969 Returns a TYPE_DECL for the the type. */
8972 cp_parser_type_name (cp_parser* parser)
8977 /* We can't know yet whether it is a class-name or not. */
8978 cp_parser_parse_tentatively (parser);
8979 /* Try a class-name. */
8980 type_decl = cp_parser_class_name (parser,
8981 /*typename_keyword_p=*/false,
8982 /*template_keyword_p=*/false,
8984 /*check_dependency_p=*/true,
8985 /*class_head_p=*/false,
8986 /*is_declaration=*/false);
8987 /* If it's not a class-name, keep looking. */
8988 if (!cp_parser_parse_definitely (parser))
8990 /* It must be a typedef-name or an enum-name. */
8991 identifier = cp_parser_identifier (parser);
8992 if (identifier == error_mark_node)
8993 return error_mark_node;
8995 /* Look up the type-name. */
8996 type_decl = cp_parser_lookup_name_simple (parser, identifier);
8997 /* Issue an error if we did not find a type-name. */
8998 if (TREE_CODE (type_decl) != TYPE_DECL)
9000 if (!cp_parser_simulate_error (parser))
9001 cp_parser_name_lookup_error (parser, identifier, type_decl,
9003 type_decl = error_mark_node;
9005 /* Remember that the name was used in the definition of the
9006 current class so that we can check later to see if the
9007 meaning would have been different after the class was
9008 entirely defined. */
9009 else if (type_decl != error_mark_node
9011 maybe_note_name_used_in_class (identifier, type_decl);
9018 /* Parse an elaborated-type-specifier. Note that the grammar given
9019 here incorporates the resolution to DR68.
9021 elaborated-type-specifier:
9022 class-key :: [opt] nested-name-specifier [opt] identifier
9023 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
9024 enum :: [opt] nested-name-specifier [opt] identifier
9025 typename :: [opt] nested-name-specifier identifier
9026 typename :: [opt] nested-name-specifier template [opt]
9031 elaborated-type-specifier:
9032 class-key attributes :: [opt] nested-name-specifier [opt] identifier
9033 class-key attributes :: [opt] nested-name-specifier [opt]
9034 template [opt] template-id
9035 enum attributes :: [opt] nested-name-specifier [opt] identifier
9037 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
9038 declared `friend'. If IS_DECLARATION is TRUE, then this
9039 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
9040 something is being declared.
9042 Returns the TYPE specified. */
9045 cp_parser_elaborated_type_specifier (cp_parser* parser,
9047 bool is_declaration)
9049 enum tag_types tag_type;
9051 tree type = NULL_TREE;
9052 tree attributes = NULL_TREE;
9054 /* See if we're looking at the `enum' keyword. */
9055 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
9057 /* Consume the `enum' token. */
9058 cp_lexer_consume_token (parser->lexer);
9059 /* Remember that it's an enumeration type. */
9060 tag_type = enum_type;
9061 /* Parse the attributes. */
9062 attributes = cp_parser_attributes_opt (parser);
9064 /* Or, it might be `typename'. */
9065 else if (cp_lexer_next_token_is_keyword (parser->lexer,
9068 /* Consume the `typename' token. */
9069 cp_lexer_consume_token (parser->lexer);
9070 /* Remember that it's a `typename' type. */
9071 tag_type = typename_type;
9072 /* The `typename' keyword is only allowed in templates. */
9073 if (!processing_template_decl)
9074 pedwarn ("using `typename' outside of template");
9076 /* Otherwise it must be a class-key. */
9079 tag_type = cp_parser_class_key (parser);
9080 if (tag_type == none_type)
9081 return error_mark_node;
9082 /* Parse the attributes. */
9083 attributes = cp_parser_attributes_opt (parser);
9086 /* Look for the `::' operator. */
9087 cp_parser_global_scope_opt (parser,
9088 /*current_scope_valid_p=*/false);
9089 /* Look for the nested-name-specifier. */
9090 if (tag_type == typename_type)
9092 if (cp_parser_nested_name_specifier (parser,
9093 /*typename_keyword_p=*/true,
9094 /*check_dependency_p=*/true,
9098 return error_mark_node;
9101 /* Even though `typename' is not present, the proposed resolution
9102 to Core Issue 180 says that in `class A<T>::B', `B' should be
9103 considered a type-name, even if `A<T>' is dependent. */
9104 cp_parser_nested_name_specifier_opt (parser,
9105 /*typename_keyword_p=*/true,
9106 /*check_dependency_p=*/true,
9109 /* For everything but enumeration types, consider a template-id. */
9110 if (tag_type != enum_type)
9112 bool template_p = false;
9115 /* Allow the `template' keyword. */
9116 template_p = cp_parser_optional_template_keyword (parser);
9117 /* If we didn't see `template', we don't know if there's a
9118 template-id or not. */
9120 cp_parser_parse_tentatively (parser);
9121 /* Parse the template-id. */
9122 decl = cp_parser_template_id (parser, template_p,
9123 /*check_dependency_p=*/true,
9125 /* If we didn't find a template-id, look for an ordinary
9127 if (!template_p && !cp_parser_parse_definitely (parser))
9129 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
9130 in effect, then we must assume that, upon instantiation, the
9131 template will correspond to a class. */
9132 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
9133 && tag_type == typename_type)
9134 type = make_typename_type (parser->scope, decl,
9137 type = TREE_TYPE (decl);
9140 /* For an enumeration type, consider only a plain identifier. */
9143 identifier = cp_parser_identifier (parser);
9145 if (identifier == error_mark_node)
9147 parser->scope = NULL_TREE;
9148 return error_mark_node;
9151 /* For a `typename', we needn't call xref_tag. */
9152 if (tag_type == typename_type)
9153 return cp_parser_make_typename_type (parser, parser->scope,
9155 /* Look up a qualified name in the usual way. */
9160 /* In an elaborated-type-specifier, names are assumed to name
9161 types, so we set IS_TYPE to TRUE when calling
9162 cp_parser_lookup_name. */
9163 decl = cp_parser_lookup_name (parser, identifier,
9165 /*is_template=*/false,
9166 /*is_namespace=*/false,
9167 /*check_dependency=*/true);
9169 /* If we are parsing friend declaration, DECL may be a
9170 TEMPLATE_DECL tree node here. However, we need to check
9171 whether this TEMPLATE_DECL results in valid code. Consider
9172 the following example:
9175 template <class T> class C {};
9178 template <class T> friend class N::C; // #1, valid code
9180 template <class T> class Y {
9181 friend class N::C; // #2, invalid code
9184 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
9185 name lookup of `N::C'. We see that friend declaration must
9186 be template for the code to be valid. Note that
9187 processing_template_decl does not work here since it is
9188 always 1 for the above two cases. */
9190 decl = (cp_parser_maybe_treat_template_as_class
9191 (decl, /*tag_name_p=*/is_friend
9192 && parser->num_template_parameter_lists));
9194 if (TREE_CODE (decl) != TYPE_DECL)
9196 error ("expected type-name");
9197 return error_mark_node;
9200 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
9201 check_elaborated_type_specifier
9203 (parser->num_template_parameter_lists
9204 || DECL_SELF_REFERENCE_P (decl)));
9206 type = TREE_TYPE (decl);
9210 /* An elaborated-type-specifier sometimes introduces a new type and
9211 sometimes names an existing type. Normally, the rule is that it
9212 introduces a new type only if there is not an existing type of
9213 the same name already in scope. For example, given:
9216 void f() { struct S s; }
9218 the `struct S' in the body of `f' is the same `struct S' as in
9219 the global scope; the existing definition is used. However, if
9220 there were no global declaration, this would introduce a new
9221 local class named `S'.
9223 An exception to this rule applies to the following code:
9225 namespace N { struct S; }
9227 Here, the elaborated-type-specifier names a new type
9228 unconditionally; even if there is already an `S' in the
9229 containing scope this declaration names a new type.
9230 This exception only applies if the elaborated-type-specifier
9231 forms the complete declaration:
9235 A declaration consisting solely of `class-key identifier ;' is
9236 either a redeclaration of the name in the current scope or a
9237 forward declaration of the identifier as a class name. It
9238 introduces the name into the current scope.
9240 We are in this situation precisely when the next token is a `;'.
9242 An exception to the exception is that a `friend' declaration does
9243 *not* name a new type; i.e., given:
9245 struct S { friend struct T; };
9247 `T' is not a new type in the scope of `S'.
9249 Also, `new struct S' or `sizeof (struct S)' never results in the
9250 definition of a new type; a new type can only be declared in a
9251 declaration context. */
9253 /* Warn about attributes. They are ignored. */
9255 warning ("type attributes are honored only at type definition");
9257 type = xref_tag (tag_type, identifier,
9258 /*attributes=*/NULL_TREE,
9261 || cp_lexer_next_token_is_not (parser->lexer,
9263 parser->num_template_parameter_lists);
9266 if (tag_type != enum_type)
9267 cp_parser_check_class_key (tag_type, type);
9269 /* A "<" cannot follow an elaborated type specifier. If that
9270 happens, the user was probably trying to form a template-id. */
9271 cp_parser_check_for_invalid_template_id (parser, type);
9276 /* Parse an enum-specifier.
9279 enum identifier [opt] { enumerator-list [opt] }
9281 Returns an ENUM_TYPE representing the enumeration. */
9284 cp_parser_enum_specifier (cp_parser* parser)
9287 tree identifier = NULL_TREE;
9290 /* Look for the `enum' keyword. */
9291 if (!cp_parser_require_keyword (parser, RID_ENUM, "`enum'"))
9292 return error_mark_node;
9293 /* Peek at the next token. */
9294 token = cp_lexer_peek_token (parser->lexer);
9296 /* See if it is an identifier. */
9297 if (token->type == CPP_NAME)
9298 identifier = cp_parser_identifier (parser);
9300 /* Look for the `{'. */
9301 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
9302 return error_mark_node;
9304 /* At this point, we're going ahead with the enum-specifier, even
9305 if some other problem occurs. */
9306 cp_parser_commit_to_tentative_parse (parser);
9308 /* Issue an error message if type-definitions are forbidden here. */
9309 cp_parser_check_type_definition (parser);
9311 /* Create the new type. */
9312 type = start_enum (identifier ? identifier : make_anon_name ());
9314 /* Peek at the next token. */
9315 token = cp_lexer_peek_token (parser->lexer);
9316 /* If it's not a `}', then there are some enumerators. */
9317 if (token->type != CPP_CLOSE_BRACE)
9318 cp_parser_enumerator_list (parser, type);
9319 /* Look for the `}'. */
9320 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
9322 /* Finish up the enumeration. */
9328 /* Parse an enumerator-list. The enumerators all have the indicated
9332 enumerator-definition
9333 enumerator-list , enumerator-definition */
9336 cp_parser_enumerator_list (cp_parser* parser, tree type)
9342 /* Parse an enumerator-definition. */
9343 cp_parser_enumerator_definition (parser, type);
9344 /* Peek at the next token. */
9345 token = cp_lexer_peek_token (parser->lexer);
9346 /* If it's not a `,', then we've reached the end of the
9348 if (token->type != CPP_COMMA)
9350 /* Otherwise, consume the `,' and keep going. */
9351 cp_lexer_consume_token (parser->lexer);
9352 /* If the next token is a `}', there is a trailing comma. */
9353 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
9355 if (pedantic && !in_system_header)
9356 pedwarn ("comma at end of enumerator list");
9362 /* Parse an enumerator-definition. The enumerator has the indicated
9365 enumerator-definition:
9367 enumerator = constant-expression
9373 cp_parser_enumerator_definition (cp_parser* parser, tree type)
9379 /* Look for the identifier. */
9380 identifier = cp_parser_identifier (parser);
9381 if (identifier == error_mark_node)
9384 /* Peek at the next token. */
9385 token = cp_lexer_peek_token (parser->lexer);
9386 /* If it's an `=', then there's an explicit value. */
9387 if (token->type == CPP_EQ)
9389 /* Consume the `=' token. */
9390 cp_lexer_consume_token (parser->lexer);
9391 /* Parse the value. */
9392 value = cp_parser_constant_expression (parser,
9393 /*allow_non_constant_p=*/false,
9399 /* Create the enumerator. */
9400 build_enumerator (identifier, value, type);
9403 /* Parse a namespace-name.
9406 original-namespace-name
9409 Returns the NAMESPACE_DECL for the namespace. */
9412 cp_parser_namespace_name (cp_parser* parser)
9415 tree namespace_decl;
9417 /* Get the name of the namespace. */
9418 identifier = cp_parser_identifier (parser);
9419 if (identifier == error_mark_node)
9420 return error_mark_node;
9422 /* Look up the identifier in the currently active scope. Look only
9423 for namespaces, due to:
9427 When looking up a namespace-name in a using-directive or alias
9428 definition, only namespace names are considered.
9434 During the lookup of a name preceding the :: scope resolution
9435 operator, object, function, and enumerator names are ignored.
9437 (Note that cp_parser_class_or_namespace_name only calls this
9438 function if the token after the name is the scope resolution
9440 namespace_decl = cp_parser_lookup_name (parser, identifier,
9442 /*is_template=*/false,
9443 /*is_namespace=*/true,
9444 /*check_dependency=*/true);
9445 /* If it's not a namespace, issue an error. */
9446 if (namespace_decl == error_mark_node
9447 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
9449 cp_parser_error (parser, "expected namespace-name");
9450 namespace_decl = error_mark_node;
9453 return namespace_decl;
9456 /* Parse a namespace-definition.
9458 namespace-definition:
9459 named-namespace-definition
9460 unnamed-namespace-definition
9462 named-namespace-definition:
9463 original-namespace-definition
9464 extension-namespace-definition
9466 original-namespace-definition:
9467 namespace identifier { namespace-body }
9469 extension-namespace-definition:
9470 namespace original-namespace-name { namespace-body }
9472 unnamed-namespace-definition:
9473 namespace { namespace-body } */
9476 cp_parser_namespace_definition (cp_parser* parser)
9480 /* Look for the `namespace' keyword. */
9481 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
9483 /* Get the name of the namespace. We do not attempt to distinguish
9484 between an original-namespace-definition and an
9485 extension-namespace-definition at this point. The semantic
9486 analysis routines are responsible for that. */
9487 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
9488 identifier = cp_parser_identifier (parser);
9490 identifier = NULL_TREE;
9492 /* Look for the `{' to start the namespace. */
9493 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
9494 /* Start the namespace. */
9495 push_namespace (identifier);
9496 /* Parse the body of the namespace. */
9497 cp_parser_namespace_body (parser);
9498 /* Finish the namespace. */
9500 /* Look for the final `}'. */
9501 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
9504 /* Parse a namespace-body.
9507 declaration-seq [opt] */
9510 cp_parser_namespace_body (cp_parser* parser)
9512 cp_parser_declaration_seq_opt (parser);
9515 /* Parse a namespace-alias-definition.
9517 namespace-alias-definition:
9518 namespace identifier = qualified-namespace-specifier ; */
9521 cp_parser_namespace_alias_definition (cp_parser* parser)
9524 tree namespace_specifier;
9526 /* Look for the `namespace' keyword. */
9527 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
9528 /* Look for the identifier. */
9529 identifier = cp_parser_identifier (parser);
9530 if (identifier == error_mark_node)
9532 /* Look for the `=' token. */
9533 cp_parser_require (parser, CPP_EQ, "`='");
9534 /* Look for the qualified-namespace-specifier. */
9536 = cp_parser_qualified_namespace_specifier (parser);
9537 /* Look for the `;' token. */
9538 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
9540 /* Register the alias in the symbol table. */
9541 do_namespace_alias (identifier, namespace_specifier);
9544 /* Parse a qualified-namespace-specifier.
9546 qualified-namespace-specifier:
9547 :: [opt] nested-name-specifier [opt] namespace-name
9549 Returns a NAMESPACE_DECL corresponding to the specified
9553 cp_parser_qualified_namespace_specifier (cp_parser* parser)
9555 /* Look for the optional `::'. */
9556 cp_parser_global_scope_opt (parser,
9557 /*current_scope_valid_p=*/false);
9559 /* Look for the optional nested-name-specifier. */
9560 cp_parser_nested_name_specifier_opt (parser,
9561 /*typename_keyword_p=*/false,
9562 /*check_dependency_p=*/true,
9564 /*is_declaration=*/true);
9566 return cp_parser_namespace_name (parser);
9569 /* Parse a using-declaration.
9572 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
9573 using :: unqualified-id ; */
9576 cp_parser_using_declaration (cp_parser* parser)
9579 bool typename_p = false;
9580 bool global_scope_p;
9586 /* Look for the `using' keyword. */
9587 cp_parser_require_keyword (parser, RID_USING, "`using'");
9589 /* Peek at the next token. */
9590 token = cp_lexer_peek_token (parser->lexer);
9591 /* See if it's `typename'. */
9592 if (token->keyword == RID_TYPENAME)
9594 /* Remember that we've seen it. */
9596 /* Consume the `typename' token. */
9597 cp_lexer_consume_token (parser->lexer);
9600 /* Look for the optional global scope qualification. */
9602 = (cp_parser_global_scope_opt (parser,
9603 /*current_scope_valid_p=*/false)
9606 /* If we saw `typename', or didn't see `::', then there must be a
9607 nested-name-specifier present. */
9608 if (typename_p || !global_scope_p)
9609 qscope = cp_parser_nested_name_specifier (parser, typename_p,
9610 /*check_dependency_p=*/true,
9612 /*is_declaration=*/true);
9613 /* Otherwise, we could be in either of the two productions. In that
9614 case, treat the nested-name-specifier as optional. */
9616 qscope = cp_parser_nested_name_specifier_opt (parser,
9617 /*typename_keyword_p=*/false,
9618 /*check_dependency_p=*/true,
9620 /*is_declaration=*/true);
9622 qscope = global_namespace;
9624 /* Parse the unqualified-id. */
9625 identifier = cp_parser_unqualified_id (parser,
9626 /*template_keyword_p=*/false,
9627 /*check_dependency_p=*/true,
9628 /*declarator_p=*/true);
9630 /* The function we call to handle a using-declaration is different
9631 depending on what scope we are in. */
9632 if (identifier == error_mark_node)
9634 else if (TREE_CODE (identifier) != IDENTIFIER_NODE
9635 && TREE_CODE (identifier) != BIT_NOT_EXPR)
9636 /* [namespace.udecl]
9638 A using declaration shall not name a template-id. */
9639 error ("a template-id may not appear in a using-declaration");
9642 scope = current_scope ();
9643 if (scope && TYPE_P (scope))
9645 /* Create the USING_DECL. */
9646 decl = do_class_using_decl (build_nt (SCOPE_REF,
9649 /* Add it to the list of members in this class. */
9650 finish_member_declaration (decl);
9654 decl = cp_parser_lookup_name_simple (parser, identifier);
9655 if (decl == error_mark_node)
9656 cp_parser_name_lookup_error (parser, identifier, decl, NULL);
9658 do_local_using_decl (decl, qscope, identifier);
9660 do_toplevel_using_decl (decl, qscope, identifier);
9664 /* Look for the final `;'. */
9665 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
9668 /* Parse a using-directive.
9671 using namespace :: [opt] nested-name-specifier [opt]
9675 cp_parser_using_directive (cp_parser* parser)
9677 tree namespace_decl;
9680 /* Look for the `using' keyword. */
9681 cp_parser_require_keyword (parser, RID_USING, "`using'");
9682 /* And the `namespace' keyword. */
9683 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
9684 /* Look for the optional `::' operator. */
9685 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
9686 /* And the optional nested-name-specifier. */
9687 cp_parser_nested_name_specifier_opt (parser,
9688 /*typename_keyword_p=*/false,
9689 /*check_dependency_p=*/true,
9691 /*is_declaration=*/true);
9692 /* Get the namespace being used. */
9693 namespace_decl = cp_parser_namespace_name (parser);
9694 /* And any specified attributes. */
9695 attribs = cp_parser_attributes_opt (parser);
9696 /* Update the symbol table. */
9697 parse_using_directive (namespace_decl, attribs);
9698 /* Look for the final `;'. */
9699 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
9702 /* Parse an asm-definition.
9705 asm ( string-literal ) ;
9710 asm volatile [opt] ( string-literal ) ;
9711 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
9712 asm volatile [opt] ( string-literal : asm-operand-list [opt]
9713 : asm-operand-list [opt] ) ;
9714 asm volatile [opt] ( string-literal : asm-operand-list [opt]
9715 : asm-operand-list [opt]
9716 : asm-operand-list [opt] ) ; */
9719 cp_parser_asm_definition (cp_parser* parser)
9723 tree outputs = NULL_TREE;
9724 tree inputs = NULL_TREE;
9725 tree clobbers = NULL_TREE;
9727 bool volatile_p = false;
9728 bool extended_p = false;
9730 /* Look for the `asm' keyword. */
9731 cp_parser_require_keyword (parser, RID_ASM, "`asm'");
9732 /* See if the next token is `volatile'. */
9733 if (cp_parser_allow_gnu_extensions_p (parser)
9734 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
9736 /* Remember that we saw the `volatile' keyword. */
9738 /* Consume the token. */
9739 cp_lexer_consume_token (parser->lexer);
9741 /* Look for the opening `('. */
9742 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
9743 /* Look for the string. */
9744 token = cp_parser_require (parser, CPP_STRING, "asm body");
9747 string = token->value;
9748 /* If we're allowing GNU extensions, check for the extended assembly
9749 syntax. Unfortunately, the `:' tokens need not be separated by
9750 a space in C, and so, for compatibility, we tolerate that here
9751 too. Doing that means that we have to treat the `::' operator as
9753 if (cp_parser_allow_gnu_extensions_p (parser)
9754 && at_function_scope_p ()
9755 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
9756 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
9758 bool inputs_p = false;
9759 bool clobbers_p = false;
9761 /* The extended syntax was used. */
9764 /* Look for outputs. */
9765 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
9767 /* Consume the `:'. */
9768 cp_lexer_consume_token (parser->lexer);
9769 /* Parse the output-operands. */
9770 if (cp_lexer_next_token_is_not (parser->lexer,
9772 && cp_lexer_next_token_is_not (parser->lexer,
9774 && cp_lexer_next_token_is_not (parser->lexer,
9776 outputs = cp_parser_asm_operand_list (parser);
9778 /* If the next token is `::', there are no outputs, and the
9779 next token is the beginning of the inputs. */
9780 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
9782 /* Consume the `::' token. */
9783 cp_lexer_consume_token (parser->lexer);
9784 /* The inputs are coming next. */
9788 /* Look for inputs. */
9790 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
9793 /* Consume the `:'. */
9794 cp_lexer_consume_token (parser->lexer);
9795 /* Parse the output-operands. */
9796 if (cp_lexer_next_token_is_not (parser->lexer,
9798 && cp_lexer_next_token_is_not (parser->lexer,
9800 && cp_lexer_next_token_is_not (parser->lexer,
9802 inputs = cp_parser_asm_operand_list (parser);
9804 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
9805 /* The clobbers are coming next. */
9808 /* Look for clobbers. */
9810 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
9813 /* Consume the `:'. */
9814 cp_lexer_consume_token (parser->lexer);
9815 /* Parse the clobbers. */
9816 if (cp_lexer_next_token_is_not (parser->lexer,
9818 clobbers = cp_parser_asm_clobber_list (parser);
9821 /* Look for the closing `)'. */
9822 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
9823 cp_parser_skip_to_closing_parenthesis (parser, true, false,
9824 /*consume_paren=*/true);
9825 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
9827 /* Create the ASM_STMT. */
9828 if (at_function_scope_p ())
9831 finish_asm_stmt (volatile_p
9832 ? ridpointers[(int) RID_VOLATILE] : NULL_TREE,
9833 string, outputs, inputs, clobbers);
9834 /* If the extended syntax was not used, mark the ASM_STMT. */
9836 ASM_INPUT_P (asm_stmt) = 1;
9839 assemble_asm (string);
9842 /* Declarators [gram.dcl.decl] */
9844 /* Parse an init-declarator.
9847 declarator initializer [opt]
9852 declarator asm-specification [opt] attributes [opt] initializer [opt]
9854 function-definition:
9855 decl-specifier-seq [opt] declarator ctor-initializer [opt]
9857 decl-specifier-seq [opt] declarator function-try-block
9861 function-definition:
9862 __extension__ function-definition
9864 The DECL_SPECIFIERS and PREFIX_ATTRIBUTES apply to this declarator.
9865 Returns a representation of the entity declared. If MEMBER_P is TRUE,
9866 then this declarator appears in a class scope. The new DECL created
9867 by this declarator is returned.
9869 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
9870 for a function-definition here as well. If the declarator is a
9871 declarator for a function-definition, *FUNCTION_DEFINITION_P will
9872 be TRUE upon return. By that point, the function-definition will
9873 have been completely parsed.
9875 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
9879 cp_parser_init_declarator (cp_parser* parser,
9880 tree decl_specifiers,
9881 tree prefix_attributes,
9882 bool function_definition_allowed_p,
9884 int declares_class_or_enum,
9885 bool* function_definition_p)
9890 tree asm_specification;
9892 tree decl = NULL_TREE;
9894 bool is_initialized;
9895 bool is_parenthesized_init;
9896 bool is_non_constant_init;
9897 int ctor_dtor_or_conv_p;
9900 /* Assume that this is not the declarator for a function
9902 if (function_definition_p)
9903 *function_definition_p = false;
9905 /* Defer access checks while parsing the declarator; we cannot know
9906 what names are accessible until we know what is being
9908 resume_deferring_access_checks ();
9910 /* Parse the declarator. */
9912 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
9913 &ctor_dtor_or_conv_p,
9914 /*parenthesized_p=*/NULL);
9915 /* Gather up the deferred checks. */
9916 stop_deferring_access_checks ();
9918 /* If the DECLARATOR was erroneous, there's no need to go
9920 if (declarator == error_mark_node)
9921 return error_mark_node;
9923 cp_parser_check_for_definition_in_return_type (declarator,
9924 declares_class_or_enum);
9926 /* Figure out what scope the entity declared by the DECLARATOR is
9927 located in. `grokdeclarator' sometimes changes the scope, so
9928 we compute it now. */
9929 scope = get_scope_of_declarator (declarator);
9931 /* If we're allowing GNU extensions, look for an asm-specification
9933 if (cp_parser_allow_gnu_extensions_p (parser))
9935 /* Look for an asm-specification. */
9936 asm_specification = cp_parser_asm_specification_opt (parser);
9937 /* And attributes. */
9938 attributes = cp_parser_attributes_opt (parser);
9942 asm_specification = NULL_TREE;
9943 attributes = NULL_TREE;
9946 /* Peek at the next token. */
9947 token = cp_lexer_peek_token (parser->lexer);
9948 /* Check to see if the token indicates the start of a
9949 function-definition. */
9950 if (cp_parser_token_starts_function_definition_p (token))
9952 if (!function_definition_allowed_p)
9954 /* If a function-definition should not appear here, issue an
9956 cp_parser_error (parser,
9957 "a function-definition is not allowed here");
9958 return error_mark_node;
9962 /* Neither attributes nor an asm-specification are allowed
9963 on a function-definition. */
9964 if (asm_specification)
9965 error ("an asm-specification is not allowed on a function-definition");
9967 error ("attributes are not allowed on a function-definition");
9968 /* This is a function-definition. */
9969 *function_definition_p = true;
9971 /* Parse the function definition. */
9973 decl = cp_parser_save_member_function_body (parser,
9979 = (cp_parser_function_definition_from_specifiers_and_declarator
9980 (parser, decl_specifiers, prefix_attributes, declarator));
9988 Only in function declarations for constructors, destructors, and
9989 type conversions can the decl-specifier-seq be omitted.
9991 We explicitly postpone this check past the point where we handle
9992 function-definitions because we tolerate function-definitions
9993 that are missing their return types in some modes. */
9994 if (!decl_specifiers && ctor_dtor_or_conv_p <= 0)
9996 cp_parser_error (parser,
9997 "expected constructor, destructor, or type conversion");
9998 return error_mark_node;
10001 /* An `=' or an `(' indicates an initializer. */
10002 is_initialized = (token->type == CPP_EQ
10003 || token->type == CPP_OPEN_PAREN);
10004 /* If the init-declarator isn't initialized and isn't followed by a
10005 `,' or `;', it's not a valid init-declarator. */
10006 if (!is_initialized
10007 && token->type != CPP_COMMA
10008 && token->type != CPP_SEMICOLON)
10010 cp_parser_error (parser, "expected init-declarator");
10011 return error_mark_node;
10014 /* Because start_decl has side-effects, we should only call it if we
10015 know we're going ahead. By this point, we know that we cannot
10016 possibly be looking at any other construct. */
10017 cp_parser_commit_to_tentative_parse (parser);
10019 /* If the decl specifiers were bad, issue an error now that we're
10020 sure this was intended to be a declarator. Then continue
10021 declaring the variable(s), as int, to try to cut down on further
10023 if (decl_specifiers != NULL
10024 && TREE_VALUE (decl_specifiers) == error_mark_node)
10026 cp_parser_error (parser, "invalid type in declaration");
10027 TREE_VALUE (decl_specifiers) = integer_type_node;
10030 /* Check to see whether or not this declaration is a friend. */
10031 friend_p = cp_parser_friend_p (decl_specifiers);
10033 /* Check that the number of template-parameter-lists is OK. */
10034 if (!cp_parser_check_declarator_template_parameters (parser, declarator))
10035 return error_mark_node;
10037 /* Enter the newly declared entry in the symbol table. If we're
10038 processing a declaration in a class-specifier, we wait until
10039 after processing the initializer. */
10042 if (parser->in_unbraced_linkage_specification_p)
10044 decl_specifiers = tree_cons (error_mark_node,
10045 get_identifier ("extern"),
10047 have_extern_spec = false;
10049 decl = start_decl (declarator, decl_specifiers,
10050 is_initialized, attributes, prefix_attributes);
10053 /* Enter the SCOPE. That way unqualified names appearing in the
10054 initializer will be looked up in SCOPE. */
10056 push_scope (scope);
10058 /* Perform deferred access control checks, now that we know in which
10059 SCOPE the declared entity resides. */
10060 if (!member_p && decl)
10062 tree saved_current_function_decl = NULL_TREE;
10064 /* If the entity being declared is a function, pretend that we
10065 are in its scope. If it is a `friend', it may have access to
10066 things that would not otherwise be accessible. */
10067 if (TREE_CODE (decl) == FUNCTION_DECL)
10069 saved_current_function_decl = current_function_decl;
10070 current_function_decl = decl;
10073 /* Perform the access control checks for the declarator and the
10074 the decl-specifiers. */
10075 perform_deferred_access_checks ();
10077 /* Restore the saved value. */
10078 if (TREE_CODE (decl) == FUNCTION_DECL)
10079 current_function_decl = saved_current_function_decl;
10082 /* Parse the initializer. */
10083 if (is_initialized)
10084 initializer = cp_parser_initializer (parser,
10085 &is_parenthesized_init,
10086 &is_non_constant_init);
10089 initializer = NULL_TREE;
10090 is_parenthesized_init = false;
10091 is_non_constant_init = true;
10094 /* The old parser allows attributes to appear after a parenthesized
10095 initializer. Mark Mitchell proposed removing this functionality
10096 on the GCC mailing lists on 2002-08-13. This parser accepts the
10097 attributes -- but ignores them. */
10098 if (cp_parser_allow_gnu_extensions_p (parser) && is_parenthesized_init)
10099 if (cp_parser_attributes_opt (parser))
10100 warning ("attributes after parenthesized initializer ignored");
10102 /* Leave the SCOPE, now that we have processed the initializer. It
10103 is important to do this before calling cp_finish_decl because it
10104 makes decisions about whether to create DECL_STMTs or not based
10105 on the current scope. */
10109 /* For an in-class declaration, use `grokfield' to create the
10113 decl = grokfield (declarator, decl_specifiers,
10114 initializer, /*asmspec=*/NULL_TREE,
10115 /*attributes=*/NULL_TREE);
10116 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
10117 cp_parser_save_default_args (parser, decl);
10120 /* Finish processing the declaration. But, skip friend
10122 if (!friend_p && decl)
10123 cp_finish_decl (decl,
10126 /* If the initializer is in parentheses, then this is
10127 a direct-initialization, which means that an
10128 `explicit' constructor is OK. Otherwise, an
10129 `explicit' constructor cannot be used. */
10130 ((is_parenthesized_init || !is_initialized)
10131 ? 0 : LOOKUP_ONLYCONVERTING));
10133 /* Remember whether or not variables were initialized by
10134 constant-expressions. */
10135 if (decl && TREE_CODE (decl) == VAR_DECL
10136 && is_initialized && !is_non_constant_init)
10137 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = true;
10142 /* Parse a declarator.
10146 ptr-operator declarator
10148 abstract-declarator:
10149 ptr-operator abstract-declarator [opt]
10150 direct-abstract-declarator
10155 attributes [opt] direct-declarator
10156 attributes [opt] ptr-operator declarator
10158 abstract-declarator:
10159 attributes [opt] ptr-operator abstract-declarator [opt]
10160 attributes [opt] direct-abstract-declarator
10162 Returns a representation of the declarator. If the declarator has
10163 the form `* declarator', then an INDIRECT_REF is returned, whose
10164 only operand is the sub-declarator. Analogously, `& declarator' is
10165 represented as an ADDR_EXPR. For `X::* declarator', a SCOPE_REF is
10166 used. The first operand is the TYPE for `X'. The second operand
10167 is an INDIRECT_REF whose operand is the sub-declarator.
10169 Otherwise, the representation is as for a direct-declarator.
10171 (It would be better to define a structure type to represent
10172 declarators, rather than abusing `tree' nodes to represent
10173 declarators. That would be much clearer and save some memory.
10174 There is no reason for declarators to be garbage-collected, for
10175 example; they are created during parser and no longer needed after
10176 `grokdeclarator' has been called.)
10178 For a ptr-operator that has the optional cv-qualifier-seq,
10179 cv-qualifiers will be stored in the TREE_TYPE of the INDIRECT_REF
10182 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
10183 detect constructor, destructor or conversion operators. It is set
10184 to -1 if the declarator is a name, and +1 if it is a
10185 function. Otherwise it is set to zero. Usually you just want to
10186 test for >0, but internally the negative value is used.
10188 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
10189 a decl-specifier-seq unless it declares a constructor, destructor,
10190 or conversion. It might seem that we could check this condition in
10191 semantic analysis, rather than parsing, but that makes it difficult
10192 to handle something like `f()'. We want to notice that there are
10193 no decl-specifiers, and therefore realize that this is an
10194 expression, not a declaration.)
10196 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
10197 the declarator is a direct-declarator of the form "(...)". */
10200 cp_parser_declarator (cp_parser* parser,
10201 cp_parser_declarator_kind dcl_kind,
10202 int* ctor_dtor_or_conv_p,
10203 bool* parenthesized_p)
10207 enum tree_code code;
10208 tree cv_qualifier_seq;
10210 tree attributes = NULL_TREE;
10212 /* Assume this is not a constructor, destructor, or type-conversion
10214 if (ctor_dtor_or_conv_p)
10215 *ctor_dtor_or_conv_p = 0;
10217 if (cp_parser_allow_gnu_extensions_p (parser))
10218 attributes = cp_parser_attributes_opt (parser);
10220 /* Peek at the next token. */
10221 token = cp_lexer_peek_token (parser->lexer);
10223 /* Check for the ptr-operator production. */
10224 cp_parser_parse_tentatively (parser);
10225 /* Parse the ptr-operator. */
10226 code = cp_parser_ptr_operator (parser,
10228 &cv_qualifier_seq);
10229 /* If that worked, then we have a ptr-operator. */
10230 if (cp_parser_parse_definitely (parser))
10232 /* If a ptr-operator was found, then this declarator was not
10234 if (parenthesized_p)
10235 *parenthesized_p = true;
10236 /* The dependent declarator is optional if we are parsing an
10237 abstract-declarator. */
10238 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
10239 cp_parser_parse_tentatively (parser);
10241 /* Parse the dependent declarator. */
10242 declarator = cp_parser_declarator (parser, dcl_kind,
10243 /*ctor_dtor_or_conv_p=*/NULL,
10244 /*parenthesized_p=*/NULL);
10246 /* If we are parsing an abstract-declarator, we must handle the
10247 case where the dependent declarator is absent. */
10248 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
10249 && !cp_parser_parse_definitely (parser))
10250 declarator = NULL_TREE;
10252 /* Build the representation of the ptr-operator. */
10253 if (code == INDIRECT_REF)
10254 declarator = make_pointer_declarator (cv_qualifier_seq,
10257 declarator = make_reference_declarator (cv_qualifier_seq,
10259 /* Handle the pointer-to-member case. */
10261 declarator = build_nt (SCOPE_REF, class_type, declarator);
10263 /* Everything else is a direct-declarator. */
10266 if (parenthesized_p)
10267 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
10269 declarator = cp_parser_direct_declarator (parser, dcl_kind,
10270 ctor_dtor_or_conv_p);
10273 if (attributes && declarator != error_mark_node)
10274 declarator = tree_cons (attributes, declarator, NULL_TREE);
10279 /* Parse a direct-declarator or direct-abstract-declarator.
10283 direct-declarator ( parameter-declaration-clause )
10284 cv-qualifier-seq [opt]
10285 exception-specification [opt]
10286 direct-declarator [ constant-expression [opt] ]
10289 direct-abstract-declarator:
10290 direct-abstract-declarator [opt]
10291 ( parameter-declaration-clause )
10292 cv-qualifier-seq [opt]
10293 exception-specification [opt]
10294 direct-abstract-declarator [opt] [ constant-expression [opt] ]
10295 ( abstract-declarator )
10297 Returns a representation of the declarator. DCL_KIND is
10298 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
10299 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
10300 we are parsing a direct-declarator. It is
10301 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
10302 of ambiguity we prefer an abstract declarator, as per
10303 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P is as for
10304 cp_parser_declarator.
10306 For the declarator-id production, the representation is as for an
10307 id-expression, except that a qualified name is represented as a
10308 SCOPE_REF. A function-declarator is represented as a CALL_EXPR;
10309 see the documentation of the FUNCTION_DECLARATOR_* macros for
10310 information about how to find the various declarator components.
10311 An array-declarator is represented as an ARRAY_REF. The
10312 direct-declarator is the first operand; the constant-expression
10313 indicating the size of the array is the second operand. */
10316 cp_parser_direct_declarator (cp_parser* parser,
10317 cp_parser_declarator_kind dcl_kind,
10318 int* ctor_dtor_or_conv_p)
10321 tree declarator = NULL_TREE;
10322 tree scope = NULL_TREE;
10323 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
10324 bool saved_in_declarator_p = parser->in_declarator_p;
10329 /* Peek at the next token. */
10330 token = cp_lexer_peek_token (parser->lexer);
10331 if (token->type == CPP_OPEN_PAREN)
10333 /* This is either a parameter-declaration-clause, or a
10334 parenthesized declarator. When we know we are parsing a
10335 named declarator, it must be a parenthesized declarator
10336 if FIRST is true. For instance, `(int)' is a
10337 parameter-declaration-clause, with an omitted
10338 direct-abstract-declarator. But `((*))', is a
10339 parenthesized abstract declarator. Finally, when T is a
10340 template parameter `(T)' is a
10341 parameter-declaration-clause, and not a parenthesized
10344 We first try and parse a parameter-declaration-clause,
10345 and then try a nested declarator (if FIRST is true).
10347 It is not an error for it not to be a
10348 parameter-declaration-clause, even when FIRST is
10354 The first is the declaration of a function while the
10355 second is a the definition of a variable, including its
10358 Having seen only the parenthesis, we cannot know which of
10359 these two alternatives should be selected. Even more
10360 complex are examples like:
10365 The former is a function-declaration; the latter is a
10366 variable initialization.
10368 Thus again, we try a parameter-declaration-clause, and if
10369 that fails, we back out and return. */
10371 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
10374 unsigned saved_num_template_parameter_lists;
10376 cp_parser_parse_tentatively (parser);
10378 /* Consume the `('. */
10379 cp_lexer_consume_token (parser->lexer);
10382 /* If this is going to be an abstract declarator, we're
10383 in a declarator and we can't have default args. */
10384 parser->default_arg_ok_p = false;
10385 parser->in_declarator_p = true;
10388 /* Inside the function parameter list, surrounding
10389 template-parameter-lists do not apply. */
10390 saved_num_template_parameter_lists
10391 = parser->num_template_parameter_lists;
10392 parser->num_template_parameter_lists = 0;
10394 /* Parse the parameter-declaration-clause. */
10395 params = cp_parser_parameter_declaration_clause (parser);
10397 parser->num_template_parameter_lists
10398 = saved_num_template_parameter_lists;
10400 /* If all went well, parse the cv-qualifier-seq and the
10401 exception-specification. */
10402 if (cp_parser_parse_definitely (parser))
10404 tree cv_qualifiers;
10405 tree exception_specification;
10407 if (ctor_dtor_or_conv_p)
10408 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
10410 /* Consume the `)'. */
10411 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
10413 /* Parse the cv-qualifier-seq. */
10414 cv_qualifiers = cp_parser_cv_qualifier_seq_opt (parser);
10415 /* And the exception-specification. */
10416 exception_specification
10417 = cp_parser_exception_specification_opt (parser);
10419 /* Create the function-declarator. */
10420 declarator = make_call_declarator (declarator,
10423 exception_specification);
10424 /* Any subsequent parameter lists are to do with
10425 return type, so are not those of the declared
10427 parser->default_arg_ok_p = false;
10429 /* Repeat the main loop. */
10434 /* If this is the first, we can try a parenthesized
10438 bool saved_in_type_id_in_expr_p;
10440 parser->default_arg_ok_p = saved_default_arg_ok_p;
10441 parser->in_declarator_p = saved_in_declarator_p;
10443 /* Consume the `('. */
10444 cp_lexer_consume_token (parser->lexer);
10445 /* Parse the nested declarator. */
10446 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
10447 parser->in_type_id_in_expr_p = true;
10449 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
10450 /*parenthesized_p=*/NULL);
10451 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
10453 /* Expect a `)'. */
10454 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
10455 declarator = error_mark_node;
10456 if (declarator == error_mark_node)
10459 goto handle_declarator;
10461 /* Otherwise, we must be done. */
10465 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
10466 && token->type == CPP_OPEN_SQUARE)
10468 /* Parse an array-declarator. */
10471 if (ctor_dtor_or_conv_p)
10472 *ctor_dtor_or_conv_p = 0;
10475 parser->default_arg_ok_p = false;
10476 parser->in_declarator_p = true;
10477 /* Consume the `['. */
10478 cp_lexer_consume_token (parser->lexer);
10479 /* Peek at the next token. */
10480 token = cp_lexer_peek_token (parser->lexer);
10481 /* If the next token is `]', then there is no
10482 constant-expression. */
10483 if (token->type != CPP_CLOSE_SQUARE)
10485 bool non_constant_p;
10488 = cp_parser_constant_expression (parser,
10489 /*allow_non_constant=*/true,
10491 if (!non_constant_p)
10492 bounds = fold_non_dependent_expr (bounds);
10495 bounds = NULL_TREE;
10496 /* Look for the closing `]'. */
10497 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'"))
10499 declarator = error_mark_node;
10503 declarator = build_nt (ARRAY_REF, declarator, bounds);
10505 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
10507 /* Parse a declarator-id */
10508 if (dcl_kind == CP_PARSER_DECLARATOR_EITHER)
10509 cp_parser_parse_tentatively (parser);
10510 declarator = cp_parser_declarator_id (parser);
10511 if (dcl_kind == CP_PARSER_DECLARATOR_EITHER)
10513 if (!cp_parser_parse_definitely (parser))
10514 declarator = error_mark_node;
10515 else if (TREE_CODE (declarator) != IDENTIFIER_NODE)
10517 cp_parser_error (parser, "expected unqualified-id");
10518 declarator = error_mark_node;
10522 if (declarator == error_mark_node)
10525 if (TREE_CODE (declarator) == SCOPE_REF
10526 && !current_scope ())
10528 tree scope = TREE_OPERAND (declarator, 0);
10530 /* In the declaration of a member of a template class
10531 outside of the class itself, the SCOPE will sometimes
10532 be a TYPENAME_TYPE. For example, given:
10534 template <typename T>
10535 int S<T>::R::i = 3;
10537 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
10538 this context, we must resolve S<T>::R to an ordinary
10539 type, rather than a typename type.
10541 The reason we normally avoid resolving TYPENAME_TYPEs
10542 is that a specialization of `S' might render
10543 `S<T>::R' not a type. However, if `S' is
10544 specialized, then this `i' will not be used, so there
10545 is no harm in resolving the types here. */
10546 if (TREE_CODE (scope) == TYPENAME_TYPE)
10550 /* Resolve the TYPENAME_TYPE. */
10551 type = resolve_typename_type (scope,
10552 /*only_current_p=*/false);
10553 /* If that failed, the declarator is invalid. */
10554 if (type != error_mark_node)
10556 /* Build a new DECLARATOR. */
10557 declarator = build_nt (SCOPE_REF,
10559 TREE_OPERAND (declarator, 1));
10563 /* Check to see whether the declarator-id names a constructor,
10564 destructor, or conversion. */
10565 if (declarator && ctor_dtor_or_conv_p
10566 && ((TREE_CODE (declarator) == SCOPE_REF
10567 && CLASS_TYPE_P (TREE_OPERAND (declarator, 0)))
10568 || (TREE_CODE (declarator) != SCOPE_REF
10569 && at_class_scope_p ())))
10571 tree unqualified_name;
10574 /* Get the unqualified part of the name. */
10575 if (TREE_CODE (declarator) == SCOPE_REF)
10577 class_type = TREE_OPERAND (declarator, 0);
10578 unqualified_name = TREE_OPERAND (declarator, 1);
10582 class_type = current_class_type;
10583 unqualified_name = declarator;
10586 /* See if it names ctor, dtor or conv. */
10587 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR
10588 || IDENTIFIER_TYPENAME_P (unqualified_name)
10589 || constructor_name_p (unqualified_name, class_type))
10590 *ctor_dtor_or_conv_p = -1;
10593 handle_declarator:;
10594 scope = get_scope_of_declarator (declarator);
10596 /* Any names that appear after the declarator-id for a member
10597 are looked up in the containing scope. */
10598 push_scope (scope);
10599 parser->in_declarator_p = true;
10600 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
10602 && (TREE_CODE (declarator) == SCOPE_REF
10603 || TREE_CODE (declarator) == IDENTIFIER_NODE)))
10604 /* Default args are only allowed on function
10606 parser->default_arg_ok_p = saved_default_arg_ok_p;
10608 parser->default_arg_ok_p = false;
10617 /* For an abstract declarator, we might wind up with nothing at this
10618 point. That's an error; the declarator is not optional. */
10620 cp_parser_error (parser, "expected declarator");
10622 /* If we entered a scope, we must exit it now. */
10626 parser->default_arg_ok_p = saved_default_arg_ok_p;
10627 parser->in_declarator_p = saved_in_declarator_p;
10632 /* Parse a ptr-operator.
10635 * cv-qualifier-seq [opt]
10637 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
10642 & cv-qualifier-seq [opt]
10644 Returns INDIRECT_REF if a pointer, or pointer-to-member, was
10645 used. Returns ADDR_EXPR if a reference was used. In the
10646 case of a pointer-to-member, *TYPE is filled in with the
10647 TYPE containing the member. *CV_QUALIFIER_SEQ is filled in
10648 with the cv-qualifier-seq, or NULL_TREE, if there are no
10649 cv-qualifiers. Returns ERROR_MARK if an error occurred. */
10651 static enum tree_code
10652 cp_parser_ptr_operator (cp_parser* parser,
10654 tree* cv_qualifier_seq)
10656 enum tree_code code = ERROR_MARK;
10659 /* Assume that it's not a pointer-to-member. */
10661 /* And that there are no cv-qualifiers. */
10662 *cv_qualifier_seq = NULL_TREE;
10664 /* Peek at the next token. */
10665 token = cp_lexer_peek_token (parser->lexer);
10666 /* If it's a `*' or `&' we have a pointer or reference. */
10667 if (token->type == CPP_MULT || token->type == CPP_AND)
10669 /* Remember which ptr-operator we were processing. */
10670 code = (token->type == CPP_AND ? ADDR_EXPR : INDIRECT_REF);
10672 /* Consume the `*' or `&'. */
10673 cp_lexer_consume_token (parser->lexer);
10675 /* A `*' can be followed by a cv-qualifier-seq, and so can a
10676 `&', if we are allowing GNU extensions. (The only qualifier
10677 that can legally appear after `&' is `restrict', but that is
10678 enforced during semantic analysis. */
10679 if (code == INDIRECT_REF
10680 || cp_parser_allow_gnu_extensions_p (parser))
10681 *cv_qualifier_seq = cp_parser_cv_qualifier_seq_opt (parser);
10685 /* Try the pointer-to-member case. */
10686 cp_parser_parse_tentatively (parser);
10687 /* Look for the optional `::' operator. */
10688 cp_parser_global_scope_opt (parser,
10689 /*current_scope_valid_p=*/false);
10690 /* Look for the nested-name specifier. */
10691 cp_parser_nested_name_specifier (parser,
10692 /*typename_keyword_p=*/false,
10693 /*check_dependency_p=*/true,
10695 /*is_declaration=*/false);
10696 /* If we found it, and the next token is a `*', then we are
10697 indeed looking at a pointer-to-member operator. */
10698 if (!cp_parser_error_occurred (parser)
10699 && cp_parser_require (parser, CPP_MULT, "`*'"))
10701 /* The type of which the member is a member is given by the
10703 *type = parser->scope;
10704 /* The next name will not be qualified. */
10705 parser->scope = NULL_TREE;
10706 parser->qualifying_scope = NULL_TREE;
10707 parser->object_scope = NULL_TREE;
10708 /* Indicate that the `*' operator was used. */
10709 code = INDIRECT_REF;
10710 /* Look for the optional cv-qualifier-seq. */
10711 *cv_qualifier_seq = cp_parser_cv_qualifier_seq_opt (parser);
10713 /* If that didn't work we don't have a ptr-operator. */
10714 if (!cp_parser_parse_definitely (parser))
10715 cp_parser_error (parser, "expected ptr-operator");
10721 /* Parse an (optional) cv-qualifier-seq.
10724 cv-qualifier cv-qualifier-seq [opt]
10726 Returns a TREE_LIST. The TREE_VALUE of each node is the
10727 representation of a cv-qualifier. */
10730 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
10732 tree cv_qualifiers = NULL_TREE;
10738 /* Look for the next cv-qualifier. */
10739 cv_qualifier = cp_parser_cv_qualifier_opt (parser);
10740 /* If we didn't find one, we're done. */
10744 /* Add this cv-qualifier to the list. */
10746 = tree_cons (NULL_TREE, cv_qualifier, cv_qualifiers);
10749 /* We built up the list in reverse order. */
10750 return nreverse (cv_qualifiers);
10753 /* Parse an (optional) cv-qualifier.
10765 cp_parser_cv_qualifier_opt (cp_parser* parser)
10768 tree cv_qualifier = NULL_TREE;
10770 /* Peek at the next token. */
10771 token = cp_lexer_peek_token (parser->lexer);
10772 /* See if it's a cv-qualifier. */
10773 switch (token->keyword)
10778 /* Save the value of the token. */
10779 cv_qualifier = token->value;
10780 /* Consume the token. */
10781 cp_lexer_consume_token (parser->lexer);
10788 return cv_qualifier;
10791 /* Parse a declarator-id.
10795 :: [opt] nested-name-specifier [opt] type-name
10797 In the `id-expression' case, the value returned is as for
10798 cp_parser_id_expression if the id-expression was an unqualified-id.
10799 If the id-expression was a qualified-id, then a SCOPE_REF is
10800 returned. The first operand is the scope (either a NAMESPACE_DECL
10801 or TREE_TYPE), but the second is still just a representation of an
10805 cp_parser_declarator_id (cp_parser* parser)
10807 tree id_expression;
10809 /* The expression must be an id-expression. Assume that qualified
10810 names are the names of types so that:
10813 int S<T>::R::i = 3;
10815 will work; we must treat `S<T>::R' as the name of a type.
10816 Similarly, assume that qualified names are templates, where
10820 int S<T>::R<T>::i = 3;
10823 id_expression = cp_parser_id_expression (parser,
10824 /*template_keyword_p=*/false,
10825 /*check_dependency_p=*/false,
10826 /*template_p=*/NULL,
10827 /*declarator_p=*/true);
10828 /* If the name was qualified, create a SCOPE_REF to represent
10832 id_expression = build_nt (SCOPE_REF, parser->scope, id_expression);
10833 parser->scope = NULL_TREE;
10836 return id_expression;
10839 /* Parse a type-id.
10842 type-specifier-seq abstract-declarator [opt]
10844 Returns the TYPE specified. */
10847 cp_parser_type_id (cp_parser* parser)
10849 tree type_specifier_seq;
10850 tree abstract_declarator;
10852 /* Parse the type-specifier-seq. */
10854 = cp_parser_type_specifier_seq (parser);
10855 if (type_specifier_seq == error_mark_node)
10856 return error_mark_node;
10858 /* There might or might not be an abstract declarator. */
10859 cp_parser_parse_tentatively (parser);
10860 /* Look for the declarator. */
10861 abstract_declarator
10862 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
10863 /*parenthesized_p=*/NULL);
10864 /* Check to see if there really was a declarator. */
10865 if (!cp_parser_parse_definitely (parser))
10866 abstract_declarator = NULL_TREE;
10868 return groktypename (build_tree_list (type_specifier_seq,
10869 abstract_declarator));
10872 /* Parse a type-specifier-seq.
10874 type-specifier-seq:
10875 type-specifier type-specifier-seq [opt]
10879 type-specifier-seq:
10880 attributes type-specifier-seq [opt]
10882 Returns a TREE_LIST. Either the TREE_VALUE of each node is a
10883 type-specifier, or the TREE_PURPOSE is a list of attributes. */
10886 cp_parser_type_specifier_seq (cp_parser* parser)
10888 bool seen_type_specifier = false;
10889 tree type_specifier_seq = NULL_TREE;
10891 /* Parse the type-specifiers and attributes. */
10894 tree type_specifier;
10896 /* Check for attributes first. */
10897 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
10899 type_specifier_seq = tree_cons (cp_parser_attributes_opt (parser),
10901 type_specifier_seq);
10905 /* After the first type-specifier, others are optional. */
10906 if (seen_type_specifier)
10907 cp_parser_parse_tentatively (parser);
10908 /* Look for the type-specifier. */
10909 type_specifier = cp_parser_type_specifier (parser,
10910 CP_PARSER_FLAGS_NONE,
10911 /*is_friend=*/false,
10912 /*is_declaration=*/false,
10915 /* If the first type-specifier could not be found, this is not a
10916 type-specifier-seq at all. */
10917 if (!seen_type_specifier && type_specifier == error_mark_node)
10918 return error_mark_node;
10919 /* If subsequent type-specifiers could not be found, the
10920 type-specifier-seq is complete. */
10921 else if (seen_type_specifier && !cp_parser_parse_definitely (parser))
10924 /* Add the new type-specifier to the list. */
10926 = tree_cons (NULL_TREE, type_specifier, type_specifier_seq);
10927 seen_type_specifier = true;
10930 /* We built up the list in reverse order. */
10931 return nreverse (type_specifier_seq);
10934 /* Parse a parameter-declaration-clause.
10936 parameter-declaration-clause:
10937 parameter-declaration-list [opt] ... [opt]
10938 parameter-declaration-list , ...
10940 Returns a representation for the parameter declarations. Each node
10941 is a TREE_LIST. (See cp_parser_parameter_declaration for the exact
10942 representation.) If the parameter-declaration-clause ends with an
10943 ellipsis, PARMLIST_ELLIPSIS_P will hold of the first node in the
10944 list. A return value of NULL_TREE indicates a
10945 parameter-declaration-clause consisting only of an ellipsis. */
10948 cp_parser_parameter_declaration_clause (cp_parser* parser)
10954 /* Peek at the next token. */
10955 token = cp_lexer_peek_token (parser->lexer);
10956 /* Check for trivial parameter-declaration-clauses. */
10957 if (token->type == CPP_ELLIPSIS)
10959 /* Consume the `...' token. */
10960 cp_lexer_consume_token (parser->lexer);
10963 else if (token->type == CPP_CLOSE_PAREN)
10964 /* There are no parameters. */
10966 #ifndef NO_IMPLICIT_EXTERN_C
10967 if (in_system_header && current_class_type == NULL
10968 && current_lang_name == lang_name_c)
10972 return void_list_node;
10974 /* Check for `(void)', too, which is a special case. */
10975 else if (token->keyword == RID_VOID
10976 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
10977 == CPP_CLOSE_PAREN))
10979 /* Consume the `void' token. */
10980 cp_lexer_consume_token (parser->lexer);
10981 /* There are no parameters. */
10982 return void_list_node;
10985 /* Parse the parameter-declaration-list. */
10986 parameters = cp_parser_parameter_declaration_list (parser);
10987 /* If a parse error occurred while parsing the
10988 parameter-declaration-list, then the entire
10989 parameter-declaration-clause is erroneous. */
10990 if (parameters == error_mark_node)
10991 return error_mark_node;
10993 /* Peek at the next token. */
10994 token = cp_lexer_peek_token (parser->lexer);
10995 /* If it's a `,', the clause should terminate with an ellipsis. */
10996 if (token->type == CPP_COMMA)
10998 /* Consume the `,'. */
10999 cp_lexer_consume_token (parser->lexer);
11000 /* Expect an ellipsis. */
11002 = (cp_parser_require (parser, CPP_ELLIPSIS, "`...'") != NULL);
11004 /* It might also be `...' if the optional trailing `,' was
11006 else if (token->type == CPP_ELLIPSIS)
11008 /* Consume the `...' token. */
11009 cp_lexer_consume_token (parser->lexer);
11010 /* And remember that we saw it. */
11014 ellipsis_p = false;
11016 /* Finish the parameter list. */
11017 return finish_parmlist (parameters, ellipsis_p);
11020 /* Parse a parameter-declaration-list.
11022 parameter-declaration-list:
11023 parameter-declaration
11024 parameter-declaration-list , parameter-declaration
11026 Returns a representation of the parameter-declaration-list, as for
11027 cp_parser_parameter_declaration_clause. However, the
11028 `void_list_node' is never appended to the list. */
11031 cp_parser_parameter_declaration_list (cp_parser* parser)
11033 tree parameters = NULL_TREE;
11035 /* Look for more parameters. */
11039 bool parenthesized_p;
11040 /* Parse the parameter. */
11042 = cp_parser_parameter_declaration (parser,
11043 /*template_parm_p=*/false,
11046 /* If a parse error occurred parsing the parameter declaration,
11047 then the entire parameter-declaration-list is erroneous. */
11048 if (parameter == error_mark_node)
11050 parameters = error_mark_node;
11053 /* Add the new parameter to the list. */
11054 TREE_CHAIN (parameter) = parameters;
11055 parameters = parameter;
11057 /* Peek at the next token. */
11058 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
11059 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11060 /* The parameter-declaration-list is complete. */
11062 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
11066 /* Peek at the next token. */
11067 token = cp_lexer_peek_nth_token (parser->lexer, 2);
11068 /* If it's an ellipsis, then the list is complete. */
11069 if (token->type == CPP_ELLIPSIS)
11071 /* Otherwise, there must be more parameters. Consume the
11073 cp_lexer_consume_token (parser->lexer);
11074 /* When parsing something like:
11076 int i(float f, double d)
11078 we can tell after seeing the declaration for "f" that we
11079 are not looking at an initialization of a variable "i",
11080 but rather at the declaration of a function "i".
11082 Due to the fact that the parsing of template arguments
11083 (as specified to a template-id) requires backtracking we
11084 cannot use this technique when inside a template argument
11086 if (!parser->in_template_argument_list_p
11087 && cp_parser_parsing_tentatively (parser)
11088 && !cp_parser_committed_to_tentative_parse (parser)
11089 /* However, a parameter-declaration of the form
11090 "foat(f)" (which is a valid declaration of a
11091 parameter "f") can also be interpreted as an
11092 expression (the conversion of "f" to "float"). */
11093 && !parenthesized_p)
11094 cp_parser_commit_to_tentative_parse (parser);
11098 cp_parser_error (parser, "expected `,' or `...'");
11099 if (!cp_parser_parsing_tentatively (parser)
11100 || cp_parser_committed_to_tentative_parse (parser))
11101 cp_parser_skip_to_closing_parenthesis (parser,
11102 /*recovering=*/true,
11103 /*or_comma=*/false,
11104 /*consume_paren=*/false);
11109 /* We built up the list in reverse order; straighten it out now. */
11110 return nreverse (parameters);
11113 /* Parse a parameter declaration.
11115 parameter-declaration:
11116 decl-specifier-seq declarator
11117 decl-specifier-seq declarator = assignment-expression
11118 decl-specifier-seq abstract-declarator [opt]
11119 decl-specifier-seq abstract-declarator [opt] = assignment-expression
11121 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
11122 declares a template parameter. (In that case, a non-nested `>'
11123 token encountered during the parsing of the assignment-expression
11124 is not interpreted as a greater-than operator.)
11126 Returns a TREE_LIST representing the parameter-declaration. The
11127 TREE_PURPOSE is the default argument expression, or NULL_TREE if
11128 there is no default argument. The TREE_VALUE is a representation
11129 of the decl-specifier-seq and declarator. In particular, the
11130 TREE_VALUE will be a TREE_LIST whose TREE_PURPOSE represents the
11131 decl-specifier-seq and whose TREE_VALUE represents the declarator.
11132 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
11133 the declarator is of the form "(p)". */
11136 cp_parser_parameter_declaration (cp_parser *parser,
11137 bool template_parm_p,
11138 bool *parenthesized_p)
11140 int declares_class_or_enum;
11141 bool greater_than_is_operator_p;
11142 tree decl_specifiers;
11145 tree default_argument;
11148 const char *saved_message;
11150 /* In a template parameter, `>' is not an operator.
11154 When parsing a default template-argument for a non-type
11155 template-parameter, the first non-nested `>' is taken as the end
11156 of the template parameter-list rather than a greater-than
11158 greater_than_is_operator_p = !template_parm_p;
11160 /* Type definitions may not appear in parameter types. */
11161 saved_message = parser->type_definition_forbidden_message;
11162 parser->type_definition_forbidden_message
11163 = "types may not be defined in parameter types";
11165 /* Parse the declaration-specifiers. */
11167 = cp_parser_decl_specifier_seq (parser,
11168 CP_PARSER_FLAGS_NONE,
11170 &declares_class_or_enum);
11171 /* If an error occurred, there's no reason to attempt to parse the
11172 rest of the declaration. */
11173 if (cp_parser_error_occurred (parser))
11175 parser->type_definition_forbidden_message = saved_message;
11176 return error_mark_node;
11179 /* Peek at the next token. */
11180 token = cp_lexer_peek_token (parser->lexer);
11181 /* If the next token is a `)', `,', `=', `>', or `...', then there
11182 is no declarator. */
11183 if (token->type == CPP_CLOSE_PAREN
11184 || token->type == CPP_COMMA
11185 || token->type == CPP_EQ
11186 || token->type == CPP_ELLIPSIS
11187 || token->type == CPP_GREATER)
11189 declarator = NULL_TREE;
11190 if (parenthesized_p)
11191 *parenthesized_p = false;
11193 /* Otherwise, there should be a declarator. */
11196 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
11197 parser->default_arg_ok_p = false;
11199 /* After seeing a decl-specifier-seq, if the next token is not a
11200 "(", there is no possibility that the code is a valid
11201 expression. Therefore, if parsing tentatively, we commit at
11203 if (!parser->in_template_argument_list_p
11204 /* In an expression context, having seen:
11208 we cannot be sure whether we are looking at a
11209 function-type (taking a "char" as a parameter) or a cast
11210 of some object of type "char" to "int". */
11211 && !parser->in_type_id_in_expr_p
11212 && cp_parser_parsing_tentatively (parser)
11213 && !cp_parser_committed_to_tentative_parse (parser)
11214 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
11215 cp_parser_commit_to_tentative_parse (parser);
11216 /* Parse the declarator. */
11217 declarator = cp_parser_declarator (parser,
11218 CP_PARSER_DECLARATOR_EITHER,
11219 /*ctor_dtor_or_conv_p=*/NULL,
11221 parser->default_arg_ok_p = saved_default_arg_ok_p;
11222 /* After the declarator, allow more attributes. */
11223 attributes = chainon (attributes, cp_parser_attributes_opt (parser));
11226 /* The restriction on defining new types applies only to the type
11227 of the parameter, not to the default argument. */
11228 parser->type_definition_forbidden_message = saved_message;
11230 /* If the next token is `=', then process a default argument. */
11231 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11233 bool saved_greater_than_is_operator_p;
11234 /* Consume the `='. */
11235 cp_lexer_consume_token (parser->lexer);
11237 /* If we are defining a class, then the tokens that make up the
11238 default argument must be saved and processed later. */
11239 if (!template_parm_p && at_class_scope_p ()
11240 && TYPE_BEING_DEFINED (current_class_type))
11242 unsigned depth = 0;
11244 /* Create a DEFAULT_ARG to represented the unparsed default
11246 default_argument = make_node (DEFAULT_ARG);
11247 DEFARG_TOKENS (default_argument) = cp_token_cache_new ();
11249 /* Add tokens until we have processed the entire default
11256 /* Peek at the next token. */
11257 token = cp_lexer_peek_token (parser->lexer);
11258 /* What we do depends on what token we have. */
11259 switch (token->type)
11261 /* In valid code, a default argument must be
11262 immediately followed by a `,' `)', or `...'. */
11264 case CPP_CLOSE_PAREN:
11266 /* If we run into a non-nested `;', `}', or `]',
11267 then the code is invalid -- but the default
11268 argument is certainly over. */
11269 case CPP_SEMICOLON:
11270 case CPP_CLOSE_BRACE:
11271 case CPP_CLOSE_SQUARE:
11274 /* Update DEPTH, if necessary. */
11275 else if (token->type == CPP_CLOSE_PAREN
11276 || token->type == CPP_CLOSE_BRACE
11277 || token->type == CPP_CLOSE_SQUARE)
11281 case CPP_OPEN_PAREN:
11282 case CPP_OPEN_SQUARE:
11283 case CPP_OPEN_BRACE:
11288 /* If we see a non-nested `>', and `>' is not an
11289 operator, then it marks the end of the default
11291 if (!depth && !greater_than_is_operator_p)
11295 /* If we run out of tokens, issue an error message. */
11297 error ("file ends in default argument");
11303 /* In these cases, we should look for template-ids.
11304 For example, if the default argument is
11305 `X<int, double>()', we need to do name lookup to
11306 figure out whether or not `X' is a template; if
11307 so, the `,' does not end the default argument.
11309 That is not yet done. */
11316 /* If we've reached the end, stop. */
11320 /* Add the token to the token block. */
11321 token = cp_lexer_consume_token (parser->lexer);
11322 cp_token_cache_push_token (DEFARG_TOKENS (default_argument),
11326 /* Outside of a class definition, we can just parse the
11327 assignment-expression. */
11330 bool saved_local_variables_forbidden_p;
11332 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
11334 saved_greater_than_is_operator_p
11335 = parser->greater_than_is_operator_p;
11336 parser->greater_than_is_operator_p = greater_than_is_operator_p;
11337 /* Local variable names (and the `this' keyword) may not
11338 appear in a default argument. */
11339 saved_local_variables_forbidden_p
11340 = parser->local_variables_forbidden_p;
11341 parser->local_variables_forbidden_p = true;
11342 /* Parse the assignment-expression. */
11343 default_argument = cp_parser_assignment_expression (parser);
11344 /* Restore saved state. */
11345 parser->greater_than_is_operator_p
11346 = saved_greater_than_is_operator_p;
11347 parser->local_variables_forbidden_p
11348 = saved_local_variables_forbidden_p;
11350 if (!parser->default_arg_ok_p)
11352 if (!flag_pedantic_errors)
11353 warning ("deprecated use of default argument for parameter of non-function");
11356 error ("default arguments are only permitted for function parameters");
11357 default_argument = NULL_TREE;
11362 default_argument = NULL_TREE;
11364 /* Create the representation of the parameter. */
11366 decl_specifiers = tree_cons (attributes, NULL_TREE, decl_specifiers);
11367 parameter = build_tree_list (default_argument,
11368 build_tree_list (decl_specifiers,
11374 /* Parse a function-body.
11377 compound_statement */
11380 cp_parser_function_body (cp_parser *parser)
11382 cp_parser_compound_statement (parser, false);
11385 /* Parse a ctor-initializer-opt followed by a function-body. Return
11386 true if a ctor-initializer was present. */
11389 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
11392 bool ctor_initializer_p;
11394 /* Begin the function body. */
11395 body = begin_function_body ();
11396 /* Parse the optional ctor-initializer. */
11397 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
11398 /* Parse the function-body. */
11399 cp_parser_function_body (parser);
11400 /* Finish the function body. */
11401 finish_function_body (body);
11403 return ctor_initializer_p;
11406 /* Parse an initializer.
11409 = initializer-clause
11410 ( expression-list )
11412 Returns a expression representing the initializer. If no
11413 initializer is present, NULL_TREE is returned.
11415 *IS_PARENTHESIZED_INIT is set to TRUE if the `( expression-list )'
11416 production is used, and zero otherwise. *IS_PARENTHESIZED_INIT is
11417 set to FALSE if there is no initializer present. If there is an
11418 initializer, and it is not a constant-expression, *NON_CONSTANT_P
11419 is set to true; otherwise it is set to false. */
11422 cp_parser_initializer (cp_parser* parser, bool* is_parenthesized_init,
11423 bool* non_constant_p)
11428 /* Peek at the next token. */
11429 token = cp_lexer_peek_token (parser->lexer);
11431 /* Let our caller know whether or not this initializer was
11433 *is_parenthesized_init = (token->type == CPP_OPEN_PAREN);
11434 /* Assume that the initializer is constant. */
11435 *non_constant_p = false;
11437 if (token->type == CPP_EQ)
11439 /* Consume the `='. */
11440 cp_lexer_consume_token (parser->lexer);
11441 /* Parse the initializer-clause. */
11442 init = cp_parser_initializer_clause (parser, non_constant_p);
11444 else if (token->type == CPP_OPEN_PAREN)
11445 init = cp_parser_parenthesized_expression_list (parser, false,
11449 /* Anything else is an error. */
11450 cp_parser_error (parser, "expected initializer");
11451 init = error_mark_node;
11457 /* Parse an initializer-clause.
11459 initializer-clause:
11460 assignment-expression
11461 { initializer-list , [opt] }
11464 Returns an expression representing the initializer.
11466 If the `assignment-expression' production is used the value
11467 returned is simply a representation for the expression.
11469 Otherwise, a CONSTRUCTOR is returned. The CONSTRUCTOR_ELTS will be
11470 the elements of the initializer-list (or NULL_TREE, if the last
11471 production is used). The TREE_TYPE for the CONSTRUCTOR will be
11472 NULL_TREE. There is no way to detect whether or not the optional
11473 trailing `,' was provided. NON_CONSTANT_P is as for
11474 cp_parser_initializer. */
11477 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
11481 /* If it is not a `{', then we are looking at an
11482 assignment-expression. */
11483 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
11485 = cp_parser_constant_expression (parser,
11486 /*allow_non_constant_p=*/true,
11490 /* Consume the `{' token. */
11491 cp_lexer_consume_token (parser->lexer);
11492 /* Create a CONSTRUCTOR to represent the braced-initializer. */
11493 initializer = make_node (CONSTRUCTOR);
11494 /* Mark it with TREE_HAS_CONSTRUCTOR. This should not be
11495 necessary, but check_initializer depends upon it, for
11497 TREE_HAS_CONSTRUCTOR (initializer) = 1;
11498 /* If it's not a `}', then there is a non-trivial initializer. */
11499 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
11501 /* Parse the initializer list. */
11502 CONSTRUCTOR_ELTS (initializer)
11503 = cp_parser_initializer_list (parser, non_constant_p);
11504 /* A trailing `,' token is allowed. */
11505 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
11506 cp_lexer_consume_token (parser->lexer);
11508 /* Now, there should be a trailing `}'. */
11509 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
11512 return initializer;
11515 /* Parse an initializer-list.
11519 initializer-list , initializer-clause
11524 identifier : initializer-clause
11525 initializer-list, identifier : initializer-clause
11527 Returns a TREE_LIST. The TREE_VALUE of each node is an expression
11528 for the initializer. If the TREE_PURPOSE is non-NULL, it is the
11529 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
11530 as for cp_parser_initializer. */
11533 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
11535 tree initializers = NULL_TREE;
11537 /* Assume all of the expressions are constant. */
11538 *non_constant_p = false;
11540 /* Parse the rest of the list. */
11546 bool clause_non_constant_p;
11548 /* If the next token is an identifier and the following one is a
11549 colon, we are looking at the GNU designated-initializer
11551 if (cp_parser_allow_gnu_extensions_p (parser)
11552 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
11553 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
11555 /* Consume the identifier. */
11556 identifier = cp_lexer_consume_token (parser->lexer)->value;
11557 /* Consume the `:'. */
11558 cp_lexer_consume_token (parser->lexer);
11561 identifier = NULL_TREE;
11563 /* Parse the initializer. */
11564 initializer = cp_parser_initializer_clause (parser,
11565 &clause_non_constant_p);
11566 /* If any clause is non-constant, so is the entire initializer. */
11567 if (clause_non_constant_p)
11568 *non_constant_p = true;
11569 /* Add it to the list. */
11570 initializers = tree_cons (identifier, initializer, initializers);
11572 /* If the next token is not a comma, we have reached the end of
11574 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11577 /* Peek at the next token. */
11578 token = cp_lexer_peek_nth_token (parser->lexer, 2);
11579 /* If the next token is a `}', then we're still done. An
11580 initializer-clause can have a trailing `,' after the
11581 initializer-list and before the closing `}'. */
11582 if (token->type == CPP_CLOSE_BRACE)
11585 /* Consume the `,' token. */
11586 cp_lexer_consume_token (parser->lexer);
11589 /* The initializers were built up in reverse order, so we need to
11590 reverse them now. */
11591 return nreverse (initializers);
11594 /* Classes [gram.class] */
11596 /* Parse a class-name.
11602 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
11603 to indicate that names looked up in dependent types should be
11604 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
11605 keyword has been used to indicate that the name that appears next
11606 is a template. TYPE_P is true iff the next name should be treated
11607 as class-name, even if it is declared to be some other kind of name
11608 as well. If CHECK_DEPENDENCY_P is FALSE, names are looked up in
11609 dependent scopes. If CLASS_HEAD_P is TRUE, this class is the class
11610 being defined in a class-head.
11612 Returns the TYPE_DECL representing the class. */
11615 cp_parser_class_name (cp_parser *parser,
11616 bool typename_keyword_p,
11617 bool template_keyword_p,
11619 bool check_dependency_p,
11621 bool is_declaration)
11628 /* All class-names start with an identifier. */
11629 token = cp_lexer_peek_token (parser->lexer);
11630 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
11632 cp_parser_error (parser, "expected class-name");
11633 return error_mark_node;
11636 /* PARSER->SCOPE can be cleared when parsing the template-arguments
11637 to a template-id, so we save it here. */
11638 scope = parser->scope;
11639 if (scope == error_mark_node)
11640 return error_mark_node;
11642 /* Any name names a type if we're following the `typename' keyword
11643 in a qualified name where the enclosing scope is type-dependent. */
11644 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
11645 && dependent_type_p (scope));
11646 /* Handle the common case (an identifier, but not a template-id)
11648 if (token->type == CPP_NAME
11649 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
11653 /* Look for the identifier. */
11654 identifier = cp_parser_identifier (parser);
11655 /* If the next token isn't an identifier, we are certainly not
11656 looking at a class-name. */
11657 if (identifier == error_mark_node)
11658 decl = error_mark_node;
11659 /* If we know this is a type-name, there's no need to look it
11661 else if (typename_p)
11665 /* If the next token is a `::', then the name must be a type
11668 [basic.lookup.qual]
11670 During the lookup for a name preceding the :: scope
11671 resolution operator, object, function, and enumerator
11672 names are ignored. */
11673 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
11675 /* Look up the name. */
11676 decl = cp_parser_lookup_name (parser, identifier,
11678 /*is_template=*/false,
11679 /*is_namespace=*/false,
11680 check_dependency_p);
11685 /* Try a template-id. */
11686 decl = cp_parser_template_id (parser, template_keyword_p,
11687 check_dependency_p,
11689 if (decl == error_mark_node)
11690 return error_mark_node;
11693 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
11695 /* If this is a typename, create a TYPENAME_TYPE. */
11696 if (typename_p && decl != error_mark_node)
11698 decl = make_typename_type (scope, decl, /*complain=*/1);
11699 if (decl != error_mark_node)
11700 decl = TYPE_NAME (decl);
11703 /* Check to see that it is really the name of a class. */
11704 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
11705 && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
11706 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
11707 /* Situations like this:
11709 template <typename T> struct A {
11710 typename T::template X<int>::I i;
11713 are problematic. Is `T::template X<int>' a class-name? The
11714 standard does not seem to be definitive, but there is no other
11715 valid interpretation of the following `::'. Therefore, those
11716 names are considered class-names. */
11717 decl = TYPE_NAME (make_typename_type (scope, decl, tf_error));
11718 else if (decl == error_mark_node
11719 || TREE_CODE (decl) != TYPE_DECL
11720 || !IS_AGGR_TYPE (TREE_TYPE (decl)))
11722 cp_parser_error (parser, "expected class-name");
11723 return error_mark_node;
11729 /* Parse a class-specifier.
11732 class-head { member-specification [opt] }
11734 Returns the TREE_TYPE representing the class. */
11737 cp_parser_class_specifier (cp_parser* parser)
11741 tree attributes = NULL_TREE;
11742 int has_trailing_semicolon;
11743 bool nested_name_specifier_p;
11744 unsigned saved_num_template_parameter_lists;
11746 push_deferring_access_checks (dk_no_deferred);
11748 /* Parse the class-head. */
11749 type = cp_parser_class_head (parser,
11750 &nested_name_specifier_p);
11751 /* If the class-head was a semantic disaster, skip the entire body
11755 cp_parser_skip_to_end_of_block_or_statement (parser);
11756 pop_deferring_access_checks ();
11757 return error_mark_node;
11760 /* Look for the `{'. */
11761 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
11763 pop_deferring_access_checks ();
11764 return error_mark_node;
11767 /* Issue an error message if type-definitions are forbidden here. */
11768 cp_parser_check_type_definition (parser);
11769 /* Remember that we are defining one more class. */
11770 ++parser->num_classes_being_defined;
11771 /* Inside the class, surrounding template-parameter-lists do not
11773 saved_num_template_parameter_lists
11774 = parser->num_template_parameter_lists;
11775 parser->num_template_parameter_lists = 0;
11777 /* Start the class. */
11778 if (nested_name_specifier_p)
11779 push_scope (CP_DECL_CONTEXT (TYPE_MAIN_DECL (type)));
11780 type = begin_class_definition (type);
11781 if (type == error_mark_node)
11782 /* If the type is erroneous, skip the entire body of the class. */
11783 cp_parser_skip_to_closing_brace (parser);
11785 /* Parse the member-specification. */
11786 cp_parser_member_specification_opt (parser);
11787 /* Look for the trailing `}'. */
11788 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
11789 /* We get better error messages by noticing a common problem: a
11790 missing trailing `;'. */
11791 token = cp_lexer_peek_token (parser->lexer);
11792 has_trailing_semicolon = (token->type == CPP_SEMICOLON);
11793 /* Look for attributes to apply to this class. */
11794 if (cp_parser_allow_gnu_extensions_p (parser))
11795 attributes = cp_parser_attributes_opt (parser);
11796 /* If we got any attributes in class_head, xref_tag will stick them in
11797 TREE_TYPE of the type. Grab them now. */
11798 if (type != error_mark_node)
11800 attributes = chainon (TYPE_ATTRIBUTES (type), attributes);
11801 TYPE_ATTRIBUTES (type) = NULL_TREE;
11802 type = finish_struct (type, attributes);
11804 if (nested_name_specifier_p)
11805 pop_scope (CP_DECL_CONTEXT (TYPE_MAIN_DECL (type)));
11806 /* If this class is not itself within the scope of another class,
11807 then we need to parse the bodies of all of the queued function
11808 definitions. Note that the queued functions defined in a class
11809 are not always processed immediately following the
11810 class-specifier for that class. Consider:
11813 struct B { void f() { sizeof (A); } };
11816 If `f' were processed before the processing of `A' were
11817 completed, there would be no way to compute the size of `A'.
11818 Note that the nesting we are interested in here is lexical --
11819 not the semantic nesting given by TYPE_CONTEXT. In particular,
11822 struct A { struct B; };
11823 struct A::B { void f() { } };
11825 there is no need to delay the parsing of `A::B::f'. */
11826 if (--parser->num_classes_being_defined == 0)
11831 /* In a first pass, parse default arguments to the functions.
11832 Then, in a second pass, parse the bodies of the functions.
11833 This two-phased approach handles cases like:
11841 for (TREE_PURPOSE (parser->unparsed_functions_queues)
11842 = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
11843 (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
11844 TREE_PURPOSE (parser->unparsed_functions_queues)
11845 = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
11847 fn = TREE_VALUE (queue_entry);
11848 /* Make sure that any template parameters are in scope. */
11849 maybe_begin_member_template_processing (fn);
11850 /* If there are default arguments that have not yet been processed,
11851 take care of them now. */
11852 cp_parser_late_parsing_default_args (parser, fn);
11853 /* Remove any template parameters from the symbol table. */
11854 maybe_end_member_template_processing ();
11856 /* Now parse the body of the functions. */
11857 for (TREE_VALUE (parser->unparsed_functions_queues)
11858 = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
11859 (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
11860 TREE_VALUE (parser->unparsed_functions_queues)
11861 = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
11863 /* Figure out which function we need to process. */
11864 fn = TREE_VALUE (queue_entry);
11866 /* A hack to prevent garbage collection. */
11869 /* Parse the function. */
11870 cp_parser_late_parsing_for_member (parser, fn);
11876 /* Put back any saved access checks. */
11877 pop_deferring_access_checks ();
11879 /* Restore the count of active template-parameter-lists. */
11880 parser->num_template_parameter_lists
11881 = saved_num_template_parameter_lists;
11886 /* Parse a class-head.
11889 class-key identifier [opt] base-clause [opt]
11890 class-key nested-name-specifier identifier base-clause [opt]
11891 class-key nested-name-specifier [opt] template-id
11895 class-key attributes identifier [opt] base-clause [opt]
11896 class-key attributes nested-name-specifier identifier base-clause [opt]
11897 class-key attributes nested-name-specifier [opt] template-id
11900 Returns the TYPE of the indicated class. Sets
11901 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
11902 involving a nested-name-specifier was used, and FALSE otherwise.
11904 Returns NULL_TREE if the class-head is syntactically valid, but
11905 semantically invalid in a way that means we should skip the entire
11906 body of the class. */
11909 cp_parser_class_head (cp_parser* parser,
11910 bool* nested_name_specifier_p)
11913 tree nested_name_specifier;
11914 enum tag_types class_key;
11915 tree id = NULL_TREE;
11916 tree type = NULL_TREE;
11918 bool template_id_p = false;
11919 bool qualified_p = false;
11920 bool invalid_nested_name_p = false;
11921 bool invalid_explicit_specialization_p = false;
11922 unsigned num_templates;
11924 /* Assume no nested-name-specifier will be present. */
11925 *nested_name_specifier_p = false;
11926 /* Assume no template parameter lists will be used in defining the
11930 /* Look for the class-key. */
11931 class_key = cp_parser_class_key (parser);
11932 if (class_key == none_type)
11933 return error_mark_node;
11935 /* Parse the attributes. */
11936 attributes = cp_parser_attributes_opt (parser);
11938 /* If the next token is `::', that is invalid -- but sometimes
11939 people do try to write:
11943 Handle this gracefully by accepting the extra qualifier, and then
11944 issuing an error about it later if this really is a
11945 class-head. If it turns out just to be an elaborated type
11946 specifier, remain silent. */
11947 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
11948 qualified_p = true;
11950 push_deferring_access_checks (dk_no_check);
11952 /* Determine the name of the class. Begin by looking for an
11953 optional nested-name-specifier. */
11954 nested_name_specifier
11955 = cp_parser_nested_name_specifier_opt (parser,
11956 /*typename_keyword_p=*/false,
11957 /*check_dependency_p=*/false,
11959 /*is_declaration=*/false);
11960 /* If there was a nested-name-specifier, then there *must* be an
11962 if (nested_name_specifier)
11964 /* Although the grammar says `identifier', it really means
11965 `class-name' or `template-name'. You are only allowed to
11966 define a class that has already been declared with this
11969 The proposed resolution for Core Issue 180 says that whever
11970 you see `class T::X' you should treat `X' as a type-name.
11972 It is OK to define an inaccessible class; for example:
11974 class A { class B; };
11977 We do not know if we will see a class-name, or a
11978 template-name. We look for a class-name first, in case the
11979 class-name is a template-id; if we looked for the
11980 template-name first we would stop after the template-name. */
11981 cp_parser_parse_tentatively (parser);
11982 type = cp_parser_class_name (parser,
11983 /*typename_keyword_p=*/false,
11984 /*template_keyword_p=*/false,
11986 /*check_dependency_p=*/false,
11987 /*class_head_p=*/true,
11988 /*is_declaration=*/false);
11989 /* If that didn't work, ignore the nested-name-specifier. */
11990 if (!cp_parser_parse_definitely (parser))
11992 invalid_nested_name_p = true;
11993 id = cp_parser_identifier (parser);
11994 if (id == error_mark_node)
11997 /* If we could not find a corresponding TYPE, treat this
11998 declaration like an unqualified declaration. */
11999 if (type == error_mark_node)
12000 nested_name_specifier = NULL_TREE;
12001 /* Otherwise, count the number of templates used in TYPE and its
12002 containing scopes. */
12007 for (scope = TREE_TYPE (type);
12008 scope && TREE_CODE (scope) != NAMESPACE_DECL;
12009 scope = (TYPE_P (scope)
12010 ? TYPE_CONTEXT (scope)
12011 : DECL_CONTEXT (scope)))
12013 && CLASS_TYPE_P (scope)
12014 && CLASSTYPE_TEMPLATE_INFO (scope)
12015 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
12016 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
12020 /* Otherwise, the identifier is optional. */
12023 /* We don't know whether what comes next is a template-id,
12024 an identifier, or nothing at all. */
12025 cp_parser_parse_tentatively (parser);
12026 /* Check for a template-id. */
12027 id = cp_parser_template_id (parser,
12028 /*template_keyword_p=*/false,
12029 /*check_dependency_p=*/true,
12030 /*is_declaration=*/true);
12031 /* If that didn't work, it could still be an identifier. */
12032 if (!cp_parser_parse_definitely (parser))
12034 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12035 id = cp_parser_identifier (parser);
12041 template_id_p = true;
12046 pop_deferring_access_checks ();
12048 cp_parser_check_for_invalid_template_id (parser, id);
12050 /* If it's not a `:' or a `{' then we can't really be looking at a
12051 class-head, since a class-head only appears as part of a
12052 class-specifier. We have to detect this situation before calling
12053 xref_tag, since that has irreversible side-effects. */
12054 if (!cp_parser_next_token_starts_class_definition_p (parser))
12056 cp_parser_error (parser, "expected `{' or `:'");
12057 return error_mark_node;
12060 /* At this point, we're going ahead with the class-specifier, even
12061 if some other problem occurs. */
12062 cp_parser_commit_to_tentative_parse (parser);
12063 /* Issue the error about the overly-qualified name now. */
12065 cp_parser_error (parser,
12066 "global qualification of class name is invalid");
12067 else if (invalid_nested_name_p)
12068 cp_parser_error (parser,
12069 "qualified name does not name a class");
12070 else if (nested_name_specifier)
12073 /* Figure out in what scope the declaration is being placed. */
12074 scope = current_scope ();
12076 scope = current_namespace;
12077 /* If that scope does not contain the scope in which the
12078 class was originally declared, the program is invalid. */
12079 if (scope && !is_ancestor (scope, nested_name_specifier))
12081 error ("declaration of `%D' in `%D' which does not "
12082 "enclose `%D'", type, scope, nested_name_specifier);
12088 A declarator-id shall not be qualified exception of the
12089 definition of a ... nested class outside of its class
12090 ... [or] a the definition or explicit instantiation of a
12091 class member of a namespace outside of its namespace. */
12092 if (scope == nested_name_specifier)
12094 pedwarn ("extra qualification ignored");
12095 nested_name_specifier = NULL_TREE;
12099 /* An explicit-specialization must be preceded by "template <>". If
12100 it is not, try to recover gracefully. */
12101 if (at_namespace_scope_p ()
12102 && parser->num_template_parameter_lists == 0
12105 error ("an explicit specialization must be preceded by 'template <>'");
12106 invalid_explicit_specialization_p = true;
12107 /* Take the same action that would have been taken by
12108 cp_parser_explicit_specialization. */
12109 ++parser->num_template_parameter_lists;
12110 begin_specialization ();
12112 /* There must be no "return" statements between this point and the
12113 end of this function; set "type "to the correct return value and
12114 use "goto done;" to return. */
12115 /* Make sure that the right number of template parameters were
12117 if (!cp_parser_check_template_parameters (parser, num_templates))
12119 /* If something went wrong, there is no point in even trying to
12120 process the class-definition. */
12125 /* Look up the type. */
12128 type = TREE_TYPE (id);
12129 maybe_process_partial_specialization (type);
12131 else if (!nested_name_specifier)
12133 /* If the class was unnamed, create a dummy name. */
12135 id = make_anon_name ();
12136 type = xref_tag (class_key, id, attributes, /*globalize=*/false,
12137 parser->num_template_parameter_lists);
12145 template <typename T> struct S { struct T };
12146 template <typename T> struct S<T>::T { };
12148 we will get a TYPENAME_TYPE when processing the definition of
12149 `S::T'. We need to resolve it to the actual type before we
12150 try to define it. */
12151 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
12153 class_type = resolve_typename_type (TREE_TYPE (type),
12154 /*only_current_p=*/false);
12155 if (class_type != error_mark_node)
12156 type = TYPE_NAME (class_type);
12159 cp_parser_error (parser, "could not resolve typename type");
12160 type = error_mark_node;
12164 maybe_process_partial_specialization (TREE_TYPE (type));
12165 class_type = current_class_type;
12166 /* Enter the scope indicated by the nested-name-specifier. */
12167 if (nested_name_specifier)
12168 push_scope (nested_name_specifier);
12169 /* Get the canonical version of this type. */
12170 type = TYPE_MAIN_DECL (TREE_TYPE (type));
12171 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
12172 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
12173 type = push_template_decl (type);
12174 type = TREE_TYPE (type);
12175 if (nested_name_specifier)
12177 *nested_name_specifier_p = true;
12178 pop_scope (nested_name_specifier);
12181 /* Indicate whether this class was declared as a `class' or as a
12183 if (TREE_CODE (type) == RECORD_TYPE)
12184 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
12185 cp_parser_check_class_key (class_key, type);
12187 /* Enter the scope containing the class; the names of base classes
12188 should be looked up in that context. For example, given:
12190 struct A { struct B {}; struct C; };
12191 struct A::C : B {};
12194 if (nested_name_specifier)
12195 push_scope (nested_name_specifier);
12196 /* Now, look for the base-clause. */
12197 token = cp_lexer_peek_token (parser->lexer);
12198 if (token->type == CPP_COLON)
12202 /* Get the list of base-classes. */
12203 bases = cp_parser_base_clause (parser);
12204 /* Process them. */
12205 xref_basetypes (type, bases);
12207 /* Leave the scope given by the nested-name-specifier. We will
12208 enter the class scope itself while processing the members. */
12209 if (nested_name_specifier)
12210 pop_scope (nested_name_specifier);
12213 if (invalid_explicit_specialization_p)
12215 end_specialization ();
12216 --parser->num_template_parameter_lists;
12221 /* Parse a class-key.
12228 Returns the kind of class-key specified, or none_type to indicate
12231 static enum tag_types
12232 cp_parser_class_key (cp_parser* parser)
12235 enum tag_types tag_type;
12237 /* Look for the class-key. */
12238 token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
12242 /* Check to see if the TOKEN is a class-key. */
12243 tag_type = cp_parser_token_is_class_key (token);
12245 cp_parser_error (parser, "expected class-key");
12249 /* Parse an (optional) member-specification.
12251 member-specification:
12252 member-declaration member-specification [opt]
12253 access-specifier : member-specification [opt] */
12256 cp_parser_member_specification_opt (cp_parser* parser)
12263 /* Peek at the next token. */
12264 token = cp_lexer_peek_token (parser->lexer);
12265 /* If it's a `}', or EOF then we've seen all the members. */
12266 if (token->type == CPP_CLOSE_BRACE || token->type == CPP_EOF)
12269 /* See if this token is a keyword. */
12270 keyword = token->keyword;
12274 case RID_PROTECTED:
12276 /* Consume the access-specifier. */
12277 cp_lexer_consume_token (parser->lexer);
12278 /* Remember which access-specifier is active. */
12279 current_access_specifier = token->value;
12280 /* Look for the `:'. */
12281 cp_parser_require (parser, CPP_COLON, "`:'");
12285 /* Otherwise, the next construction must be a
12286 member-declaration. */
12287 cp_parser_member_declaration (parser);
12292 /* Parse a member-declaration.
12294 member-declaration:
12295 decl-specifier-seq [opt] member-declarator-list [opt] ;
12296 function-definition ; [opt]
12297 :: [opt] nested-name-specifier template [opt] unqualified-id ;
12299 template-declaration
12301 member-declarator-list:
12303 member-declarator-list , member-declarator
12306 declarator pure-specifier [opt]
12307 declarator constant-initializer [opt]
12308 identifier [opt] : constant-expression
12312 member-declaration:
12313 __extension__ member-declaration
12316 declarator attributes [opt] pure-specifier [opt]
12317 declarator attributes [opt] constant-initializer [opt]
12318 identifier [opt] attributes [opt] : constant-expression */
12321 cp_parser_member_declaration (cp_parser* parser)
12323 tree decl_specifiers;
12324 tree prefix_attributes;
12326 int declares_class_or_enum;
12329 int saved_pedantic;
12331 /* Check for the `__extension__' keyword. */
12332 if (cp_parser_extension_opt (parser, &saved_pedantic))
12335 cp_parser_member_declaration (parser);
12336 /* Restore the old value of the PEDANTIC flag. */
12337 pedantic = saved_pedantic;
12342 /* Check for a template-declaration. */
12343 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
12345 /* Parse the template-declaration. */
12346 cp_parser_template_declaration (parser, /*member_p=*/true);
12351 /* Check for a using-declaration. */
12352 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
12354 /* Parse the using-declaration. */
12355 cp_parser_using_declaration (parser);
12360 /* Parse the decl-specifier-seq. */
12362 = cp_parser_decl_specifier_seq (parser,
12363 CP_PARSER_FLAGS_OPTIONAL,
12364 &prefix_attributes,
12365 &declares_class_or_enum);
12366 /* Check for an invalid type-name. */
12367 if (cp_parser_parse_and_diagnose_invalid_type_name (parser))
12369 /* If there is no declarator, then the decl-specifier-seq should
12371 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12373 /* If there was no decl-specifier-seq, and the next token is a
12374 `;', then we have something like:
12380 Each member-declaration shall declare at least one member
12381 name of the class. */
12382 if (!decl_specifiers)
12385 pedwarn ("extra semicolon");
12391 /* See if this declaration is a friend. */
12392 friend_p = cp_parser_friend_p (decl_specifiers);
12393 /* If there were decl-specifiers, check to see if there was
12394 a class-declaration. */
12395 type = check_tag_decl (decl_specifiers);
12396 /* Nested classes have already been added to the class, but
12397 a `friend' needs to be explicitly registered. */
12400 /* If the `friend' keyword was present, the friend must
12401 be introduced with a class-key. */
12402 if (!declares_class_or_enum)
12403 error ("a class-key must be used when declaring a friend");
12406 template <typename T> struct A {
12407 friend struct A<T>::B;
12410 A<T>::B will be represented by a TYPENAME_TYPE, and
12411 therefore not recognized by check_tag_decl. */
12416 for (specifier = decl_specifiers;
12418 specifier = TREE_CHAIN (specifier))
12420 tree s = TREE_VALUE (specifier);
12422 if (TREE_CODE (s) == IDENTIFIER_NODE)
12423 get_global_value_if_present (s, &type);
12424 if (TREE_CODE (s) == TYPE_DECL)
12433 if (!type || !TYPE_P (type))
12434 error ("friend declaration does not name a class or "
12437 make_friend_class (current_class_type, type,
12438 /*complain=*/true);
12440 /* If there is no TYPE, an error message will already have
12444 /* An anonymous aggregate has to be handled specially; such
12445 a declaration really declares a data member (with a
12446 particular type), as opposed to a nested class. */
12447 else if (ANON_AGGR_TYPE_P (type))
12449 /* Remove constructors and such from TYPE, now that we
12450 know it is an anonymous aggregate. */
12451 fixup_anonymous_aggr (type);
12452 /* And make the corresponding data member. */
12453 decl = build_decl (FIELD_DECL, NULL_TREE, type);
12454 /* Add it to the class. */
12455 finish_member_declaration (decl);
12458 cp_parser_check_access_in_redeclaration (TYPE_NAME (type));
12463 /* See if these declarations will be friends. */
12464 friend_p = cp_parser_friend_p (decl_specifiers);
12466 /* Keep going until we hit the `;' at the end of the
12468 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12470 tree attributes = NULL_TREE;
12471 tree first_attribute;
12473 /* Peek at the next token. */
12474 token = cp_lexer_peek_token (parser->lexer);
12476 /* Check for a bitfield declaration. */
12477 if (token->type == CPP_COLON
12478 || (token->type == CPP_NAME
12479 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
12485 /* Get the name of the bitfield. Note that we cannot just
12486 check TOKEN here because it may have been invalidated by
12487 the call to cp_lexer_peek_nth_token above. */
12488 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
12489 identifier = cp_parser_identifier (parser);
12491 identifier = NULL_TREE;
12493 /* Consume the `:' token. */
12494 cp_lexer_consume_token (parser->lexer);
12495 /* Get the width of the bitfield. */
12497 = cp_parser_constant_expression (parser,
12498 /*allow_non_constant=*/false,
12501 /* Look for attributes that apply to the bitfield. */
12502 attributes = cp_parser_attributes_opt (parser);
12503 /* Remember which attributes are prefix attributes and
12505 first_attribute = attributes;
12506 /* Combine the attributes. */
12507 attributes = chainon (prefix_attributes, attributes);
12509 /* Create the bitfield declaration. */
12510 decl = grokbitfield (identifier,
12513 /* Apply the attributes. */
12514 cplus_decl_attributes (&decl, attributes, /*flags=*/0);
12520 tree asm_specification;
12521 int ctor_dtor_or_conv_p;
12523 /* Parse the declarator. */
12525 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
12526 &ctor_dtor_or_conv_p,
12527 /*parenthesized_p=*/NULL);
12529 /* If something went wrong parsing the declarator, make sure
12530 that we at least consume some tokens. */
12531 if (declarator == error_mark_node)
12533 /* Skip to the end of the statement. */
12534 cp_parser_skip_to_end_of_statement (parser);
12535 /* If the next token is not a semicolon, that is
12536 probably because we just skipped over the body of
12537 a function. So, we consume a semicolon if
12538 present, but do not issue an error message if it
12540 if (cp_lexer_next_token_is (parser->lexer,
12542 cp_lexer_consume_token (parser->lexer);
12546 cp_parser_check_for_definition_in_return_type
12547 (declarator, declares_class_or_enum);
12549 /* Look for an asm-specification. */
12550 asm_specification = cp_parser_asm_specification_opt (parser);
12551 /* Look for attributes that apply to the declaration. */
12552 attributes = cp_parser_attributes_opt (parser);
12553 /* Remember which attributes are prefix attributes and
12555 first_attribute = attributes;
12556 /* Combine the attributes. */
12557 attributes = chainon (prefix_attributes, attributes);
12559 /* If it's an `=', then we have a constant-initializer or a
12560 pure-specifier. It is not correct to parse the
12561 initializer before registering the member declaration
12562 since the member declaration should be in scope while
12563 its initializer is processed. However, the rest of the
12564 front end does not yet provide an interface that allows
12565 us to handle this correctly. */
12566 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12570 A pure-specifier shall be used only in the declaration of
12571 a virtual function.
12573 A member-declarator can contain a constant-initializer
12574 only if it declares a static member of integral or
12577 Therefore, if the DECLARATOR is for a function, we look
12578 for a pure-specifier; otherwise, we look for a
12579 constant-initializer. When we call `grokfield', it will
12580 perform more stringent semantics checks. */
12581 if (TREE_CODE (declarator) == CALL_EXPR)
12582 initializer = cp_parser_pure_specifier (parser);
12584 /* Parse the initializer. */
12585 initializer = cp_parser_constant_initializer (parser);
12587 /* Otherwise, there is no initializer. */
12589 initializer = NULL_TREE;
12591 /* See if we are probably looking at a function
12592 definition. We are certainly not looking at at a
12593 member-declarator. Calling `grokfield' has
12594 side-effects, so we must not do it unless we are sure
12595 that we are looking at a member-declarator. */
12596 if (cp_parser_token_starts_function_definition_p
12597 (cp_lexer_peek_token (parser->lexer)))
12599 /* The grammar does not allow a pure-specifier to be
12600 used when a member function is defined. (It is
12601 possible that this fact is an oversight in the
12602 standard, since a pure function may be defined
12603 outside of the class-specifier. */
12605 error ("pure-specifier on function-definition");
12606 decl = cp_parser_save_member_function_body (parser,
12610 /* If the member was not a friend, declare it here. */
12612 finish_member_declaration (decl);
12613 /* Peek at the next token. */
12614 token = cp_lexer_peek_token (parser->lexer);
12615 /* If the next token is a semicolon, consume it. */
12616 if (token->type == CPP_SEMICOLON)
12617 cp_lexer_consume_token (parser->lexer);
12622 /* Create the declaration. */
12623 decl = grokfield (declarator, decl_specifiers,
12624 initializer, asm_specification,
12626 /* Any initialization must have been from a
12627 constant-expression. */
12628 if (decl && TREE_CODE (decl) == VAR_DECL && initializer)
12629 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = 1;
12633 /* Reset PREFIX_ATTRIBUTES. */
12634 while (attributes && TREE_CHAIN (attributes) != first_attribute)
12635 attributes = TREE_CHAIN (attributes);
12637 TREE_CHAIN (attributes) = NULL_TREE;
12639 /* If there is any qualification still in effect, clear it
12640 now; we will be starting fresh with the next declarator. */
12641 parser->scope = NULL_TREE;
12642 parser->qualifying_scope = NULL_TREE;
12643 parser->object_scope = NULL_TREE;
12644 /* If it's a `,', then there are more declarators. */
12645 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
12646 cp_lexer_consume_token (parser->lexer);
12647 /* If the next token isn't a `;', then we have a parse error. */
12648 else if (cp_lexer_next_token_is_not (parser->lexer,
12651 cp_parser_error (parser, "expected `;'");
12652 /* Skip tokens until we find a `;'. */
12653 cp_parser_skip_to_end_of_statement (parser);
12660 /* Add DECL to the list of members. */
12662 finish_member_declaration (decl);
12664 if (TREE_CODE (decl) == FUNCTION_DECL)
12665 cp_parser_save_default_args (parser, decl);
12670 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
12673 /* Parse a pure-specifier.
12678 Returns INTEGER_ZERO_NODE if a pure specifier is found.
12679 Otherwise, ERROR_MARK_NODE is returned. */
12682 cp_parser_pure_specifier (cp_parser* parser)
12686 /* Look for the `=' token. */
12687 if (!cp_parser_require (parser, CPP_EQ, "`='"))
12688 return error_mark_node;
12689 /* Look for the `0' token. */
12690 token = cp_parser_require (parser, CPP_NUMBER, "`0'");
12691 /* Unfortunately, this will accept `0L' and `0x00' as well. We need
12692 to get information from the lexer about how the number was
12693 spelled in order to fix this problem. */
12694 if (!token || !integer_zerop (token->value))
12695 return error_mark_node;
12697 return integer_zero_node;
12700 /* Parse a constant-initializer.
12702 constant-initializer:
12703 = constant-expression
12705 Returns a representation of the constant-expression. */
12708 cp_parser_constant_initializer (cp_parser* parser)
12710 /* Look for the `=' token. */
12711 if (!cp_parser_require (parser, CPP_EQ, "`='"))
12712 return error_mark_node;
12714 /* It is invalid to write:
12716 struct S { static const int i = { 7 }; };
12719 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12721 cp_parser_error (parser,
12722 "a brace-enclosed initializer is not allowed here");
12723 /* Consume the opening brace. */
12724 cp_lexer_consume_token (parser->lexer);
12725 /* Skip the initializer. */
12726 cp_parser_skip_to_closing_brace (parser);
12727 /* Look for the trailing `}'. */
12728 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
12730 return error_mark_node;
12733 return cp_parser_constant_expression (parser,
12734 /*allow_non_constant=*/false,
12738 /* Derived classes [gram.class.derived] */
12740 /* Parse a base-clause.
12743 : base-specifier-list
12745 base-specifier-list:
12747 base-specifier-list , base-specifier
12749 Returns a TREE_LIST representing the base-classes, in the order in
12750 which they were declared. The representation of each node is as
12751 described by cp_parser_base_specifier.
12753 In the case that no bases are specified, this function will return
12754 NULL_TREE, not ERROR_MARK_NODE. */
12757 cp_parser_base_clause (cp_parser* parser)
12759 tree bases = NULL_TREE;
12761 /* Look for the `:' that begins the list. */
12762 cp_parser_require (parser, CPP_COLON, "`:'");
12764 /* Scan the base-specifier-list. */
12770 /* Look for the base-specifier. */
12771 base = cp_parser_base_specifier (parser);
12772 /* Add BASE to the front of the list. */
12773 if (base != error_mark_node)
12775 TREE_CHAIN (base) = bases;
12778 /* Peek at the next token. */
12779 token = cp_lexer_peek_token (parser->lexer);
12780 /* If it's not a comma, then the list is complete. */
12781 if (token->type != CPP_COMMA)
12783 /* Consume the `,'. */
12784 cp_lexer_consume_token (parser->lexer);
12787 /* PARSER->SCOPE may still be non-NULL at this point, if the last
12788 base class had a qualified name. However, the next name that
12789 appears is certainly not qualified. */
12790 parser->scope = NULL_TREE;
12791 parser->qualifying_scope = NULL_TREE;
12792 parser->object_scope = NULL_TREE;
12794 return nreverse (bases);
12797 /* Parse a base-specifier.
12800 :: [opt] nested-name-specifier [opt] class-name
12801 virtual access-specifier [opt] :: [opt] nested-name-specifier
12803 access-specifier virtual [opt] :: [opt] nested-name-specifier
12806 Returns a TREE_LIST. The TREE_PURPOSE will be one of
12807 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
12808 indicate the specifiers provided. The TREE_VALUE will be a TYPE
12809 (or the ERROR_MARK_NODE) indicating the type that was specified. */
12812 cp_parser_base_specifier (cp_parser* parser)
12816 bool virtual_p = false;
12817 bool duplicate_virtual_error_issued_p = false;
12818 bool duplicate_access_error_issued_p = false;
12819 bool class_scope_p, template_p;
12820 tree access = access_default_node;
12823 /* Process the optional `virtual' and `access-specifier'. */
12826 /* Peek at the next token. */
12827 token = cp_lexer_peek_token (parser->lexer);
12828 /* Process `virtual'. */
12829 switch (token->keyword)
12832 /* If `virtual' appears more than once, issue an error. */
12833 if (virtual_p && !duplicate_virtual_error_issued_p)
12835 cp_parser_error (parser,
12836 "`virtual' specified more than once in base-specified");
12837 duplicate_virtual_error_issued_p = true;
12842 /* Consume the `virtual' token. */
12843 cp_lexer_consume_token (parser->lexer);
12848 case RID_PROTECTED:
12850 /* If more than one access specifier appears, issue an
12852 if (access != access_default_node
12853 && !duplicate_access_error_issued_p)
12855 cp_parser_error (parser,
12856 "more than one access specifier in base-specified");
12857 duplicate_access_error_issued_p = true;
12860 access = ridpointers[(int) token->keyword];
12862 /* Consume the access-specifier. */
12863 cp_lexer_consume_token (parser->lexer);
12872 /* It is not uncommon to see programs mechanically, erroneously, use
12873 the 'typename' keyword to denote (dependent) qualified types
12874 as base classes. */
12875 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
12877 if (!processing_template_decl)
12878 error ("keyword `typename' not allowed outside of templates");
12880 error ("keyword `typename' not allowed in this context "
12881 "(the base class is implicitly a type)");
12882 cp_lexer_consume_token (parser->lexer);
12885 /* Look for the optional `::' operator. */
12886 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
12887 /* Look for the nested-name-specifier. The simplest way to
12892 The keyword `typename' is not permitted in a base-specifier or
12893 mem-initializer; in these contexts a qualified name that
12894 depends on a template-parameter is implicitly assumed to be a
12897 is to pretend that we have seen the `typename' keyword at this
12899 cp_parser_nested_name_specifier_opt (parser,
12900 /*typename_keyword_p=*/true,
12901 /*check_dependency_p=*/true,
12903 /*is_declaration=*/true);
12904 /* If the base class is given by a qualified name, assume that names
12905 we see are type names or templates, as appropriate. */
12906 class_scope_p = (parser->scope && TYPE_P (parser->scope));
12907 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
12909 /* Finally, look for the class-name. */
12910 type = cp_parser_class_name (parser,
12914 /*check_dependency_p=*/true,
12915 /*class_head_p=*/false,
12916 /*is_declaration=*/true);
12918 if (type == error_mark_node)
12919 return error_mark_node;
12921 return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
12924 /* Exception handling [gram.exception] */
12926 /* Parse an (optional) exception-specification.
12928 exception-specification:
12929 throw ( type-id-list [opt] )
12931 Returns a TREE_LIST representing the exception-specification. The
12932 TREE_VALUE of each node is a type. */
12935 cp_parser_exception_specification_opt (cp_parser* parser)
12940 /* Peek at the next token. */
12941 token = cp_lexer_peek_token (parser->lexer);
12942 /* If it's not `throw', then there's no exception-specification. */
12943 if (!cp_parser_is_keyword (token, RID_THROW))
12946 /* Consume the `throw'. */
12947 cp_lexer_consume_token (parser->lexer);
12949 /* Look for the `('. */
12950 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
12952 /* Peek at the next token. */
12953 token = cp_lexer_peek_token (parser->lexer);
12954 /* If it's not a `)', then there is a type-id-list. */
12955 if (token->type != CPP_CLOSE_PAREN)
12957 const char *saved_message;
12959 /* Types may not be defined in an exception-specification. */
12960 saved_message = parser->type_definition_forbidden_message;
12961 parser->type_definition_forbidden_message
12962 = "types may not be defined in an exception-specification";
12963 /* Parse the type-id-list. */
12964 type_id_list = cp_parser_type_id_list (parser);
12965 /* Restore the saved message. */
12966 parser->type_definition_forbidden_message = saved_message;
12969 type_id_list = empty_except_spec;
12971 /* Look for the `)'. */
12972 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
12974 return type_id_list;
12977 /* Parse an (optional) type-id-list.
12981 type-id-list , type-id
12983 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
12984 in the order that the types were presented. */
12987 cp_parser_type_id_list (cp_parser* parser)
12989 tree types = NULL_TREE;
12996 /* Get the next type-id. */
12997 type = cp_parser_type_id (parser);
12998 /* Add it to the list. */
12999 types = add_exception_specifier (types, type, /*complain=*/1);
13000 /* Peek at the next token. */
13001 token = cp_lexer_peek_token (parser->lexer);
13002 /* If it is not a `,', we are done. */
13003 if (token->type != CPP_COMMA)
13005 /* Consume the `,'. */
13006 cp_lexer_consume_token (parser->lexer);
13009 return nreverse (types);
13012 /* Parse a try-block.
13015 try compound-statement handler-seq */
13018 cp_parser_try_block (cp_parser* parser)
13022 cp_parser_require_keyword (parser, RID_TRY, "`try'");
13023 try_block = begin_try_block ();
13024 cp_parser_compound_statement (parser, false);
13025 finish_try_block (try_block);
13026 cp_parser_handler_seq (parser);
13027 finish_handler_sequence (try_block);
13032 /* Parse a function-try-block.
13034 function-try-block:
13035 try ctor-initializer [opt] function-body handler-seq */
13038 cp_parser_function_try_block (cp_parser* parser)
13041 bool ctor_initializer_p;
13043 /* Look for the `try' keyword. */
13044 if (!cp_parser_require_keyword (parser, RID_TRY, "`try'"))
13046 /* Let the rest of the front-end know where we are. */
13047 try_block = begin_function_try_block ();
13048 /* Parse the function-body. */
13050 = cp_parser_ctor_initializer_opt_and_function_body (parser);
13051 /* We're done with the `try' part. */
13052 finish_function_try_block (try_block);
13053 /* Parse the handlers. */
13054 cp_parser_handler_seq (parser);
13055 /* We're done with the handlers. */
13056 finish_function_handler_sequence (try_block);
13058 return ctor_initializer_p;
13061 /* Parse a handler-seq.
13064 handler handler-seq [opt] */
13067 cp_parser_handler_seq (cp_parser* parser)
13073 /* Parse the handler. */
13074 cp_parser_handler (parser);
13075 /* Peek at the next token. */
13076 token = cp_lexer_peek_token (parser->lexer);
13077 /* If it's not `catch' then there are no more handlers. */
13078 if (!cp_parser_is_keyword (token, RID_CATCH))
13083 /* Parse a handler.
13086 catch ( exception-declaration ) compound-statement */
13089 cp_parser_handler (cp_parser* parser)
13094 cp_parser_require_keyword (parser, RID_CATCH, "`catch'");
13095 handler = begin_handler ();
13096 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13097 declaration = cp_parser_exception_declaration (parser);
13098 finish_handler_parms (declaration, handler);
13099 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13100 cp_parser_compound_statement (parser, false);
13101 finish_handler (handler);
13104 /* Parse an exception-declaration.
13106 exception-declaration:
13107 type-specifier-seq declarator
13108 type-specifier-seq abstract-declarator
13112 Returns a VAR_DECL for the declaration, or NULL_TREE if the
13113 ellipsis variant is used. */
13116 cp_parser_exception_declaration (cp_parser* parser)
13118 tree type_specifiers;
13120 const char *saved_message;
13122 /* If it's an ellipsis, it's easy to handle. */
13123 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13125 /* Consume the `...' token. */
13126 cp_lexer_consume_token (parser->lexer);
13130 /* Types may not be defined in exception-declarations. */
13131 saved_message = parser->type_definition_forbidden_message;
13132 parser->type_definition_forbidden_message
13133 = "types may not be defined in exception-declarations";
13135 /* Parse the type-specifier-seq. */
13136 type_specifiers = cp_parser_type_specifier_seq (parser);
13137 /* If it's a `)', then there is no declarator. */
13138 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
13139 declarator = NULL_TREE;
13141 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
13142 /*ctor_dtor_or_conv_p=*/NULL,
13143 /*parenthesized_p=*/NULL);
13145 /* Restore the saved message. */
13146 parser->type_definition_forbidden_message = saved_message;
13148 return start_handler_parms (type_specifiers, declarator);
13151 /* Parse a throw-expression.
13154 throw assignment-expression [opt]
13156 Returns a THROW_EXPR representing the throw-expression. */
13159 cp_parser_throw_expression (cp_parser* parser)
13164 cp_parser_require_keyword (parser, RID_THROW, "`throw'");
13165 token = cp_lexer_peek_token (parser->lexer);
13166 /* Figure out whether or not there is an assignment-expression
13167 following the "throw" keyword. */
13168 if (token->type == CPP_COMMA
13169 || token->type == CPP_SEMICOLON
13170 || token->type == CPP_CLOSE_PAREN
13171 || token->type == CPP_CLOSE_SQUARE
13172 || token->type == CPP_CLOSE_BRACE
13173 || token->type == CPP_COLON)
13174 expression = NULL_TREE;
13176 expression = cp_parser_assignment_expression (parser);
13178 return build_throw (expression);
13181 /* GNU Extensions */
13183 /* Parse an (optional) asm-specification.
13186 asm ( string-literal )
13188 If the asm-specification is present, returns a STRING_CST
13189 corresponding to the string-literal. Otherwise, returns
13193 cp_parser_asm_specification_opt (cp_parser* parser)
13196 tree asm_specification;
13198 /* Peek at the next token. */
13199 token = cp_lexer_peek_token (parser->lexer);
13200 /* If the next token isn't the `asm' keyword, then there's no
13201 asm-specification. */
13202 if (!cp_parser_is_keyword (token, RID_ASM))
13205 /* Consume the `asm' token. */
13206 cp_lexer_consume_token (parser->lexer);
13207 /* Look for the `('. */
13208 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13210 /* Look for the string-literal. */
13211 token = cp_parser_require (parser, CPP_STRING, "string-literal");
13213 asm_specification = token->value;
13215 asm_specification = NULL_TREE;
13217 /* Look for the `)'. */
13218 cp_parser_require (parser, CPP_CLOSE_PAREN, "`('");
13220 return asm_specification;
13223 /* Parse an asm-operand-list.
13227 asm-operand-list , asm-operand
13230 string-literal ( expression )
13231 [ string-literal ] string-literal ( expression )
13233 Returns a TREE_LIST representing the operands. The TREE_VALUE of
13234 each node is the expression. The TREE_PURPOSE is itself a
13235 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
13236 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
13237 is a STRING_CST for the string literal before the parenthesis. */
13240 cp_parser_asm_operand_list (cp_parser* parser)
13242 tree asm_operands = NULL_TREE;
13246 tree string_literal;
13251 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
13253 /* Consume the `[' token. */
13254 cp_lexer_consume_token (parser->lexer);
13255 /* Read the operand name. */
13256 name = cp_parser_identifier (parser);
13257 if (name != error_mark_node)
13258 name = build_string (IDENTIFIER_LENGTH (name),
13259 IDENTIFIER_POINTER (name));
13260 /* Look for the closing `]'. */
13261 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
13265 /* Look for the string-literal. */
13266 token = cp_parser_require (parser, CPP_STRING, "string-literal");
13267 string_literal = token ? token->value : error_mark_node;
13268 /* Look for the `('. */
13269 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13270 /* Parse the expression. */
13271 expression = cp_parser_expression (parser);
13272 /* Look for the `)'. */
13273 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13274 /* Add this operand to the list. */
13275 asm_operands = tree_cons (build_tree_list (name, string_literal),
13278 /* If the next token is not a `,', there are no more
13280 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13282 /* Consume the `,'. */
13283 cp_lexer_consume_token (parser->lexer);
13286 return nreverse (asm_operands);
13289 /* Parse an asm-clobber-list.
13293 asm-clobber-list , string-literal
13295 Returns a TREE_LIST, indicating the clobbers in the order that they
13296 appeared. The TREE_VALUE of each node is a STRING_CST. */
13299 cp_parser_asm_clobber_list (cp_parser* parser)
13301 tree clobbers = NULL_TREE;
13306 tree string_literal;
13308 /* Look for the string literal. */
13309 token = cp_parser_require (parser, CPP_STRING, "string-literal");
13310 string_literal = token ? token->value : error_mark_node;
13311 /* Add it to the list. */
13312 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
13313 /* If the next token is not a `,', then the list is
13315 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13317 /* Consume the `,' token. */
13318 cp_lexer_consume_token (parser->lexer);
13324 /* Parse an (optional) series of attributes.
13327 attributes attribute
13330 __attribute__ (( attribute-list [opt] ))
13332 The return value is as for cp_parser_attribute_list. */
13335 cp_parser_attributes_opt (cp_parser* parser)
13337 tree attributes = NULL_TREE;
13342 tree attribute_list;
13344 /* Peek at the next token. */
13345 token = cp_lexer_peek_token (parser->lexer);
13346 /* If it's not `__attribute__', then we're done. */
13347 if (token->keyword != RID_ATTRIBUTE)
13350 /* Consume the `__attribute__' keyword. */
13351 cp_lexer_consume_token (parser->lexer);
13352 /* Look for the two `(' tokens. */
13353 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13354 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13356 /* Peek at the next token. */
13357 token = cp_lexer_peek_token (parser->lexer);
13358 if (token->type != CPP_CLOSE_PAREN)
13359 /* Parse the attribute-list. */
13360 attribute_list = cp_parser_attribute_list (parser);
13362 /* If the next token is a `)', then there is no attribute
13364 attribute_list = NULL;
13366 /* Look for the two `)' tokens. */
13367 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13368 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13370 /* Add these new attributes to the list. */
13371 attributes = chainon (attributes, attribute_list);
13377 /* Parse an attribute-list.
13381 attribute-list , attribute
13385 identifier ( identifier )
13386 identifier ( identifier , expression-list )
13387 identifier ( expression-list )
13389 Returns a TREE_LIST. Each node corresponds to an attribute. THe
13390 TREE_PURPOSE of each node is the identifier indicating which
13391 attribute is in use. The TREE_VALUE represents the arguments, if
13395 cp_parser_attribute_list (cp_parser* parser)
13397 tree attribute_list = NULL_TREE;
13405 /* Look for the identifier. We also allow keywords here; for
13406 example `__attribute__ ((const))' is legal. */
13407 token = cp_lexer_peek_token (parser->lexer);
13408 if (token->type != CPP_NAME
13409 && token->type != CPP_KEYWORD)
13410 return error_mark_node;
13411 /* Consume the token. */
13412 token = cp_lexer_consume_token (parser->lexer);
13414 /* Save away the identifier that indicates which attribute this is. */
13415 identifier = token->value;
13416 attribute = build_tree_list (identifier, NULL_TREE);
13418 /* Peek at the next token. */
13419 token = cp_lexer_peek_token (parser->lexer);
13420 /* If it's an `(', then parse the attribute arguments. */
13421 if (token->type == CPP_OPEN_PAREN)
13425 arguments = (cp_parser_parenthesized_expression_list
13426 (parser, true, /*non_constant_p=*/NULL));
13427 /* Save the identifier and arguments away. */
13428 TREE_VALUE (attribute) = arguments;
13431 /* Add this attribute to the list. */
13432 TREE_CHAIN (attribute) = attribute_list;
13433 attribute_list = attribute;
13435 /* Now, look for more attributes. */
13436 token = cp_lexer_peek_token (parser->lexer);
13437 /* If the next token isn't a `,', we're done. */
13438 if (token->type != CPP_COMMA)
13441 /* Consume the comma and keep going. */
13442 cp_lexer_consume_token (parser->lexer);
13445 /* We built up the list in reverse order. */
13446 return nreverse (attribute_list);
13449 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
13450 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
13451 current value of the PEDANTIC flag, regardless of whether or not
13452 the `__extension__' keyword is present. The caller is responsible
13453 for restoring the value of the PEDANTIC flag. */
13456 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
13458 /* Save the old value of the PEDANTIC flag. */
13459 *saved_pedantic = pedantic;
13461 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
13463 /* Consume the `__extension__' token. */
13464 cp_lexer_consume_token (parser->lexer);
13465 /* We're not being pedantic while the `__extension__' keyword is
13475 /* Parse a label declaration.
13478 __label__ label-declarator-seq ;
13480 label-declarator-seq:
13481 identifier , label-declarator-seq
13485 cp_parser_label_declaration (cp_parser* parser)
13487 /* Look for the `__label__' keyword. */
13488 cp_parser_require_keyword (parser, RID_LABEL, "`__label__'");
13494 /* Look for an identifier. */
13495 identifier = cp_parser_identifier (parser);
13496 /* Declare it as a lobel. */
13497 finish_label_decl (identifier);
13498 /* If the next token is a `;', stop. */
13499 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13501 /* Look for the `,' separating the label declarations. */
13502 cp_parser_require (parser, CPP_COMMA, "`,'");
13505 /* Look for the final `;'. */
13506 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
13509 /* Support Functions */
13511 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
13512 NAME should have one of the representations used for an
13513 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
13514 is returned. If PARSER->SCOPE is a dependent type, then a
13515 SCOPE_REF is returned.
13517 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
13518 returned; the name was already resolved when the TEMPLATE_ID_EXPR
13519 was formed. Abstractly, such entities should not be passed to this
13520 function, because they do not need to be looked up, but it is
13521 simpler to check for this special case here, rather than at the
13524 In cases not explicitly covered above, this function returns a
13525 DECL, OVERLOAD, or baselink representing the result of the lookup.
13526 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
13529 If IS_TYPE is TRUE, bindings that do not refer to types are
13532 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
13535 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
13538 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
13542 cp_parser_lookup_name (cp_parser *parser, tree name,
13543 bool is_type, bool is_template, bool is_namespace,
13544 bool check_dependency)
13547 tree object_type = parser->context->object_type;
13549 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
13550 no longer valid. Note that if we are parsing tentatively, and
13551 the parse fails, OBJECT_TYPE will be automatically restored. */
13552 parser->context->object_type = NULL_TREE;
13554 if (name == error_mark_node)
13555 return error_mark_node;
13557 /* A template-id has already been resolved; there is no lookup to
13559 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
13561 if (BASELINK_P (name))
13563 my_friendly_assert ((TREE_CODE (BASELINK_FUNCTIONS (name))
13564 == TEMPLATE_ID_EXPR),
13569 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
13570 it should already have been checked to make sure that the name
13571 used matches the type being destroyed. */
13572 if (TREE_CODE (name) == BIT_NOT_EXPR)
13576 /* Figure out to which type this destructor applies. */
13578 type = parser->scope;
13579 else if (object_type)
13580 type = object_type;
13582 type = current_class_type;
13583 /* If that's not a class type, there is no destructor. */
13584 if (!type || !CLASS_TYPE_P (type))
13585 return error_mark_node;
13586 if (!CLASSTYPE_DESTRUCTORS (type))
13587 return error_mark_node;
13588 /* If it was a class type, return the destructor. */
13589 return CLASSTYPE_DESTRUCTORS (type);
13592 /* By this point, the NAME should be an ordinary identifier. If
13593 the id-expression was a qualified name, the qualifying scope is
13594 stored in PARSER->SCOPE at this point. */
13595 my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE,
13598 /* Perform the lookup. */
13603 if (parser->scope == error_mark_node)
13604 return error_mark_node;
13606 /* If the SCOPE is dependent, the lookup must be deferred until
13607 the template is instantiated -- unless we are explicitly
13608 looking up names in uninstantiated templates. Even then, we
13609 cannot look up the name if the scope is not a class type; it
13610 might, for example, be a template type parameter. */
13611 dependent_p = (TYPE_P (parser->scope)
13612 && !(parser->in_declarator_p
13613 && currently_open_class (parser->scope))
13614 && dependent_type_p (parser->scope));
13615 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
13619 /* The resolution to Core Issue 180 says that `struct A::B'
13620 should be considered a type-name, even if `A' is
13622 decl = TYPE_NAME (make_typename_type (parser->scope,
13625 else if (is_template)
13626 decl = make_unbound_class_template (parser->scope,
13630 decl = build_nt (SCOPE_REF, parser->scope, name);
13634 /* If PARSER->SCOPE is a dependent type, then it must be a
13635 class type, and we must not be checking dependencies;
13636 otherwise, we would have processed this lookup above. So
13637 that PARSER->SCOPE is not considered a dependent base by
13638 lookup_member, we must enter the scope here. */
13640 push_scope (parser->scope);
13641 /* If the PARSER->SCOPE is a a template specialization, it
13642 may be instantiated during name lookup. In that case,
13643 errors may be issued. Even if we rollback the current
13644 tentative parse, those errors are valid. */
13645 decl = lookup_qualified_name (parser->scope, name, is_type,
13646 /*complain=*/true);
13648 pop_scope (parser->scope);
13650 parser->qualifying_scope = parser->scope;
13651 parser->object_scope = NULL_TREE;
13653 else if (object_type)
13655 tree object_decl = NULL_TREE;
13656 /* Look up the name in the scope of the OBJECT_TYPE, unless the
13657 OBJECT_TYPE is not a class. */
13658 if (CLASS_TYPE_P (object_type))
13659 /* If the OBJECT_TYPE is a template specialization, it may
13660 be instantiated during name lookup. In that case, errors
13661 may be issued. Even if we rollback the current tentative
13662 parse, those errors are valid. */
13663 object_decl = lookup_member (object_type,
13665 /*protect=*/0, is_type);
13666 /* Look it up in the enclosing context, too. */
13667 decl = lookup_name_real (name, is_type, /*nonclass=*/0,
13670 parser->object_scope = object_type;
13671 parser->qualifying_scope = NULL_TREE;
13673 decl = object_decl;
13677 decl = lookup_name_real (name, is_type, /*nonclass=*/0,
13680 parser->qualifying_scope = NULL_TREE;
13681 parser->object_scope = NULL_TREE;
13684 /* If the lookup failed, let our caller know. */
13686 || decl == error_mark_node
13687 || (TREE_CODE (decl) == FUNCTION_DECL
13688 && DECL_ANTICIPATED (decl)))
13689 return error_mark_node;
13691 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
13692 if (TREE_CODE (decl) == TREE_LIST)
13694 /* The error message we have to print is too complicated for
13695 cp_parser_error, so we incorporate its actions directly. */
13696 if (!cp_parser_simulate_error (parser))
13698 error ("reference to `%D' is ambiguous", name);
13699 print_candidates (decl);
13701 return error_mark_node;
13704 my_friendly_assert (DECL_P (decl)
13705 || TREE_CODE (decl) == OVERLOAD
13706 || TREE_CODE (decl) == SCOPE_REF
13707 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
13708 || BASELINK_P (decl),
13711 /* If we have resolved the name of a member declaration, check to
13712 see if the declaration is accessible. When the name resolves to
13713 set of overloaded functions, accessibility is checked when
13714 overload resolution is done.
13716 During an explicit instantiation, access is not checked at all,
13717 as per [temp.explicit]. */
13719 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
13724 /* Like cp_parser_lookup_name, but for use in the typical case where
13725 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
13726 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
13729 cp_parser_lookup_name_simple (cp_parser* parser, tree name)
13731 return cp_parser_lookup_name (parser, name,
13733 /*is_template=*/false,
13734 /*is_namespace=*/false,
13735 /*check_dependency=*/true);
13738 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
13739 the current context, return the TYPE_DECL. If TAG_NAME_P is
13740 true, the DECL indicates the class being defined in a class-head,
13741 or declared in an elaborated-type-specifier.
13743 Otherwise, return DECL. */
13746 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
13748 /* If the TEMPLATE_DECL is being declared as part of a class-head,
13749 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
13752 template <typename T> struct B;
13755 template <typename T> struct A::B {};
13757 Similarly, in a elaborated-type-specifier:
13759 namespace N { struct X{}; }
13762 template <typename T> friend struct N::X;
13765 However, if the DECL refers to a class type, and we are in
13766 the scope of the class, then the name lookup automatically
13767 finds the TYPE_DECL created by build_self_reference rather
13768 than a TEMPLATE_DECL. For example, in:
13770 template <class T> struct S {
13774 there is no need to handle such case. */
13776 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
13777 return DECL_TEMPLATE_RESULT (decl);
13782 /* If too many, or too few, template-parameter lists apply to the
13783 declarator, issue an error message. Returns TRUE if all went well,
13784 and FALSE otherwise. */
13787 cp_parser_check_declarator_template_parameters (cp_parser* parser,
13790 unsigned num_templates;
13792 /* We haven't seen any classes that involve template parameters yet. */
13795 switch (TREE_CODE (declarator))
13802 tree main_declarator = TREE_OPERAND (declarator, 0);
13804 cp_parser_check_declarator_template_parameters (parser,
13813 scope = TREE_OPERAND (declarator, 0);
13814 member = TREE_OPERAND (declarator, 1);
13816 /* If this is a pointer-to-member, then we are not interested
13817 in the SCOPE, because it does not qualify the thing that is
13819 if (TREE_CODE (member) == INDIRECT_REF)
13820 return (cp_parser_check_declarator_template_parameters
13823 while (scope && CLASS_TYPE_P (scope))
13825 /* You're supposed to have one `template <...>'
13826 for every template class, but you don't need one
13827 for a full specialization. For example:
13829 template <class T> struct S{};
13830 template <> struct S<int> { void f(); };
13831 void S<int>::f () {}
13833 is correct; there shouldn't be a `template <>' for
13834 the definition of `S<int>::f'. */
13835 if (CLASSTYPE_TEMPLATE_INFO (scope)
13836 && (CLASSTYPE_TEMPLATE_INSTANTIATION (scope)
13837 || uses_template_parms (CLASSTYPE_TI_ARGS (scope)))
13838 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
13841 scope = TYPE_CONTEXT (scope);
13845 /* Fall through. */
13848 /* If the DECLARATOR has the form `X<y>' then it uses one
13849 additional level of template parameters. */
13850 if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
13853 return cp_parser_check_template_parameters (parser,
13858 /* NUM_TEMPLATES were used in the current declaration. If that is
13859 invalid, return FALSE and issue an error messages. Otherwise,
13863 cp_parser_check_template_parameters (cp_parser* parser,
13864 unsigned num_templates)
13866 /* If there are more template classes than parameter lists, we have
13869 template <class T> void S<T>::R<T>::f (); */
13870 if (parser->num_template_parameter_lists < num_templates)
13872 error ("too few template-parameter-lists");
13875 /* If there are the same number of template classes and parameter
13876 lists, that's OK. */
13877 if (parser->num_template_parameter_lists == num_templates)
13879 /* If there are more, but only one more, then we are referring to a
13880 member template. That's OK too. */
13881 if (parser->num_template_parameter_lists == num_templates + 1)
13883 /* Otherwise, there are too many template parameter lists. We have
13886 template <class T> template <class U> void S::f(); */
13887 error ("too many template-parameter-lists");
13891 /* Parse a binary-expression of the general form:
13895 binary-expression <token> <expr>
13897 The TOKEN_TREE_MAP maps <token> types to <expr> codes. FN is used
13898 to parser the <expr>s. If the first production is used, then the
13899 value returned by FN is returned directly. Otherwise, a node with
13900 the indicated EXPR_TYPE is returned, with operands corresponding to
13901 the two sub-expressions. */
13904 cp_parser_binary_expression (cp_parser* parser,
13905 const cp_parser_token_tree_map token_tree_map,
13906 cp_parser_expression_fn fn)
13910 /* Parse the first expression. */
13911 lhs = (*fn) (parser);
13912 /* Now, look for more expressions. */
13916 const cp_parser_token_tree_map_node *map_node;
13919 /* Peek at the next token. */
13920 token = cp_lexer_peek_token (parser->lexer);
13921 /* If the token is `>', and that's not an operator at the
13922 moment, then we're done. */
13923 if (token->type == CPP_GREATER
13924 && !parser->greater_than_is_operator_p)
13926 /* If we find one of the tokens we want, build the corresponding
13927 tree representation. */
13928 for (map_node = token_tree_map;
13929 map_node->token_type != CPP_EOF;
13931 if (map_node->token_type == token->type)
13933 /* Consume the operator token. */
13934 cp_lexer_consume_token (parser->lexer);
13935 /* Parse the right-hand side of the expression. */
13936 rhs = (*fn) (parser);
13937 /* Build the binary tree node. */
13938 lhs = build_x_binary_op (map_node->tree_type, lhs, rhs);
13942 /* If the token wasn't one of the ones we want, we're done. */
13943 if (map_node->token_type == CPP_EOF)
13950 /* Parse an optional `::' token indicating that the following name is
13951 from the global namespace. If so, PARSER->SCOPE is set to the
13952 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
13953 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
13954 Returns the new value of PARSER->SCOPE, if the `::' token is
13955 present, and NULL_TREE otherwise. */
13958 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
13962 /* Peek at the next token. */
13963 token = cp_lexer_peek_token (parser->lexer);
13964 /* If we're looking at a `::' token then we're starting from the
13965 global namespace, not our current location. */
13966 if (token->type == CPP_SCOPE)
13968 /* Consume the `::' token. */
13969 cp_lexer_consume_token (parser->lexer);
13970 /* Set the SCOPE so that we know where to start the lookup. */
13971 parser->scope = global_namespace;
13972 parser->qualifying_scope = global_namespace;
13973 parser->object_scope = NULL_TREE;
13975 return parser->scope;
13977 else if (!current_scope_valid_p)
13979 parser->scope = NULL_TREE;
13980 parser->qualifying_scope = NULL_TREE;
13981 parser->object_scope = NULL_TREE;
13987 /* Returns TRUE if the upcoming token sequence is the start of a
13988 constructor declarator. If FRIEND_P is true, the declarator is
13989 preceded by the `friend' specifier. */
13992 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
13994 bool constructor_p;
13995 tree type_decl = NULL_TREE;
13996 bool nested_name_p;
13997 cp_token *next_token;
13999 /* The common case is that this is not a constructor declarator, so
14000 try to avoid doing lots of work if at all possible. It's not
14001 valid declare a constructor at function scope. */
14002 if (at_function_scope_p ())
14004 /* And only certain tokens can begin a constructor declarator. */
14005 next_token = cp_lexer_peek_token (parser->lexer);
14006 if (next_token->type != CPP_NAME
14007 && next_token->type != CPP_SCOPE
14008 && next_token->type != CPP_NESTED_NAME_SPECIFIER
14009 && next_token->type != CPP_TEMPLATE_ID)
14012 /* Parse tentatively; we are going to roll back all of the tokens
14014 cp_parser_parse_tentatively (parser);
14015 /* Assume that we are looking at a constructor declarator. */
14016 constructor_p = true;
14018 /* Look for the optional `::' operator. */
14019 cp_parser_global_scope_opt (parser,
14020 /*current_scope_valid_p=*/false);
14021 /* Look for the nested-name-specifier. */
14023 = (cp_parser_nested_name_specifier_opt (parser,
14024 /*typename_keyword_p=*/false,
14025 /*check_dependency_p=*/false,
14027 /*is_declaration=*/false)
14029 /* Outside of a class-specifier, there must be a
14030 nested-name-specifier. */
14031 if (!nested_name_p &&
14032 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
14034 constructor_p = false;
14035 /* If we still think that this might be a constructor-declarator,
14036 look for a class-name. */
14041 template <typename T> struct S { S(); };
14042 template <typename T> S<T>::S ();
14044 we must recognize that the nested `S' names a class.
14047 template <typename T> S<T>::S<T> ();
14049 we must recognize that the nested `S' names a template. */
14050 type_decl = cp_parser_class_name (parser,
14051 /*typename_keyword_p=*/false,
14052 /*template_keyword_p=*/false,
14054 /*check_dependency_p=*/false,
14055 /*class_head_p=*/false,
14056 /*is_declaration=*/false);
14057 /* If there was no class-name, then this is not a constructor. */
14058 constructor_p = !cp_parser_error_occurred (parser);
14061 /* If we're still considering a constructor, we have to see a `(',
14062 to begin the parameter-declaration-clause, followed by either a
14063 `)', an `...', or a decl-specifier. We need to check for a
14064 type-specifier to avoid being fooled into thinking that:
14068 is a constructor. (It is actually a function named `f' that
14069 takes one parameter (of type `int') and returns a value of type
14072 && cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
14074 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
14075 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
14076 && !cp_parser_storage_class_specifier_opt (parser))
14079 unsigned saved_num_template_parameter_lists;
14081 /* Names appearing in the type-specifier should be looked up
14082 in the scope of the class. */
14083 if (current_class_type)
14087 type = TREE_TYPE (type_decl);
14088 if (TREE_CODE (type) == TYPENAME_TYPE)
14090 type = resolve_typename_type (type,
14091 /*only_current_p=*/false);
14092 if (type == error_mark_node)
14094 cp_parser_abort_tentative_parse (parser);
14101 /* Inside the constructor parameter list, surrounding
14102 template-parameter-lists do not apply. */
14103 saved_num_template_parameter_lists
14104 = parser->num_template_parameter_lists;
14105 parser->num_template_parameter_lists = 0;
14107 /* Look for the type-specifier. */
14108 cp_parser_type_specifier (parser,
14109 CP_PARSER_FLAGS_NONE,
14110 /*is_friend=*/false,
14111 /*is_declarator=*/true,
14112 /*declares_class_or_enum=*/NULL,
14113 /*is_cv_qualifier=*/NULL);
14115 parser->num_template_parameter_lists
14116 = saved_num_template_parameter_lists;
14118 /* Leave the scope of the class. */
14122 constructor_p = !cp_parser_error_occurred (parser);
14126 constructor_p = false;
14127 /* We did not really want to consume any tokens. */
14128 cp_parser_abort_tentative_parse (parser);
14130 return constructor_p;
14133 /* Parse the definition of the function given by the DECL_SPECIFIERS,
14134 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
14135 they must be performed once we are in the scope of the function.
14137 Returns the function defined. */
14140 cp_parser_function_definition_from_specifiers_and_declarator
14141 (cp_parser* parser,
14142 tree decl_specifiers,
14149 /* Begin the function-definition. */
14150 success_p = begin_function_definition (decl_specifiers,
14154 /* If there were names looked up in the decl-specifier-seq that we
14155 did not check, check them now. We must wait until we are in the
14156 scope of the function to perform the checks, since the function
14157 might be a friend. */
14158 perform_deferred_access_checks ();
14162 /* If begin_function_definition didn't like the definition, skip
14163 the entire function. */
14164 error ("invalid function declaration");
14165 cp_parser_skip_to_end_of_block_or_statement (parser);
14166 fn = error_mark_node;
14169 fn = cp_parser_function_definition_after_declarator (parser,
14170 /*inline_p=*/false);
14175 /* Parse the part of a function-definition that follows the
14176 declarator. INLINE_P is TRUE iff this function is an inline
14177 function defined with a class-specifier.
14179 Returns the function defined. */
14182 cp_parser_function_definition_after_declarator (cp_parser* parser,
14186 bool ctor_initializer_p = false;
14187 bool saved_in_unbraced_linkage_specification_p;
14188 unsigned saved_num_template_parameter_lists;
14190 /* If the next token is `return', then the code may be trying to
14191 make use of the "named return value" extension that G++ used to
14193 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
14195 /* Consume the `return' keyword. */
14196 cp_lexer_consume_token (parser->lexer);
14197 /* Look for the identifier that indicates what value is to be
14199 cp_parser_identifier (parser);
14200 /* Issue an error message. */
14201 error ("named return values are no longer supported");
14202 /* Skip tokens until we reach the start of the function body. */
14203 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
14204 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
14205 cp_lexer_consume_token (parser->lexer);
14207 /* The `extern' in `extern "C" void f () { ... }' does not apply to
14208 anything declared inside `f'. */
14209 saved_in_unbraced_linkage_specification_p
14210 = parser->in_unbraced_linkage_specification_p;
14211 parser->in_unbraced_linkage_specification_p = false;
14212 /* Inside the function, surrounding template-parameter-lists do not
14214 saved_num_template_parameter_lists
14215 = parser->num_template_parameter_lists;
14216 parser->num_template_parameter_lists = 0;
14217 /* If the next token is `try', then we are looking at a
14218 function-try-block. */
14219 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
14220 ctor_initializer_p = cp_parser_function_try_block (parser);
14221 /* A function-try-block includes the function-body, so we only do
14222 this next part if we're not processing a function-try-block. */
14225 = cp_parser_ctor_initializer_opt_and_function_body (parser);
14227 /* Finish the function. */
14228 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
14229 (inline_p ? 2 : 0));
14230 /* Generate code for it, if necessary. */
14231 expand_or_defer_fn (fn);
14232 /* Restore the saved values. */
14233 parser->in_unbraced_linkage_specification_p
14234 = saved_in_unbraced_linkage_specification_p;
14235 parser->num_template_parameter_lists
14236 = saved_num_template_parameter_lists;
14241 /* Parse a template-declaration, assuming that the `export' (and
14242 `extern') keywords, if present, has already been scanned. MEMBER_P
14243 is as for cp_parser_template_declaration. */
14246 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
14248 tree decl = NULL_TREE;
14249 tree parameter_list;
14250 bool friend_p = false;
14252 /* Look for the `template' keyword. */
14253 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'"))
14257 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
14260 /* If the next token is `>', then we have an invalid
14261 specialization. Rather than complain about an invalid template
14262 parameter, issue an error message here. */
14263 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
14265 cp_parser_error (parser, "invalid explicit specialization");
14266 begin_specialization ();
14267 parameter_list = NULL_TREE;
14271 /* Parse the template parameters. */
14272 begin_template_parm_list ();
14273 parameter_list = cp_parser_template_parameter_list (parser);
14274 parameter_list = end_template_parm_list (parameter_list);
14277 /* Look for the `>'. */
14278 cp_parser_skip_until_found (parser, CPP_GREATER, "`>'");
14279 /* We just processed one more parameter list. */
14280 ++parser->num_template_parameter_lists;
14281 /* If the next token is `template', there are more template
14283 if (cp_lexer_next_token_is_keyword (parser->lexer,
14285 cp_parser_template_declaration_after_export (parser, member_p);
14288 decl = cp_parser_single_declaration (parser,
14292 /* If this is a member template declaration, let the front
14294 if (member_p && !friend_p && decl)
14296 if (TREE_CODE (decl) == TYPE_DECL)
14297 cp_parser_check_access_in_redeclaration (decl);
14299 decl = finish_member_template_decl (decl);
14301 else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
14302 make_friend_class (current_class_type, TREE_TYPE (decl),
14303 /*complain=*/true);
14305 /* We are done with the current parameter list. */
14306 --parser->num_template_parameter_lists;
14309 finish_template_decl (parameter_list);
14311 /* Register member declarations. */
14312 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
14313 finish_member_declaration (decl);
14315 /* If DECL is a function template, we must return to parse it later.
14316 (Even though there is no definition, there might be default
14317 arguments that need handling.) */
14318 if (member_p && decl
14319 && (TREE_CODE (decl) == FUNCTION_DECL
14320 || DECL_FUNCTION_TEMPLATE_P (decl)))
14321 TREE_VALUE (parser->unparsed_functions_queues)
14322 = tree_cons (NULL_TREE, decl,
14323 TREE_VALUE (parser->unparsed_functions_queues));
14326 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
14327 `function-definition' sequence. MEMBER_P is true, this declaration
14328 appears in a class scope.
14330 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
14331 *FRIEND_P is set to TRUE iff the declaration is a friend. */
14334 cp_parser_single_declaration (cp_parser* parser,
14338 int declares_class_or_enum;
14339 tree decl = NULL_TREE;
14340 tree decl_specifiers;
14342 bool function_definition_p = false;
14344 /* Defer access checks until we know what is being declared. */
14345 push_deferring_access_checks (dk_deferred);
14347 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
14350 = cp_parser_decl_specifier_seq (parser,
14351 CP_PARSER_FLAGS_OPTIONAL,
14353 &declares_class_or_enum);
14355 *friend_p = cp_parser_friend_p (decl_specifiers);
14356 /* Gather up the access checks that occurred the
14357 decl-specifier-seq. */
14358 stop_deferring_access_checks ();
14360 /* Check for the declaration of a template class. */
14361 if (declares_class_or_enum)
14363 if (cp_parser_declares_only_class_p (parser))
14365 decl = shadow_tag (decl_specifiers);
14367 decl = TYPE_NAME (decl);
14369 decl = error_mark_node;
14374 /* If it's not a template class, try for a template function. If
14375 the next token is a `;', then this declaration does not declare
14376 anything. But, if there were errors in the decl-specifiers, then
14377 the error might well have come from an attempted class-specifier.
14378 In that case, there's no need to warn about a missing declarator. */
14380 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
14381 || !value_member (error_mark_node, decl_specifiers)))
14382 decl = cp_parser_init_declarator (parser,
14385 /*function_definition_allowed_p=*/true,
14387 declares_class_or_enum,
14388 &function_definition_p);
14390 pop_deferring_access_checks ();
14392 /* Clear any current qualification; whatever comes next is the start
14393 of something new. */
14394 parser->scope = NULL_TREE;
14395 parser->qualifying_scope = NULL_TREE;
14396 parser->object_scope = NULL_TREE;
14397 /* Look for a trailing `;' after the declaration. */
14398 if (!function_definition_p
14399 && !cp_parser_require (parser, CPP_SEMICOLON, "`;'"))
14400 cp_parser_skip_to_end_of_block_or_statement (parser);
14405 /* Parse a cast-expression that is not the operand of a unary "&". */
14408 cp_parser_simple_cast_expression (cp_parser *parser)
14410 return cp_parser_cast_expression (parser, /*address_p=*/false);
14413 /* Parse a functional cast to TYPE. Returns an expression
14414 representing the cast. */
14417 cp_parser_functional_cast (cp_parser* parser, tree type)
14419 tree expression_list;
14422 = cp_parser_parenthesized_expression_list (parser, false,
14423 /*non_constant_p=*/NULL);
14425 return build_functional_cast (type, expression_list);
14428 /* Save the tokens that make up the body of a member function defined
14429 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
14430 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
14431 specifiers applied to the declaration. Returns the FUNCTION_DECL
14432 for the member function. */
14435 cp_parser_save_member_function_body (cp_parser* parser,
14436 tree decl_specifiers,
14440 cp_token_cache *cache;
14443 /* Create the function-declaration. */
14444 fn = start_method (decl_specifiers, declarator, attributes);
14445 /* If something went badly wrong, bail out now. */
14446 if (fn == error_mark_node)
14448 /* If there's a function-body, skip it. */
14449 if (cp_parser_token_starts_function_definition_p
14450 (cp_lexer_peek_token (parser->lexer)))
14451 cp_parser_skip_to_end_of_block_or_statement (parser);
14452 return error_mark_node;
14455 /* Remember it, if there default args to post process. */
14456 cp_parser_save_default_args (parser, fn);
14458 /* Create a token cache. */
14459 cache = cp_token_cache_new ();
14460 /* Save away the tokens that make up the body of the
14462 cp_parser_cache_group (parser, cache, CPP_CLOSE_BRACE, /*depth=*/0);
14463 /* Handle function try blocks. */
14464 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
14465 cp_parser_cache_group (parser, cache, CPP_CLOSE_BRACE, /*depth=*/0);
14467 /* Save away the inline definition; we will process it when the
14468 class is complete. */
14469 DECL_PENDING_INLINE_INFO (fn) = cache;
14470 DECL_PENDING_INLINE_P (fn) = 1;
14472 /* We need to know that this was defined in the class, so that
14473 friend templates are handled correctly. */
14474 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
14476 /* We're done with the inline definition. */
14477 finish_method (fn);
14479 /* Add FN to the queue of functions to be parsed later. */
14480 TREE_VALUE (parser->unparsed_functions_queues)
14481 = tree_cons (NULL_TREE, fn,
14482 TREE_VALUE (parser->unparsed_functions_queues));
14487 /* Parse a template-argument-list, as well as the trailing ">" (but
14488 not the opening ">"). See cp_parser_template_argument_list for the
14492 cp_parser_enclosed_template_argument_list (cp_parser* parser)
14496 tree saved_qualifying_scope;
14497 tree saved_object_scope;
14498 bool saved_greater_than_is_operator_p;
14502 When parsing a template-id, the first non-nested `>' is taken as
14503 the end of the template-argument-list rather than a greater-than
14505 saved_greater_than_is_operator_p
14506 = parser->greater_than_is_operator_p;
14507 parser->greater_than_is_operator_p = false;
14508 /* Parsing the argument list may modify SCOPE, so we save it
14510 saved_scope = parser->scope;
14511 saved_qualifying_scope = parser->qualifying_scope;
14512 saved_object_scope = parser->object_scope;
14513 /* Parse the template-argument-list itself. */
14514 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
14515 arguments = NULL_TREE;
14517 arguments = cp_parser_template_argument_list (parser);
14518 /* Look for the `>' that ends the template-argument-list. If we find
14519 a '>>' instead, it's probably just a typo. */
14520 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
14522 if (!saved_greater_than_is_operator_p)
14524 /* If we're in a nested template argument list, the '>>' has to be
14525 a typo for '> >'. We emit the error message, but we continue
14526 parsing and we push a '>' as next token, so that the argument
14527 list will be parsed correctly.. */
14529 error ("`>>' should be `> >' within a nested template argument list");
14530 token = cp_lexer_peek_token (parser->lexer);
14531 token->type = CPP_GREATER;
14535 /* If this is not a nested template argument list, the '>>' is
14536 a typo for '>'. Emit an error message and continue. */
14537 error ("spurious `>>', use `>' to terminate a template argument list");
14538 cp_lexer_consume_token (parser->lexer);
14541 else if (!cp_parser_require (parser, CPP_GREATER, "`>'"))
14542 error ("missing `>' to terminate the template argument list");
14543 /* The `>' token might be a greater-than operator again now. */
14544 parser->greater_than_is_operator_p
14545 = saved_greater_than_is_operator_p;
14546 /* Restore the SAVED_SCOPE. */
14547 parser->scope = saved_scope;
14548 parser->qualifying_scope = saved_qualifying_scope;
14549 parser->object_scope = saved_object_scope;
14554 /* MEMBER_FUNCTION is a member function, or a friend. If default
14555 arguments, or the body of the function have not yet been parsed,
14559 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
14561 cp_lexer *saved_lexer;
14563 /* If this member is a template, get the underlying
14565 if (DECL_FUNCTION_TEMPLATE_P (member_function))
14566 member_function = DECL_TEMPLATE_RESULT (member_function);
14568 /* There should not be any class definitions in progress at this
14569 point; the bodies of members are only parsed outside of all class
14571 my_friendly_assert (parser->num_classes_being_defined == 0, 20010816);
14572 /* While we're parsing the member functions we might encounter more
14573 classes. We want to handle them right away, but we don't want
14574 them getting mixed up with functions that are currently in the
14576 parser->unparsed_functions_queues
14577 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
14579 /* Make sure that any template parameters are in scope. */
14580 maybe_begin_member_template_processing (member_function);
14582 /* If the body of the function has not yet been parsed, parse it
14584 if (DECL_PENDING_INLINE_P (member_function))
14586 tree function_scope;
14587 cp_token_cache *tokens;
14589 /* The function is no longer pending; we are processing it. */
14590 tokens = DECL_PENDING_INLINE_INFO (member_function);
14591 DECL_PENDING_INLINE_INFO (member_function) = NULL;
14592 DECL_PENDING_INLINE_P (member_function) = 0;
14593 /* If this was an inline function in a local class, enter the scope
14594 of the containing function. */
14595 function_scope = decl_function_context (member_function);
14596 if (function_scope)
14597 push_function_context_to (function_scope);
14599 /* Save away the current lexer. */
14600 saved_lexer = parser->lexer;
14601 /* Make a new lexer to feed us the tokens saved for this function. */
14602 parser->lexer = cp_lexer_new_from_tokens (tokens);
14603 parser->lexer->next = saved_lexer;
14605 /* Set the current source position to be the location of the first
14606 token in the saved inline body. */
14607 cp_lexer_peek_token (parser->lexer);
14609 /* Let the front end know that we going to be defining this
14611 start_function (NULL_TREE, member_function, NULL_TREE,
14612 SF_PRE_PARSED | SF_INCLASS_INLINE);
14614 /* Now, parse the body of the function. */
14615 cp_parser_function_definition_after_declarator (parser,
14616 /*inline_p=*/true);
14618 /* Leave the scope of the containing function. */
14619 if (function_scope)
14620 pop_function_context_from (function_scope);
14621 /* Restore the lexer. */
14622 parser->lexer = saved_lexer;
14625 /* Remove any template parameters from the symbol table. */
14626 maybe_end_member_template_processing ();
14628 /* Restore the queue. */
14629 parser->unparsed_functions_queues
14630 = TREE_CHAIN (parser->unparsed_functions_queues);
14633 /* If DECL contains any default args, remember it on the unparsed
14634 functions queue. */
14637 cp_parser_save_default_args (cp_parser* parser, tree decl)
14641 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
14643 probe = TREE_CHAIN (probe))
14644 if (TREE_PURPOSE (probe))
14646 TREE_PURPOSE (parser->unparsed_functions_queues)
14647 = tree_cons (NULL_TREE, decl,
14648 TREE_PURPOSE (parser->unparsed_functions_queues));
14654 /* FN is a FUNCTION_DECL which may contains a parameter with an
14655 unparsed DEFAULT_ARG. Parse the default args now. */
14658 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
14660 cp_lexer *saved_lexer;
14661 cp_token_cache *tokens;
14662 bool saved_local_variables_forbidden_p;
14665 /* While we're parsing the default args, we might (due to the
14666 statement expression extension) encounter more classes. We want
14667 to handle them right away, but we don't want them getting mixed
14668 up with default args that are currently in the queue. */
14669 parser->unparsed_functions_queues
14670 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
14672 for (parameters = TYPE_ARG_TYPES (TREE_TYPE (fn));
14674 parameters = TREE_CHAIN (parameters))
14676 if (!TREE_PURPOSE (parameters)
14677 || TREE_CODE (TREE_PURPOSE (parameters)) != DEFAULT_ARG)
14680 /* Save away the current lexer. */
14681 saved_lexer = parser->lexer;
14682 /* Create a new one, using the tokens we have saved. */
14683 tokens = DEFARG_TOKENS (TREE_PURPOSE (parameters));
14684 parser->lexer = cp_lexer_new_from_tokens (tokens);
14686 /* Set the current source position to be the location of the
14687 first token in the default argument. */
14688 cp_lexer_peek_token (parser->lexer);
14690 /* Local variable names (and the `this' keyword) may not appear
14691 in a default argument. */
14692 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
14693 parser->local_variables_forbidden_p = true;
14694 /* Parse the assignment-expression. */
14695 if (DECL_CLASS_SCOPE_P (fn))
14696 push_nested_class (DECL_CONTEXT (fn));
14697 TREE_PURPOSE (parameters) = cp_parser_assignment_expression (parser);
14698 if (DECL_CLASS_SCOPE_P (fn))
14699 pop_nested_class ();
14701 /* Restore saved state. */
14702 parser->lexer = saved_lexer;
14703 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
14706 /* Restore the queue. */
14707 parser->unparsed_functions_queues
14708 = TREE_CHAIN (parser->unparsed_functions_queues);
14711 /* Parse the operand of `sizeof' (or a similar operator). Returns
14712 either a TYPE or an expression, depending on the form of the
14713 input. The KEYWORD indicates which kind of expression we have
14717 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
14719 static const char *format;
14720 tree expr = NULL_TREE;
14721 const char *saved_message;
14722 bool saved_integral_constant_expression_p;
14724 /* Initialize FORMAT the first time we get here. */
14726 format = "types may not be defined in `%s' expressions";
14728 /* Types cannot be defined in a `sizeof' expression. Save away the
14730 saved_message = parser->type_definition_forbidden_message;
14731 /* And create the new one. */
14732 parser->type_definition_forbidden_message
14733 = xmalloc (strlen (format)
14734 + strlen (IDENTIFIER_POINTER (ridpointers[keyword]))
14736 sprintf ((char *) parser->type_definition_forbidden_message,
14737 format, IDENTIFIER_POINTER (ridpointers[keyword]));
14739 /* The restrictions on constant-expressions do not apply inside
14740 sizeof expressions. */
14741 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
14742 parser->integral_constant_expression_p = false;
14744 /* Do not actually evaluate the expression. */
14746 /* If it's a `(', then we might be looking at the type-id
14748 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
14751 bool saved_in_type_id_in_expr_p;
14753 /* We can't be sure yet whether we're looking at a type-id or an
14755 cp_parser_parse_tentatively (parser);
14756 /* Consume the `('. */
14757 cp_lexer_consume_token (parser->lexer);
14758 /* Parse the type-id. */
14759 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
14760 parser->in_type_id_in_expr_p = true;
14761 type = cp_parser_type_id (parser);
14762 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
14763 /* Now, look for the trailing `)'. */
14764 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14765 /* If all went well, then we're done. */
14766 if (cp_parser_parse_definitely (parser))
14768 /* Build a list of decl-specifiers; right now, we have only
14769 a single type-specifier. */
14770 type = build_tree_list (NULL_TREE,
14773 /* Call grokdeclarator to figure out what type this is. */
14774 expr = grokdeclarator (NULL_TREE,
14778 /*attrlist=*/NULL);
14782 /* If the type-id production did not work out, then we must be
14783 looking at the unary-expression production. */
14785 expr = cp_parser_unary_expression (parser, /*address_p=*/false);
14786 /* Go back to evaluating expressions. */
14789 /* Free the message we created. */
14790 free ((char *) parser->type_definition_forbidden_message);
14791 /* And restore the old one. */
14792 parser->type_definition_forbidden_message = saved_message;
14793 parser->integral_constant_expression_p = saved_integral_constant_expression_p;
14798 /* If the current declaration has no declarator, return true. */
14801 cp_parser_declares_only_class_p (cp_parser *parser)
14803 /* If the next token is a `;' or a `,' then there is no
14805 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
14806 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
14809 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
14810 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
14813 cp_parser_friend_p (tree decl_specifiers)
14815 while (decl_specifiers)
14817 /* See if this decl-specifier is `friend'. */
14818 if (TREE_CODE (TREE_VALUE (decl_specifiers)) == IDENTIFIER_NODE
14819 && C_RID_CODE (TREE_VALUE (decl_specifiers)) == RID_FRIEND)
14822 /* Go on to the next decl-specifier. */
14823 decl_specifiers = TREE_CHAIN (decl_specifiers);
14829 /* If the next token is of the indicated TYPE, consume it. Otherwise,
14830 issue an error message indicating that TOKEN_DESC was expected.
14832 Returns the token consumed, if the token had the appropriate type.
14833 Otherwise, returns NULL. */
14836 cp_parser_require (cp_parser* parser,
14837 enum cpp_ttype type,
14838 const char* token_desc)
14840 if (cp_lexer_next_token_is (parser->lexer, type))
14841 return cp_lexer_consume_token (parser->lexer);
14844 /* Output the MESSAGE -- unless we're parsing tentatively. */
14845 if (!cp_parser_simulate_error (parser))
14847 char *message = concat ("expected ", token_desc, NULL);
14848 cp_parser_error (parser, message);
14855 /* Like cp_parser_require, except that tokens will be skipped until
14856 the desired token is found. An error message is still produced if
14857 the next token is not as expected. */
14860 cp_parser_skip_until_found (cp_parser* parser,
14861 enum cpp_ttype type,
14862 const char* token_desc)
14865 unsigned nesting_depth = 0;
14867 if (cp_parser_require (parser, type, token_desc))
14870 /* Skip tokens until the desired token is found. */
14873 /* Peek at the next token. */
14874 token = cp_lexer_peek_token (parser->lexer);
14875 /* If we've reached the token we want, consume it and
14877 if (token->type == type && !nesting_depth)
14879 cp_lexer_consume_token (parser->lexer);
14882 /* If we've run out of tokens, stop. */
14883 if (token->type == CPP_EOF)
14885 if (token->type == CPP_OPEN_BRACE
14886 || token->type == CPP_OPEN_PAREN
14887 || token->type == CPP_OPEN_SQUARE)
14889 else if (token->type == CPP_CLOSE_BRACE
14890 || token->type == CPP_CLOSE_PAREN
14891 || token->type == CPP_CLOSE_SQUARE)
14893 if (nesting_depth-- == 0)
14896 /* Consume this token. */
14897 cp_lexer_consume_token (parser->lexer);
14901 /* If the next token is the indicated keyword, consume it. Otherwise,
14902 issue an error message indicating that TOKEN_DESC was expected.
14904 Returns the token consumed, if the token had the appropriate type.
14905 Otherwise, returns NULL. */
14908 cp_parser_require_keyword (cp_parser* parser,
14910 const char* token_desc)
14912 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
14914 if (token && token->keyword != keyword)
14916 dyn_string_t error_msg;
14918 /* Format the error message. */
14919 error_msg = dyn_string_new (0);
14920 dyn_string_append_cstr (error_msg, "expected ");
14921 dyn_string_append_cstr (error_msg, token_desc);
14922 cp_parser_error (parser, error_msg->s);
14923 dyn_string_delete (error_msg);
14930 /* Returns TRUE iff TOKEN is a token that can begin the body of a
14931 function-definition. */
14934 cp_parser_token_starts_function_definition_p (cp_token* token)
14936 return (/* An ordinary function-body begins with an `{'. */
14937 token->type == CPP_OPEN_BRACE
14938 /* A ctor-initializer begins with a `:'. */
14939 || token->type == CPP_COLON
14940 /* A function-try-block begins with `try'. */
14941 || token->keyword == RID_TRY
14942 /* The named return value extension begins with `return'. */
14943 || token->keyword == RID_RETURN);
14946 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
14950 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
14954 token = cp_lexer_peek_token (parser->lexer);
14955 return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
14958 /* Returns TRUE iff the next token is the "," or ">" ending a
14959 template-argument. ">>" is also accepted (after the full
14960 argument was parsed) because it's probably a typo for "> >",
14961 and there is a specific diagnostic for this. */
14964 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
14968 token = cp_lexer_peek_token (parser->lexer);
14969 return (token->type == CPP_COMMA || token->type == CPP_GREATER
14970 || token->type == CPP_RSHIFT);
14973 /* Returns TRUE iff the n-th token is a ">", or the n-th is a "[" and the
14974 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
14977 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
14982 token = cp_lexer_peek_nth_token (parser->lexer, n);
14983 if (token->type == CPP_LESS)
14985 /* Check for the sequence `<::' in the original code. It would be lexed as
14986 `[:', where `[' is a digraph, and there is no whitespace before
14988 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
14991 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
14992 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
14998 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
14999 or none_type otherwise. */
15001 static enum tag_types
15002 cp_parser_token_is_class_key (cp_token* token)
15004 switch (token->keyword)
15009 return record_type;
15018 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
15021 cp_parser_check_class_key (enum tag_types class_key, tree type)
15023 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
15024 pedwarn ("`%s' tag used in naming `%#T'",
15025 class_key == union_type ? "union"
15026 : class_key == record_type ? "struct" : "class",
15030 /* Issue an error message if DECL is redeclared with different
15031 access than its original declaration [class.access.spec/3].
15032 This applies to nested classes and nested class templates.
15035 static void cp_parser_check_access_in_redeclaration (tree decl)
15037 if (!CLASS_TYPE_P (TREE_TYPE (decl)))
15040 if ((TREE_PRIVATE (decl)
15041 != (current_access_specifier == access_private_node))
15042 || (TREE_PROTECTED (decl)
15043 != (current_access_specifier == access_protected_node)))
15044 error ("%D redeclared with different access", decl);
15047 /* Look for the `template' keyword, as a syntactic disambiguator.
15048 Return TRUE iff it is present, in which case it will be
15052 cp_parser_optional_template_keyword (cp_parser *parser)
15054 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
15056 /* The `template' keyword can only be used within templates;
15057 outside templates the parser can always figure out what is a
15058 template and what is not. */
15059 if (!processing_template_decl)
15061 error ("`template' (as a disambiguator) is only allowed "
15062 "within templates");
15063 /* If this part of the token stream is rescanned, the same
15064 error message would be generated. So, we purge the token
15065 from the stream. */
15066 cp_lexer_purge_token (parser->lexer);
15071 /* Consume the `template' keyword. */
15072 cp_lexer_consume_token (parser->lexer);
15080 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
15081 set PARSER->SCOPE, and perform other related actions. */
15084 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
15089 /* Get the stored value. */
15090 value = cp_lexer_consume_token (parser->lexer)->value;
15091 /* Perform any access checks that were deferred. */
15092 for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
15093 perform_or_defer_access_check (TREE_PURPOSE (check), TREE_VALUE (check));
15094 /* Set the scope from the stored value. */
15095 parser->scope = TREE_VALUE (value);
15096 parser->qualifying_scope = TREE_TYPE (value);
15097 parser->object_scope = NULL_TREE;
15100 /* Add tokens to CACHE until a non-nested END token appears. */
15103 cp_parser_cache_group (cp_parser *parser,
15104 cp_token_cache *cache,
15105 enum cpp_ttype end,
15112 /* Abort a parenthesized expression if we encounter a brace. */
15113 if ((end == CPP_CLOSE_PAREN || depth == 0)
15114 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
15116 /* If we've reached the end of the file, stop. */
15117 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
15119 /* Consume the next token. */
15120 token = cp_lexer_consume_token (parser->lexer);
15121 /* Add this token to the tokens we are saving. */
15122 cp_token_cache_push_token (cache, token);
15123 /* See if it starts a new group. */
15124 if (token->type == CPP_OPEN_BRACE)
15126 cp_parser_cache_group (parser, cache, CPP_CLOSE_BRACE, depth + 1);
15130 else if (token->type == CPP_OPEN_PAREN)
15131 cp_parser_cache_group (parser, cache, CPP_CLOSE_PAREN, depth + 1);
15132 else if (token->type == end)
15137 /* Begin parsing tentatively. We always save tokens while parsing
15138 tentatively so that if the tentative parsing fails we can restore the
15142 cp_parser_parse_tentatively (cp_parser* parser)
15144 /* Enter a new parsing context. */
15145 parser->context = cp_parser_context_new (parser->context);
15146 /* Begin saving tokens. */
15147 cp_lexer_save_tokens (parser->lexer);
15148 /* In order to avoid repetitive access control error messages,
15149 access checks are queued up until we are no longer parsing
15151 push_deferring_access_checks (dk_deferred);
15154 /* Commit to the currently active tentative parse. */
15157 cp_parser_commit_to_tentative_parse (cp_parser* parser)
15159 cp_parser_context *context;
15162 /* Mark all of the levels as committed. */
15163 lexer = parser->lexer;
15164 for (context = parser->context; context->next; context = context->next)
15166 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
15168 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
15169 while (!cp_lexer_saving_tokens (lexer))
15170 lexer = lexer->next;
15171 cp_lexer_commit_tokens (lexer);
15175 /* Abort the currently active tentative parse. All consumed tokens
15176 will be rolled back, and no diagnostics will be issued. */
15179 cp_parser_abort_tentative_parse (cp_parser* parser)
15181 cp_parser_simulate_error (parser);
15182 /* Now, pretend that we want to see if the construct was
15183 successfully parsed. */
15184 cp_parser_parse_definitely (parser);
15187 /* Stop parsing tentatively. If a parse error has occurred, restore the
15188 token stream. Otherwise, commit to the tokens we have consumed.
15189 Returns true if no error occurred; false otherwise. */
15192 cp_parser_parse_definitely (cp_parser* parser)
15194 bool error_occurred;
15195 cp_parser_context *context;
15197 /* Remember whether or not an error occurred, since we are about to
15198 destroy that information. */
15199 error_occurred = cp_parser_error_occurred (parser);
15200 /* Remove the topmost context from the stack. */
15201 context = parser->context;
15202 parser->context = context->next;
15203 /* If no parse errors occurred, commit to the tentative parse. */
15204 if (!error_occurred)
15206 /* Commit to the tokens read tentatively, unless that was
15208 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
15209 cp_lexer_commit_tokens (parser->lexer);
15211 pop_to_parent_deferring_access_checks ();
15213 /* Otherwise, if errors occurred, roll back our state so that things
15214 are just as they were before we began the tentative parse. */
15217 cp_lexer_rollback_tokens (parser->lexer);
15218 pop_deferring_access_checks ();
15220 /* Add the context to the front of the free list. */
15221 context->next = cp_parser_context_free_list;
15222 cp_parser_context_free_list = context;
15224 return !error_occurred;
15227 /* Returns true if we are parsing tentatively -- but have decided that
15228 we will stick with this tentative parse, even if errors occur. */
15231 cp_parser_committed_to_tentative_parse (cp_parser* parser)
15233 return (cp_parser_parsing_tentatively (parser)
15234 && parser->context->status == CP_PARSER_STATUS_KIND_COMMITTED);
15237 /* Returns nonzero iff an error has occurred during the most recent
15238 tentative parse. */
15241 cp_parser_error_occurred (cp_parser* parser)
15243 return (cp_parser_parsing_tentatively (parser)
15244 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
15247 /* Returns nonzero if GNU extensions are allowed. */
15250 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
15252 return parser->allow_gnu_extensions_p;
15259 static GTY (()) cp_parser *the_parser;
15261 /* External interface. */
15263 /* Parse one entire translation unit. */
15266 c_parse_file (void)
15268 bool error_occurred;
15270 the_parser = cp_parser_new ();
15271 push_deferring_access_checks (flag_access_control
15272 ? dk_no_deferred : dk_no_check);
15273 error_occurred = cp_parser_translation_unit (the_parser);
15277 /* This variable must be provided by every front end. */
15281 #include "gt-cp-parser.h"