2 Copyright (C) 2000, 2001, 2002, 2003, 2004 Free Software Foundation, Inc.
3 Written by Mark Mitchell <mark@codesourcery.com>.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 GCC is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
24 #include "coretypes.h"
26 #include "dyn-string.h"
34 #include "diagnostic.h"
44 A cp_lexer represents a stream of cp_tokens. It allows arbitrary
50 We use a circular buffer to store incoming tokens.
52 Some artifacts of the C++ language (such as the
53 expression/declaration ambiguity) require arbitrary look-ahead.
54 The strategy we adopt for dealing with these problems is to attempt
55 to parse one construct (e.g., the declaration) and fall back to the
56 other (e.g., the expression) if that attempt does not succeed.
57 Therefore, we must sometimes store an arbitrary number of tokens.
59 The parser routinely peeks at the next token, and then consumes it
60 later. That also requires a buffer in which to store the tokens.
62 In order to easily permit adding tokens to the end of the buffer,
63 while removing them from the beginning of the buffer, we use a
68 typedef struct cp_token GTY (())
70 /* The kind of token. */
71 ENUM_BITFIELD (cpp_ttype) type : 8;
72 /* If this token is a keyword, this value indicates which keyword.
73 Otherwise, this value is RID_MAX. */
74 ENUM_BITFIELD (rid) keyword : 8;
77 /* The value associated with this token, if any. */
79 /* The location at which this token was found. */
83 /* The number of tokens in a single token block.
84 Computed so that cp_token_block fits in a 512B allocation unit. */
86 #define CP_TOKEN_BLOCK_NUM_TOKENS ((512 - 3*sizeof (char*))/sizeof (cp_token))
88 /* A group of tokens. These groups are chained together to store
89 large numbers of tokens. (For example, a token block is created
90 when the body of an inline member function is first encountered;
91 the tokens are processed later after the class definition is
94 This somewhat ungainly data structure (as opposed to, say, a
95 variable-length array), is used due to constraints imposed by the
96 current garbage-collection methodology. If it is made more
97 flexible, we could perhaps simplify the data structures involved. */
99 typedef struct cp_token_block GTY (())
102 cp_token tokens[CP_TOKEN_BLOCK_NUM_TOKENS];
103 /* The number of tokens in this block. */
105 /* The next token block in the chain. */
106 struct cp_token_block *next;
107 /* The previous block in the chain. */
108 struct cp_token_block *prev;
111 typedef struct cp_token_cache GTY (())
113 /* The first block in the cache. NULL if there are no tokens in the
115 cp_token_block *first;
116 /* The last block in the cache. NULL If there are no tokens in the
118 cp_token_block *last;
123 static cp_token_cache *cp_token_cache_new
125 static void cp_token_cache_push_token
126 (cp_token_cache *, cp_token *);
128 /* Create a new cp_token_cache. */
130 static cp_token_cache *
131 cp_token_cache_new (void)
133 return ggc_alloc_cleared (sizeof (cp_token_cache));
136 /* Add *TOKEN to *CACHE. */
139 cp_token_cache_push_token (cp_token_cache *cache,
142 cp_token_block *b = cache->last;
144 /* See if we need to allocate a new token block. */
145 if (!b || b->num_tokens == CP_TOKEN_BLOCK_NUM_TOKENS)
147 b = ggc_alloc_cleared (sizeof (cp_token_block));
148 b->prev = cache->last;
151 cache->last->next = b;
155 cache->first = cache->last = b;
157 /* Add this token to the current token block. */
158 b->tokens[b->num_tokens++] = *token;
161 /* The cp_lexer structure represents the C++ lexer. It is responsible
162 for managing the token stream from the preprocessor and supplying
165 typedef struct cp_lexer GTY (())
167 /* The memory allocated for the buffer. Never NULL. */
168 cp_token * GTY ((length ("(%h.buffer_end - %h.buffer)"))) buffer;
169 /* A pointer just past the end of the memory allocated for the buffer. */
170 cp_token * GTY ((skip (""))) buffer_end;
171 /* The first valid token in the buffer, or NULL if none. */
172 cp_token * GTY ((skip (""))) first_token;
173 /* The next available token. If NEXT_TOKEN is NULL, then there are
174 no more available tokens. */
175 cp_token * GTY ((skip (""))) next_token;
176 /* A pointer just past the last available token. If FIRST_TOKEN is
177 NULL, however, there are no available tokens, and then this
178 location is simply the place in which the next token read will be
179 placed. If LAST_TOKEN == FIRST_TOKEN, then the buffer is full.
180 When the LAST_TOKEN == BUFFER, then the last token is at the
181 highest memory address in the BUFFER. */
182 cp_token * GTY ((skip (""))) last_token;
184 /* A stack indicating positions at which cp_lexer_save_tokens was
185 called. The top entry is the most recent position at which we
186 began saving tokens. The entries are differences in token
187 position between FIRST_TOKEN and the first saved token.
189 If the stack is non-empty, we are saving tokens. When a token is
190 consumed, the NEXT_TOKEN pointer will move, but the FIRST_TOKEN
191 pointer will not. The token stream will be preserved so that it
192 can be reexamined later.
194 If the stack is empty, then we are not saving tokens. Whenever a
195 token is consumed, the FIRST_TOKEN pointer will be moved, and the
196 consumed token will be gone forever. */
197 varray_type saved_tokens;
199 /* The STRING_CST tokens encountered while processing the current
201 varray_type string_tokens;
203 /* True if we should obtain more tokens from the preprocessor; false
204 if we are processing a saved token cache. */
207 /* True if we should output debugging information. */
210 /* The next lexer in a linked list of lexers. */
211 struct cp_lexer *next;
216 static cp_lexer *cp_lexer_new_main
218 static cp_lexer *cp_lexer_new_from_tokens
219 (struct cp_token_cache *);
220 static int cp_lexer_saving_tokens
222 static cp_token *cp_lexer_next_token
223 (cp_lexer *, cp_token *);
224 static cp_token *cp_lexer_prev_token
225 (cp_lexer *, cp_token *);
226 static ptrdiff_t cp_lexer_token_difference
227 (cp_lexer *, cp_token *, cp_token *);
228 static cp_token *cp_lexer_read_token
230 static void cp_lexer_maybe_grow_buffer
232 static void cp_lexer_get_preprocessor_token
233 (cp_lexer *, cp_token *);
234 static cp_token *cp_lexer_peek_token
236 static cp_token *cp_lexer_peek_nth_token
237 (cp_lexer *, size_t);
238 static inline bool cp_lexer_next_token_is
239 (cp_lexer *, enum cpp_ttype);
240 static bool cp_lexer_next_token_is_not
241 (cp_lexer *, enum cpp_ttype);
242 static bool cp_lexer_next_token_is_keyword
243 (cp_lexer *, enum rid);
244 static cp_token *cp_lexer_consume_token
246 static void cp_lexer_purge_token
248 static void cp_lexer_purge_tokens_after
249 (cp_lexer *, cp_token *);
250 static void cp_lexer_save_tokens
252 static void cp_lexer_commit_tokens
254 static void cp_lexer_rollback_tokens
256 static inline void cp_lexer_set_source_position_from_token
257 (cp_lexer *, const cp_token *);
258 static void cp_lexer_print_token
259 (FILE *, cp_token *);
260 static inline bool cp_lexer_debugging_p
262 static void cp_lexer_start_debugging
263 (cp_lexer *) ATTRIBUTE_UNUSED;
264 static void cp_lexer_stop_debugging
265 (cp_lexer *) ATTRIBUTE_UNUSED;
267 /* Manifest constants. */
269 #define CP_TOKEN_BUFFER_SIZE 5
270 #define CP_SAVED_TOKENS_SIZE 5
272 /* A token type for keywords, as opposed to ordinary identifiers. */
273 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
275 /* A token type for template-ids. If a template-id is processed while
276 parsing tentatively, it is replaced with a CPP_TEMPLATE_ID token;
277 the value of the CPP_TEMPLATE_ID is whatever was returned by
278 cp_parser_template_id. */
279 #define CPP_TEMPLATE_ID ((enum cpp_ttype) (CPP_KEYWORD + 1))
281 /* A token type for nested-name-specifiers. If a
282 nested-name-specifier is processed while parsing tentatively, it is
283 replaced with a CPP_NESTED_NAME_SPECIFIER token; the value of the
284 CPP_NESTED_NAME_SPECIFIER is whatever was returned by
285 cp_parser_nested_name_specifier_opt. */
286 #define CPP_NESTED_NAME_SPECIFIER ((enum cpp_ttype) (CPP_TEMPLATE_ID + 1))
288 /* A token type for tokens that are not tokens at all; these are used
289 to mark the end of a token block. */
290 #define CPP_NONE (CPP_NESTED_NAME_SPECIFIER + 1)
294 /* The stream to which debugging output should be written. */
295 static FILE *cp_lexer_debug_stream;
297 /* Create a new main C++ lexer, the lexer that gets tokens from the
301 cp_lexer_new_main (void)
304 cp_token first_token;
306 /* It's possible that lexing the first token will load a PCH file,
307 which is a GC collection point. So we have to grab the first
308 token before allocating any memory. */
309 cp_lexer_get_preprocessor_token (NULL, &first_token);
310 c_common_no_more_pch ();
312 /* Allocate the memory. */
313 lexer = ggc_alloc_cleared (sizeof (cp_lexer));
315 /* Create the circular buffer. */
316 lexer->buffer = ggc_calloc (CP_TOKEN_BUFFER_SIZE, sizeof (cp_token));
317 lexer->buffer_end = lexer->buffer + CP_TOKEN_BUFFER_SIZE;
319 /* There is one token in the buffer. */
320 lexer->last_token = lexer->buffer + 1;
321 lexer->first_token = lexer->buffer;
322 lexer->next_token = lexer->buffer;
323 memcpy (lexer->buffer, &first_token, sizeof (cp_token));
325 /* This lexer obtains more tokens by calling c_lex. */
326 lexer->main_lexer_p = true;
328 /* Create the SAVED_TOKENS stack. */
329 VARRAY_INT_INIT (lexer->saved_tokens, CP_SAVED_TOKENS_SIZE, "saved_tokens");
331 /* Create the STRINGS array. */
332 VARRAY_TREE_INIT (lexer->string_tokens, 32, "strings");
334 /* Assume we are not debugging. */
335 lexer->debugging_p = false;
340 /* Create a new lexer whose token stream is primed with the TOKENS.
341 When these tokens are exhausted, no new tokens will be read. */
344 cp_lexer_new_from_tokens (cp_token_cache *tokens)
348 cp_token_block *block;
349 ptrdiff_t num_tokens;
351 /* Allocate the memory. */
352 lexer = ggc_alloc_cleared (sizeof (cp_lexer));
354 /* Create a new buffer, appropriately sized. */
356 for (block = tokens->first; block != NULL; block = block->next)
357 num_tokens += block->num_tokens;
358 lexer->buffer = ggc_alloc (num_tokens * sizeof (cp_token));
359 lexer->buffer_end = lexer->buffer + num_tokens;
361 /* Install the tokens. */
362 token = lexer->buffer;
363 for (block = tokens->first; block != NULL; block = block->next)
365 memcpy (token, block->tokens, block->num_tokens * sizeof (cp_token));
366 token += block->num_tokens;
369 /* The FIRST_TOKEN is the beginning of the buffer. */
370 lexer->first_token = lexer->buffer;
371 /* The next available token is also at the beginning of the buffer. */
372 lexer->next_token = lexer->buffer;
373 /* The buffer is full. */
374 lexer->last_token = lexer->first_token;
376 /* This lexer doesn't obtain more tokens. */
377 lexer->main_lexer_p = false;
379 /* Create the SAVED_TOKENS stack. */
380 VARRAY_INT_INIT (lexer->saved_tokens, CP_SAVED_TOKENS_SIZE, "saved_tokens");
382 /* Create the STRINGS array. */
383 VARRAY_TREE_INIT (lexer->string_tokens, 32, "strings");
385 /* Assume we are not debugging. */
386 lexer->debugging_p = false;
391 /* Returns nonzero if debugging information should be output. */
394 cp_lexer_debugging_p (cp_lexer *lexer)
396 return lexer->debugging_p;
399 /* Set the current source position from the information stored in
403 cp_lexer_set_source_position_from_token (cp_lexer *lexer ATTRIBUTE_UNUSED ,
404 const cp_token *token)
406 /* Ideally, the source position information would not be a global
407 variable, but it is. */
409 /* Update the line number. */
410 if (token->type != CPP_EOF)
411 input_location = token->location;
414 /* TOKEN points into the circular token buffer. Return a pointer to
415 the next token in the buffer. */
417 static inline cp_token *
418 cp_lexer_next_token (cp_lexer* lexer, cp_token* token)
421 if (token == lexer->buffer_end)
422 token = lexer->buffer;
426 /* TOKEN points into the circular token buffer. Return a pointer to
427 the previous token in the buffer. */
429 static inline cp_token *
430 cp_lexer_prev_token (cp_lexer* lexer, cp_token* token)
432 if (token == lexer->buffer)
433 token = lexer->buffer_end;
437 /* nonzero if we are presently saving tokens. */
440 cp_lexer_saving_tokens (const cp_lexer* lexer)
442 return VARRAY_ACTIVE_SIZE (lexer->saved_tokens) != 0;
445 /* Return a pointer to the token that is N tokens beyond TOKEN in the
449 cp_lexer_advance_token (cp_lexer *lexer, cp_token *token, ptrdiff_t n)
452 if (token >= lexer->buffer_end)
453 token = lexer->buffer + (token - lexer->buffer_end);
457 /* Returns the number of times that START would have to be incremented
458 to reach FINISH. If START and FINISH are the same, returns zero. */
461 cp_lexer_token_difference (cp_lexer* lexer, cp_token* start, cp_token* finish)
464 return finish - start;
466 return ((lexer->buffer_end - lexer->buffer)
470 /* Obtain another token from the C preprocessor and add it to the
471 token buffer. Returns the newly read token. */
474 cp_lexer_read_token (cp_lexer* lexer)
478 /* Make sure there is room in the buffer. */
479 cp_lexer_maybe_grow_buffer (lexer);
481 /* If there weren't any tokens, then this one will be the first. */
482 if (!lexer->first_token)
483 lexer->first_token = lexer->last_token;
484 /* Similarly, if there were no available tokens, there is one now. */
485 if (!lexer->next_token)
486 lexer->next_token = lexer->last_token;
488 /* Figure out where we're going to store the new token. */
489 token = lexer->last_token;
491 /* Get a new token from the preprocessor. */
492 cp_lexer_get_preprocessor_token (lexer, token);
494 /* Increment LAST_TOKEN. */
495 lexer->last_token = cp_lexer_next_token (lexer, token);
497 /* Strings should have type `const char []'. Right now, we will
498 have an ARRAY_TYPE that is constant rather than an array of
500 FIXME: Make fix_string_type get this right in the first place. */
501 if ((token->type == CPP_STRING || token->type == CPP_WSTRING)
502 && flag_const_strings)
506 /* Get the current type. It will be an ARRAY_TYPE. */
507 type = TREE_TYPE (token->value);
508 /* Use build_cplus_array_type to rebuild the array, thereby
509 getting the right type. */
510 type = build_cplus_array_type (TREE_TYPE (type), TYPE_DOMAIN (type));
511 /* Reset the type of the token. */
512 TREE_TYPE (token->value) = type;
518 /* If the circular buffer is full, make it bigger. */
521 cp_lexer_maybe_grow_buffer (cp_lexer* lexer)
523 /* If the buffer is full, enlarge it. */
524 if (lexer->last_token == lexer->first_token)
526 cp_token *new_buffer;
527 cp_token *old_buffer;
528 cp_token *new_first_token;
529 ptrdiff_t buffer_length;
530 size_t num_tokens_to_copy;
532 /* Remember the current buffer pointer. It will become invalid,
533 but we will need to do pointer arithmetic involving this
535 old_buffer = lexer->buffer;
536 /* Compute the current buffer size. */
537 buffer_length = lexer->buffer_end - lexer->buffer;
538 /* Allocate a buffer twice as big. */
539 new_buffer = ggc_realloc (lexer->buffer,
540 2 * buffer_length * sizeof (cp_token));
542 /* Because the buffer is circular, logically consecutive tokens
543 are not necessarily placed consecutively in memory.
544 Therefore, we must keep move the tokens that were before
545 FIRST_TOKEN to the second half of the newly allocated
547 num_tokens_to_copy = (lexer->first_token - old_buffer);
548 memcpy (new_buffer + buffer_length,
550 num_tokens_to_copy * sizeof (cp_token));
551 /* Clear the rest of the buffer. We never look at this storage,
552 but the garbage collector may. */
553 memset (new_buffer + buffer_length + num_tokens_to_copy, 0,
554 (buffer_length - num_tokens_to_copy) * sizeof (cp_token));
556 /* Now recompute all of the buffer pointers. */
558 = new_buffer + (lexer->first_token - old_buffer);
559 if (lexer->next_token != NULL)
561 ptrdiff_t next_token_delta;
563 if (lexer->next_token > lexer->first_token)
564 next_token_delta = lexer->next_token - lexer->first_token;
567 buffer_length - (lexer->first_token - lexer->next_token);
568 lexer->next_token = new_first_token + next_token_delta;
570 lexer->last_token = new_first_token + buffer_length;
571 lexer->buffer = new_buffer;
572 lexer->buffer_end = new_buffer + buffer_length * 2;
573 lexer->first_token = new_first_token;
577 /* Store the next token from the preprocessor in *TOKEN. */
580 cp_lexer_get_preprocessor_token (cp_lexer *lexer ATTRIBUTE_UNUSED ,
585 /* If this not the main lexer, return a terminating CPP_EOF token. */
586 if (lexer != NULL && !lexer->main_lexer_p)
588 token->type = CPP_EOF;
589 token->location.line = 0;
590 token->location.file = NULL;
591 token->value = NULL_TREE;
592 token->keyword = RID_MAX;
598 /* Keep going until we get a token we like. */
601 /* Get a new token from the preprocessor. */
602 token->type = c_lex_with_flags (&token->value, &token->flags);
603 /* Issue messages about tokens we cannot process. */
609 error ("invalid token");
613 /* This is a good token, so we exit the loop. */
618 /* Now we've got our token. */
619 token->location = input_location;
621 /* Check to see if this token is a keyword. */
622 if (token->type == CPP_NAME
623 && C_IS_RESERVED_WORD (token->value))
625 /* Mark this token as a keyword. */
626 token->type = CPP_KEYWORD;
627 /* Record which keyword. */
628 token->keyword = C_RID_CODE (token->value);
629 /* Update the value. Some keywords are mapped to particular
630 entities, rather than simply having the value of the
631 corresponding IDENTIFIER_NODE. For example, `__const' is
632 mapped to `const'. */
633 token->value = ridpointers[token->keyword];
636 token->keyword = RID_MAX;
639 /* Return a pointer to the next token in the token stream, but do not
643 cp_lexer_peek_token (cp_lexer* lexer)
647 /* If there are no tokens, read one now. */
648 if (!lexer->next_token)
649 cp_lexer_read_token (lexer);
651 /* Provide debugging output. */
652 if (cp_lexer_debugging_p (lexer))
654 fprintf (cp_lexer_debug_stream, "cp_lexer: peeking at token: ");
655 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
656 fprintf (cp_lexer_debug_stream, "\n");
659 token = lexer->next_token;
660 cp_lexer_set_source_position_from_token (lexer, token);
664 /* Return true if the next token has the indicated TYPE. */
667 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
671 /* Peek at the next token. */
672 token = cp_lexer_peek_token (lexer);
673 /* Check to see if it has the indicated TYPE. */
674 return token->type == type;
677 /* Return true if the next token does not have the indicated TYPE. */
680 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
682 return !cp_lexer_next_token_is (lexer, type);
685 /* Return true if the next token is the indicated KEYWORD. */
688 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
692 /* Peek at the next token. */
693 token = cp_lexer_peek_token (lexer);
694 /* Check to see if it is the indicated keyword. */
695 return token->keyword == keyword;
698 /* Return a pointer to the Nth token in the token stream. If N is 1,
699 then this is precisely equivalent to cp_lexer_peek_token. */
702 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
706 /* N is 1-based, not zero-based. */
707 my_friendly_assert (n > 0, 20000224);
709 /* Skip ahead from NEXT_TOKEN, reading more tokens as necessary. */
710 token = lexer->next_token;
711 /* If there are no tokens in the buffer, get one now. */
714 cp_lexer_read_token (lexer);
715 token = lexer->next_token;
718 /* Now, read tokens until we have enough. */
721 /* Advance to the next token. */
722 token = cp_lexer_next_token (lexer, token);
723 /* If that's all the tokens we have, read a new one. */
724 if (token == lexer->last_token)
725 token = cp_lexer_read_token (lexer);
731 /* Consume the next token. The pointer returned is valid only until
732 another token is read. Callers should preserve copy the token
733 explicitly if they will need its value for a longer period of
737 cp_lexer_consume_token (cp_lexer* lexer)
741 /* If there are no tokens, read one now. */
742 if (!lexer->next_token)
743 cp_lexer_read_token (lexer);
745 /* Remember the token we'll be returning. */
746 token = lexer->next_token;
748 /* Increment NEXT_TOKEN. */
749 lexer->next_token = cp_lexer_next_token (lexer,
751 /* Check to see if we're all out of tokens. */
752 if (lexer->next_token == lexer->last_token)
753 lexer->next_token = NULL;
755 /* If we're not saving tokens, then move FIRST_TOKEN too. */
756 if (!cp_lexer_saving_tokens (lexer))
758 /* If there are no tokens available, set FIRST_TOKEN to NULL. */
759 if (!lexer->next_token)
760 lexer->first_token = NULL;
762 lexer->first_token = lexer->next_token;
765 /* Provide debugging output. */
766 if (cp_lexer_debugging_p (lexer))
768 fprintf (cp_lexer_debug_stream, "cp_lexer: consuming token: ");
769 cp_lexer_print_token (cp_lexer_debug_stream, token);
770 fprintf (cp_lexer_debug_stream, "\n");
776 /* Permanently remove the next token from the token stream. There
777 must be a valid next token already; this token never reads
778 additional tokens from the preprocessor. */
781 cp_lexer_purge_token (cp_lexer *lexer)
784 cp_token *next_token;
786 token = lexer->next_token;
789 next_token = cp_lexer_next_token (lexer, token);
790 if (next_token == lexer->last_token)
792 *token = *next_token;
796 lexer->last_token = token;
797 /* The token purged may have been the only token remaining; if so,
799 if (lexer->next_token == token)
800 lexer->next_token = NULL;
803 /* Permanently remove all tokens after TOKEN, up to, but not
804 including, the token that will be returned next by
805 cp_lexer_peek_token. */
808 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *token)
814 if (lexer->next_token)
816 /* Copy the tokens that have not yet been read to the location
817 immediately following TOKEN. */
818 t1 = cp_lexer_next_token (lexer, token);
819 t2 = peek = cp_lexer_peek_token (lexer);
820 /* Move tokens into the vacant area between TOKEN and PEEK. */
821 while (t2 != lexer->last_token)
824 t1 = cp_lexer_next_token (lexer, t1);
825 t2 = cp_lexer_next_token (lexer, t2);
827 /* Now, the next available token is right after TOKEN. */
828 lexer->next_token = cp_lexer_next_token (lexer, token);
829 /* And the last token is wherever we ended up. */
830 lexer->last_token = t1;
834 /* There are no tokens in the buffer, so there is nothing to
835 copy. The last token in the buffer is TOKEN itself. */
836 lexer->last_token = cp_lexer_next_token (lexer, token);
840 /* Begin saving tokens. All tokens consumed after this point will be
844 cp_lexer_save_tokens (cp_lexer* lexer)
846 /* Provide debugging output. */
847 if (cp_lexer_debugging_p (lexer))
848 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
850 /* Make sure that LEXER->NEXT_TOKEN is non-NULL so that we can
851 restore the tokens if required. */
852 if (!lexer->next_token)
853 cp_lexer_read_token (lexer);
855 VARRAY_PUSH_INT (lexer->saved_tokens,
856 cp_lexer_token_difference (lexer,
861 /* Commit to the portion of the token stream most recently saved. */
864 cp_lexer_commit_tokens (cp_lexer* lexer)
866 /* Provide debugging output. */
867 if (cp_lexer_debugging_p (lexer))
868 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
870 VARRAY_POP (lexer->saved_tokens);
873 /* Return all tokens saved since the last call to cp_lexer_save_tokens
874 to the token stream. Stop saving tokens. */
877 cp_lexer_rollback_tokens (cp_lexer* lexer)
881 /* Provide debugging output. */
882 if (cp_lexer_debugging_p (lexer))
883 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
885 /* Find the token that was the NEXT_TOKEN when we started saving
887 delta = VARRAY_TOP_INT(lexer->saved_tokens);
888 /* Make it the next token again now. */
889 lexer->next_token = cp_lexer_advance_token (lexer,
892 /* It might be the case that there were no tokens when we started
893 saving tokens, but that there are some tokens now. */
894 if (!lexer->next_token && lexer->first_token)
895 lexer->next_token = lexer->first_token;
897 /* Stop saving tokens. */
898 VARRAY_POP (lexer->saved_tokens);
901 /* Print a representation of the TOKEN on the STREAM. */
904 cp_lexer_print_token (FILE * stream, cp_token* token)
906 const char *token_type = NULL;
908 /* Figure out what kind of token this is. */
916 token_type = "COMMA";
920 token_type = "OPEN_PAREN";
923 case CPP_CLOSE_PAREN:
924 token_type = "CLOSE_PAREN";
928 token_type = "OPEN_BRACE";
931 case CPP_CLOSE_BRACE:
932 token_type = "CLOSE_BRACE";
936 token_type = "SEMICOLON";
948 token_type = "keyword";
951 /* This is not a token that we know how to handle yet. */
956 /* If we have a name for the token, print it out. Otherwise, we
957 simply give the numeric code. */
959 fprintf (stream, "%s", token_type);
961 fprintf (stream, "%d", token->type);
962 /* And, for an identifier, print the identifier name. */
963 if (token->type == CPP_NAME
964 /* Some keywords have a value that is not an IDENTIFIER_NODE.
965 For example, `struct' is mapped to an INTEGER_CST. */
966 || (token->type == CPP_KEYWORD
967 && TREE_CODE (token->value) == IDENTIFIER_NODE))
968 fprintf (stream, " %s", IDENTIFIER_POINTER (token->value));
971 /* Start emitting debugging information. */
974 cp_lexer_start_debugging (cp_lexer* lexer)
976 ++lexer->debugging_p;
979 /* Stop emitting debugging information. */
982 cp_lexer_stop_debugging (cp_lexer* lexer)
984 --lexer->debugging_p;
993 A cp_parser parses the token stream as specified by the C++
994 grammar. Its job is purely parsing, not semantic analysis. For
995 example, the parser breaks the token stream into declarators,
996 expressions, statements, and other similar syntactic constructs.
997 It does not check that the types of the expressions on either side
998 of an assignment-statement are compatible, or that a function is
999 not declared with a parameter of type `void'.
1001 The parser invokes routines elsewhere in the compiler to perform
1002 semantic analysis and to build up the abstract syntax tree for the
1005 The parser (and the template instantiation code, which is, in a
1006 way, a close relative of parsing) are the only parts of the
1007 compiler that should be calling push_scope and pop_scope, or
1008 related functions. The parser (and template instantiation code)
1009 keeps track of what scope is presently active; everything else
1010 should simply honor that. (The code that generates static
1011 initializers may also need to set the scope, in order to check
1012 access control correctly when emitting the initializers.)
1017 The parser is of the standard recursive-descent variety. Upcoming
1018 tokens in the token stream are examined in order to determine which
1019 production to use when parsing a non-terminal. Some C++ constructs
1020 require arbitrary look ahead to disambiguate. For example, it is
1021 impossible, in the general case, to tell whether a statement is an
1022 expression or declaration without scanning the entire statement.
1023 Therefore, the parser is capable of "parsing tentatively." When the
1024 parser is not sure what construct comes next, it enters this mode.
1025 Then, while we attempt to parse the construct, the parser queues up
1026 error messages, rather than issuing them immediately, and saves the
1027 tokens it consumes. If the construct is parsed successfully, the
1028 parser "commits", i.e., it issues any queued error messages and
1029 the tokens that were being preserved are permanently discarded.
1030 If, however, the construct is not parsed successfully, the parser
1031 rolls back its state completely so that it can resume parsing using
1032 a different alternative.
1037 The performance of the parser could probably be improved
1038 substantially. Some possible improvements include:
1040 - The expression parser recurses through the various levels of
1041 precedence as specified in the grammar, rather than using an
1042 operator-precedence technique. Therefore, parsing a simple
1043 identifier requires multiple recursive calls.
1045 - We could often eliminate the need to parse tentatively by
1046 looking ahead a little bit. In some places, this approach
1047 might not entirely eliminate the need to parse tentatively, but
1048 it might still speed up the average case. */
1050 /* Flags that are passed to some parsing functions. These values can
1051 be bitwise-ored together. */
1053 typedef enum cp_parser_flags
1056 CP_PARSER_FLAGS_NONE = 0x0,
1057 /* The construct is optional. If it is not present, then no error
1058 should be issued. */
1059 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1060 /* When parsing a type-specifier, do not allow user-defined types. */
1061 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2
1064 /* The different kinds of declarators we want to parse. */
1066 typedef enum cp_parser_declarator_kind
1068 /* We want an abstract declartor. */
1069 CP_PARSER_DECLARATOR_ABSTRACT,
1070 /* We want a named declarator. */
1071 CP_PARSER_DECLARATOR_NAMED,
1072 /* We don't mind, but the name must be an unqualified-id. */
1073 CP_PARSER_DECLARATOR_EITHER
1074 } cp_parser_declarator_kind;
1076 /* A mapping from a token type to a corresponding tree node type. */
1078 typedef struct cp_parser_token_tree_map_node
1080 /* The token type. */
1081 ENUM_BITFIELD (cpp_ttype) token_type : 8;
1082 /* The corresponding tree code. */
1083 ENUM_BITFIELD (tree_code) tree_type : 8;
1084 } cp_parser_token_tree_map_node;
1086 /* A complete map consists of several ordinary entries, followed by a
1087 terminator. The terminating entry has a token_type of CPP_EOF. */
1089 typedef cp_parser_token_tree_map_node cp_parser_token_tree_map[];
1091 /* The status of a tentative parse. */
1093 typedef enum cp_parser_status_kind
1095 /* No errors have occurred. */
1096 CP_PARSER_STATUS_KIND_NO_ERROR,
1097 /* An error has occurred. */
1098 CP_PARSER_STATUS_KIND_ERROR,
1099 /* We are committed to this tentative parse, whether or not an error
1101 CP_PARSER_STATUS_KIND_COMMITTED
1102 } cp_parser_status_kind;
1104 /* Context that is saved and restored when parsing tentatively. */
1106 typedef struct cp_parser_context GTY (())
1108 /* If this is a tentative parsing context, the status of the
1110 enum cp_parser_status_kind status;
1111 /* If non-NULL, we have just seen a `x->' or `x.' expression. Names
1112 that are looked up in this context must be looked up both in the
1113 scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1114 the context of the containing expression. */
1116 /* The next parsing context in the stack. */
1117 struct cp_parser_context *next;
1118 } cp_parser_context;
1122 /* Constructors and destructors. */
1124 static cp_parser_context *cp_parser_context_new
1125 (cp_parser_context *);
1127 /* Class variables. */
1129 static GTY((deletable (""))) cp_parser_context* cp_parser_context_free_list;
1131 /* Constructors and destructors. */
1133 /* Construct a new context. The context below this one on the stack
1134 is given by NEXT. */
1136 static cp_parser_context *
1137 cp_parser_context_new (cp_parser_context* next)
1139 cp_parser_context *context;
1141 /* Allocate the storage. */
1142 if (cp_parser_context_free_list != NULL)
1144 /* Pull the first entry from the free list. */
1145 context = cp_parser_context_free_list;
1146 cp_parser_context_free_list = context->next;
1147 memset (context, 0, sizeof (*context));
1150 context = ggc_alloc_cleared (sizeof (cp_parser_context));
1151 /* No errors have occurred yet in this context. */
1152 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1153 /* If this is not the bottomost context, copy information that we
1154 need from the previous context. */
1157 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1158 expression, then we are parsing one in this context, too. */
1159 context->object_type = next->object_type;
1160 /* Thread the stack. */
1161 context->next = next;
1167 /* The cp_parser structure represents the C++ parser. */
1169 typedef struct cp_parser GTY(())
1171 /* The lexer from which we are obtaining tokens. */
1174 /* The scope in which names should be looked up. If NULL_TREE, then
1175 we look up names in the scope that is currently open in the
1176 source program. If non-NULL, this is either a TYPE or
1177 NAMESPACE_DECL for the scope in which we should look.
1179 This value is not cleared automatically after a name is looked
1180 up, so we must be careful to clear it before starting a new look
1181 up sequence. (If it is not cleared, then `X::Y' followed by `Z'
1182 will look up `Z' in the scope of `X', rather than the current
1183 scope.) Unfortunately, it is difficult to tell when name lookup
1184 is complete, because we sometimes peek at a token, look it up,
1185 and then decide not to consume it. */
1188 /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1189 last lookup took place. OBJECT_SCOPE is used if an expression
1190 like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1191 respectively. QUALIFYING_SCOPE is used for an expression of the
1192 form "X::Y"; it refers to X. */
1194 tree qualifying_scope;
1196 /* A stack of parsing contexts. All but the bottom entry on the
1197 stack will be tentative contexts.
1199 We parse tentatively in order to determine which construct is in
1200 use in some situations. For example, in order to determine
1201 whether a statement is an expression-statement or a
1202 declaration-statement we parse it tentatively as a
1203 declaration-statement. If that fails, we then reparse the same
1204 token stream as an expression-statement. */
1205 cp_parser_context *context;
1207 /* True if we are parsing GNU C++. If this flag is not set, then
1208 GNU extensions are not recognized. */
1209 bool allow_gnu_extensions_p;
1211 /* TRUE if the `>' token should be interpreted as the greater-than
1212 operator. FALSE if it is the end of a template-id or
1213 template-parameter-list. */
1214 bool greater_than_is_operator_p;
1216 /* TRUE if default arguments are allowed within a parameter list
1217 that starts at this point. FALSE if only a gnu extension makes
1218 them permissible. */
1219 bool default_arg_ok_p;
1221 /* TRUE if we are parsing an integral constant-expression. See
1222 [expr.const] for a precise definition. */
1223 bool integral_constant_expression_p;
1225 /* TRUE if we are parsing an integral constant-expression -- but a
1226 non-constant expression should be permitted as well. This flag
1227 is used when parsing an array bound so that GNU variable-length
1228 arrays are tolerated. */
1229 bool allow_non_integral_constant_expression_p;
1231 /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1232 been seen that makes the expression non-constant. */
1233 bool non_integral_constant_expression_p;
1235 /* TRUE if we are parsing the argument to "__offsetof__". */
1238 /* TRUE if local variable names and `this' are forbidden in the
1240 bool local_variables_forbidden_p;
1242 /* TRUE if the declaration we are parsing is part of a
1243 linkage-specification of the form `extern string-literal
1245 bool in_unbraced_linkage_specification_p;
1247 /* TRUE if we are presently parsing a declarator, after the
1248 direct-declarator. */
1249 bool in_declarator_p;
1251 /* TRUE if we are presently parsing a template-argument-list. */
1252 bool in_template_argument_list_p;
1254 /* TRUE if we are presently parsing the body of an
1255 iteration-statement. */
1256 bool in_iteration_statement_p;
1258 /* TRUE if we are presently parsing the body of a switch
1260 bool in_switch_statement_p;
1262 /* TRUE if we are parsing a type-id in an expression context. In
1263 such a situation, both "type (expr)" and "type (type)" are valid
1265 bool in_type_id_in_expr_p;
1267 /* If non-NULL, then we are parsing a construct where new type
1268 definitions are not permitted. The string stored here will be
1269 issued as an error message if a type is defined. */
1270 const char *type_definition_forbidden_message;
1272 /* A list of lists. The outer list is a stack, used for member
1273 functions of local classes. At each level there are two sub-list,
1274 one on TREE_VALUE and one on TREE_PURPOSE. Each of those
1275 sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their
1276 TREE_VALUE's. The functions are chained in reverse declaration
1279 The TREE_PURPOSE sublist contains those functions with default
1280 arguments that need post processing, and the TREE_VALUE sublist
1281 contains those functions with definitions that need post
1284 These lists can only be processed once the outermost class being
1285 defined is complete. */
1286 tree unparsed_functions_queues;
1288 /* The number of classes whose definitions are currently in
1290 unsigned num_classes_being_defined;
1292 /* The number of template parameter lists that apply directly to the
1293 current declaration. */
1294 unsigned num_template_parameter_lists;
1297 /* The type of a function that parses some kind of expression. */
1298 typedef tree (*cp_parser_expression_fn) (cp_parser *);
1302 /* Constructors and destructors. */
1304 static cp_parser *cp_parser_new
1307 /* Routines to parse various constructs.
1309 Those that return `tree' will return the error_mark_node (rather
1310 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1311 Sometimes, they will return an ordinary node if error-recovery was
1312 attempted, even though a parse error occurred. So, to check
1313 whether or not a parse error occurred, you should always use
1314 cp_parser_error_occurred. If the construct is optional (indicated
1315 either by an `_opt' in the name of the function that does the
1316 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1317 the construct is not present. */
1319 /* Lexical conventions [gram.lex] */
1321 static tree cp_parser_identifier
1324 /* Basic concepts [gram.basic] */
1326 static bool cp_parser_translation_unit
1329 /* Expressions [gram.expr] */
1331 static tree cp_parser_primary_expression
1332 (cp_parser *, cp_id_kind *, tree *);
1333 static tree cp_parser_id_expression
1334 (cp_parser *, bool, bool, bool *, bool);
1335 static tree cp_parser_unqualified_id
1336 (cp_parser *, bool, bool, bool);
1337 static tree cp_parser_nested_name_specifier_opt
1338 (cp_parser *, bool, bool, bool, bool);
1339 static tree cp_parser_nested_name_specifier
1340 (cp_parser *, bool, bool, bool, bool);
1341 static tree cp_parser_class_or_namespace_name
1342 (cp_parser *, bool, bool, bool, bool, bool);
1343 static tree cp_parser_postfix_expression
1344 (cp_parser *, bool);
1345 static tree cp_parser_parenthesized_expression_list
1346 (cp_parser *, bool, bool *);
1347 static void cp_parser_pseudo_destructor_name
1348 (cp_parser *, tree *, tree *);
1349 static tree cp_parser_unary_expression
1350 (cp_parser *, bool);
1351 static enum tree_code cp_parser_unary_operator
1353 static tree cp_parser_new_expression
1355 static tree cp_parser_new_placement
1357 static tree cp_parser_new_type_id
1359 static tree cp_parser_new_declarator_opt
1361 static tree cp_parser_direct_new_declarator
1363 static tree cp_parser_new_initializer
1365 static tree cp_parser_delete_expression
1367 static tree cp_parser_cast_expression
1368 (cp_parser *, bool);
1369 static tree cp_parser_pm_expression
1371 static tree cp_parser_multiplicative_expression
1373 static tree cp_parser_additive_expression
1375 static tree cp_parser_shift_expression
1377 static tree cp_parser_relational_expression
1379 static tree cp_parser_equality_expression
1381 static tree cp_parser_and_expression
1383 static tree cp_parser_exclusive_or_expression
1385 static tree cp_parser_inclusive_or_expression
1387 static tree cp_parser_logical_and_expression
1389 static tree cp_parser_logical_or_expression
1391 static tree cp_parser_question_colon_clause
1392 (cp_parser *, tree);
1393 static tree cp_parser_assignment_expression
1395 static enum tree_code cp_parser_assignment_operator_opt
1397 static tree cp_parser_expression
1399 static tree cp_parser_constant_expression
1400 (cp_parser *, bool, bool *);
1402 /* Statements [gram.stmt.stmt] */
1404 static void cp_parser_statement
1405 (cp_parser *, bool);
1406 static tree cp_parser_labeled_statement
1407 (cp_parser *, bool);
1408 static tree cp_parser_expression_statement
1409 (cp_parser *, bool);
1410 static tree cp_parser_compound_statement
1411 (cp_parser *, bool);
1412 static void cp_parser_statement_seq_opt
1413 (cp_parser *, bool);
1414 static tree cp_parser_selection_statement
1416 static tree cp_parser_condition
1418 static tree cp_parser_iteration_statement
1420 static void cp_parser_for_init_statement
1422 static tree cp_parser_jump_statement
1424 static void cp_parser_declaration_statement
1427 static tree cp_parser_implicitly_scoped_statement
1429 static void cp_parser_already_scoped_statement
1432 /* Declarations [gram.dcl.dcl] */
1434 static void cp_parser_declaration_seq_opt
1436 static void cp_parser_declaration
1438 static void cp_parser_block_declaration
1439 (cp_parser *, bool);
1440 static void cp_parser_simple_declaration
1441 (cp_parser *, bool);
1442 static tree cp_parser_decl_specifier_seq
1443 (cp_parser *, cp_parser_flags, tree *, int *);
1444 static tree cp_parser_storage_class_specifier_opt
1446 static tree cp_parser_function_specifier_opt
1448 static tree cp_parser_type_specifier
1449 (cp_parser *, cp_parser_flags, bool, bool, int *, bool *);
1450 static tree cp_parser_simple_type_specifier
1451 (cp_parser *, cp_parser_flags, bool);
1452 static tree cp_parser_type_name
1454 static tree cp_parser_elaborated_type_specifier
1455 (cp_parser *, bool, bool);
1456 static tree cp_parser_enum_specifier
1458 static void cp_parser_enumerator_list
1459 (cp_parser *, tree);
1460 static void cp_parser_enumerator_definition
1461 (cp_parser *, tree);
1462 static tree cp_parser_namespace_name
1464 static void cp_parser_namespace_definition
1466 static void cp_parser_namespace_body
1468 static tree cp_parser_qualified_namespace_specifier
1470 static void cp_parser_namespace_alias_definition
1472 static void cp_parser_using_declaration
1474 static void cp_parser_using_directive
1476 static void cp_parser_asm_definition
1478 static void cp_parser_linkage_specification
1481 /* Declarators [gram.dcl.decl] */
1483 static tree cp_parser_init_declarator
1484 (cp_parser *, tree, tree, bool, bool, int, bool *);
1485 static tree cp_parser_declarator
1486 (cp_parser *, cp_parser_declarator_kind, int *, bool *);
1487 static tree cp_parser_direct_declarator
1488 (cp_parser *, cp_parser_declarator_kind, int *);
1489 static enum tree_code cp_parser_ptr_operator
1490 (cp_parser *, tree *, tree *);
1491 static tree cp_parser_cv_qualifier_seq_opt
1493 static tree cp_parser_cv_qualifier_opt
1495 static tree cp_parser_declarator_id
1497 static tree cp_parser_type_id
1499 static tree cp_parser_type_specifier_seq
1501 static tree cp_parser_parameter_declaration_clause
1503 static tree cp_parser_parameter_declaration_list
1505 static tree cp_parser_parameter_declaration
1506 (cp_parser *, bool, bool *);
1507 static void cp_parser_function_body
1509 static tree cp_parser_initializer
1510 (cp_parser *, bool *, bool *);
1511 static tree cp_parser_initializer_clause
1512 (cp_parser *, bool *);
1513 static tree cp_parser_initializer_list
1514 (cp_parser *, bool *);
1516 static bool cp_parser_ctor_initializer_opt_and_function_body
1519 /* Classes [gram.class] */
1521 static tree cp_parser_class_name
1522 (cp_parser *, bool, bool, bool, bool, bool, bool);
1523 static tree cp_parser_class_specifier
1525 static tree cp_parser_class_head
1526 (cp_parser *, bool *, tree *);
1527 static enum tag_types cp_parser_class_key
1529 static void cp_parser_member_specification_opt
1531 static void cp_parser_member_declaration
1533 static tree cp_parser_pure_specifier
1535 static tree cp_parser_constant_initializer
1538 /* Derived classes [gram.class.derived] */
1540 static tree cp_parser_base_clause
1542 static tree cp_parser_base_specifier
1545 /* Special member functions [gram.special] */
1547 static tree cp_parser_conversion_function_id
1549 static tree cp_parser_conversion_type_id
1551 static tree cp_parser_conversion_declarator_opt
1553 static bool cp_parser_ctor_initializer_opt
1555 static void cp_parser_mem_initializer_list
1557 static tree cp_parser_mem_initializer
1559 static tree cp_parser_mem_initializer_id
1562 /* Overloading [gram.over] */
1564 static tree cp_parser_operator_function_id
1566 static tree cp_parser_operator
1569 /* Templates [gram.temp] */
1571 static void cp_parser_template_declaration
1572 (cp_parser *, bool);
1573 static tree cp_parser_template_parameter_list
1575 static tree cp_parser_template_parameter
1577 static tree cp_parser_type_parameter
1579 static tree cp_parser_template_id
1580 (cp_parser *, bool, bool, bool);
1581 static tree cp_parser_template_name
1582 (cp_parser *, bool, bool, bool, bool *);
1583 static tree cp_parser_template_argument_list
1585 static tree cp_parser_template_argument
1587 static void cp_parser_explicit_instantiation
1589 static void cp_parser_explicit_specialization
1592 /* Exception handling [gram.exception] */
1594 static tree cp_parser_try_block
1596 static bool cp_parser_function_try_block
1598 static void cp_parser_handler_seq
1600 static void cp_parser_handler
1602 static tree cp_parser_exception_declaration
1604 static tree cp_parser_throw_expression
1606 static tree cp_parser_exception_specification_opt
1608 static tree cp_parser_type_id_list
1611 /* GNU Extensions */
1613 static tree cp_parser_asm_specification_opt
1615 static tree cp_parser_asm_operand_list
1617 static tree cp_parser_asm_clobber_list
1619 static tree cp_parser_attributes_opt
1621 static tree cp_parser_attribute_list
1623 static bool cp_parser_extension_opt
1624 (cp_parser *, int *);
1625 static void cp_parser_label_declaration
1628 /* Utility Routines */
1630 static tree cp_parser_lookup_name
1631 (cp_parser *, tree, bool, bool, bool, bool);
1632 static tree cp_parser_lookup_name_simple
1633 (cp_parser *, tree);
1634 static tree cp_parser_maybe_treat_template_as_class
1636 static bool cp_parser_check_declarator_template_parameters
1637 (cp_parser *, tree);
1638 static bool cp_parser_check_template_parameters
1639 (cp_parser *, unsigned);
1640 static tree cp_parser_simple_cast_expression
1642 static tree cp_parser_binary_expression
1643 (cp_parser *, const cp_parser_token_tree_map, cp_parser_expression_fn);
1644 static tree cp_parser_global_scope_opt
1645 (cp_parser *, bool);
1646 static bool cp_parser_constructor_declarator_p
1647 (cp_parser *, bool);
1648 static tree cp_parser_function_definition_from_specifiers_and_declarator
1649 (cp_parser *, tree, tree, tree);
1650 static tree cp_parser_function_definition_after_declarator
1651 (cp_parser *, bool);
1652 static void cp_parser_template_declaration_after_export
1653 (cp_parser *, bool);
1654 static tree cp_parser_single_declaration
1655 (cp_parser *, bool, bool *);
1656 static tree cp_parser_functional_cast
1657 (cp_parser *, tree);
1658 static tree cp_parser_save_member_function_body
1659 (cp_parser *, tree, tree, tree);
1660 static tree cp_parser_enclosed_template_argument_list
1662 static void cp_parser_save_default_args
1663 (cp_parser *, tree);
1664 static void cp_parser_late_parsing_for_member
1665 (cp_parser *, tree);
1666 static void cp_parser_late_parsing_default_args
1667 (cp_parser *, tree);
1668 static tree cp_parser_sizeof_operand
1669 (cp_parser *, enum rid);
1670 static bool cp_parser_declares_only_class_p
1672 static bool cp_parser_friend_p
1674 static cp_token *cp_parser_require
1675 (cp_parser *, enum cpp_ttype, const char *);
1676 static cp_token *cp_parser_require_keyword
1677 (cp_parser *, enum rid, const char *);
1678 static bool cp_parser_token_starts_function_definition_p
1680 static bool cp_parser_next_token_starts_class_definition_p
1682 static bool cp_parser_next_token_ends_template_argument_p
1684 static bool cp_parser_nth_token_starts_template_argument_list_p
1685 (cp_parser *, size_t);
1686 static enum tag_types cp_parser_token_is_class_key
1688 static void cp_parser_check_class_key
1689 (enum tag_types, tree type);
1690 static void cp_parser_check_access_in_redeclaration
1692 static bool cp_parser_optional_template_keyword
1694 static void cp_parser_pre_parsed_nested_name_specifier
1696 static void cp_parser_cache_group
1697 (cp_parser *, cp_token_cache *, enum cpp_ttype, unsigned);
1698 static void cp_parser_parse_tentatively
1700 static void cp_parser_commit_to_tentative_parse
1702 static void cp_parser_abort_tentative_parse
1704 static bool cp_parser_parse_definitely
1706 static inline bool cp_parser_parsing_tentatively
1708 static bool cp_parser_committed_to_tentative_parse
1710 static void cp_parser_error
1711 (cp_parser *, const char *);
1712 static void cp_parser_name_lookup_error
1713 (cp_parser *, tree, tree, const char *);
1714 static bool cp_parser_simulate_error
1716 static void cp_parser_check_type_definition
1718 static void cp_parser_check_for_definition_in_return_type
1720 static void cp_parser_check_for_invalid_template_id
1721 (cp_parser *, tree);
1722 static bool cp_parser_non_integral_constant_expression
1723 (cp_parser *, const char *);
1724 static bool cp_parser_diagnose_invalid_type_name
1726 static int cp_parser_skip_to_closing_parenthesis
1727 (cp_parser *, bool, bool, bool);
1728 static void cp_parser_skip_to_end_of_statement
1730 static void cp_parser_consume_semicolon_at_end_of_statement
1732 static void cp_parser_skip_to_end_of_block_or_statement
1734 static void cp_parser_skip_to_closing_brace
1736 static void cp_parser_skip_until_found
1737 (cp_parser *, enum cpp_ttype, const char *);
1738 static bool cp_parser_error_occurred
1740 static bool cp_parser_allow_gnu_extensions_p
1742 static bool cp_parser_is_string_literal
1744 static bool cp_parser_is_keyword
1745 (cp_token *, enum rid);
1747 /* Returns nonzero if we are parsing tentatively. */
1750 cp_parser_parsing_tentatively (cp_parser* parser)
1752 return parser->context->next != NULL;
1755 /* Returns nonzero if TOKEN is a string literal. */
1758 cp_parser_is_string_literal (cp_token* token)
1760 return (token->type == CPP_STRING || token->type == CPP_WSTRING);
1763 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
1766 cp_parser_is_keyword (cp_token* token, enum rid keyword)
1768 return token->keyword == keyword;
1771 /* Issue the indicated error MESSAGE. */
1774 cp_parser_error (cp_parser* parser, const char* message)
1776 /* Output the MESSAGE -- unless we're parsing tentatively. */
1777 if (!cp_parser_simulate_error (parser))
1780 token = cp_lexer_peek_token (parser->lexer);
1781 c_parse_error (message,
1782 /* Because c_parser_error does not understand
1783 CPP_KEYWORD, keywords are treated like
1785 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
1790 /* Issue an error about name-lookup failing. NAME is the
1791 IDENTIFIER_NODE DECL is the result of
1792 the lookup (as returned from cp_parser_lookup_name). DESIRED is
1793 the thing that we hoped to find. */
1796 cp_parser_name_lookup_error (cp_parser* parser,
1799 const char* desired)
1801 /* If name lookup completely failed, tell the user that NAME was not
1803 if (decl == error_mark_node)
1805 if (parser->scope && parser->scope != global_namespace)
1806 error ("`%D::%D' has not been declared",
1807 parser->scope, name);
1808 else if (parser->scope == global_namespace)
1809 error ("`::%D' has not been declared", name);
1811 error ("`%D' has not been declared", name);
1813 else if (parser->scope && parser->scope != global_namespace)
1814 error ("`%D::%D' %s", parser->scope, name, desired);
1815 else if (parser->scope == global_namespace)
1816 error ("`::%D' %s", name, desired);
1818 error ("`%D' %s", name, desired);
1821 /* If we are parsing tentatively, remember that an error has occurred
1822 during this tentative parse. Returns true if the error was
1823 simulated; false if a messgae should be issued by the caller. */
1826 cp_parser_simulate_error (cp_parser* parser)
1828 if (cp_parser_parsing_tentatively (parser)
1829 && !cp_parser_committed_to_tentative_parse (parser))
1831 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
1837 /* This function is called when a type is defined. If type
1838 definitions are forbidden at this point, an error message is
1842 cp_parser_check_type_definition (cp_parser* parser)
1844 /* If types are forbidden here, issue a message. */
1845 if (parser->type_definition_forbidden_message)
1846 /* Use `%s' to print the string in case there are any escape
1847 characters in the message. */
1848 error ("%s", parser->type_definition_forbidden_message);
1851 /* This function is called when a declaration is parsed. If
1852 DECLARATOR is a function declarator and DECLARES_CLASS_OR_ENUM
1853 indicates that a type was defined in the decl-specifiers for DECL,
1854 then an error is issued. */
1857 cp_parser_check_for_definition_in_return_type (tree declarator,
1858 int declares_class_or_enum)
1860 /* [dcl.fct] forbids type definitions in return types.
1861 Unfortunately, it's not easy to know whether or not we are
1862 processing a return type until after the fact. */
1864 && (TREE_CODE (declarator) == INDIRECT_REF
1865 || TREE_CODE (declarator) == ADDR_EXPR))
1866 declarator = TREE_OPERAND (declarator, 0);
1868 && TREE_CODE (declarator) == CALL_EXPR
1869 && declares_class_or_enum & 2)
1870 error ("new types may not be defined in a return type");
1873 /* A type-specifier (TYPE) has been parsed which cannot be followed by
1874 "<" in any valid C++ program. If the next token is indeed "<",
1875 issue a message warning the user about what appears to be an
1876 invalid attempt to form a template-id. */
1879 cp_parser_check_for_invalid_template_id (cp_parser* parser,
1885 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
1888 error ("`%T' is not a template", type);
1889 else if (TREE_CODE (type) == IDENTIFIER_NODE)
1890 error ("`%s' is not a template", IDENTIFIER_POINTER (type));
1892 error ("invalid template-id");
1893 /* Remember the location of the invalid "<". */
1894 if (cp_parser_parsing_tentatively (parser)
1895 && !cp_parser_committed_to_tentative_parse (parser))
1897 token = cp_lexer_peek_token (parser->lexer);
1898 token = cp_lexer_prev_token (parser->lexer, token);
1899 start = cp_lexer_token_difference (parser->lexer,
1900 parser->lexer->first_token,
1905 /* Consume the "<". */
1906 cp_lexer_consume_token (parser->lexer);
1907 /* Parse the template arguments. */
1908 cp_parser_enclosed_template_argument_list (parser);
1909 /* Permanently remove the invalid template arguments so that
1910 this error message is not issued again. */
1913 token = cp_lexer_advance_token (parser->lexer,
1914 parser->lexer->first_token,
1916 cp_lexer_purge_tokens_after (parser->lexer, token);
1921 /* If parsing an integral constant-expression, issue an error message
1922 about the fact that THING appeared and return true. Otherwise,
1923 return false, marking the current expression as non-constant. */
1926 cp_parser_non_integral_constant_expression (cp_parser *parser,
1929 if (parser->integral_constant_expression_p)
1931 if (!parser->allow_non_integral_constant_expression_p)
1933 error ("%s cannot appear in a constant-expression", thing);
1936 parser->non_integral_constant_expression_p = true;
1941 /* Check for a common situation where a type-name should be present,
1942 but is not, and issue a sensible error message. Returns true if an
1943 invalid type-name was detected. */
1946 cp_parser_diagnose_invalid_type_name (cp_parser *parser)
1948 /* If the next two tokens are both identifiers, the code is
1949 erroneous. The usual cause of this situation is code like:
1953 where "T" should name a type -- but does not. */
1954 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
1955 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
1959 /* If parsing tentatively, we should commit; we really are
1960 looking at a declaration. */
1961 /* Consume the first identifier. */
1962 name = cp_lexer_consume_token (parser->lexer)->value;
1963 /* Issue an error message. */
1964 error ("`%s' does not name a type", IDENTIFIER_POINTER (name));
1965 /* If we're in a template class, it's possible that the user was
1966 referring to a type from a base class. For example:
1968 template <typename T> struct A { typedef T X; };
1969 template <typename T> struct B : public A<T> { X x; };
1971 The user should have said "typename A<T>::X". */
1972 if (processing_template_decl && current_class_type)
1976 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
1980 tree base_type = BINFO_TYPE (b);
1981 if (CLASS_TYPE_P (base_type)
1982 && dependent_type_p (base_type))
1985 /* Go from a particular instantiation of the
1986 template (which will have an empty TYPE_FIELDs),
1987 to the main version. */
1988 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
1989 for (field = TYPE_FIELDS (base_type);
1991 field = TREE_CHAIN (field))
1992 if (TREE_CODE (field) == TYPE_DECL
1993 && DECL_NAME (field) == name)
1995 error ("(perhaps `typename %T::%s' was intended)",
1996 BINFO_TYPE (b), IDENTIFIER_POINTER (name));
2004 /* Skip to the end of the declaration; there's no point in
2005 trying to process it. */
2006 cp_parser_skip_to_end_of_statement (parser);
2014 /* Consume tokens up to, and including, the next non-nested closing `)'.
2015 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
2016 are doing error recovery. Returns -1 if OR_COMMA is true and we
2017 found an unnested comma. */
2020 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2025 unsigned paren_depth = 0;
2026 unsigned brace_depth = 0;
2028 if (recovering && !or_comma && cp_parser_parsing_tentatively (parser)
2029 && !cp_parser_committed_to_tentative_parse (parser))
2036 /* If we've run out of tokens, then there is no closing `)'. */
2037 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2040 token = cp_lexer_peek_token (parser->lexer);
2042 /* This matches the processing in skip_to_end_of_statement. */
2043 if (token->type == CPP_SEMICOLON && !brace_depth)
2045 if (token->type == CPP_OPEN_BRACE)
2047 if (token->type == CPP_CLOSE_BRACE)
2052 if (recovering && or_comma && token->type == CPP_COMMA
2053 && !brace_depth && !paren_depth)
2058 /* If it is an `(', we have entered another level of nesting. */
2059 if (token->type == CPP_OPEN_PAREN)
2061 /* If it is a `)', then we might be done. */
2062 else if (token->type == CPP_CLOSE_PAREN && !paren_depth--)
2065 cp_lexer_consume_token (parser->lexer);
2070 /* Consume the token. */
2071 cp_lexer_consume_token (parser->lexer);
2075 /* Consume tokens until we reach the end of the current statement.
2076 Normally, that will be just before consuming a `;'. However, if a
2077 non-nested `}' comes first, then we stop before consuming that. */
2080 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2082 unsigned nesting_depth = 0;
2088 /* Peek at the next token. */
2089 token = cp_lexer_peek_token (parser->lexer);
2090 /* If we've run out of tokens, stop. */
2091 if (token->type == CPP_EOF)
2093 /* If the next token is a `;', we have reached the end of the
2095 if (token->type == CPP_SEMICOLON && !nesting_depth)
2097 /* If the next token is a non-nested `}', then we have reached
2098 the end of the current block. */
2099 if (token->type == CPP_CLOSE_BRACE)
2101 /* If this is a non-nested `}', stop before consuming it.
2102 That way, when confronted with something like:
2106 we stop before consuming the closing `}', even though we
2107 have not yet reached a `;'. */
2108 if (nesting_depth == 0)
2110 /* If it is the closing `}' for a block that we have
2111 scanned, stop -- but only after consuming the token.
2117 we will stop after the body of the erroneously declared
2118 function, but before consuming the following `typedef'
2120 if (--nesting_depth == 0)
2122 cp_lexer_consume_token (parser->lexer);
2126 /* If it the next token is a `{', then we are entering a new
2127 block. Consume the entire block. */
2128 else if (token->type == CPP_OPEN_BRACE)
2130 /* Consume the token. */
2131 cp_lexer_consume_token (parser->lexer);
2135 /* This function is called at the end of a statement or declaration.
2136 If the next token is a semicolon, it is consumed; otherwise, error
2137 recovery is attempted. */
2140 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2142 /* Look for the trailing `;'. */
2143 if (!cp_parser_require (parser, CPP_SEMICOLON, "`;'"))
2145 /* If there is additional (erroneous) input, skip to the end of
2147 cp_parser_skip_to_end_of_statement (parser);
2148 /* If the next token is now a `;', consume it. */
2149 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2150 cp_lexer_consume_token (parser->lexer);
2154 /* Skip tokens until we have consumed an entire block, or until we
2155 have consumed a non-nested `;'. */
2158 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2160 unsigned nesting_depth = 0;
2166 /* Peek at the next token. */
2167 token = cp_lexer_peek_token (parser->lexer);
2168 /* If we've run out of tokens, stop. */
2169 if (token->type == CPP_EOF)
2171 /* If the next token is a `;', we have reached the end of the
2173 if (token->type == CPP_SEMICOLON && !nesting_depth)
2175 /* Consume the `;'. */
2176 cp_lexer_consume_token (parser->lexer);
2179 /* Consume the token. */
2180 token = cp_lexer_consume_token (parser->lexer);
2181 /* If the next token is a non-nested `}', then we have reached
2182 the end of the current block. */
2183 if (token->type == CPP_CLOSE_BRACE
2184 && (nesting_depth == 0 || --nesting_depth == 0))
2186 /* If it the next token is a `{', then we are entering a new
2187 block. Consume the entire block. */
2188 if (token->type == CPP_OPEN_BRACE)
2193 /* Skip tokens until a non-nested closing curly brace is the next
2197 cp_parser_skip_to_closing_brace (cp_parser *parser)
2199 unsigned nesting_depth = 0;
2205 /* Peek at the next token. */
2206 token = cp_lexer_peek_token (parser->lexer);
2207 /* If we've run out of tokens, stop. */
2208 if (token->type == CPP_EOF)
2210 /* If the next token is a non-nested `}', then we have reached
2211 the end of the current block. */
2212 if (token->type == CPP_CLOSE_BRACE && nesting_depth-- == 0)
2214 /* If it the next token is a `{', then we are entering a new
2215 block. Consume the entire block. */
2216 else if (token->type == CPP_OPEN_BRACE)
2218 /* Consume the token. */
2219 cp_lexer_consume_token (parser->lexer);
2223 /* Create a new C++ parser. */
2226 cp_parser_new (void)
2231 /* cp_lexer_new_main is called before calling ggc_alloc because
2232 cp_lexer_new_main might load a PCH file. */
2233 lexer = cp_lexer_new_main ();
2235 parser = ggc_alloc_cleared (sizeof (cp_parser));
2236 parser->lexer = lexer;
2237 parser->context = cp_parser_context_new (NULL);
2239 /* For now, we always accept GNU extensions. */
2240 parser->allow_gnu_extensions_p = 1;
2242 /* The `>' token is a greater-than operator, not the end of a
2244 parser->greater_than_is_operator_p = true;
2246 parser->default_arg_ok_p = true;
2248 /* We are not parsing a constant-expression. */
2249 parser->integral_constant_expression_p = false;
2250 parser->allow_non_integral_constant_expression_p = false;
2251 parser->non_integral_constant_expression_p = false;
2253 /* We are not parsing offsetof. */
2254 parser->in_offsetof_p = false;
2256 /* Local variable names are not forbidden. */
2257 parser->local_variables_forbidden_p = false;
2259 /* We are not processing an `extern "C"' declaration. */
2260 parser->in_unbraced_linkage_specification_p = false;
2262 /* We are not processing a declarator. */
2263 parser->in_declarator_p = false;
2265 /* We are not processing a template-argument-list. */
2266 parser->in_template_argument_list_p = false;
2268 /* We are not in an iteration statement. */
2269 parser->in_iteration_statement_p = false;
2271 /* We are not in a switch statement. */
2272 parser->in_switch_statement_p = false;
2274 /* We are not parsing a type-id inside an expression. */
2275 parser->in_type_id_in_expr_p = false;
2277 /* The unparsed function queue is empty. */
2278 parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2280 /* There are no classes being defined. */
2281 parser->num_classes_being_defined = 0;
2283 /* No template parameters apply. */
2284 parser->num_template_parameter_lists = 0;
2289 /* Lexical conventions [gram.lex] */
2291 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
2295 cp_parser_identifier (cp_parser* parser)
2299 /* Look for the identifier. */
2300 token = cp_parser_require (parser, CPP_NAME, "identifier");
2301 /* Return the value. */
2302 return token ? token->value : error_mark_node;
2305 /* Basic concepts [gram.basic] */
2307 /* Parse a translation-unit.
2310 declaration-seq [opt]
2312 Returns TRUE if all went well. */
2315 cp_parser_translation_unit (cp_parser* parser)
2319 cp_parser_declaration_seq_opt (parser);
2321 /* If there are no tokens left then all went well. */
2322 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2325 /* Otherwise, issue an error message. */
2326 cp_parser_error (parser, "expected declaration");
2330 /* Consume the EOF token. */
2331 cp_parser_require (parser, CPP_EOF, "end-of-file");
2334 finish_translation_unit ();
2336 /* All went well. */
2340 /* Expressions [gram.expr] */
2342 /* Parse a primary-expression.
2353 ( compound-statement )
2354 __builtin_va_arg ( assignment-expression , type-id )
2359 Returns a representation of the expression.
2361 *IDK indicates what kind of id-expression (if any) was present.
2363 *QUALIFYING_CLASS is set to a non-NULL value if the id-expression can be
2364 used as the operand of a pointer-to-member. In that case,
2365 *QUALIFYING_CLASS gives the class that is used as the qualifying
2366 class in the pointer-to-member. */
2369 cp_parser_primary_expression (cp_parser *parser,
2371 tree *qualifying_class)
2375 /* Assume the primary expression is not an id-expression. */
2376 *idk = CP_ID_KIND_NONE;
2377 /* And that it cannot be used as pointer-to-member. */
2378 *qualifying_class = NULL_TREE;
2380 /* Peek at the next token. */
2381 token = cp_lexer_peek_token (parser->lexer);
2382 switch (token->type)
2395 token = cp_lexer_consume_token (parser->lexer);
2396 return token->value;
2398 case CPP_OPEN_PAREN:
2401 bool saved_greater_than_is_operator_p;
2403 /* Consume the `('. */
2404 cp_lexer_consume_token (parser->lexer);
2405 /* Within a parenthesized expression, a `>' token is always
2406 the greater-than operator. */
2407 saved_greater_than_is_operator_p
2408 = parser->greater_than_is_operator_p;
2409 parser->greater_than_is_operator_p = true;
2410 /* If we see `( { ' then we are looking at the beginning of
2411 a GNU statement-expression. */
2412 if (cp_parser_allow_gnu_extensions_p (parser)
2413 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
2415 /* Statement-expressions are not allowed by the standard. */
2417 pedwarn ("ISO C++ forbids braced-groups within expressions");
2419 /* And they're not allowed outside of a function-body; you
2420 cannot, for example, write:
2422 int i = ({ int j = 3; j + 1; });
2424 at class or namespace scope. */
2425 if (!at_function_scope_p ())
2426 error ("statement-expressions are allowed only inside functions");
2427 /* Start the statement-expression. */
2428 expr = begin_stmt_expr ();
2429 /* Parse the compound-statement. */
2430 cp_parser_compound_statement (parser, true);
2432 expr = finish_stmt_expr (expr, false);
2436 /* Parse the parenthesized expression. */
2437 expr = cp_parser_expression (parser);
2438 /* Let the front end know that this expression was
2439 enclosed in parentheses. This matters in case, for
2440 example, the expression is of the form `A::B', since
2441 `&A::B' might be a pointer-to-member, but `&(A::B)' is
2443 finish_parenthesized_expr (expr);
2445 /* The `>' token might be the end of a template-id or
2446 template-parameter-list now. */
2447 parser->greater_than_is_operator_p
2448 = saved_greater_than_is_operator_p;
2449 /* Consume the `)'. */
2450 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
2451 cp_parser_skip_to_end_of_statement (parser);
2457 switch (token->keyword)
2459 /* These two are the boolean literals. */
2461 cp_lexer_consume_token (parser->lexer);
2462 return boolean_true_node;
2464 cp_lexer_consume_token (parser->lexer);
2465 return boolean_false_node;
2467 /* The `__null' literal. */
2469 cp_lexer_consume_token (parser->lexer);
2472 /* Recognize the `this' keyword. */
2474 cp_lexer_consume_token (parser->lexer);
2475 if (parser->local_variables_forbidden_p)
2477 error ("`this' may not be used in this context");
2478 return error_mark_node;
2480 /* Pointers cannot appear in constant-expressions. */
2481 if (cp_parser_non_integral_constant_expression (parser,
2483 return error_mark_node;
2484 return finish_this_expr ();
2486 /* The `operator' keyword can be the beginning of an
2491 case RID_FUNCTION_NAME:
2492 case RID_PRETTY_FUNCTION_NAME:
2493 case RID_C99_FUNCTION_NAME:
2494 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
2495 __func__ are the names of variables -- but they are
2496 treated specially. Therefore, they are handled here,
2497 rather than relying on the generic id-expression logic
2498 below. Grammatically, these names are id-expressions.
2500 Consume the token. */
2501 token = cp_lexer_consume_token (parser->lexer);
2502 /* Look up the name. */
2503 return finish_fname (token->value);
2510 /* The `__builtin_va_arg' construct is used to handle
2511 `va_arg'. Consume the `__builtin_va_arg' token. */
2512 cp_lexer_consume_token (parser->lexer);
2513 /* Look for the opening `('. */
2514 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
2515 /* Now, parse the assignment-expression. */
2516 expression = cp_parser_assignment_expression (parser);
2517 /* Look for the `,'. */
2518 cp_parser_require (parser, CPP_COMMA, "`,'");
2519 /* Parse the type-id. */
2520 type = cp_parser_type_id (parser);
2521 /* Look for the closing `)'. */
2522 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
2523 /* Using `va_arg' in a constant-expression is not
2525 if (cp_parser_non_integral_constant_expression (parser,
2527 return error_mark_node;
2528 return build_x_va_arg (expression, type);
2534 bool saved_in_offsetof_p;
2536 /* Consume the "__offsetof__" token. */
2537 cp_lexer_consume_token (parser->lexer);
2538 /* Consume the opening `('. */
2539 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
2540 /* Parse the parenthesized (almost) constant-expression. */
2541 saved_in_offsetof_p = parser->in_offsetof_p;
2542 parser->in_offsetof_p = true;
2544 = cp_parser_constant_expression (parser,
2545 /*allow_non_constant_p=*/false,
2546 /*non_constant_p=*/NULL);
2547 parser->in_offsetof_p = saved_in_offsetof_p;
2548 /* Consume the closing ')'. */
2549 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
2555 cp_parser_error (parser, "expected primary-expression");
2556 return error_mark_node;
2559 /* An id-expression can start with either an identifier, a
2560 `::' as the beginning of a qualified-id, or the "operator"
2564 case CPP_TEMPLATE_ID:
2565 case CPP_NESTED_NAME_SPECIFIER:
2569 const char *error_msg;
2572 /* Parse the id-expression. */
2574 = cp_parser_id_expression (parser,
2575 /*template_keyword_p=*/false,
2576 /*check_dependency_p=*/true,
2577 /*template_p=*/NULL,
2578 /*declarator_p=*/false);
2579 if (id_expression == error_mark_node)
2580 return error_mark_node;
2581 /* If we have a template-id, then no further lookup is
2582 required. If the template-id was for a template-class, we
2583 will sometimes have a TYPE_DECL at this point. */
2584 else if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
2585 || TREE_CODE (id_expression) == TYPE_DECL)
2586 decl = id_expression;
2587 /* Look up the name. */
2590 decl = cp_parser_lookup_name_simple (parser, id_expression);
2591 /* If name lookup gives us a SCOPE_REF, then the
2592 qualifying scope was dependent. Just propagate the
2594 if (TREE_CODE (decl) == SCOPE_REF)
2596 if (TYPE_P (TREE_OPERAND (decl, 0)))
2597 *qualifying_class = TREE_OPERAND (decl, 0);
2600 /* Check to see if DECL is a local variable in a context
2601 where that is forbidden. */
2602 if (parser->local_variables_forbidden_p
2603 && local_variable_p (decl))
2605 /* It might be that we only found DECL because we are
2606 trying to be generous with pre-ISO scoping rules.
2607 For example, consider:
2611 for (int i = 0; i < 10; ++i) {}
2612 extern void f(int j = i);
2615 Here, name look up will originally find the out
2616 of scope `i'. We need to issue a warning message,
2617 but then use the global `i'. */
2618 decl = check_for_out_of_scope_variable (decl);
2619 if (local_variable_p (decl))
2621 error ("local variable `%D' may not appear in this context",
2623 return error_mark_node;
2628 decl = finish_id_expression (id_expression, decl, parser->scope,
2629 idk, qualifying_class,
2630 parser->integral_constant_expression_p,
2631 parser->allow_non_integral_constant_expression_p,
2632 &parser->non_integral_constant_expression_p,
2635 cp_parser_error (parser, error_msg);
2639 /* Anything else is an error. */
2641 cp_parser_error (parser, "expected primary-expression");
2642 return error_mark_node;
2646 /* Parse an id-expression.
2653 :: [opt] nested-name-specifier template [opt] unqualified-id
2655 :: operator-function-id
2658 Return a representation of the unqualified portion of the
2659 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
2660 a `::' or nested-name-specifier.
2662 Often, if the id-expression was a qualified-id, the caller will
2663 want to make a SCOPE_REF to represent the qualified-id. This
2664 function does not do this in order to avoid wastefully creating
2665 SCOPE_REFs when they are not required.
2667 If TEMPLATE_KEYWORD_P is true, then we have just seen the
2670 If CHECK_DEPENDENCY_P is false, then names are looked up inside
2671 uninstantiated templates.
2673 If *TEMPLATE_P is non-NULL, it is set to true iff the
2674 `template' keyword is used to explicitly indicate that the entity
2675 named is a template.
2677 If DECLARATOR_P is true, the id-expression is appearing as part of
2678 a declarator, rather than as part of an expression. */
2681 cp_parser_id_expression (cp_parser *parser,
2682 bool template_keyword_p,
2683 bool check_dependency_p,
2687 bool global_scope_p;
2688 bool nested_name_specifier_p;
2690 /* Assume the `template' keyword was not used. */
2692 *template_p = false;
2694 /* Look for the optional `::' operator. */
2696 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
2698 /* Look for the optional nested-name-specifier. */
2699 nested_name_specifier_p
2700 = (cp_parser_nested_name_specifier_opt (parser,
2701 /*typename_keyword_p=*/false,
2704 /*is_declarator=*/false)
2706 /* If there is a nested-name-specifier, then we are looking at
2707 the first qualified-id production. */
2708 if (nested_name_specifier_p)
2711 tree saved_object_scope;
2712 tree saved_qualifying_scope;
2713 tree unqualified_id;
2716 /* See if the next token is the `template' keyword. */
2718 template_p = &is_template;
2719 *template_p = cp_parser_optional_template_keyword (parser);
2720 /* Name lookup we do during the processing of the
2721 unqualified-id might obliterate SCOPE. */
2722 saved_scope = parser->scope;
2723 saved_object_scope = parser->object_scope;
2724 saved_qualifying_scope = parser->qualifying_scope;
2725 /* Process the final unqualified-id. */
2726 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
2729 /* Restore the SAVED_SCOPE for our caller. */
2730 parser->scope = saved_scope;
2731 parser->object_scope = saved_object_scope;
2732 parser->qualifying_scope = saved_qualifying_scope;
2734 return unqualified_id;
2736 /* Otherwise, if we are in global scope, then we are looking at one
2737 of the other qualified-id productions. */
2738 else if (global_scope_p)
2743 /* Peek at the next token. */
2744 token = cp_lexer_peek_token (parser->lexer);
2746 /* If it's an identifier, and the next token is not a "<", then
2747 we can avoid the template-id case. This is an optimization
2748 for this common case. */
2749 if (token->type == CPP_NAME
2750 && !cp_parser_nth_token_starts_template_argument_list_p
2752 return cp_parser_identifier (parser);
2754 cp_parser_parse_tentatively (parser);
2755 /* Try a template-id. */
2756 id = cp_parser_template_id (parser,
2757 /*template_keyword_p=*/false,
2758 /*check_dependency_p=*/true,
2760 /* If that worked, we're done. */
2761 if (cp_parser_parse_definitely (parser))
2764 /* Peek at the next token. (Changes in the token buffer may
2765 have invalidated the pointer obtained above.) */
2766 token = cp_lexer_peek_token (parser->lexer);
2768 switch (token->type)
2771 return cp_parser_identifier (parser);
2774 if (token->keyword == RID_OPERATOR)
2775 return cp_parser_operator_function_id (parser);
2779 cp_parser_error (parser, "expected id-expression");
2780 return error_mark_node;
2784 return cp_parser_unqualified_id (parser, template_keyword_p,
2785 /*check_dependency_p=*/true,
2789 /* Parse an unqualified-id.
2793 operator-function-id
2794 conversion-function-id
2798 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
2799 keyword, in a construct like `A::template ...'.
2801 Returns a representation of unqualified-id. For the `identifier'
2802 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
2803 production a BIT_NOT_EXPR is returned; the operand of the
2804 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
2805 other productions, see the documentation accompanying the
2806 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
2807 names are looked up in uninstantiated templates. If DECLARATOR_P
2808 is true, the unqualified-id is appearing as part of a declarator,
2809 rather than as part of an expression. */
2812 cp_parser_unqualified_id (cp_parser* parser,
2813 bool template_keyword_p,
2814 bool check_dependency_p,
2819 /* Peek at the next token. */
2820 token = cp_lexer_peek_token (parser->lexer);
2822 switch (token->type)
2828 /* We don't know yet whether or not this will be a
2830 cp_parser_parse_tentatively (parser);
2831 /* Try a template-id. */
2832 id = cp_parser_template_id (parser, template_keyword_p,
2835 /* If it worked, we're done. */
2836 if (cp_parser_parse_definitely (parser))
2838 /* Otherwise, it's an ordinary identifier. */
2839 return cp_parser_identifier (parser);
2842 case CPP_TEMPLATE_ID:
2843 return cp_parser_template_id (parser, template_keyword_p,
2850 tree qualifying_scope;
2854 /* Consume the `~' token. */
2855 cp_lexer_consume_token (parser->lexer);
2856 /* Parse the class-name. The standard, as written, seems to
2859 template <typename T> struct S { ~S (); };
2860 template <typename T> S<T>::~S() {}
2862 is invalid, since `~' must be followed by a class-name, but
2863 `S<T>' is dependent, and so not known to be a class.
2864 That's not right; we need to look in uninstantiated
2865 templates. A further complication arises from:
2867 template <typename T> void f(T t) {
2871 Here, it is not possible to look up `T' in the scope of `T'
2872 itself. We must look in both the current scope, and the
2873 scope of the containing complete expression.
2875 Yet another issue is:
2884 The standard does not seem to say that the `S' in `~S'
2885 should refer to the type `S' and not the data member
2888 /* DR 244 says that we look up the name after the "~" in the
2889 same scope as we looked up the qualifying name. That idea
2890 isn't fully worked out; it's more complicated than that. */
2891 scope = parser->scope;
2892 object_scope = parser->object_scope;
2893 qualifying_scope = parser->qualifying_scope;
2895 /* If the name is of the form "X::~X" it's OK. */
2896 if (scope && TYPE_P (scope)
2897 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
2898 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
2900 && (cp_lexer_peek_token (parser->lexer)->value
2901 == TYPE_IDENTIFIER (scope)))
2903 cp_lexer_consume_token (parser->lexer);
2904 return build_nt (BIT_NOT_EXPR, scope);
2907 /* If there was an explicit qualification (S::~T), first look
2908 in the scope given by the qualification (i.e., S). */
2911 cp_parser_parse_tentatively (parser);
2912 type_decl = cp_parser_class_name (parser,
2913 /*typename_keyword_p=*/false,
2914 /*template_keyword_p=*/false,
2916 /*check_dependency=*/false,
2917 /*class_head_p=*/false,
2919 if (cp_parser_parse_definitely (parser))
2920 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
2922 /* In "N::S::~S", look in "N" as well. */
2923 if (scope && qualifying_scope)
2925 cp_parser_parse_tentatively (parser);
2926 parser->scope = qualifying_scope;
2927 parser->object_scope = NULL_TREE;
2928 parser->qualifying_scope = NULL_TREE;
2930 = cp_parser_class_name (parser,
2931 /*typename_keyword_p=*/false,
2932 /*template_keyword_p=*/false,
2934 /*check_dependency=*/false,
2935 /*class_head_p=*/false,
2937 if (cp_parser_parse_definitely (parser))
2938 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
2940 /* In "p->S::~T", look in the scope given by "*p" as well. */
2941 else if (object_scope)
2943 cp_parser_parse_tentatively (parser);
2944 parser->scope = object_scope;
2945 parser->object_scope = NULL_TREE;
2946 parser->qualifying_scope = NULL_TREE;
2948 = cp_parser_class_name (parser,
2949 /*typename_keyword_p=*/false,
2950 /*template_keyword_p=*/false,
2952 /*check_dependency=*/false,
2953 /*class_head_p=*/false,
2955 if (cp_parser_parse_definitely (parser))
2956 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
2958 /* Look in the surrounding context. */
2959 parser->scope = NULL_TREE;
2960 parser->object_scope = NULL_TREE;
2961 parser->qualifying_scope = NULL_TREE;
2963 = cp_parser_class_name (parser,
2964 /*typename_keyword_p=*/false,
2965 /*template_keyword_p=*/false,
2967 /*check_dependency=*/false,
2968 /*class_head_p=*/false,
2970 /* If an error occurred, assume that the name of the
2971 destructor is the same as the name of the qualifying
2972 class. That allows us to keep parsing after running
2973 into ill-formed destructor names. */
2974 if (type_decl == error_mark_node && scope && TYPE_P (scope))
2975 return build_nt (BIT_NOT_EXPR, scope);
2976 else if (type_decl == error_mark_node)
2977 return error_mark_node;
2981 A typedef-name that names a class shall not be used as the
2982 identifier in the declarator for a destructor declaration. */
2984 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
2985 && !DECL_SELF_REFERENCE_P (type_decl))
2986 error ("typedef-name `%D' used as destructor declarator",
2989 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
2993 if (token->keyword == RID_OPERATOR)
2997 /* This could be a template-id, so we try that first. */
2998 cp_parser_parse_tentatively (parser);
2999 /* Try a template-id. */
3000 id = cp_parser_template_id (parser, template_keyword_p,
3001 /*check_dependency_p=*/true,
3003 /* If that worked, we're done. */
3004 if (cp_parser_parse_definitely (parser))
3006 /* We still don't know whether we're looking at an
3007 operator-function-id or a conversion-function-id. */
3008 cp_parser_parse_tentatively (parser);
3009 /* Try an operator-function-id. */
3010 id = cp_parser_operator_function_id (parser);
3011 /* If that didn't work, try a conversion-function-id. */
3012 if (!cp_parser_parse_definitely (parser))
3013 id = cp_parser_conversion_function_id (parser);
3020 cp_parser_error (parser, "expected unqualified-id");
3021 return error_mark_node;
3025 /* Parse an (optional) nested-name-specifier.
3027 nested-name-specifier:
3028 class-or-namespace-name :: nested-name-specifier [opt]
3029 class-or-namespace-name :: template nested-name-specifier [opt]
3031 PARSER->SCOPE should be set appropriately before this function is
3032 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
3033 effect. TYPE_P is TRUE if we non-type bindings should be ignored
3036 Sets PARSER->SCOPE to the class (TYPE) or namespace
3037 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
3038 it unchanged if there is no nested-name-specifier. Returns the new
3039 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
3041 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
3042 part of a declaration and/or decl-specifier. */
3045 cp_parser_nested_name_specifier_opt (cp_parser *parser,
3046 bool typename_keyword_p,
3047 bool check_dependency_p,
3049 bool is_declaration)
3051 bool success = false;
3052 tree access_check = NULL_TREE;
3056 /* If the next token corresponds to a nested name specifier, there
3057 is no need to reparse it. However, if CHECK_DEPENDENCY_P is
3058 false, it may have been true before, in which case something
3059 like `A<X>::B<Y>::C' may have resulted in a nested-name-specifier
3060 of `A<X>::', where it should now be `A<X>::B<Y>::'. So, when
3061 CHECK_DEPENDENCY_P is false, we have to fall through into the
3063 if (check_dependency_p
3064 && cp_lexer_next_token_is (parser->lexer, CPP_NESTED_NAME_SPECIFIER))
3066 cp_parser_pre_parsed_nested_name_specifier (parser);
3067 return parser->scope;
3070 /* Remember where the nested-name-specifier starts. */
3071 if (cp_parser_parsing_tentatively (parser)
3072 && !cp_parser_committed_to_tentative_parse (parser))
3074 token = cp_lexer_peek_token (parser->lexer);
3075 start = cp_lexer_token_difference (parser->lexer,
3076 parser->lexer->first_token,
3082 push_deferring_access_checks (dk_deferred);
3088 tree saved_qualifying_scope;
3089 bool template_keyword_p;
3091 /* Spot cases that cannot be the beginning of a
3092 nested-name-specifier. */
3093 token = cp_lexer_peek_token (parser->lexer);
3095 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
3096 the already parsed nested-name-specifier. */
3097 if (token->type == CPP_NESTED_NAME_SPECIFIER)
3099 /* Grab the nested-name-specifier and continue the loop. */
3100 cp_parser_pre_parsed_nested_name_specifier (parser);
3105 /* Spot cases that cannot be the beginning of a
3106 nested-name-specifier. On the second and subsequent times
3107 through the loop, we look for the `template' keyword. */
3108 if (success && token->keyword == RID_TEMPLATE)
3110 /* A template-id can start a nested-name-specifier. */
3111 else if (token->type == CPP_TEMPLATE_ID)
3115 /* If the next token is not an identifier, then it is
3116 definitely not a class-or-namespace-name. */
3117 if (token->type != CPP_NAME)
3119 /* If the following token is neither a `<' (to begin a
3120 template-id), nor a `::', then we are not looking at a
3121 nested-name-specifier. */
3122 token = cp_lexer_peek_nth_token (parser->lexer, 2);
3123 if (token->type != CPP_SCOPE
3124 && !cp_parser_nth_token_starts_template_argument_list_p
3129 /* The nested-name-specifier is optional, so we parse
3131 cp_parser_parse_tentatively (parser);
3133 /* Look for the optional `template' keyword, if this isn't the
3134 first time through the loop. */
3136 template_keyword_p = cp_parser_optional_template_keyword (parser);
3138 template_keyword_p = false;
3140 /* Save the old scope since the name lookup we are about to do
3141 might destroy it. */
3142 old_scope = parser->scope;
3143 saved_qualifying_scope = parser->qualifying_scope;
3144 /* Parse the qualifying entity. */
3146 = cp_parser_class_or_namespace_name (parser,
3152 /* Look for the `::' token. */
3153 cp_parser_require (parser, CPP_SCOPE, "`::'");
3155 /* If we found what we wanted, we keep going; otherwise, we're
3157 if (!cp_parser_parse_definitely (parser))
3159 bool error_p = false;
3161 /* Restore the OLD_SCOPE since it was valid before the
3162 failed attempt at finding the last
3163 class-or-namespace-name. */
3164 parser->scope = old_scope;
3165 parser->qualifying_scope = saved_qualifying_scope;
3166 /* If the next token is an identifier, and the one after
3167 that is a `::', then any valid interpretation would have
3168 found a class-or-namespace-name. */
3169 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
3170 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3172 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
3175 token = cp_lexer_consume_token (parser->lexer);
3180 decl = cp_parser_lookup_name_simple (parser, token->value);
3181 if (TREE_CODE (decl) == TEMPLATE_DECL)
3182 error ("`%D' used without template parameters",
3185 cp_parser_name_lookup_error
3186 (parser, token->value, decl,
3187 "is not a class or namespace");
3188 parser->scope = NULL_TREE;
3190 /* Treat this as a successful nested-name-specifier
3195 If the name found is not a class-name (clause
3196 _class_) or namespace-name (_namespace.def_), the
3197 program is ill-formed. */
3200 cp_lexer_consume_token (parser->lexer);
3205 /* We've found one valid nested-name-specifier. */
3207 /* Make sure we look in the right scope the next time through
3209 parser->scope = (TREE_CODE (new_scope) == TYPE_DECL
3210 ? TREE_TYPE (new_scope)
3212 /* If it is a class scope, try to complete it; we are about to
3213 be looking up names inside the class. */
3214 if (TYPE_P (parser->scope)
3215 /* Since checking types for dependency can be expensive,
3216 avoid doing it if the type is already complete. */
3217 && !COMPLETE_TYPE_P (parser->scope)
3218 /* Do not try to complete dependent types. */
3219 && !dependent_type_p (parser->scope))
3220 complete_type (parser->scope);
3223 /* Retrieve any deferred checks. Do not pop this access checks yet
3224 so the memory will not be reclaimed during token replacing below. */
3225 access_check = get_deferred_access_checks ();
3227 /* If parsing tentatively, replace the sequence of tokens that makes
3228 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
3229 token. That way, should we re-parse the token stream, we will
3230 not have to repeat the effort required to do the parse, nor will
3231 we issue duplicate error messages. */
3232 if (success && start >= 0)
3234 /* Find the token that corresponds to the start of the
3236 token = cp_lexer_advance_token (parser->lexer,
3237 parser->lexer->first_token,
3240 /* Reset the contents of the START token. */
3241 token->type = CPP_NESTED_NAME_SPECIFIER;
3242 token->value = build_tree_list (access_check, parser->scope);
3243 TREE_TYPE (token->value) = parser->qualifying_scope;
3244 token->keyword = RID_MAX;
3245 /* Purge all subsequent tokens. */
3246 cp_lexer_purge_tokens_after (parser->lexer, token);
3249 pop_deferring_access_checks ();
3250 return success ? parser->scope : NULL_TREE;
3253 /* Parse a nested-name-specifier. See
3254 cp_parser_nested_name_specifier_opt for details. This function
3255 behaves identically, except that it will an issue an error if no
3256 nested-name-specifier is present, and it will return
3257 ERROR_MARK_NODE, rather than NULL_TREE, if no nested-name-specifier
3261 cp_parser_nested_name_specifier (cp_parser *parser,
3262 bool typename_keyword_p,
3263 bool check_dependency_p,
3265 bool is_declaration)
3269 /* Look for the nested-name-specifier. */
3270 scope = cp_parser_nested_name_specifier_opt (parser,
3275 /* If it was not present, issue an error message. */
3278 cp_parser_error (parser, "expected nested-name-specifier");
3279 parser->scope = NULL_TREE;
3280 return error_mark_node;
3286 /* Parse a class-or-namespace-name.
3288 class-or-namespace-name:
3292 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
3293 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
3294 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
3295 TYPE_P is TRUE iff the next name should be taken as a class-name,
3296 even the same name is declared to be another entity in the same
3299 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
3300 specified by the class-or-namespace-name. If neither is found the
3301 ERROR_MARK_NODE is returned. */
3304 cp_parser_class_or_namespace_name (cp_parser *parser,
3305 bool typename_keyword_p,
3306 bool template_keyword_p,
3307 bool check_dependency_p,
3309 bool is_declaration)
3312 tree saved_qualifying_scope;
3313 tree saved_object_scope;
3317 /* Before we try to parse the class-name, we must save away the
3318 current PARSER->SCOPE since cp_parser_class_name will destroy
3320 saved_scope = parser->scope;
3321 saved_qualifying_scope = parser->qualifying_scope;
3322 saved_object_scope = parser->object_scope;
3323 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
3324 there is no need to look for a namespace-name. */
3325 only_class_p = template_keyword_p || (saved_scope && TYPE_P (saved_scope));
3327 cp_parser_parse_tentatively (parser);
3328 scope = cp_parser_class_name (parser,
3333 /*class_head_p=*/false,
3335 /* If that didn't work, try for a namespace-name. */
3336 if (!only_class_p && !cp_parser_parse_definitely (parser))
3338 /* Restore the saved scope. */
3339 parser->scope = saved_scope;
3340 parser->qualifying_scope = saved_qualifying_scope;
3341 parser->object_scope = saved_object_scope;
3342 /* If we are not looking at an identifier followed by the scope
3343 resolution operator, then this is not part of a
3344 nested-name-specifier. (Note that this function is only used
3345 to parse the components of a nested-name-specifier.) */
3346 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
3347 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
3348 return error_mark_node;
3349 scope = cp_parser_namespace_name (parser);
3355 /* Parse a postfix-expression.
3359 postfix-expression [ expression ]
3360 postfix-expression ( expression-list [opt] )
3361 simple-type-specifier ( expression-list [opt] )
3362 typename :: [opt] nested-name-specifier identifier
3363 ( expression-list [opt] )
3364 typename :: [opt] nested-name-specifier template [opt] template-id
3365 ( expression-list [opt] )
3366 postfix-expression . template [opt] id-expression
3367 postfix-expression -> template [opt] id-expression
3368 postfix-expression . pseudo-destructor-name
3369 postfix-expression -> pseudo-destructor-name
3370 postfix-expression ++
3371 postfix-expression --
3372 dynamic_cast < type-id > ( expression )
3373 static_cast < type-id > ( expression )
3374 reinterpret_cast < type-id > ( expression )
3375 const_cast < type-id > ( expression )
3376 typeid ( expression )
3382 ( type-id ) { initializer-list , [opt] }
3384 This extension is a GNU version of the C99 compound-literal
3385 construct. (The C99 grammar uses `type-name' instead of `type-id',
3386 but they are essentially the same concept.)
3388 If ADDRESS_P is true, the postfix expression is the operand of the
3391 Returns a representation of the expression. */
3394 cp_parser_postfix_expression (cp_parser *parser, bool address_p)
3398 cp_id_kind idk = CP_ID_KIND_NONE;
3399 tree postfix_expression = NULL_TREE;
3400 /* Non-NULL only if the current postfix-expression can be used to
3401 form a pointer-to-member. In that case, QUALIFYING_CLASS is the
3402 class used to qualify the member. */
3403 tree qualifying_class = NULL_TREE;
3405 /* Peek at the next token. */
3406 token = cp_lexer_peek_token (parser->lexer);
3407 /* Some of the productions are determined by keywords. */
3408 keyword = token->keyword;
3418 const char *saved_message;
3420 /* All of these can be handled in the same way from the point
3421 of view of parsing. Begin by consuming the token
3422 identifying the cast. */
3423 cp_lexer_consume_token (parser->lexer);
3425 /* New types cannot be defined in the cast. */
3426 saved_message = parser->type_definition_forbidden_message;
3427 parser->type_definition_forbidden_message
3428 = "types may not be defined in casts";
3430 /* Look for the opening `<'. */
3431 cp_parser_require (parser, CPP_LESS, "`<'");
3432 /* Parse the type to which we are casting. */
3433 type = cp_parser_type_id (parser);
3434 /* Look for the closing `>'. */
3435 cp_parser_require (parser, CPP_GREATER, "`>'");
3436 /* Restore the old message. */
3437 parser->type_definition_forbidden_message = saved_message;
3439 /* And the expression which is being cast. */
3440 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3441 expression = cp_parser_expression (parser);
3442 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3444 /* Only type conversions to integral or enumeration types
3445 can be used in constant-expressions. */
3446 if (parser->integral_constant_expression_p
3447 && !dependent_type_p (type)
3448 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type)
3449 /* A cast to pointer or reference type is allowed in the
3450 implementation of "offsetof". */
3451 && !(parser->in_offsetof_p && POINTER_TYPE_P (type))
3452 && (cp_parser_non_integral_constant_expression
3454 "a cast to a type other than an integral or "
3455 "enumeration type")))
3456 return error_mark_node;
3462 = build_dynamic_cast (type, expression);
3466 = build_static_cast (type, expression);
3470 = build_reinterpret_cast (type, expression);
3474 = build_const_cast (type, expression);
3485 const char *saved_message;
3486 bool saved_in_type_id_in_expr_p;
3488 /* Consume the `typeid' token. */
3489 cp_lexer_consume_token (parser->lexer);
3490 /* Look for the `(' token. */
3491 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3492 /* Types cannot be defined in a `typeid' expression. */
3493 saved_message = parser->type_definition_forbidden_message;
3494 parser->type_definition_forbidden_message
3495 = "types may not be defined in a `typeid\' expression";
3496 /* We can't be sure yet whether we're looking at a type-id or an
3498 cp_parser_parse_tentatively (parser);
3499 /* Try a type-id first. */
3500 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
3501 parser->in_type_id_in_expr_p = true;
3502 type = cp_parser_type_id (parser);
3503 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
3504 /* Look for the `)' token. Otherwise, we can't be sure that
3505 we're not looking at an expression: consider `typeid (int
3506 (3))', for example. */
3507 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3508 /* If all went well, simply lookup the type-id. */
3509 if (cp_parser_parse_definitely (parser))
3510 postfix_expression = get_typeid (type);
3511 /* Otherwise, fall back to the expression variant. */
3516 /* Look for an expression. */
3517 expression = cp_parser_expression (parser);
3518 /* Compute its typeid. */
3519 postfix_expression = build_typeid (expression);
3520 /* Look for the `)' token. */
3521 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3523 /* `typeid' may not appear in an integral constant expression. */
3524 if (cp_parser_non_integral_constant_expression(parser,
3525 "`typeid' operator"))
3526 return error_mark_node;
3527 /* Restore the saved message. */
3528 parser->type_definition_forbidden_message = saved_message;
3534 bool template_p = false;
3538 /* Consume the `typename' token. */
3539 cp_lexer_consume_token (parser->lexer);
3540 /* Look for the optional `::' operator. */
3541 cp_parser_global_scope_opt (parser,
3542 /*current_scope_valid_p=*/false);
3543 /* Look for the nested-name-specifier. */
3544 cp_parser_nested_name_specifier (parser,
3545 /*typename_keyword_p=*/true,
3546 /*check_dependency_p=*/true,
3548 /*is_declaration=*/true);
3549 /* Look for the optional `template' keyword. */
3550 template_p = cp_parser_optional_template_keyword (parser);
3551 /* We don't know whether we're looking at a template-id or an
3553 cp_parser_parse_tentatively (parser);
3554 /* Try a template-id. */
3555 id = cp_parser_template_id (parser, template_p,
3556 /*check_dependency_p=*/true,
3557 /*is_declaration=*/true);
3558 /* If that didn't work, try an identifier. */
3559 if (!cp_parser_parse_definitely (parser))
3560 id = cp_parser_identifier (parser);
3561 /* If we look up a template-id in a non-dependent qualifying
3562 scope, there's no need to create a dependent type. */
3563 if (TREE_CODE (id) == TYPE_DECL
3564 && !dependent_type_p (parser->scope))
3565 type = TREE_TYPE (id);
3566 /* Create a TYPENAME_TYPE to represent the type to which the
3567 functional cast is being performed. */
3569 type = make_typename_type (parser->scope, id,
3572 postfix_expression = cp_parser_functional_cast (parser, type);
3580 /* If the next thing is a simple-type-specifier, we may be
3581 looking at a functional cast. We could also be looking at
3582 an id-expression. So, we try the functional cast, and if
3583 that doesn't work we fall back to the primary-expression. */
3584 cp_parser_parse_tentatively (parser);
3585 /* Look for the simple-type-specifier. */
3586 type = cp_parser_simple_type_specifier (parser,
3587 CP_PARSER_FLAGS_NONE,
3588 /*identifier_p=*/false);
3589 /* Parse the cast itself. */
3590 if (!cp_parser_error_occurred (parser))
3592 = cp_parser_functional_cast (parser, type);
3593 /* If that worked, we're done. */
3594 if (cp_parser_parse_definitely (parser))
3597 /* If the functional-cast didn't work out, try a
3598 compound-literal. */
3599 if (cp_parser_allow_gnu_extensions_p (parser)
3600 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
3602 tree initializer_list = NULL_TREE;
3603 bool saved_in_type_id_in_expr_p;
3605 cp_parser_parse_tentatively (parser);
3606 /* Consume the `('. */
3607 cp_lexer_consume_token (parser->lexer);
3608 /* Parse the type. */
3609 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
3610 parser->in_type_id_in_expr_p = true;
3611 type = cp_parser_type_id (parser);
3612 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
3613 /* Look for the `)'. */
3614 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3615 /* Look for the `{'. */
3616 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
3617 /* If things aren't going well, there's no need to
3619 if (!cp_parser_error_occurred (parser))
3621 bool non_constant_p;
3622 /* Parse the initializer-list. */
3624 = cp_parser_initializer_list (parser, &non_constant_p);
3625 /* Allow a trailing `,'. */
3626 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
3627 cp_lexer_consume_token (parser->lexer);
3628 /* Look for the final `}'. */
3629 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
3631 /* If that worked, we're definitely looking at a
3632 compound-literal expression. */
3633 if (cp_parser_parse_definitely (parser))
3635 /* Warn the user that a compound literal is not
3636 allowed in standard C++. */
3638 pedwarn ("ISO C++ forbids compound-literals");
3639 /* Form the representation of the compound-literal. */
3641 = finish_compound_literal (type, initializer_list);
3646 /* It must be a primary-expression. */
3647 postfix_expression = cp_parser_primary_expression (parser,
3654 /* If we were avoiding committing to the processing of a
3655 qualified-id until we knew whether or not we had a
3656 pointer-to-member, we now know. */
3657 if (qualifying_class)
3661 /* Peek at the next token. */
3662 token = cp_lexer_peek_token (parser->lexer);
3663 done = (token->type != CPP_OPEN_SQUARE
3664 && token->type != CPP_OPEN_PAREN
3665 && token->type != CPP_DOT
3666 && token->type != CPP_DEREF
3667 && token->type != CPP_PLUS_PLUS
3668 && token->type != CPP_MINUS_MINUS);
3670 postfix_expression = finish_qualified_id_expr (qualifying_class,
3675 return postfix_expression;
3678 /* Keep looping until the postfix-expression is complete. */
3681 if (idk == CP_ID_KIND_UNQUALIFIED
3682 && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
3683 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
3684 /* It is not a Koenig lookup function call. */
3686 = unqualified_name_lookup_error (postfix_expression);
3688 /* Peek at the next token. */
3689 token = cp_lexer_peek_token (parser->lexer);
3691 switch (token->type)
3693 case CPP_OPEN_SQUARE:
3694 /* postfix-expression [ expression ] */
3698 /* Consume the `[' token. */
3699 cp_lexer_consume_token (parser->lexer);
3700 /* Parse the index expression. */
3701 index = cp_parser_expression (parser);
3702 /* Look for the closing `]'. */
3703 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
3705 /* Build the ARRAY_REF. */
3707 = grok_array_decl (postfix_expression, index);
3708 idk = CP_ID_KIND_NONE;
3709 /* Array references are not permitted in
3710 constant-expressions (but they are allowed
3712 if (!parser->in_offsetof_p
3713 && cp_parser_non_integral_constant_expression
3714 (parser, "an array reference"))
3715 postfix_expression = error_mark_node;
3719 case CPP_OPEN_PAREN:
3720 /* postfix-expression ( expression-list [opt] ) */
3723 tree args = (cp_parser_parenthesized_expression_list
3724 (parser, false, /*non_constant_p=*/NULL));
3726 if (args == error_mark_node)
3728 postfix_expression = error_mark_node;
3732 /* Function calls are not permitted in
3733 constant-expressions. */
3734 if (cp_parser_non_integral_constant_expression (parser,
3737 postfix_expression = error_mark_node;
3742 if (idk == CP_ID_KIND_UNQUALIFIED)
3744 /* We do not perform argument-dependent lookup if
3745 normal lookup finds a non-function, in accordance
3746 with the expected resolution of DR 218. */
3748 && (is_overloaded_fn (postfix_expression)
3749 || TREE_CODE (postfix_expression) == IDENTIFIER_NODE))
3753 = perform_koenig_lookup (postfix_expression, args);
3755 else if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
3757 = unqualified_fn_lookup_error (postfix_expression);
3760 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
3762 tree instance = TREE_OPERAND (postfix_expression, 0);
3763 tree fn = TREE_OPERAND (postfix_expression, 1);
3765 if (processing_template_decl
3766 && (type_dependent_expression_p (instance)
3767 || (!BASELINK_P (fn)
3768 && TREE_CODE (fn) != FIELD_DECL)
3769 || type_dependent_expression_p (fn)
3770 || any_type_dependent_arguments_p (args)))
3773 = build_min_nt (CALL_EXPR, postfix_expression, args);
3777 if (BASELINK_P (fn))
3779 = (build_new_method_call
3780 (instance, fn, args, NULL_TREE,
3781 (idk == CP_ID_KIND_QUALIFIED
3782 ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL)));
3785 = finish_call_expr (postfix_expression, args,
3786 /*disallow_virtual=*/false,
3787 /*koenig_p=*/false);
3789 else if (TREE_CODE (postfix_expression) == OFFSET_REF
3790 || TREE_CODE (postfix_expression) == MEMBER_REF
3791 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
3792 postfix_expression = (build_offset_ref_call_from_tree
3793 (postfix_expression, args));
3794 else if (idk == CP_ID_KIND_QUALIFIED)
3795 /* A call to a static class member, or a namespace-scope
3798 = finish_call_expr (postfix_expression, args,
3799 /*disallow_virtual=*/true,
3802 /* All other function calls. */
3804 = finish_call_expr (postfix_expression, args,
3805 /*disallow_virtual=*/false,
3808 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
3809 idk = CP_ID_KIND_NONE;
3815 /* postfix-expression . template [opt] id-expression
3816 postfix-expression . pseudo-destructor-name
3817 postfix-expression -> template [opt] id-expression
3818 postfix-expression -> pseudo-destructor-name */
3823 tree scope = NULL_TREE;
3824 enum cpp_ttype token_type = token->type;
3826 /* If this is a `->' operator, dereference the pointer. */
3827 if (token->type == CPP_DEREF)
3828 postfix_expression = build_x_arrow (postfix_expression);
3829 /* Check to see whether or not the expression is
3831 dependent_p = type_dependent_expression_p (postfix_expression);
3832 /* The identifier following the `->' or `.' is not
3834 parser->scope = NULL_TREE;
3835 parser->qualifying_scope = NULL_TREE;
3836 parser->object_scope = NULL_TREE;
3837 idk = CP_ID_KIND_NONE;
3838 /* Enter the scope corresponding to the type of the object
3839 given by the POSTFIX_EXPRESSION. */
3841 && TREE_TYPE (postfix_expression) != NULL_TREE)
3843 scope = TREE_TYPE (postfix_expression);
3844 /* According to the standard, no expression should
3845 ever have reference type. Unfortunately, we do not
3846 currently match the standard in this respect in
3847 that our internal representation of an expression
3848 may have reference type even when the standard says
3849 it does not. Therefore, we have to manually obtain
3850 the underlying type here. */
3851 scope = non_reference (scope);
3852 /* The type of the POSTFIX_EXPRESSION must be
3854 scope = complete_type_or_else (scope, NULL_TREE);
3855 /* Let the name lookup machinery know that we are
3856 processing a class member access expression. */
3857 parser->context->object_type = scope;
3858 /* If something went wrong, we want to be able to
3859 discern that case, as opposed to the case where
3860 there was no SCOPE due to the type of expression
3863 scope = error_mark_node;
3864 /* If the SCOPE was erroneous, make the various
3865 semantic analysis functions exit quickly -- and
3866 without issuing additional error messages. */
3867 if (scope == error_mark_node)
3868 postfix_expression = error_mark_node;
3871 /* Consume the `.' or `->' operator. */
3872 cp_lexer_consume_token (parser->lexer);
3873 /* If the SCOPE is not a scalar type, we are looking at an
3874 ordinary class member access expression, rather than a
3875 pseudo-destructor-name. */
3876 if (!scope || !SCALAR_TYPE_P (scope))
3878 template_p = cp_parser_optional_template_keyword (parser);
3879 /* Parse the id-expression. */
3880 name = cp_parser_id_expression (parser,
3882 /*check_dependency_p=*/true,
3883 /*template_p=*/NULL,
3884 /*declarator_p=*/false);
3885 /* In general, build a SCOPE_REF if the member name is
3886 qualified. However, if the name was not dependent
3887 and has already been resolved; there is no need to
3888 build the SCOPE_REF. For example;
3890 struct X { void f(); };
3891 template <typename T> void f(T* t) { t->X::f(); }
3893 Even though "t" is dependent, "X::f" is not and has
3894 been resolved to a BASELINK; there is no need to
3895 include scope information. */
3897 /* But we do need to remember that there was an explicit
3898 scope for virtual function calls. */
3900 idk = CP_ID_KIND_QUALIFIED;
3902 if (name != error_mark_node
3903 && !BASELINK_P (name)
3906 name = build_nt (SCOPE_REF, parser->scope, name);
3907 parser->scope = NULL_TREE;
3908 parser->qualifying_scope = NULL_TREE;
3909 parser->object_scope = NULL_TREE;
3911 if (scope && name && BASELINK_P (name))
3912 adjust_result_of_qualified_name_lookup
3913 (name, BINFO_TYPE (BASELINK_BINFO (name)), scope);
3915 = finish_class_member_access_expr (postfix_expression, name);
3917 /* Otherwise, try the pseudo-destructor-name production. */
3923 /* Parse the pseudo-destructor-name. */
3924 cp_parser_pseudo_destructor_name (parser, &s, &type);
3925 /* Form the call. */
3927 = finish_pseudo_destructor_expr (postfix_expression,
3928 s, TREE_TYPE (type));
3931 /* We no longer need to look up names in the scope of the
3932 object on the left-hand side of the `.' or `->'
3934 parser->context->object_type = NULL_TREE;
3935 /* These operators may not appear in constant-expressions. */
3936 if (/* The "->" operator is allowed in the implementation
3937 of "offsetof". The "." operator may appear in the
3938 name of the member. */
3939 !parser->in_offsetof_p
3940 && (cp_parser_non_integral_constant_expression
3942 token_type == CPP_DEREF ? "'->'" : "`.'")))
3943 postfix_expression = error_mark_node;
3948 /* postfix-expression ++ */
3949 /* Consume the `++' token. */
3950 cp_lexer_consume_token (parser->lexer);
3951 /* Generate a representation for the complete expression. */
3953 = finish_increment_expr (postfix_expression,
3954 POSTINCREMENT_EXPR);
3955 /* Increments may not appear in constant-expressions. */
3956 if (cp_parser_non_integral_constant_expression (parser,
3958 postfix_expression = error_mark_node;
3959 idk = CP_ID_KIND_NONE;
3962 case CPP_MINUS_MINUS:
3963 /* postfix-expression -- */
3964 /* Consume the `--' token. */
3965 cp_lexer_consume_token (parser->lexer);
3966 /* Generate a representation for the complete expression. */
3968 = finish_increment_expr (postfix_expression,
3969 POSTDECREMENT_EXPR);
3970 /* Decrements may not appear in constant-expressions. */
3971 if (cp_parser_non_integral_constant_expression (parser,
3973 postfix_expression = error_mark_node;
3974 idk = CP_ID_KIND_NONE;
3978 return postfix_expression;
3982 /* We should never get here. */
3984 return error_mark_node;
3987 /* Parse a parenthesized expression-list.
3990 assignment-expression
3991 expression-list, assignment-expression
3996 identifier, expression-list
3998 Returns a TREE_LIST. The TREE_VALUE of each node is a
3999 representation of an assignment-expression. Note that a TREE_LIST
4000 is returned even if there is only a single expression in the list.
4001 error_mark_node is returned if the ( and or ) are
4002 missing. NULL_TREE is returned on no expressions. The parentheses
4003 are eaten. IS_ATTRIBUTE_LIST is true if this is really an attribute
4004 list being parsed. If NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P
4005 indicates whether or not all of the expressions in the list were
4009 cp_parser_parenthesized_expression_list (cp_parser* parser,
4010 bool is_attribute_list,
4011 bool *non_constant_p)
4013 tree expression_list = NULL_TREE;
4014 tree identifier = NULL_TREE;
4016 /* Assume all the expressions will be constant. */
4018 *non_constant_p = false;
4020 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
4021 return error_mark_node;
4023 /* Consume expressions until there are no more. */
4024 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
4029 /* At the beginning of attribute lists, check to see if the
4030 next token is an identifier. */
4031 if (is_attribute_list
4032 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
4036 /* Consume the identifier. */
4037 token = cp_lexer_consume_token (parser->lexer);
4038 /* Save the identifier. */
4039 identifier = token->value;
4043 /* Parse the next assignment-expression. */
4046 bool expr_non_constant_p;
4047 expr = (cp_parser_constant_expression
4048 (parser, /*allow_non_constant_p=*/true,
4049 &expr_non_constant_p));
4050 if (expr_non_constant_p)
4051 *non_constant_p = true;
4054 expr = cp_parser_assignment_expression (parser);
4056 /* Add it to the list. We add error_mark_node
4057 expressions to the list, so that we can still tell if
4058 the correct form for a parenthesized expression-list
4059 is found. That gives better errors. */
4060 expression_list = tree_cons (NULL_TREE, expr, expression_list);
4062 if (expr == error_mark_node)
4066 /* After the first item, attribute lists look the same as
4067 expression lists. */
4068 is_attribute_list = false;
4071 /* If the next token isn't a `,', then we are done. */
4072 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
4075 /* Otherwise, consume the `,' and keep going. */
4076 cp_lexer_consume_token (parser->lexer);
4079 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
4084 /* We try and resync to an unnested comma, as that will give the
4085 user better diagnostics. */
4086 ending = cp_parser_skip_to_closing_parenthesis (parser,
4087 /*recovering=*/true,
4089 /*consume_paren=*/true);
4093 return error_mark_node;
4096 /* We built up the list in reverse order so we must reverse it now. */
4097 expression_list = nreverse (expression_list);
4099 expression_list = tree_cons (NULL_TREE, identifier, expression_list);
4101 return expression_list;
4104 /* Parse a pseudo-destructor-name.
4106 pseudo-destructor-name:
4107 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
4108 :: [opt] nested-name-specifier template template-id :: ~ type-name
4109 :: [opt] nested-name-specifier [opt] ~ type-name
4111 If either of the first two productions is used, sets *SCOPE to the
4112 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
4113 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
4114 or ERROR_MARK_NODE if the parse fails. */
4117 cp_parser_pseudo_destructor_name (cp_parser* parser,
4121 bool nested_name_specifier_p;
4123 /* Look for the optional `::' operator. */
4124 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
4125 /* Look for the optional nested-name-specifier. */
4126 nested_name_specifier_p
4127 = (cp_parser_nested_name_specifier_opt (parser,
4128 /*typename_keyword_p=*/false,
4129 /*check_dependency_p=*/true,
4131 /*is_declaration=*/true)
4133 /* Now, if we saw a nested-name-specifier, we might be doing the
4134 second production. */
4135 if (nested_name_specifier_p
4136 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
4138 /* Consume the `template' keyword. */
4139 cp_lexer_consume_token (parser->lexer);
4140 /* Parse the template-id. */
4141 cp_parser_template_id (parser,
4142 /*template_keyword_p=*/true,
4143 /*check_dependency_p=*/false,
4144 /*is_declaration=*/true);
4145 /* Look for the `::' token. */
4146 cp_parser_require (parser, CPP_SCOPE, "`::'");
4148 /* If the next token is not a `~', then there might be some
4149 additional qualification. */
4150 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
4152 /* Look for the type-name. */
4153 *scope = TREE_TYPE (cp_parser_type_name (parser));
4155 /* If we didn't get an aggregate type, or we don't have ::~,
4156 then something has gone wrong. Since the only caller of this
4157 function is looking for something after `.' or `->' after a
4158 scalar type, most likely the program is trying to get a
4159 member of a non-aggregate type. */
4160 if (*scope == error_mark_node
4161 || cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE)
4162 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_COMPL)
4164 cp_parser_error (parser, "request for member of non-aggregate type");
4165 *type = error_mark_node;
4169 /* Look for the `::' token. */
4170 cp_parser_require (parser, CPP_SCOPE, "`::'");
4175 /* Look for the `~'. */
4176 cp_parser_require (parser, CPP_COMPL, "`~'");
4177 /* Look for the type-name again. We are not responsible for
4178 checking that it matches the first type-name. */
4179 *type = cp_parser_type_name (parser);
4182 /* Parse a unary-expression.
4188 unary-operator cast-expression
4189 sizeof unary-expression
4197 __extension__ cast-expression
4198 __alignof__ unary-expression
4199 __alignof__ ( type-id )
4200 __real__ cast-expression
4201 __imag__ cast-expression
4204 ADDRESS_P is true iff the unary-expression is appearing as the
4205 operand of the `&' operator.
4207 Returns a representation of the expression. */
4210 cp_parser_unary_expression (cp_parser *parser, bool address_p)
4213 enum tree_code unary_operator;
4215 /* Peek at the next token. */
4216 token = cp_lexer_peek_token (parser->lexer);
4217 /* Some keywords give away the kind of expression. */
4218 if (token->type == CPP_KEYWORD)
4220 enum rid keyword = token->keyword;
4230 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
4231 /* Consume the token. */
4232 cp_lexer_consume_token (parser->lexer);
4233 /* Parse the operand. */
4234 operand = cp_parser_sizeof_operand (parser, keyword);
4236 if (TYPE_P (operand))
4237 return cxx_sizeof_or_alignof_type (operand, op, true);
4239 return cxx_sizeof_or_alignof_expr (operand, op);
4243 return cp_parser_new_expression (parser);
4246 return cp_parser_delete_expression (parser);
4250 /* The saved value of the PEDANTIC flag. */
4254 /* Save away the PEDANTIC flag. */
4255 cp_parser_extension_opt (parser, &saved_pedantic);
4256 /* Parse the cast-expression. */
4257 expr = cp_parser_simple_cast_expression (parser);
4258 /* Restore the PEDANTIC flag. */
4259 pedantic = saved_pedantic;
4269 /* Consume the `__real__' or `__imag__' token. */
4270 cp_lexer_consume_token (parser->lexer);
4271 /* Parse the cast-expression. */
4272 expression = cp_parser_simple_cast_expression (parser);
4273 /* Create the complete representation. */
4274 return build_x_unary_op ((keyword == RID_REALPART
4275 ? REALPART_EXPR : IMAGPART_EXPR),
4285 /* Look for the `:: new' and `:: delete', which also signal the
4286 beginning of a new-expression, or delete-expression,
4287 respectively. If the next token is `::', then it might be one of
4289 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
4293 /* See if the token after the `::' is one of the keywords in
4294 which we're interested. */
4295 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
4296 /* If it's `new', we have a new-expression. */
4297 if (keyword == RID_NEW)
4298 return cp_parser_new_expression (parser);
4299 /* Similarly, for `delete'. */
4300 else if (keyword == RID_DELETE)
4301 return cp_parser_delete_expression (parser);
4304 /* Look for a unary operator. */
4305 unary_operator = cp_parser_unary_operator (token);
4306 /* The `++' and `--' operators can be handled similarly, even though
4307 they are not technically unary-operators in the grammar. */
4308 if (unary_operator == ERROR_MARK)
4310 if (token->type == CPP_PLUS_PLUS)
4311 unary_operator = PREINCREMENT_EXPR;
4312 else if (token->type == CPP_MINUS_MINUS)
4313 unary_operator = PREDECREMENT_EXPR;
4314 /* Handle the GNU address-of-label extension. */
4315 else if (cp_parser_allow_gnu_extensions_p (parser)
4316 && token->type == CPP_AND_AND)
4320 /* Consume the '&&' token. */
4321 cp_lexer_consume_token (parser->lexer);
4322 /* Look for the identifier. */
4323 identifier = cp_parser_identifier (parser);
4324 /* Create an expression representing the address. */
4325 return finish_label_address_expr (identifier);
4328 if (unary_operator != ERROR_MARK)
4330 tree cast_expression;
4331 tree expression = error_mark_node;
4332 const char *non_constant_p = NULL;
4334 /* Consume the operator token. */
4335 token = cp_lexer_consume_token (parser->lexer);
4336 /* Parse the cast-expression. */
4338 = cp_parser_cast_expression (parser, unary_operator == ADDR_EXPR);
4339 /* Now, build an appropriate representation. */
4340 switch (unary_operator)
4343 non_constant_p = "`*'";
4344 expression = build_x_indirect_ref (cast_expression, "unary *");
4348 /* The "&" operator is allowed in the implementation of
4350 if (!parser->in_offsetof_p)
4351 non_constant_p = "`&'";
4354 expression = build_x_unary_op (unary_operator, cast_expression);
4357 case PREINCREMENT_EXPR:
4358 case PREDECREMENT_EXPR:
4359 non_constant_p = (unary_operator == PREINCREMENT_EXPR
4364 case TRUTH_NOT_EXPR:
4365 expression = finish_unary_op_expr (unary_operator, cast_expression);
4373 && cp_parser_non_integral_constant_expression (parser,
4375 expression = error_mark_node;
4380 return cp_parser_postfix_expression (parser, address_p);
4383 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
4384 unary-operator, the corresponding tree code is returned. */
4386 static enum tree_code
4387 cp_parser_unary_operator (cp_token* token)
4389 switch (token->type)
4392 return INDIRECT_REF;
4398 return CONVERT_EXPR;
4404 return TRUTH_NOT_EXPR;
4407 return BIT_NOT_EXPR;
4414 /* Parse a new-expression.
4417 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
4418 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
4420 Returns a representation of the expression. */
4423 cp_parser_new_expression (cp_parser* parser)
4425 bool global_scope_p;
4430 /* Look for the optional `::' operator. */
4432 = (cp_parser_global_scope_opt (parser,
4433 /*current_scope_valid_p=*/false)
4435 /* Look for the `new' operator. */
4436 cp_parser_require_keyword (parser, RID_NEW, "`new'");
4437 /* There's no easy way to tell a new-placement from the
4438 `( type-id )' construct. */
4439 cp_parser_parse_tentatively (parser);
4440 /* Look for a new-placement. */
4441 placement = cp_parser_new_placement (parser);
4442 /* If that didn't work out, there's no new-placement. */
4443 if (!cp_parser_parse_definitely (parser))
4444 placement = NULL_TREE;
4446 /* If the next token is a `(', then we have a parenthesized
4448 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4450 /* Consume the `('. */
4451 cp_lexer_consume_token (parser->lexer);
4452 /* Parse the type-id. */
4453 type = cp_parser_type_id (parser);
4454 /* Look for the closing `)'. */
4455 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4456 /* There should not be a direct-new-declarator in this production,
4457 but GCC used to allowed this, so we check and emit a sensible error
4458 message for this case. */
4459 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
4461 error ("array bound forbidden after parenthesized type-id");
4462 inform ("try removing the parentheses around the type-id");
4463 cp_parser_direct_new_declarator (parser);
4466 /* Otherwise, there must be a new-type-id. */
4468 type = cp_parser_new_type_id (parser);
4470 /* If the next token is a `(', then we have a new-initializer. */
4471 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4472 initializer = cp_parser_new_initializer (parser);
4474 initializer = NULL_TREE;
4476 /* A new-expression may not appear in an integral constant
4478 if (cp_parser_non_integral_constant_expression (parser, "`new'"))
4479 return error_mark_node;
4481 /* Create a representation of the new-expression. */
4482 return build_new (placement, type, initializer, global_scope_p);
4485 /* Parse a new-placement.
4490 Returns the same representation as for an expression-list. */
4493 cp_parser_new_placement (cp_parser* parser)
4495 tree expression_list;
4497 /* Parse the expression-list. */
4498 expression_list = (cp_parser_parenthesized_expression_list
4499 (parser, false, /*non_constant_p=*/NULL));
4501 return expression_list;
4504 /* Parse a new-type-id.
4507 type-specifier-seq new-declarator [opt]
4509 Returns a TREE_LIST whose TREE_PURPOSE is the type-specifier-seq,
4510 and whose TREE_VALUE is the new-declarator. */
4513 cp_parser_new_type_id (cp_parser* parser)
4515 tree type_specifier_seq;
4517 const char *saved_message;
4519 /* The type-specifier sequence must not contain type definitions.
4520 (It cannot contain declarations of new types either, but if they
4521 are not definitions we will catch that because they are not
4523 saved_message = parser->type_definition_forbidden_message;
4524 parser->type_definition_forbidden_message
4525 = "types may not be defined in a new-type-id";
4526 /* Parse the type-specifier-seq. */
4527 type_specifier_seq = cp_parser_type_specifier_seq (parser);
4528 /* Restore the old message. */
4529 parser->type_definition_forbidden_message = saved_message;
4530 /* Parse the new-declarator. */
4531 declarator = cp_parser_new_declarator_opt (parser);
4533 return build_tree_list (type_specifier_seq, declarator);
4536 /* Parse an (optional) new-declarator.
4539 ptr-operator new-declarator [opt]
4540 direct-new-declarator
4542 Returns a representation of the declarator. See
4543 cp_parser_declarator for the representations used. */
4546 cp_parser_new_declarator_opt (cp_parser* parser)
4548 enum tree_code code;
4550 tree cv_qualifier_seq;
4552 /* We don't know if there's a ptr-operator next, or not. */
4553 cp_parser_parse_tentatively (parser);
4554 /* Look for a ptr-operator. */
4555 code = cp_parser_ptr_operator (parser, &type, &cv_qualifier_seq);
4556 /* If that worked, look for more new-declarators. */
4557 if (cp_parser_parse_definitely (parser))
4561 /* Parse another optional declarator. */
4562 declarator = cp_parser_new_declarator_opt (parser);
4564 /* Create the representation of the declarator. */
4565 if (code == INDIRECT_REF)
4566 declarator = make_pointer_declarator (cv_qualifier_seq,
4569 declarator = make_reference_declarator (cv_qualifier_seq,
4572 /* Handle the pointer-to-member case. */
4574 declarator = build_nt (SCOPE_REF, type, declarator);
4579 /* If the next token is a `[', there is a direct-new-declarator. */
4580 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
4581 return cp_parser_direct_new_declarator (parser);
4586 /* Parse a direct-new-declarator.
4588 direct-new-declarator:
4590 direct-new-declarator [constant-expression]
4592 Returns an ARRAY_REF, following the same conventions as are
4593 documented for cp_parser_direct_declarator. */
4596 cp_parser_direct_new_declarator (cp_parser* parser)
4598 tree declarator = NULL_TREE;
4604 /* Look for the opening `['. */
4605 cp_parser_require (parser, CPP_OPEN_SQUARE, "`['");
4606 /* The first expression is not required to be constant. */
4609 expression = cp_parser_expression (parser);
4610 /* The standard requires that the expression have integral
4611 type. DR 74 adds enumeration types. We believe that the
4612 real intent is that these expressions be handled like the
4613 expression in a `switch' condition, which also allows
4614 classes with a single conversion to integral or
4615 enumeration type. */
4616 if (!processing_template_decl)
4619 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
4624 error ("expression in new-declarator must have integral or enumeration type");
4625 expression = error_mark_node;
4629 /* But all the other expressions must be. */
4632 = cp_parser_constant_expression (parser,
4633 /*allow_non_constant=*/false,
4635 /* Look for the closing `]'. */
4636 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4638 /* Add this bound to the declarator. */
4639 declarator = build_nt (ARRAY_REF, declarator, expression);
4641 /* If the next token is not a `[', then there are no more
4643 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
4650 /* Parse a new-initializer.
4653 ( expression-list [opt] )
4655 Returns a representation of the expression-list. If there is no
4656 expression-list, VOID_ZERO_NODE is returned. */
4659 cp_parser_new_initializer (cp_parser* parser)
4661 tree expression_list;
4663 expression_list = (cp_parser_parenthesized_expression_list
4664 (parser, false, /*non_constant_p=*/NULL));
4665 if (!expression_list)
4666 expression_list = void_zero_node;
4668 return expression_list;
4671 /* Parse a delete-expression.
4674 :: [opt] delete cast-expression
4675 :: [opt] delete [ ] cast-expression
4677 Returns a representation of the expression. */
4680 cp_parser_delete_expression (cp_parser* parser)
4682 bool global_scope_p;
4686 /* Look for the optional `::' operator. */
4688 = (cp_parser_global_scope_opt (parser,
4689 /*current_scope_valid_p=*/false)
4691 /* Look for the `delete' keyword. */
4692 cp_parser_require_keyword (parser, RID_DELETE, "`delete'");
4693 /* See if the array syntax is in use. */
4694 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
4696 /* Consume the `[' token. */
4697 cp_lexer_consume_token (parser->lexer);
4698 /* Look for the `]' token. */
4699 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4700 /* Remember that this is the `[]' construct. */
4706 /* Parse the cast-expression. */
4707 expression = cp_parser_simple_cast_expression (parser);
4709 /* A delete-expression may not appear in an integral constant
4711 if (cp_parser_non_integral_constant_expression (parser, "`delete'"))
4712 return error_mark_node;
4714 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
4717 /* Parse a cast-expression.
4721 ( type-id ) cast-expression
4723 Returns a representation of the expression. */
4726 cp_parser_cast_expression (cp_parser *parser, bool address_p)
4728 /* If it's a `(', then we might be looking at a cast. */
4729 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4731 tree type = NULL_TREE;
4732 tree expr = NULL_TREE;
4733 bool compound_literal_p;
4734 const char *saved_message;
4736 /* There's no way to know yet whether or not this is a cast.
4737 For example, `(int (3))' is a unary-expression, while `(int)
4738 3' is a cast. So, we resort to parsing tentatively. */
4739 cp_parser_parse_tentatively (parser);
4740 /* Types may not be defined in a cast. */
4741 saved_message = parser->type_definition_forbidden_message;
4742 parser->type_definition_forbidden_message
4743 = "types may not be defined in casts";
4744 /* Consume the `('. */
4745 cp_lexer_consume_token (parser->lexer);
4746 /* A very tricky bit is that `(struct S) { 3 }' is a
4747 compound-literal (which we permit in C++ as an extension).
4748 But, that construct is not a cast-expression -- it is a
4749 postfix-expression. (The reason is that `(struct S) { 3 }.i'
4750 is legal; if the compound-literal were a cast-expression,
4751 you'd need an extra set of parentheses.) But, if we parse
4752 the type-id, and it happens to be a class-specifier, then we
4753 will commit to the parse at that point, because we cannot
4754 undo the action that is done when creating a new class. So,
4755 then we cannot back up and do a postfix-expression.
4757 Therefore, we scan ahead to the closing `)', and check to see
4758 if the token after the `)' is a `{'. If so, we are not
4759 looking at a cast-expression.
4761 Save tokens so that we can put them back. */
4762 cp_lexer_save_tokens (parser->lexer);
4763 /* Skip tokens until the next token is a closing parenthesis.
4764 If we find the closing `)', and the next token is a `{', then
4765 we are looking at a compound-literal. */
4767 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
4768 /*consume_paren=*/true)
4769 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
4770 /* Roll back the tokens we skipped. */
4771 cp_lexer_rollback_tokens (parser->lexer);
4772 /* If we were looking at a compound-literal, simulate an error
4773 so that the call to cp_parser_parse_definitely below will
4775 if (compound_literal_p)
4776 cp_parser_simulate_error (parser);
4779 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4780 parser->in_type_id_in_expr_p = true;
4781 /* Look for the type-id. */
4782 type = cp_parser_type_id (parser);
4783 /* Look for the closing `)'. */
4784 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4785 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4788 /* Restore the saved message. */
4789 parser->type_definition_forbidden_message = saved_message;
4791 /* If ok so far, parse the dependent expression. We cannot be
4792 sure it is a cast. Consider `(T ())'. It is a parenthesized
4793 ctor of T, but looks like a cast to function returning T
4794 without a dependent expression. */
4795 if (!cp_parser_error_occurred (parser))
4796 expr = cp_parser_simple_cast_expression (parser);
4798 if (cp_parser_parse_definitely (parser))
4800 /* Warn about old-style casts, if so requested. */
4801 if (warn_old_style_cast
4802 && !in_system_header
4803 && !VOID_TYPE_P (type)
4804 && current_lang_name != lang_name_c)
4805 warning ("use of old-style cast");
4807 /* Only type conversions to integral or enumeration types
4808 can be used in constant-expressions. */
4809 if (parser->integral_constant_expression_p
4810 && !dependent_type_p (type)
4811 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type)
4812 && (cp_parser_non_integral_constant_expression
4814 "a casts to a type other than an integral or "
4815 "enumeration type")))
4816 return error_mark_node;
4818 /* Perform the cast. */
4819 expr = build_c_cast (type, expr);
4824 /* If we get here, then it's not a cast, so it must be a
4825 unary-expression. */
4826 return cp_parser_unary_expression (parser, address_p);
4829 /* Parse a pm-expression.
4833 pm-expression .* cast-expression
4834 pm-expression ->* cast-expression
4836 Returns a representation of the expression. */
4839 cp_parser_pm_expression (cp_parser* parser)
4841 static const cp_parser_token_tree_map map = {
4842 { CPP_DEREF_STAR, MEMBER_REF },
4843 { CPP_DOT_STAR, DOTSTAR_EXPR },
4844 { CPP_EOF, ERROR_MARK }
4847 return cp_parser_binary_expression (parser, map,
4848 cp_parser_simple_cast_expression);
4851 /* Parse a multiplicative-expression.
4853 mulitplicative-expression:
4855 multiplicative-expression * pm-expression
4856 multiplicative-expression / pm-expression
4857 multiplicative-expression % pm-expression
4859 Returns a representation of the expression. */
4862 cp_parser_multiplicative_expression (cp_parser* parser)
4864 static const cp_parser_token_tree_map map = {
4865 { CPP_MULT, MULT_EXPR },
4866 { CPP_DIV, TRUNC_DIV_EXPR },
4867 { CPP_MOD, TRUNC_MOD_EXPR },
4868 { CPP_EOF, ERROR_MARK }
4871 return cp_parser_binary_expression (parser,
4873 cp_parser_pm_expression);
4876 /* Parse an additive-expression.
4878 additive-expression:
4879 multiplicative-expression
4880 additive-expression + multiplicative-expression
4881 additive-expression - multiplicative-expression
4883 Returns a representation of the expression. */
4886 cp_parser_additive_expression (cp_parser* parser)
4888 static const cp_parser_token_tree_map map = {
4889 { CPP_PLUS, PLUS_EXPR },
4890 { CPP_MINUS, MINUS_EXPR },
4891 { CPP_EOF, ERROR_MARK }
4894 return cp_parser_binary_expression (parser,
4896 cp_parser_multiplicative_expression);
4899 /* Parse a shift-expression.
4903 shift-expression << additive-expression
4904 shift-expression >> additive-expression
4906 Returns a representation of the expression. */
4909 cp_parser_shift_expression (cp_parser* parser)
4911 static const cp_parser_token_tree_map map = {
4912 { CPP_LSHIFT, LSHIFT_EXPR },
4913 { CPP_RSHIFT, RSHIFT_EXPR },
4914 { CPP_EOF, ERROR_MARK }
4917 return cp_parser_binary_expression (parser,
4919 cp_parser_additive_expression);
4922 /* Parse a relational-expression.
4924 relational-expression:
4926 relational-expression < shift-expression
4927 relational-expression > shift-expression
4928 relational-expression <= shift-expression
4929 relational-expression >= shift-expression
4933 relational-expression:
4934 relational-expression <? shift-expression
4935 relational-expression >? shift-expression
4937 Returns a representation of the expression. */
4940 cp_parser_relational_expression (cp_parser* parser)
4942 static const cp_parser_token_tree_map map = {
4943 { CPP_LESS, LT_EXPR },
4944 { CPP_GREATER, GT_EXPR },
4945 { CPP_LESS_EQ, LE_EXPR },
4946 { CPP_GREATER_EQ, GE_EXPR },
4947 { CPP_MIN, MIN_EXPR },
4948 { CPP_MAX, MAX_EXPR },
4949 { CPP_EOF, ERROR_MARK }
4952 return cp_parser_binary_expression (parser,
4954 cp_parser_shift_expression);
4957 /* Parse an equality-expression.
4959 equality-expression:
4960 relational-expression
4961 equality-expression == relational-expression
4962 equality-expression != relational-expression
4964 Returns a representation of the expression. */
4967 cp_parser_equality_expression (cp_parser* parser)
4969 static const cp_parser_token_tree_map map = {
4970 { CPP_EQ_EQ, EQ_EXPR },
4971 { CPP_NOT_EQ, NE_EXPR },
4972 { CPP_EOF, ERROR_MARK }
4975 return cp_parser_binary_expression (parser,
4977 cp_parser_relational_expression);
4980 /* Parse an and-expression.
4984 and-expression & equality-expression
4986 Returns a representation of the expression. */
4989 cp_parser_and_expression (cp_parser* parser)
4991 static const cp_parser_token_tree_map map = {
4992 { CPP_AND, BIT_AND_EXPR },
4993 { CPP_EOF, ERROR_MARK }
4996 return cp_parser_binary_expression (parser,
4998 cp_parser_equality_expression);
5001 /* Parse an exclusive-or-expression.
5003 exclusive-or-expression:
5005 exclusive-or-expression ^ and-expression
5007 Returns a representation of the expression. */
5010 cp_parser_exclusive_or_expression (cp_parser* parser)
5012 static const cp_parser_token_tree_map map = {
5013 { CPP_XOR, BIT_XOR_EXPR },
5014 { CPP_EOF, ERROR_MARK }
5017 return cp_parser_binary_expression (parser,
5019 cp_parser_and_expression);
5023 /* Parse an inclusive-or-expression.
5025 inclusive-or-expression:
5026 exclusive-or-expression
5027 inclusive-or-expression | exclusive-or-expression
5029 Returns a representation of the expression. */
5032 cp_parser_inclusive_or_expression (cp_parser* parser)
5034 static const cp_parser_token_tree_map map = {
5035 { CPP_OR, BIT_IOR_EXPR },
5036 { CPP_EOF, ERROR_MARK }
5039 return cp_parser_binary_expression (parser,
5041 cp_parser_exclusive_or_expression);
5044 /* Parse a logical-and-expression.
5046 logical-and-expression:
5047 inclusive-or-expression
5048 logical-and-expression && inclusive-or-expression
5050 Returns a representation of the expression. */
5053 cp_parser_logical_and_expression (cp_parser* parser)
5055 static const cp_parser_token_tree_map map = {
5056 { CPP_AND_AND, TRUTH_ANDIF_EXPR },
5057 { CPP_EOF, ERROR_MARK }
5060 return cp_parser_binary_expression (parser,
5062 cp_parser_inclusive_or_expression);
5065 /* Parse a logical-or-expression.
5067 logical-or-expression:
5068 logical-and-expression
5069 logical-or-expression || logical-and-expression
5071 Returns a representation of the expression. */
5074 cp_parser_logical_or_expression (cp_parser* parser)
5076 static const cp_parser_token_tree_map map = {
5077 { CPP_OR_OR, TRUTH_ORIF_EXPR },
5078 { CPP_EOF, ERROR_MARK }
5081 return cp_parser_binary_expression (parser,
5083 cp_parser_logical_and_expression);
5086 /* Parse the `? expression : assignment-expression' part of a
5087 conditional-expression. The LOGICAL_OR_EXPR is the
5088 logical-or-expression that started the conditional-expression.
5089 Returns a representation of the entire conditional-expression.
5091 This routine is used by cp_parser_assignment_expression.
5093 ? expression : assignment-expression
5097 ? : assignment-expression */
5100 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
5103 tree assignment_expr;
5105 /* Consume the `?' token. */
5106 cp_lexer_consume_token (parser->lexer);
5107 if (cp_parser_allow_gnu_extensions_p (parser)
5108 && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
5109 /* Implicit true clause. */
5112 /* Parse the expression. */
5113 expr = cp_parser_expression (parser);
5115 /* The next token should be a `:'. */
5116 cp_parser_require (parser, CPP_COLON, "`:'");
5117 /* Parse the assignment-expression. */
5118 assignment_expr = cp_parser_assignment_expression (parser);
5120 /* Build the conditional-expression. */
5121 return build_x_conditional_expr (logical_or_expr,
5126 /* Parse an assignment-expression.
5128 assignment-expression:
5129 conditional-expression
5130 logical-or-expression assignment-operator assignment_expression
5133 Returns a representation for the expression. */
5136 cp_parser_assignment_expression (cp_parser* parser)
5140 /* If the next token is the `throw' keyword, then we're looking at
5141 a throw-expression. */
5142 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
5143 expr = cp_parser_throw_expression (parser);
5144 /* Otherwise, it must be that we are looking at a
5145 logical-or-expression. */
5148 /* Parse the logical-or-expression. */
5149 expr = cp_parser_logical_or_expression (parser);
5150 /* If the next token is a `?' then we're actually looking at a
5151 conditional-expression. */
5152 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
5153 return cp_parser_question_colon_clause (parser, expr);
5156 enum tree_code assignment_operator;
5158 /* If it's an assignment-operator, we're using the second
5161 = cp_parser_assignment_operator_opt (parser);
5162 if (assignment_operator != ERROR_MARK)
5166 /* Parse the right-hand side of the assignment. */
5167 rhs = cp_parser_assignment_expression (parser);
5168 /* An assignment may not appear in a
5169 constant-expression. */
5170 if (cp_parser_non_integral_constant_expression (parser,
5172 return error_mark_node;
5173 /* Build the assignment expression. */
5174 expr = build_x_modify_expr (expr,
5175 assignment_operator,
5184 /* Parse an (optional) assignment-operator.
5186 assignment-operator: one of
5187 = *= /= %= += -= >>= <<= &= ^= |=
5191 assignment-operator: one of
5194 If the next token is an assignment operator, the corresponding tree
5195 code is returned, and the token is consumed. For example, for
5196 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
5197 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
5198 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
5199 operator, ERROR_MARK is returned. */
5201 static enum tree_code
5202 cp_parser_assignment_operator_opt (cp_parser* parser)
5207 /* Peek at the next toen. */
5208 token = cp_lexer_peek_token (parser->lexer);
5210 switch (token->type)
5221 op = TRUNC_DIV_EXPR;
5225 op = TRUNC_MOD_EXPR;
5265 /* Nothing else is an assignment operator. */
5269 /* If it was an assignment operator, consume it. */
5270 if (op != ERROR_MARK)
5271 cp_lexer_consume_token (parser->lexer);
5276 /* Parse an expression.
5279 assignment-expression
5280 expression , assignment-expression
5282 Returns a representation of the expression. */
5285 cp_parser_expression (cp_parser* parser)
5287 tree expression = NULL_TREE;
5291 tree assignment_expression;
5293 /* Parse the next assignment-expression. */
5294 assignment_expression
5295 = cp_parser_assignment_expression (parser);
5296 /* If this is the first assignment-expression, we can just
5299 expression = assignment_expression;
5301 expression = build_x_compound_expr (expression,
5302 assignment_expression);
5303 /* If the next token is not a comma, then we are done with the
5305 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5307 /* Consume the `,'. */
5308 cp_lexer_consume_token (parser->lexer);
5309 /* A comma operator cannot appear in a constant-expression. */
5310 if (cp_parser_non_integral_constant_expression (parser,
5311 "a comma operator"))
5312 expression = error_mark_node;
5318 /* Parse a constant-expression.
5320 constant-expression:
5321 conditional-expression
5323 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
5324 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
5325 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
5326 is false, NON_CONSTANT_P should be NULL. */
5329 cp_parser_constant_expression (cp_parser* parser,
5330 bool allow_non_constant_p,
5331 bool *non_constant_p)
5333 bool saved_integral_constant_expression_p;
5334 bool saved_allow_non_integral_constant_expression_p;
5335 bool saved_non_integral_constant_expression_p;
5338 /* It might seem that we could simply parse the
5339 conditional-expression, and then check to see if it were
5340 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
5341 one that the compiler can figure out is constant, possibly after
5342 doing some simplifications or optimizations. The standard has a
5343 precise definition of constant-expression, and we must honor
5344 that, even though it is somewhat more restrictive.
5350 is not a legal declaration, because `(2, 3)' is not a
5351 constant-expression. The `,' operator is forbidden in a
5352 constant-expression. However, GCC's constant-folding machinery
5353 will fold this operation to an INTEGER_CST for `3'. */
5355 /* Save the old settings. */
5356 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
5357 saved_allow_non_integral_constant_expression_p
5358 = parser->allow_non_integral_constant_expression_p;
5359 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
5360 /* We are now parsing a constant-expression. */
5361 parser->integral_constant_expression_p = true;
5362 parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
5363 parser->non_integral_constant_expression_p = false;
5364 /* Although the grammar says "conditional-expression", we parse an
5365 "assignment-expression", which also permits "throw-expression"
5366 and the use of assignment operators. In the case that
5367 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
5368 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
5369 actually essential that we look for an assignment-expression.
5370 For example, cp_parser_initializer_clauses uses this function to
5371 determine whether a particular assignment-expression is in fact
5373 expression = cp_parser_assignment_expression (parser);
5374 /* Restore the old settings. */
5375 parser->integral_constant_expression_p = saved_integral_constant_expression_p;
5376 parser->allow_non_integral_constant_expression_p
5377 = saved_allow_non_integral_constant_expression_p;
5378 if (allow_non_constant_p)
5379 *non_constant_p = parser->non_integral_constant_expression_p;
5380 parser->non_integral_constant_expression_p = saved_non_integral_constant_expression_p;
5385 /* Statements [gram.stmt.stmt] */
5387 /* Parse a statement.
5391 expression-statement
5396 declaration-statement
5400 cp_parser_statement (cp_parser* parser, bool in_statement_expr_p)
5404 int statement_line_number;
5406 /* There is no statement yet. */
5407 statement = NULL_TREE;
5408 /* Peek at the next token. */
5409 token = cp_lexer_peek_token (parser->lexer);
5410 /* Remember the line number of the first token in the statement. */
5411 statement_line_number = token->location.line;
5412 /* If this is a keyword, then that will often determine what kind of
5413 statement we have. */
5414 if (token->type == CPP_KEYWORD)
5416 enum rid keyword = token->keyword;
5422 statement = cp_parser_labeled_statement (parser,
5423 in_statement_expr_p);
5428 statement = cp_parser_selection_statement (parser);
5434 statement = cp_parser_iteration_statement (parser);
5441 statement = cp_parser_jump_statement (parser);
5445 statement = cp_parser_try_block (parser);
5449 /* It might be a keyword like `int' that can start a
5450 declaration-statement. */
5454 else if (token->type == CPP_NAME)
5456 /* If the next token is a `:', then we are looking at a
5457 labeled-statement. */
5458 token = cp_lexer_peek_nth_token (parser->lexer, 2);
5459 if (token->type == CPP_COLON)
5460 statement = cp_parser_labeled_statement (parser, in_statement_expr_p);
5462 /* Anything that starts with a `{' must be a compound-statement. */
5463 else if (token->type == CPP_OPEN_BRACE)
5464 statement = cp_parser_compound_statement (parser, false);
5466 /* Everything else must be a declaration-statement or an
5467 expression-statement. Try for the declaration-statement
5468 first, unless we are looking at a `;', in which case we know that
5469 we have an expression-statement. */
5472 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
5474 cp_parser_parse_tentatively (parser);
5475 /* Try to parse the declaration-statement. */
5476 cp_parser_declaration_statement (parser);
5477 /* If that worked, we're done. */
5478 if (cp_parser_parse_definitely (parser))
5481 /* Look for an expression-statement instead. */
5482 statement = cp_parser_expression_statement (parser, in_statement_expr_p);
5485 /* Set the line number for the statement. */
5486 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
5487 STMT_LINENO (statement) = statement_line_number;
5490 /* Parse a labeled-statement.
5493 identifier : statement
5494 case constant-expression : statement
5500 case constant-expression ... constant-expression : statement
5502 Returns the new CASE_LABEL, for a `case' or `default' label. For
5503 an ordinary label, returns a LABEL_STMT. */
5506 cp_parser_labeled_statement (cp_parser* parser, bool in_statement_expr_p)
5509 tree statement = error_mark_node;
5511 /* The next token should be an identifier. */
5512 token = cp_lexer_peek_token (parser->lexer);
5513 if (token->type != CPP_NAME
5514 && token->type != CPP_KEYWORD)
5516 cp_parser_error (parser, "expected labeled-statement");
5517 return error_mark_node;
5520 switch (token->keyword)
5527 /* Consume the `case' token. */
5528 cp_lexer_consume_token (parser->lexer);
5529 /* Parse the constant-expression. */
5530 expr = cp_parser_constant_expression (parser,
5531 /*allow_non_constant_p=*/false,
5534 ellipsis = cp_lexer_peek_token (parser->lexer);
5535 if (ellipsis->type == CPP_ELLIPSIS)
5537 /* Consume the `...' token. */
5538 cp_lexer_consume_token (parser->lexer);
5540 cp_parser_constant_expression (parser,
5541 /*allow_non_constant_p=*/false,
5543 /* We don't need to emit warnings here, as the common code
5544 will do this for us. */
5547 expr_hi = NULL_TREE;
5549 if (!parser->in_switch_statement_p)
5550 error ("case label `%E' not within a switch statement", expr);
5552 statement = finish_case_label (expr, expr_hi);
5557 /* Consume the `default' token. */
5558 cp_lexer_consume_token (parser->lexer);
5559 if (!parser->in_switch_statement_p)
5560 error ("case label not within a switch statement");
5562 statement = finish_case_label (NULL_TREE, NULL_TREE);
5566 /* Anything else must be an ordinary label. */
5567 statement = finish_label_stmt (cp_parser_identifier (parser));
5571 /* Require the `:' token. */
5572 cp_parser_require (parser, CPP_COLON, "`:'");
5573 /* Parse the labeled statement. */
5574 cp_parser_statement (parser, in_statement_expr_p);
5576 /* Return the label, in the case of a `case' or `default' label. */
5580 /* Parse an expression-statement.
5582 expression-statement:
5585 Returns the new EXPR_STMT -- or NULL_TREE if the expression
5586 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
5587 indicates whether this expression-statement is part of an
5588 expression statement. */
5591 cp_parser_expression_statement (cp_parser* parser, bool in_statement_expr_p)
5593 tree statement = NULL_TREE;
5595 /* If the next token is a ';', then there is no expression
5597 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
5598 statement = cp_parser_expression (parser);
5600 /* Consume the final `;'. */
5601 cp_parser_consume_semicolon_at_end_of_statement (parser);
5603 if (in_statement_expr_p
5604 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
5606 /* This is the final expression statement of a statement
5608 statement = finish_stmt_expr_expr (statement);
5611 statement = finish_expr_stmt (statement);
5618 /* Parse a compound-statement.
5621 { statement-seq [opt] }
5623 Returns a COMPOUND_STMT representing the statement. */
5626 cp_parser_compound_statement (cp_parser *parser, bool in_statement_expr_p)
5630 /* Consume the `{'. */
5631 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
5632 return error_mark_node;
5633 /* Begin the compound-statement. */
5634 compound_stmt = begin_compound_stmt (/*has_no_scope=*/false);
5635 /* Parse an (optional) statement-seq. */
5636 cp_parser_statement_seq_opt (parser, in_statement_expr_p);
5637 /* Finish the compound-statement. */
5638 finish_compound_stmt (compound_stmt);
5639 /* Consume the `}'. */
5640 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
5642 return compound_stmt;
5645 /* Parse an (optional) statement-seq.
5649 statement-seq [opt] statement */
5652 cp_parser_statement_seq_opt (cp_parser* parser, bool in_statement_expr_p)
5654 /* Scan statements until there aren't any more. */
5657 /* If we're looking at a `}', then we've run out of statements. */
5658 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE)
5659 || cp_lexer_next_token_is (parser->lexer, CPP_EOF))
5662 /* Parse the statement. */
5663 cp_parser_statement (parser, in_statement_expr_p);
5667 /* Parse a selection-statement.
5669 selection-statement:
5670 if ( condition ) statement
5671 if ( condition ) statement else statement
5672 switch ( condition ) statement
5674 Returns the new IF_STMT or SWITCH_STMT. */
5677 cp_parser_selection_statement (cp_parser* parser)
5682 /* Peek at the next token. */
5683 token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
5685 /* See what kind of keyword it is. */
5686 keyword = token->keyword;
5695 /* Look for the `('. */
5696 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
5698 cp_parser_skip_to_end_of_statement (parser);
5699 return error_mark_node;
5702 /* Begin the selection-statement. */
5703 if (keyword == RID_IF)
5704 statement = begin_if_stmt ();
5706 statement = begin_switch_stmt ();
5708 /* Parse the condition. */
5709 condition = cp_parser_condition (parser);
5710 /* Look for the `)'. */
5711 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
5712 cp_parser_skip_to_closing_parenthesis (parser, true, false,
5713 /*consume_paren=*/true);
5715 if (keyword == RID_IF)
5719 /* Add the condition. */
5720 finish_if_stmt_cond (condition, statement);
5722 /* Parse the then-clause. */
5723 then_stmt = cp_parser_implicitly_scoped_statement (parser);
5724 finish_then_clause (statement);
5726 /* If the next token is `else', parse the else-clause. */
5727 if (cp_lexer_next_token_is_keyword (parser->lexer,
5732 /* Consume the `else' keyword. */
5733 cp_lexer_consume_token (parser->lexer);
5734 /* Parse the else-clause. */
5736 = cp_parser_implicitly_scoped_statement (parser);
5737 finish_else_clause (statement);
5740 /* Now we're all done with the if-statement. */
5746 bool in_switch_statement_p;
5748 /* Add the condition. */
5749 finish_switch_cond (condition, statement);
5751 /* Parse the body of the switch-statement. */
5752 in_switch_statement_p = parser->in_switch_statement_p;
5753 parser->in_switch_statement_p = true;
5754 body = cp_parser_implicitly_scoped_statement (parser);
5755 parser->in_switch_statement_p = in_switch_statement_p;
5757 /* Now we're all done with the switch-statement. */
5758 finish_switch_stmt (statement);
5766 cp_parser_error (parser, "expected selection-statement");
5767 return error_mark_node;
5771 /* Parse a condition.
5775 type-specifier-seq declarator = assignment-expression
5780 type-specifier-seq declarator asm-specification [opt]
5781 attributes [opt] = assignment-expression
5783 Returns the expression that should be tested. */
5786 cp_parser_condition (cp_parser* parser)
5788 tree type_specifiers;
5789 const char *saved_message;
5791 /* Try the declaration first. */
5792 cp_parser_parse_tentatively (parser);
5793 /* New types are not allowed in the type-specifier-seq for a
5795 saved_message = parser->type_definition_forbidden_message;
5796 parser->type_definition_forbidden_message
5797 = "types may not be defined in conditions";
5798 /* Parse the type-specifier-seq. */
5799 type_specifiers = cp_parser_type_specifier_seq (parser);
5800 /* Restore the saved message. */
5801 parser->type_definition_forbidden_message = saved_message;
5802 /* If all is well, we might be looking at a declaration. */
5803 if (!cp_parser_error_occurred (parser))
5806 tree asm_specification;
5809 tree initializer = NULL_TREE;
5811 /* Parse the declarator. */
5812 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
5813 /*ctor_dtor_or_conv_p=*/NULL,
5814 /*parenthesized_p=*/NULL);
5815 /* Parse the attributes. */
5816 attributes = cp_parser_attributes_opt (parser);
5817 /* Parse the asm-specification. */
5818 asm_specification = cp_parser_asm_specification_opt (parser);
5819 /* If the next token is not an `=', then we might still be
5820 looking at an expression. For example:
5824 looks like a decl-specifier-seq and a declarator -- but then
5825 there is no `=', so this is an expression. */
5826 cp_parser_require (parser, CPP_EQ, "`='");
5827 /* If we did see an `=', then we are looking at a declaration
5829 if (cp_parser_parse_definitely (parser))
5831 /* Create the declaration. */
5832 decl = start_decl (declarator, type_specifiers,
5833 /*initialized_p=*/true,
5834 attributes, /*prefix_attributes=*/NULL_TREE);
5835 /* Parse the assignment-expression. */
5836 initializer = cp_parser_assignment_expression (parser);
5838 /* Process the initializer. */
5839 cp_finish_decl (decl,
5842 LOOKUP_ONLYCONVERTING);
5844 return convert_from_reference (decl);
5847 /* If we didn't even get past the declarator successfully, we are
5848 definitely not looking at a declaration. */
5850 cp_parser_abort_tentative_parse (parser);
5852 /* Otherwise, we are looking at an expression. */
5853 return cp_parser_expression (parser);
5856 /* Parse an iteration-statement.
5858 iteration-statement:
5859 while ( condition ) statement
5860 do statement while ( expression ) ;
5861 for ( for-init-statement condition [opt] ; expression [opt] )
5864 Returns the new WHILE_STMT, DO_STMT, or FOR_STMT. */
5867 cp_parser_iteration_statement (cp_parser* parser)
5872 bool in_iteration_statement_p;
5875 /* Peek at the next token. */
5876 token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
5878 return error_mark_node;
5880 /* Remember whether or not we are already within an iteration
5882 in_iteration_statement_p = parser->in_iteration_statement_p;
5884 /* See what kind of keyword it is. */
5885 keyword = token->keyword;
5892 /* Begin the while-statement. */
5893 statement = begin_while_stmt ();
5894 /* Look for the `('. */
5895 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
5896 /* Parse the condition. */
5897 condition = cp_parser_condition (parser);
5898 finish_while_stmt_cond (condition, statement);
5899 /* Look for the `)'. */
5900 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5901 /* Parse the dependent statement. */
5902 parser->in_iteration_statement_p = true;
5903 cp_parser_already_scoped_statement (parser);
5904 parser->in_iteration_statement_p = in_iteration_statement_p;
5905 /* We're done with the while-statement. */
5906 finish_while_stmt (statement);
5914 /* Begin the do-statement. */
5915 statement = begin_do_stmt ();
5916 /* Parse the body of the do-statement. */
5917 parser->in_iteration_statement_p = true;
5918 cp_parser_implicitly_scoped_statement (parser);
5919 parser->in_iteration_statement_p = in_iteration_statement_p;
5920 finish_do_body (statement);
5921 /* Look for the `while' keyword. */
5922 cp_parser_require_keyword (parser, RID_WHILE, "`while'");
5923 /* Look for the `('. */
5924 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
5925 /* Parse the expression. */
5926 expression = cp_parser_expression (parser);
5927 /* We're done with the do-statement. */
5928 finish_do_stmt (expression, statement);
5929 /* Look for the `)'. */
5930 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5931 /* Look for the `;'. */
5932 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
5938 tree condition = NULL_TREE;
5939 tree expression = NULL_TREE;
5941 /* Begin the for-statement. */
5942 statement = begin_for_stmt ();
5943 /* Look for the `('. */
5944 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
5945 /* Parse the initialization. */
5946 cp_parser_for_init_statement (parser);
5947 finish_for_init_stmt (statement);
5949 /* If there's a condition, process it. */
5950 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
5951 condition = cp_parser_condition (parser);
5952 finish_for_cond (condition, statement);
5953 /* Look for the `;'. */
5954 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
5956 /* If there's an expression, process it. */
5957 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
5958 expression = cp_parser_expression (parser);
5959 finish_for_expr (expression, statement);
5960 /* Look for the `)'. */
5961 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5963 /* Parse the body of the for-statement. */
5964 parser->in_iteration_statement_p = true;
5965 cp_parser_already_scoped_statement (parser);
5966 parser->in_iteration_statement_p = in_iteration_statement_p;
5968 /* We're done with the for-statement. */
5969 finish_for_stmt (statement);
5974 cp_parser_error (parser, "expected iteration-statement");
5975 statement = error_mark_node;
5982 /* Parse a for-init-statement.
5985 expression-statement
5986 simple-declaration */
5989 cp_parser_for_init_statement (cp_parser* parser)
5991 /* If the next token is a `;', then we have an empty
5992 expression-statement. Grammatically, this is also a
5993 simple-declaration, but an invalid one, because it does not
5994 declare anything. Therefore, if we did not handle this case
5995 specially, we would issue an error message about an invalid
5997 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
5999 /* We're going to speculatively look for a declaration, falling back
6000 to an expression, if necessary. */
6001 cp_parser_parse_tentatively (parser);
6002 /* Parse the declaration. */
6003 cp_parser_simple_declaration (parser,
6004 /*function_definition_allowed_p=*/false);
6005 /* If the tentative parse failed, then we shall need to look for an
6006 expression-statement. */
6007 if (cp_parser_parse_definitely (parser))
6011 cp_parser_expression_statement (parser, false);
6014 /* Parse a jump-statement.
6019 return expression [opt] ;
6027 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_STMT, or
6031 cp_parser_jump_statement (cp_parser* parser)
6033 tree statement = error_mark_node;
6037 /* Peek at the next token. */
6038 token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
6040 return error_mark_node;
6042 /* See what kind of keyword it is. */
6043 keyword = token->keyword;
6047 if (!parser->in_switch_statement_p
6048 && !parser->in_iteration_statement_p)
6050 error ("break statement not within loop or switch");
6051 statement = error_mark_node;
6054 statement = finish_break_stmt ();
6055 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6059 if (!parser->in_iteration_statement_p)
6061 error ("continue statement not within a loop");
6062 statement = error_mark_node;
6065 statement = finish_continue_stmt ();
6066 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6073 /* If the next token is a `;', then there is no
6075 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6076 expr = cp_parser_expression (parser);
6079 /* Build the return-statement. */
6080 statement = finish_return_stmt (expr);
6081 /* Look for the final `;'. */
6082 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6087 /* Create the goto-statement. */
6088 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
6090 /* Issue a warning about this use of a GNU extension. */
6092 pedwarn ("ISO C++ forbids computed gotos");
6093 /* Consume the '*' token. */
6094 cp_lexer_consume_token (parser->lexer);
6095 /* Parse the dependent expression. */
6096 finish_goto_stmt (cp_parser_expression (parser));
6099 finish_goto_stmt (cp_parser_identifier (parser));
6100 /* Look for the final `;'. */
6101 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6105 cp_parser_error (parser, "expected jump-statement");
6112 /* Parse a declaration-statement.
6114 declaration-statement:
6115 block-declaration */
6118 cp_parser_declaration_statement (cp_parser* parser)
6120 /* Parse the block-declaration. */
6121 cp_parser_block_declaration (parser, /*statement_p=*/true);
6123 /* Finish off the statement. */
6127 /* Some dependent statements (like `if (cond) statement'), are
6128 implicitly in their own scope. In other words, if the statement is
6129 a single statement (as opposed to a compound-statement), it is
6130 none-the-less treated as if it were enclosed in braces. Any
6131 declarations appearing in the dependent statement are out of scope
6132 after control passes that point. This function parses a statement,
6133 but ensures that is in its own scope, even if it is not a
6136 Returns the new statement. */
6139 cp_parser_implicitly_scoped_statement (cp_parser* parser)
6143 /* If the token is not a `{', then we must take special action. */
6144 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
6146 /* Create a compound-statement. */
6147 statement = begin_compound_stmt (/*has_no_scope=*/false);
6148 /* Parse the dependent-statement. */
6149 cp_parser_statement (parser, false);
6150 /* Finish the dummy compound-statement. */
6151 finish_compound_stmt (statement);
6153 /* Otherwise, we simply parse the statement directly. */
6155 statement = cp_parser_compound_statement (parser, false);
6157 /* Return the statement. */
6161 /* For some dependent statements (like `while (cond) statement'), we
6162 have already created a scope. Therefore, even if the dependent
6163 statement is a compound-statement, we do not want to create another
6167 cp_parser_already_scoped_statement (cp_parser* parser)
6169 /* If the token is not a `{', then we must take special action. */
6170 if (cp_lexer_next_token_is_not(parser->lexer, CPP_OPEN_BRACE))
6174 /* Create a compound-statement. */
6175 statement = begin_compound_stmt (/*has_no_scope=*/true);
6176 /* Parse the dependent-statement. */
6177 cp_parser_statement (parser, false);
6178 /* Finish the dummy compound-statement. */
6179 finish_compound_stmt (statement);
6181 /* Otherwise, we simply parse the statement directly. */
6183 cp_parser_statement (parser, false);
6186 /* Declarations [gram.dcl.dcl] */
6188 /* Parse an optional declaration-sequence.
6192 declaration-seq declaration */
6195 cp_parser_declaration_seq_opt (cp_parser* parser)
6201 token = cp_lexer_peek_token (parser->lexer);
6203 if (token->type == CPP_CLOSE_BRACE
6204 || token->type == CPP_EOF)
6207 if (token->type == CPP_SEMICOLON)
6209 /* A declaration consisting of a single semicolon is
6210 invalid. Allow it unless we're being pedantic. */
6211 if (pedantic && !in_system_header)
6212 pedwarn ("extra `;'");
6213 cp_lexer_consume_token (parser->lexer);
6217 /* The C lexer modifies PENDING_LANG_CHANGE when it wants the
6218 parser to enter or exit implicit `extern "C"' blocks. */
6219 while (pending_lang_change > 0)
6221 push_lang_context (lang_name_c);
6222 --pending_lang_change;
6224 while (pending_lang_change < 0)
6226 pop_lang_context ();
6227 ++pending_lang_change;
6230 /* Parse the declaration itself. */
6231 cp_parser_declaration (parser);
6235 /* Parse a declaration.
6240 template-declaration
6241 explicit-instantiation
6242 explicit-specialization
6243 linkage-specification
6244 namespace-definition
6249 __extension__ declaration */
6252 cp_parser_declaration (cp_parser* parser)
6258 /* Check for the `__extension__' keyword. */
6259 if (cp_parser_extension_opt (parser, &saved_pedantic))
6261 /* Parse the qualified declaration. */
6262 cp_parser_declaration (parser);
6263 /* Restore the PEDANTIC flag. */
6264 pedantic = saved_pedantic;
6269 /* Try to figure out what kind of declaration is present. */
6270 token1 = *cp_lexer_peek_token (parser->lexer);
6271 if (token1.type != CPP_EOF)
6272 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
6274 /* If the next token is `extern' and the following token is a string
6275 literal, then we have a linkage specification. */
6276 if (token1.keyword == RID_EXTERN
6277 && cp_parser_is_string_literal (&token2))
6278 cp_parser_linkage_specification (parser);
6279 /* If the next token is `template', then we have either a template
6280 declaration, an explicit instantiation, or an explicit
6282 else if (token1.keyword == RID_TEMPLATE)
6284 /* `template <>' indicates a template specialization. */
6285 if (token2.type == CPP_LESS
6286 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
6287 cp_parser_explicit_specialization (parser);
6288 /* `template <' indicates a template declaration. */
6289 else if (token2.type == CPP_LESS)
6290 cp_parser_template_declaration (parser, /*member_p=*/false);
6291 /* Anything else must be an explicit instantiation. */
6293 cp_parser_explicit_instantiation (parser);
6295 /* If the next token is `export', then we have a template
6297 else if (token1.keyword == RID_EXPORT)
6298 cp_parser_template_declaration (parser, /*member_p=*/false);
6299 /* If the next token is `extern', 'static' or 'inline' and the one
6300 after that is `template', we have a GNU extended explicit
6301 instantiation directive. */
6302 else if (cp_parser_allow_gnu_extensions_p (parser)
6303 && (token1.keyword == RID_EXTERN
6304 || token1.keyword == RID_STATIC
6305 || token1.keyword == RID_INLINE)
6306 && token2.keyword == RID_TEMPLATE)
6307 cp_parser_explicit_instantiation (parser);
6308 /* If the next token is `namespace', check for a named or unnamed
6309 namespace definition. */
6310 else if (token1.keyword == RID_NAMESPACE
6311 && (/* A named namespace definition. */
6312 (token2.type == CPP_NAME
6313 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
6315 /* An unnamed namespace definition. */
6316 || token2.type == CPP_OPEN_BRACE))
6317 cp_parser_namespace_definition (parser);
6318 /* We must have either a block declaration or a function
6321 /* Try to parse a block-declaration, or a function-definition. */
6322 cp_parser_block_declaration (parser, /*statement_p=*/false);
6325 /* Parse a block-declaration.
6330 namespace-alias-definition
6337 __extension__ block-declaration
6340 If STATEMENT_P is TRUE, then this block-declaration is occurring as
6341 part of a declaration-statement. */
6344 cp_parser_block_declaration (cp_parser *parser,
6350 /* Check for the `__extension__' keyword. */
6351 if (cp_parser_extension_opt (parser, &saved_pedantic))
6353 /* Parse the qualified declaration. */
6354 cp_parser_block_declaration (parser, statement_p);
6355 /* Restore the PEDANTIC flag. */
6356 pedantic = saved_pedantic;
6361 /* Peek at the next token to figure out which kind of declaration is
6363 token1 = cp_lexer_peek_token (parser->lexer);
6365 /* If the next keyword is `asm', we have an asm-definition. */
6366 if (token1->keyword == RID_ASM)
6369 cp_parser_commit_to_tentative_parse (parser);
6370 cp_parser_asm_definition (parser);
6372 /* If the next keyword is `namespace', we have a
6373 namespace-alias-definition. */
6374 else if (token1->keyword == RID_NAMESPACE)
6375 cp_parser_namespace_alias_definition (parser);
6376 /* If the next keyword is `using', we have either a
6377 using-declaration or a using-directive. */
6378 else if (token1->keyword == RID_USING)
6383 cp_parser_commit_to_tentative_parse (parser);
6384 /* If the token after `using' is `namespace', then we have a
6386 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
6387 if (token2->keyword == RID_NAMESPACE)
6388 cp_parser_using_directive (parser);
6389 /* Otherwise, it's a using-declaration. */
6391 cp_parser_using_declaration (parser);
6393 /* If the next keyword is `__label__' we have a label declaration. */
6394 else if (token1->keyword == RID_LABEL)
6397 cp_parser_commit_to_tentative_parse (parser);
6398 cp_parser_label_declaration (parser);
6400 /* Anything else must be a simple-declaration. */
6402 cp_parser_simple_declaration (parser, !statement_p);
6405 /* Parse a simple-declaration.
6408 decl-specifier-seq [opt] init-declarator-list [opt] ;
6410 init-declarator-list:
6412 init-declarator-list , init-declarator
6414 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
6415 function-definition as a simple-declaration. */
6418 cp_parser_simple_declaration (cp_parser* parser,
6419 bool function_definition_allowed_p)
6421 tree decl_specifiers;
6423 int declares_class_or_enum;
6424 bool saw_declarator;
6426 /* Defer access checks until we know what is being declared; the
6427 checks for names appearing in the decl-specifier-seq should be
6428 done as if we were in the scope of the thing being declared. */
6429 push_deferring_access_checks (dk_deferred);
6431 /* Parse the decl-specifier-seq. We have to keep track of whether
6432 or not the decl-specifier-seq declares a named class or
6433 enumeration type, since that is the only case in which the
6434 init-declarator-list is allowed to be empty.
6438 In a simple-declaration, the optional init-declarator-list can be
6439 omitted only when declaring a class or enumeration, that is when
6440 the decl-specifier-seq contains either a class-specifier, an
6441 elaborated-type-specifier, or an enum-specifier. */
6443 = cp_parser_decl_specifier_seq (parser,
6444 CP_PARSER_FLAGS_OPTIONAL,
6446 &declares_class_or_enum);
6447 /* We no longer need to defer access checks. */
6448 stop_deferring_access_checks ();
6450 /* In a block scope, a valid declaration must always have a
6451 decl-specifier-seq. By not trying to parse declarators, we can
6452 resolve the declaration/expression ambiguity more quickly. */
6453 if (!function_definition_allowed_p && !decl_specifiers)
6455 cp_parser_error (parser, "expected declaration");
6459 /* If the next two tokens are both identifiers, the code is
6460 erroneous. The usual cause of this situation is code like:
6464 where "T" should name a type -- but does not. */
6465 if (cp_parser_diagnose_invalid_type_name (parser))
6467 /* If parsing tentatively, we should commit; we really are
6468 looking at a declaration. */
6469 cp_parser_commit_to_tentative_parse (parser);
6474 /* Keep going until we hit the `;' at the end of the simple
6476 saw_declarator = false;
6477 while (cp_lexer_next_token_is_not (parser->lexer,
6481 bool function_definition_p;
6484 saw_declarator = true;
6485 /* Parse the init-declarator. */
6486 decl = cp_parser_init_declarator (parser, decl_specifiers, attributes,
6487 function_definition_allowed_p,
6489 declares_class_or_enum,
6490 &function_definition_p);
6491 /* If an error occurred while parsing tentatively, exit quickly.
6492 (That usually happens when in the body of a function; each
6493 statement is treated as a declaration-statement until proven
6495 if (cp_parser_error_occurred (parser))
6497 /* Handle function definitions specially. */
6498 if (function_definition_p)
6500 /* If the next token is a `,', then we are probably
6501 processing something like:
6505 which is erroneous. */
6506 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
6507 error ("mixing declarations and function-definitions is forbidden");
6508 /* Otherwise, we're done with the list of declarators. */
6511 pop_deferring_access_checks ();
6515 /* The next token should be either a `,' or a `;'. */
6516 token = cp_lexer_peek_token (parser->lexer);
6517 /* If it's a `,', there are more declarators to come. */
6518 if (token->type == CPP_COMMA)
6519 cp_lexer_consume_token (parser->lexer);
6520 /* If it's a `;', we are done. */
6521 else if (token->type == CPP_SEMICOLON)
6523 /* Anything else is an error. */
6526 cp_parser_error (parser, "expected `,' or `;'");
6527 /* Skip tokens until we reach the end of the statement. */
6528 cp_parser_skip_to_end_of_statement (parser);
6529 /* If the next token is now a `;', consume it. */
6530 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
6531 cp_lexer_consume_token (parser->lexer);
6534 /* After the first time around, a function-definition is not
6535 allowed -- even if it was OK at first. For example:
6540 function_definition_allowed_p = false;
6543 /* Issue an error message if no declarators are present, and the
6544 decl-specifier-seq does not itself declare a class or
6546 if (!saw_declarator)
6548 if (cp_parser_declares_only_class_p (parser))
6549 shadow_tag (decl_specifiers);
6550 /* Perform any deferred access checks. */
6551 perform_deferred_access_checks ();
6554 /* Consume the `;'. */
6555 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6558 pop_deferring_access_checks ();
6561 /* Parse a decl-specifier-seq.
6564 decl-specifier-seq [opt] decl-specifier
6567 storage-class-specifier
6578 Returns a TREE_LIST, giving the decl-specifiers in the order they
6579 appear in the source code. The TREE_VALUE of each node is the
6580 decl-specifier. For a keyword (such as `auto' or `friend'), the
6581 TREE_VALUE is simply the corresponding TREE_IDENTIFIER. For the
6582 representation of a type-specifier, see cp_parser_type_specifier.
6584 If there are attributes, they will be stored in *ATTRIBUTES,
6585 represented as described above cp_parser_attributes.
6587 If FRIEND_IS_NOT_CLASS_P is non-NULL, and the `friend' specifier
6588 appears, and the entity that will be a friend is not going to be a
6589 class, then *FRIEND_IS_NOT_CLASS_P will be set to TRUE. Note that
6590 even if *FRIEND_IS_NOT_CLASS_P is FALSE, the entity to which
6591 friendship is granted might not be a class.
6593 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
6596 1: one of the decl-specifiers is an elaborated-type-specifier
6597 (i.e., a type declaration)
6598 2: one of the decl-specifiers is an enum-specifier or a
6599 class-specifier (i.e., a type definition)
6604 cp_parser_decl_specifier_seq (cp_parser* parser,
6605 cp_parser_flags flags,
6607 int* declares_class_or_enum)
6609 tree decl_specs = NULL_TREE;
6610 bool friend_p = false;
6611 bool constructor_possible_p = !parser->in_declarator_p;
6613 /* Assume no class or enumeration type is declared. */
6614 *declares_class_or_enum = 0;
6616 /* Assume there are no attributes. */
6617 *attributes = NULL_TREE;
6619 /* Keep reading specifiers until there are no more to read. */
6622 tree decl_spec = NULL_TREE;
6626 /* Peek at the next token. */
6627 token = cp_lexer_peek_token (parser->lexer);
6628 /* Handle attributes. */
6629 if (token->keyword == RID_ATTRIBUTE)
6631 /* Parse the attributes. */
6632 decl_spec = cp_parser_attributes_opt (parser);
6633 /* Add them to the list. */
6634 *attributes = chainon (*attributes, decl_spec);
6637 /* If the next token is an appropriate keyword, we can simply
6638 add it to the list. */
6639 switch (token->keyword)
6645 error ("duplicate `friend'");
6648 /* The representation of the specifier is simply the
6649 appropriate TREE_IDENTIFIER node. */
6650 decl_spec = token->value;
6651 /* Consume the token. */
6652 cp_lexer_consume_token (parser->lexer);
6655 /* function-specifier:
6662 decl_spec = cp_parser_function_specifier_opt (parser);
6668 /* The representation of the specifier is simply the
6669 appropriate TREE_IDENTIFIER node. */
6670 decl_spec = token->value;
6671 /* Consume the token. */
6672 cp_lexer_consume_token (parser->lexer);
6673 /* A constructor declarator cannot appear in a typedef. */
6674 constructor_possible_p = false;
6675 /* The "typedef" keyword can only occur in a declaration; we
6676 may as well commit at this point. */
6677 cp_parser_commit_to_tentative_parse (parser);
6680 /* storage-class-specifier:
6695 decl_spec = cp_parser_storage_class_specifier_opt (parser);
6702 /* Constructors are a special case. The `S' in `S()' is not a
6703 decl-specifier; it is the beginning of the declarator. */
6704 constructor_p = (!decl_spec
6705 && constructor_possible_p
6706 && cp_parser_constructor_declarator_p (parser,
6709 /* If we don't have a DECL_SPEC yet, then we must be looking at
6710 a type-specifier. */
6711 if (!decl_spec && !constructor_p)
6713 int decl_spec_declares_class_or_enum;
6714 bool is_cv_qualifier;
6717 = cp_parser_type_specifier (parser, flags,
6719 /*is_declaration=*/true,
6720 &decl_spec_declares_class_or_enum,
6723 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
6725 /* If this type-specifier referenced a user-defined type
6726 (a typedef, class-name, etc.), then we can't allow any
6727 more such type-specifiers henceforth.
6731 The longest sequence of decl-specifiers that could
6732 possibly be a type name is taken as the
6733 decl-specifier-seq of a declaration. The sequence shall
6734 be self-consistent as described below.
6738 As a general rule, at most one type-specifier is allowed
6739 in the complete decl-specifier-seq of a declaration. The
6740 only exceptions are the following:
6742 -- const or volatile can be combined with any other
6745 -- signed or unsigned can be combined with char, long,
6753 void g (const int Pc);
6755 Here, Pc is *not* part of the decl-specifier seq; it's
6756 the declarator. Therefore, once we see a type-specifier
6757 (other than a cv-qualifier), we forbid any additional
6758 user-defined types. We *do* still allow things like `int
6759 int' to be considered a decl-specifier-seq, and issue the
6760 error message later. */
6761 if (decl_spec && !is_cv_qualifier)
6762 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
6763 /* A constructor declarator cannot follow a type-specifier. */
6765 constructor_possible_p = false;
6768 /* If we still do not have a DECL_SPEC, then there are no more
6772 /* Issue an error message, unless the entire construct was
6774 if (!(flags & CP_PARSER_FLAGS_OPTIONAL))
6776 cp_parser_error (parser, "expected decl specifier");
6777 return error_mark_node;
6783 /* Add the DECL_SPEC to the list of specifiers. */
6784 if (decl_specs == NULL || TREE_VALUE (decl_specs) != error_mark_node)
6785 decl_specs = tree_cons (NULL_TREE, decl_spec, decl_specs);
6787 /* After we see one decl-specifier, further decl-specifiers are
6789 flags |= CP_PARSER_FLAGS_OPTIONAL;
6792 /* Don't allow a friend specifier with a class definition. */
6793 if (friend_p && (*declares_class_or_enum & 2))
6794 error ("class definition may not be declared a friend");
6796 /* We have built up the DECL_SPECS in reverse order. Return them in
6797 the correct order. */
6798 return nreverse (decl_specs);
6801 /* Parse an (optional) storage-class-specifier.
6803 storage-class-specifier:
6812 storage-class-specifier:
6815 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
6818 cp_parser_storage_class_specifier_opt (cp_parser* parser)
6820 switch (cp_lexer_peek_token (parser->lexer)->keyword)
6828 /* Consume the token. */
6829 return cp_lexer_consume_token (parser->lexer)->value;
6836 /* Parse an (optional) function-specifier.
6843 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
6846 cp_parser_function_specifier_opt (cp_parser* parser)
6848 switch (cp_lexer_peek_token (parser->lexer)->keyword)
6853 /* Consume the token. */
6854 return cp_lexer_consume_token (parser->lexer)->value;
6861 /* Parse a linkage-specification.
6863 linkage-specification:
6864 extern string-literal { declaration-seq [opt] }
6865 extern string-literal declaration */
6868 cp_parser_linkage_specification (cp_parser* parser)
6873 /* Look for the `extern' keyword. */
6874 cp_parser_require_keyword (parser, RID_EXTERN, "`extern'");
6876 /* Peek at the next token. */
6877 token = cp_lexer_peek_token (parser->lexer);
6878 /* If it's not a string-literal, then there's a problem. */
6879 if (!cp_parser_is_string_literal (token))
6881 cp_parser_error (parser, "expected language-name");
6884 /* Consume the token. */
6885 cp_lexer_consume_token (parser->lexer);
6887 /* Transform the literal into an identifier. If the literal is a
6888 wide-character string, or contains embedded NULs, then we can't
6889 handle it as the user wants. */
6890 if (token->type == CPP_WSTRING
6891 || (strlen (TREE_STRING_POINTER (token->value))
6892 != (size_t) (TREE_STRING_LENGTH (token->value) - 1)))
6894 cp_parser_error (parser, "invalid linkage-specification");
6895 /* Assume C++ linkage. */
6896 linkage = get_identifier ("c++");
6898 /* If it's a simple string constant, things are easier. */
6900 linkage = get_identifier (TREE_STRING_POINTER (token->value));
6902 /* We're now using the new linkage. */
6903 push_lang_context (linkage);
6905 /* If the next token is a `{', then we're using the first
6907 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6909 /* Consume the `{' token. */
6910 cp_lexer_consume_token (parser->lexer);
6911 /* Parse the declarations. */
6912 cp_parser_declaration_seq_opt (parser);
6913 /* Look for the closing `}'. */
6914 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
6916 /* Otherwise, there's just one declaration. */
6919 bool saved_in_unbraced_linkage_specification_p;
6921 saved_in_unbraced_linkage_specification_p
6922 = parser->in_unbraced_linkage_specification_p;
6923 parser->in_unbraced_linkage_specification_p = true;
6924 have_extern_spec = true;
6925 cp_parser_declaration (parser);
6926 have_extern_spec = false;
6927 parser->in_unbraced_linkage_specification_p
6928 = saved_in_unbraced_linkage_specification_p;
6931 /* We're done with the linkage-specification. */
6932 pop_lang_context ();
6935 /* Special member functions [gram.special] */
6937 /* Parse a conversion-function-id.
6939 conversion-function-id:
6940 operator conversion-type-id
6942 Returns an IDENTIFIER_NODE representing the operator. */
6945 cp_parser_conversion_function_id (cp_parser* parser)
6949 tree saved_qualifying_scope;
6950 tree saved_object_scope;
6953 /* Look for the `operator' token. */
6954 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
6955 return error_mark_node;
6956 /* When we parse the conversion-type-id, the current scope will be
6957 reset. However, we need that information in able to look up the
6958 conversion function later, so we save it here. */
6959 saved_scope = parser->scope;
6960 saved_qualifying_scope = parser->qualifying_scope;
6961 saved_object_scope = parser->object_scope;
6962 /* We must enter the scope of the class so that the names of
6963 entities declared within the class are available in the
6964 conversion-type-id. For example, consider:
6971 S::operator I() { ... }
6973 In order to see that `I' is a type-name in the definition, we
6974 must be in the scope of `S'. */
6976 pop_p = push_scope (saved_scope);
6977 /* Parse the conversion-type-id. */
6978 type = cp_parser_conversion_type_id (parser);
6979 /* Leave the scope of the class, if any. */
6981 pop_scope (saved_scope);
6982 /* Restore the saved scope. */
6983 parser->scope = saved_scope;
6984 parser->qualifying_scope = saved_qualifying_scope;
6985 parser->object_scope = saved_object_scope;
6986 /* If the TYPE is invalid, indicate failure. */
6987 if (type == error_mark_node)
6988 return error_mark_node;
6989 return mangle_conv_op_name_for_type (type);
6992 /* Parse a conversion-type-id:
6995 type-specifier-seq conversion-declarator [opt]
6997 Returns the TYPE specified. */
7000 cp_parser_conversion_type_id (cp_parser* parser)
7003 tree type_specifiers;
7006 /* Parse the attributes. */
7007 attributes = cp_parser_attributes_opt (parser);
7008 /* Parse the type-specifiers. */
7009 type_specifiers = cp_parser_type_specifier_seq (parser);
7010 /* If that didn't work, stop. */
7011 if (type_specifiers == error_mark_node)
7012 return error_mark_node;
7013 /* Parse the conversion-declarator. */
7014 declarator = cp_parser_conversion_declarator_opt (parser);
7016 return grokdeclarator (declarator, type_specifiers, TYPENAME,
7017 /*initialized=*/0, &attributes);
7020 /* Parse an (optional) conversion-declarator.
7022 conversion-declarator:
7023 ptr-operator conversion-declarator [opt]
7025 Returns a representation of the declarator. See
7026 cp_parser_declarator for details. */
7029 cp_parser_conversion_declarator_opt (cp_parser* parser)
7031 enum tree_code code;
7033 tree cv_qualifier_seq;
7035 /* We don't know if there's a ptr-operator next, or not. */
7036 cp_parser_parse_tentatively (parser);
7037 /* Try the ptr-operator. */
7038 code = cp_parser_ptr_operator (parser, &class_type,
7040 /* If it worked, look for more conversion-declarators. */
7041 if (cp_parser_parse_definitely (parser))
7045 /* Parse another optional declarator. */
7046 declarator = cp_parser_conversion_declarator_opt (parser);
7048 /* Create the representation of the declarator. */
7049 if (code == INDIRECT_REF)
7050 declarator = make_pointer_declarator (cv_qualifier_seq,
7053 declarator = make_reference_declarator (cv_qualifier_seq,
7056 /* Handle the pointer-to-member case. */
7058 declarator = build_nt (SCOPE_REF, class_type, declarator);
7066 /* Parse an (optional) ctor-initializer.
7069 : mem-initializer-list
7071 Returns TRUE iff the ctor-initializer was actually present. */
7074 cp_parser_ctor_initializer_opt (cp_parser* parser)
7076 /* If the next token is not a `:', then there is no
7077 ctor-initializer. */
7078 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
7080 /* Do default initialization of any bases and members. */
7081 if (DECL_CONSTRUCTOR_P (current_function_decl))
7082 finish_mem_initializers (NULL_TREE);
7087 /* Consume the `:' token. */
7088 cp_lexer_consume_token (parser->lexer);
7089 /* And the mem-initializer-list. */
7090 cp_parser_mem_initializer_list (parser);
7095 /* Parse a mem-initializer-list.
7097 mem-initializer-list:
7099 mem-initializer , mem-initializer-list */
7102 cp_parser_mem_initializer_list (cp_parser* parser)
7104 tree mem_initializer_list = NULL_TREE;
7106 /* Let the semantic analysis code know that we are starting the
7107 mem-initializer-list. */
7108 if (!DECL_CONSTRUCTOR_P (current_function_decl))
7109 error ("only constructors take base initializers");
7111 /* Loop through the list. */
7114 tree mem_initializer;
7116 /* Parse the mem-initializer. */
7117 mem_initializer = cp_parser_mem_initializer (parser);
7118 /* Add it to the list, unless it was erroneous. */
7119 if (mem_initializer)
7121 TREE_CHAIN (mem_initializer) = mem_initializer_list;
7122 mem_initializer_list = mem_initializer;
7124 /* If the next token is not a `,', we're done. */
7125 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7127 /* Consume the `,' token. */
7128 cp_lexer_consume_token (parser->lexer);
7131 /* Perform semantic analysis. */
7132 if (DECL_CONSTRUCTOR_P (current_function_decl))
7133 finish_mem_initializers (mem_initializer_list);
7136 /* Parse a mem-initializer.
7139 mem-initializer-id ( expression-list [opt] )
7144 ( expression-list [opt] )
7146 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
7147 class) or FIELD_DECL (for a non-static data member) to initialize;
7148 the TREE_VALUE is the expression-list. */
7151 cp_parser_mem_initializer (cp_parser* parser)
7153 tree mem_initializer_id;
7154 tree expression_list;
7157 /* Find out what is being initialized. */
7158 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7160 pedwarn ("anachronistic old-style base class initializer");
7161 mem_initializer_id = NULL_TREE;
7164 mem_initializer_id = cp_parser_mem_initializer_id (parser);
7165 member = expand_member_init (mem_initializer_id);
7166 if (member && !DECL_P (member))
7167 in_base_initializer = 1;
7170 = cp_parser_parenthesized_expression_list (parser, false,
7171 /*non_constant_p=*/NULL);
7172 if (!expression_list)
7173 expression_list = void_type_node;
7175 in_base_initializer = 0;
7177 return member ? build_tree_list (member, expression_list) : NULL_TREE;
7180 /* Parse a mem-initializer-id.
7183 :: [opt] nested-name-specifier [opt] class-name
7186 Returns a TYPE indicating the class to be initializer for the first
7187 production. Returns an IDENTIFIER_NODE indicating the data member
7188 to be initialized for the second production. */
7191 cp_parser_mem_initializer_id (cp_parser* parser)
7193 bool global_scope_p;
7194 bool nested_name_specifier_p;
7195 bool template_p = false;
7198 /* `typename' is not allowed in this context ([temp.res]). */
7199 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
7201 error ("keyword `typename' not allowed in this context (a qualified "
7202 "member initializer is implicitly a type)");
7203 cp_lexer_consume_token (parser->lexer);
7205 /* Look for the optional `::' operator. */
7207 = (cp_parser_global_scope_opt (parser,
7208 /*current_scope_valid_p=*/false)
7210 /* Look for the optional nested-name-specifier. The simplest way to
7215 The keyword `typename' is not permitted in a base-specifier or
7216 mem-initializer; in these contexts a qualified name that
7217 depends on a template-parameter is implicitly assumed to be a
7220 is to assume that we have seen the `typename' keyword at this
7222 nested_name_specifier_p
7223 = (cp_parser_nested_name_specifier_opt (parser,
7224 /*typename_keyword_p=*/true,
7225 /*check_dependency_p=*/true,
7227 /*is_declaration=*/true)
7229 if (nested_name_specifier_p)
7230 template_p = cp_parser_optional_template_keyword (parser);
7231 /* If there is a `::' operator or a nested-name-specifier, then we
7232 are definitely looking for a class-name. */
7233 if (global_scope_p || nested_name_specifier_p)
7234 return cp_parser_class_name (parser,
7235 /*typename_keyword_p=*/true,
7236 /*template_keyword_p=*/template_p,
7238 /*check_dependency_p=*/true,
7239 /*class_head_p=*/false,
7240 /*is_declaration=*/true);
7241 /* Otherwise, we could also be looking for an ordinary identifier. */
7242 cp_parser_parse_tentatively (parser);
7243 /* Try a class-name. */
7244 id = cp_parser_class_name (parser,
7245 /*typename_keyword_p=*/true,
7246 /*template_keyword_p=*/false,
7248 /*check_dependency_p=*/true,
7249 /*class_head_p=*/false,
7250 /*is_declaration=*/true);
7251 /* If we found one, we're done. */
7252 if (cp_parser_parse_definitely (parser))
7254 /* Otherwise, look for an ordinary identifier. */
7255 return cp_parser_identifier (parser);
7258 /* Overloading [gram.over] */
7260 /* Parse an operator-function-id.
7262 operator-function-id:
7265 Returns an IDENTIFIER_NODE for the operator which is a
7266 human-readable spelling of the identifier, e.g., `operator +'. */
7269 cp_parser_operator_function_id (cp_parser* parser)
7271 /* Look for the `operator' keyword. */
7272 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
7273 return error_mark_node;
7274 /* And then the name of the operator itself. */
7275 return cp_parser_operator (parser);
7278 /* Parse an operator.
7281 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
7282 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
7283 || ++ -- , ->* -> () []
7290 Returns an IDENTIFIER_NODE for the operator which is a
7291 human-readable spelling of the identifier, e.g., `operator +'. */
7294 cp_parser_operator (cp_parser* parser)
7296 tree id = NULL_TREE;
7299 /* Peek at the next token. */
7300 token = cp_lexer_peek_token (parser->lexer);
7301 /* Figure out which operator we have. */
7302 switch (token->type)
7308 /* The keyword should be either `new' or `delete'. */
7309 if (token->keyword == RID_NEW)
7311 else if (token->keyword == RID_DELETE)
7316 /* Consume the `new' or `delete' token. */
7317 cp_lexer_consume_token (parser->lexer);
7319 /* Peek at the next token. */
7320 token = cp_lexer_peek_token (parser->lexer);
7321 /* If it's a `[' token then this is the array variant of the
7323 if (token->type == CPP_OPEN_SQUARE)
7325 /* Consume the `[' token. */
7326 cp_lexer_consume_token (parser->lexer);
7327 /* Look for the `]' token. */
7328 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
7329 id = ansi_opname (op == NEW_EXPR
7330 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
7332 /* Otherwise, we have the non-array variant. */
7334 id = ansi_opname (op);
7340 id = ansi_opname (PLUS_EXPR);
7344 id = ansi_opname (MINUS_EXPR);
7348 id = ansi_opname (MULT_EXPR);
7352 id = ansi_opname (TRUNC_DIV_EXPR);
7356 id = ansi_opname (TRUNC_MOD_EXPR);
7360 id = ansi_opname (BIT_XOR_EXPR);
7364 id = ansi_opname (BIT_AND_EXPR);
7368 id = ansi_opname (BIT_IOR_EXPR);
7372 id = ansi_opname (BIT_NOT_EXPR);
7376 id = ansi_opname (TRUTH_NOT_EXPR);
7380 id = ansi_assopname (NOP_EXPR);
7384 id = ansi_opname (LT_EXPR);
7388 id = ansi_opname (GT_EXPR);
7392 id = ansi_assopname (PLUS_EXPR);
7396 id = ansi_assopname (MINUS_EXPR);
7400 id = ansi_assopname (MULT_EXPR);
7404 id = ansi_assopname (TRUNC_DIV_EXPR);
7408 id = ansi_assopname (TRUNC_MOD_EXPR);
7412 id = ansi_assopname (BIT_XOR_EXPR);
7416 id = ansi_assopname (BIT_AND_EXPR);
7420 id = ansi_assopname (BIT_IOR_EXPR);
7424 id = ansi_opname (LSHIFT_EXPR);
7428 id = ansi_opname (RSHIFT_EXPR);
7432 id = ansi_assopname (LSHIFT_EXPR);
7436 id = ansi_assopname (RSHIFT_EXPR);
7440 id = ansi_opname (EQ_EXPR);
7444 id = ansi_opname (NE_EXPR);
7448 id = ansi_opname (LE_EXPR);
7451 case CPP_GREATER_EQ:
7452 id = ansi_opname (GE_EXPR);
7456 id = ansi_opname (TRUTH_ANDIF_EXPR);
7460 id = ansi_opname (TRUTH_ORIF_EXPR);
7464 id = ansi_opname (POSTINCREMENT_EXPR);
7467 case CPP_MINUS_MINUS:
7468 id = ansi_opname (PREDECREMENT_EXPR);
7472 id = ansi_opname (COMPOUND_EXPR);
7475 case CPP_DEREF_STAR:
7476 id = ansi_opname (MEMBER_REF);
7480 id = ansi_opname (COMPONENT_REF);
7483 case CPP_OPEN_PAREN:
7484 /* Consume the `('. */
7485 cp_lexer_consume_token (parser->lexer);
7486 /* Look for the matching `)'. */
7487 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
7488 return ansi_opname (CALL_EXPR);
7490 case CPP_OPEN_SQUARE:
7491 /* Consume the `['. */
7492 cp_lexer_consume_token (parser->lexer);
7493 /* Look for the matching `]'. */
7494 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
7495 return ansi_opname (ARRAY_REF);
7499 id = ansi_opname (MIN_EXPR);
7503 id = ansi_opname (MAX_EXPR);
7507 id = ansi_assopname (MIN_EXPR);
7511 id = ansi_assopname (MAX_EXPR);
7515 /* Anything else is an error. */
7519 /* If we have selected an identifier, we need to consume the
7522 cp_lexer_consume_token (parser->lexer);
7523 /* Otherwise, no valid operator name was present. */
7526 cp_parser_error (parser, "expected operator");
7527 id = error_mark_node;
7533 /* Parse a template-declaration.
7535 template-declaration:
7536 export [opt] template < template-parameter-list > declaration
7538 If MEMBER_P is TRUE, this template-declaration occurs within a
7541 The grammar rule given by the standard isn't correct. What
7544 template-declaration:
7545 export [opt] template-parameter-list-seq
7546 decl-specifier-seq [opt] init-declarator [opt] ;
7547 export [opt] template-parameter-list-seq
7550 template-parameter-list-seq:
7551 template-parameter-list-seq [opt]
7552 template < template-parameter-list > */
7555 cp_parser_template_declaration (cp_parser* parser, bool member_p)
7557 /* Check for `export'. */
7558 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
7560 /* Consume the `export' token. */
7561 cp_lexer_consume_token (parser->lexer);
7562 /* Warn that we do not support `export'. */
7563 warning ("keyword `export' not implemented, and will be ignored");
7566 cp_parser_template_declaration_after_export (parser, member_p);
7569 /* Parse a template-parameter-list.
7571 template-parameter-list:
7573 template-parameter-list , template-parameter
7575 Returns a TREE_LIST. Each node represents a template parameter.
7576 The nodes are connected via their TREE_CHAINs. */
7579 cp_parser_template_parameter_list (cp_parser* parser)
7581 tree parameter_list = NULL_TREE;
7588 /* Parse the template-parameter. */
7589 parameter = cp_parser_template_parameter (parser);
7590 /* Add it to the list. */
7591 parameter_list = process_template_parm (parameter_list,
7594 /* Peek at the next token. */
7595 token = cp_lexer_peek_token (parser->lexer);
7596 /* If it's not a `,', we're done. */
7597 if (token->type != CPP_COMMA)
7599 /* Otherwise, consume the `,' token. */
7600 cp_lexer_consume_token (parser->lexer);
7603 return parameter_list;
7606 /* Parse a template-parameter.
7610 parameter-declaration
7612 Returns a TREE_LIST. The TREE_VALUE represents the parameter. The
7613 TREE_PURPOSE is the default value, if any. */
7616 cp_parser_template_parameter (cp_parser* parser)
7620 /* Peek at the next token. */
7621 token = cp_lexer_peek_token (parser->lexer);
7622 /* If it is `class' or `template', we have a type-parameter. */
7623 if (token->keyword == RID_TEMPLATE)
7624 return cp_parser_type_parameter (parser);
7625 /* If it is `class' or `typename' we do not know yet whether it is a
7626 type parameter or a non-type parameter. Consider:
7628 template <typename T, typename T::X X> ...
7632 template <class C, class D*> ...
7634 Here, the first parameter is a type parameter, and the second is
7635 a non-type parameter. We can tell by looking at the token after
7636 the identifier -- if it is a `,', `=', or `>' then we have a type
7638 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
7640 /* Peek at the token after `class' or `typename'. */
7641 token = cp_lexer_peek_nth_token (parser->lexer, 2);
7642 /* If it's an identifier, skip it. */
7643 if (token->type == CPP_NAME)
7644 token = cp_lexer_peek_nth_token (parser->lexer, 3);
7645 /* Now, see if the token looks like the end of a template
7647 if (token->type == CPP_COMMA
7648 || token->type == CPP_EQ
7649 || token->type == CPP_GREATER)
7650 return cp_parser_type_parameter (parser);
7653 /* Otherwise, it is a non-type parameter.
7657 When parsing a default template-argument for a non-type
7658 template-parameter, the first non-nested `>' is taken as the end
7659 of the template parameter-list rather than a greater-than
7662 cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
7663 /*parenthesized_p=*/NULL);
7666 /* Parse a type-parameter.
7669 class identifier [opt]
7670 class identifier [opt] = type-id
7671 typename identifier [opt]
7672 typename identifier [opt] = type-id
7673 template < template-parameter-list > class identifier [opt]
7674 template < template-parameter-list > class identifier [opt]
7677 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
7678 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
7679 the declaration of the parameter. */
7682 cp_parser_type_parameter (cp_parser* parser)
7687 /* Look for a keyword to tell us what kind of parameter this is. */
7688 token = cp_parser_require (parser, CPP_KEYWORD,
7689 "`class', `typename', or `template'");
7691 return error_mark_node;
7693 switch (token->keyword)
7699 tree default_argument;
7701 /* If the next token is an identifier, then it names the
7703 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
7704 identifier = cp_parser_identifier (parser);
7706 identifier = NULL_TREE;
7708 /* Create the parameter. */
7709 parameter = finish_template_type_parm (class_type_node, identifier);
7711 /* If the next token is an `=', we have a default argument. */
7712 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7714 /* Consume the `=' token. */
7715 cp_lexer_consume_token (parser->lexer);
7716 /* Parse the default-argument. */
7717 default_argument = cp_parser_type_id (parser);
7720 default_argument = NULL_TREE;
7722 /* Create the combined representation of the parameter and the
7723 default argument. */
7724 parameter = build_tree_list (default_argument, parameter);
7730 tree parameter_list;
7732 tree default_argument;
7734 /* Look for the `<'. */
7735 cp_parser_require (parser, CPP_LESS, "`<'");
7736 /* Parse the template-parameter-list. */
7737 begin_template_parm_list ();
7739 = cp_parser_template_parameter_list (parser);
7740 parameter_list = end_template_parm_list (parameter_list);
7741 /* Look for the `>'. */
7742 cp_parser_require (parser, CPP_GREATER, "`>'");
7743 /* Look for the `class' keyword. */
7744 cp_parser_require_keyword (parser, RID_CLASS, "`class'");
7745 /* If the next token is an `=', then there is a
7746 default-argument. If the next token is a `>', we are at
7747 the end of the parameter-list. If the next token is a `,',
7748 then we are at the end of this parameter. */
7749 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
7750 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
7751 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7752 identifier = cp_parser_identifier (parser);
7754 identifier = NULL_TREE;
7755 /* Create the template parameter. */
7756 parameter = finish_template_template_parm (class_type_node,
7759 /* If the next token is an `=', then there is a
7760 default-argument. */
7761 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7765 /* Consume the `='. */
7766 cp_lexer_consume_token (parser->lexer);
7767 /* Parse the id-expression. */
7769 = cp_parser_id_expression (parser,
7770 /*template_keyword_p=*/false,
7771 /*check_dependency_p=*/true,
7772 /*template_p=*/&is_template,
7773 /*declarator_p=*/false);
7774 if (TREE_CODE (default_argument) == TYPE_DECL)
7775 /* If the id-expression was a template-id that refers to
7776 a template-class, we already have the declaration here,
7777 so no further lookup is needed. */
7780 /* Look up the name. */
7782 = cp_parser_lookup_name (parser, default_argument,
7784 /*is_template=*/is_template,
7785 /*is_namespace=*/false,
7786 /*check_dependency=*/true);
7787 /* See if the default argument is valid. */
7789 = check_template_template_default_arg (default_argument);
7792 default_argument = NULL_TREE;
7794 /* Create the combined representation of the parameter and the
7795 default argument. */
7796 parameter = build_tree_list (default_argument, parameter);
7801 /* Anything else is an error. */
7802 cp_parser_error (parser,
7803 "expected `class', `typename', or `template'");
7804 parameter = error_mark_node;
7810 /* Parse a template-id.
7813 template-name < template-argument-list [opt] >
7815 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
7816 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
7817 returned. Otherwise, if the template-name names a function, or set
7818 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
7819 names a class, returns a TYPE_DECL for the specialization.
7821 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
7822 uninstantiated templates. */
7825 cp_parser_template_id (cp_parser *parser,
7826 bool template_keyword_p,
7827 bool check_dependency_p,
7828 bool is_declaration)
7833 ptrdiff_t start_of_id;
7834 tree access_check = NULL_TREE;
7835 cp_token *next_token, *next_token_2;
7838 /* If the next token corresponds to a template-id, there is no need
7840 next_token = cp_lexer_peek_token (parser->lexer);
7841 if (next_token->type == CPP_TEMPLATE_ID)
7846 /* Get the stored value. */
7847 value = cp_lexer_consume_token (parser->lexer)->value;
7848 /* Perform any access checks that were deferred. */
7849 for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
7850 perform_or_defer_access_check (TREE_PURPOSE (check),
7851 TREE_VALUE (check));
7852 /* Return the stored value. */
7853 return TREE_VALUE (value);
7856 /* Avoid performing name lookup if there is no possibility of
7857 finding a template-id. */
7858 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
7859 || (next_token->type == CPP_NAME
7860 && !cp_parser_nth_token_starts_template_argument_list_p
7863 cp_parser_error (parser, "expected template-id");
7864 return error_mark_node;
7867 /* Remember where the template-id starts. */
7868 if (cp_parser_parsing_tentatively (parser)
7869 && !cp_parser_committed_to_tentative_parse (parser))
7871 next_token = cp_lexer_peek_token (parser->lexer);
7872 start_of_id = cp_lexer_token_difference (parser->lexer,
7873 parser->lexer->first_token,
7879 push_deferring_access_checks (dk_deferred);
7881 /* Parse the template-name. */
7882 is_identifier = false;
7883 template = cp_parser_template_name (parser, template_keyword_p,
7887 if (template == error_mark_node || is_identifier)
7889 pop_deferring_access_checks ();
7893 /* If we find the sequence `[:' after a template-name, it's probably
7894 a digraph-typo for `< ::'. Substitute the tokens and check if we can
7895 parse correctly the argument list. */
7896 next_token = cp_lexer_peek_nth_token (parser->lexer, 1);
7897 next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
7898 if (next_token->type == CPP_OPEN_SQUARE
7899 && next_token->flags & DIGRAPH
7900 && next_token_2->type == CPP_COLON
7901 && !(next_token_2->flags & PREV_WHITE))
7903 cp_parser_parse_tentatively (parser);
7904 /* Change `:' into `::'. */
7905 next_token_2->type = CPP_SCOPE;
7906 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
7908 cp_lexer_consume_token (parser->lexer);
7909 /* Parse the arguments. */
7910 arguments = cp_parser_enclosed_template_argument_list (parser);
7911 if (!cp_parser_parse_definitely (parser))
7913 /* If we couldn't parse an argument list, then we revert our changes
7914 and return simply an error. Maybe this is not a template-id
7916 next_token_2->type = CPP_COLON;
7917 cp_parser_error (parser, "expected `<'");
7918 pop_deferring_access_checks ();
7919 return error_mark_node;
7921 /* Otherwise, emit an error about the invalid digraph, but continue
7922 parsing because we got our argument list. */
7923 pedwarn ("`<::' cannot begin a template-argument list");
7924 inform ("`<:' is an alternate spelling for `['. Insert whitespace "
7925 "between `<' and `::'");
7926 if (!flag_permissive)
7931 inform ("(if you use `-fpermissive' G++ will accept your code)");
7938 /* Look for the `<' that starts the template-argument-list. */
7939 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
7941 pop_deferring_access_checks ();
7942 return error_mark_node;
7944 /* Parse the arguments. */
7945 arguments = cp_parser_enclosed_template_argument_list (parser);
7948 /* Build a representation of the specialization. */
7949 if (TREE_CODE (template) == IDENTIFIER_NODE)
7950 template_id = build_min_nt (TEMPLATE_ID_EXPR, template, arguments);
7951 else if (DECL_CLASS_TEMPLATE_P (template)
7952 || DECL_TEMPLATE_TEMPLATE_PARM_P (template))
7954 = finish_template_type (template, arguments,
7955 cp_lexer_next_token_is (parser->lexer,
7959 /* If it's not a class-template or a template-template, it should be
7960 a function-template. */
7961 my_friendly_assert ((DECL_FUNCTION_TEMPLATE_P (template)
7962 || TREE_CODE (template) == OVERLOAD
7963 || BASELINK_P (template)),
7966 template_id = lookup_template_function (template, arguments);
7969 /* Retrieve any deferred checks. Do not pop this access checks yet
7970 so the memory will not be reclaimed during token replacing below. */
7971 access_check = get_deferred_access_checks ();
7973 /* If parsing tentatively, replace the sequence of tokens that makes
7974 up the template-id with a CPP_TEMPLATE_ID token. That way,
7975 should we re-parse the token stream, we will not have to repeat
7976 the effort required to do the parse, nor will we issue duplicate
7977 error messages about problems during instantiation of the
7979 if (start_of_id >= 0)
7983 /* Find the token that corresponds to the start of the
7985 token = cp_lexer_advance_token (parser->lexer,
7986 parser->lexer->first_token,
7989 /* Reset the contents of the START_OF_ID token. */
7990 token->type = CPP_TEMPLATE_ID;
7991 token->value = build_tree_list (access_check, template_id);
7992 token->keyword = RID_MAX;
7993 /* Purge all subsequent tokens. */
7994 cp_lexer_purge_tokens_after (parser->lexer, token);
7997 pop_deferring_access_checks ();
8001 /* Parse a template-name.
8006 The standard should actually say:
8010 operator-function-id
8012 A defect report has been filed about this issue.
8014 A conversion-function-id cannot be a template name because they cannot
8015 be part of a template-id. In fact, looking at this code:
8019 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
8020 It is impossible to call a templated conversion-function-id with an
8021 explicit argument list, since the only allowed template parameter is
8022 the type to which it is converting.
8024 If TEMPLATE_KEYWORD_P is true, then we have just seen the
8025 `template' keyword, in a construction like:
8029 In that case `f' is taken to be a template-name, even though there
8030 is no way of knowing for sure.
8032 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
8033 name refers to a set of overloaded functions, at least one of which
8034 is a template, or an IDENTIFIER_NODE with the name of the template,
8035 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
8036 names are looked up inside uninstantiated templates. */
8039 cp_parser_template_name (cp_parser* parser,
8040 bool template_keyword_p,
8041 bool check_dependency_p,
8042 bool is_declaration,
8043 bool *is_identifier)
8049 /* If the next token is `operator', then we have either an
8050 operator-function-id or a conversion-function-id. */
8051 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
8053 /* We don't know whether we're looking at an
8054 operator-function-id or a conversion-function-id. */
8055 cp_parser_parse_tentatively (parser);
8056 /* Try an operator-function-id. */
8057 identifier = cp_parser_operator_function_id (parser);
8058 /* If that didn't work, try a conversion-function-id. */
8059 if (!cp_parser_parse_definitely (parser))
8061 cp_parser_error (parser, "expected template-name");
8062 return error_mark_node;
8065 /* Look for the identifier. */
8067 identifier = cp_parser_identifier (parser);
8069 /* If we didn't find an identifier, we don't have a template-id. */
8070 if (identifier == error_mark_node)
8071 return error_mark_node;
8073 /* If the name immediately followed the `template' keyword, then it
8074 is a template-name. However, if the next token is not `<', then
8075 we do not treat it as a template-name, since it is not being used
8076 as part of a template-id. This enables us to handle constructs
8079 template <typename T> struct S { S(); };
8080 template <typename T> S<T>::S();
8082 correctly. We would treat `S' as a template -- if it were `S<T>'
8083 -- but we do not if there is no `<'. */
8085 if (processing_template_decl
8086 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
8088 /* In a declaration, in a dependent context, we pretend that the
8089 "template" keyword was present in order to improve error
8090 recovery. For example, given:
8092 template <typename T> void f(T::X<int>);
8094 we want to treat "X<int>" as a template-id. */
8096 && !template_keyword_p
8097 && parser->scope && TYPE_P (parser->scope)
8098 && dependent_type_p (parser->scope)
8099 /* Do not do this for dtors (or ctors), since they never
8100 need the template keyword before their name. */
8101 && !constructor_name_p (identifier, parser->scope))
8105 /* Explain what went wrong. */
8106 error ("non-template `%D' used as template", identifier);
8107 inform ("use `%T::template %D' to indicate that it is a template",
8108 parser->scope, identifier);
8109 /* If parsing tentatively, find the location of the "<"
8111 if (cp_parser_parsing_tentatively (parser)
8112 && !cp_parser_committed_to_tentative_parse (parser))
8114 cp_parser_simulate_error (parser);
8115 token = cp_lexer_peek_token (parser->lexer);
8116 token = cp_lexer_prev_token (parser->lexer, token);
8117 start = cp_lexer_token_difference (parser->lexer,
8118 parser->lexer->first_token,
8123 /* Parse the template arguments so that we can issue error
8124 messages about them. */
8125 cp_lexer_consume_token (parser->lexer);
8126 cp_parser_enclosed_template_argument_list (parser);
8127 /* Skip tokens until we find a good place from which to
8128 continue parsing. */
8129 cp_parser_skip_to_closing_parenthesis (parser,
8130 /*recovering=*/true,
8132 /*consume_paren=*/false);
8133 /* If parsing tentatively, permanently remove the
8134 template argument list. That will prevent duplicate
8135 error messages from being issued about the missing
8136 "template" keyword. */
8139 token = cp_lexer_advance_token (parser->lexer,
8140 parser->lexer->first_token,
8142 cp_lexer_purge_tokens_after (parser->lexer, token);
8145 *is_identifier = true;
8149 /* If the "template" keyword is present, then there is generally
8150 no point in doing name-lookup, so we just return IDENTIFIER.
8151 But, if the qualifying scope is non-dependent then we can
8152 (and must) do name-lookup normally. */
8153 if (template_keyword_p
8155 || (TYPE_P (parser->scope)
8156 && dependent_type_p (parser->scope))))
8160 /* Look up the name. */
8161 decl = cp_parser_lookup_name (parser, identifier,
8163 /*is_template=*/false,
8164 /*is_namespace=*/false,
8165 check_dependency_p);
8166 decl = maybe_get_template_decl_from_type_decl (decl);
8168 /* If DECL is a template, then the name was a template-name. */
8169 if (TREE_CODE (decl) == TEMPLATE_DECL)
8173 /* The standard does not explicitly indicate whether a name that
8174 names a set of overloaded declarations, some of which are
8175 templates, is a template-name. However, such a name should
8176 be a template-name; otherwise, there is no way to form a
8177 template-id for the overloaded templates. */
8178 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
8179 if (TREE_CODE (fns) == OVERLOAD)
8183 for (fn = fns; fn; fn = OVL_NEXT (fn))
8184 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
8189 /* Otherwise, the name does not name a template. */
8190 cp_parser_error (parser, "expected template-name");
8191 return error_mark_node;
8195 /* If DECL is dependent, and refers to a function, then just return
8196 its name; we will look it up again during template instantiation. */
8197 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
8199 tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
8200 if (TYPE_P (scope) && dependent_type_p (scope))
8207 /* Parse a template-argument-list.
8209 template-argument-list:
8211 template-argument-list , template-argument
8213 Returns a TREE_VEC containing the arguments. */
8216 cp_parser_template_argument_list (cp_parser* parser)
8218 tree fixed_args[10];
8219 unsigned n_args = 0;
8220 unsigned alloced = 10;
8221 tree *arg_ary = fixed_args;
8223 bool saved_in_template_argument_list_p;
8225 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
8226 parser->in_template_argument_list_p = true;
8232 /* Consume the comma. */
8233 cp_lexer_consume_token (parser->lexer);
8235 /* Parse the template-argument. */
8236 argument = cp_parser_template_argument (parser);
8237 if (n_args == alloced)
8241 if (arg_ary == fixed_args)
8243 arg_ary = xmalloc (sizeof (tree) * alloced);
8244 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
8247 arg_ary = xrealloc (arg_ary, sizeof (tree) * alloced);
8249 arg_ary[n_args++] = argument;
8251 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
8253 vec = make_tree_vec (n_args);
8256 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
8258 if (arg_ary != fixed_args)
8260 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
8264 /* Parse a template-argument.
8267 assignment-expression
8271 The representation is that of an assignment-expression, type-id, or
8272 id-expression -- except that the qualified id-expression is
8273 evaluated, so that the value returned is either a DECL or an
8276 Although the standard says "assignment-expression", it forbids
8277 throw-expressions or assignments in the template argument.
8278 Therefore, we use "conditional-expression" instead. */
8281 cp_parser_template_argument (cp_parser* parser)
8286 bool maybe_type_id = false;
8289 tree qualifying_class;
8291 /* There's really no way to know what we're looking at, so we just
8292 try each alternative in order.
8296 In a template-argument, an ambiguity between a type-id and an
8297 expression is resolved to a type-id, regardless of the form of
8298 the corresponding template-parameter.
8300 Therefore, we try a type-id first. */
8301 cp_parser_parse_tentatively (parser);
8302 argument = cp_parser_type_id (parser);
8303 /* If there was no error parsing the type-id but the next token is a '>>',
8304 we probably found a typo for '> >'. But there are type-id which are
8305 also valid expressions. For instance:
8307 struct X { int operator >> (int); };
8308 template <int V> struct Foo {};
8311 Here 'X()' is a valid type-id of a function type, but the user just
8312 wanted to write the expression "X() >> 5". Thus, we remember that we
8313 found a valid type-id, but we still try to parse the argument as an
8314 expression to see what happens. */
8315 if (!cp_parser_error_occurred (parser)
8316 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
8318 maybe_type_id = true;
8319 cp_parser_abort_tentative_parse (parser);
8323 /* If the next token isn't a `,' or a `>', then this argument wasn't
8324 really finished. This means that the argument is not a valid
8326 if (!cp_parser_next_token_ends_template_argument_p (parser))
8327 cp_parser_error (parser, "expected template-argument");
8328 /* If that worked, we're done. */
8329 if (cp_parser_parse_definitely (parser))
8332 /* We're still not sure what the argument will be. */
8333 cp_parser_parse_tentatively (parser);
8334 /* Try a template. */
8335 argument = cp_parser_id_expression (parser,
8336 /*template_keyword_p=*/false,
8337 /*check_dependency_p=*/true,
8339 /*declarator_p=*/false);
8340 /* If the next token isn't a `,' or a `>', then this argument wasn't
8342 if (!cp_parser_next_token_ends_template_argument_p (parser))
8343 cp_parser_error (parser, "expected template-argument");
8344 if (!cp_parser_error_occurred (parser))
8346 /* Figure out what is being referred to. If the id-expression
8347 was for a class template specialization, then we will have a
8348 TYPE_DECL at this point. There is no need to do name lookup
8349 at this point in that case. */
8350 if (TREE_CODE (argument) != TYPE_DECL)
8351 argument = cp_parser_lookup_name (parser, argument,
8353 /*is_template=*/template_p,
8354 /*is_namespace=*/false,
8355 /*check_dependency=*/true);
8356 if (TREE_CODE (argument) != TEMPLATE_DECL
8357 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
8358 cp_parser_error (parser, "expected template-name");
8360 if (cp_parser_parse_definitely (parser))
8362 /* It must be a non-type argument. There permitted cases are given
8363 in [temp.arg.nontype]:
8365 -- an integral constant-expression of integral or enumeration
8368 -- the name of a non-type template-parameter; or
8370 -- the name of an object or function with external linkage...
8372 -- the address of an object or function with external linkage...
8374 -- a pointer to member... */
8375 /* Look for a non-type template parameter. */
8376 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
8378 cp_parser_parse_tentatively (parser);
8379 argument = cp_parser_primary_expression (parser,
8382 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
8383 || !cp_parser_next_token_ends_template_argument_p (parser))
8384 cp_parser_simulate_error (parser);
8385 if (cp_parser_parse_definitely (parser))
8388 /* If the next token is "&", the argument must be the address of an
8389 object or function with external linkage. */
8390 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
8392 cp_lexer_consume_token (parser->lexer);
8393 /* See if we might have an id-expression. */
8394 token = cp_lexer_peek_token (parser->lexer);
8395 if (token->type == CPP_NAME
8396 || token->keyword == RID_OPERATOR
8397 || token->type == CPP_SCOPE
8398 || token->type == CPP_TEMPLATE_ID
8399 || token->type == CPP_NESTED_NAME_SPECIFIER)
8401 cp_parser_parse_tentatively (parser);
8402 argument = cp_parser_primary_expression (parser,
8405 if (cp_parser_error_occurred (parser)
8406 || !cp_parser_next_token_ends_template_argument_p (parser))
8407 cp_parser_abort_tentative_parse (parser);
8410 if (qualifying_class)
8411 argument = finish_qualified_id_expr (qualifying_class,
8415 if (TREE_CODE (argument) == VAR_DECL)
8417 /* A variable without external linkage might still be a
8418 valid constant-expression, so no error is issued here
8419 if the external-linkage check fails. */
8420 if (!DECL_EXTERNAL_LINKAGE_P (argument))
8421 cp_parser_simulate_error (parser);
8423 else if (is_overloaded_fn (argument))
8424 /* All overloaded functions are allowed; if the external
8425 linkage test does not pass, an error will be issued
8429 && (TREE_CODE (argument) == OFFSET_REF
8430 || TREE_CODE (argument) == SCOPE_REF))
8431 /* A pointer-to-member. */
8434 cp_parser_simulate_error (parser);
8436 if (cp_parser_parse_definitely (parser))
8439 argument = build_x_unary_op (ADDR_EXPR, argument);
8444 /* If the argument started with "&", there are no other valid
8445 alternatives at this point. */
8448 cp_parser_error (parser, "invalid non-type template argument");
8449 return error_mark_node;
8451 /* If the argument wasn't successfully parsed as a type-id followed
8452 by '>>', the argument can only be a constant expression now.
8453 Otherwise, we try parsing the constant-expression tentatively,
8454 because the argument could really be a type-id. */
8456 cp_parser_parse_tentatively (parser);
8457 argument = cp_parser_constant_expression (parser,
8458 /*allow_non_constant_p=*/false,
8459 /*non_constant_p=*/NULL);
8460 argument = fold_non_dependent_expr (argument);
8463 if (!cp_parser_next_token_ends_template_argument_p (parser))
8464 cp_parser_error (parser, "expected template-argument");
8465 if (cp_parser_parse_definitely (parser))
8467 /* We did our best to parse the argument as a non type-id, but that
8468 was the only alternative that matched (albeit with a '>' after
8469 it). We can assume it's just a typo from the user, and a
8470 diagnostic will then be issued. */
8471 return cp_parser_type_id (parser);
8474 /* Parse an explicit-instantiation.
8476 explicit-instantiation:
8477 template declaration
8479 Although the standard says `declaration', what it really means is:
8481 explicit-instantiation:
8482 template decl-specifier-seq [opt] declarator [opt] ;
8484 Things like `template int S<int>::i = 5, int S<double>::j;' are not
8485 supposed to be allowed. A defect report has been filed about this
8490 explicit-instantiation:
8491 storage-class-specifier template
8492 decl-specifier-seq [opt] declarator [opt] ;
8493 function-specifier template
8494 decl-specifier-seq [opt] declarator [opt] ; */
8497 cp_parser_explicit_instantiation (cp_parser* parser)
8499 int declares_class_or_enum;
8500 tree decl_specifiers;
8502 tree extension_specifier = NULL_TREE;
8504 /* Look for an (optional) storage-class-specifier or
8505 function-specifier. */
8506 if (cp_parser_allow_gnu_extensions_p (parser))
8509 = cp_parser_storage_class_specifier_opt (parser);
8510 if (!extension_specifier)
8511 extension_specifier = cp_parser_function_specifier_opt (parser);
8514 /* Look for the `template' keyword. */
8515 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
8516 /* Let the front end know that we are processing an explicit
8518 begin_explicit_instantiation ();
8519 /* [temp.explicit] says that we are supposed to ignore access
8520 control while processing explicit instantiation directives. */
8521 push_deferring_access_checks (dk_no_check);
8522 /* Parse a decl-specifier-seq. */
8524 = cp_parser_decl_specifier_seq (parser,
8525 CP_PARSER_FLAGS_OPTIONAL,
8527 &declares_class_or_enum);
8528 /* If there was exactly one decl-specifier, and it declared a class,
8529 and there's no declarator, then we have an explicit type
8531 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
8535 type = check_tag_decl (decl_specifiers);
8536 /* Turn access control back on for names used during
8537 template instantiation. */
8538 pop_deferring_access_checks ();
8540 do_type_instantiation (type, extension_specifier, /*complain=*/1);
8547 /* Parse the declarator. */
8549 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
8550 /*ctor_dtor_or_conv_p=*/NULL,
8551 /*parenthesized_p=*/NULL);
8552 cp_parser_check_for_definition_in_return_type (declarator,
8553 declares_class_or_enum);
8554 if (declarator != error_mark_node)
8556 decl = grokdeclarator (declarator, decl_specifiers,
8558 /* Turn access control back on for names used during
8559 template instantiation. */
8560 pop_deferring_access_checks ();
8561 /* Do the explicit instantiation. */
8562 do_decl_instantiation (decl, extension_specifier);
8566 pop_deferring_access_checks ();
8567 /* Skip the body of the explicit instantiation. */
8568 cp_parser_skip_to_end_of_statement (parser);
8571 /* We're done with the instantiation. */
8572 end_explicit_instantiation ();
8574 cp_parser_consume_semicolon_at_end_of_statement (parser);
8577 /* Parse an explicit-specialization.
8579 explicit-specialization:
8580 template < > declaration
8582 Although the standard says `declaration', what it really means is:
8584 explicit-specialization:
8585 template <> decl-specifier [opt] init-declarator [opt] ;
8586 template <> function-definition
8587 template <> explicit-specialization
8588 template <> template-declaration */
8591 cp_parser_explicit_specialization (cp_parser* parser)
8593 /* Look for the `template' keyword. */
8594 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
8595 /* Look for the `<'. */
8596 cp_parser_require (parser, CPP_LESS, "`<'");
8597 /* Look for the `>'. */
8598 cp_parser_require (parser, CPP_GREATER, "`>'");
8599 /* We have processed another parameter list. */
8600 ++parser->num_template_parameter_lists;
8601 /* Let the front end know that we are beginning a specialization. */
8602 begin_specialization ();
8604 /* If the next keyword is `template', we need to figure out whether
8605 or not we're looking a template-declaration. */
8606 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
8608 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
8609 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
8610 cp_parser_template_declaration_after_export (parser,
8611 /*member_p=*/false);
8613 cp_parser_explicit_specialization (parser);
8616 /* Parse the dependent declaration. */
8617 cp_parser_single_declaration (parser,
8621 /* We're done with the specialization. */
8622 end_specialization ();
8623 /* We're done with this parameter list. */
8624 --parser->num_template_parameter_lists;
8627 /* Parse a type-specifier.
8630 simple-type-specifier
8633 elaborated-type-specifier
8641 Returns a representation of the type-specifier. If the
8642 type-specifier is a keyword (like `int' or `const', or
8643 `__complex__') then the corresponding IDENTIFIER_NODE is returned.
8644 For a class-specifier, enum-specifier, or elaborated-type-specifier
8645 a TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
8647 If IS_FRIEND is TRUE then this type-specifier is being declared a
8648 `friend'. If IS_DECLARATION is TRUE, then this type-specifier is
8649 appearing in a decl-specifier-seq.
8651 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
8652 class-specifier, enum-specifier, or elaborated-type-specifier, then
8653 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
8654 if a type is declared; 2 if it is defined. Otherwise, it is set to
8657 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
8658 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
8662 cp_parser_type_specifier (cp_parser* parser,
8663 cp_parser_flags flags,
8665 bool is_declaration,
8666 int* declares_class_or_enum,
8667 bool* is_cv_qualifier)
8669 tree type_spec = NULL_TREE;
8673 /* Assume this type-specifier does not declare a new type. */
8674 if (declares_class_or_enum)
8675 *declares_class_or_enum = 0;
8676 /* And that it does not specify a cv-qualifier. */
8677 if (is_cv_qualifier)
8678 *is_cv_qualifier = false;
8679 /* Peek at the next token. */
8680 token = cp_lexer_peek_token (parser->lexer);
8682 /* If we're looking at a keyword, we can use that to guide the
8683 production we choose. */
8684 keyword = token->keyword;
8687 /* Any of these indicate either a class-specifier, or an
8688 elaborated-type-specifier. */
8693 /* Parse tentatively so that we can back up if we don't find a
8694 class-specifier or enum-specifier. */
8695 cp_parser_parse_tentatively (parser);
8696 /* Look for the class-specifier or enum-specifier. */
8697 if (keyword == RID_ENUM)
8698 type_spec = cp_parser_enum_specifier (parser);
8700 type_spec = cp_parser_class_specifier (parser);
8702 /* If that worked, we're done. */
8703 if (cp_parser_parse_definitely (parser))
8705 if (declares_class_or_enum)
8706 *declares_class_or_enum = 2;
8713 /* Look for an elaborated-type-specifier. */
8714 type_spec = cp_parser_elaborated_type_specifier (parser,
8717 /* We're declaring a class or enum -- unless we're using
8719 if (declares_class_or_enum && keyword != RID_TYPENAME)
8720 *declares_class_or_enum = 1;
8726 type_spec = cp_parser_cv_qualifier_opt (parser);
8727 /* Even though we call a routine that looks for an optional
8728 qualifier, we know that there should be one. */
8729 my_friendly_assert (type_spec != NULL, 20000328);
8730 /* This type-specifier was a cv-qualified. */
8731 if (is_cv_qualifier)
8732 *is_cv_qualifier = true;
8737 /* The `__complex__' keyword is a GNU extension. */
8738 return cp_lexer_consume_token (parser->lexer)->value;
8744 /* If we do not already have a type-specifier, assume we are looking
8745 at a simple-type-specifier. */
8746 type_spec = cp_parser_simple_type_specifier (parser, flags,
8747 /*identifier_p=*/true);
8749 /* If we didn't find a type-specifier, and a type-specifier was not
8750 optional in this context, issue an error message. */
8751 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
8753 cp_parser_error (parser, "expected type specifier");
8754 return error_mark_node;
8760 /* Parse a simple-type-specifier.
8762 simple-type-specifier:
8763 :: [opt] nested-name-specifier [opt] type-name
8764 :: [opt] nested-name-specifier template template-id
8779 simple-type-specifier:
8780 __typeof__ unary-expression
8781 __typeof__ ( type-id )
8783 For the various keywords, the value returned is simply the
8784 TREE_IDENTIFIER representing the keyword if IDENTIFIER_P is true.
8785 For the first two productions, and if IDENTIFIER_P is false, the
8786 value returned is the indicated TYPE_DECL. */
8789 cp_parser_simple_type_specifier (cp_parser* parser, cp_parser_flags flags,
8792 tree type = NULL_TREE;
8795 /* Peek at the next token. */
8796 token = cp_lexer_peek_token (parser->lexer);
8798 /* If we're looking at a keyword, things are easy. */
8799 switch (token->keyword)
8802 type = char_type_node;
8805 type = wchar_type_node;
8808 type = boolean_type_node;
8811 type = short_integer_type_node;
8814 type = integer_type_node;
8817 type = long_integer_type_node;
8820 type = integer_type_node;
8823 type = unsigned_type_node;
8826 type = float_type_node;
8829 type = double_type_node;
8832 type = void_type_node;
8839 /* Consume the `typeof' token. */
8840 cp_lexer_consume_token (parser->lexer);
8841 /* Parse the operand to `typeof'. */
8842 operand = cp_parser_sizeof_operand (parser, RID_TYPEOF);
8843 /* If it is not already a TYPE, take its type. */
8844 if (!TYPE_P (operand))
8845 operand = finish_typeof (operand);
8854 /* If the type-specifier was for a built-in type, we're done. */
8859 /* Consume the token. */
8860 id = cp_lexer_consume_token (parser->lexer)->value;
8862 /* There is no valid C++ program where a non-template type is
8863 followed by a "<". That usually indicates that the user thought
8864 that the type was a template. */
8865 cp_parser_check_for_invalid_template_id (parser, type);
8867 return identifier_p ? id : TYPE_NAME (type);
8870 /* The type-specifier must be a user-defined type. */
8871 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
8875 /* Don't gobble tokens or issue error messages if this is an
8876 optional type-specifier. */
8877 if (flags & CP_PARSER_FLAGS_OPTIONAL)
8878 cp_parser_parse_tentatively (parser);
8880 /* Look for the optional `::' operator. */
8881 cp_parser_global_scope_opt (parser,
8882 /*current_scope_valid_p=*/false);
8883 /* Look for the nested-name specifier. */
8885 = (cp_parser_nested_name_specifier_opt (parser,
8886 /*typename_keyword_p=*/false,
8887 /*check_dependency_p=*/true,
8889 /*is_declaration=*/false)
8891 /* If we have seen a nested-name-specifier, and the next token
8892 is `template', then we are using the template-id production. */
8894 && cp_parser_optional_template_keyword (parser))
8896 /* Look for the template-id. */
8897 type = cp_parser_template_id (parser,
8898 /*template_keyword_p=*/true,
8899 /*check_dependency_p=*/true,
8900 /*is_declaration=*/false);
8901 /* If the template-id did not name a type, we are out of
8903 if (TREE_CODE (type) != TYPE_DECL)
8905 cp_parser_error (parser, "expected template-id for type");
8909 /* Otherwise, look for a type-name. */
8911 type = cp_parser_type_name (parser);
8912 /* Keep track of all name-lookups performed in class scopes. */
8915 && TREE_CODE (type) == TYPE_DECL
8916 && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
8917 maybe_note_name_used_in_class (DECL_NAME (type), type);
8918 /* If it didn't work out, we don't have a TYPE. */
8919 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
8920 && !cp_parser_parse_definitely (parser))
8924 /* If we didn't get a type-name, issue an error message. */
8925 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
8927 cp_parser_error (parser, "expected type-name");
8928 return error_mark_node;
8931 /* There is no valid C++ program where a non-template type is
8932 followed by a "<". That usually indicates that the user thought
8933 that the type was a template. */
8934 if (type && type != error_mark_node)
8935 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type));
8940 /* Parse a type-name.
8953 Returns a TYPE_DECL for the the type. */
8956 cp_parser_type_name (cp_parser* parser)
8961 /* We can't know yet whether it is a class-name or not. */
8962 cp_parser_parse_tentatively (parser);
8963 /* Try a class-name. */
8964 type_decl = cp_parser_class_name (parser,
8965 /*typename_keyword_p=*/false,
8966 /*template_keyword_p=*/false,
8968 /*check_dependency_p=*/true,
8969 /*class_head_p=*/false,
8970 /*is_declaration=*/false);
8971 /* If it's not a class-name, keep looking. */
8972 if (!cp_parser_parse_definitely (parser))
8974 /* It must be a typedef-name or an enum-name. */
8975 identifier = cp_parser_identifier (parser);
8976 if (identifier == error_mark_node)
8977 return error_mark_node;
8979 /* Look up the type-name. */
8980 type_decl = cp_parser_lookup_name_simple (parser, identifier);
8981 /* Issue an error if we did not find a type-name. */
8982 if (TREE_CODE (type_decl) != TYPE_DECL)
8984 if (!cp_parser_simulate_error (parser))
8985 cp_parser_name_lookup_error (parser, identifier, type_decl,
8987 type_decl = error_mark_node;
8989 /* Remember that the name was used in the definition of the
8990 current class so that we can check later to see if the
8991 meaning would have been different after the class was
8992 entirely defined. */
8993 else if (type_decl != error_mark_node
8995 maybe_note_name_used_in_class (identifier, type_decl);
9002 /* Parse an elaborated-type-specifier. Note that the grammar given
9003 here incorporates the resolution to DR68.
9005 elaborated-type-specifier:
9006 class-key :: [opt] nested-name-specifier [opt] identifier
9007 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
9008 enum :: [opt] nested-name-specifier [opt] identifier
9009 typename :: [opt] nested-name-specifier identifier
9010 typename :: [opt] nested-name-specifier template [opt]
9015 elaborated-type-specifier:
9016 class-key attributes :: [opt] nested-name-specifier [opt] identifier
9017 class-key attributes :: [opt] nested-name-specifier [opt]
9018 template [opt] template-id
9019 enum attributes :: [opt] nested-name-specifier [opt] identifier
9021 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
9022 declared `friend'. If IS_DECLARATION is TRUE, then this
9023 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
9024 something is being declared.
9026 Returns the TYPE specified. */
9029 cp_parser_elaborated_type_specifier (cp_parser* parser,
9031 bool is_declaration)
9033 enum tag_types tag_type;
9035 tree type = NULL_TREE;
9036 tree attributes = NULL_TREE;
9038 /* See if we're looking at the `enum' keyword. */
9039 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
9041 /* Consume the `enum' token. */
9042 cp_lexer_consume_token (parser->lexer);
9043 /* Remember that it's an enumeration type. */
9044 tag_type = enum_type;
9045 /* Parse the attributes. */
9046 attributes = cp_parser_attributes_opt (parser);
9048 /* Or, it might be `typename'. */
9049 else if (cp_lexer_next_token_is_keyword (parser->lexer,
9052 /* Consume the `typename' token. */
9053 cp_lexer_consume_token (parser->lexer);
9054 /* Remember that it's a `typename' type. */
9055 tag_type = typename_type;
9056 /* The `typename' keyword is only allowed in templates. */
9057 if (!processing_template_decl)
9058 pedwarn ("using `typename' outside of template");
9060 /* Otherwise it must be a class-key. */
9063 tag_type = cp_parser_class_key (parser);
9064 if (tag_type == none_type)
9065 return error_mark_node;
9066 /* Parse the attributes. */
9067 attributes = cp_parser_attributes_opt (parser);
9070 /* Look for the `::' operator. */
9071 cp_parser_global_scope_opt (parser,
9072 /*current_scope_valid_p=*/false);
9073 /* Look for the nested-name-specifier. */
9074 if (tag_type == typename_type)
9076 if (cp_parser_nested_name_specifier (parser,
9077 /*typename_keyword_p=*/true,
9078 /*check_dependency_p=*/true,
9082 return error_mark_node;
9085 /* Even though `typename' is not present, the proposed resolution
9086 to Core Issue 180 says that in `class A<T>::B', `B' should be
9087 considered a type-name, even if `A<T>' is dependent. */
9088 cp_parser_nested_name_specifier_opt (parser,
9089 /*typename_keyword_p=*/true,
9090 /*check_dependency_p=*/true,
9093 /* For everything but enumeration types, consider a template-id. */
9094 if (tag_type != enum_type)
9096 bool template_p = false;
9099 /* Allow the `template' keyword. */
9100 template_p = cp_parser_optional_template_keyword (parser);
9101 /* If we didn't see `template', we don't know if there's a
9102 template-id or not. */
9104 cp_parser_parse_tentatively (parser);
9105 /* Parse the template-id. */
9106 decl = cp_parser_template_id (parser, template_p,
9107 /*check_dependency_p=*/true,
9109 /* If we didn't find a template-id, look for an ordinary
9111 if (!template_p && !cp_parser_parse_definitely (parser))
9113 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
9114 in effect, then we must assume that, upon instantiation, the
9115 template will correspond to a class. */
9116 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
9117 && tag_type == typename_type)
9118 type = make_typename_type (parser->scope, decl,
9121 type = TREE_TYPE (decl);
9124 /* For an enumeration type, consider only a plain identifier. */
9127 identifier = cp_parser_identifier (parser);
9129 if (identifier == error_mark_node)
9131 parser->scope = NULL_TREE;
9132 return error_mark_node;
9135 /* For a `typename', we needn't call xref_tag. */
9136 if (tag_type == typename_type)
9137 return make_typename_type (parser->scope, identifier,
9139 /* Look up a qualified name in the usual way. */
9144 /* In an elaborated-type-specifier, names are assumed to name
9145 types, so we set IS_TYPE to TRUE when calling
9146 cp_parser_lookup_name. */
9147 decl = cp_parser_lookup_name (parser, identifier,
9149 /*is_template=*/false,
9150 /*is_namespace=*/false,
9151 /*check_dependency=*/true);
9153 /* If we are parsing friend declaration, DECL may be a
9154 TEMPLATE_DECL tree node here. However, we need to check
9155 whether this TEMPLATE_DECL results in valid code. Consider
9156 the following example:
9159 template <class T> class C {};
9162 template <class T> friend class N::C; // #1, valid code
9164 template <class T> class Y {
9165 friend class N::C; // #2, invalid code
9168 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
9169 name lookup of `N::C'. We see that friend declaration must
9170 be template for the code to be valid. Note that
9171 processing_template_decl does not work here since it is
9172 always 1 for the above two cases. */
9174 decl = (cp_parser_maybe_treat_template_as_class
9175 (decl, /*tag_name_p=*/is_friend
9176 && parser->num_template_parameter_lists));
9178 if (TREE_CODE (decl) != TYPE_DECL)
9180 error ("expected type-name");
9181 return error_mark_node;
9184 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
9185 check_elaborated_type_specifier
9187 (parser->num_template_parameter_lists
9188 || DECL_SELF_REFERENCE_P (decl)));
9190 type = TREE_TYPE (decl);
9194 /* An elaborated-type-specifier sometimes introduces a new type and
9195 sometimes names an existing type. Normally, the rule is that it
9196 introduces a new type only if there is not an existing type of
9197 the same name already in scope. For example, given:
9200 void f() { struct S s; }
9202 the `struct S' in the body of `f' is the same `struct S' as in
9203 the global scope; the existing definition is used. However, if
9204 there were no global declaration, this would introduce a new
9205 local class named `S'.
9207 An exception to this rule applies to the following code:
9209 namespace N { struct S; }
9211 Here, the elaborated-type-specifier names a new type
9212 unconditionally; even if there is already an `S' in the
9213 containing scope this declaration names a new type.
9214 This exception only applies if the elaborated-type-specifier
9215 forms the complete declaration:
9219 A declaration consisting solely of `class-key identifier ;' is
9220 either a redeclaration of the name in the current scope or a
9221 forward declaration of the identifier as a class name. It
9222 introduces the name into the current scope.
9224 We are in this situation precisely when the next token is a `;'.
9226 An exception to the exception is that a `friend' declaration does
9227 *not* name a new type; i.e., given:
9229 struct S { friend struct T; };
9231 `T' is not a new type in the scope of `S'.
9233 Also, `new struct S' or `sizeof (struct S)' never results in the
9234 definition of a new type; a new type can only be declared in a
9235 declaration context. */
9237 /* Warn about attributes. They are ignored. */
9239 warning ("type attributes are honored only at type definition");
9241 type = xref_tag (tag_type, identifier,
9244 || cp_lexer_next_token_is_not (parser->lexer,
9246 parser->num_template_parameter_lists);
9249 if (tag_type != enum_type)
9250 cp_parser_check_class_key (tag_type, type);
9252 /* A "<" cannot follow an elaborated type specifier. If that
9253 happens, the user was probably trying to form a template-id. */
9254 cp_parser_check_for_invalid_template_id (parser, type);
9259 /* Parse an enum-specifier.
9262 enum identifier [opt] { enumerator-list [opt] }
9264 Returns an ENUM_TYPE representing the enumeration. */
9267 cp_parser_enum_specifier (cp_parser* parser)
9270 tree identifier = NULL_TREE;
9273 /* Look for the `enum' keyword. */
9274 if (!cp_parser_require_keyword (parser, RID_ENUM, "`enum'"))
9275 return error_mark_node;
9276 /* Peek at the next token. */
9277 token = cp_lexer_peek_token (parser->lexer);
9279 /* See if it is an identifier. */
9280 if (token->type == CPP_NAME)
9281 identifier = cp_parser_identifier (parser);
9283 /* Look for the `{'. */
9284 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
9285 return error_mark_node;
9287 /* At this point, we're going ahead with the enum-specifier, even
9288 if some other problem occurs. */
9289 cp_parser_commit_to_tentative_parse (parser);
9291 /* Issue an error message if type-definitions are forbidden here. */
9292 cp_parser_check_type_definition (parser);
9294 /* Create the new type. */
9295 type = start_enum (identifier ? identifier : make_anon_name ());
9297 /* Peek at the next token. */
9298 token = cp_lexer_peek_token (parser->lexer);
9299 /* If it's not a `}', then there are some enumerators. */
9300 if (token->type != CPP_CLOSE_BRACE)
9301 cp_parser_enumerator_list (parser, type);
9302 /* Look for the `}'. */
9303 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
9305 /* Finish up the enumeration. */
9311 /* Parse an enumerator-list. The enumerators all have the indicated
9315 enumerator-definition
9316 enumerator-list , enumerator-definition */
9319 cp_parser_enumerator_list (cp_parser* parser, tree type)
9325 /* Parse an enumerator-definition. */
9326 cp_parser_enumerator_definition (parser, type);
9327 /* Peek at the next token. */
9328 token = cp_lexer_peek_token (parser->lexer);
9329 /* If it's not a `,', then we've reached the end of the
9331 if (token->type != CPP_COMMA)
9333 /* Otherwise, consume the `,' and keep going. */
9334 cp_lexer_consume_token (parser->lexer);
9335 /* If the next token is a `}', there is a trailing comma. */
9336 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
9338 if (pedantic && !in_system_header)
9339 pedwarn ("comma at end of enumerator list");
9345 /* Parse an enumerator-definition. The enumerator has the indicated
9348 enumerator-definition:
9350 enumerator = constant-expression
9356 cp_parser_enumerator_definition (cp_parser* parser, tree type)
9362 /* Look for the identifier. */
9363 identifier = cp_parser_identifier (parser);
9364 if (identifier == error_mark_node)
9367 /* Peek at the next token. */
9368 token = cp_lexer_peek_token (parser->lexer);
9369 /* If it's an `=', then there's an explicit value. */
9370 if (token->type == CPP_EQ)
9372 /* Consume the `=' token. */
9373 cp_lexer_consume_token (parser->lexer);
9374 /* Parse the value. */
9375 value = cp_parser_constant_expression (parser,
9376 /*allow_non_constant_p=*/false,
9382 /* Create the enumerator. */
9383 build_enumerator (identifier, value, type);
9386 /* Parse a namespace-name.
9389 original-namespace-name
9392 Returns the NAMESPACE_DECL for the namespace. */
9395 cp_parser_namespace_name (cp_parser* parser)
9398 tree namespace_decl;
9400 /* Get the name of the namespace. */
9401 identifier = cp_parser_identifier (parser);
9402 if (identifier == error_mark_node)
9403 return error_mark_node;
9405 /* Look up the identifier in the currently active scope. Look only
9406 for namespaces, due to:
9410 When looking up a namespace-name in a using-directive or alias
9411 definition, only namespace names are considered.
9417 During the lookup of a name preceding the :: scope resolution
9418 operator, object, function, and enumerator names are ignored.
9420 (Note that cp_parser_class_or_namespace_name only calls this
9421 function if the token after the name is the scope resolution
9423 namespace_decl = cp_parser_lookup_name (parser, identifier,
9425 /*is_template=*/false,
9426 /*is_namespace=*/true,
9427 /*check_dependency=*/true);
9428 /* If it's not a namespace, issue an error. */
9429 if (namespace_decl == error_mark_node
9430 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
9432 cp_parser_error (parser, "expected namespace-name");
9433 namespace_decl = error_mark_node;
9436 return namespace_decl;
9439 /* Parse a namespace-definition.
9441 namespace-definition:
9442 named-namespace-definition
9443 unnamed-namespace-definition
9445 named-namespace-definition:
9446 original-namespace-definition
9447 extension-namespace-definition
9449 original-namespace-definition:
9450 namespace identifier { namespace-body }
9452 extension-namespace-definition:
9453 namespace original-namespace-name { namespace-body }
9455 unnamed-namespace-definition:
9456 namespace { namespace-body } */
9459 cp_parser_namespace_definition (cp_parser* parser)
9463 /* Look for the `namespace' keyword. */
9464 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
9466 /* Get the name of the namespace. We do not attempt to distinguish
9467 between an original-namespace-definition and an
9468 extension-namespace-definition at this point. The semantic
9469 analysis routines are responsible for that. */
9470 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
9471 identifier = cp_parser_identifier (parser);
9473 identifier = NULL_TREE;
9475 /* Look for the `{' to start the namespace. */
9476 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
9477 /* Start the namespace. */
9478 push_namespace (identifier);
9479 /* Parse the body of the namespace. */
9480 cp_parser_namespace_body (parser);
9481 /* Finish the namespace. */
9483 /* Look for the final `}'. */
9484 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
9487 /* Parse a namespace-body.
9490 declaration-seq [opt] */
9493 cp_parser_namespace_body (cp_parser* parser)
9495 cp_parser_declaration_seq_opt (parser);
9498 /* Parse a namespace-alias-definition.
9500 namespace-alias-definition:
9501 namespace identifier = qualified-namespace-specifier ; */
9504 cp_parser_namespace_alias_definition (cp_parser* parser)
9507 tree namespace_specifier;
9509 /* Look for the `namespace' keyword. */
9510 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
9511 /* Look for the identifier. */
9512 identifier = cp_parser_identifier (parser);
9513 if (identifier == error_mark_node)
9515 /* Look for the `=' token. */
9516 cp_parser_require (parser, CPP_EQ, "`='");
9517 /* Look for the qualified-namespace-specifier. */
9519 = cp_parser_qualified_namespace_specifier (parser);
9520 /* Look for the `;' token. */
9521 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
9523 /* Register the alias in the symbol table. */
9524 do_namespace_alias (identifier, namespace_specifier);
9527 /* Parse a qualified-namespace-specifier.
9529 qualified-namespace-specifier:
9530 :: [opt] nested-name-specifier [opt] namespace-name
9532 Returns a NAMESPACE_DECL corresponding to the specified
9536 cp_parser_qualified_namespace_specifier (cp_parser* parser)
9538 /* Look for the optional `::'. */
9539 cp_parser_global_scope_opt (parser,
9540 /*current_scope_valid_p=*/false);
9542 /* Look for the optional nested-name-specifier. */
9543 cp_parser_nested_name_specifier_opt (parser,
9544 /*typename_keyword_p=*/false,
9545 /*check_dependency_p=*/true,
9547 /*is_declaration=*/true);
9549 return cp_parser_namespace_name (parser);
9552 /* Parse a using-declaration.
9555 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
9556 using :: unqualified-id ; */
9559 cp_parser_using_declaration (cp_parser* parser)
9562 bool typename_p = false;
9563 bool global_scope_p;
9569 /* Look for the `using' keyword. */
9570 cp_parser_require_keyword (parser, RID_USING, "`using'");
9572 /* Peek at the next token. */
9573 token = cp_lexer_peek_token (parser->lexer);
9574 /* See if it's `typename'. */
9575 if (token->keyword == RID_TYPENAME)
9577 /* Remember that we've seen it. */
9579 /* Consume the `typename' token. */
9580 cp_lexer_consume_token (parser->lexer);
9583 /* Look for the optional global scope qualification. */
9585 = (cp_parser_global_scope_opt (parser,
9586 /*current_scope_valid_p=*/false)
9589 /* If we saw `typename', or didn't see `::', then there must be a
9590 nested-name-specifier present. */
9591 if (typename_p || !global_scope_p)
9592 qscope = cp_parser_nested_name_specifier (parser, typename_p,
9593 /*check_dependency_p=*/true,
9595 /*is_declaration=*/true);
9596 /* Otherwise, we could be in either of the two productions. In that
9597 case, treat the nested-name-specifier as optional. */
9599 qscope = cp_parser_nested_name_specifier_opt (parser,
9600 /*typename_keyword_p=*/false,
9601 /*check_dependency_p=*/true,
9603 /*is_declaration=*/true);
9605 qscope = global_namespace;
9607 /* Parse the unqualified-id. */
9608 identifier = cp_parser_unqualified_id (parser,
9609 /*template_keyword_p=*/false,
9610 /*check_dependency_p=*/true,
9611 /*declarator_p=*/true);
9613 /* The function we call to handle a using-declaration is different
9614 depending on what scope we are in. */
9615 if (identifier == error_mark_node)
9617 else if (TREE_CODE (identifier) != IDENTIFIER_NODE
9618 && TREE_CODE (identifier) != BIT_NOT_EXPR)
9619 /* [namespace.udecl]
9621 A using declaration shall not name a template-id. */
9622 error ("a template-id may not appear in a using-declaration");
9625 scope = current_scope ();
9626 if (scope && TYPE_P (scope))
9628 /* Create the USING_DECL. */
9629 decl = do_class_using_decl (build_nt (SCOPE_REF,
9632 /* Add it to the list of members in this class. */
9633 finish_member_declaration (decl);
9637 decl = cp_parser_lookup_name_simple (parser, identifier);
9638 if (decl == error_mark_node)
9639 cp_parser_name_lookup_error (parser, identifier, decl, NULL);
9641 do_local_using_decl (decl, qscope, identifier);
9643 do_toplevel_using_decl (decl, qscope, identifier);
9647 /* Look for the final `;'. */
9648 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
9651 /* Parse a using-directive.
9654 using namespace :: [opt] nested-name-specifier [opt]
9658 cp_parser_using_directive (cp_parser* parser)
9660 tree namespace_decl;
9663 /* Look for the `using' keyword. */
9664 cp_parser_require_keyword (parser, RID_USING, "`using'");
9665 /* And the `namespace' keyword. */
9666 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
9667 /* Look for the optional `::' operator. */
9668 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
9669 /* And the optional nested-name-specifier. */
9670 cp_parser_nested_name_specifier_opt (parser,
9671 /*typename_keyword_p=*/false,
9672 /*check_dependency_p=*/true,
9674 /*is_declaration=*/true);
9675 /* Get the namespace being used. */
9676 namespace_decl = cp_parser_namespace_name (parser);
9677 /* And any specified attributes. */
9678 attribs = cp_parser_attributes_opt (parser);
9679 /* Update the symbol table. */
9680 parse_using_directive (namespace_decl, attribs);
9681 /* Look for the final `;'. */
9682 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
9685 /* Parse an asm-definition.
9688 asm ( string-literal ) ;
9693 asm volatile [opt] ( string-literal ) ;
9694 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
9695 asm volatile [opt] ( string-literal : asm-operand-list [opt]
9696 : asm-operand-list [opt] ) ;
9697 asm volatile [opt] ( string-literal : asm-operand-list [opt]
9698 : asm-operand-list [opt]
9699 : asm-operand-list [opt] ) ; */
9702 cp_parser_asm_definition (cp_parser* parser)
9706 tree outputs = NULL_TREE;
9707 tree inputs = NULL_TREE;
9708 tree clobbers = NULL_TREE;
9710 bool volatile_p = false;
9711 bool extended_p = false;
9713 /* Look for the `asm' keyword. */
9714 cp_parser_require_keyword (parser, RID_ASM, "`asm'");
9715 /* See if the next token is `volatile'. */
9716 if (cp_parser_allow_gnu_extensions_p (parser)
9717 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
9719 /* Remember that we saw the `volatile' keyword. */
9721 /* Consume the token. */
9722 cp_lexer_consume_token (parser->lexer);
9724 /* Look for the opening `('. */
9725 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
9726 /* Look for the string. */
9727 token = cp_parser_require (parser, CPP_STRING, "asm body");
9730 string = token->value;
9731 /* If we're allowing GNU extensions, check for the extended assembly
9732 syntax. Unfortunately, the `:' tokens need not be separated by
9733 a space in C, and so, for compatibility, we tolerate that here
9734 too. Doing that means that we have to treat the `::' operator as
9736 if (cp_parser_allow_gnu_extensions_p (parser)
9737 && at_function_scope_p ()
9738 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
9739 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
9741 bool inputs_p = false;
9742 bool clobbers_p = false;
9744 /* The extended syntax was used. */
9747 /* Look for outputs. */
9748 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
9750 /* Consume the `:'. */
9751 cp_lexer_consume_token (parser->lexer);
9752 /* Parse the output-operands. */
9753 if (cp_lexer_next_token_is_not (parser->lexer,
9755 && cp_lexer_next_token_is_not (parser->lexer,
9757 && cp_lexer_next_token_is_not (parser->lexer,
9759 outputs = cp_parser_asm_operand_list (parser);
9761 /* If the next token is `::', there are no outputs, and the
9762 next token is the beginning of the inputs. */
9763 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
9765 /* Consume the `::' token. */
9766 cp_lexer_consume_token (parser->lexer);
9767 /* The inputs are coming next. */
9771 /* Look for inputs. */
9773 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
9776 /* Consume the `:'. */
9777 cp_lexer_consume_token (parser->lexer);
9778 /* Parse the output-operands. */
9779 if (cp_lexer_next_token_is_not (parser->lexer,
9781 && cp_lexer_next_token_is_not (parser->lexer,
9783 && cp_lexer_next_token_is_not (parser->lexer,
9785 inputs = cp_parser_asm_operand_list (parser);
9787 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
9788 /* The clobbers are coming next. */
9791 /* Look for clobbers. */
9793 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
9796 /* Consume the `:'. */
9797 cp_lexer_consume_token (parser->lexer);
9798 /* Parse the clobbers. */
9799 if (cp_lexer_next_token_is_not (parser->lexer,
9801 clobbers = cp_parser_asm_clobber_list (parser);
9804 /* Look for the closing `)'. */
9805 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
9806 cp_parser_skip_to_closing_parenthesis (parser, true, false,
9807 /*consume_paren=*/true);
9808 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
9810 /* Create the ASM_STMT. */
9811 if (at_function_scope_p ())
9814 finish_asm_stmt (volatile_p
9815 ? ridpointers[(int) RID_VOLATILE] : NULL_TREE,
9816 string, outputs, inputs, clobbers);
9817 /* If the extended syntax was not used, mark the ASM_STMT. */
9819 ASM_INPUT_P (asm_stmt) = 1;
9822 assemble_asm (string);
9825 /* Declarators [gram.dcl.decl] */
9827 /* Parse an init-declarator.
9830 declarator initializer [opt]
9835 declarator asm-specification [opt] attributes [opt] initializer [opt]
9837 function-definition:
9838 decl-specifier-seq [opt] declarator ctor-initializer [opt]
9840 decl-specifier-seq [opt] declarator function-try-block
9844 function-definition:
9845 __extension__ function-definition
9847 The DECL_SPECIFIERS and PREFIX_ATTRIBUTES apply to this declarator.
9848 Returns a representation of the entity declared. If MEMBER_P is TRUE,
9849 then this declarator appears in a class scope. The new DECL created
9850 by this declarator is returned.
9852 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
9853 for a function-definition here as well. If the declarator is a
9854 declarator for a function-definition, *FUNCTION_DEFINITION_P will
9855 be TRUE upon return. By that point, the function-definition will
9856 have been completely parsed.
9858 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
9862 cp_parser_init_declarator (cp_parser* parser,
9863 tree decl_specifiers,
9864 tree prefix_attributes,
9865 bool function_definition_allowed_p,
9867 int declares_class_or_enum,
9868 bool* function_definition_p)
9873 tree asm_specification;
9875 tree decl = NULL_TREE;
9877 bool is_initialized;
9878 bool is_parenthesized_init;
9879 bool is_non_constant_init;
9880 int ctor_dtor_or_conv_p;
9884 /* Assume that this is not the declarator for a function
9886 if (function_definition_p)
9887 *function_definition_p = false;
9889 /* Defer access checks while parsing the declarator; we cannot know
9890 what names are accessible until we know what is being
9892 resume_deferring_access_checks ();
9894 /* Parse the declarator. */
9896 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
9897 &ctor_dtor_or_conv_p,
9898 /*parenthesized_p=*/NULL);
9899 /* Gather up the deferred checks. */
9900 stop_deferring_access_checks ();
9902 /* If the DECLARATOR was erroneous, there's no need to go
9904 if (declarator == error_mark_node)
9905 return error_mark_node;
9907 cp_parser_check_for_definition_in_return_type (declarator,
9908 declares_class_or_enum);
9910 /* Figure out what scope the entity declared by the DECLARATOR is
9911 located in. `grokdeclarator' sometimes changes the scope, so
9912 we compute it now. */
9913 scope = get_scope_of_declarator (declarator);
9915 /* If we're allowing GNU extensions, look for an asm-specification
9917 if (cp_parser_allow_gnu_extensions_p (parser))
9919 /* Look for an asm-specification. */
9920 asm_specification = cp_parser_asm_specification_opt (parser);
9921 /* And attributes. */
9922 attributes = cp_parser_attributes_opt (parser);
9926 asm_specification = NULL_TREE;
9927 attributes = NULL_TREE;
9930 /* Peek at the next token. */
9931 token = cp_lexer_peek_token (parser->lexer);
9932 /* Check to see if the token indicates the start of a
9933 function-definition. */
9934 if (cp_parser_token_starts_function_definition_p (token))
9936 if (!function_definition_allowed_p)
9938 /* If a function-definition should not appear here, issue an
9940 cp_parser_error (parser,
9941 "a function-definition is not allowed here");
9942 return error_mark_node;
9946 /* Neither attributes nor an asm-specification are allowed
9947 on a function-definition. */
9948 if (asm_specification)
9949 error ("an asm-specification is not allowed on a function-definition");
9951 error ("attributes are not allowed on a function-definition");
9952 /* This is a function-definition. */
9953 *function_definition_p = true;
9955 /* Parse the function definition. */
9957 decl = cp_parser_save_member_function_body (parser,
9963 = (cp_parser_function_definition_from_specifiers_and_declarator
9964 (parser, decl_specifiers, prefix_attributes, declarator));
9972 Only in function declarations for constructors, destructors, and
9973 type conversions can the decl-specifier-seq be omitted.
9975 We explicitly postpone this check past the point where we handle
9976 function-definitions because we tolerate function-definitions
9977 that are missing their return types in some modes. */
9978 if (!decl_specifiers && ctor_dtor_or_conv_p <= 0)
9980 cp_parser_error (parser,
9981 "expected constructor, destructor, or type conversion");
9982 return error_mark_node;
9985 /* An `=' or an `(' indicates an initializer. */
9986 is_initialized = (token->type == CPP_EQ
9987 || token->type == CPP_OPEN_PAREN);
9988 /* If the init-declarator isn't initialized and isn't followed by a
9989 `,' or `;', it's not a valid init-declarator. */
9991 && token->type != CPP_COMMA
9992 && token->type != CPP_SEMICOLON)
9994 cp_parser_error (parser, "expected init-declarator");
9995 return error_mark_node;
9998 /* Because start_decl has side-effects, we should only call it if we
9999 know we're going ahead. By this point, we know that we cannot
10000 possibly be looking at any other construct. */
10001 cp_parser_commit_to_tentative_parse (parser);
10003 /* If the decl specifiers were bad, issue an error now that we're
10004 sure this was intended to be a declarator. Then continue
10005 declaring the variable(s), as int, to try to cut down on further
10007 if (decl_specifiers != NULL
10008 && TREE_VALUE (decl_specifiers) == error_mark_node)
10010 cp_parser_error (parser, "invalid type in declaration");
10011 TREE_VALUE (decl_specifiers) = integer_type_node;
10014 /* Check to see whether or not this declaration is a friend. */
10015 friend_p = cp_parser_friend_p (decl_specifiers);
10017 /* Check that the number of template-parameter-lists is OK. */
10018 if (!cp_parser_check_declarator_template_parameters (parser, declarator))
10019 return error_mark_node;
10021 /* Enter the newly declared entry in the symbol table. If we're
10022 processing a declaration in a class-specifier, we wait until
10023 after processing the initializer. */
10026 if (parser->in_unbraced_linkage_specification_p)
10028 decl_specifiers = tree_cons (error_mark_node,
10029 get_identifier ("extern"),
10031 have_extern_spec = false;
10033 decl = start_decl (declarator, decl_specifiers,
10034 is_initialized, attributes, prefix_attributes);
10037 /* Enter the SCOPE. That way unqualified names appearing in the
10038 initializer will be looked up in SCOPE. */
10040 pop_p = push_scope (scope);
10042 /* Perform deferred access control checks, now that we know in which
10043 SCOPE the declared entity resides. */
10044 if (!member_p && decl)
10046 tree saved_current_function_decl = NULL_TREE;
10048 /* If the entity being declared is a function, pretend that we
10049 are in its scope. If it is a `friend', it may have access to
10050 things that would not otherwise be accessible. */
10051 if (TREE_CODE (decl) == FUNCTION_DECL)
10053 saved_current_function_decl = current_function_decl;
10054 current_function_decl = decl;
10057 /* Perform the access control checks for the declarator and the
10058 the decl-specifiers. */
10059 perform_deferred_access_checks ();
10061 /* Restore the saved value. */
10062 if (TREE_CODE (decl) == FUNCTION_DECL)
10063 current_function_decl = saved_current_function_decl;
10066 /* Parse the initializer. */
10067 if (is_initialized)
10068 initializer = cp_parser_initializer (parser,
10069 &is_parenthesized_init,
10070 &is_non_constant_init);
10073 initializer = NULL_TREE;
10074 is_parenthesized_init = false;
10075 is_non_constant_init = true;
10078 /* The old parser allows attributes to appear after a parenthesized
10079 initializer. Mark Mitchell proposed removing this functionality
10080 on the GCC mailing lists on 2002-08-13. This parser accepts the
10081 attributes -- but ignores them. */
10082 if (cp_parser_allow_gnu_extensions_p (parser) && is_parenthesized_init)
10083 if (cp_parser_attributes_opt (parser))
10084 warning ("attributes after parenthesized initializer ignored");
10086 /* Leave the SCOPE, now that we have processed the initializer. It
10087 is important to do this before calling cp_finish_decl because it
10088 makes decisions about whether to create DECL_STMTs or not based
10089 on the current scope. */
10093 /* For an in-class declaration, use `grokfield' to create the
10097 decl = grokfield (declarator, decl_specifiers,
10098 initializer, /*asmspec=*/NULL_TREE,
10099 /*attributes=*/NULL_TREE);
10100 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
10101 cp_parser_save_default_args (parser, decl);
10104 /* Finish processing the declaration. But, skip friend
10106 if (!friend_p && decl)
10107 cp_finish_decl (decl,
10110 /* If the initializer is in parentheses, then this is
10111 a direct-initialization, which means that an
10112 `explicit' constructor is OK. Otherwise, an
10113 `explicit' constructor cannot be used. */
10114 ((is_parenthesized_init || !is_initialized)
10115 ? 0 : LOOKUP_ONLYCONVERTING));
10117 /* Remember whether or not variables were initialized by
10118 constant-expressions. */
10119 if (decl && TREE_CODE (decl) == VAR_DECL
10120 && is_initialized && !is_non_constant_init)
10121 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = true;
10126 /* Parse a declarator.
10130 ptr-operator declarator
10132 abstract-declarator:
10133 ptr-operator abstract-declarator [opt]
10134 direct-abstract-declarator
10139 attributes [opt] direct-declarator
10140 attributes [opt] ptr-operator declarator
10142 abstract-declarator:
10143 attributes [opt] ptr-operator abstract-declarator [opt]
10144 attributes [opt] direct-abstract-declarator
10146 Returns a representation of the declarator. If the declarator has
10147 the form `* declarator', then an INDIRECT_REF is returned, whose
10148 only operand is the sub-declarator. Analogously, `& declarator' is
10149 represented as an ADDR_EXPR. For `X::* declarator', a SCOPE_REF is
10150 used. The first operand is the TYPE for `X'. The second operand
10151 is an INDIRECT_REF whose operand is the sub-declarator.
10153 Otherwise, the representation is as for a direct-declarator.
10155 (It would be better to define a structure type to represent
10156 declarators, rather than abusing `tree' nodes to represent
10157 declarators. That would be much clearer and save some memory.
10158 There is no reason for declarators to be garbage-collected, for
10159 example; they are created during parser and no longer needed after
10160 `grokdeclarator' has been called.)
10162 For a ptr-operator that has the optional cv-qualifier-seq,
10163 cv-qualifiers will be stored in the TREE_TYPE of the INDIRECT_REF
10166 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
10167 detect constructor, destructor or conversion operators. It is set
10168 to -1 if the declarator is a name, and +1 if it is a
10169 function. Otherwise it is set to zero. Usually you just want to
10170 test for >0, but internally the negative value is used.
10172 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
10173 a decl-specifier-seq unless it declares a constructor, destructor,
10174 or conversion. It might seem that we could check this condition in
10175 semantic analysis, rather than parsing, but that makes it difficult
10176 to handle something like `f()'. We want to notice that there are
10177 no decl-specifiers, and therefore realize that this is an
10178 expression, not a declaration.)
10180 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
10181 the declarator is a direct-declarator of the form "(...)". */
10184 cp_parser_declarator (cp_parser* parser,
10185 cp_parser_declarator_kind dcl_kind,
10186 int* ctor_dtor_or_conv_p,
10187 bool* parenthesized_p)
10191 enum tree_code code;
10192 tree cv_qualifier_seq;
10194 tree attributes = NULL_TREE;
10196 /* Assume this is not a constructor, destructor, or type-conversion
10198 if (ctor_dtor_or_conv_p)
10199 *ctor_dtor_or_conv_p = 0;
10201 if (cp_parser_allow_gnu_extensions_p (parser))
10202 attributes = cp_parser_attributes_opt (parser);
10204 /* Peek at the next token. */
10205 token = cp_lexer_peek_token (parser->lexer);
10207 /* Check for the ptr-operator production. */
10208 cp_parser_parse_tentatively (parser);
10209 /* Parse the ptr-operator. */
10210 code = cp_parser_ptr_operator (parser,
10212 &cv_qualifier_seq);
10213 /* If that worked, then we have a ptr-operator. */
10214 if (cp_parser_parse_definitely (parser))
10216 /* If a ptr-operator was found, then this declarator was not
10218 if (parenthesized_p)
10219 *parenthesized_p = true;
10220 /* The dependent declarator is optional if we are parsing an
10221 abstract-declarator. */
10222 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
10223 cp_parser_parse_tentatively (parser);
10225 /* Parse the dependent declarator. */
10226 declarator = cp_parser_declarator (parser, dcl_kind,
10227 /*ctor_dtor_or_conv_p=*/NULL,
10228 /*parenthesized_p=*/NULL);
10230 /* If we are parsing an abstract-declarator, we must handle the
10231 case where the dependent declarator is absent. */
10232 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
10233 && !cp_parser_parse_definitely (parser))
10234 declarator = NULL_TREE;
10236 /* Build the representation of the ptr-operator. */
10237 if (code == INDIRECT_REF)
10238 declarator = make_pointer_declarator (cv_qualifier_seq,
10241 declarator = make_reference_declarator (cv_qualifier_seq,
10243 /* Handle the pointer-to-member case. */
10245 declarator = build_nt (SCOPE_REF, class_type, declarator);
10247 /* Everything else is a direct-declarator. */
10250 if (parenthesized_p)
10251 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
10253 declarator = cp_parser_direct_declarator (parser, dcl_kind,
10254 ctor_dtor_or_conv_p);
10257 if (attributes && declarator != error_mark_node)
10258 declarator = tree_cons (attributes, declarator, NULL_TREE);
10263 /* Parse a direct-declarator or direct-abstract-declarator.
10267 direct-declarator ( parameter-declaration-clause )
10268 cv-qualifier-seq [opt]
10269 exception-specification [opt]
10270 direct-declarator [ constant-expression [opt] ]
10273 direct-abstract-declarator:
10274 direct-abstract-declarator [opt]
10275 ( parameter-declaration-clause )
10276 cv-qualifier-seq [opt]
10277 exception-specification [opt]
10278 direct-abstract-declarator [opt] [ constant-expression [opt] ]
10279 ( abstract-declarator )
10281 Returns a representation of the declarator. DCL_KIND is
10282 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
10283 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
10284 we are parsing a direct-declarator. It is
10285 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
10286 of ambiguity we prefer an abstract declarator, as per
10287 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P is as for
10288 cp_parser_declarator.
10290 For the declarator-id production, the representation is as for an
10291 id-expression, except that a qualified name is represented as a
10292 SCOPE_REF. A function-declarator is represented as a CALL_EXPR;
10293 see the documentation of the FUNCTION_DECLARATOR_* macros for
10294 information about how to find the various declarator components.
10295 An array-declarator is represented as an ARRAY_REF. The
10296 direct-declarator is the first operand; the constant-expression
10297 indicating the size of the array is the second operand. */
10300 cp_parser_direct_declarator (cp_parser* parser,
10301 cp_parser_declarator_kind dcl_kind,
10302 int* ctor_dtor_or_conv_p)
10305 tree declarator = NULL_TREE;
10306 tree scope = NULL_TREE;
10307 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
10308 bool saved_in_declarator_p = parser->in_declarator_p;
10310 bool pop_p = false;
10314 /* Peek at the next token. */
10315 token = cp_lexer_peek_token (parser->lexer);
10316 if (token->type == CPP_OPEN_PAREN)
10318 /* This is either a parameter-declaration-clause, or a
10319 parenthesized declarator. When we know we are parsing a
10320 named declarator, it must be a parenthesized declarator
10321 if FIRST is true. For instance, `(int)' is a
10322 parameter-declaration-clause, with an omitted
10323 direct-abstract-declarator. But `((*))', is a
10324 parenthesized abstract declarator. Finally, when T is a
10325 template parameter `(T)' is a
10326 parameter-declaration-clause, and not a parenthesized
10329 We first try and parse a parameter-declaration-clause,
10330 and then try a nested declarator (if FIRST is true).
10332 It is not an error for it not to be a
10333 parameter-declaration-clause, even when FIRST is
10339 The first is the declaration of a function while the
10340 second is a the definition of a variable, including its
10343 Having seen only the parenthesis, we cannot know which of
10344 these two alternatives should be selected. Even more
10345 complex are examples like:
10350 The former is a function-declaration; the latter is a
10351 variable initialization.
10353 Thus again, we try a parameter-declaration-clause, and if
10354 that fails, we back out and return. */
10356 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
10359 unsigned saved_num_template_parameter_lists;
10361 cp_parser_parse_tentatively (parser);
10363 /* Consume the `('. */
10364 cp_lexer_consume_token (parser->lexer);
10367 /* If this is going to be an abstract declarator, we're
10368 in a declarator and we can't have default args. */
10369 parser->default_arg_ok_p = false;
10370 parser->in_declarator_p = true;
10373 /* Inside the function parameter list, surrounding
10374 template-parameter-lists do not apply. */
10375 saved_num_template_parameter_lists
10376 = parser->num_template_parameter_lists;
10377 parser->num_template_parameter_lists = 0;
10379 /* Parse the parameter-declaration-clause. */
10380 params = cp_parser_parameter_declaration_clause (parser);
10382 parser->num_template_parameter_lists
10383 = saved_num_template_parameter_lists;
10385 /* If all went well, parse the cv-qualifier-seq and the
10386 exception-specification. */
10387 if (cp_parser_parse_definitely (parser))
10389 tree cv_qualifiers;
10390 tree exception_specification;
10392 if (ctor_dtor_or_conv_p)
10393 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
10395 /* Consume the `)'. */
10396 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
10398 /* Parse the cv-qualifier-seq. */
10399 cv_qualifiers = cp_parser_cv_qualifier_seq_opt (parser);
10400 /* And the exception-specification. */
10401 exception_specification
10402 = cp_parser_exception_specification_opt (parser);
10404 /* Create the function-declarator. */
10405 declarator = make_call_declarator (declarator,
10408 exception_specification);
10409 /* Any subsequent parameter lists are to do with
10410 return type, so are not those of the declared
10412 parser->default_arg_ok_p = false;
10414 /* Repeat the main loop. */
10419 /* If this is the first, we can try a parenthesized
10423 bool saved_in_type_id_in_expr_p;
10425 parser->default_arg_ok_p = saved_default_arg_ok_p;
10426 parser->in_declarator_p = saved_in_declarator_p;
10428 /* Consume the `('. */
10429 cp_lexer_consume_token (parser->lexer);
10430 /* Parse the nested declarator. */
10431 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
10432 parser->in_type_id_in_expr_p = true;
10434 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
10435 /*parenthesized_p=*/NULL);
10436 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
10438 /* Expect a `)'. */
10439 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
10440 declarator = error_mark_node;
10441 if (declarator == error_mark_node)
10444 goto handle_declarator;
10446 /* Otherwise, we must be done. */
10450 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
10451 && token->type == CPP_OPEN_SQUARE)
10453 /* Parse an array-declarator. */
10456 if (ctor_dtor_or_conv_p)
10457 *ctor_dtor_or_conv_p = 0;
10460 parser->default_arg_ok_p = false;
10461 parser->in_declarator_p = true;
10462 /* Consume the `['. */
10463 cp_lexer_consume_token (parser->lexer);
10464 /* Peek at the next token. */
10465 token = cp_lexer_peek_token (parser->lexer);
10466 /* If the next token is `]', then there is no
10467 constant-expression. */
10468 if (token->type != CPP_CLOSE_SQUARE)
10470 bool non_constant_p;
10473 = cp_parser_constant_expression (parser,
10474 /*allow_non_constant=*/true,
10476 if (!non_constant_p)
10477 bounds = fold_non_dependent_expr (bounds);
10480 bounds = NULL_TREE;
10481 /* Look for the closing `]'. */
10482 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'"))
10484 declarator = error_mark_node;
10488 declarator = build_nt (ARRAY_REF, declarator, bounds);
10490 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
10492 /* Parse a declarator-id */
10493 if (dcl_kind == CP_PARSER_DECLARATOR_EITHER)
10494 cp_parser_parse_tentatively (parser);
10495 declarator = cp_parser_declarator_id (parser);
10496 if (dcl_kind == CP_PARSER_DECLARATOR_EITHER)
10498 if (!cp_parser_parse_definitely (parser))
10499 declarator = error_mark_node;
10500 else if (TREE_CODE (declarator) != IDENTIFIER_NODE)
10502 cp_parser_error (parser, "expected unqualified-id");
10503 declarator = error_mark_node;
10507 if (declarator == error_mark_node)
10510 if (TREE_CODE (declarator) == SCOPE_REF
10511 && !current_scope ())
10513 tree scope = TREE_OPERAND (declarator, 0);
10515 /* In the declaration of a member of a template class
10516 outside of the class itself, the SCOPE will sometimes
10517 be a TYPENAME_TYPE. For example, given:
10519 template <typename T>
10520 int S<T>::R::i = 3;
10522 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
10523 this context, we must resolve S<T>::R to an ordinary
10524 type, rather than a typename type.
10526 The reason we normally avoid resolving TYPENAME_TYPEs
10527 is that a specialization of `S' might render
10528 `S<T>::R' not a type. However, if `S' is
10529 specialized, then this `i' will not be used, so there
10530 is no harm in resolving the types here. */
10531 if (TREE_CODE (scope) == TYPENAME_TYPE)
10535 /* Resolve the TYPENAME_TYPE. */
10536 type = resolve_typename_type (scope,
10537 /*only_current_p=*/false);
10538 /* If that failed, the declarator is invalid. */
10539 if (type == error_mark_node)
10540 error ("`%T::%D' is not a type",
10541 TYPE_CONTEXT (scope),
10542 TYPE_IDENTIFIER (scope));
10543 /* Build a new DECLARATOR. */
10544 declarator = build_nt (SCOPE_REF,
10546 TREE_OPERAND (declarator, 1));
10550 /* Check to see whether the declarator-id names a constructor,
10551 destructor, or conversion. */
10552 if (declarator && ctor_dtor_or_conv_p
10553 && ((TREE_CODE (declarator) == SCOPE_REF
10554 && CLASS_TYPE_P (TREE_OPERAND (declarator, 0)))
10555 || (TREE_CODE (declarator) != SCOPE_REF
10556 && at_class_scope_p ())))
10558 tree unqualified_name;
10561 /* Get the unqualified part of the name. */
10562 if (TREE_CODE (declarator) == SCOPE_REF)
10564 class_type = TREE_OPERAND (declarator, 0);
10565 unqualified_name = TREE_OPERAND (declarator, 1);
10569 class_type = current_class_type;
10570 unqualified_name = declarator;
10573 /* See if it names ctor, dtor or conv. */
10574 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR
10575 || IDENTIFIER_TYPENAME_P (unqualified_name)
10576 || constructor_name_p (unqualified_name, class_type)
10577 || (TREE_CODE (unqualified_name) == TYPE_DECL
10578 && same_type_p (TREE_TYPE (unqualified_name),
10580 *ctor_dtor_or_conv_p = -1;
10583 handle_declarator:;
10584 scope = get_scope_of_declarator (declarator);
10586 /* Any names that appear after the declarator-id for a
10587 member are looked up in the containing scope. */
10588 pop_p = push_scope (scope);
10589 parser->in_declarator_p = true;
10590 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
10592 && (TREE_CODE (declarator) == SCOPE_REF
10593 || TREE_CODE (declarator) == IDENTIFIER_NODE)))
10594 /* Default args are only allowed on function
10596 parser->default_arg_ok_p = saved_default_arg_ok_p;
10598 parser->default_arg_ok_p = false;
10607 /* For an abstract declarator, we might wind up with nothing at this
10608 point. That's an error; the declarator is not optional. */
10610 cp_parser_error (parser, "expected declarator");
10612 /* If we entered a scope, we must exit it now. */
10616 parser->default_arg_ok_p = saved_default_arg_ok_p;
10617 parser->in_declarator_p = saved_in_declarator_p;
10622 /* Parse a ptr-operator.
10625 * cv-qualifier-seq [opt]
10627 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
10632 & cv-qualifier-seq [opt]
10634 Returns INDIRECT_REF if a pointer, or pointer-to-member, was
10635 used. Returns ADDR_EXPR if a reference was used. In the
10636 case of a pointer-to-member, *TYPE is filled in with the
10637 TYPE containing the member. *CV_QUALIFIER_SEQ is filled in
10638 with the cv-qualifier-seq, or NULL_TREE, if there are no
10639 cv-qualifiers. Returns ERROR_MARK if an error occurred. */
10641 static enum tree_code
10642 cp_parser_ptr_operator (cp_parser* parser,
10644 tree* cv_qualifier_seq)
10646 enum tree_code code = ERROR_MARK;
10649 /* Assume that it's not a pointer-to-member. */
10651 /* And that there are no cv-qualifiers. */
10652 *cv_qualifier_seq = NULL_TREE;
10654 /* Peek at the next token. */
10655 token = cp_lexer_peek_token (parser->lexer);
10656 /* If it's a `*' or `&' we have a pointer or reference. */
10657 if (token->type == CPP_MULT || token->type == CPP_AND)
10659 /* Remember which ptr-operator we were processing. */
10660 code = (token->type == CPP_AND ? ADDR_EXPR : INDIRECT_REF);
10662 /* Consume the `*' or `&'. */
10663 cp_lexer_consume_token (parser->lexer);
10665 /* A `*' can be followed by a cv-qualifier-seq, and so can a
10666 `&', if we are allowing GNU extensions. (The only qualifier
10667 that can legally appear after `&' is `restrict', but that is
10668 enforced during semantic analysis. */
10669 if (code == INDIRECT_REF
10670 || cp_parser_allow_gnu_extensions_p (parser))
10671 *cv_qualifier_seq = cp_parser_cv_qualifier_seq_opt (parser);
10675 /* Try the pointer-to-member case. */
10676 cp_parser_parse_tentatively (parser);
10677 /* Look for the optional `::' operator. */
10678 cp_parser_global_scope_opt (parser,
10679 /*current_scope_valid_p=*/false);
10680 /* Look for the nested-name specifier. */
10681 cp_parser_nested_name_specifier (parser,
10682 /*typename_keyword_p=*/false,
10683 /*check_dependency_p=*/true,
10685 /*is_declaration=*/false);
10686 /* If we found it, and the next token is a `*', then we are
10687 indeed looking at a pointer-to-member operator. */
10688 if (!cp_parser_error_occurred (parser)
10689 && cp_parser_require (parser, CPP_MULT, "`*'"))
10691 /* The type of which the member is a member is given by the
10693 *type = parser->scope;
10694 /* The next name will not be qualified. */
10695 parser->scope = NULL_TREE;
10696 parser->qualifying_scope = NULL_TREE;
10697 parser->object_scope = NULL_TREE;
10698 /* Indicate that the `*' operator was used. */
10699 code = INDIRECT_REF;
10700 /* Look for the optional cv-qualifier-seq. */
10701 *cv_qualifier_seq = cp_parser_cv_qualifier_seq_opt (parser);
10703 /* If that didn't work we don't have a ptr-operator. */
10704 if (!cp_parser_parse_definitely (parser))
10705 cp_parser_error (parser, "expected ptr-operator");
10711 /* Parse an (optional) cv-qualifier-seq.
10714 cv-qualifier cv-qualifier-seq [opt]
10716 Returns a TREE_LIST. The TREE_VALUE of each node is the
10717 representation of a cv-qualifier. */
10720 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
10722 tree cv_qualifiers = NULL_TREE;
10728 /* Look for the next cv-qualifier. */
10729 cv_qualifier = cp_parser_cv_qualifier_opt (parser);
10730 /* If we didn't find one, we're done. */
10734 /* Add this cv-qualifier to the list. */
10736 = tree_cons (NULL_TREE, cv_qualifier, cv_qualifiers);
10739 /* We built up the list in reverse order. */
10740 return nreverse (cv_qualifiers);
10743 /* Parse an (optional) cv-qualifier.
10755 cp_parser_cv_qualifier_opt (cp_parser* parser)
10758 tree cv_qualifier = NULL_TREE;
10760 /* Peek at the next token. */
10761 token = cp_lexer_peek_token (parser->lexer);
10762 /* See if it's a cv-qualifier. */
10763 switch (token->keyword)
10768 /* Save the value of the token. */
10769 cv_qualifier = token->value;
10770 /* Consume the token. */
10771 cp_lexer_consume_token (parser->lexer);
10778 return cv_qualifier;
10781 /* Parse a declarator-id.
10785 :: [opt] nested-name-specifier [opt] type-name
10787 In the `id-expression' case, the value returned is as for
10788 cp_parser_id_expression if the id-expression was an unqualified-id.
10789 If the id-expression was a qualified-id, then a SCOPE_REF is
10790 returned. The first operand is the scope (either a NAMESPACE_DECL
10791 or TREE_TYPE), but the second is still just a representation of an
10795 cp_parser_declarator_id (cp_parser* parser)
10797 tree id_expression;
10799 /* The expression must be an id-expression. Assume that qualified
10800 names are the names of types so that:
10803 int S<T>::R::i = 3;
10805 will work; we must treat `S<T>::R' as the name of a type.
10806 Similarly, assume that qualified names are templates, where
10810 int S<T>::R<T>::i = 3;
10813 id_expression = cp_parser_id_expression (parser,
10814 /*template_keyword_p=*/false,
10815 /*check_dependency_p=*/false,
10816 /*template_p=*/NULL,
10817 /*declarator_p=*/true);
10818 /* If the name was qualified, create a SCOPE_REF to represent
10822 id_expression = build_nt (SCOPE_REF, parser->scope, id_expression);
10823 parser->scope = NULL_TREE;
10826 return id_expression;
10829 /* Parse a type-id.
10832 type-specifier-seq abstract-declarator [opt]
10834 Returns the TYPE specified. */
10837 cp_parser_type_id (cp_parser* parser)
10839 tree type_specifier_seq;
10840 tree abstract_declarator;
10842 /* Parse the type-specifier-seq. */
10844 = cp_parser_type_specifier_seq (parser);
10845 if (type_specifier_seq == error_mark_node)
10846 return error_mark_node;
10848 /* There might or might not be an abstract declarator. */
10849 cp_parser_parse_tentatively (parser);
10850 /* Look for the declarator. */
10851 abstract_declarator
10852 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
10853 /*parenthesized_p=*/NULL);
10854 /* Check to see if there really was a declarator. */
10855 if (!cp_parser_parse_definitely (parser))
10856 abstract_declarator = NULL_TREE;
10858 return groktypename (build_tree_list (type_specifier_seq,
10859 abstract_declarator));
10862 /* Parse a type-specifier-seq.
10864 type-specifier-seq:
10865 type-specifier type-specifier-seq [opt]
10869 type-specifier-seq:
10870 attributes type-specifier-seq [opt]
10872 Returns a TREE_LIST. Either the TREE_VALUE of each node is a
10873 type-specifier, or the TREE_PURPOSE is a list of attributes. */
10876 cp_parser_type_specifier_seq (cp_parser* parser)
10878 bool seen_type_specifier = false;
10879 tree type_specifier_seq = NULL_TREE;
10881 /* Parse the type-specifiers and attributes. */
10884 tree type_specifier;
10886 /* Check for attributes first. */
10887 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
10889 type_specifier_seq = tree_cons (cp_parser_attributes_opt (parser),
10891 type_specifier_seq);
10895 /* After the first type-specifier, others are optional. */
10896 if (seen_type_specifier)
10897 cp_parser_parse_tentatively (parser);
10898 /* Look for the type-specifier. */
10899 type_specifier = cp_parser_type_specifier (parser,
10900 CP_PARSER_FLAGS_NONE,
10901 /*is_friend=*/false,
10902 /*is_declaration=*/false,
10905 /* If the first type-specifier could not be found, this is not a
10906 type-specifier-seq at all. */
10907 if (!seen_type_specifier && type_specifier == error_mark_node)
10908 return error_mark_node;
10909 /* If subsequent type-specifiers could not be found, the
10910 type-specifier-seq is complete. */
10911 else if (seen_type_specifier && !cp_parser_parse_definitely (parser))
10914 /* Add the new type-specifier to the list. */
10916 = tree_cons (NULL_TREE, type_specifier, type_specifier_seq);
10917 seen_type_specifier = true;
10920 /* We built up the list in reverse order. */
10921 return nreverse (type_specifier_seq);
10924 /* Parse a parameter-declaration-clause.
10926 parameter-declaration-clause:
10927 parameter-declaration-list [opt] ... [opt]
10928 parameter-declaration-list , ...
10930 Returns a representation for the parameter declarations. Each node
10931 is a TREE_LIST. (See cp_parser_parameter_declaration for the exact
10932 representation.) If the parameter-declaration-clause ends with an
10933 ellipsis, PARMLIST_ELLIPSIS_P will hold of the first node in the
10934 list. A return value of NULL_TREE indicates a
10935 parameter-declaration-clause consisting only of an ellipsis. */
10938 cp_parser_parameter_declaration_clause (cp_parser* parser)
10944 /* Peek at the next token. */
10945 token = cp_lexer_peek_token (parser->lexer);
10946 /* Check for trivial parameter-declaration-clauses. */
10947 if (token->type == CPP_ELLIPSIS)
10949 /* Consume the `...' token. */
10950 cp_lexer_consume_token (parser->lexer);
10953 else if (token->type == CPP_CLOSE_PAREN)
10954 /* There are no parameters. */
10956 #ifndef NO_IMPLICIT_EXTERN_C
10957 if (in_system_header && current_class_type == NULL
10958 && current_lang_name == lang_name_c)
10962 return void_list_node;
10964 /* Check for `(void)', too, which is a special case. */
10965 else if (token->keyword == RID_VOID
10966 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
10967 == CPP_CLOSE_PAREN))
10969 /* Consume the `void' token. */
10970 cp_lexer_consume_token (parser->lexer);
10971 /* There are no parameters. */
10972 return void_list_node;
10975 /* Parse the parameter-declaration-list. */
10976 parameters = cp_parser_parameter_declaration_list (parser);
10977 /* If a parse error occurred while parsing the
10978 parameter-declaration-list, then the entire
10979 parameter-declaration-clause is erroneous. */
10980 if (parameters == error_mark_node)
10981 return error_mark_node;
10983 /* Peek at the next token. */
10984 token = cp_lexer_peek_token (parser->lexer);
10985 /* If it's a `,', the clause should terminate with an ellipsis. */
10986 if (token->type == CPP_COMMA)
10988 /* Consume the `,'. */
10989 cp_lexer_consume_token (parser->lexer);
10990 /* Expect an ellipsis. */
10992 = (cp_parser_require (parser, CPP_ELLIPSIS, "`...'") != NULL);
10994 /* It might also be `...' if the optional trailing `,' was
10996 else if (token->type == CPP_ELLIPSIS)
10998 /* Consume the `...' token. */
10999 cp_lexer_consume_token (parser->lexer);
11000 /* And remember that we saw it. */
11004 ellipsis_p = false;
11006 /* Finish the parameter list. */
11007 return finish_parmlist (parameters, ellipsis_p);
11010 /* Parse a parameter-declaration-list.
11012 parameter-declaration-list:
11013 parameter-declaration
11014 parameter-declaration-list , parameter-declaration
11016 Returns a representation of the parameter-declaration-list, as for
11017 cp_parser_parameter_declaration_clause. However, the
11018 `void_list_node' is never appended to the list. */
11021 cp_parser_parameter_declaration_list (cp_parser* parser)
11023 tree parameters = NULL_TREE;
11025 /* Look for more parameters. */
11029 bool parenthesized_p;
11030 /* Parse the parameter. */
11032 = cp_parser_parameter_declaration (parser,
11033 /*template_parm_p=*/false,
11036 /* If a parse error occurred parsing the parameter declaration,
11037 then the entire parameter-declaration-list is erroneous. */
11038 if (parameter == error_mark_node)
11040 parameters = error_mark_node;
11043 /* Add the new parameter to the list. */
11044 TREE_CHAIN (parameter) = parameters;
11045 parameters = parameter;
11047 /* Peek at the next token. */
11048 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
11049 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11050 /* The parameter-declaration-list is complete. */
11052 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
11056 /* Peek at the next token. */
11057 token = cp_lexer_peek_nth_token (parser->lexer, 2);
11058 /* If it's an ellipsis, then the list is complete. */
11059 if (token->type == CPP_ELLIPSIS)
11061 /* Otherwise, there must be more parameters. Consume the
11063 cp_lexer_consume_token (parser->lexer);
11064 /* When parsing something like:
11066 int i(float f, double d)
11068 we can tell after seeing the declaration for "f" that we
11069 are not looking at an initialization of a variable "i",
11070 but rather at the declaration of a function "i".
11072 Due to the fact that the parsing of template arguments
11073 (as specified to a template-id) requires backtracking we
11074 cannot use this technique when inside a template argument
11076 if (!parser->in_template_argument_list_p
11077 && !parser->in_type_id_in_expr_p
11078 && cp_parser_parsing_tentatively (parser)
11079 && !cp_parser_committed_to_tentative_parse (parser)
11080 /* However, a parameter-declaration of the form
11081 "foat(f)" (which is a valid declaration of a
11082 parameter "f") can also be interpreted as an
11083 expression (the conversion of "f" to "float"). */
11084 && !parenthesized_p)
11085 cp_parser_commit_to_tentative_parse (parser);
11089 cp_parser_error (parser, "expected `,' or `...'");
11090 if (!cp_parser_parsing_tentatively (parser)
11091 || cp_parser_committed_to_tentative_parse (parser))
11092 cp_parser_skip_to_closing_parenthesis (parser,
11093 /*recovering=*/true,
11094 /*or_comma=*/false,
11095 /*consume_paren=*/false);
11100 /* We built up the list in reverse order; straighten it out now. */
11101 return nreverse (parameters);
11104 /* Parse a parameter declaration.
11106 parameter-declaration:
11107 decl-specifier-seq declarator
11108 decl-specifier-seq declarator = assignment-expression
11109 decl-specifier-seq abstract-declarator [opt]
11110 decl-specifier-seq abstract-declarator [opt] = assignment-expression
11112 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
11113 declares a template parameter. (In that case, a non-nested `>'
11114 token encountered during the parsing of the assignment-expression
11115 is not interpreted as a greater-than operator.)
11117 Returns a TREE_LIST representing the parameter-declaration. The
11118 TREE_PURPOSE is the default argument expression, or NULL_TREE if
11119 there is no default argument. The TREE_VALUE is a representation
11120 of the decl-specifier-seq and declarator. In particular, the
11121 TREE_VALUE will be a TREE_LIST whose TREE_PURPOSE represents the
11122 decl-specifier-seq and whose TREE_VALUE represents the declarator.
11123 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
11124 the declarator is of the form "(p)". */
11127 cp_parser_parameter_declaration (cp_parser *parser,
11128 bool template_parm_p,
11129 bool *parenthesized_p)
11131 int declares_class_or_enum;
11132 bool greater_than_is_operator_p;
11133 tree decl_specifiers;
11136 tree default_argument;
11139 const char *saved_message;
11141 /* In a template parameter, `>' is not an operator.
11145 When parsing a default template-argument for a non-type
11146 template-parameter, the first non-nested `>' is taken as the end
11147 of the template parameter-list rather than a greater-than
11149 greater_than_is_operator_p = !template_parm_p;
11151 /* Type definitions may not appear in parameter types. */
11152 saved_message = parser->type_definition_forbidden_message;
11153 parser->type_definition_forbidden_message
11154 = "types may not be defined in parameter types";
11156 /* Parse the declaration-specifiers. */
11158 = cp_parser_decl_specifier_seq (parser,
11159 CP_PARSER_FLAGS_NONE,
11161 &declares_class_or_enum);
11162 /* If an error occurred, there's no reason to attempt to parse the
11163 rest of the declaration. */
11164 if (cp_parser_error_occurred (parser))
11166 parser->type_definition_forbidden_message = saved_message;
11167 return error_mark_node;
11170 /* Peek at the next token. */
11171 token = cp_lexer_peek_token (parser->lexer);
11172 /* If the next token is a `)', `,', `=', `>', or `...', then there
11173 is no declarator. */
11174 if (token->type == CPP_CLOSE_PAREN
11175 || token->type == CPP_COMMA
11176 || token->type == CPP_EQ
11177 || token->type == CPP_ELLIPSIS
11178 || token->type == CPP_GREATER)
11180 declarator = NULL_TREE;
11181 if (parenthesized_p)
11182 *parenthesized_p = false;
11184 /* Otherwise, there should be a declarator. */
11187 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
11188 parser->default_arg_ok_p = false;
11190 /* After seeing a decl-specifier-seq, if the next token is not a
11191 "(", there is no possibility that the code is a valid
11192 expression. Therefore, if parsing tentatively, we commit at
11194 if (!parser->in_template_argument_list_p
11195 /* In an expression context, having seen:
11199 we cannot be sure whether we are looking at a
11200 function-type (taking a "char" as a parameter) or a cast
11201 of some object of type "char" to "int". */
11202 && !parser->in_type_id_in_expr_p
11203 && cp_parser_parsing_tentatively (parser)
11204 && !cp_parser_committed_to_tentative_parse (parser)
11205 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
11206 cp_parser_commit_to_tentative_parse (parser);
11207 /* Parse the declarator. */
11208 declarator = cp_parser_declarator (parser,
11209 CP_PARSER_DECLARATOR_EITHER,
11210 /*ctor_dtor_or_conv_p=*/NULL,
11212 parser->default_arg_ok_p = saved_default_arg_ok_p;
11213 /* After the declarator, allow more attributes. */
11214 attributes = chainon (attributes, cp_parser_attributes_opt (parser));
11217 /* The restriction on defining new types applies only to the type
11218 of the parameter, not to the default argument. */
11219 parser->type_definition_forbidden_message = saved_message;
11221 /* If the next token is `=', then process a default argument. */
11222 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11224 bool saved_greater_than_is_operator_p;
11225 /* Consume the `='. */
11226 cp_lexer_consume_token (parser->lexer);
11228 /* If we are defining a class, then the tokens that make up the
11229 default argument must be saved and processed later. */
11230 if (!template_parm_p && at_class_scope_p ()
11231 && TYPE_BEING_DEFINED (current_class_type))
11233 unsigned depth = 0;
11235 /* Create a DEFAULT_ARG to represented the unparsed default
11237 default_argument = make_node (DEFAULT_ARG);
11238 DEFARG_TOKENS (default_argument) = cp_token_cache_new ();
11240 /* Add tokens until we have processed the entire default
11247 /* Peek at the next token. */
11248 token = cp_lexer_peek_token (parser->lexer);
11249 /* What we do depends on what token we have. */
11250 switch (token->type)
11252 /* In valid code, a default argument must be
11253 immediately followed by a `,' `)', or `...'. */
11255 case CPP_CLOSE_PAREN:
11257 /* If we run into a non-nested `;', `}', or `]',
11258 then the code is invalid -- but the default
11259 argument is certainly over. */
11260 case CPP_SEMICOLON:
11261 case CPP_CLOSE_BRACE:
11262 case CPP_CLOSE_SQUARE:
11265 /* Update DEPTH, if necessary. */
11266 else if (token->type == CPP_CLOSE_PAREN
11267 || token->type == CPP_CLOSE_BRACE
11268 || token->type == CPP_CLOSE_SQUARE)
11272 case CPP_OPEN_PAREN:
11273 case CPP_OPEN_SQUARE:
11274 case CPP_OPEN_BRACE:
11279 /* If we see a non-nested `>', and `>' is not an
11280 operator, then it marks the end of the default
11282 if (!depth && !greater_than_is_operator_p)
11286 /* If we run out of tokens, issue an error message. */
11288 error ("file ends in default argument");
11294 /* In these cases, we should look for template-ids.
11295 For example, if the default argument is
11296 `X<int, double>()', we need to do name lookup to
11297 figure out whether or not `X' is a template; if
11298 so, the `,' does not end the default argument.
11300 That is not yet done. */
11307 /* If we've reached the end, stop. */
11311 /* Add the token to the token block. */
11312 token = cp_lexer_consume_token (parser->lexer);
11313 cp_token_cache_push_token (DEFARG_TOKENS (default_argument),
11317 /* Outside of a class definition, we can just parse the
11318 assignment-expression. */
11321 bool saved_local_variables_forbidden_p;
11323 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
11325 saved_greater_than_is_operator_p
11326 = parser->greater_than_is_operator_p;
11327 parser->greater_than_is_operator_p = greater_than_is_operator_p;
11328 /* Local variable names (and the `this' keyword) may not
11329 appear in a default argument. */
11330 saved_local_variables_forbidden_p
11331 = parser->local_variables_forbidden_p;
11332 parser->local_variables_forbidden_p = true;
11333 /* Parse the assignment-expression. */
11334 default_argument = cp_parser_assignment_expression (parser);
11335 /* Restore saved state. */
11336 parser->greater_than_is_operator_p
11337 = saved_greater_than_is_operator_p;
11338 parser->local_variables_forbidden_p
11339 = saved_local_variables_forbidden_p;
11341 if (!parser->default_arg_ok_p)
11343 if (!flag_pedantic_errors)
11344 warning ("deprecated use of default argument for parameter of non-function");
11347 error ("default arguments are only permitted for function parameters");
11348 default_argument = NULL_TREE;
11353 default_argument = NULL_TREE;
11355 /* Create the representation of the parameter. */
11357 decl_specifiers = tree_cons (attributes, NULL_TREE, decl_specifiers);
11358 parameter = build_tree_list (default_argument,
11359 build_tree_list (decl_specifiers,
11365 /* Parse a function-body.
11368 compound_statement */
11371 cp_parser_function_body (cp_parser *parser)
11373 cp_parser_compound_statement (parser, false);
11376 /* Parse a ctor-initializer-opt followed by a function-body. Return
11377 true if a ctor-initializer was present. */
11380 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
11383 bool ctor_initializer_p;
11385 /* Begin the function body. */
11386 body = begin_function_body ();
11387 /* Parse the optional ctor-initializer. */
11388 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
11389 /* Parse the function-body. */
11390 cp_parser_function_body (parser);
11391 /* Finish the function body. */
11392 finish_function_body (body);
11394 return ctor_initializer_p;
11397 /* Parse an initializer.
11400 = initializer-clause
11401 ( expression-list )
11403 Returns a expression representing the initializer. If no
11404 initializer is present, NULL_TREE is returned.
11406 *IS_PARENTHESIZED_INIT is set to TRUE if the `( expression-list )'
11407 production is used, and zero otherwise. *IS_PARENTHESIZED_INIT is
11408 set to FALSE if there is no initializer present. If there is an
11409 initializer, and it is not a constant-expression, *NON_CONSTANT_P
11410 is set to true; otherwise it is set to false. */
11413 cp_parser_initializer (cp_parser* parser, bool* is_parenthesized_init,
11414 bool* non_constant_p)
11419 /* Peek at the next token. */
11420 token = cp_lexer_peek_token (parser->lexer);
11422 /* Let our caller know whether or not this initializer was
11424 *is_parenthesized_init = (token->type == CPP_OPEN_PAREN);
11425 /* Assume that the initializer is constant. */
11426 *non_constant_p = false;
11428 if (token->type == CPP_EQ)
11430 /* Consume the `='. */
11431 cp_lexer_consume_token (parser->lexer);
11432 /* Parse the initializer-clause. */
11433 init = cp_parser_initializer_clause (parser, non_constant_p);
11435 else if (token->type == CPP_OPEN_PAREN)
11436 init = cp_parser_parenthesized_expression_list (parser, false,
11440 /* Anything else is an error. */
11441 cp_parser_error (parser, "expected initializer");
11442 init = error_mark_node;
11448 /* Parse an initializer-clause.
11450 initializer-clause:
11451 assignment-expression
11452 { initializer-list , [opt] }
11455 Returns an expression representing the initializer.
11457 If the `assignment-expression' production is used the value
11458 returned is simply a representation for the expression.
11460 Otherwise, a CONSTRUCTOR is returned. The CONSTRUCTOR_ELTS will be
11461 the elements of the initializer-list (or NULL_TREE, if the last
11462 production is used). The TREE_TYPE for the CONSTRUCTOR will be
11463 NULL_TREE. There is no way to detect whether or not the optional
11464 trailing `,' was provided. NON_CONSTANT_P is as for
11465 cp_parser_initializer. */
11468 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
11472 /* If it is not a `{', then we are looking at an
11473 assignment-expression. */
11474 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
11477 = cp_parser_constant_expression (parser,
11478 /*allow_non_constant_p=*/true,
11480 if (!*non_constant_p)
11481 initializer = fold_non_dependent_expr (initializer);
11485 /* Consume the `{' token. */
11486 cp_lexer_consume_token (parser->lexer);
11487 /* Create a CONSTRUCTOR to represent the braced-initializer. */
11488 initializer = make_node (CONSTRUCTOR);
11489 /* Mark it with TREE_HAS_CONSTRUCTOR. This should not be
11490 necessary, but check_initializer depends upon it, for
11492 TREE_HAS_CONSTRUCTOR (initializer) = 1;
11493 /* If it's not a `}', then there is a non-trivial initializer. */
11494 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
11496 /* Parse the initializer list. */
11497 CONSTRUCTOR_ELTS (initializer)
11498 = cp_parser_initializer_list (parser, non_constant_p);
11499 /* A trailing `,' token is allowed. */
11500 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
11501 cp_lexer_consume_token (parser->lexer);
11503 /* Now, there should be a trailing `}'. */
11504 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
11507 return initializer;
11510 /* Parse an initializer-list.
11514 initializer-list , initializer-clause
11519 identifier : initializer-clause
11520 initializer-list, identifier : initializer-clause
11522 Returns a TREE_LIST. The TREE_VALUE of each node is an expression
11523 for the initializer. If the TREE_PURPOSE is non-NULL, it is the
11524 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
11525 as for cp_parser_initializer. */
11528 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
11530 tree initializers = NULL_TREE;
11532 /* Assume all of the expressions are constant. */
11533 *non_constant_p = false;
11535 /* Parse the rest of the list. */
11541 bool clause_non_constant_p;
11543 /* If the next token is an identifier and the following one is a
11544 colon, we are looking at the GNU designated-initializer
11546 if (cp_parser_allow_gnu_extensions_p (parser)
11547 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
11548 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
11550 /* Consume the identifier. */
11551 identifier = cp_lexer_consume_token (parser->lexer)->value;
11552 /* Consume the `:'. */
11553 cp_lexer_consume_token (parser->lexer);
11556 identifier = NULL_TREE;
11558 /* Parse the initializer. */
11559 initializer = cp_parser_initializer_clause (parser,
11560 &clause_non_constant_p);
11561 /* If any clause is non-constant, so is the entire initializer. */
11562 if (clause_non_constant_p)
11563 *non_constant_p = true;
11564 /* Add it to the list. */
11565 initializers = tree_cons (identifier, initializer, initializers);
11567 /* If the next token is not a comma, we have reached the end of
11569 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11572 /* Peek at the next token. */
11573 token = cp_lexer_peek_nth_token (parser->lexer, 2);
11574 /* If the next token is a `}', then we're still done. An
11575 initializer-clause can have a trailing `,' after the
11576 initializer-list and before the closing `}'. */
11577 if (token->type == CPP_CLOSE_BRACE)
11580 /* Consume the `,' token. */
11581 cp_lexer_consume_token (parser->lexer);
11584 /* The initializers were built up in reverse order, so we need to
11585 reverse them now. */
11586 return nreverse (initializers);
11589 /* Classes [gram.class] */
11591 /* Parse a class-name.
11597 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
11598 to indicate that names looked up in dependent types should be
11599 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
11600 keyword has been used to indicate that the name that appears next
11601 is a template. TYPE_P is true iff the next name should be treated
11602 as class-name, even if it is declared to be some other kind of name
11603 as well. If CHECK_DEPENDENCY_P is FALSE, names are looked up in
11604 dependent scopes. If CLASS_HEAD_P is TRUE, this class is the class
11605 being defined in a class-head.
11607 Returns the TYPE_DECL representing the class. */
11610 cp_parser_class_name (cp_parser *parser,
11611 bool typename_keyword_p,
11612 bool template_keyword_p,
11614 bool check_dependency_p,
11616 bool is_declaration)
11623 /* All class-names start with an identifier. */
11624 token = cp_lexer_peek_token (parser->lexer);
11625 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
11627 cp_parser_error (parser, "expected class-name");
11628 return error_mark_node;
11631 /* PARSER->SCOPE can be cleared when parsing the template-arguments
11632 to a template-id, so we save it here. */
11633 scope = parser->scope;
11634 if (scope == error_mark_node)
11635 return error_mark_node;
11637 /* Any name names a type if we're following the `typename' keyword
11638 in a qualified name where the enclosing scope is type-dependent. */
11639 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
11640 && dependent_type_p (scope));
11641 /* Handle the common case (an identifier, but not a template-id)
11643 if (token->type == CPP_NAME
11644 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
11648 /* Look for the identifier. */
11649 identifier = cp_parser_identifier (parser);
11650 /* If the next token isn't an identifier, we are certainly not
11651 looking at a class-name. */
11652 if (identifier == error_mark_node)
11653 decl = error_mark_node;
11654 /* If we know this is a type-name, there's no need to look it
11656 else if (typename_p)
11660 /* If the next token is a `::', then the name must be a type
11663 [basic.lookup.qual]
11665 During the lookup for a name preceding the :: scope
11666 resolution operator, object, function, and enumerator
11667 names are ignored. */
11668 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
11670 /* Look up the name. */
11671 decl = cp_parser_lookup_name (parser, identifier,
11673 /*is_template=*/false,
11674 /*is_namespace=*/false,
11675 check_dependency_p);
11680 /* Try a template-id. */
11681 decl = cp_parser_template_id (parser, template_keyword_p,
11682 check_dependency_p,
11684 if (decl == error_mark_node)
11685 return error_mark_node;
11688 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
11690 /* If this is a typename, create a TYPENAME_TYPE. */
11691 if (typename_p && decl != error_mark_node)
11693 decl = make_typename_type (scope, decl, /*complain=*/1);
11694 if (decl != error_mark_node)
11695 decl = TYPE_NAME (decl);
11698 /* Check to see that it is really the name of a class. */
11699 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
11700 && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
11701 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
11702 /* Situations like this:
11704 template <typename T> struct A {
11705 typename T::template X<int>::I i;
11708 are problematic. Is `T::template X<int>' a class-name? The
11709 standard does not seem to be definitive, but there is no other
11710 valid interpretation of the following `::'. Therefore, those
11711 names are considered class-names. */
11712 decl = TYPE_NAME (make_typename_type (scope, decl, tf_error));
11713 else if (decl == error_mark_node
11714 || TREE_CODE (decl) != TYPE_DECL
11715 || !IS_AGGR_TYPE (TREE_TYPE (decl)))
11717 cp_parser_error (parser, "expected class-name");
11718 return error_mark_node;
11724 /* Parse a class-specifier.
11727 class-head { member-specification [opt] }
11729 Returns the TREE_TYPE representing the class. */
11732 cp_parser_class_specifier (cp_parser* parser)
11737 int has_trailing_semicolon;
11738 bool nested_name_specifier_p;
11739 unsigned saved_num_template_parameter_lists;
11740 bool pop_p = false;
11742 push_deferring_access_checks (dk_no_deferred);
11744 /* Parse the class-head. */
11745 type = cp_parser_class_head (parser,
11746 &nested_name_specifier_p,
11748 /* If the class-head was a semantic disaster, skip the entire body
11752 cp_parser_skip_to_end_of_block_or_statement (parser);
11753 pop_deferring_access_checks ();
11754 return error_mark_node;
11757 /* Look for the `{'. */
11758 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
11760 pop_deferring_access_checks ();
11761 return error_mark_node;
11764 /* Issue an error message if type-definitions are forbidden here. */
11765 cp_parser_check_type_definition (parser);
11766 /* Remember that we are defining one more class. */
11767 ++parser->num_classes_being_defined;
11768 /* Inside the class, surrounding template-parameter-lists do not
11770 saved_num_template_parameter_lists
11771 = parser->num_template_parameter_lists;
11772 parser->num_template_parameter_lists = 0;
11774 /* Start the class. */
11775 if (nested_name_specifier_p)
11776 pop_p = push_scope (CP_DECL_CONTEXT (TYPE_MAIN_DECL (type)));
11777 type = begin_class_definition (type);
11778 if (type == error_mark_node)
11779 /* If the type is erroneous, skip the entire body of the class. */
11780 cp_parser_skip_to_closing_brace (parser);
11782 /* Parse the member-specification. */
11783 cp_parser_member_specification_opt (parser);
11784 /* Look for the trailing `}'. */
11785 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
11786 /* We get better error messages by noticing a common problem: a
11787 missing trailing `;'. */
11788 token = cp_lexer_peek_token (parser->lexer);
11789 has_trailing_semicolon = (token->type == CPP_SEMICOLON);
11790 /* Look for trailing attributes to apply to this class. */
11791 if (cp_parser_allow_gnu_extensions_p (parser))
11793 tree sub_attr = cp_parser_attributes_opt (parser);
11794 attributes = chainon (attributes, sub_attr);
11796 if (type != error_mark_node)
11797 type = finish_struct (type, attributes);
11799 pop_scope (CP_DECL_CONTEXT (TYPE_MAIN_DECL (type)));
11800 /* If this class is not itself within the scope of another class,
11801 then we need to parse the bodies of all of the queued function
11802 definitions. Note that the queued functions defined in a class
11803 are not always processed immediately following the
11804 class-specifier for that class. Consider:
11807 struct B { void f() { sizeof (A); } };
11810 If `f' were processed before the processing of `A' were
11811 completed, there would be no way to compute the size of `A'.
11812 Note that the nesting we are interested in here is lexical --
11813 not the semantic nesting given by TYPE_CONTEXT. In particular,
11816 struct A { struct B; };
11817 struct A::B { void f() { } };
11819 there is no need to delay the parsing of `A::B::f'. */
11820 if (--parser->num_classes_being_defined == 0)
11825 /* In a first pass, parse default arguments to the functions.
11826 Then, in a second pass, parse the bodies of the functions.
11827 This two-phased approach handles cases like:
11835 for (TREE_PURPOSE (parser->unparsed_functions_queues)
11836 = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
11837 (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
11838 TREE_PURPOSE (parser->unparsed_functions_queues)
11839 = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
11841 fn = TREE_VALUE (queue_entry);
11842 /* Make sure that any template parameters are in scope. */
11843 maybe_begin_member_template_processing (fn);
11844 /* If there are default arguments that have not yet been processed,
11845 take care of them now. */
11846 cp_parser_late_parsing_default_args (parser, fn);
11847 /* Remove any template parameters from the symbol table. */
11848 maybe_end_member_template_processing ();
11850 /* Now parse the body of the functions. */
11851 for (TREE_VALUE (parser->unparsed_functions_queues)
11852 = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
11853 (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
11854 TREE_VALUE (parser->unparsed_functions_queues)
11855 = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
11857 /* Figure out which function we need to process. */
11858 fn = TREE_VALUE (queue_entry);
11860 /* A hack to prevent garbage collection. */
11863 /* Parse the function. */
11864 cp_parser_late_parsing_for_member (parser, fn);
11870 /* Put back any saved access checks. */
11871 pop_deferring_access_checks ();
11873 /* Restore the count of active template-parameter-lists. */
11874 parser->num_template_parameter_lists
11875 = saved_num_template_parameter_lists;
11880 /* Parse a class-head.
11883 class-key identifier [opt] base-clause [opt]
11884 class-key nested-name-specifier identifier base-clause [opt]
11885 class-key nested-name-specifier [opt] template-id
11889 class-key attributes identifier [opt] base-clause [opt]
11890 class-key attributes nested-name-specifier identifier base-clause [opt]
11891 class-key attributes nested-name-specifier [opt] template-id
11894 Returns the TYPE of the indicated class. Sets
11895 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
11896 involving a nested-name-specifier was used, and FALSE otherwise.
11898 Returns NULL_TREE if the class-head is syntactically valid, but
11899 semantically invalid in a way that means we should skip the entire
11900 body of the class. */
11903 cp_parser_class_head (cp_parser* parser,
11904 bool* nested_name_specifier_p,
11905 tree *attributes_p)
11908 tree nested_name_specifier;
11909 enum tag_types class_key;
11910 tree id = NULL_TREE;
11911 tree type = NULL_TREE;
11913 bool template_id_p = false;
11914 bool qualified_p = false;
11915 bool invalid_nested_name_p = false;
11916 bool invalid_explicit_specialization_p = false;
11917 bool pop_p = false;
11918 unsigned num_templates;
11920 /* Assume no nested-name-specifier will be present. */
11921 *nested_name_specifier_p = false;
11922 /* Assume no template parameter lists will be used in defining the
11926 /* Look for the class-key. */
11927 class_key = cp_parser_class_key (parser);
11928 if (class_key == none_type)
11929 return error_mark_node;
11931 /* Parse the attributes. */
11932 attributes = cp_parser_attributes_opt (parser);
11934 /* If the next token is `::', that is invalid -- but sometimes
11935 people do try to write:
11939 Handle this gracefully by accepting the extra qualifier, and then
11940 issuing an error about it later if this really is a
11941 class-head. If it turns out just to be an elaborated type
11942 specifier, remain silent. */
11943 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
11944 qualified_p = true;
11946 push_deferring_access_checks (dk_no_check);
11948 /* Determine the name of the class. Begin by looking for an
11949 optional nested-name-specifier. */
11950 nested_name_specifier
11951 = cp_parser_nested_name_specifier_opt (parser,
11952 /*typename_keyword_p=*/false,
11953 /*check_dependency_p=*/false,
11955 /*is_declaration=*/false);
11956 /* If there was a nested-name-specifier, then there *must* be an
11958 if (nested_name_specifier)
11960 /* Although the grammar says `identifier', it really means
11961 `class-name' or `template-name'. You are only allowed to
11962 define a class that has already been declared with this
11965 The proposed resolution for Core Issue 180 says that whever
11966 you see `class T::X' you should treat `X' as a type-name.
11968 It is OK to define an inaccessible class; for example:
11970 class A { class B; };
11973 We do not know if we will see a class-name, or a
11974 template-name. We look for a class-name first, in case the
11975 class-name is a template-id; if we looked for the
11976 template-name first we would stop after the template-name. */
11977 cp_parser_parse_tentatively (parser);
11978 type = cp_parser_class_name (parser,
11979 /*typename_keyword_p=*/false,
11980 /*template_keyword_p=*/false,
11982 /*check_dependency_p=*/false,
11983 /*class_head_p=*/true,
11984 /*is_declaration=*/false);
11985 /* If that didn't work, ignore the nested-name-specifier. */
11986 if (!cp_parser_parse_definitely (parser))
11988 invalid_nested_name_p = true;
11989 id = cp_parser_identifier (parser);
11990 if (id == error_mark_node)
11993 /* If we could not find a corresponding TYPE, treat this
11994 declaration like an unqualified declaration. */
11995 if (type == error_mark_node)
11996 nested_name_specifier = NULL_TREE;
11997 /* Otherwise, count the number of templates used in TYPE and its
11998 containing scopes. */
12003 for (scope = TREE_TYPE (type);
12004 scope && TREE_CODE (scope) != NAMESPACE_DECL;
12005 scope = (TYPE_P (scope)
12006 ? TYPE_CONTEXT (scope)
12007 : DECL_CONTEXT (scope)))
12009 && CLASS_TYPE_P (scope)
12010 && CLASSTYPE_TEMPLATE_INFO (scope)
12011 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
12012 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
12016 /* Otherwise, the identifier is optional. */
12019 /* We don't know whether what comes next is a template-id,
12020 an identifier, or nothing at all. */
12021 cp_parser_parse_tentatively (parser);
12022 /* Check for a template-id. */
12023 id = cp_parser_template_id (parser,
12024 /*template_keyword_p=*/false,
12025 /*check_dependency_p=*/true,
12026 /*is_declaration=*/true);
12027 /* If that didn't work, it could still be an identifier. */
12028 if (!cp_parser_parse_definitely (parser))
12030 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12031 id = cp_parser_identifier (parser);
12037 template_id_p = true;
12042 pop_deferring_access_checks ();
12045 cp_parser_check_for_invalid_template_id (parser, id);
12047 /* If it's not a `:' or a `{' then we can't really be looking at a
12048 class-head, since a class-head only appears as part of a
12049 class-specifier. We have to detect this situation before calling
12050 xref_tag, since that has irreversible side-effects. */
12051 if (!cp_parser_next_token_starts_class_definition_p (parser))
12053 cp_parser_error (parser, "expected `{' or `:'");
12054 return error_mark_node;
12057 /* At this point, we're going ahead with the class-specifier, even
12058 if some other problem occurs. */
12059 cp_parser_commit_to_tentative_parse (parser);
12060 /* Issue the error about the overly-qualified name now. */
12062 cp_parser_error (parser,
12063 "global qualification of class name is invalid");
12064 else if (invalid_nested_name_p)
12065 cp_parser_error (parser,
12066 "qualified name does not name a class");
12067 else if (nested_name_specifier)
12070 /* Figure out in what scope the declaration is being placed. */
12071 scope = current_scope ();
12073 scope = current_namespace;
12074 /* If that scope does not contain the scope in which the
12075 class was originally declared, the program is invalid. */
12076 if (scope && !is_ancestor (scope, nested_name_specifier))
12078 error ("declaration of `%D' in `%D' which does not "
12079 "enclose `%D'", type, scope, nested_name_specifier);
12085 A declarator-id shall not be qualified exception of the
12086 definition of a ... nested class outside of its class
12087 ... [or] a the definition or explicit instantiation of a
12088 class member of a namespace outside of its namespace. */
12089 if (scope == nested_name_specifier)
12091 pedwarn ("extra qualification ignored");
12092 nested_name_specifier = NULL_TREE;
12096 /* An explicit-specialization must be preceded by "template <>". If
12097 it is not, try to recover gracefully. */
12098 if (at_namespace_scope_p ()
12099 && parser->num_template_parameter_lists == 0
12102 error ("an explicit specialization must be preceded by 'template <>'");
12103 invalid_explicit_specialization_p = true;
12104 /* Take the same action that would have been taken by
12105 cp_parser_explicit_specialization. */
12106 ++parser->num_template_parameter_lists;
12107 begin_specialization ();
12109 /* There must be no "return" statements between this point and the
12110 end of this function; set "type "to the correct return value and
12111 use "goto done;" to return. */
12112 /* Make sure that the right number of template parameters were
12114 if (!cp_parser_check_template_parameters (parser, num_templates))
12116 /* If something went wrong, there is no point in even trying to
12117 process the class-definition. */
12122 /* Look up the type. */
12125 type = TREE_TYPE (id);
12126 maybe_process_partial_specialization (type);
12128 else if (!nested_name_specifier)
12130 /* If the class was unnamed, create a dummy name. */
12132 id = make_anon_name ();
12133 type = xref_tag (class_key, id, /*globalize=*/false,
12134 parser->num_template_parameter_lists);
12139 bool pop_p = false;
12143 template <typename T> struct S { struct T };
12144 template <typename T> struct S<T>::T { };
12146 we will get a TYPENAME_TYPE when processing the definition of
12147 `S::T'. We need to resolve it to the actual type before we
12148 try to define it. */
12149 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
12151 class_type = resolve_typename_type (TREE_TYPE (type),
12152 /*only_current_p=*/false);
12153 if (class_type != error_mark_node)
12154 type = TYPE_NAME (class_type);
12157 cp_parser_error (parser, "could not resolve typename type");
12158 type = error_mark_node;
12162 maybe_process_partial_specialization (TREE_TYPE (type));
12163 class_type = current_class_type;
12164 /* Enter the scope indicated by the nested-name-specifier. */
12165 if (nested_name_specifier)
12166 pop_p = push_scope (nested_name_specifier);
12167 /* Get the canonical version of this type. */
12168 type = TYPE_MAIN_DECL (TREE_TYPE (type));
12169 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
12170 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
12171 type = push_template_decl (type);
12172 type = TREE_TYPE (type);
12173 if (nested_name_specifier)
12175 *nested_name_specifier_p = true;
12177 pop_scope (nested_name_specifier);
12180 /* Indicate whether this class was declared as a `class' or as a
12182 if (TREE_CODE (type) == RECORD_TYPE)
12183 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
12184 cp_parser_check_class_key (class_key, type);
12186 /* Enter the scope containing the class; the names of base classes
12187 should be looked up in that context. For example, given:
12189 struct A { struct B {}; struct C; };
12190 struct A::C : B {};
12193 if (nested_name_specifier)
12194 pop_p = push_scope (nested_name_specifier);
12195 /* Now, look for the base-clause. */
12196 token = cp_lexer_peek_token (parser->lexer);
12197 if (token->type == CPP_COLON)
12201 /* Get the list of base-classes. */
12202 bases = cp_parser_base_clause (parser);
12203 /* Process them. */
12204 xref_basetypes (type, bases);
12206 /* Leave the scope given by the nested-name-specifier. We will
12207 enter the class scope itself while processing the members. */
12209 pop_scope (nested_name_specifier);
12212 if (invalid_explicit_specialization_p)
12214 end_specialization ();
12215 --parser->num_template_parameter_lists;
12217 *attributes_p = attributes;
12221 /* Parse a class-key.
12228 Returns the kind of class-key specified, or none_type to indicate
12231 static enum tag_types
12232 cp_parser_class_key (cp_parser* parser)
12235 enum tag_types tag_type;
12237 /* Look for the class-key. */
12238 token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
12242 /* Check to see if the TOKEN is a class-key. */
12243 tag_type = cp_parser_token_is_class_key (token);
12245 cp_parser_error (parser, "expected class-key");
12249 /* Parse an (optional) member-specification.
12251 member-specification:
12252 member-declaration member-specification [opt]
12253 access-specifier : member-specification [opt] */
12256 cp_parser_member_specification_opt (cp_parser* parser)
12263 /* Peek at the next token. */
12264 token = cp_lexer_peek_token (parser->lexer);
12265 /* If it's a `}', or EOF then we've seen all the members. */
12266 if (token->type == CPP_CLOSE_BRACE || token->type == CPP_EOF)
12269 /* See if this token is a keyword. */
12270 keyword = token->keyword;
12274 case RID_PROTECTED:
12276 /* Consume the access-specifier. */
12277 cp_lexer_consume_token (parser->lexer);
12278 /* Remember which access-specifier is active. */
12279 current_access_specifier = token->value;
12280 /* Look for the `:'. */
12281 cp_parser_require (parser, CPP_COLON, "`:'");
12285 /* Otherwise, the next construction must be a
12286 member-declaration. */
12287 cp_parser_member_declaration (parser);
12292 /* Parse a member-declaration.
12294 member-declaration:
12295 decl-specifier-seq [opt] member-declarator-list [opt] ;
12296 function-definition ; [opt]
12297 :: [opt] nested-name-specifier template [opt] unqualified-id ;
12299 template-declaration
12301 member-declarator-list:
12303 member-declarator-list , member-declarator
12306 declarator pure-specifier [opt]
12307 declarator constant-initializer [opt]
12308 identifier [opt] : constant-expression
12312 member-declaration:
12313 __extension__ member-declaration
12316 declarator attributes [opt] pure-specifier [opt]
12317 declarator attributes [opt] constant-initializer [opt]
12318 identifier [opt] attributes [opt] : constant-expression */
12321 cp_parser_member_declaration (cp_parser* parser)
12323 tree decl_specifiers;
12324 tree prefix_attributes;
12326 int declares_class_or_enum;
12329 int saved_pedantic;
12331 /* Check for the `__extension__' keyword. */
12332 if (cp_parser_extension_opt (parser, &saved_pedantic))
12335 cp_parser_member_declaration (parser);
12336 /* Restore the old value of the PEDANTIC flag. */
12337 pedantic = saved_pedantic;
12342 /* Check for a template-declaration. */
12343 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
12345 /* Parse the template-declaration. */
12346 cp_parser_template_declaration (parser, /*member_p=*/true);
12351 /* Check for a using-declaration. */
12352 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
12354 /* Parse the using-declaration. */
12355 cp_parser_using_declaration (parser);
12360 /* Parse the decl-specifier-seq. */
12362 = cp_parser_decl_specifier_seq (parser,
12363 CP_PARSER_FLAGS_OPTIONAL,
12364 &prefix_attributes,
12365 &declares_class_or_enum);
12366 /* Check for an invalid type-name. */
12367 if (cp_parser_diagnose_invalid_type_name (parser))
12369 /* If there is no declarator, then the decl-specifier-seq should
12371 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12373 /* If there was no decl-specifier-seq, and the next token is a
12374 `;', then we have something like:
12380 Each member-declaration shall declare at least one member
12381 name of the class. */
12382 if (!decl_specifiers)
12385 pedwarn ("extra semicolon");
12391 /* See if this declaration is a friend. */
12392 friend_p = cp_parser_friend_p (decl_specifiers);
12393 /* If there were decl-specifiers, check to see if there was
12394 a class-declaration. */
12395 type = check_tag_decl (decl_specifiers);
12396 /* Nested classes have already been added to the class, but
12397 a `friend' needs to be explicitly registered. */
12400 /* If the `friend' keyword was present, the friend must
12401 be introduced with a class-key. */
12402 if (!declares_class_or_enum)
12403 error ("a class-key must be used when declaring a friend");
12406 template <typename T> struct A {
12407 friend struct A<T>::B;
12410 A<T>::B will be represented by a TYPENAME_TYPE, and
12411 therefore not recognized by check_tag_decl. */
12416 for (specifier = decl_specifiers;
12418 specifier = TREE_CHAIN (specifier))
12420 tree s = TREE_VALUE (specifier);
12422 if (TREE_CODE (s) == IDENTIFIER_NODE)
12423 get_global_value_if_present (s, &type);
12424 if (TREE_CODE (s) == TYPE_DECL)
12433 if (!type || !TYPE_P (type))
12434 error ("friend declaration does not name a class or "
12437 make_friend_class (current_class_type, type,
12438 /*complain=*/true);
12440 /* If there is no TYPE, an error message will already have
12444 /* An anonymous aggregate has to be handled specially; such
12445 a declaration really declares a data member (with a
12446 particular type), as opposed to a nested class. */
12447 else if (ANON_AGGR_TYPE_P (type))
12449 /* Remove constructors and such from TYPE, now that we
12450 know it is an anonymous aggregate. */
12451 fixup_anonymous_aggr (type);
12452 /* And make the corresponding data member. */
12453 decl = build_decl (FIELD_DECL, NULL_TREE, type);
12454 /* Add it to the class. */
12455 finish_member_declaration (decl);
12458 cp_parser_check_access_in_redeclaration (TYPE_NAME (type));
12463 /* See if these declarations will be friends. */
12464 friend_p = cp_parser_friend_p (decl_specifiers);
12466 /* Keep going until we hit the `;' at the end of the
12468 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12470 tree attributes = NULL_TREE;
12471 tree first_attribute;
12473 /* Peek at the next token. */
12474 token = cp_lexer_peek_token (parser->lexer);
12476 /* Check for a bitfield declaration. */
12477 if (token->type == CPP_COLON
12478 || (token->type == CPP_NAME
12479 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
12485 /* Get the name of the bitfield. Note that we cannot just
12486 check TOKEN here because it may have been invalidated by
12487 the call to cp_lexer_peek_nth_token above. */
12488 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
12489 identifier = cp_parser_identifier (parser);
12491 identifier = NULL_TREE;
12493 /* Consume the `:' token. */
12494 cp_lexer_consume_token (parser->lexer);
12495 /* Get the width of the bitfield. */
12497 = cp_parser_constant_expression (parser,
12498 /*allow_non_constant=*/false,
12501 /* Look for attributes that apply to the bitfield. */
12502 attributes = cp_parser_attributes_opt (parser);
12503 /* Remember which attributes are prefix attributes and
12505 first_attribute = attributes;
12506 /* Combine the attributes. */
12507 attributes = chainon (prefix_attributes, attributes);
12509 /* Create the bitfield declaration. */
12510 decl = grokbitfield (identifier,
12513 /* Apply the attributes. */
12514 cplus_decl_attributes (&decl, attributes, /*flags=*/0);
12520 tree asm_specification;
12521 int ctor_dtor_or_conv_p;
12523 /* Parse the declarator. */
12525 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
12526 &ctor_dtor_or_conv_p,
12527 /*parenthesized_p=*/NULL);
12529 /* If something went wrong parsing the declarator, make sure
12530 that we at least consume some tokens. */
12531 if (declarator == error_mark_node)
12533 /* Skip to the end of the statement. */
12534 cp_parser_skip_to_end_of_statement (parser);
12535 /* If the next token is not a semicolon, that is
12536 probably because we just skipped over the body of
12537 a function. So, we consume a semicolon if
12538 present, but do not issue an error message if it
12540 if (cp_lexer_next_token_is (parser->lexer,
12542 cp_lexer_consume_token (parser->lexer);
12546 cp_parser_check_for_definition_in_return_type
12547 (declarator, declares_class_or_enum);
12549 /* Look for an asm-specification. */
12550 asm_specification = cp_parser_asm_specification_opt (parser);
12551 /* Look for attributes that apply to the declaration. */
12552 attributes = cp_parser_attributes_opt (parser);
12553 /* Remember which attributes are prefix attributes and
12555 first_attribute = attributes;
12556 /* Combine the attributes. */
12557 attributes = chainon (prefix_attributes, attributes);
12559 /* If it's an `=', then we have a constant-initializer or a
12560 pure-specifier. It is not correct to parse the
12561 initializer before registering the member declaration
12562 since the member declaration should be in scope while
12563 its initializer is processed. However, the rest of the
12564 front end does not yet provide an interface that allows
12565 us to handle this correctly. */
12566 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12570 A pure-specifier shall be used only in the declaration of
12571 a virtual function.
12573 A member-declarator can contain a constant-initializer
12574 only if it declares a static member of integral or
12577 Therefore, if the DECLARATOR is for a function, we look
12578 for a pure-specifier; otherwise, we look for a
12579 constant-initializer. When we call `grokfield', it will
12580 perform more stringent semantics checks. */
12581 if (TREE_CODE (declarator) == CALL_EXPR)
12582 initializer = cp_parser_pure_specifier (parser);
12584 /* Parse the initializer. */
12585 initializer = cp_parser_constant_initializer (parser);
12587 /* Otherwise, there is no initializer. */
12589 initializer = NULL_TREE;
12591 /* See if we are probably looking at a function
12592 definition. We are certainly not looking at at a
12593 member-declarator. Calling `grokfield' has
12594 side-effects, so we must not do it unless we are sure
12595 that we are looking at a member-declarator. */
12596 if (cp_parser_token_starts_function_definition_p
12597 (cp_lexer_peek_token (parser->lexer)))
12599 /* The grammar does not allow a pure-specifier to be
12600 used when a member function is defined. (It is
12601 possible that this fact is an oversight in the
12602 standard, since a pure function may be defined
12603 outside of the class-specifier. */
12605 error ("pure-specifier on function-definition");
12606 decl = cp_parser_save_member_function_body (parser,
12610 /* If the member was not a friend, declare it here. */
12612 finish_member_declaration (decl);
12613 /* Peek at the next token. */
12614 token = cp_lexer_peek_token (parser->lexer);
12615 /* If the next token is a semicolon, consume it. */
12616 if (token->type == CPP_SEMICOLON)
12617 cp_lexer_consume_token (parser->lexer);
12622 /* Create the declaration. */
12623 decl = grokfield (declarator, decl_specifiers,
12624 initializer, asm_specification,
12626 /* Any initialization must have been from a
12627 constant-expression. */
12628 if (decl && TREE_CODE (decl) == VAR_DECL && initializer)
12629 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = 1;
12633 /* Reset PREFIX_ATTRIBUTES. */
12634 while (attributes && TREE_CHAIN (attributes) != first_attribute)
12635 attributes = TREE_CHAIN (attributes);
12637 TREE_CHAIN (attributes) = NULL_TREE;
12639 /* If there is any qualification still in effect, clear it
12640 now; we will be starting fresh with the next declarator. */
12641 parser->scope = NULL_TREE;
12642 parser->qualifying_scope = NULL_TREE;
12643 parser->object_scope = NULL_TREE;
12644 /* If it's a `,', then there are more declarators. */
12645 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
12646 cp_lexer_consume_token (parser->lexer);
12647 /* If the next token isn't a `;', then we have a parse error. */
12648 else if (cp_lexer_next_token_is_not (parser->lexer,
12651 cp_parser_error (parser, "expected `;'");
12652 /* Skip tokens until we find a `;'. */
12653 cp_parser_skip_to_end_of_statement (parser);
12660 /* Add DECL to the list of members. */
12662 finish_member_declaration (decl);
12664 if (TREE_CODE (decl) == FUNCTION_DECL)
12665 cp_parser_save_default_args (parser, decl);
12670 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
12673 /* Parse a pure-specifier.
12678 Returns INTEGER_ZERO_NODE if a pure specifier is found.
12679 Otherwise, ERROR_MARK_NODE is returned. */
12682 cp_parser_pure_specifier (cp_parser* parser)
12686 /* Look for the `=' token. */
12687 if (!cp_parser_require (parser, CPP_EQ, "`='"))
12688 return error_mark_node;
12689 /* Look for the `0' token. */
12690 token = cp_parser_require (parser, CPP_NUMBER, "`0'");
12691 /* Unfortunately, this will accept `0L' and `0x00' as well. We need
12692 to get information from the lexer about how the number was
12693 spelled in order to fix this problem. */
12694 if (!token || !integer_zerop (token->value))
12695 return error_mark_node;
12697 return integer_zero_node;
12700 /* Parse a constant-initializer.
12702 constant-initializer:
12703 = constant-expression
12705 Returns a representation of the constant-expression. */
12708 cp_parser_constant_initializer (cp_parser* parser)
12710 /* Look for the `=' token. */
12711 if (!cp_parser_require (parser, CPP_EQ, "`='"))
12712 return error_mark_node;
12714 /* It is invalid to write:
12716 struct S { static const int i = { 7 }; };
12719 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12721 cp_parser_error (parser,
12722 "a brace-enclosed initializer is not allowed here");
12723 /* Consume the opening brace. */
12724 cp_lexer_consume_token (parser->lexer);
12725 /* Skip the initializer. */
12726 cp_parser_skip_to_closing_brace (parser);
12727 /* Look for the trailing `}'. */
12728 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
12730 return error_mark_node;
12733 return cp_parser_constant_expression (parser,
12734 /*allow_non_constant=*/false,
12738 /* Derived classes [gram.class.derived] */
12740 /* Parse a base-clause.
12743 : base-specifier-list
12745 base-specifier-list:
12747 base-specifier-list , base-specifier
12749 Returns a TREE_LIST representing the base-classes, in the order in
12750 which they were declared. The representation of each node is as
12751 described by cp_parser_base_specifier.
12753 In the case that no bases are specified, this function will return
12754 NULL_TREE, not ERROR_MARK_NODE. */
12757 cp_parser_base_clause (cp_parser* parser)
12759 tree bases = NULL_TREE;
12761 /* Look for the `:' that begins the list. */
12762 cp_parser_require (parser, CPP_COLON, "`:'");
12764 /* Scan the base-specifier-list. */
12770 /* Look for the base-specifier. */
12771 base = cp_parser_base_specifier (parser);
12772 /* Add BASE to the front of the list. */
12773 if (base != error_mark_node)
12775 TREE_CHAIN (base) = bases;
12778 /* Peek at the next token. */
12779 token = cp_lexer_peek_token (parser->lexer);
12780 /* If it's not a comma, then the list is complete. */
12781 if (token->type != CPP_COMMA)
12783 /* Consume the `,'. */
12784 cp_lexer_consume_token (parser->lexer);
12787 /* PARSER->SCOPE may still be non-NULL at this point, if the last
12788 base class had a qualified name. However, the next name that
12789 appears is certainly not qualified. */
12790 parser->scope = NULL_TREE;
12791 parser->qualifying_scope = NULL_TREE;
12792 parser->object_scope = NULL_TREE;
12794 return nreverse (bases);
12797 /* Parse a base-specifier.
12800 :: [opt] nested-name-specifier [opt] class-name
12801 virtual access-specifier [opt] :: [opt] nested-name-specifier
12803 access-specifier virtual [opt] :: [opt] nested-name-specifier
12806 Returns a TREE_LIST. The TREE_PURPOSE will be one of
12807 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
12808 indicate the specifiers provided. The TREE_VALUE will be a TYPE
12809 (or the ERROR_MARK_NODE) indicating the type that was specified. */
12812 cp_parser_base_specifier (cp_parser* parser)
12816 bool virtual_p = false;
12817 bool duplicate_virtual_error_issued_p = false;
12818 bool duplicate_access_error_issued_p = false;
12819 bool class_scope_p, template_p;
12820 tree access = access_default_node;
12823 /* Process the optional `virtual' and `access-specifier'. */
12826 /* Peek at the next token. */
12827 token = cp_lexer_peek_token (parser->lexer);
12828 /* Process `virtual'. */
12829 switch (token->keyword)
12832 /* If `virtual' appears more than once, issue an error. */
12833 if (virtual_p && !duplicate_virtual_error_issued_p)
12835 cp_parser_error (parser,
12836 "`virtual' specified more than once in base-specified");
12837 duplicate_virtual_error_issued_p = true;
12842 /* Consume the `virtual' token. */
12843 cp_lexer_consume_token (parser->lexer);
12848 case RID_PROTECTED:
12850 /* If more than one access specifier appears, issue an
12852 if (access != access_default_node
12853 && !duplicate_access_error_issued_p)
12855 cp_parser_error (parser,
12856 "more than one access specifier in base-specified");
12857 duplicate_access_error_issued_p = true;
12860 access = ridpointers[(int) token->keyword];
12862 /* Consume the access-specifier. */
12863 cp_lexer_consume_token (parser->lexer);
12872 /* It is not uncommon to see programs mechanically, errouneously, use
12873 the 'typename' keyword to denote (dependent) qualified types
12874 as base classes. */
12875 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
12877 if (!processing_template_decl)
12878 error ("keyword `typename' not allowed outside of templates");
12880 error ("keyword `typename' not allowed in this context "
12881 "(the base class is implicitly a type)");
12882 cp_lexer_consume_token (parser->lexer);
12885 /* Look for the optional `::' operator. */
12886 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
12887 /* Look for the nested-name-specifier. The simplest way to
12892 The keyword `typename' is not permitted in a base-specifier or
12893 mem-initializer; in these contexts a qualified name that
12894 depends on a template-parameter is implicitly assumed to be a
12897 is to pretend that we have seen the `typename' keyword at this
12899 cp_parser_nested_name_specifier_opt (parser,
12900 /*typename_keyword_p=*/true,
12901 /*check_dependency_p=*/true,
12903 /*is_declaration=*/true);
12904 /* If the base class is given by a qualified name, assume that names
12905 we see are type names or templates, as appropriate. */
12906 class_scope_p = (parser->scope && TYPE_P (parser->scope));
12907 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
12909 /* Finally, look for the class-name. */
12910 type = cp_parser_class_name (parser,
12914 /*check_dependency_p=*/true,
12915 /*class_head_p=*/false,
12916 /*is_declaration=*/true);
12918 if (type == error_mark_node)
12919 return error_mark_node;
12921 return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
12924 /* Exception handling [gram.exception] */
12926 /* Parse an (optional) exception-specification.
12928 exception-specification:
12929 throw ( type-id-list [opt] )
12931 Returns a TREE_LIST representing the exception-specification. The
12932 TREE_VALUE of each node is a type. */
12935 cp_parser_exception_specification_opt (cp_parser* parser)
12940 /* Peek at the next token. */
12941 token = cp_lexer_peek_token (parser->lexer);
12942 /* If it's not `throw', then there's no exception-specification. */
12943 if (!cp_parser_is_keyword (token, RID_THROW))
12946 /* Consume the `throw'. */
12947 cp_lexer_consume_token (parser->lexer);
12949 /* Look for the `('. */
12950 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
12952 /* Peek at the next token. */
12953 token = cp_lexer_peek_token (parser->lexer);
12954 /* If it's not a `)', then there is a type-id-list. */
12955 if (token->type != CPP_CLOSE_PAREN)
12957 const char *saved_message;
12959 /* Types may not be defined in an exception-specification. */
12960 saved_message = parser->type_definition_forbidden_message;
12961 parser->type_definition_forbidden_message
12962 = "types may not be defined in an exception-specification";
12963 /* Parse the type-id-list. */
12964 type_id_list = cp_parser_type_id_list (parser);
12965 /* Restore the saved message. */
12966 parser->type_definition_forbidden_message = saved_message;
12969 type_id_list = empty_except_spec;
12971 /* Look for the `)'. */
12972 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
12974 return type_id_list;
12977 /* Parse an (optional) type-id-list.
12981 type-id-list , type-id
12983 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
12984 in the order that the types were presented. */
12987 cp_parser_type_id_list (cp_parser* parser)
12989 tree types = NULL_TREE;
12996 /* Get the next type-id. */
12997 type = cp_parser_type_id (parser);
12998 /* Add it to the list. */
12999 types = add_exception_specifier (types, type, /*complain=*/1);
13000 /* Peek at the next token. */
13001 token = cp_lexer_peek_token (parser->lexer);
13002 /* If it is not a `,', we are done. */
13003 if (token->type != CPP_COMMA)
13005 /* Consume the `,'. */
13006 cp_lexer_consume_token (parser->lexer);
13009 return nreverse (types);
13012 /* Parse a try-block.
13015 try compound-statement handler-seq */
13018 cp_parser_try_block (cp_parser* parser)
13022 cp_parser_require_keyword (parser, RID_TRY, "`try'");
13023 try_block = begin_try_block ();
13024 cp_parser_compound_statement (parser, false);
13025 finish_try_block (try_block);
13026 cp_parser_handler_seq (parser);
13027 finish_handler_sequence (try_block);
13032 /* Parse a function-try-block.
13034 function-try-block:
13035 try ctor-initializer [opt] function-body handler-seq */
13038 cp_parser_function_try_block (cp_parser* parser)
13041 bool ctor_initializer_p;
13043 /* Look for the `try' keyword. */
13044 if (!cp_parser_require_keyword (parser, RID_TRY, "`try'"))
13046 /* Let the rest of the front-end know where we are. */
13047 try_block = begin_function_try_block ();
13048 /* Parse the function-body. */
13050 = cp_parser_ctor_initializer_opt_and_function_body (parser);
13051 /* We're done with the `try' part. */
13052 finish_function_try_block (try_block);
13053 /* Parse the handlers. */
13054 cp_parser_handler_seq (parser);
13055 /* We're done with the handlers. */
13056 finish_function_handler_sequence (try_block);
13058 return ctor_initializer_p;
13061 /* Parse a handler-seq.
13064 handler handler-seq [opt] */
13067 cp_parser_handler_seq (cp_parser* parser)
13073 /* Parse the handler. */
13074 cp_parser_handler (parser);
13075 /* Peek at the next token. */
13076 token = cp_lexer_peek_token (parser->lexer);
13077 /* If it's not `catch' then there are no more handlers. */
13078 if (!cp_parser_is_keyword (token, RID_CATCH))
13083 /* Parse a handler.
13086 catch ( exception-declaration ) compound-statement */
13089 cp_parser_handler (cp_parser* parser)
13094 cp_parser_require_keyword (parser, RID_CATCH, "`catch'");
13095 handler = begin_handler ();
13096 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13097 declaration = cp_parser_exception_declaration (parser);
13098 finish_handler_parms (declaration, handler);
13099 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13100 cp_parser_compound_statement (parser, false);
13101 finish_handler (handler);
13104 /* Parse an exception-declaration.
13106 exception-declaration:
13107 type-specifier-seq declarator
13108 type-specifier-seq abstract-declarator
13112 Returns a VAR_DECL for the declaration, or NULL_TREE if the
13113 ellipsis variant is used. */
13116 cp_parser_exception_declaration (cp_parser* parser)
13118 tree type_specifiers;
13120 const char *saved_message;
13122 /* If it's an ellipsis, it's easy to handle. */
13123 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13125 /* Consume the `...' token. */
13126 cp_lexer_consume_token (parser->lexer);
13130 /* Types may not be defined in exception-declarations. */
13131 saved_message = parser->type_definition_forbidden_message;
13132 parser->type_definition_forbidden_message
13133 = "types may not be defined in exception-declarations";
13135 /* Parse the type-specifier-seq. */
13136 type_specifiers = cp_parser_type_specifier_seq (parser);
13137 /* If it's a `)', then there is no declarator. */
13138 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
13139 declarator = NULL_TREE;
13141 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
13142 /*ctor_dtor_or_conv_p=*/NULL,
13143 /*parenthesized_p=*/NULL);
13145 /* Restore the saved message. */
13146 parser->type_definition_forbidden_message = saved_message;
13148 return start_handler_parms (type_specifiers, declarator);
13151 /* Parse a throw-expression.
13154 throw assignment-expression [opt]
13156 Returns a THROW_EXPR representing the throw-expression. */
13159 cp_parser_throw_expression (cp_parser* parser)
13164 cp_parser_require_keyword (parser, RID_THROW, "`throw'");
13165 token = cp_lexer_peek_token (parser->lexer);
13166 /* Figure out whether or not there is an assignment-expression
13167 following the "throw" keyword. */
13168 if (token->type == CPP_COMMA
13169 || token->type == CPP_SEMICOLON
13170 || token->type == CPP_CLOSE_PAREN
13171 || token->type == CPP_CLOSE_SQUARE
13172 || token->type == CPP_CLOSE_BRACE
13173 || token->type == CPP_COLON)
13174 expression = NULL_TREE;
13176 expression = cp_parser_assignment_expression (parser);
13178 return build_throw (expression);
13181 /* GNU Extensions */
13183 /* Parse an (optional) asm-specification.
13186 asm ( string-literal )
13188 If the asm-specification is present, returns a STRING_CST
13189 corresponding to the string-literal. Otherwise, returns
13193 cp_parser_asm_specification_opt (cp_parser* parser)
13196 tree asm_specification;
13198 /* Peek at the next token. */
13199 token = cp_lexer_peek_token (parser->lexer);
13200 /* If the next token isn't the `asm' keyword, then there's no
13201 asm-specification. */
13202 if (!cp_parser_is_keyword (token, RID_ASM))
13205 /* Consume the `asm' token. */
13206 cp_lexer_consume_token (parser->lexer);
13207 /* Look for the `('. */
13208 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13210 /* Look for the string-literal. */
13211 token = cp_parser_require (parser, CPP_STRING, "string-literal");
13213 asm_specification = token->value;
13215 asm_specification = NULL_TREE;
13217 /* Look for the `)'. */
13218 cp_parser_require (parser, CPP_CLOSE_PAREN, "`('");
13220 return asm_specification;
13223 /* Parse an asm-operand-list.
13227 asm-operand-list , asm-operand
13230 string-literal ( expression )
13231 [ string-literal ] string-literal ( expression )
13233 Returns a TREE_LIST representing the operands. The TREE_VALUE of
13234 each node is the expression. The TREE_PURPOSE is itself a
13235 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
13236 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
13237 is a STRING_CST for the string literal before the parenthesis. */
13240 cp_parser_asm_operand_list (cp_parser* parser)
13242 tree asm_operands = NULL_TREE;
13246 tree string_literal;
13251 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
13253 /* Consume the `[' token. */
13254 cp_lexer_consume_token (parser->lexer);
13255 /* Read the operand name. */
13256 name = cp_parser_identifier (parser);
13257 if (name != error_mark_node)
13258 name = build_string (IDENTIFIER_LENGTH (name),
13259 IDENTIFIER_POINTER (name));
13260 /* Look for the closing `]'. */
13261 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
13265 /* Look for the string-literal. */
13266 token = cp_parser_require (parser, CPP_STRING, "string-literal");
13267 string_literal = token ? token->value : error_mark_node;
13268 /* Look for the `('. */
13269 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13270 /* Parse the expression. */
13271 expression = cp_parser_expression (parser);
13272 /* Look for the `)'. */
13273 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13274 /* Add this operand to the list. */
13275 asm_operands = tree_cons (build_tree_list (name, string_literal),
13278 /* If the next token is not a `,', there are no more
13280 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13282 /* Consume the `,'. */
13283 cp_lexer_consume_token (parser->lexer);
13286 return nreverse (asm_operands);
13289 /* Parse an asm-clobber-list.
13293 asm-clobber-list , string-literal
13295 Returns a TREE_LIST, indicating the clobbers in the order that they
13296 appeared. The TREE_VALUE of each node is a STRING_CST. */
13299 cp_parser_asm_clobber_list (cp_parser* parser)
13301 tree clobbers = NULL_TREE;
13306 tree string_literal;
13308 /* Look for the string literal. */
13309 token = cp_parser_require (parser, CPP_STRING, "string-literal");
13310 string_literal = token ? token->value : error_mark_node;
13311 /* Add it to the list. */
13312 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
13313 /* If the next token is not a `,', then the list is
13315 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13317 /* Consume the `,' token. */
13318 cp_lexer_consume_token (parser->lexer);
13324 /* Parse an (optional) series of attributes.
13327 attributes attribute
13330 __attribute__ (( attribute-list [opt] ))
13332 The return value is as for cp_parser_attribute_list. */
13335 cp_parser_attributes_opt (cp_parser* parser)
13337 tree attributes = NULL_TREE;
13342 tree attribute_list;
13344 /* Peek at the next token. */
13345 token = cp_lexer_peek_token (parser->lexer);
13346 /* If it's not `__attribute__', then we're done. */
13347 if (token->keyword != RID_ATTRIBUTE)
13350 /* Consume the `__attribute__' keyword. */
13351 cp_lexer_consume_token (parser->lexer);
13352 /* Look for the two `(' tokens. */
13353 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13354 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13356 /* Peek at the next token. */
13357 token = cp_lexer_peek_token (parser->lexer);
13358 if (token->type != CPP_CLOSE_PAREN)
13359 /* Parse the attribute-list. */
13360 attribute_list = cp_parser_attribute_list (parser);
13362 /* If the next token is a `)', then there is no attribute
13364 attribute_list = NULL;
13366 /* Look for the two `)' tokens. */
13367 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13368 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13370 /* Add these new attributes to the list. */
13371 attributes = chainon (attributes, attribute_list);
13377 /* Parse an attribute-list.
13381 attribute-list , attribute
13385 identifier ( identifier )
13386 identifier ( identifier , expression-list )
13387 identifier ( expression-list )
13389 Returns a TREE_LIST. Each node corresponds to an attribute. THe
13390 TREE_PURPOSE of each node is the identifier indicating which
13391 attribute is in use. The TREE_VALUE represents the arguments, if
13395 cp_parser_attribute_list (cp_parser* parser)
13397 tree attribute_list = NULL_TREE;
13405 /* Look for the identifier. We also allow keywords here; for
13406 example `__attribute__ ((const))' is legal. */
13407 token = cp_lexer_peek_token (parser->lexer);
13408 if (token->type != CPP_NAME
13409 && token->type != CPP_KEYWORD)
13410 return error_mark_node;
13411 /* Consume the token. */
13412 token = cp_lexer_consume_token (parser->lexer);
13414 /* Save away the identifier that indicates which attribute this is. */
13415 identifier = token->value;
13416 attribute = build_tree_list (identifier, NULL_TREE);
13418 /* Peek at the next token. */
13419 token = cp_lexer_peek_token (parser->lexer);
13420 /* If it's an `(', then parse the attribute arguments. */
13421 if (token->type == CPP_OPEN_PAREN)
13425 arguments = (cp_parser_parenthesized_expression_list
13426 (parser, true, /*non_constant_p=*/NULL));
13427 /* Save the identifier and arguments away. */
13428 TREE_VALUE (attribute) = arguments;
13431 /* Add this attribute to the list. */
13432 TREE_CHAIN (attribute) = attribute_list;
13433 attribute_list = attribute;
13435 /* Now, look for more attributes. */
13436 token = cp_lexer_peek_token (parser->lexer);
13437 /* If the next token isn't a `,', we're done. */
13438 if (token->type != CPP_COMMA)
13441 /* Consume the comma and keep going. */
13442 cp_lexer_consume_token (parser->lexer);
13445 /* We built up the list in reverse order. */
13446 return nreverse (attribute_list);
13449 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
13450 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
13451 current value of the PEDANTIC flag, regardless of whether or not
13452 the `__extension__' keyword is present. The caller is responsible
13453 for restoring the value of the PEDANTIC flag. */
13456 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
13458 /* Save the old value of the PEDANTIC flag. */
13459 *saved_pedantic = pedantic;
13461 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
13463 /* Consume the `__extension__' token. */
13464 cp_lexer_consume_token (parser->lexer);
13465 /* We're not being pedantic while the `__extension__' keyword is
13475 /* Parse a label declaration.
13478 __label__ label-declarator-seq ;
13480 label-declarator-seq:
13481 identifier , label-declarator-seq
13485 cp_parser_label_declaration (cp_parser* parser)
13487 /* Look for the `__label__' keyword. */
13488 cp_parser_require_keyword (parser, RID_LABEL, "`__label__'");
13494 /* Look for an identifier. */
13495 identifier = cp_parser_identifier (parser);
13496 /* Declare it as a lobel. */
13497 finish_label_decl (identifier);
13498 /* If the next token is a `;', stop. */
13499 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13501 /* Look for the `,' separating the label declarations. */
13502 cp_parser_require (parser, CPP_COMMA, "`,'");
13505 /* Look for the final `;'. */
13506 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
13509 /* Support Functions */
13511 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
13512 NAME should have one of the representations used for an
13513 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
13514 is returned. If PARSER->SCOPE is a dependent type, then a
13515 SCOPE_REF is returned.
13517 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
13518 returned; the name was already resolved when the TEMPLATE_ID_EXPR
13519 was formed. Abstractly, such entities should not be passed to this
13520 function, because they do not need to be looked up, but it is
13521 simpler to check for this special case here, rather than at the
13524 In cases not explicitly covered above, this function returns a
13525 DECL, OVERLOAD, or baselink representing the result of the lookup.
13526 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
13529 If IS_TYPE is TRUE, bindings that do not refer to types are
13532 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
13535 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
13538 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
13542 cp_parser_lookup_name (cp_parser *parser, tree name,
13543 bool is_type, bool is_template, bool is_namespace,
13544 bool check_dependency)
13547 tree object_type = parser->context->object_type;
13549 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
13550 no longer valid. Note that if we are parsing tentatively, and
13551 the parse fails, OBJECT_TYPE will be automatically restored. */
13552 parser->context->object_type = NULL_TREE;
13554 if (name == error_mark_node)
13555 return error_mark_node;
13557 /* A template-id has already been resolved; there is no lookup to
13559 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
13561 if (BASELINK_P (name))
13563 my_friendly_assert ((TREE_CODE (BASELINK_FUNCTIONS (name))
13564 == TEMPLATE_ID_EXPR),
13569 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
13570 it should already have been checked to make sure that the name
13571 used matches the type being destroyed. */
13572 if (TREE_CODE (name) == BIT_NOT_EXPR)
13576 /* Figure out to which type this destructor applies. */
13578 type = parser->scope;
13579 else if (object_type)
13580 type = object_type;
13582 type = current_class_type;
13583 /* If that's not a class type, there is no destructor. */
13584 if (!type || !CLASS_TYPE_P (type))
13585 return error_mark_node;
13586 if (!CLASSTYPE_DESTRUCTORS (type))
13587 return error_mark_node;
13588 /* If it was a class type, return the destructor. */
13589 return CLASSTYPE_DESTRUCTORS (type);
13592 /* By this point, the NAME should be an ordinary identifier. If
13593 the id-expression was a qualified name, the qualifying scope is
13594 stored in PARSER->SCOPE at this point. */
13595 my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE,
13598 /* Perform the lookup. */
13603 if (parser->scope == error_mark_node)
13604 return error_mark_node;
13606 /* If the SCOPE is dependent, the lookup must be deferred until
13607 the template is instantiated -- unless we are explicitly
13608 looking up names in uninstantiated templates. Even then, we
13609 cannot look up the name if the scope is not a class type; it
13610 might, for example, be a template type parameter. */
13611 dependent_p = (TYPE_P (parser->scope)
13612 && !(parser->in_declarator_p
13613 && currently_open_class (parser->scope))
13614 && dependent_type_p (parser->scope));
13615 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
13619 /* The resolution to Core Issue 180 says that `struct A::B'
13620 should be considered a type-name, even if `A' is
13622 decl = TYPE_NAME (make_typename_type (parser->scope,
13625 else if (is_template)
13626 decl = make_unbound_class_template (parser->scope,
13630 decl = build_nt (SCOPE_REF, parser->scope, name);
13634 bool pop_p = false;
13636 /* If PARSER->SCOPE is a dependent type, then it must be a
13637 class type, and we must not be checking dependencies;
13638 otherwise, we would have processed this lookup above. So
13639 that PARSER->SCOPE is not considered a dependent base by
13640 lookup_member, we must enter the scope here. */
13642 pop_p = push_scope (parser->scope);
13643 /* If the PARSER->SCOPE is a a template specialization, it
13644 may be instantiated during name lookup. In that case,
13645 errors may be issued. Even if we rollback the current
13646 tentative parse, those errors are valid. */
13647 decl = lookup_qualified_name (parser->scope, name, is_type,
13648 /*complain=*/true);
13650 pop_scope (parser->scope);
13652 parser->qualifying_scope = parser->scope;
13653 parser->object_scope = NULL_TREE;
13655 else if (object_type)
13657 tree object_decl = NULL_TREE;
13658 /* Look up the name in the scope of the OBJECT_TYPE, unless the
13659 OBJECT_TYPE is not a class. */
13660 if (CLASS_TYPE_P (object_type))
13661 /* If the OBJECT_TYPE is a template specialization, it may
13662 be instantiated during name lookup. In that case, errors
13663 may be issued. Even if we rollback the current tentative
13664 parse, those errors are valid. */
13665 object_decl = lookup_member (object_type,
13667 /*protect=*/0, is_type);
13668 /* Look it up in the enclosing context, too. */
13669 decl = lookup_name_real (name, is_type, /*nonclass=*/0,
13672 parser->object_scope = object_type;
13673 parser->qualifying_scope = NULL_TREE;
13675 decl = object_decl;
13679 decl = lookup_name_real (name, is_type, /*nonclass=*/0,
13682 parser->qualifying_scope = NULL_TREE;
13683 parser->object_scope = NULL_TREE;
13686 /* If the lookup failed, let our caller know. */
13688 || decl == error_mark_node
13689 || (TREE_CODE (decl) == FUNCTION_DECL
13690 && DECL_ANTICIPATED (decl)))
13691 return error_mark_node;
13693 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
13694 if (TREE_CODE (decl) == TREE_LIST)
13696 /* The error message we have to print is too complicated for
13697 cp_parser_error, so we incorporate its actions directly. */
13698 if (!cp_parser_simulate_error (parser))
13700 error ("reference to `%D' is ambiguous", name);
13701 print_candidates (decl);
13703 return error_mark_node;
13706 my_friendly_assert (DECL_P (decl)
13707 || TREE_CODE (decl) == OVERLOAD
13708 || TREE_CODE (decl) == SCOPE_REF
13709 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
13710 || BASELINK_P (decl),
13713 /* If we have resolved the name of a member declaration, check to
13714 see if the declaration is accessible. When the name resolves to
13715 set of overloaded functions, accessibility is checked when
13716 overload resolution is done.
13718 During an explicit instantiation, access is not checked at all,
13719 as per [temp.explicit]. */
13721 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
13726 /* Like cp_parser_lookup_name, but for use in the typical case where
13727 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
13728 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
13731 cp_parser_lookup_name_simple (cp_parser* parser, tree name)
13733 return cp_parser_lookup_name (parser, name,
13735 /*is_template=*/false,
13736 /*is_namespace=*/false,
13737 /*check_dependency=*/true);
13740 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
13741 the current context, return the TYPE_DECL. If TAG_NAME_P is
13742 true, the DECL indicates the class being defined in a class-head,
13743 or declared in an elaborated-type-specifier.
13745 Otherwise, return DECL. */
13748 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
13750 /* If the TEMPLATE_DECL is being declared as part of a class-head,
13751 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
13754 template <typename T> struct B;
13757 template <typename T> struct A::B {};
13759 Similarly, in a elaborated-type-specifier:
13761 namespace N { struct X{}; }
13764 template <typename T> friend struct N::X;
13767 However, if the DECL refers to a class type, and we are in
13768 the scope of the class, then the name lookup automatically
13769 finds the TYPE_DECL created by build_self_reference rather
13770 than a TEMPLATE_DECL. For example, in:
13772 template <class T> struct S {
13776 there is no need to handle such case. */
13778 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
13779 return DECL_TEMPLATE_RESULT (decl);
13784 /* If too many, or too few, template-parameter lists apply to the
13785 declarator, issue an error message. Returns TRUE if all went well,
13786 and FALSE otherwise. */
13789 cp_parser_check_declarator_template_parameters (cp_parser* parser,
13792 unsigned num_templates;
13794 /* We haven't seen any classes that involve template parameters yet. */
13797 switch (TREE_CODE (declarator))
13804 tree main_declarator = TREE_OPERAND (declarator, 0);
13806 cp_parser_check_declarator_template_parameters (parser,
13815 scope = TREE_OPERAND (declarator, 0);
13816 member = TREE_OPERAND (declarator, 1);
13818 /* If this is a pointer-to-member, then we are not interested
13819 in the SCOPE, because it does not qualify the thing that is
13821 if (TREE_CODE (member) == INDIRECT_REF)
13822 return (cp_parser_check_declarator_template_parameters
13825 while (scope && CLASS_TYPE_P (scope))
13827 /* You're supposed to have one `template <...>'
13828 for every template class, but you don't need one
13829 for a full specialization. For example:
13831 template <class T> struct S{};
13832 template <> struct S<int> { void f(); };
13833 void S<int>::f () {}
13835 is correct; there shouldn't be a `template <>' for
13836 the definition of `S<int>::f'. */
13837 if (CLASSTYPE_TEMPLATE_INFO (scope)
13838 && (CLASSTYPE_TEMPLATE_INSTANTIATION (scope)
13839 || uses_template_parms (CLASSTYPE_TI_ARGS (scope)))
13840 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
13843 scope = TYPE_CONTEXT (scope);
13847 /* Fall through. */
13850 /* If the DECLARATOR has the form `X<y>' then it uses one
13851 additional level of template parameters. */
13852 if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
13855 return cp_parser_check_template_parameters (parser,
13860 /* NUM_TEMPLATES were used in the current declaration. If that is
13861 invalid, return FALSE and issue an error messages. Otherwise,
13865 cp_parser_check_template_parameters (cp_parser* parser,
13866 unsigned num_templates)
13868 /* If there are more template classes than parameter lists, we have
13871 template <class T> void S<T>::R<T>::f (); */
13872 if (parser->num_template_parameter_lists < num_templates)
13874 error ("too few template-parameter-lists");
13877 /* If there are the same number of template classes and parameter
13878 lists, that's OK. */
13879 if (parser->num_template_parameter_lists == num_templates)
13881 /* If there are more, but only one more, then we are referring to a
13882 member template. That's OK too. */
13883 if (parser->num_template_parameter_lists == num_templates + 1)
13885 /* Otherwise, there are too many template parameter lists. We have
13888 template <class T> template <class U> void S::f(); */
13889 error ("too many template-parameter-lists");
13893 /* Parse a binary-expression of the general form:
13897 binary-expression <token> <expr>
13899 The TOKEN_TREE_MAP maps <token> types to <expr> codes. FN is used
13900 to parser the <expr>s. If the first production is used, then the
13901 value returned by FN is returned directly. Otherwise, a node with
13902 the indicated EXPR_TYPE is returned, with operands corresponding to
13903 the two sub-expressions. */
13906 cp_parser_binary_expression (cp_parser* parser,
13907 const cp_parser_token_tree_map token_tree_map,
13908 cp_parser_expression_fn fn)
13912 /* Parse the first expression. */
13913 lhs = (*fn) (parser);
13914 /* Now, look for more expressions. */
13918 const cp_parser_token_tree_map_node *map_node;
13921 /* Peek at the next token. */
13922 token = cp_lexer_peek_token (parser->lexer);
13923 /* If the token is `>', and that's not an operator at the
13924 moment, then we're done. */
13925 if (token->type == CPP_GREATER
13926 && !parser->greater_than_is_operator_p)
13928 /* If we find one of the tokens we want, build the corresponding
13929 tree representation. */
13930 for (map_node = token_tree_map;
13931 map_node->token_type != CPP_EOF;
13933 if (map_node->token_type == token->type)
13935 /* Assume that an overloaded operator will not be used. */
13936 bool overloaded_p = false;
13938 /* Consume the operator token. */
13939 cp_lexer_consume_token (parser->lexer);
13940 /* Parse the right-hand side of the expression. */
13941 rhs = (*fn) (parser);
13942 /* Build the binary tree node. */
13943 lhs = build_x_binary_op (map_node->tree_type, lhs, rhs,
13945 /* If the binary operator required the use of an
13946 overloaded operator, then this expression cannot be an
13947 integral constant-expression. An overloaded operator
13948 can be used even if both operands are otherwise
13949 permissible in an integral constant-expression if at
13950 least one of the operands is of enumeration type. */
13952 && (cp_parser_non_integral_constant_expression
13953 (parser, "calls to overloaded operators")))
13954 lhs = error_mark_node;
13958 /* If the token wasn't one of the ones we want, we're done. */
13959 if (map_node->token_type == CPP_EOF)
13966 /* Parse an optional `::' token indicating that the following name is
13967 from the global namespace. If so, PARSER->SCOPE is set to the
13968 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
13969 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
13970 Returns the new value of PARSER->SCOPE, if the `::' token is
13971 present, and NULL_TREE otherwise. */
13974 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
13978 /* Peek at the next token. */
13979 token = cp_lexer_peek_token (parser->lexer);
13980 /* If we're looking at a `::' token then we're starting from the
13981 global namespace, not our current location. */
13982 if (token->type == CPP_SCOPE)
13984 /* Consume the `::' token. */
13985 cp_lexer_consume_token (parser->lexer);
13986 /* Set the SCOPE so that we know where to start the lookup. */
13987 parser->scope = global_namespace;
13988 parser->qualifying_scope = global_namespace;
13989 parser->object_scope = NULL_TREE;
13991 return parser->scope;
13993 else if (!current_scope_valid_p)
13995 parser->scope = NULL_TREE;
13996 parser->qualifying_scope = NULL_TREE;
13997 parser->object_scope = NULL_TREE;
14003 /* Returns TRUE if the upcoming token sequence is the start of a
14004 constructor declarator. If FRIEND_P is true, the declarator is
14005 preceded by the `friend' specifier. */
14008 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
14010 bool constructor_p;
14011 tree type_decl = NULL_TREE;
14012 bool nested_name_p;
14013 cp_token *next_token;
14015 /* The common case is that this is not a constructor declarator, so
14016 try to avoid doing lots of work if at all possible. It's not
14017 valid declare a constructor at function scope. */
14018 if (at_function_scope_p ())
14020 /* And only certain tokens can begin a constructor declarator. */
14021 next_token = cp_lexer_peek_token (parser->lexer);
14022 if (next_token->type != CPP_NAME
14023 && next_token->type != CPP_SCOPE
14024 && next_token->type != CPP_NESTED_NAME_SPECIFIER
14025 && next_token->type != CPP_TEMPLATE_ID)
14028 /* Parse tentatively; we are going to roll back all of the tokens
14030 cp_parser_parse_tentatively (parser);
14031 /* Assume that we are looking at a constructor declarator. */
14032 constructor_p = true;
14034 /* Look for the optional `::' operator. */
14035 cp_parser_global_scope_opt (parser,
14036 /*current_scope_valid_p=*/false);
14037 /* Look for the nested-name-specifier. */
14039 = (cp_parser_nested_name_specifier_opt (parser,
14040 /*typename_keyword_p=*/false,
14041 /*check_dependency_p=*/false,
14043 /*is_declaration=*/false)
14045 /* Outside of a class-specifier, there must be a
14046 nested-name-specifier. */
14047 if (!nested_name_p &&
14048 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
14050 constructor_p = false;
14051 /* If we still think that this might be a constructor-declarator,
14052 look for a class-name. */
14057 template <typename T> struct S { S(); };
14058 template <typename T> S<T>::S ();
14060 we must recognize that the nested `S' names a class.
14063 template <typename T> S<T>::S<T> ();
14065 we must recognize that the nested `S' names a template. */
14066 type_decl = cp_parser_class_name (parser,
14067 /*typename_keyword_p=*/false,
14068 /*template_keyword_p=*/false,
14070 /*check_dependency_p=*/false,
14071 /*class_head_p=*/false,
14072 /*is_declaration=*/false);
14073 /* If there was no class-name, then this is not a constructor. */
14074 constructor_p = !cp_parser_error_occurred (parser);
14077 /* If we're still considering a constructor, we have to see a `(',
14078 to begin the parameter-declaration-clause, followed by either a
14079 `)', an `...', or a decl-specifier. We need to check for a
14080 type-specifier to avoid being fooled into thinking that:
14084 is a constructor. (It is actually a function named `f' that
14085 takes one parameter (of type `int') and returns a value of type
14088 && cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
14090 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
14091 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
14092 /* A parameter declaration begins with a decl-specifier,
14093 which is either the "attribute" keyword, a storage class
14094 specifier, or (usually) a type-specifier. */
14095 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE)
14096 && !cp_parser_storage_class_specifier_opt (parser))
14099 bool pop_p = false;
14100 unsigned saved_num_template_parameter_lists;
14102 /* Names appearing in the type-specifier should be looked up
14103 in the scope of the class. */
14104 if (current_class_type)
14108 type = TREE_TYPE (type_decl);
14109 if (TREE_CODE (type) == TYPENAME_TYPE)
14111 type = resolve_typename_type (type,
14112 /*only_current_p=*/false);
14113 if (type == error_mark_node)
14115 cp_parser_abort_tentative_parse (parser);
14119 pop_p = push_scope (type);
14122 /* Inside the constructor parameter list, surrounding
14123 template-parameter-lists do not apply. */
14124 saved_num_template_parameter_lists
14125 = parser->num_template_parameter_lists;
14126 parser->num_template_parameter_lists = 0;
14128 /* Look for the type-specifier. */
14129 cp_parser_type_specifier (parser,
14130 CP_PARSER_FLAGS_NONE,
14131 /*is_friend=*/false,
14132 /*is_declarator=*/true,
14133 /*declares_class_or_enum=*/NULL,
14134 /*is_cv_qualifier=*/NULL);
14136 parser->num_template_parameter_lists
14137 = saved_num_template_parameter_lists;
14139 /* Leave the scope of the class. */
14143 constructor_p = !cp_parser_error_occurred (parser);
14147 constructor_p = false;
14148 /* We did not really want to consume any tokens. */
14149 cp_parser_abort_tentative_parse (parser);
14151 return constructor_p;
14154 /* Parse the definition of the function given by the DECL_SPECIFIERS,
14155 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
14156 they must be performed once we are in the scope of the function.
14158 Returns the function defined. */
14161 cp_parser_function_definition_from_specifiers_and_declarator
14162 (cp_parser* parser,
14163 tree decl_specifiers,
14170 /* Begin the function-definition. */
14171 success_p = begin_function_definition (decl_specifiers,
14175 /* If there were names looked up in the decl-specifier-seq that we
14176 did not check, check them now. We must wait until we are in the
14177 scope of the function to perform the checks, since the function
14178 might be a friend. */
14179 perform_deferred_access_checks ();
14183 /* If begin_function_definition didn't like the definition, skip
14184 the entire function. */
14185 error ("invalid function declaration");
14186 cp_parser_skip_to_end_of_block_or_statement (parser);
14187 fn = error_mark_node;
14190 fn = cp_parser_function_definition_after_declarator (parser,
14191 /*inline_p=*/false);
14196 /* Parse the part of a function-definition that follows the
14197 declarator. INLINE_P is TRUE iff this function is an inline
14198 function defined with a class-specifier.
14200 Returns the function defined. */
14203 cp_parser_function_definition_after_declarator (cp_parser* parser,
14207 bool ctor_initializer_p = false;
14208 bool saved_in_unbraced_linkage_specification_p;
14209 unsigned saved_num_template_parameter_lists;
14211 /* If the next token is `return', then the code may be trying to
14212 make use of the "named return value" extension that G++ used to
14214 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
14216 /* Consume the `return' keyword. */
14217 cp_lexer_consume_token (parser->lexer);
14218 /* Look for the identifier that indicates what value is to be
14220 cp_parser_identifier (parser);
14221 /* Issue an error message. */
14222 error ("named return values are no longer supported");
14223 /* Skip tokens until we reach the start of the function body. */
14224 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
14225 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
14226 cp_lexer_consume_token (parser->lexer);
14228 /* The `extern' in `extern "C" void f () { ... }' does not apply to
14229 anything declared inside `f'. */
14230 saved_in_unbraced_linkage_specification_p
14231 = parser->in_unbraced_linkage_specification_p;
14232 parser->in_unbraced_linkage_specification_p = false;
14233 /* Inside the function, surrounding template-parameter-lists do not
14235 saved_num_template_parameter_lists
14236 = parser->num_template_parameter_lists;
14237 parser->num_template_parameter_lists = 0;
14238 /* If the next token is `try', then we are looking at a
14239 function-try-block. */
14240 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
14241 ctor_initializer_p = cp_parser_function_try_block (parser);
14242 /* A function-try-block includes the function-body, so we only do
14243 this next part if we're not processing a function-try-block. */
14246 = cp_parser_ctor_initializer_opt_and_function_body (parser);
14248 /* Finish the function. */
14249 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
14250 (inline_p ? 2 : 0));
14251 /* Generate code for it, if necessary. */
14252 expand_or_defer_fn (fn);
14253 /* Restore the saved values. */
14254 parser->in_unbraced_linkage_specification_p
14255 = saved_in_unbraced_linkage_specification_p;
14256 parser->num_template_parameter_lists
14257 = saved_num_template_parameter_lists;
14262 /* Parse a template-declaration, assuming that the `export' (and
14263 `extern') keywords, if present, has already been scanned. MEMBER_P
14264 is as for cp_parser_template_declaration. */
14267 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
14269 tree decl = NULL_TREE;
14270 tree parameter_list;
14271 bool friend_p = false;
14273 /* Look for the `template' keyword. */
14274 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'"))
14278 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
14281 /* If the next token is `>', then we have an invalid
14282 specialization. Rather than complain about an invalid template
14283 parameter, issue an error message here. */
14284 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
14286 cp_parser_error (parser, "invalid explicit specialization");
14287 begin_specialization ();
14288 parameter_list = NULL_TREE;
14292 /* Parse the template parameters. */
14293 begin_template_parm_list ();
14294 parameter_list = cp_parser_template_parameter_list (parser);
14295 parameter_list = end_template_parm_list (parameter_list);
14298 /* Look for the `>'. */
14299 cp_parser_skip_until_found (parser, CPP_GREATER, "`>'");
14300 /* We just processed one more parameter list. */
14301 ++parser->num_template_parameter_lists;
14302 /* If the next token is `template', there are more template
14304 if (cp_lexer_next_token_is_keyword (parser->lexer,
14306 cp_parser_template_declaration_after_export (parser, member_p);
14309 decl = cp_parser_single_declaration (parser,
14313 /* If this is a member template declaration, let the front
14315 if (member_p && !friend_p && decl)
14317 if (TREE_CODE (decl) == TYPE_DECL)
14318 cp_parser_check_access_in_redeclaration (decl);
14320 decl = finish_member_template_decl (decl);
14322 else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
14323 make_friend_class (current_class_type, TREE_TYPE (decl),
14324 /*complain=*/true);
14326 /* We are done with the current parameter list. */
14327 --parser->num_template_parameter_lists;
14330 finish_template_decl (parameter_list);
14332 /* Register member declarations. */
14333 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
14334 finish_member_declaration (decl);
14336 /* If DECL is a function template, we must return to parse it later.
14337 (Even though there is no definition, there might be default
14338 arguments that need handling.) */
14339 if (member_p && decl
14340 && (TREE_CODE (decl) == FUNCTION_DECL
14341 || DECL_FUNCTION_TEMPLATE_P (decl)))
14342 TREE_VALUE (parser->unparsed_functions_queues)
14343 = tree_cons (NULL_TREE, decl,
14344 TREE_VALUE (parser->unparsed_functions_queues));
14347 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
14348 `function-definition' sequence. MEMBER_P is true, this declaration
14349 appears in a class scope.
14351 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
14352 *FRIEND_P is set to TRUE iff the declaration is a friend. */
14355 cp_parser_single_declaration (cp_parser* parser,
14359 int declares_class_or_enum;
14360 tree decl = NULL_TREE;
14361 tree decl_specifiers;
14363 bool function_definition_p = false;
14365 /* Defer access checks until we know what is being declared. */
14366 push_deferring_access_checks (dk_deferred);
14368 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
14371 = cp_parser_decl_specifier_seq (parser,
14372 CP_PARSER_FLAGS_OPTIONAL,
14374 &declares_class_or_enum);
14376 *friend_p = cp_parser_friend_p (decl_specifiers);
14377 /* Gather up the access checks that occurred the
14378 decl-specifier-seq. */
14379 stop_deferring_access_checks ();
14381 /* Check for the declaration of a template class. */
14382 if (declares_class_or_enum)
14384 if (cp_parser_declares_only_class_p (parser))
14386 decl = shadow_tag (decl_specifiers);
14388 decl = TYPE_NAME (decl);
14390 decl = error_mark_node;
14395 /* If it's not a template class, try for a template function. If
14396 the next token is a `;', then this declaration does not declare
14397 anything. But, if there were errors in the decl-specifiers, then
14398 the error might well have come from an attempted class-specifier.
14399 In that case, there's no need to warn about a missing declarator. */
14401 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
14402 || !value_member (error_mark_node, decl_specifiers)))
14403 decl = cp_parser_init_declarator (parser,
14406 /*function_definition_allowed_p=*/true,
14408 declares_class_or_enum,
14409 &function_definition_p);
14411 pop_deferring_access_checks ();
14413 /* Clear any current qualification; whatever comes next is the start
14414 of something new. */
14415 parser->scope = NULL_TREE;
14416 parser->qualifying_scope = NULL_TREE;
14417 parser->object_scope = NULL_TREE;
14418 /* Look for a trailing `;' after the declaration. */
14419 if (!function_definition_p
14420 && !cp_parser_require (parser, CPP_SEMICOLON, "`;'"))
14421 cp_parser_skip_to_end_of_block_or_statement (parser);
14426 /* Parse a cast-expression that is not the operand of a unary "&". */
14429 cp_parser_simple_cast_expression (cp_parser *parser)
14431 return cp_parser_cast_expression (parser, /*address_p=*/false);
14434 /* Parse a functional cast to TYPE. Returns an expression
14435 representing the cast. */
14438 cp_parser_functional_cast (cp_parser* parser, tree type)
14440 tree expression_list;
14444 = cp_parser_parenthesized_expression_list (parser, false,
14445 /*non_constant_p=*/NULL);
14447 cast = build_functional_cast (type, expression_list);
14448 /* [expr.const]/1: In an integral constant expression "only type
14449 conversions to integral or enumeration type can be used". */
14450 if (cast != error_mark_node && !type_dependent_expression_p (type)
14451 && !INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (type)))
14453 if (cp_parser_non_integral_constant_expression
14454 (parser, "a call to a constructor"))
14455 return error_mark_node;
14460 /* Save the tokens that make up the body of a member function defined
14461 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
14462 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
14463 specifiers applied to the declaration. Returns the FUNCTION_DECL
14464 for the member function. */
14467 cp_parser_save_member_function_body (cp_parser* parser,
14468 tree decl_specifiers,
14472 cp_token_cache *cache;
14475 /* Create the function-declaration. */
14476 fn = start_method (decl_specifiers, declarator, attributes);
14477 /* If something went badly wrong, bail out now. */
14478 if (fn == error_mark_node)
14480 /* If there's a function-body, skip it. */
14481 if (cp_parser_token_starts_function_definition_p
14482 (cp_lexer_peek_token (parser->lexer)))
14483 cp_parser_skip_to_end_of_block_or_statement (parser);
14484 return error_mark_node;
14487 /* Remember it, if there default args to post process. */
14488 cp_parser_save_default_args (parser, fn);
14490 /* Create a token cache. */
14491 cache = cp_token_cache_new ();
14492 /* Save away the tokens that make up the body of the
14494 cp_parser_cache_group (parser, cache, CPP_CLOSE_BRACE, /*depth=*/0);
14495 /* Handle function try blocks. */
14496 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
14497 cp_parser_cache_group (parser, cache, CPP_CLOSE_BRACE, /*depth=*/0);
14499 /* Save away the inline definition; we will process it when the
14500 class is complete. */
14501 DECL_PENDING_INLINE_INFO (fn) = cache;
14502 DECL_PENDING_INLINE_P (fn) = 1;
14504 /* We need to know that this was defined in the class, so that
14505 friend templates are handled correctly. */
14506 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
14508 /* We're done with the inline definition. */
14509 finish_method (fn);
14511 /* Add FN to the queue of functions to be parsed later. */
14512 TREE_VALUE (parser->unparsed_functions_queues)
14513 = tree_cons (NULL_TREE, fn,
14514 TREE_VALUE (parser->unparsed_functions_queues));
14519 /* Parse a template-argument-list, as well as the trailing ">" (but
14520 not the opening ">"). See cp_parser_template_argument_list for the
14524 cp_parser_enclosed_template_argument_list (cp_parser* parser)
14528 tree saved_qualifying_scope;
14529 tree saved_object_scope;
14530 bool saved_greater_than_is_operator_p;
14534 When parsing a template-id, the first non-nested `>' is taken as
14535 the end of the template-argument-list rather than a greater-than
14537 saved_greater_than_is_operator_p
14538 = parser->greater_than_is_operator_p;
14539 parser->greater_than_is_operator_p = false;
14540 /* Parsing the argument list may modify SCOPE, so we save it
14542 saved_scope = parser->scope;
14543 saved_qualifying_scope = parser->qualifying_scope;
14544 saved_object_scope = parser->object_scope;
14545 /* Parse the template-argument-list itself. */
14546 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
14547 arguments = NULL_TREE;
14549 arguments = cp_parser_template_argument_list (parser);
14550 /* Look for the `>' that ends the template-argument-list. If we find
14551 a '>>' instead, it's probably just a typo. */
14552 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
14554 if (!saved_greater_than_is_operator_p)
14556 /* If we're in a nested template argument list, the '>>' has to be
14557 a typo for '> >'. We emit the error message, but we continue
14558 parsing and we push a '>' as next token, so that the argument
14559 list will be parsed correctly.. */
14561 error ("`>>' should be `> >' within a nested template argument list");
14562 token = cp_lexer_peek_token (parser->lexer);
14563 token->type = CPP_GREATER;
14567 /* If this is not a nested template argument list, the '>>' is
14568 a typo for '>'. Emit an error message and continue. */
14569 error ("spurious `>>', use `>' to terminate a template argument list");
14570 cp_lexer_consume_token (parser->lexer);
14573 else if (!cp_parser_require (parser, CPP_GREATER, "`>'"))
14574 error ("missing `>' to terminate the template argument list");
14575 /* The `>' token might be a greater-than operator again now. */
14576 parser->greater_than_is_operator_p
14577 = saved_greater_than_is_operator_p;
14578 /* Restore the SAVED_SCOPE. */
14579 parser->scope = saved_scope;
14580 parser->qualifying_scope = saved_qualifying_scope;
14581 parser->object_scope = saved_object_scope;
14586 /* MEMBER_FUNCTION is a member function, or a friend. If default
14587 arguments, or the body of the function have not yet been parsed,
14591 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
14593 cp_lexer *saved_lexer;
14595 /* If this member is a template, get the underlying
14597 if (DECL_FUNCTION_TEMPLATE_P (member_function))
14598 member_function = DECL_TEMPLATE_RESULT (member_function);
14600 /* There should not be any class definitions in progress at this
14601 point; the bodies of members are only parsed outside of all class
14603 my_friendly_assert (parser->num_classes_being_defined == 0, 20010816);
14604 /* While we're parsing the member functions we might encounter more
14605 classes. We want to handle them right away, but we don't want
14606 them getting mixed up with functions that are currently in the
14608 parser->unparsed_functions_queues
14609 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
14611 /* Make sure that any template parameters are in scope. */
14612 maybe_begin_member_template_processing (member_function);
14614 /* If the body of the function has not yet been parsed, parse it
14616 if (DECL_PENDING_INLINE_P (member_function))
14618 tree function_scope;
14619 cp_token_cache *tokens;
14621 /* The function is no longer pending; we are processing it. */
14622 tokens = DECL_PENDING_INLINE_INFO (member_function);
14623 DECL_PENDING_INLINE_INFO (member_function) = NULL;
14624 DECL_PENDING_INLINE_P (member_function) = 0;
14625 /* If this was an inline function in a local class, enter the scope
14626 of the containing function. */
14627 function_scope = decl_function_context (member_function);
14628 if (function_scope)
14629 push_function_context_to (function_scope);
14631 /* Save away the current lexer. */
14632 saved_lexer = parser->lexer;
14633 /* Make a new lexer to feed us the tokens saved for this function. */
14634 parser->lexer = cp_lexer_new_from_tokens (tokens);
14635 parser->lexer->next = saved_lexer;
14637 /* Set the current source position to be the location of the first
14638 token in the saved inline body. */
14639 cp_lexer_peek_token (parser->lexer);
14641 /* Let the front end know that we going to be defining this
14643 start_function (NULL_TREE, member_function, NULL_TREE,
14644 SF_PRE_PARSED | SF_INCLASS_INLINE);
14646 /* Now, parse the body of the function. */
14647 cp_parser_function_definition_after_declarator (parser,
14648 /*inline_p=*/true);
14650 /* Leave the scope of the containing function. */
14651 if (function_scope)
14652 pop_function_context_from (function_scope);
14653 /* Restore the lexer. */
14654 parser->lexer = saved_lexer;
14657 /* Remove any template parameters from the symbol table. */
14658 maybe_end_member_template_processing ();
14660 /* Restore the queue. */
14661 parser->unparsed_functions_queues
14662 = TREE_CHAIN (parser->unparsed_functions_queues);
14665 /* If DECL contains any default args, remember it on the unparsed
14666 functions queue. */
14669 cp_parser_save_default_args (cp_parser* parser, tree decl)
14673 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
14675 probe = TREE_CHAIN (probe))
14676 if (TREE_PURPOSE (probe))
14678 TREE_PURPOSE (parser->unparsed_functions_queues)
14679 = tree_cons (NULL_TREE, decl,
14680 TREE_PURPOSE (parser->unparsed_functions_queues));
14686 /* FN is a FUNCTION_DECL which may contains a parameter with an
14687 unparsed DEFAULT_ARG. Parse the default args now. */
14690 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
14692 cp_lexer *saved_lexer;
14693 cp_token_cache *tokens;
14694 bool saved_local_variables_forbidden_p;
14697 /* While we're parsing the default args, we might (due to the
14698 statement expression extension) encounter more classes. We want
14699 to handle them right away, but we don't want them getting mixed
14700 up with default args that are currently in the queue. */
14701 parser->unparsed_functions_queues
14702 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
14704 for (parameters = TYPE_ARG_TYPES (TREE_TYPE (fn));
14706 parameters = TREE_CHAIN (parameters))
14708 if (!TREE_PURPOSE (parameters)
14709 || TREE_CODE (TREE_PURPOSE (parameters)) != DEFAULT_ARG)
14712 /* Save away the current lexer. */
14713 saved_lexer = parser->lexer;
14714 /* Create a new one, using the tokens we have saved. */
14715 tokens = DEFARG_TOKENS (TREE_PURPOSE (parameters));
14716 parser->lexer = cp_lexer_new_from_tokens (tokens);
14718 /* Set the current source position to be the location of the
14719 first token in the default argument. */
14720 cp_lexer_peek_token (parser->lexer);
14722 /* Local variable names (and the `this' keyword) may not appear
14723 in a default argument. */
14724 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
14725 parser->local_variables_forbidden_p = true;
14726 /* Parse the assignment-expression. */
14727 if (DECL_CLASS_SCOPE_P (fn))
14728 push_nested_class (DECL_CONTEXT (fn));
14729 TREE_PURPOSE (parameters) = cp_parser_assignment_expression (parser);
14730 if (DECL_CLASS_SCOPE_P (fn))
14731 pop_nested_class ();
14733 /* If the token stream has not been completely used up, then
14734 there was extra junk after the end of the default
14736 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
14737 cp_parser_error (parser, "expected `,'");
14739 /* Restore saved state. */
14740 parser->lexer = saved_lexer;
14741 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
14744 /* Restore the queue. */
14745 parser->unparsed_functions_queues
14746 = TREE_CHAIN (parser->unparsed_functions_queues);
14749 /* Parse the operand of `sizeof' (or a similar operator). Returns
14750 either a TYPE or an expression, depending on the form of the
14751 input. The KEYWORD indicates which kind of expression we have
14755 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
14757 static const char *format;
14758 tree expr = NULL_TREE;
14759 const char *saved_message;
14760 bool saved_integral_constant_expression_p;
14762 /* Initialize FORMAT the first time we get here. */
14764 format = "types may not be defined in `%s' expressions";
14766 /* Types cannot be defined in a `sizeof' expression. Save away the
14768 saved_message = parser->type_definition_forbidden_message;
14769 /* And create the new one. */
14770 parser->type_definition_forbidden_message
14771 = xmalloc (strlen (format)
14772 + strlen (IDENTIFIER_POINTER (ridpointers[keyword]))
14774 sprintf ((char *) parser->type_definition_forbidden_message,
14775 format, IDENTIFIER_POINTER (ridpointers[keyword]));
14777 /* The restrictions on constant-expressions do not apply inside
14778 sizeof expressions. */
14779 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
14780 parser->integral_constant_expression_p = false;
14782 /* Do not actually evaluate the expression. */
14784 /* If it's a `(', then we might be looking at the type-id
14786 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
14789 bool saved_in_type_id_in_expr_p;
14791 /* We can't be sure yet whether we're looking at a type-id or an
14793 cp_parser_parse_tentatively (parser);
14794 /* Consume the `('. */
14795 cp_lexer_consume_token (parser->lexer);
14796 /* Parse the type-id. */
14797 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
14798 parser->in_type_id_in_expr_p = true;
14799 type = cp_parser_type_id (parser);
14800 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
14801 /* Now, look for the trailing `)'. */
14802 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14803 /* If all went well, then we're done. */
14804 if (cp_parser_parse_definitely (parser))
14806 /* Build a list of decl-specifiers; right now, we have only
14807 a single type-specifier. */
14808 type = build_tree_list (NULL_TREE,
14811 /* Call grokdeclarator to figure out what type this is. */
14812 expr = grokdeclarator (NULL_TREE,
14816 /*attrlist=*/NULL);
14820 /* If the type-id production did not work out, then we must be
14821 looking at the unary-expression production. */
14823 expr = cp_parser_unary_expression (parser, /*address_p=*/false);
14824 /* Go back to evaluating expressions. */
14827 /* Free the message we created. */
14828 free ((char *) parser->type_definition_forbidden_message);
14829 /* And restore the old one. */
14830 parser->type_definition_forbidden_message = saved_message;
14831 parser->integral_constant_expression_p = saved_integral_constant_expression_p;
14836 /* If the current declaration has no declarator, return true. */
14839 cp_parser_declares_only_class_p (cp_parser *parser)
14841 /* If the next token is a `;' or a `,' then there is no
14843 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
14844 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
14847 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
14848 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
14851 cp_parser_friend_p (tree decl_specifiers)
14853 while (decl_specifiers)
14855 /* See if this decl-specifier is `friend'. */
14856 if (TREE_CODE (TREE_VALUE (decl_specifiers)) == IDENTIFIER_NODE
14857 && C_RID_CODE (TREE_VALUE (decl_specifiers)) == RID_FRIEND)
14860 /* Go on to the next decl-specifier. */
14861 decl_specifiers = TREE_CHAIN (decl_specifiers);
14867 /* If the next token is of the indicated TYPE, consume it. Otherwise,
14868 issue an error message indicating that TOKEN_DESC was expected.
14870 Returns the token consumed, if the token had the appropriate type.
14871 Otherwise, returns NULL. */
14874 cp_parser_require (cp_parser* parser,
14875 enum cpp_ttype type,
14876 const char* token_desc)
14878 if (cp_lexer_next_token_is (parser->lexer, type))
14879 return cp_lexer_consume_token (parser->lexer);
14882 /* Output the MESSAGE -- unless we're parsing tentatively. */
14883 if (!cp_parser_simulate_error (parser))
14885 char *message = concat ("expected ", token_desc, NULL);
14886 cp_parser_error (parser, message);
14893 /* Like cp_parser_require, except that tokens will be skipped until
14894 the desired token is found. An error message is still produced if
14895 the next token is not as expected. */
14898 cp_parser_skip_until_found (cp_parser* parser,
14899 enum cpp_ttype type,
14900 const char* token_desc)
14903 unsigned nesting_depth = 0;
14905 if (cp_parser_require (parser, type, token_desc))
14908 /* Skip tokens until the desired token is found. */
14911 /* Peek at the next token. */
14912 token = cp_lexer_peek_token (parser->lexer);
14913 /* If we've reached the token we want, consume it and
14915 if (token->type == type && !nesting_depth)
14917 cp_lexer_consume_token (parser->lexer);
14920 /* If we've run out of tokens, stop. */
14921 if (token->type == CPP_EOF)
14923 if (token->type == CPP_OPEN_BRACE
14924 || token->type == CPP_OPEN_PAREN
14925 || token->type == CPP_OPEN_SQUARE)
14927 else if (token->type == CPP_CLOSE_BRACE
14928 || token->type == CPP_CLOSE_PAREN
14929 || token->type == CPP_CLOSE_SQUARE)
14931 if (nesting_depth-- == 0)
14934 /* Consume this token. */
14935 cp_lexer_consume_token (parser->lexer);
14939 /* If the next token is the indicated keyword, consume it. Otherwise,
14940 issue an error message indicating that TOKEN_DESC was expected.
14942 Returns the token consumed, if the token had the appropriate type.
14943 Otherwise, returns NULL. */
14946 cp_parser_require_keyword (cp_parser* parser,
14948 const char* token_desc)
14950 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
14952 if (token && token->keyword != keyword)
14954 dyn_string_t error_msg;
14956 /* Format the error message. */
14957 error_msg = dyn_string_new (0);
14958 dyn_string_append_cstr (error_msg, "expected ");
14959 dyn_string_append_cstr (error_msg, token_desc);
14960 cp_parser_error (parser, error_msg->s);
14961 dyn_string_delete (error_msg);
14968 /* Returns TRUE iff TOKEN is a token that can begin the body of a
14969 function-definition. */
14972 cp_parser_token_starts_function_definition_p (cp_token* token)
14974 return (/* An ordinary function-body begins with an `{'. */
14975 token->type == CPP_OPEN_BRACE
14976 /* A ctor-initializer begins with a `:'. */
14977 || token->type == CPP_COLON
14978 /* A function-try-block begins with `try'. */
14979 || token->keyword == RID_TRY
14980 /* The named return value extension begins with `return'. */
14981 || token->keyword == RID_RETURN);
14984 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
14988 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
14992 token = cp_lexer_peek_token (parser->lexer);
14993 return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
14996 /* Returns TRUE iff the next token is the "," or ">" ending a
14997 template-argument. ">>" is also accepted (after the full
14998 argument was parsed) because it's probably a typo for "> >",
14999 and there is a specific diagnostic for this. */
15002 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
15006 token = cp_lexer_peek_token (parser->lexer);
15007 return (token->type == CPP_COMMA || token->type == CPP_GREATER
15008 || token->type == CPP_RSHIFT);
15011 /* Returns TRUE iff the n-th token is a ">", or the n-th is a "[" and the
15012 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
15015 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
15020 token = cp_lexer_peek_nth_token (parser->lexer, n);
15021 if (token->type == CPP_LESS)
15023 /* Check for the sequence `<::' in the original code. It would be lexed as
15024 `[:', where `[' is a digraph, and there is no whitespace before
15026 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
15029 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
15030 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
15036 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
15037 or none_type otherwise. */
15039 static enum tag_types
15040 cp_parser_token_is_class_key (cp_token* token)
15042 switch (token->keyword)
15047 return record_type;
15056 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
15059 cp_parser_check_class_key (enum tag_types class_key, tree type)
15061 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
15062 pedwarn ("`%s' tag used in naming `%#T'",
15063 class_key == union_type ? "union"
15064 : class_key == record_type ? "struct" : "class",
15068 /* Issue an error message if DECL is redeclared with different
15069 access than its original declaration [class.access.spec/3].
15070 This applies to nested classes and nested class templates.
15073 static void cp_parser_check_access_in_redeclaration (tree decl)
15075 if (!CLASS_TYPE_P (TREE_TYPE (decl)))
15078 if ((TREE_PRIVATE (decl)
15079 != (current_access_specifier == access_private_node))
15080 || (TREE_PROTECTED (decl)
15081 != (current_access_specifier == access_protected_node)))
15082 error ("%D redeclared with different access", decl);
15085 /* Look for the `template' keyword, as a syntactic disambiguator.
15086 Return TRUE iff it is present, in which case it will be
15090 cp_parser_optional_template_keyword (cp_parser *parser)
15092 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
15094 /* The `template' keyword can only be used within templates;
15095 outside templates the parser can always figure out what is a
15096 template and what is not. */
15097 if (!processing_template_decl)
15099 error ("`template' (as a disambiguator) is only allowed "
15100 "within templates");
15101 /* If this part of the token stream is rescanned, the same
15102 error message would be generated. So, we purge the token
15103 from the stream. */
15104 cp_lexer_purge_token (parser->lexer);
15109 /* Consume the `template' keyword. */
15110 cp_lexer_consume_token (parser->lexer);
15118 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
15119 set PARSER->SCOPE, and perform other related actions. */
15122 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
15127 /* Get the stored value. */
15128 value = cp_lexer_consume_token (parser->lexer)->value;
15129 /* Perform any access checks that were deferred. */
15130 for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
15131 perform_or_defer_access_check (TREE_PURPOSE (check), TREE_VALUE (check));
15132 /* Set the scope from the stored value. */
15133 parser->scope = TREE_VALUE (value);
15134 parser->qualifying_scope = TREE_TYPE (value);
15135 parser->object_scope = NULL_TREE;
15138 /* Add tokens to CACHE until an non-nested END token appears. */
15141 cp_parser_cache_group (cp_parser *parser,
15142 cp_token_cache *cache,
15143 enum cpp_ttype end,
15150 /* Abort a parenthesized expression if we encounter a brace. */
15151 if ((end == CPP_CLOSE_PAREN || depth == 0)
15152 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
15154 /* If we've reached the end of the file, stop. */
15155 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
15157 /* Consume the next token. */
15158 token = cp_lexer_consume_token (parser->lexer);
15159 /* Add this token to the tokens we are saving. */
15160 cp_token_cache_push_token (cache, token);
15161 /* See if it starts a new group. */
15162 if (token->type == CPP_OPEN_BRACE)
15164 cp_parser_cache_group (parser, cache, CPP_CLOSE_BRACE, depth + 1);
15168 else if (token->type == CPP_OPEN_PAREN)
15169 cp_parser_cache_group (parser, cache, CPP_CLOSE_PAREN, depth + 1);
15170 else if (token->type == end)
15175 /* Begin parsing tentatively. We always save tokens while parsing
15176 tentatively so that if the tentative parsing fails we can restore the
15180 cp_parser_parse_tentatively (cp_parser* parser)
15182 /* Enter a new parsing context. */
15183 parser->context = cp_parser_context_new (parser->context);
15184 /* Begin saving tokens. */
15185 cp_lexer_save_tokens (parser->lexer);
15186 /* In order to avoid repetitive access control error messages,
15187 access checks are queued up until we are no longer parsing
15189 push_deferring_access_checks (dk_deferred);
15192 /* Commit to the currently active tentative parse. */
15195 cp_parser_commit_to_tentative_parse (cp_parser* parser)
15197 cp_parser_context *context;
15200 /* Mark all of the levels as committed. */
15201 lexer = parser->lexer;
15202 for (context = parser->context; context->next; context = context->next)
15204 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
15206 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
15207 while (!cp_lexer_saving_tokens (lexer))
15208 lexer = lexer->next;
15209 cp_lexer_commit_tokens (lexer);
15213 /* Abort the currently active tentative parse. All consumed tokens
15214 will be rolled back, and no diagnostics will be issued. */
15217 cp_parser_abort_tentative_parse (cp_parser* parser)
15219 cp_parser_simulate_error (parser);
15220 /* Now, pretend that we want to see if the construct was
15221 successfully parsed. */
15222 cp_parser_parse_definitely (parser);
15225 /* Stop parsing tentatively. If a parse error has occurred, restore the
15226 token stream. Otherwise, commit to the tokens we have consumed.
15227 Returns true if no error occurred; false otherwise. */
15230 cp_parser_parse_definitely (cp_parser* parser)
15232 bool error_occurred;
15233 cp_parser_context *context;
15235 /* Remember whether or not an error occurred, since we are about to
15236 destroy that information. */
15237 error_occurred = cp_parser_error_occurred (parser);
15238 /* Remove the topmost context from the stack. */
15239 context = parser->context;
15240 parser->context = context->next;
15241 /* If no parse errors occurred, commit to the tentative parse. */
15242 if (!error_occurred)
15244 /* Commit to the tokens read tentatively, unless that was
15246 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
15247 cp_lexer_commit_tokens (parser->lexer);
15249 pop_to_parent_deferring_access_checks ();
15251 /* Otherwise, if errors occurred, roll back our state so that things
15252 are just as they were before we began the tentative parse. */
15255 cp_lexer_rollback_tokens (parser->lexer);
15256 pop_deferring_access_checks ();
15258 /* Add the context to the front of the free list. */
15259 context->next = cp_parser_context_free_list;
15260 cp_parser_context_free_list = context;
15262 return !error_occurred;
15265 /* Returns true if we are parsing tentatively -- but have decided that
15266 we will stick with this tentative parse, even if errors occur. */
15269 cp_parser_committed_to_tentative_parse (cp_parser* parser)
15271 return (cp_parser_parsing_tentatively (parser)
15272 && parser->context->status == CP_PARSER_STATUS_KIND_COMMITTED);
15275 /* Returns nonzero iff an error has occurred during the most recent
15276 tentative parse. */
15279 cp_parser_error_occurred (cp_parser* parser)
15281 return (cp_parser_parsing_tentatively (parser)
15282 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
15285 /* Returns nonzero if GNU extensions are allowed. */
15288 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
15290 return parser->allow_gnu_extensions_p;
15297 static GTY (()) cp_parser *the_parser;
15299 /* External interface. */
15301 /* Parse one entire translation unit. */
15304 c_parse_file (void)
15306 bool error_occurred;
15308 the_parser = cp_parser_new ();
15309 push_deferring_access_checks (flag_access_control
15310 ? dk_no_deferred : dk_no_check);
15311 error_occurred = cp_parser_translation_unit (the_parser);
15315 /* This variable must be provided by every front end. */
15319 #include "gt-cp-parser.h"