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)
504 if (c_lex_string_translate)
506 tree value = token->value;
509 /* We might as well go ahead and release the chained
510 translated string such that we can reuse its memory. */
511 if (TREE_CHAIN (value))
512 value = TREE_CHAIN (token->value);
514 /* Get the current type. It will be an ARRAY_TYPE. */
515 type = TREE_TYPE (value);
516 /* Use build_cplus_array_type to rebuild the array, thereby
517 getting the right type. */
518 type = build_cplus_array_type (TREE_TYPE (type),
520 /* Reset the type of the token. */
521 TREE_TYPE (value) = type;
528 /* If the circular buffer is full, make it bigger. */
531 cp_lexer_maybe_grow_buffer (cp_lexer* lexer)
533 /* If the buffer is full, enlarge it. */
534 if (lexer->last_token == lexer->first_token)
536 cp_token *new_buffer;
537 cp_token *old_buffer;
538 cp_token *new_first_token;
539 ptrdiff_t buffer_length;
540 size_t num_tokens_to_copy;
542 /* Remember the current buffer pointer. It will become invalid,
543 but we will need to do pointer arithmetic involving this
545 old_buffer = lexer->buffer;
546 /* Compute the current buffer size. */
547 buffer_length = lexer->buffer_end - lexer->buffer;
548 /* Allocate a buffer twice as big. */
549 new_buffer = ggc_realloc (lexer->buffer,
550 2 * buffer_length * sizeof (cp_token));
552 /* Because the buffer is circular, logically consecutive tokens
553 are not necessarily placed consecutively in memory.
554 Therefore, we must keep move the tokens that were before
555 FIRST_TOKEN to the second half of the newly allocated
557 num_tokens_to_copy = (lexer->first_token - old_buffer);
558 memcpy (new_buffer + buffer_length,
560 num_tokens_to_copy * sizeof (cp_token));
561 /* Clear the rest of the buffer. We never look at this storage,
562 but the garbage collector may. */
563 memset (new_buffer + buffer_length + num_tokens_to_copy, 0,
564 (buffer_length - num_tokens_to_copy) * sizeof (cp_token));
566 /* Now recompute all of the buffer pointers. */
568 = new_buffer + (lexer->first_token - old_buffer);
569 if (lexer->next_token != NULL)
571 ptrdiff_t next_token_delta;
573 if (lexer->next_token > lexer->first_token)
574 next_token_delta = lexer->next_token - lexer->first_token;
577 buffer_length - (lexer->first_token - lexer->next_token);
578 lexer->next_token = new_first_token + next_token_delta;
580 lexer->last_token = new_first_token + buffer_length;
581 lexer->buffer = new_buffer;
582 lexer->buffer_end = new_buffer + buffer_length * 2;
583 lexer->first_token = new_first_token;
587 /* Store the next token from the preprocessor in *TOKEN. */
590 cp_lexer_get_preprocessor_token (cp_lexer *lexer ATTRIBUTE_UNUSED ,
595 /* If this not the main lexer, return a terminating CPP_EOF token. */
596 if (lexer != NULL && !lexer->main_lexer_p)
598 token->type = CPP_EOF;
599 token->location.line = 0;
600 token->location.file = NULL;
601 token->value = NULL_TREE;
602 token->keyword = RID_MAX;
608 /* Keep going until we get a token we like. */
611 /* Get a new token from the preprocessor. */
612 token->type = c_lex_with_flags (&token->value, &token->flags);
613 /* Issue messages about tokens we cannot process. */
619 error ("invalid token");
623 /* This is a good token, so we exit the loop. */
628 /* Now we've got our token. */
629 token->location = input_location;
631 /* Check to see if this token is a keyword. */
632 if (token->type == CPP_NAME
633 && C_IS_RESERVED_WORD (token->value))
635 /* Mark this token as a keyword. */
636 token->type = CPP_KEYWORD;
637 /* Record which keyword. */
638 token->keyword = C_RID_CODE (token->value);
639 /* Update the value. Some keywords are mapped to particular
640 entities, rather than simply having the value of the
641 corresponding IDENTIFIER_NODE. For example, `__const' is
642 mapped to `const'. */
643 token->value = ridpointers[token->keyword];
646 token->keyword = RID_MAX;
649 /* Return a pointer to the next token in the token stream, but do not
653 cp_lexer_peek_token (cp_lexer* lexer)
657 /* If there are no tokens, read one now. */
658 if (!lexer->next_token)
659 cp_lexer_read_token (lexer);
661 /* Provide debugging output. */
662 if (cp_lexer_debugging_p (lexer))
664 fprintf (cp_lexer_debug_stream, "cp_lexer: peeking at token: ");
665 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
666 fprintf (cp_lexer_debug_stream, "\n");
669 token = lexer->next_token;
670 cp_lexer_set_source_position_from_token (lexer, token);
674 /* Return true if the next token has the indicated TYPE. */
677 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
681 /* Peek at the next token. */
682 token = cp_lexer_peek_token (lexer);
683 /* Check to see if it has the indicated TYPE. */
684 return token->type == type;
687 /* Return true if the next token does not have the indicated TYPE. */
690 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
692 return !cp_lexer_next_token_is (lexer, type);
695 /* Return true if the next token is the indicated KEYWORD. */
698 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
702 /* Peek at the next token. */
703 token = cp_lexer_peek_token (lexer);
704 /* Check to see if it is the indicated keyword. */
705 return token->keyword == keyword;
708 /* Return a pointer to the Nth token in the token stream. If N is 1,
709 then this is precisely equivalent to cp_lexer_peek_token. */
712 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
716 /* N is 1-based, not zero-based. */
717 my_friendly_assert (n > 0, 20000224);
719 /* Skip ahead from NEXT_TOKEN, reading more tokens as necessary. */
720 token = lexer->next_token;
721 /* If there are no tokens in the buffer, get one now. */
724 cp_lexer_read_token (lexer);
725 token = lexer->next_token;
728 /* Now, read tokens until we have enough. */
731 /* Advance to the next token. */
732 token = cp_lexer_next_token (lexer, token);
733 /* If that's all the tokens we have, read a new one. */
734 if (token == lexer->last_token)
735 token = cp_lexer_read_token (lexer);
741 /* Consume the next token. The pointer returned is valid only until
742 another token is read. Callers should preserve copy the token
743 explicitly if they will need its value for a longer period of
747 cp_lexer_consume_token (cp_lexer* lexer)
751 /* If there are no tokens, read one now. */
752 if (!lexer->next_token)
753 cp_lexer_read_token (lexer);
755 /* Remember the token we'll be returning. */
756 token = lexer->next_token;
758 /* Increment NEXT_TOKEN. */
759 lexer->next_token = cp_lexer_next_token (lexer,
761 /* Check to see if we're all out of tokens. */
762 if (lexer->next_token == lexer->last_token)
763 lexer->next_token = NULL;
765 /* If we're not saving tokens, then move FIRST_TOKEN too. */
766 if (!cp_lexer_saving_tokens (lexer))
768 /* If there are no tokens available, set FIRST_TOKEN to NULL. */
769 if (!lexer->next_token)
770 lexer->first_token = NULL;
772 lexer->first_token = lexer->next_token;
775 /* Provide debugging output. */
776 if (cp_lexer_debugging_p (lexer))
778 fprintf (cp_lexer_debug_stream, "cp_lexer: consuming token: ");
779 cp_lexer_print_token (cp_lexer_debug_stream, token);
780 fprintf (cp_lexer_debug_stream, "\n");
786 /* Permanently remove the next token from the token stream. There
787 must be a valid next token already; this token never reads
788 additional tokens from the preprocessor. */
791 cp_lexer_purge_token (cp_lexer *lexer)
794 cp_token *next_token;
796 token = lexer->next_token;
799 next_token = cp_lexer_next_token (lexer, token);
800 if (next_token == lexer->last_token)
802 *token = *next_token;
806 lexer->last_token = token;
807 /* The token purged may have been the only token remaining; if so,
809 if (lexer->next_token == token)
810 lexer->next_token = NULL;
813 /* Permanently remove all tokens after TOKEN, up to, but not
814 including, the token that will be returned next by
815 cp_lexer_peek_token. */
818 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *token)
824 if (lexer->next_token)
826 /* Copy the tokens that have not yet been read to the location
827 immediately following TOKEN. */
828 t1 = cp_lexer_next_token (lexer, token);
829 t2 = peek = cp_lexer_peek_token (lexer);
830 /* Move tokens into the vacant area between TOKEN and PEEK. */
831 while (t2 != lexer->last_token)
834 t1 = cp_lexer_next_token (lexer, t1);
835 t2 = cp_lexer_next_token (lexer, t2);
837 /* Now, the next available token is right after TOKEN. */
838 lexer->next_token = cp_lexer_next_token (lexer, token);
839 /* And the last token is wherever we ended up. */
840 lexer->last_token = t1;
844 /* There are no tokens in the buffer, so there is nothing to
845 copy. The last token in the buffer is TOKEN itself. */
846 lexer->last_token = cp_lexer_next_token (lexer, token);
850 /* Begin saving tokens. All tokens consumed after this point will be
854 cp_lexer_save_tokens (cp_lexer* lexer)
856 /* Provide debugging output. */
857 if (cp_lexer_debugging_p (lexer))
858 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
860 /* Make sure that LEXER->NEXT_TOKEN is non-NULL so that we can
861 restore the tokens if required. */
862 if (!lexer->next_token)
863 cp_lexer_read_token (lexer);
865 VARRAY_PUSH_INT (lexer->saved_tokens,
866 cp_lexer_token_difference (lexer,
871 /* Commit to the portion of the token stream most recently saved. */
874 cp_lexer_commit_tokens (cp_lexer* lexer)
876 /* Provide debugging output. */
877 if (cp_lexer_debugging_p (lexer))
878 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
880 VARRAY_POP (lexer->saved_tokens);
883 /* Return all tokens saved since the last call to cp_lexer_save_tokens
884 to the token stream. Stop saving tokens. */
887 cp_lexer_rollback_tokens (cp_lexer* lexer)
891 /* Provide debugging output. */
892 if (cp_lexer_debugging_p (lexer))
893 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
895 /* Find the token that was the NEXT_TOKEN when we started saving
897 delta = VARRAY_TOP_INT(lexer->saved_tokens);
898 /* Make it the next token again now. */
899 lexer->next_token = cp_lexer_advance_token (lexer,
902 /* It might be the case that there were no tokens when we started
903 saving tokens, but that there are some tokens now. */
904 if (!lexer->next_token && lexer->first_token)
905 lexer->next_token = lexer->first_token;
907 /* Stop saving tokens. */
908 VARRAY_POP (lexer->saved_tokens);
911 /* Print a representation of the TOKEN on the STREAM. */
914 cp_lexer_print_token (FILE * stream, cp_token* token)
916 const char *token_type = NULL;
918 /* Figure out what kind of token this is. */
926 token_type = "COMMA";
930 token_type = "OPEN_PAREN";
933 case CPP_CLOSE_PAREN:
934 token_type = "CLOSE_PAREN";
938 token_type = "OPEN_BRACE";
941 case CPP_CLOSE_BRACE:
942 token_type = "CLOSE_BRACE";
946 token_type = "SEMICOLON";
958 token_type = "keyword";
961 /* This is not a token that we know how to handle yet. */
966 /* If we have a name for the token, print it out. Otherwise, we
967 simply give the numeric code. */
969 fprintf (stream, "%s", token_type);
971 fprintf (stream, "%d", token->type);
972 /* And, for an identifier, print the identifier name. */
973 if (token->type == CPP_NAME
974 /* Some keywords have a value that is not an IDENTIFIER_NODE.
975 For example, `struct' is mapped to an INTEGER_CST. */
976 || (token->type == CPP_KEYWORD
977 && TREE_CODE (token->value) == IDENTIFIER_NODE))
978 fprintf (stream, " %s", IDENTIFIER_POINTER (token->value));
981 /* Start emitting debugging information. */
984 cp_lexer_start_debugging (cp_lexer* lexer)
986 ++lexer->debugging_p;
989 /* Stop emitting debugging information. */
992 cp_lexer_stop_debugging (cp_lexer* lexer)
994 --lexer->debugging_p;
1003 A cp_parser parses the token stream as specified by the C++
1004 grammar. Its job is purely parsing, not semantic analysis. For
1005 example, the parser breaks the token stream into declarators,
1006 expressions, statements, and other similar syntactic constructs.
1007 It does not check that the types of the expressions on either side
1008 of an assignment-statement are compatible, or that a function is
1009 not declared with a parameter of type `void'.
1011 The parser invokes routines elsewhere in the compiler to perform
1012 semantic analysis and to build up the abstract syntax tree for the
1015 The parser (and the template instantiation code, which is, in a
1016 way, a close relative of parsing) are the only parts of the
1017 compiler that should be calling push_scope and pop_scope, or
1018 related functions. The parser (and template instantiation code)
1019 keeps track of what scope is presently active; everything else
1020 should simply honor that. (The code that generates static
1021 initializers may also need to set the scope, in order to check
1022 access control correctly when emitting the initializers.)
1027 The parser is of the standard recursive-descent variety. Upcoming
1028 tokens in the token stream are examined in order to determine which
1029 production to use when parsing a non-terminal. Some C++ constructs
1030 require arbitrary look ahead to disambiguate. For example, it is
1031 impossible, in the general case, to tell whether a statement is an
1032 expression or declaration without scanning the entire statement.
1033 Therefore, the parser is capable of "parsing tentatively." When the
1034 parser is not sure what construct comes next, it enters this mode.
1035 Then, while we attempt to parse the construct, the parser queues up
1036 error messages, rather than issuing them immediately, and saves the
1037 tokens it consumes. If the construct is parsed successfully, the
1038 parser "commits", i.e., it issues any queued error messages and
1039 the tokens that were being preserved are permanently discarded.
1040 If, however, the construct is not parsed successfully, the parser
1041 rolls back its state completely so that it can resume parsing using
1042 a different alternative.
1047 The performance of the parser could probably be improved
1048 substantially. Some possible improvements include:
1050 - The expression parser recurses through the various levels of
1051 precedence as specified in the grammar, rather than using an
1052 operator-precedence technique. Therefore, parsing a simple
1053 identifier requires multiple recursive calls.
1055 - We could often eliminate the need to parse tentatively by
1056 looking ahead a little bit. In some places, this approach
1057 might not entirely eliminate the need to parse tentatively, but
1058 it might still speed up the average case. */
1060 /* Flags that are passed to some parsing functions. These values can
1061 be bitwise-ored together. */
1063 typedef enum cp_parser_flags
1066 CP_PARSER_FLAGS_NONE = 0x0,
1067 /* The construct is optional. If it is not present, then no error
1068 should be issued. */
1069 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1070 /* When parsing a type-specifier, do not allow user-defined types. */
1071 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2
1074 /* The different kinds of declarators we want to parse. */
1076 typedef enum cp_parser_declarator_kind
1078 /* We want an abstract declarator. */
1079 CP_PARSER_DECLARATOR_ABSTRACT,
1080 /* We want a named declarator. */
1081 CP_PARSER_DECLARATOR_NAMED,
1082 /* We don't mind, but the name must be an unqualified-id. */
1083 CP_PARSER_DECLARATOR_EITHER
1084 } cp_parser_declarator_kind;
1086 /* A mapping from a token type to a corresponding tree node type. */
1088 typedef struct cp_parser_token_tree_map_node
1090 /* The token type. */
1091 ENUM_BITFIELD (cpp_ttype) token_type : 8;
1092 /* The corresponding tree code. */
1093 ENUM_BITFIELD (tree_code) tree_type : 8;
1094 } cp_parser_token_tree_map_node;
1096 /* A complete map consists of several ordinary entries, followed by a
1097 terminator. The terminating entry has a token_type of CPP_EOF. */
1099 typedef cp_parser_token_tree_map_node cp_parser_token_tree_map[];
1101 /* The status of a tentative parse. */
1103 typedef enum cp_parser_status_kind
1105 /* No errors have occurred. */
1106 CP_PARSER_STATUS_KIND_NO_ERROR,
1107 /* An error has occurred. */
1108 CP_PARSER_STATUS_KIND_ERROR,
1109 /* We are committed to this tentative parse, whether or not an error
1111 CP_PARSER_STATUS_KIND_COMMITTED
1112 } cp_parser_status_kind;
1114 /* Context that is saved and restored when parsing tentatively. */
1116 typedef struct cp_parser_context GTY (())
1118 /* If this is a tentative parsing context, the status of the
1120 enum cp_parser_status_kind status;
1121 /* If non-NULL, we have just seen a `x->' or `x.' expression. Names
1122 that are looked up in this context must be looked up both in the
1123 scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1124 the context of the containing expression. */
1126 /* The next parsing context in the stack. */
1127 struct cp_parser_context *next;
1128 } cp_parser_context;
1132 /* Constructors and destructors. */
1134 static cp_parser_context *cp_parser_context_new
1135 (cp_parser_context *);
1137 /* Class variables. */
1139 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1141 /* Constructors and destructors. */
1143 /* Construct a new context. The context below this one on the stack
1144 is given by NEXT. */
1146 static cp_parser_context *
1147 cp_parser_context_new (cp_parser_context* next)
1149 cp_parser_context *context;
1151 /* Allocate the storage. */
1152 if (cp_parser_context_free_list != NULL)
1154 /* Pull the first entry from the free list. */
1155 context = cp_parser_context_free_list;
1156 cp_parser_context_free_list = context->next;
1157 memset (context, 0, sizeof (*context));
1160 context = ggc_alloc_cleared (sizeof (cp_parser_context));
1161 /* No errors have occurred yet in this context. */
1162 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1163 /* If this is not the bottomost context, copy information that we
1164 need from the previous context. */
1167 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1168 expression, then we are parsing one in this context, too. */
1169 context->object_type = next->object_type;
1170 /* Thread the stack. */
1171 context->next = next;
1177 /* The cp_parser structure represents the C++ parser. */
1179 typedef struct cp_parser GTY(())
1181 /* The lexer from which we are obtaining tokens. */
1184 /* The scope in which names should be looked up. If NULL_TREE, then
1185 we look up names in the scope that is currently open in the
1186 source program. If non-NULL, this is either a TYPE or
1187 NAMESPACE_DECL for the scope in which we should look.
1189 This value is not cleared automatically after a name is looked
1190 up, so we must be careful to clear it before starting a new look
1191 up sequence. (If it is not cleared, then `X::Y' followed by `Z'
1192 will look up `Z' in the scope of `X', rather than the current
1193 scope.) Unfortunately, it is difficult to tell when name lookup
1194 is complete, because we sometimes peek at a token, look it up,
1195 and then decide not to consume it. */
1198 /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1199 last lookup took place. OBJECT_SCOPE is used if an expression
1200 like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1201 respectively. QUALIFYING_SCOPE is used for an expression of the
1202 form "X::Y"; it refers to X. */
1204 tree qualifying_scope;
1206 /* A stack of parsing contexts. All but the bottom entry on the
1207 stack will be tentative contexts.
1209 We parse tentatively in order to determine which construct is in
1210 use in some situations. For example, in order to determine
1211 whether a statement is an expression-statement or a
1212 declaration-statement we parse it tentatively as a
1213 declaration-statement. If that fails, we then reparse the same
1214 token stream as an expression-statement. */
1215 cp_parser_context *context;
1217 /* True if we are parsing GNU C++. If this flag is not set, then
1218 GNU extensions are not recognized. */
1219 bool allow_gnu_extensions_p;
1221 /* TRUE if the `>' token should be interpreted as the greater-than
1222 operator. FALSE if it is the end of a template-id or
1223 template-parameter-list. */
1224 bool greater_than_is_operator_p;
1226 /* TRUE if default arguments are allowed within a parameter list
1227 that starts at this point. FALSE if only a gnu extension makes
1228 them permissible. */
1229 bool default_arg_ok_p;
1231 /* TRUE if we are parsing an integral constant-expression. See
1232 [expr.const] for a precise definition. */
1233 bool integral_constant_expression_p;
1235 /* TRUE if we are parsing an integral constant-expression -- but a
1236 non-constant expression should be permitted as well. This flag
1237 is used when parsing an array bound so that GNU variable-length
1238 arrays are tolerated. */
1239 bool allow_non_integral_constant_expression_p;
1241 /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1242 been seen that makes the expression non-constant. */
1243 bool non_integral_constant_expression_p;
1245 /* TRUE if local variable names and `this' are forbidden in the
1247 bool local_variables_forbidden_p;
1249 /* TRUE if the declaration we are parsing is part of a
1250 linkage-specification of the form `extern string-literal
1252 bool in_unbraced_linkage_specification_p;
1254 /* TRUE if we are presently parsing a declarator, after the
1255 direct-declarator. */
1256 bool in_declarator_p;
1258 /* TRUE if we are presently parsing a template-argument-list. */
1259 bool in_template_argument_list_p;
1261 /* TRUE if we are presently parsing the body of an
1262 iteration-statement. */
1263 bool in_iteration_statement_p;
1265 /* TRUE if we are presently parsing the body of a switch
1267 bool in_switch_statement_p;
1269 /* TRUE if we are parsing a type-id in an expression context. In
1270 such a situation, both "type (expr)" and "type (type)" are valid
1272 bool in_type_id_in_expr_p;
1274 /* If non-NULL, then we are parsing a construct where new type
1275 definitions are not permitted. The string stored here will be
1276 issued as an error message if a type is defined. */
1277 const char *type_definition_forbidden_message;
1279 /* A list of lists. The outer list is a stack, used for member
1280 functions of local classes. At each level there are two sub-list,
1281 one on TREE_VALUE and one on TREE_PURPOSE. Each of those
1282 sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their
1283 TREE_VALUE's. The functions are chained in reverse declaration
1286 The TREE_PURPOSE sublist contains those functions with default
1287 arguments that need post processing, and the TREE_VALUE sublist
1288 contains those functions with definitions that need post
1291 These lists can only be processed once the outermost class being
1292 defined is complete. */
1293 tree unparsed_functions_queues;
1295 /* The number of classes whose definitions are currently in
1297 unsigned num_classes_being_defined;
1299 /* The number of template parameter lists that apply directly to the
1300 current declaration. */
1301 unsigned num_template_parameter_lists;
1304 /* The type of a function that parses some kind of expression. */
1305 typedef tree (*cp_parser_expression_fn) (cp_parser *);
1309 /* Constructors and destructors. */
1311 static cp_parser *cp_parser_new
1314 /* Routines to parse various constructs.
1316 Those that return `tree' will return the error_mark_node (rather
1317 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1318 Sometimes, they will return an ordinary node if error-recovery was
1319 attempted, even though a parse error occurred. So, to check
1320 whether or not a parse error occurred, you should always use
1321 cp_parser_error_occurred. If the construct is optional (indicated
1322 either by an `_opt' in the name of the function that does the
1323 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1324 the construct is not present. */
1326 /* Lexical conventions [gram.lex] */
1328 static tree cp_parser_identifier
1331 /* Basic concepts [gram.basic] */
1333 static bool cp_parser_translation_unit
1336 /* Expressions [gram.expr] */
1338 static tree cp_parser_primary_expression
1339 (cp_parser *, cp_id_kind *, tree *);
1340 static tree cp_parser_id_expression
1341 (cp_parser *, bool, bool, bool *, bool);
1342 static tree cp_parser_unqualified_id
1343 (cp_parser *, bool, bool, bool);
1344 static tree cp_parser_nested_name_specifier_opt
1345 (cp_parser *, bool, bool, bool, bool);
1346 static tree cp_parser_nested_name_specifier
1347 (cp_parser *, bool, bool, bool, bool);
1348 static tree cp_parser_class_or_namespace_name
1349 (cp_parser *, bool, bool, bool, bool, bool);
1350 static tree cp_parser_postfix_expression
1351 (cp_parser *, bool);
1352 static tree cp_parser_postfix_open_square_expression
1353 (cp_parser *, tree, bool);
1354 static tree cp_parser_postfix_dot_deref_expression
1355 (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *);
1356 static tree cp_parser_parenthesized_expression_list
1357 (cp_parser *, bool, bool *);
1358 static void cp_parser_pseudo_destructor_name
1359 (cp_parser *, tree *, tree *);
1360 static tree cp_parser_unary_expression
1361 (cp_parser *, bool);
1362 static enum tree_code cp_parser_unary_operator
1364 static tree cp_parser_new_expression
1366 static tree cp_parser_new_placement
1368 static tree cp_parser_new_type_id
1370 static tree cp_parser_new_declarator_opt
1372 static tree cp_parser_direct_new_declarator
1374 static tree cp_parser_new_initializer
1376 static tree cp_parser_delete_expression
1378 static tree cp_parser_cast_expression
1379 (cp_parser *, bool);
1380 static tree cp_parser_pm_expression
1382 static tree cp_parser_multiplicative_expression
1384 static tree cp_parser_additive_expression
1386 static tree cp_parser_shift_expression
1388 static tree cp_parser_relational_expression
1390 static tree cp_parser_equality_expression
1392 static tree cp_parser_and_expression
1394 static tree cp_parser_exclusive_or_expression
1396 static tree cp_parser_inclusive_or_expression
1398 static tree cp_parser_logical_and_expression
1400 static tree cp_parser_logical_or_expression
1402 static tree cp_parser_question_colon_clause
1403 (cp_parser *, tree);
1404 static tree cp_parser_assignment_expression
1406 static enum tree_code cp_parser_assignment_operator_opt
1408 static tree cp_parser_expression
1410 static tree cp_parser_constant_expression
1411 (cp_parser *, bool, bool *);
1412 static tree cp_parser_builtin_offsetof
1415 /* Statements [gram.stmt.stmt] */
1417 static void cp_parser_statement
1418 (cp_parser *, tree);
1419 static tree cp_parser_labeled_statement
1420 (cp_parser *, tree);
1421 static tree cp_parser_expression_statement
1422 (cp_parser *, tree);
1423 static tree cp_parser_compound_statement
1424 (cp_parser *, tree, bool);
1425 static void cp_parser_statement_seq_opt
1426 (cp_parser *, tree);
1427 static tree cp_parser_selection_statement
1429 static tree cp_parser_condition
1431 static tree cp_parser_iteration_statement
1433 static void cp_parser_for_init_statement
1435 static tree cp_parser_jump_statement
1437 static void cp_parser_declaration_statement
1440 static tree cp_parser_implicitly_scoped_statement
1442 static void cp_parser_already_scoped_statement
1445 /* Declarations [gram.dcl.dcl] */
1447 static void cp_parser_declaration_seq_opt
1449 static void cp_parser_declaration
1451 static void cp_parser_block_declaration
1452 (cp_parser *, bool);
1453 static void cp_parser_simple_declaration
1454 (cp_parser *, bool);
1455 static tree cp_parser_decl_specifier_seq
1456 (cp_parser *, cp_parser_flags, tree *, int *);
1457 static tree cp_parser_storage_class_specifier_opt
1459 static tree cp_parser_function_specifier_opt
1461 static tree cp_parser_type_specifier
1462 (cp_parser *, cp_parser_flags, bool, bool, int *, bool *);
1463 static tree cp_parser_simple_type_specifier
1464 (cp_parser *, cp_parser_flags, bool);
1465 static tree cp_parser_type_name
1467 static tree cp_parser_elaborated_type_specifier
1468 (cp_parser *, bool, bool);
1469 static tree cp_parser_enum_specifier
1471 static void cp_parser_enumerator_list
1472 (cp_parser *, tree);
1473 static void cp_parser_enumerator_definition
1474 (cp_parser *, tree);
1475 static tree cp_parser_namespace_name
1477 static void cp_parser_namespace_definition
1479 static void cp_parser_namespace_body
1481 static tree cp_parser_qualified_namespace_specifier
1483 static void cp_parser_namespace_alias_definition
1485 static void cp_parser_using_declaration
1487 static void cp_parser_using_directive
1489 static void cp_parser_asm_definition
1491 static void cp_parser_linkage_specification
1494 /* Declarators [gram.dcl.decl] */
1496 static tree cp_parser_init_declarator
1497 (cp_parser *, tree, tree, bool, bool, int, bool *);
1498 static tree cp_parser_declarator
1499 (cp_parser *, cp_parser_declarator_kind, int *, bool *);
1500 static tree cp_parser_direct_declarator
1501 (cp_parser *, cp_parser_declarator_kind, int *);
1502 static enum tree_code cp_parser_ptr_operator
1503 (cp_parser *, tree *, tree *);
1504 static tree cp_parser_cv_qualifier_seq_opt
1506 static tree cp_parser_cv_qualifier_opt
1508 static tree cp_parser_declarator_id
1510 static tree cp_parser_type_id
1512 static tree cp_parser_type_specifier_seq
1514 static tree cp_parser_parameter_declaration_clause
1516 static tree cp_parser_parameter_declaration_list
1518 static tree cp_parser_parameter_declaration
1519 (cp_parser *, bool, bool *);
1520 static void cp_parser_function_body
1522 static tree cp_parser_initializer
1523 (cp_parser *, bool *, bool *);
1524 static tree cp_parser_initializer_clause
1525 (cp_parser *, bool *);
1526 static tree cp_parser_initializer_list
1527 (cp_parser *, bool *);
1529 static bool cp_parser_ctor_initializer_opt_and_function_body
1532 /* Classes [gram.class] */
1534 static tree cp_parser_class_name
1535 (cp_parser *, bool, bool, bool, bool, bool, bool);
1536 static tree cp_parser_class_specifier
1538 static tree cp_parser_class_head
1539 (cp_parser *, bool *, tree *);
1540 static enum tag_types cp_parser_class_key
1542 static void cp_parser_member_specification_opt
1544 static void cp_parser_member_declaration
1546 static tree cp_parser_pure_specifier
1548 static tree cp_parser_constant_initializer
1551 /* Derived classes [gram.class.derived] */
1553 static tree cp_parser_base_clause
1555 static tree cp_parser_base_specifier
1558 /* Special member functions [gram.special] */
1560 static tree cp_parser_conversion_function_id
1562 static tree cp_parser_conversion_type_id
1564 static tree cp_parser_conversion_declarator_opt
1566 static bool cp_parser_ctor_initializer_opt
1568 static void cp_parser_mem_initializer_list
1570 static tree cp_parser_mem_initializer
1572 static tree cp_parser_mem_initializer_id
1575 /* Overloading [gram.over] */
1577 static tree cp_parser_operator_function_id
1579 static tree cp_parser_operator
1582 /* Templates [gram.temp] */
1584 static void cp_parser_template_declaration
1585 (cp_parser *, bool);
1586 static tree cp_parser_template_parameter_list
1588 static tree cp_parser_template_parameter
1590 static tree cp_parser_type_parameter
1592 static tree cp_parser_template_id
1593 (cp_parser *, bool, bool, bool);
1594 static tree cp_parser_template_name
1595 (cp_parser *, bool, bool, bool, bool *);
1596 static tree cp_parser_template_argument_list
1598 static tree cp_parser_template_argument
1600 static void cp_parser_explicit_instantiation
1602 static void cp_parser_explicit_specialization
1605 /* Exception handling [gram.exception] */
1607 static tree cp_parser_try_block
1609 static bool cp_parser_function_try_block
1611 static void cp_parser_handler_seq
1613 static void cp_parser_handler
1615 static tree cp_parser_exception_declaration
1617 static tree cp_parser_throw_expression
1619 static tree cp_parser_exception_specification_opt
1621 static tree cp_parser_type_id_list
1624 /* GNU Extensions */
1626 static tree cp_parser_asm_specification_opt
1628 static tree cp_parser_asm_operand_list
1630 static tree cp_parser_asm_clobber_list
1632 static tree cp_parser_attributes_opt
1634 static tree cp_parser_attribute_list
1636 static bool cp_parser_extension_opt
1637 (cp_parser *, int *);
1638 static void cp_parser_label_declaration
1641 /* Utility Routines */
1643 static tree cp_parser_lookup_name
1644 (cp_parser *, tree, bool, bool, bool, bool);
1645 static tree cp_parser_lookup_name_simple
1646 (cp_parser *, tree);
1647 static tree cp_parser_maybe_treat_template_as_class
1649 static bool cp_parser_check_declarator_template_parameters
1650 (cp_parser *, tree);
1651 static bool cp_parser_check_template_parameters
1652 (cp_parser *, unsigned);
1653 static tree cp_parser_simple_cast_expression
1655 static tree cp_parser_binary_expression
1656 (cp_parser *, const cp_parser_token_tree_map, cp_parser_expression_fn);
1657 static tree cp_parser_global_scope_opt
1658 (cp_parser *, bool);
1659 static bool cp_parser_constructor_declarator_p
1660 (cp_parser *, bool);
1661 static tree cp_parser_function_definition_from_specifiers_and_declarator
1662 (cp_parser *, tree, tree, tree);
1663 static tree cp_parser_function_definition_after_declarator
1664 (cp_parser *, bool);
1665 static void cp_parser_template_declaration_after_export
1666 (cp_parser *, bool);
1667 static tree cp_parser_single_declaration
1668 (cp_parser *, bool, bool *);
1669 static tree cp_parser_functional_cast
1670 (cp_parser *, tree);
1671 static tree cp_parser_save_member_function_body
1672 (cp_parser *, tree, tree, tree);
1673 static tree cp_parser_enclosed_template_argument_list
1675 static void cp_parser_save_default_args
1676 (cp_parser *, tree);
1677 static void cp_parser_late_parsing_for_member
1678 (cp_parser *, tree);
1679 static void cp_parser_late_parsing_default_args
1680 (cp_parser *, tree);
1681 static tree cp_parser_sizeof_operand
1682 (cp_parser *, enum rid);
1683 static bool cp_parser_declares_only_class_p
1685 static bool cp_parser_friend_p
1687 static cp_token *cp_parser_require
1688 (cp_parser *, enum cpp_ttype, const char *);
1689 static cp_token *cp_parser_require_keyword
1690 (cp_parser *, enum rid, const char *);
1691 static bool cp_parser_token_starts_function_definition_p
1693 static bool cp_parser_next_token_starts_class_definition_p
1695 static bool cp_parser_next_token_ends_template_argument_p
1697 static bool cp_parser_nth_token_starts_template_argument_list_p
1698 (cp_parser *, size_t);
1699 static enum tag_types cp_parser_token_is_class_key
1701 static void cp_parser_check_class_key
1702 (enum tag_types, tree type);
1703 static void cp_parser_check_access_in_redeclaration
1705 static bool cp_parser_optional_template_keyword
1707 static void cp_parser_pre_parsed_nested_name_specifier
1709 static void cp_parser_cache_group
1710 (cp_parser *, cp_token_cache *, enum cpp_ttype, unsigned);
1711 static void cp_parser_parse_tentatively
1713 static void cp_parser_commit_to_tentative_parse
1715 static void cp_parser_abort_tentative_parse
1717 static bool cp_parser_parse_definitely
1719 static inline bool cp_parser_parsing_tentatively
1721 static bool cp_parser_committed_to_tentative_parse
1723 static void cp_parser_error
1724 (cp_parser *, const char *);
1725 static void cp_parser_name_lookup_error
1726 (cp_parser *, tree, tree, const char *);
1727 static bool cp_parser_simulate_error
1729 static void cp_parser_check_type_definition
1731 static void cp_parser_check_for_definition_in_return_type
1733 static void cp_parser_check_for_invalid_template_id
1734 (cp_parser *, tree);
1735 static bool cp_parser_non_integral_constant_expression
1736 (cp_parser *, const char *);
1737 static void cp_parser_diagnose_invalid_type_name
1738 (cp_parser *, tree, tree);
1739 static bool cp_parser_parse_and_diagnose_invalid_type_name
1741 static int cp_parser_skip_to_closing_parenthesis
1742 (cp_parser *, bool, bool, bool);
1743 static void cp_parser_skip_to_end_of_statement
1745 static void cp_parser_consume_semicolon_at_end_of_statement
1747 static void cp_parser_skip_to_end_of_block_or_statement
1749 static void cp_parser_skip_to_closing_brace
1751 static void cp_parser_skip_until_found
1752 (cp_parser *, enum cpp_ttype, const char *);
1753 static bool cp_parser_error_occurred
1755 static bool cp_parser_allow_gnu_extensions_p
1757 static bool cp_parser_is_string_literal
1759 static bool cp_parser_is_keyword
1760 (cp_token *, enum rid);
1761 static tree cp_parser_make_typename_type
1762 (cp_parser *, tree, tree);
1764 /* Returns nonzero if we are parsing tentatively. */
1767 cp_parser_parsing_tentatively (cp_parser* parser)
1769 return parser->context->next != NULL;
1772 /* Returns nonzero if TOKEN is a string literal. */
1775 cp_parser_is_string_literal (cp_token* token)
1777 return (token->type == CPP_STRING || token->type == CPP_WSTRING);
1780 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
1783 cp_parser_is_keyword (cp_token* token, enum rid keyword)
1785 return token->keyword == keyword;
1788 /* Issue the indicated error MESSAGE. */
1791 cp_parser_error (cp_parser* parser, const char* message)
1793 /* Output the MESSAGE -- unless we're parsing tentatively. */
1794 if (!cp_parser_simulate_error (parser))
1797 token = cp_lexer_peek_token (parser->lexer);
1798 c_parse_error (message,
1799 /* Because c_parser_error does not understand
1800 CPP_KEYWORD, keywords are treated like
1802 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
1807 /* Issue an error about name-lookup failing. NAME is the
1808 IDENTIFIER_NODE DECL is the result of
1809 the lookup (as returned from cp_parser_lookup_name). DESIRED is
1810 the thing that we hoped to find. */
1813 cp_parser_name_lookup_error (cp_parser* parser,
1816 const char* desired)
1818 /* If name lookup completely failed, tell the user that NAME was not
1820 if (decl == error_mark_node)
1822 if (parser->scope && parser->scope != global_namespace)
1823 error ("`%D::%D' has not been declared",
1824 parser->scope, name);
1825 else if (parser->scope == global_namespace)
1826 error ("`::%D' has not been declared", name);
1828 error ("`%D' has not been declared", name);
1830 else if (parser->scope && parser->scope != global_namespace)
1831 error ("`%D::%D' %s", parser->scope, name, desired);
1832 else if (parser->scope == global_namespace)
1833 error ("`::%D' %s", name, desired);
1835 error ("`%D' %s", name, desired);
1838 /* If we are parsing tentatively, remember that an error has occurred
1839 during this tentative parse. Returns true if the error was
1840 simulated; false if a message should be issued by the caller. */
1843 cp_parser_simulate_error (cp_parser* parser)
1845 if (cp_parser_parsing_tentatively (parser)
1846 && !cp_parser_committed_to_tentative_parse (parser))
1848 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
1854 /* This function is called when a type is defined. If type
1855 definitions are forbidden at this point, an error message is
1859 cp_parser_check_type_definition (cp_parser* parser)
1861 /* If types are forbidden here, issue a message. */
1862 if (parser->type_definition_forbidden_message)
1863 /* Use `%s' to print the string in case there are any escape
1864 characters in the message. */
1865 error ("%s", parser->type_definition_forbidden_message);
1868 /* This function is called when a declaration is parsed. If
1869 DECLARATOR is a function declarator and DECLARES_CLASS_OR_ENUM
1870 indicates that a type was defined in the decl-specifiers for DECL,
1871 then an error is issued. */
1874 cp_parser_check_for_definition_in_return_type (tree declarator,
1875 int declares_class_or_enum)
1877 /* [dcl.fct] forbids type definitions in return types.
1878 Unfortunately, it's not easy to know whether or not we are
1879 processing a return type until after the fact. */
1881 && (TREE_CODE (declarator) == INDIRECT_REF
1882 || TREE_CODE (declarator) == ADDR_EXPR))
1883 declarator = TREE_OPERAND (declarator, 0);
1885 && TREE_CODE (declarator) == CALL_EXPR
1886 && declares_class_or_enum & 2)
1887 error ("new types may not be defined in a return type");
1890 /* A type-specifier (TYPE) has been parsed which cannot be followed by
1891 "<" in any valid C++ program. If the next token is indeed "<",
1892 issue a message warning the user about what appears to be an
1893 invalid attempt to form a template-id. */
1896 cp_parser_check_for_invalid_template_id (cp_parser* parser,
1902 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
1905 error ("`%T' is not a template", type);
1906 else if (TREE_CODE (type) == IDENTIFIER_NODE)
1907 error ("`%E' is not a template", type);
1909 error ("invalid template-id");
1910 /* Remember the location of the invalid "<". */
1911 if (cp_parser_parsing_tentatively (parser)
1912 && !cp_parser_committed_to_tentative_parse (parser))
1914 token = cp_lexer_peek_token (parser->lexer);
1915 token = cp_lexer_prev_token (parser->lexer, token);
1916 start = cp_lexer_token_difference (parser->lexer,
1917 parser->lexer->first_token,
1922 /* Consume the "<". */
1923 cp_lexer_consume_token (parser->lexer);
1924 /* Parse the template arguments. */
1925 cp_parser_enclosed_template_argument_list (parser);
1926 /* Permanently remove the invalid template arguments so that
1927 this error message is not issued again. */
1930 token = cp_lexer_advance_token (parser->lexer,
1931 parser->lexer->first_token,
1933 cp_lexer_purge_tokens_after (parser->lexer, token);
1938 /* If parsing an integral constant-expression, issue an error message
1939 about the fact that THING appeared and return true. Otherwise,
1940 return false, marking the current expression as non-constant. */
1943 cp_parser_non_integral_constant_expression (cp_parser *parser,
1946 if (parser->integral_constant_expression_p)
1948 if (!parser->allow_non_integral_constant_expression_p)
1950 error ("%s cannot appear in a constant-expression", thing);
1953 parser->non_integral_constant_expression_p = true;
1958 /* Emit a diagnostic for an invalid type name. Consider also if it is
1959 qualified or not and the result of a lookup, to provide a better
1963 cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree scope, tree id)
1965 tree decl, old_scope;
1966 /* Try to lookup the identifier. */
1967 old_scope = parser->scope;
1968 parser->scope = scope;
1969 decl = cp_parser_lookup_name_simple (parser, id);
1970 parser->scope = old_scope;
1971 /* If the lookup found a template-name, it means that the user forgot
1972 to specify an argument list. Emit an useful error message. */
1973 if (TREE_CODE (decl) == TEMPLATE_DECL)
1974 error ("invalid use of template-name `%E' without an argument list",
1976 else if (!parser->scope)
1978 /* Issue an error message. */
1979 error ("`%E' does not name a type", id);
1980 /* If we're in a template class, it's possible that the user was
1981 referring to a type from a base class. For example:
1983 template <typename T> struct A { typedef T X; };
1984 template <typename T> struct B : public A<T> { X x; };
1986 The user should have said "typename A<T>::X". */
1987 if (processing_template_decl && current_class_type)
1991 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
1995 tree base_type = BINFO_TYPE (b);
1996 if (CLASS_TYPE_P (base_type)
1997 && dependent_type_p (base_type))
2000 /* Go from a particular instantiation of the
2001 template (which will have an empty TYPE_FIELDs),
2002 to the main version. */
2003 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2004 for (field = TYPE_FIELDS (base_type);
2006 field = TREE_CHAIN (field))
2007 if (TREE_CODE (field) == TYPE_DECL
2008 && DECL_NAME (field) == id)
2010 inform ("(perhaps `typename %T::%E' was intended)",
2011 BINFO_TYPE (b), id);
2020 /* Here we diagnose qualified-ids where the scope is actually correct,
2021 but the identifier does not resolve to a valid type name. */
2024 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2025 error ("`%E' in namespace `%E' does not name a type",
2027 else if (TYPE_P (parser->scope))
2028 error ("`%E' in class `%T' does not name a type",
2035 /* Check for a common situation where a type-name should be present,
2036 but is not, and issue a sensible error message. Returns true if an
2037 invalid type-name was detected.
2039 The situation handled by this function are variable declarations of the
2040 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2041 Usually, `ID' should name a type, but if we got here it means that it
2042 does not. We try to emit the best possible error message depending on
2043 how exactly the id-expression looks like.
2047 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2051 cp_parser_parse_tentatively (parser);
2052 id = cp_parser_id_expression (parser,
2053 /*template_keyword_p=*/false,
2054 /*check_dependency_p=*/true,
2055 /*template_p=*/NULL,
2056 /*declarator_p=*/true);
2057 /* After the id-expression, there should be a plain identifier,
2058 otherwise this is not a simple variable declaration. Also, if
2059 the scope is dependent, we cannot do much. */
2060 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME)
2061 || (parser->scope && TYPE_P (parser->scope)
2062 && dependent_type_p (parser->scope)))
2064 cp_parser_abort_tentative_parse (parser);
2067 if (!cp_parser_parse_definitely (parser))
2070 /* If we got here, this cannot be a valid variable declaration, thus
2071 the cp_parser_id_expression must have resolved to a plain identifier
2072 node (not a TYPE_DECL or TEMPLATE_ID_EXPR). */
2073 my_friendly_assert (TREE_CODE (id) == IDENTIFIER_NODE, 20030203);
2074 /* Emit a diagnostic for the invalid type. */
2075 cp_parser_diagnose_invalid_type_name (parser, parser->scope, id);
2076 /* Skip to the end of the declaration; there's no point in
2077 trying to process it. */
2078 cp_parser_skip_to_end_of_block_or_statement (parser);
2082 /* Consume tokens up to, and including, the next non-nested closing `)'.
2083 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
2084 are doing error recovery. Returns -1 if OR_COMMA is true and we
2085 found an unnested comma. */
2088 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2093 unsigned paren_depth = 0;
2094 unsigned brace_depth = 0;
2095 int saved_c_lex_string_translate = c_lex_string_translate;
2098 if (recovering && !or_comma && cp_parser_parsing_tentatively (parser)
2099 && !cp_parser_committed_to_tentative_parse (parser))
2103 /* If we're looking ahead, keep both translated and untranslated
2105 c_lex_string_translate = -1;
2111 /* If we've run out of tokens, then there is no closing `)'. */
2112 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2118 token = cp_lexer_peek_token (parser->lexer);
2120 /* This matches the processing in skip_to_end_of_statement. */
2121 if (token->type == CPP_SEMICOLON && !brace_depth)
2126 if (token->type == CPP_OPEN_BRACE)
2128 if (token->type == CPP_CLOSE_BRACE)
2136 if (recovering && or_comma && token->type == CPP_COMMA
2137 && !brace_depth && !paren_depth)
2145 /* If it is an `(', we have entered another level of nesting. */
2146 if (token->type == CPP_OPEN_PAREN)
2148 /* If it is a `)', then we might be done. */
2149 else if (token->type == CPP_CLOSE_PAREN && !paren_depth--)
2152 cp_lexer_consume_token (parser->lexer);
2160 /* Consume the token. */
2161 cp_lexer_consume_token (parser->lexer);
2164 c_lex_string_translate = saved_c_lex_string_translate;
2168 /* Consume tokens until we reach the end of the current statement.
2169 Normally, that will be just before consuming a `;'. However, if a
2170 non-nested `}' comes first, then we stop before consuming that. */
2173 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2175 unsigned nesting_depth = 0;
2181 /* Peek at the next token. */
2182 token = cp_lexer_peek_token (parser->lexer);
2183 /* If we've run out of tokens, stop. */
2184 if (token->type == CPP_EOF)
2186 /* If the next token is a `;', we have reached the end of the
2188 if (token->type == CPP_SEMICOLON && !nesting_depth)
2190 /* If the next token is a non-nested `}', then we have reached
2191 the end of the current block. */
2192 if (token->type == CPP_CLOSE_BRACE)
2194 /* If this is a non-nested `}', stop before consuming it.
2195 That way, when confronted with something like:
2199 we stop before consuming the closing `}', even though we
2200 have not yet reached a `;'. */
2201 if (nesting_depth == 0)
2203 /* If it is the closing `}' for a block that we have
2204 scanned, stop -- but only after consuming the token.
2210 we will stop after the body of the erroneously declared
2211 function, but before consuming the following `typedef'
2213 if (--nesting_depth == 0)
2215 cp_lexer_consume_token (parser->lexer);
2219 /* If it the next token is a `{', then we are entering a new
2220 block. Consume the entire block. */
2221 else if (token->type == CPP_OPEN_BRACE)
2223 /* Consume the token. */
2224 cp_lexer_consume_token (parser->lexer);
2228 /* This function is called at the end of a statement or declaration.
2229 If the next token is a semicolon, it is consumed; otherwise, error
2230 recovery is attempted. */
2233 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2235 /* Look for the trailing `;'. */
2236 if (!cp_parser_require (parser, CPP_SEMICOLON, "`;'"))
2238 /* If there is additional (erroneous) input, skip to the end of
2240 cp_parser_skip_to_end_of_statement (parser);
2241 /* If the next token is now a `;', consume it. */
2242 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2243 cp_lexer_consume_token (parser->lexer);
2247 /* Skip tokens until we have consumed an entire block, or until we
2248 have consumed a non-nested `;'. */
2251 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2253 unsigned nesting_depth = 0;
2259 /* Peek at the next token. */
2260 token = cp_lexer_peek_token (parser->lexer);
2261 /* If we've run out of tokens, stop. */
2262 if (token->type == CPP_EOF)
2264 /* If the next token is a `;', we have reached the end of the
2266 if (token->type == CPP_SEMICOLON && !nesting_depth)
2268 /* Consume the `;'. */
2269 cp_lexer_consume_token (parser->lexer);
2272 /* Consume the token. */
2273 token = cp_lexer_consume_token (parser->lexer);
2274 /* If the next token is a non-nested `}', then we have reached
2275 the end of the current block. */
2276 if (token->type == CPP_CLOSE_BRACE
2277 && (nesting_depth == 0 || --nesting_depth == 0))
2279 /* If it the next token is a `{', then we are entering a new
2280 block. Consume the entire block. */
2281 if (token->type == CPP_OPEN_BRACE)
2286 /* Skip tokens until a non-nested closing curly brace is the next
2290 cp_parser_skip_to_closing_brace (cp_parser *parser)
2292 unsigned nesting_depth = 0;
2298 /* Peek at the next token. */
2299 token = cp_lexer_peek_token (parser->lexer);
2300 /* If we've run out of tokens, stop. */
2301 if (token->type == CPP_EOF)
2303 /* If the next token is a non-nested `}', then we have reached
2304 the end of the current block. */
2305 if (token->type == CPP_CLOSE_BRACE && nesting_depth-- == 0)
2307 /* If it the next token is a `{', then we are entering a new
2308 block. Consume the entire block. */
2309 else if (token->type == CPP_OPEN_BRACE)
2311 /* Consume the token. */
2312 cp_lexer_consume_token (parser->lexer);
2316 /* This is a simple wrapper around make_typename_type. When the id is
2317 an unresolved identifier node, we can provide a superior diagnostic
2318 using cp_parser_diagnose_invalid_type_name. */
2321 cp_parser_make_typename_type (cp_parser *parser, tree scope, tree id)
2324 if (TREE_CODE (id) == IDENTIFIER_NODE)
2326 result = make_typename_type (scope, id, /*complain=*/0);
2327 if (result == error_mark_node)
2328 cp_parser_diagnose_invalid_type_name (parser, scope, id);
2331 return make_typename_type (scope, id, tf_error);
2335 /* Create a new C++ parser. */
2338 cp_parser_new (void)
2343 /* cp_lexer_new_main is called before calling ggc_alloc because
2344 cp_lexer_new_main might load a PCH file. */
2345 lexer = cp_lexer_new_main ();
2347 parser = ggc_alloc_cleared (sizeof (cp_parser));
2348 parser->lexer = lexer;
2349 parser->context = cp_parser_context_new (NULL);
2351 /* For now, we always accept GNU extensions. */
2352 parser->allow_gnu_extensions_p = 1;
2354 /* The `>' token is a greater-than operator, not the end of a
2356 parser->greater_than_is_operator_p = true;
2358 parser->default_arg_ok_p = true;
2360 /* We are not parsing a constant-expression. */
2361 parser->integral_constant_expression_p = false;
2362 parser->allow_non_integral_constant_expression_p = false;
2363 parser->non_integral_constant_expression_p = false;
2365 /* Local variable names are not forbidden. */
2366 parser->local_variables_forbidden_p = false;
2368 /* We are not processing an `extern "C"' declaration. */
2369 parser->in_unbraced_linkage_specification_p = false;
2371 /* We are not processing a declarator. */
2372 parser->in_declarator_p = false;
2374 /* We are not processing a template-argument-list. */
2375 parser->in_template_argument_list_p = false;
2377 /* We are not in an iteration statement. */
2378 parser->in_iteration_statement_p = false;
2380 /* We are not in a switch statement. */
2381 parser->in_switch_statement_p = false;
2383 /* We are not parsing a type-id inside an expression. */
2384 parser->in_type_id_in_expr_p = false;
2386 /* The unparsed function queue is empty. */
2387 parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2389 /* There are no classes being defined. */
2390 parser->num_classes_being_defined = 0;
2392 /* No template parameters apply. */
2393 parser->num_template_parameter_lists = 0;
2398 /* Lexical conventions [gram.lex] */
2400 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
2404 cp_parser_identifier (cp_parser* parser)
2408 /* Look for the identifier. */
2409 token = cp_parser_require (parser, CPP_NAME, "identifier");
2410 /* Return the value. */
2411 return token ? token->value : error_mark_node;
2414 /* Basic concepts [gram.basic] */
2416 /* Parse a translation-unit.
2419 declaration-seq [opt]
2421 Returns TRUE if all went well. */
2424 cp_parser_translation_unit (cp_parser* parser)
2428 cp_parser_declaration_seq_opt (parser);
2430 /* If there are no tokens left then all went well. */
2431 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2434 /* Otherwise, issue an error message. */
2435 cp_parser_error (parser, "expected declaration");
2439 /* Consume the EOF token. */
2440 cp_parser_require (parser, CPP_EOF, "end-of-file");
2443 finish_translation_unit ();
2445 /* All went well. */
2449 /* Expressions [gram.expr] */
2451 /* Parse a primary-expression.
2462 ( compound-statement )
2463 __builtin_va_arg ( assignment-expression , type-id )
2468 Returns a representation of the expression.
2470 *IDK indicates what kind of id-expression (if any) was present.
2472 *QUALIFYING_CLASS is set to a non-NULL value if the id-expression can be
2473 used as the operand of a pointer-to-member. In that case,
2474 *QUALIFYING_CLASS gives the class that is used as the qualifying
2475 class in the pointer-to-member. */
2478 cp_parser_primary_expression (cp_parser *parser,
2480 tree *qualifying_class)
2484 /* Assume the primary expression is not an id-expression. */
2485 *idk = CP_ID_KIND_NONE;
2486 /* And that it cannot be used as pointer-to-member. */
2487 *qualifying_class = NULL_TREE;
2489 /* Peek at the next token. */
2490 token = cp_lexer_peek_token (parser->lexer);
2491 switch (token->type)
2502 token = cp_lexer_consume_token (parser->lexer);
2503 return token->value;
2507 token = cp_lexer_consume_token (parser->lexer);
2508 if (TREE_CHAIN (token->value))
2509 return TREE_CHAIN (token->value);
2511 return token->value;
2513 case CPP_OPEN_PAREN:
2516 bool saved_greater_than_is_operator_p;
2518 /* Consume the `('. */
2519 cp_lexer_consume_token (parser->lexer);
2520 /* Within a parenthesized expression, a `>' token is always
2521 the greater-than operator. */
2522 saved_greater_than_is_operator_p
2523 = parser->greater_than_is_operator_p;
2524 parser->greater_than_is_operator_p = true;
2525 /* If we see `( { ' then we are looking at the beginning of
2526 a GNU statement-expression. */
2527 if (cp_parser_allow_gnu_extensions_p (parser)
2528 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
2530 /* Statement-expressions are not allowed by the standard. */
2532 pedwarn ("ISO C++ forbids braced-groups within expressions");
2534 /* And they're not allowed outside of a function-body; you
2535 cannot, for example, write:
2537 int i = ({ int j = 3; j + 1; });
2539 at class or namespace scope. */
2540 if (!at_function_scope_p ())
2541 error ("statement-expressions are allowed only inside functions");
2542 /* Start the statement-expression. */
2543 expr = begin_stmt_expr ();
2544 /* Parse the compound-statement. */
2545 cp_parser_compound_statement (parser, expr, false);
2547 expr = finish_stmt_expr (expr, false);
2551 /* Parse the parenthesized expression. */
2552 expr = cp_parser_expression (parser);
2553 /* Let the front end know that this expression was
2554 enclosed in parentheses. This matters in case, for
2555 example, the expression is of the form `A::B', since
2556 `&A::B' might be a pointer-to-member, but `&(A::B)' is
2558 finish_parenthesized_expr (expr);
2560 /* The `>' token might be the end of a template-id or
2561 template-parameter-list now. */
2562 parser->greater_than_is_operator_p
2563 = saved_greater_than_is_operator_p;
2564 /* Consume the `)'. */
2565 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
2566 cp_parser_skip_to_end_of_statement (parser);
2572 switch (token->keyword)
2574 /* These two are the boolean literals. */
2576 cp_lexer_consume_token (parser->lexer);
2577 return boolean_true_node;
2579 cp_lexer_consume_token (parser->lexer);
2580 return boolean_false_node;
2582 /* The `__null' literal. */
2584 cp_lexer_consume_token (parser->lexer);
2587 /* Recognize the `this' keyword. */
2589 cp_lexer_consume_token (parser->lexer);
2590 if (parser->local_variables_forbidden_p)
2592 error ("`this' may not be used in this context");
2593 return error_mark_node;
2595 /* Pointers cannot appear in constant-expressions. */
2596 if (cp_parser_non_integral_constant_expression (parser,
2598 return error_mark_node;
2599 return finish_this_expr ();
2601 /* The `operator' keyword can be the beginning of an
2606 case RID_FUNCTION_NAME:
2607 case RID_PRETTY_FUNCTION_NAME:
2608 case RID_C99_FUNCTION_NAME:
2609 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
2610 __func__ are the names of variables -- but they are
2611 treated specially. Therefore, they are handled here,
2612 rather than relying on the generic id-expression logic
2613 below. Grammatically, these names are id-expressions.
2615 Consume the token. */
2616 token = cp_lexer_consume_token (parser->lexer);
2617 /* Look up the name. */
2618 return finish_fname (token->value);
2625 /* The `__builtin_va_arg' construct is used to handle
2626 `va_arg'. Consume the `__builtin_va_arg' token. */
2627 cp_lexer_consume_token (parser->lexer);
2628 /* Look for the opening `('. */
2629 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
2630 /* Now, parse the assignment-expression. */
2631 expression = cp_parser_assignment_expression (parser);
2632 /* Look for the `,'. */
2633 cp_parser_require (parser, CPP_COMMA, "`,'");
2634 /* Parse the type-id. */
2635 type = cp_parser_type_id (parser);
2636 /* Look for the closing `)'. */
2637 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
2638 /* Using `va_arg' in a constant-expression is not
2640 if (cp_parser_non_integral_constant_expression (parser,
2642 return error_mark_node;
2643 return build_x_va_arg (expression, type);
2647 return cp_parser_builtin_offsetof (parser);
2650 cp_parser_error (parser, "expected primary-expression");
2651 return error_mark_node;
2654 /* An id-expression can start with either an identifier, a
2655 `::' as the beginning of a qualified-id, or the "operator"
2659 case CPP_TEMPLATE_ID:
2660 case CPP_NESTED_NAME_SPECIFIER:
2664 const char *error_msg;
2667 /* Parse the id-expression. */
2669 = cp_parser_id_expression (parser,
2670 /*template_keyword_p=*/false,
2671 /*check_dependency_p=*/true,
2672 /*template_p=*/NULL,
2673 /*declarator_p=*/false);
2674 if (id_expression == error_mark_node)
2675 return error_mark_node;
2676 /* If we have a template-id, then no further lookup is
2677 required. If the template-id was for a template-class, we
2678 will sometimes have a TYPE_DECL at this point. */
2679 else if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
2680 || TREE_CODE (id_expression) == TYPE_DECL)
2681 decl = id_expression;
2682 /* Look up the name. */
2685 decl = cp_parser_lookup_name_simple (parser, id_expression);
2686 /* If name lookup gives us a SCOPE_REF, then the
2687 qualifying scope was dependent. Just propagate the
2689 if (TREE_CODE (decl) == SCOPE_REF)
2691 if (TYPE_P (TREE_OPERAND (decl, 0)))
2692 *qualifying_class = TREE_OPERAND (decl, 0);
2695 /* Check to see if DECL is a local variable in a context
2696 where that is forbidden. */
2697 if (parser->local_variables_forbidden_p
2698 && local_variable_p (decl))
2700 /* It might be that we only found DECL because we are
2701 trying to be generous with pre-ISO scoping rules.
2702 For example, consider:
2706 for (int i = 0; i < 10; ++i) {}
2707 extern void f(int j = i);
2710 Here, name look up will originally find the out
2711 of scope `i'. We need to issue a warning message,
2712 but then use the global `i'. */
2713 decl = check_for_out_of_scope_variable (decl);
2714 if (local_variable_p (decl))
2716 error ("local variable `%D' may not appear in this context",
2718 return error_mark_node;
2723 decl = finish_id_expression (id_expression, decl, parser->scope,
2724 idk, qualifying_class,
2725 parser->integral_constant_expression_p,
2726 parser->allow_non_integral_constant_expression_p,
2727 &parser->non_integral_constant_expression_p,
2730 cp_parser_error (parser, error_msg);
2734 /* Anything else is an error. */
2736 cp_parser_error (parser, "expected primary-expression");
2737 return error_mark_node;
2741 /* Parse an id-expression.
2748 :: [opt] nested-name-specifier template [opt] unqualified-id
2750 :: operator-function-id
2753 Return a representation of the unqualified portion of the
2754 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
2755 a `::' or nested-name-specifier.
2757 Often, if the id-expression was a qualified-id, the caller will
2758 want to make a SCOPE_REF to represent the qualified-id. This
2759 function does not do this in order to avoid wastefully creating
2760 SCOPE_REFs when they are not required.
2762 If TEMPLATE_KEYWORD_P is true, then we have just seen the
2765 If CHECK_DEPENDENCY_P is false, then names are looked up inside
2766 uninstantiated templates.
2768 If *TEMPLATE_P is non-NULL, it is set to true iff the
2769 `template' keyword is used to explicitly indicate that the entity
2770 named is a template.
2772 If DECLARATOR_P is true, the id-expression is appearing as part of
2773 a declarator, rather than as part of an expression. */
2776 cp_parser_id_expression (cp_parser *parser,
2777 bool template_keyword_p,
2778 bool check_dependency_p,
2782 bool global_scope_p;
2783 bool nested_name_specifier_p;
2785 /* Assume the `template' keyword was not used. */
2787 *template_p = false;
2789 /* Look for the optional `::' operator. */
2791 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
2793 /* Look for the optional nested-name-specifier. */
2794 nested_name_specifier_p
2795 = (cp_parser_nested_name_specifier_opt (parser,
2796 /*typename_keyword_p=*/false,
2799 /*is_declarator=*/false)
2801 /* If there is a nested-name-specifier, then we are looking at
2802 the first qualified-id production. */
2803 if (nested_name_specifier_p)
2806 tree saved_object_scope;
2807 tree saved_qualifying_scope;
2808 tree unqualified_id;
2811 /* See if the next token is the `template' keyword. */
2813 template_p = &is_template;
2814 *template_p = cp_parser_optional_template_keyword (parser);
2815 /* Name lookup we do during the processing of the
2816 unqualified-id might obliterate SCOPE. */
2817 saved_scope = parser->scope;
2818 saved_object_scope = parser->object_scope;
2819 saved_qualifying_scope = parser->qualifying_scope;
2820 /* Process the final unqualified-id. */
2821 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
2824 /* Restore the SAVED_SCOPE for our caller. */
2825 parser->scope = saved_scope;
2826 parser->object_scope = saved_object_scope;
2827 parser->qualifying_scope = saved_qualifying_scope;
2829 return unqualified_id;
2831 /* Otherwise, if we are in global scope, then we are looking at one
2832 of the other qualified-id productions. */
2833 else if (global_scope_p)
2838 /* Peek at the next token. */
2839 token = cp_lexer_peek_token (parser->lexer);
2841 /* If it's an identifier, and the next token is not a "<", then
2842 we can avoid the template-id case. This is an optimization
2843 for this common case. */
2844 if (token->type == CPP_NAME
2845 && !cp_parser_nth_token_starts_template_argument_list_p
2847 return cp_parser_identifier (parser);
2849 cp_parser_parse_tentatively (parser);
2850 /* Try a template-id. */
2851 id = cp_parser_template_id (parser,
2852 /*template_keyword_p=*/false,
2853 /*check_dependency_p=*/true,
2855 /* If that worked, we're done. */
2856 if (cp_parser_parse_definitely (parser))
2859 /* Peek at the next token. (Changes in the token buffer may
2860 have invalidated the pointer obtained above.) */
2861 token = cp_lexer_peek_token (parser->lexer);
2863 switch (token->type)
2866 return cp_parser_identifier (parser);
2869 if (token->keyword == RID_OPERATOR)
2870 return cp_parser_operator_function_id (parser);
2874 cp_parser_error (parser, "expected id-expression");
2875 return error_mark_node;
2879 return cp_parser_unqualified_id (parser, template_keyword_p,
2880 /*check_dependency_p=*/true,
2884 /* Parse an unqualified-id.
2888 operator-function-id
2889 conversion-function-id
2893 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
2894 keyword, in a construct like `A::template ...'.
2896 Returns a representation of unqualified-id. For the `identifier'
2897 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
2898 production a BIT_NOT_EXPR is returned; the operand of the
2899 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
2900 other productions, see the documentation accompanying the
2901 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
2902 names are looked up in uninstantiated templates. If DECLARATOR_P
2903 is true, the unqualified-id is appearing as part of a declarator,
2904 rather than as part of an expression. */
2907 cp_parser_unqualified_id (cp_parser* parser,
2908 bool template_keyword_p,
2909 bool check_dependency_p,
2914 /* Peek at the next token. */
2915 token = cp_lexer_peek_token (parser->lexer);
2917 switch (token->type)
2923 /* We don't know yet whether or not this will be a
2925 cp_parser_parse_tentatively (parser);
2926 /* Try a template-id. */
2927 id = cp_parser_template_id (parser, template_keyword_p,
2930 /* If it worked, we're done. */
2931 if (cp_parser_parse_definitely (parser))
2933 /* Otherwise, it's an ordinary identifier. */
2934 return cp_parser_identifier (parser);
2937 case CPP_TEMPLATE_ID:
2938 return cp_parser_template_id (parser, template_keyword_p,
2945 tree qualifying_scope;
2949 /* Consume the `~' token. */
2950 cp_lexer_consume_token (parser->lexer);
2951 /* Parse the class-name. The standard, as written, seems to
2954 template <typename T> struct S { ~S (); };
2955 template <typename T> S<T>::~S() {}
2957 is invalid, since `~' must be followed by a class-name, but
2958 `S<T>' is dependent, and so not known to be a class.
2959 That's not right; we need to look in uninstantiated
2960 templates. A further complication arises from:
2962 template <typename T> void f(T t) {
2966 Here, it is not possible to look up `T' in the scope of `T'
2967 itself. We must look in both the current scope, and the
2968 scope of the containing complete expression.
2970 Yet another issue is:
2979 The standard does not seem to say that the `S' in `~S'
2980 should refer to the type `S' and not the data member
2983 /* DR 244 says that we look up the name after the "~" in the
2984 same scope as we looked up the qualifying name. That idea
2985 isn't fully worked out; it's more complicated than that. */
2986 scope = parser->scope;
2987 object_scope = parser->object_scope;
2988 qualifying_scope = parser->qualifying_scope;
2990 /* If the name is of the form "X::~X" it's OK. */
2991 if (scope && TYPE_P (scope)
2992 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
2993 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
2995 && (cp_lexer_peek_token (parser->lexer)->value
2996 == TYPE_IDENTIFIER (scope)))
2998 cp_lexer_consume_token (parser->lexer);
2999 return build_nt (BIT_NOT_EXPR, scope);
3002 /* If there was an explicit qualification (S::~T), first look
3003 in the scope given by the qualification (i.e., S). */
3006 cp_parser_parse_tentatively (parser);
3007 type_decl = cp_parser_class_name (parser,
3008 /*typename_keyword_p=*/false,
3009 /*template_keyword_p=*/false,
3011 /*check_dependency=*/false,
3012 /*class_head_p=*/false,
3014 if (cp_parser_parse_definitely (parser))
3015 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3017 /* In "N::S::~S", look in "N" as well. */
3018 if (scope && qualifying_scope)
3020 cp_parser_parse_tentatively (parser);
3021 parser->scope = qualifying_scope;
3022 parser->object_scope = NULL_TREE;
3023 parser->qualifying_scope = NULL_TREE;
3025 = cp_parser_class_name (parser,
3026 /*typename_keyword_p=*/false,
3027 /*template_keyword_p=*/false,
3029 /*check_dependency=*/false,
3030 /*class_head_p=*/false,
3032 if (cp_parser_parse_definitely (parser))
3033 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3035 /* In "p->S::~T", look in the scope given by "*p" as well. */
3036 else if (object_scope)
3038 cp_parser_parse_tentatively (parser);
3039 parser->scope = object_scope;
3040 parser->object_scope = NULL_TREE;
3041 parser->qualifying_scope = NULL_TREE;
3043 = cp_parser_class_name (parser,
3044 /*typename_keyword_p=*/false,
3045 /*template_keyword_p=*/false,
3047 /*check_dependency=*/false,
3048 /*class_head_p=*/false,
3050 if (cp_parser_parse_definitely (parser))
3051 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3053 /* Look in the surrounding context. */
3054 parser->scope = NULL_TREE;
3055 parser->object_scope = NULL_TREE;
3056 parser->qualifying_scope = NULL_TREE;
3058 = cp_parser_class_name (parser,
3059 /*typename_keyword_p=*/false,
3060 /*template_keyword_p=*/false,
3062 /*check_dependency=*/false,
3063 /*class_head_p=*/false,
3065 /* If an error occurred, assume that the name of the
3066 destructor is the same as the name of the qualifying
3067 class. That allows us to keep parsing after running
3068 into ill-formed destructor names. */
3069 if (type_decl == error_mark_node && scope && TYPE_P (scope))
3070 return build_nt (BIT_NOT_EXPR, scope);
3071 else if (type_decl == error_mark_node)
3072 return error_mark_node;
3076 A typedef-name that names a class shall not be used as the
3077 identifier in the declarator for a destructor declaration. */
3079 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
3080 && !DECL_SELF_REFERENCE_P (type_decl))
3081 error ("typedef-name `%D' used as destructor declarator",
3084 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3088 if (token->keyword == RID_OPERATOR)
3092 /* This could be a template-id, so we try that first. */
3093 cp_parser_parse_tentatively (parser);
3094 /* Try a template-id. */
3095 id = cp_parser_template_id (parser, template_keyword_p,
3096 /*check_dependency_p=*/true,
3098 /* If that worked, we're done. */
3099 if (cp_parser_parse_definitely (parser))
3101 /* We still don't know whether we're looking at an
3102 operator-function-id or a conversion-function-id. */
3103 cp_parser_parse_tentatively (parser);
3104 /* Try an operator-function-id. */
3105 id = cp_parser_operator_function_id (parser);
3106 /* If that didn't work, try a conversion-function-id. */
3107 if (!cp_parser_parse_definitely (parser))
3108 id = cp_parser_conversion_function_id (parser);
3115 cp_parser_error (parser, "expected unqualified-id");
3116 return error_mark_node;
3120 /* Parse an (optional) nested-name-specifier.
3122 nested-name-specifier:
3123 class-or-namespace-name :: nested-name-specifier [opt]
3124 class-or-namespace-name :: template nested-name-specifier [opt]
3126 PARSER->SCOPE should be set appropriately before this function is
3127 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
3128 effect. TYPE_P is TRUE if we non-type bindings should be ignored
3131 Sets PARSER->SCOPE to the class (TYPE) or namespace
3132 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
3133 it unchanged if there is no nested-name-specifier. Returns the new
3134 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
3136 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
3137 part of a declaration and/or decl-specifier. */
3140 cp_parser_nested_name_specifier_opt (cp_parser *parser,
3141 bool typename_keyword_p,
3142 bool check_dependency_p,
3144 bool is_declaration)
3146 bool success = false;
3147 tree access_check = NULL_TREE;
3151 /* If the next token corresponds to a nested name specifier, there
3152 is no need to reparse it. However, if CHECK_DEPENDENCY_P is
3153 false, it may have been true before, in which case something
3154 like `A<X>::B<Y>::C' may have resulted in a nested-name-specifier
3155 of `A<X>::', where it should now be `A<X>::B<Y>::'. So, when
3156 CHECK_DEPENDENCY_P is false, we have to fall through into the
3158 if (check_dependency_p
3159 && cp_lexer_next_token_is (parser->lexer, CPP_NESTED_NAME_SPECIFIER))
3161 cp_parser_pre_parsed_nested_name_specifier (parser);
3162 return parser->scope;
3165 /* Remember where the nested-name-specifier starts. */
3166 if (cp_parser_parsing_tentatively (parser)
3167 && !cp_parser_committed_to_tentative_parse (parser))
3169 token = cp_lexer_peek_token (parser->lexer);
3170 start = cp_lexer_token_difference (parser->lexer,
3171 parser->lexer->first_token,
3177 push_deferring_access_checks (dk_deferred);
3183 tree saved_qualifying_scope;
3184 bool template_keyword_p;
3186 /* Spot cases that cannot be the beginning of a
3187 nested-name-specifier. */
3188 token = cp_lexer_peek_token (parser->lexer);
3190 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
3191 the already parsed nested-name-specifier. */
3192 if (token->type == CPP_NESTED_NAME_SPECIFIER)
3194 /* Grab the nested-name-specifier and continue the loop. */
3195 cp_parser_pre_parsed_nested_name_specifier (parser);
3200 /* Spot cases that cannot be the beginning of a
3201 nested-name-specifier. On the second and subsequent times
3202 through the loop, we look for the `template' keyword. */
3203 if (success && token->keyword == RID_TEMPLATE)
3205 /* A template-id can start a nested-name-specifier. */
3206 else if (token->type == CPP_TEMPLATE_ID)
3210 /* If the next token is not an identifier, then it is
3211 definitely not a class-or-namespace-name. */
3212 if (token->type != CPP_NAME)
3214 /* If the following token is neither a `<' (to begin a
3215 template-id), nor a `::', then we are not looking at a
3216 nested-name-specifier. */
3217 token = cp_lexer_peek_nth_token (parser->lexer, 2);
3218 if (token->type != CPP_SCOPE
3219 && !cp_parser_nth_token_starts_template_argument_list_p
3224 /* The nested-name-specifier is optional, so we parse
3226 cp_parser_parse_tentatively (parser);
3228 /* Look for the optional `template' keyword, if this isn't the
3229 first time through the loop. */
3231 template_keyword_p = cp_parser_optional_template_keyword (parser);
3233 template_keyword_p = false;
3235 /* Save the old scope since the name lookup we are about to do
3236 might destroy it. */
3237 old_scope = parser->scope;
3238 saved_qualifying_scope = parser->qualifying_scope;
3239 /* Parse the qualifying entity. */
3241 = cp_parser_class_or_namespace_name (parser,
3247 /* Look for the `::' token. */
3248 cp_parser_require (parser, CPP_SCOPE, "`::'");
3250 /* If we found what we wanted, we keep going; otherwise, we're
3252 if (!cp_parser_parse_definitely (parser))
3254 bool error_p = false;
3256 /* Restore the OLD_SCOPE since it was valid before the
3257 failed attempt at finding the last
3258 class-or-namespace-name. */
3259 parser->scope = old_scope;
3260 parser->qualifying_scope = saved_qualifying_scope;
3261 /* If the next token is an identifier, and the one after
3262 that is a `::', then any valid interpretation would have
3263 found a class-or-namespace-name. */
3264 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
3265 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3267 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
3270 token = cp_lexer_consume_token (parser->lexer);
3275 decl = cp_parser_lookup_name_simple (parser, token->value);
3276 if (TREE_CODE (decl) == TEMPLATE_DECL)
3277 error ("`%D' used without template parameters",
3280 cp_parser_name_lookup_error
3281 (parser, token->value, decl,
3282 "is not a class or namespace");
3283 parser->scope = NULL_TREE;
3285 /* Treat this as a successful nested-name-specifier
3290 If the name found is not a class-name (clause
3291 _class_) or namespace-name (_namespace.def_), the
3292 program is ill-formed. */
3295 cp_lexer_consume_token (parser->lexer);
3300 /* We've found one valid nested-name-specifier. */
3302 /* Make sure we look in the right scope the next time through
3304 parser->scope = (TREE_CODE (new_scope) == TYPE_DECL
3305 ? TREE_TYPE (new_scope)
3307 /* If it is a class scope, try to complete it; we are about to
3308 be looking up names inside the class. */
3309 if (TYPE_P (parser->scope)
3310 /* Since checking types for dependency can be expensive,
3311 avoid doing it if the type is already complete. */
3312 && !COMPLETE_TYPE_P (parser->scope)
3313 /* Do not try to complete dependent types. */
3314 && !dependent_type_p (parser->scope))
3315 complete_type (parser->scope);
3318 /* Retrieve any deferred checks. Do not pop this access checks yet
3319 so the memory will not be reclaimed during token replacing below. */
3320 access_check = get_deferred_access_checks ();
3322 /* If parsing tentatively, replace the sequence of tokens that makes
3323 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
3324 token. That way, should we re-parse the token stream, we will
3325 not have to repeat the effort required to do the parse, nor will
3326 we issue duplicate error messages. */
3327 if (success && start >= 0)
3329 /* Find the token that corresponds to the start of the
3331 token = cp_lexer_advance_token (parser->lexer,
3332 parser->lexer->first_token,
3335 /* Reset the contents of the START token. */
3336 token->type = CPP_NESTED_NAME_SPECIFIER;
3337 token->value = build_tree_list (access_check, parser->scope);
3338 TREE_TYPE (token->value) = parser->qualifying_scope;
3339 token->keyword = RID_MAX;
3340 /* Purge all subsequent tokens. */
3341 cp_lexer_purge_tokens_after (parser->lexer, token);
3344 pop_deferring_access_checks ();
3345 return success ? parser->scope : NULL_TREE;
3348 /* Parse a nested-name-specifier. See
3349 cp_parser_nested_name_specifier_opt for details. This function
3350 behaves identically, except that it will an issue an error if no
3351 nested-name-specifier is present, and it will return
3352 ERROR_MARK_NODE, rather than NULL_TREE, if no nested-name-specifier
3356 cp_parser_nested_name_specifier (cp_parser *parser,
3357 bool typename_keyword_p,
3358 bool check_dependency_p,
3360 bool is_declaration)
3364 /* Look for the nested-name-specifier. */
3365 scope = cp_parser_nested_name_specifier_opt (parser,
3370 /* If it was not present, issue an error message. */
3373 cp_parser_error (parser, "expected nested-name-specifier");
3374 parser->scope = NULL_TREE;
3375 return error_mark_node;
3381 /* Parse a class-or-namespace-name.
3383 class-or-namespace-name:
3387 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
3388 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
3389 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
3390 TYPE_P is TRUE iff the next name should be taken as a class-name,
3391 even the same name is declared to be another entity in the same
3394 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
3395 specified by the class-or-namespace-name. If neither is found the
3396 ERROR_MARK_NODE is returned. */
3399 cp_parser_class_or_namespace_name (cp_parser *parser,
3400 bool typename_keyword_p,
3401 bool template_keyword_p,
3402 bool check_dependency_p,
3404 bool is_declaration)
3407 tree saved_qualifying_scope;
3408 tree saved_object_scope;
3412 /* Before we try to parse the class-name, we must save away the
3413 current PARSER->SCOPE since cp_parser_class_name will destroy
3415 saved_scope = parser->scope;
3416 saved_qualifying_scope = parser->qualifying_scope;
3417 saved_object_scope = parser->object_scope;
3418 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
3419 there is no need to look for a namespace-name. */
3420 only_class_p = template_keyword_p || (saved_scope && TYPE_P (saved_scope));
3422 cp_parser_parse_tentatively (parser);
3423 scope = cp_parser_class_name (parser,
3428 /*class_head_p=*/false,
3430 /* If that didn't work, try for a namespace-name. */
3431 if (!only_class_p && !cp_parser_parse_definitely (parser))
3433 /* Restore the saved scope. */
3434 parser->scope = saved_scope;
3435 parser->qualifying_scope = saved_qualifying_scope;
3436 parser->object_scope = saved_object_scope;
3437 /* If we are not looking at an identifier followed by the scope
3438 resolution operator, then this is not part of a
3439 nested-name-specifier. (Note that this function is only used
3440 to parse the components of a nested-name-specifier.) */
3441 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
3442 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
3443 return error_mark_node;
3444 scope = cp_parser_namespace_name (parser);
3450 /* Parse a postfix-expression.
3454 postfix-expression [ expression ]
3455 postfix-expression ( expression-list [opt] )
3456 simple-type-specifier ( expression-list [opt] )
3457 typename :: [opt] nested-name-specifier identifier
3458 ( expression-list [opt] )
3459 typename :: [opt] nested-name-specifier template [opt] template-id
3460 ( expression-list [opt] )
3461 postfix-expression . template [opt] id-expression
3462 postfix-expression -> template [opt] id-expression
3463 postfix-expression . pseudo-destructor-name
3464 postfix-expression -> pseudo-destructor-name
3465 postfix-expression ++
3466 postfix-expression --
3467 dynamic_cast < type-id > ( expression )
3468 static_cast < type-id > ( expression )
3469 reinterpret_cast < type-id > ( expression )
3470 const_cast < type-id > ( expression )
3471 typeid ( expression )
3477 ( type-id ) { initializer-list , [opt] }
3479 This extension is a GNU version of the C99 compound-literal
3480 construct. (The C99 grammar uses `type-name' instead of `type-id',
3481 but they are essentially the same concept.)
3483 If ADDRESS_P is true, the postfix expression is the operand of the
3486 Returns a representation of the expression. */
3489 cp_parser_postfix_expression (cp_parser *parser, bool address_p)
3493 cp_id_kind idk = CP_ID_KIND_NONE;
3494 tree postfix_expression = NULL_TREE;
3495 /* Non-NULL only if the current postfix-expression can be used to
3496 form a pointer-to-member. In that case, QUALIFYING_CLASS is the
3497 class used to qualify the member. */
3498 tree qualifying_class = NULL_TREE;
3500 /* Peek at the next token. */
3501 token = cp_lexer_peek_token (parser->lexer);
3502 /* Some of the productions are determined by keywords. */
3503 keyword = token->keyword;
3513 const char *saved_message;
3515 /* All of these can be handled in the same way from the point
3516 of view of parsing. Begin by consuming the token
3517 identifying the cast. */
3518 cp_lexer_consume_token (parser->lexer);
3520 /* New types cannot be defined in the cast. */
3521 saved_message = parser->type_definition_forbidden_message;
3522 parser->type_definition_forbidden_message
3523 = "types may not be defined in casts";
3525 /* Look for the opening `<'. */
3526 cp_parser_require (parser, CPP_LESS, "`<'");
3527 /* Parse the type to which we are casting. */
3528 type = cp_parser_type_id (parser);
3529 /* Look for the closing `>'. */
3530 cp_parser_require (parser, CPP_GREATER, "`>'");
3531 /* Restore the old message. */
3532 parser->type_definition_forbidden_message = saved_message;
3534 /* And the expression which is being cast. */
3535 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3536 expression = cp_parser_expression (parser);
3537 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3539 /* Only type conversions to integral or enumeration types
3540 can be used in constant-expressions. */
3541 if (parser->integral_constant_expression_p
3542 && !dependent_type_p (type)
3543 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type)
3544 && (cp_parser_non_integral_constant_expression
3546 "a cast to a type other than an integral or "
3547 "enumeration type")))
3548 return error_mark_node;
3554 = build_dynamic_cast (type, expression);
3558 = build_static_cast (type, expression);
3562 = build_reinterpret_cast (type, expression);
3566 = build_const_cast (type, expression);
3577 const char *saved_message;
3578 bool saved_in_type_id_in_expr_p;
3580 /* Consume the `typeid' token. */
3581 cp_lexer_consume_token (parser->lexer);
3582 /* Look for the `(' token. */
3583 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3584 /* Types cannot be defined in a `typeid' expression. */
3585 saved_message = parser->type_definition_forbidden_message;
3586 parser->type_definition_forbidden_message
3587 = "types may not be defined in a `typeid\' expression";
3588 /* We can't be sure yet whether we're looking at a type-id or an
3590 cp_parser_parse_tentatively (parser);
3591 /* Try a type-id first. */
3592 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
3593 parser->in_type_id_in_expr_p = true;
3594 type = cp_parser_type_id (parser);
3595 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
3596 /* Look for the `)' token. Otherwise, we can't be sure that
3597 we're not looking at an expression: consider `typeid (int
3598 (3))', for example. */
3599 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3600 /* If all went well, simply lookup the type-id. */
3601 if (cp_parser_parse_definitely (parser))
3602 postfix_expression = get_typeid (type);
3603 /* Otherwise, fall back to the expression variant. */
3608 /* Look for an expression. */
3609 expression = cp_parser_expression (parser);
3610 /* Compute its typeid. */
3611 postfix_expression = build_typeid (expression);
3612 /* Look for the `)' token. */
3613 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3615 /* `typeid' may not appear in an integral constant expression. */
3616 if (cp_parser_non_integral_constant_expression(parser,
3617 "`typeid' operator"))
3618 return error_mark_node;
3619 /* Restore the saved message. */
3620 parser->type_definition_forbidden_message = saved_message;
3626 bool template_p = false;
3630 /* Consume the `typename' token. */
3631 cp_lexer_consume_token (parser->lexer);
3632 /* Look for the optional `::' operator. */
3633 cp_parser_global_scope_opt (parser,
3634 /*current_scope_valid_p=*/false);
3635 /* Look for the nested-name-specifier. */
3636 cp_parser_nested_name_specifier (parser,
3637 /*typename_keyword_p=*/true,
3638 /*check_dependency_p=*/true,
3640 /*is_declaration=*/true);
3641 /* Look for the optional `template' keyword. */
3642 template_p = cp_parser_optional_template_keyword (parser);
3643 /* We don't know whether we're looking at a template-id or an
3645 cp_parser_parse_tentatively (parser);
3646 /* Try a template-id. */
3647 id = cp_parser_template_id (parser, template_p,
3648 /*check_dependency_p=*/true,
3649 /*is_declaration=*/true);
3650 /* If that didn't work, try an identifier. */
3651 if (!cp_parser_parse_definitely (parser))
3652 id = cp_parser_identifier (parser);
3653 /* If we look up a template-id in a non-dependent qualifying
3654 scope, there's no need to create a dependent type. */
3655 if (TREE_CODE (id) == TYPE_DECL
3656 && !dependent_type_p (parser->scope))
3657 type = TREE_TYPE (id);
3658 /* Create a TYPENAME_TYPE to represent the type to which the
3659 functional cast is being performed. */
3661 type = make_typename_type (parser->scope, id,
3664 postfix_expression = cp_parser_functional_cast (parser, type);
3672 /* If the next thing is a simple-type-specifier, we may be
3673 looking at a functional cast. We could also be looking at
3674 an id-expression. So, we try the functional cast, and if
3675 that doesn't work we fall back to the primary-expression. */
3676 cp_parser_parse_tentatively (parser);
3677 /* Look for the simple-type-specifier. */
3678 type = cp_parser_simple_type_specifier (parser,
3679 CP_PARSER_FLAGS_NONE,
3680 /*identifier_p=*/false);
3681 /* Parse the cast itself. */
3682 if (!cp_parser_error_occurred (parser))
3684 = cp_parser_functional_cast (parser, type);
3685 /* If that worked, we're done. */
3686 if (cp_parser_parse_definitely (parser))
3689 /* If the functional-cast didn't work out, try a
3690 compound-literal. */
3691 if (cp_parser_allow_gnu_extensions_p (parser)
3692 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
3694 tree initializer_list = NULL_TREE;
3695 bool saved_in_type_id_in_expr_p;
3697 cp_parser_parse_tentatively (parser);
3698 /* Consume the `('. */
3699 cp_lexer_consume_token (parser->lexer);
3700 /* Parse the type. */
3701 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
3702 parser->in_type_id_in_expr_p = true;
3703 type = cp_parser_type_id (parser);
3704 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
3705 /* Look for the `)'. */
3706 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3707 /* Look for the `{'. */
3708 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
3709 /* If things aren't going well, there's no need to
3711 if (!cp_parser_error_occurred (parser))
3713 bool non_constant_p;
3714 /* Parse the initializer-list. */
3716 = cp_parser_initializer_list (parser, &non_constant_p);
3717 /* Allow a trailing `,'. */
3718 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
3719 cp_lexer_consume_token (parser->lexer);
3720 /* Look for the final `}'. */
3721 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
3723 /* If that worked, we're definitely looking at a
3724 compound-literal expression. */
3725 if (cp_parser_parse_definitely (parser))
3727 /* Warn the user that a compound literal is not
3728 allowed in standard C++. */
3730 pedwarn ("ISO C++ forbids compound-literals");
3731 /* Form the representation of the compound-literal. */
3733 = finish_compound_literal (type, initializer_list);
3738 /* It must be a primary-expression. */
3739 postfix_expression = cp_parser_primary_expression (parser,
3746 /* If we were avoiding committing to the processing of a
3747 qualified-id until we knew whether or not we had a
3748 pointer-to-member, we now know. */
3749 if (qualifying_class)
3753 /* Peek at the next token. */
3754 token = cp_lexer_peek_token (parser->lexer);
3755 done = (token->type != CPP_OPEN_SQUARE
3756 && token->type != CPP_OPEN_PAREN
3757 && token->type != CPP_DOT
3758 && token->type != CPP_DEREF
3759 && token->type != CPP_PLUS_PLUS
3760 && token->type != CPP_MINUS_MINUS);
3762 postfix_expression = finish_qualified_id_expr (qualifying_class,
3767 return postfix_expression;
3770 /* Keep looping until the postfix-expression is complete. */
3773 if (idk == CP_ID_KIND_UNQUALIFIED
3774 && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
3775 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
3776 /* It is not a Koenig lookup function call. */
3778 = unqualified_name_lookup_error (postfix_expression);
3780 /* Peek at the next token. */
3781 token = cp_lexer_peek_token (parser->lexer);
3783 switch (token->type)
3785 case CPP_OPEN_SQUARE:
3787 = cp_parser_postfix_open_square_expression (parser,
3790 idk = CP_ID_KIND_NONE;
3793 case CPP_OPEN_PAREN:
3794 /* postfix-expression ( expression-list [opt] ) */
3797 tree args = (cp_parser_parenthesized_expression_list
3798 (parser, false, /*non_constant_p=*/NULL));
3800 if (args == error_mark_node)
3802 postfix_expression = error_mark_node;
3806 /* Function calls are not permitted in
3807 constant-expressions. */
3808 if (cp_parser_non_integral_constant_expression (parser,
3811 postfix_expression = error_mark_node;
3816 if (idk == CP_ID_KIND_UNQUALIFIED)
3818 /* We do not perform argument-dependent lookup if
3819 normal lookup finds a non-function, in accordance
3820 with the expected resolution of DR 218. */
3822 && (is_overloaded_fn (postfix_expression)
3823 || TREE_CODE (postfix_expression) == IDENTIFIER_NODE))
3827 = perform_koenig_lookup (postfix_expression, args);
3829 else if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
3831 = unqualified_fn_lookup_error (postfix_expression);
3834 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
3836 tree instance = TREE_OPERAND (postfix_expression, 0);
3837 tree fn = TREE_OPERAND (postfix_expression, 1);
3839 if (processing_template_decl
3840 && (type_dependent_expression_p (instance)
3841 || (!BASELINK_P (fn)
3842 && TREE_CODE (fn) != FIELD_DECL)
3843 || type_dependent_expression_p (fn)
3844 || any_type_dependent_arguments_p (args)))
3847 = build_min_nt (CALL_EXPR, postfix_expression,
3852 if (BASELINK_P (fn))
3854 = (build_new_method_call
3855 (instance, fn, args, NULL_TREE,
3856 (idk == CP_ID_KIND_QUALIFIED
3857 ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL)));
3860 = finish_call_expr (postfix_expression, args,
3861 /*disallow_virtual=*/false,
3862 /*koenig_p=*/false);
3864 else if (TREE_CODE (postfix_expression) == OFFSET_REF
3865 || TREE_CODE (postfix_expression) == MEMBER_REF
3866 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
3867 postfix_expression = (build_offset_ref_call_from_tree
3868 (postfix_expression, args));
3869 else if (idk == CP_ID_KIND_QUALIFIED)
3870 /* A call to a static class member, or a namespace-scope
3873 = finish_call_expr (postfix_expression, args,
3874 /*disallow_virtual=*/true,
3877 /* All other function calls. */
3879 = finish_call_expr (postfix_expression, args,
3880 /*disallow_virtual=*/false,
3883 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
3884 idk = CP_ID_KIND_NONE;
3890 /* postfix-expression . template [opt] id-expression
3891 postfix-expression . pseudo-destructor-name
3892 postfix-expression -> template [opt] id-expression
3893 postfix-expression -> pseudo-destructor-name */
3895 /* Consume the `.' or `->' operator. */
3896 cp_lexer_consume_token (parser->lexer);
3899 = cp_parser_postfix_dot_deref_expression (parser, token->type,
3905 /* postfix-expression ++ */
3906 /* Consume the `++' token. */
3907 cp_lexer_consume_token (parser->lexer);
3908 /* Generate a representation for the complete expression. */
3910 = finish_increment_expr (postfix_expression,
3911 POSTINCREMENT_EXPR);
3912 /* Increments may not appear in constant-expressions. */
3913 if (cp_parser_non_integral_constant_expression (parser,
3915 postfix_expression = error_mark_node;
3916 idk = CP_ID_KIND_NONE;
3919 case CPP_MINUS_MINUS:
3920 /* postfix-expression -- */
3921 /* Consume the `--' token. */
3922 cp_lexer_consume_token (parser->lexer);
3923 /* Generate a representation for the complete expression. */
3925 = finish_increment_expr (postfix_expression,
3926 POSTDECREMENT_EXPR);
3927 /* Decrements may not appear in constant-expressions. */
3928 if (cp_parser_non_integral_constant_expression (parser,
3930 postfix_expression = error_mark_node;
3931 idk = CP_ID_KIND_NONE;
3935 return postfix_expression;
3939 /* We should never get here. */
3941 return error_mark_node;
3944 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
3945 by cp_parser_builtin_offsetof. We're looking for
3947 postfix-expression [ expression ]
3949 FOR_OFFSETOF is set if we're being called in that context, which
3950 changes how we deal with integer constant expressions. */
3953 cp_parser_postfix_open_square_expression (cp_parser *parser,
3954 tree postfix_expression,
3959 /* Consume the `[' token. */
3960 cp_lexer_consume_token (parser->lexer);
3962 /* Parse the index expression. */
3963 /* ??? For offsetof, there is a question of what to allow here. If
3964 offsetof is not being used in an integral constant expression context,
3965 then we *could* get the right answer by computing the value at runtime.
3966 If we are in an integral constant expression context, then we might
3967 could accept any constant expression; hard to say without analysis.
3968 Rather than open the barn door too wide right away, allow only integer
3969 constant expresions here. */
3971 index = cp_parser_constant_expression (parser, false, NULL);
3973 index = cp_parser_expression (parser);
3975 /* Look for the closing `]'. */
3976 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
3978 /* Build the ARRAY_REF. */
3979 postfix_expression = grok_array_decl (postfix_expression, index);
3981 /* When not doing offsetof, array references are not permitted in
3982 constant-expressions. */
3984 && (cp_parser_non_integral_constant_expression
3985 (parser, "an array reference")))
3986 postfix_expression = error_mark_node;
3988 return postfix_expression;
3991 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
3992 by cp_parser_builtin_offsetof. We're looking for
3994 postfix-expression . template [opt] id-expression
3995 postfix-expression . pseudo-destructor-name
3996 postfix-expression -> template [opt] id-expression
3997 postfix-expression -> pseudo-destructor-name
3999 FOR_OFFSETOF is set if we're being called in that context. That sorta
4000 limits what of the above we'll actually accept, but nevermind.
4001 TOKEN_TYPE is the "." or "->" token, which will already have been
4002 removed from the stream. */
4005 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
4006 enum cpp_ttype token_type,
4007 tree postfix_expression,
4008 bool for_offsetof, cp_id_kind *idk)
4013 tree scope = NULL_TREE;
4015 /* If this is a `->' operator, dereference the pointer. */
4016 if (token_type == CPP_DEREF)
4017 postfix_expression = build_x_arrow (postfix_expression);
4018 /* Check to see whether or not the expression is type-dependent. */
4019 dependent_p = type_dependent_expression_p (postfix_expression);
4020 /* The identifier following the `->' or `.' is not qualified. */
4021 parser->scope = NULL_TREE;
4022 parser->qualifying_scope = NULL_TREE;
4023 parser->object_scope = NULL_TREE;
4024 *idk = CP_ID_KIND_NONE;
4025 /* Enter the scope corresponding to the type of the object
4026 given by the POSTFIX_EXPRESSION. */
4027 if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
4029 scope = TREE_TYPE (postfix_expression);
4030 /* According to the standard, no expression should ever have
4031 reference type. Unfortunately, we do not currently match
4032 the standard in this respect in that our internal representation
4033 of an expression may have reference type even when the standard
4034 says it does not. Therefore, we have to manually obtain the
4035 underlying type here. */
4036 scope = non_reference (scope);
4037 /* The type of the POSTFIX_EXPRESSION must be complete. */
4038 scope = complete_type_or_else (scope, NULL_TREE);
4039 /* Let the name lookup machinery know that we are processing a
4040 class member access expression. */
4041 parser->context->object_type = scope;
4042 /* If something went wrong, we want to be able to discern that case,
4043 as opposed to the case where there was no SCOPE due to the type
4044 of expression being dependent. */
4046 scope = error_mark_node;
4047 /* If the SCOPE was erroneous, make the various semantic analysis
4048 functions exit quickly -- and without issuing additional error
4050 if (scope == error_mark_node)
4051 postfix_expression = error_mark_node;
4054 /* If the SCOPE is not a scalar type, we are looking at an
4055 ordinary class member access expression, rather than a
4056 pseudo-destructor-name. */
4057 if (!scope || !SCALAR_TYPE_P (scope))
4059 template_p = cp_parser_optional_template_keyword (parser);
4060 /* Parse the id-expression. */
4061 name = cp_parser_id_expression (parser, template_p,
4062 /*check_dependency_p=*/true,
4063 /*template_p=*/NULL,
4064 /*declarator_p=*/false);
4065 /* In general, build a SCOPE_REF if the member name is qualified.
4066 However, if the name was not dependent and has already been
4067 resolved; there is no need to build the SCOPE_REF. For example;
4069 struct X { void f(); };
4070 template <typename T> void f(T* t) { t->X::f(); }
4072 Even though "t" is dependent, "X::f" is not and has been resolved
4073 to a BASELINK; there is no need to include scope information. */
4075 /* But we do need to remember that there was an explicit scope for
4076 virtual function calls. */
4078 *idk = CP_ID_KIND_QUALIFIED;
4080 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
4082 name = build_nt (SCOPE_REF, parser->scope, name);
4083 parser->scope = NULL_TREE;
4084 parser->qualifying_scope = NULL_TREE;
4085 parser->object_scope = NULL_TREE;
4087 if (scope && name && BASELINK_P (name))
4088 adjust_result_of_qualified_name_lookup
4089 (name, BINFO_TYPE (BASELINK_BINFO (name)), scope);
4091 = finish_class_member_access_expr (postfix_expression, name);
4093 /* Otherwise, try the pseudo-destructor-name production. */
4099 /* Parse the pseudo-destructor-name. */
4100 cp_parser_pseudo_destructor_name (parser, &s, &type);
4101 /* Form the call. */
4103 = finish_pseudo_destructor_expr (postfix_expression,
4104 s, TREE_TYPE (type));
4107 /* We no longer need to look up names in the scope of the object on
4108 the left-hand side of the `.' or `->' operator. */
4109 parser->context->object_type = NULL_TREE;
4111 /* Outside of offsetof, these operators may not appear in
4112 constant-expressions. */
4114 && (cp_parser_non_integral_constant_expression
4115 (parser, token_type == CPP_DEREF ? "'->'" : "`.'")))
4116 postfix_expression = error_mark_node;
4118 return postfix_expression;
4121 /* Parse a parenthesized expression-list.
4124 assignment-expression
4125 expression-list, assignment-expression
4130 identifier, expression-list
4132 Returns a TREE_LIST. The TREE_VALUE of each node is a
4133 representation of an assignment-expression. Note that a TREE_LIST
4134 is returned even if there is only a single expression in the list.
4135 error_mark_node is returned if the ( and or ) are
4136 missing. NULL_TREE is returned on no expressions. The parentheses
4137 are eaten. IS_ATTRIBUTE_LIST is true if this is really an attribute
4138 list being parsed. If NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P
4139 indicates whether or not all of the expressions in the list were
4143 cp_parser_parenthesized_expression_list (cp_parser* parser,
4144 bool is_attribute_list,
4145 bool *non_constant_p)
4147 tree expression_list = NULL_TREE;
4148 tree identifier = NULL_TREE;
4150 /* Assume all the expressions will be constant. */
4152 *non_constant_p = false;
4154 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
4155 return error_mark_node;
4157 /* Consume expressions until there are no more. */
4158 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
4163 /* At the beginning of attribute lists, check to see if the
4164 next token is an identifier. */
4165 if (is_attribute_list
4166 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
4170 /* Consume the identifier. */
4171 token = cp_lexer_consume_token (parser->lexer);
4172 /* Save the identifier. */
4173 identifier = token->value;
4177 /* Parse the next assignment-expression. */
4180 bool expr_non_constant_p;
4181 expr = (cp_parser_constant_expression
4182 (parser, /*allow_non_constant_p=*/true,
4183 &expr_non_constant_p));
4184 if (expr_non_constant_p)
4185 *non_constant_p = true;
4188 expr = cp_parser_assignment_expression (parser);
4190 /* Add it to the list. We add error_mark_node
4191 expressions to the list, so that we can still tell if
4192 the correct form for a parenthesized expression-list
4193 is found. That gives better errors. */
4194 expression_list = tree_cons (NULL_TREE, expr, expression_list);
4196 if (expr == error_mark_node)
4200 /* After the first item, attribute lists look the same as
4201 expression lists. */
4202 is_attribute_list = false;
4205 /* If the next token isn't a `,', then we are done. */
4206 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
4209 /* Otherwise, consume the `,' and keep going. */
4210 cp_lexer_consume_token (parser->lexer);
4213 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
4218 /* We try and resync to an unnested comma, as that will give the
4219 user better diagnostics. */
4220 ending = cp_parser_skip_to_closing_parenthesis (parser,
4221 /*recovering=*/true,
4223 /*consume_paren=*/true);
4227 return error_mark_node;
4230 /* We built up the list in reverse order so we must reverse it now. */
4231 expression_list = nreverse (expression_list);
4233 expression_list = tree_cons (NULL_TREE, identifier, expression_list);
4235 return expression_list;
4238 /* Parse a pseudo-destructor-name.
4240 pseudo-destructor-name:
4241 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
4242 :: [opt] nested-name-specifier template template-id :: ~ type-name
4243 :: [opt] nested-name-specifier [opt] ~ type-name
4245 If either of the first two productions is used, sets *SCOPE to the
4246 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
4247 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
4248 or ERROR_MARK_NODE if the parse fails. */
4251 cp_parser_pseudo_destructor_name (cp_parser* parser,
4255 bool nested_name_specifier_p;
4257 /* Look for the optional `::' operator. */
4258 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
4259 /* Look for the optional nested-name-specifier. */
4260 nested_name_specifier_p
4261 = (cp_parser_nested_name_specifier_opt (parser,
4262 /*typename_keyword_p=*/false,
4263 /*check_dependency_p=*/true,
4265 /*is_declaration=*/true)
4267 /* Now, if we saw a nested-name-specifier, we might be doing the
4268 second production. */
4269 if (nested_name_specifier_p
4270 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
4272 /* Consume the `template' keyword. */
4273 cp_lexer_consume_token (parser->lexer);
4274 /* Parse the template-id. */
4275 cp_parser_template_id (parser,
4276 /*template_keyword_p=*/true,
4277 /*check_dependency_p=*/false,
4278 /*is_declaration=*/true);
4279 /* Look for the `::' token. */
4280 cp_parser_require (parser, CPP_SCOPE, "`::'");
4282 /* If the next token is not a `~', then there might be some
4283 additional qualification. */
4284 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
4286 /* Look for the type-name. */
4287 *scope = TREE_TYPE (cp_parser_type_name (parser));
4289 /* If we didn't get an aggregate type, or we don't have ::~,
4290 then something has gone wrong. Since the only caller of this
4291 function is looking for something after `.' or `->' after a
4292 scalar type, most likely the program is trying to get a
4293 member of a non-aggregate type. */
4294 if (*scope == error_mark_node
4295 || cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE)
4296 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_COMPL)
4298 cp_parser_error (parser, "request for member of non-aggregate type");
4299 *type = error_mark_node;
4303 /* Look for the `::' token. */
4304 cp_parser_require (parser, CPP_SCOPE, "`::'");
4309 /* Look for the `~'. */
4310 cp_parser_require (parser, CPP_COMPL, "`~'");
4311 /* Look for the type-name again. We are not responsible for
4312 checking that it matches the first type-name. */
4313 *type = cp_parser_type_name (parser);
4316 /* Parse a unary-expression.
4322 unary-operator cast-expression
4323 sizeof unary-expression
4331 __extension__ cast-expression
4332 __alignof__ unary-expression
4333 __alignof__ ( type-id )
4334 __real__ cast-expression
4335 __imag__ cast-expression
4338 ADDRESS_P is true iff the unary-expression is appearing as the
4339 operand of the `&' operator.
4341 Returns a representation of the expression. */
4344 cp_parser_unary_expression (cp_parser *parser, bool address_p)
4347 enum tree_code unary_operator;
4349 /* Peek at the next token. */
4350 token = cp_lexer_peek_token (parser->lexer);
4351 /* Some keywords give away the kind of expression. */
4352 if (token->type == CPP_KEYWORD)
4354 enum rid keyword = token->keyword;
4364 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
4365 /* Consume the token. */
4366 cp_lexer_consume_token (parser->lexer);
4367 /* Parse the operand. */
4368 operand = cp_parser_sizeof_operand (parser, keyword);
4370 if (TYPE_P (operand))
4371 return cxx_sizeof_or_alignof_type (operand, op, true);
4373 return cxx_sizeof_or_alignof_expr (operand, op);
4377 return cp_parser_new_expression (parser);
4380 return cp_parser_delete_expression (parser);
4384 /* The saved value of the PEDANTIC flag. */
4388 /* Save away the PEDANTIC flag. */
4389 cp_parser_extension_opt (parser, &saved_pedantic);
4390 /* Parse the cast-expression. */
4391 expr = cp_parser_simple_cast_expression (parser);
4392 /* Restore the PEDANTIC flag. */
4393 pedantic = saved_pedantic;
4403 /* Consume the `__real__' or `__imag__' token. */
4404 cp_lexer_consume_token (parser->lexer);
4405 /* Parse the cast-expression. */
4406 expression = cp_parser_simple_cast_expression (parser);
4407 /* Create the complete representation. */
4408 return build_x_unary_op ((keyword == RID_REALPART
4409 ? REALPART_EXPR : IMAGPART_EXPR),
4419 /* Look for the `:: new' and `:: delete', which also signal the
4420 beginning of a new-expression, or delete-expression,
4421 respectively. If the next token is `::', then it might be one of
4423 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
4427 /* See if the token after the `::' is one of the keywords in
4428 which we're interested. */
4429 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
4430 /* If it's `new', we have a new-expression. */
4431 if (keyword == RID_NEW)
4432 return cp_parser_new_expression (parser);
4433 /* Similarly, for `delete'. */
4434 else if (keyword == RID_DELETE)
4435 return cp_parser_delete_expression (parser);
4438 /* Look for a unary operator. */
4439 unary_operator = cp_parser_unary_operator (token);
4440 /* The `++' and `--' operators can be handled similarly, even though
4441 they are not technically unary-operators in the grammar. */
4442 if (unary_operator == ERROR_MARK)
4444 if (token->type == CPP_PLUS_PLUS)
4445 unary_operator = PREINCREMENT_EXPR;
4446 else if (token->type == CPP_MINUS_MINUS)
4447 unary_operator = PREDECREMENT_EXPR;
4448 /* Handle the GNU address-of-label extension. */
4449 else if (cp_parser_allow_gnu_extensions_p (parser)
4450 && token->type == CPP_AND_AND)
4454 /* Consume the '&&' token. */
4455 cp_lexer_consume_token (parser->lexer);
4456 /* Look for the identifier. */
4457 identifier = cp_parser_identifier (parser);
4458 /* Create an expression representing the address. */
4459 return finish_label_address_expr (identifier);
4462 if (unary_operator != ERROR_MARK)
4464 tree cast_expression;
4465 tree expression = error_mark_node;
4466 const char *non_constant_p = NULL;
4468 /* Consume the operator token. */
4469 token = cp_lexer_consume_token (parser->lexer);
4470 /* Parse the cast-expression. */
4472 = cp_parser_cast_expression (parser, unary_operator == ADDR_EXPR);
4473 /* Now, build an appropriate representation. */
4474 switch (unary_operator)
4477 non_constant_p = "`*'";
4478 expression = build_x_indirect_ref (cast_expression, "unary *");
4482 non_constant_p = "`&'";
4485 expression = build_x_unary_op (unary_operator, cast_expression);
4488 case PREINCREMENT_EXPR:
4489 case PREDECREMENT_EXPR:
4490 non_constant_p = (unary_operator == PREINCREMENT_EXPR
4495 case TRUTH_NOT_EXPR:
4496 expression = finish_unary_op_expr (unary_operator, cast_expression);
4504 && cp_parser_non_integral_constant_expression (parser,
4506 expression = error_mark_node;
4511 return cp_parser_postfix_expression (parser, address_p);
4514 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
4515 unary-operator, the corresponding tree code is returned. */
4517 static enum tree_code
4518 cp_parser_unary_operator (cp_token* token)
4520 switch (token->type)
4523 return INDIRECT_REF;
4529 return CONVERT_EXPR;
4535 return TRUTH_NOT_EXPR;
4538 return BIT_NOT_EXPR;
4545 /* Parse a new-expression.
4548 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
4549 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
4551 Returns a representation of the expression. */
4554 cp_parser_new_expression (cp_parser* parser)
4556 bool global_scope_p;
4561 /* Look for the optional `::' operator. */
4563 = (cp_parser_global_scope_opt (parser,
4564 /*current_scope_valid_p=*/false)
4566 /* Look for the `new' operator. */
4567 cp_parser_require_keyword (parser, RID_NEW, "`new'");
4568 /* There's no easy way to tell a new-placement from the
4569 `( type-id )' construct. */
4570 cp_parser_parse_tentatively (parser);
4571 /* Look for a new-placement. */
4572 placement = cp_parser_new_placement (parser);
4573 /* If that didn't work out, there's no new-placement. */
4574 if (!cp_parser_parse_definitely (parser))
4575 placement = NULL_TREE;
4577 /* If the next token is a `(', then we have a parenthesized
4579 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4581 /* Consume the `('. */
4582 cp_lexer_consume_token (parser->lexer);
4583 /* Parse the type-id. */
4584 type = cp_parser_type_id (parser);
4585 /* Look for the closing `)'. */
4586 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4587 /* There should not be a direct-new-declarator in this production,
4588 but GCC used to allowed this, so we check and emit a sensible error
4589 message for this case. */
4590 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
4592 error ("array bound forbidden after parenthesized type-id");
4593 inform ("try removing the parentheses around the type-id");
4594 cp_parser_direct_new_declarator (parser);
4597 /* Otherwise, there must be a new-type-id. */
4599 type = cp_parser_new_type_id (parser);
4601 /* If the next token is a `(', then we have a new-initializer. */
4602 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4603 initializer = cp_parser_new_initializer (parser);
4605 initializer = NULL_TREE;
4607 /* A new-expression may not appear in an integral constant
4609 if (cp_parser_non_integral_constant_expression (parser, "`new'"))
4610 return error_mark_node;
4612 /* Create a representation of the new-expression. */
4613 return build_new (placement, type, initializer, global_scope_p);
4616 /* Parse a new-placement.
4621 Returns the same representation as for an expression-list. */
4624 cp_parser_new_placement (cp_parser* parser)
4626 tree expression_list;
4628 /* Parse the expression-list. */
4629 expression_list = (cp_parser_parenthesized_expression_list
4630 (parser, false, /*non_constant_p=*/NULL));
4632 return expression_list;
4635 /* Parse a new-type-id.
4638 type-specifier-seq new-declarator [opt]
4640 Returns a TREE_LIST whose TREE_PURPOSE is the type-specifier-seq,
4641 and whose TREE_VALUE is the new-declarator. */
4644 cp_parser_new_type_id (cp_parser* parser)
4646 tree type_specifier_seq;
4648 const char *saved_message;
4650 /* The type-specifier sequence must not contain type definitions.
4651 (It cannot contain declarations of new types either, but if they
4652 are not definitions we will catch that because they are not
4654 saved_message = parser->type_definition_forbidden_message;
4655 parser->type_definition_forbidden_message
4656 = "types may not be defined in a new-type-id";
4657 /* Parse the type-specifier-seq. */
4658 type_specifier_seq = cp_parser_type_specifier_seq (parser);
4659 /* Restore the old message. */
4660 parser->type_definition_forbidden_message = saved_message;
4661 /* Parse the new-declarator. */
4662 declarator = cp_parser_new_declarator_opt (parser);
4664 return build_tree_list (type_specifier_seq, declarator);
4667 /* Parse an (optional) new-declarator.
4670 ptr-operator new-declarator [opt]
4671 direct-new-declarator
4673 Returns a representation of the declarator. See
4674 cp_parser_declarator for the representations used. */
4677 cp_parser_new_declarator_opt (cp_parser* parser)
4679 enum tree_code code;
4681 tree cv_qualifier_seq;
4683 /* We don't know if there's a ptr-operator next, or not. */
4684 cp_parser_parse_tentatively (parser);
4685 /* Look for a ptr-operator. */
4686 code = cp_parser_ptr_operator (parser, &type, &cv_qualifier_seq);
4687 /* If that worked, look for more new-declarators. */
4688 if (cp_parser_parse_definitely (parser))
4692 /* Parse another optional declarator. */
4693 declarator = cp_parser_new_declarator_opt (parser);
4695 /* Create the representation of the declarator. */
4696 if (code == INDIRECT_REF)
4697 declarator = make_pointer_declarator (cv_qualifier_seq,
4700 declarator = make_reference_declarator (cv_qualifier_seq,
4703 /* Handle the pointer-to-member case. */
4705 declarator = build_nt (SCOPE_REF, type, declarator);
4710 /* If the next token is a `[', there is a direct-new-declarator. */
4711 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
4712 return cp_parser_direct_new_declarator (parser);
4717 /* Parse a direct-new-declarator.
4719 direct-new-declarator:
4721 direct-new-declarator [constant-expression]
4723 Returns an ARRAY_REF, following the same conventions as are
4724 documented for cp_parser_direct_declarator. */
4727 cp_parser_direct_new_declarator (cp_parser* parser)
4729 tree declarator = NULL_TREE;
4735 /* Look for the opening `['. */
4736 cp_parser_require (parser, CPP_OPEN_SQUARE, "`['");
4737 /* The first expression is not required to be constant. */
4740 expression = cp_parser_expression (parser);
4741 /* The standard requires that the expression have integral
4742 type. DR 74 adds enumeration types. We believe that the
4743 real intent is that these expressions be handled like the
4744 expression in a `switch' condition, which also allows
4745 classes with a single conversion to integral or
4746 enumeration type. */
4747 if (!processing_template_decl)
4750 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
4755 error ("expression in new-declarator must have integral or enumeration type");
4756 expression = error_mark_node;
4760 /* But all the other expressions must be. */
4763 = cp_parser_constant_expression (parser,
4764 /*allow_non_constant=*/false,
4766 /* Look for the closing `]'. */
4767 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4769 /* Add this bound to the declarator. */
4770 declarator = build_nt (ARRAY_REF, declarator, expression,
4771 NULL_TREE, NULL_TREE);
4773 /* If the next token is not a `[', then there are no more
4775 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
4782 /* Parse a new-initializer.
4785 ( expression-list [opt] )
4787 Returns a representation of the expression-list. If there is no
4788 expression-list, VOID_ZERO_NODE is returned. */
4791 cp_parser_new_initializer (cp_parser* parser)
4793 tree expression_list;
4795 expression_list = (cp_parser_parenthesized_expression_list
4796 (parser, false, /*non_constant_p=*/NULL));
4797 if (!expression_list)
4798 expression_list = void_zero_node;
4800 return expression_list;
4803 /* Parse a delete-expression.
4806 :: [opt] delete cast-expression
4807 :: [opt] delete [ ] cast-expression
4809 Returns a representation of the expression. */
4812 cp_parser_delete_expression (cp_parser* parser)
4814 bool global_scope_p;
4818 /* Look for the optional `::' operator. */
4820 = (cp_parser_global_scope_opt (parser,
4821 /*current_scope_valid_p=*/false)
4823 /* Look for the `delete' keyword. */
4824 cp_parser_require_keyword (parser, RID_DELETE, "`delete'");
4825 /* See if the array syntax is in use. */
4826 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
4828 /* Consume the `[' token. */
4829 cp_lexer_consume_token (parser->lexer);
4830 /* Look for the `]' token. */
4831 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4832 /* Remember that this is the `[]' construct. */
4838 /* Parse the cast-expression. */
4839 expression = cp_parser_simple_cast_expression (parser);
4841 /* A delete-expression may not appear in an integral constant
4843 if (cp_parser_non_integral_constant_expression (parser, "`delete'"))
4844 return error_mark_node;
4846 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
4849 /* Parse a cast-expression.
4853 ( type-id ) cast-expression
4855 Returns a representation of the expression. */
4858 cp_parser_cast_expression (cp_parser *parser, bool address_p)
4860 /* If it's a `(', then we might be looking at a cast. */
4861 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4863 tree type = NULL_TREE;
4864 tree expr = NULL_TREE;
4865 bool compound_literal_p;
4866 const char *saved_message;
4868 /* There's no way to know yet whether or not this is a cast.
4869 For example, `(int (3))' is a unary-expression, while `(int)
4870 3' is a cast. So, we resort to parsing tentatively. */
4871 cp_parser_parse_tentatively (parser);
4872 /* Types may not be defined in a cast. */
4873 saved_message = parser->type_definition_forbidden_message;
4874 parser->type_definition_forbidden_message
4875 = "types may not be defined in casts";
4876 /* Consume the `('. */
4877 cp_lexer_consume_token (parser->lexer);
4878 /* A very tricky bit is that `(struct S) { 3 }' is a
4879 compound-literal (which we permit in C++ as an extension).
4880 But, that construct is not a cast-expression -- it is a
4881 postfix-expression. (The reason is that `(struct S) { 3 }.i'
4882 is legal; if the compound-literal were a cast-expression,
4883 you'd need an extra set of parentheses.) But, if we parse
4884 the type-id, and it happens to be a class-specifier, then we
4885 will commit to the parse at that point, because we cannot
4886 undo the action that is done when creating a new class. So,
4887 then we cannot back up and do a postfix-expression.
4889 Therefore, we scan ahead to the closing `)', and check to see
4890 if the token after the `)' is a `{'. If so, we are not
4891 looking at a cast-expression.
4893 Save tokens so that we can put them back. */
4894 cp_lexer_save_tokens (parser->lexer);
4895 /* Skip tokens until the next token is a closing parenthesis.
4896 If we find the closing `)', and the next token is a `{', then
4897 we are looking at a compound-literal. */
4899 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
4900 /*consume_paren=*/true)
4901 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
4902 /* Roll back the tokens we skipped. */
4903 cp_lexer_rollback_tokens (parser->lexer);
4904 /* If we were looking at a compound-literal, simulate an error
4905 so that the call to cp_parser_parse_definitely below will
4907 if (compound_literal_p)
4908 cp_parser_simulate_error (parser);
4911 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4912 parser->in_type_id_in_expr_p = true;
4913 /* Look for the type-id. */
4914 type = cp_parser_type_id (parser);
4915 /* Look for the closing `)'. */
4916 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4917 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4920 /* Restore the saved message. */
4921 parser->type_definition_forbidden_message = saved_message;
4923 /* If ok so far, parse the dependent expression. We cannot be
4924 sure it is a cast. Consider `(T ())'. It is a parenthesized
4925 ctor of T, but looks like a cast to function returning T
4926 without a dependent expression. */
4927 if (!cp_parser_error_occurred (parser))
4928 expr = cp_parser_simple_cast_expression (parser);
4930 if (cp_parser_parse_definitely (parser))
4932 /* Warn about old-style casts, if so requested. */
4933 if (warn_old_style_cast
4934 && !in_system_header
4935 && !VOID_TYPE_P (type)
4936 && current_lang_name != lang_name_c)
4937 warning ("use of old-style cast");
4939 /* Only type conversions to integral or enumeration types
4940 can be used in constant-expressions. */
4941 if (parser->integral_constant_expression_p
4942 && !dependent_type_p (type)
4943 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type)
4944 && (cp_parser_non_integral_constant_expression
4946 "a cast to a type other than an integral or "
4947 "enumeration type")))
4948 return error_mark_node;
4950 /* Perform the cast. */
4951 expr = build_c_cast (type, expr);
4956 /* If we get here, then it's not a cast, so it must be a
4957 unary-expression. */
4958 return cp_parser_unary_expression (parser, address_p);
4961 /* Parse a pm-expression.
4965 pm-expression .* cast-expression
4966 pm-expression ->* cast-expression
4968 Returns a representation of the expression. */
4971 cp_parser_pm_expression (cp_parser* parser)
4973 static const cp_parser_token_tree_map map = {
4974 { CPP_DEREF_STAR, MEMBER_REF },
4975 { CPP_DOT_STAR, DOTSTAR_EXPR },
4976 { CPP_EOF, ERROR_MARK }
4979 return cp_parser_binary_expression (parser, map,
4980 cp_parser_simple_cast_expression);
4983 /* Parse a multiplicative-expression.
4985 multiplicative-expression:
4987 multiplicative-expression * pm-expression
4988 multiplicative-expression / pm-expression
4989 multiplicative-expression % pm-expression
4991 Returns a representation of the expression. */
4994 cp_parser_multiplicative_expression (cp_parser* parser)
4996 static const cp_parser_token_tree_map map = {
4997 { CPP_MULT, MULT_EXPR },
4998 { CPP_DIV, TRUNC_DIV_EXPR },
4999 { CPP_MOD, TRUNC_MOD_EXPR },
5000 { CPP_EOF, ERROR_MARK }
5003 return cp_parser_binary_expression (parser,
5005 cp_parser_pm_expression);
5008 /* Parse an additive-expression.
5010 additive-expression:
5011 multiplicative-expression
5012 additive-expression + multiplicative-expression
5013 additive-expression - multiplicative-expression
5015 Returns a representation of the expression. */
5018 cp_parser_additive_expression (cp_parser* parser)
5020 static const cp_parser_token_tree_map map = {
5021 { CPP_PLUS, PLUS_EXPR },
5022 { CPP_MINUS, MINUS_EXPR },
5023 { CPP_EOF, ERROR_MARK }
5026 return cp_parser_binary_expression (parser,
5028 cp_parser_multiplicative_expression);
5031 /* Parse a shift-expression.
5035 shift-expression << additive-expression
5036 shift-expression >> additive-expression
5038 Returns a representation of the expression. */
5041 cp_parser_shift_expression (cp_parser* parser)
5043 static const cp_parser_token_tree_map map = {
5044 { CPP_LSHIFT, LSHIFT_EXPR },
5045 { CPP_RSHIFT, RSHIFT_EXPR },
5046 { CPP_EOF, ERROR_MARK }
5049 return cp_parser_binary_expression (parser,
5051 cp_parser_additive_expression);
5054 /* Parse a relational-expression.
5056 relational-expression:
5058 relational-expression < shift-expression
5059 relational-expression > shift-expression
5060 relational-expression <= shift-expression
5061 relational-expression >= shift-expression
5065 relational-expression:
5066 relational-expression <? shift-expression
5067 relational-expression >? shift-expression
5069 Returns a representation of the expression. */
5072 cp_parser_relational_expression (cp_parser* parser)
5074 static const cp_parser_token_tree_map map = {
5075 { CPP_LESS, LT_EXPR },
5076 { CPP_GREATER, GT_EXPR },
5077 { CPP_LESS_EQ, LE_EXPR },
5078 { CPP_GREATER_EQ, GE_EXPR },
5079 { CPP_MIN, MIN_EXPR },
5080 { CPP_MAX, MAX_EXPR },
5081 { CPP_EOF, ERROR_MARK }
5084 return cp_parser_binary_expression (parser,
5086 cp_parser_shift_expression);
5089 /* Parse an equality-expression.
5091 equality-expression:
5092 relational-expression
5093 equality-expression == relational-expression
5094 equality-expression != relational-expression
5096 Returns a representation of the expression. */
5099 cp_parser_equality_expression (cp_parser* parser)
5101 static const cp_parser_token_tree_map map = {
5102 { CPP_EQ_EQ, EQ_EXPR },
5103 { CPP_NOT_EQ, NE_EXPR },
5104 { CPP_EOF, ERROR_MARK }
5107 return cp_parser_binary_expression (parser,
5109 cp_parser_relational_expression);
5112 /* Parse an and-expression.
5116 and-expression & equality-expression
5118 Returns a representation of the expression. */
5121 cp_parser_and_expression (cp_parser* parser)
5123 static const cp_parser_token_tree_map map = {
5124 { CPP_AND, BIT_AND_EXPR },
5125 { CPP_EOF, ERROR_MARK }
5128 return cp_parser_binary_expression (parser,
5130 cp_parser_equality_expression);
5133 /* Parse an exclusive-or-expression.
5135 exclusive-or-expression:
5137 exclusive-or-expression ^ and-expression
5139 Returns a representation of the expression. */
5142 cp_parser_exclusive_or_expression (cp_parser* parser)
5144 static const cp_parser_token_tree_map map = {
5145 { CPP_XOR, BIT_XOR_EXPR },
5146 { CPP_EOF, ERROR_MARK }
5149 return cp_parser_binary_expression (parser,
5151 cp_parser_and_expression);
5155 /* Parse an inclusive-or-expression.
5157 inclusive-or-expression:
5158 exclusive-or-expression
5159 inclusive-or-expression | exclusive-or-expression
5161 Returns a representation of the expression. */
5164 cp_parser_inclusive_or_expression (cp_parser* parser)
5166 static const cp_parser_token_tree_map map = {
5167 { CPP_OR, BIT_IOR_EXPR },
5168 { CPP_EOF, ERROR_MARK }
5171 return cp_parser_binary_expression (parser,
5173 cp_parser_exclusive_or_expression);
5176 /* Parse a logical-and-expression.
5178 logical-and-expression:
5179 inclusive-or-expression
5180 logical-and-expression && inclusive-or-expression
5182 Returns a representation of the expression. */
5185 cp_parser_logical_and_expression (cp_parser* parser)
5187 static const cp_parser_token_tree_map map = {
5188 { CPP_AND_AND, TRUTH_ANDIF_EXPR },
5189 { CPP_EOF, ERROR_MARK }
5192 return cp_parser_binary_expression (parser,
5194 cp_parser_inclusive_or_expression);
5197 /* Parse a logical-or-expression.
5199 logical-or-expression:
5200 logical-and-expression
5201 logical-or-expression || logical-and-expression
5203 Returns a representation of the expression. */
5206 cp_parser_logical_or_expression (cp_parser* parser)
5208 static const cp_parser_token_tree_map map = {
5209 { CPP_OR_OR, TRUTH_ORIF_EXPR },
5210 { CPP_EOF, ERROR_MARK }
5213 return cp_parser_binary_expression (parser,
5215 cp_parser_logical_and_expression);
5218 /* Parse the `? expression : assignment-expression' part of a
5219 conditional-expression. The LOGICAL_OR_EXPR is the
5220 logical-or-expression that started the conditional-expression.
5221 Returns a representation of the entire conditional-expression.
5223 This routine is used by cp_parser_assignment_expression.
5225 ? expression : assignment-expression
5229 ? : assignment-expression */
5232 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
5235 tree assignment_expr;
5237 /* Consume the `?' token. */
5238 cp_lexer_consume_token (parser->lexer);
5239 if (cp_parser_allow_gnu_extensions_p (parser)
5240 && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
5241 /* Implicit true clause. */
5244 /* Parse the expression. */
5245 expr = cp_parser_expression (parser);
5247 /* The next token should be a `:'. */
5248 cp_parser_require (parser, CPP_COLON, "`:'");
5249 /* Parse the assignment-expression. */
5250 assignment_expr = cp_parser_assignment_expression (parser);
5252 /* Build the conditional-expression. */
5253 return build_x_conditional_expr (logical_or_expr,
5258 /* Parse an assignment-expression.
5260 assignment-expression:
5261 conditional-expression
5262 logical-or-expression assignment-operator assignment_expression
5265 Returns a representation for the expression. */
5268 cp_parser_assignment_expression (cp_parser* parser)
5272 /* If the next token is the `throw' keyword, then we're looking at
5273 a throw-expression. */
5274 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
5275 expr = cp_parser_throw_expression (parser);
5276 /* Otherwise, it must be that we are looking at a
5277 logical-or-expression. */
5280 /* Parse the logical-or-expression. */
5281 expr = cp_parser_logical_or_expression (parser);
5282 /* If the next token is a `?' then we're actually looking at a
5283 conditional-expression. */
5284 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
5285 return cp_parser_question_colon_clause (parser, expr);
5288 enum tree_code assignment_operator;
5290 /* If it's an assignment-operator, we're using the second
5293 = cp_parser_assignment_operator_opt (parser);
5294 if (assignment_operator != ERROR_MARK)
5298 /* Parse the right-hand side of the assignment. */
5299 rhs = cp_parser_assignment_expression (parser);
5300 /* An assignment may not appear in a
5301 constant-expression. */
5302 if (cp_parser_non_integral_constant_expression (parser,
5304 return error_mark_node;
5305 /* Build the assignment expression. */
5306 expr = build_x_modify_expr (expr,
5307 assignment_operator,
5316 /* Parse an (optional) assignment-operator.
5318 assignment-operator: one of
5319 = *= /= %= += -= >>= <<= &= ^= |=
5323 assignment-operator: one of
5326 If the next token is an assignment operator, the corresponding tree
5327 code is returned, and the token is consumed. For example, for
5328 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
5329 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
5330 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
5331 operator, ERROR_MARK is returned. */
5333 static enum tree_code
5334 cp_parser_assignment_operator_opt (cp_parser* parser)
5339 /* Peek at the next toen. */
5340 token = cp_lexer_peek_token (parser->lexer);
5342 switch (token->type)
5353 op = TRUNC_DIV_EXPR;
5357 op = TRUNC_MOD_EXPR;
5397 /* Nothing else is an assignment operator. */
5401 /* If it was an assignment operator, consume it. */
5402 if (op != ERROR_MARK)
5403 cp_lexer_consume_token (parser->lexer);
5408 /* Parse an expression.
5411 assignment-expression
5412 expression , assignment-expression
5414 Returns a representation of the expression. */
5417 cp_parser_expression (cp_parser* parser)
5419 tree expression = NULL_TREE;
5423 tree assignment_expression;
5425 /* Parse the next assignment-expression. */
5426 assignment_expression
5427 = cp_parser_assignment_expression (parser);
5428 /* If this is the first assignment-expression, we can just
5431 expression = assignment_expression;
5433 expression = build_x_compound_expr (expression,
5434 assignment_expression);
5435 /* If the next token is not a comma, then we are done with the
5437 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5439 /* Consume the `,'. */
5440 cp_lexer_consume_token (parser->lexer);
5441 /* A comma operator cannot appear in a constant-expression. */
5442 if (cp_parser_non_integral_constant_expression (parser,
5443 "a comma operator"))
5444 expression = error_mark_node;
5450 /* Parse a constant-expression.
5452 constant-expression:
5453 conditional-expression
5455 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
5456 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
5457 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
5458 is false, NON_CONSTANT_P should be NULL. */
5461 cp_parser_constant_expression (cp_parser* parser,
5462 bool allow_non_constant_p,
5463 bool *non_constant_p)
5465 bool saved_integral_constant_expression_p;
5466 bool saved_allow_non_integral_constant_expression_p;
5467 bool saved_non_integral_constant_expression_p;
5470 /* It might seem that we could simply parse the
5471 conditional-expression, and then check to see if it were
5472 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
5473 one that the compiler can figure out is constant, possibly after
5474 doing some simplifications or optimizations. The standard has a
5475 precise definition of constant-expression, and we must honor
5476 that, even though it is somewhat more restrictive.
5482 is not a legal declaration, because `(2, 3)' is not a
5483 constant-expression. The `,' operator is forbidden in a
5484 constant-expression. However, GCC's constant-folding machinery
5485 will fold this operation to an INTEGER_CST for `3'. */
5487 /* Save the old settings. */
5488 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
5489 saved_allow_non_integral_constant_expression_p
5490 = parser->allow_non_integral_constant_expression_p;
5491 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
5492 /* We are now parsing a constant-expression. */
5493 parser->integral_constant_expression_p = true;
5494 parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
5495 parser->non_integral_constant_expression_p = false;
5496 /* Although the grammar says "conditional-expression", we parse an
5497 "assignment-expression", which also permits "throw-expression"
5498 and the use of assignment operators. In the case that
5499 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
5500 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
5501 actually essential that we look for an assignment-expression.
5502 For example, cp_parser_initializer_clauses uses this function to
5503 determine whether a particular assignment-expression is in fact
5505 expression = cp_parser_assignment_expression (parser);
5506 /* Restore the old settings. */
5507 parser->integral_constant_expression_p = saved_integral_constant_expression_p;
5508 parser->allow_non_integral_constant_expression_p
5509 = saved_allow_non_integral_constant_expression_p;
5510 if (allow_non_constant_p)
5511 *non_constant_p = parser->non_integral_constant_expression_p;
5512 parser->non_integral_constant_expression_p = saved_non_integral_constant_expression_p;
5517 /* Parse __builtin_offsetof.
5519 offsetof-expression:
5520 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
5522 offsetof-member-designator:
5524 | offsetof-member-designator "." id-expression
5525 | offsetof-member-designator "[" expression "]"
5529 cp_parser_builtin_offsetof (cp_parser *parser)
5531 int save_ice_p, save_non_ice_p;
5535 /* We're about to accept non-integral-constant things, but will
5536 definitely yield an integral constant expression. Save and
5537 restore these values around our local parsing. */
5538 save_ice_p = parser->integral_constant_expression_p;
5539 save_non_ice_p = parser->non_integral_constant_expression_p;
5541 /* Consume the "__builtin_offsetof" token. */
5542 cp_lexer_consume_token (parser->lexer);
5543 /* Consume the opening `('. */
5544 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
5545 /* Parse the type-id. */
5546 type = cp_parser_type_id (parser);
5547 /* Look for the `,'. */
5548 cp_parser_require (parser, CPP_COMMA, "`,'");
5550 /* Build the (type *)null that begins the traditional offsetof macro. */
5551 expr = build_static_cast (build_pointer_type (type), null_pointer_node);
5553 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
5554 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
5558 cp_token *token = cp_lexer_peek_token (parser->lexer);
5559 switch (token->type)
5561 case CPP_OPEN_SQUARE:
5562 /* offsetof-member-designator "[" expression "]" */
5563 expr = cp_parser_postfix_open_square_expression (parser, expr, true);
5567 /* offsetof-member-designator "." identifier */
5568 cp_lexer_consume_token (parser->lexer);
5569 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT, expr,
5573 case CPP_CLOSE_PAREN:
5574 /* Consume the ")" token. */
5575 cp_lexer_consume_token (parser->lexer);
5579 /* Error. We know the following require will fail, but
5580 that gives the proper error message. */
5581 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5582 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
5583 expr = error_mark_node;
5589 /* We've finished the parsing, now finish with the semantics. At present
5590 we're just mirroring the traditional macro implementation. Better
5591 would be to do the lowering of the ADDR_EXPR to flat pointer arithmetic
5592 here rather than in build_x_unary_op. */
5593 expr = build_reinterpret_cast (build_reference_type (char_type_node), expr);
5594 expr = build_x_unary_op (ADDR_EXPR, expr);
5595 expr = build_reinterpret_cast (size_type_node, expr);
5598 parser->integral_constant_expression_p = save_ice_p;
5599 parser->non_integral_constant_expression_p = save_non_ice_p;
5604 /* Statements [gram.stmt.stmt] */
5606 /* Parse a statement.
5610 expression-statement
5615 declaration-statement
5619 cp_parser_statement (cp_parser* parser, tree in_statement_expr)
5623 location_t statement_locus;
5625 /* There is no statement yet. */
5626 statement = NULL_TREE;
5627 /* Peek at the next token. */
5628 token = cp_lexer_peek_token (parser->lexer);
5629 /* Remember the location of the first token in the statement. */
5630 statement_locus = token->location;
5631 /* If this is a keyword, then that will often determine what kind of
5632 statement we have. */
5633 if (token->type == CPP_KEYWORD)
5635 enum rid keyword = token->keyword;
5641 statement = cp_parser_labeled_statement (parser,
5647 statement = cp_parser_selection_statement (parser);
5653 statement = cp_parser_iteration_statement (parser);
5660 statement = cp_parser_jump_statement (parser);
5664 statement = cp_parser_try_block (parser);
5668 /* It might be a keyword like `int' that can start a
5669 declaration-statement. */
5673 else if (token->type == CPP_NAME)
5675 /* If the next token is a `:', then we are looking at a
5676 labeled-statement. */
5677 token = cp_lexer_peek_nth_token (parser->lexer, 2);
5678 if (token->type == CPP_COLON)
5679 statement = cp_parser_labeled_statement (parser, in_statement_expr);
5681 /* Anything that starts with a `{' must be a compound-statement. */
5682 else if (token->type == CPP_OPEN_BRACE)
5683 statement = cp_parser_compound_statement (parser, NULL, false);
5685 /* Everything else must be a declaration-statement or an
5686 expression-statement. Try for the declaration-statement
5687 first, unless we are looking at a `;', in which case we know that
5688 we have an expression-statement. */
5691 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
5693 cp_parser_parse_tentatively (parser);
5694 /* Try to parse the declaration-statement. */
5695 cp_parser_declaration_statement (parser);
5696 /* If that worked, we're done. */
5697 if (cp_parser_parse_definitely (parser))
5700 /* Look for an expression-statement instead. */
5701 statement = cp_parser_expression_statement (parser, in_statement_expr);
5704 /* Set the line number for the statement. */
5705 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
5707 SET_EXPR_LOCUS (statement, NULL);
5708 annotate_with_locus (statement, statement_locus);
5712 /* Parse a labeled-statement.
5715 identifier : statement
5716 case constant-expression : statement
5722 case constant-expression ... constant-expression : statement
5724 Returns the new CASE_LABEL_EXPR, for a `case' or `default' label.
5725 For an ordinary label, returns a LABEL_EXPR. */
5728 cp_parser_labeled_statement (cp_parser* parser, tree in_statement_expr)
5731 tree statement = error_mark_node;
5733 /* The next token should be an identifier. */
5734 token = cp_lexer_peek_token (parser->lexer);
5735 if (token->type != CPP_NAME
5736 && token->type != CPP_KEYWORD)
5738 cp_parser_error (parser, "expected labeled-statement");
5739 return error_mark_node;
5742 switch (token->keyword)
5749 /* Consume the `case' token. */
5750 cp_lexer_consume_token (parser->lexer);
5751 /* Parse the constant-expression. */
5752 expr = cp_parser_constant_expression (parser,
5753 /*allow_non_constant_p=*/false,
5756 ellipsis = cp_lexer_peek_token (parser->lexer);
5757 if (ellipsis->type == CPP_ELLIPSIS)
5759 /* Consume the `...' token. */
5760 cp_lexer_consume_token (parser->lexer);
5762 cp_parser_constant_expression (parser,
5763 /*allow_non_constant_p=*/false,
5765 /* We don't need to emit warnings here, as the common code
5766 will do this for us. */
5769 expr_hi = NULL_TREE;
5771 if (!parser->in_switch_statement_p)
5772 error ("case label `%E' not within a switch statement", expr);
5774 statement = finish_case_label (expr, expr_hi);
5779 /* Consume the `default' token. */
5780 cp_lexer_consume_token (parser->lexer);
5781 if (!parser->in_switch_statement_p)
5782 error ("case label not within a switch statement");
5784 statement = finish_case_label (NULL_TREE, NULL_TREE);
5788 /* Anything else must be an ordinary label. */
5789 statement = finish_label_stmt (cp_parser_identifier (parser));
5793 /* Require the `:' token. */
5794 cp_parser_require (parser, CPP_COLON, "`:'");
5795 /* Parse the labeled statement. */
5796 cp_parser_statement (parser, in_statement_expr);
5798 /* Return the label, in the case of a `case' or `default' label. */
5802 /* Parse an expression-statement.
5804 expression-statement:
5807 Returns the new EXPR_STMT -- or NULL_TREE if the expression
5808 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
5809 indicates whether this expression-statement is part of an
5810 expression statement. */
5813 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
5815 tree statement = NULL_TREE;
5817 /* If the next token is a ';', then there is no expression
5819 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
5820 statement = cp_parser_expression (parser);
5822 /* Consume the final `;'. */
5823 cp_parser_consume_semicolon_at_end_of_statement (parser);
5825 if (in_statement_expr
5826 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
5828 /* This is the final expression statement of a statement
5830 statement = finish_stmt_expr_expr (statement, in_statement_expr);
5833 statement = finish_expr_stmt (statement);
5840 /* Parse a compound-statement.
5843 { statement-seq [opt] }
5845 Returns a tree representing the statement. */
5848 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
5853 /* Consume the `{'. */
5854 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
5855 return error_mark_node;
5856 /* Begin the compound-statement. */
5857 compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
5858 /* Parse an (optional) statement-seq. */
5859 cp_parser_statement_seq_opt (parser, in_statement_expr);
5860 /* Finish the compound-statement. */
5861 finish_compound_stmt (compound_stmt);
5862 /* Consume the `}'. */
5863 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
5865 return compound_stmt;
5868 /* Parse an (optional) statement-seq.
5872 statement-seq [opt] statement */
5875 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
5877 /* Scan statements until there aren't any more. */
5880 /* If we're looking at a `}', then we've run out of statements. */
5881 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE)
5882 || cp_lexer_next_token_is (parser->lexer, CPP_EOF))
5885 /* Parse the statement. */
5886 cp_parser_statement (parser, in_statement_expr);
5890 /* Parse a selection-statement.
5892 selection-statement:
5893 if ( condition ) statement
5894 if ( condition ) statement else statement
5895 switch ( condition ) statement
5897 Returns the new IF_STMT or SWITCH_STMT. */
5900 cp_parser_selection_statement (cp_parser* parser)
5905 /* Peek at the next token. */
5906 token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
5908 /* See what kind of keyword it is. */
5909 keyword = token->keyword;
5918 /* Look for the `('. */
5919 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
5921 cp_parser_skip_to_end_of_statement (parser);
5922 return error_mark_node;
5925 /* Begin the selection-statement. */
5926 if (keyword == RID_IF)
5927 statement = begin_if_stmt ();
5929 statement = begin_switch_stmt ();
5931 /* Parse the condition. */
5932 condition = cp_parser_condition (parser);
5933 /* Look for the `)'. */
5934 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
5935 cp_parser_skip_to_closing_parenthesis (parser, true, false,
5936 /*consume_paren=*/true);
5938 if (keyword == RID_IF)
5940 /* Add the condition. */
5941 finish_if_stmt_cond (condition, statement);
5943 /* Parse the then-clause. */
5944 cp_parser_implicitly_scoped_statement (parser);
5945 finish_then_clause (statement);
5947 /* If the next token is `else', parse the else-clause. */
5948 if (cp_lexer_next_token_is_keyword (parser->lexer,
5951 /* Consume the `else' keyword. */
5952 cp_lexer_consume_token (parser->lexer);
5953 begin_else_clause (statement);
5954 /* Parse the else-clause. */
5955 cp_parser_implicitly_scoped_statement (parser);
5956 finish_else_clause (statement);
5959 /* Now we're all done with the if-statement. */
5960 finish_if_stmt (statement);
5964 bool in_switch_statement_p;
5966 /* Add the condition. */
5967 finish_switch_cond (condition, statement);
5969 /* Parse the body of the switch-statement. */
5970 in_switch_statement_p = parser->in_switch_statement_p;
5971 parser->in_switch_statement_p = true;
5972 cp_parser_implicitly_scoped_statement (parser);
5973 parser->in_switch_statement_p = in_switch_statement_p;
5975 /* Now we're all done with the switch-statement. */
5976 finish_switch_stmt (statement);
5984 cp_parser_error (parser, "expected selection-statement");
5985 return error_mark_node;
5989 /* Parse a condition.
5993 type-specifier-seq declarator = assignment-expression
5998 type-specifier-seq declarator asm-specification [opt]
5999 attributes [opt] = assignment-expression
6001 Returns the expression that should be tested. */
6004 cp_parser_condition (cp_parser* parser)
6006 tree type_specifiers;
6007 const char *saved_message;
6009 /* Try the declaration first. */
6010 cp_parser_parse_tentatively (parser);
6011 /* New types are not allowed in the type-specifier-seq for a
6013 saved_message = parser->type_definition_forbidden_message;
6014 parser->type_definition_forbidden_message
6015 = "types may not be defined in conditions";
6016 /* Parse the type-specifier-seq. */
6017 type_specifiers = cp_parser_type_specifier_seq (parser);
6018 /* Restore the saved message. */
6019 parser->type_definition_forbidden_message = saved_message;
6020 /* If all is well, we might be looking at a declaration. */
6021 if (!cp_parser_error_occurred (parser))
6024 tree asm_specification;
6027 tree initializer = NULL_TREE;
6029 /* Parse the declarator. */
6030 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
6031 /*ctor_dtor_or_conv_p=*/NULL,
6032 /*parenthesized_p=*/NULL);
6033 /* Parse the attributes. */
6034 attributes = cp_parser_attributes_opt (parser);
6035 /* Parse the asm-specification. */
6036 asm_specification = cp_parser_asm_specification_opt (parser);
6037 /* If the next token is not an `=', then we might still be
6038 looking at an expression. For example:
6042 looks like a decl-specifier-seq and a declarator -- but then
6043 there is no `=', so this is an expression. */
6044 cp_parser_require (parser, CPP_EQ, "`='");
6045 /* If we did see an `=', then we are looking at a declaration
6047 if (cp_parser_parse_definitely (parser))
6049 /* Create the declaration. */
6050 decl = start_decl (declarator, type_specifiers,
6051 /*initialized_p=*/true,
6052 attributes, /*prefix_attributes=*/NULL_TREE);
6053 /* Parse the assignment-expression. */
6054 initializer = cp_parser_assignment_expression (parser);
6056 /* Process the initializer. */
6057 cp_finish_decl (decl,
6060 LOOKUP_ONLYCONVERTING);
6062 return convert_from_reference (decl);
6065 /* If we didn't even get past the declarator successfully, we are
6066 definitely not looking at a declaration. */
6068 cp_parser_abort_tentative_parse (parser);
6070 /* Otherwise, we are looking at an expression. */
6071 return cp_parser_expression (parser);
6074 /* Parse an iteration-statement.
6076 iteration-statement:
6077 while ( condition ) statement
6078 do statement while ( expression ) ;
6079 for ( for-init-statement condition [opt] ; expression [opt] )
6082 Returns the new WHILE_STMT, DO_STMT, or FOR_STMT. */
6085 cp_parser_iteration_statement (cp_parser* parser)
6090 bool in_iteration_statement_p;
6093 /* Peek at the next token. */
6094 token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
6096 return error_mark_node;
6098 /* Remember whether or not we are already within an iteration
6100 in_iteration_statement_p = parser->in_iteration_statement_p;
6102 /* See what kind of keyword it is. */
6103 keyword = token->keyword;
6110 /* Begin the while-statement. */
6111 statement = begin_while_stmt ();
6112 /* Look for the `('. */
6113 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6114 /* Parse the condition. */
6115 condition = cp_parser_condition (parser);
6116 finish_while_stmt_cond (condition, statement);
6117 /* Look for the `)'. */
6118 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6119 /* Parse the dependent statement. */
6120 parser->in_iteration_statement_p = true;
6121 cp_parser_already_scoped_statement (parser);
6122 parser->in_iteration_statement_p = in_iteration_statement_p;
6123 /* We're done with the while-statement. */
6124 finish_while_stmt (statement);
6132 /* Begin the do-statement. */
6133 statement = begin_do_stmt ();
6134 /* Parse the body of the do-statement. */
6135 parser->in_iteration_statement_p = true;
6136 cp_parser_implicitly_scoped_statement (parser);
6137 parser->in_iteration_statement_p = in_iteration_statement_p;
6138 finish_do_body (statement);
6139 /* Look for the `while' keyword. */
6140 cp_parser_require_keyword (parser, RID_WHILE, "`while'");
6141 /* Look for the `('. */
6142 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6143 /* Parse the expression. */
6144 expression = cp_parser_expression (parser);
6145 /* We're done with the do-statement. */
6146 finish_do_stmt (expression, statement);
6147 /* Look for the `)'. */
6148 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6149 /* Look for the `;'. */
6150 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6156 tree condition = NULL_TREE;
6157 tree expression = NULL_TREE;
6159 /* Begin the for-statement. */
6160 statement = begin_for_stmt ();
6161 /* Look for the `('. */
6162 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6163 /* Parse the initialization. */
6164 cp_parser_for_init_statement (parser);
6165 finish_for_init_stmt (statement);
6167 /* If there's a condition, process it. */
6168 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6169 condition = cp_parser_condition (parser);
6170 finish_for_cond (condition, statement);
6171 /* Look for the `;'. */
6172 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6174 /* If there's an expression, process it. */
6175 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
6176 expression = cp_parser_expression (parser);
6177 finish_for_expr (expression, statement);
6178 /* Look for the `)'. */
6179 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6181 /* Parse the body of the for-statement. */
6182 parser->in_iteration_statement_p = true;
6183 cp_parser_already_scoped_statement (parser);
6184 parser->in_iteration_statement_p = in_iteration_statement_p;
6186 /* We're done with the for-statement. */
6187 finish_for_stmt (statement);
6192 cp_parser_error (parser, "expected iteration-statement");
6193 statement = error_mark_node;
6200 /* Parse a for-init-statement.
6203 expression-statement
6204 simple-declaration */
6207 cp_parser_for_init_statement (cp_parser* parser)
6209 /* If the next token is a `;', then we have an empty
6210 expression-statement. Grammatically, this is also a
6211 simple-declaration, but an invalid one, because it does not
6212 declare anything. Therefore, if we did not handle this case
6213 specially, we would issue an error message about an invalid
6215 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6217 /* We're going to speculatively look for a declaration, falling back
6218 to an expression, if necessary. */
6219 cp_parser_parse_tentatively (parser);
6220 /* Parse the declaration. */
6221 cp_parser_simple_declaration (parser,
6222 /*function_definition_allowed_p=*/false);
6223 /* If the tentative parse failed, then we shall need to look for an
6224 expression-statement. */
6225 if (cp_parser_parse_definitely (parser))
6229 cp_parser_expression_statement (parser, false);
6232 /* Parse a jump-statement.
6237 return expression [opt] ;
6245 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
6248 cp_parser_jump_statement (cp_parser* parser)
6250 tree statement = error_mark_node;
6254 /* Peek at the next token. */
6255 token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
6257 return error_mark_node;
6259 /* See what kind of keyword it is. */
6260 keyword = token->keyword;
6264 if (!parser->in_switch_statement_p
6265 && !parser->in_iteration_statement_p)
6267 error ("break statement not within loop or switch");
6268 statement = error_mark_node;
6271 statement = finish_break_stmt ();
6272 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6276 if (!parser->in_iteration_statement_p)
6278 error ("continue statement not within a loop");
6279 statement = error_mark_node;
6282 statement = finish_continue_stmt ();
6283 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6290 /* If the next token is a `;', then there is no
6292 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6293 expr = cp_parser_expression (parser);
6296 /* Build the return-statement. */
6297 statement = finish_return_stmt (expr);
6298 /* Look for the final `;'. */
6299 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6304 /* Create the goto-statement. */
6305 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
6307 /* Issue a warning about this use of a GNU extension. */
6309 pedwarn ("ISO C++ forbids computed gotos");
6310 /* Consume the '*' token. */
6311 cp_lexer_consume_token (parser->lexer);
6312 /* Parse the dependent expression. */
6313 finish_goto_stmt (cp_parser_expression (parser));
6316 finish_goto_stmt (cp_parser_identifier (parser));
6317 /* Look for the final `;'. */
6318 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6322 cp_parser_error (parser, "expected jump-statement");
6329 /* Parse a declaration-statement.
6331 declaration-statement:
6332 block-declaration */
6335 cp_parser_declaration_statement (cp_parser* parser)
6337 /* Parse the block-declaration. */
6338 cp_parser_block_declaration (parser, /*statement_p=*/true);
6340 /* Finish off the statement. */
6344 /* Some dependent statements (like `if (cond) statement'), are
6345 implicitly in their own scope. In other words, if the statement is
6346 a single statement (as opposed to a compound-statement), it is
6347 none-the-less treated as if it were enclosed in braces. Any
6348 declarations appearing in the dependent statement are out of scope
6349 after control passes that point. This function parses a statement,
6350 but ensures that is in its own scope, even if it is not a
6353 Returns the new statement. */
6356 cp_parser_implicitly_scoped_statement (cp_parser* parser)
6360 /* If the token is not a `{', then we must take special action. */
6361 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
6363 /* Create a compound-statement. */
6364 statement = begin_compound_stmt (0);
6365 /* Parse the dependent-statement. */
6366 cp_parser_statement (parser, false);
6367 /* Finish the dummy compound-statement. */
6368 finish_compound_stmt (statement);
6370 /* Otherwise, we simply parse the statement directly. */
6372 statement = cp_parser_compound_statement (parser, NULL, false);
6374 /* Return the statement. */
6378 /* For some dependent statements (like `while (cond) statement'), we
6379 have already created a scope. Therefore, even if the dependent
6380 statement is a compound-statement, we do not want to create another
6384 cp_parser_already_scoped_statement (cp_parser* parser)
6386 /* If the token is a `{', then we must take special action. */
6387 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
6388 cp_parser_statement (parser, false);
6391 /* Avoid calling cp_parser_compound_statement, so that we
6392 don't create a new scope. Do everything else by hand. */
6393 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
6394 cp_parser_statement_seq_opt (parser, false);
6395 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
6399 /* Declarations [gram.dcl.dcl] */
6401 /* Parse an optional declaration-sequence.
6405 declaration-seq declaration */
6408 cp_parser_declaration_seq_opt (cp_parser* parser)
6414 token = cp_lexer_peek_token (parser->lexer);
6416 if (token->type == CPP_CLOSE_BRACE
6417 || token->type == CPP_EOF)
6420 if (token->type == CPP_SEMICOLON)
6422 /* A declaration consisting of a single semicolon is
6423 invalid. Allow it unless we're being pedantic. */
6424 if (pedantic && !in_system_header)
6425 pedwarn ("extra `;'");
6426 cp_lexer_consume_token (parser->lexer);
6430 /* The C lexer modifies PENDING_LANG_CHANGE when it wants the
6431 parser to enter or exit implicit `extern "C"' blocks. */
6432 while (pending_lang_change > 0)
6434 push_lang_context (lang_name_c);
6435 --pending_lang_change;
6437 while (pending_lang_change < 0)
6439 pop_lang_context ();
6440 ++pending_lang_change;
6443 /* Parse the declaration itself. */
6444 cp_parser_declaration (parser);
6448 /* Parse a declaration.
6453 template-declaration
6454 explicit-instantiation
6455 explicit-specialization
6456 linkage-specification
6457 namespace-definition
6462 __extension__ declaration */
6465 cp_parser_declaration (cp_parser* parser)
6471 /* Set this here since we can be called after
6472 pushing the linkage specification. */
6473 c_lex_string_translate = 1;
6475 /* Check for the `__extension__' keyword. */
6476 if (cp_parser_extension_opt (parser, &saved_pedantic))
6478 /* Parse the qualified declaration. */
6479 cp_parser_declaration (parser);
6480 /* Restore the PEDANTIC flag. */
6481 pedantic = saved_pedantic;
6486 /* Try to figure out what kind of declaration is present. */
6487 token1 = *cp_lexer_peek_token (parser->lexer);
6489 /* Don't translate the CPP_STRING in extern "C". */
6490 if (token1.keyword == RID_EXTERN)
6491 c_lex_string_translate = 0;
6493 if (token1.type != CPP_EOF)
6494 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
6496 c_lex_string_translate = 1;
6498 /* If the next token is `extern' and the following token is a string
6499 literal, then we have a linkage specification. */
6500 if (token1.keyword == RID_EXTERN
6501 && cp_parser_is_string_literal (&token2))
6502 cp_parser_linkage_specification (parser);
6503 /* If the next token is `template', then we have either a template
6504 declaration, an explicit instantiation, or an explicit
6506 else if (token1.keyword == RID_TEMPLATE)
6508 /* `template <>' indicates a template specialization. */
6509 if (token2.type == CPP_LESS
6510 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
6511 cp_parser_explicit_specialization (parser);
6512 /* `template <' indicates a template declaration. */
6513 else if (token2.type == CPP_LESS)
6514 cp_parser_template_declaration (parser, /*member_p=*/false);
6515 /* Anything else must be an explicit instantiation. */
6517 cp_parser_explicit_instantiation (parser);
6519 /* If the next token is `export', then we have a template
6521 else if (token1.keyword == RID_EXPORT)
6522 cp_parser_template_declaration (parser, /*member_p=*/false);
6523 /* If the next token is `extern', 'static' or 'inline' and the one
6524 after that is `template', we have a GNU extended explicit
6525 instantiation directive. */
6526 else if (cp_parser_allow_gnu_extensions_p (parser)
6527 && (token1.keyword == RID_EXTERN
6528 || token1.keyword == RID_STATIC
6529 || token1.keyword == RID_INLINE)
6530 && token2.keyword == RID_TEMPLATE)
6531 cp_parser_explicit_instantiation (parser);
6532 /* If the next token is `namespace', check for a named or unnamed
6533 namespace definition. */
6534 else if (token1.keyword == RID_NAMESPACE
6535 && (/* A named namespace definition. */
6536 (token2.type == CPP_NAME
6537 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
6539 /* An unnamed namespace definition. */
6540 || token2.type == CPP_OPEN_BRACE))
6541 cp_parser_namespace_definition (parser);
6542 /* We must have either a block declaration or a function
6545 /* Try to parse a block-declaration, or a function-definition. */
6546 cp_parser_block_declaration (parser, /*statement_p=*/false);
6549 /* Parse a block-declaration.
6554 namespace-alias-definition
6561 __extension__ block-declaration
6564 If STATEMENT_P is TRUE, then this block-declaration is occurring as
6565 part of a declaration-statement. */
6568 cp_parser_block_declaration (cp_parser *parser,
6574 /* Check for the `__extension__' keyword. */
6575 if (cp_parser_extension_opt (parser, &saved_pedantic))
6577 /* Parse the qualified declaration. */
6578 cp_parser_block_declaration (parser, statement_p);
6579 /* Restore the PEDANTIC flag. */
6580 pedantic = saved_pedantic;
6585 /* Peek at the next token to figure out which kind of declaration is
6587 token1 = cp_lexer_peek_token (parser->lexer);
6589 /* If the next keyword is `asm', we have an asm-definition. */
6590 if (token1->keyword == RID_ASM)
6593 cp_parser_commit_to_tentative_parse (parser);
6594 cp_parser_asm_definition (parser);
6596 /* If the next keyword is `namespace', we have a
6597 namespace-alias-definition. */
6598 else if (token1->keyword == RID_NAMESPACE)
6599 cp_parser_namespace_alias_definition (parser);
6600 /* If the next keyword is `using', we have either a
6601 using-declaration or a using-directive. */
6602 else if (token1->keyword == RID_USING)
6607 cp_parser_commit_to_tentative_parse (parser);
6608 /* If the token after `using' is `namespace', then we have a
6610 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
6611 if (token2->keyword == RID_NAMESPACE)
6612 cp_parser_using_directive (parser);
6613 /* Otherwise, it's a using-declaration. */
6615 cp_parser_using_declaration (parser);
6617 /* If the next keyword is `__label__' we have a label declaration. */
6618 else if (token1->keyword == RID_LABEL)
6621 cp_parser_commit_to_tentative_parse (parser);
6622 cp_parser_label_declaration (parser);
6624 /* Anything else must be a simple-declaration. */
6626 cp_parser_simple_declaration (parser, !statement_p);
6629 /* Parse a simple-declaration.
6632 decl-specifier-seq [opt] init-declarator-list [opt] ;
6634 init-declarator-list:
6636 init-declarator-list , init-declarator
6638 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
6639 function-definition as a simple-declaration. */
6642 cp_parser_simple_declaration (cp_parser* parser,
6643 bool function_definition_allowed_p)
6645 tree decl_specifiers;
6647 int declares_class_or_enum;
6648 bool saw_declarator;
6650 /* Defer access checks until we know what is being declared; the
6651 checks for names appearing in the decl-specifier-seq should be
6652 done as if we were in the scope of the thing being declared. */
6653 push_deferring_access_checks (dk_deferred);
6655 /* Parse the decl-specifier-seq. We have to keep track of whether
6656 or not the decl-specifier-seq declares a named class or
6657 enumeration type, since that is the only case in which the
6658 init-declarator-list is allowed to be empty.
6662 In a simple-declaration, the optional init-declarator-list can be
6663 omitted only when declaring a class or enumeration, that is when
6664 the decl-specifier-seq contains either a class-specifier, an
6665 elaborated-type-specifier, or an enum-specifier. */
6667 = cp_parser_decl_specifier_seq (parser,
6668 CP_PARSER_FLAGS_OPTIONAL,
6670 &declares_class_or_enum);
6671 /* We no longer need to defer access checks. */
6672 stop_deferring_access_checks ();
6674 /* In a block scope, a valid declaration must always have a
6675 decl-specifier-seq. By not trying to parse declarators, we can
6676 resolve the declaration/expression ambiguity more quickly. */
6677 if (!function_definition_allowed_p && !decl_specifiers)
6679 cp_parser_error (parser, "expected declaration");
6683 /* If the next two tokens are both identifiers, the code is
6684 erroneous. The usual cause of this situation is code like:
6688 where "T" should name a type -- but does not. */
6689 if (cp_parser_parse_and_diagnose_invalid_type_name (parser))
6691 /* If parsing tentatively, we should commit; we really are
6692 looking at a declaration. */
6693 cp_parser_commit_to_tentative_parse (parser);
6698 /* Keep going until we hit the `;' at the end of the simple
6700 saw_declarator = false;
6701 while (cp_lexer_next_token_is_not (parser->lexer,
6705 bool function_definition_p;
6708 saw_declarator = true;
6709 /* Parse the init-declarator. */
6710 decl = cp_parser_init_declarator (parser, decl_specifiers, attributes,
6711 function_definition_allowed_p,
6713 declares_class_or_enum,
6714 &function_definition_p);
6715 /* If an error occurred while parsing tentatively, exit quickly.
6716 (That usually happens when in the body of a function; each
6717 statement is treated as a declaration-statement until proven
6719 if (cp_parser_error_occurred (parser))
6721 /* Handle function definitions specially. */
6722 if (function_definition_p)
6724 /* If the next token is a `,', then we are probably
6725 processing something like:
6729 which is erroneous. */
6730 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
6731 error ("mixing declarations and function-definitions is forbidden");
6732 /* Otherwise, we're done with the list of declarators. */
6735 pop_deferring_access_checks ();
6739 /* The next token should be either a `,' or a `;'. */
6740 token = cp_lexer_peek_token (parser->lexer);
6741 /* If it's a `,', there are more declarators to come. */
6742 if (token->type == CPP_COMMA)
6743 cp_lexer_consume_token (parser->lexer);
6744 /* If it's a `;', we are done. */
6745 else if (token->type == CPP_SEMICOLON)
6747 /* Anything else is an error. */
6750 cp_parser_error (parser, "expected `,' or `;'");
6751 /* Skip tokens until we reach the end of the statement. */
6752 cp_parser_skip_to_end_of_statement (parser);
6753 /* If the next token is now a `;', consume it. */
6754 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
6755 cp_lexer_consume_token (parser->lexer);
6758 /* After the first time around, a function-definition is not
6759 allowed -- even if it was OK at first. For example:
6764 function_definition_allowed_p = false;
6767 /* Issue an error message if no declarators are present, and the
6768 decl-specifier-seq does not itself declare a class or
6770 if (!saw_declarator)
6772 if (cp_parser_declares_only_class_p (parser))
6773 shadow_tag (decl_specifiers);
6774 /* Perform any deferred access checks. */
6775 perform_deferred_access_checks ();
6778 /* Consume the `;'. */
6779 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6782 pop_deferring_access_checks ();
6785 /* Parse a decl-specifier-seq.
6788 decl-specifier-seq [opt] decl-specifier
6791 storage-class-specifier
6802 Returns a TREE_LIST, giving the decl-specifiers in the order they
6803 appear in the source code. The TREE_VALUE of each node is the
6804 decl-specifier. For a keyword (such as `auto' or `friend'), the
6805 TREE_VALUE is simply the corresponding TREE_IDENTIFIER. For the
6806 representation of a type-specifier, see cp_parser_type_specifier.
6808 If there are attributes, they will be stored in *ATTRIBUTES,
6809 represented as described above cp_parser_attributes.
6811 If FRIEND_IS_NOT_CLASS_P is non-NULL, and the `friend' specifier
6812 appears, and the entity that will be a friend is not going to be a
6813 class, then *FRIEND_IS_NOT_CLASS_P will be set to TRUE. Note that
6814 even if *FRIEND_IS_NOT_CLASS_P is FALSE, the entity to which
6815 friendship is granted might not be a class.
6817 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
6820 1: one of the decl-specifiers is an elaborated-type-specifier
6821 (i.e., a type declaration)
6822 2: one of the decl-specifiers is an enum-specifier or a
6823 class-specifier (i.e., a type definition)
6828 cp_parser_decl_specifier_seq (cp_parser* parser,
6829 cp_parser_flags flags,
6831 int* declares_class_or_enum)
6833 tree decl_specs = NULL_TREE;
6834 bool friend_p = false;
6835 bool constructor_possible_p = !parser->in_declarator_p;
6837 /* Assume no class or enumeration type is declared. */
6838 *declares_class_or_enum = 0;
6840 /* Assume there are no attributes. */
6841 *attributes = NULL_TREE;
6843 /* Keep reading specifiers until there are no more to read. */
6846 tree decl_spec = NULL_TREE;
6850 /* Peek at the next token. */
6851 token = cp_lexer_peek_token (parser->lexer);
6852 /* Handle attributes. */
6853 if (token->keyword == RID_ATTRIBUTE)
6855 /* Parse the attributes. */
6856 decl_spec = cp_parser_attributes_opt (parser);
6857 /* Add them to the list. */
6858 *attributes = chainon (*attributes, decl_spec);
6861 /* If the next token is an appropriate keyword, we can simply
6862 add it to the list. */
6863 switch (token->keyword)
6869 error ("duplicate `friend'");
6872 /* The representation of the specifier is simply the
6873 appropriate TREE_IDENTIFIER node. */
6874 decl_spec = token->value;
6875 /* Consume the token. */
6876 cp_lexer_consume_token (parser->lexer);
6879 /* function-specifier:
6886 decl_spec = cp_parser_function_specifier_opt (parser);
6892 /* The representation of the specifier is simply the
6893 appropriate TREE_IDENTIFIER node. */
6894 decl_spec = token->value;
6895 /* Consume the token. */
6896 cp_lexer_consume_token (parser->lexer);
6897 /* A constructor declarator cannot appear in a typedef. */
6898 constructor_possible_p = false;
6899 /* The "typedef" keyword can only occur in a declaration; we
6900 may as well commit at this point. */
6901 cp_parser_commit_to_tentative_parse (parser);
6904 /* storage-class-specifier:
6919 decl_spec = cp_parser_storage_class_specifier_opt (parser);
6926 /* Constructors are a special case. The `S' in `S()' is not a
6927 decl-specifier; it is the beginning of the declarator. */
6928 constructor_p = (!decl_spec
6929 && constructor_possible_p
6930 && cp_parser_constructor_declarator_p (parser,
6933 /* If we don't have a DECL_SPEC yet, then we must be looking at
6934 a type-specifier. */
6935 if (!decl_spec && !constructor_p)
6937 int decl_spec_declares_class_or_enum;
6938 bool is_cv_qualifier;
6941 = cp_parser_type_specifier (parser, flags,
6943 /*is_declaration=*/true,
6944 &decl_spec_declares_class_or_enum,
6947 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
6949 /* If this type-specifier referenced a user-defined type
6950 (a typedef, class-name, etc.), then we can't allow any
6951 more such type-specifiers henceforth.
6955 The longest sequence of decl-specifiers that could
6956 possibly be a type name is taken as the
6957 decl-specifier-seq of a declaration. The sequence shall
6958 be self-consistent as described below.
6962 As a general rule, at most one type-specifier is allowed
6963 in the complete decl-specifier-seq of a declaration. The
6964 only exceptions are the following:
6966 -- const or volatile can be combined with any other
6969 -- signed or unsigned can be combined with char, long,
6977 void g (const int Pc);
6979 Here, Pc is *not* part of the decl-specifier seq; it's
6980 the declarator. Therefore, once we see a type-specifier
6981 (other than a cv-qualifier), we forbid any additional
6982 user-defined types. We *do* still allow things like `int
6983 int' to be considered a decl-specifier-seq, and issue the
6984 error message later. */
6985 if (decl_spec && !is_cv_qualifier)
6986 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
6987 /* A constructor declarator cannot follow a type-specifier. */
6989 constructor_possible_p = false;
6992 /* If we still do not have a DECL_SPEC, then there are no more
6996 /* Issue an error message, unless the entire construct was
6998 if (!(flags & CP_PARSER_FLAGS_OPTIONAL))
7000 cp_parser_error (parser, "expected decl specifier");
7001 return error_mark_node;
7007 /* Add the DECL_SPEC to the list of specifiers. */
7008 if (decl_specs == NULL || TREE_VALUE (decl_specs) != error_mark_node)
7009 decl_specs = tree_cons (NULL_TREE, decl_spec, decl_specs);
7011 /* After we see one decl-specifier, further decl-specifiers are
7013 flags |= CP_PARSER_FLAGS_OPTIONAL;
7016 /* Don't allow a friend specifier with a class definition. */
7017 if (friend_p && (*declares_class_or_enum & 2))
7018 error ("class definition may not be declared a friend");
7020 /* We have built up the DECL_SPECS in reverse order. Return them in
7021 the correct order. */
7022 return nreverse (decl_specs);
7025 /* Parse an (optional) storage-class-specifier.
7027 storage-class-specifier:
7036 storage-class-specifier:
7039 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
7042 cp_parser_storage_class_specifier_opt (cp_parser* parser)
7044 switch (cp_lexer_peek_token (parser->lexer)->keyword)
7052 /* Consume the token. */
7053 return cp_lexer_consume_token (parser->lexer)->value;
7060 /* Parse an (optional) function-specifier.
7067 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
7070 cp_parser_function_specifier_opt (cp_parser* parser)
7072 switch (cp_lexer_peek_token (parser->lexer)->keyword)
7077 /* Consume the token. */
7078 return cp_lexer_consume_token (parser->lexer)->value;
7085 /* Parse a linkage-specification.
7087 linkage-specification:
7088 extern string-literal { declaration-seq [opt] }
7089 extern string-literal declaration */
7092 cp_parser_linkage_specification (cp_parser* parser)
7097 /* Look for the `extern' keyword. */
7098 cp_parser_require_keyword (parser, RID_EXTERN, "`extern'");
7100 /* Peek at the next token. */
7101 token = cp_lexer_peek_token (parser->lexer);
7102 /* If it's not a string-literal, then there's a problem. */
7103 if (!cp_parser_is_string_literal (token))
7105 cp_parser_error (parser, "expected language-name");
7108 /* Consume the token. */
7109 cp_lexer_consume_token (parser->lexer);
7111 /* Transform the literal into an identifier. If the literal is a
7112 wide-character string, or contains embedded NULs, then we can't
7113 handle it as the user wants. */
7114 if (token->type == CPP_WSTRING
7115 || (strlen (TREE_STRING_POINTER (token->value))
7116 != (size_t) (TREE_STRING_LENGTH (token->value) - 1)))
7118 cp_parser_error (parser, "invalid linkage-specification");
7119 /* Assume C++ linkage. */
7120 linkage = get_identifier ("c++");
7122 /* If the string is chained to another string, take the latter,
7123 that's the untranslated string. */
7124 else if (TREE_CHAIN (token->value))
7125 linkage = get_identifier (TREE_STRING_POINTER (TREE_CHAIN (token->value)));
7126 /* If it's a simple string constant, things are easier. */
7128 linkage = get_identifier (TREE_STRING_POINTER (token->value));
7130 /* We're now using the new linkage. */
7131 push_lang_context (linkage);
7133 /* If the next token is a `{', then we're using the first
7135 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7137 /* Consume the `{' token. */
7138 cp_lexer_consume_token (parser->lexer);
7139 /* Parse the declarations. */
7140 cp_parser_declaration_seq_opt (parser);
7141 /* Look for the closing `}'. */
7142 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
7144 /* Otherwise, there's just one declaration. */
7147 bool saved_in_unbraced_linkage_specification_p;
7149 saved_in_unbraced_linkage_specification_p
7150 = parser->in_unbraced_linkage_specification_p;
7151 parser->in_unbraced_linkage_specification_p = true;
7152 have_extern_spec = true;
7153 cp_parser_declaration (parser);
7154 have_extern_spec = false;
7155 parser->in_unbraced_linkage_specification_p
7156 = saved_in_unbraced_linkage_specification_p;
7159 /* We're done with the linkage-specification. */
7160 pop_lang_context ();
7163 /* Special member functions [gram.special] */
7165 /* Parse a conversion-function-id.
7167 conversion-function-id:
7168 operator conversion-type-id
7170 Returns an IDENTIFIER_NODE representing the operator. */
7173 cp_parser_conversion_function_id (cp_parser* parser)
7177 tree saved_qualifying_scope;
7178 tree saved_object_scope;
7181 /* Look for the `operator' token. */
7182 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
7183 return error_mark_node;
7184 /* When we parse the conversion-type-id, the current scope will be
7185 reset. However, we need that information in able to look up the
7186 conversion function later, so we save it here. */
7187 saved_scope = parser->scope;
7188 saved_qualifying_scope = parser->qualifying_scope;
7189 saved_object_scope = parser->object_scope;
7190 /* We must enter the scope of the class so that the names of
7191 entities declared within the class are available in the
7192 conversion-type-id. For example, consider:
7199 S::operator I() { ... }
7201 In order to see that `I' is a type-name in the definition, we
7202 must be in the scope of `S'. */
7204 pop_p = push_scope (saved_scope);
7205 /* Parse the conversion-type-id. */
7206 type = cp_parser_conversion_type_id (parser);
7207 /* Leave the scope of the class, if any. */
7209 pop_scope (saved_scope);
7210 /* Restore the saved scope. */
7211 parser->scope = saved_scope;
7212 parser->qualifying_scope = saved_qualifying_scope;
7213 parser->object_scope = saved_object_scope;
7214 /* If the TYPE is invalid, indicate failure. */
7215 if (type == error_mark_node)
7216 return error_mark_node;
7217 return mangle_conv_op_name_for_type (type);
7220 /* Parse a conversion-type-id:
7223 type-specifier-seq conversion-declarator [opt]
7225 Returns the TYPE specified. */
7228 cp_parser_conversion_type_id (cp_parser* parser)
7231 tree type_specifiers;
7234 /* Parse the attributes. */
7235 attributes = cp_parser_attributes_opt (parser);
7236 /* Parse the type-specifiers. */
7237 type_specifiers = cp_parser_type_specifier_seq (parser);
7238 /* If that didn't work, stop. */
7239 if (type_specifiers == error_mark_node)
7240 return error_mark_node;
7241 /* Parse the conversion-declarator. */
7242 declarator = cp_parser_conversion_declarator_opt (parser);
7244 return grokdeclarator (declarator, type_specifiers, TYPENAME,
7245 /*initialized=*/0, &attributes);
7248 /* Parse an (optional) conversion-declarator.
7250 conversion-declarator:
7251 ptr-operator conversion-declarator [opt]
7253 Returns a representation of the declarator. See
7254 cp_parser_declarator for details. */
7257 cp_parser_conversion_declarator_opt (cp_parser* parser)
7259 enum tree_code code;
7261 tree cv_qualifier_seq;
7263 /* We don't know if there's a ptr-operator next, or not. */
7264 cp_parser_parse_tentatively (parser);
7265 /* Try the ptr-operator. */
7266 code = cp_parser_ptr_operator (parser, &class_type,
7268 /* If it worked, look for more conversion-declarators. */
7269 if (cp_parser_parse_definitely (parser))
7273 /* Parse another optional declarator. */
7274 declarator = cp_parser_conversion_declarator_opt (parser);
7276 /* Create the representation of the declarator. */
7277 if (code == INDIRECT_REF)
7278 declarator = make_pointer_declarator (cv_qualifier_seq,
7281 declarator = make_reference_declarator (cv_qualifier_seq,
7284 /* Handle the pointer-to-member case. */
7286 declarator = build_nt (SCOPE_REF, class_type, declarator);
7294 /* Parse an (optional) ctor-initializer.
7297 : mem-initializer-list
7299 Returns TRUE iff the ctor-initializer was actually present. */
7302 cp_parser_ctor_initializer_opt (cp_parser* parser)
7304 /* If the next token is not a `:', then there is no
7305 ctor-initializer. */
7306 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
7308 /* Do default initialization of any bases and members. */
7309 if (DECL_CONSTRUCTOR_P (current_function_decl))
7310 finish_mem_initializers (NULL_TREE);
7315 /* Consume the `:' token. */
7316 cp_lexer_consume_token (parser->lexer);
7317 /* And the mem-initializer-list. */
7318 cp_parser_mem_initializer_list (parser);
7323 /* Parse a mem-initializer-list.
7325 mem-initializer-list:
7327 mem-initializer , mem-initializer-list */
7330 cp_parser_mem_initializer_list (cp_parser* parser)
7332 tree mem_initializer_list = NULL_TREE;
7334 /* Let the semantic analysis code know that we are starting the
7335 mem-initializer-list. */
7336 if (!DECL_CONSTRUCTOR_P (current_function_decl))
7337 error ("only constructors take base initializers");
7339 /* Loop through the list. */
7342 tree mem_initializer;
7344 /* Parse the mem-initializer. */
7345 mem_initializer = cp_parser_mem_initializer (parser);
7346 /* Add it to the list, unless it was erroneous. */
7347 if (mem_initializer)
7349 TREE_CHAIN (mem_initializer) = mem_initializer_list;
7350 mem_initializer_list = mem_initializer;
7352 /* If the next token is not a `,', we're done. */
7353 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7355 /* Consume the `,' token. */
7356 cp_lexer_consume_token (parser->lexer);
7359 /* Perform semantic analysis. */
7360 if (DECL_CONSTRUCTOR_P (current_function_decl))
7361 finish_mem_initializers (mem_initializer_list);
7364 /* Parse a mem-initializer.
7367 mem-initializer-id ( expression-list [opt] )
7372 ( expression-list [opt] )
7374 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
7375 class) or FIELD_DECL (for a non-static data member) to initialize;
7376 the TREE_VALUE is the expression-list. */
7379 cp_parser_mem_initializer (cp_parser* parser)
7381 tree mem_initializer_id;
7382 tree expression_list;
7385 /* Find out what is being initialized. */
7386 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7388 pedwarn ("anachronistic old-style base class initializer");
7389 mem_initializer_id = NULL_TREE;
7392 mem_initializer_id = cp_parser_mem_initializer_id (parser);
7393 member = expand_member_init (mem_initializer_id);
7394 if (member && !DECL_P (member))
7395 in_base_initializer = 1;
7398 = cp_parser_parenthesized_expression_list (parser, false,
7399 /*non_constant_p=*/NULL);
7400 if (!expression_list)
7401 expression_list = void_type_node;
7403 in_base_initializer = 0;
7405 return member ? build_tree_list (member, expression_list) : NULL_TREE;
7408 /* Parse a mem-initializer-id.
7411 :: [opt] nested-name-specifier [opt] class-name
7414 Returns a TYPE indicating the class to be initializer for the first
7415 production. Returns an IDENTIFIER_NODE indicating the data member
7416 to be initialized for the second production. */
7419 cp_parser_mem_initializer_id (cp_parser* parser)
7421 bool global_scope_p;
7422 bool nested_name_specifier_p;
7423 bool template_p = false;
7426 /* `typename' is not allowed in this context ([temp.res]). */
7427 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
7429 error ("keyword `typename' not allowed in this context (a qualified "
7430 "member initializer is implicitly a type)");
7431 cp_lexer_consume_token (parser->lexer);
7433 /* Look for the optional `::' operator. */
7435 = (cp_parser_global_scope_opt (parser,
7436 /*current_scope_valid_p=*/false)
7438 /* Look for the optional nested-name-specifier. The simplest way to
7443 The keyword `typename' is not permitted in a base-specifier or
7444 mem-initializer; in these contexts a qualified name that
7445 depends on a template-parameter is implicitly assumed to be a
7448 is to assume that we have seen the `typename' keyword at this
7450 nested_name_specifier_p
7451 = (cp_parser_nested_name_specifier_opt (parser,
7452 /*typename_keyword_p=*/true,
7453 /*check_dependency_p=*/true,
7455 /*is_declaration=*/true)
7457 if (nested_name_specifier_p)
7458 template_p = cp_parser_optional_template_keyword (parser);
7459 /* If there is a `::' operator or a nested-name-specifier, then we
7460 are definitely looking for a class-name. */
7461 if (global_scope_p || nested_name_specifier_p)
7462 return cp_parser_class_name (parser,
7463 /*typename_keyword_p=*/true,
7464 /*template_keyword_p=*/template_p,
7466 /*check_dependency_p=*/true,
7467 /*class_head_p=*/false,
7468 /*is_declaration=*/true);
7469 /* Otherwise, we could also be looking for an ordinary identifier. */
7470 cp_parser_parse_tentatively (parser);
7471 /* Try a class-name. */
7472 id = cp_parser_class_name (parser,
7473 /*typename_keyword_p=*/true,
7474 /*template_keyword_p=*/false,
7476 /*check_dependency_p=*/true,
7477 /*class_head_p=*/false,
7478 /*is_declaration=*/true);
7479 /* If we found one, we're done. */
7480 if (cp_parser_parse_definitely (parser))
7482 /* Otherwise, look for an ordinary identifier. */
7483 return cp_parser_identifier (parser);
7486 /* Overloading [gram.over] */
7488 /* Parse an operator-function-id.
7490 operator-function-id:
7493 Returns an IDENTIFIER_NODE for the operator which is a
7494 human-readable spelling of the identifier, e.g., `operator +'. */
7497 cp_parser_operator_function_id (cp_parser* parser)
7499 /* Look for the `operator' keyword. */
7500 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
7501 return error_mark_node;
7502 /* And then the name of the operator itself. */
7503 return cp_parser_operator (parser);
7506 /* Parse an operator.
7509 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
7510 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
7511 || ++ -- , ->* -> () []
7518 Returns an IDENTIFIER_NODE for the operator which is a
7519 human-readable spelling of the identifier, e.g., `operator +'. */
7522 cp_parser_operator (cp_parser* parser)
7524 tree id = NULL_TREE;
7527 /* Peek at the next token. */
7528 token = cp_lexer_peek_token (parser->lexer);
7529 /* Figure out which operator we have. */
7530 switch (token->type)
7536 /* The keyword should be either `new' or `delete'. */
7537 if (token->keyword == RID_NEW)
7539 else if (token->keyword == RID_DELETE)
7544 /* Consume the `new' or `delete' token. */
7545 cp_lexer_consume_token (parser->lexer);
7547 /* Peek at the next token. */
7548 token = cp_lexer_peek_token (parser->lexer);
7549 /* If it's a `[' token then this is the array variant of the
7551 if (token->type == CPP_OPEN_SQUARE)
7553 /* Consume the `[' token. */
7554 cp_lexer_consume_token (parser->lexer);
7555 /* Look for the `]' token. */
7556 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
7557 id = ansi_opname (op == NEW_EXPR
7558 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
7560 /* Otherwise, we have the non-array variant. */
7562 id = ansi_opname (op);
7568 id = ansi_opname (PLUS_EXPR);
7572 id = ansi_opname (MINUS_EXPR);
7576 id = ansi_opname (MULT_EXPR);
7580 id = ansi_opname (TRUNC_DIV_EXPR);
7584 id = ansi_opname (TRUNC_MOD_EXPR);
7588 id = ansi_opname (BIT_XOR_EXPR);
7592 id = ansi_opname (BIT_AND_EXPR);
7596 id = ansi_opname (BIT_IOR_EXPR);
7600 id = ansi_opname (BIT_NOT_EXPR);
7604 id = ansi_opname (TRUTH_NOT_EXPR);
7608 id = ansi_assopname (NOP_EXPR);
7612 id = ansi_opname (LT_EXPR);
7616 id = ansi_opname (GT_EXPR);
7620 id = ansi_assopname (PLUS_EXPR);
7624 id = ansi_assopname (MINUS_EXPR);
7628 id = ansi_assopname (MULT_EXPR);
7632 id = ansi_assopname (TRUNC_DIV_EXPR);
7636 id = ansi_assopname (TRUNC_MOD_EXPR);
7640 id = ansi_assopname (BIT_XOR_EXPR);
7644 id = ansi_assopname (BIT_AND_EXPR);
7648 id = ansi_assopname (BIT_IOR_EXPR);
7652 id = ansi_opname (LSHIFT_EXPR);
7656 id = ansi_opname (RSHIFT_EXPR);
7660 id = ansi_assopname (LSHIFT_EXPR);
7664 id = ansi_assopname (RSHIFT_EXPR);
7668 id = ansi_opname (EQ_EXPR);
7672 id = ansi_opname (NE_EXPR);
7676 id = ansi_opname (LE_EXPR);
7679 case CPP_GREATER_EQ:
7680 id = ansi_opname (GE_EXPR);
7684 id = ansi_opname (TRUTH_ANDIF_EXPR);
7688 id = ansi_opname (TRUTH_ORIF_EXPR);
7692 id = ansi_opname (POSTINCREMENT_EXPR);
7695 case CPP_MINUS_MINUS:
7696 id = ansi_opname (PREDECREMENT_EXPR);
7700 id = ansi_opname (COMPOUND_EXPR);
7703 case CPP_DEREF_STAR:
7704 id = ansi_opname (MEMBER_REF);
7708 id = ansi_opname (COMPONENT_REF);
7711 case CPP_OPEN_PAREN:
7712 /* Consume the `('. */
7713 cp_lexer_consume_token (parser->lexer);
7714 /* Look for the matching `)'. */
7715 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
7716 return ansi_opname (CALL_EXPR);
7718 case CPP_OPEN_SQUARE:
7719 /* Consume the `['. */
7720 cp_lexer_consume_token (parser->lexer);
7721 /* Look for the matching `]'. */
7722 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
7723 return ansi_opname (ARRAY_REF);
7727 id = ansi_opname (MIN_EXPR);
7731 id = ansi_opname (MAX_EXPR);
7735 id = ansi_assopname (MIN_EXPR);
7739 id = ansi_assopname (MAX_EXPR);
7743 /* Anything else is an error. */
7747 /* If we have selected an identifier, we need to consume the
7750 cp_lexer_consume_token (parser->lexer);
7751 /* Otherwise, no valid operator name was present. */
7754 cp_parser_error (parser, "expected operator");
7755 id = error_mark_node;
7761 /* Parse a template-declaration.
7763 template-declaration:
7764 export [opt] template < template-parameter-list > declaration
7766 If MEMBER_P is TRUE, this template-declaration occurs within a
7769 The grammar rule given by the standard isn't correct. What
7772 template-declaration:
7773 export [opt] template-parameter-list-seq
7774 decl-specifier-seq [opt] init-declarator [opt] ;
7775 export [opt] template-parameter-list-seq
7778 template-parameter-list-seq:
7779 template-parameter-list-seq [opt]
7780 template < template-parameter-list > */
7783 cp_parser_template_declaration (cp_parser* parser, bool member_p)
7785 /* Check for `export'. */
7786 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
7788 /* Consume the `export' token. */
7789 cp_lexer_consume_token (parser->lexer);
7790 /* Warn that we do not support `export'. */
7791 warning ("keyword `export' not implemented, and will be ignored");
7794 cp_parser_template_declaration_after_export (parser, member_p);
7797 /* Parse a template-parameter-list.
7799 template-parameter-list:
7801 template-parameter-list , template-parameter
7803 Returns a TREE_LIST. Each node represents a template parameter.
7804 The nodes are connected via their TREE_CHAINs. */
7807 cp_parser_template_parameter_list (cp_parser* parser)
7809 tree parameter_list = NULL_TREE;
7816 /* Parse the template-parameter. */
7817 parameter = cp_parser_template_parameter (parser);
7818 /* Add it to the list. */
7819 parameter_list = process_template_parm (parameter_list,
7822 /* Peek at the next token. */
7823 token = cp_lexer_peek_token (parser->lexer);
7824 /* If it's not a `,', we're done. */
7825 if (token->type != CPP_COMMA)
7827 /* Otherwise, consume the `,' token. */
7828 cp_lexer_consume_token (parser->lexer);
7831 return parameter_list;
7834 /* Parse a template-parameter.
7838 parameter-declaration
7840 Returns a TREE_LIST. The TREE_VALUE represents the parameter. The
7841 TREE_PURPOSE is the default value, if any. */
7844 cp_parser_template_parameter (cp_parser* parser)
7848 /* Peek at the next token. */
7849 token = cp_lexer_peek_token (parser->lexer);
7850 /* If it is `class' or `template', we have a type-parameter. */
7851 if (token->keyword == RID_TEMPLATE)
7852 return cp_parser_type_parameter (parser);
7853 /* If it is `class' or `typename' we do not know yet whether it is a
7854 type parameter or a non-type parameter. Consider:
7856 template <typename T, typename T::X X> ...
7860 template <class C, class D*> ...
7862 Here, the first parameter is a type parameter, and the second is
7863 a non-type parameter. We can tell by looking at the token after
7864 the identifier -- if it is a `,', `=', or `>' then we have a type
7866 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
7868 /* Peek at the token after `class' or `typename'. */
7869 token = cp_lexer_peek_nth_token (parser->lexer, 2);
7870 /* If it's an identifier, skip it. */
7871 if (token->type == CPP_NAME)
7872 token = cp_lexer_peek_nth_token (parser->lexer, 3);
7873 /* Now, see if the token looks like the end of a template
7875 if (token->type == CPP_COMMA
7876 || token->type == CPP_EQ
7877 || token->type == CPP_GREATER)
7878 return cp_parser_type_parameter (parser);
7881 /* Otherwise, it is a non-type parameter.
7885 When parsing a default template-argument for a non-type
7886 template-parameter, the first non-nested `>' is taken as the end
7887 of the template parameter-list rather than a greater-than
7890 cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
7891 /*parenthesized_p=*/NULL);
7894 /* Parse a type-parameter.
7897 class identifier [opt]
7898 class identifier [opt] = type-id
7899 typename identifier [opt]
7900 typename identifier [opt] = type-id
7901 template < template-parameter-list > class identifier [opt]
7902 template < template-parameter-list > class identifier [opt]
7905 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
7906 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
7907 the declaration of the parameter. */
7910 cp_parser_type_parameter (cp_parser* parser)
7915 /* Look for a keyword to tell us what kind of parameter this is. */
7916 token = cp_parser_require (parser, CPP_KEYWORD,
7917 "`class', `typename', or `template'");
7919 return error_mark_node;
7921 switch (token->keyword)
7927 tree default_argument;
7929 /* If the next token is an identifier, then it names the
7931 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
7932 identifier = cp_parser_identifier (parser);
7934 identifier = NULL_TREE;
7936 /* Create the parameter. */
7937 parameter = finish_template_type_parm (class_type_node, identifier);
7939 /* If the next token is an `=', we have a default argument. */
7940 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7942 /* Consume the `=' token. */
7943 cp_lexer_consume_token (parser->lexer);
7944 /* Parse the default-argument. */
7945 default_argument = cp_parser_type_id (parser);
7948 default_argument = NULL_TREE;
7950 /* Create the combined representation of the parameter and the
7951 default argument. */
7952 parameter = build_tree_list (default_argument, parameter);
7958 tree parameter_list;
7960 tree default_argument;
7962 /* Look for the `<'. */
7963 cp_parser_require (parser, CPP_LESS, "`<'");
7964 /* Parse the template-parameter-list. */
7965 begin_template_parm_list ();
7967 = cp_parser_template_parameter_list (parser);
7968 parameter_list = end_template_parm_list (parameter_list);
7969 /* Look for the `>'. */
7970 cp_parser_require (parser, CPP_GREATER, "`>'");
7971 /* Look for the `class' keyword. */
7972 cp_parser_require_keyword (parser, RID_CLASS, "`class'");
7973 /* If the next token is an `=', then there is a
7974 default-argument. If the next token is a `>', we are at
7975 the end of the parameter-list. If the next token is a `,',
7976 then we are at the end of this parameter. */
7977 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
7978 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
7979 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7980 identifier = cp_parser_identifier (parser);
7982 identifier = NULL_TREE;
7983 /* Create the template parameter. */
7984 parameter = finish_template_template_parm (class_type_node,
7987 /* If the next token is an `=', then there is a
7988 default-argument. */
7989 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7993 /* Consume the `='. */
7994 cp_lexer_consume_token (parser->lexer);
7995 /* Parse the id-expression. */
7997 = cp_parser_id_expression (parser,
7998 /*template_keyword_p=*/false,
7999 /*check_dependency_p=*/true,
8000 /*template_p=*/&is_template,
8001 /*declarator_p=*/false);
8002 if (TREE_CODE (default_argument) == TYPE_DECL)
8003 /* If the id-expression was a template-id that refers to
8004 a template-class, we already have the declaration here,
8005 so no further lookup is needed. */
8008 /* Look up the name. */
8010 = cp_parser_lookup_name (parser, default_argument,
8012 /*is_template=*/is_template,
8013 /*is_namespace=*/false,
8014 /*check_dependency=*/true);
8015 /* See if the default argument is valid. */
8017 = check_template_template_default_arg (default_argument);
8020 default_argument = NULL_TREE;
8022 /* Create the combined representation of the parameter and the
8023 default argument. */
8024 parameter = build_tree_list (default_argument, parameter);
8029 /* Anything else is an error. */
8030 cp_parser_error (parser,
8031 "expected `class', `typename', or `template'");
8032 parameter = error_mark_node;
8038 /* Parse a template-id.
8041 template-name < template-argument-list [opt] >
8043 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
8044 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
8045 returned. Otherwise, if the template-name names a function, or set
8046 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
8047 names a class, returns a TYPE_DECL for the specialization.
8049 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
8050 uninstantiated templates. */
8053 cp_parser_template_id (cp_parser *parser,
8054 bool template_keyword_p,
8055 bool check_dependency_p,
8056 bool is_declaration)
8061 ptrdiff_t start_of_id;
8062 tree access_check = NULL_TREE;
8063 cp_token *next_token, *next_token_2;
8066 /* If the next token corresponds to a template-id, there is no need
8068 next_token = cp_lexer_peek_token (parser->lexer);
8069 if (next_token->type == CPP_TEMPLATE_ID)
8074 /* Get the stored value. */
8075 value = cp_lexer_consume_token (parser->lexer)->value;
8076 /* Perform any access checks that were deferred. */
8077 for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
8078 perform_or_defer_access_check (TREE_PURPOSE (check),
8079 TREE_VALUE (check));
8080 /* Return the stored value. */
8081 return TREE_VALUE (value);
8084 /* Avoid performing name lookup if there is no possibility of
8085 finding a template-id. */
8086 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
8087 || (next_token->type == CPP_NAME
8088 && !cp_parser_nth_token_starts_template_argument_list_p
8091 cp_parser_error (parser, "expected template-id");
8092 return error_mark_node;
8095 /* Remember where the template-id starts. */
8096 if (cp_parser_parsing_tentatively (parser)
8097 && !cp_parser_committed_to_tentative_parse (parser))
8099 next_token = cp_lexer_peek_token (parser->lexer);
8100 start_of_id = cp_lexer_token_difference (parser->lexer,
8101 parser->lexer->first_token,
8107 push_deferring_access_checks (dk_deferred);
8109 /* Parse the template-name. */
8110 is_identifier = false;
8111 template = cp_parser_template_name (parser, template_keyword_p,
8115 if (template == error_mark_node || is_identifier)
8117 pop_deferring_access_checks ();
8121 /* If we find the sequence `[:' after a template-name, it's probably
8122 a digraph-typo for `< ::'. Substitute the tokens and check if we can
8123 parse correctly the argument list. */
8124 next_token = cp_lexer_peek_nth_token (parser->lexer, 1);
8125 next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
8126 if (next_token->type == CPP_OPEN_SQUARE
8127 && next_token->flags & DIGRAPH
8128 && next_token_2->type == CPP_COLON
8129 && !(next_token_2->flags & PREV_WHITE))
8131 cp_parser_parse_tentatively (parser);
8132 /* Change `:' into `::'. */
8133 next_token_2->type = CPP_SCOPE;
8134 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
8136 cp_lexer_consume_token (parser->lexer);
8137 /* Parse the arguments. */
8138 arguments = cp_parser_enclosed_template_argument_list (parser);
8139 if (!cp_parser_parse_definitely (parser))
8141 /* If we couldn't parse an argument list, then we revert our changes
8142 and return simply an error. Maybe this is not a template-id
8144 next_token_2->type = CPP_COLON;
8145 cp_parser_error (parser, "expected `<'");
8146 pop_deferring_access_checks ();
8147 return error_mark_node;
8149 /* Otherwise, emit an error about the invalid digraph, but continue
8150 parsing because we got our argument list. */
8151 pedwarn ("`<::' cannot begin a template-argument list");
8152 inform ("`<:' is an alternate spelling for `['. Insert whitespace "
8153 "between `<' and `::'");
8154 if (!flag_permissive)
8159 inform ("(if you use `-fpermissive' G++ will accept your code)");
8166 /* Look for the `<' that starts the template-argument-list. */
8167 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
8169 pop_deferring_access_checks ();
8170 return error_mark_node;
8172 /* Parse the arguments. */
8173 arguments = cp_parser_enclosed_template_argument_list (parser);
8176 /* Build a representation of the specialization. */
8177 if (TREE_CODE (template) == IDENTIFIER_NODE)
8178 template_id = build_min_nt (TEMPLATE_ID_EXPR, template, arguments);
8179 else if (DECL_CLASS_TEMPLATE_P (template)
8180 || DECL_TEMPLATE_TEMPLATE_PARM_P (template))
8182 = finish_template_type (template, arguments,
8183 cp_lexer_next_token_is (parser->lexer,
8187 /* If it's not a class-template or a template-template, it should be
8188 a function-template. */
8189 my_friendly_assert ((DECL_FUNCTION_TEMPLATE_P (template)
8190 || TREE_CODE (template) == OVERLOAD
8191 || BASELINK_P (template)),
8194 template_id = lookup_template_function (template, arguments);
8197 /* Retrieve any deferred checks. Do not pop this access checks yet
8198 so the memory will not be reclaimed during token replacing below. */
8199 access_check = get_deferred_access_checks ();
8201 /* If parsing tentatively, replace the sequence of tokens that makes
8202 up the template-id with a CPP_TEMPLATE_ID token. That way,
8203 should we re-parse the token stream, we will not have to repeat
8204 the effort required to do the parse, nor will we issue duplicate
8205 error messages about problems during instantiation of the
8207 if (start_of_id >= 0)
8211 /* Find the token that corresponds to the start of the
8213 token = cp_lexer_advance_token (parser->lexer,
8214 parser->lexer->first_token,
8217 /* Reset the contents of the START_OF_ID token. */
8218 token->type = CPP_TEMPLATE_ID;
8219 token->value = build_tree_list (access_check, template_id);
8220 token->keyword = RID_MAX;
8221 /* Purge all subsequent tokens. */
8222 cp_lexer_purge_tokens_after (parser->lexer, token);
8225 pop_deferring_access_checks ();
8229 /* Parse a template-name.
8234 The standard should actually say:
8238 operator-function-id
8240 A defect report has been filed about this issue.
8242 A conversion-function-id cannot be a template name because they cannot
8243 be part of a template-id. In fact, looking at this code:
8247 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
8248 It is impossible to call a templated conversion-function-id with an
8249 explicit argument list, since the only allowed template parameter is
8250 the type to which it is converting.
8252 If TEMPLATE_KEYWORD_P is true, then we have just seen the
8253 `template' keyword, in a construction like:
8257 In that case `f' is taken to be a template-name, even though there
8258 is no way of knowing for sure.
8260 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
8261 name refers to a set of overloaded functions, at least one of which
8262 is a template, or an IDENTIFIER_NODE with the name of the template,
8263 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
8264 names are looked up inside uninstantiated templates. */
8267 cp_parser_template_name (cp_parser* parser,
8268 bool template_keyword_p,
8269 bool check_dependency_p,
8270 bool is_declaration,
8271 bool *is_identifier)
8277 /* If the next token is `operator', then we have either an
8278 operator-function-id or a conversion-function-id. */
8279 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
8281 /* We don't know whether we're looking at an
8282 operator-function-id or a conversion-function-id. */
8283 cp_parser_parse_tentatively (parser);
8284 /* Try an operator-function-id. */
8285 identifier = cp_parser_operator_function_id (parser);
8286 /* If that didn't work, try a conversion-function-id. */
8287 if (!cp_parser_parse_definitely (parser))
8289 cp_parser_error (parser, "expected template-name");
8290 return error_mark_node;
8293 /* Look for the identifier. */
8295 identifier = cp_parser_identifier (parser);
8297 /* If we didn't find an identifier, we don't have a template-id. */
8298 if (identifier == error_mark_node)
8299 return error_mark_node;
8301 /* If the name immediately followed the `template' keyword, then it
8302 is a template-name. However, if the next token is not `<', then
8303 we do not treat it as a template-name, since it is not being used
8304 as part of a template-id. This enables us to handle constructs
8307 template <typename T> struct S { S(); };
8308 template <typename T> S<T>::S();
8310 correctly. We would treat `S' as a template -- if it were `S<T>'
8311 -- but we do not if there is no `<'. */
8313 if (processing_template_decl
8314 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
8316 /* In a declaration, in a dependent context, we pretend that the
8317 "template" keyword was present in order to improve error
8318 recovery. For example, given:
8320 template <typename T> void f(T::X<int>);
8322 we want to treat "X<int>" as a template-id. */
8324 && !template_keyword_p
8325 && parser->scope && TYPE_P (parser->scope)
8326 && dependent_type_p (parser->scope)
8327 /* Do not do this for dtors (or ctors), since they never
8328 need the template keyword before their name. */
8329 && !constructor_name_p (identifier, parser->scope))
8333 /* Explain what went wrong. */
8334 error ("non-template `%D' used as template", identifier);
8335 inform ("use `%T::template %D' to indicate that it is a template",
8336 parser->scope, identifier);
8337 /* If parsing tentatively, find the location of the "<"
8339 if (cp_parser_parsing_tentatively (parser)
8340 && !cp_parser_committed_to_tentative_parse (parser))
8342 cp_parser_simulate_error (parser);
8343 token = cp_lexer_peek_token (parser->lexer);
8344 token = cp_lexer_prev_token (parser->lexer, token);
8345 start = cp_lexer_token_difference (parser->lexer,
8346 parser->lexer->first_token,
8351 /* Parse the template arguments so that we can issue error
8352 messages about them. */
8353 cp_lexer_consume_token (parser->lexer);
8354 cp_parser_enclosed_template_argument_list (parser);
8355 /* Skip tokens until we find a good place from which to
8356 continue parsing. */
8357 cp_parser_skip_to_closing_parenthesis (parser,
8358 /*recovering=*/true,
8360 /*consume_paren=*/false);
8361 /* If parsing tentatively, permanently remove the
8362 template argument list. That will prevent duplicate
8363 error messages from being issued about the missing
8364 "template" keyword. */
8367 token = cp_lexer_advance_token (parser->lexer,
8368 parser->lexer->first_token,
8370 cp_lexer_purge_tokens_after (parser->lexer, token);
8373 *is_identifier = true;
8377 /* If the "template" keyword is present, then there is generally
8378 no point in doing name-lookup, so we just return IDENTIFIER.
8379 But, if the qualifying scope is non-dependent then we can
8380 (and must) do name-lookup normally. */
8381 if (template_keyword_p
8383 || (TYPE_P (parser->scope)
8384 && dependent_type_p (parser->scope))))
8388 /* Look up the name. */
8389 decl = cp_parser_lookup_name (parser, identifier,
8391 /*is_template=*/false,
8392 /*is_namespace=*/false,
8393 check_dependency_p);
8394 decl = maybe_get_template_decl_from_type_decl (decl);
8396 /* If DECL is a template, then the name was a template-name. */
8397 if (TREE_CODE (decl) == TEMPLATE_DECL)
8401 /* The standard does not explicitly indicate whether a name that
8402 names a set of overloaded declarations, some of which are
8403 templates, is a template-name. However, such a name should
8404 be a template-name; otherwise, there is no way to form a
8405 template-id for the overloaded templates. */
8406 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
8407 if (TREE_CODE (fns) == OVERLOAD)
8411 for (fn = fns; fn; fn = OVL_NEXT (fn))
8412 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
8417 /* Otherwise, the name does not name a template. */
8418 cp_parser_error (parser, "expected template-name");
8419 return error_mark_node;
8423 /* If DECL is dependent, and refers to a function, then just return
8424 its name; we will look it up again during template instantiation. */
8425 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
8427 tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
8428 if (TYPE_P (scope) && dependent_type_p (scope))
8435 /* Parse a template-argument-list.
8437 template-argument-list:
8439 template-argument-list , template-argument
8441 Returns a TREE_VEC containing the arguments. */
8444 cp_parser_template_argument_list (cp_parser* parser)
8446 tree fixed_args[10];
8447 unsigned n_args = 0;
8448 unsigned alloced = 10;
8449 tree *arg_ary = fixed_args;
8451 bool saved_in_template_argument_list_p;
8453 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
8454 parser->in_template_argument_list_p = true;
8460 /* Consume the comma. */
8461 cp_lexer_consume_token (parser->lexer);
8463 /* Parse the template-argument. */
8464 argument = cp_parser_template_argument (parser);
8465 if (n_args == alloced)
8469 if (arg_ary == fixed_args)
8471 arg_ary = xmalloc (sizeof (tree) * alloced);
8472 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
8475 arg_ary = xrealloc (arg_ary, sizeof (tree) * alloced);
8477 arg_ary[n_args++] = argument;
8479 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
8481 vec = make_tree_vec (n_args);
8484 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
8486 if (arg_ary != fixed_args)
8488 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
8492 /* Parse a template-argument.
8495 assignment-expression
8499 The representation is that of an assignment-expression, type-id, or
8500 id-expression -- except that the qualified id-expression is
8501 evaluated, so that the value returned is either a DECL or an
8504 Although the standard says "assignment-expression", it forbids
8505 throw-expressions or assignments in the template argument.
8506 Therefore, we use "conditional-expression" instead. */
8509 cp_parser_template_argument (cp_parser* parser)
8514 bool maybe_type_id = false;
8517 tree qualifying_class;
8519 /* There's really no way to know what we're looking at, so we just
8520 try each alternative in order.
8524 In a template-argument, an ambiguity between a type-id and an
8525 expression is resolved to a type-id, regardless of the form of
8526 the corresponding template-parameter.
8528 Therefore, we try a type-id first. */
8529 cp_parser_parse_tentatively (parser);
8530 argument = cp_parser_type_id (parser);
8531 /* If there was no error parsing the type-id but the next token is a '>>',
8532 we probably found a typo for '> >'. But there are type-id which are
8533 also valid expressions. For instance:
8535 struct X { int operator >> (int); };
8536 template <int V> struct Foo {};
8539 Here 'X()' is a valid type-id of a function type, but the user just
8540 wanted to write the expression "X() >> 5". Thus, we remember that we
8541 found a valid type-id, but we still try to parse the argument as an
8542 expression to see what happens. */
8543 if (!cp_parser_error_occurred (parser)
8544 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
8546 maybe_type_id = true;
8547 cp_parser_abort_tentative_parse (parser);
8551 /* If the next token isn't a `,' or a `>', then this argument wasn't
8552 really finished. This means that the argument is not a valid
8554 if (!cp_parser_next_token_ends_template_argument_p (parser))
8555 cp_parser_error (parser, "expected template-argument");
8556 /* If that worked, we're done. */
8557 if (cp_parser_parse_definitely (parser))
8560 /* We're still not sure what the argument will be. */
8561 cp_parser_parse_tentatively (parser);
8562 /* Try a template. */
8563 argument = cp_parser_id_expression (parser,
8564 /*template_keyword_p=*/false,
8565 /*check_dependency_p=*/true,
8567 /*declarator_p=*/false);
8568 /* If the next token isn't a `,' or a `>', then this argument wasn't
8570 if (!cp_parser_next_token_ends_template_argument_p (parser))
8571 cp_parser_error (parser, "expected template-argument");
8572 if (!cp_parser_error_occurred (parser))
8574 /* Figure out what is being referred to. If the id-expression
8575 was for a class template specialization, then we will have a
8576 TYPE_DECL at this point. There is no need to do name lookup
8577 at this point in that case. */
8578 if (TREE_CODE (argument) != TYPE_DECL)
8579 argument = cp_parser_lookup_name (parser, argument,
8581 /*is_template=*/template_p,
8582 /*is_namespace=*/false,
8583 /*check_dependency=*/true);
8584 if (TREE_CODE (argument) != TEMPLATE_DECL
8585 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
8586 cp_parser_error (parser, "expected template-name");
8588 if (cp_parser_parse_definitely (parser))
8590 /* It must be a non-type argument. There permitted cases are given
8591 in [temp.arg.nontype]:
8593 -- an integral constant-expression of integral or enumeration
8596 -- the name of a non-type template-parameter; or
8598 -- the name of an object or function with external linkage...
8600 -- the address of an object or function with external linkage...
8602 -- a pointer to member... */
8603 /* Look for a non-type template parameter. */
8604 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
8606 cp_parser_parse_tentatively (parser);
8607 argument = cp_parser_primary_expression (parser,
8610 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
8611 || !cp_parser_next_token_ends_template_argument_p (parser))
8612 cp_parser_simulate_error (parser);
8613 if (cp_parser_parse_definitely (parser))
8616 /* If the next token is "&", the argument must be the address of an
8617 object or function with external linkage. */
8618 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
8620 cp_lexer_consume_token (parser->lexer);
8621 /* See if we might have an id-expression. */
8622 token = cp_lexer_peek_token (parser->lexer);
8623 if (token->type == CPP_NAME
8624 || token->keyword == RID_OPERATOR
8625 || token->type == CPP_SCOPE
8626 || token->type == CPP_TEMPLATE_ID
8627 || token->type == CPP_NESTED_NAME_SPECIFIER)
8629 cp_parser_parse_tentatively (parser);
8630 argument = cp_parser_primary_expression (parser,
8633 if (cp_parser_error_occurred (parser)
8634 || !cp_parser_next_token_ends_template_argument_p (parser))
8635 cp_parser_abort_tentative_parse (parser);
8638 if (qualifying_class)
8639 argument = finish_qualified_id_expr (qualifying_class,
8643 if (TREE_CODE (argument) == VAR_DECL)
8645 /* A variable without external linkage might still be a
8646 valid constant-expression, so no error is issued here
8647 if the external-linkage check fails. */
8648 if (!DECL_EXTERNAL_LINKAGE_P (argument))
8649 cp_parser_simulate_error (parser);
8651 else if (is_overloaded_fn (argument))
8652 /* All overloaded functions are allowed; if the external
8653 linkage test does not pass, an error will be issued
8657 && (TREE_CODE (argument) == OFFSET_REF
8658 || TREE_CODE (argument) == SCOPE_REF))
8659 /* A pointer-to-member. */
8662 cp_parser_simulate_error (parser);
8664 if (cp_parser_parse_definitely (parser))
8667 argument = build_x_unary_op (ADDR_EXPR, argument);
8672 /* If the argument started with "&", there are no other valid
8673 alternatives at this point. */
8676 cp_parser_error (parser, "invalid non-type template argument");
8677 return error_mark_node;
8679 /* If the argument wasn't successfully parsed as a type-id followed
8680 by '>>', the argument can only be a constant expression now.
8681 Otherwise, we try parsing the constant-expression tentatively,
8682 because the argument could really be a type-id. */
8684 cp_parser_parse_tentatively (parser);
8685 argument = cp_parser_constant_expression (parser,
8686 /*allow_non_constant_p=*/false,
8687 /*non_constant_p=*/NULL);
8688 argument = fold_non_dependent_expr (argument);
8691 if (!cp_parser_next_token_ends_template_argument_p (parser))
8692 cp_parser_error (parser, "expected template-argument");
8693 if (cp_parser_parse_definitely (parser))
8695 /* We did our best to parse the argument as a non type-id, but that
8696 was the only alternative that matched (albeit with a '>' after
8697 it). We can assume it's just a typo from the user, and a
8698 diagnostic will then be issued. */
8699 return cp_parser_type_id (parser);
8702 /* Parse an explicit-instantiation.
8704 explicit-instantiation:
8705 template declaration
8707 Although the standard says `declaration', what it really means is:
8709 explicit-instantiation:
8710 template decl-specifier-seq [opt] declarator [opt] ;
8712 Things like `template int S<int>::i = 5, int S<double>::j;' are not
8713 supposed to be allowed. A defect report has been filed about this
8718 explicit-instantiation:
8719 storage-class-specifier template
8720 decl-specifier-seq [opt] declarator [opt] ;
8721 function-specifier template
8722 decl-specifier-seq [opt] declarator [opt] ; */
8725 cp_parser_explicit_instantiation (cp_parser* parser)
8727 int declares_class_or_enum;
8728 tree decl_specifiers;
8730 tree extension_specifier = NULL_TREE;
8732 /* Look for an (optional) storage-class-specifier or
8733 function-specifier. */
8734 if (cp_parser_allow_gnu_extensions_p (parser))
8737 = cp_parser_storage_class_specifier_opt (parser);
8738 if (!extension_specifier)
8739 extension_specifier = cp_parser_function_specifier_opt (parser);
8742 /* Look for the `template' keyword. */
8743 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
8744 /* Let the front end know that we are processing an explicit
8746 begin_explicit_instantiation ();
8747 /* [temp.explicit] says that we are supposed to ignore access
8748 control while processing explicit instantiation directives. */
8749 push_deferring_access_checks (dk_no_check);
8750 /* Parse a decl-specifier-seq. */
8752 = cp_parser_decl_specifier_seq (parser,
8753 CP_PARSER_FLAGS_OPTIONAL,
8755 &declares_class_or_enum);
8756 /* If there was exactly one decl-specifier, and it declared a class,
8757 and there's no declarator, then we have an explicit type
8759 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
8763 type = check_tag_decl (decl_specifiers);
8764 /* Turn access control back on for names used during
8765 template instantiation. */
8766 pop_deferring_access_checks ();
8768 do_type_instantiation (type, extension_specifier, /*complain=*/1);
8775 /* Parse the declarator. */
8777 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
8778 /*ctor_dtor_or_conv_p=*/NULL,
8779 /*parenthesized_p=*/NULL);
8780 cp_parser_check_for_definition_in_return_type (declarator,
8781 declares_class_or_enum);
8782 if (declarator != error_mark_node)
8784 decl = grokdeclarator (declarator, decl_specifiers,
8786 /* Turn access control back on for names used during
8787 template instantiation. */
8788 pop_deferring_access_checks ();
8789 /* Do the explicit instantiation. */
8790 do_decl_instantiation (decl, extension_specifier);
8794 pop_deferring_access_checks ();
8795 /* Skip the body of the explicit instantiation. */
8796 cp_parser_skip_to_end_of_statement (parser);
8799 /* We're done with the instantiation. */
8800 end_explicit_instantiation ();
8802 cp_parser_consume_semicolon_at_end_of_statement (parser);
8805 /* Parse an explicit-specialization.
8807 explicit-specialization:
8808 template < > declaration
8810 Although the standard says `declaration', what it really means is:
8812 explicit-specialization:
8813 template <> decl-specifier [opt] init-declarator [opt] ;
8814 template <> function-definition
8815 template <> explicit-specialization
8816 template <> template-declaration */
8819 cp_parser_explicit_specialization (cp_parser* parser)
8821 /* Look for the `template' keyword. */
8822 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
8823 /* Look for the `<'. */
8824 cp_parser_require (parser, CPP_LESS, "`<'");
8825 /* Look for the `>'. */
8826 cp_parser_require (parser, CPP_GREATER, "`>'");
8827 /* We have processed another parameter list. */
8828 ++parser->num_template_parameter_lists;
8829 /* Let the front end know that we are beginning a specialization. */
8830 begin_specialization ();
8832 /* If the next keyword is `template', we need to figure out whether
8833 or not we're looking a template-declaration. */
8834 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
8836 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
8837 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
8838 cp_parser_template_declaration_after_export (parser,
8839 /*member_p=*/false);
8841 cp_parser_explicit_specialization (parser);
8844 /* Parse the dependent declaration. */
8845 cp_parser_single_declaration (parser,
8849 /* We're done with the specialization. */
8850 end_specialization ();
8851 /* We're done with this parameter list. */
8852 --parser->num_template_parameter_lists;
8855 /* Parse a type-specifier.
8858 simple-type-specifier
8861 elaborated-type-specifier
8869 Returns a representation of the type-specifier. If the
8870 type-specifier is a keyword (like `int' or `const', or
8871 `__complex__') then the corresponding IDENTIFIER_NODE is returned.
8872 For a class-specifier, enum-specifier, or elaborated-type-specifier
8873 a TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
8875 If IS_FRIEND is TRUE then this type-specifier is being declared a
8876 `friend'. If IS_DECLARATION is TRUE, then this type-specifier is
8877 appearing in a decl-specifier-seq.
8879 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
8880 class-specifier, enum-specifier, or elaborated-type-specifier, then
8881 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
8882 if a type is declared; 2 if it is defined. Otherwise, it is set to
8885 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
8886 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
8890 cp_parser_type_specifier (cp_parser* parser,
8891 cp_parser_flags flags,
8893 bool is_declaration,
8894 int* declares_class_or_enum,
8895 bool* is_cv_qualifier)
8897 tree type_spec = NULL_TREE;
8901 /* Assume this type-specifier does not declare a new type. */
8902 if (declares_class_or_enum)
8903 *declares_class_or_enum = 0;
8904 /* And that it does not specify a cv-qualifier. */
8905 if (is_cv_qualifier)
8906 *is_cv_qualifier = false;
8907 /* Peek at the next token. */
8908 token = cp_lexer_peek_token (parser->lexer);
8910 /* If we're looking at a keyword, we can use that to guide the
8911 production we choose. */
8912 keyword = token->keyword;
8915 /* Any of these indicate either a class-specifier, or an
8916 elaborated-type-specifier. */
8921 /* Parse tentatively so that we can back up if we don't find a
8922 class-specifier or enum-specifier. */
8923 cp_parser_parse_tentatively (parser);
8924 /* Look for the class-specifier or enum-specifier. */
8925 if (keyword == RID_ENUM)
8926 type_spec = cp_parser_enum_specifier (parser);
8928 type_spec = cp_parser_class_specifier (parser);
8930 /* If that worked, we're done. */
8931 if (cp_parser_parse_definitely (parser))
8933 if (declares_class_or_enum)
8934 *declares_class_or_enum = 2;
8941 /* Look for an elaborated-type-specifier. */
8942 type_spec = cp_parser_elaborated_type_specifier (parser,
8945 /* We're declaring a class or enum -- unless we're using
8947 if (declares_class_or_enum && keyword != RID_TYPENAME)
8948 *declares_class_or_enum = 1;
8954 type_spec = cp_parser_cv_qualifier_opt (parser);
8955 /* Even though we call a routine that looks for an optional
8956 qualifier, we know that there should be one. */
8957 my_friendly_assert (type_spec != NULL, 20000328);
8958 /* This type-specifier was a cv-qualified. */
8959 if (is_cv_qualifier)
8960 *is_cv_qualifier = true;
8965 /* The `__complex__' keyword is a GNU extension. */
8966 return cp_lexer_consume_token (parser->lexer)->value;
8972 /* If we do not already have a type-specifier, assume we are looking
8973 at a simple-type-specifier. */
8974 type_spec = cp_parser_simple_type_specifier (parser, flags,
8975 /*identifier_p=*/true);
8977 /* If we didn't find a type-specifier, and a type-specifier was not
8978 optional in this context, issue an error message. */
8979 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
8981 cp_parser_error (parser, "expected type specifier");
8982 return error_mark_node;
8988 /* Parse a simple-type-specifier.
8990 simple-type-specifier:
8991 :: [opt] nested-name-specifier [opt] type-name
8992 :: [opt] nested-name-specifier template template-id
9007 simple-type-specifier:
9008 __typeof__ unary-expression
9009 __typeof__ ( type-id )
9011 For the various keywords, the value returned is simply the
9012 TREE_IDENTIFIER representing the keyword if IDENTIFIER_P is true.
9013 For the first two productions, and if IDENTIFIER_P is false, the
9014 value returned is the indicated TYPE_DECL. */
9017 cp_parser_simple_type_specifier (cp_parser* parser, cp_parser_flags flags,
9020 tree type = NULL_TREE;
9023 /* Peek at the next token. */
9024 token = cp_lexer_peek_token (parser->lexer);
9026 /* If we're looking at a keyword, things are easy. */
9027 switch (token->keyword)
9030 type = char_type_node;
9033 type = wchar_type_node;
9036 type = boolean_type_node;
9039 type = short_integer_type_node;
9042 type = integer_type_node;
9045 type = long_integer_type_node;
9048 type = integer_type_node;
9051 type = unsigned_type_node;
9054 type = float_type_node;
9057 type = double_type_node;
9060 type = void_type_node;
9067 /* Consume the `typeof' token. */
9068 cp_lexer_consume_token (parser->lexer);
9069 /* Parse the operand to `typeof'. */
9070 operand = cp_parser_sizeof_operand (parser, RID_TYPEOF);
9071 /* If it is not already a TYPE, take its type. */
9072 if (!TYPE_P (operand))
9073 operand = finish_typeof (operand);
9082 /* If the type-specifier was for a built-in type, we're done. */
9087 /* Consume the token. */
9088 id = cp_lexer_consume_token (parser->lexer)->value;
9090 /* There is no valid C++ program where a non-template type is
9091 followed by a "<". That usually indicates that the user thought
9092 that the type was a template. */
9093 cp_parser_check_for_invalid_template_id (parser, type);
9095 return identifier_p ? id : TYPE_NAME (type);
9098 /* The type-specifier must be a user-defined type. */
9099 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
9103 /* Don't gobble tokens or issue error messages if this is an
9104 optional type-specifier. */
9105 if (flags & CP_PARSER_FLAGS_OPTIONAL)
9106 cp_parser_parse_tentatively (parser);
9108 /* Look for the optional `::' operator. */
9109 cp_parser_global_scope_opt (parser,
9110 /*current_scope_valid_p=*/false);
9111 /* Look for the nested-name specifier. */
9113 = (cp_parser_nested_name_specifier_opt (parser,
9114 /*typename_keyword_p=*/false,
9115 /*check_dependency_p=*/true,
9117 /*is_declaration=*/false)
9119 /* If we have seen a nested-name-specifier, and the next token
9120 is `template', then we are using the template-id production. */
9122 && cp_parser_optional_template_keyword (parser))
9124 /* Look for the template-id. */
9125 type = cp_parser_template_id (parser,
9126 /*template_keyword_p=*/true,
9127 /*check_dependency_p=*/true,
9128 /*is_declaration=*/false);
9129 /* If the template-id did not name a type, we are out of
9131 if (TREE_CODE (type) != TYPE_DECL)
9133 cp_parser_error (parser, "expected template-id for type");
9137 /* Otherwise, look for a type-name. */
9139 type = cp_parser_type_name (parser);
9140 /* Keep track of all name-lookups performed in class scopes. */
9143 && TREE_CODE (type) == TYPE_DECL
9144 && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
9145 maybe_note_name_used_in_class (DECL_NAME (type), type);
9146 /* If it didn't work out, we don't have a TYPE. */
9147 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
9148 && !cp_parser_parse_definitely (parser))
9152 /* If we didn't get a type-name, issue an error message. */
9153 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
9155 cp_parser_error (parser, "expected type-name");
9156 return error_mark_node;
9159 /* There is no valid C++ program where a non-template type is
9160 followed by a "<". That usually indicates that the user thought
9161 that the type was a template. */
9162 if (type && type != error_mark_node)
9163 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type));
9168 /* Parse a type-name.
9181 Returns a TYPE_DECL for the the type. */
9184 cp_parser_type_name (cp_parser* parser)
9189 /* We can't know yet whether it is a class-name or not. */
9190 cp_parser_parse_tentatively (parser);
9191 /* Try a class-name. */
9192 type_decl = cp_parser_class_name (parser,
9193 /*typename_keyword_p=*/false,
9194 /*template_keyword_p=*/false,
9196 /*check_dependency_p=*/true,
9197 /*class_head_p=*/false,
9198 /*is_declaration=*/false);
9199 /* If it's not a class-name, keep looking. */
9200 if (!cp_parser_parse_definitely (parser))
9202 /* It must be a typedef-name or an enum-name. */
9203 identifier = cp_parser_identifier (parser);
9204 if (identifier == error_mark_node)
9205 return error_mark_node;
9207 /* Look up the type-name. */
9208 type_decl = cp_parser_lookup_name_simple (parser, identifier);
9209 /* Issue an error if we did not find a type-name. */
9210 if (TREE_CODE (type_decl) != TYPE_DECL)
9212 if (!cp_parser_simulate_error (parser))
9213 cp_parser_name_lookup_error (parser, identifier, type_decl,
9215 type_decl = error_mark_node;
9217 /* Remember that the name was used in the definition of the
9218 current class so that we can check later to see if the
9219 meaning would have been different after the class was
9220 entirely defined. */
9221 else if (type_decl != error_mark_node
9223 maybe_note_name_used_in_class (identifier, type_decl);
9230 /* Parse an elaborated-type-specifier. Note that the grammar given
9231 here incorporates the resolution to DR68.
9233 elaborated-type-specifier:
9234 class-key :: [opt] nested-name-specifier [opt] identifier
9235 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
9236 enum :: [opt] nested-name-specifier [opt] identifier
9237 typename :: [opt] nested-name-specifier identifier
9238 typename :: [opt] nested-name-specifier template [opt]
9243 elaborated-type-specifier:
9244 class-key attributes :: [opt] nested-name-specifier [opt] identifier
9245 class-key attributes :: [opt] nested-name-specifier [opt]
9246 template [opt] template-id
9247 enum attributes :: [opt] nested-name-specifier [opt] identifier
9249 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
9250 declared `friend'. If IS_DECLARATION is TRUE, then this
9251 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
9252 something is being declared.
9254 Returns the TYPE specified. */
9257 cp_parser_elaborated_type_specifier (cp_parser* parser,
9259 bool is_declaration)
9261 enum tag_types tag_type;
9263 tree type = NULL_TREE;
9264 tree attributes = NULL_TREE;
9266 /* See if we're looking at the `enum' keyword. */
9267 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
9269 /* Consume the `enum' token. */
9270 cp_lexer_consume_token (parser->lexer);
9271 /* Remember that it's an enumeration type. */
9272 tag_type = enum_type;
9273 /* Parse the attributes. */
9274 attributes = cp_parser_attributes_opt (parser);
9276 /* Or, it might be `typename'. */
9277 else if (cp_lexer_next_token_is_keyword (parser->lexer,
9280 /* Consume the `typename' token. */
9281 cp_lexer_consume_token (parser->lexer);
9282 /* Remember that it's a `typename' type. */
9283 tag_type = typename_type;
9284 /* The `typename' keyword is only allowed in templates. */
9285 if (!processing_template_decl)
9286 pedwarn ("using `typename' outside of template");
9288 /* Otherwise it must be a class-key. */
9291 tag_type = cp_parser_class_key (parser);
9292 if (tag_type == none_type)
9293 return error_mark_node;
9294 /* Parse the attributes. */
9295 attributes = cp_parser_attributes_opt (parser);
9298 /* Look for the `::' operator. */
9299 cp_parser_global_scope_opt (parser,
9300 /*current_scope_valid_p=*/false);
9301 /* Look for the nested-name-specifier. */
9302 if (tag_type == typename_type)
9304 if (cp_parser_nested_name_specifier (parser,
9305 /*typename_keyword_p=*/true,
9306 /*check_dependency_p=*/true,
9310 return error_mark_node;
9313 /* Even though `typename' is not present, the proposed resolution
9314 to Core Issue 180 says that in `class A<T>::B', `B' should be
9315 considered a type-name, even if `A<T>' is dependent. */
9316 cp_parser_nested_name_specifier_opt (parser,
9317 /*typename_keyword_p=*/true,
9318 /*check_dependency_p=*/true,
9321 /* For everything but enumeration types, consider a template-id. */
9322 if (tag_type != enum_type)
9324 bool template_p = false;
9327 /* Allow the `template' keyword. */
9328 template_p = cp_parser_optional_template_keyword (parser);
9329 /* If we didn't see `template', we don't know if there's a
9330 template-id or not. */
9332 cp_parser_parse_tentatively (parser);
9333 /* Parse the template-id. */
9334 decl = cp_parser_template_id (parser, template_p,
9335 /*check_dependency_p=*/true,
9337 /* If we didn't find a template-id, look for an ordinary
9339 if (!template_p && !cp_parser_parse_definitely (parser))
9341 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
9342 in effect, then we must assume that, upon instantiation, the
9343 template will correspond to a class. */
9344 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
9345 && tag_type == typename_type)
9346 type = make_typename_type (parser->scope, decl,
9349 type = TREE_TYPE (decl);
9352 /* For an enumeration type, consider only a plain identifier. */
9355 identifier = cp_parser_identifier (parser);
9357 if (identifier == error_mark_node)
9359 parser->scope = NULL_TREE;
9360 return error_mark_node;
9363 /* For a `typename', we needn't call xref_tag. */
9364 if (tag_type == typename_type)
9365 return cp_parser_make_typename_type (parser, parser->scope,
9367 /* Look up a qualified name in the usual way. */
9372 /* In an elaborated-type-specifier, names are assumed to name
9373 types, so we set IS_TYPE to TRUE when calling
9374 cp_parser_lookup_name. */
9375 decl = cp_parser_lookup_name (parser, identifier,
9377 /*is_template=*/false,
9378 /*is_namespace=*/false,
9379 /*check_dependency=*/true);
9381 /* If we are parsing friend declaration, DECL may be a
9382 TEMPLATE_DECL tree node here. However, we need to check
9383 whether this TEMPLATE_DECL results in valid code. Consider
9384 the following example:
9387 template <class T> class C {};
9390 template <class T> friend class N::C; // #1, valid code
9392 template <class T> class Y {
9393 friend class N::C; // #2, invalid code
9396 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
9397 name lookup of `N::C'. We see that friend declaration must
9398 be template for the code to be valid. Note that
9399 processing_template_decl does not work here since it is
9400 always 1 for the above two cases. */
9402 decl = (cp_parser_maybe_treat_template_as_class
9403 (decl, /*tag_name_p=*/is_friend
9404 && parser->num_template_parameter_lists));
9406 if (TREE_CODE (decl) != TYPE_DECL)
9408 error ("expected type-name");
9409 return error_mark_node;
9412 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
9413 check_elaborated_type_specifier
9415 (parser->num_template_parameter_lists
9416 || DECL_SELF_REFERENCE_P (decl)));
9418 type = TREE_TYPE (decl);
9422 /* An elaborated-type-specifier sometimes introduces a new type and
9423 sometimes names an existing type. Normally, the rule is that it
9424 introduces a new type only if there is not an existing type of
9425 the same name already in scope. For example, given:
9428 void f() { struct S s; }
9430 the `struct S' in the body of `f' is the same `struct S' as in
9431 the global scope; the existing definition is used. However, if
9432 there were no global declaration, this would introduce a new
9433 local class named `S'.
9435 An exception to this rule applies to the following code:
9437 namespace N { struct S; }
9439 Here, the elaborated-type-specifier names a new type
9440 unconditionally; even if there is already an `S' in the
9441 containing scope this declaration names a new type.
9442 This exception only applies if the elaborated-type-specifier
9443 forms the complete declaration:
9447 A declaration consisting solely of `class-key identifier ;' is
9448 either a redeclaration of the name in the current scope or a
9449 forward declaration of the identifier as a class name. It
9450 introduces the name into the current scope.
9452 We are in this situation precisely when the next token is a `;'.
9454 An exception to the exception is that a `friend' declaration does
9455 *not* name a new type; i.e., given:
9457 struct S { friend struct T; };
9459 `T' is not a new type in the scope of `S'.
9461 Also, `new struct S' or `sizeof (struct S)' never results in the
9462 definition of a new type; a new type can only be declared in a
9463 declaration context. */
9465 /* Warn about attributes. They are ignored. */
9467 warning ("type attributes are honored only at type definition");
9469 type = xref_tag (tag_type, identifier,
9472 || cp_lexer_next_token_is_not (parser->lexer,
9474 parser->num_template_parameter_lists);
9477 if (tag_type != enum_type)
9478 cp_parser_check_class_key (tag_type, type);
9480 /* A "<" cannot follow an elaborated type specifier. If that
9481 happens, the user was probably trying to form a template-id. */
9482 cp_parser_check_for_invalid_template_id (parser, type);
9487 /* Parse an enum-specifier.
9490 enum identifier [opt] { enumerator-list [opt] }
9492 Returns an ENUM_TYPE representing the enumeration. */
9495 cp_parser_enum_specifier (cp_parser* parser)
9498 tree identifier = NULL_TREE;
9501 /* Look for the `enum' keyword. */
9502 if (!cp_parser_require_keyword (parser, RID_ENUM, "`enum'"))
9503 return error_mark_node;
9504 /* Peek at the next token. */
9505 token = cp_lexer_peek_token (parser->lexer);
9507 /* See if it is an identifier. */
9508 if (token->type == CPP_NAME)
9509 identifier = cp_parser_identifier (parser);
9511 /* Look for the `{'. */
9512 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
9513 return error_mark_node;
9515 /* At this point, we're going ahead with the enum-specifier, even
9516 if some other problem occurs. */
9517 cp_parser_commit_to_tentative_parse (parser);
9519 /* Issue an error message if type-definitions are forbidden here. */
9520 cp_parser_check_type_definition (parser);
9522 /* Create the new type. */
9523 type = start_enum (identifier ? identifier : make_anon_name ());
9525 /* Peek at the next token. */
9526 token = cp_lexer_peek_token (parser->lexer);
9527 /* If it's not a `}', then there are some enumerators. */
9528 if (token->type != CPP_CLOSE_BRACE)
9529 cp_parser_enumerator_list (parser, type);
9530 /* Look for the `}'. */
9531 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
9533 /* Finish up the enumeration. */
9539 /* Parse an enumerator-list. The enumerators all have the indicated
9543 enumerator-definition
9544 enumerator-list , enumerator-definition */
9547 cp_parser_enumerator_list (cp_parser* parser, tree type)
9553 /* Parse an enumerator-definition. */
9554 cp_parser_enumerator_definition (parser, type);
9555 /* Peek at the next token. */
9556 token = cp_lexer_peek_token (parser->lexer);
9557 /* If it's not a `,', then we've reached the end of the
9559 if (token->type != CPP_COMMA)
9561 /* Otherwise, consume the `,' and keep going. */
9562 cp_lexer_consume_token (parser->lexer);
9563 /* If the next token is a `}', there is a trailing comma. */
9564 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
9566 if (pedantic && !in_system_header)
9567 pedwarn ("comma at end of enumerator list");
9573 /* Parse an enumerator-definition. The enumerator has the indicated
9576 enumerator-definition:
9578 enumerator = constant-expression
9584 cp_parser_enumerator_definition (cp_parser* parser, tree type)
9590 /* Look for the identifier. */
9591 identifier = cp_parser_identifier (parser);
9592 if (identifier == error_mark_node)
9595 /* Peek at the next token. */
9596 token = cp_lexer_peek_token (parser->lexer);
9597 /* If it's an `=', then there's an explicit value. */
9598 if (token->type == CPP_EQ)
9600 /* Consume the `=' token. */
9601 cp_lexer_consume_token (parser->lexer);
9602 /* Parse the value. */
9603 value = cp_parser_constant_expression (parser,
9604 /*allow_non_constant_p=*/false,
9610 /* Create the enumerator. */
9611 build_enumerator (identifier, value, type);
9614 /* Parse a namespace-name.
9617 original-namespace-name
9620 Returns the NAMESPACE_DECL for the namespace. */
9623 cp_parser_namespace_name (cp_parser* parser)
9626 tree namespace_decl;
9628 /* Get the name of the namespace. */
9629 identifier = cp_parser_identifier (parser);
9630 if (identifier == error_mark_node)
9631 return error_mark_node;
9633 /* Look up the identifier in the currently active scope. Look only
9634 for namespaces, due to:
9638 When looking up a namespace-name in a using-directive or alias
9639 definition, only namespace names are considered.
9645 During the lookup of a name preceding the :: scope resolution
9646 operator, object, function, and enumerator names are ignored.
9648 (Note that cp_parser_class_or_namespace_name only calls this
9649 function if the token after the name is the scope resolution
9651 namespace_decl = cp_parser_lookup_name (parser, identifier,
9653 /*is_template=*/false,
9654 /*is_namespace=*/true,
9655 /*check_dependency=*/true);
9656 /* If it's not a namespace, issue an error. */
9657 if (namespace_decl == error_mark_node
9658 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
9660 cp_parser_error (parser, "expected namespace-name");
9661 namespace_decl = error_mark_node;
9664 return namespace_decl;
9667 /* Parse a namespace-definition.
9669 namespace-definition:
9670 named-namespace-definition
9671 unnamed-namespace-definition
9673 named-namespace-definition:
9674 original-namespace-definition
9675 extension-namespace-definition
9677 original-namespace-definition:
9678 namespace identifier { namespace-body }
9680 extension-namespace-definition:
9681 namespace original-namespace-name { namespace-body }
9683 unnamed-namespace-definition:
9684 namespace { namespace-body } */
9687 cp_parser_namespace_definition (cp_parser* parser)
9691 /* Look for the `namespace' keyword. */
9692 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
9694 /* Get the name of the namespace. We do not attempt to distinguish
9695 between an original-namespace-definition and an
9696 extension-namespace-definition at this point. The semantic
9697 analysis routines are responsible for that. */
9698 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
9699 identifier = cp_parser_identifier (parser);
9701 identifier = NULL_TREE;
9703 /* Look for the `{' to start the namespace. */
9704 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
9705 /* Start the namespace. */
9706 push_namespace (identifier);
9707 /* Parse the body of the namespace. */
9708 cp_parser_namespace_body (parser);
9709 /* Finish the namespace. */
9711 /* Look for the final `}'. */
9712 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
9715 /* Parse a namespace-body.
9718 declaration-seq [opt] */
9721 cp_parser_namespace_body (cp_parser* parser)
9723 cp_parser_declaration_seq_opt (parser);
9726 /* Parse a namespace-alias-definition.
9728 namespace-alias-definition:
9729 namespace identifier = qualified-namespace-specifier ; */
9732 cp_parser_namespace_alias_definition (cp_parser* parser)
9735 tree namespace_specifier;
9737 /* Look for the `namespace' keyword. */
9738 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
9739 /* Look for the identifier. */
9740 identifier = cp_parser_identifier (parser);
9741 if (identifier == error_mark_node)
9743 /* Look for the `=' token. */
9744 cp_parser_require (parser, CPP_EQ, "`='");
9745 /* Look for the qualified-namespace-specifier. */
9747 = cp_parser_qualified_namespace_specifier (parser);
9748 /* Look for the `;' token. */
9749 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
9751 /* Register the alias in the symbol table. */
9752 do_namespace_alias (identifier, namespace_specifier);
9755 /* Parse a qualified-namespace-specifier.
9757 qualified-namespace-specifier:
9758 :: [opt] nested-name-specifier [opt] namespace-name
9760 Returns a NAMESPACE_DECL corresponding to the specified
9764 cp_parser_qualified_namespace_specifier (cp_parser* parser)
9766 /* Look for the optional `::'. */
9767 cp_parser_global_scope_opt (parser,
9768 /*current_scope_valid_p=*/false);
9770 /* Look for the optional nested-name-specifier. */
9771 cp_parser_nested_name_specifier_opt (parser,
9772 /*typename_keyword_p=*/false,
9773 /*check_dependency_p=*/true,
9775 /*is_declaration=*/true);
9777 return cp_parser_namespace_name (parser);
9780 /* Parse a using-declaration.
9783 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
9784 using :: unqualified-id ; */
9787 cp_parser_using_declaration (cp_parser* parser)
9790 bool typename_p = false;
9791 bool global_scope_p;
9797 /* Look for the `using' keyword. */
9798 cp_parser_require_keyword (parser, RID_USING, "`using'");
9800 /* Peek at the next token. */
9801 token = cp_lexer_peek_token (parser->lexer);
9802 /* See if it's `typename'. */
9803 if (token->keyword == RID_TYPENAME)
9805 /* Remember that we've seen it. */
9807 /* Consume the `typename' token. */
9808 cp_lexer_consume_token (parser->lexer);
9811 /* Look for the optional global scope qualification. */
9813 = (cp_parser_global_scope_opt (parser,
9814 /*current_scope_valid_p=*/false)
9817 /* If we saw `typename', or didn't see `::', then there must be a
9818 nested-name-specifier present. */
9819 if (typename_p || !global_scope_p)
9820 qscope = cp_parser_nested_name_specifier (parser, typename_p,
9821 /*check_dependency_p=*/true,
9823 /*is_declaration=*/true);
9824 /* Otherwise, we could be in either of the two productions. In that
9825 case, treat the nested-name-specifier as optional. */
9827 qscope = cp_parser_nested_name_specifier_opt (parser,
9828 /*typename_keyword_p=*/false,
9829 /*check_dependency_p=*/true,
9831 /*is_declaration=*/true);
9833 qscope = global_namespace;
9835 /* Parse the unqualified-id. */
9836 identifier = cp_parser_unqualified_id (parser,
9837 /*template_keyword_p=*/false,
9838 /*check_dependency_p=*/true,
9839 /*declarator_p=*/true);
9841 /* The function we call to handle a using-declaration is different
9842 depending on what scope we are in. */
9843 if (identifier == error_mark_node)
9845 else if (TREE_CODE (identifier) != IDENTIFIER_NODE
9846 && TREE_CODE (identifier) != BIT_NOT_EXPR)
9847 /* [namespace.udecl]
9849 A using declaration shall not name a template-id. */
9850 error ("a template-id may not appear in a using-declaration");
9853 scope = current_scope ();
9854 if (scope && TYPE_P (scope))
9856 /* Create the USING_DECL. */
9857 decl = do_class_using_decl (build_nt (SCOPE_REF,
9860 /* Add it to the list of members in this class. */
9861 finish_member_declaration (decl);
9865 decl = cp_parser_lookup_name_simple (parser, identifier);
9866 if (decl == error_mark_node)
9867 cp_parser_name_lookup_error (parser, identifier, decl, NULL);
9869 do_local_using_decl (decl, qscope, identifier);
9871 do_toplevel_using_decl (decl, qscope, identifier);
9875 /* Look for the final `;'. */
9876 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
9879 /* Parse a using-directive.
9882 using namespace :: [opt] nested-name-specifier [opt]
9886 cp_parser_using_directive (cp_parser* parser)
9888 tree namespace_decl;
9891 /* Look for the `using' keyword. */
9892 cp_parser_require_keyword (parser, RID_USING, "`using'");
9893 /* And the `namespace' keyword. */
9894 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
9895 /* Look for the optional `::' operator. */
9896 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
9897 /* And the optional nested-name-specifier. */
9898 cp_parser_nested_name_specifier_opt (parser,
9899 /*typename_keyword_p=*/false,
9900 /*check_dependency_p=*/true,
9902 /*is_declaration=*/true);
9903 /* Get the namespace being used. */
9904 namespace_decl = cp_parser_namespace_name (parser);
9905 /* And any specified attributes. */
9906 attribs = cp_parser_attributes_opt (parser);
9907 /* Update the symbol table. */
9908 parse_using_directive (namespace_decl, attribs);
9909 /* Look for the final `;'. */
9910 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
9913 /* Parse an asm-definition.
9916 asm ( string-literal ) ;
9921 asm volatile [opt] ( string-literal ) ;
9922 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
9923 asm volatile [opt] ( string-literal : asm-operand-list [opt]
9924 : asm-operand-list [opt] ) ;
9925 asm volatile [opt] ( string-literal : asm-operand-list [opt]
9926 : asm-operand-list [opt]
9927 : asm-operand-list [opt] ) ; */
9930 cp_parser_asm_definition (cp_parser* parser)
9934 tree outputs = NULL_TREE;
9935 tree inputs = NULL_TREE;
9936 tree clobbers = NULL_TREE;
9938 bool volatile_p = false;
9939 bool extended_p = false;
9941 /* Look for the `asm' keyword. */
9942 cp_parser_require_keyword (parser, RID_ASM, "`asm'");
9943 /* See if the next token is `volatile'. */
9944 if (cp_parser_allow_gnu_extensions_p (parser)
9945 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
9947 /* Remember that we saw the `volatile' keyword. */
9949 /* Consume the token. */
9950 cp_lexer_consume_token (parser->lexer);
9952 /* Look for the opening `('. */
9953 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
9954 /* Look for the string. */
9955 c_lex_string_translate = 0;
9956 token = cp_parser_require (parser, CPP_STRING, "asm body");
9959 string = token->value;
9960 /* If we're allowing GNU extensions, check for the extended assembly
9961 syntax. Unfortunately, the `:' tokens need not be separated by
9962 a space in C, and so, for compatibility, we tolerate that here
9963 too. Doing that means that we have to treat the `::' operator as
9965 if (cp_parser_allow_gnu_extensions_p (parser)
9966 && at_function_scope_p ()
9967 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
9968 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
9970 bool inputs_p = false;
9971 bool clobbers_p = false;
9973 /* The extended syntax was used. */
9976 /* Look for outputs. */
9977 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
9979 /* Consume the `:'. */
9980 cp_lexer_consume_token (parser->lexer);
9981 /* Parse the output-operands. */
9982 if (cp_lexer_next_token_is_not (parser->lexer,
9984 && cp_lexer_next_token_is_not (parser->lexer,
9986 && cp_lexer_next_token_is_not (parser->lexer,
9988 outputs = cp_parser_asm_operand_list (parser);
9990 /* If the next token is `::', there are no outputs, and the
9991 next token is the beginning of the inputs. */
9992 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
9994 /* Consume the `::' token. */
9995 cp_lexer_consume_token (parser->lexer);
9996 /* The inputs are coming next. */
10000 /* Look for inputs. */
10002 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10005 /* Consume the `:'. */
10006 cp_lexer_consume_token (parser->lexer);
10007 /* Parse the output-operands. */
10008 if (cp_lexer_next_token_is_not (parser->lexer,
10010 && cp_lexer_next_token_is_not (parser->lexer,
10012 && cp_lexer_next_token_is_not (parser->lexer,
10014 inputs = cp_parser_asm_operand_list (parser);
10016 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
10017 /* The clobbers are coming next. */
10020 /* Look for clobbers. */
10022 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10025 /* Consume the `:'. */
10026 cp_lexer_consume_token (parser->lexer);
10027 /* Parse the clobbers. */
10028 if (cp_lexer_next_token_is_not (parser->lexer,
10030 clobbers = cp_parser_asm_clobber_list (parser);
10033 /* Look for the closing `)'. */
10034 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
10035 cp_parser_skip_to_closing_parenthesis (parser, true, false,
10036 /*consume_paren=*/true);
10037 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10039 /* Create the ASM_EXPR. */
10040 if (at_function_scope_p ())
10042 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
10044 /* If the extended syntax was not used, mark the ASM_EXPR. */
10046 ASM_INPUT_P (asm_stmt) = 1;
10049 assemble_asm (string);
10052 c_lex_string_translate = 1;
10055 /* Declarators [gram.dcl.decl] */
10057 /* Parse an init-declarator.
10060 declarator initializer [opt]
10065 declarator asm-specification [opt] attributes [opt] initializer [opt]
10067 function-definition:
10068 decl-specifier-seq [opt] declarator ctor-initializer [opt]
10070 decl-specifier-seq [opt] declarator function-try-block
10074 function-definition:
10075 __extension__ function-definition
10077 The DECL_SPECIFIERS and PREFIX_ATTRIBUTES apply to this declarator.
10078 Returns a representation of the entity declared. If MEMBER_P is TRUE,
10079 then this declarator appears in a class scope. The new DECL created
10080 by this declarator is returned.
10082 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
10083 for a function-definition here as well. If the declarator is a
10084 declarator for a function-definition, *FUNCTION_DEFINITION_P will
10085 be TRUE upon return. By that point, the function-definition will
10086 have been completely parsed.
10088 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
10092 cp_parser_init_declarator (cp_parser* parser,
10093 tree decl_specifiers,
10094 tree prefix_attributes,
10095 bool function_definition_allowed_p,
10097 int declares_class_or_enum,
10098 bool* function_definition_p)
10103 tree asm_specification;
10105 tree decl = NULL_TREE;
10107 bool is_initialized;
10108 bool is_parenthesized_init;
10109 bool is_non_constant_init;
10110 int ctor_dtor_or_conv_p;
10112 bool pop_p = false;
10114 /* Assume that this is not the declarator for a function
10116 if (function_definition_p)
10117 *function_definition_p = false;
10119 /* Defer access checks while parsing the declarator; we cannot know
10120 what names are accessible until we know what is being
10122 resume_deferring_access_checks ();
10124 /* Parse the declarator. */
10126 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
10127 &ctor_dtor_or_conv_p,
10128 /*parenthesized_p=*/NULL);
10129 /* Gather up the deferred checks. */
10130 stop_deferring_access_checks ();
10132 /* If the DECLARATOR was erroneous, there's no need to go
10134 if (declarator == error_mark_node)
10135 return error_mark_node;
10137 cp_parser_check_for_definition_in_return_type (declarator,
10138 declares_class_or_enum);
10140 /* Figure out what scope the entity declared by the DECLARATOR is
10141 located in. `grokdeclarator' sometimes changes the scope, so
10142 we compute it now. */
10143 scope = get_scope_of_declarator (declarator);
10145 /* If we're allowing GNU extensions, look for an asm-specification
10147 if (cp_parser_allow_gnu_extensions_p (parser))
10149 /* Look for an asm-specification. */
10150 asm_specification = cp_parser_asm_specification_opt (parser);
10151 /* And attributes. */
10152 attributes = cp_parser_attributes_opt (parser);
10156 asm_specification = NULL_TREE;
10157 attributes = NULL_TREE;
10160 /* Peek at the next token. */
10161 token = cp_lexer_peek_token (parser->lexer);
10162 /* Check to see if the token indicates the start of a
10163 function-definition. */
10164 if (cp_parser_token_starts_function_definition_p (token))
10166 if (!function_definition_allowed_p)
10168 /* If a function-definition should not appear here, issue an
10170 cp_parser_error (parser,
10171 "a function-definition is not allowed here");
10172 return error_mark_node;
10176 /* Neither attributes nor an asm-specification are allowed
10177 on a function-definition. */
10178 if (asm_specification)
10179 error ("an asm-specification is not allowed on a function-definition");
10181 error ("attributes are not allowed on a function-definition");
10182 /* This is a function-definition. */
10183 *function_definition_p = true;
10185 /* Parse the function definition. */
10187 decl = cp_parser_save_member_function_body (parser,
10190 prefix_attributes);
10193 = (cp_parser_function_definition_from_specifiers_and_declarator
10194 (parser, decl_specifiers, prefix_attributes, declarator));
10202 Only in function declarations for constructors, destructors, and
10203 type conversions can the decl-specifier-seq be omitted.
10205 We explicitly postpone this check past the point where we handle
10206 function-definitions because we tolerate function-definitions
10207 that are missing their return types in some modes. */
10208 if (!decl_specifiers && ctor_dtor_or_conv_p <= 0)
10210 cp_parser_error (parser,
10211 "expected constructor, destructor, or type conversion");
10212 return error_mark_node;
10215 /* An `=' or an `(' indicates an initializer. */
10216 is_initialized = (token->type == CPP_EQ
10217 || token->type == CPP_OPEN_PAREN);
10218 /* If the init-declarator isn't initialized and isn't followed by a
10219 `,' or `;', it's not a valid init-declarator. */
10220 if (!is_initialized
10221 && token->type != CPP_COMMA
10222 && token->type != CPP_SEMICOLON)
10224 cp_parser_error (parser, "expected init-declarator");
10225 return error_mark_node;
10228 /* Because start_decl has side-effects, we should only call it if we
10229 know we're going ahead. By this point, we know that we cannot
10230 possibly be looking at any other construct. */
10231 cp_parser_commit_to_tentative_parse (parser);
10233 /* If the decl specifiers were bad, issue an error now that we're
10234 sure this was intended to be a declarator. Then continue
10235 declaring the variable(s), as int, to try to cut down on further
10237 if (decl_specifiers != NULL
10238 && TREE_VALUE (decl_specifiers) == error_mark_node)
10240 cp_parser_error (parser, "invalid type in declaration");
10241 TREE_VALUE (decl_specifiers) = integer_type_node;
10244 /* Check to see whether or not this declaration is a friend. */
10245 friend_p = cp_parser_friend_p (decl_specifiers);
10247 /* Check that the number of template-parameter-lists is OK. */
10248 if (!cp_parser_check_declarator_template_parameters (parser, declarator))
10249 return error_mark_node;
10251 /* Enter the newly declared entry in the symbol table. If we're
10252 processing a declaration in a class-specifier, we wait until
10253 after processing the initializer. */
10256 if (parser->in_unbraced_linkage_specification_p)
10258 decl_specifiers = tree_cons (error_mark_node,
10259 get_identifier ("extern"),
10261 have_extern_spec = false;
10263 decl = start_decl (declarator, decl_specifiers,
10264 is_initialized, attributes, prefix_attributes);
10267 /* Enter the SCOPE. That way unqualified names appearing in the
10268 initializer will be looked up in SCOPE. */
10270 pop_p = push_scope (scope);
10272 /* Perform deferred access control checks, now that we know in which
10273 SCOPE the declared entity resides. */
10274 if (!member_p && decl)
10276 tree saved_current_function_decl = NULL_TREE;
10278 /* If the entity being declared is a function, pretend that we
10279 are in its scope. If it is a `friend', it may have access to
10280 things that would not otherwise be accessible. */
10281 if (TREE_CODE (decl) == FUNCTION_DECL)
10283 saved_current_function_decl = current_function_decl;
10284 current_function_decl = decl;
10287 /* Perform the access control checks for the declarator and the
10288 the decl-specifiers. */
10289 perform_deferred_access_checks ();
10291 /* Restore the saved value. */
10292 if (TREE_CODE (decl) == FUNCTION_DECL)
10293 current_function_decl = saved_current_function_decl;
10296 /* Parse the initializer. */
10297 if (is_initialized)
10298 initializer = cp_parser_initializer (parser,
10299 &is_parenthesized_init,
10300 &is_non_constant_init);
10303 initializer = NULL_TREE;
10304 is_parenthesized_init = false;
10305 is_non_constant_init = true;
10308 /* The old parser allows attributes to appear after a parenthesized
10309 initializer. Mark Mitchell proposed removing this functionality
10310 on the GCC mailing lists on 2002-08-13. This parser accepts the
10311 attributes -- but ignores them. */
10312 if (cp_parser_allow_gnu_extensions_p (parser) && is_parenthesized_init)
10313 if (cp_parser_attributes_opt (parser))
10314 warning ("attributes after parenthesized initializer ignored");
10316 /* Leave the SCOPE, now that we have processed the initializer. It
10317 is important to do this before calling cp_finish_decl because it
10318 makes decisions about whether to create DECL_STMTs or not based
10319 on the current scope. */
10323 /* For an in-class declaration, use `grokfield' to create the
10327 decl = grokfield (declarator, decl_specifiers,
10328 initializer, /*asmspec=*/NULL_TREE,
10329 /*attributes=*/NULL_TREE);
10330 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
10331 cp_parser_save_default_args (parser, decl);
10334 /* Finish processing the declaration. But, skip friend
10336 if (!friend_p && decl)
10337 cp_finish_decl (decl,
10340 /* If the initializer is in parentheses, then this is
10341 a direct-initialization, which means that an
10342 `explicit' constructor is OK. Otherwise, an
10343 `explicit' constructor cannot be used. */
10344 ((is_parenthesized_init || !is_initialized)
10345 ? 0 : LOOKUP_ONLYCONVERTING));
10347 /* Remember whether or not variables were initialized by
10348 constant-expressions. */
10349 if (decl && TREE_CODE (decl) == VAR_DECL
10350 && is_initialized && !is_non_constant_init)
10351 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = true;
10356 /* Parse a declarator.
10360 ptr-operator declarator
10362 abstract-declarator:
10363 ptr-operator abstract-declarator [opt]
10364 direct-abstract-declarator
10369 attributes [opt] direct-declarator
10370 attributes [opt] ptr-operator declarator
10372 abstract-declarator:
10373 attributes [opt] ptr-operator abstract-declarator [opt]
10374 attributes [opt] direct-abstract-declarator
10376 Returns a representation of the declarator. If the declarator has
10377 the form `* declarator', then an INDIRECT_REF is returned, whose
10378 only operand is the sub-declarator. Analogously, `& declarator' is
10379 represented as an ADDR_EXPR. For `X::* declarator', a SCOPE_REF is
10380 used. The first operand is the TYPE for `X'. The second operand
10381 is an INDIRECT_REF whose operand is the sub-declarator.
10383 Otherwise, the representation is as for a direct-declarator.
10385 (It would be better to define a structure type to represent
10386 declarators, rather than abusing `tree' nodes to represent
10387 declarators. That would be much clearer and save some memory.
10388 There is no reason for declarators to be garbage-collected, for
10389 example; they are created during parser and no longer needed after
10390 `grokdeclarator' has been called.)
10392 For a ptr-operator that has the optional cv-qualifier-seq,
10393 cv-qualifiers will be stored in the TREE_TYPE of the INDIRECT_REF
10396 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
10397 detect constructor, destructor or conversion operators. It is set
10398 to -1 if the declarator is a name, and +1 if it is a
10399 function. Otherwise it is set to zero. Usually you just want to
10400 test for >0, but internally the negative value is used.
10402 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
10403 a decl-specifier-seq unless it declares a constructor, destructor,
10404 or conversion. It might seem that we could check this condition in
10405 semantic analysis, rather than parsing, but that makes it difficult
10406 to handle something like `f()'. We want to notice that there are
10407 no decl-specifiers, and therefore realize that this is an
10408 expression, not a declaration.)
10410 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
10411 the declarator is a direct-declarator of the form "(...)". */
10414 cp_parser_declarator (cp_parser* parser,
10415 cp_parser_declarator_kind dcl_kind,
10416 int* ctor_dtor_or_conv_p,
10417 bool* parenthesized_p)
10421 enum tree_code code;
10422 tree cv_qualifier_seq;
10424 tree attributes = NULL_TREE;
10426 /* Assume this is not a constructor, destructor, or type-conversion
10428 if (ctor_dtor_or_conv_p)
10429 *ctor_dtor_or_conv_p = 0;
10431 if (cp_parser_allow_gnu_extensions_p (parser))
10432 attributes = cp_parser_attributes_opt (parser);
10434 /* Peek at the next token. */
10435 token = cp_lexer_peek_token (parser->lexer);
10437 /* Check for the ptr-operator production. */
10438 cp_parser_parse_tentatively (parser);
10439 /* Parse the ptr-operator. */
10440 code = cp_parser_ptr_operator (parser,
10442 &cv_qualifier_seq);
10443 /* If that worked, then we have a ptr-operator. */
10444 if (cp_parser_parse_definitely (parser))
10446 /* If a ptr-operator was found, then this declarator was not
10448 if (parenthesized_p)
10449 *parenthesized_p = true;
10450 /* The dependent declarator is optional if we are parsing an
10451 abstract-declarator. */
10452 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
10453 cp_parser_parse_tentatively (parser);
10455 /* Parse the dependent declarator. */
10456 declarator = cp_parser_declarator (parser, dcl_kind,
10457 /*ctor_dtor_or_conv_p=*/NULL,
10458 /*parenthesized_p=*/NULL);
10460 /* If we are parsing an abstract-declarator, we must handle the
10461 case where the dependent declarator is absent. */
10462 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
10463 && !cp_parser_parse_definitely (parser))
10464 declarator = NULL_TREE;
10466 /* Build the representation of the ptr-operator. */
10467 if (code == INDIRECT_REF)
10468 declarator = make_pointer_declarator (cv_qualifier_seq,
10471 declarator = make_reference_declarator (cv_qualifier_seq,
10473 /* Handle the pointer-to-member case. */
10475 declarator = build_nt (SCOPE_REF, class_type, declarator);
10477 /* Everything else is a direct-declarator. */
10480 if (parenthesized_p)
10481 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
10483 declarator = cp_parser_direct_declarator (parser, dcl_kind,
10484 ctor_dtor_or_conv_p);
10487 if (attributes && declarator != error_mark_node)
10488 declarator = tree_cons (attributes, declarator, NULL_TREE);
10493 /* Parse a direct-declarator or direct-abstract-declarator.
10497 direct-declarator ( parameter-declaration-clause )
10498 cv-qualifier-seq [opt]
10499 exception-specification [opt]
10500 direct-declarator [ constant-expression [opt] ]
10503 direct-abstract-declarator:
10504 direct-abstract-declarator [opt]
10505 ( parameter-declaration-clause )
10506 cv-qualifier-seq [opt]
10507 exception-specification [opt]
10508 direct-abstract-declarator [opt] [ constant-expression [opt] ]
10509 ( abstract-declarator )
10511 Returns a representation of the declarator. DCL_KIND is
10512 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
10513 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
10514 we are parsing a direct-declarator. It is
10515 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
10516 of ambiguity we prefer an abstract declarator, as per
10517 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P is as for
10518 cp_parser_declarator.
10520 For the declarator-id production, the representation is as for an
10521 id-expression, except that a qualified name is represented as a
10522 SCOPE_REF. A function-declarator is represented as a CALL_EXPR;
10523 see the documentation of the FUNCTION_DECLARATOR_* macros for
10524 information about how to find the various declarator components.
10525 An array-declarator is represented as an ARRAY_REF. The
10526 direct-declarator is the first operand; the constant-expression
10527 indicating the size of the array is the second operand. */
10530 cp_parser_direct_declarator (cp_parser* parser,
10531 cp_parser_declarator_kind dcl_kind,
10532 int* ctor_dtor_or_conv_p)
10535 tree declarator = NULL_TREE;
10536 tree scope = NULL_TREE;
10537 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
10538 bool saved_in_declarator_p = parser->in_declarator_p;
10540 bool pop_p = false;
10544 /* Peek at the next token. */
10545 token = cp_lexer_peek_token (parser->lexer);
10546 if (token->type == CPP_OPEN_PAREN)
10548 /* This is either a parameter-declaration-clause, or a
10549 parenthesized declarator. When we know we are parsing a
10550 named declarator, it must be a parenthesized declarator
10551 if FIRST is true. For instance, `(int)' is a
10552 parameter-declaration-clause, with an omitted
10553 direct-abstract-declarator. But `((*))', is a
10554 parenthesized abstract declarator. Finally, when T is a
10555 template parameter `(T)' is a
10556 parameter-declaration-clause, and not a parenthesized
10559 We first try and parse a parameter-declaration-clause,
10560 and then try a nested declarator (if FIRST is true).
10562 It is not an error for it not to be a
10563 parameter-declaration-clause, even when FIRST is
10569 The first is the declaration of a function while the
10570 second is a the definition of a variable, including its
10573 Having seen only the parenthesis, we cannot know which of
10574 these two alternatives should be selected. Even more
10575 complex are examples like:
10580 The former is a function-declaration; the latter is a
10581 variable initialization.
10583 Thus again, we try a parameter-declaration-clause, and if
10584 that fails, we back out and return. */
10586 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
10589 unsigned saved_num_template_parameter_lists;
10591 cp_parser_parse_tentatively (parser);
10593 /* Consume the `('. */
10594 cp_lexer_consume_token (parser->lexer);
10597 /* If this is going to be an abstract declarator, we're
10598 in a declarator and we can't have default args. */
10599 parser->default_arg_ok_p = false;
10600 parser->in_declarator_p = true;
10603 /* Inside the function parameter list, surrounding
10604 template-parameter-lists do not apply. */
10605 saved_num_template_parameter_lists
10606 = parser->num_template_parameter_lists;
10607 parser->num_template_parameter_lists = 0;
10609 /* Parse the parameter-declaration-clause. */
10610 params = cp_parser_parameter_declaration_clause (parser);
10612 parser->num_template_parameter_lists
10613 = saved_num_template_parameter_lists;
10615 /* If all went well, parse the cv-qualifier-seq and the
10616 exception-specification. */
10617 if (cp_parser_parse_definitely (parser))
10619 tree cv_qualifiers;
10620 tree exception_specification;
10622 if (ctor_dtor_or_conv_p)
10623 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
10625 /* Consume the `)'. */
10626 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
10628 /* Parse the cv-qualifier-seq. */
10629 cv_qualifiers = cp_parser_cv_qualifier_seq_opt (parser);
10630 /* And the exception-specification. */
10631 exception_specification
10632 = cp_parser_exception_specification_opt (parser);
10634 /* Create the function-declarator. */
10635 declarator = make_call_declarator (declarator,
10638 exception_specification);
10639 /* Any subsequent parameter lists are to do with
10640 return type, so are not those of the declared
10642 parser->default_arg_ok_p = false;
10644 /* Repeat the main loop. */
10649 /* If this is the first, we can try a parenthesized
10653 bool saved_in_type_id_in_expr_p;
10655 parser->default_arg_ok_p = saved_default_arg_ok_p;
10656 parser->in_declarator_p = saved_in_declarator_p;
10658 /* Consume the `('. */
10659 cp_lexer_consume_token (parser->lexer);
10660 /* Parse the nested declarator. */
10661 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
10662 parser->in_type_id_in_expr_p = true;
10664 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
10665 /*parenthesized_p=*/NULL);
10666 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
10668 /* Expect a `)'. */
10669 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
10670 declarator = error_mark_node;
10671 if (declarator == error_mark_node)
10674 goto handle_declarator;
10676 /* Otherwise, we must be done. */
10680 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
10681 && token->type == CPP_OPEN_SQUARE)
10683 /* Parse an array-declarator. */
10686 if (ctor_dtor_or_conv_p)
10687 *ctor_dtor_or_conv_p = 0;
10690 parser->default_arg_ok_p = false;
10691 parser->in_declarator_p = true;
10692 /* Consume the `['. */
10693 cp_lexer_consume_token (parser->lexer);
10694 /* Peek at the next token. */
10695 token = cp_lexer_peek_token (parser->lexer);
10696 /* If the next token is `]', then there is no
10697 constant-expression. */
10698 if (token->type != CPP_CLOSE_SQUARE)
10700 bool non_constant_p;
10703 = cp_parser_constant_expression (parser,
10704 /*allow_non_constant=*/true,
10706 if (!non_constant_p)
10707 bounds = fold_non_dependent_expr (bounds);
10710 bounds = NULL_TREE;
10711 /* Look for the closing `]'. */
10712 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'"))
10714 declarator = error_mark_node;
10718 declarator = build_nt (ARRAY_REF, declarator, bounds,
10719 NULL_TREE, NULL_TREE);
10721 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
10723 /* Parse a declarator-id */
10724 if (dcl_kind == CP_PARSER_DECLARATOR_EITHER)
10725 cp_parser_parse_tentatively (parser);
10726 declarator = cp_parser_declarator_id (parser);
10727 if (dcl_kind == CP_PARSER_DECLARATOR_EITHER)
10729 if (!cp_parser_parse_definitely (parser))
10730 declarator = error_mark_node;
10731 else if (TREE_CODE (declarator) != IDENTIFIER_NODE)
10733 cp_parser_error (parser, "expected unqualified-id");
10734 declarator = error_mark_node;
10738 if (declarator == error_mark_node)
10741 if (TREE_CODE (declarator) == SCOPE_REF
10742 && !current_scope ())
10744 tree scope = TREE_OPERAND (declarator, 0);
10746 /* In the declaration of a member of a template class
10747 outside of the class itself, the SCOPE will sometimes
10748 be a TYPENAME_TYPE. For example, given:
10750 template <typename T>
10751 int S<T>::R::i = 3;
10753 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
10754 this context, we must resolve S<T>::R to an ordinary
10755 type, rather than a typename type.
10757 The reason we normally avoid resolving TYPENAME_TYPEs
10758 is that a specialization of `S' might render
10759 `S<T>::R' not a type. However, if `S' is
10760 specialized, then this `i' will not be used, so there
10761 is no harm in resolving the types here. */
10762 if (TREE_CODE (scope) == TYPENAME_TYPE)
10766 /* Resolve the TYPENAME_TYPE. */
10767 type = resolve_typename_type (scope,
10768 /*only_current_p=*/false);
10769 /* If that failed, the declarator is invalid. */
10770 if (type == error_mark_node)
10771 error ("`%T::%D' is not a type",
10772 TYPE_CONTEXT (scope),
10773 TYPE_IDENTIFIER (scope));
10774 /* Build a new DECLARATOR. */
10775 declarator = build_nt (SCOPE_REF,
10777 TREE_OPERAND (declarator, 1));
10781 /* Check to see whether the declarator-id names a constructor,
10782 destructor, or conversion. */
10783 if (declarator && ctor_dtor_or_conv_p
10784 && ((TREE_CODE (declarator) == SCOPE_REF
10785 && CLASS_TYPE_P (TREE_OPERAND (declarator, 0)))
10786 || (TREE_CODE (declarator) != SCOPE_REF
10787 && at_class_scope_p ())))
10789 tree unqualified_name;
10792 /* Get the unqualified part of the name. */
10793 if (TREE_CODE (declarator) == SCOPE_REF)
10795 class_type = TREE_OPERAND (declarator, 0);
10796 unqualified_name = TREE_OPERAND (declarator, 1);
10800 class_type = current_class_type;
10801 unqualified_name = declarator;
10804 /* See if it names ctor, dtor or conv. */
10805 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR
10806 || IDENTIFIER_TYPENAME_P (unqualified_name)
10807 || constructor_name_p (unqualified_name, class_type)
10808 || (TREE_CODE (unqualified_name) == TYPE_DECL
10809 && same_type_p (TREE_TYPE (unqualified_name),
10811 *ctor_dtor_or_conv_p = -1;
10812 if (TREE_CODE (declarator) == SCOPE_REF
10813 && TREE_CODE (unqualified_name) == TYPE_DECL
10814 && CLASSTYPE_USE_TEMPLATE (TREE_TYPE (unqualified_name)))
10816 error ("invalid use of constructor as a template");
10817 inform ("use `%T::%D' instead of `%T::%T' to name the "
10818 "constructor in a qualified name", class_type,
10819 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
10820 class_type, class_type);
10824 handle_declarator:;
10825 scope = get_scope_of_declarator (declarator);
10827 /* Any names that appear after the declarator-id for a
10828 member are looked up in the containing scope. */
10829 pop_p = push_scope (scope);
10830 parser->in_declarator_p = true;
10831 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
10833 && (TREE_CODE (declarator) == SCOPE_REF
10834 || TREE_CODE (declarator) == IDENTIFIER_NODE)))
10835 /* Default args are only allowed on function
10837 parser->default_arg_ok_p = saved_default_arg_ok_p;
10839 parser->default_arg_ok_p = false;
10848 /* For an abstract declarator, we might wind up with nothing at this
10849 point. That's an error; the declarator is not optional. */
10851 cp_parser_error (parser, "expected declarator");
10853 /* If we entered a scope, we must exit it now. */
10857 parser->default_arg_ok_p = saved_default_arg_ok_p;
10858 parser->in_declarator_p = saved_in_declarator_p;
10863 /* Parse a ptr-operator.
10866 * cv-qualifier-seq [opt]
10868 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
10873 & cv-qualifier-seq [opt]
10875 Returns INDIRECT_REF if a pointer, or pointer-to-member, was
10876 used. Returns ADDR_EXPR if a reference was used. In the
10877 case of a pointer-to-member, *TYPE is filled in with the
10878 TYPE containing the member. *CV_QUALIFIER_SEQ is filled in
10879 with the cv-qualifier-seq, or NULL_TREE, if there are no
10880 cv-qualifiers. Returns ERROR_MARK if an error occurred. */
10882 static enum tree_code
10883 cp_parser_ptr_operator (cp_parser* parser,
10885 tree* cv_qualifier_seq)
10887 enum tree_code code = ERROR_MARK;
10890 /* Assume that it's not a pointer-to-member. */
10892 /* And that there are no cv-qualifiers. */
10893 *cv_qualifier_seq = NULL_TREE;
10895 /* Peek at the next token. */
10896 token = cp_lexer_peek_token (parser->lexer);
10897 /* If it's a `*' or `&' we have a pointer or reference. */
10898 if (token->type == CPP_MULT || token->type == CPP_AND)
10900 /* Remember which ptr-operator we were processing. */
10901 code = (token->type == CPP_AND ? ADDR_EXPR : INDIRECT_REF);
10903 /* Consume the `*' or `&'. */
10904 cp_lexer_consume_token (parser->lexer);
10906 /* A `*' can be followed by a cv-qualifier-seq, and so can a
10907 `&', if we are allowing GNU extensions. (The only qualifier
10908 that can legally appear after `&' is `restrict', but that is
10909 enforced during semantic analysis. */
10910 if (code == INDIRECT_REF
10911 || cp_parser_allow_gnu_extensions_p (parser))
10912 *cv_qualifier_seq = cp_parser_cv_qualifier_seq_opt (parser);
10916 /* Try the pointer-to-member case. */
10917 cp_parser_parse_tentatively (parser);
10918 /* Look for the optional `::' operator. */
10919 cp_parser_global_scope_opt (parser,
10920 /*current_scope_valid_p=*/false);
10921 /* Look for the nested-name specifier. */
10922 cp_parser_nested_name_specifier (parser,
10923 /*typename_keyword_p=*/false,
10924 /*check_dependency_p=*/true,
10926 /*is_declaration=*/false);
10927 /* If we found it, and the next token is a `*', then we are
10928 indeed looking at a pointer-to-member operator. */
10929 if (!cp_parser_error_occurred (parser)
10930 && cp_parser_require (parser, CPP_MULT, "`*'"))
10932 /* The type of which the member is a member is given by the
10934 *type = parser->scope;
10935 /* The next name will not be qualified. */
10936 parser->scope = NULL_TREE;
10937 parser->qualifying_scope = NULL_TREE;
10938 parser->object_scope = NULL_TREE;
10939 /* Indicate that the `*' operator was used. */
10940 code = INDIRECT_REF;
10941 /* Look for the optional cv-qualifier-seq. */
10942 *cv_qualifier_seq = cp_parser_cv_qualifier_seq_opt (parser);
10944 /* If that didn't work we don't have a ptr-operator. */
10945 if (!cp_parser_parse_definitely (parser))
10946 cp_parser_error (parser, "expected ptr-operator");
10952 /* Parse an (optional) cv-qualifier-seq.
10955 cv-qualifier cv-qualifier-seq [opt]
10957 Returns a TREE_LIST. The TREE_VALUE of each node is the
10958 representation of a cv-qualifier. */
10961 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
10963 tree cv_qualifiers = NULL_TREE;
10969 /* Look for the next cv-qualifier. */
10970 cv_qualifier = cp_parser_cv_qualifier_opt (parser);
10971 /* If we didn't find one, we're done. */
10975 /* Add this cv-qualifier to the list. */
10977 = tree_cons (NULL_TREE, cv_qualifier, cv_qualifiers);
10980 /* We built up the list in reverse order. */
10981 return nreverse (cv_qualifiers);
10984 /* Parse an (optional) cv-qualifier.
10996 cp_parser_cv_qualifier_opt (cp_parser* parser)
10999 tree cv_qualifier = NULL_TREE;
11001 /* Peek at the next token. */
11002 token = cp_lexer_peek_token (parser->lexer);
11003 /* See if it's a cv-qualifier. */
11004 switch (token->keyword)
11009 /* Save the value of the token. */
11010 cv_qualifier = token->value;
11011 /* Consume the token. */
11012 cp_lexer_consume_token (parser->lexer);
11019 return cv_qualifier;
11022 /* Parse a declarator-id.
11026 :: [opt] nested-name-specifier [opt] type-name
11028 In the `id-expression' case, the value returned is as for
11029 cp_parser_id_expression if the id-expression was an unqualified-id.
11030 If the id-expression was a qualified-id, then a SCOPE_REF is
11031 returned. The first operand is the scope (either a NAMESPACE_DECL
11032 or TREE_TYPE), but the second is still just a representation of an
11036 cp_parser_declarator_id (cp_parser* parser)
11038 tree id_expression;
11040 /* The expression must be an id-expression. Assume that qualified
11041 names are the names of types so that:
11044 int S<T>::R::i = 3;
11046 will work; we must treat `S<T>::R' as the name of a type.
11047 Similarly, assume that qualified names are templates, where
11051 int S<T>::R<T>::i = 3;
11054 id_expression = cp_parser_id_expression (parser,
11055 /*template_keyword_p=*/false,
11056 /*check_dependency_p=*/false,
11057 /*template_p=*/NULL,
11058 /*declarator_p=*/true);
11059 /* If the name was qualified, create a SCOPE_REF to represent
11063 id_expression = build_nt (SCOPE_REF, parser->scope, id_expression);
11064 parser->scope = NULL_TREE;
11067 return id_expression;
11070 /* Parse a type-id.
11073 type-specifier-seq abstract-declarator [opt]
11075 Returns the TYPE specified. */
11078 cp_parser_type_id (cp_parser* parser)
11080 tree type_specifier_seq;
11081 tree abstract_declarator;
11083 /* Parse the type-specifier-seq. */
11085 = cp_parser_type_specifier_seq (parser);
11086 if (type_specifier_seq == error_mark_node)
11087 return error_mark_node;
11089 /* There might or might not be an abstract declarator. */
11090 cp_parser_parse_tentatively (parser);
11091 /* Look for the declarator. */
11092 abstract_declarator
11093 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
11094 /*parenthesized_p=*/NULL);
11095 /* Check to see if there really was a declarator. */
11096 if (!cp_parser_parse_definitely (parser))
11097 abstract_declarator = NULL_TREE;
11099 return groktypename (build_tree_list (type_specifier_seq,
11100 abstract_declarator));
11103 /* Parse a type-specifier-seq.
11105 type-specifier-seq:
11106 type-specifier type-specifier-seq [opt]
11110 type-specifier-seq:
11111 attributes type-specifier-seq [opt]
11113 Returns a TREE_LIST. Either the TREE_VALUE of each node is a
11114 type-specifier, or the TREE_PURPOSE is a list of attributes. */
11117 cp_parser_type_specifier_seq (cp_parser* parser)
11119 bool seen_type_specifier = false;
11120 tree type_specifier_seq = NULL_TREE;
11122 /* Parse the type-specifiers and attributes. */
11125 tree type_specifier;
11127 /* Check for attributes first. */
11128 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
11130 type_specifier_seq = tree_cons (cp_parser_attributes_opt (parser),
11132 type_specifier_seq);
11136 /* After the first type-specifier, others are optional. */
11137 if (seen_type_specifier)
11138 cp_parser_parse_tentatively (parser);
11139 /* Look for the type-specifier. */
11140 type_specifier = cp_parser_type_specifier (parser,
11141 CP_PARSER_FLAGS_NONE,
11142 /*is_friend=*/false,
11143 /*is_declaration=*/false,
11146 /* If the first type-specifier could not be found, this is not a
11147 type-specifier-seq at all. */
11148 if (!seen_type_specifier && type_specifier == error_mark_node)
11149 return error_mark_node;
11150 /* If subsequent type-specifiers could not be found, the
11151 type-specifier-seq is complete. */
11152 else if (seen_type_specifier && !cp_parser_parse_definitely (parser))
11155 /* Add the new type-specifier to the list. */
11157 = tree_cons (NULL_TREE, type_specifier, type_specifier_seq);
11158 seen_type_specifier = true;
11161 /* We built up the list in reverse order. */
11162 return nreverse (type_specifier_seq);
11165 /* Parse a parameter-declaration-clause.
11167 parameter-declaration-clause:
11168 parameter-declaration-list [opt] ... [opt]
11169 parameter-declaration-list , ...
11171 Returns a representation for the parameter declarations. Each node
11172 is a TREE_LIST. (See cp_parser_parameter_declaration for the exact
11173 representation.) If the parameter-declaration-clause ends with an
11174 ellipsis, PARMLIST_ELLIPSIS_P will hold of the first node in the
11175 list. A return value of NULL_TREE indicates a
11176 parameter-declaration-clause consisting only of an ellipsis. */
11179 cp_parser_parameter_declaration_clause (cp_parser* parser)
11185 /* Peek at the next token. */
11186 token = cp_lexer_peek_token (parser->lexer);
11187 /* Check for trivial parameter-declaration-clauses. */
11188 if (token->type == CPP_ELLIPSIS)
11190 /* Consume the `...' token. */
11191 cp_lexer_consume_token (parser->lexer);
11194 else if (token->type == CPP_CLOSE_PAREN)
11195 /* There are no parameters. */
11197 #ifndef NO_IMPLICIT_EXTERN_C
11198 if (in_system_header && current_class_type == NULL
11199 && current_lang_name == lang_name_c)
11203 return void_list_node;
11205 /* Check for `(void)', too, which is a special case. */
11206 else if (token->keyword == RID_VOID
11207 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
11208 == CPP_CLOSE_PAREN))
11210 /* Consume the `void' token. */
11211 cp_lexer_consume_token (parser->lexer);
11212 /* There are no parameters. */
11213 return void_list_node;
11216 /* Parse the parameter-declaration-list. */
11217 parameters = cp_parser_parameter_declaration_list (parser);
11218 /* If a parse error occurred while parsing the
11219 parameter-declaration-list, then the entire
11220 parameter-declaration-clause is erroneous. */
11221 if (parameters == error_mark_node)
11222 return error_mark_node;
11224 /* Peek at the next token. */
11225 token = cp_lexer_peek_token (parser->lexer);
11226 /* If it's a `,', the clause should terminate with an ellipsis. */
11227 if (token->type == CPP_COMMA)
11229 /* Consume the `,'. */
11230 cp_lexer_consume_token (parser->lexer);
11231 /* Expect an ellipsis. */
11233 = (cp_parser_require (parser, CPP_ELLIPSIS, "`...'") != NULL);
11235 /* It might also be `...' if the optional trailing `,' was
11237 else if (token->type == CPP_ELLIPSIS)
11239 /* Consume the `...' token. */
11240 cp_lexer_consume_token (parser->lexer);
11241 /* And remember that we saw it. */
11245 ellipsis_p = false;
11247 /* Finish the parameter list. */
11248 return finish_parmlist (parameters, ellipsis_p);
11251 /* Parse a parameter-declaration-list.
11253 parameter-declaration-list:
11254 parameter-declaration
11255 parameter-declaration-list , parameter-declaration
11257 Returns a representation of the parameter-declaration-list, as for
11258 cp_parser_parameter_declaration_clause. However, the
11259 `void_list_node' is never appended to the list. */
11262 cp_parser_parameter_declaration_list (cp_parser* parser)
11264 tree parameters = NULL_TREE;
11266 /* Look for more parameters. */
11270 bool parenthesized_p;
11271 /* Parse the parameter. */
11273 = cp_parser_parameter_declaration (parser,
11274 /*template_parm_p=*/false,
11277 /* If a parse error occurred parsing the parameter declaration,
11278 then the entire parameter-declaration-list is erroneous. */
11279 if (parameter == error_mark_node)
11281 parameters = error_mark_node;
11284 /* Add the new parameter to the list. */
11285 TREE_CHAIN (parameter) = parameters;
11286 parameters = parameter;
11288 /* Peek at the next token. */
11289 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
11290 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11291 /* The parameter-declaration-list is complete. */
11293 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
11297 /* Peek at the next token. */
11298 token = cp_lexer_peek_nth_token (parser->lexer, 2);
11299 /* If it's an ellipsis, then the list is complete. */
11300 if (token->type == CPP_ELLIPSIS)
11302 /* Otherwise, there must be more parameters. Consume the
11304 cp_lexer_consume_token (parser->lexer);
11305 /* When parsing something like:
11307 int i(float f, double d)
11309 we can tell after seeing the declaration for "f" that we
11310 are not looking at an initialization of a variable "i",
11311 but rather at the declaration of a function "i".
11313 Due to the fact that the parsing of template arguments
11314 (as specified to a template-id) requires backtracking we
11315 cannot use this technique when inside a template argument
11317 if (!parser->in_template_argument_list_p
11318 && !parser->in_type_id_in_expr_p
11319 && cp_parser_parsing_tentatively (parser)
11320 && !cp_parser_committed_to_tentative_parse (parser)
11321 /* However, a parameter-declaration of the form
11322 "foat(f)" (which is a valid declaration of a
11323 parameter "f") can also be interpreted as an
11324 expression (the conversion of "f" to "float"). */
11325 && !parenthesized_p)
11326 cp_parser_commit_to_tentative_parse (parser);
11330 cp_parser_error (parser, "expected `,' or `...'");
11331 if (!cp_parser_parsing_tentatively (parser)
11332 || cp_parser_committed_to_tentative_parse (parser))
11333 cp_parser_skip_to_closing_parenthesis (parser,
11334 /*recovering=*/true,
11335 /*or_comma=*/false,
11336 /*consume_paren=*/false);
11341 /* We built up the list in reverse order; straighten it out now. */
11342 return nreverse (parameters);
11345 /* Parse a parameter declaration.
11347 parameter-declaration:
11348 decl-specifier-seq declarator
11349 decl-specifier-seq declarator = assignment-expression
11350 decl-specifier-seq abstract-declarator [opt]
11351 decl-specifier-seq abstract-declarator [opt] = assignment-expression
11353 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
11354 declares a template parameter. (In that case, a non-nested `>'
11355 token encountered during the parsing of the assignment-expression
11356 is not interpreted as a greater-than operator.)
11358 Returns a TREE_LIST representing the parameter-declaration. The
11359 TREE_PURPOSE is the default argument expression, or NULL_TREE if
11360 there is no default argument. The TREE_VALUE is a representation
11361 of the decl-specifier-seq and declarator. In particular, the
11362 TREE_VALUE will be a TREE_LIST whose TREE_PURPOSE represents the
11363 decl-specifier-seq and whose TREE_VALUE represents the declarator.
11364 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
11365 the declarator is of the form "(p)". */
11368 cp_parser_parameter_declaration (cp_parser *parser,
11369 bool template_parm_p,
11370 bool *parenthesized_p)
11372 int declares_class_or_enum;
11373 bool greater_than_is_operator_p;
11374 tree decl_specifiers;
11377 tree default_argument;
11380 const char *saved_message;
11382 /* In a template parameter, `>' is not an operator.
11386 When parsing a default template-argument for a non-type
11387 template-parameter, the first non-nested `>' is taken as the end
11388 of the template parameter-list rather than a greater-than
11390 greater_than_is_operator_p = !template_parm_p;
11392 /* Type definitions may not appear in parameter types. */
11393 saved_message = parser->type_definition_forbidden_message;
11394 parser->type_definition_forbidden_message
11395 = "types may not be defined in parameter types";
11397 /* Parse the declaration-specifiers. */
11399 = cp_parser_decl_specifier_seq (parser,
11400 CP_PARSER_FLAGS_NONE,
11402 &declares_class_or_enum);
11403 /* If an error occurred, there's no reason to attempt to parse the
11404 rest of the declaration. */
11405 if (cp_parser_error_occurred (parser))
11407 parser->type_definition_forbidden_message = saved_message;
11408 return error_mark_node;
11411 /* Peek at the next token. */
11412 token = cp_lexer_peek_token (parser->lexer);
11413 /* If the next token is a `)', `,', `=', `>', or `...', then there
11414 is no declarator. */
11415 if (token->type == CPP_CLOSE_PAREN
11416 || token->type == CPP_COMMA
11417 || token->type == CPP_EQ
11418 || token->type == CPP_ELLIPSIS
11419 || token->type == CPP_GREATER)
11421 declarator = NULL_TREE;
11422 if (parenthesized_p)
11423 *parenthesized_p = false;
11425 /* Otherwise, there should be a declarator. */
11428 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
11429 parser->default_arg_ok_p = false;
11431 /* After seeing a decl-specifier-seq, if the next token is not a
11432 "(", there is no possibility that the code is a valid
11433 expression. Therefore, if parsing tentatively, we commit at
11435 if (!parser->in_template_argument_list_p
11436 /* In an expression context, having seen:
11440 we cannot be sure whether we are looking at a
11441 function-type (taking a "char" as a parameter) or a cast
11442 of some object of type "char" to "int". */
11443 && !parser->in_type_id_in_expr_p
11444 && cp_parser_parsing_tentatively (parser)
11445 && !cp_parser_committed_to_tentative_parse (parser)
11446 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
11447 cp_parser_commit_to_tentative_parse (parser);
11448 /* Parse the declarator. */
11449 declarator = cp_parser_declarator (parser,
11450 CP_PARSER_DECLARATOR_EITHER,
11451 /*ctor_dtor_or_conv_p=*/NULL,
11453 parser->default_arg_ok_p = saved_default_arg_ok_p;
11454 /* After the declarator, allow more attributes. */
11455 attributes = chainon (attributes, cp_parser_attributes_opt (parser));
11458 /* The restriction on defining new types applies only to the type
11459 of the parameter, not to the default argument. */
11460 parser->type_definition_forbidden_message = saved_message;
11462 /* If the next token is `=', then process a default argument. */
11463 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11465 bool saved_greater_than_is_operator_p;
11466 /* Consume the `='. */
11467 cp_lexer_consume_token (parser->lexer);
11469 /* If we are defining a class, then the tokens that make up the
11470 default argument must be saved and processed later. */
11471 if (!template_parm_p && at_class_scope_p ()
11472 && TYPE_BEING_DEFINED (current_class_type))
11474 unsigned depth = 0;
11476 /* Create a DEFAULT_ARG to represented the unparsed default
11478 default_argument = make_node (DEFAULT_ARG);
11479 DEFARG_TOKENS (default_argument) = cp_token_cache_new ();
11481 /* Add tokens until we have processed the entire default
11488 /* Peek at the next token. */
11489 token = cp_lexer_peek_token (parser->lexer);
11490 /* What we do depends on what token we have. */
11491 switch (token->type)
11493 /* In valid code, a default argument must be
11494 immediately followed by a `,' `)', or `...'. */
11496 case CPP_CLOSE_PAREN:
11498 /* If we run into a non-nested `;', `}', or `]',
11499 then the code is invalid -- but the default
11500 argument is certainly over. */
11501 case CPP_SEMICOLON:
11502 case CPP_CLOSE_BRACE:
11503 case CPP_CLOSE_SQUARE:
11506 /* Update DEPTH, if necessary. */
11507 else if (token->type == CPP_CLOSE_PAREN
11508 || token->type == CPP_CLOSE_BRACE
11509 || token->type == CPP_CLOSE_SQUARE)
11513 case CPP_OPEN_PAREN:
11514 case CPP_OPEN_SQUARE:
11515 case CPP_OPEN_BRACE:
11520 /* If we see a non-nested `>', and `>' is not an
11521 operator, then it marks the end of the default
11523 if (!depth && !greater_than_is_operator_p)
11527 /* If we run out of tokens, issue an error message. */
11529 error ("file ends in default argument");
11535 /* In these cases, we should look for template-ids.
11536 For example, if the default argument is
11537 `X<int, double>()', we need to do name lookup to
11538 figure out whether or not `X' is a template; if
11539 so, the `,' does not end the default argument.
11541 That is not yet done. */
11548 /* If we've reached the end, stop. */
11552 /* Add the token to the token block. */
11553 token = cp_lexer_consume_token (parser->lexer);
11554 cp_token_cache_push_token (DEFARG_TOKENS (default_argument),
11558 /* Outside of a class definition, we can just parse the
11559 assignment-expression. */
11562 bool saved_local_variables_forbidden_p;
11564 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
11566 saved_greater_than_is_operator_p
11567 = parser->greater_than_is_operator_p;
11568 parser->greater_than_is_operator_p = greater_than_is_operator_p;
11569 /* Local variable names (and the `this' keyword) may not
11570 appear in a default argument. */
11571 saved_local_variables_forbidden_p
11572 = parser->local_variables_forbidden_p;
11573 parser->local_variables_forbidden_p = true;
11574 /* Parse the assignment-expression. */
11575 default_argument = cp_parser_assignment_expression (parser);
11576 /* Restore saved state. */
11577 parser->greater_than_is_operator_p
11578 = saved_greater_than_is_operator_p;
11579 parser->local_variables_forbidden_p
11580 = saved_local_variables_forbidden_p;
11582 if (!parser->default_arg_ok_p)
11584 if (!flag_pedantic_errors)
11585 warning ("deprecated use of default argument for parameter of non-function");
11588 error ("default arguments are only permitted for function parameters");
11589 default_argument = NULL_TREE;
11594 default_argument = NULL_TREE;
11596 /* Create the representation of the parameter. */
11598 decl_specifiers = tree_cons (attributes, NULL_TREE, decl_specifiers);
11599 parameter = build_tree_list (default_argument,
11600 build_tree_list (decl_specifiers,
11606 /* Parse a function-body.
11609 compound_statement */
11612 cp_parser_function_body (cp_parser *parser)
11614 cp_parser_compound_statement (parser, NULL, false);
11617 /* Parse a ctor-initializer-opt followed by a function-body. Return
11618 true if a ctor-initializer was present. */
11621 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
11624 bool ctor_initializer_p;
11626 /* Begin the function body. */
11627 body = begin_function_body ();
11628 /* Parse the optional ctor-initializer. */
11629 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
11630 /* Parse the function-body. */
11631 cp_parser_function_body (parser);
11632 /* Finish the function body. */
11633 finish_function_body (body);
11635 return ctor_initializer_p;
11638 /* Parse an initializer.
11641 = initializer-clause
11642 ( expression-list )
11644 Returns a expression representing the initializer. If no
11645 initializer is present, NULL_TREE is returned.
11647 *IS_PARENTHESIZED_INIT is set to TRUE if the `( expression-list )'
11648 production is used, and zero otherwise. *IS_PARENTHESIZED_INIT is
11649 set to FALSE if there is no initializer present. If there is an
11650 initializer, and it is not a constant-expression, *NON_CONSTANT_P
11651 is set to true; otherwise it is set to false. */
11654 cp_parser_initializer (cp_parser* parser, bool* is_parenthesized_init,
11655 bool* non_constant_p)
11660 /* Peek at the next token. */
11661 token = cp_lexer_peek_token (parser->lexer);
11663 /* Let our caller know whether or not this initializer was
11665 *is_parenthesized_init = (token->type == CPP_OPEN_PAREN);
11666 /* Assume that the initializer is constant. */
11667 *non_constant_p = false;
11669 if (token->type == CPP_EQ)
11671 /* Consume the `='. */
11672 cp_lexer_consume_token (parser->lexer);
11673 /* Parse the initializer-clause. */
11674 init = cp_parser_initializer_clause (parser, non_constant_p);
11676 else if (token->type == CPP_OPEN_PAREN)
11677 init = cp_parser_parenthesized_expression_list (parser, false,
11681 /* Anything else is an error. */
11682 cp_parser_error (parser, "expected initializer");
11683 init = error_mark_node;
11689 /* Parse an initializer-clause.
11691 initializer-clause:
11692 assignment-expression
11693 { initializer-list , [opt] }
11696 Returns an expression representing the initializer.
11698 If the `assignment-expression' production is used the value
11699 returned is simply a representation for the expression.
11701 Otherwise, a CONSTRUCTOR is returned. The CONSTRUCTOR_ELTS will be
11702 the elements of the initializer-list (or NULL_TREE, if the last
11703 production is used). The TREE_TYPE for the CONSTRUCTOR will be
11704 NULL_TREE. There is no way to detect whether or not the optional
11705 trailing `,' was provided. NON_CONSTANT_P is as for
11706 cp_parser_initializer. */
11709 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
11713 /* If it is not a `{', then we are looking at an
11714 assignment-expression. */
11715 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
11718 = cp_parser_constant_expression (parser,
11719 /*allow_non_constant_p=*/true,
11721 if (!*non_constant_p)
11722 initializer = fold_non_dependent_expr (initializer);
11726 /* Consume the `{' token. */
11727 cp_lexer_consume_token (parser->lexer);
11728 /* Create a CONSTRUCTOR to represent the braced-initializer. */
11729 initializer = make_node (CONSTRUCTOR);
11730 /* If it's not a `}', then there is a non-trivial initializer. */
11731 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
11733 /* Parse the initializer list. */
11734 CONSTRUCTOR_ELTS (initializer)
11735 = cp_parser_initializer_list (parser, non_constant_p);
11736 /* A trailing `,' token is allowed. */
11737 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
11738 cp_lexer_consume_token (parser->lexer);
11740 /* Now, there should be a trailing `}'. */
11741 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
11744 return initializer;
11747 /* Parse an initializer-list.
11751 initializer-list , initializer-clause
11756 identifier : initializer-clause
11757 initializer-list, identifier : initializer-clause
11759 Returns a TREE_LIST. The TREE_VALUE of each node is an expression
11760 for the initializer. If the TREE_PURPOSE is non-NULL, it is the
11761 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
11762 as for cp_parser_initializer. */
11765 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
11767 tree initializers = NULL_TREE;
11769 /* Assume all of the expressions are constant. */
11770 *non_constant_p = false;
11772 /* Parse the rest of the list. */
11778 bool clause_non_constant_p;
11780 /* If the next token is an identifier and the following one is a
11781 colon, we are looking at the GNU designated-initializer
11783 if (cp_parser_allow_gnu_extensions_p (parser)
11784 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
11785 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
11787 /* Consume the identifier. */
11788 identifier = cp_lexer_consume_token (parser->lexer)->value;
11789 /* Consume the `:'. */
11790 cp_lexer_consume_token (parser->lexer);
11793 identifier = NULL_TREE;
11795 /* Parse the initializer. */
11796 initializer = cp_parser_initializer_clause (parser,
11797 &clause_non_constant_p);
11798 /* If any clause is non-constant, so is the entire initializer. */
11799 if (clause_non_constant_p)
11800 *non_constant_p = true;
11801 /* Add it to the list. */
11802 initializers = tree_cons (identifier, initializer, initializers);
11804 /* If the next token is not a comma, we have reached the end of
11806 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11809 /* Peek at the next token. */
11810 token = cp_lexer_peek_nth_token (parser->lexer, 2);
11811 /* If the next token is a `}', then we're still done. An
11812 initializer-clause can have a trailing `,' after the
11813 initializer-list and before the closing `}'. */
11814 if (token->type == CPP_CLOSE_BRACE)
11817 /* Consume the `,' token. */
11818 cp_lexer_consume_token (parser->lexer);
11821 /* The initializers were built up in reverse order, so we need to
11822 reverse them now. */
11823 return nreverse (initializers);
11826 /* Classes [gram.class] */
11828 /* Parse a class-name.
11834 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
11835 to indicate that names looked up in dependent types should be
11836 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
11837 keyword has been used to indicate that the name that appears next
11838 is a template. TYPE_P is true iff the next name should be treated
11839 as class-name, even if it is declared to be some other kind of name
11840 as well. If CHECK_DEPENDENCY_P is FALSE, names are looked up in
11841 dependent scopes. If CLASS_HEAD_P is TRUE, this class is the class
11842 being defined in a class-head.
11844 Returns the TYPE_DECL representing the class. */
11847 cp_parser_class_name (cp_parser *parser,
11848 bool typename_keyword_p,
11849 bool template_keyword_p,
11851 bool check_dependency_p,
11853 bool is_declaration)
11860 /* All class-names start with an identifier. */
11861 token = cp_lexer_peek_token (parser->lexer);
11862 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
11864 cp_parser_error (parser, "expected class-name");
11865 return error_mark_node;
11868 /* PARSER->SCOPE can be cleared when parsing the template-arguments
11869 to a template-id, so we save it here. */
11870 scope = parser->scope;
11871 if (scope == error_mark_node)
11872 return error_mark_node;
11874 /* Any name names a type if we're following the `typename' keyword
11875 in a qualified name where the enclosing scope is type-dependent. */
11876 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
11877 && dependent_type_p (scope));
11878 /* Handle the common case (an identifier, but not a template-id)
11880 if (token->type == CPP_NAME
11881 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
11885 /* Look for the identifier. */
11886 identifier = cp_parser_identifier (parser);
11887 /* If the next token isn't an identifier, we are certainly not
11888 looking at a class-name. */
11889 if (identifier == error_mark_node)
11890 decl = error_mark_node;
11891 /* If we know this is a type-name, there's no need to look it
11893 else if (typename_p)
11897 /* If the next token is a `::', then the name must be a type
11900 [basic.lookup.qual]
11902 During the lookup for a name preceding the :: scope
11903 resolution operator, object, function, and enumerator
11904 names are ignored. */
11905 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
11907 /* Look up the name. */
11908 decl = cp_parser_lookup_name (parser, identifier,
11910 /*is_template=*/false,
11911 /*is_namespace=*/false,
11912 check_dependency_p);
11917 /* Try a template-id. */
11918 decl = cp_parser_template_id (parser, template_keyword_p,
11919 check_dependency_p,
11921 if (decl == error_mark_node)
11922 return error_mark_node;
11925 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
11927 /* If this is a typename, create a TYPENAME_TYPE. */
11928 if (typename_p && decl != error_mark_node)
11930 decl = make_typename_type (scope, decl, /*complain=*/1);
11931 if (decl != error_mark_node)
11932 decl = TYPE_NAME (decl);
11935 /* Check to see that it is really the name of a class. */
11936 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
11937 && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
11938 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
11939 /* Situations like this:
11941 template <typename T> struct A {
11942 typename T::template X<int>::I i;
11945 are problematic. Is `T::template X<int>' a class-name? The
11946 standard does not seem to be definitive, but there is no other
11947 valid interpretation of the following `::'. Therefore, those
11948 names are considered class-names. */
11949 decl = TYPE_NAME (make_typename_type (scope, decl, tf_error));
11950 else if (decl == error_mark_node
11951 || TREE_CODE (decl) != TYPE_DECL
11952 || !IS_AGGR_TYPE (TREE_TYPE (decl)))
11954 cp_parser_error (parser, "expected class-name");
11955 return error_mark_node;
11961 /* Parse a class-specifier.
11964 class-head { member-specification [opt] }
11966 Returns the TREE_TYPE representing the class. */
11969 cp_parser_class_specifier (cp_parser* parser)
11973 tree attributes = NULL_TREE;
11974 int has_trailing_semicolon;
11975 bool nested_name_specifier_p;
11976 unsigned saved_num_template_parameter_lists;
11977 bool pop_p = false;
11979 push_deferring_access_checks (dk_no_deferred);
11981 /* Parse the class-head. */
11982 type = cp_parser_class_head (parser,
11983 &nested_name_specifier_p,
11985 /* If the class-head was a semantic disaster, skip the entire body
11989 cp_parser_skip_to_end_of_block_or_statement (parser);
11990 pop_deferring_access_checks ();
11991 return error_mark_node;
11994 /* Look for the `{'. */
11995 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
11997 pop_deferring_access_checks ();
11998 return error_mark_node;
12001 /* Issue an error message if type-definitions are forbidden here. */
12002 cp_parser_check_type_definition (parser);
12003 /* Remember that we are defining one more class. */
12004 ++parser->num_classes_being_defined;
12005 /* Inside the class, surrounding template-parameter-lists do not
12007 saved_num_template_parameter_lists
12008 = parser->num_template_parameter_lists;
12009 parser->num_template_parameter_lists = 0;
12011 /* Start the class. */
12012 if (nested_name_specifier_p)
12013 pop_p = push_scope (CP_DECL_CONTEXT (TYPE_MAIN_DECL (type)));
12014 type = begin_class_definition (type);
12015 if (type == error_mark_node)
12016 /* If the type is erroneous, skip the entire body of the class. */
12017 cp_parser_skip_to_closing_brace (parser);
12019 /* Parse the member-specification. */
12020 cp_parser_member_specification_opt (parser);
12021 /* Look for the trailing `}'. */
12022 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
12023 /* We get better error messages by noticing a common problem: a
12024 missing trailing `;'. */
12025 token = cp_lexer_peek_token (parser->lexer);
12026 has_trailing_semicolon = (token->type == CPP_SEMICOLON);
12027 /* Look for trailing attributes to apply to this class. */
12028 if (cp_parser_allow_gnu_extensions_p (parser))
12030 tree sub_attr = cp_parser_attributes_opt (parser);
12031 attributes = chainon (attributes, sub_attr);
12033 if (type != error_mark_node)
12034 type = finish_struct (type, attributes);
12036 pop_scope (CP_DECL_CONTEXT (TYPE_MAIN_DECL (type)));
12037 /* If this class is not itself within the scope of another class,
12038 then we need to parse the bodies of all of the queued function
12039 definitions. Note that the queued functions defined in a class
12040 are not always processed immediately following the
12041 class-specifier for that class. Consider:
12044 struct B { void f() { sizeof (A); } };
12047 If `f' were processed before the processing of `A' were
12048 completed, there would be no way to compute the size of `A'.
12049 Note that the nesting we are interested in here is lexical --
12050 not the semantic nesting given by TYPE_CONTEXT. In particular,
12053 struct A { struct B; };
12054 struct A::B { void f() { } };
12056 there is no need to delay the parsing of `A::B::f'. */
12057 if (--parser->num_classes_being_defined == 0)
12062 /* In a first pass, parse default arguments to the functions.
12063 Then, in a second pass, parse the bodies of the functions.
12064 This two-phased approach handles cases like:
12072 for (TREE_PURPOSE (parser->unparsed_functions_queues)
12073 = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
12074 (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
12075 TREE_PURPOSE (parser->unparsed_functions_queues)
12076 = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
12078 fn = TREE_VALUE (queue_entry);
12079 /* Make sure that any template parameters are in scope. */
12080 maybe_begin_member_template_processing (fn);
12081 /* If there are default arguments that have not yet been processed,
12082 take care of them now. */
12083 cp_parser_late_parsing_default_args (parser, fn);
12084 /* Remove any template parameters from the symbol table. */
12085 maybe_end_member_template_processing ();
12087 /* Now parse the body of the functions. */
12088 for (TREE_VALUE (parser->unparsed_functions_queues)
12089 = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
12090 (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
12091 TREE_VALUE (parser->unparsed_functions_queues)
12092 = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
12094 /* Figure out which function we need to process. */
12095 fn = TREE_VALUE (queue_entry);
12097 /* A hack to prevent garbage collection. */
12100 /* Parse the function. */
12101 cp_parser_late_parsing_for_member (parser, fn);
12107 /* Put back any saved access checks. */
12108 pop_deferring_access_checks ();
12110 /* Restore the count of active template-parameter-lists. */
12111 parser->num_template_parameter_lists
12112 = saved_num_template_parameter_lists;
12117 /* Parse a class-head.
12120 class-key identifier [opt] base-clause [opt]
12121 class-key nested-name-specifier identifier base-clause [opt]
12122 class-key nested-name-specifier [opt] template-id
12126 class-key attributes identifier [opt] base-clause [opt]
12127 class-key attributes nested-name-specifier identifier base-clause [opt]
12128 class-key attributes nested-name-specifier [opt] template-id
12131 Returns the TYPE of the indicated class. Sets
12132 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
12133 involving a nested-name-specifier was used, and FALSE otherwise.
12135 Returns NULL_TREE if the class-head is syntactically valid, but
12136 semantically invalid in a way that means we should skip the entire
12137 body of the class. */
12140 cp_parser_class_head (cp_parser* parser,
12141 bool* nested_name_specifier_p,
12142 tree *attributes_p)
12145 tree nested_name_specifier;
12146 enum tag_types class_key;
12147 tree id = NULL_TREE;
12148 tree type = NULL_TREE;
12150 bool template_id_p = false;
12151 bool qualified_p = false;
12152 bool invalid_nested_name_p = false;
12153 bool invalid_explicit_specialization_p = false;
12154 bool pop_p = false;
12155 unsigned num_templates;
12157 /* Assume no nested-name-specifier will be present. */
12158 *nested_name_specifier_p = false;
12159 /* Assume no template parameter lists will be used in defining the
12163 /* Look for the class-key. */
12164 class_key = cp_parser_class_key (parser);
12165 if (class_key == none_type)
12166 return error_mark_node;
12168 /* Parse the attributes. */
12169 attributes = cp_parser_attributes_opt (parser);
12171 /* If the next token is `::', that is invalid -- but sometimes
12172 people do try to write:
12176 Handle this gracefully by accepting the extra qualifier, and then
12177 issuing an error about it later if this really is a
12178 class-head. If it turns out just to be an elaborated type
12179 specifier, remain silent. */
12180 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
12181 qualified_p = true;
12183 push_deferring_access_checks (dk_no_check);
12185 /* Determine the name of the class. Begin by looking for an
12186 optional nested-name-specifier. */
12187 nested_name_specifier
12188 = cp_parser_nested_name_specifier_opt (parser,
12189 /*typename_keyword_p=*/false,
12190 /*check_dependency_p=*/false,
12192 /*is_declaration=*/false);
12193 /* If there was a nested-name-specifier, then there *must* be an
12195 if (nested_name_specifier)
12197 /* Although the grammar says `identifier', it really means
12198 `class-name' or `template-name'. You are only allowed to
12199 define a class that has already been declared with this
12202 The proposed resolution for Core Issue 180 says that whever
12203 you see `class T::X' you should treat `X' as a type-name.
12205 It is OK to define an inaccessible class; for example:
12207 class A { class B; };
12210 We do not know if we will see a class-name, or a
12211 template-name. We look for a class-name first, in case the
12212 class-name is a template-id; if we looked for the
12213 template-name first we would stop after the template-name. */
12214 cp_parser_parse_tentatively (parser);
12215 type = cp_parser_class_name (parser,
12216 /*typename_keyword_p=*/false,
12217 /*template_keyword_p=*/false,
12219 /*check_dependency_p=*/false,
12220 /*class_head_p=*/true,
12221 /*is_declaration=*/false);
12222 /* If that didn't work, ignore the nested-name-specifier. */
12223 if (!cp_parser_parse_definitely (parser))
12225 invalid_nested_name_p = true;
12226 id = cp_parser_identifier (parser);
12227 if (id == error_mark_node)
12230 /* If we could not find a corresponding TYPE, treat this
12231 declaration like an unqualified declaration. */
12232 if (type == error_mark_node)
12233 nested_name_specifier = NULL_TREE;
12234 /* Otherwise, count the number of templates used in TYPE and its
12235 containing scopes. */
12240 for (scope = TREE_TYPE (type);
12241 scope && TREE_CODE (scope) != NAMESPACE_DECL;
12242 scope = (TYPE_P (scope)
12243 ? TYPE_CONTEXT (scope)
12244 : DECL_CONTEXT (scope)))
12246 && CLASS_TYPE_P (scope)
12247 && CLASSTYPE_TEMPLATE_INFO (scope)
12248 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
12249 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
12253 /* Otherwise, the identifier is optional. */
12256 /* We don't know whether what comes next is a template-id,
12257 an identifier, or nothing at all. */
12258 cp_parser_parse_tentatively (parser);
12259 /* Check for a template-id. */
12260 id = cp_parser_template_id (parser,
12261 /*template_keyword_p=*/false,
12262 /*check_dependency_p=*/true,
12263 /*is_declaration=*/true);
12264 /* If that didn't work, it could still be an identifier. */
12265 if (!cp_parser_parse_definitely (parser))
12267 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12268 id = cp_parser_identifier (parser);
12274 template_id_p = true;
12279 pop_deferring_access_checks ();
12282 cp_parser_check_for_invalid_template_id (parser, id);
12284 /* If it's not a `:' or a `{' then we can't really be looking at a
12285 class-head, since a class-head only appears as part of a
12286 class-specifier. We have to detect this situation before calling
12287 xref_tag, since that has irreversible side-effects. */
12288 if (!cp_parser_next_token_starts_class_definition_p (parser))
12290 cp_parser_error (parser, "expected `{' or `:'");
12291 return error_mark_node;
12294 /* At this point, we're going ahead with the class-specifier, even
12295 if some other problem occurs. */
12296 cp_parser_commit_to_tentative_parse (parser);
12297 /* Issue the error about the overly-qualified name now. */
12299 cp_parser_error (parser,
12300 "global qualification of class name is invalid");
12301 else if (invalid_nested_name_p)
12302 cp_parser_error (parser,
12303 "qualified name does not name a class");
12304 else if (nested_name_specifier)
12307 /* Figure out in what scope the declaration is being placed. */
12308 scope = current_scope ();
12310 scope = current_namespace;
12311 /* If that scope does not contain the scope in which the
12312 class was originally declared, the program is invalid. */
12313 if (scope && !is_ancestor (scope, nested_name_specifier))
12315 error ("declaration of `%D' in `%D' which does not "
12316 "enclose `%D'", type, scope, nested_name_specifier);
12322 A declarator-id shall not be qualified exception of the
12323 definition of a ... nested class outside of its class
12324 ... [or] a the definition or explicit instantiation of a
12325 class member of a namespace outside of its namespace. */
12326 if (scope == nested_name_specifier)
12328 pedwarn ("extra qualification ignored");
12329 nested_name_specifier = NULL_TREE;
12333 /* An explicit-specialization must be preceded by "template <>". If
12334 it is not, try to recover gracefully. */
12335 if (at_namespace_scope_p ()
12336 && parser->num_template_parameter_lists == 0
12339 error ("an explicit specialization must be preceded by 'template <>'");
12340 invalid_explicit_specialization_p = true;
12341 /* Take the same action that would have been taken by
12342 cp_parser_explicit_specialization. */
12343 ++parser->num_template_parameter_lists;
12344 begin_specialization ();
12346 /* There must be no "return" statements between this point and the
12347 end of this function; set "type "to the correct return value and
12348 use "goto done;" to return. */
12349 /* Make sure that the right number of template parameters were
12351 if (!cp_parser_check_template_parameters (parser, num_templates))
12353 /* If something went wrong, there is no point in even trying to
12354 process the class-definition. */
12359 /* Look up the type. */
12362 type = TREE_TYPE (id);
12363 maybe_process_partial_specialization (type);
12365 else if (!nested_name_specifier)
12367 /* If the class was unnamed, create a dummy name. */
12369 id = make_anon_name ();
12370 type = xref_tag (class_key, id, /*globalize=*/false,
12371 parser->num_template_parameter_lists);
12376 bool pop_p = false;
12380 template <typename T> struct S { struct T };
12381 template <typename T> struct S<T>::T { };
12383 we will get a TYPENAME_TYPE when processing the definition of
12384 `S::T'. We need to resolve it to the actual type before we
12385 try to define it. */
12386 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
12388 class_type = resolve_typename_type (TREE_TYPE (type),
12389 /*only_current_p=*/false);
12390 if (class_type != error_mark_node)
12391 type = TYPE_NAME (class_type);
12394 cp_parser_error (parser, "could not resolve typename type");
12395 type = error_mark_node;
12399 maybe_process_partial_specialization (TREE_TYPE (type));
12400 class_type = current_class_type;
12401 /* Enter the scope indicated by the nested-name-specifier. */
12402 if (nested_name_specifier)
12403 pop_p = push_scope (nested_name_specifier);
12404 /* Get the canonical version of this type. */
12405 type = TYPE_MAIN_DECL (TREE_TYPE (type));
12406 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
12407 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
12408 type = push_template_decl (type);
12409 type = TREE_TYPE (type);
12410 if (nested_name_specifier)
12412 *nested_name_specifier_p = true;
12414 pop_scope (nested_name_specifier);
12417 /* Indicate whether this class was declared as a `class' or as a
12419 if (TREE_CODE (type) == RECORD_TYPE)
12420 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
12421 cp_parser_check_class_key (class_key, type);
12423 /* Enter the scope containing the class; the names of base classes
12424 should be looked up in that context. For example, given:
12426 struct A { struct B {}; struct C; };
12427 struct A::C : B {};
12430 if (nested_name_specifier)
12431 pop_p = push_scope (nested_name_specifier);
12432 /* Now, look for the base-clause. */
12433 token = cp_lexer_peek_token (parser->lexer);
12434 if (token->type == CPP_COLON)
12438 /* Get the list of base-classes. */
12439 bases = cp_parser_base_clause (parser);
12440 /* Process them. */
12441 xref_basetypes (type, bases);
12443 /* Leave the scope given by the nested-name-specifier. We will
12444 enter the class scope itself while processing the members. */
12446 pop_scope (nested_name_specifier);
12449 if (invalid_explicit_specialization_p)
12451 end_specialization ();
12452 --parser->num_template_parameter_lists;
12454 *attributes_p = attributes;
12458 /* Parse a class-key.
12465 Returns the kind of class-key specified, or none_type to indicate
12468 static enum tag_types
12469 cp_parser_class_key (cp_parser* parser)
12472 enum tag_types tag_type;
12474 /* Look for the class-key. */
12475 token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
12479 /* Check to see if the TOKEN is a class-key. */
12480 tag_type = cp_parser_token_is_class_key (token);
12482 cp_parser_error (parser, "expected class-key");
12486 /* Parse an (optional) member-specification.
12488 member-specification:
12489 member-declaration member-specification [opt]
12490 access-specifier : member-specification [opt] */
12493 cp_parser_member_specification_opt (cp_parser* parser)
12500 /* Peek at the next token. */
12501 token = cp_lexer_peek_token (parser->lexer);
12502 /* If it's a `}', or EOF then we've seen all the members. */
12503 if (token->type == CPP_CLOSE_BRACE || token->type == CPP_EOF)
12506 /* See if this token is a keyword. */
12507 keyword = token->keyword;
12511 case RID_PROTECTED:
12513 /* Consume the access-specifier. */
12514 cp_lexer_consume_token (parser->lexer);
12515 /* Remember which access-specifier is active. */
12516 current_access_specifier = token->value;
12517 /* Look for the `:'. */
12518 cp_parser_require (parser, CPP_COLON, "`:'");
12522 /* Otherwise, the next construction must be a
12523 member-declaration. */
12524 cp_parser_member_declaration (parser);
12529 /* Parse a member-declaration.
12531 member-declaration:
12532 decl-specifier-seq [opt] member-declarator-list [opt] ;
12533 function-definition ; [opt]
12534 :: [opt] nested-name-specifier template [opt] unqualified-id ;
12536 template-declaration
12538 member-declarator-list:
12540 member-declarator-list , member-declarator
12543 declarator pure-specifier [opt]
12544 declarator constant-initializer [opt]
12545 identifier [opt] : constant-expression
12549 member-declaration:
12550 __extension__ member-declaration
12553 declarator attributes [opt] pure-specifier [opt]
12554 declarator attributes [opt] constant-initializer [opt]
12555 identifier [opt] attributes [opt] : constant-expression */
12558 cp_parser_member_declaration (cp_parser* parser)
12560 tree decl_specifiers;
12561 tree prefix_attributes;
12563 int declares_class_or_enum;
12566 int saved_pedantic;
12568 /* Check for the `__extension__' keyword. */
12569 if (cp_parser_extension_opt (parser, &saved_pedantic))
12572 cp_parser_member_declaration (parser);
12573 /* Restore the old value of the PEDANTIC flag. */
12574 pedantic = saved_pedantic;
12579 /* Check for a template-declaration. */
12580 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
12582 /* Parse the template-declaration. */
12583 cp_parser_template_declaration (parser, /*member_p=*/true);
12588 /* Check for a using-declaration. */
12589 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
12591 /* Parse the using-declaration. */
12592 cp_parser_using_declaration (parser);
12597 /* Parse the decl-specifier-seq. */
12599 = cp_parser_decl_specifier_seq (parser,
12600 CP_PARSER_FLAGS_OPTIONAL,
12601 &prefix_attributes,
12602 &declares_class_or_enum);
12603 /* Check for an invalid type-name. */
12604 if (cp_parser_parse_and_diagnose_invalid_type_name (parser))
12606 /* If there is no declarator, then the decl-specifier-seq should
12608 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12610 /* If there was no decl-specifier-seq, and the next token is a
12611 `;', then we have something like:
12617 Each member-declaration shall declare at least one member
12618 name of the class. */
12619 if (!decl_specifiers)
12622 pedwarn ("extra semicolon");
12628 /* See if this declaration is a friend. */
12629 friend_p = cp_parser_friend_p (decl_specifiers);
12630 /* If there were decl-specifiers, check to see if there was
12631 a class-declaration. */
12632 type = check_tag_decl (decl_specifiers);
12633 /* Nested classes have already been added to the class, but
12634 a `friend' needs to be explicitly registered. */
12637 /* If the `friend' keyword was present, the friend must
12638 be introduced with a class-key. */
12639 if (!declares_class_or_enum)
12640 error ("a class-key must be used when declaring a friend");
12643 template <typename T> struct A {
12644 friend struct A<T>::B;
12647 A<T>::B will be represented by a TYPENAME_TYPE, and
12648 therefore not recognized by check_tag_decl. */
12653 for (specifier = decl_specifiers;
12655 specifier = TREE_CHAIN (specifier))
12657 tree s = TREE_VALUE (specifier);
12659 if (TREE_CODE (s) == IDENTIFIER_NODE)
12660 get_global_value_if_present (s, &type);
12661 if (TREE_CODE (s) == TYPE_DECL)
12670 if (!type || !TYPE_P (type))
12671 error ("friend declaration does not name a class or "
12674 make_friend_class (current_class_type, type,
12675 /*complain=*/true);
12677 /* If there is no TYPE, an error message will already have
12681 /* An anonymous aggregate has to be handled specially; such
12682 a declaration really declares a data member (with a
12683 particular type), as opposed to a nested class. */
12684 else if (ANON_AGGR_TYPE_P (type))
12686 /* Remove constructors and such from TYPE, now that we
12687 know it is an anonymous aggregate. */
12688 fixup_anonymous_aggr (type);
12689 /* And make the corresponding data member. */
12690 decl = build_decl (FIELD_DECL, NULL_TREE, type);
12691 /* Add it to the class. */
12692 finish_member_declaration (decl);
12695 cp_parser_check_access_in_redeclaration (TYPE_NAME (type));
12700 /* See if these declarations will be friends. */
12701 friend_p = cp_parser_friend_p (decl_specifiers);
12703 /* Keep going until we hit the `;' at the end of the
12705 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12707 tree attributes = NULL_TREE;
12708 tree first_attribute;
12710 /* Peek at the next token. */
12711 token = cp_lexer_peek_token (parser->lexer);
12713 /* Check for a bitfield declaration. */
12714 if (token->type == CPP_COLON
12715 || (token->type == CPP_NAME
12716 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
12722 /* Get the name of the bitfield. Note that we cannot just
12723 check TOKEN here because it may have been invalidated by
12724 the call to cp_lexer_peek_nth_token above. */
12725 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
12726 identifier = cp_parser_identifier (parser);
12728 identifier = NULL_TREE;
12730 /* Consume the `:' token. */
12731 cp_lexer_consume_token (parser->lexer);
12732 /* Get the width of the bitfield. */
12734 = cp_parser_constant_expression (parser,
12735 /*allow_non_constant=*/false,
12738 /* Look for attributes that apply to the bitfield. */
12739 attributes = cp_parser_attributes_opt (parser);
12740 /* Remember which attributes are prefix attributes and
12742 first_attribute = attributes;
12743 /* Combine the attributes. */
12744 attributes = chainon (prefix_attributes, attributes);
12746 /* Create the bitfield declaration. */
12747 decl = grokbitfield (identifier,
12750 /* Apply the attributes. */
12751 cplus_decl_attributes (&decl, attributes, /*flags=*/0);
12757 tree asm_specification;
12758 int ctor_dtor_or_conv_p;
12760 /* Parse the declarator. */
12762 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
12763 &ctor_dtor_or_conv_p,
12764 /*parenthesized_p=*/NULL);
12766 /* If something went wrong parsing the declarator, make sure
12767 that we at least consume some tokens. */
12768 if (declarator == error_mark_node)
12770 /* Skip to the end of the statement. */
12771 cp_parser_skip_to_end_of_statement (parser);
12772 /* If the next token is not a semicolon, that is
12773 probably because we just skipped over the body of
12774 a function. So, we consume a semicolon if
12775 present, but do not issue an error message if it
12777 if (cp_lexer_next_token_is (parser->lexer,
12779 cp_lexer_consume_token (parser->lexer);
12783 cp_parser_check_for_definition_in_return_type
12784 (declarator, declares_class_or_enum);
12786 /* Look for an asm-specification. */
12787 asm_specification = cp_parser_asm_specification_opt (parser);
12788 /* Look for attributes that apply to the declaration. */
12789 attributes = cp_parser_attributes_opt (parser);
12790 /* Remember which attributes are prefix attributes and
12792 first_attribute = attributes;
12793 /* Combine the attributes. */
12794 attributes = chainon (prefix_attributes, attributes);
12796 /* If it's an `=', then we have a constant-initializer or a
12797 pure-specifier. It is not correct to parse the
12798 initializer before registering the member declaration
12799 since the member declaration should be in scope while
12800 its initializer is processed. However, the rest of the
12801 front end does not yet provide an interface that allows
12802 us to handle this correctly. */
12803 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12807 A pure-specifier shall be used only in the declaration of
12808 a virtual function.
12810 A member-declarator can contain a constant-initializer
12811 only if it declares a static member of integral or
12814 Therefore, if the DECLARATOR is for a function, we look
12815 for a pure-specifier; otherwise, we look for a
12816 constant-initializer. When we call `grokfield', it will
12817 perform more stringent semantics checks. */
12818 if (TREE_CODE (declarator) == CALL_EXPR)
12819 initializer = cp_parser_pure_specifier (parser);
12821 /* Parse the initializer. */
12822 initializer = cp_parser_constant_initializer (parser);
12824 /* Otherwise, there is no initializer. */
12826 initializer = NULL_TREE;
12828 /* See if we are probably looking at a function
12829 definition. We are certainly not looking at at a
12830 member-declarator. Calling `grokfield' has
12831 side-effects, so we must not do it unless we are sure
12832 that we are looking at a member-declarator. */
12833 if (cp_parser_token_starts_function_definition_p
12834 (cp_lexer_peek_token (parser->lexer)))
12836 /* The grammar does not allow a pure-specifier to be
12837 used when a member function is defined. (It is
12838 possible that this fact is an oversight in the
12839 standard, since a pure function may be defined
12840 outside of the class-specifier. */
12842 error ("pure-specifier on function-definition");
12843 decl = cp_parser_save_member_function_body (parser,
12847 /* If the member was not a friend, declare it here. */
12849 finish_member_declaration (decl);
12850 /* Peek at the next token. */
12851 token = cp_lexer_peek_token (parser->lexer);
12852 /* If the next token is a semicolon, consume it. */
12853 if (token->type == CPP_SEMICOLON)
12854 cp_lexer_consume_token (parser->lexer);
12859 /* Create the declaration. */
12860 decl = grokfield (declarator, decl_specifiers,
12861 initializer, asm_specification,
12863 /* Any initialization must have been from a
12864 constant-expression. */
12865 if (decl && TREE_CODE (decl) == VAR_DECL && initializer)
12866 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = 1;
12870 /* Reset PREFIX_ATTRIBUTES. */
12871 while (attributes && TREE_CHAIN (attributes) != first_attribute)
12872 attributes = TREE_CHAIN (attributes);
12874 TREE_CHAIN (attributes) = NULL_TREE;
12876 /* If there is any qualification still in effect, clear it
12877 now; we will be starting fresh with the next declarator. */
12878 parser->scope = NULL_TREE;
12879 parser->qualifying_scope = NULL_TREE;
12880 parser->object_scope = NULL_TREE;
12881 /* If it's a `,', then there are more declarators. */
12882 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
12883 cp_lexer_consume_token (parser->lexer);
12884 /* If the next token isn't a `;', then we have a parse error. */
12885 else if (cp_lexer_next_token_is_not (parser->lexer,
12888 cp_parser_error (parser, "expected `;'");
12889 /* Skip tokens until we find a `;'. */
12890 cp_parser_skip_to_end_of_statement (parser);
12897 /* Add DECL to the list of members. */
12899 finish_member_declaration (decl);
12901 if (TREE_CODE (decl) == FUNCTION_DECL)
12902 cp_parser_save_default_args (parser, decl);
12907 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
12910 /* Parse a pure-specifier.
12915 Returns INTEGER_ZERO_NODE if a pure specifier is found.
12916 Otherwise, ERROR_MARK_NODE is returned. */
12919 cp_parser_pure_specifier (cp_parser* parser)
12923 /* Look for the `=' token. */
12924 if (!cp_parser_require (parser, CPP_EQ, "`='"))
12925 return error_mark_node;
12926 /* Look for the `0' token. */
12927 token = cp_parser_require (parser, CPP_NUMBER, "`0'");
12928 /* Unfortunately, this will accept `0L' and `0x00' as well. We need
12929 to get information from the lexer about how the number was
12930 spelled in order to fix this problem. */
12931 if (!token || !integer_zerop (token->value))
12932 return error_mark_node;
12934 return integer_zero_node;
12937 /* Parse a constant-initializer.
12939 constant-initializer:
12940 = constant-expression
12942 Returns a representation of the constant-expression. */
12945 cp_parser_constant_initializer (cp_parser* parser)
12947 /* Look for the `=' token. */
12948 if (!cp_parser_require (parser, CPP_EQ, "`='"))
12949 return error_mark_node;
12951 /* It is invalid to write:
12953 struct S { static const int i = { 7 }; };
12956 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12958 cp_parser_error (parser,
12959 "a brace-enclosed initializer is not allowed here");
12960 /* Consume the opening brace. */
12961 cp_lexer_consume_token (parser->lexer);
12962 /* Skip the initializer. */
12963 cp_parser_skip_to_closing_brace (parser);
12964 /* Look for the trailing `}'. */
12965 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
12967 return error_mark_node;
12970 return cp_parser_constant_expression (parser,
12971 /*allow_non_constant=*/false,
12975 /* Derived classes [gram.class.derived] */
12977 /* Parse a base-clause.
12980 : base-specifier-list
12982 base-specifier-list:
12984 base-specifier-list , base-specifier
12986 Returns a TREE_LIST representing the base-classes, in the order in
12987 which they were declared. The representation of each node is as
12988 described by cp_parser_base_specifier.
12990 In the case that no bases are specified, this function will return
12991 NULL_TREE, not ERROR_MARK_NODE. */
12994 cp_parser_base_clause (cp_parser* parser)
12996 tree bases = NULL_TREE;
12998 /* Look for the `:' that begins the list. */
12999 cp_parser_require (parser, CPP_COLON, "`:'");
13001 /* Scan the base-specifier-list. */
13007 /* Look for the base-specifier. */
13008 base = cp_parser_base_specifier (parser);
13009 /* Add BASE to the front of the list. */
13010 if (base != error_mark_node)
13012 TREE_CHAIN (base) = bases;
13015 /* Peek at the next token. */
13016 token = cp_lexer_peek_token (parser->lexer);
13017 /* If it's not a comma, then the list is complete. */
13018 if (token->type != CPP_COMMA)
13020 /* Consume the `,'. */
13021 cp_lexer_consume_token (parser->lexer);
13024 /* PARSER->SCOPE may still be non-NULL at this point, if the last
13025 base class had a qualified name. However, the next name that
13026 appears is certainly not qualified. */
13027 parser->scope = NULL_TREE;
13028 parser->qualifying_scope = NULL_TREE;
13029 parser->object_scope = NULL_TREE;
13031 return nreverse (bases);
13034 /* Parse a base-specifier.
13037 :: [opt] nested-name-specifier [opt] class-name
13038 virtual access-specifier [opt] :: [opt] nested-name-specifier
13040 access-specifier virtual [opt] :: [opt] nested-name-specifier
13043 Returns a TREE_LIST. The TREE_PURPOSE will be one of
13044 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
13045 indicate the specifiers provided. The TREE_VALUE will be a TYPE
13046 (or the ERROR_MARK_NODE) indicating the type that was specified. */
13049 cp_parser_base_specifier (cp_parser* parser)
13053 bool virtual_p = false;
13054 bool duplicate_virtual_error_issued_p = false;
13055 bool duplicate_access_error_issued_p = false;
13056 bool class_scope_p, template_p;
13057 tree access = access_default_node;
13060 /* Process the optional `virtual' and `access-specifier'. */
13063 /* Peek at the next token. */
13064 token = cp_lexer_peek_token (parser->lexer);
13065 /* Process `virtual'. */
13066 switch (token->keyword)
13069 /* If `virtual' appears more than once, issue an error. */
13070 if (virtual_p && !duplicate_virtual_error_issued_p)
13072 cp_parser_error (parser,
13073 "`virtual' specified more than once in base-specified");
13074 duplicate_virtual_error_issued_p = true;
13079 /* Consume the `virtual' token. */
13080 cp_lexer_consume_token (parser->lexer);
13085 case RID_PROTECTED:
13087 /* If more than one access specifier appears, issue an
13089 if (access != access_default_node
13090 && !duplicate_access_error_issued_p)
13092 cp_parser_error (parser,
13093 "more than one access specifier in base-specified");
13094 duplicate_access_error_issued_p = true;
13097 access = ridpointers[(int) token->keyword];
13099 /* Consume the access-specifier. */
13100 cp_lexer_consume_token (parser->lexer);
13109 /* It is not uncommon to see programs mechanically, erroneously, use
13110 the 'typename' keyword to denote (dependent) qualified types
13111 as base classes. */
13112 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
13114 if (!processing_template_decl)
13115 error ("keyword `typename' not allowed outside of templates");
13117 error ("keyword `typename' not allowed in this context "
13118 "(the base class is implicitly a type)");
13119 cp_lexer_consume_token (parser->lexer);
13122 /* Look for the optional `::' operator. */
13123 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
13124 /* Look for the nested-name-specifier. The simplest way to
13129 The keyword `typename' is not permitted in a base-specifier or
13130 mem-initializer; in these contexts a qualified name that
13131 depends on a template-parameter is implicitly assumed to be a
13134 is to pretend that we have seen the `typename' keyword at this
13136 cp_parser_nested_name_specifier_opt (parser,
13137 /*typename_keyword_p=*/true,
13138 /*check_dependency_p=*/true,
13140 /*is_declaration=*/true);
13141 /* If the base class is given by a qualified name, assume that names
13142 we see are type names or templates, as appropriate. */
13143 class_scope_p = (parser->scope && TYPE_P (parser->scope));
13144 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
13146 /* Finally, look for the class-name. */
13147 type = cp_parser_class_name (parser,
13151 /*check_dependency_p=*/true,
13152 /*class_head_p=*/false,
13153 /*is_declaration=*/true);
13155 if (type == error_mark_node)
13156 return error_mark_node;
13158 return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
13161 /* Exception handling [gram.exception] */
13163 /* Parse an (optional) exception-specification.
13165 exception-specification:
13166 throw ( type-id-list [opt] )
13168 Returns a TREE_LIST representing the exception-specification. The
13169 TREE_VALUE of each node is a type. */
13172 cp_parser_exception_specification_opt (cp_parser* parser)
13177 /* Peek at the next token. */
13178 token = cp_lexer_peek_token (parser->lexer);
13179 /* If it's not `throw', then there's no exception-specification. */
13180 if (!cp_parser_is_keyword (token, RID_THROW))
13183 /* Consume the `throw'. */
13184 cp_lexer_consume_token (parser->lexer);
13186 /* Look for the `('. */
13187 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13189 /* Peek at the next token. */
13190 token = cp_lexer_peek_token (parser->lexer);
13191 /* If it's not a `)', then there is a type-id-list. */
13192 if (token->type != CPP_CLOSE_PAREN)
13194 const char *saved_message;
13196 /* Types may not be defined in an exception-specification. */
13197 saved_message = parser->type_definition_forbidden_message;
13198 parser->type_definition_forbidden_message
13199 = "types may not be defined in an exception-specification";
13200 /* Parse the type-id-list. */
13201 type_id_list = cp_parser_type_id_list (parser);
13202 /* Restore the saved message. */
13203 parser->type_definition_forbidden_message = saved_message;
13206 type_id_list = empty_except_spec;
13208 /* Look for the `)'. */
13209 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13211 return type_id_list;
13214 /* Parse an (optional) type-id-list.
13218 type-id-list , type-id
13220 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
13221 in the order that the types were presented. */
13224 cp_parser_type_id_list (cp_parser* parser)
13226 tree types = NULL_TREE;
13233 /* Get the next type-id. */
13234 type = cp_parser_type_id (parser);
13235 /* Add it to the list. */
13236 types = add_exception_specifier (types, type, /*complain=*/1);
13237 /* Peek at the next token. */
13238 token = cp_lexer_peek_token (parser->lexer);
13239 /* If it is not a `,', we are done. */
13240 if (token->type != CPP_COMMA)
13242 /* Consume the `,'. */
13243 cp_lexer_consume_token (parser->lexer);
13246 return nreverse (types);
13249 /* Parse a try-block.
13252 try compound-statement handler-seq */
13255 cp_parser_try_block (cp_parser* parser)
13259 cp_parser_require_keyword (parser, RID_TRY, "`try'");
13260 try_block = begin_try_block ();
13261 cp_parser_compound_statement (parser, NULL, true);
13262 finish_try_block (try_block);
13263 cp_parser_handler_seq (parser);
13264 finish_handler_sequence (try_block);
13269 /* Parse a function-try-block.
13271 function-try-block:
13272 try ctor-initializer [opt] function-body handler-seq */
13275 cp_parser_function_try_block (cp_parser* parser)
13278 bool ctor_initializer_p;
13280 /* Look for the `try' keyword. */
13281 if (!cp_parser_require_keyword (parser, RID_TRY, "`try'"))
13283 /* Let the rest of the front-end know where we are. */
13284 try_block = begin_function_try_block ();
13285 /* Parse the function-body. */
13287 = cp_parser_ctor_initializer_opt_and_function_body (parser);
13288 /* We're done with the `try' part. */
13289 finish_function_try_block (try_block);
13290 /* Parse the handlers. */
13291 cp_parser_handler_seq (parser);
13292 /* We're done with the handlers. */
13293 finish_function_handler_sequence (try_block);
13295 return ctor_initializer_p;
13298 /* Parse a handler-seq.
13301 handler handler-seq [opt] */
13304 cp_parser_handler_seq (cp_parser* parser)
13310 /* Parse the handler. */
13311 cp_parser_handler (parser);
13312 /* Peek at the next token. */
13313 token = cp_lexer_peek_token (parser->lexer);
13314 /* If it's not `catch' then there are no more handlers. */
13315 if (!cp_parser_is_keyword (token, RID_CATCH))
13320 /* Parse a handler.
13323 catch ( exception-declaration ) compound-statement */
13326 cp_parser_handler (cp_parser* parser)
13331 cp_parser_require_keyword (parser, RID_CATCH, "`catch'");
13332 handler = begin_handler ();
13333 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13334 declaration = cp_parser_exception_declaration (parser);
13335 finish_handler_parms (declaration, handler);
13336 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13337 cp_parser_compound_statement (parser, NULL, false);
13338 finish_handler (handler);
13341 /* Parse an exception-declaration.
13343 exception-declaration:
13344 type-specifier-seq declarator
13345 type-specifier-seq abstract-declarator
13349 Returns a VAR_DECL for the declaration, or NULL_TREE if the
13350 ellipsis variant is used. */
13353 cp_parser_exception_declaration (cp_parser* parser)
13355 tree type_specifiers;
13357 const char *saved_message;
13359 /* If it's an ellipsis, it's easy to handle. */
13360 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13362 /* Consume the `...' token. */
13363 cp_lexer_consume_token (parser->lexer);
13367 /* Types may not be defined in exception-declarations. */
13368 saved_message = parser->type_definition_forbidden_message;
13369 parser->type_definition_forbidden_message
13370 = "types may not be defined in exception-declarations";
13372 /* Parse the type-specifier-seq. */
13373 type_specifiers = cp_parser_type_specifier_seq (parser);
13374 /* If it's a `)', then there is no declarator. */
13375 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
13376 declarator = NULL_TREE;
13378 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
13379 /*ctor_dtor_or_conv_p=*/NULL,
13380 /*parenthesized_p=*/NULL);
13382 /* Restore the saved message. */
13383 parser->type_definition_forbidden_message = saved_message;
13385 return start_handler_parms (type_specifiers, declarator);
13388 /* Parse a throw-expression.
13391 throw assignment-expression [opt]
13393 Returns a THROW_EXPR representing the throw-expression. */
13396 cp_parser_throw_expression (cp_parser* parser)
13401 cp_parser_require_keyword (parser, RID_THROW, "`throw'");
13402 token = cp_lexer_peek_token (parser->lexer);
13403 /* Figure out whether or not there is an assignment-expression
13404 following the "throw" keyword. */
13405 if (token->type == CPP_COMMA
13406 || token->type == CPP_SEMICOLON
13407 || token->type == CPP_CLOSE_PAREN
13408 || token->type == CPP_CLOSE_SQUARE
13409 || token->type == CPP_CLOSE_BRACE
13410 || token->type == CPP_COLON)
13411 expression = NULL_TREE;
13413 expression = cp_parser_assignment_expression (parser);
13415 return build_throw (expression);
13418 /* GNU Extensions */
13420 /* Parse an (optional) asm-specification.
13423 asm ( string-literal )
13425 If the asm-specification is present, returns a STRING_CST
13426 corresponding to the string-literal. Otherwise, returns
13430 cp_parser_asm_specification_opt (cp_parser* parser)
13433 tree asm_specification;
13435 /* Peek at the next token. */
13436 token = cp_lexer_peek_token (parser->lexer);
13437 /* If the next token isn't the `asm' keyword, then there's no
13438 asm-specification. */
13439 if (!cp_parser_is_keyword (token, RID_ASM))
13442 /* Consume the `asm' token. */
13443 cp_lexer_consume_token (parser->lexer);
13444 /* Look for the `('. */
13445 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13447 /* Look for the string-literal. */
13448 token = cp_parser_require (parser, CPP_STRING, "string-literal");
13450 asm_specification = token->value;
13452 asm_specification = NULL_TREE;
13454 /* Look for the `)'. */
13455 cp_parser_require (parser, CPP_CLOSE_PAREN, "`('");
13457 return asm_specification;
13460 /* Parse an asm-operand-list.
13464 asm-operand-list , asm-operand
13467 string-literal ( expression )
13468 [ string-literal ] string-literal ( expression )
13470 Returns a TREE_LIST representing the operands. The TREE_VALUE of
13471 each node is the expression. The TREE_PURPOSE is itself a
13472 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
13473 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
13474 is a STRING_CST for the string literal before the parenthesis. */
13477 cp_parser_asm_operand_list (cp_parser* parser)
13479 tree asm_operands = NULL_TREE;
13483 tree string_literal;
13488 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
13490 /* Consume the `[' token. */
13491 cp_lexer_consume_token (parser->lexer);
13492 /* Read the operand name. */
13493 name = cp_parser_identifier (parser);
13494 if (name != error_mark_node)
13495 name = build_string (IDENTIFIER_LENGTH (name),
13496 IDENTIFIER_POINTER (name));
13497 /* Look for the closing `]'. */
13498 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
13502 /* Look for the string-literal. */
13503 token = cp_parser_require (parser, CPP_STRING, "string-literal");
13504 string_literal = token ? token->value : error_mark_node;
13505 c_lex_string_translate = 1;
13506 /* Look for the `('. */
13507 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13508 /* Parse the expression. */
13509 expression = cp_parser_expression (parser);
13510 /* Look for the `)'. */
13511 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13512 c_lex_string_translate = 0;
13513 /* Add this operand to the list. */
13514 asm_operands = tree_cons (build_tree_list (name, string_literal),
13517 /* If the next token is not a `,', there are no more
13519 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13521 /* Consume the `,'. */
13522 cp_lexer_consume_token (parser->lexer);
13525 return nreverse (asm_operands);
13528 /* Parse an asm-clobber-list.
13532 asm-clobber-list , string-literal
13534 Returns a TREE_LIST, indicating the clobbers in the order that they
13535 appeared. The TREE_VALUE of each node is a STRING_CST. */
13538 cp_parser_asm_clobber_list (cp_parser* parser)
13540 tree clobbers = NULL_TREE;
13545 tree string_literal;
13547 /* Look for the string literal. */
13548 token = cp_parser_require (parser, CPP_STRING, "string-literal");
13549 string_literal = token ? token->value : error_mark_node;
13550 /* Add it to the list. */
13551 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
13552 /* If the next token is not a `,', then the list is
13554 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13556 /* Consume the `,' token. */
13557 cp_lexer_consume_token (parser->lexer);
13563 /* Parse an (optional) series of attributes.
13566 attributes attribute
13569 __attribute__ (( attribute-list [opt] ))
13571 The return value is as for cp_parser_attribute_list. */
13574 cp_parser_attributes_opt (cp_parser* parser)
13576 tree attributes = NULL_TREE;
13581 tree attribute_list;
13583 /* Peek at the next token. */
13584 token = cp_lexer_peek_token (parser->lexer);
13585 /* If it's not `__attribute__', then we're done. */
13586 if (token->keyword != RID_ATTRIBUTE)
13589 /* Consume the `__attribute__' keyword. */
13590 cp_lexer_consume_token (parser->lexer);
13591 /* Look for the two `(' tokens. */
13592 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13593 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13595 /* Peek at the next token. */
13596 token = cp_lexer_peek_token (parser->lexer);
13597 if (token->type != CPP_CLOSE_PAREN)
13598 /* Parse the attribute-list. */
13599 attribute_list = cp_parser_attribute_list (parser);
13601 /* If the next token is a `)', then there is no attribute
13603 attribute_list = NULL;
13605 /* Look for the two `)' tokens. */
13606 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13607 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13609 /* Add these new attributes to the list. */
13610 attributes = chainon (attributes, attribute_list);
13616 /* Parse an attribute-list.
13620 attribute-list , attribute
13624 identifier ( identifier )
13625 identifier ( identifier , expression-list )
13626 identifier ( expression-list )
13628 Returns a TREE_LIST. Each node corresponds to an attribute. THe
13629 TREE_PURPOSE of each node is the identifier indicating which
13630 attribute is in use. The TREE_VALUE represents the arguments, if
13634 cp_parser_attribute_list (cp_parser* parser)
13636 tree attribute_list = NULL_TREE;
13638 c_lex_string_translate = 0;
13645 /* Look for the identifier. We also allow keywords here; for
13646 example `__attribute__ ((const))' is legal. */
13647 token = cp_lexer_peek_token (parser->lexer);
13648 if (token->type != CPP_NAME
13649 && token->type != CPP_KEYWORD)
13650 return error_mark_node;
13651 /* Consume the token. */
13652 token = cp_lexer_consume_token (parser->lexer);
13654 /* Save away the identifier that indicates which attribute this is. */
13655 identifier = token->value;
13656 attribute = build_tree_list (identifier, NULL_TREE);
13658 /* Peek at the next token. */
13659 token = cp_lexer_peek_token (parser->lexer);
13660 /* If it's an `(', then parse the attribute arguments. */
13661 if (token->type == CPP_OPEN_PAREN)
13665 arguments = (cp_parser_parenthesized_expression_list
13666 (parser, true, /*non_constant_p=*/NULL));
13667 /* Save the identifier and arguments away. */
13668 TREE_VALUE (attribute) = arguments;
13671 /* Add this attribute to the list. */
13672 TREE_CHAIN (attribute) = attribute_list;
13673 attribute_list = attribute;
13675 /* Now, look for more attributes. */
13676 token = cp_lexer_peek_token (parser->lexer);
13677 /* If the next token isn't a `,', we're done. */
13678 if (token->type != CPP_COMMA)
13681 /* Consume the comma and keep going. */
13682 cp_lexer_consume_token (parser->lexer);
13684 c_lex_string_translate = 1;
13686 /* We built up the list in reverse order. */
13687 return nreverse (attribute_list);
13690 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
13691 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
13692 current value of the PEDANTIC flag, regardless of whether or not
13693 the `__extension__' keyword is present. The caller is responsible
13694 for restoring the value of the PEDANTIC flag. */
13697 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
13699 /* Save the old value of the PEDANTIC flag. */
13700 *saved_pedantic = pedantic;
13702 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
13704 /* Consume the `__extension__' token. */
13705 cp_lexer_consume_token (parser->lexer);
13706 /* We're not being pedantic while the `__extension__' keyword is
13716 /* Parse a label declaration.
13719 __label__ label-declarator-seq ;
13721 label-declarator-seq:
13722 identifier , label-declarator-seq
13726 cp_parser_label_declaration (cp_parser* parser)
13728 /* Look for the `__label__' keyword. */
13729 cp_parser_require_keyword (parser, RID_LABEL, "`__label__'");
13735 /* Look for an identifier. */
13736 identifier = cp_parser_identifier (parser);
13737 /* Declare it as a lobel. */
13738 finish_label_decl (identifier);
13739 /* If the next token is a `;', stop. */
13740 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13742 /* Look for the `,' separating the label declarations. */
13743 cp_parser_require (parser, CPP_COMMA, "`,'");
13746 /* Look for the final `;'. */
13747 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
13750 /* Support Functions */
13752 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
13753 NAME should have one of the representations used for an
13754 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
13755 is returned. If PARSER->SCOPE is a dependent type, then a
13756 SCOPE_REF is returned.
13758 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
13759 returned; the name was already resolved when the TEMPLATE_ID_EXPR
13760 was formed. Abstractly, such entities should not be passed to this
13761 function, because they do not need to be looked up, but it is
13762 simpler to check for this special case here, rather than at the
13765 In cases not explicitly covered above, this function returns a
13766 DECL, OVERLOAD, or baselink representing the result of the lookup.
13767 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
13770 If IS_TYPE is TRUE, bindings that do not refer to types are
13773 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
13776 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
13779 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
13783 cp_parser_lookup_name (cp_parser *parser, tree name,
13784 bool is_type, bool is_template, bool is_namespace,
13785 bool check_dependency)
13788 tree object_type = parser->context->object_type;
13790 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
13791 no longer valid. Note that if we are parsing tentatively, and
13792 the parse fails, OBJECT_TYPE will be automatically restored. */
13793 parser->context->object_type = NULL_TREE;
13795 if (name == error_mark_node)
13796 return error_mark_node;
13798 /* A template-id has already been resolved; there is no lookup to
13800 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
13802 if (BASELINK_P (name))
13804 my_friendly_assert ((TREE_CODE (BASELINK_FUNCTIONS (name))
13805 == TEMPLATE_ID_EXPR),
13810 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
13811 it should already have been checked to make sure that the name
13812 used matches the type being destroyed. */
13813 if (TREE_CODE (name) == BIT_NOT_EXPR)
13817 /* Figure out to which type this destructor applies. */
13819 type = parser->scope;
13820 else if (object_type)
13821 type = object_type;
13823 type = current_class_type;
13824 /* If that's not a class type, there is no destructor. */
13825 if (!type || !CLASS_TYPE_P (type))
13826 return error_mark_node;
13827 if (!CLASSTYPE_DESTRUCTORS (type))
13828 return error_mark_node;
13829 /* If it was a class type, return the destructor. */
13830 return CLASSTYPE_DESTRUCTORS (type);
13833 /* By this point, the NAME should be an ordinary identifier. If
13834 the id-expression was a qualified name, the qualifying scope is
13835 stored in PARSER->SCOPE at this point. */
13836 my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE,
13839 /* Perform the lookup. */
13844 if (parser->scope == error_mark_node)
13845 return error_mark_node;
13847 /* If the SCOPE is dependent, the lookup must be deferred until
13848 the template is instantiated -- unless we are explicitly
13849 looking up names in uninstantiated templates. Even then, we
13850 cannot look up the name if the scope is not a class type; it
13851 might, for example, be a template type parameter. */
13852 dependent_p = (TYPE_P (parser->scope)
13853 && !(parser->in_declarator_p
13854 && currently_open_class (parser->scope))
13855 && dependent_type_p (parser->scope));
13856 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
13860 /* The resolution to Core Issue 180 says that `struct A::B'
13861 should be considered a type-name, even if `A' is
13863 decl = TYPE_NAME (make_typename_type (parser->scope,
13866 else if (is_template)
13867 decl = make_unbound_class_template (parser->scope,
13871 decl = build_nt (SCOPE_REF, parser->scope, name);
13875 bool pop_p = false;
13877 /* If PARSER->SCOPE is a dependent type, then it must be a
13878 class type, and we must not be checking dependencies;
13879 otherwise, we would have processed this lookup above. So
13880 that PARSER->SCOPE is not considered a dependent base by
13881 lookup_member, we must enter the scope here. */
13883 pop_p = push_scope (parser->scope);
13884 /* If the PARSER->SCOPE is a a template specialization, it
13885 may be instantiated during name lookup. In that case,
13886 errors may be issued. Even if we rollback the current
13887 tentative parse, those errors are valid. */
13888 decl = lookup_qualified_name (parser->scope, name, is_type,
13889 /*complain=*/true);
13891 pop_scope (parser->scope);
13893 parser->qualifying_scope = parser->scope;
13894 parser->object_scope = NULL_TREE;
13896 else if (object_type)
13898 tree object_decl = NULL_TREE;
13899 /* Look up the name in the scope of the OBJECT_TYPE, unless the
13900 OBJECT_TYPE is not a class. */
13901 if (CLASS_TYPE_P (object_type))
13902 /* If the OBJECT_TYPE is a template specialization, it may
13903 be instantiated during name lookup. In that case, errors
13904 may be issued. Even if we rollback the current tentative
13905 parse, those errors are valid. */
13906 object_decl = lookup_member (object_type,
13908 /*protect=*/0, is_type);
13909 /* Look it up in the enclosing context, too. */
13910 decl = lookup_name_real (name, is_type, /*nonclass=*/0,
13913 parser->object_scope = object_type;
13914 parser->qualifying_scope = NULL_TREE;
13916 decl = object_decl;
13920 decl = lookup_name_real (name, is_type, /*nonclass=*/0,
13923 parser->qualifying_scope = NULL_TREE;
13924 parser->object_scope = NULL_TREE;
13927 /* If the lookup failed, let our caller know. */
13929 || decl == error_mark_node
13930 || (TREE_CODE (decl) == FUNCTION_DECL
13931 && DECL_ANTICIPATED (decl)))
13932 return error_mark_node;
13934 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
13935 if (TREE_CODE (decl) == TREE_LIST)
13937 /* The error message we have to print is too complicated for
13938 cp_parser_error, so we incorporate its actions directly. */
13939 if (!cp_parser_simulate_error (parser))
13941 error ("reference to `%D' is ambiguous", name);
13942 print_candidates (decl);
13944 return error_mark_node;
13947 my_friendly_assert (DECL_P (decl)
13948 || TREE_CODE (decl) == OVERLOAD
13949 || TREE_CODE (decl) == SCOPE_REF
13950 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
13951 || BASELINK_P (decl),
13954 /* If we have resolved the name of a member declaration, check to
13955 see if the declaration is accessible. When the name resolves to
13956 set of overloaded functions, accessibility is checked when
13957 overload resolution is done.
13959 During an explicit instantiation, access is not checked at all,
13960 as per [temp.explicit]. */
13962 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
13967 /* Like cp_parser_lookup_name, but for use in the typical case where
13968 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
13969 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
13972 cp_parser_lookup_name_simple (cp_parser* parser, tree name)
13974 return cp_parser_lookup_name (parser, name,
13976 /*is_template=*/false,
13977 /*is_namespace=*/false,
13978 /*check_dependency=*/true);
13981 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
13982 the current context, return the TYPE_DECL. If TAG_NAME_P is
13983 true, the DECL indicates the class being defined in a class-head,
13984 or declared in an elaborated-type-specifier.
13986 Otherwise, return DECL. */
13989 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
13991 /* If the TEMPLATE_DECL is being declared as part of a class-head,
13992 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
13995 template <typename T> struct B;
13998 template <typename T> struct A::B {};
14000 Similarly, in a elaborated-type-specifier:
14002 namespace N { struct X{}; }
14005 template <typename T> friend struct N::X;
14008 However, if the DECL refers to a class type, and we are in
14009 the scope of the class, then the name lookup automatically
14010 finds the TYPE_DECL created by build_self_reference rather
14011 than a TEMPLATE_DECL. For example, in:
14013 template <class T> struct S {
14017 there is no need to handle such case. */
14019 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
14020 return DECL_TEMPLATE_RESULT (decl);
14025 /* If too many, or too few, template-parameter lists apply to the
14026 declarator, issue an error message. Returns TRUE if all went well,
14027 and FALSE otherwise. */
14030 cp_parser_check_declarator_template_parameters (cp_parser* parser,
14033 unsigned num_templates;
14035 /* We haven't seen any classes that involve template parameters yet. */
14038 switch (TREE_CODE (declarator))
14045 tree main_declarator = TREE_OPERAND (declarator, 0);
14047 cp_parser_check_declarator_template_parameters (parser,
14056 scope = TREE_OPERAND (declarator, 0);
14057 member = TREE_OPERAND (declarator, 1);
14059 /* If this is a pointer-to-member, then we are not interested
14060 in the SCOPE, because it does not qualify the thing that is
14062 if (TREE_CODE (member) == INDIRECT_REF)
14063 return (cp_parser_check_declarator_template_parameters
14066 while (scope && CLASS_TYPE_P (scope))
14068 /* You're supposed to have one `template <...>'
14069 for every template class, but you don't need one
14070 for a full specialization. For example:
14072 template <class T> struct S{};
14073 template <> struct S<int> { void f(); };
14074 void S<int>::f () {}
14076 is correct; there shouldn't be a `template <>' for
14077 the definition of `S<int>::f'. */
14078 if (CLASSTYPE_TEMPLATE_INFO (scope)
14079 && (CLASSTYPE_TEMPLATE_INSTANTIATION (scope)
14080 || uses_template_parms (CLASSTYPE_TI_ARGS (scope)))
14081 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
14084 scope = TYPE_CONTEXT (scope);
14088 /* Fall through. */
14091 /* If the DECLARATOR has the form `X<y>' then it uses one
14092 additional level of template parameters. */
14093 if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
14096 return cp_parser_check_template_parameters (parser,
14101 /* NUM_TEMPLATES were used in the current declaration. If that is
14102 invalid, return FALSE and issue an error messages. Otherwise,
14106 cp_parser_check_template_parameters (cp_parser* parser,
14107 unsigned num_templates)
14109 /* If there are more template classes than parameter lists, we have
14112 template <class T> void S<T>::R<T>::f (); */
14113 if (parser->num_template_parameter_lists < num_templates)
14115 error ("too few template-parameter-lists");
14118 /* If there are the same number of template classes and parameter
14119 lists, that's OK. */
14120 if (parser->num_template_parameter_lists == num_templates)
14122 /* If there are more, but only one more, then we are referring to a
14123 member template. That's OK too. */
14124 if (parser->num_template_parameter_lists == num_templates + 1)
14126 /* Otherwise, there are too many template parameter lists. We have
14129 template <class T> template <class U> void S::f(); */
14130 error ("too many template-parameter-lists");
14134 /* Parse a binary-expression of the general form:
14138 binary-expression <token> <expr>
14140 The TOKEN_TREE_MAP maps <token> types to <expr> codes. FN is used
14141 to parser the <expr>s. If the first production is used, then the
14142 value returned by FN is returned directly. Otherwise, a node with
14143 the indicated EXPR_TYPE is returned, with operands corresponding to
14144 the two sub-expressions. */
14147 cp_parser_binary_expression (cp_parser* parser,
14148 const cp_parser_token_tree_map token_tree_map,
14149 cp_parser_expression_fn fn)
14153 /* Parse the first expression. */
14154 lhs = (*fn) (parser);
14155 /* Now, look for more expressions. */
14159 const cp_parser_token_tree_map_node *map_node;
14162 /* Peek at the next token. */
14163 token = cp_lexer_peek_token (parser->lexer);
14164 /* If the token is `>', and that's not an operator at the
14165 moment, then we're done. */
14166 if (token->type == CPP_GREATER
14167 && !parser->greater_than_is_operator_p)
14169 /* If we find one of the tokens we want, build the corresponding
14170 tree representation. */
14171 for (map_node = token_tree_map;
14172 map_node->token_type != CPP_EOF;
14174 if (map_node->token_type == token->type)
14176 /* Assume that an overloaded operator will not be used. */
14177 bool overloaded_p = false;
14179 /* Consume the operator token. */
14180 cp_lexer_consume_token (parser->lexer);
14181 /* Parse the right-hand side of the expression. */
14182 rhs = (*fn) (parser);
14183 /* Build the binary tree node. */
14184 lhs = build_x_binary_op (map_node->tree_type, lhs, rhs,
14186 /* If the binary operator required the use of an
14187 overloaded operator, then this expression cannot be an
14188 integral constant-expression. An overloaded operator
14189 can be used even if both operands are otherwise
14190 permissible in an integral constant-expression if at
14191 least one of the operands is of enumeration type. */
14193 && (cp_parser_non_integral_constant_expression
14194 (parser, "calls to overloaded operators")))
14195 lhs = error_mark_node;
14199 /* If the token wasn't one of the ones we want, we're done. */
14200 if (map_node->token_type == CPP_EOF)
14207 /* Parse an optional `::' token indicating that the following name is
14208 from the global namespace. If so, PARSER->SCOPE is set to the
14209 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
14210 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
14211 Returns the new value of PARSER->SCOPE, if the `::' token is
14212 present, and NULL_TREE otherwise. */
14215 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
14219 /* Peek at the next token. */
14220 token = cp_lexer_peek_token (parser->lexer);
14221 /* If we're looking at a `::' token then we're starting from the
14222 global namespace, not our current location. */
14223 if (token->type == CPP_SCOPE)
14225 /* Consume the `::' token. */
14226 cp_lexer_consume_token (parser->lexer);
14227 /* Set the SCOPE so that we know where to start the lookup. */
14228 parser->scope = global_namespace;
14229 parser->qualifying_scope = global_namespace;
14230 parser->object_scope = NULL_TREE;
14232 return parser->scope;
14234 else if (!current_scope_valid_p)
14236 parser->scope = NULL_TREE;
14237 parser->qualifying_scope = NULL_TREE;
14238 parser->object_scope = NULL_TREE;
14244 /* Returns TRUE if the upcoming token sequence is the start of a
14245 constructor declarator. If FRIEND_P is true, the declarator is
14246 preceded by the `friend' specifier. */
14249 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
14251 bool constructor_p;
14252 tree type_decl = NULL_TREE;
14253 bool nested_name_p;
14254 cp_token *next_token;
14256 /* The common case is that this is not a constructor declarator, so
14257 try to avoid doing lots of work if at all possible. It's not
14258 valid declare a constructor at function scope. */
14259 if (at_function_scope_p ())
14261 /* And only certain tokens can begin a constructor declarator. */
14262 next_token = cp_lexer_peek_token (parser->lexer);
14263 if (next_token->type != CPP_NAME
14264 && next_token->type != CPP_SCOPE
14265 && next_token->type != CPP_NESTED_NAME_SPECIFIER
14266 && next_token->type != CPP_TEMPLATE_ID)
14269 /* Parse tentatively; we are going to roll back all of the tokens
14271 cp_parser_parse_tentatively (parser);
14272 /* Assume that we are looking at a constructor declarator. */
14273 constructor_p = true;
14275 /* Look for the optional `::' operator. */
14276 cp_parser_global_scope_opt (parser,
14277 /*current_scope_valid_p=*/false);
14278 /* Look for the nested-name-specifier. */
14280 = (cp_parser_nested_name_specifier_opt (parser,
14281 /*typename_keyword_p=*/false,
14282 /*check_dependency_p=*/false,
14284 /*is_declaration=*/false)
14286 /* Outside of a class-specifier, there must be a
14287 nested-name-specifier. */
14288 if (!nested_name_p &&
14289 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
14291 constructor_p = false;
14292 /* If we still think that this might be a constructor-declarator,
14293 look for a class-name. */
14298 template <typename T> struct S { S(); };
14299 template <typename T> S<T>::S ();
14301 we must recognize that the nested `S' names a class.
14304 template <typename T> S<T>::S<T> ();
14306 we must recognize that the nested `S' names a template. */
14307 type_decl = cp_parser_class_name (parser,
14308 /*typename_keyword_p=*/false,
14309 /*template_keyword_p=*/false,
14311 /*check_dependency_p=*/false,
14312 /*class_head_p=*/false,
14313 /*is_declaration=*/false);
14314 /* If there was no class-name, then this is not a constructor. */
14315 constructor_p = !cp_parser_error_occurred (parser);
14318 /* If we're still considering a constructor, we have to see a `(',
14319 to begin the parameter-declaration-clause, followed by either a
14320 `)', an `...', or a decl-specifier. We need to check for a
14321 type-specifier to avoid being fooled into thinking that:
14325 is a constructor. (It is actually a function named `f' that
14326 takes one parameter (of type `int') and returns a value of type
14329 && cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
14331 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
14332 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
14333 /* A parameter declaration begins with a decl-specifier,
14334 which is either the "attribute" keyword, a storage class
14335 specifier, or (usually) a type-specifier. */
14336 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE)
14337 && !cp_parser_storage_class_specifier_opt (parser))
14340 bool pop_p = false;
14341 unsigned saved_num_template_parameter_lists;
14343 /* Names appearing in the type-specifier should be looked up
14344 in the scope of the class. */
14345 if (current_class_type)
14349 type = TREE_TYPE (type_decl);
14350 if (TREE_CODE (type) == TYPENAME_TYPE)
14352 type = resolve_typename_type (type,
14353 /*only_current_p=*/false);
14354 if (type == error_mark_node)
14356 cp_parser_abort_tentative_parse (parser);
14360 pop_p = push_scope (type);
14363 /* Inside the constructor parameter list, surrounding
14364 template-parameter-lists do not apply. */
14365 saved_num_template_parameter_lists
14366 = parser->num_template_parameter_lists;
14367 parser->num_template_parameter_lists = 0;
14369 /* Look for the type-specifier. */
14370 cp_parser_type_specifier (parser,
14371 CP_PARSER_FLAGS_NONE,
14372 /*is_friend=*/false,
14373 /*is_declarator=*/true,
14374 /*declares_class_or_enum=*/NULL,
14375 /*is_cv_qualifier=*/NULL);
14377 parser->num_template_parameter_lists
14378 = saved_num_template_parameter_lists;
14380 /* Leave the scope of the class. */
14384 constructor_p = !cp_parser_error_occurred (parser);
14388 constructor_p = false;
14389 /* We did not really want to consume any tokens. */
14390 cp_parser_abort_tentative_parse (parser);
14392 return constructor_p;
14395 /* Parse the definition of the function given by the DECL_SPECIFIERS,
14396 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
14397 they must be performed once we are in the scope of the function.
14399 Returns the function defined. */
14402 cp_parser_function_definition_from_specifiers_and_declarator
14403 (cp_parser* parser,
14404 tree decl_specifiers,
14411 /* Begin the function-definition. */
14412 success_p = begin_function_definition (decl_specifiers,
14416 /* If there were names looked up in the decl-specifier-seq that we
14417 did not check, check them now. We must wait until we are in the
14418 scope of the function to perform the checks, since the function
14419 might be a friend. */
14420 perform_deferred_access_checks ();
14424 /* If begin_function_definition didn't like the definition, skip
14425 the entire function. */
14426 error ("invalid function declaration");
14427 cp_parser_skip_to_end_of_block_or_statement (parser);
14428 fn = error_mark_node;
14431 fn = cp_parser_function_definition_after_declarator (parser,
14432 /*inline_p=*/false);
14437 /* Parse the part of a function-definition that follows the
14438 declarator. INLINE_P is TRUE iff this function is an inline
14439 function defined with a class-specifier.
14441 Returns the function defined. */
14444 cp_parser_function_definition_after_declarator (cp_parser* parser,
14448 bool ctor_initializer_p = false;
14449 bool saved_in_unbraced_linkage_specification_p;
14450 unsigned saved_num_template_parameter_lists;
14452 /* If the next token is `return', then the code may be trying to
14453 make use of the "named return value" extension that G++ used to
14455 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
14457 /* Consume the `return' keyword. */
14458 cp_lexer_consume_token (parser->lexer);
14459 /* Look for the identifier that indicates what value is to be
14461 cp_parser_identifier (parser);
14462 /* Issue an error message. */
14463 error ("named return values are no longer supported");
14464 /* Skip tokens until we reach the start of the function body. */
14465 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
14466 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
14467 cp_lexer_consume_token (parser->lexer);
14469 /* The `extern' in `extern "C" void f () { ... }' does not apply to
14470 anything declared inside `f'. */
14471 saved_in_unbraced_linkage_specification_p
14472 = parser->in_unbraced_linkage_specification_p;
14473 parser->in_unbraced_linkage_specification_p = false;
14474 /* Inside the function, surrounding template-parameter-lists do not
14476 saved_num_template_parameter_lists
14477 = parser->num_template_parameter_lists;
14478 parser->num_template_parameter_lists = 0;
14479 /* If the next token is `try', then we are looking at a
14480 function-try-block. */
14481 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
14482 ctor_initializer_p = cp_parser_function_try_block (parser);
14483 /* A function-try-block includes the function-body, so we only do
14484 this next part if we're not processing a function-try-block. */
14487 = cp_parser_ctor_initializer_opt_and_function_body (parser);
14489 /* Finish the function. */
14490 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
14491 (inline_p ? 2 : 0));
14492 /* Generate code for it, if necessary. */
14493 expand_or_defer_fn (fn);
14494 /* Restore the saved values. */
14495 parser->in_unbraced_linkage_specification_p
14496 = saved_in_unbraced_linkage_specification_p;
14497 parser->num_template_parameter_lists
14498 = saved_num_template_parameter_lists;
14503 /* Parse a template-declaration, assuming that the `export' (and
14504 `extern') keywords, if present, has already been scanned. MEMBER_P
14505 is as for cp_parser_template_declaration. */
14508 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
14510 tree decl = NULL_TREE;
14511 tree parameter_list;
14512 bool friend_p = false;
14514 /* Look for the `template' keyword. */
14515 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'"))
14519 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
14522 /* If the next token is `>', then we have an invalid
14523 specialization. Rather than complain about an invalid template
14524 parameter, issue an error message here. */
14525 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
14527 cp_parser_error (parser, "invalid explicit specialization");
14528 begin_specialization ();
14529 parameter_list = NULL_TREE;
14533 /* Parse the template parameters. */
14534 begin_template_parm_list ();
14535 parameter_list = cp_parser_template_parameter_list (parser);
14536 parameter_list = end_template_parm_list (parameter_list);
14539 /* Look for the `>'. */
14540 cp_parser_skip_until_found (parser, CPP_GREATER, "`>'");
14541 /* We just processed one more parameter list. */
14542 ++parser->num_template_parameter_lists;
14543 /* If the next token is `template', there are more template
14545 if (cp_lexer_next_token_is_keyword (parser->lexer,
14547 cp_parser_template_declaration_after_export (parser, member_p);
14550 decl = cp_parser_single_declaration (parser,
14554 /* If this is a member template declaration, let the front
14556 if (member_p && !friend_p && decl)
14558 if (TREE_CODE (decl) == TYPE_DECL)
14559 cp_parser_check_access_in_redeclaration (decl);
14561 decl = finish_member_template_decl (decl);
14563 else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
14564 make_friend_class (current_class_type, TREE_TYPE (decl),
14565 /*complain=*/true);
14567 /* We are done with the current parameter list. */
14568 --parser->num_template_parameter_lists;
14571 finish_template_decl (parameter_list);
14573 /* Register member declarations. */
14574 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
14575 finish_member_declaration (decl);
14577 /* If DECL is a function template, we must return to parse it later.
14578 (Even though there is no definition, there might be default
14579 arguments that need handling.) */
14580 if (member_p && decl
14581 && (TREE_CODE (decl) == FUNCTION_DECL
14582 || DECL_FUNCTION_TEMPLATE_P (decl)))
14583 TREE_VALUE (parser->unparsed_functions_queues)
14584 = tree_cons (NULL_TREE, decl,
14585 TREE_VALUE (parser->unparsed_functions_queues));
14588 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
14589 `function-definition' sequence. MEMBER_P is true, this declaration
14590 appears in a class scope.
14592 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
14593 *FRIEND_P is set to TRUE iff the declaration is a friend. */
14596 cp_parser_single_declaration (cp_parser* parser,
14600 int declares_class_or_enum;
14601 tree decl = NULL_TREE;
14602 tree decl_specifiers;
14604 bool function_definition_p = false;
14606 /* Defer access checks until we know what is being declared. */
14607 push_deferring_access_checks (dk_deferred);
14609 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
14612 = cp_parser_decl_specifier_seq (parser,
14613 CP_PARSER_FLAGS_OPTIONAL,
14615 &declares_class_or_enum);
14617 *friend_p = cp_parser_friend_p (decl_specifiers);
14618 /* Gather up the access checks that occurred the
14619 decl-specifier-seq. */
14620 stop_deferring_access_checks ();
14622 /* Check for the declaration of a template class. */
14623 if (declares_class_or_enum)
14625 if (cp_parser_declares_only_class_p (parser))
14627 decl = shadow_tag (decl_specifiers);
14629 decl = TYPE_NAME (decl);
14631 decl = error_mark_node;
14636 /* If it's not a template class, try for a template function. If
14637 the next token is a `;', then this declaration does not declare
14638 anything. But, if there were errors in the decl-specifiers, then
14639 the error might well have come from an attempted class-specifier.
14640 In that case, there's no need to warn about a missing declarator. */
14642 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
14643 || !value_member (error_mark_node, decl_specifiers)))
14644 decl = cp_parser_init_declarator (parser,
14647 /*function_definition_allowed_p=*/true,
14649 declares_class_or_enum,
14650 &function_definition_p);
14652 pop_deferring_access_checks ();
14654 /* Clear any current qualification; whatever comes next is the start
14655 of something new. */
14656 parser->scope = NULL_TREE;
14657 parser->qualifying_scope = NULL_TREE;
14658 parser->object_scope = NULL_TREE;
14659 /* Look for a trailing `;' after the declaration. */
14660 if (!function_definition_p
14661 && !cp_parser_require (parser, CPP_SEMICOLON, "`;'"))
14662 cp_parser_skip_to_end_of_block_or_statement (parser);
14667 /* Parse a cast-expression that is not the operand of a unary "&". */
14670 cp_parser_simple_cast_expression (cp_parser *parser)
14672 return cp_parser_cast_expression (parser, /*address_p=*/false);
14675 /* Parse a functional cast to TYPE. Returns an expression
14676 representing the cast. */
14679 cp_parser_functional_cast (cp_parser* parser, tree type)
14681 tree expression_list;
14685 = cp_parser_parenthesized_expression_list (parser, false,
14686 /*non_constant_p=*/NULL);
14688 cast = build_functional_cast (type, expression_list);
14689 /* [expr.const]/1: In an integral constant expression "only type
14690 conversions to integral or enumeration type can be used". */
14691 if (cast != error_mark_node && !type_dependent_expression_p (type)
14692 && !INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (type)))
14694 if (cp_parser_non_integral_constant_expression
14695 (parser, "a call to a constructor"))
14696 return error_mark_node;
14701 /* Save the tokens that make up the body of a member function defined
14702 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
14703 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
14704 specifiers applied to the declaration. Returns the FUNCTION_DECL
14705 for the member function. */
14708 cp_parser_save_member_function_body (cp_parser* parser,
14709 tree decl_specifiers,
14713 cp_token_cache *cache;
14716 /* Create the function-declaration. */
14717 fn = start_method (decl_specifiers, declarator, attributes);
14718 /* If something went badly wrong, bail out now. */
14719 if (fn == error_mark_node)
14721 /* If there's a function-body, skip it. */
14722 if (cp_parser_token_starts_function_definition_p
14723 (cp_lexer_peek_token (parser->lexer)))
14724 cp_parser_skip_to_end_of_block_or_statement (parser);
14725 return error_mark_node;
14728 /* Remember it, if there default args to post process. */
14729 cp_parser_save_default_args (parser, fn);
14731 /* Create a token cache. */
14732 cache = cp_token_cache_new ();
14733 /* Save away the tokens that make up the body of the
14735 cp_parser_cache_group (parser, cache, CPP_CLOSE_BRACE, /*depth=*/0);
14736 /* Handle function try blocks. */
14737 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
14738 cp_parser_cache_group (parser, cache, CPP_CLOSE_BRACE, /*depth=*/0);
14740 /* Save away the inline definition; we will process it when the
14741 class is complete. */
14742 DECL_PENDING_INLINE_INFO (fn) = cache;
14743 DECL_PENDING_INLINE_P (fn) = 1;
14745 /* We need to know that this was defined in the class, so that
14746 friend templates are handled correctly. */
14747 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
14749 /* We're done with the inline definition. */
14750 finish_method (fn);
14752 /* Add FN to the queue of functions to be parsed later. */
14753 TREE_VALUE (parser->unparsed_functions_queues)
14754 = tree_cons (NULL_TREE, fn,
14755 TREE_VALUE (parser->unparsed_functions_queues));
14760 /* Parse a template-argument-list, as well as the trailing ">" (but
14761 not the opening ">"). See cp_parser_template_argument_list for the
14765 cp_parser_enclosed_template_argument_list (cp_parser* parser)
14769 tree saved_qualifying_scope;
14770 tree saved_object_scope;
14771 bool saved_greater_than_is_operator_p;
14775 When parsing a template-id, the first non-nested `>' is taken as
14776 the end of the template-argument-list rather than a greater-than
14778 saved_greater_than_is_operator_p
14779 = parser->greater_than_is_operator_p;
14780 parser->greater_than_is_operator_p = false;
14781 /* Parsing the argument list may modify SCOPE, so we save it
14783 saved_scope = parser->scope;
14784 saved_qualifying_scope = parser->qualifying_scope;
14785 saved_object_scope = parser->object_scope;
14786 /* Parse the template-argument-list itself. */
14787 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
14788 arguments = NULL_TREE;
14790 arguments = cp_parser_template_argument_list (parser);
14791 /* Look for the `>' that ends the template-argument-list. If we find
14792 a '>>' instead, it's probably just a typo. */
14793 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
14795 if (!saved_greater_than_is_operator_p)
14797 /* If we're in a nested template argument list, the '>>' has to be
14798 a typo for '> >'. We emit the error message, but we continue
14799 parsing and we push a '>' as next token, so that the argument
14800 list will be parsed correctly.. */
14802 error ("`>>' should be `> >' within a nested template argument list");
14803 token = cp_lexer_peek_token (parser->lexer);
14804 token->type = CPP_GREATER;
14808 /* If this is not a nested template argument list, the '>>' is
14809 a typo for '>'. Emit an error message and continue. */
14810 error ("spurious `>>', use `>' to terminate a template argument list");
14811 cp_lexer_consume_token (parser->lexer);
14814 else if (!cp_parser_require (parser, CPP_GREATER, "`>'"))
14815 error ("missing `>' to terminate the template argument list");
14816 /* The `>' token might be a greater-than operator again now. */
14817 parser->greater_than_is_operator_p
14818 = saved_greater_than_is_operator_p;
14819 /* Restore the SAVED_SCOPE. */
14820 parser->scope = saved_scope;
14821 parser->qualifying_scope = saved_qualifying_scope;
14822 parser->object_scope = saved_object_scope;
14827 /* MEMBER_FUNCTION is a member function, or a friend. If default
14828 arguments, or the body of the function have not yet been parsed,
14832 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
14834 cp_lexer *saved_lexer;
14836 /* If this member is a template, get the underlying
14838 if (DECL_FUNCTION_TEMPLATE_P (member_function))
14839 member_function = DECL_TEMPLATE_RESULT (member_function);
14841 /* There should not be any class definitions in progress at this
14842 point; the bodies of members are only parsed outside of all class
14844 my_friendly_assert (parser->num_classes_being_defined == 0, 20010816);
14845 /* While we're parsing the member functions we might encounter more
14846 classes. We want to handle them right away, but we don't want
14847 them getting mixed up with functions that are currently in the
14849 parser->unparsed_functions_queues
14850 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
14852 /* Make sure that any template parameters are in scope. */
14853 maybe_begin_member_template_processing (member_function);
14855 /* If the body of the function has not yet been parsed, parse it
14857 if (DECL_PENDING_INLINE_P (member_function))
14859 tree function_scope;
14860 cp_token_cache *tokens;
14862 /* The function is no longer pending; we are processing it. */
14863 tokens = DECL_PENDING_INLINE_INFO (member_function);
14864 DECL_PENDING_INLINE_INFO (member_function) = NULL;
14865 DECL_PENDING_INLINE_P (member_function) = 0;
14866 /* If this was an inline function in a local class, enter the scope
14867 of the containing function. */
14868 function_scope = decl_function_context (member_function);
14869 if (function_scope)
14870 push_function_context_to (function_scope);
14872 /* Save away the current lexer. */
14873 saved_lexer = parser->lexer;
14874 /* Make a new lexer to feed us the tokens saved for this function. */
14875 parser->lexer = cp_lexer_new_from_tokens (tokens);
14876 parser->lexer->next = saved_lexer;
14878 /* Set the current source position to be the location of the first
14879 token in the saved inline body. */
14880 cp_lexer_peek_token (parser->lexer);
14882 /* Let the front end know that we going to be defining this
14884 start_function (NULL_TREE, member_function, NULL_TREE,
14885 SF_PRE_PARSED | SF_INCLASS_INLINE);
14887 /* Now, parse the body of the function. */
14888 cp_parser_function_definition_after_declarator (parser,
14889 /*inline_p=*/true);
14891 /* Leave the scope of the containing function. */
14892 if (function_scope)
14893 pop_function_context_from (function_scope);
14894 /* Restore the lexer. */
14895 parser->lexer = saved_lexer;
14898 /* Remove any template parameters from the symbol table. */
14899 maybe_end_member_template_processing ();
14901 /* Restore the queue. */
14902 parser->unparsed_functions_queues
14903 = TREE_CHAIN (parser->unparsed_functions_queues);
14906 /* If DECL contains any default args, remember it on the unparsed
14907 functions queue. */
14910 cp_parser_save_default_args (cp_parser* parser, tree decl)
14914 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
14916 probe = TREE_CHAIN (probe))
14917 if (TREE_PURPOSE (probe))
14919 TREE_PURPOSE (parser->unparsed_functions_queues)
14920 = tree_cons (NULL_TREE, decl,
14921 TREE_PURPOSE (parser->unparsed_functions_queues));
14927 /* FN is a FUNCTION_DECL which may contains a parameter with an
14928 unparsed DEFAULT_ARG. Parse the default args now. */
14931 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
14933 cp_lexer *saved_lexer;
14934 cp_token_cache *tokens;
14935 bool saved_local_variables_forbidden_p;
14938 /* While we're parsing the default args, we might (due to the
14939 statement expression extension) encounter more classes. We want
14940 to handle them right away, but we don't want them getting mixed
14941 up with default args that are currently in the queue. */
14942 parser->unparsed_functions_queues
14943 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
14945 for (parameters = TYPE_ARG_TYPES (TREE_TYPE (fn));
14947 parameters = TREE_CHAIN (parameters))
14949 if (!TREE_PURPOSE (parameters)
14950 || TREE_CODE (TREE_PURPOSE (parameters)) != DEFAULT_ARG)
14953 /* Save away the current lexer. */
14954 saved_lexer = parser->lexer;
14955 /* Create a new one, using the tokens we have saved. */
14956 tokens = DEFARG_TOKENS (TREE_PURPOSE (parameters));
14957 parser->lexer = cp_lexer_new_from_tokens (tokens);
14959 /* Set the current source position to be the location of the
14960 first token in the default argument. */
14961 cp_lexer_peek_token (parser->lexer);
14963 /* Local variable names (and the `this' keyword) may not appear
14964 in a default argument. */
14965 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
14966 parser->local_variables_forbidden_p = true;
14967 /* Parse the assignment-expression. */
14968 if (DECL_CLASS_SCOPE_P (fn))
14969 push_nested_class (DECL_CONTEXT (fn));
14970 TREE_PURPOSE (parameters) = cp_parser_assignment_expression (parser);
14971 if (DECL_CLASS_SCOPE_P (fn))
14972 pop_nested_class ();
14974 /* If the token stream has not been completely used up, then
14975 there was extra junk after the end of the default
14977 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
14978 cp_parser_error (parser, "expected `,'");
14980 /* Restore saved state. */
14981 parser->lexer = saved_lexer;
14982 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
14985 /* Restore the queue. */
14986 parser->unparsed_functions_queues
14987 = TREE_CHAIN (parser->unparsed_functions_queues);
14990 /* Parse the operand of `sizeof' (or a similar operator). Returns
14991 either a TYPE or an expression, depending on the form of the
14992 input. The KEYWORD indicates which kind of expression we have
14996 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
14998 static const char *format;
14999 tree expr = NULL_TREE;
15000 const char *saved_message;
15001 bool saved_integral_constant_expression_p;
15003 /* Initialize FORMAT the first time we get here. */
15005 format = "types may not be defined in `%s' expressions";
15007 /* Types cannot be defined in a `sizeof' expression. Save away the
15009 saved_message = parser->type_definition_forbidden_message;
15010 /* And create the new one. */
15011 parser->type_definition_forbidden_message
15012 = xmalloc (strlen (format)
15013 + strlen (IDENTIFIER_POINTER (ridpointers[keyword]))
15015 sprintf ((char *) parser->type_definition_forbidden_message,
15016 format, IDENTIFIER_POINTER (ridpointers[keyword]));
15018 /* The restrictions on constant-expressions do not apply inside
15019 sizeof expressions. */
15020 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
15021 parser->integral_constant_expression_p = false;
15023 /* Do not actually evaluate the expression. */
15025 /* If it's a `(', then we might be looking at the type-id
15027 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
15030 bool saved_in_type_id_in_expr_p;
15032 /* We can't be sure yet whether we're looking at a type-id or an
15034 cp_parser_parse_tentatively (parser);
15035 /* Consume the `('. */
15036 cp_lexer_consume_token (parser->lexer);
15037 /* Parse the type-id. */
15038 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
15039 parser->in_type_id_in_expr_p = true;
15040 type = cp_parser_type_id (parser);
15041 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
15042 /* Now, look for the trailing `)'. */
15043 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
15044 /* If all went well, then we're done. */
15045 if (cp_parser_parse_definitely (parser))
15047 /* Build a list of decl-specifiers; right now, we have only
15048 a single type-specifier. */
15049 type = build_tree_list (NULL_TREE,
15052 /* Call grokdeclarator to figure out what type this is. */
15053 expr = grokdeclarator (NULL_TREE,
15057 /*attrlist=*/NULL);
15061 /* If the type-id production did not work out, then we must be
15062 looking at the unary-expression production. */
15064 expr = cp_parser_unary_expression (parser, /*address_p=*/false);
15065 /* Go back to evaluating expressions. */
15068 /* Free the message we created. */
15069 free ((char *) parser->type_definition_forbidden_message);
15070 /* And restore the old one. */
15071 parser->type_definition_forbidden_message = saved_message;
15072 parser->integral_constant_expression_p = saved_integral_constant_expression_p;
15077 /* If the current declaration has no declarator, return true. */
15080 cp_parser_declares_only_class_p (cp_parser *parser)
15082 /* If the next token is a `;' or a `,' then there is no
15084 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
15085 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
15088 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
15089 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
15092 cp_parser_friend_p (tree decl_specifiers)
15094 while (decl_specifiers)
15096 /* See if this decl-specifier is `friend'. */
15097 if (TREE_CODE (TREE_VALUE (decl_specifiers)) == IDENTIFIER_NODE
15098 && C_RID_CODE (TREE_VALUE (decl_specifiers)) == RID_FRIEND)
15101 /* Go on to the next decl-specifier. */
15102 decl_specifiers = TREE_CHAIN (decl_specifiers);
15108 /* If the next token is of the indicated TYPE, consume it. Otherwise,
15109 issue an error message indicating that TOKEN_DESC was expected.
15111 Returns the token consumed, if the token had the appropriate type.
15112 Otherwise, returns NULL. */
15115 cp_parser_require (cp_parser* parser,
15116 enum cpp_ttype type,
15117 const char* token_desc)
15119 if (cp_lexer_next_token_is (parser->lexer, type))
15120 return cp_lexer_consume_token (parser->lexer);
15123 /* Output the MESSAGE -- unless we're parsing tentatively. */
15124 if (!cp_parser_simulate_error (parser))
15126 char *message = concat ("expected ", token_desc, NULL);
15127 cp_parser_error (parser, message);
15134 /* Like cp_parser_require, except that tokens will be skipped until
15135 the desired token is found. An error message is still produced if
15136 the next token is not as expected. */
15139 cp_parser_skip_until_found (cp_parser* parser,
15140 enum cpp_ttype type,
15141 const char* token_desc)
15144 unsigned nesting_depth = 0;
15146 if (cp_parser_require (parser, type, token_desc))
15149 /* Skip tokens until the desired token is found. */
15152 /* Peek at the next token. */
15153 token = cp_lexer_peek_token (parser->lexer);
15154 /* If we've reached the token we want, consume it and
15156 if (token->type == type && !nesting_depth)
15158 cp_lexer_consume_token (parser->lexer);
15161 /* If we've run out of tokens, stop. */
15162 if (token->type == CPP_EOF)
15164 if (token->type == CPP_OPEN_BRACE
15165 || token->type == CPP_OPEN_PAREN
15166 || token->type == CPP_OPEN_SQUARE)
15168 else if (token->type == CPP_CLOSE_BRACE
15169 || token->type == CPP_CLOSE_PAREN
15170 || token->type == CPP_CLOSE_SQUARE)
15172 if (nesting_depth-- == 0)
15175 /* Consume this token. */
15176 cp_lexer_consume_token (parser->lexer);
15180 /* If the next token is the indicated keyword, consume it. Otherwise,
15181 issue an error message indicating that TOKEN_DESC was expected.
15183 Returns the token consumed, if the token had the appropriate type.
15184 Otherwise, returns NULL. */
15187 cp_parser_require_keyword (cp_parser* parser,
15189 const char* token_desc)
15191 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
15193 if (token && token->keyword != keyword)
15195 dyn_string_t error_msg;
15197 /* Format the error message. */
15198 error_msg = dyn_string_new (0);
15199 dyn_string_append_cstr (error_msg, "expected ");
15200 dyn_string_append_cstr (error_msg, token_desc);
15201 cp_parser_error (parser, error_msg->s);
15202 dyn_string_delete (error_msg);
15209 /* Returns TRUE iff TOKEN is a token that can begin the body of a
15210 function-definition. */
15213 cp_parser_token_starts_function_definition_p (cp_token* token)
15215 return (/* An ordinary function-body begins with an `{'. */
15216 token->type == CPP_OPEN_BRACE
15217 /* A ctor-initializer begins with a `:'. */
15218 || token->type == CPP_COLON
15219 /* A function-try-block begins with `try'. */
15220 || token->keyword == RID_TRY
15221 /* The named return value extension begins with `return'. */
15222 || token->keyword == RID_RETURN);
15225 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
15229 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
15233 token = cp_lexer_peek_token (parser->lexer);
15234 return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
15237 /* Returns TRUE iff the next token is the "," or ">" ending a
15238 template-argument. ">>" is also accepted (after the full
15239 argument was parsed) because it's probably a typo for "> >",
15240 and there is a specific diagnostic for this. */
15243 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
15247 token = cp_lexer_peek_token (parser->lexer);
15248 return (token->type == CPP_COMMA || token->type == CPP_GREATER
15249 || token->type == CPP_RSHIFT);
15252 /* Returns TRUE iff the n-th token is a ">", or the n-th is a "[" and the
15253 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
15256 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
15261 token = cp_lexer_peek_nth_token (parser->lexer, n);
15262 if (token->type == CPP_LESS)
15264 /* Check for the sequence `<::' in the original code. It would be lexed as
15265 `[:', where `[' is a digraph, and there is no whitespace before
15267 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
15270 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
15271 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
15277 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
15278 or none_type otherwise. */
15280 static enum tag_types
15281 cp_parser_token_is_class_key (cp_token* token)
15283 switch (token->keyword)
15288 return record_type;
15297 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
15300 cp_parser_check_class_key (enum tag_types class_key, tree type)
15302 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
15303 pedwarn ("`%s' tag used in naming `%#T'",
15304 class_key == union_type ? "union"
15305 : class_key == record_type ? "struct" : "class",
15309 /* Issue an error message if DECL is redeclared with different
15310 access than its original declaration [class.access.spec/3].
15311 This applies to nested classes and nested class templates.
15314 static void cp_parser_check_access_in_redeclaration (tree decl)
15316 if (!CLASS_TYPE_P (TREE_TYPE (decl)))
15319 if ((TREE_PRIVATE (decl)
15320 != (current_access_specifier == access_private_node))
15321 || (TREE_PROTECTED (decl)
15322 != (current_access_specifier == access_protected_node)))
15323 error ("%D redeclared with different access", decl);
15326 /* Look for the `template' keyword, as a syntactic disambiguator.
15327 Return TRUE iff it is present, in which case it will be
15331 cp_parser_optional_template_keyword (cp_parser *parser)
15333 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
15335 /* The `template' keyword can only be used within templates;
15336 outside templates the parser can always figure out what is a
15337 template and what is not. */
15338 if (!processing_template_decl)
15340 error ("`template' (as a disambiguator) is only allowed "
15341 "within templates");
15342 /* If this part of the token stream is rescanned, the same
15343 error message would be generated. So, we purge the token
15344 from the stream. */
15345 cp_lexer_purge_token (parser->lexer);
15350 /* Consume the `template' keyword. */
15351 cp_lexer_consume_token (parser->lexer);
15359 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
15360 set PARSER->SCOPE, and perform other related actions. */
15363 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
15368 /* Get the stored value. */
15369 value = cp_lexer_consume_token (parser->lexer)->value;
15370 /* Perform any access checks that were deferred. */
15371 for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
15372 perform_or_defer_access_check (TREE_PURPOSE (check), TREE_VALUE (check));
15373 /* Set the scope from the stored value. */
15374 parser->scope = TREE_VALUE (value);
15375 parser->qualifying_scope = TREE_TYPE (value);
15376 parser->object_scope = NULL_TREE;
15379 /* Add tokens to CACHE until a non-nested END token appears. */
15382 cp_parser_cache_group_1 (cp_parser *parser,
15383 cp_token_cache *cache,
15384 enum cpp_ttype end,
15391 /* Abort a parenthesized expression if we encounter a brace. */
15392 if ((end == CPP_CLOSE_PAREN || depth == 0)
15393 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
15395 /* If we've reached the end of the file, stop. */
15396 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
15398 /* Consume the next token. */
15399 token = cp_lexer_consume_token (parser->lexer);
15400 /* Add this token to the tokens we are saving. */
15401 cp_token_cache_push_token (cache, token);
15402 /* See if it starts a new group. */
15403 if (token->type == CPP_OPEN_BRACE)
15405 cp_parser_cache_group_1 (parser, cache, CPP_CLOSE_BRACE, depth + 1);
15409 else if (token->type == CPP_OPEN_PAREN)
15410 cp_parser_cache_group_1 (parser, cache, CPP_CLOSE_PAREN, depth + 1);
15411 else if (token->type == end)
15416 /* Convenient interface for cp_parser_cache_group_1 that makes sure we
15417 preserve string tokens in both translated and untranslated
15421 cp_parser_cache_group (cp_parser *parser,
15422 cp_token_cache *cache,
15423 enum cpp_ttype end,
15426 int saved_c_lex_string_translate;
15428 saved_c_lex_string_translate = c_lex_string_translate;
15429 c_lex_string_translate = -1;
15431 cp_parser_cache_group_1 (parser, cache, end, depth);
15433 c_lex_string_translate = saved_c_lex_string_translate;
15437 /* Begin parsing tentatively. We always save tokens while parsing
15438 tentatively so that if the tentative parsing fails we can restore the
15442 cp_parser_parse_tentatively (cp_parser* parser)
15444 /* Enter a new parsing context. */
15445 parser->context = cp_parser_context_new (parser->context);
15446 /* Begin saving tokens. */
15447 cp_lexer_save_tokens (parser->lexer);
15448 /* In order to avoid repetitive access control error messages,
15449 access checks are queued up until we are no longer parsing
15451 push_deferring_access_checks (dk_deferred);
15454 /* Commit to the currently active tentative parse. */
15457 cp_parser_commit_to_tentative_parse (cp_parser* parser)
15459 cp_parser_context *context;
15462 /* Mark all of the levels as committed. */
15463 lexer = parser->lexer;
15464 for (context = parser->context; context->next; context = context->next)
15466 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
15468 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
15469 while (!cp_lexer_saving_tokens (lexer))
15470 lexer = lexer->next;
15471 cp_lexer_commit_tokens (lexer);
15475 /* Abort the currently active tentative parse. All consumed tokens
15476 will be rolled back, and no diagnostics will be issued. */
15479 cp_parser_abort_tentative_parse (cp_parser* parser)
15481 cp_parser_simulate_error (parser);
15482 /* Now, pretend that we want to see if the construct was
15483 successfully parsed. */
15484 cp_parser_parse_definitely (parser);
15487 /* Stop parsing tentatively. If a parse error has occurred, restore the
15488 token stream. Otherwise, commit to the tokens we have consumed.
15489 Returns true if no error occurred; false otherwise. */
15492 cp_parser_parse_definitely (cp_parser* parser)
15494 bool error_occurred;
15495 cp_parser_context *context;
15497 /* Remember whether or not an error occurred, since we are about to
15498 destroy that information. */
15499 error_occurred = cp_parser_error_occurred (parser);
15500 /* Remove the topmost context from the stack. */
15501 context = parser->context;
15502 parser->context = context->next;
15503 /* If no parse errors occurred, commit to the tentative parse. */
15504 if (!error_occurred)
15506 /* Commit to the tokens read tentatively, unless that was
15508 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
15509 cp_lexer_commit_tokens (parser->lexer);
15511 pop_to_parent_deferring_access_checks ();
15513 /* Otherwise, if errors occurred, roll back our state so that things
15514 are just as they were before we began the tentative parse. */
15517 cp_lexer_rollback_tokens (parser->lexer);
15518 pop_deferring_access_checks ();
15520 /* Add the context to the front of the free list. */
15521 context->next = cp_parser_context_free_list;
15522 cp_parser_context_free_list = context;
15524 return !error_occurred;
15527 /* Returns true if we are parsing tentatively -- but have decided that
15528 we will stick with this tentative parse, even if errors occur. */
15531 cp_parser_committed_to_tentative_parse (cp_parser* parser)
15533 return (cp_parser_parsing_tentatively (parser)
15534 && parser->context->status == CP_PARSER_STATUS_KIND_COMMITTED);
15537 /* Returns nonzero iff an error has occurred during the most recent
15538 tentative parse. */
15541 cp_parser_error_occurred (cp_parser* parser)
15543 return (cp_parser_parsing_tentatively (parser)
15544 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
15547 /* Returns nonzero if GNU extensions are allowed. */
15550 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
15552 return parser->allow_gnu_extensions_p;
15558 static GTY (()) cp_parser *the_parser;
15560 /* External interface. */
15562 /* Parse one entire translation unit. */
15565 c_parse_file (void)
15567 bool error_occurred;
15568 static bool already_called = false;
15570 if (already_called)
15572 sorry ("inter-module optimizations not implemented for C++");
15575 already_called = true;
15577 the_parser = cp_parser_new ();
15578 push_deferring_access_checks (flag_access_control
15579 ? dk_no_deferred : dk_no_check);
15580 error_occurred = cp_parser_translation_unit (the_parser);
15584 /* This variable must be provided by every front end. */
15588 #include "gt-cp-parser.h"