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 message should be issued by the caller. */
1830 cp_parser_simulate_error (cp_parser* parser)
1832 if (cp_parser_parsing_tentatively (parser)
1833 && !cp_parser_committed_to_tentative_parse (parser))
1835 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
1841 /* This function is called when a type is defined. If type
1842 definitions are forbidden at this point, an error message is
1846 cp_parser_check_type_definition (cp_parser* parser)
1848 /* If types are forbidden here, issue a message. */
1849 if (parser->type_definition_forbidden_message)
1850 /* Use `%s' to print the string in case there are any escape
1851 characters in the message. */
1852 error ("%s", parser->type_definition_forbidden_message);
1855 /* This function is called when a declaration is parsed. If
1856 DECLARATOR is a function declarator and DECLARES_CLASS_OR_ENUM
1857 indicates that a type was defined in the decl-specifiers for DECL,
1858 then an error is issued. */
1861 cp_parser_check_for_definition_in_return_type (tree declarator,
1862 int declares_class_or_enum)
1864 /* [dcl.fct] forbids type definitions in return types.
1865 Unfortunately, it's not easy to know whether or not we are
1866 processing a return type until after the fact. */
1868 && (TREE_CODE (declarator) == INDIRECT_REF
1869 || TREE_CODE (declarator) == ADDR_EXPR))
1870 declarator = TREE_OPERAND (declarator, 0);
1872 && TREE_CODE (declarator) == CALL_EXPR
1873 && declares_class_or_enum & 2)
1874 error ("new types may not be defined in a return type");
1877 /* A type-specifier (TYPE) has been parsed which cannot be followed by
1878 "<" in any valid C++ program. If the next token is indeed "<",
1879 issue a message warning the user about what appears to be an
1880 invalid attempt to form a template-id. */
1883 cp_parser_check_for_invalid_template_id (cp_parser* parser,
1889 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
1892 error ("`%T' is not a template", type);
1893 else if (TREE_CODE (type) == IDENTIFIER_NODE)
1894 error ("`%s' is not a template", IDENTIFIER_POINTER (type));
1896 error ("invalid template-id");
1897 /* Remember the location of the invalid "<". */
1898 if (cp_parser_parsing_tentatively (parser)
1899 && !cp_parser_committed_to_tentative_parse (parser))
1901 token = cp_lexer_peek_token (parser->lexer);
1902 token = cp_lexer_prev_token (parser->lexer, token);
1903 start = cp_lexer_token_difference (parser->lexer,
1904 parser->lexer->first_token,
1909 /* Consume the "<". */
1910 cp_lexer_consume_token (parser->lexer);
1911 /* Parse the template arguments. */
1912 cp_parser_enclosed_template_argument_list (parser);
1913 /* Permanently remove the invalid template arguments so that
1914 this error message is not issued again. */
1917 token = cp_lexer_advance_token (parser->lexer,
1918 parser->lexer->first_token,
1920 cp_lexer_purge_tokens_after (parser->lexer, token);
1925 /* 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)
3813 /* We do not perform argument-dependent lookup if
3814 normal lookup finds a non-function, in accordance
3815 with the expected resolution of DR 218. */
3817 && (is_overloaded_fn (postfix_expression)
3818 || TREE_CODE (postfix_expression) == IDENTIFIER_NODE))
3822 = perform_koenig_lookup (postfix_expression, args);
3824 else if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
3826 = unqualified_fn_lookup_error (postfix_expression);
3829 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
3831 tree instance = TREE_OPERAND (postfix_expression, 0);
3832 tree fn = TREE_OPERAND (postfix_expression, 1);
3834 if (processing_template_decl
3835 && (type_dependent_expression_p (instance)
3836 || (!BASELINK_P (fn)
3837 && TREE_CODE (fn) != FIELD_DECL)
3838 || type_dependent_expression_p (fn)
3839 || any_type_dependent_arguments_p (args)))
3842 = build_min_nt (CALL_EXPR, postfix_expression, args);
3846 if (BASELINK_P (fn))
3848 = (build_new_method_call
3849 (instance, fn, args, NULL_TREE,
3850 (idk == CP_ID_KIND_QUALIFIED
3851 ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL)));
3854 = finish_call_expr (postfix_expression, args,
3855 /*disallow_virtual=*/false,
3856 /*koenig_p=*/false);
3858 else if (TREE_CODE (postfix_expression) == OFFSET_REF
3859 || TREE_CODE (postfix_expression) == MEMBER_REF
3860 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
3861 postfix_expression = (build_offset_ref_call_from_tree
3862 (postfix_expression, args));
3863 else if (idk == CP_ID_KIND_QUALIFIED)
3864 /* A call to a static class member, or a namespace-scope
3867 = finish_call_expr (postfix_expression, args,
3868 /*disallow_virtual=*/true,
3871 /* All other function calls. */
3873 = finish_call_expr (postfix_expression, args,
3874 /*disallow_virtual=*/false,
3877 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
3878 idk = CP_ID_KIND_NONE;
3884 /* postfix-expression . template [opt] id-expression
3885 postfix-expression . pseudo-destructor-name
3886 postfix-expression -> template [opt] id-expression
3887 postfix-expression -> pseudo-destructor-name */
3892 tree scope = NULL_TREE;
3893 enum cpp_ttype token_type = token->type;
3895 /* If this is a `->' operator, dereference the pointer. */
3896 if (token->type == CPP_DEREF)
3897 postfix_expression = build_x_arrow (postfix_expression);
3898 /* Check to see whether or not the expression is
3900 dependent_p = type_dependent_expression_p (postfix_expression);
3901 /* The identifier following the `->' or `.' is not
3903 parser->scope = NULL_TREE;
3904 parser->qualifying_scope = NULL_TREE;
3905 parser->object_scope = NULL_TREE;
3906 idk = CP_ID_KIND_NONE;
3907 /* Enter the scope corresponding to the type of the object
3908 given by the POSTFIX_EXPRESSION. */
3910 && TREE_TYPE (postfix_expression) != NULL_TREE)
3912 scope = TREE_TYPE (postfix_expression);
3913 /* According to the standard, no expression should
3914 ever have reference type. Unfortunately, we do not
3915 currently match the standard in this respect in
3916 that our internal representation of an expression
3917 may have reference type even when the standard says
3918 it does not. Therefore, we have to manually obtain
3919 the underlying type here. */
3920 scope = non_reference (scope);
3921 /* The type of the POSTFIX_EXPRESSION must be
3923 scope = complete_type_or_else (scope, NULL_TREE);
3924 /* Let the name lookup machinery know that we are
3925 processing a class member access expression. */
3926 parser->context->object_type = scope;
3927 /* If something went wrong, we want to be able to
3928 discern that case, as opposed to the case where
3929 there was no SCOPE due to the type of expression
3932 scope = error_mark_node;
3933 /* If the SCOPE was erroneous, make the various
3934 semantic analysis functions exit quickly -- and
3935 without issuing additional error messages. */
3936 if (scope == error_mark_node)
3937 postfix_expression = error_mark_node;
3940 /* Consume the `.' or `->' operator. */
3941 cp_lexer_consume_token (parser->lexer);
3942 /* If the SCOPE is not a scalar type, we are looking at an
3943 ordinary class member access expression, rather than a
3944 pseudo-destructor-name. */
3945 if (!scope || !SCALAR_TYPE_P (scope))
3947 template_p = cp_parser_optional_template_keyword (parser);
3948 /* Parse the id-expression. */
3949 name = cp_parser_id_expression (parser,
3951 /*check_dependency_p=*/true,
3952 /*template_p=*/NULL,
3953 /*declarator_p=*/false);
3954 /* In general, build a SCOPE_REF if the member name is
3955 qualified. However, if the name was not dependent
3956 and has already been resolved; there is no need to
3957 build the SCOPE_REF. For example;
3959 struct X { void f(); };
3960 template <typename T> void f(T* t) { t->X::f(); }
3962 Even though "t" is dependent, "X::f" is not and has
3963 been resolved to a BASELINK; there is no need to
3964 include scope information. */
3966 /* But we do need to remember that there was an explicit
3967 scope for virtual function calls. */
3969 idk = CP_ID_KIND_QUALIFIED;
3971 if (name != error_mark_node
3972 && !BASELINK_P (name)
3975 name = build_nt (SCOPE_REF, parser->scope, name);
3976 parser->scope = NULL_TREE;
3977 parser->qualifying_scope = NULL_TREE;
3978 parser->object_scope = NULL_TREE;
3981 = finish_class_member_access_expr (postfix_expression, name);
3983 /* Otherwise, try the pseudo-destructor-name production. */
3989 /* Parse the pseudo-destructor-name. */
3990 cp_parser_pseudo_destructor_name (parser, &s, &type);
3991 /* Form the call. */
3993 = finish_pseudo_destructor_expr (postfix_expression,
3994 s, TREE_TYPE (type));
3997 /* We no longer need to look up names in the scope of the
3998 object on the left-hand side of the `.' or `->'
4000 parser->context->object_type = NULL_TREE;
4001 /* These operators may not appear in constant-expressions. */
4002 if (parser->integral_constant_expression_p
4003 /* The "->" operator is allowed in the implementation
4004 of "offsetof". The "." operator may appear in the
4005 name of the member. */
4006 && !parser->in_offsetof_p)
4008 if (!parser->allow_non_integral_constant_expression_p)
4010 = (cp_parser_non_integral_constant_expression
4011 (token_type == CPP_DEREF ? "'->'" : "`.'"));
4012 parser->non_integral_constant_expression_p = true;
4018 /* postfix-expression ++ */
4019 /* Consume the `++' token. */
4020 cp_lexer_consume_token (parser->lexer);
4021 /* Generate a representation for the complete expression. */
4023 = finish_increment_expr (postfix_expression,
4024 POSTINCREMENT_EXPR);
4025 /* Increments may not appear in constant-expressions. */
4026 if (parser->integral_constant_expression_p)
4028 if (!parser->allow_non_integral_constant_expression_p)
4030 = cp_parser_non_integral_constant_expression ("an increment");
4031 parser->non_integral_constant_expression_p = true;
4033 idk = CP_ID_KIND_NONE;
4036 case CPP_MINUS_MINUS:
4037 /* postfix-expression -- */
4038 /* Consume the `--' token. */
4039 cp_lexer_consume_token (parser->lexer);
4040 /* Generate a representation for the complete expression. */
4042 = finish_increment_expr (postfix_expression,
4043 POSTDECREMENT_EXPR);
4044 /* Decrements may not appear in constant-expressions. */
4045 if (parser->integral_constant_expression_p)
4047 if (!parser->allow_non_integral_constant_expression_p)
4049 = cp_parser_non_integral_constant_expression ("a decrement");
4050 parser->non_integral_constant_expression_p = true;
4052 idk = CP_ID_KIND_NONE;
4056 return postfix_expression;
4060 /* We should never get here. */
4062 return error_mark_node;
4065 /* Parse a parenthesized expression-list.
4068 assignment-expression
4069 expression-list, assignment-expression
4074 identifier, expression-list
4076 Returns a TREE_LIST. The TREE_VALUE of each node is a
4077 representation of an assignment-expression. Note that a TREE_LIST
4078 is returned even if there is only a single expression in the list.
4079 error_mark_node is returned if the ( and or ) are
4080 missing. NULL_TREE is returned on no expressions. The parentheses
4081 are eaten. IS_ATTRIBUTE_LIST is true if this is really an attribute
4082 list being parsed. If NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P
4083 indicates whether or not all of the expressions in the list were
4087 cp_parser_parenthesized_expression_list (cp_parser* parser,
4088 bool is_attribute_list,
4089 bool *non_constant_p)
4091 tree expression_list = NULL_TREE;
4092 tree identifier = NULL_TREE;
4094 /* Assume all the expressions will be constant. */
4096 *non_constant_p = false;
4098 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
4099 return error_mark_node;
4101 /* Consume expressions until there are no more. */
4102 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
4107 /* At the beginning of attribute lists, check to see if the
4108 next token is an identifier. */
4109 if (is_attribute_list
4110 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
4114 /* Consume the identifier. */
4115 token = cp_lexer_consume_token (parser->lexer);
4116 /* Save the identifier. */
4117 identifier = token->value;
4121 /* Parse the next assignment-expression. */
4124 bool expr_non_constant_p;
4125 expr = (cp_parser_constant_expression
4126 (parser, /*allow_non_constant_p=*/true,
4127 &expr_non_constant_p));
4128 if (expr_non_constant_p)
4129 *non_constant_p = true;
4132 expr = cp_parser_assignment_expression (parser);
4134 /* Add it to the list. We add error_mark_node
4135 expressions to the list, so that we can still tell if
4136 the correct form for a parenthesized expression-list
4137 is found. That gives better errors. */
4138 expression_list = tree_cons (NULL_TREE, expr, expression_list);
4140 if (expr == error_mark_node)
4144 /* After the first item, attribute lists look the same as
4145 expression lists. */
4146 is_attribute_list = false;
4149 /* If the next token isn't a `,', then we are done. */
4150 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
4153 /* Otherwise, consume the `,' and keep going. */
4154 cp_lexer_consume_token (parser->lexer);
4157 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
4162 /* We try and resync to an unnested comma, as that will give the
4163 user better diagnostics. */
4164 ending = cp_parser_skip_to_closing_parenthesis (parser,
4165 /*recovering=*/true,
4167 /*consume_paren=*/true);
4171 return error_mark_node;
4174 /* We built up the list in reverse order so we must reverse it now. */
4175 expression_list = nreverse (expression_list);
4177 expression_list = tree_cons (NULL_TREE, identifier, expression_list);
4179 return expression_list;
4182 /* Parse a pseudo-destructor-name.
4184 pseudo-destructor-name:
4185 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
4186 :: [opt] nested-name-specifier template template-id :: ~ type-name
4187 :: [opt] nested-name-specifier [opt] ~ type-name
4189 If either of the first two productions is used, sets *SCOPE to the
4190 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
4191 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
4192 or ERROR_MARK_NODE if the parse fails. */
4195 cp_parser_pseudo_destructor_name (cp_parser* parser,
4199 bool nested_name_specifier_p;
4201 /* Look for the optional `::' operator. */
4202 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
4203 /* Look for the optional nested-name-specifier. */
4204 nested_name_specifier_p
4205 = (cp_parser_nested_name_specifier_opt (parser,
4206 /*typename_keyword_p=*/false,
4207 /*check_dependency_p=*/true,
4209 /*is_declaration=*/true)
4211 /* Now, if we saw a nested-name-specifier, we might be doing the
4212 second production. */
4213 if (nested_name_specifier_p
4214 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
4216 /* Consume the `template' keyword. */
4217 cp_lexer_consume_token (parser->lexer);
4218 /* Parse the template-id. */
4219 cp_parser_template_id (parser,
4220 /*template_keyword_p=*/true,
4221 /*check_dependency_p=*/false,
4222 /*is_declaration=*/true);
4223 /* Look for the `::' token. */
4224 cp_parser_require (parser, CPP_SCOPE, "`::'");
4226 /* If the next token is not a `~', then there might be some
4227 additional qualification. */
4228 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
4230 /* Look for the type-name. */
4231 *scope = TREE_TYPE (cp_parser_type_name (parser));
4233 /* If we didn't get an aggregate type, or we don't have ::~,
4234 then something has gone wrong. Since the only caller of this
4235 function is looking for something after `.' or `->' after a
4236 scalar type, most likely the program is trying to get a
4237 member of a non-aggregate type. */
4238 if (*scope == error_mark_node
4239 || cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE)
4240 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_COMPL)
4242 cp_parser_error (parser, "request for member of non-aggregate type");
4243 *type = error_mark_node;
4247 /* Look for the `::' token. */
4248 cp_parser_require (parser, CPP_SCOPE, "`::'");
4253 /* Look for the `~'. */
4254 cp_parser_require (parser, CPP_COMPL, "`~'");
4255 /* Look for the type-name again. We are not responsible for
4256 checking that it matches the first type-name. */
4257 *type = cp_parser_type_name (parser);
4260 /* Parse a unary-expression.
4266 unary-operator cast-expression
4267 sizeof unary-expression
4275 __extension__ cast-expression
4276 __alignof__ unary-expression
4277 __alignof__ ( type-id )
4278 __real__ cast-expression
4279 __imag__ cast-expression
4282 ADDRESS_P is true iff the unary-expression is appearing as the
4283 operand of the `&' operator.
4285 Returns a representation of the expression. */
4288 cp_parser_unary_expression (cp_parser *parser, bool address_p)
4291 enum tree_code unary_operator;
4293 /* Peek at the next token. */
4294 token = cp_lexer_peek_token (parser->lexer);
4295 /* Some keywords give away the kind of expression. */
4296 if (token->type == CPP_KEYWORD)
4298 enum rid keyword = token->keyword;
4308 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
4309 /* Consume the token. */
4310 cp_lexer_consume_token (parser->lexer);
4311 /* Parse the operand. */
4312 operand = cp_parser_sizeof_operand (parser, keyword);
4314 if (TYPE_P (operand))
4315 return cxx_sizeof_or_alignof_type (operand, op, true);
4317 return cxx_sizeof_or_alignof_expr (operand, op);
4321 return cp_parser_new_expression (parser);
4324 return cp_parser_delete_expression (parser);
4328 /* The saved value of the PEDANTIC flag. */
4332 /* Save away the PEDANTIC flag. */
4333 cp_parser_extension_opt (parser, &saved_pedantic);
4334 /* Parse the cast-expression. */
4335 expr = cp_parser_simple_cast_expression (parser);
4336 /* Restore the PEDANTIC flag. */
4337 pedantic = saved_pedantic;
4347 /* Consume the `__real__' or `__imag__' token. */
4348 cp_lexer_consume_token (parser->lexer);
4349 /* Parse the cast-expression. */
4350 expression = cp_parser_simple_cast_expression (parser);
4351 /* Create the complete representation. */
4352 return build_x_unary_op ((keyword == RID_REALPART
4353 ? REALPART_EXPR : IMAGPART_EXPR),
4363 /* Look for the `:: new' and `:: delete', which also signal the
4364 beginning of a new-expression, or delete-expression,
4365 respectively. If the next token is `::', then it might be one of
4367 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
4371 /* See if the token after the `::' is one of the keywords in
4372 which we're interested. */
4373 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
4374 /* If it's `new', we have a new-expression. */
4375 if (keyword == RID_NEW)
4376 return cp_parser_new_expression (parser);
4377 /* Similarly, for `delete'. */
4378 else if (keyword == RID_DELETE)
4379 return cp_parser_delete_expression (parser);
4382 /* Look for a unary operator. */
4383 unary_operator = cp_parser_unary_operator (token);
4384 /* The `++' and `--' operators can be handled similarly, even though
4385 they are not technically unary-operators in the grammar. */
4386 if (unary_operator == ERROR_MARK)
4388 if (token->type == CPP_PLUS_PLUS)
4389 unary_operator = PREINCREMENT_EXPR;
4390 else if (token->type == CPP_MINUS_MINUS)
4391 unary_operator = PREDECREMENT_EXPR;
4392 /* Handle the GNU address-of-label extension. */
4393 else if (cp_parser_allow_gnu_extensions_p (parser)
4394 && token->type == CPP_AND_AND)
4398 /* Consume the '&&' token. */
4399 cp_lexer_consume_token (parser->lexer);
4400 /* Look for the identifier. */
4401 identifier = cp_parser_identifier (parser);
4402 /* Create an expression representing the address. */
4403 return finish_label_address_expr (identifier);
4406 if (unary_operator != ERROR_MARK)
4408 tree cast_expression;
4409 tree expression = error_mark_node;
4410 const char *non_constant_p = NULL;
4412 /* Consume the operator token. */
4413 token = cp_lexer_consume_token (parser->lexer);
4414 /* Parse the cast-expression. */
4416 = cp_parser_cast_expression (parser, unary_operator == ADDR_EXPR);
4417 /* Now, build an appropriate representation. */
4418 switch (unary_operator)
4421 non_constant_p = "`*'";
4422 expression = build_x_indirect_ref (cast_expression, "unary *");
4426 /* The "&" operator is allowed in the implementation of
4428 if (!parser->in_offsetof_p)
4429 non_constant_p = "`&'";
4432 expression = build_x_unary_op (unary_operator, cast_expression);
4435 case PREINCREMENT_EXPR:
4436 case PREDECREMENT_EXPR:
4437 non_constant_p = (unary_operator == PREINCREMENT_EXPR
4442 case TRUTH_NOT_EXPR:
4443 expression = finish_unary_op_expr (unary_operator, cast_expression);
4450 if (non_constant_p && parser->integral_constant_expression_p)
4452 if (!parser->allow_non_integral_constant_expression_p)
4453 return cp_parser_non_integral_constant_expression (non_constant_p);
4454 parser->non_integral_constant_expression_p = true;
4460 return cp_parser_postfix_expression (parser, address_p);
4463 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
4464 unary-operator, the corresponding tree code is returned. */
4466 static enum tree_code
4467 cp_parser_unary_operator (cp_token* token)
4469 switch (token->type)
4472 return INDIRECT_REF;
4478 return CONVERT_EXPR;
4484 return TRUTH_NOT_EXPR;
4487 return BIT_NOT_EXPR;
4494 /* Parse a new-expression.
4497 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
4498 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
4500 Returns a representation of the expression. */
4503 cp_parser_new_expression (cp_parser* parser)
4505 bool global_scope_p;
4510 /* Look for the optional `::' operator. */
4512 = (cp_parser_global_scope_opt (parser,
4513 /*current_scope_valid_p=*/false)
4515 /* Look for the `new' operator. */
4516 cp_parser_require_keyword (parser, RID_NEW, "`new'");
4517 /* There's no easy way to tell a new-placement from the
4518 `( type-id )' construct. */
4519 cp_parser_parse_tentatively (parser);
4520 /* Look for a new-placement. */
4521 placement = cp_parser_new_placement (parser);
4522 /* If that didn't work out, there's no new-placement. */
4523 if (!cp_parser_parse_definitely (parser))
4524 placement = NULL_TREE;
4526 /* If the next token is a `(', then we have a parenthesized
4528 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4530 /* Consume the `('. */
4531 cp_lexer_consume_token (parser->lexer);
4532 /* Parse the type-id. */
4533 type = cp_parser_type_id (parser);
4534 /* Look for the closing `)'. */
4535 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4536 /* There should not be a direct-new-declarator in this production,
4537 but GCC used to allowed this, so we check and emit a sensible error
4538 message for this case. */
4539 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
4541 error ("array bound forbidden after parenthesized type-id");
\r
4542 inform ("try removing the parentheses around the type-id");
\r
4543 cp_parser_direct_new_declarator (parser);
4546 /* Otherwise, there must be a new-type-id. */
4548 type = cp_parser_new_type_id (parser);
4550 /* If the next token is a `(', then we have a new-initializer. */
4551 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4552 initializer = cp_parser_new_initializer (parser);
4554 initializer = NULL_TREE;
4556 /* Create a representation of the new-expression. */
4557 return build_new (placement, type, initializer, global_scope_p);
4560 /* Parse a new-placement.
4565 Returns the same representation as for an expression-list. */
4568 cp_parser_new_placement (cp_parser* parser)
4570 tree expression_list;
4572 /* Parse the expression-list. */
4573 expression_list = (cp_parser_parenthesized_expression_list
4574 (parser, false, /*non_constant_p=*/NULL));
4576 return expression_list;
4579 /* Parse a new-type-id.
4582 type-specifier-seq new-declarator [opt]
4584 Returns a TREE_LIST whose TREE_PURPOSE is the type-specifier-seq,
4585 and whose TREE_VALUE is the new-declarator. */
4588 cp_parser_new_type_id (cp_parser* parser)
4590 tree type_specifier_seq;
4592 const char *saved_message;
4594 /* The type-specifier sequence must not contain type definitions.
4595 (It cannot contain declarations of new types either, but if they
4596 are not definitions we will catch that because they are not
4598 saved_message = parser->type_definition_forbidden_message;
4599 parser->type_definition_forbidden_message
4600 = "types may not be defined in a new-type-id";
4601 /* Parse the type-specifier-seq. */
4602 type_specifier_seq = cp_parser_type_specifier_seq (parser);
4603 /* Restore the old message. */
4604 parser->type_definition_forbidden_message = saved_message;
4605 /* Parse the new-declarator. */
4606 declarator = cp_parser_new_declarator_opt (parser);
4608 return build_tree_list (type_specifier_seq, declarator);
4611 /* Parse an (optional) new-declarator.
4614 ptr-operator new-declarator [opt]
4615 direct-new-declarator
4617 Returns a representation of the declarator. See
4618 cp_parser_declarator for the representations used. */
4621 cp_parser_new_declarator_opt (cp_parser* parser)
4623 enum tree_code code;
4625 tree cv_qualifier_seq;
4627 /* We don't know if there's a ptr-operator next, or not. */
4628 cp_parser_parse_tentatively (parser);
4629 /* Look for a ptr-operator. */
4630 code = cp_parser_ptr_operator (parser, &type, &cv_qualifier_seq);
4631 /* If that worked, look for more new-declarators. */
4632 if (cp_parser_parse_definitely (parser))
4636 /* Parse another optional declarator. */
4637 declarator = cp_parser_new_declarator_opt (parser);
4639 /* Create the representation of the declarator. */
4640 if (code == INDIRECT_REF)
4641 declarator = make_pointer_declarator (cv_qualifier_seq,
4644 declarator = make_reference_declarator (cv_qualifier_seq,
4647 /* Handle the pointer-to-member case. */
4649 declarator = build_nt (SCOPE_REF, type, declarator);
4654 /* If the next token is a `[', there is a direct-new-declarator. */
4655 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
4656 return cp_parser_direct_new_declarator (parser);
4661 /* Parse a direct-new-declarator.
4663 direct-new-declarator:
4665 direct-new-declarator [constant-expression]
4667 Returns an ARRAY_REF, following the same conventions as are
4668 documented for cp_parser_direct_declarator. */
4671 cp_parser_direct_new_declarator (cp_parser* parser)
4673 tree declarator = NULL_TREE;
4679 /* Look for the opening `['. */
4680 cp_parser_require (parser, CPP_OPEN_SQUARE, "`['");
4681 /* The first expression is not required to be constant. */
4684 expression = cp_parser_expression (parser);
4685 /* The standard requires that the expression have integral
4686 type. DR 74 adds enumeration types. We believe that the
4687 real intent is that these expressions be handled like the
4688 expression in a `switch' condition, which also allows
4689 classes with a single conversion to integral or
4690 enumeration type. */
4691 if (!processing_template_decl)
4694 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
4699 error ("expression in new-declarator must have integral or enumeration type");
4700 expression = error_mark_node;
4704 /* But all the other expressions must be. */
4707 = cp_parser_constant_expression (parser,
4708 /*allow_non_constant=*/false,
4710 /* Look for the closing `]'. */
4711 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4713 /* Add this bound to the declarator. */
4714 declarator = build_nt (ARRAY_REF, declarator, expression);
4716 /* If the next token is not a `[', then there are no more
4718 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
4725 /* Parse a new-initializer.
4728 ( expression-list [opt] )
4730 Returns a representation of the expression-list. If there is no
4731 expression-list, VOID_ZERO_NODE is returned. */
4734 cp_parser_new_initializer (cp_parser* parser)
4736 tree expression_list;
4738 expression_list = (cp_parser_parenthesized_expression_list
4739 (parser, false, /*non_constant_p=*/NULL));
4740 if (!expression_list)
4741 expression_list = void_zero_node;
4743 return expression_list;
4746 /* Parse a delete-expression.
4749 :: [opt] delete cast-expression
4750 :: [opt] delete [ ] cast-expression
4752 Returns a representation of the expression. */
4755 cp_parser_delete_expression (cp_parser* parser)
4757 bool global_scope_p;
4761 /* Look for the optional `::' operator. */
4763 = (cp_parser_global_scope_opt (parser,
4764 /*current_scope_valid_p=*/false)
4766 /* Look for the `delete' keyword. */
4767 cp_parser_require_keyword (parser, RID_DELETE, "`delete'");
4768 /* See if the array syntax is in use. */
4769 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
4771 /* Consume the `[' token. */
4772 cp_lexer_consume_token (parser->lexer);
4773 /* Look for the `]' token. */
4774 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4775 /* Remember that this is the `[]' construct. */
4781 /* Parse the cast-expression. */
4782 expression = cp_parser_simple_cast_expression (parser);
4784 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
4787 /* Parse a cast-expression.
4791 ( type-id ) cast-expression
4793 Returns a representation of the expression. */
4796 cp_parser_cast_expression (cp_parser *parser, bool address_p)
4798 /* If it's a `(', then we might be looking at a cast. */
4799 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4801 tree type = NULL_TREE;
4802 tree expr = NULL_TREE;
4803 bool compound_literal_p;
4804 const char *saved_message;
4806 /* There's no way to know yet whether or not this is a cast.
4807 For example, `(int (3))' is a unary-expression, while `(int)
4808 3' is a cast. So, we resort to parsing tentatively. */
4809 cp_parser_parse_tentatively (parser);
4810 /* Types may not be defined in a cast. */
4811 saved_message = parser->type_definition_forbidden_message;
4812 parser->type_definition_forbidden_message
4813 = "types may not be defined in casts";
4814 /* Consume the `('. */
4815 cp_lexer_consume_token (parser->lexer);
4816 /* A very tricky bit is that `(struct S) { 3 }' is a
4817 compound-literal (which we permit in C++ as an extension).
4818 But, that construct is not a cast-expression -- it is a
4819 postfix-expression. (The reason is that `(struct S) { 3 }.i'
4820 is legal; if the compound-literal were a cast-expression,
4821 you'd need an extra set of parentheses.) But, if we parse
4822 the type-id, and it happens to be a class-specifier, then we
4823 will commit to the parse at that point, because we cannot
4824 undo the action that is done when creating a new class. So,
4825 then we cannot back up and do a postfix-expression.
4827 Therefore, we scan ahead to the closing `)', and check to see
4828 if the token after the `)' is a `{'. If so, we are not
4829 looking at a cast-expression.
4831 Save tokens so that we can put them back. */
4832 cp_lexer_save_tokens (parser->lexer);
4833 /* Skip tokens until the next token is a closing parenthesis.
4834 If we find the closing `)', and the next token is a `{', then
4835 we are looking at a compound-literal. */
4837 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
4838 /*consume_paren=*/true)
4839 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
4840 /* Roll back the tokens we skipped. */
4841 cp_lexer_rollback_tokens (parser->lexer);
4842 /* If we were looking at a compound-literal, simulate an error
4843 so that the call to cp_parser_parse_definitely below will
4845 if (compound_literal_p)
4846 cp_parser_simulate_error (parser);
4849 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4850 parser->in_type_id_in_expr_p = true;
4851 /* Look for the type-id. */
4852 type = cp_parser_type_id (parser);
4853 /* Look for the closing `)'. */
4854 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4855 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4858 /* Restore the saved message. */
4859 parser->type_definition_forbidden_message = saved_message;
4861 /* If ok so far, parse the dependent expression. We cannot be
4862 sure it is a cast. Consider `(T ())'. It is a parenthesized
4863 ctor of T, but looks like a cast to function returning T
4864 without a dependent expression. */
4865 if (!cp_parser_error_occurred (parser))
4866 expr = cp_parser_simple_cast_expression (parser);
4868 if (cp_parser_parse_definitely (parser))
4870 /* Warn about old-style casts, if so requested. */
4871 if (warn_old_style_cast
4872 && !in_system_header
4873 && !VOID_TYPE_P (type)
4874 && current_lang_name != lang_name_c)
4875 warning ("use of old-style cast");
4877 /* Only type conversions to integral or enumeration types
4878 can be used in constant-expressions. */
4879 if (parser->integral_constant_expression_p
4880 && !dependent_type_p (type)
4881 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type))
4883 if (!parser->allow_non_integral_constant_expression_p)
4884 return (cp_parser_non_integral_constant_expression
4885 ("a casts to a type other than an integral or "
4886 "enumeration type"));
4887 parser->non_integral_constant_expression_p = true;
4889 /* Perform the cast. */
4890 expr = build_c_cast (type, expr);
4895 /* If we get here, then it's not a cast, so it must be a
4896 unary-expression. */
4897 return cp_parser_unary_expression (parser, address_p);
4900 /* Parse a pm-expression.
4904 pm-expression .* cast-expression
4905 pm-expression ->* cast-expression
4907 Returns a representation of the expression. */
4910 cp_parser_pm_expression (cp_parser* parser)
4912 static const cp_parser_token_tree_map map = {
4913 { CPP_DEREF_STAR, MEMBER_REF },
4914 { CPP_DOT_STAR, DOTSTAR_EXPR },
4915 { CPP_EOF, ERROR_MARK }
4918 return cp_parser_binary_expression (parser, map,
4919 cp_parser_simple_cast_expression);
4922 /* Parse a multiplicative-expression.
4924 multiplicative-expression:
4926 multiplicative-expression * pm-expression
4927 multiplicative-expression / pm-expression
4928 multiplicative-expression % pm-expression
4930 Returns a representation of the expression. */
4933 cp_parser_multiplicative_expression (cp_parser* parser)
4935 static const cp_parser_token_tree_map map = {
4936 { CPP_MULT, MULT_EXPR },
4937 { CPP_DIV, TRUNC_DIV_EXPR },
4938 { CPP_MOD, TRUNC_MOD_EXPR },
4939 { CPP_EOF, ERROR_MARK }
4942 return cp_parser_binary_expression (parser,
4944 cp_parser_pm_expression);
4947 /* Parse an additive-expression.
4949 additive-expression:
4950 multiplicative-expression
4951 additive-expression + multiplicative-expression
4952 additive-expression - multiplicative-expression
4954 Returns a representation of the expression. */
4957 cp_parser_additive_expression (cp_parser* parser)
4959 static const cp_parser_token_tree_map map = {
4960 { CPP_PLUS, PLUS_EXPR },
4961 { CPP_MINUS, MINUS_EXPR },
4962 { CPP_EOF, ERROR_MARK }
4965 return cp_parser_binary_expression (parser,
4967 cp_parser_multiplicative_expression);
4970 /* Parse a shift-expression.
4974 shift-expression << additive-expression
4975 shift-expression >> additive-expression
4977 Returns a representation of the expression. */
4980 cp_parser_shift_expression (cp_parser* parser)
4982 static const cp_parser_token_tree_map map = {
4983 { CPP_LSHIFT, LSHIFT_EXPR },
4984 { CPP_RSHIFT, RSHIFT_EXPR },
4985 { CPP_EOF, ERROR_MARK }
4988 return cp_parser_binary_expression (parser,
4990 cp_parser_additive_expression);
4993 /* Parse a relational-expression.
4995 relational-expression:
4997 relational-expression < shift-expression
4998 relational-expression > shift-expression
4999 relational-expression <= shift-expression
5000 relational-expression >= shift-expression
5004 relational-expression:
5005 relational-expression <? shift-expression
5006 relational-expression >? shift-expression
5008 Returns a representation of the expression. */
5011 cp_parser_relational_expression (cp_parser* parser)
5013 static const cp_parser_token_tree_map map = {
5014 { CPP_LESS, LT_EXPR },
5015 { CPP_GREATER, GT_EXPR },
5016 { CPP_LESS_EQ, LE_EXPR },
5017 { CPP_GREATER_EQ, GE_EXPR },
5018 { CPP_MIN, MIN_EXPR },
5019 { CPP_MAX, MAX_EXPR },
5020 { CPP_EOF, ERROR_MARK }
5023 return cp_parser_binary_expression (parser,
5025 cp_parser_shift_expression);
5028 /* Parse an equality-expression.
5030 equality-expression:
5031 relational-expression
5032 equality-expression == relational-expression
5033 equality-expression != relational-expression
5035 Returns a representation of the expression. */
5038 cp_parser_equality_expression (cp_parser* parser)
5040 static const cp_parser_token_tree_map map = {
5041 { CPP_EQ_EQ, EQ_EXPR },
5042 { CPP_NOT_EQ, NE_EXPR },
5043 { CPP_EOF, ERROR_MARK }
5046 return cp_parser_binary_expression (parser,
5048 cp_parser_relational_expression);
5051 /* Parse an and-expression.
5055 and-expression & equality-expression
5057 Returns a representation of the expression. */
5060 cp_parser_and_expression (cp_parser* parser)
5062 static const cp_parser_token_tree_map map = {
5063 { CPP_AND, BIT_AND_EXPR },
5064 { CPP_EOF, ERROR_MARK }
5067 return cp_parser_binary_expression (parser,
5069 cp_parser_equality_expression);
5072 /* Parse an exclusive-or-expression.
5074 exclusive-or-expression:
5076 exclusive-or-expression ^ and-expression
5078 Returns a representation of the expression. */
5081 cp_parser_exclusive_or_expression (cp_parser* parser)
5083 static const cp_parser_token_tree_map map = {
5084 { CPP_XOR, BIT_XOR_EXPR },
5085 { CPP_EOF, ERROR_MARK }
5088 return cp_parser_binary_expression (parser,
5090 cp_parser_and_expression);
5094 /* Parse an inclusive-or-expression.
5096 inclusive-or-expression:
5097 exclusive-or-expression
5098 inclusive-or-expression | exclusive-or-expression
5100 Returns a representation of the expression. */
5103 cp_parser_inclusive_or_expression (cp_parser* parser)
5105 static const cp_parser_token_tree_map map = {
5106 { CPP_OR, BIT_IOR_EXPR },
5107 { CPP_EOF, ERROR_MARK }
5110 return cp_parser_binary_expression (parser,
5112 cp_parser_exclusive_or_expression);
5115 /* Parse a logical-and-expression.
5117 logical-and-expression:
5118 inclusive-or-expression
5119 logical-and-expression && inclusive-or-expression
5121 Returns a representation of the expression. */
5124 cp_parser_logical_and_expression (cp_parser* parser)
5126 static const cp_parser_token_tree_map map = {
5127 { CPP_AND_AND, TRUTH_ANDIF_EXPR },
5128 { CPP_EOF, ERROR_MARK }
5131 return cp_parser_binary_expression (parser,
5133 cp_parser_inclusive_or_expression);
5136 /* Parse a logical-or-expression.
5138 logical-or-expression:
5139 logical-and-expression
5140 logical-or-expression || logical-and-expression
5142 Returns a representation of the expression. */
5145 cp_parser_logical_or_expression (cp_parser* parser)
5147 static const cp_parser_token_tree_map map = {
5148 { CPP_OR_OR, TRUTH_ORIF_EXPR },
5149 { CPP_EOF, ERROR_MARK }
5152 return cp_parser_binary_expression (parser,
5154 cp_parser_logical_and_expression);
5157 /* Parse the `? expression : assignment-expression' part of a
5158 conditional-expression. The LOGICAL_OR_EXPR is the
5159 logical-or-expression that started the conditional-expression.
5160 Returns a representation of the entire conditional-expression.
5162 This routine is used by cp_parser_assignment_expression.
5164 ? expression : assignment-expression
5168 ? : assignment-expression */
5171 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
5174 tree assignment_expr;
5176 /* Consume the `?' token. */
5177 cp_lexer_consume_token (parser->lexer);
5178 if (cp_parser_allow_gnu_extensions_p (parser)
5179 && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
5180 /* Implicit true clause. */
5183 /* Parse the expression. */
5184 expr = cp_parser_expression (parser);
5186 /* The next token should be a `:'. */
5187 cp_parser_require (parser, CPP_COLON, "`:'");
5188 /* Parse the assignment-expression. */
5189 assignment_expr = cp_parser_assignment_expression (parser);
5191 /* Build the conditional-expression. */
5192 return build_x_conditional_expr (logical_or_expr,
5197 /* Parse an assignment-expression.
5199 assignment-expression:
5200 conditional-expression
5201 logical-or-expression assignment-operator assignment_expression
5204 Returns a representation for the expression. */
5207 cp_parser_assignment_expression (cp_parser* parser)
5211 /* If the next token is the `throw' keyword, then we're looking at
5212 a throw-expression. */
5213 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
5214 expr = cp_parser_throw_expression (parser);
5215 /* Otherwise, it must be that we are looking at a
5216 logical-or-expression. */
5219 /* Parse the logical-or-expression. */
5220 expr = cp_parser_logical_or_expression (parser);
5221 /* If the next token is a `?' then we're actually looking at a
5222 conditional-expression. */
5223 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
5224 return cp_parser_question_colon_clause (parser, expr);
5227 enum tree_code assignment_operator;
5229 /* If it's an assignment-operator, we're using the second
5232 = cp_parser_assignment_operator_opt (parser);
5233 if (assignment_operator != ERROR_MARK)
5237 /* Parse the right-hand side of the assignment. */
5238 rhs = cp_parser_assignment_expression (parser);
5239 /* An assignment may not appear in a
5240 constant-expression. */
5241 if (parser->integral_constant_expression_p)
5243 if (!parser->allow_non_integral_constant_expression_p)
5244 return cp_parser_non_integral_constant_expression ("an assignment");
5245 parser->non_integral_constant_expression_p = true;
5247 /* Build the assignment expression. */
5248 expr = build_x_modify_expr (expr,
5249 assignment_operator,
5258 /* Parse an (optional) assignment-operator.
5260 assignment-operator: one of
5261 = *= /= %= += -= >>= <<= &= ^= |=
5265 assignment-operator: one of
5268 If the next token is an assignment operator, the corresponding tree
5269 code is returned, and the token is consumed. For example, for
5270 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
5271 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
5272 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
5273 operator, ERROR_MARK is returned. */
5275 static enum tree_code
5276 cp_parser_assignment_operator_opt (cp_parser* parser)
5281 /* Peek at the next toen. */
5282 token = cp_lexer_peek_token (parser->lexer);
5284 switch (token->type)
5295 op = TRUNC_DIV_EXPR;
5299 op = TRUNC_MOD_EXPR;
5339 /* Nothing else is an assignment operator. */
5343 /* If it was an assignment operator, consume it. */
5344 if (op != ERROR_MARK)
5345 cp_lexer_consume_token (parser->lexer);
5350 /* Parse an expression.
5353 assignment-expression
5354 expression , assignment-expression
5356 Returns a representation of the expression. */
5359 cp_parser_expression (cp_parser* parser)
5361 tree expression = NULL_TREE;
5365 tree assignment_expression;
5367 /* Parse the next assignment-expression. */
5368 assignment_expression
5369 = cp_parser_assignment_expression (parser);
5370 /* If this is the first assignment-expression, we can just
5373 expression = assignment_expression;
5375 expression = build_x_compound_expr (expression,
5376 assignment_expression);
5377 /* If the next token is not a comma, then we are done with the
5379 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5381 /* Consume the `,'. */
5382 cp_lexer_consume_token (parser->lexer);
5383 /* A comma operator cannot appear in a constant-expression. */
5384 if (parser->integral_constant_expression_p)
5386 if (!parser->allow_non_integral_constant_expression_p)
5388 = cp_parser_non_integral_constant_expression ("a comma operator");
5389 parser->non_integral_constant_expression_p = true;
5396 /* Parse a constant-expression.
5398 constant-expression:
5399 conditional-expression
5401 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
5402 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
5403 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
5404 is false, NON_CONSTANT_P should be NULL. */
5407 cp_parser_constant_expression (cp_parser* parser,
5408 bool allow_non_constant_p,
5409 bool *non_constant_p)
5411 bool saved_integral_constant_expression_p;
5412 bool saved_allow_non_integral_constant_expression_p;
5413 bool saved_non_integral_constant_expression_p;
5416 /* It might seem that we could simply parse the
5417 conditional-expression, and then check to see if it were
5418 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
5419 one that the compiler can figure out is constant, possibly after
5420 doing some simplifications or optimizations. The standard has a
5421 precise definition of constant-expression, and we must honor
5422 that, even though it is somewhat more restrictive.
5428 is not a legal declaration, because `(2, 3)' is not a
5429 constant-expression. The `,' operator is forbidden in a
5430 constant-expression. However, GCC's constant-folding machinery
5431 will fold this operation to an INTEGER_CST for `3'. */
5433 /* Save the old settings. */
5434 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
5435 saved_allow_non_integral_constant_expression_p
5436 = parser->allow_non_integral_constant_expression_p;
5437 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
5438 /* We are now parsing a constant-expression. */
5439 parser->integral_constant_expression_p = true;
5440 parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
5441 parser->non_integral_constant_expression_p = false;
5442 /* Although the grammar says "conditional-expression", we parse an
5443 "assignment-expression", which also permits "throw-expression"
5444 and the use of assignment operators. In the case that
5445 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
5446 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
5447 actually essential that we look for an assignment-expression.
5448 For example, cp_parser_initializer_clauses uses this function to
5449 determine whether a particular assignment-expression is in fact
5451 expression = cp_parser_assignment_expression (parser);
5452 /* Restore the old settings. */
5453 parser->integral_constant_expression_p = saved_integral_constant_expression_p;
5454 parser->allow_non_integral_constant_expression_p
5455 = saved_allow_non_integral_constant_expression_p;
5456 if (allow_non_constant_p)
5457 *non_constant_p = parser->non_integral_constant_expression_p;
5458 parser->non_integral_constant_expression_p = saved_non_integral_constant_expression_p;
5463 /* Statements [gram.stmt.stmt] */
5465 /* Parse a statement.
5469 expression-statement
5474 declaration-statement
5478 cp_parser_statement (cp_parser* parser, bool in_statement_expr_p)
5482 int statement_line_number;
5484 /* There is no statement yet. */
5485 statement = NULL_TREE;
5486 /* Peek at the next token. */
5487 token = cp_lexer_peek_token (parser->lexer);
5488 /* Remember the line number of the first token in the statement. */
5489 statement_line_number = token->location.line;
5490 /* If this is a keyword, then that will often determine what kind of
5491 statement we have. */
5492 if (token->type == CPP_KEYWORD)
5494 enum rid keyword = token->keyword;
5500 statement = cp_parser_labeled_statement (parser,
5501 in_statement_expr_p);
5506 statement = cp_parser_selection_statement (parser);
5512 statement = cp_parser_iteration_statement (parser);
5519 statement = cp_parser_jump_statement (parser);
5523 statement = cp_parser_try_block (parser);
5527 /* It might be a keyword like `int' that can start a
5528 declaration-statement. */
5532 else if (token->type == CPP_NAME)
5534 /* If the next token is a `:', then we are looking at a
5535 labeled-statement. */
5536 token = cp_lexer_peek_nth_token (parser->lexer, 2);
5537 if (token->type == CPP_COLON)
5538 statement = cp_parser_labeled_statement (parser, in_statement_expr_p);
5540 /* Anything that starts with a `{' must be a compound-statement. */
5541 else if (token->type == CPP_OPEN_BRACE)
5542 statement = cp_parser_compound_statement (parser, false);
5544 /* Everything else must be a declaration-statement or an
5545 expression-statement. Try for the declaration-statement
5546 first, unless we are looking at a `;', in which case we know that
5547 we have an expression-statement. */
5550 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
5552 cp_parser_parse_tentatively (parser);
5553 /* Try to parse the declaration-statement. */
5554 cp_parser_declaration_statement (parser);
5555 /* If that worked, we're done. */
5556 if (cp_parser_parse_definitely (parser))
5559 /* Look for an expression-statement instead. */
5560 statement = cp_parser_expression_statement (parser, in_statement_expr_p);
5563 /* Set the line number for the statement. */
5564 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
5565 STMT_LINENO (statement) = statement_line_number;
5568 /* Parse a labeled-statement.
5571 identifier : statement
5572 case constant-expression : statement
5578 case constant-expression ... constant-expression : statement
5580 Returns the new CASE_LABEL, for a `case' or `default' label. For
5581 an ordinary label, returns a LABEL_STMT. */
5584 cp_parser_labeled_statement (cp_parser* parser, bool in_statement_expr_p)
5587 tree statement = error_mark_node;
5589 /* The next token should be an identifier. */
5590 token = cp_lexer_peek_token (parser->lexer);
5591 if (token->type != CPP_NAME
5592 && token->type != CPP_KEYWORD)
5594 cp_parser_error (parser, "expected labeled-statement");
5595 return error_mark_node;
5598 switch (token->keyword)
5605 /* Consume the `case' token. */
5606 cp_lexer_consume_token (parser->lexer);
5607 /* Parse the constant-expression. */
5608 expr = cp_parser_constant_expression (parser,
5609 /*allow_non_constant_p=*/false,
5612 ellipsis = cp_lexer_peek_token (parser->lexer);
5613 if (ellipsis->type == CPP_ELLIPSIS)
5615 /* Consume the `...' token. */
5616 cp_lexer_consume_token (parser->lexer);
5618 cp_parser_constant_expression (parser,
5619 /*allow_non_constant_p=*/false,
5621 /* We don't need to emit warnings here, as the common code
5622 will do this for us. */
5625 expr_hi = NULL_TREE;
5627 if (!parser->in_switch_statement_p)
5628 error ("case label `%E' not within a switch statement", expr);
5630 statement = finish_case_label (expr, expr_hi);
5635 /* Consume the `default' token. */
5636 cp_lexer_consume_token (parser->lexer);
5637 if (!parser->in_switch_statement_p)
5638 error ("case label not within a switch statement");
5640 statement = finish_case_label (NULL_TREE, NULL_TREE);
5644 /* Anything else must be an ordinary label. */
5645 statement = finish_label_stmt (cp_parser_identifier (parser));
5649 /* Require the `:' token. */
5650 cp_parser_require (parser, CPP_COLON, "`:'");
5651 /* Parse the labeled statement. */
5652 cp_parser_statement (parser, in_statement_expr_p);
5654 /* Return the label, in the case of a `case' or `default' label. */
5658 /* Parse an expression-statement.
5660 expression-statement:
5663 Returns the new EXPR_STMT -- or NULL_TREE if the expression
5664 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
5665 indicates whether this expression-statement is part of an
5666 expression statement. */
5669 cp_parser_expression_statement (cp_parser* parser, bool in_statement_expr_p)
5671 tree statement = NULL_TREE;
5673 /* If the next token is a ';', then there is no expression
5675 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
5676 statement = cp_parser_expression (parser);
5678 /* Consume the final `;'. */
5679 cp_parser_consume_semicolon_at_end_of_statement (parser);
5681 if (in_statement_expr_p
5682 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
5684 /* This is the final expression statement of a statement
5686 statement = finish_stmt_expr_expr (statement);
5689 statement = finish_expr_stmt (statement);
5696 /* Parse a compound-statement.
5699 { statement-seq [opt] }
5701 Returns a COMPOUND_STMT representing the statement. */
5704 cp_parser_compound_statement (cp_parser *parser, bool in_statement_expr_p)
5708 /* Consume the `{'. */
5709 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
5710 return error_mark_node;
5711 /* Begin the compound-statement. */
5712 compound_stmt = begin_compound_stmt (/*has_no_scope=*/false);
5713 /* Parse an (optional) statement-seq. */
5714 cp_parser_statement_seq_opt (parser, in_statement_expr_p);
5715 /* Finish the compound-statement. */
5716 finish_compound_stmt (compound_stmt);
5717 /* Consume the `}'. */
5718 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
5720 return compound_stmt;
5723 /* Parse an (optional) statement-seq.
5727 statement-seq [opt] statement */
5730 cp_parser_statement_seq_opt (cp_parser* parser, bool in_statement_expr_p)
5732 /* Scan statements until there aren't any more. */
5735 /* If we're looking at a `}', then we've run out of statements. */
5736 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE)
5737 || cp_lexer_next_token_is (parser->lexer, CPP_EOF))
5740 /* Parse the statement. */
5741 cp_parser_statement (parser, in_statement_expr_p);
5745 /* Parse a selection-statement.
5747 selection-statement:
5748 if ( condition ) statement
5749 if ( condition ) statement else statement
5750 switch ( condition ) statement
5752 Returns the new IF_STMT or SWITCH_STMT. */
5755 cp_parser_selection_statement (cp_parser* parser)
5760 /* Peek at the next token. */
5761 token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
5763 /* See what kind of keyword it is. */
5764 keyword = token->keyword;
5773 /* Look for the `('. */
5774 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
5776 cp_parser_skip_to_end_of_statement (parser);
5777 return error_mark_node;
5780 /* Begin the selection-statement. */
5781 if (keyword == RID_IF)
5782 statement = begin_if_stmt ();
5784 statement = begin_switch_stmt ();
5786 /* Parse the condition. */
5787 condition = cp_parser_condition (parser);
5788 /* Look for the `)'. */
5789 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
5790 cp_parser_skip_to_closing_parenthesis (parser, true, false,
5791 /*consume_paren=*/true);
5793 if (keyword == RID_IF)
5797 /* Add the condition. */
5798 finish_if_stmt_cond (condition, statement);
5800 /* Parse the then-clause. */
5801 then_stmt = cp_parser_implicitly_scoped_statement (parser);
5802 finish_then_clause (statement);
5804 /* If the next token is `else', parse the else-clause. */
5805 if (cp_lexer_next_token_is_keyword (parser->lexer,
5810 /* Consume the `else' keyword. */
5811 cp_lexer_consume_token (parser->lexer);
5812 /* Parse the else-clause. */
5814 = cp_parser_implicitly_scoped_statement (parser);
5815 finish_else_clause (statement);
5818 /* Now we're all done with the if-statement. */
5824 bool in_switch_statement_p;
5826 /* Add the condition. */
5827 finish_switch_cond (condition, statement);
5829 /* Parse the body of the switch-statement. */
5830 in_switch_statement_p = parser->in_switch_statement_p;
5831 parser->in_switch_statement_p = true;
5832 body = cp_parser_implicitly_scoped_statement (parser);
5833 parser->in_switch_statement_p = in_switch_statement_p;
5835 /* Now we're all done with the switch-statement. */
5836 finish_switch_stmt (statement);
5844 cp_parser_error (parser, "expected selection-statement");
5845 return error_mark_node;
5849 /* Parse a condition.
5853 type-specifier-seq declarator = assignment-expression
5858 type-specifier-seq declarator asm-specification [opt]
5859 attributes [opt] = assignment-expression
5861 Returns the expression that should be tested. */
5864 cp_parser_condition (cp_parser* parser)
5866 tree type_specifiers;
5867 const char *saved_message;
5869 /* Try the declaration first. */
5870 cp_parser_parse_tentatively (parser);
5871 /* New types are not allowed in the type-specifier-seq for a
5873 saved_message = parser->type_definition_forbidden_message;
5874 parser->type_definition_forbidden_message
5875 = "types may not be defined in conditions";
5876 /* Parse the type-specifier-seq. */
5877 type_specifiers = cp_parser_type_specifier_seq (parser);
5878 /* Restore the saved message. */
5879 parser->type_definition_forbidden_message = saved_message;
5880 /* If all is well, we might be looking at a declaration. */
5881 if (!cp_parser_error_occurred (parser))
5884 tree asm_specification;
5887 tree initializer = NULL_TREE;
5889 /* Parse the declarator. */
5890 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
5891 /*ctor_dtor_or_conv_p=*/NULL,
5892 /*parenthesized_p=*/NULL);
5893 /* Parse the attributes. */
5894 attributes = cp_parser_attributes_opt (parser);
5895 /* Parse the asm-specification. */
5896 asm_specification = cp_parser_asm_specification_opt (parser);
5897 /* If the next token is not an `=', then we might still be
5898 looking at an expression. For example:
5902 looks like a decl-specifier-seq and a declarator -- but then
5903 there is no `=', so this is an expression. */
5904 cp_parser_require (parser, CPP_EQ, "`='");
5905 /* If we did see an `=', then we are looking at a declaration
5907 if (cp_parser_parse_definitely (parser))
5909 /* Create the declaration. */
5910 decl = start_decl (declarator, type_specifiers,
5911 /*initialized_p=*/true,
5912 attributes, /*prefix_attributes=*/NULL_TREE);
5913 /* Parse the assignment-expression. */
5914 initializer = cp_parser_assignment_expression (parser);
5916 /* Process the initializer. */
5917 cp_finish_decl (decl,
5920 LOOKUP_ONLYCONVERTING);
5922 return convert_from_reference (decl);
5925 /* If we didn't even get past the declarator successfully, we are
5926 definitely not looking at a declaration. */
5928 cp_parser_abort_tentative_parse (parser);
5930 /* Otherwise, we are looking at an expression. */
5931 return cp_parser_expression (parser);
5934 /* Parse an iteration-statement.
5936 iteration-statement:
5937 while ( condition ) statement
5938 do statement while ( expression ) ;
5939 for ( for-init-statement condition [opt] ; expression [opt] )
5942 Returns the new WHILE_STMT, DO_STMT, or FOR_STMT. */
5945 cp_parser_iteration_statement (cp_parser* parser)
5950 bool in_iteration_statement_p;
5953 /* Peek at the next token. */
5954 token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
5956 return error_mark_node;
5958 /* Remember whether or not we are already within an iteration
5960 in_iteration_statement_p = parser->in_iteration_statement_p;
5962 /* See what kind of keyword it is. */
5963 keyword = token->keyword;
5970 /* Begin the while-statement. */
5971 statement = begin_while_stmt ();
5972 /* Look for the `('. */
5973 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
5974 /* Parse the condition. */
5975 condition = cp_parser_condition (parser);
5976 finish_while_stmt_cond (condition, statement);
5977 /* Look for the `)'. */
5978 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5979 /* Parse the dependent statement. */
5980 parser->in_iteration_statement_p = true;
5981 cp_parser_already_scoped_statement (parser);
5982 parser->in_iteration_statement_p = in_iteration_statement_p;
5983 /* We're done with the while-statement. */
5984 finish_while_stmt (statement);
5992 /* Begin the do-statement. */
5993 statement = begin_do_stmt ();
5994 /* Parse the body of the do-statement. */
5995 parser->in_iteration_statement_p = true;
5996 cp_parser_implicitly_scoped_statement (parser);
5997 parser->in_iteration_statement_p = in_iteration_statement_p;
5998 finish_do_body (statement);
5999 /* Look for the `while' keyword. */
6000 cp_parser_require_keyword (parser, RID_WHILE, "`while'");
6001 /* Look for the `('. */
6002 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6003 /* Parse the expression. */
6004 expression = cp_parser_expression (parser);
6005 /* We're done with the do-statement. */
6006 finish_do_stmt (expression, statement);
6007 /* Look for the `)'. */
6008 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6009 /* Look for the `;'. */
6010 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6016 tree condition = NULL_TREE;
6017 tree expression = NULL_TREE;
6019 /* Begin the for-statement. */
6020 statement = begin_for_stmt ();
6021 /* Look for the `('. */
6022 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6023 /* Parse the initialization. */
6024 cp_parser_for_init_statement (parser);
6025 finish_for_init_stmt (statement);
6027 /* If there's a condition, process it. */
6028 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6029 condition = cp_parser_condition (parser);
6030 finish_for_cond (condition, statement);
6031 /* Look for the `;'. */
6032 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6034 /* If there's an expression, process it. */
6035 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
6036 expression = cp_parser_expression (parser);
6037 finish_for_expr (expression, statement);
6038 /* Look for the `)'. */
6039 cp_parser_require (parser, CPP_CLOSE_PAREN, "`;'");
6041 /* Parse the body of the for-statement. */
6042 parser->in_iteration_statement_p = true;
6043 cp_parser_already_scoped_statement (parser);
6044 parser->in_iteration_statement_p = in_iteration_statement_p;
6046 /* We're done with the for-statement. */
6047 finish_for_stmt (statement);
6052 cp_parser_error (parser, "expected iteration-statement");
6053 statement = error_mark_node;
6060 /* Parse a for-init-statement.
6063 expression-statement
6064 simple-declaration */
6067 cp_parser_for_init_statement (cp_parser* parser)
6069 /* If the next token is a `;', then we have an empty
6070 expression-statement. Grammatically, this is also a
6071 simple-declaration, but an invalid one, because it does not
6072 declare anything. Therefore, if we did not handle this case
6073 specially, we would issue an error message about an invalid
6075 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6077 /* We're going to speculatively look for a declaration, falling back
6078 to an expression, if necessary. */
6079 cp_parser_parse_tentatively (parser);
6080 /* Parse the declaration. */
6081 cp_parser_simple_declaration (parser,
6082 /*function_definition_allowed_p=*/false);
6083 /* If the tentative parse failed, then we shall need to look for an
6084 expression-statement. */
6085 if (cp_parser_parse_definitely (parser))
6089 cp_parser_expression_statement (parser, false);
6092 /* Parse a jump-statement.
6097 return expression [opt] ;
6105 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_STMT, or
6109 cp_parser_jump_statement (cp_parser* parser)
6111 tree statement = error_mark_node;
6115 /* Peek at the next token. */
6116 token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
6118 return error_mark_node;
6120 /* See what kind of keyword it is. */
6121 keyword = token->keyword;
6125 if (!parser->in_switch_statement_p
6126 && !parser->in_iteration_statement_p)
6128 error ("break statement not within loop or switch");
6129 statement = error_mark_node;
6132 statement = finish_break_stmt ();
6133 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6137 if (!parser->in_iteration_statement_p)
6139 error ("continue statement not within a loop");
6140 statement = error_mark_node;
6143 statement = finish_continue_stmt ();
6144 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6151 /* If the next token is a `;', then there is no
6153 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6154 expr = cp_parser_expression (parser);
6157 /* Build the return-statement. */
6158 statement = finish_return_stmt (expr);
6159 /* Look for the final `;'. */
6160 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6165 /* Create the goto-statement. */
6166 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
6168 /* Issue a warning about this use of a GNU extension. */
6170 pedwarn ("ISO C++ forbids computed gotos");
6171 /* Consume the '*' token. */
6172 cp_lexer_consume_token (parser->lexer);
6173 /* Parse the dependent expression. */
6174 finish_goto_stmt (cp_parser_expression (parser));
6177 finish_goto_stmt (cp_parser_identifier (parser));
6178 /* Look for the final `;'. */
6179 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6183 cp_parser_error (parser, "expected jump-statement");
6190 /* Parse a declaration-statement.
6192 declaration-statement:
6193 block-declaration */
6196 cp_parser_declaration_statement (cp_parser* parser)
6198 /* Parse the block-declaration. */
6199 cp_parser_block_declaration (parser, /*statement_p=*/true);
6201 /* Finish off the statement. */
6205 /* Some dependent statements (like `if (cond) statement'), are
6206 implicitly in their own scope. In other words, if the statement is
6207 a single statement (as opposed to a compound-statement), it is
6208 none-the-less treated as if it were enclosed in braces. Any
6209 declarations appearing in the dependent statement are out of scope
6210 after control passes that point. This function parses a statement,
6211 but ensures that is in its own scope, even if it is not a
6214 Returns the new statement. */
6217 cp_parser_implicitly_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))
6224 /* Create a compound-statement. */
6225 statement = begin_compound_stmt (/*has_no_scope=*/false);
6226 /* Parse the dependent-statement. */
6227 cp_parser_statement (parser, false);
6228 /* Finish the dummy compound-statement. */
6229 finish_compound_stmt (statement);
6231 /* Otherwise, we simply parse the statement directly. */
6233 statement = cp_parser_compound_statement (parser, false);
6235 /* Return the statement. */
6239 /* For some dependent statements (like `while (cond) statement'), we
6240 have already created a scope. Therefore, even if the dependent
6241 statement is a compound-statement, we do not want to create another
6245 cp_parser_already_scoped_statement (cp_parser* parser)
6247 /* If the token is not a `{', then we must take special action. */
6248 if (cp_lexer_next_token_is_not(parser->lexer, CPP_OPEN_BRACE))
6252 /* Create a compound-statement. */
6253 statement = begin_compound_stmt (/*has_no_scope=*/true);
6254 /* Parse the dependent-statement. */
6255 cp_parser_statement (parser, false);
6256 /* Finish the dummy compound-statement. */
6257 finish_compound_stmt (statement);
6259 /* Otherwise, we simply parse the statement directly. */
6261 cp_parser_statement (parser, false);
6264 /* Declarations [gram.dcl.dcl] */
6266 /* Parse an optional declaration-sequence.
6270 declaration-seq declaration */
6273 cp_parser_declaration_seq_opt (cp_parser* parser)
6279 token = cp_lexer_peek_token (parser->lexer);
6281 if (token->type == CPP_CLOSE_BRACE
6282 || token->type == CPP_EOF)
6285 if (token->type == CPP_SEMICOLON)
6287 /* A declaration consisting of a single semicolon is
6288 invalid. Allow it unless we're being pedantic. */
6289 if (pedantic && !in_system_header)
6290 pedwarn ("extra `;'");
6291 cp_lexer_consume_token (parser->lexer);
6295 /* The C lexer modifies PENDING_LANG_CHANGE when it wants the
6296 parser to enter or exit implicit `extern "C"' blocks. */
6297 while (pending_lang_change > 0)
6299 push_lang_context (lang_name_c);
6300 --pending_lang_change;
6302 while (pending_lang_change < 0)
6304 pop_lang_context ();
6305 ++pending_lang_change;
6308 /* Parse the declaration itself. */
6309 cp_parser_declaration (parser);
6313 /* Parse a declaration.
6318 template-declaration
6319 explicit-instantiation
6320 explicit-specialization
6321 linkage-specification
6322 namespace-definition
6327 __extension__ declaration */
6330 cp_parser_declaration (cp_parser* parser)
6336 /* Set this here since we can be called after
6337 pushing the linkage specification. */
6338 c_lex_string_translate = true;
6340 /* Check for the `__extension__' keyword. */
6341 if (cp_parser_extension_opt (parser, &saved_pedantic))
6343 /* Parse the qualified declaration. */
6344 cp_parser_declaration (parser);
6345 /* Restore the PEDANTIC flag. */
6346 pedantic = saved_pedantic;
6351 /* Try to figure out what kind of declaration is present. */
6352 token1 = *cp_lexer_peek_token (parser->lexer);
6354 /* Don't translate the CPP_STRING in extern "C". */
6355 if (token1.keyword == RID_EXTERN)
6356 c_lex_string_translate = false;
6358 if (token1.type != CPP_EOF)
6359 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
6361 /* If the next token is `extern' and the following token is a string
6362 literal, then we have a linkage specification. */
6363 if (token1.keyword == RID_EXTERN
6364 && cp_parser_is_string_literal (&token2))
6365 cp_parser_linkage_specification (parser);
6366 /* If the next token is `template', then we have either a template
6367 declaration, an explicit instantiation, or an explicit
6369 else if (token1.keyword == RID_TEMPLATE)
6371 /* `template <>' indicates a template specialization. */
6372 if (token2.type == CPP_LESS
6373 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
6374 cp_parser_explicit_specialization (parser);
6375 /* `template <' indicates a template declaration. */
6376 else if (token2.type == CPP_LESS)
6377 cp_parser_template_declaration (parser, /*member_p=*/false);
6378 /* Anything else must be an explicit instantiation. */
6380 cp_parser_explicit_instantiation (parser);
6382 /* If the next token is `export', then we have a template
6384 else if (token1.keyword == RID_EXPORT)
6385 cp_parser_template_declaration (parser, /*member_p=*/false);
6386 /* If the next token is `extern', 'static' or 'inline' and the one
6387 after that is `template', we have a GNU extended explicit
6388 instantiation directive. */
6389 else if (cp_parser_allow_gnu_extensions_p (parser)
6390 && (token1.keyword == RID_EXTERN
6391 || token1.keyword == RID_STATIC
6392 || token1.keyword == RID_INLINE)
6393 && token2.keyword == RID_TEMPLATE)
6394 cp_parser_explicit_instantiation (parser);
6395 /* If the next token is `namespace', check for a named or unnamed
6396 namespace definition. */
6397 else if (token1.keyword == RID_NAMESPACE
6398 && (/* A named namespace definition. */
6399 (token2.type == CPP_NAME
6400 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
6402 /* An unnamed namespace definition. */
6403 || token2.type == CPP_OPEN_BRACE))
6404 cp_parser_namespace_definition (parser);
6405 /* We must have either a block declaration or a function
6408 /* Try to parse a block-declaration, or a function-definition. */
6409 cp_parser_block_declaration (parser, /*statement_p=*/false);
6411 c_lex_string_translate = true;
6414 /* Parse a block-declaration.
6419 namespace-alias-definition
6426 __extension__ block-declaration
6429 If STATEMENT_P is TRUE, then this block-declaration is occurring as
6430 part of a declaration-statement. */
6433 cp_parser_block_declaration (cp_parser *parser,
6439 /* Check for the `__extension__' keyword. */
6440 if (cp_parser_extension_opt (parser, &saved_pedantic))
6442 /* Parse the qualified declaration. */
6443 cp_parser_block_declaration (parser, statement_p);
6444 /* Restore the PEDANTIC flag. */
6445 pedantic = saved_pedantic;
6450 /* Peek at the next token to figure out which kind of declaration is
6452 token1 = cp_lexer_peek_token (parser->lexer);
6454 /* If the next keyword is `asm', we have an asm-definition. */
6455 if (token1->keyword == RID_ASM)
6458 cp_parser_commit_to_tentative_parse (parser);
6459 cp_parser_asm_definition (parser);
6461 /* If the next keyword is `namespace', we have a
6462 namespace-alias-definition. */
6463 else if (token1->keyword == RID_NAMESPACE)
6464 cp_parser_namespace_alias_definition (parser);
6465 /* If the next keyword is `using', we have either a
6466 using-declaration or a using-directive. */
6467 else if (token1->keyword == RID_USING)
6472 cp_parser_commit_to_tentative_parse (parser);
6473 /* If the token after `using' is `namespace', then we have a
6475 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
6476 if (token2->keyword == RID_NAMESPACE)
6477 cp_parser_using_directive (parser);
6478 /* Otherwise, it's a using-declaration. */
6480 cp_parser_using_declaration (parser);
6482 /* If the next keyword is `__label__' we have a label declaration. */
6483 else if (token1->keyword == RID_LABEL)
6486 cp_parser_commit_to_tentative_parse (parser);
6487 cp_parser_label_declaration (parser);
6489 /* Anything else must be a simple-declaration. */
6491 cp_parser_simple_declaration (parser, !statement_p);
6494 /* Parse a simple-declaration.
6497 decl-specifier-seq [opt] init-declarator-list [opt] ;
6499 init-declarator-list:
6501 init-declarator-list , init-declarator
6503 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
6504 function-definition as a simple-declaration. */
6507 cp_parser_simple_declaration (cp_parser* parser,
6508 bool function_definition_allowed_p)
6510 tree decl_specifiers;
6512 int declares_class_or_enum;
6513 bool saw_declarator;
6515 /* Defer access checks until we know what is being declared; the
6516 checks for names appearing in the decl-specifier-seq should be
6517 done as if we were in the scope of the thing being declared. */
6518 push_deferring_access_checks (dk_deferred);
6520 /* Parse the decl-specifier-seq. We have to keep track of whether
6521 or not the decl-specifier-seq declares a named class or
6522 enumeration type, since that is the only case in which the
6523 init-declarator-list is allowed to be empty.
6527 In a simple-declaration, the optional init-declarator-list can be
6528 omitted only when declaring a class or enumeration, that is when
6529 the decl-specifier-seq contains either a class-specifier, an
6530 elaborated-type-specifier, or an enum-specifier. */
6532 = cp_parser_decl_specifier_seq (parser,
6533 CP_PARSER_FLAGS_OPTIONAL,
6535 &declares_class_or_enum);
6536 /* We no longer need to defer access checks. */
6537 stop_deferring_access_checks ();
6539 /* In a block scope, a valid declaration must always have a
6540 decl-specifier-seq. By not trying to parse declarators, we can
6541 resolve the declaration/expression ambiguity more quickly. */
6542 if (!function_definition_allowed_p && !decl_specifiers)
6544 cp_parser_error (parser, "expected declaration");
6548 /* If the next two tokens are both identifiers, the code is
6549 erroneous. The usual cause of this situation is code like:
6553 where "T" should name a type -- but does not. */
6554 if (cp_parser_parse_and_diagnose_invalid_type_name (parser))
6556 /* If parsing tentatively, we should commit; we really are
6557 looking at a declaration. */
6558 cp_parser_commit_to_tentative_parse (parser);
6563 /* Keep going until we hit the `;' at the end of the simple
6565 saw_declarator = false;
6566 while (cp_lexer_next_token_is_not (parser->lexer,
6570 bool function_definition_p;
6573 saw_declarator = true;
6574 /* Parse the init-declarator. */
6575 decl = cp_parser_init_declarator (parser, decl_specifiers, attributes,
6576 function_definition_allowed_p,
6578 declares_class_or_enum,
6579 &function_definition_p);
6580 /* If an error occurred while parsing tentatively, exit quickly.
6581 (That usually happens when in the body of a function; each
6582 statement is treated as a declaration-statement until proven
6584 if (cp_parser_error_occurred (parser))
6586 /* Handle function definitions specially. */
6587 if (function_definition_p)
6589 /* If the next token is a `,', then we are probably
6590 processing something like:
6594 which is erroneous. */
6595 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
6596 error ("mixing declarations and function-definitions is forbidden");
6597 /* Otherwise, we're done with the list of declarators. */
6600 pop_deferring_access_checks ();
6604 /* The next token should be either a `,' or a `;'. */
6605 token = cp_lexer_peek_token (parser->lexer);
6606 /* If it's a `,', there are more declarators to come. */
6607 if (token->type == CPP_COMMA)
6608 cp_lexer_consume_token (parser->lexer);
6609 /* If it's a `;', we are done. */
6610 else if (token->type == CPP_SEMICOLON)
6612 /* Anything else is an error. */
6615 cp_parser_error (parser, "expected `,' or `;'");
6616 /* Skip tokens until we reach the end of the statement. */
6617 cp_parser_skip_to_end_of_statement (parser);
6618 /* If the next token is now a `;', consume it. */
6619 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
6620 cp_lexer_consume_token (parser->lexer);
6623 /* After the first time around, a function-definition is not
6624 allowed -- even if it was OK at first. For example:
6629 function_definition_allowed_p = false;
6632 /* Issue an error message if no declarators are present, and the
6633 decl-specifier-seq does not itself declare a class or
6635 if (!saw_declarator)
6637 if (cp_parser_declares_only_class_p (parser))
6638 shadow_tag (decl_specifiers);
6639 /* Perform any deferred access checks. */
6640 perform_deferred_access_checks ();
6643 /* Consume the `;'. */
6644 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6647 pop_deferring_access_checks ();
6650 /* Parse a decl-specifier-seq.
6653 decl-specifier-seq [opt] decl-specifier
6656 storage-class-specifier
6665 decl-specifier-seq [opt] attributes
6667 Returns a TREE_LIST, giving the decl-specifiers in the order they
6668 appear in the source code. The TREE_VALUE of each node is the
6669 decl-specifier. For a keyword (such as `auto' or `friend'), the
6670 TREE_VALUE is simply the corresponding TREE_IDENTIFIER. For the
6671 representation of a type-specifier, see cp_parser_type_specifier.
6673 If there are attributes, they will be stored in *ATTRIBUTES,
6674 represented as described above cp_parser_attributes.
6676 If FRIEND_IS_NOT_CLASS_P is non-NULL, and the `friend' specifier
6677 appears, and the entity that will be a friend is not going to be a
6678 class, then *FRIEND_IS_NOT_CLASS_P will be set to TRUE. Note that
6679 even if *FRIEND_IS_NOT_CLASS_P is FALSE, the entity to which
6680 friendship is granted might not be a class.
6682 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
6685 1: one of the decl-specifiers is an elaborated-type-specifier
6686 (i.e., a type declaration)
6687 2: one of the decl-specifiers is an enum-specifier or a
6688 class-specifier (i.e., a type definition)
6693 cp_parser_decl_specifier_seq (cp_parser* parser,
6694 cp_parser_flags flags,
6696 int* declares_class_or_enum)
6698 tree decl_specs = NULL_TREE;
6699 bool friend_p = false;
6700 bool constructor_possible_p = !parser->in_declarator_p;
6702 /* Assume no class or enumeration type is declared. */
6703 *declares_class_or_enum = 0;
6705 /* Assume there are no attributes. */
6706 *attributes = NULL_TREE;
6708 /* Keep reading specifiers until there are no more to read. */
6711 tree decl_spec = NULL_TREE;
6715 /* Peek at the next token. */
6716 token = cp_lexer_peek_token (parser->lexer);
6717 /* Handle attributes. */
6718 if (token->keyword == RID_ATTRIBUTE)
6720 /* Parse the attributes. */
6721 decl_spec = cp_parser_attributes_opt (parser);
6722 /* Add them to the list. */
6723 *attributes = chainon (*attributes, decl_spec);
6726 /* If the next token is an appropriate keyword, we can simply
6727 add it to the list. */
6728 switch (token->keyword)
6734 error ("duplicate `friend'");
6737 /* The representation of the specifier is simply the
6738 appropriate TREE_IDENTIFIER node. */
6739 decl_spec = token->value;
6740 /* Consume the token. */
6741 cp_lexer_consume_token (parser->lexer);
6744 /* function-specifier:
6751 decl_spec = cp_parser_function_specifier_opt (parser);
6757 /* The representation of the specifier is simply the
6758 appropriate TREE_IDENTIFIER node. */
6759 decl_spec = token->value;
6760 /* Consume the token. */
6761 cp_lexer_consume_token (parser->lexer);
6762 /* A constructor declarator cannot appear in a typedef. */
6763 constructor_possible_p = false;
6764 /* The "typedef" keyword can only occur in a declaration; we
6765 may as well commit at this point. */
6766 cp_parser_commit_to_tentative_parse (parser);
6769 /* storage-class-specifier:
6784 decl_spec = cp_parser_storage_class_specifier_opt (parser);
6791 /* Constructors are a special case. The `S' in `S()' is not a
6792 decl-specifier; it is the beginning of the declarator. */
6793 constructor_p = (!decl_spec
6794 && constructor_possible_p
6795 && cp_parser_constructor_declarator_p (parser,
6798 /* If we don't have a DECL_SPEC yet, then we must be looking at
6799 a type-specifier. */
6800 if (!decl_spec && !constructor_p)
6802 int decl_spec_declares_class_or_enum;
6803 bool is_cv_qualifier;
6806 = cp_parser_type_specifier (parser, flags,
6808 /*is_declaration=*/true,
6809 &decl_spec_declares_class_or_enum,
6812 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
6814 /* If this type-specifier referenced a user-defined type
6815 (a typedef, class-name, etc.), then we can't allow any
6816 more such type-specifiers henceforth.
6820 The longest sequence of decl-specifiers that could
6821 possibly be a type name is taken as the
6822 decl-specifier-seq of a declaration. The sequence shall
6823 be self-consistent as described below.
6827 As a general rule, at most one type-specifier is allowed
6828 in the complete decl-specifier-seq of a declaration. The
6829 only exceptions are the following:
6831 -- const or volatile can be combined with any other
6834 -- signed or unsigned can be combined with char, long,
6842 void g (const int Pc);
6844 Here, Pc is *not* part of the decl-specifier seq; it's
6845 the declarator. Therefore, once we see a type-specifier
6846 (other than a cv-qualifier), we forbid any additional
6847 user-defined types. We *do* still allow things like `int
6848 int' to be considered a decl-specifier-seq, and issue the
6849 error message later. */
6850 if (decl_spec && !is_cv_qualifier)
6851 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
6852 /* A constructor declarator cannot follow a type-specifier. */
6854 constructor_possible_p = false;
6857 /* If we still do not have a DECL_SPEC, then there are no more
6861 /* Issue an error message, unless the entire construct was
6863 if (!(flags & CP_PARSER_FLAGS_OPTIONAL))
6865 cp_parser_error (parser, "expected decl specifier");
6866 return error_mark_node;
6872 /* Add the DECL_SPEC to the list of specifiers. */
6873 if (decl_specs == NULL || TREE_VALUE (decl_specs) != error_mark_node)
6874 decl_specs = tree_cons (NULL_TREE, decl_spec, decl_specs);
6876 /* After we see one decl-specifier, further decl-specifiers are
6878 flags |= CP_PARSER_FLAGS_OPTIONAL;
6881 /* Don't allow a friend specifier with a class definition. */
6882 if (friend_p && (*declares_class_or_enum & 2))
6883 error ("class definition may not be declared a friend");
6885 /* We have built up the DECL_SPECS in reverse order. Return them in
6886 the correct order. */
6887 return nreverse (decl_specs);
6890 /* Parse an (optional) storage-class-specifier.
6892 storage-class-specifier:
6901 storage-class-specifier:
6904 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
6907 cp_parser_storage_class_specifier_opt (cp_parser* parser)
6909 switch (cp_lexer_peek_token (parser->lexer)->keyword)
6917 /* Consume the token. */
6918 return cp_lexer_consume_token (parser->lexer)->value;
6925 /* Parse an (optional) function-specifier.
6932 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
6935 cp_parser_function_specifier_opt (cp_parser* parser)
6937 switch (cp_lexer_peek_token (parser->lexer)->keyword)
6942 /* Consume the token. */
6943 return cp_lexer_consume_token (parser->lexer)->value;
6950 /* Parse a linkage-specification.
6952 linkage-specification:
6953 extern string-literal { declaration-seq [opt] }
6954 extern string-literal declaration */
6957 cp_parser_linkage_specification (cp_parser* parser)
6962 /* Look for the `extern' keyword. */
6963 cp_parser_require_keyword (parser, RID_EXTERN, "`extern'");
6965 /* Peek at the next token. */
6966 token = cp_lexer_peek_token (parser->lexer);
6967 /* If it's not a string-literal, then there's a problem. */
6968 if (!cp_parser_is_string_literal (token))
6970 cp_parser_error (parser, "expected language-name");
6973 /* Consume the token. */
6974 cp_lexer_consume_token (parser->lexer);
6976 /* Transform the literal into an identifier. If the literal is a
6977 wide-character string, or contains embedded NULs, then we can't
6978 handle it as the user wants. */
6979 if (token->type == CPP_WSTRING
6980 || (strlen (TREE_STRING_POINTER (token->value))
6981 != (size_t) (TREE_STRING_LENGTH (token->value) - 1)))
6983 cp_parser_error (parser, "invalid linkage-specification");
6984 /* Assume C++ linkage. */
6985 linkage = get_identifier ("c++");
6987 /* If it's a simple string constant, things are easier. */
6989 linkage = get_identifier (TREE_STRING_POINTER (token->value));
6991 /* We're now using the new linkage. */
6992 push_lang_context (linkage);
6994 /* If the next token is a `{', then we're using the first
6996 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6998 /* Consume the `{' token. */
6999 cp_lexer_consume_token (parser->lexer);
7000 /* Parse the declarations. */
7001 cp_parser_declaration_seq_opt (parser);
7002 /* Look for the closing `}'. */
7003 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
7005 /* Otherwise, there's just one declaration. */
7008 bool saved_in_unbraced_linkage_specification_p;
7010 saved_in_unbraced_linkage_specification_p
7011 = parser->in_unbraced_linkage_specification_p;
7012 parser->in_unbraced_linkage_specification_p = true;
7013 have_extern_spec = true;
7014 cp_parser_declaration (parser);
7015 have_extern_spec = false;
7016 parser->in_unbraced_linkage_specification_p
7017 = saved_in_unbraced_linkage_specification_p;
7020 /* We're done with the linkage-specification. */
7021 pop_lang_context ();
7024 /* Special member functions [gram.special] */
7026 /* Parse a conversion-function-id.
7028 conversion-function-id:
7029 operator conversion-type-id
7031 Returns an IDENTIFIER_NODE representing the operator. */
7034 cp_parser_conversion_function_id (cp_parser* parser)
7038 tree saved_qualifying_scope;
7039 tree saved_object_scope;
7042 /* Look for the `operator' token. */
7043 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
7044 return error_mark_node;
7045 /* When we parse the conversion-type-id, the current scope will be
7046 reset. However, we need that information in able to look up the
7047 conversion function later, so we save it here. */
7048 saved_scope = parser->scope;
7049 saved_qualifying_scope = parser->qualifying_scope;
7050 saved_object_scope = parser->object_scope;
7051 /* We must enter the scope of the class so that the names of
7052 entities declared within the class are available in the
7053 conversion-type-id. For example, consider:
7060 S::operator I() { ... }
7062 In order to see that `I' is a type-name in the definition, we
7063 must be in the scope of `S'. */
7065 pop_p = push_scope (saved_scope);
7066 /* Parse the conversion-type-id. */
7067 type = cp_parser_conversion_type_id (parser);
7068 /* Leave the scope of the class, if any. */
7070 pop_scope (saved_scope);
7071 /* Restore the saved scope. */
7072 parser->scope = saved_scope;
7073 parser->qualifying_scope = saved_qualifying_scope;
7074 parser->object_scope = saved_object_scope;
7075 /* If the TYPE is invalid, indicate failure. */
7076 if (type == error_mark_node)
7077 return error_mark_node;
7078 return mangle_conv_op_name_for_type (type);
7081 /* Parse a conversion-type-id:
7084 type-specifier-seq conversion-declarator [opt]
7086 Returns the TYPE specified. */
7089 cp_parser_conversion_type_id (cp_parser* parser)
7092 tree type_specifiers;
7095 /* Parse the attributes. */
7096 attributes = cp_parser_attributes_opt (parser);
7097 /* Parse the type-specifiers. */
7098 type_specifiers = cp_parser_type_specifier_seq (parser);
7099 /* If that didn't work, stop. */
7100 if (type_specifiers == error_mark_node)
7101 return error_mark_node;
7102 /* Parse the conversion-declarator. */
7103 declarator = cp_parser_conversion_declarator_opt (parser);
7105 return grokdeclarator (declarator, type_specifiers, TYPENAME,
7106 /*initialized=*/0, &attributes);
7109 /* Parse an (optional) conversion-declarator.
7111 conversion-declarator:
7112 ptr-operator conversion-declarator [opt]
7114 Returns a representation of the declarator. See
7115 cp_parser_declarator for details. */
7118 cp_parser_conversion_declarator_opt (cp_parser* parser)
7120 enum tree_code code;
7122 tree cv_qualifier_seq;
7124 /* We don't know if there's a ptr-operator next, or not. */
7125 cp_parser_parse_tentatively (parser);
7126 /* Try the ptr-operator. */
7127 code = cp_parser_ptr_operator (parser, &class_type,
7129 /* If it worked, look for more conversion-declarators. */
7130 if (cp_parser_parse_definitely (parser))
7134 /* Parse another optional declarator. */
7135 declarator = cp_parser_conversion_declarator_opt (parser);
7137 /* Create the representation of the declarator. */
7138 if (code == INDIRECT_REF)
7139 declarator = make_pointer_declarator (cv_qualifier_seq,
7142 declarator = make_reference_declarator (cv_qualifier_seq,
7145 /* Handle the pointer-to-member case. */
7147 declarator = build_nt (SCOPE_REF, class_type, declarator);
7155 /* Parse an (optional) ctor-initializer.
7158 : mem-initializer-list
7160 Returns TRUE iff the ctor-initializer was actually present. */
7163 cp_parser_ctor_initializer_opt (cp_parser* parser)
7165 /* If the next token is not a `:', then there is no
7166 ctor-initializer. */
7167 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
7169 /* Do default initialization of any bases and members. */
7170 if (DECL_CONSTRUCTOR_P (current_function_decl))
7171 finish_mem_initializers (NULL_TREE);
7176 /* Consume the `:' token. */
7177 cp_lexer_consume_token (parser->lexer);
7178 /* And the mem-initializer-list. */
7179 cp_parser_mem_initializer_list (parser);
7184 /* Parse a mem-initializer-list.
7186 mem-initializer-list:
7188 mem-initializer , mem-initializer-list */
7191 cp_parser_mem_initializer_list (cp_parser* parser)
7193 tree mem_initializer_list = NULL_TREE;
7195 /* Let the semantic analysis code know that we are starting the
7196 mem-initializer-list. */
7197 if (!DECL_CONSTRUCTOR_P (current_function_decl))
7198 error ("only constructors take base initializers");
7200 /* Loop through the list. */
7203 tree mem_initializer;
7205 /* Parse the mem-initializer. */
7206 mem_initializer = cp_parser_mem_initializer (parser);
7207 /* Add it to the list, unless it was erroneous. */
7208 if (mem_initializer)
7210 TREE_CHAIN (mem_initializer) = mem_initializer_list;
7211 mem_initializer_list = mem_initializer;
7213 /* If the next token is not a `,', we're done. */
7214 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7216 /* Consume the `,' token. */
7217 cp_lexer_consume_token (parser->lexer);
7220 /* Perform semantic analysis. */
7221 if (DECL_CONSTRUCTOR_P (current_function_decl))
7222 finish_mem_initializers (mem_initializer_list);
7225 /* Parse a mem-initializer.
7228 mem-initializer-id ( expression-list [opt] )
7233 ( expression-list [opt] )
7235 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
7236 class) or FIELD_DECL (for a non-static data member) to initialize;
7237 the TREE_VALUE is the expression-list. */
7240 cp_parser_mem_initializer (cp_parser* parser)
7242 tree mem_initializer_id;
7243 tree expression_list;
7246 /* Find out what is being initialized. */
7247 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7249 pedwarn ("anachronistic old-style base class initializer");
7250 mem_initializer_id = NULL_TREE;
7253 mem_initializer_id = cp_parser_mem_initializer_id (parser);
7254 member = expand_member_init (mem_initializer_id);
7255 if (member && !DECL_P (member))
7256 in_base_initializer = 1;
7259 = cp_parser_parenthesized_expression_list (parser, false,
7260 /*non_constant_p=*/NULL);
7261 if (!expression_list)
7262 expression_list = void_type_node;
7264 in_base_initializer = 0;
7266 return member ? build_tree_list (member, expression_list) : NULL_TREE;
7269 /* Parse a mem-initializer-id.
7272 :: [opt] nested-name-specifier [opt] class-name
7275 Returns a TYPE indicating the class to be initializer for the first
7276 production. Returns an IDENTIFIER_NODE indicating the data member
7277 to be initialized for the second production. */
7280 cp_parser_mem_initializer_id (cp_parser* parser)
7282 bool global_scope_p;
7283 bool nested_name_specifier_p;
7286 /* Look for the optional `::' operator. */
7288 = (cp_parser_global_scope_opt (parser,
7289 /*current_scope_valid_p=*/false)
7291 /* Look for the optional nested-name-specifier. The simplest way to
7296 The keyword `typename' is not permitted in a base-specifier or
7297 mem-initializer; in these contexts a qualified name that
7298 depends on a template-parameter is implicitly assumed to be a
7301 is to assume that we have seen the `typename' keyword at this
7303 nested_name_specifier_p
7304 = (cp_parser_nested_name_specifier_opt (parser,
7305 /*typename_keyword_p=*/true,
7306 /*check_dependency_p=*/true,
7308 /*is_declaration=*/true)
7310 /* If there is a `::' operator or a nested-name-specifier, then we
7311 are definitely looking for a class-name. */
7312 if (global_scope_p || nested_name_specifier_p)
7313 return cp_parser_class_name (parser,
7314 /*typename_keyword_p=*/true,
7315 /*template_keyword_p=*/false,
7317 /*check_dependency_p=*/true,
7318 /*class_head_p=*/false,
7319 /*is_declaration=*/true);
7320 /* Otherwise, we could also be looking for an ordinary identifier. */
7321 cp_parser_parse_tentatively (parser);
7322 /* Try a class-name. */
7323 id = cp_parser_class_name (parser,
7324 /*typename_keyword_p=*/true,
7325 /*template_keyword_p=*/false,
7327 /*check_dependency_p=*/true,
7328 /*class_head_p=*/false,
7329 /*is_declaration=*/true);
7330 /* If we found one, we're done. */
7331 if (cp_parser_parse_definitely (parser))
7333 /* Otherwise, look for an ordinary identifier. */
7334 return cp_parser_identifier (parser);
7337 /* Overloading [gram.over] */
7339 /* Parse an operator-function-id.
7341 operator-function-id:
7344 Returns an IDENTIFIER_NODE for the operator which is a
7345 human-readable spelling of the identifier, e.g., `operator +'. */
7348 cp_parser_operator_function_id (cp_parser* parser)
7350 /* Look for the `operator' keyword. */
7351 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
7352 return error_mark_node;
7353 /* And then the name of the operator itself. */
7354 return cp_parser_operator (parser);
7357 /* Parse an operator.
7360 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
7361 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
7362 || ++ -- , ->* -> () []
7369 Returns an IDENTIFIER_NODE for the operator which is a
7370 human-readable spelling of the identifier, e.g., `operator +'. */
7373 cp_parser_operator (cp_parser* parser)
7375 tree id = NULL_TREE;
7378 /* Peek at the next token. */
7379 token = cp_lexer_peek_token (parser->lexer);
7380 /* Figure out which operator we have. */
7381 switch (token->type)
7387 /* The keyword should be either `new' or `delete'. */
7388 if (token->keyword == RID_NEW)
7390 else if (token->keyword == RID_DELETE)
7395 /* Consume the `new' or `delete' token. */
7396 cp_lexer_consume_token (parser->lexer);
7398 /* Peek at the next token. */
7399 token = cp_lexer_peek_token (parser->lexer);
7400 /* If it's a `[' token then this is the array variant of the
7402 if (token->type == CPP_OPEN_SQUARE)
7404 /* Consume the `[' token. */
7405 cp_lexer_consume_token (parser->lexer);
7406 /* Look for the `]' token. */
7407 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
7408 id = ansi_opname (op == NEW_EXPR
7409 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
7411 /* Otherwise, we have the non-array variant. */
7413 id = ansi_opname (op);
7419 id = ansi_opname (PLUS_EXPR);
7423 id = ansi_opname (MINUS_EXPR);
7427 id = ansi_opname (MULT_EXPR);
7431 id = ansi_opname (TRUNC_DIV_EXPR);
7435 id = ansi_opname (TRUNC_MOD_EXPR);
7439 id = ansi_opname (BIT_XOR_EXPR);
7443 id = ansi_opname (BIT_AND_EXPR);
7447 id = ansi_opname (BIT_IOR_EXPR);
7451 id = ansi_opname (BIT_NOT_EXPR);
7455 id = ansi_opname (TRUTH_NOT_EXPR);
7459 id = ansi_assopname (NOP_EXPR);
7463 id = ansi_opname (LT_EXPR);
7467 id = ansi_opname (GT_EXPR);
7471 id = ansi_assopname (PLUS_EXPR);
7475 id = ansi_assopname (MINUS_EXPR);
7479 id = ansi_assopname (MULT_EXPR);
7483 id = ansi_assopname (TRUNC_DIV_EXPR);
7487 id = ansi_assopname (TRUNC_MOD_EXPR);
7491 id = ansi_assopname (BIT_XOR_EXPR);
7495 id = ansi_assopname (BIT_AND_EXPR);
7499 id = ansi_assopname (BIT_IOR_EXPR);
7503 id = ansi_opname (LSHIFT_EXPR);
7507 id = ansi_opname (RSHIFT_EXPR);
7511 id = ansi_assopname (LSHIFT_EXPR);
7515 id = ansi_assopname (RSHIFT_EXPR);
7519 id = ansi_opname (EQ_EXPR);
7523 id = ansi_opname (NE_EXPR);
7527 id = ansi_opname (LE_EXPR);
7530 case CPP_GREATER_EQ:
7531 id = ansi_opname (GE_EXPR);
7535 id = ansi_opname (TRUTH_ANDIF_EXPR);
7539 id = ansi_opname (TRUTH_ORIF_EXPR);
7543 id = ansi_opname (POSTINCREMENT_EXPR);
7546 case CPP_MINUS_MINUS:
7547 id = ansi_opname (PREDECREMENT_EXPR);
7551 id = ansi_opname (COMPOUND_EXPR);
7554 case CPP_DEREF_STAR:
7555 id = ansi_opname (MEMBER_REF);
7559 id = ansi_opname (COMPONENT_REF);
7562 case CPP_OPEN_PAREN:
7563 /* Consume the `('. */
7564 cp_lexer_consume_token (parser->lexer);
7565 /* Look for the matching `)'. */
7566 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
7567 return ansi_opname (CALL_EXPR);
7569 case CPP_OPEN_SQUARE:
7570 /* Consume the `['. */
7571 cp_lexer_consume_token (parser->lexer);
7572 /* Look for the matching `]'. */
7573 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
7574 return ansi_opname (ARRAY_REF);
7578 id = ansi_opname (MIN_EXPR);
7582 id = ansi_opname (MAX_EXPR);
7586 id = ansi_assopname (MIN_EXPR);
7590 id = ansi_assopname (MAX_EXPR);
7594 /* Anything else is an error. */
7598 /* If we have selected an identifier, we need to consume the
7601 cp_lexer_consume_token (parser->lexer);
7602 /* Otherwise, no valid operator name was present. */
7605 cp_parser_error (parser, "expected operator");
7606 id = error_mark_node;
7612 /* Parse a template-declaration.
7614 template-declaration:
7615 export [opt] template < template-parameter-list > declaration
7617 If MEMBER_P is TRUE, this template-declaration occurs within a
7620 The grammar rule given by the standard isn't correct. What
7623 template-declaration:
7624 export [opt] template-parameter-list-seq
7625 decl-specifier-seq [opt] init-declarator [opt] ;
7626 export [opt] template-parameter-list-seq
7629 template-parameter-list-seq:
7630 template-parameter-list-seq [opt]
7631 template < template-parameter-list > */
7634 cp_parser_template_declaration (cp_parser* parser, bool member_p)
7636 /* Check for `export'. */
7637 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
7639 /* Consume the `export' token. */
7640 cp_lexer_consume_token (parser->lexer);
7641 /* Warn that we do not support `export'. */
7642 warning ("keyword `export' not implemented, and will be ignored");
7645 cp_parser_template_declaration_after_export (parser, member_p);
7648 /* Parse a template-parameter-list.
7650 template-parameter-list:
7652 template-parameter-list , template-parameter
7654 Returns a TREE_LIST. Each node represents a template parameter.
7655 The nodes are connected via their TREE_CHAINs. */
7658 cp_parser_template_parameter_list (cp_parser* parser)
7660 tree parameter_list = NULL_TREE;
7667 /* Parse the template-parameter. */
7668 parameter = cp_parser_template_parameter (parser);
7669 /* Add it to the list. */
7670 parameter_list = process_template_parm (parameter_list,
7673 /* Peek at the next token. */
7674 token = cp_lexer_peek_token (parser->lexer);
7675 /* If it's not a `,', we're done. */
7676 if (token->type != CPP_COMMA)
7678 /* Otherwise, consume the `,' token. */
7679 cp_lexer_consume_token (parser->lexer);
7682 return parameter_list;
7685 /* Parse a template-parameter.
7689 parameter-declaration
7691 Returns a TREE_LIST. The TREE_VALUE represents the parameter. The
7692 TREE_PURPOSE is the default value, if any. */
7695 cp_parser_template_parameter (cp_parser* parser)
7699 /* Peek at the next token. */
7700 token = cp_lexer_peek_token (parser->lexer);
7701 /* If it is `class' or `template', we have a type-parameter. */
7702 if (token->keyword == RID_TEMPLATE)
7703 return cp_parser_type_parameter (parser);
7704 /* If it is `class' or `typename' we do not know yet whether it is a
7705 type parameter or a non-type parameter. Consider:
7707 template <typename T, typename T::X X> ...
7711 template <class C, class D*> ...
7713 Here, the first parameter is a type parameter, and the second is
7714 a non-type parameter. We can tell by looking at the token after
7715 the identifier -- if it is a `,', `=', or `>' then we have a type
7717 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
7719 /* Peek at the token after `class' or `typename'. */
7720 token = cp_lexer_peek_nth_token (parser->lexer, 2);
7721 /* If it's an identifier, skip it. */
7722 if (token->type == CPP_NAME)
7723 token = cp_lexer_peek_nth_token (parser->lexer, 3);
7724 /* Now, see if the token looks like the end of a template
7726 if (token->type == CPP_COMMA
7727 || token->type == CPP_EQ
7728 || token->type == CPP_GREATER)
7729 return cp_parser_type_parameter (parser);
7732 /* Otherwise, it is a non-type parameter.
7736 When parsing a default template-argument for a non-type
7737 template-parameter, the first non-nested `>' is taken as the end
7738 of the template parameter-list rather than a greater-than
7741 cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
7742 /*parenthesized_p=*/NULL);
7745 /* Parse a type-parameter.
7748 class identifier [opt]
7749 class identifier [opt] = type-id
7750 typename identifier [opt]
7751 typename identifier [opt] = type-id
7752 template < template-parameter-list > class identifier [opt]
7753 template < template-parameter-list > class identifier [opt]
7756 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
7757 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
7758 the declaration of the parameter. */
7761 cp_parser_type_parameter (cp_parser* parser)
7766 /* Look for a keyword to tell us what kind of parameter this is. */
7767 token = cp_parser_require (parser, CPP_KEYWORD,
7768 "`class', `typename', or `template'");
7770 return error_mark_node;
7772 switch (token->keyword)
7778 tree default_argument;
7780 /* If the next token is an identifier, then it names the
7782 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
7783 identifier = cp_parser_identifier (parser);
7785 identifier = NULL_TREE;
7787 /* Create the parameter. */
7788 parameter = finish_template_type_parm (class_type_node, identifier);
7790 /* If the next token is an `=', we have a default argument. */
7791 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7793 /* Consume the `=' token. */
7794 cp_lexer_consume_token (parser->lexer);
7795 /* Parse the default-argument. */
7796 default_argument = cp_parser_type_id (parser);
7799 default_argument = NULL_TREE;
7801 /* Create the combined representation of the parameter and the
7802 default argument. */
7803 parameter = build_tree_list (default_argument, parameter);
7809 tree parameter_list;
7811 tree default_argument;
7813 /* Look for the `<'. */
7814 cp_parser_require (parser, CPP_LESS, "`<'");
7815 /* Parse the template-parameter-list. */
7816 begin_template_parm_list ();
7818 = cp_parser_template_parameter_list (parser);
7819 parameter_list = end_template_parm_list (parameter_list);
7820 /* Look for the `>'. */
7821 cp_parser_require (parser, CPP_GREATER, "`>'");
7822 /* Look for the `class' keyword. */
7823 cp_parser_require_keyword (parser, RID_CLASS, "`class'");
7824 /* If the next token is an `=', then there is a
7825 default-argument. If the next token is a `>', we are at
7826 the end of the parameter-list. If the next token is a `,',
7827 then we are at the end of this parameter. */
7828 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
7829 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
7830 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7831 identifier = cp_parser_identifier (parser);
7833 identifier = NULL_TREE;
7834 /* Create the template parameter. */
7835 parameter = finish_template_template_parm (class_type_node,
7838 /* If the next token is an `=', then there is a
7839 default-argument. */
7840 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7844 /* Consume the `='. */
7845 cp_lexer_consume_token (parser->lexer);
7846 /* Parse the id-expression. */
7848 = cp_parser_id_expression (parser,
7849 /*template_keyword_p=*/false,
7850 /*check_dependency_p=*/true,
7851 /*template_p=*/&is_template,
7852 /*declarator_p=*/false);
7853 if (TREE_CODE (default_argument) == TYPE_DECL)
7854 /* If the id-expression was a template-id that refers to
7855 a template-class, we already have the declaration here,
7856 so no further lookup is needed. */
7859 /* Look up the name. */
7861 = cp_parser_lookup_name (parser, default_argument,
7863 /*is_template=*/is_template,
7864 /*is_namespace=*/false,
7865 /*check_dependency=*/true);
7866 /* See if the default argument is valid. */
7868 = check_template_template_default_arg (default_argument);
7871 default_argument = NULL_TREE;
7873 /* Create the combined representation of the parameter and the
7874 default argument. */
7875 parameter = build_tree_list (default_argument, parameter);
7880 /* Anything else is an error. */
7881 cp_parser_error (parser,
7882 "expected `class', `typename', or `template'");
7883 parameter = error_mark_node;
7889 /* Parse a template-id.
7892 template-name < template-argument-list [opt] >
7894 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
7895 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
7896 returned. Otherwise, if the template-name names a function, or set
7897 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
7898 names a class, returns a TYPE_DECL for the specialization.
7900 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
7901 uninstantiated templates. */
7904 cp_parser_template_id (cp_parser *parser,
7905 bool template_keyword_p,
7906 bool check_dependency_p,
7907 bool is_declaration)
7912 ptrdiff_t start_of_id;
7913 tree access_check = NULL_TREE;
7914 cp_token *next_token, *next_token_2;
7917 /* If the next token corresponds to a template-id, there is no need
7919 next_token = cp_lexer_peek_token (parser->lexer);
7920 if (next_token->type == CPP_TEMPLATE_ID)
7925 /* Get the stored value. */
7926 value = cp_lexer_consume_token (parser->lexer)->value;
7927 /* Perform any access checks that were deferred. */
7928 for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
7929 perform_or_defer_access_check (TREE_PURPOSE (check),
7930 TREE_VALUE (check));
7931 /* Return the stored value. */
7932 return TREE_VALUE (value);
7935 /* Avoid performing name lookup if there is no possibility of
7936 finding a template-id. */
7937 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
7938 || (next_token->type == CPP_NAME
7939 && !cp_parser_nth_token_starts_template_argument_list_p
7942 cp_parser_error (parser, "expected template-id");
7943 return error_mark_node;
7946 /* Remember where the template-id starts. */
7947 if (cp_parser_parsing_tentatively (parser)
7948 && !cp_parser_committed_to_tentative_parse (parser))
7950 next_token = cp_lexer_peek_token (parser->lexer);
7951 start_of_id = cp_lexer_token_difference (parser->lexer,
7952 parser->lexer->first_token,
7958 push_deferring_access_checks (dk_deferred);
7960 /* Parse the template-name. */
7961 is_identifier = false;
7962 template = cp_parser_template_name (parser, template_keyword_p,
7966 if (template == error_mark_node || is_identifier)
7968 pop_deferring_access_checks ();
7972 /* If we find the sequence `[:' after a template-name, it's probably
7973 a digraph-typo for `< ::'. Substitute the tokens and check if we can
7974 parse correctly the argument list. */
7975 next_token = cp_lexer_peek_nth_token (parser->lexer, 1);
7976 next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
7977 if (next_token->type == CPP_OPEN_SQUARE
7978 && next_token->flags & DIGRAPH
7979 && next_token_2->type == CPP_COLON
7980 && !(next_token_2->flags & PREV_WHITE))
7982 cp_parser_parse_tentatively (parser);
7983 /* Change `:' into `::'. */
7984 next_token_2->type = CPP_SCOPE;
7985 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
7987 cp_lexer_consume_token (parser->lexer);
7988 /* Parse the arguments. */
7989 arguments = cp_parser_enclosed_template_argument_list (parser);
7990 if (!cp_parser_parse_definitely (parser))
7992 /* If we couldn't parse an argument list, then we revert our changes
7993 and return simply an error. Maybe this is not a template-id
7995 next_token_2->type = CPP_COLON;
7996 cp_parser_error (parser, "expected `<'");
7997 pop_deferring_access_checks ();
7998 return error_mark_node;
8000 /* Otherwise, emit an error about the invalid digraph, but continue
8001 parsing because we got our argument list. */
8002 pedwarn ("`<::' cannot begin a template-argument list");
8003 inform ("`<:' is an alternate spelling for `['. Insert whitespace "
8004 "between `<' and `::'");
8005 if (!flag_permissive)
8010 inform ("(if you use `-fpermissive' G++ will accept your code)");
8017 /* Look for the `<' that starts the template-argument-list. */
8018 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
8020 pop_deferring_access_checks ();
8021 return error_mark_node;
8023 /* Parse the arguments. */
8024 arguments = cp_parser_enclosed_template_argument_list (parser);
8027 /* Build a representation of the specialization. */
8028 if (TREE_CODE (template) == IDENTIFIER_NODE)
8029 template_id = build_min_nt (TEMPLATE_ID_EXPR, template, arguments);
8030 else if (DECL_CLASS_TEMPLATE_P (template)
8031 || DECL_TEMPLATE_TEMPLATE_PARM_P (template))
8033 = finish_template_type (template, arguments,
8034 cp_lexer_next_token_is (parser->lexer,
8038 /* If it's not a class-template or a template-template, it should be
8039 a function-template. */
8040 my_friendly_assert ((DECL_FUNCTION_TEMPLATE_P (template)
8041 || TREE_CODE (template) == OVERLOAD
8042 || BASELINK_P (template)),
8045 template_id = lookup_template_function (template, arguments);
8048 /* Retrieve any deferred checks. Do not pop this access checks yet
8049 so the memory will not be reclaimed during token replacing below. */
8050 access_check = get_deferred_access_checks ();
8052 /* If parsing tentatively, replace the sequence of tokens that makes
8053 up the template-id with a CPP_TEMPLATE_ID token. That way,
8054 should we re-parse the token stream, we will not have to repeat
8055 the effort required to do the parse, nor will we issue duplicate
8056 error messages about problems during instantiation of the
8058 if (start_of_id >= 0)
8062 /* Find the token that corresponds to the start of the
8064 token = cp_lexer_advance_token (parser->lexer,
8065 parser->lexer->first_token,
8068 /* Reset the contents of the START_OF_ID token. */
8069 token->type = CPP_TEMPLATE_ID;
8070 token->value = build_tree_list (access_check, template_id);
8071 token->keyword = RID_MAX;
8072 /* Purge all subsequent tokens. */
8073 cp_lexer_purge_tokens_after (parser->lexer, token);
8076 pop_deferring_access_checks ();
8080 /* Parse a template-name.
8085 The standard should actually say:
8089 operator-function-id
8091 A defect report has been filed about this issue.
8093 A conversion-function-id cannot be a template name because they cannot
8094 be part of a template-id. In fact, looking at this code:
8098 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
8099 It is impossible to call a templated conversion-function-id with an
8100 explicit argument list, since the only allowed template parameter is
8101 the type to which it is converting.
8103 If TEMPLATE_KEYWORD_P is true, then we have just seen the
8104 `template' keyword, in a construction like:
8108 In that case `f' is taken to be a template-name, even though there
8109 is no way of knowing for sure.
8111 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
8112 name refers to a set of overloaded functions, at least one of which
8113 is a template, or an IDENTIFIER_NODE with the name of the template,
8114 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
8115 names are looked up inside uninstantiated templates. */
8118 cp_parser_template_name (cp_parser* parser,
8119 bool template_keyword_p,
8120 bool check_dependency_p,
8121 bool is_declaration,
8122 bool *is_identifier)
8128 /* If the next token is `operator', then we have either an
8129 operator-function-id or a conversion-function-id. */
8130 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
8132 /* We don't know whether we're looking at an
8133 operator-function-id or a conversion-function-id. */
8134 cp_parser_parse_tentatively (parser);
8135 /* Try an operator-function-id. */
8136 identifier = cp_parser_operator_function_id (parser);
8137 /* If that didn't work, try a conversion-function-id. */
8138 if (!cp_parser_parse_definitely (parser))
8140 cp_parser_error (parser, "expected template-name");
8141 return error_mark_node;
8144 /* Look for the identifier. */
8146 identifier = cp_parser_identifier (parser);
8148 /* If we didn't find an identifier, we don't have a template-id. */
8149 if (identifier == error_mark_node)
8150 return error_mark_node;
8152 /* If the name immediately followed the `template' keyword, then it
8153 is a template-name. However, if the next token is not `<', then
8154 we do not treat it as a template-name, since it is not being used
8155 as part of a template-id. This enables us to handle constructs
8158 template <typename T> struct S { S(); };
8159 template <typename T> S<T>::S();
8161 correctly. We would treat `S' as a template -- if it were `S<T>'
8162 -- but we do not if there is no `<'. */
8164 if (processing_template_decl
8165 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
8167 /* In a declaration, in a dependent context, we pretend that the
8168 "template" keyword was present in order to improve error
8169 recovery. For example, given:
8171 template <typename T> void f(T::X<int>);
8173 we want to treat "X<int>" as a template-id. */
8175 && !template_keyword_p
8176 && parser->scope && TYPE_P (parser->scope)
8177 && dependent_type_p (parser->scope))
8181 /* Explain what went wrong. */
8182 error ("non-template `%D' used as template", identifier);
8183 error ("(use `%T::template %D' to indicate that it is a template)",
8184 parser->scope, identifier);
8185 /* If parsing tentatively, find the location of the "<"
8187 if (cp_parser_parsing_tentatively (parser)
8188 && !cp_parser_committed_to_tentative_parse (parser))
8190 cp_parser_simulate_error (parser);
8191 token = cp_lexer_peek_token (parser->lexer);
8192 token = cp_lexer_prev_token (parser->lexer, token);
8193 start = cp_lexer_token_difference (parser->lexer,
8194 parser->lexer->first_token,
8199 /* Parse the template arguments so that we can issue error
8200 messages about them. */
8201 cp_lexer_consume_token (parser->lexer);
8202 cp_parser_enclosed_template_argument_list (parser);
8203 /* Skip tokens until we find a good place from which to
8204 continue parsing. */
8205 cp_parser_skip_to_closing_parenthesis (parser,
8206 /*recovering=*/true,
8208 /*consume_paren=*/false);
8209 /* If parsing tentatively, permanently remove the
8210 template argument list. That will prevent duplicate
8211 error messages from being issued about the missing
8212 "template" keyword. */
8215 token = cp_lexer_advance_token (parser->lexer,
8216 parser->lexer->first_token,
8218 cp_lexer_purge_tokens_after (parser->lexer, token);
8221 *is_identifier = true;
8224 if (template_keyword_p)
8228 /* Look up the name. */
8229 decl = cp_parser_lookup_name (parser, identifier,
8231 /*is_template=*/false,
8232 /*is_namespace=*/false,
8233 check_dependency_p);
8234 decl = maybe_get_template_decl_from_type_decl (decl);
8236 /* If DECL is a template, then the name was a template-name. */
8237 if (TREE_CODE (decl) == TEMPLATE_DECL)
8241 /* The standard does not explicitly indicate whether a name that
8242 names a set of overloaded declarations, some of which are
8243 templates, is a template-name. However, such a name should
8244 be a template-name; otherwise, there is no way to form a
8245 template-id for the overloaded templates. */
8246 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
8247 if (TREE_CODE (fns) == OVERLOAD)
8251 for (fn = fns; fn; fn = OVL_NEXT (fn))
8252 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
8257 /* Otherwise, the name does not name a template. */
8258 cp_parser_error (parser, "expected template-name");
8259 return error_mark_node;
8263 /* If DECL is dependent, and refers to a function, then just return
8264 its name; we will look it up again during template instantiation. */
8265 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
8267 tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
8268 if (TYPE_P (scope) && dependent_type_p (scope))
8275 /* Parse a template-argument-list.
8277 template-argument-list:
8279 template-argument-list , template-argument
8281 Returns a TREE_VEC containing the arguments. */
8284 cp_parser_template_argument_list (cp_parser* parser)
8286 tree fixed_args[10];
8287 unsigned n_args = 0;
8288 unsigned alloced = 10;
8289 tree *arg_ary = fixed_args;
8291 bool saved_in_template_argument_list_p;
8293 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
8294 parser->in_template_argument_list_p = true;
8300 /* Consume the comma. */
8301 cp_lexer_consume_token (parser->lexer);
8303 /* Parse the template-argument. */
8304 argument = cp_parser_template_argument (parser);
8305 if (n_args == alloced)
8309 if (arg_ary == fixed_args)
8311 arg_ary = xmalloc (sizeof (tree) * alloced);
8312 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
8315 arg_ary = xrealloc (arg_ary, sizeof (tree) * alloced);
8317 arg_ary[n_args++] = argument;
8319 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
8321 vec = make_tree_vec (n_args);
8324 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
8326 if (arg_ary != fixed_args)
8328 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
8332 /* Parse a template-argument.
8335 assignment-expression
8339 The representation is that of an assignment-expression, type-id, or
8340 id-expression -- except that the qualified id-expression is
8341 evaluated, so that the value returned is either a DECL or an
8344 Although the standard says "assignment-expression", it forbids
8345 throw-expressions or assignments in the template argument.
8346 Therefore, we use "conditional-expression" instead. */
8349 cp_parser_template_argument (cp_parser* parser)
8354 bool maybe_type_id = false;
8357 tree qualifying_class;
8359 /* There's really no way to know what we're looking at, so we just
8360 try each alternative in order.
8364 In a template-argument, an ambiguity between a type-id and an
8365 expression is resolved to a type-id, regardless of the form of
8366 the corresponding template-parameter.
8368 Therefore, we try a type-id first. */
8369 cp_parser_parse_tentatively (parser);
8370 argument = cp_parser_type_id (parser);
8371 /* If there was no error parsing the type-id but the next token is a '>>',
8372 we probably found a typo for '> >'. But there are type-id which are
8373 also valid expressions. For instance:
8375 struct X { int operator >> (int); };
8376 template <int V> struct Foo {};
8379 Here 'X()' is a valid type-id of a function type, but the user just
8380 wanted to write the expression "X() >> 5". Thus, we remember that we
8381 found a valid type-id, but we still try to parse the argument as an
8382 expression to see what happens. */
8383 if (!cp_parser_error_occurred (parser)
8384 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
8386 maybe_type_id = true;
8387 cp_parser_abort_tentative_parse (parser);
8391 /* If the next token isn't a `,' or a `>', then this argument wasn't
8392 really finished. This means that the argument is not a valid
8394 if (!cp_parser_next_token_ends_template_argument_p (parser))
8395 cp_parser_error (parser, "expected template-argument");
8396 /* If that worked, we're done. */
8397 if (cp_parser_parse_definitely (parser))
8400 /* We're still not sure what the argument will be. */
8401 cp_parser_parse_tentatively (parser);
8402 /* Try a template. */
8403 argument = cp_parser_id_expression (parser,
8404 /*template_keyword_p=*/false,
8405 /*check_dependency_p=*/true,
8407 /*declarator_p=*/false);
8408 /* If the next token isn't a `,' or a `>', then this argument wasn't
8410 if (!cp_parser_next_token_ends_template_argument_p (parser))
8411 cp_parser_error (parser, "expected template-argument");
8412 if (!cp_parser_error_occurred (parser))
8414 /* Figure out what is being referred to. */
8415 argument = cp_parser_lookup_name (parser, argument,
8417 /*is_template=*/template_p,
8418 /*is_namespace=*/false,
8419 /*check_dependency=*/true);
8420 if (TREE_CODE (argument) != TEMPLATE_DECL
8421 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
8422 cp_parser_error (parser, "expected template-name");
8424 if (cp_parser_parse_definitely (parser))
8426 /* It must be a non-type argument. There permitted cases are given
8427 in [temp.arg.nontype]:
8429 -- an integral constant-expression of integral or enumeration
8432 -- the name of a non-type template-parameter; or
8434 -- the name of an object or function with external linkage...
8436 -- the address of an object or function with external linkage...
8438 -- a pointer to member... */
8439 /* Look for a non-type template parameter. */
8440 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
8442 cp_parser_parse_tentatively (parser);
8443 argument = cp_parser_primary_expression (parser,
8446 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
8447 || !cp_parser_next_token_ends_template_argument_p (parser))
8448 cp_parser_simulate_error (parser);
8449 if (cp_parser_parse_definitely (parser))
8452 /* If the next token is "&", the argument must be the address of an
8453 object or function with external linkage. */
8454 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
8456 cp_lexer_consume_token (parser->lexer);
8457 /* See if we might have an id-expression. */
8458 token = cp_lexer_peek_token (parser->lexer);
8459 if (token->type == CPP_NAME
8460 || token->keyword == RID_OPERATOR
8461 || token->type == CPP_SCOPE
8462 || token->type == CPP_TEMPLATE_ID
8463 || token->type == CPP_NESTED_NAME_SPECIFIER)
8465 cp_parser_parse_tentatively (parser);
8466 argument = cp_parser_primary_expression (parser,
8469 if (cp_parser_error_occurred (parser)
8470 || !cp_parser_next_token_ends_template_argument_p (parser))
8471 cp_parser_abort_tentative_parse (parser);
8474 if (qualifying_class)
8475 argument = finish_qualified_id_expr (qualifying_class,
8479 if (TREE_CODE (argument) == VAR_DECL)
8481 /* A variable without external linkage might still be a
8482 valid constant-expression, so no error is issued here
8483 if the external-linkage check fails. */
8484 if (!DECL_EXTERNAL_LINKAGE_P (argument))
8485 cp_parser_simulate_error (parser);
8487 else if (is_overloaded_fn (argument))
8488 /* All overloaded functions are allowed; if the external
8489 linkage test does not pass, an error will be issued
8493 && (TREE_CODE (argument) == OFFSET_REF
8494 || TREE_CODE (argument) == SCOPE_REF))
8495 /* A pointer-to-member. */
8498 cp_parser_simulate_error (parser);
8500 if (cp_parser_parse_definitely (parser))
8503 argument = build_x_unary_op (ADDR_EXPR, argument);
8508 /* If the argument started with "&", there are no other valid
8509 alternatives at this point. */
8512 cp_parser_error (parser, "invalid non-type template argument");
8513 return error_mark_node;
8515 /* If the argument wasn't successfully parsed as a type-id followed
8516 by '>>', the argument can only be a constant expression now.
8517 Otherwise, we try parsing the constant-expression tentatively,
8518 because the argument could really be a type-id. */
8520 cp_parser_parse_tentatively (parser);
8521 argument = cp_parser_constant_expression (parser,
8522 /*allow_non_constant_p=*/false,
8523 /*non_constant_p=*/NULL);
8524 argument = fold_non_dependent_expr (argument);
8527 if (!cp_parser_next_token_ends_template_argument_p (parser))
8528 cp_parser_error (parser, "expected template-argument");
8529 if (cp_parser_parse_definitely (parser))
8531 /* We did our best to parse the argument as a non type-id, but that
8532 was the only alternative that matched (albeit with a '>' after
8533 it). We can assume it's just a typo from the user, and a
8534 diagnostic will then be issued. */
8535 return cp_parser_type_id (parser);
8538 /* Parse an explicit-instantiation.
8540 explicit-instantiation:
8541 template declaration
8543 Although the standard says `declaration', what it really means is:
8545 explicit-instantiation:
8546 template decl-specifier-seq [opt] declarator [opt] ;
8548 Things like `template int S<int>::i = 5, int S<double>::j;' are not
8549 supposed to be allowed. A defect report has been filed about this
8554 explicit-instantiation:
8555 storage-class-specifier template
8556 decl-specifier-seq [opt] declarator [opt] ;
8557 function-specifier template
8558 decl-specifier-seq [opt] declarator [opt] ; */
8561 cp_parser_explicit_instantiation (cp_parser* parser)
8563 int declares_class_or_enum;
8564 tree decl_specifiers;
8566 tree extension_specifier = NULL_TREE;
8568 /* Look for an (optional) storage-class-specifier or
8569 function-specifier. */
8570 if (cp_parser_allow_gnu_extensions_p (parser))
8573 = cp_parser_storage_class_specifier_opt (parser);
8574 if (!extension_specifier)
8575 extension_specifier = cp_parser_function_specifier_opt (parser);
8578 /* Look for the `template' keyword. */
8579 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
8580 /* Let the front end know that we are processing an explicit
8582 begin_explicit_instantiation ();
8583 /* [temp.explicit] says that we are supposed to ignore access
8584 control while processing explicit instantiation directives. */
8585 push_deferring_access_checks (dk_no_check);
8586 /* Parse a decl-specifier-seq. */
8588 = cp_parser_decl_specifier_seq (parser,
8589 CP_PARSER_FLAGS_OPTIONAL,
8591 &declares_class_or_enum);
8592 /* If there was exactly one decl-specifier, and it declared a class,
8593 and there's no declarator, then we have an explicit type
8595 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
8599 type = check_tag_decl (decl_specifiers);
8600 /* Turn access control back on for names used during
8601 template instantiation. */
8602 pop_deferring_access_checks ();
8604 do_type_instantiation (type, extension_specifier, /*complain=*/1);
8611 /* Parse the declarator. */
8613 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
8614 /*ctor_dtor_or_conv_p=*/NULL,
8615 /*parenthesized_p=*/NULL);
8616 cp_parser_check_for_definition_in_return_type (declarator,
8617 declares_class_or_enum);
8618 if (declarator != error_mark_node)
8620 decl = grokdeclarator (declarator, decl_specifiers,
8622 /* Turn access control back on for names used during
8623 template instantiation. */
8624 pop_deferring_access_checks ();
8625 /* Do the explicit instantiation. */
8626 do_decl_instantiation (decl, extension_specifier);
8630 pop_deferring_access_checks ();
8631 /* Skip the body of the explicit instantiation. */
8632 cp_parser_skip_to_end_of_statement (parser);
8635 /* We're done with the instantiation. */
8636 end_explicit_instantiation ();
8638 cp_parser_consume_semicolon_at_end_of_statement (parser);
8641 /* Parse an explicit-specialization.
8643 explicit-specialization:
8644 template < > declaration
8646 Although the standard says `declaration', what it really means is:
8648 explicit-specialization:
8649 template <> decl-specifier [opt] init-declarator [opt] ;
8650 template <> function-definition
8651 template <> explicit-specialization
8652 template <> template-declaration */
8655 cp_parser_explicit_specialization (cp_parser* parser)
8657 /* Look for the `template' keyword. */
8658 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
8659 /* Look for the `<'. */
8660 cp_parser_require (parser, CPP_LESS, "`<'");
8661 /* Look for the `>'. */
8662 cp_parser_require (parser, CPP_GREATER, "`>'");
8663 /* We have processed another parameter list. */
8664 ++parser->num_template_parameter_lists;
8665 /* Let the front end know that we are beginning a specialization. */
8666 begin_specialization ();
8668 /* If the next keyword is `template', we need to figure out whether
8669 or not we're looking a template-declaration. */
8670 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
8672 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
8673 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
8674 cp_parser_template_declaration_after_export (parser,
8675 /*member_p=*/false);
8677 cp_parser_explicit_specialization (parser);
8680 /* Parse the dependent declaration. */
8681 cp_parser_single_declaration (parser,
8685 /* We're done with the specialization. */
8686 end_specialization ();
8687 /* We're done with this parameter list. */
8688 --parser->num_template_parameter_lists;
8691 /* Parse a type-specifier.
8694 simple-type-specifier
8697 elaborated-type-specifier
8705 Returns a representation of the type-specifier. If the
8706 type-specifier is a keyword (like `int' or `const', or
8707 `__complex__') then the corresponding IDENTIFIER_NODE is returned.
8708 For a class-specifier, enum-specifier, or elaborated-type-specifier
8709 a TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
8711 If IS_FRIEND is TRUE then this type-specifier is being declared a
8712 `friend'. If IS_DECLARATION is TRUE, then this type-specifier is
8713 appearing in a decl-specifier-seq.
8715 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
8716 class-specifier, enum-specifier, or elaborated-type-specifier, then
8717 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
8718 if a type is declared; 2 if it is defined. Otherwise, it is set to
8721 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
8722 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
8726 cp_parser_type_specifier (cp_parser* parser,
8727 cp_parser_flags flags,
8729 bool is_declaration,
8730 int* declares_class_or_enum,
8731 bool* is_cv_qualifier)
8733 tree type_spec = NULL_TREE;
8737 /* Assume this type-specifier does not declare a new type. */
8738 if (declares_class_or_enum)
8739 *declares_class_or_enum = 0;
8740 /* And that it does not specify a cv-qualifier. */
8741 if (is_cv_qualifier)
8742 *is_cv_qualifier = false;
8743 /* Peek at the next token. */
8744 token = cp_lexer_peek_token (parser->lexer);
8746 /* If we're looking at a keyword, we can use that to guide the
8747 production we choose. */
8748 keyword = token->keyword;
8751 /* Any of these indicate either a class-specifier, or an
8752 elaborated-type-specifier. */
8757 /* Parse tentatively so that we can back up if we don't find a
8758 class-specifier or enum-specifier. */
8759 cp_parser_parse_tentatively (parser);
8760 /* Look for the class-specifier or enum-specifier. */
8761 if (keyword == RID_ENUM)
8762 type_spec = cp_parser_enum_specifier (parser);
8764 type_spec = cp_parser_class_specifier (parser);
8766 /* If that worked, we're done. */
8767 if (cp_parser_parse_definitely (parser))
8769 if (declares_class_or_enum)
8770 *declares_class_or_enum = 2;
8777 /* Look for an elaborated-type-specifier. */
8778 type_spec = cp_parser_elaborated_type_specifier (parser,
8781 /* We're declaring a class or enum -- unless we're using
8783 if (declares_class_or_enum && keyword != RID_TYPENAME)
8784 *declares_class_or_enum = 1;
8790 type_spec = cp_parser_cv_qualifier_opt (parser);
8791 /* Even though we call a routine that looks for an optional
8792 qualifier, we know that there should be one. */
8793 my_friendly_assert (type_spec != NULL, 20000328);
8794 /* This type-specifier was a cv-qualified. */
8795 if (is_cv_qualifier)
8796 *is_cv_qualifier = true;
8801 /* The `__complex__' keyword is a GNU extension. */
8802 return cp_lexer_consume_token (parser->lexer)->value;
8808 /* If we do not already have a type-specifier, assume we are looking
8809 at a simple-type-specifier. */
8810 type_spec = cp_parser_simple_type_specifier (parser, flags,
8811 /*identifier_p=*/true);
8813 /* If we didn't find a type-specifier, and a type-specifier was not
8814 optional in this context, issue an error message. */
8815 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
8817 cp_parser_error (parser, "expected type specifier");
8818 return error_mark_node;
8824 /* Parse a simple-type-specifier.
8826 simple-type-specifier:
8827 :: [opt] nested-name-specifier [opt] type-name
8828 :: [opt] nested-name-specifier template template-id
8843 simple-type-specifier:
8844 __typeof__ unary-expression
8845 __typeof__ ( type-id )
8847 For the various keywords, the value returned is simply the
8848 TREE_IDENTIFIER representing the keyword if IDENTIFIER_P is true.
8849 For the first two productions, and if IDENTIFIER_P is false, the
8850 value returned is the indicated TYPE_DECL. */
8853 cp_parser_simple_type_specifier (cp_parser* parser, cp_parser_flags flags,
8856 tree type = NULL_TREE;
8859 /* Peek at the next token. */
8860 token = cp_lexer_peek_token (parser->lexer);
8862 /* If we're looking at a keyword, things are easy. */
8863 switch (token->keyword)
8866 type = char_type_node;
8869 type = wchar_type_node;
8872 type = boolean_type_node;
8875 type = short_integer_type_node;
8878 type = integer_type_node;
8881 type = long_integer_type_node;
8884 type = integer_type_node;
8887 type = unsigned_type_node;
8890 type = float_type_node;
8893 type = double_type_node;
8896 type = void_type_node;
8903 /* Consume the `typeof' token. */
8904 cp_lexer_consume_token (parser->lexer);
8905 /* Parse the operand to `typeof'. */
8906 operand = cp_parser_sizeof_operand (parser, RID_TYPEOF);
8907 /* If it is not already a TYPE, take its type. */
8908 if (!TYPE_P (operand))
8909 operand = finish_typeof (operand);
8918 /* If the type-specifier was for a built-in type, we're done. */
8923 /* Consume the token. */
8924 id = cp_lexer_consume_token (parser->lexer)->value;
8926 /* There is no valid C++ program where a non-template type is
8927 followed by a "<". That usually indicates that the user thought
8928 that the type was a template. */
8929 cp_parser_check_for_invalid_template_id (parser, type);
8931 return identifier_p ? id : TYPE_NAME (type);
8934 /* The type-specifier must be a user-defined type. */
8935 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
8937 /* Don't gobble tokens or issue error messages if this is an
8938 optional type-specifier. */
8939 if (flags & CP_PARSER_FLAGS_OPTIONAL)
8940 cp_parser_parse_tentatively (parser);
8942 /* Look for the optional `::' operator. */
8943 cp_parser_global_scope_opt (parser,
8944 /*current_scope_valid_p=*/false);
8945 /* Look for the nested-name specifier. */
8946 cp_parser_nested_name_specifier_opt (parser,
8947 /*typename_keyword_p=*/false,
8948 /*check_dependency_p=*/true,
8950 /*is_declaration=*/false);
8951 /* If we have seen a nested-name-specifier, and the next token
8952 is `template', then we are using the template-id production. */
8954 && cp_parser_optional_template_keyword (parser))
8956 /* Look for the template-id. */
8957 type = cp_parser_template_id (parser,
8958 /*template_keyword_p=*/true,
8959 /*check_dependency_p=*/true,
8960 /*is_declaration=*/false);
8961 /* If the template-id did not name a type, we are out of
8963 if (TREE_CODE (type) != TYPE_DECL)
8965 cp_parser_error (parser, "expected template-id for type");
8969 /* Otherwise, look for a type-name. */
8971 type = cp_parser_type_name (parser);
8972 /* If it didn't work out, we don't have a TYPE. */
8973 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
8974 && !cp_parser_parse_definitely (parser))
8978 /* If we didn't get a type-name, issue an error message. */
8979 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
8981 cp_parser_error (parser, "expected type-name");
8982 return error_mark_node;
8985 /* There is no valid C++ program where a non-template type is
8986 followed by a "<". That usually indicates that the user thought
8987 that the type was a template. */
8988 if (type && type != error_mark_node)
8989 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type));
8994 /* Parse a type-name.
9007 Returns a TYPE_DECL for the the type. */
9010 cp_parser_type_name (cp_parser* parser)
9015 /* We can't know yet whether it is a class-name or not. */
9016 cp_parser_parse_tentatively (parser);
9017 /* Try a class-name. */
9018 type_decl = cp_parser_class_name (parser,
9019 /*typename_keyword_p=*/false,
9020 /*template_keyword_p=*/false,
9022 /*check_dependency_p=*/true,
9023 /*class_head_p=*/false,
9024 /*is_declaration=*/false);
9025 /* If it's not a class-name, keep looking. */
9026 if (!cp_parser_parse_definitely (parser))
9028 /* It must be a typedef-name or an enum-name. */
9029 identifier = cp_parser_identifier (parser);
9030 if (identifier == error_mark_node)
9031 return error_mark_node;
9033 /* Look up the type-name. */
9034 type_decl = cp_parser_lookup_name_simple (parser, identifier);
9035 /* Issue an error if we did not find a type-name. */
9036 if (TREE_CODE (type_decl) != TYPE_DECL)
9038 if (!cp_parser_simulate_error (parser))
9039 cp_parser_name_lookup_error (parser, identifier, type_decl,
9041 type_decl = error_mark_node;
9043 /* Remember that the name was used in the definition of the
9044 current class so that we can check later to see if the
9045 meaning would have been different after the class was
9046 entirely defined. */
9047 else if (type_decl != error_mark_node
9049 maybe_note_name_used_in_class (identifier, type_decl);
9056 /* Parse an elaborated-type-specifier. Note that the grammar given
9057 here incorporates the resolution to DR68.
9059 elaborated-type-specifier:
9060 class-key :: [opt] nested-name-specifier [opt] identifier
9061 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
9062 enum :: [opt] nested-name-specifier [opt] identifier
9063 typename :: [opt] nested-name-specifier identifier
9064 typename :: [opt] nested-name-specifier template [opt]
9069 elaborated-type-specifier:
9070 class-key attributes :: [opt] nested-name-specifier [opt] identifier
9071 class-key attributes :: [opt] nested-name-specifier [opt]
9072 template [opt] template-id
9073 enum attributes :: [opt] nested-name-specifier [opt] identifier
9075 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
9076 declared `friend'. If IS_DECLARATION is TRUE, then this
9077 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
9078 something is being declared.
9080 Returns the TYPE specified. */
9083 cp_parser_elaborated_type_specifier (cp_parser* parser,
9085 bool is_declaration)
9087 enum tag_types tag_type;
9089 tree type = NULL_TREE;
9090 tree attributes = NULL_TREE;
9092 /* See if we're looking at the `enum' keyword. */
9093 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
9095 /* Consume the `enum' token. */
9096 cp_lexer_consume_token (parser->lexer);
9097 /* Remember that it's an enumeration type. */
9098 tag_type = enum_type;
9099 /* Parse the attributes. */
9100 attributes = cp_parser_attributes_opt (parser);
9102 /* Or, it might be `typename'. */
9103 else if (cp_lexer_next_token_is_keyword (parser->lexer,
9106 /* Consume the `typename' token. */
9107 cp_lexer_consume_token (parser->lexer);
9108 /* Remember that it's a `typename' type. */
9109 tag_type = typename_type;
9110 /* The `typename' keyword is only allowed in templates. */
9111 if (!processing_template_decl)
9112 pedwarn ("using `typename' outside of template");
9114 /* Otherwise it must be a class-key. */
9117 tag_type = cp_parser_class_key (parser);
9118 if (tag_type == none_type)
9119 return error_mark_node;
9120 /* Parse the attributes. */
9121 attributes = cp_parser_attributes_opt (parser);
9124 /* Look for the `::' operator. */
9125 cp_parser_global_scope_opt (parser,
9126 /*current_scope_valid_p=*/false);
9127 /* Look for the nested-name-specifier. */
9128 if (tag_type == typename_type)
9130 if (cp_parser_nested_name_specifier (parser,
9131 /*typename_keyword_p=*/true,
9132 /*check_dependency_p=*/true,
9136 return error_mark_node;
9139 /* Even though `typename' is not present, the proposed resolution
9140 to Core Issue 180 says that in `class A<T>::B', `B' should be
9141 considered a type-name, even if `A<T>' is dependent. */
9142 cp_parser_nested_name_specifier_opt (parser,
9143 /*typename_keyword_p=*/true,
9144 /*check_dependency_p=*/true,
9147 /* For everything but enumeration types, consider a template-id. */
9148 if (tag_type != enum_type)
9150 bool template_p = false;
9153 /* Allow the `template' keyword. */
9154 template_p = cp_parser_optional_template_keyword (parser);
9155 /* If we didn't see `template', we don't know if there's a
9156 template-id or not. */
9158 cp_parser_parse_tentatively (parser);
9159 /* Parse the template-id. */
9160 decl = cp_parser_template_id (parser, template_p,
9161 /*check_dependency_p=*/true,
9163 /* If we didn't find a template-id, look for an ordinary
9165 if (!template_p && !cp_parser_parse_definitely (parser))
9167 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
9168 in effect, then we must assume that, upon instantiation, the
9169 template will correspond to a class. */
9170 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
9171 && tag_type == typename_type)
9172 type = make_typename_type (parser->scope, decl,
9175 type = TREE_TYPE (decl);
9178 /* For an enumeration type, consider only a plain identifier. */
9181 identifier = cp_parser_identifier (parser);
9183 if (identifier == error_mark_node)
9185 parser->scope = NULL_TREE;
9186 return error_mark_node;
9189 /* For a `typename', we needn't call xref_tag. */
9190 if (tag_type == typename_type)
9191 return cp_parser_make_typename_type (parser, parser->scope,
9193 /* Look up a qualified name in the usual way. */
9198 /* In an elaborated-type-specifier, names are assumed to name
9199 types, so we set IS_TYPE to TRUE when calling
9200 cp_parser_lookup_name. */
9201 decl = cp_parser_lookup_name (parser, identifier,
9203 /*is_template=*/false,
9204 /*is_namespace=*/false,
9205 /*check_dependency=*/true);
9207 /* If we are parsing friend declaration, DECL may be a
9208 TEMPLATE_DECL tree node here. However, we need to check
9209 whether this TEMPLATE_DECL results in valid code. Consider
9210 the following example:
9213 template <class T> class C {};
9216 template <class T> friend class N::C; // #1, valid code
9218 template <class T> class Y {
9219 friend class N::C; // #2, invalid code
9222 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
9223 name lookup of `N::C'. We see that friend declaration must
9224 be template for the code to be valid. Note that
9225 processing_template_decl does not work here since it is
9226 always 1 for the above two cases. */
9228 decl = (cp_parser_maybe_treat_template_as_class
9229 (decl, /*tag_name_p=*/is_friend
9230 && parser->num_template_parameter_lists));
9232 if (TREE_CODE (decl) != TYPE_DECL)
9234 error ("expected type-name");
9235 return error_mark_node;
9238 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
9239 check_elaborated_type_specifier
9241 (parser->num_template_parameter_lists
9242 || DECL_SELF_REFERENCE_P (decl)));
9244 type = TREE_TYPE (decl);
9248 /* An elaborated-type-specifier sometimes introduces a new type and
9249 sometimes names an existing type. Normally, the rule is that it
9250 introduces a new type only if there is not an existing type of
9251 the same name already in scope. For example, given:
9254 void f() { struct S s; }
9256 the `struct S' in the body of `f' is the same `struct S' as in
9257 the global scope; the existing definition is used. However, if
9258 there were no global declaration, this would introduce a new
9259 local class named `S'.
9261 An exception to this rule applies to the following code:
9263 namespace N { struct S; }
9265 Here, the elaborated-type-specifier names a new type
9266 unconditionally; even if there is already an `S' in the
9267 containing scope this declaration names a new type.
9268 This exception only applies if the elaborated-type-specifier
9269 forms the complete declaration:
9273 A declaration consisting solely of `class-key identifier ;' is
9274 either a redeclaration of the name in the current scope or a
9275 forward declaration of the identifier as a class name. It
9276 introduces the name into the current scope.
9278 We are in this situation precisely when the next token is a `;'.
9280 An exception to the exception is that a `friend' declaration does
9281 *not* name a new type; i.e., given:
9283 struct S { friend struct T; };
9285 `T' is not a new type in the scope of `S'.
9287 Also, `new struct S' or `sizeof (struct S)' never results in the
9288 definition of a new type; a new type can only be declared in a
9289 declaration context. */
9291 /* Warn about attributes. They are ignored. */
9293 warning ("type attributes are honored only at type definition");
9295 type = xref_tag (tag_type, identifier,
9296 /*attributes=*/NULL_TREE,
9299 || cp_lexer_next_token_is_not (parser->lexer,
9301 parser->num_template_parameter_lists);
9304 if (tag_type != enum_type)
9305 cp_parser_check_class_key (tag_type, type);
9307 /* A "<" cannot follow an elaborated type specifier. If that
9308 happens, the user was probably trying to form a template-id. */
9309 cp_parser_check_for_invalid_template_id (parser, type);
9314 /* Parse an enum-specifier.
9317 enum identifier [opt] { enumerator-list [opt] }
9319 Returns an ENUM_TYPE representing the enumeration. */
9322 cp_parser_enum_specifier (cp_parser* parser)
9325 tree identifier = NULL_TREE;
9328 /* Look for the `enum' keyword. */
9329 if (!cp_parser_require_keyword (parser, RID_ENUM, "`enum'"))
9330 return error_mark_node;
9331 /* Peek at the next token. */
9332 token = cp_lexer_peek_token (parser->lexer);
9334 /* See if it is an identifier. */
9335 if (token->type == CPP_NAME)
9336 identifier = cp_parser_identifier (parser);
9338 /* Look for the `{'. */
9339 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
9340 return error_mark_node;
9342 /* At this point, we're going ahead with the enum-specifier, even
9343 if some other problem occurs. */
9344 cp_parser_commit_to_tentative_parse (parser);
9346 /* Issue an error message if type-definitions are forbidden here. */
9347 cp_parser_check_type_definition (parser);
9349 /* Create the new type. */
9350 type = start_enum (identifier ? identifier : make_anon_name ());
9352 /* Peek at the next token. */
9353 token = cp_lexer_peek_token (parser->lexer);
9354 /* If it's not a `}', then there are some enumerators. */
9355 if (token->type != CPP_CLOSE_BRACE)
9356 cp_parser_enumerator_list (parser, type);
9357 /* Look for the `}'. */
9358 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
9360 /* Finish up the enumeration. */
9366 /* Parse an enumerator-list. The enumerators all have the indicated
9370 enumerator-definition
9371 enumerator-list , enumerator-definition */
9374 cp_parser_enumerator_list (cp_parser* parser, tree type)
9380 /* Parse an enumerator-definition. */
9381 cp_parser_enumerator_definition (parser, type);
9382 /* Peek at the next token. */
9383 token = cp_lexer_peek_token (parser->lexer);
9384 /* If it's not a `,', then we've reached the end of the
9386 if (token->type != CPP_COMMA)
9388 /* Otherwise, consume the `,' and keep going. */
9389 cp_lexer_consume_token (parser->lexer);
9390 /* If the next token is a `}', there is a trailing comma. */
9391 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
9393 if (pedantic && !in_system_header)
9394 pedwarn ("comma at end of enumerator list");
9400 /* Parse an enumerator-definition. The enumerator has the indicated
9403 enumerator-definition:
9405 enumerator = constant-expression
9411 cp_parser_enumerator_definition (cp_parser* parser, tree type)
9417 /* Look for the identifier. */
9418 identifier = cp_parser_identifier (parser);
9419 if (identifier == error_mark_node)
9422 /* Peek at the next token. */
9423 token = cp_lexer_peek_token (parser->lexer);
9424 /* If it's an `=', then there's an explicit value. */
9425 if (token->type == CPP_EQ)
9427 /* Consume the `=' token. */
9428 cp_lexer_consume_token (parser->lexer);
9429 /* Parse the value. */
9430 value = cp_parser_constant_expression (parser,
9431 /*allow_non_constant_p=*/false,
9437 /* Create the enumerator. */
9438 build_enumerator (identifier, value, type);
9441 /* Parse a namespace-name.
9444 original-namespace-name
9447 Returns the NAMESPACE_DECL for the namespace. */
9450 cp_parser_namespace_name (cp_parser* parser)
9453 tree namespace_decl;
9455 /* Get the name of the namespace. */
9456 identifier = cp_parser_identifier (parser);
9457 if (identifier == error_mark_node)
9458 return error_mark_node;
9460 /* Look up the identifier in the currently active scope. Look only
9461 for namespaces, due to:
9465 When looking up a namespace-name in a using-directive or alias
9466 definition, only namespace names are considered.
9472 During the lookup of a name preceding the :: scope resolution
9473 operator, object, function, and enumerator names are ignored.
9475 (Note that cp_parser_class_or_namespace_name only calls this
9476 function if the token after the name is the scope resolution
9478 namespace_decl = cp_parser_lookup_name (parser, identifier,
9480 /*is_template=*/false,
9481 /*is_namespace=*/true,
9482 /*check_dependency=*/true);
9483 /* If it's not a namespace, issue an error. */
9484 if (namespace_decl == error_mark_node
9485 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
9487 cp_parser_error (parser, "expected namespace-name");
9488 namespace_decl = error_mark_node;
9491 return namespace_decl;
9494 /* Parse a namespace-definition.
9496 namespace-definition:
9497 named-namespace-definition
9498 unnamed-namespace-definition
9500 named-namespace-definition:
9501 original-namespace-definition
9502 extension-namespace-definition
9504 original-namespace-definition:
9505 namespace identifier { namespace-body }
9507 extension-namespace-definition:
9508 namespace original-namespace-name { namespace-body }
9510 unnamed-namespace-definition:
9511 namespace { namespace-body } */
9514 cp_parser_namespace_definition (cp_parser* parser)
9518 /* Look for the `namespace' keyword. */
9519 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
9521 /* Get the name of the namespace. We do not attempt to distinguish
9522 between an original-namespace-definition and an
9523 extension-namespace-definition at this point. The semantic
9524 analysis routines are responsible for that. */
9525 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
9526 identifier = cp_parser_identifier (parser);
9528 identifier = NULL_TREE;
9530 /* Look for the `{' to start the namespace. */
9531 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
9532 /* Start the namespace. */
9533 push_namespace (identifier);
9534 /* Parse the body of the namespace. */
9535 cp_parser_namespace_body (parser);
9536 /* Finish the namespace. */
9538 /* Look for the final `}'. */
9539 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
9542 /* Parse a namespace-body.
9545 declaration-seq [opt] */
9548 cp_parser_namespace_body (cp_parser* parser)
9550 cp_parser_declaration_seq_opt (parser);
9553 /* Parse a namespace-alias-definition.
9555 namespace-alias-definition:
9556 namespace identifier = qualified-namespace-specifier ; */
9559 cp_parser_namespace_alias_definition (cp_parser* parser)
9562 tree namespace_specifier;
9564 /* Look for the `namespace' keyword. */
9565 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
9566 /* Look for the identifier. */
9567 identifier = cp_parser_identifier (parser);
9568 if (identifier == error_mark_node)
9570 /* Look for the `=' token. */
9571 cp_parser_require (parser, CPP_EQ, "`='");
9572 /* Look for the qualified-namespace-specifier. */
9574 = cp_parser_qualified_namespace_specifier (parser);
9575 /* Look for the `;' token. */
9576 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
9578 /* Register the alias in the symbol table. */
9579 do_namespace_alias (identifier, namespace_specifier);
9582 /* Parse a qualified-namespace-specifier.
9584 qualified-namespace-specifier:
9585 :: [opt] nested-name-specifier [opt] namespace-name
9587 Returns a NAMESPACE_DECL corresponding to the specified
9591 cp_parser_qualified_namespace_specifier (cp_parser* parser)
9593 /* Look for the optional `::'. */
9594 cp_parser_global_scope_opt (parser,
9595 /*current_scope_valid_p=*/false);
9597 /* Look for the optional nested-name-specifier. */
9598 cp_parser_nested_name_specifier_opt (parser,
9599 /*typename_keyword_p=*/false,
9600 /*check_dependency_p=*/true,
9602 /*is_declaration=*/true);
9604 return cp_parser_namespace_name (parser);
9607 /* Parse a using-declaration.
9610 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
9611 using :: unqualified-id ; */
9614 cp_parser_using_declaration (cp_parser* parser)
9617 bool typename_p = false;
9618 bool global_scope_p;
9624 /* Look for the `using' keyword. */
9625 cp_parser_require_keyword (parser, RID_USING, "`using'");
9627 /* Peek at the next token. */
9628 token = cp_lexer_peek_token (parser->lexer);
9629 /* See if it's `typename'. */
9630 if (token->keyword == RID_TYPENAME)
9632 /* Remember that we've seen it. */
9634 /* Consume the `typename' token. */
9635 cp_lexer_consume_token (parser->lexer);
9638 /* Look for the optional global scope qualification. */
9640 = (cp_parser_global_scope_opt (parser,
9641 /*current_scope_valid_p=*/false)
9644 /* If we saw `typename', or didn't see `::', then there must be a
9645 nested-name-specifier present. */
9646 if (typename_p || !global_scope_p)
9647 qscope = cp_parser_nested_name_specifier (parser, typename_p,
9648 /*check_dependency_p=*/true,
9650 /*is_declaration=*/true);
9651 /* Otherwise, we could be in either of the two productions. In that
9652 case, treat the nested-name-specifier as optional. */
9654 qscope = cp_parser_nested_name_specifier_opt (parser,
9655 /*typename_keyword_p=*/false,
9656 /*check_dependency_p=*/true,
9658 /*is_declaration=*/true);
9660 qscope = global_namespace;
9662 /* Parse the unqualified-id. */
9663 identifier = cp_parser_unqualified_id (parser,
9664 /*template_keyword_p=*/false,
9665 /*check_dependency_p=*/true,
9666 /*declarator_p=*/true);
9668 /* The function we call to handle a using-declaration is different
9669 depending on what scope we are in. */
9670 if (identifier == error_mark_node)
9672 else if (TREE_CODE (identifier) != IDENTIFIER_NODE
9673 && TREE_CODE (identifier) != BIT_NOT_EXPR)
9674 /* [namespace.udecl]
9676 A using declaration shall not name a template-id. */
9677 error ("a template-id may not appear in a using-declaration");
9680 scope = current_scope ();
9681 if (scope && TYPE_P (scope))
9683 /* Create the USING_DECL. */
9684 decl = do_class_using_decl (build_nt (SCOPE_REF,
9687 /* Add it to the list of members in this class. */
9688 finish_member_declaration (decl);
9692 decl = cp_parser_lookup_name_simple (parser, identifier);
9693 if (decl == error_mark_node)
9694 cp_parser_name_lookup_error (parser, identifier, decl, NULL);
9696 do_local_using_decl (decl, qscope, identifier);
9698 do_toplevel_using_decl (decl, qscope, identifier);
9702 /* Look for the final `;'. */
9703 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
9706 /* Parse a using-directive.
9709 using namespace :: [opt] nested-name-specifier [opt]
9713 cp_parser_using_directive (cp_parser* parser)
9715 tree namespace_decl;
9718 /* Look for the `using' keyword. */
9719 cp_parser_require_keyword (parser, RID_USING, "`using'");
9720 /* And the `namespace' keyword. */
9721 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
9722 /* Look for the optional `::' operator. */
9723 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
9724 /* And the optional nested-name-specifier. */
9725 cp_parser_nested_name_specifier_opt (parser,
9726 /*typename_keyword_p=*/false,
9727 /*check_dependency_p=*/true,
9729 /*is_declaration=*/true);
9730 /* Get the namespace being used. */
9731 namespace_decl = cp_parser_namespace_name (parser);
9732 /* And any specified attributes. */
9733 attribs = cp_parser_attributes_opt (parser);
9734 /* Update the symbol table. */
9735 parse_using_directive (namespace_decl, attribs);
9736 /* Look for the final `;'. */
9737 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
9740 /* Parse an asm-definition.
9743 asm ( string-literal ) ;
9748 asm volatile [opt] ( string-literal ) ;
9749 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
9750 asm volatile [opt] ( string-literal : asm-operand-list [opt]
9751 : asm-operand-list [opt] ) ;
9752 asm volatile [opt] ( string-literal : asm-operand-list [opt]
9753 : asm-operand-list [opt]
9754 : asm-operand-list [opt] ) ; */
9757 cp_parser_asm_definition (cp_parser* parser)
9761 tree outputs = NULL_TREE;
9762 tree inputs = NULL_TREE;
9763 tree clobbers = NULL_TREE;
9765 bool volatile_p = false;
9766 bool extended_p = false;
9768 /* Look for the `asm' keyword. */
9769 cp_parser_require_keyword (parser, RID_ASM, "`asm'");
9770 /* See if the next token is `volatile'. */
9771 if (cp_parser_allow_gnu_extensions_p (parser)
9772 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
9774 /* Remember that we saw the `volatile' keyword. */
9776 /* Consume the token. */
9777 cp_lexer_consume_token (parser->lexer);
9779 /* Look for the opening `('. */
9780 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
9781 /* Look for the string. */
9782 c_lex_string_translate = false;
9783 token = cp_parser_require (parser, CPP_STRING, "asm body");
9786 string = token->value;
9787 /* If we're allowing GNU extensions, check for the extended assembly
9788 syntax. Unfortunately, the `:' tokens need not be separated by
9789 a space in C, and so, for compatibility, we tolerate that here
9790 too. Doing that means that we have to treat the `::' operator as
9792 if (cp_parser_allow_gnu_extensions_p (parser)
9793 && at_function_scope_p ()
9794 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
9795 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
9797 bool inputs_p = false;
9798 bool clobbers_p = false;
9800 /* The extended syntax was used. */
9803 /* Look for outputs. */
9804 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
9806 /* Consume the `:'. */
9807 cp_lexer_consume_token (parser->lexer);
9808 /* Parse the output-operands. */
9809 if (cp_lexer_next_token_is_not (parser->lexer,
9811 && cp_lexer_next_token_is_not (parser->lexer,
9813 && cp_lexer_next_token_is_not (parser->lexer,
9815 outputs = cp_parser_asm_operand_list (parser);
9817 /* If the next token is `::', there are no outputs, and the
9818 next token is the beginning of the inputs. */
9819 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
9821 /* Consume the `::' token. */
9822 cp_lexer_consume_token (parser->lexer);
9823 /* The inputs are coming next. */
9827 /* Look for inputs. */
9829 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
9832 /* Consume the `:'. */
9833 cp_lexer_consume_token (parser->lexer);
9834 /* Parse the output-operands. */
9835 if (cp_lexer_next_token_is_not (parser->lexer,
9837 && cp_lexer_next_token_is_not (parser->lexer,
9839 && cp_lexer_next_token_is_not (parser->lexer,
9841 inputs = cp_parser_asm_operand_list (parser);
9843 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
9844 /* The clobbers are coming next. */
9847 /* Look for clobbers. */
9849 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
9852 /* Consume the `:'. */
9853 cp_lexer_consume_token (parser->lexer);
9854 /* Parse the clobbers. */
9855 if (cp_lexer_next_token_is_not (parser->lexer,
9857 clobbers = cp_parser_asm_clobber_list (parser);
9860 /* Look for the closing `)'. */
9861 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
9862 cp_parser_skip_to_closing_parenthesis (parser, true, false,
9863 /*consume_paren=*/true);
9864 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
9866 /* Create the ASM_STMT. */
9867 if (at_function_scope_p ())
9870 finish_asm_stmt (volatile_p
9871 ? ridpointers[(int) RID_VOLATILE] : NULL_TREE,
9872 string, outputs, inputs, clobbers);
9873 /* If the extended syntax was not used, mark the ASM_STMT. */
9875 ASM_INPUT_P (asm_stmt) = 1;
9878 assemble_asm (string);
9881 c_lex_string_translate = true;
9884 /* Declarators [gram.dcl.decl] */
9886 /* Parse an init-declarator.
9889 declarator initializer [opt]
9894 declarator asm-specification [opt] attributes [opt] initializer [opt]
9896 function-definition:
9897 decl-specifier-seq [opt] declarator ctor-initializer [opt]
9899 decl-specifier-seq [opt] declarator function-try-block
9903 function-definition:
9904 __extension__ function-definition
9906 The DECL_SPECIFIERS and PREFIX_ATTRIBUTES apply to this declarator.
9907 Returns a representation of the entity declared. If MEMBER_P is TRUE,
9908 then this declarator appears in a class scope. The new DECL created
9909 by this declarator is returned.
9911 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
9912 for a function-definition here as well. If the declarator is a
9913 declarator for a function-definition, *FUNCTION_DEFINITION_P will
9914 be TRUE upon return. By that point, the function-definition will
9915 have been completely parsed.
9917 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
9921 cp_parser_init_declarator (cp_parser* parser,
9922 tree decl_specifiers,
9923 tree prefix_attributes,
9924 bool function_definition_allowed_p,
9926 int declares_class_or_enum,
9927 bool* function_definition_p)
9932 tree asm_specification;
9934 tree decl = NULL_TREE;
9936 bool is_initialized;
9937 bool is_parenthesized_init;
9938 bool is_non_constant_init;
9939 int ctor_dtor_or_conv_p;
9943 /* Assume that this is not the declarator for a function
9945 if (function_definition_p)
9946 *function_definition_p = false;
9948 /* Defer access checks while parsing the declarator; we cannot know
9949 what names are accessible until we know what is being
9951 resume_deferring_access_checks ();
9953 /* Parse the declarator. */
9955 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
9956 &ctor_dtor_or_conv_p,
9957 /*parenthesized_p=*/NULL);
9958 /* Gather up the deferred checks. */
9959 stop_deferring_access_checks ();
9961 /* If the DECLARATOR was erroneous, there's no need to go
9963 if (declarator == error_mark_node)
9964 return error_mark_node;
9966 cp_parser_check_for_definition_in_return_type (declarator,
9967 declares_class_or_enum);
9969 /* Figure out what scope the entity declared by the DECLARATOR is
9970 located in. `grokdeclarator' sometimes changes the scope, so
9971 we compute it now. */
9972 scope = get_scope_of_declarator (declarator);
9974 /* If we're allowing GNU extensions, look for an asm-specification
9976 if (cp_parser_allow_gnu_extensions_p (parser))
9978 /* Look for an asm-specification. */
9979 asm_specification = cp_parser_asm_specification_opt (parser);
9980 /* And attributes. */
9981 attributes = cp_parser_attributes_opt (parser);
9985 asm_specification = NULL_TREE;
9986 attributes = NULL_TREE;
9989 /* Peek at the next token. */
9990 token = cp_lexer_peek_token (parser->lexer);
9991 /* Check to see if the token indicates the start of a
9992 function-definition. */
9993 if (cp_parser_token_starts_function_definition_p (token))
9995 if (!function_definition_allowed_p)
9997 /* If a function-definition should not appear here, issue an
9999 cp_parser_error (parser,
10000 "a function-definition is not allowed here");
10001 return error_mark_node;
10005 /* Neither attributes nor an asm-specification are allowed
10006 on a function-definition. */
10007 if (asm_specification)
10008 error ("an asm-specification is not allowed on a function-definition");
10010 error ("attributes are not allowed on a function-definition");
10011 /* This is a function-definition. */
10012 *function_definition_p = true;
10014 /* Parse the function definition. */
10016 decl = cp_parser_save_member_function_body (parser,
10019 prefix_attributes);
10022 = (cp_parser_function_definition_from_specifiers_and_declarator
10023 (parser, decl_specifiers, prefix_attributes, declarator));
10031 Only in function declarations for constructors, destructors, and
10032 type conversions can the decl-specifier-seq be omitted.
10034 We explicitly postpone this check past the point where we handle
10035 function-definitions because we tolerate function-definitions
10036 that are missing their return types in some modes. */
10037 if (!decl_specifiers && ctor_dtor_or_conv_p <= 0)
10039 cp_parser_error (parser,
10040 "expected constructor, destructor, or type conversion");
10041 return error_mark_node;
10044 /* An `=' or an `(' indicates an initializer. */
10045 is_initialized = (token->type == CPP_EQ
10046 || token->type == CPP_OPEN_PAREN);
10047 /* If the init-declarator isn't initialized and isn't followed by a
10048 `,' or `;', it's not a valid init-declarator. */
10049 if (!is_initialized
10050 && token->type != CPP_COMMA
10051 && token->type != CPP_SEMICOLON)
10053 cp_parser_error (parser, "expected init-declarator");
10054 return error_mark_node;
10057 /* Because start_decl has side-effects, we should only call it if we
10058 know we're going ahead. By this point, we know that we cannot
10059 possibly be looking at any other construct. */
10060 cp_parser_commit_to_tentative_parse (parser);
10062 /* If the decl specifiers were bad, issue an error now that we're
10063 sure this was intended to be a declarator. Then continue
10064 declaring the variable(s), as int, to try to cut down on further
10066 if (decl_specifiers != NULL
10067 && TREE_VALUE (decl_specifiers) == error_mark_node)
10069 cp_parser_error (parser, "invalid type in declaration");
10070 TREE_VALUE (decl_specifiers) = integer_type_node;
10073 /* Check to see whether or not this declaration is a friend. */
10074 friend_p = cp_parser_friend_p (decl_specifiers);
10076 /* Check that the number of template-parameter-lists is OK. */
10077 if (!cp_parser_check_declarator_template_parameters (parser, declarator))
10078 return error_mark_node;
10080 /* Enter the newly declared entry in the symbol table. If we're
10081 processing a declaration in a class-specifier, we wait until
10082 after processing the initializer. */
10085 if (parser->in_unbraced_linkage_specification_p)
10087 decl_specifiers = tree_cons (error_mark_node,
10088 get_identifier ("extern"),
10090 have_extern_spec = false;
10092 decl = start_decl (declarator, decl_specifiers,
10093 is_initialized, attributes, prefix_attributes);
10096 /* Enter the SCOPE. That way unqualified names appearing in the
10097 initializer will be looked up in SCOPE. */
10099 pop_p = push_scope (scope);
10101 /* Perform deferred access control checks, now that we know in which
10102 SCOPE the declared entity resides. */
10103 if (!member_p && decl)
10105 tree saved_current_function_decl = NULL_TREE;
10107 /* If the entity being declared is a function, pretend that we
10108 are in its scope. If it is a `friend', it may have access to
10109 things that would not otherwise be accessible. */
10110 if (TREE_CODE (decl) == FUNCTION_DECL)
10112 saved_current_function_decl = current_function_decl;
10113 current_function_decl = decl;
10116 /* Perform the access control checks for the declarator and the
10117 the decl-specifiers. */
10118 perform_deferred_access_checks ();
10120 /* Restore the saved value. */
10121 if (TREE_CODE (decl) == FUNCTION_DECL)
10122 current_function_decl = saved_current_function_decl;
10125 /* Parse the initializer. */
10126 if (is_initialized)
10127 initializer = cp_parser_initializer (parser,
10128 &is_parenthesized_init,
10129 &is_non_constant_init);
10132 initializer = NULL_TREE;
10133 is_parenthesized_init = false;
10134 is_non_constant_init = true;
10137 /* The old parser allows attributes to appear after a parenthesized
10138 initializer. Mark Mitchell proposed removing this functionality
10139 on the GCC mailing lists on 2002-08-13. This parser accepts the
10140 attributes -- but ignores them. */
10141 if (cp_parser_allow_gnu_extensions_p (parser) && is_parenthesized_init)
10142 if (cp_parser_attributes_opt (parser))
10143 warning ("attributes after parenthesized initializer ignored");
10145 /* Leave the SCOPE, now that we have processed the initializer. It
10146 is important to do this before calling cp_finish_decl because it
10147 makes decisions about whether to create DECL_STMTs or not based
10148 on the current scope. */
10152 /* For an in-class declaration, use `grokfield' to create the
10156 decl = grokfield (declarator, decl_specifiers,
10157 initializer, /*asmspec=*/NULL_TREE,
10158 /*attributes=*/NULL_TREE);
10159 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
10160 cp_parser_save_default_args (parser, decl);
10163 /* Finish processing the declaration. But, skip friend
10165 if (!friend_p && decl)
10166 cp_finish_decl (decl,
10169 /* If the initializer is in parentheses, then this is
10170 a direct-initialization, which means that an
10171 `explicit' constructor is OK. Otherwise, an
10172 `explicit' constructor cannot be used. */
10173 ((is_parenthesized_init || !is_initialized)
10174 ? 0 : LOOKUP_ONLYCONVERTING));
10176 /* Remember whether or not variables were initialized by
10177 constant-expressions. */
10178 if (decl && TREE_CODE (decl) == VAR_DECL
10179 && is_initialized && !is_non_constant_init)
10180 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = true;
10185 /* Parse a declarator.
10189 ptr-operator declarator
10191 abstract-declarator:
10192 ptr-operator abstract-declarator [opt]
10193 direct-abstract-declarator
10198 attributes [opt] direct-declarator
10199 attributes [opt] ptr-operator declarator
10201 abstract-declarator:
10202 attributes [opt] ptr-operator abstract-declarator [opt]
10203 attributes [opt] direct-abstract-declarator
10205 Returns a representation of the declarator. If the declarator has
10206 the form `* declarator', then an INDIRECT_REF is returned, whose
10207 only operand is the sub-declarator. Analogously, `& declarator' is
10208 represented as an ADDR_EXPR. For `X::* declarator', a SCOPE_REF is
10209 used. The first operand is the TYPE for `X'. The second operand
10210 is an INDIRECT_REF whose operand is the sub-declarator.
10212 Otherwise, the representation is as for a direct-declarator.
10214 (It would be better to define a structure type to represent
10215 declarators, rather than abusing `tree' nodes to represent
10216 declarators. That would be much clearer and save some memory.
10217 There is no reason for declarators to be garbage-collected, for
10218 example; they are created during parser and no longer needed after
10219 `grokdeclarator' has been called.)
10221 For a ptr-operator that has the optional cv-qualifier-seq,
10222 cv-qualifiers will be stored in the TREE_TYPE of the INDIRECT_REF
10225 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
10226 detect constructor, destructor or conversion operators. It is set
10227 to -1 if the declarator is a name, and +1 if it is a
10228 function. Otherwise it is set to zero. Usually you just want to
10229 test for >0, but internally the negative value is used.
10231 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
10232 a decl-specifier-seq unless it declares a constructor, destructor,
10233 or conversion. It might seem that we could check this condition in
10234 semantic analysis, rather than parsing, but that makes it difficult
10235 to handle something like `f()'. We want to notice that there are
10236 no decl-specifiers, and therefore realize that this is an
10237 expression, not a declaration.)
10239 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
10240 the declarator is a direct-declarator of the form "(...)". */
10243 cp_parser_declarator (cp_parser* parser,
10244 cp_parser_declarator_kind dcl_kind,
10245 int* ctor_dtor_or_conv_p,
10246 bool* parenthesized_p)
10250 enum tree_code code;
10251 tree cv_qualifier_seq;
10253 tree attributes = NULL_TREE;
10255 /* Assume this is not a constructor, destructor, or type-conversion
10257 if (ctor_dtor_or_conv_p)
10258 *ctor_dtor_or_conv_p = 0;
10260 if (cp_parser_allow_gnu_extensions_p (parser))
10261 attributes = cp_parser_attributes_opt (parser);
10263 /* Peek at the next token. */
10264 token = cp_lexer_peek_token (parser->lexer);
10266 /* Check for the ptr-operator production. */
10267 cp_parser_parse_tentatively (parser);
10268 /* Parse the ptr-operator. */
10269 code = cp_parser_ptr_operator (parser,
10271 &cv_qualifier_seq);
10272 /* If that worked, then we have a ptr-operator. */
10273 if (cp_parser_parse_definitely (parser))
10275 /* If a ptr-operator was found, then this declarator was not
10277 if (parenthesized_p)
10278 *parenthesized_p = true;
10279 /* The dependent declarator is optional if we are parsing an
10280 abstract-declarator. */
10281 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
10282 cp_parser_parse_tentatively (parser);
10284 /* Parse the dependent declarator. */
10285 declarator = cp_parser_declarator (parser, dcl_kind,
10286 /*ctor_dtor_or_conv_p=*/NULL,
10287 /*parenthesized_p=*/NULL);
10289 /* If we are parsing an abstract-declarator, we must handle the
10290 case where the dependent declarator is absent. */
10291 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
10292 && !cp_parser_parse_definitely (parser))
10293 declarator = NULL_TREE;
10295 /* Build the representation of the ptr-operator. */
10296 if (code == INDIRECT_REF)
10297 declarator = make_pointer_declarator (cv_qualifier_seq,
10300 declarator = make_reference_declarator (cv_qualifier_seq,
10302 /* Handle the pointer-to-member case. */
10304 declarator = build_nt (SCOPE_REF, class_type, declarator);
10306 /* Everything else is a direct-declarator. */
10309 if (parenthesized_p)
10310 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
10312 declarator = cp_parser_direct_declarator (parser, dcl_kind,
10313 ctor_dtor_or_conv_p);
10316 if (attributes && declarator != error_mark_node)
10317 declarator = tree_cons (attributes, declarator, NULL_TREE);
10322 /* Parse a direct-declarator or direct-abstract-declarator.
10326 direct-declarator ( parameter-declaration-clause )
10327 cv-qualifier-seq [opt]
10328 exception-specification [opt]
10329 direct-declarator [ constant-expression [opt] ]
10332 direct-abstract-declarator:
10333 direct-abstract-declarator [opt]
10334 ( parameter-declaration-clause )
10335 cv-qualifier-seq [opt]
10336 exception-specification [opt]
10337 direct-abstract-declarator [opt] [ constant-expression [opt] ]
10338 ( abstract-declarator )
10340 Returns a representation of the declarator. DCL_KIND is
10341 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
10342 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
10343 we are parsing a direct-declarator. It is
10344 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
10345 of ambiguity we prefer an abstract declarator, as per
10346 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P is as for
10347 cp_parser_declarator.
10349 For the declarator-id production, the representation is as for an
10350 id-expression, except that a qualified name is represented as a
10351 SCOPE_REF. A function-declarator is represented as a CALL_EXPR;
10352 see the documentation of the FUNCTION_DECLARATOR_* macros for
10353 information about how to find the various declarator components.
10354 An array-declarator is represented as an ARRAY_REF. The
10355 direct-declarator is the first operand; the constant-expression
10356 indicating the size of the array is the second operand. */
10359 cp_parser_direct_declarator (cp_parser* parser,
10360 cp_parser_declarator_kind dcl_kind,
10361 int* ctor_dtor_or_conv_p)
10364 tree declarator = NULL_TREE;
10365 tree scope = NULL_TREE;
10366 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
10367 bool saved_in_declarator_p = parser->in_declarator_p;
10369 bool pop_p = false;
10373 /* Peek at the next token. */
10374 token = cp_lexer_peek_token (parser->lexer);
10375 if (token->type == CPP_OPEN_PAREN)
10377 /* This is either a parameter-declaration-clause, or a
10378 parenthesized declarator. When we know we are parsing a
10379 named declarator, it must be a parenthesized declarator
10380 if FIRST is true. For instance, `(int)' is a
10381 parameter-declaration-clause, with an omitted
10382 direct-abstract-declarator. But `((*))', is a
10383 parenthesized abstract declarator. Finally, when T is a
10384 template parameter `(T)' is a
10385 parameter-declaration-clause, and not a parenthesized
10388 We first try and parse a parameter-declaration-clause,
10389 and then try a nested declarator (if FIRST is true).
10391 It is not an error for it not to be a
10392 parameter-declaration-clause, even when FIRST is
10398 The first is the declaration of a function while the
10399 second is a the definition of a variable, including its
10402 Having seen only the parenthesis, we cannot know which of
10403 these two alternatives should be selected. Even more
10404 complex are examples like:
10409 The former is a function-declaration; the latter is a
10410 variable initialization.
10412 Thus again, we try a parameter-declaration-clause, and if
10413 that fails, we back out and return. */
10415 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
10418 unsigned saved_num_template_parameter_lists;
10420 cp_parser_parse_tentatively (parser);
10422 /* Consume the `('. */
10423 cp_lexer_consume_token (parser->lexer);
10426 /* If this is going to be an abstract declarator, we're
10427 in a declarator and we can't have default args. */
10428 parser->default_arg_ok_p = false;
10429 parser->in_declarator_p = true;
10432 /* Inside the function parameter list, surrounding
10433 template-parameter-lists do not apply. */
10434 saved_num_template_parameter_lists
10435 = parser->num_template_parameter_lists;
10436 parser->num_template_parameter_lists = 0;
10438 /* Parse the parameter-declaration-clause. */
10439 params = cp_parser_parameter_declaration_clause (parser);
10441 parser->num_template_parameter_lists
10442 = saved_num_template_parameter_lists;
10444 /* If all went well, parse the cv-qualifier-seq and the
10445 exception-specification. */
10446 if (cp_parser_parse_definitely (parser))
10448 tree cv_qualifiers;
10449 tree exception_specification;
10451 if (ctor_dtor_or_conv_p)
10452 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
10454 /* Consume the `)'. */
10455 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
10457 /* Parse the cv-qualifier-seq. */
10458 cv_qualifiers = cp_parser_cv_qualifier_seq_opt (parser);
10459 /* And the exception-specification. */
10460 exception_specification
10461 = cp_parser_exception_specification_opt (parser);
10463 /* Create the function-declarator. */
10464 declarator = make_call_declarator (declarator,
10467 exception_specification);
10468 /* Any subsequent parameter lists are to do with
10469 return type, so are not those of the declared
10471 parser->default_arg_ok_p = false;
10473 /* Repeat the main loop. */
10478 /* If this is the first, we can try a parenthesized
10482 bool saved_in_type_id_in_expr_p;
10484 parser->default_arg_ok_p = saved_default_arg_ok_p;
10485 parser->in_declarator_p = saved_in_declarator_p;
10487 /* Consume the `('. */
10488 cp_lexer_consume_token (parser->lexer);
10489 /* Parse the nested declarator. */
10490 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
10491 parser->in_type_id_in_expr_p = true;
10493 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
10494 /*parenthesized_p=*/NULL);
10495 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
10497 /* Expect a `)'. */
10498 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
10499 declarator = error_mark_node;
10500 if (declarator == error_mark_node)
10503 goto handle_declarator;
10505 /* Otherwise, we must be done. */
10509 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
10510 && token->type == CPP_OPEN_SQUARE)
10512 /* Parse an array-declarator. */
10515 if (ctor_dtor_or_conv_p)
10516 *ctor_dtor_or_conv_p = 0;
10519 parser->default_arg_ok_p = false;
10520 parser->in_declarator_p = true;
10521 /* Consume the `['. */
10522 cp_lexer_consume_token (parser->lexer);
10523 /* Peek at the next token. */
10524 token = cp_lexer_peek_token (parser->lexer);
10525 /* If the next token is `]', then there is no
10526 constant-expression. */
10527 if (token->type != CPP_CLOSE_SQUARE)
10529 bool non_constant_p;
10532 = cp_parser_constant_expression (parser,
10533 /*allow_non_constant=*/true,
10535 if (!non_constant_p)
10536 bounds = fold_non_dependent_expr (bounds);
10539 bounds = NULL_TREE;
10540 /* Look for the closing `]'. */
10541 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'"))
10543 declarator = error_mark_node;
10547 declarator = build_nt (ARRAY_REF, declarator, bounds);
10549 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
10551 /* Parse a declarator-id */
10552 if (dcl_kind == CP_PARSER_DECLARATOR_EITHER)
10553 cp_parser_parse_tentatively (parser);
10554 declarator = cp_parser_declarator_id (parser);
10555 if (dcl_kind == CP_PARSER_DECLARATOR_EITHER)
10557 if (!cp_parser_parse_definitely (parser))
10558 declarator = error_mark_node;
10559 else if (TREE_CODE (declarator) != IDENTIFIER_NODE)
10561 cp_parser_error (parser, "expected unqualified-id");
10562 declarator = error_mark_node;
10566 if (declarator == error_mark_node)
10569 if (TREE_CODE (declarator) == SCOPE_REF
10570 && !current_scope ())
10572 tree scope = TREE_OPERAND (declarator, 0);
10574 /* In the declaration of a member of a template class
10575 outside of the class itself, the SCOPE will sometimes
10576 be a TYPENAME_TYPE. For example, given:
10578 template <typename T>
10579 int S<T>::R::i = 3;
10581 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
10582 this context, we must resolve S<T>::R to an ordinary
10583 type, rather than a typename type.
10585 The reason we normally avoid resolving TYPENAME_TYPEs
10586 is that a specialization of `S' might render
10587 `S<T>::R' not a type. However, if `S' is
10588 specialized, then this `i' will not be used, so there
10589 is no harm in resolving the types here. */
10590 if (TREE_CODE (scope) == TYPENAME_TYPE)
10594 /* Resolve the TYPENAME_TYPE. */
10595 type = resolve_typename_type (scope,
10596 /*only_current_p=*/false);
10597 /* If that failed, the declarator is invalid. */
10598 if (type != error_mark_node)
10600 /* Build a new DECLARATOR. */
10601 declarator = build_nt (SCOPE_REF,
10603 TREE_OPERAND (declarator, 1));
10607 /* Check to see whether the declarator-id names a constructor,
10608 destructor, or conversion. */
10609 if (declarator && ctor_dtor_or_conv_p
10610 && ((TREE_CODE (declarator) == SCOPE_REF
10611 && CLASS_TYPE_P (TREE_OPERAND (declarator, 0)))
10612 || (TREE_CODE (declarator) != SCOPE_REF
10613 && at_class_scope_p ())))
10615 tree unqualified_name;
10618 /* Get the unqualified part of the name. */
10619 if (TREE_CODE (declarator) == SCOPE_REF)
10621 class_type = TREE_OPERAND (declarator, 0);
10622 unqualified_name = TREE_OPERAND (declarator, 1);
10626 class_type = current_class_type;
10627 unqualified_name = declarator;
10630 /* See if it names ctor, dtor or conv. */
10631 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR
10632 || IDENTIFIER_TYPENAME_P (unqualified_name)
10633 || constructor_name_p (unqualified_name, class_type)
10634 || (TREE_CODE (unqualified_name) == TYPE_DECL
10635 && same_type_p (TREE_TYPE (unqualified_name),
10637 *ctor_dtor_or_conv_p = -1;
10640 handle_declarator:;
10641 scope = get_scope_of_declarator (declarator);
10643 /* Any names that appear after the declarator-id for a
10644 member are looked up in the containing scope. */
10645 pop_p = push_scope (scope);
10646 parser->in_declarator_p = true;
10647 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
10649 && (TREE_CODE (declarator) == SCOPE_REF
10650 || TREE_CODE (declarator) == IDENTIFIER_NODE)))
10651 /* Default args are only allowed on function
10653 parser->default_arg_ok_p = saved_default_arg_ok_p;
10655 parser->default_arg_ok_p = false;
10664 /* For an abstract declarator, we might wind up with nothing at this
10665 point. That's an error; the declarator is not optional. */
10667 cp_parser_error (parser, "expected declarator");
10669 /* If we entered a scope, we must exit it now. */
10673 parser->default_arg_ok_p = saved_default_arg_ok_p;
10674 parser->in_declarator_p = saved_in_declarator_p;
10679 /* Parse a ptr-operator.
10682 * cv-qualifier-seq [opt]
10684 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
10689 & cv-qualifier-seq [opt]
10691 Returns INDIRECT_REF if a pointer, or pointer-to-member, was
10692 used. Returns ADDR_EXPR if a reference was used. In the
10693 case of a pointer-to-member, *TYPE is filled in with the
10694 TYPE containing the member. *CV_QUALIFIER_SEQ is filled in
10695 with the cv-qualifier-seq, or NULL_TREE, if there are no
10696 cv-qualifiers. Returns ERROR_MARK if an error occurred. */
10698 static enum tree_code
10699 cp_parser_ptr_operator (cp_parser* parser,
10701 tree* cv_qualifier_seq)
10703 enum tree_code code = ERROR_MARK;
10706 /* Assume that it's not a pointer-to-member. */
10708 /* And that there are no cv-qualifiers. */
10709 *cv_qualifier_seq = NULL_TREE;
10711 /* Peek at the next token. */
10712 token = cp_lexer_peek_token (parser->lexer);
10713 /* If it's a `*' or `&' we have a pointer or reference. */
10714 if (token->type == CPP_MULT || token->type == CPP_AND)
10716 /* Remember which ptr-operator we were processing. */
10717 code = (token->type == CPP_AND ? ADDR_EXPR : INDIRECT_REF);
10719 /* Consume the `*' or `&'. */
10720 cp_lexer_consume_token (parser->lexer);
10722 /* A `*' can be followed by a cv-qualifier-seq, and so can a
10723 `&', if we are allowing GNU extensions. (The only qualifier
10724 that can legally appear after `&' is `restrict', but that is
10725 enforced during semantic analysis. */
10726 if (code == INDIRECT_REF
10727 || cp_parser_allow_gnu_extensions_p (parser))
10728 *cv_qualifier_seq = cp_parser_cv_qualifier_seq_opt (parser);
10732 /* Try the pointer-to-member case. */
10733 cp_parser_parse_tentatively (parser);
10734 /* Look for the optional `::' operator. */
10735 cp_parser_global_scope_opt (parser,
10736 /*current_scope_valid_p=*/false);
10737 /* Look for the nested-name specifier. */
10738 cp_parser_nested_name_specifier (parser,
10739 /*typename_keyword_p=*/false,
10740 /*check_dependency_p=*/true,
10742 /*is_declaration=*/false);
10743 /* If we found it, and the next token is a `*', then we are
10744 indeed looking at a pointer-to-member operator. */
10745 if (!cp_parser_error_occurred (parser)
10746 && cp_parser_require (parser, CPP_MULT, "`*'"))
10748 /* The type of which the member is a member is given by the
10750 *type = parser->scope;
10751 /* The next name will not be qualified. */
10752 parser->scope = NULL_TREE;
10753 parser->qualifying_scope = NULL_TREE;
10754 parser->object_scope = NULL_TREE;
10755 /* Indicate that the `*' operator was used. */
10756 code = INDIRECT_REF;
10757 /* Look for the optional cv-qualifier-seq. */
10758 *cv_qualifier_seq = cp_parser_cv_qualifier_seq_opt (parser);
10760 /* If that didn't work we don't have a ptr-operator. */
10761 if (!cp_parser_parse_definitely (parser))
10762 cp_parser_error (parser, "expected ptr-operator");
10768 /* Parse an (optional) cv-qualifier-seq.
10771 cv-qualifier cv-qualifier-seq [opt]
10773 Returns a TREE_LIST. The TREE_VALUE of each node is the
10774 representation of a cv-qualifier. */
10777 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
10779 tree cv_qualifiers = NULL_TREE;
10785 /* Look for the next cv-qualifier. */
10786 cv_qualifier = cp_parser_cv_qualifier_opt (parser);
10787 /* If we didn't find one, we're done. */
10791 /* Add this cv-qualifier to the list. */
10793 = tree_cons (NULL_TREE, cv_qualifier, cv_qualifiers);
10796 /* We built up the list in reverse order. */
10797 return nreverse (cv_qualifiers);
10800 /* Parse an (optional) cv-qualifier.
10812 cp_parser_cv_qualifier_opt (cp_parser* parser)
10815 tree cv_qualifier = NULL_TREE;
10817 /* Peek at the next token. */
10818 token = cp_lexer_peek_token (parser->lexer);
10819 /* See if it's a cv-qualifier. */
10820 switch (token->keyword)
10825 /* Save the value of the token. */
10826 cv_qualifier = token->value;
10827 /* Consume the token. */
10828 cp_lexer_consume_token (parser->lexer);
10835 return cv_qualifier;
10838 /* Parse a declarator-id.
10842 :: [opt] nested-name-specifier [opt] type-name
10844 In the `id-expression' case, the value returned is as for
10845 cp_parser_id_expression if the id-expression was an unqualified-id.
10846 If the id-expression was a qualified-id, then a SCOPE_REF is
10847 returned. The first operand is the scope (either a NAMESPACE_DECL
10848 or TREE_TYPE), but the second is still just a representation of an
10852 cp_parser_declarator_id (cp_parser* parser)
10854 tree id_expression;
10856 /* The expression must be an id-expression. Assume that qualified
10857 names are the names of types so that:
10860 int S<T>::R::i = 3;
10862 will work; we must treat `S<T>::R' as the name of a type.
10863 Similarly, assume that qualified names are templates, where
10867 int S<T>::R<T>::i = 3;
10870 id_expression = cp_parser_id_expression (parser,
10871 /*template_keyword_p=*/false,
10872 /*check_dependency_p=*/false,
10873 /*template_p=*/NULL,
10874 /*declarator_p=*/true);
10875 /* If the name was qualified, create a SCOPE_REF to represent
10879 id_expression = build_nt (SCOPE_REF, parser->scope, id_expression);
10880 parser->scope = NULL_TREE;
10883 return id_expression;
10886 /* Parse a type-id.
10889 type-specifier-seq abstract-declarator [opt]
10891 Returns the TYPE specified. */
10894 cp_parser_type_id (cp_parser* parser)
10896 tree type_specifier_seq;
10897 tree abstract_declarator;
10899 /* Parse the type-specifier-seq. */
10901 = cp_parser_type_specifier_seq (parser);
10902 if (type_specifier_seq == error_mark_node)
10903 return error_mark_node;
10905 /* There might or might not be an abstract declarator. */
10906 cp_parser_parse_tentatively (parser);
10907 /* Look for the declarator. */
10908 abstract_declarator
10909 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
10910 /*parenthesized_p=*/NULL);
10911 /* Check to see if there really was a declarator. */
10912 if (!cp_parser_parse_definitely (parser))
10913 abstract_declarator = NULL_TREE;
10915 return groktypename (build_tree_list (type_specifier_seq,
10916 abstract_declarator));
10919 /* Parse a type-specifier-seq.
10921 type-specifier-seq:
10922 type-specifier type-specifier-seq [opt]
10926 type-specifier-seq:
10927 attributes type-specifier-seq [opt]
10929 Returns a TREE_LIST. Either the TREE_VALUE of each node is a
10930 type-specifier, or the TREE_PURPOSE is a list of attributes. */
10933 cp_parser_type_specifier_seq (cp_parser* parser)
10935 bool seen_type_specifier = false;
10936 tree type_specifier_seq = NULL_TREE;
10938 /* Parse the type-specifiers and attributes. */
10941 tree type_specifier;
10943 /* Check for attributes first. */
10944 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
10946 type_specifier_seq = tree_cons (cp_parser_attributes_opt (parser),
10948 type_specifier_seq);
10952 /* After the first type-specifier, others are optional. */
10953 if (seen_type_specifier)
10954 cp_parser_parse_tentatively (parser);
10955 /* Look for the type-specifier. */
10956 type_specifier = cp_parser_type_specifier (parser,
10957 CP_PARSER_FLAGS_NONE,
10958 /*is_friend=*/false,
10959 /*is_declaration=*/false,
10962 /* If the first type-specifier could not be found, this is not a
10963 type-specifier-seq at all. */
10964 if (!seen_type_specifier && type_specifier == error_mark_node)
10965 return error_mark_node;
10966 /* If subsequent type-specifiers could not be found, the
10967 type-specifier-seq is complete. */
10968 else if (seen_type_specifier && !cp_parser_parse_definitely (parser))
10971 /* Add the new type-specifier to the list. */
10973 = tree_cons (NULL_TREE, type_specifier, type_specifier_seq);
10974 seen_type_specifier = true;
10977 /* We built up the list in reverse order. */
10978 return nreverse (type_specifier_seq);
10981 /* Parse a parameter-declaration-clause.
10983 parameter-declaration-clause:
10984 parameter-declaration-list [opt] ... [opt]
10985 parameter-declaration-list , ...
10987 Returns a representation for the parameter declarations. Each node
10988 is a TREE_LIST. (See cp_parser_parameter_declaration for the exact
10989 representation.) If the parameter-declaration-clause ends with an
10990 ellipsis, PARMLIST_ELLIPSIS_P will hold of the first node in the
10991 list. A return value of NULL_TREE indicates a
10992 parameter-declaration-clause consisting only of an ellipsis. */
10995 cp_parser_parameter_declaration_clause (cp_parser* parser)
11001 /* Peek at the next token. */
11002 token = cp_lexer_peek_token (parser->lexer);
11003 /* Check for trivial parameter-declaration-clauses. */
11004 if (token->type == CPP_ELLIPSIS)
11006 /* Consume the `...' token. */
11007 cp_lexer_consume_token (parser->lexer);
11010 else if (token->type == CPP_CLOSE_PAREN)
11011 /* There are no parameters. */
11013 #ifndef NO_IMPLICIT_EXTERN_C
11014 if (in_system_header && current_class_type == NULL
11015 && current_lang_name == lang_name_c)
11019 return void_list_node;
11021 /* Check for `(void)', too, which is a special case. */
11022 else if (token->keyword == RID_VOID
11023 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
11024 == CPP_CLOSE_PAREN))
11026 /* Consume the `void' token. */
11027 cp_lexer_consume_token (parser->lexer);
11028 /* There are no parameters. */
11029 return void_list_node;
11032 /* Parse the parameter-declaration-list. */
11033 parameters = cp_parser_parameter_declaration_list (parser);
11034 /* If a parse error occurred while parsing the
11035 parameter-declaration-list, then the entire
11036 parameter-declaration-clause is erroneous. */
11037 if (parameters == error_mark_node)
11038 return error_mark_node;
11040 /* Peek at the next token. */
11041 token = cp_lexer_peek_token (parser->lexer);
11042 /* If it's a `,', the clause should terminate with an ellipsis. */
11043 if (token->type == CPP_COMMA)
11045 /* Consume the `,'. */
11046 cp_lexer_consume_token (parser->lexer);
11047 /* Expect an ellipsis. */
11049 = (cp_parser_require (parser, CPP_ELLIPSIS, "`...'") != NULL);
11051 /* It might also be `...' if the optional trailing `,' was
11053 else if (token->type == CPP_ELLIPSIS)
11055 /* Consume the `...' token. */
11056 cp_lexer_consume_token (parser->lexer);
11057 /* And remember that we saw it. */
11061 ellipsis_p = false;
11063 /* Finish the parameter list. */
11064 return finish_parmlist (parameters, ellipsis_p);
11067 /* Parse a parameter-declaration-list.
11069 parameter-declaration-list:
11070 parameter-declaration
11071 parameter-declaration-list , parameter-declaration
11073 Returns a representation of the parameter-declaration-list, as for
11074 cp_parser_parameter_declaration_clause. However, the
11075 `void_list_node' is never appended to the list. */
11078 cp_parser_parameter_declaration_list (cp_parser* parser)
11080 tree parameters = NULL_TREE;
11082 /* Look for more parameters. */
11086 bool parenthesized_p;
11087 /* Parse the parameter. */
11089 = cp_parser_parameter_declaration (parser,
11090 /*template_parm_p=*/false,
11093 /* If a parse error occurred parsing the parameter declaration,
11094 then the entire parameter-declaration-list is erroneous. */
11095 if (parameter == error_mark_node)
11097 parameters = error_mark_node;
11100 /* Add the new parameter to the list. */
11101 TREE_CHAIN (parameter) = parameters;
11102 parameters = parameter;
11104 /* Peek at the next token. */
11105 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
11106 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11107 /* The parameter-declaration-list is complete. */
11109 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
11113 /* Peek at the next token. */
11114 token = cp_lexer_peek_nth_token (parser->lexer, 2);
11115 /* If it's an ellipsis, then the list is complete. */
11116 if (token->type == CPP_ELLIPSIS)
11118 /* Otherwise, there must be more parameters. Consume the
11120 cp_lexer_consume_token (parser->lexer);
11121 /* When parsing something like:
11123 int i(float f, double d)
11125 we can tell after seeing the declaration for "f" that we
11126 are not looking at an initialization of a variable "i",
11127 but rather at the declaration of a function "i".
11129 Due to the fact that the parsing of template arguments
11130 (as specified to a template-id) requires backtracking we
11131 cannot use this technique when inside a template argument
11133 if (!parser->in_template_argument_list_p
11134 && !parser->in_type_id_in_expr_p
11135 && cp_parser_parsing_tentatively (parser)
11136 && !cp_parser_committed_to_tentative_parse (parser)
11137 /* However, a parameter-declaration of the form
11138 "foat(f)" (which is a valid declaration of a
11139 parameter "f") can also be interpreted as an
11140 expression (the conversion of "f" to "float"). */
11141 && !parenthesized_p)
11142 cp_parser_commit_to_tentative_parse (parser);
11146 cp_parser_error (parser, "expected `,' or `...'");
11147 if (!cp_parser_parsing_tentatively (parser)
11148 || cp_parser_committed_to_tentative_parse (parser))
11149 cp_parser_skip_to_closing_parenthesis (parser,
11150 /*recovering=*/true,
11151 /*or_comma=*/false,
11152 /*consume_paren=*/false);
11157 /* We built up the list in reverse order; straighten it out now. */
11158 return nreverse (parameters);
11161 /* Parse a parameter declaration.
11163 parameter-declaration:
11164 decl-specifier-seq declarator
11165 decl-specifier-seq declarator = assignment-expression
11166 decl-specifier-seq abstract-declarator [opt]
11167 decl-specifier-seq abstract-declarator [opt] = assignment-expression
11169 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
11170 declares a template parameter. (In that case, a non-nested `>'
11171 token encountered during the parsing of the assignment-expression
11172 is not interpreted as a greater-than operator.)
11174 Returns a TREE_LIST representing the parameter-declaration. The
11175 TREE_PURPOSE is the default argument expression, or NULL_TREE if
11176 there is no default argument. The TREE_VALUE is a representation
11177 of the decl-specifier-seq and declarator. In particular, the
11178 TREE_VALUE will be a TREE_LIST whose TREE_PURPOSE represents the
11179 decl-specifier-seq and whose TREE_VALUE represents the declarator.
11180 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
11181 the declarator is of the form "(p)". */
11184 cp_parser_parameter_declaration (cp_parser *parser,
11185 bool template_parm_p,
11186 bool *parenthesized_p)
11188 int declares_class_or_enum;
11189 bool greater_than_is_operator_p;
11190 tree decl_specifiers;
11193 tree default_argument;
11196 const char *saved_message;
11198 /* In a template parameter, `>' is not an operator.
11202 When parsing a default template-argument for a non-type
11203 template-parameter, the first non-nested `>' is taken as the end
11204 of the template parameter-list rather than a greater-than
11206 greater_than_is_operator_p = !template_parm_p;
11208 /* Type definitions may not appear in parameter types. */
11209 saved_message = parser->type_definition_forbidden_message;
11210 parser->type_definition_forbidden_message
11211 = "types may not be defined in parameter types";
11213 /* Parse the declaration-specifiers. */
11215 = cp_parser_decl_specifier_seq (parser,
11216 CP_PARSER_FLAGS_NONE,
11218 &declares_class_or_enum);
11219 /* If an error occurred, there's no reason to attempt to parse the
11220 rest of the declaration. */
11221 if (cp_parser_error_occurred (parser))
11223 parser->type_definition_forbidden_message = saved_message;
11224 return error_mark_node;
11227 /* Peek at the next token. */
11228 token = cp_lexer_peek_token (parser->lexer);
11229 /* If the next token is a `)', `,', `=', `>', or `...', then there
11230 is no declarator. */
11231 if (token->type == CPP_CLOSE_PAREN
11232 || token->type == CPP_COMMA
11233 || token->type == CPP_EQ
11234 || token->type == CPP_ELLIPSIS
11235 || token->type == CPP_GREATER)
11237 declarator = NULL_TREE;
11238 if (parenthesized_p)
11239 *parenthesized_p = false;
11241 /* Otherwise, there should be a declarator. */
11244 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
11245 parser->default_arg_ok_p = false;
11247 /* After seeing a decl-specifier-seq, if the next token is not a
11248 "(", there is no possibility that the code is a valid
11249 expression. Therefore, if parsing tentatively, we commit at
11251 if (!parser->in_template_argument_list_p
11252 /* In an expression context, having seen:
11256 we cannot be sure whether we are looking at a
11257 function-type (taking a "char" as a parameter) or a cast
11258 of some object of type "char" to "int". */
11259 && !parser->in_type_id_in_expr_p
11260 && cp_parser_parsing_tentatively (parser)
11261 && !cp_parser_committed_to_tentative_parse (parser)
11262 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
11263 cp_parser_commit_to_tentative_parse (parser);
11264 /* Parse the declarator. */
11265 declarator = cp_parser_declarator (parser,
11266 CP_PARSER_DECLARATOR_EITHER,
11267 /*ctor_dtor_or_conv_p=*/NULL,
11269 parser->default_arg_ok_p = saved_default_arg_ok_p;
11270 /* After the declarator, allow more attributes. */
11271 attributes = chainon (attributes, cp_parser_attributes_opt (parser));
11274 /* The restriction on defining new types applies only to the type
11275 of the parameter, not to the default argument. */
11276 parser->type_definition_forbidden_message = saved_message;
11278 /* If the next token is `=', then process a default argument. */
11279 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11281 bool saved_greater_than_is_operator_p;
11282 /* Consume the `='. */
11283 cp_lexer_consume_token (parser->lexer);
11285 /* If we are defining a class, then the tokens that make up the
11286 default argument must be saved and processed later. */
11287 if (!template_parm_p && at_class_scope_p ()
11288 && TYPE_BEING_DEFINED (current_class_type))
11290 unsigned depth = 0;
11292 /* Create a DEFAULT_ARG to represented the unparsed default
11294 default_argument = make_node (DEFAULT_ARG);
11295 DEFARG_TOKENS (default_argument) = cp_token_cache_new ();
11297 /* Add tokens until we have processed the entire default
11304 /* Peek at the next token. */
11305 token = cp_lexer_peek_token (parser->lexer);
11306 /* What we do depends on what token we have. */
11307 switch (token->type)
11309 /* In valid code, a default argument must be
11310 immediately followed by a `,' `)', or `...'. */
11312 case CPP_CLOSE_PAREN:
11314 /* If we run into a non-nested `;', `}', or `]',
11315 then the code is invalid -- but the default
11316 argument is certainly over. */
11317 case CPP_SEMICOLON:
11318 case CPP_CLOSE_BRACE:
11319 case CPP_CLOSE_SQUARE:
11322 /* Update DEPTH, if necessary. */
11323 else if (token->type == CPP_CLOSE_PAREN
11324 || token->type == CPP_CLOSE_BRACE
11325 || token->type == CPP_CLOSE_SQUARE)
11329 case CPP_OPEN_PAREN:
11330 case CPP_OPEN_SQUARE:
11331 case CPP_OPEN_BRACE:
11336 /* If we see a non-nested `>', and `>' is not an
11337 operator, then it marks the end of the default
11339 if (!depth && !greater_than_is_operator_p)
11343 /* If we run out of tokens, issue an error message. */
11345 error ("file ends in default argument");
11351 /* In these cases, we should look for template-ids.
11352 For example, if the default argument is
11353 `X<int, double>()', we need to do name lookup to
11354 figure out whether or not `X' is a template; if
11355 so, the `,' does not end the default argument.
11357 That is not yet done. */
11364 /* If we've reached the end, stop. */
11368 /* Add the token to the token block. */
11369 token = cp_lexer_consume_token (parser->lexer);
11370 cp_token_cache_push_token (DEFARG_TOKENS (default_argument),
11374 /* Outside of a class definition, we can just parse the
11375 assignment-expression. */
11378 bool saved_local_variables_forbidden_p;
11380 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
11382 saved_greater_than_is_operator_p
11383 = parser->greater_than_is_operator_p;
11384 parser->greater_than_is_operator_p = greater_than_is_operator_p;
11385 /* Local variable names (and the `this' keyword) may not
11386 appear in a default argument. */
11387 saved_local_variables_forbidden_p
11388 = parser->local_variables_forbidden_p;
11389 parser->local_variables_forbidden_p = true;
11390 /* Parse the assignment-expression. */
11391 default_argument = cp_parser_assignment_expression (parser);
11392 /* Restore saved state. */
11393 parser->greater_than_is_operator_p
11394 = saved_greater_than_is_operator_p;
11395 parser->local_variables_forbidden_p
11396 = saved_local_variables_forbidden_p;
11398 if (!parser->default_arg_ok_p)
11400 if (!flag_pedantic_errors)
11401 warning ("deprecated use of default argument for parameter of non-function");
11404 error ("default arguments are only permitted for function parameters");
11405 default_argument = NULL_TREE;
11410 default_argument = NULL_TREE;
11412 /* Create the representation of the parameter. */
11414 decl_specifiers = tree_cons (attributes, NULL_TREE, decl_specifiers);
11415 parameter = build_tree_list (default_argument,
11416 build_tree_list (decl_specifiers,
11422 /* Parse a function-body.
11425 compound_statement */
11428 cp_parser_function_body (cp_parser *parser)
11430 cp_parser_compound_statement (parser, false);
11433 /* Parse a ctor-initializer-opt followed by a function-body. Return
11434 true if a ctor-initializer was present. */
11437 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
11440 bool ctor_initializer_p;
11442 /* Begin the function body. */
11443 body = begin_function_body ();
11444 /* Parse the optional ctor-initializer. */
11445 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
11446 /* Parse the function-body. */
11447 cp_parser_function_body (parser);
11448 /* Finish the function body. */
11449 finish_function_body (body);
11451 return ctor_initializer_p;
11454 /* Parse an initializer.
11457 = initializer-clause
11458 ( expression-list )
11460 Returns a expression representing the initializer. If no
11461 initializer is present, NULL_TREE is returned.
11463 *IS_PARENTHESIZED_INIT is set to TRUE if the `( expression-list )'
11464 production is used, and zero otherwise. *IS_PARENTHESIZED_INIT is
11465 set to FALSE if there is no initializer present. If there is an
11466 initializer, and it is not a constant-expression, *NON_CONSTANT_P
11467 is set to true; otherwise it is set to false. */
11470 cp_parser_initializer (cp_parser* parser, bool* is_parenthesized_init,
11471 bool* non_constant_p)
11476 /* Peek at the next token. */
11477 token = cp_lexer_peek_token (parser->lexer);
11479 /* Let our caller know whether or not this initializer was
11481 *is_parenthesized_init = (token->type == CPP_OPEN_PAREN);
11482 /* Assume that the initializer is constant. */
11483 *non_constant_p = false;
11485 if (token->type == CPP_EQ)
11487 /* Consume the `='. */
11488 cp_lexer_consume_token (parser->lexer);
11489 /* Parse the initializer-clause. */
11490 init = cp_parser_initializer_clause (parser, non_constant_p);
11492 else if (token->type == CPP_OPEN_PAREN)
11493 init = cp_parser_parenthesized_expression_list (parser, false,
11497 /* Anything else is an error. */
11498 cp_parser_error (parser, "expected initializer");
11499 init = error_mark_node;
11505 /* Parse an initializer-clause.
11507 initializer-clause:
11508 assignment-expression
11509 { initializer-list , [opt] }
11512 Returns an expression representing the initializer.
11514 If the `assignment-expression' production is used the value
11515 returned is simply a representation for the expression.
11517 Otherwise, a CONSTRUCTOR is returned. The CONSTRUCTOR_ELTS will be
11518 the elements of the initializer-list (or NULL_TREE, if the last
11519 production is used). The TREE_TYPE for the CONSTRUCTOR will be
11520 NULL_TREE. There is no way to detect whether or not the optional
11521 trailing `,' was provided. NON_CONSTANT_P is as for
11522 cp_parser_initializer. */
11525 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
11529 /* If it is not a `{', then we are looking at an
11530 assignment-expression. */
11531 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
11533 = cp_parser_constant_expression (parser,
11534 /*allow_non_constant_p=*/true,
11538 /* Consume the `{' token. */
11539 cp_lexer_consume_token (parser->lexer);
11540 /* Create a CONSTRUCTOR to represent the braced-initializer. */
11541 initializer = make_node (CONSTRUCTOR);
11542 /* Mark it with TREE_HAS_CONSTRUCTOR. This should not be
11543 necessary, but check_initializer depends upon it, for
11545 TREE_HAS_CONSTRUCTOR (initializer) = 1;
11546 /* If it's not a `}', then there is a non-trivial initializer. */
11547 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
11549 /* Parse the initializer list. */
11550 CONSTRUCTOR_ELTS (initializer)
11551 = cp_parser_initializer_list (parser, non_constant_p);
11552 /* A trailing `,' token is allowed. */
11553 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
11554 cp_lexer_consume_token (parser->lexer);
11556 /* Now, there should be a trailing `}'. */
11557 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
11560 return initializer;
11563 /* Parse an initializer-list.
11567 initializer-list , initializer-clause
11572 identifier : initializer-clause
11573 initializer-list, identifier : initializer-clause
11575 Returns a TREE_LIST. The TREE_VALUE of each node is an expression
11576 for the initializer. If the TREE_PURPOSE is non-NULL, it is the
11577 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
11578 as for cp_parser_initializer. */
11581 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
11583 tree initializers = NULL_TREE;
11585 /* Assume all of the expressions are constant. */
11586 *non_constant_p = false;
11588 /* Parse the rest of the list. */
11594 bool clause_non_constant_p;
11596 /* If the next token is an identifier and the following one is a
11597 colon, we are looking at the GNU designated-initializer
11599 if (cp_parser_allow_gnu_extensions_p (parser)
11600 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
11601 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
11603 /* Consume the identifier. */
11604 identifier = cp_lexer_consume_token (parser->lexer)->value;
11605 /* Consume the `:'. */
11606 cp_lexer_consume_token (parser->lexer);
11609 identifier = NULL_TREE;
11611 /* Parse the initializer. */
11612 initializer = cp_parser_initializer_clause (parser,
11613 &clause_non_constant_p);
11614 /* If any clause is non-constant, so is the entire initializer. */
11615 if (clause_non_constant_p)
11616 *non_constant_p = true;
11617 /* Add it to the list. */
11618 initializers = tree_cons (identifier, initializer, initializers);
11620 /* If the next token is not a comma, we have reached the end of
11622 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11625 /* Peek at the next token. */
11626 token = cp_lexer_peek_nth_token (parser->lexer, 2);
11627 /* If the next token is a `}', then we're still done. An
11628 initializer-clause can have a trailing `,' after the
11629 initializer-list and before the closing `}'. */
11630 if (token->type == CPP_CLOSE_BRACE)
11633 /* Consume the `,' token. */
11634 cp_lexer_consume_token (parser->lexer);
11637 /* The initializers were built up in reverse order, so we need to
11638 reverse them now. */
11639 return nreverse (initializers);
11642 /* Classes [gram.class] */
11644 /* Parse a class-name.
11650 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
11651 to indicate that names looked up in dependent types should be
11652 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
11653 keyword has been used to indicate that the name that appears next
11654 is a template. TYPE_P is true iff the next name should be treated
11655 as class-name, even if it is declared to be some other kind of name
11656 as well. If CHECK_DEPENDENCY_P is FALSE, names are looked up in
11657 dependent scopes. If CLASS_HEAD_P is TRUE, this class is the class
11658 being defined in a class-head.
11660 Returns the TYPE_DECL representing the class. */
11663 cp_parser_class_name (cp_parser *parser,
11664 bool typename_keyword_p,
11665 bool template_keyword_p,
11667 bool check_dependency_p,
11669 bool is_declaration)
11676 /* All class-names start with an identifier. */
11677 token = cp_lexer_peek_token (parser->lexer);
11678 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
11680 cp_parser_error (parser, "expected class-name");
11681 return error_mark_node;
11684 /* PARSER->SCOPE can be cleared when parsing the template-arguments
11685 to a template-id, so we save it here. */
11686 scope = parser->scope;
11687 if (scope == error_mark_node)
11688 return error_mark_node;
11690 /* Any name names a type if we're following the `typename' keyword
11691 in a qualified name where the enclosing scope is type-dependent. */
11692 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
11693 && dependent_type_p (scope));
11694 /* Handle the common case (an identifier, but not a template-id)
11696 if (token->type == CPP_NAME
11697 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
11701 /* Look for the identifier. */
11702 identifier = cp_parser_identifier (parser);
11703 /* If the next token isn't an identifier, we are certainly not
11704 looking at a class-name. */
11705 if (identifier == error_mark_node)
11706 decl = error_mark_node;
11707 /* If we know this is a type-name, there's no need to look it
11709 else if (typename_p)
11713 /* If the next token is a `::', then the name must be a type
11716 [basic.lookup.qual]
11718 During the lookup for a name preceding the :: scope
11719 resolution operator, object, function, and enumerator
11720 names are ignored. */
11721 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
11723 /* Look up the name. */
11724 decl = cp_parser_lookup_name (parser, identifier,
11726 /*is_template=*/false,
11727 /*is_namespace=*/false,
11728 check_dependency_p);
11733 /* Try a template-id. */
11734 decl = cp_parser_template_id (parser, template_keyword_p,
11735 check_dependency_p,
11737 if (decl == error_mark_node)
11738 return error_mark_node;
11741 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
11743 /* If this is a typename, create a TYPENAME_TYPE. */
11744 if (typename_p && decl != error_mark_node)
11746 decl = make_typename_type (scope, decl, /*complain=*/1);
11747 if (decl != error_mark_node)
11748 decl = TYPE_NAME (decl);
11751 /* Check to see that it is really the name of a class. */
11752 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
11753 && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
11754 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
11755 /* Situations like this:
11757 template <typename T> struct A {
11758 typename T::template X<int>::I i;
11761 are problematic. Is `T::template X<int>' a class-name? The
11762 standard does not seem to be definitive, but there is no other
11763 valid interpretation of the following `::'. Therefore, those
11764 names are considered class-names. */
11765 decl = TYPE_NAME (make_typename_type (scope, decl, tf_error));
11766 else if (decl == error_mark_node
11767 || TREE_CODE (decl) != TYPE_DECL
11768 || !IS_AGGR_TYPE (TREE_TYPE (decl)))
11770 cp_parser_error (parser, "expected class-name");
11771 return error_mark_node;
11777 /* Parse a class-specifier.
11780 class-head { member-specification [opt] }
11782 Returns the TREE_TYPE representing the class. */
11785 cp_parser_class_specifier (cp_parser* parser)
11789 tree attributes = NULL_TREE;
11790 int has_trailing_semicolon;
11791 bool nested_name_specifier_p;
11792 unsigned saved_num_template_parameter_lists;
11793 bool pop_p = false;
11795 push_deferring_access_checks (dk_no_deferred);
11797 /* Parse the class-head. */
11798 type = cp_parser_class_head (parser,
11799 &nested_name_specifier_p);
11800 /* If the class-head was a semantic disaster, skip the entire body
11804 cp_parser_skip_to_end_of_block_or_statement (parser);
11805 pop_deferring_access_checks ();
11806 return error_mark_node;
11809 /* Look for the `{'. */
11810 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
11812 pop_deferring_access_checks ();
11813 return error_mark_node;
11816 /* Issue an error message if type-definitions are forbidden here. */
11817 cp_parser_check_type_definition (parser);
11818 /* Remember that we are defining one more class. */
11819 ++parser->num_classes_being_defined;
11820 /* Inside the class, surrounding template-parameter-lists do not
11822 saved_num_template_parameter_lists
11823 = parser->num_template_parameter_lists;
11824 parser->num_template_parameter_lists = 0;
11826 /* Start the class. */
11827 if (nested_name_specifier_p)
11828 pop_p = push_scope (CP_DECL_CONTEXT (TYPE_MAIN_DECL (type)));
11829 type = begin_class_definition (type);
11830 if (type == error_mark_node)
11831 /* If the type is erroneous, skip the entire body of the class. */
11832 cp_parser_skip_to_closing_brace (parser);
11834 /* Parse the member-specification. */
11835 cp_parser_member_specification_opt (parser);
11836 /* Look for the trailing `}'. */
11837 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
11838 /* We get better error messages by noticing a common problem: a
11839 missing trailing `;'. */
11840 token = cp_lexer_peek_token (parser->lexer);
11841 has_trailing_semicolon = (token->type == CPP_SEMICOLON);
11842 /* Look for attributes to apply to this class. */
11843 if (cp_parser_allow_gnu_extensions_p (parser))
11844 attributes = cp_parser_attributes_opt (parser);
11845 /* If we got any attributes in class_head, xref_tag will stick them in
11846 TREE_TYPE of the type. Grab them now. */
11847 if (type != error_mark_node)
11849 attributes = chainon (TYPE_ATTRIBUTES (type), attributes);
11850 TYPE_ATTRIBUTES (type) = NULL_TREE;
11851 type = finish_struct (type, attributes);
11854 pop_scope (CP_DECL_CONTEXT (TYPE_MAIN_DECL (type)));
11855 /* If this class is not itself within the scope of another class,
11856 then we need to parse the bodies of all of the queued function
11857 definitions. Note that the queued functions defined in a class
11858 are not always processed immediately following the
11859 class-specifier for that class. Consider:
11862 struct B { void f() { sizeof (A); } };
11865 If `f' were processed before the processing of `A' were
11866 completed, there would be no way to compute the size of `A'.
11867 Note that the nesting we are interested in here is lexical --
11868 not the semantic nesting given by TYPE_CONTEXT. In particular,
11871 struct A { struct B; };
11872 struct A::B { void f() { } };
11874 there is no need to delay the parsing of `A::B::f'. */
11875 if (--parser->num_classes_being_defined == 0)
11880 /* In a first pass, parse default arguments to the functions.
11881 Then, in a second pass, parse the bodies of the functions.
11882 This two-phased approach handles cases like:
11890 for (TREE_PURPOSE (parser->unparsed_functions_queues)
11891 = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
11892 (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
11893 TREE_PURPOSE (parser->unparsed_functions_queues)
11894 = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
11896 fn = TREE_VALUE (queue_entry);
11897 /* Make sure that any template parameters are in scope. */
11898 maybe_begin_member_template_processing (fn);
11899 /* If there are default arguments that have not yet been processed,
11900 take care of them now. */
11901 cp_parser_late_parsing_default_args (parser, fn);
11902 /* Remove any template parameters from the symbol table. */
11903 maybe_end_member_template_processing ();
11905 /* Now parse the body of the functions. */
11906 for (TREE_VALUE (parser->unparsed_functions_queues)
11907 = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
11908 (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
11909 TREE_VALUE (parser->unparsed_functions_queues)
11910 = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
11912 /* Figure out which function we need to process. */
11913 fn = TREE_VALUE (queue_entry);
11915 /* A hack to prevent garbage collection. */
11918 /* Parse the function. */
11919 cp_parser_late_parsing_for_member (parser, fn);
11925 /* Put back any saved access checks. */
11926 pop_deferring_access_checks ();
11928 /* Restore the count of active template-parameter-lists. */
11929 parser->num_template_parameter_lists
11930 = saved_num_template_parameter_lists;
11935 /* Parse a class-head.
11938 class-key identifier [opt] base-clause [opt]
11939 class-key nested-name-specifier identifier base-clause [opt]
11940 class-key nested-name-specifier [opt] template-id
11944 class-key attributes identifier [opt] base-clause [opt]
11945 class-key attributes nested-name-specifier identifier base-clause [opt]
11946 class-key attributes nested-name-specifier [opt] template-id
11949 Returns the TYPE of the indicated class. Sets
11950 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
11951 involving a nested-name-specifier was used, and FALSE otherwise.
11953 Returns NULL_TREE if the class-head is syntactically valid, but
11954 semantically invalid in a way that means we should skip the entire
11955 body of the class. */
11958 cp_parser_class_head (cp_parser* parser,
11959 bool* nested_name_specifier_p)
11962 tree nested_name_specifier;
11963 enum tag_types class_key;
11964 tree id = NULL_TREE;
11965 tree type = NULL_TREE;
11967 bool template_id_p = false;
11968 bool qualified_p = false;
11969 bool invalid_nested_name_p = false;
11970 bool invalid_explicit_specialization_p = false;
11971 bool pop_p = false;
11972 unsigned num_templates;
11974 /* Assume no nested-name-specifier will be present. */
11975 *nested_name_specifier_p = false;
11976 /* Assume no template parameter lists will be used in defining the
11980 /* Look for the class-key. */
11981 class_key = cp_parser_class_key (parser);
11982 if (class_key == none_type)
11983 return error_mark_node;
11985 /* Parse the attributes. */
11986 attributes = cp_parser_attributes_opt (parser);
11988 /* If the next token is `::', that is invalid -- but sometimes
11989 people do try to write:
11993 Handle this gracefully by accepting the extra qualifier, and then
11994 issuing an error about it later if this really is a
11995 class-head. If it turns out just to be an elaborated type
11996 specifier, remain silent. */
11997 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
11998 qualified_p = true;
12000 push_deferring_access_checks (dk_no_check);
12002 /* Determine the name of the class. Begin by looking for an
12003 optional nested-name-specifier. */
12004 nested_name_specifier
12005 = cp_parser_nested_name_specifier_opt (parser,
12006 /*typename_keyword_p=*/false,
12007 /*check_dependency_p=*/false,
12009 /*is_declaration=*/false);
12010 /* If there was a nested-name-specifier, then there *must* be an
12012 if (nested_name_specifier)
12014 /* Although the grammar says `identifier', it really means
12015 `class-name' or `template-name'. You are only allowed to
12016 define a class that has already been declared with this
12019 The proposed resolution for Core Issue 180 says that whever
12020 you see `class T::X' you should treat `X' as a type-name.
12022 It is OK to define an inaccessible class; for example:
12024 class A { class B; };
12027 We do not know if we will see a class-name, or a
12028 template-name. We look for a class-name first, in case the
12029 class-name is a template-id; if we looked for the
12030 template-name first we would stop after the template-name. */
12031 cp_parser_parse_tentatively (parser);
12032 type = cp_parser_class_name (parser,
12033 /*typename_keyword_p=*/false,
12034 /*template_keyword_p=*/false,
12036 /*check_dependency_p=*/false,
12037 /*class_head_p=*/true,
12038 /*is_declaration=*/false);
12039 /* If that didn't work, ignore the nested-name-specifier. */
12040 if (!cp_parser_parse_definitely (parser))
12042 invalid_nested_name_p = true;
12043 id = cp_parser_identifier (parser);
12044 if (id == error_mark_node)
12047 /* If we could not find a corresponding TYPE, treat this
12048 declaration like an unqualified declaration. */
12049 if (type == error_mark_node)
12050 nested_name_specifier = NULL_TREE;
12051 /* Otherwise, count the number of templates used in TYPE and its
12052 containing scopes. */
12057 for (scope = TREE_TYPE (type);
12058 scope && TREE_CODE (scope) != NAMESPACE_DECL;
12059 scope = (TYPE_P (scope)
12060 ? TYPE_CONTEXT (scope)
12061 : DECL_CONTEXT (scope)))
12063 && CLASS_TYPE_P (scope)
12064 && CLASSTYPE_TEMPLATE_INFO (scope)
12065 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
12066 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
12070 /* Otherwise, the identifier is optional. */
12073 /* We don't know whether what comes next is a template-id,
12074 an identifier, or nothing at all. */
12075 cp_parser_parse_tentatively (parser);
12076 /* Check for a template-id. */
12077 id = cp_parser_template_id (parser,
12078 /*template_keyword_p=*/false,
12079 /*check_dependency_p=*/true,
12080 /*is_declaration=*/true);
12081 /* If that didn't work, it could still be an identifier. */
12082 if (!cp_parser_parse_definitely (parser))
12084 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12085 id = cp_parser_identifier (parser);
12091 template_id_p = true;
12096 pop_deferring_access_checks ();
12098 cp_parser_check_for_invalid_template_id (parser, id);
12100 /* If it's not a `:' or a `{' then we can't really be looking at a
12101 class-head, since a class-head only appears as part of a
12102 class-specifier. We have to detect this situation before calling
12103 xref_tag, since that has irreversible side-effects. */
12104 if (!cp_parser_next_token_starts_class_definition_p (parser))
12106 cp_parser_error (parser, "expected `{' or `:'");
12107 return error_mark_node;
12110 /* At this point, we're going ahead with the class-specifier, even
12111 if some other problem occurs. */
12112 cp_parser_commit_to_tentative_parse (parser);
12113 /* Issue the error about the overly-qualified name now. */
12115 cp_parser_error (parser,
12116 "global qualification of class name is invalid");
12117 else if (invalid_nested_name_p)
12118 cp_parser_error (parser,
12119 "qualified name does not name a class");
12120 else if (nested_name_specifier)
12123 /* Figure out in what scope the declaration is being placed. */
12124 scope = current_scope ();
12126 scope = current_namespace;
12127 /* If that scope does not contain the scope in which the
12128 class was originally declared, the program is invalid. */
12129 if (scope && !is_ancestor (scope, nested_name_specifier))
12131 error ("declaration of `%D' in `%D' which does not "
12132 "enclose `%D'", type, scope, nested_name_specifier);
12138 A declarator-id shall not be qualified exception of the
12139 definition of a ... nested class outside of its class
12140 ... [or] a the definition or explicit instantiation of a
12141 class member of a namespace outside of its namespace. */
12142 if (scope == nested_name_specifier)
12144 pedwarn ("extra qualification ignored");
12145 nested_name_specifier = NULL_TREE;
12149 /* An explicit-specialization must be preceded by "template <>". If
12150 it is not, try to recover gracefully. */
12151 if (at_namespace_scope_p ()
12152 && parser->num_template_parameter_lists == 0
12155 error ("an explicit specialization must be preceded by 'template <>'");
12156 invalid_explicit_specialization_p = true;
12157 /* Take the same action that would have been taken by
12158 cp_parser_explicit_specialization. */
12159 ++parser->num_template_parameter_lists;
12160 begin_specialization ();
12162 /* There must be no "return" statements between this point and the
12163 end of this function; set "type "to the correct return value and
12164 use "goto done;" to return. */
12165 /* Make sure that the right number of template parameters were
12167 if (!cp_parser_check_template_parameters (parser, num_templates))
12169 /* If something went wrong, there is no point in even trying to
12170 process the class-definition. */
12175 /* Look up the type. */
12178 type = TREE_TYPE (id);
12179 maybe_process_partial_specialization (type);
12181 else if (!nested_name_specifier)
12183 /* If the class was unnamed, create a dummy name. */
12185 id = make_anon_name ();
12186 type = xref_tag (class_key, id, attributes, /*globalize=*/false,
12187 parser->num_template_parameter_lists);
12192 bool pop_p = false;
12196 template <typename T> struct S { struct T };
12197 template <typename T> struct S<T>::T { };
12199 we will get a TYPENAME_TYPE when processing the definition of
12200 `S::T'. We need to resolve it to the actual type before we
12201 try to define it. */
12202 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
12204 class_type = resolve_typename_type (TREE_TYPE (type),
12205 /*only_current_p=*/false);
12206 if (class_type != error_mark_node)
12207 type = TYPE_NAME (class_type);
12210 cp_parser_error (parser, "could not resolve typename type");
12211 type = error_mark_node;
12215 maybe_process_partial_specialization (TREE_TYPE (type));
12216 class_type = current_class_type;
12217 /* Enter the scope indicated by the nested-name-specifier. */
12218 if (nested_name_specifier)
12219 pop_p = push_scope (nested_name_specifier);
12220 /* Get the canonical version of this type. */
12221 type = TYPE_MAIN_DECL (TREE_TYPE (type));
12222 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
12223 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
12224 type = push_template_decl (type);
12225 type = TREE_TYPE (type);
12226 if (nested_name_specifier)
12228 *nested_name_specifier_p = true;
12230 pop_scope (nested_name_specifier);
12233 /* Indicate whether this class was declared as a `class' or as a
12235 if (TREE_CODE (type) == RECORD_TYPE)
12236 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
12237 cp_parser_check_class_key (class_key, type);
12239 /* Enter the scope containing the class; the names of base classes
12240 should be looked up in that context. For example, given:
12242 struct A { struct B {}; struct C; };
12243 struct A::C : B {};
12246 if (nested_name_specifier)
12247 pop_p = push_scope (nested_name_specifier);
12248 /* Now, look for the base-clause. */
12249 token = cp_lexer_peek_token (parser->lexer);
12250 if (token->type == CPP_COLON)
12254 /* Get the list of base-classes. */
12255 bases = cp_parser_base_clause (parser);
12256 /* Process them. */
12257 xref_basetypes (type, bases);
12259 /* Leave the scope given by the nested-name-specifier. We will
12260 enter the class scope itself while processing the members. */
12262 pop_scope (nested_name_specifier);
12265 if (invalid_explicit_specialization_p)
12267 end_specialization ();
12268 --parser->num_template_parameter_lists;
12273 /* Parse a class-key.
12280 Returns the kind of class-key specified, or none_type to indicate
12283 static enum tag_types
12284 cp_parser_class_key (cp_parser* parser)
12287 enum tag_types tag_type;
12289 /* Look for the class-key. */
12290 token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
12294 /* Check to see if the TOKEN is a class-key. */
12295 tag_type = cp_parser_token_is_class_key (token);
12297 cp_parser_error (parser, "expected class-key");
12301 /* Parse an (optional) member-specification.
12303 member-specification:
12304 member-declaration member-specification [opt]
12305 access-specifier : member-specification [opt] */
12308 cp_parser_member_specification_opt (cp_parser* parser)
12315 /* Peek at the next token. */
12316 token = cp_lexer_peek_token (parser->lexer);
12317 /* If it's a `}', or EOF then we've seen all the members. */
12318 if (token->type == CPP_CLOSE_BRACE || token->type == CPP_EOF)
12321 /* See if this token is a keyword. */
12322 keyword = token->keyword;
12326 case RID_PROTECTED:
12328 /* Consume the access-specifier. */
12329 cp_lexer_consume_token (parser->lexer);
12330 /* Remember which access-specifier is active. */
12331 current_access_specifier = token->value;
12332 /* Look for the `:'. */
12333 cp_parser_require (parser, CPP_COLON, "`:'");
12337 /* Otherwise, the next construction must be a
12338 member-declaration. */
12339 cp_parser_member_declaration (parser);
12344 /* Parse a member-declaration.
12346 member-declaration:
12347 decl-specifier-seq [opt] member-declarator-list [opt] ;
12348 function-definition ; [opt]
12349 :: [opt] nested-name-specifier template [opt] unqualified-id ;
12351 template-declaration
12353 member-declarator-list:
12355 member-declarator-list , member-declarator
12358 declarator pure-specifier [opt]
12359 declarator constant-initializer [opt]
12360 identifier [opt] : constant-expression
12364 member-declaration:
12365 __extension__ member-declaration
12368 declarator attributes [opt] pure-specifier [opt]
12369 declarator attributes [opt] constant-initializer [opt]
12370 identifier [opt] attributes [opt] : constant-expression */
12373 cp_parser_member_declaration (cp_parser* parser)
12375 tree decl_specifiers;
12376 tree prefix_attributes;
12378 int declares_class_or_enum;
12381 int saved_pedantic;
12383 /* Check for the `__extension__' keyword. */
12384 if (cp_parser_extension_opt (parser, &saved_pedantic))
12387 cp_parser_member_declaration (parser);
12388 /* Restore the old value of the PEDANTIC flag. */
12389 pedantic = saved_pedantic;
12394 /* Check for a template-declaration. */
12395 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
12397 /* Parse the template-declaration. */
12398 cp_parser_template_declaration (parser, /*member_p=*/true);
12403 /* Check for a using-declaration. */
12404 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
12406 /* Parse the using-declaration. */
12407 cp_parser_using_declaration (parser);
12412 /* Parse the decl-specifier-seq. */
12414 = cp_parser_decl_specifier_seq (parser,
12415 CP_PARSER_FLAGS_OPTIONAL,
12416 &prefix_attributes,
12417 &declares_class_or_enum);
12418 /* Check for an invalid type-name. */
12419 if (cp_parser_parse_and_diagnose_invalid_type_name (parser))
12421 /* If there is no declarator, then the decl-specifier-seq should
12423 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12425 /* If there was no decl-specifier-seq, and the next token is a
12426 `;', then we have something like:
12432 Each member-declaration shall declare at least one member
12433 name of the class. */
12434 if (!decl_specifiers)
12437 pedwarn ("extra semicolon");
12443 /* See if this declaration is a friend. */
12444 friend_p = cp_parser_friend_p (decl_specifiers);
12445 /* If there were decl-specifiers, check to see if there was
12446 a class-declaration. */
12447 type = check_tag_decl (decl_specifiers);
12448 /* Nested classes have already been added to the class, but
12449 a `friend' needs to be explicitly registered. */
12452 /* If the `friend' keyword was present, the friend must
12453 be introduced with a class-key. */
12454 if (!declares_class_or_enum)
12455 error ("a class-key must be used when declaring a friend");
12458 template <typename T> struct A {
12459 friend struct A<T>::B;
12462 A<T>::B will be represented by a TYPENAME_TYPE, and
12463 therefore not recognized by check_tag_decl. */
12468 for (specifier = decl_specifiers;
12470 specifier = TREE_CHAIN (specifier))
12472 tree s = TREE_VALUE (specifier);
12474 if (TREE_CODE (s) == IDENTIFIER_NODE)
12475 get_global_value_if_present (s, &type);
12476 if (TREE_CODE (s) == TYPE_DECL)
12485 if (!type || !TYPE_P (type))
12486 error ("friend declaration does not name a class or "
12489 make_friend_class (current_class_type, type,
12490 /*complain=*/true);
12492 /* If there is no TYPE, an error message will already have
12496 /* An anonymous aggregate has to be handled specially; such
12497 a declaration really declares a data member (with a
12498 particular type), as opposed to a nested class. */
12499 else if (ANON_AGGR_TYPE_P (type))
12501 /* Remove constructors and such from TYPE, now that we
12502 know it is an anonymous aggregate. */
12503 fixup_anonymous_aggr (type);
12504 /* And make the corresponding data member. */
12505 decl = build_decl (FIELD_DECL, NULL_TREE, type);
12506 /* Add it to the class. */
12507 finish_member_declaration (decl);
12510 cp_parser_check_access_in_redeclaration (TYPE_NAME (type));
12515 /* See if these declarations will be friends. */
12516 friend_p = cp_parser_friend_p (decl_specifiers);
12518 /* Keep going until we hit the `;' at the end of the
12520 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12522 tree attributes = NULL_TREE;
12523 tree first_attribute;
12525 /* Peek at the next token. */
12526 token = cp_lexer_peek_token (parser->lexer);
12528 /* Check for a bitfield declaration. */
12529 if (token->type == CPP_COLON
12530 || (token->type == CPP_NAME
12531 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
12537 /* Get the name of the bitfield. Note that we cannot just
12538 check TOKEN here because it may have been invalidated by
12539 the call to cp_lexer_peek_nth_token above. */
12540 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
12541 identifier = cp_parser_identifier (parser);
12543 identifier = NULL_TREE;
12545 /* Consume the `:' token. */
12546 cp_lexer_consume_token (parser->lexer);
12547 /* Get the width of the bitfield. */
12549 = cp_parser_constant_expression (parser,
12550 /*allow_non_constant=*/false,
12553 /* Look for attributes that apply to the bitfield. */
12554 attributes = cp_parser_attributes_opt (parser);
12555 /* Remember which attributes are prefix attributes and
12557 first_attribute = attributes;
12558 /* Combine the attributes. */
12559 attributes = chainon (prefix_attributes, attributes);
12561 /* Create the bitfield declaration. */
12562 decl = grokbitfield (identifier,
12565 /* Apply the attributes. */
12566 cplus_decl_attributes (&decl, attributes, /*flags=*/0);
12572 tree asm_specification;
12573 int ctor_dtor_or_conv_p;
12575 /* Parse the declarator. */
12577 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
12578 &ctor_dtor_or_conv_p,
12579 /*parenthesized_p=*/NULL);
12581 /* If something went wrong parsing the declarator, make sure
12582 that we at least consume some tokens. */
12583 if (declarator == error_mark_node)
12585 /* Skip to the end of the statement. */
12586 cp_parser_skip_to_end_of_statement (parser);
12587 /* If the next token is not a semicolon, that is
12588 probably because we just skipped over the body of
12589 a function. So, we consume a semicolon if
12590 present, but do not issue an error message if it
12592 if (cp_lexer_next_token_is (parser->lexer,
12594 cp_lexer_consume_token (parser->lexer);
12598 cp_parser_check_for_definition_in_return_type
12599 (declarator, declares_class_or_enum);
12601 /* Look for an asm-specification. */
12602 asm_specification = cp_parser_asm_specification_opt (parser);
12603 /* Look for attributes that apply to the declaration. */
12604 attributes = cp_parser_attributes_opt (parser);
12605 /* Remember which attributes are prefix attributes and
12607 first_attribute = attributes;
12608 /* Combine the attributes. */
12609 attributes = chainon (prefix_attributes, attributes);
12611 /* If it's an `=', then we have a constant-initializer or a
12612 pure-specifier. It is not correct to parse the
12613 initializer before registering the member declaration
12614 since the member declaration should be in scope while
12615 its initializer is processed. However, the rest of the
12616 front end does not yet provide an interface that allows
12617 us to handle this correctly. */
12618 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12622 A pure-specifier shall be used only in the declaration of
12623 a virtual function.
12625 A member-declarator can contain a constant-initializer
12626 only if it declares a static member of integral or
12629 Therefore, if the DECLARATOR is for a function, we look
12630 for a pure-specifier; otherwise, we look for a
12631 constant-initializer. When we call `grokfield', it will
12632 perform more stringent semantics checks. */
12633 if (TREE_CODE (declarator) == CALL_EXPR)
12634 initializer = cp_parser_pure_specifier (parser);
12636 /* Parse the initializer. */
12637 initializer = cp_parser_constant_initializer (parser);
12639 /* Otherwise, there is no initializer. */
12641 initializer = NULL_TREE;
12643 /* See if we are probably looking at a function
12644 definition. We are certainly not looking at at a
12645 member-declarator. Calling `grokfield' has
12646 side-effects, so we must not do it unless we are sure
12647 that we are looking at a member-declarator. */
12648 if (cp_parser_token_starts_function_definition_p
12649 (cp_lexer_peek_token (parser->lexer)))
12651 /* The grammar does not allow a pure-specifier to be
12652 used when a member function is defined. (It is
12653 possible that this fact is an oversight in the
12654 standard, since a pure function may be defined
12655 outside of the class-specifier. */
12657 error ("pure-specifier on function-definition");
12658 decl = cp_parser_save_member_function_body (parser,
12662 /* If the member was not a friend, declare it here. */
12664 finish_member_declaration (decl);
12665 /* Peek at the next token. */
12666 token = cp_lexer_peek_token (parser->lexer);
12667 /* If the next token is a semicolon, consume it. */
12668 if (token->type == CPP_SEMICOLON)
12669 cp_lexer_consume_token (parser->lexer);
12674 /* Create the declaration. */
12675 decl = grokfield (declarator, decl_specifiers,
12676 initializer, asm_specification,
12678 /* Any initialization must have been from a
12679 constant-expression. */
12680 if (decl && TREE_CODE (decl) == VAR_DECL && initializer)
12681 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = 1;
12685 /* Reset PREFIX_ATTRIBUTES. */
12686 while (attributes && TREE_CHAIN (attributes) != first_attribute)
12687 attributes = TREE_CHAIN (attributes);
12689 TREE_CHAIN (attributes) = NULL_TREE;
12691 /* If there is any qualification still in effect, clear it
12692 now; we will be starting fresh with the next declarator. */
12693 parser->scope = NULL_TREE;
12694 parser->qualifying_scope = NULL_TREE;
12695 parser->object_scope = NULL_TREE;
12696 /* If it's a `,', then there are more declarators. */
12697 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
12698 cp_lexer_consume_token (parser->lexer);
12699 /* If the next token isn't a `;', then we have a parse error. */
12700 else if (cp_lexer_next_token_is_not (parser->lexer,
12703 cp_parser_error (parser, "expected `;'");
12704 /* Skip tokens until we find a `;'. */
12705 cp_parser_skip_to_end_of_statement (parser);
12712 /* Add DECL to the list of members. */
12714 finish_member_declaration (decl);
12716 if (TREE_CODE (decl) == FUNCTION_DECL)
12717 cp_parser_save_default_args (parser, decl);
12722 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
12725 /* Parse a pure-specifier.
12730 Returns INTEGER_ZERO_NODE if a pure specifier is found.
12731 Otherwise, ERROR_MARK_NODE is returned. */
12734 cp_parser_pure_specifier (cp_parser* parser)
12738 /* Look for the `=' token. */
12739 if (!cp_parser_require (parser, CPP_EQ, "`='"))
12740 return error_mark_node;
12741 /* Look for the `0' token. */
12742 token = cp_parser_require (parser, CPP_NUMBER, "`0'");
12743 /* Unfortunately, this will accept `0L' and `0x00' as well. We need
12744 to get information from the lexer about how the number was
12745 spelled in order to fix this problem. */
12746 if (!token || !integer_zerop (token->value))
12747 return error_mark_node;
12749 return integer_zero_node;
12752 /* Parse a constant-initializer.
12754 constant-initializer:
12755 = constant-expression
12757 Returns a representation of the constant-expression. */
12760 cp_parser_constant_initializer (cp_parser* parser)
12762 /* Look for the `=' token. */
12763 if (!cp_parser_require (parser, CPP_EQ, "`='"))
12764 return error_mark_node;
12766 /* It is invalid to write:
12768 struct S { static const int i = { 7 }; };
12771 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12773 cp_parser_error (parser,
12774 "a brace-enclosed initializer is not allowed here");
12775 /* Consume the opening brace. */
12776 cp_lexer_consume_token (parser->lexer);
12777 /* Skip the initializer. */
12778 cp_parser_skip_to_closing_brace (parser);
12779 /* Look for the trailing `}'. */
12780 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
12782 return error_mark_node;
12785 return cp_parser_constant_expression (parser,
12786 /*allow_non_constant=*/false,
12790 /* Derived classes [gram.class.derived] */
12792 /* Parse a base-clause.
12795 : base-specifier-list
12797 base-specifier-list:
12799 base-specifier-list , base-specifier
12801 Returns a TREE_LIST representing the base-classes, in the order in
12802 which they were declared. The representation of each node is as
12803 described by cp_parser_base_specifier.
12805 In the case that no bases are specified, this function will return
12806 NULL_TREE, not ERROR_MARK_NODE. */
12809 cp_parser_base_clause (cp_parser* parser)
12811 tree bases = NULL_TREE;
12813 /* Look for the `:' that begins the list. */
12814 cp_parser_require (parser, CPP_COLON, "`:'");
12816 /* Scan the base-specifier-list. */
12822 /* Look for the base-specifier. */
12823 base = cp_parser_base_specifier (parser);
12824 /* Add BASE to the front of the list. */
12825 if (base != error_mark_node)
12827 TREE_CHAIN (base) = bases;
12830 /* Peek at the next token. */
12831 token = cp_lexer_peek_token (parser->lexer);
12832 /* If it's not a comma, then the list is complete. */
12833 if (token->type != CPP_COMMA)
12835 /* Consume the `,'. */
12836 cp_lexer_consume_token (parser->lexer);
12839 /* PARSER->SCOPE may still be non-NULL at this point, if the last
12840 base class had a qualified name. However, the next name that
12841 appears is certainly not qualified. */
12842 parser->scope = NULL_TREE;
12843 parser->qualifying_scope = NULL_TREE;
12844 parser->object_scope = NULL_TREE;
12846 return nreverse (bases);
12849 /* Parse a base-specifier.
12852 :: [opt] nested-name-specifier [opt] class-name
12853 virtual access-specifier [opt] :: [opt] nested-name-specifier
12855 access-specifier virtual [opt] :: [opt] nested-name-specifier
12858 Returns a TREE_LIST. The TREE_PURPOSE will be one of
12859 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
12860 indicate the specifiers provided. The TREE_VALUE will be a TYPE
12861 (or the ERROR_MARK_NODE) indicating the type that was specified. */
12864 cp_parser_base_specifier (cp_parser* parser)
12868 bool virtual_p = false;
12869 bool duplicate_virtual_error_issued_p = false;
12870 bool duplicate_access_error_issued_p = false;
12871 bool class_scope_p, template_p;
12872 tree access = access_default_node;
12875 /* Process the optional `virtual' and `access-specifier'. */
12878 /* Peek at the next token. */
12879 token = cp_lexer_peek_token (parser->lexer);
12880 /* Process `virtual'. */
12881 switch (token->keyword)
12884 /* If `virtual' appears more than once, issue an error. */
12885 if (virtual_p && !duplicate_virtual_error_issued_p)
12887 cp_parser_error (parser,
12888 "`virtual' specified more than once in base-specified");
12889 duplicate_virtual_error_issued_p = true;
12894 /* Consume the `virtual' token. */
12895 cp_lexer_consume_token (parser->lexer);
12900 case RID_PROTECTED:
12902 /* If more than one access specifier appears, issue an
12904 if (access != access_default_node
12905 && !duplicate_access_error_issued_p)
12907 cp_parser_error (parser,
12908 "more than one access specifier in base-specified");
12909 duplicate_access_error_issued_p = true;
12912 access = ridpointers[(int) token->keyword];
12914 /* Consume the access-specifier. */
12915 cp_lexer_consume_token (parser->lexer);
12924 /* It is not uncommon to see programs mechanically, erroneously, use
12925 the 'typename' keyword to denote (dependent) qualified types
12926 as base classes. */
12927 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
12929 if (!processing_template_decl)
12930 error ("keyword `typename' not allowed outside of templates");
12932 error ("keyword `typename' not allowed in this context "
12933 "(the base class is implicitly a type)");
12934 cp_lexer_consume_token (parser->lexer);
12937 /* Look for the optional `::' operator. */
12938 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
12939 /* Look for the nested-name-specifier. The simplest way to
12944 The keyword `typename' is not permitted in a base-specifier or
12945 mem-initializer; in these contexts a qualified name that
12946 depends on a template-parameter is implicitly assumed to be a
12949 is to pretend that we have seen the `typename' keyword at this
12951 cp_parser_nested_name_specifier_opt (parser,
12952 /*typename_keyword_p=*/true,
12953 /*check_dependency_p=*/true,
12955 /*is_declaration=*/true);
12956 /* If the base class is given by a qualified name, assume that names
12957 we see are type names or templates, as appropriate. */
12958 class_scope_p = (parser->scope && TYPE_P (parser->scope));
12959 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
12961 /* Finally, look for the class-name. */
12962 type = cp_parser_class_name (parser,
12966 /*check_dependency_p=*/true,
12967 /*class_head_p=*/false,
12968 /*is_declaration=*/true);
12970 if (type == error_mark_node)
12971 return error_mark_node;
12973 return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
12976 /* Exception handling [gram.exception] */
12978 /* Parse an (optional) exception-specification.
12980 exception-specification:
12981 throw ( type-id-list [opt] )
12983 Returns a TREE_LIST representing the exception-specification. The
12984 TREE_VALUE of each node is a type. */
12987 cp_parser_exception_specification_opt (cp_parser* parser)
12992 /* Peek at the next token. */
12993 token = cp_lexer_peek_token (parser->lexer);
12994 /* If it's not `throw', then there's no exception-specification. */
12995 if (!cp_parser_is_keyword (token, RID_THROW))
12998 /* Consume the `throw'. */
12999 cp_lexer_consume_token (parser->lexer);
13001 /* Look for the `('. */
13002 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13004 /* Peek at the next token. */
13005 token = cp_lexer_peek_token (parser->lexer);
13006 /* If it's not a `)', then there is a type-id-list. */
13007 if (token->type != CPP_CLOSE_PAREN)
13009 const char *saved_message;
13011 /* Types may not be defined in an exception-specification. */
13012 saved_message = parser->type_definition_forbidden_message;
13013 parser->type_definition_forbidden_message
13014 = "types may not be defined in an exception-specification";
13015 /* Parse the type-id-list. */
13016 type_id_list = cp_parser_type_id_list (parser);
13017 /* Restore the saved message. */
13018 parser->type_definition_forbidden_message = saved_message;
13021 type_id_list = empty_except_spec;
13023 /* Look for the `)'. */
13024 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13026 return type_id_list;
13029 /* Parse an (optional) type-id-list.
13033 type-id-list , type-id
13035 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
13036 in the order that the types were presented. */
13039 cp_parser_type_id_list (cp_parser* parser)
13041 tree types = NULL_TREE;
13048 /* Get the next type-id. */
13049 type = cp_parser_type_id (parser);
13050 /* Add it to the list. */
13051 types = add_exception_specifier (types, type, /*complain=*/1);
13052 /* Peek at the next token. */
13053 token = cp_lexer_peek_token (parser->lexer);
13054 /* If it is not a `,', we are done. */
13055 if (token->type != CPP_COMMA)
13057 /* Consume the `,'. */
13058 cp_lexer_consume_token (parser->lexer);
13061 return nreverse (types);
13064 /* Parse a try-block.
13067 try compound-statement handler-seq */
13070 cp_parser_try_block (cp_parser* parser)
13074 cp_parser_require_keyword (parser, RID_TRY, "`try'");
13075 try_block = begin_try_block ();
13076 cp_parser_compound_statement (parser, false);
13077 finish_try_block (try_block);
13078 cp_parser_handler_seq (parser);
13079 finish_handler_sequence (try_block);
13084 /* Parse a function-try-block.
13086 function-try-block:
13087 try ctor-initializer [opt] function-body handler-seq */
13090 cp_parser_function_try_block (cp_parser* parser)
13093 bool ctor_initializer_p;
13095 /* Look for the `try' keyword. */
13096 if (!cp_parser_require_keyword (parser, RID_TRY, "`try'"))
13098 /* Let the rest of the front-end know where we are. */
13099 try_block = begin_function_try_block ();
13100 /* Parse the function-body. */
13102 = cp_parser_ctor_initializer_opt_and_function_body (parser);
13103 /* We're done with the `try' part. */
13104 finish_function_try_block (try_block);
13105 /* Parse the handlers. */
13106 cp_parser_handler_seq (parser);
13107 /* We're done with the handlers. */
13108 finish_function_handler_sequence (try_block);
13110 return ctor_initializer_p;
13113 /* Parse a handler-seq.
13116 handler handler-seq [opt] */
13119 cp_parser_handler_seq (cp_parser* parser)
13125 /* Parse the handler. */
13126 cp_parser_handler (parser);
13127 /* Peek at the next token. */
13128 token = cp_lexer_peek_token (parser->lexer);
13129 /* If it's not `catch' then there are no more handlers. */
13130 if (!cp_parser_is_keyword (token, RID_CATCH))
13135 /* Parse a handler.
13138 catch ( exception-declaration ) compound-statement */
13141 cp_parser_handler (cp_parser* parser)
13146 cp_parser_require_keyword (parser, RID_CATCH, "`catch'");
13147 handler = begin_handler ();
13148 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13149 declaration = cp_parser_exception_declaration (parser);
13150 finish_handler_parms (declaration, handler);
13151 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13152 cp_parser_compound_statement (parser, false);
13153 finish_handler (handler);
13156 /* Parse an exception-declaration.
13158 exception-declaration:
13159 type-specifier-seq declarator
13160 type-specifier-seq abstract-declarator
13164 Returns a VAR_DECL for the declaration, or NULL_TREE if the
13165 ellipsis variant is used. */
13168 cp_parser_exception_declaration (cp_parser* parser)
13170 tree type_specifiers;
13172 const char *saved_message;
13174 /* If it's an ellipsis, it's easy to handle. */
13175 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13177 /* Consume the `...' token. */
13178 cp_lexer_consume_token (parser->lexer);
13182 /* Types may not be defined in exception-declarations. */
13183 saved_message = parser->type_definition_forbidden_message;
13184 parser->type_definition_forbidden_message
13185 = "types may not be defined in exception-declarations";
13187 /* Parse the type-specifier-seq. */
13188 type_specifiers = cp_parser_type_specifier_seq (parser);
13189 /* If it's a `)', then there is no declarator. */
13190 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
13191 declarator = NULL_TREE;
13193 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
13194 /*ctor_dtor_or_conv_p=*/NULL,
13195 /*parenthesized_p=*/NULL);
13197 /* Restore the saved message. */
13198 parser->type_definition_forbidden_message = saved_message;
13200 return start_handler_parms (type_specifiers, declarator);
13203 /* Parse a throw-expression.
13206 throw assignment-expression [opt]
13208 Returns a THROW_EXPR representing the throw-expression. */
13211 cp_parser_throw_expression (cp_parser* parser)
13216 cp_parser_require_keyword (parser, RID_THROW, "`throw'");
13217 token = cp_lexer_peek_token (parser->lexer);
13218 /* Figure out whether or not there is an assignment-expression
13219 following the "throw" keyword. */
13220 if (token->type == CPP_COMMA
13221 || token->type == CPP_SEMICOLON
13222 || token->type == CPP_CLOSE_PAREN
13223 || token->type == CPP_CLOSE_SQUARE
13224 || token->type == CPP_CLOSE_BRACE
13225 || token->type == CPP_COLON)
13226 expression = NULL_TREE;
13228 expression = cp_parser_assignment_expression (parser);
13230 return build_throw (expression);
13233 /* GNU Extensions */
13235 /* Parse an (optional) asm-specification.
13238 asm ( string-literal )
13240 If the asm-specification is present, returns a STRING_CST
13241 corresponding to the string-literal. Otherwise, returns
13245 cp_parser_asm_specification_opt (cp_parser* parser)
13248 tree asm_specification;
13250 /* Peek at the next token. */
13251 token = cp_lexer_peek_token (parser->lexer);
13252 /* If the next token isn't the `asm' keyword, then there's no
13253 asm-specification. */
13254 if (!cp_parser_is_keyword (token, RID_ASM))
13257 /* Consume the `asm' token. */
13258 cp_lexer_consume_token (parser->lexer);
13259 /* Look for the `('. */
13260 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13262 /* Look for the string-literal. */
13263 token = cp_parser_require (parser, CPP_STRING, "string-literal");
13265 asm_specification = token->value;
13267 asm_specification = NULL_TREE;
13269 /* Look for the `)'. */
13270 cp_parser_require (parser, CPP_CLOSE_PAREN, "`('");
13272 return asm_specification;
13275 /* Parse an asm-operand-list.
13279 asm-operand-list , asm-operand
13282 string-literal ( expression )
13283 [ string-literal ] string-literal ( expression )
13285 Returns a TREE_LIST representing the operands. The TREE_VALUE of
13286 each node is the expression. The TREE_PURPOSE is itself a
13287 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
13288 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
13289 is a STRING_CST for the string literal before the parenthesis. */
13292 cp_parser_asm_operand_list (cp_parser* parser)
13294 tree asm_operands = NULL_TREE;
13298 tree string_literal;
13303 c_lex_string_translate = false;
13305 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
13307 /* Consume the `[' token. */
13308 cp_lexer_consume_token (parser->lexer);
13309 /* Read the operand name. */
13310 name = cp_parser_identifier (parser);
13311 if (name != error_mark_node)
13312 name = build_string (IDENTIFIER_LENGTH (name),
13313 IDENTIFIER_POINTER (name));
13314 /* Look for the closing `]'. */
13315 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
13319 /* Look for the string-literal. */
13320 token = cp_parser_require (parser, CPP_STRING, "string-literal");
13321 string_literal = token ? token->value : error_mark_node;
13322 c_lex_string_translate = true;
13323 /* Look for the `('. */
13324 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13325 /* Parse the expression. */
13326 expression = cp_parser_expression (parser);
13327 /* Look for the `)'. */
13328 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13329 c_lex_string_translate = false;
13330 /* Add this operand to the list. */
13331 asm_operands = tree_cons (build_tree_list (name, string_literal),
13334 /* If the next token is not a `,', there are no more
13336 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13338 /* Consume the `,'. */
13339 cp_lexer_consume_token (parser->lexer);
13342 return nreverse (asm_operands);
13345 /* Parse an asm-clobber-list.
13349 asm-clobber-list , string-literal
13351 Returns a TREE_LIST, indicating the clobbers in the order that they
13352 appeared. The TREE_VALUE of each node is a STRING_CST. */
13355 cp_parser_asm_clobber_list (cp_parser* parser)
13357 tree clobbers = NULL_TREE;
13362 tree string_literal;
13364 /* Look for the string literal. */
13365 token = cp_parser_require (parser, CPP_STRING, "string-literal");
13366 string_literal = token ? token->value : error_mark_node;
13367 /* Add it to the list. */
13368 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
13369 /* If the next token is not a `,', then the list is
13371 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13373 /* Consume the `,' token. */
13374 cp_lexer_consume_token (parser->lexer);
13380 /* Parse an (optional) series of attributes.
13383 attributes attribute
13386 __attribute__ (( attribute-list [opt] ))
13388 The return value is as for cp_parser_attribute_list. */
13391 cp_parser_attributes_opt (cp_parser* parser)
13393 tree attributes = NULL_TREE;
13398 tree attribute_list;
13400 /* Peek at the next token. */
13401 token = cp_lexer_peek_token (parser->lexer);
13402 /* If it's not `__attribute__', then we're done. */
13403 if (token->keyword != RID_ATTRIBUTE)
13406 /* Consume the `__attribute__' keyword. */
13407 cp_lexer_consume_token (parser->lexer);
13408 /* Look for the two `(' tokens. */
13409 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13410 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13412 /* Peek at the next token. */
13413 token = cp_lexer_peek_token (parser->lexer);
13414 if (token->type != CPP_CLOSE_PAREN)
13415 /* Parse the attribute-list. */
13416 attribute_list = cp_parser_attribute_list (parser);
13418 /* If the next token is a `)', then there is no attribute
13420 attribute_list = NULL;
13422 /* Look for the two `)' tokens. */
13423 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13424 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13426 /* Add these new attributes to the list. */
13427 attributes = chainon (attributes, attribute_list);
13433 /* Parse an attribute-list.
13437 attribute-list , attribute
13441 identifier ( identifier )
13442 identifier ( identifier , expression-list )
13443 identifier ( expression-list )
13445 Returns a TREE_LIST. Each node corresponds to an attribute. THe
13446 TREE_PURPOSE of each node is the identifier indicating which
13447 attribute is in use. The TREE_VALUE represents the arguments, if
13451 cp_parser_attribute_list (cp_parser* parser)
13453 tree attribute_list = NULL_TREE;
13455 c_lex_string_translate = false;
13462 /* Look for the identifier. We also allow keywords here; for
13463 example `__attribute__ ((const))' is legal. */
13464 token = cp_lexer_peek_token (parser->lexer);
13465 if (token->type != CPP_NAME
13466 && token->type != CPP_KEYWORD)
13467 return error_mark_node;
13468 /* Consume the token. */
13469 token = cp_lexer_consume_token (parser->lexer);
13471 /* Save away the identifier that indicates which attribute this is. */
13472 identifier = token->value;
13473 attribute = build_tree_list (identifier, NULL_TREE);
13475 /* Peek at the next token. */
13476 token = cp_lexer_peek_token (parser->lexer);
13477 /* If it's an `(', then parse the attribute arguments. */
13478 if (token->type == CPP_OPEN_PAREN)
13482 arguments = (cp_parser_parenthesized_expression_list
13483 (parser, true, /*non_constant_p=*/NULL));
13484 /* Save the identifier and arguments away. */
13485 TREE_VALUE (attribute) = arguments;
13488 /* Add this attribute to the list. */
13489 TREE_CHAIN (attribute) = attribute_list;
13490 attribute_list = attribute;
13492 /* Now, look for more attributes. */
13493 token = cp_lexer_peek_token (parser->lexer);
13494 /* If the next token isn't a `,', we're done. */
13495 if (token->type != CPP_COMMA)
13498 /* Consume the comma and keep going. */
13499 cp_lexer_consume_token (parser->lexer);
13501 c_lex_string_translate = true;
13503 /* We built up the list in reverse order. */
13504 return nreverse (attribute_list);
13507 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
13508 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
13509 current value of the PEDANTIC flag, regardless of whether or not
13510 the `__extension__' keyword is present. The caller is responsible
13511 for restoring the value of the PEDANTIC flag. */
13514 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
13516 /* Save the old value of the PEDANTIC flag. */
13517 *saved_pedantic = pedantic;
13519 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
13521 /* Consume the `__extension__' token. */
13522 cp_lexer_consume_token (parser->lexer);
13523 /* We're not being pedantic while the `__extension__' keyword is
13533 /* Parse a label declaration.
13536 __label__ label-declarator-seq ;
13538 label-declarator-seq:
13539 identifier , label-declarator-seq
13543 cp_parser_label_declaration (cp_parser* parser)
13545 /* Look for the `__label__' keyword. */
13546 cp_parser_require_keyword (parser, RID_LABEL, "`__label__'");
13552 /* Look for an identifier. */
13553 identifier = cp_parser_identifier (parser);
13554 /* Declare it as a lobel. */
13555 finish_label_decl (identifier);
13556 /* If the next token is a `;', stop. */
13557 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13559 /* Look for the `,' separating the label declarations. */
13560 cp_parser_require (parser, CPP_COMMA, "`,'");
13563 /* Look for the final `;'. */
13564 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
13567 /* Support Functions */
13569 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
13570 NAME should have one of the representations used for an
13571 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
13572 is returned. If PARSER->SCOPE is a dependent type, then a
13573 SCOPE_REF is returned.
13575 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
13576 returned; the name was already resolved when the TEMPLATE_ID_EXPR
13577 was formed. Abstractly, such entities should not be passed to this
13578 function, because they do not need to be looked up, but it is
13579 simpler to check for this special case here, rather than at the
13582 In cases not explicitly covered above, this function returns a
13583 DECL, OVERLOAD, or baselink representing the result of the lookup.
13584 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
13587 If IS_TYPE is TRUE, bindings that do not refer to types are
13590 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
13593 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
13596 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
13600 cp_parser_lookup_name (cp_parser *parser, tree name,
13601 bool is_type, bool is_template, bool is_namespace,
13602 bool check_dependency)
13605 tree object_type = parser->context->object_type;
13607 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
13608 no longer valid. Note that if we are parsing tentatively, and
13609 the parse fails, OBJECT_TYPE will be automatically restored. */
13610 parser->context->object_type = NULL_TREE;
13612 if (name == error_mark_node)
13613 return error_mark_node;
13615 /* A template-id has already been resolved; there is no lookup to
13617 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
13619 if (BASELINK_P (name))
13621 my_friendly_assert ((TREE_CODE (BASELINK_FUNCTIONS (name))
13622 == TEMPLATE_ID_EXPR),
13627 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
13628 it should already have been checked to make sure that the name
13629 used matches the type being destroyed. */
13630 if (TREE_CODE (name) == BIT_NOT_EXPR)
13634 /* Figure out to which type this destructor applies. */
13636 type = parser->scope;
13637 else if (object_type)
13638 type = object_type;
13640 type = current_class_type;
13641 /* If that's not a class type, there is no destructor. */
13642 if (!type || !CLASS_TYPE_P (type))
13643 return error_mark_node;
13644 if (!CLASSTYPE_DESTRUCTORS (type))
13645 return error_mark_node;
13646 /* If it was a class type, return the destructor. */
13647 return CLASSTYPE_DESTRUCTORS (type);
13650 /* By this point, the NAME should be an ordinary identifier. If
13651 the id-expression was a qualified name, the qualifying scope is
13652 stored in PARSER->SCOPE at this point. */
13653 my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE,
13656 /* Perform the lookup. */
13661 if (parser->scope == error_mark_node)
13662 return error_mark_node;
13664 /* If the SCOPE is dependent, the lookup must be deferred until
13665 the template is instantiated -- unless we are explicitly
13666 looking up names in uninstantiated templates. Even then, we
13667 cannot look up the name if the scope is not a class type; it
13668 might, for example, be a template type parameter. */
13669 dependent_p = (TYPE_P (parser->scope)
13670 && !(parser->in_declarator_p
13671 && currently_open_class (parser->scope))
13672 && dependent_type_p (parser->scope));
13673 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
13677 /* The resolution to Core Issue 180 says that `struct A::B'
13678 should be considered a type-name, even if `A' is
13680 decl = TYPE_NAME (make_typename_type (parser->scope,
13683 else if (is_template)
13684 decl = make_unbound_class_template (parser->scope,
13688 decl = build_nt (SCOPE_REF, parser->scope, name);
13692 bool pop_p = false;
13694 /* If PARSER->SCOPE is a dependent type, then it must be a
13695 class type, and we must not be checking dependencies;
13696 otherwise, we would have processed this lookup above. So
13697 that PARSER->SCOPE is not considered a dependent base by
13698 lookup_member, we must enter the scope here. */
13700 pop_p = push_scope (parser->scope);
13701 /* If the PARSER->SCOPE is a a template specialization, it
13702 may be instantiated during name lookup. In that case,
13703 errors may be issued. Even if we rollback the current
13704 tentative parse, those errors are valid. */
13705 decl = lookup_qualified_name (parser->scope, name, is_type,
13706 /*complain=*/true);
13708 pop_scope (parser->scope);
13710 parser->qualifying_scope = parser->scope;
13711 parser->object_scope = NULL_TREE;
13713 else if (object_type)
13715 tree object_decl = NULL_TREE;
13716 /* Look up the name in the scope of the OBJECT_TYPE, unless the
13717 OBJECT_TYPE is not a class. */
13718 if (CLASS_TYPE_P (object_type))
13719 /* If the OBJECT_TYPE is a template specialization, it may
13720 be instantiated during name lookup. In that case, errors
13721 may be issued. Even if we rollback the current tentative
13722 parse, those errors are valid. */
13723 object_decl = lookup_member (object_type,
13725 /*protect=*/0, is_type);
13726 /* Look it up in the enclosing context, too. */
13727 decl = lookup_name_real (name, is_type, /*nonclass=*/0,
13730 parser->object_scope = object_type;
13731 parser->qualifying_scope = NULL_TREE;
13733 decl = object_decl;
13737 decl = lookup_name_real (name, is_type, /*nonclass=*/0,
13740 parser->qualifying_scope = NULL_TREE;
13741 parser->object_scope = NULL_TREE;
13744 /* If the lookup failed, let our caller know. */
13746 || decl == error_mark_node
13747 || (TREE_CODE (decl) == FUNCTION_DECL
13748 && DECL_ANTICIPATED (decl)))
13749 return error_mark_node;
13751 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
13752 if (TREE_CODE (decl) == TREE_LIST)
13754 /* The error message we have to print is too complicated for
13755 cp_parser_error, so we incorporate its actions directly. */
13756 if (!cp_parser_simulate_error (parser))
13758 error ("reference to `%D' is ambiguous", name);
13759 print_candidates (decl);
13761 return error_mark_node;
13764 my_friendly_assert (DECL_P (decl)
13765 || TREE_CODE (decl) == OVERLOAD
13766 || TREE_CODE (decl) == SCOPE_REF
13767 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
13768 || BASELINK_P (decl),
13771 /* If we have resolved the name of a member declaration, check to
13772 see if the declaration is accessible. When the name resolves to
13773 set of overloaded functions, accessibility is checked when
13774 overload resolution is done.
13776 During an explicit instantiation, access is not checked at all,
13777 as per [temp.explicit]. */
13779 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
13784 /* Like cp_parser_lookup_name, but for use in the typical case where
13785 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
13786 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
13789 cp_parser_lookup_name_simple (cp_parser* parser, tree name)
13791 return cp_parser_lookup_name (parser, name,
13793 /*is_template=*/false,
13794 /*is_namespace=*/false,
13795 /*check_dependency=*/true);
13798 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
13799 the current context, return the TYPE_DECL. If TAG_NAME_P is
13800 true, the DECL indicates the class being defined in a class-head,
13801 or declared in an elaborated-type-specifier.
13803 Otherwise, return DECL. */
13806 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
13808 /* If the TEMPLATE_DECL is being declared as part of a class-head,
13809 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
13812 template <typename T> struct B;
13815 template <typename T> struct A::B {};
13817 Similarly, in a elaborated-type-specifier:
13819 namespace N { struct X{}; }
13822 template <typename T> friend struct N::X;
13825 However, if the DECL refers to a class type, and we are in
13826 the scope of the class, then the name lookup automatically
13827 finds the TYPE_DECL created by build_self_reference rather
13828 than a TEMPLATE_DECL. For example, in:
13830 template <class T> struct S {
13834 there is no need to handle such case. */
13836 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
13837 return DECL_TEMPLATE_RESULT (decl);
13842 /* If too many, or too few, template-parameter lists apply to the
13843 declarator, issue an error message. Returns TRUE if all went well,
13844 and FALSE otherwise. */
13847 cp_parser_check_declarator_template_parameters (cp_parser* parser,
13850 unsigned num_templates;
13852 /* We haven't seen any classes that involve template parameters yet. */
13855 switch (TREE_CODE (declarator))
13862 tree main_declarator = TREE_OPERAND (declarator, 0);
13864 cp_parser_check_declarator_template_parameters (parser,
13873 scope = TREE_OPERAND (declarator, 0);
13874 member = TREE_OPERAND (declarator, 1);
13876 /* If this is a pointer-to-member, then we are not interested
13877 in the SCOPE, because it does not qualify the thing that is
13879 if (TREE_CODE (member) == INDIRECT_REF)
13880 return (cp_parser_check_declarator_template_parameters
13883 while (scope && CLASS_TYPE_P (scope))
13885 /* You're supposed to have one `template <...>'
13886 for every template class, but you don't need one
13887 for a full specialization. For example:
13889 template <class T> struct S{};
13890 template <> struct S<int> { void f(); };
13891 void S<int>::f () {}
13893 is correct; there shouldn't be a `template <>' for
13894 the definition of `S<int>::f'. */
13895 if (CLASSTYPE_TEMPLATE_INFO (scope)
13896 && (CLASSTYPE_TEMPLATE_INSTANTIATION (scope)
13897 || uses_template_parms (CLASSTYPE_TI_ARGS (scope)))
13898 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
13901 scope = TYPE_CONTEXT (scope);
13905 /* Fall through. */
13908 /* If the DECLARATOR has the form `X<y>' then it uses one
13909 additional level of template parameters. */
13910 if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
13913 return cp_parser_check_template_parameters (parser,
13918 /* NUM_TEMPLATES were used in the current declaration. If that is
13919 invalid, return FALSE and issue an error messages. Otherwise,
13923 cp_parser_check_template_parameters (cp_parser* parser,
13924 unsigned num_templates)
13926 /* If there are more template classes than parameter lists, we have
13929 template <class T> void S<T>::R<T>::f (); */
13930 if (parser->num_template_parameter_lists < num_templates)
13932 error ("too few template-parameter-lists");
13935 /* If there are the same number of template classes and parameter
13936 lists, that's OK. */
13937 if (parser->num_template_parameter_lists == num_templates)
13939 /* If there are more, but only one more, then we are referring to a
13940 member template. That's OK too. */
13941 if (parser->num_template_parameter_lists == num_templates + 1)
13943 /* Otherwise, there are too many template parameter lists. We have
13946 template <class T> template <class U> void S::f(); */
13947 error ("too many template-parameter-lists");
13951 /* Parse a binary-expression of the general form:
13955 binary-expression <token> <expr>
13957 The TOKEN_TREE_MAP maps <token> types to <expr> codes. FN is used
13958 to parser the <expr>s. If the first production is used, then the
13959 value returned by FN is returned directly. Otherwise, a node with
13960 the indicated EXPR_TYPE is returned, with operands corresponding to
13961 the two sub-expressions. */
13964 cp_parser_binary_expression (cp_parser* parser,
13965 const cp_parser_token_tree_map token_tree_map,
13966 cp_parser_expression_fn fn)
13970 /* Parse the first expression. */
13971 lhs = (*fn) (parser);
13972 /* Now, look for more expressions. */
13976 const cp_parser_token_tree_map_node *map_node;
13979 /* Peek at the next token. */
13980 token = cp_lexer_peek_token (parser->lexer);
13981 /* If the token is `>', and that's not an operator at the
13982 moment, then we're done. */
13983 if (token->type == CPP_GREATER
13984 && !parser->greater_than_is_operator_p)
13986 /* If we find one of the tokens we want, build the corresponding
13987 tree representation. */
13988 for (map_node = token_tree_map;
13989 map_node->token_type != CPP_EOF;
13991 if (map_node->token_type == token->type)
13993 /* Consume the operator token. */
13994 cp_lexer_consume_token (parser->lexer);
13995 /* Parse the right-hand side of the expression. */
13996 rhs = (*fn) (parser);
13997 /* Build the binary tree node. */
13998 lhs = build_x_binary_op (map_node->tree_type, lhs, rhs);
14002 /* If the token wasn't one of the ones we want, we're done. */
14003 if (map_node->token_type == CPP_EOF)
14010 /* Parse an optional `::' token indicating that the following name is
14011 from the global namespace. If so, PARSER->SCOPE is set to the
14012 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
14013 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
14014 Returns the new value of PARSER->SCOPE, if the `::' token is
14015 present, and NULL_TREE otherwise. */
14018 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
14022 /* Peek at the next token. */
14023 token = cp_lexer_peek_token (parser->lexer);
14024 /* If we're looking at a `::' token then we're starting from the
14025 global namespace, not our current location. */
14026 if (token->type == CPP_SCOPE)
14028 /* Consume the `::' token. */
14029 cp_lexer_consume_token (parser->lexer);
14030 /* Set the SCOPE so that we know where to start the lookup. */
14031 parser->scope = global_namespace;
14032 parser->qualifying_scope = global_namespace;
14033 parser->object_scope = NULL_TREE;
14035 return parser->scope;
14037 else if (!current_scope_valid_p)
14039 parser->scope = NULL_TREE;
14040 parser->qualifying_scope = NULL_TREE;
14041 parser->object_scope = NULL_TREE;
14047 /* Returns TRUE if the upcoming token sequence is the start of a
14048 constructor declarator. If FRIEND_P is true, the declarator is
14049 preceded by the `friend' specifier. */
14052 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
14054 bool constructor_p;
14055 tree type_decl = NULL_TREE;
14056 bool nested_name_p;
14057 cp_token *next_token;
14059 /* The common case is that this is not a constructor declarator, so
14060 try to avoid doing lots of work if at all possible. It's not
14061 valid declare a constructor at function scope. */
14062 if (at_function_scope_p ())
14064 /* And only certain tokens can begin a constructor declarator. */
14065 next_token = cp_lexer_peek_token (parser->lexer);
14066 if (next_token->type != CPP_NAME
14067 && next_token->type != CPP_SCOPE
14068 && next_token->type != CPP_NESTED_NAME_SPECIFIER
14069 && next_token->type != CPP_TEMPLATE_ID)
14072 /* Parse tentatively; we are going to roll back all of the tokens
14074 cp_parser_parse_tentatively (parser);
14075 /* Assume that we are looking at a constructor declarator. */
14076 constructor_p = true;
14078 /* Look for the optional `::' operator. */
14079 cp_parser_global_scope_opt (parser,
14080 /*current_scope_valid_p=*/false);
14081 /* Look for the nested-name-specifier. */
14083 = (cp_parser_nested_name_specifier_opt (parser,
14084 /*typename_keyword_p=*/false,
14085 /*check_dependency_p=*/false,
14087 /*is_declaration=*/false)
14089 /* Outside of a class-specifier, there must be a
14090 nested-name-specifier. */
14091 if (!nested_name_p &&
14092 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
14094 constructor_p = false;
14095 /* If we still think that this might be a constructor-declarator,
14096 look for a class-name. */
14101 template <typename T> struct S { S(); };
14102 template <typename T> S<T>::S ();
14104 we must recognize that the nested `S' names a class.
14107 template <typename T> S<T>::S<T> ();
14109 we must recognize that the nested `S' names a template. */
14110 type_decl = cp_parser_class_name (parser,
14111 /*typename_keyword_p=*/false,
14112 /*template_keyword_p=*/false,
14114 /*check_dependency_p=*/false,
14115 /*class_head_p=*/false,
14116 /*is_declaration=*/false);
14117 /* If there was no class-name, then this is not a constructor. */
14118 constructor_p = !cp_parser_error_occurred (parser);
14121 /* If we're still considering a constructor, we have to see a `(',
14122 to begin the parameter-declaration-clause, followed by either a
14123 `)', an `...', or a decl-specifier. We need to check for a
14124 type-specifier to avoid being fooled into thinking that:
14128 is a constructor. (It is actually a function named `f' that
14129 takes one parameter (of type `int') and returns a value of type
14132 && cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
14134 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
14135 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
14136 && !cp_parser_storage_class_specifier_opt (parser))
14139 bool pop_p = false;
14140 unsigned saved_num_template_parameter_lists;
14142 /* Names appearing in the type-specifier should be looked up
14143 in the scope of the class. */
14144 if (current_class_type)
14148 type = TREE_TYPE (type_decl);
14149 if (TREE_CODE (type) == TYPENAME_TYPE)
14151 type = resolve_typename_type (type,
14152 /*only_current_p=*/false);
14153 if (type == error_mark_node)
14155 cp_parser_abort_tentative_parse (parser);
14159 pop_p = push_scope (type);
14162 /* Inside the constructor parameter list, surrounding
14163 template-parameter-lists do not apply. */
14164 saved_num_template_parameter_lists
14165 = parser->num_template_parameter_lists;
14166 parser->num_template_parameter_lists = 0;
14168 /* Look for the type-specifier. */
14169 cp_parser_type_specifier (parser,
14170 CP_PARSER_FLAGS_NONE,
14171 /*is_friend=*/false,
14172 /*is_declarator=*/true,
14173 /*declares_class_or_enum=*/NULL,
14174 /*is_cv_qualifier=*/NULL);
14176 parser->num_template_parameter_lists
14177 = saved_num_template_parameter_lists;
14179 /* Leave the scope of the class. */
14183 constructor_p = !cp_parser_error_occurred (parser);
14187 constructor_p = false;
14188 /* We did not really want to consume any tokens. */
14189 cp_parser_abort_tentative_parse (parser);
14191 return constructor_p;
14194 /* Parse the definition of the function given by the DECL_SPECIFIERS,
14195 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
14196 they must be performed once we are in the scope of the function.
14198 Returns the function defined. */
14201 cp_parser_function_definition_from_specifiers_and_declarator
14202 (cp_parser* parser,
14203 tree decl_specifiers,
14210 /* Begin the function-definition. */
14211 success_p = begin_function_definition (decl_specifiers,
14215 /* If there were names looked up in the decl-specifier-seq that we
14216 did not check, check them now. We must wait until we are in the
14217 scope of the function to perform the checks, since the function
14218 might be a friend. */
14219 perform_deferred_access_checks ();
14223 /* If begin_function_definition didn't like the definition, skip
14224 the entire function. */
14225 error ("invalid function declaration");
14226 cp_parser_skip_to_end_of_block_or_statement (parser);
14227 fn = error_mark_node;
14230 fn = cp_parser_function_definition_after_declarator (parser,
14231 /*inline_p=*/false);
14236 /* Parse the part of a function-definition that follows the
14237 declarator. INLINE_P is TRUE iff this function is an inline
14238 function defined with a class-specifier.
14240 Returns the function defined. */
14243 cp_parser_function_definition_after_declarator (cp_parser* parser,
14247 bool ctor_initializer_p = false;
14248 bool saved_in_unbraced_linkage_specification_p;
14249 unsigned saved_num_template_parameter_lists;
14251 /* If the next token is `return', then the code may be trying to
14252 make use of the "named return value" extension that G++ used to
14254 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
14256 /* Consume the `return' keyword. */
14257 cp_lexer_consume_token (parser->lexer);
14258 /* Look for the identifier that indicates what value is to be
14260 cp_parser_identifier (parser);
14261 /* Issue an error message. */
14262 error ("named return values are no longer supported");
14263 /* Skip tokens until we reach the start of the function body. */
14264 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
14265 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
14266 cp_lexer_consume_token (parser->lexer);
14268 /* The `extern' in `extern "C" void f () { ... }' does not apply to
14269 anything declared inside `f'. */
14270 saved_in_unbraced_linkage_specification_p
14271 = parser->in_unbraced_linkage_specification_p;
14272 parser->in_unbraced_linkage_specification_p = false;
14273 /* Inside the function, surrounding template-parameter-lists do not
14275 saved_num_template_parameter_lists
14276 = parser->num_template_parameter_lists;
14277 parser->num_template_parameter_lists = 0;
14278 /* If the next token is `try', then we are looking at a
14279 function-try-block. */
14280 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
14281 ctor_initializer_p = cp_parser_function_try_block (parser);
14282 /* A function-try-block includes the function-body, so we only do
14283 this next part if we're not processing a function-try-block. */
14286 = cp_parser_ctor_initializer_opt_and_function_body (parser);
14288 /* Finish the function. */
14289 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
14290 (inline_p ? 2 : 0));
14291 /* Generate code for it, if necessary. */
14292 expand_or_defer_fn (fn);
14293 /* Restore the saved values. */
14294 parser->in_unbraced_linkage_specification_p
14295 = saved_in_unbraced_linkage_specification_p;
14296 parser->num_template_parameter_lists
14297 = saved_num_template_parameter_lists;
14302 /* Parse a template-declaration, assuming that the `export' (and
14303 `extern') keywords, if present, has already been scanned. MEMBER_P
14304 is as for cp_parser_template_declaration. */
14307 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
14309 tree decl = NULL_TREE;
14310 tree parameter_list;
14311 bool friend_p = false;
14313 /* Look for the `template' keyword. */
14314 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'"))
14318 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
14321 /* If the next token is `>', then we have an invalid
14322 specialization. Rather than complain about an invalid template
14323 parameter, issue an error message here. */
14324 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
14326 cp_parser_error (parser, "invalid explicit specialization");
14327 begin_specialization ();
14328 parameter_list = NULL_TREE;
14332 /* Parse the template parameters. */
14333 begin_template_parm_list ();
14334 parameter_list = cp_parser_template_parameter_list (parser);
14335 parameter_list = end_template_parm_list (parameter_list);
14338 /* Look for the `>'. */
14339 cp_parser_skip_until_found (parser, CPP_GREATER, "`>'");
14340 /* We just processed one more parameter list. */
14341 ++parser->num_template_parameter_lists;
14342 /* If the next token is `template', there are more template
14344 if (cp_lexer_next_token_is_keyword (parser->lexer,
14346 cp_parser_template_declaration_after_export (parser, member_p);
14349 decl = cp_parser_single_declaration (parser,
14353 /* If this is a member template declaration, let the front
14355 if (member_p && !friend_p && decl)
14357 if (TREE_CODE (decl) == TYPE_DECL)
14358 cp_parser_check_access_in_redeclaration (decl);
14360 decl = finish_member_template_decl (decl);
14362 else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
14363 make_friend_class (current_class_type, TREE_TYPE (decl),
14364 /*complain=*/true);
14366 /* We are done with the current parameter list. */
14367 --parser->num_template_parameter_lists;
14370 finish_template_decl (parameter_list);
14372 /* Register member declarations. */
14373 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
14374 finish_member_declaration (decl);
14376 /* If DECL is a function template, we must return to parse it later.
14377 (Even though there is no definition, there might be default
14378 arguments that need handling.) */
14379 if (member_p && decl
14380 && (TREE_CODE (decl) == FUNCTION_DECL
14381 || DECL_FUNCTION_TEMPLATE_P (decl)))
14382 TREE_VALUE (parser->unparsed_functions_queues)
14383 = tree_cons (NULL_TREE, decl,
14384 TREE_VALUE (parser->unparsed_functions_queues));
14387 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
14388 `function-definition' sequence. MEMBER_P is true, this declaration
14389 appears in a class scope.
14391 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
14392 *FRIEND_P is set to TRUE iff the declaration is a friend. */
14395 cp_parser_single_declaration (cp_parser* parser,
14399 int declares_class_or_enum;
14400 tree decl = NULL_TREE;
14401 tree decl_specifiers;
14403 bool function_definition_p = false;
14405 /* Defer access checks until we know what is being declared. */
14406 push_deferring_access_checks (dk_deferred);
14408 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
14411 = cp_parser_decl_specifier_seq (parser,
14412 CP_PARSER_FLAGS_OPTIONAL,
14414 &declares_class_or_enum);
14416 *friend_p = cp_parser_friend_p (decl_specifiers);
14417 /* Gather up the access checks that occurred the
14418 decl-specifier-seq. */
14419 stop_deferring_access_checks ();
14421 /* Check for the declaration of a template class. */
14422 if (declares_class_or_enum)
14424 if (cp_parser_declares_only_class_p (parser))
14426 decl = shadow_tag (decl_specifiers);
14428 decl = TYPE_NAME (decl);
14430 decl = error_mark_node;
14435 /* If it's not a template class, try for a template function. If
14436 the next token is a `;', then this declaration does not declare
14437 anything. But, if there were errors in the decl-specifiers, then
14438 the error might well have come from an attempted class-specifier.
14439 In that case, there's no need to warn about a missing declarator. */
14441 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
14442 || !value_member (error_mark_node, decl_specifiers)))
14443 decl = cp_parser_init_declarator (parser,
14446 /*function_definition_allowed_p=*/true,
14448 declares_class_or_enum,
14449 &function_definition_p);
14451 pop_deferring_access_checks ();
14453 /* Clear any current qualification; whatever comes next is the start
14454 of something new. */
14455 parser->scope = NULL_TREE;
14456 parser->qualifying_scope = NULL_TREE;
14457 parser->object_scope = NULL_TREE;
14458 /* Look for a trailing `;' after the declaration. */
14459 if (!function_definition_p
14460 && !cp_parser_require (parser, CPP_SEMICOLON, "`;'"))
14461 cp_parser_skip_to_end_of_block_or_statement (parser);
14466 /* Parse a cast-expression that is not the operand of a unary "&". */
14469 cp_parser_simple_cast_expression (cp_parser *parser)
14471 return cp_parser_cast_expression (parser, /*address_p=*/false);
14474 /* Parse a functional cast to TYPE. Returns an expression
14475 representing the cast. */
14478 cp_parser_functional_cast (cp_parser* parser, tree type)
14480 tree expression_list;
14483 = cp_parser_parenthesized_expression_list (parser, false,
14484 /*non_constant_p=*/NULL);
14486 return build_functional_cast (type, expression_list);
14489 /* Save the tokens that make up the body of a member function defined
14490 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
14491 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
14492 specifiers applied to the declaration. Returns the FUNCTION_DECL
14493 for the member function. */
14496 cp_parser_save_member_function_body (cp_parser* parser,
14497 tree decl_specifiers,
14501 cp_token_cache *cache;
14504 /* Create the function-declaration. */
14505 fn = start_method (decl_specifiers, declarator, attributes);
14506 /* If something went badly wrong, bail out now. */
14507 if (fn == error_mark_node)
14509 /* If there's a function-body, skip it. */
14510 if (cp_parser_token_starts_function_definition_p
14511 (cp_lexer_peek_token (parser->lexer)))
14512 cp_parser_skip_to_end_of_block_or_statement (parser);
14513 return error_mark_node;
14516 /* Remember it, if there default args to post process. */
14517 cp_parser_save_default_args (parser, fn);
14519 /* Create a token cache. */
14520 cache = cp_token_cache_new ();
14521 /* Save away the tokens that make up the body of the
14523 cp_parser_cache_group (parser, cache, CPP_CLOSE_BRACE, /*depth=*/0);
14524 /* Handle function try blocks. */
14525 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
14526 cp_parser_cache_group (parser, cache, CPP_CLOSE_BRACE, /*depth=*/0);
14528 /* Save away the inline definition; we will process it when the
14529 class is complete. */
14530 DECL_PENDING_INLINE_INFO (fn) = cache;
14531 DECL_PENDING_INLINE_P (fn) = 1;
14533 /* We need to know that this was defined in the class, so that
14534 friend templates are handled correctly. */
14535 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
14537 /* We're done with the inline definition. */
14538 finish_method (fn);
14540 /* Add FN to the queue of functions to be parsed later. */
14541 TREE_VALUE (parser->unparsed_functions_queues)
14542 = tree_cons (NULL_TREE, fn,
14543 TREE_VALUE (parser->unparsed_functions_queues));
14548 /* Parse a template-argument-list, as well as the trailing ">" (but
14549 not the opening ">"). See cp_parser_template_argument_list for the
14553 cp_parser_enclosed_template_argument_list (cp_parser* parser)
14557 tree saved_qualifying_scope;
14558 tree saved_object_scope;
14559 bool saved_greater_than_is_operator_p;
14563 When parsing a template-id, the first non-nested `>' is taken as
14564 the end of the template-argument-list rather than a greater-than
14566 saved_greater_than_is_operator_p
14567 = parser->greater_than_is_operator_p;
14568 parser->greater_than_is_operator_p = false;
14569 /* Parsing the argument list may modify SCOPE, so we save it
14571 saved_scope = parser->scope;
14572 saved_qualifying_scope = parser->qualifying_scope;
14573 saved_object_scope = parser->object_scope;
14574 /* Parse the template-argument-list itself. */
14575 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
14576 arguments = NULL_TREE;
14578 arguments = cp_parser_template_argument_list (parser);
14579 /* Look for the `>' that ends the template-argument-list. If we find
14580 a '>>' instead, it's probably just a typo. */
14581 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
14583 if (!saved_greater_than_is_operator_p)
14585 /* If we're in a nested template argument list, the '>>' has to be
14586 a typo for '> >'. We emit the error message, but we continue
14587 parsing and we push a '>' as next token, so that the argument
14588 list will be parsed correctly.. */
14590 error ("`>>' should be `> >' within a nested template argument list");
14591 token = cp_lexer_peek_token (parser->lexer);
14592 token->type = CPP_GREATER;
14596 /* If this is not a nested template argument list, the '>>' is
14597 a typo for '>'. Emit an error message and continue. */
14598 error ("spurious `>>', use `>' to terminate a template argument list");
14599 cp_lexer_consume_token (parser->lexer);
14602 else if (!cp_parser_require (parser, CPP_GREATER, "`>'"))
14603 error ("missing `>' to terminate the template argument list");
14604 /* The `>' token might be a greater-than operator again now. */
14605 parser->greater_than_is_operator_p
14606 = saved_greater_than_is_operator_p;
14607 /* Restore the SAVED_SCOPE. */
14608 parser->scope = saved_scope;
14609 parser->qualifying_scope = saved_qualifying_scope;
14610 parser->object_scope = saved_object_scope;
14615 /* MEMBER_FUNCTION is a member function, or a friend. If default
14616 arguments, or the body of the function have not yet been parsed,
14620 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
14622 cp_lexer *saved_lexer;
14624 /* If this member is a template, get the underlying
14626 if (DECL_FUNCTION_TEMPLATE_P (member_function))
14627 member_function = DECL_TEMPLATE_RESULT (member_function);
14629 /* There should not be any class definitions in progress at this
14630 point; the bodies of members are only parsed outside of all class
14632 my_friendly_assert (parser->num_classes_being_defined == 0, 20010816);
14633 /* While we're parsing the member functions we might encounter more
14634 classes. We want to handle them right away, but we don't want
14635 them getting mixed up with functions that are currently in the
14637 parser->unparsed_functions_queues
14638 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
14640 /* Make sure that any template parameters are in scope. */
14641 maybe_begin_member_template_processing (member_function);
14643 /* If the body of the function has not yet been parsed, parse it
14645 if (DECL_PENDING_INLINE_P (member_function))
14647 tree function_scope;
14648 cp_token_cache *tokens;
14650 /* The function is no longer pending; we are processing it. */
14651 tokens = DECL_PENDING_INLINE_INFO (member_function);
14652 DECL_PENDING_INLINE_INFO (member_function) = NULL;
14653 DECL_PENDING_INLINE_P (member_function) = 0;
14654 /* If this was an inline function in a local class, enter the scope
14655 of the containing function. */
14656 function_scope = decl_function_context (member_function);
14657 if (function_scope)
14658 push_function_context_to (function_scope);
14660 /* Save away the current lexer. */
14661 saved_lexer = parser->lexer;
14662 /* Make a new lexer to feed us the tokens saved for this function. */
14663 parser->lexer = cp_lexer_new_from_tokens (tokens);
14664 parser->lexer->next = saved_lexer;
14666 /* Set the current source position to be the location of the first
14667 token in the saved inline body. */
14668 cp_lexer_peek_token (parser->lexer);
14670 /* Let the front end know that we going to be defining this
14672 start_function (NULL_TREE, member_function, NULL_TREE,
14673 SF_PRE_PARSED | SF_INCLASS_INLINE);
14675 /* Now, parse the body of the function. */
14676 cp_parser_function_definition_after_declarator (parser,
14677 /*inline_p=*/true);
14679 /* Leave the scope of the containing function. */
14680 if (function_scope)
14681 pop_function_context_from (function_scope);
14682 /* Restore the lexer. */
14683 parser->lexer = saved_lexer;
14686 /* Remove any template parameters from the symbol table. */
14687 maybe_end_member_template_processing ();
14689 /* Restore the queue. */
14690 parser->unparsed_functions_queues
14691 = TREE_CHAIN (parser->unparsed_functions_queues);
14694 /* If DECL contains any default args, remember it on the unparsed
14695 functions queue. */
14698 cp_parser_save_default_args (cp_parser* parser, tree decl)
14702 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
14704 probe = TREE_CHAIN (probe))
14705 if (TREE_PURPOSE (probe))
14707 TREE_PURPOSE (parser->unparsed_functions_queues)
14708 = tree_cons (NULL_TREE, decl,
14709 TREE_PURPOSE (parser->unparsed_functions_queues));
14715 /* FN is a FUNCTION_DECL which may contains a parameter with an
14716 unparsed DEFAULT_ARG. Parse the default args now. */
14719 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
14721 cp_lexer *saved_lexer;
14722 cp_token_cache *tokens;
14723 bool saved_local_variables_forbidden_p;
14726 /* While we're parsing the default args, we might (due to the
14727 statement expression extension) encounter more classes. We want
14728 to handle them right away, but we don't want them getting mixed
14729 up with default args that are currently in the queue. */
14730 parser->unparsed_functions_queues
14731 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
14733 for (parameters = TYPE_ARG_TYPES (TREE_TYPE (fn));
14735 parameters = TREE_CHAIN (parameters))
14737 if (!TREE_PURPOSE (parameters)
14738 || TREE_CODE (TREE_PURPOSE (parameters)) != DEFAULT_ARG)
14741 /* Save away the current lexer. */
14742 saved_lexer = parser->lexer;
14743 /* Create a new one, using the tokens we have saved. */
14744 tokens = DEFARG_TOKENS (TREE_PURPOSE (parameters));
14745 parser->lexer = cp_lexer_new_from_tokens (tokens);
14747 /* Set the current source position to be the location of the
14748 first token in the default argument. */
14749 cp_lexer_peek_token (parser->lexer);
14751 /* Local variable names (and the `this' keyword) may not appear
14752 in a default argument. */
14753 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
14754 parser->local_variables_forbidden_p = true;
14755 /* Parse the assignment-expression. */
14756 if (DECL_CLASS_SCOPE_P (fn))
14757 push_nested_class (DECL_CONTEXT (fn));
14758 TREE_PURPOSE (parameters) = cp_parser_assignment_expression (parser);
14759 if (DECL_CLASS_SCOPE_P (fn))
14760 pop_nested_class ();
14762 /* If the token stream has not been completely used up, then
14763 there was extra junk after the end of the default
14765 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
14766 cp_parser_error (parser, "expected `,'");
14768 /* Restore saved state. */
14769 parser->lexer = saved_lexer;
14770 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
14773 /* Restore the queue. */
14774 parser->unparsed_functions_queues
14775 = TREE_CHAIN (parser->unparsed_functions_queues);
14778 /* Parse the operand of `sizeof' (or a similar operator). Returns
14779 either a TYPE or an expression, depending on the form of the
14780 input. The KEYWORD indicates which kind of expression we have
14784 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
14786 static const char *format;
14787 tree expr = NULL_TREE;
14788 const char *saved_message;
14789 bool saved_integral_constant_expression_p;
14791 /* Initialize FORMAT the first time we get here. */
14793 format = "types may not be defined in `%s' expressions";
14795 /* Types cannot be defined in a `sizeof' expression. Save away the
14797 saved_message = parser->type_definition_forbidden_message;
14798 /* And create the new one. */
14799 parser->type_definition_forbidden_message
14800 = xmalloc (strlen (format)
14801 + strlen (IDENTIFIER_POINTER (ridpointers[keyword]))
14803 sprintf ((char *) parser->type_definition_forbidden_message,
14804 format, IDENTIFIER_POINTER (ridpointers[keyword]));
14806 /* The restrictions on constant-expressions do not apply inside
14807 sizeof expressions. */
14808 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
14809 parser->integral_constant_expression_p = false;
14811 /* Do not actually evaluate the expression. */
14813 /* If it's a `(', then we might be looking at the type-id
14815 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
14818 bool saved_in_type_id_in_expr_p;
14820 /* We can't be sure yet whether we're looking at a type-id or an
14822 cp_parser_parse_tentatively (parser);
14823 /* Consume the `('. */
14824 cp_lexer_consume_token (parser->lexer);
14825 /* Parse the type-id. */
14826 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
14827 parser->in_type_id_in_expr_p = true;
14828 type = cp_parser_type_id (parser);
14829 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
14830 /* Now, look for the trailing `)'. */
14831 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14832 /* If all went well, then we're done. */
14833 if (cp_parser_parse_definitely (parser))
14835 /* Build a list of decl-specifiers; right now, we have only
14836 a single type-specifier. */
14837 type = build_tree_list (NULL_TREE,
14840 /* Call grokdeclarator to figure out what type this is. */
14841 expr = grokdeclarator (NULL_TREE,
14845 /*attrlist=*/NULL);
14849 /* If the type-id production did not work out, then we must be
14850 looking at the unary-expression production. */
14852 expr = cp_parser_unary_expression (parser, /*address_p=*/false);
14853 /* Go back to evaluating expressions. */
14856 /* Free the message we created. */
14857 free ((char *) parser->type_definition_forbidden_message);
14858 /* And restore the old one. */
14859 parser->type_definition_forbidden_message = saved_message;
14860 parser->integral_constant_expression_p = saved_integral_constant_expression_p;
14865 /* If the current declaration has no declarator, return true. */
14868 cp_parser_declares_only_class_p (cp_parser *parser)
14870 /* If the next token is a `;' or a `,' then there is no
14872 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
14873 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
14876 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
14877 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
14880 cp_parser_friend_p (tree decl_specifiers)
14882 while (decl_specifiers)
14884 /* See if this decl-specifier is `friend'. */
14885 if (TREE_CODE (TREE_VALUE (decl_specifiers)) == IDENTIFIER_NODE
14886 && C_RID_CODE (TREE_VALUE (decl_specifiers)) == RID_FRIEND)
14889 /* Go on to the next decl-specifier. */
14890 decl_specifiers = TREE_CHAIN (decl_specifiers);
14896 /* If the next token is of the indicated TYPE, consume it. Otherwise,
14897 issue an error message indicating that TOKEN_DESC was expected.
14899 Returns the token consumed, if the token had the appropriate type.
14900 Otherwise, returns NULL. */
14903 cp_parser_require (cp_parser* parser,
14904 enum cpp_ttype type,
14905 const char* token_desc)
14907 if (cp_lexer_next_token_is (parser->lexer, type))
14908 return cp_lexer_consume_token (parser->lexer);
14911 /* Output the MESSAGE -- unless we're parsing tentatively. */
14912 if (!cp_parser_simulate_error (parser))
14914 char *message = concat ("expected ", token_desc, NULL);
14915 cp_parser_error (parser, message);
14922 /* Like cp_parser_require, except that tokens will be skipped until
14923 the desired token is found. An error message is still produced if
14924 the next token is not as expected. */
14927 cp_parser_skip_until_found (cp_parser* parser,
14928 enum cpp_ttype type,
14929 const char* token_desc)
14932 unsigned nesting_depth = 0;
14934 if (cp_parser_require (parser, type, token_desc))
14937 /* Skip tokens until the desired token is found. */
14940 /* Peek at the next token. */
14941 token = cp_lexer_peek_token (parser->lexer);
14942 /* If we've reached the token we want, consume it and
14944 if (token->type == type && !nesting_depth)
14946 cp_lexer_consume_token (parser->lexer);
14949 /* If we've run out of tokens, stop. */
14950 if (token->type == CPP_EOF)
14952 if (token->type == CPP_OPEN_BRACE
14953 || token->type == CPP_OPEN_PAREN
14954 || token->type == CPP_OPEN_SQUARE)
14956 else if (token->type == CPP_CLOSE_BRACE
14957 || token->type == CPP_CLOSE_PAREN
14958 || token->type == CPP_CLOSE_SQUARE)
14960 if (nesting_depth-- == 0)
14963 /* Consume this token. */
14964 cp_lexer_consume_token (parser->lexer);
14968 /* If the next token is the indicated keyword, consume it. Otherwise,
14969 issue an error message indicating that TOKEN_DESC was expected.
14971 Returns the token consumed, if the token had the appropriate type.
14972 Otherwise, returns NULL. */
14975 cp_parser_require_keyword (cp_parser* parser,
14977 const char* token_desc)
14979 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
14981 if (token && token->keyword != keyword)
14983 dyn_string_t error_msg;
14985 /* Format the error message. */
14986 error_msg = dyn_string_new (0);
14987 dyn_string_append_cstr (error_msg, "expected ");
14988 dyn_string_append_cstr (error_msg, token_desc);
14989 cp_parser_error (parser, error_msg->s);
14990 dyn_string_delete (error_msg);
14997 /* Returns TRUE iff TOKEN is a token that can begin the body of a
14998 function-definition. */
15001 cp_parser_token_starts_function_definition_p (cp_token* token)
15003 return (/* An ordinary function-body begins with an `{'. */
15004 token->type == CPP_OPEN_BRACE
15005 /* A ctor-initializer begins with a `:'. */
15006 || token->type == CPP_COLON
15007 /* A function-try-block begins with `try'. */
15008 || token->keyword == RID_TRY
15009 /* The named return value extension begins with `return'. */
15010 || token->keyword == RID_RETURN);
15013 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
15017 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
15021 token = cp_lexer_peek_token (parser->lexer);
15022 return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
15025 /* Returns TRUE iff the next token is the "," or ">" ending a
15026 template-argument. ">>" is also accepted (after the full
15027 argument was parsed) because it's probably a typo for "> >",
15028 and there is a specific diagnostic for this. */
15031 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
15035 token = cp_lexer_peek_token (parser->lexer);
15036 return (token->type == CPP_COMMA || token->type == CPP_GREATER
15037 || token->type == CPP_RSHIFT);
15040 /* Returns TRUE iff the n-th token is a ">", or the n-th is a "[" and the
15041 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
15044 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
15049 token = cp_lexer_peek_nth_token (parser->lexer, n);
15050 if (token->type == CPP_LESS)
15052 /* Check for the sequence `<::' in the original code. It would be lexed as
15053 `[:', where `[' is a digraph, and there is no whitespace before
15055 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
15058 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
15059 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
15065 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
15066 or none_type otherwise. */
15068 static enum tag_types
15069 cp_parser_token_is_class_key (cp_token* token)
15071 switch (token->keyword)
15076 return record_type;
15085 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
15088 cp_parser_check_class_key (enum tag_types class_key, tree type)
15090 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
15091 pedwarn ("`%s' tag used in naming `%#T'",
15092 class_key == union_type ? "union"
15093 : class_key == record_type ? "struct" : "class",
15097 /* Issue an error message if DECL is redeclared with different
15098 access than its original declaration [class.access.spec/3].
15099 This applies to nested classes and nested class templates.
15102 static void cp_parser_check_access_in_redeclaration (tree decl)
15104 if (!CLASS_TYPE_P (TREE_TYPE (decl)))
15107 if ((TREE_PRIVATE (decl)
15108 != (current_access_specifier == access_private_node))
15109 || (TREE_PROTECTED (decl)
15110 != (current_access_specifier == access_protected_node)))
15111 error ("%D redeclared with different access", decl);
15114 /* Look for the `template' keyword, as a syntactic disambiguator.
15115 Return TRUE iff it is present, in which case it will be
15119 cp_parser_optional_template_keyword (cp_parser *parser)
15121 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
15123 /* The `template' keyword can only be used within templates;
15124 outside templates the parser can always figure out what is a
15125 template and what is not. */
15126 if (!processing_template_decl)
15128 error ("`template' (as a disambiguator) is only allowed "
15129 "within templates");
15130 /* If this part of the token stream is rescanned, the same
15131 error message would be generated. So, we purge the token
15132 from the stream. */
15133 cp_lexer_purge_token (parser->lexer);
15138 /* Consume the `template' keyword. */
15139 cp_lexer_consume_token (parser->lexer);
15147 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
15148 set PARSER->SCOPE, and perform other related actions. */
15151 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
15156 /* Get the stored value. */
15157 value = cp_lexer_consume_token (parser->lexer)->value;
15158 /* Perform any access checks that were deferred. */
15159 for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
15160 perform_or_defer_access_check (TREE_PURPOSE (check), TREE_VALUE (check));
15161 /* Set the scope from the stored value. */
15162 parser->scope = TREE_VALUE (value);
15163 parser->qualifying_scope = TREE_TYPE (value);
15164 parser->object_scope = NULL_TREE;
15167 /* Add tokens to CACHE until a non-nested END token appears. */
15170 cp_parser_cache_group (cp_parser *parser,
15171 cp_token_cache *cache,
15172 enum cpp_ttype end,
15179 /* Abort a parenthesized expression if we encounter a brace. */
15180 if ((end == CPP_CLOSE_PAREN || depth == 0)
15181 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
15183 /* If we've reached the end of the file, stop. */
15184 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
15186 /* Consume the next token. */
15187 token = cp_lexer_consume_token (parser->lexer);
15188 /* Add this token to the tokens we are saving. */
15189 cp_token_cache_push_token (cache, token);
15190 /* See if it starts a new group. */
15191 if (token->type == CPP_OPEN_BRACE)
15193 cp_parser_cache_group (parser, cache, CPP_CLOSE_BRACE, depth + 1);
15197 else if (token->type == CPP_OPEN_PAREN)
15198 cp_parser_cache_group (parser, cache, CPP_CLOSE_PAREN, depth + 1);
15199 else if (token->type == end)
15204 /* Begin parsing tentatively. We always save tokens while parsing
15205 tentatively so that if the tentative parsing fails we can restore the
15209 cp_parser_parse_tentatively (cp_parser* parser)
15211 /* Enter a new parsing context. */
15212 parser->context = cp_parser_context_new (parser->context);
15213 /* Begin saving tokens. */
15214 cp_lexer_save_tokens (parser->lexer);
15215 /* In order to avoid repetitive access control error messages,
15216 access checks are queued up until we are no longer parsing
15218 push_deferring_access_checks (dk_deferred);
15221 /* Commit to the currently active tentative parse. */
15224 cp_parser_commit_to_tentative_parse (cp_parser* parser)
15226 cp_parser_context *context;
15229 /* Mark all of the levels as committed. */
15230 lexer = parser->lexer;
15231 for (context = parser->context; context->next; context = context->next)
15233 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
15235 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
15236 while (!cp_lexer_saving_tokens (lexer))
15237 lexer = lexer->next;
15238 cp_lexer_commit_tokens (lexer);
15242 /* Abort the currently active tentative parse. All consumed tokens
15243 will be rolled back, and no diagnostics will be issued. */
15246 cp_parser_abort_tentative_parse (cp_parser* parser)
15248 cp_parser_simulate_error (parser);
15249 /* Now, pretend that we want to see if the construct was
15250 successfully parsed. */
15251 cp_parser_parse_definitely (parser);
15254 /* Stop parsing tentatively. If a parse error has occurred, restore the
15255 token stream. Otherwise, commit to the tokens we have consumed.
15256 Returns true if no error occurred; false otherwise. */
15259 cp_parser_parse_definitely (cp_parser* parser)
15261 bool error_occurred;
15262 cp_parser_context *context;
15264 /* Remember whether or not an error occurred, since we are about to
15265 destroy that information. */
15266 error_occurred = cp_parser_error_occurred (parser);
15267 /* Remove the topmost context from the stack. */
15268 context = parser->context;
15269 parser->context = context->next;
15270 /* If no parse errors occurred, commit to the tentative parse. */
15271 if (!error_occurred)
15273 /* Commit to the tokens read tentatively, unless that was
15275 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
15276 cp_lexer_commit_tokens (parser->lexer);
15278 pop_to_parent_deferring_access_checks ();
15280 /* Otherwise, if errors occurred, roll back our state so that things
15281 are just as they were before we began the tentative parse. */
15284 cp_lexer_rollback_tokens (parser->lexer);
15285 pop_deferring_access_checks ();
15287 /* Add the context to the front of the free list. */
15288 context->next = cp_parser_context_free_list;
15289 cp_parser_context_free_list = context;
15291 return !error_occurred;
15294 /* Returns true if we are parsing tentatively -- but have decided that
15295 we will stick with this tentative parse, even if errors occur. */
15298 cp_parser_committed_to_tentative_parse (cp_parser* parser)
15300 return (cp_parser_parsing_tentatively (parser)
15301 && parser->context->status == CP_PARSER_STATUS_KIND_COMMITTED);
15304 /* Returns nonzero iff an error has occurred during the most recent
15305 tentative parse. */
15308 cp_parser_error_occurred (cp_parser* parser)
15310 return (cp_parser_parsing_tentatively (parser)
15311 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
15314 /* Returns nonzero if GNU extensions are allowed. */
15317 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
15319 return parser->allow_gnu_extensions_p;
15326 static GTY (()) cp_parser *the_parser;
15328 /* External interface. */
15330 /* Parse one entire translation unit. */
15333 c_parse_file (void)
15335 bool error_occurred;
15337 the_parser = cp_parser_new ();
15338 push_deferring_access_checks (flag_access_control
15339 ? dk_no_deferred : dk_no_check);
15340 error_occurred = cp_parser_translation_unit (the_parser);
15344 /* This variable must be provided by every front end. */
15348 #include "gt-cp-parser.h"