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 *, bool);
1487 static tree cp_parser_direct_declarator
1488 (cp_parser *, cp_parser_declarator_kind, int *, bool);
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,
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 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
3145 look up names in "X<T>::I" in order to determine that "Y" is
3146 a template. So, if we have a typename at this point, we make
3147 an effort to look through it. */
3149 && !typename_keyword_p
3151 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
3152 parser->scope = resolve_typename_type (parser->scope,
3153 /*only_current_p=*/false);
3154 /* Parse the qualifying entity. */
3156 = cp_parser_class_or_namespace_name (parser,
3162 /* Look for the `::' token. */
3163 cp_parser_require (parser, CPP_SCOPE, "`::'");
3165 /* If we found what we wanted, we keep going; otherwise, we're
3167 if (!cp_parser_parse_definitely (parser))
3169 bool error_p = false;
3171 /* Restore the OLD_SCOPE since it was valid before the
3172 failed attempt at finding the last
3173 class-or-namespace-name. */
3174 parser->scope = old_scope;
3175 parser->qualifying_scope = saved_qualifying_scope;
3176 /* If the next token is an identifier, and the one after
3177 that is a `::', then any valid interpretation would have
3178 found a class-or-namespace-name. */
3179 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
3180 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3182 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
3185 token = cp_lexer_consume_token (parser->lexer);
3190 decl = cp_parser_lookup_name_simple (parser, token->value);
3191 if (TREE_CODE (decl) == TEMPLATE_DECL)
3192 error ("`%D' used without template parameters",
3195 cp_parser_name_lookup_error
3196 (parser, token->value, decl,
3197 "is not a class or namespace");
3198 parser->scope = NULL_TREE;
3200 /* Treat this as a successful nested-name-specifier
3205 If the name found is not a class-name (clause
3206 _class_) or namespace-name (_namespace.def_), the
3207 program is ill-formed. */
3210 cp_lexer_consume_token (parser->lexer);
3215 /* We've found one valid nested-name-specifier. */
3217 /* Make sure we look in the right scope the next time through
3219 parser->scope = (TREE_CODE (new_scope) == TYPE_DECL
3220 ? TREE_TYPE (new_scope)
3222 /* If it is a class scope, try to complete it; we are about to
3223 be looking up names inside the class. */
3224 if (TYPE_P (parser->scope)
3225 /* Since checking types for dependency can be expensive,
3226 avoid doing it if the type is already complete. */
3227 && !COMPLETE_TYPE_P (parser->scope)
3228 /* Do not try to complete dependent types. */
3229 && !dependent_type_p (parser->scope))
3230 complete_type (parser->scope);
3233 /* Retrieve any deferred checks. Do not pop this access checks yet
3234 so the memory will not be reclaimed during token replacing below. */
3235 access_check = get_deferred_access_checks ();
3237 /* If parsing tentatively, replace the sequence of tokens that makes
3238 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
3239 token. That way, should we re-parse the token stream, we will
3240 not have to repeat the effort required to do the parse, nor will
3241 we issue duplicate error messages. */
3242 if (success && start >= 0)
3244 /* Find the token that corresponds to the start of the
3246 token = cp_lexer_advance_token (parser->lexer,
3247 parser->lexer->first_token,
3250 /* Reset the contents of the START token. */
3251 token->type = CPP_NESTED_NAME_SPECIFIER;
3252 token->value = build_tree_list (access_check, parser->scope);
3253 TREE_TYPE (token->value) = parser->qualifying_scope;
3254 token->keyword = RID_MAX;
3255 /* Purge all subsequent tokens. */
3256 cp_lexer_purge_tokens_after (parser->lexer, token);
3259 pop_deferring_access_checks ();
3260 return success ? parser->scope : NULL_TREE;
3263 /* Parse a nested-name-specifier. See
3264 cp_parser_nested_name_specifier_opt for details. This function
3265 behaves identically, except that it will an issue an error if no
3266 nested-name-specifier is present, and it will return
3267 ERROR_MARK_NODE, rather than NULL_TREE, if no nested-name-specifier
3271 cp_parser_nested_name_specifier (cp_parser *parser,
3272 bool typename_keyword_p,
3273 bool check_dependency_p,
3275 bool is_declaration)
3279 /* Look for the nested-name-specifier. */
3280 scope = cp_parser_nested_name_specifier_opt (parser,
3285 /* If it was not present, issue an error message. */
3288 cp_parser_error (parser, "expected nested-name-specifier");
3289 parser->scope = NULL_TREE;
3290 return error_mark_node;
3296 /* Parse a class-or-namespace-name.
3298 class-or-namespace-name:
3302 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
3303 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
3304 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
3305 TYPE_P is TRUE iff the next name should be taken as a class-name,
3306 even the same name is declared to be another entity in the same
3309 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
3310 specified by the class-or-namespace-name. If neither is found the
3311 ERROR_MARK_NODE is returned. */
3314 cp_parser_class_or_namespace_name (cp_parser *parser,
3315 bool typename_keyword_p,
3316 bool template_keyword_p,
3317 bool check_dependency_p,
3319 bool is_declaration)
3322 tree saved_qualifying_scope;
3323 tree saved_object_scope;
3327 /* Before we try to parse the class-name, we must save away the
3328 current PARSER->SCOPE since cp_parser_class_name will destroy
3330 saved_scope = parser->scope;
3331 saved_qualifying_scope = parser->qualifying_scope;
3332 saved_object_scope = parser->object_scope;
3333 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
3334 there is no need to look for a namespace-name. */
3335 only_class_p = template_keyword_p || (saved_scope && TYPE_P (saved_scope));
3337 cp_parser_parse_tentatively (parser);
3338 scope = cp_parser_class_name (parser,
3343 /*class_head_p=*/false,
3345 /* If that didn't work, try for a namespace-name. */
3346 if (!only_class_p && !cp_parser_parse_definitely (parser))
3348 /* Restore the saved scope. */
3349 parser->scope = saved_scope;
3350 parser->qualifying_scope = saved_qualifying_scope;
3351 parser->object_scope = saved_object_scope;
3352 /* If we are not looking at an identifier followed by the scope
3353 resolution operator, then this is not part of a
3354 nested-name-specifier. (Note that this function is only used
3355 to parse the components of a nested-name-specifier.) */
3356 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
3357 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
3358 return error_mark_node;
3359 scope = cp_parser_namespace_name (parser);
3365 /* Parse a postfix-expression.
3369 postfix-expression [ expression ]
3370 postfix-expression ( expression-list [opt] )
3371 simple-type-specifier ( expression-list [opt] )
3372 typename :: [opt] nested-name-specifier identifier
3373 ( expression-list [opt] )
3374 typename :: [opt] nested-name-specifier template [opt] template-id
3375 ( expression-list [opt] )
3376 postfix-expression . template [opt] id-expression
3377 postfix-expression -> template [opt] id-expression
3378 postfix-expression . pseudo-destructor-name
3379 postfix-expression -> pseudo-destructor-name
3380 postfix-expression ++
3381 postfix-expression --
3382 dynamic_cast < type-id > ( expression )
3383 static_cast < type-id > ( expression )
3384 reinterpret_cast < type-id > ( expression )
3385 const_cast < type-id > ( expression )
3386 typeid ( expression )
3392 ( type-id ) { initializer-list , [opt] }
3394 This extension is a GNU version of the C99 compound-literal
3395 construct. (The C99 grammar uses `type-name' instead of `type-id',
3396 but they are essentially the same concept.)
3398 If ADDRESS_P is true, the postfix expression is the operand of the
3401 Returns a representation of the expression. */
3404 cp_parser_postfix_expression (cp_parser *parser, bool address_p)
3408 cp_id_kind idk = CP_ID_KIND_NONE;
3409 tree postfix_expression = NULL_TREE;
3410 /* Non-NULL only if the current postfix-expression can be used to
3411 form a pointer-to-member. In that case, QUALIFYING_CLASS is the
3412 class used to qualify the member. */
3413 tree qualifying_class = NULL_TREE;
3415 /* Peek at the next token. */
3416 token = cp_lexer_peek_token (parser->lexer);
3417 /* Some of the productions are determined by keywords. */
3418 keyword = token->keyword;
3428 const char *saved_message;
3430 /* All of these can be handled in the same way from the point
3431 of view of parsing. Begin by consuming the token
3432 identifying the cast. */
3433 cp_lexer_consume_token (parser->lexer);
3435 /* New types cannot be defined in the cast. */
3436 saved_message = parser->type_definition_forbidden_message;
3437 parser->type_definition_forbidden_message
3438 = "types may not be defined in casts";
3440 /* Look for the opening `<'. */
3441 cp_parser_require (parser, CPP_LESS, "`<'");
3442 /* Parse the type to which we are casting. */
3443 type = cp_parser_type_id (parser);
3444 /* Look for the closing `>'. */
3445 cp_parser_require (parser, CPP_GREATER, "`>'");
3446 /* Restore the old message. */
3447 parser->type_definition_forbidden_message = saved_message;
3449 /* And the expression which is being cast. */
3450 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3451 expression = cp_parser_expression (parser);
3452 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3454 /* Only type conversions to integral or enumeration types
3455 can be used in constant-expressions. */
3456 if (parser->integral_constant_expression_p
3457 && !dependent_type_p (type)
3458 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type)
3459 /* A cast to pointer or reference type is allowed in the
3460 implementation of "offsetof". */
3461 && !(parser->in_offsetof_p && POINTER_TYPE_P (type))
3462 && (cp_parser_non_integral_constant_expression
3464 "a cast to a type other than an integral or "
3465 "enumeration type")))
3466 return error_mark_node;
3472 = build_dynamic_cast (type, expression);
3476 = build_static_cast (type, expression);
3480 = build_reinterpret_cast (type, expression);
3484 = build_const_cast (type, expression);
3495 const char *saved_message;
3496 bool saved_in_type_id_in_expr_p;
3498 /* Consume the `typeid' token. */
3499 cp_lexer_consume_token (parser->lexer);
3500 /* Look for the `(' token. */
3501 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3502 /* Types cannot be defined in a `typeid' expression. */
3503 saved_message = parser->type_definition_forbidden_message;
3504 parser->type_definition_forbidden_message
3505 = "types may not be defined in a `typeid\' expression";
3506 /* We can't be sure yet whether we're looking at a type-id or an
3508 cp_parser_parse_tentatively (parser);
3509 /* Try a type-id first. */
3510 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
3511 parser->in_type_id_in_expr_p = true;
3512 type = cp_parser_type_id (parser);
3513 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
3514 /* Look for the `)' token. Otherwise, we can't be sure that
3515 we're not looking at an expression: consider `typeid (int
3516 (3))', for example. */
3517 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3518 /* If all went well, simply lookup the type-id. */
3519 if (cp_parser_parse_definitely (parser))
3520 postfix_expression = get_typeid (type);
3521 /* Otherwise, fall back to the expression variant. */
3526 /* Look for an expression. */
3527 expression = cp_parser_expression (parser);
3528 /* Compute its typeid. */
3529 postfix_expression = build_typeid (expression);
3530 /* Look for the `)' token. */
3531 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3533 /* `typeid' may not appear in an integral constant expression. */
3534 if (cp_parser_non_integral_constant_expression(parser,
3535 "`typeid' operator"))
3536 return error_mark_node;
3537 /* Restore the saved message. */
3538 parser->type_definition_forbidden_message = saved_message;
3544 bool template_p = false;
3548 /* Consume the `typename' token. */
3549 cp_lexer_consume_token (parser->lexer);
3550 /* Look for the optional `::' operator. */
3551 cp_parser_global_scope_opt (parser,
3552 /*current_scope_valid_p=*/false);
3553 /* Look for the nested-name-specifier. */
3554 cp_parser_nested_name_specifier (parser,
3555 /*typename_keyword_p=*/true,
3556 /*check_dependency_p=*/true,
3558 /*is_declaration=*/true);
3559 /* Look for the optional `template' keyword. */
3560 template_p = cp_parser_optional_template_keyword (parser);
3561 /* We don't know whether we're looking at a template-id or an
3563 cp_parser_parse_tentatively (parser);
3564 /* Try a template-id. */
3565 id = cp_parser_template_id (parser, template_p,
3566 /*check_dependency_p=*/true,
3567 /*is_declaration=*/true);
3568 /* If that didn't work, try an identifier. */
3569 if (!cp_parser_parse_definitely (parser))
3570 id = cp_parser_identifier (parser);
3571 /* If we look up a template-id in a non-dependent qualifying
3572 scope, there's no need to create a dependent type. */
3573 if (TREE_CODE (id) == TYPE_DECL
3574 && !dependent_type_p (parser->scope))
3575 type = TREE_TYPE (id);
3576 /* Create a TYPENAME_TYPE to represent the type to which the
3577 functional cast is being performed. */
3579 type = make_typename_type (parser->scope, id,
3582 postfix_expression = cp_parser_functional_cast (parser, type);
3590 /* If the next thing is a simple-type-specifier, we may be
3591 looking at a functional cast. We could also be looking at
3592 an id-expression. So, we try the functional cast, and if
3593 that doesn't work we fall back to the primary-expression. */
3594 cp_parser_parse_tentatively (parser);
3595 /* Look for the simple-type-specifier. */
3596 type = cp_parser_simple_type_specifier (parser,
3597 CP_PARSER_FLAGS_NONE,
3598 /*identifier_p=*/false);
3599 /* Parse the cast itself. */
3600 if (!cp_parser_error_occurred (parser))
3602 = cp_parser_functional_cast (parser, type);
3603 /* If that worked, we're done. */
3604 if (cp_parser_parse_definitely (parser))
3607 /* If the functional-cast didn't work out, try a
3608 compound-literal. */
3609 if (cp_parser_allow_gnu_extensions_p (parser)
3610 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
3612 tree initializer_list = NULL_TREE;
3613 bool saved_in_type_id_in_expr_p;
3615 cp_parser_parse_tentatively (parser);
3616 /* Consume the `('. */
3617 cp_lexer_consume_token (parser->lexer);
3618 /* Parse the type. */
3619 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
3620 parser->in_type_id_in_expr_p = true;
3621 type = cp_parser_type_id (parser);
3622 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
3623 /* Look for the `)'. */
3624 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3625 /* Look for the `{'. */
3626 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
3627 /* If things aren't going well, there's no need to
3629 if (!cp_parser_error_occurred (parser))
3631 bool non_constant_p;
3632 /* Parse the initializer-list. */
3634 = cp_parser_initializer_list (parser, &non_constant_p);
3635 /* Allow a trailing `,'. */
3636 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
3637 cp_lexer_consume_token (parser->lexer);
3638 /* Look for the final `}'. */
3639 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
3641 /* If that worked, we're definitely looking at a
3642 compound-literal expression. */
3643 if (cp_parser_parse_definitely (parser))
3645 /* Warn the user that a compound literal is not
3646 allowed in standard C++. */
3648 pedwarn ("ISO C++ forbids compound-literals");
3649 /* Form the representation of the compound-literal. */
3651 = finish_compound_literal (type, initializer_list);
3656 /* It must be a primary-expression. */
3657 postfix_expression = cp_parser_primary_expression (parser,
3664 /* If we were avoiding committing to the processing of a
3665 qualified-id until we knew whether or not we had a
3666 pointer-to-member, we now know. */
3667 if (qualifying_class)
3671 /* Peek at the next token. */
3672 token = cp_lexer_peek_token (parser->lexer);
3673 done = (token->type != CPP_OPEN_SQUARE
3674 && token->type != CPP_OPEN_PAREN
3675 && token->type != CPP_DOT
3676 && token->type != CPP_DEREF
3677 && token->type != CPP_PLUS_PLUS
3678 && token->type != CPP_MINUS_MINUS);
3680 postfix_expression = finish_qualified_id_expr (qualifying_class,
3685 return postfix_expression;
3688 /* Keep looping until the postfix-expression is complete. */
3691 if (idk == CP_ID_KIND_UNQUALIFIED
3692 && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
3693 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
3694 /* It is not a Koenig lookup function call. */
3696 = unqualified_name_lookup_error (postfix_expression);
3698 /* Peek at the next token. */
3699 token = cp_lexer_peek_token (parser->lexer);
3701 switch (token->type)
3703 case CPP_OPEN_SQUARE:
3704 /* postfix-expression [ expression ] */
3708 /* Consume the `[' token. */
3709 cp_lexer_consume_token (parser->lexer);
3710 /* Parse the index expression. */
3711 index = cp_parser_expression (parser);
3712 /* Look for the closing `]'. */
3713 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
3715 /* Build the ARRAY_REF. */
3717 = grok_array_decl (postfix_expression, index);
3718 idk = CP_ID_KIND_NONE;
3719 /* Array references are not permitted in
3720 constant-expressions (but they are allowed
3722 if (!parser->in_offsetof_p
3723 && cp_parser_non_integral_constant_expression
3724 (parser, "an array reference"))
3725 postfix_expression = error_mark_node;
3729 case CPP_OPEN_PAREN:
3730 /* postfix-expression ( expression-list [opt] ) */
3733 tree args = (cp_parser_parenthesized_expression_list
3734 (parser, false, /*non_constant_p=*/NULL));
3736 if (args == error_mark_node)
3738 postfix_expression = error_mark_node;
3742 /* Function calls are not permitted in
3743 constant-expressions. */
3744 if (cp_parser_non_integral_constant_expression (parser,
3747 postfix_expression = error_mark_node;
3752 if (idk == CP_ID_KIND_UNQUALIFIED)
3754 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
3760 = perform_koenig_lookup (postfix_expression, args);
3764 = unqualified_fn_lookup_error (postfix_expression);
3766 /* We do not perform argument-dependent lookup if
3767 normal lookup finds a non-function, in accordance
3768 with the expected resolution of DR 218. */
3769 else if (args && is_overloaded_fn (postfix_expression))
3771 tree fn = get_first_fn (postfix_expression);
3772 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
3773 fn = OVL_CURRENT (TREE_OPERAND (fn, 0));
3774 /* Only do argument dependent lookup if regular
3775 lookup does not find a set of member functions.
3776 [basic.lookup.koenig]/2a */
3777 if (!DECL_FUNCTION_MEMBER_P (fn))
3781 = perform_koenig_lookup (postfix_expression, args);
3786 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
3788 tree instance = TREE_OPERAND (postfix_expression, 0);
3789 tree fn = TREE_OPERAND (postfix_expression, 1);
3791 if (processing_template_decl
3792 && (type_dependent_expression_p (instance)
3793 || (!BASELINK_P (fn)
3794 && TREE_CODE (fn) != FIELD_DECL)
3795 || type_dependent_expression_p (fn)
3796 || any_type_dependent_arguments_p (args)))
3799 = build_min_nt (CALL_EXPR, postfix_expression, args);
3803 if (BASELINK_P (fn))
3805 = (build_new_method_call
3806 (instance, fn, args, NULL_TREE,
3807 (idk == CP_ID_KIND_QUALIFIED
3808 ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL)));
3811 = finish_call_expr (postfix_expression, args,
3812 /*disallow_virtual=*/false,
3813 /*koenig_p=*/false);
3815 else if (TREE_CODE (postfix_expression) == OFFSET_REF
3816 || TREE_CODE (postfix_expression) == MEMBER_REF
3817 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
3818 postfix_expression = (build_offset_ref_call_from_tree
3819 (postfix_expression, args));
3820 else if (idk == CP_ID_KIND_QUALIFIED)
3821 /* A call to a static class member, or a namespace-scope
3824 = finish_call_expr (postfix_expression, args,
3825 /*disallow_virtual=*/true,
3828 /* All other function calls. */
3830 = finish_call_expr (postfix_expression, args,
3831 /*disallow_virtual=*/false,
3834 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
3835 idk = CP_ID_KIND_NONE;
3841 /* postfix-expression . template [opt] id-expression
3842 postfix-expression . pseudo-destructor-name
3843 postfix-expression -> template [opt] id-expression
3844 postfix-expression -> pseudo-destructor-name */
3849 bool pseudo_destructor_p;
3850 tree scope = NULL_TREE;
3851 enum cpp_ttype token_type = token->type;
3853 /* If this is a `->' operator, dereference the pointer. */
3854 if (token->type == CPP_DEREF)
3855 postfix_expression = build_x_arrow (postfix_expression);
3856 /* Check to see whether or not the expression is
3858 dependent_p = type_dependent_expression_p (postfix_expression);
3859 /* The identifier following the `->' or `.' is not
3861 parser->scope = NULL_TREE;
3862 parser->qualifying_scope = NULL_TREE;
3863 parser->object_scope = NULL_TREE;
3864 idk = CP_ID_KIND_NONE;
3865 /* Enter the scope corresponding to the type of the object
3866 given by the POSTFIX_EXPRESSION. */
3868 && TREE_TYPE (postfix_expression) != NULL_TREE)
3870 scope = TREE_TYPE (postfix_expression);
3871 /* According to the standard, no expression should
3872 ever have reference type. Unfortunately, we do not
3873 currently match the standard in this respect in
3874 that our internal representation of an expression
3875 may have reference type even when the standard says
3876 it does not. Therefore, we have to manually obtain
3877 the underlying type here. */
3878 scope = non_reference (scope);
3879 /* The type of the POSTFIX_EXPRESSION must be
3881 scope = complete_type_or_else (scope, NULL_TREE);
3882 /* Let the name lookup machinery know that we are
3883 processing a class member access expression. */
3884 parser->context->object_type = scope;
3885 /* If something went wrong, we want to be able to
3886 discern that case, as opposed to the case where
3887 there was no SCOPE due to the type of expression
3890 scope = error_mark_node;
3891 /* If the SCOPE was erroneous, make the various
3892 semantic analysis functions exit quickly -- and
3893 without issuing additional error messages. */
3894 if (scope == error_mark_node)
3895 postfix_expression = error_mark_node;
3898 /* Consume the `.' or `->' operator. */
3899 cp_lexer_consume_token (parser->lexer);
3901 /* Assume this expression is not a pseudo-destructor access. */
3902 pseudo_destructor_p = false;
3904 /* If the SCOPE is a scalar type, then, if this is a valid program,
3905 we must be looking at a pseudo-destructor-name. */
3906 if (scope && SCALAR_TYPE_P (scope))
3911 cp_parser_parse_tentatively (parser);
3912 /* Parse the pseudo-destructor-name. */
3913 cp_parser_pseudo_destructor_name (parser, &s, &type);
3914 if (cp_parser_parse_definitely (parser))
3916 pseudo_destructor_p = true;
3918 = finish_pseudo_destructor_expr (postfix_expression,
3919 s, TREE_TYPE (type));
3923 if (!pseudo_destructor_p)
3925 /* If the SCOPE is not a scalar type, we are looking
3926 at an ordinary class member access expression,
3927 rather than a pseudo-destructor-name. */
3928 template_p = cp_parser_optional_template_keyword (parser);
3929 /* Parse the id-expression. */
3930 name = cp_parser_id_expression (parser,
3932 /*check_dependency_p=*/true,
3933 /*template_p=*/NULL,
3934 /*declarator_p=*/false);
3935 /* In general, build a SCOPE_REF if the member name is
3936 qualified. However, if the name was not dependent
3937 and has already been resolved; there is no need to
3938 build the SCOPE_REF. For example;
3940 struct X { void f(); };
3941 template <typename T> void f(T* t) { t->X::f(); }
3943 Even though "t" is dependent, "X::f" is not and has
3944 been resolved to a BASELINK; there is no need to
3945 include scope information. */
3947 /* But we do need to remember that there was an explicit
3948 scope for virtual function calls. */
3950 idk = CP_ID_KIND_QUALIFIED;
3952 if (name != error_mark_node
3953 && !BASELINK_P (name)
3956 name = build_nt (SCOPE_REF, parser->scope, name);
3957 parser->scope = NULL_TREE;
3958 parser->qualifying_scope = NULL_TREE;
3959 parser->object_scope = NULL_TREE;
3961 if (scope && name && BASELINK_P (name))
3962 adjust_result_of_qualified_name_lookup
3963 (name, BINFO_TYPE (BASELINK_BINFO (name)), scope);
3965 = finish_class_member_access_expr (postfix_expression, name);
3968 /* We no longer need to look up names in the scope of the
3969 object on the left-hand side of the `.' or `->'
3971 parser->context->object_type = NULL_TREE;
3972 /* These operators may not appear in constant-expressions. */
3973 if (/* The "->" operator is allowed in the implementation
3974 of "offsetof". The "." operator may appear in the
3975 name of the member. */
3976 !parser->in_offsetof_p
3977 && (cp_parser_non_integral_constant_expression
3979 token_type == CPP_DEREF ? "'->'" : "`.'")))
3980 postfix_expression = error_mark_node;
3985 /* postfix-expression ++ */
3986 /* Consume the `++' token. */
3987 cp_lexer_consume_token (parser->lexer);
3988 /* Generate a representation for the complete expression. */
3990 = finish_increment_expr (postfix_expression,
3991 POSTINCREMENT_EXPR);
3992 /* Increments may not appear in constant-expressions. */
3993 if (cp_parser_non_integral_constant_expression (parser,
3995 postfix_expression = error_mark_node;
3996 idk = CP_ID_KIND_NONE;
3999 case CPP_MINUS_MINUS:
4000 /* postfix-expression -- */
4001 /* Consume the `--' token. */
4002 cp_lexer_consume_token (parser->lexer);
4003 /* Generate a representation for the complete expression. */
4005 = finish_increment_expr (postfix_expression,
4006 POSTDECREMENT_EXPR);
4007 /* Decrements may not appear in constant-expressions. */
4008 if (cp_parser_non_integral_constant_expression (parser,
4010 postfix_expression = error_mark_node;
4011 idk = CP_ID_KIND_NONE;
4015 return postfix_expression;
4019 /* We should never get here. */
4021 return error_mark_node;
4024 /* Parse a parenthesized expression-list.
4027 assignment-expression
4028 expression-list, assignment-expression
4033 identifier, expression-list
4035 Returns a TREE_LIST. The TREE_VALUE of each node is a
4036 representation of an assignment-expression. Note that a TREE_LIST
4037 is returned even if there is only a single expression in the list.
4038 error_mark_node is returned if the ( and or ) are
4039 missing. NULL_TREE is returned on no expressions. The parentheses
4040 are eaten. IS_ATTRIBUTE_LIST is true if this is really an attribute
4041 list being parsed. If NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P
4042 indicates whether or not all of the expressions in the list were
4046 cp_parser_parenthesized_expression_list (cp_parser* parser,
4047 bool is_attribute_list,
4048 bool *non_constant_p)
4050 tree expression_list = NULL_TREE;
4051 tree identifier = NULL_TREE;
4053 /* Assume all the expressions will be constant. */
4055 *non_constant_p = false;
4057 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
4058 return error_mark_node;
4060 /* Consume expressions until there are no more. */
4061 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
4066 /* At the beginning of attribute lists, check to see if the
4067 next token is an identifier. */
4068 if (is_attribute_list
4069 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
4073 /* Consume the identifier. */
4074 token = cp_lexer_consume_token (parser->lexer);
4075 /* Save the identifier. */
4076 identifier = token->value;
4080 /* Parse the next assignment-expression. */
4083 bool expr_non_constant_p;
4084 expr = (cp_parser_constant_expression
4085 (parser, /*allow_non_constant_p=*/true,
4086 &expr_non_constant_p));
4087 if (expr_non_constant_p)
4088 *non_constant_p = true;
4091 expr = cp_parser_assignment_expression (parser);
4093 /* Add it to the list. We add error_mark_node
4094 expressions to the list, so that we can still tell if
4095 the correct form for a parenthesized expression-list
4096 is found. That gives better errors. */
4097 expression_list = tree_cons (NULL_TREE, expr, expression_list);
4099 if (expr == error_mark_node)
4103 /* After the first item, attribute lists look the same as
4104 expression lists. */
4105 is_attribute_list = false;
4108 /* If the next token isn't a `,', then we are done. */
4109 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
4112 /* Otherwise, consume the `,' and keep going. */
4113 cp_lexer_consume_token (parser->lexer);
4116 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
4121 /* We try and resync to an unnested comma, as that will give the
4122 user better diagnostics. */
4123 ending = cp_parser_skip_to_closing_parenthesis (parser,
4124 /*recovering=*/true,
4126 /*consume_paren=*/true);
4130 return error_mark_node;
4133 /* We built up the list in reverse order so we must reverse it now. */
4134 expression_list = nreverse (expression_list);
4136 expression_list = tree_cons (NULL_TREE, identifier, expression_list);
4138 return expression_list;
4141 /* Parse a pseudo-destructor-name.
4143 pseudo-destructor-name:
4144 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
4145 :: [opt] nested-name-specifier template template-id :: ~ type-name
4146 :: [opt] nested-name-specifier [opt] ~ type-name
4148 If either of the first two productions is used, sets *SCOPE to the
4149 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
4150 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
4151 or ERROR_MARK_NODE if the parse fails. */
4154 cp_parser_pseudo_destructor_name (cp_parser* parser,
4158 bool nested_name_specifier_p;
4160 /* Look for the optional `::' operator. */
4161 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
4162 /* Look for the optional nested-name-specifier. */
4163 nested_name_specifier_p
4164 = (cp_parser_nested_name_specifier_opt (parser,
4165 /*typename_keyword_p=*/false,
4166 /*check_dependency_p=*/true,
4168 /*is_declaration=*/true)
4170 /* Now, if we saw a nested-name-specifier, we might be doing the
4171 second production. */
4172 if (nested_name_specifier_p
4173 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
4175 /* Consume the `template' keyword. */
4176 cp_lexer_consume_token (parser->lexer);
4177 /* Parse the template-id. */
4178 cp_parser_template_id (parser,
4179 /*template_keyword_p=*/true,
4180 /*check_dependency_p=*/false,
4181 /*is_declaration=*/true);
4182 /* Look for the `::' token. */
4183 cp_parser_require (parser, CPP_SCOPE, "`::'");
4185 /* If the next token is not a `~', then there might be some
4186 additional qualification. */
4187 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
4189 /* Look for the type-name. */
4190 *scope = TREE_TYPE (cp_parser_type_name (parser));
4192 /* If we didn't get an aggregate type, or we don't have ::~,
4193 then something has gone wrong. Since the only caller of this
4194 function is looking for something after `.' or `->' after a
4195 scalar type, most likely the program is trying to get a
4196 member of a non-aggregate type. */
4197 if (*scope == error_mark_node
4198 || cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE)
4199 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_COMPL)
4201 cp_parser_error (parser, "request for member of non-aggregate type");
4202 *type = error_mark_node;
4206 /* Look for the `::' token. */
4207 cp_parser_require (parser, CPP_SCOPE, "`::'");
4212 /* Look for the `~'. */
4213 cp_parser_require (parser, CPP_COMPL, "`~'");
4214 /* Look for the type-name again. We are not responsible for
4215 checking that it matches the first type-name. */
4216 *type = cp_parser_type_name (parser);
4219 /* Parse a unary-expression.
4225 unary-operator cast-expression
4226 sizeof unary-expression
4234 __extension__ cast-expression
4235 __alignof__ unary-expression
4236 __alignof__ ( type-id )
4237 __real__ cast-expression
4238 __imag__ cast-expression
4241 ADDRESS_P is true iff the unary-expression is appearing as the
4242 operand of the `&' operator.
4244 Returns a representation of the expression. */
4247 cp_parser_unary_expression (cp_parser *parser, bool address_p)
4250 enum tree_code unary_operator;
4252 /* Peek at the next token. */
4253 token = cp_lexer_peek_token (parser->lexer);
4254 /* Some keywords give away the kind of expression. */
4255 if (token->type == CPP_KEYWORD)
4257 enum rid keyword = token->keyword;
4267 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
4268 /* Consume the token. */
4269 cp_lexer_consume_token (parser->lexer);
4270 /* Parse the operand. */
4271 operand = cp_parser_sizeof_operand (parser, keyword);
4273 if (TYPE_P (operand))
4274 return cxx_sizeof_or_alignof_type (operand, op, true);
4276 return cxx_sizeof_or_alignof_expr (operand, op);
4280 return cp_parser_new_expression (parser);
4283 return cp_parser_delete_expression (parser);
4287 /* The saved value of the PEDANTIC flag. */
4291 /* Save away the PEDANTIC flag. */
4292 cp_parser_extension_opt (parser, &saved_pedantic);
4293 /* Parse the cast-expression. */
4294 expr = cp_parser_simple_cast_expression (parser);
4295 /* Restore the PEDANTIC flag. */
4296 pedantic = saved_pedantic;
4306 /* Consume the `__real__' or `__imag__' token. */
4307 cp_lexer_consume_token (parser->lexer);
4308 /* Parse the cast-expression. */
4309 expression = cp_parser_simple_cast_expression (parser);
4310 /* Create the complete representation. */
4311 return build_x_unary_op ((keyword == RID_REALPART
4312 ? REALPART_EXPR : IMAGPART_EXPR),
4322 /* Look for the `:: new' and `:: delete', which also signal the
4323 beginning of a new-expression, or delete-expression,
4324 respectively. If the next token is `::', then it might be one of
4326 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
4330 /* See if the token after the `::' is one of the keywords in
4331 which we're interested. */
4332 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
4333 /* If it's `new', we have a new-expression. */
4334 if (keyword == RID_NEW)
4335 return cp_parser_new_expression (parser);
4336 /* Similarly, for `delete'. */
4337 else if (keyword == RID_DELETE)
4338 return cp_parser_delete_expression (parser);
4341 /* Look for a unary operator. */
4342 unary_operator = cp_parser_unary_operator (token);
4343 /* The `++' and `--' operators can be handled similarly, even though
4344 they are not technically unary-operators in the grammar. */
4345 if (unary_operator == ERROR_MARK)
4347 if (token->type == CPP_PLUS_PLUS)
4348 unary_operator = PREINCREMENT_EXPR;
4349 else if (token->type == CPP_MINUS_MINUS)
4350 unary_operator = PREDECREMENT_EXPR;
4351 /* Handle the GNU address-of-label extension. */
4352 else if (cp_parser_allow_gnu_extensions_p (parser)
4353 && token->type == CPP_AND_AND)
4357 /* Consume the '&&' token. */
4358 cp_lexer_consume_token (parser->lexer);
4359 /* Look for the identifier. */
4360 identifier = cp_parser_identifier (parser);
4361 /* Create an expression representing the address. */
4362 return finish_label_address_expr (identifier);
4365 if (unary_operator != ERROR_MARK)
4367 tree cast_expression;
4368 tree expression = error_mark_node;
4369 const char *non_constant_p = NULL;
4371 /* Consume the operator token. */
4372 token = cp_lexer_consume_token (parser->lexer);
4373 /* Parse the cast-expression. */
4375 = cp_parser_cast_expression (parser, unary_operator == ADDR_EXPR);
4376 /* Now, build an appropriate representation. */
4377 switch (unary_operator)
4380 non_constant_p = "`*'";
4381 expression = build_x_indirect_ref (cast_expression, "unary *");
4385 /* The "&" operator is allowed in the implementation of
4387 if (!parser->in_offsetof_p)
4388 non_constant_p = "`&'";
4391 expression = build_x_unary_op (unary_operator, cast_expression);
4394 case PREINCREMENT_EXPR:
4395 case PREDECREMENT_EXPR:
4396 non_constant_p = (unary_operator == PREINCREMENT_EXPR
4401 case TRUTH_NOT_EXPR:
4402 expression = finish_unary_op_expr (unary_operator, cast_expression);
4410 && cp_parser_non_integral_constant_expression (parser,
4412 expression = error_mark_node;
4417 return cp_parser_postfix_expression (parser, address_p);
4420 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
4421 unary-operator, the corresponding tree code is returned. */
4423 static enum tree_code
4424 cp_parser_unary_operator (cp_token* token)
4426 switch (token->type)
4429 return INDIRECT_REF;
4435 return CONVERT_EXPR;
4441 return TRUTH_NOT_EXPR;
4444 return BIT_NOT_EXPR;
4451 /* Parse a new-expression.
4454 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
4455 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
4457 Returns a representation of the expression. */
4460 cp_parser_new_expression (cp_parser* parser)
4462 bool global_scope_p;
4467 /* Look for the optional `::' operator. */
4469 = (cp_parser_global_scope_opt (parser,
4470 /*current_scope_valid_p=*/false)
4472 /* Look for the `new' operator. */
4473 cp_parser_require_keyword (parser, RID_NEW, "`new'");
4474 /* There's no easy way to tell a new-placement from the
4475 `( type-id )' construct. */
4476 cp_parser_parse_tentatively (parser);
4477 /* Look for a new-placement. */
4478 placement = cp_parser_new_placement (parser);
4479 /* If that didn't work out, there's no new-placement. */
4480 if (!cp_parser_parse_definitely (parser))
4481 placement = NULL_TREE;
4483 /* If the next token is a `(', then we have a parenthesized
4485 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4487 /* Consume the `('. */
4488 cp_lexer_consume_token (parser->lexer);
4489 /* Parse the type-id. */
4490 type = cp_parser_type_id (parser);
4491 /* Look for the closing `)'. */
4492 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4493 /* There should not be a direct-new-declarator in this production,
4494 but GCC used to allowed this, so we check and emit a sensible error
4495 message for this case. */
4496 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
4498 error ("array bound forbidden after parenthesized type-id");
4499 inform ("try removing the parentheses around the type-id");
4500 cp_parser_direct_new_declarator (parser);
4503 /* Otherwise, there must be a new-type-id. */
4505 type = cp_parser_new_type_id (parser);
4507 /* If the next token is a `(', then we have a new-initializer. */
4508 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4509 initializer = cp_parser_new_initializer (parser);
4511 initializer = NULL_TREE;
4513 /* A new-expression may not appear in an integral constant
4515 if (cp_parser_non_integral_constant_expression (parser, "`new'"))
4516 return error_mark_node;
4518 /* Create a representation of the new-expression. */
4519 return build_new (placement, type, initializer, global_scope_p);
4522 /* Parse a new-placement.
4527 Returns the same representation as for an expression-list. */
4530 cp_parser_new_placement (cp_parser* parser)
4532 tree expression_list;
4534 /* Parse the expression-list. */
4535 expression_list = (cp_parser_parenthesized_expression_list
4536 (parser, false, /*non_constant_p=*/NULL));
4538 return expression_list;
4541 /* Parse a new-type-id.
4544 type-specifier-seq new-declarator [opt]
4546 Returns a TREE_LIST whose TREE_PURPOSE is the type-specifier-seq,
4547 and whose TREE_VALUE is the new-declarator. */
4550 cp_parser_new_type_id (cp_parser* parser)
4552 tree type_specifier_seq;
4554 const char *saved_message;
4556 /* The type-specifier sequence must not contain type definitions.
4557 (It cannot contain declarations of new types either, but if they
4558 are not definitions we will catch that because they are not
4560 saved_message = parser->type_definition_forbidden_message;
4561 parser->type_definition_forbidden_message
4562 = "types may not be defined in a new-type-id";
4563 /* Parse the type-specifier-seq. */
4564 type_specifier_seq = cp_parser_type_specifier_seq (parser);
4565 /* Restore the old message. */
4566 parser->type_definition_forbidden_message = saved_message;
4567 /* Parse the new-declarator. */
4568 declarator = cp_parser_new_declarator_opt (parser);
4570 return build_tree_list (type_specifier_seq, declarator);
4573 /* Parse an (optional) new-declarator.
4576 ptr-operator new-declarator [opt]
4577 direct-new-declarator
4579 Returns a representation of the declarator. See
4580 cp_parser_declarator for the representations used. */
4583 cp_parser_new_declarator_opt (cp_parser* parser)
4585 enum tree_code code;
4587 tree cv_qualifier_seq;
4589 /* We don't know if there's a ptr-operator next, or not. */
4590 cp_parser_parse_tentatively (parser);
4591 /* Look for a ptr-operator. */
4592 code = cp_parser_ptr_operator (parser, &type, &cv_qualifier_seq);
4593 /* If that worked, look for more new-declarators. */
4594 if (cp_parser_parse_definitely (parser))
4598 /* Parse another optional declarator. */
4599 declarator = cp_parser_new_declarator_opt (parser);
4601 /* Create the representation of the declarator. */
4602 if (code == INDIRECT_REF)
4603 declarator = make_pointer_declarator (cv_qualifier_seq,
4606 declarator = make_reference_declarator (cv_qualifier_seq,
4609 /* Handle the pointer-to-member case. */
4611 declarator = build_nt (SCOPE_REF, type, declarator);
4616 /* If the next token is a `[', there is a direct-new-declarator. */
4617 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
4618 return cp_parser_direct_new_declarator (parser);
4623 /* Parse a direct-new-declarator.
4625 direct-new-declarator:
4627 direct-new-declarator [constant-expression]
4629 Returns an ARRAY_REF, following the same conventions as are
4630 documented for cp_parser_direct_declarator. */
4633 cp_parser_direct_new_declarator (cp_parser* parser)
4635 tree declarator = NULL_TREE;
4641 /* Look for the opening `['. */
4642 cp_parser_require (parser, CPP_OPEN_SQUARE, "`['");
4643 /* The first expression is not required to be constant. */
4646 expression = cp_parser_expression (parser);
4647 /* The standard requires that the expression have integral
4648 type. DR 74 adds enumeration types. We believe that the
4649 real intent is that these expressions be handled like the
4650 expression in a `switch' condition, which also allows
4651 classes with a single conversion to integral or
4652 enumeration type. */
4653 if (!processing_template_decl)
4656 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
4661 error ("expression in new-declarator must have integral or enumeration type");
4662 expression = error_mark_node;
4666 /* But all the other expressions must be. */
4669 = cp_parser_constant_expression (parser,
4670 /*allow_non_constant=*/false,
4672 /* Look for the closing `]'. */
4673 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4675 /* Add this bound to the declarator. */
4676 declarator = build_nt (ARRAY_REF, declarator, expression);
4678 /* If the next token is not a `[', then there are no more
4680 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
4687 /* Parse a new-initializer.
4690 ( expression-list [opt] )
4692 Returns a representation of the expression-list. If there is no
4693 expression-list, VOID_ZERO_NODE is returned. */
4696 cp_parser_new_initializer (cp_parser* parser)
4698 tree expression_list;
4700 expression_list = (cp_parser_parenthesized_expression_list
4701 (parser, false, /*non_constant_p=*/NULL));
4702 if (!expression_list)
4703 expression_list = void_zero_node;
4705 return expression_list;
4708 /* Parse a delete-expression.
4711 :: [opt] delete cast-expression
4712 :: [opt] delete [ ] cast-expression
4714 Returns a representation of the expression. */
4717 cp_parser_delete_expression (cp_parser* parser)
4719 bool global_scope_p;
4723 /* Look for the optional `::' operator. */
4725 = (cp_parser_global_scope_opt (parser,
4726 /*current_scope_valid_p=*/false)
4728 /* Look for the `delete' keyword. */
4729 cp_parser_require_keyword (parser, RID_DELETE, "`delete'");
4730 /* See if the array syntax is in use. */
4731 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
4733 /* Consume the `[' token. */
4734 cp_lexer_consume_token (parser->lexer);
4735 /* Look for the `]' token. */
4736 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4737 /* Remember that this is the `[]' construct. */
4743 /* Parse the cast-expression. */
4744 expression = cp_parser_simple_cast_expression (parser);
4746 /* A delete-expression may not appear in an integral constant
4748 if (cp_parser_non_integral_constant_expression (parser, "`delete'"))
4749 return error_mark_node;
4751 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
4754 /* Parse a cast-expression.
4758 ( type-id ) cast-expression
4760 Returns a representation of the expression. */
4763 cp_parser_cast_expression (cp_parser *parser, bool address_p)
4765 /* If it's a `(', then we might be looking at a cast. */
4766 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4768 tree type = NULL_TREE;
4769 tree expr = NULL_TREE;
4770 bool compound_literal_p;
4771 const char *saved_message;
4773 /* There's no way to know yet whether or not this is a cast.
4774 For example, `(int (3))' is a unary-expression, while `(int)
4775 3' is a cast. So, we resort to parsing tentatively. */
4776 cp_parser_parse_tentatively (parser);
4777 /* Types may not be defined in a cast. */
4778 saved_message = parser->type_definition_forbidden_message;
4779 parser->type_definition_forbidden_message
4780 = "types may not be defined in casts";
4781 /* Consume the `('. */
4782 cp_lexer_consume_token (parser->lexer);
4783 /* A very tricky bit is that `(struct S) { 3 }' is a
4784 compound-literal (which we permit in C++ as an extension).
4785 But, that construct is not a cast-expression -- it is a
4786 postfix-expression. (The reason is that `(struct S) { 3 }.i'
4787 is legal; if the compound-literal were a cast-expression,
4788 you'd need an extra set of parentheses.) But, if we parse
4789 the type-id, and it happens to be a class-specifier, then we
4790 will commit to the parse at that point, because we cannot
4791 undo the action that is done when creating a new class. So,
4792 then we cannot back up and do a postfix-expression.
4794 Therefore, we scan ahead to the closing `)', and check to see
4795 if the token after the `)' is a `{'. If so, we are not
4796 looking at a cast-expression.
4798 Save tokens so that we can put them back. */
4799 cp_lexer_save_tokens (parser->lexer);
4800 /* Skip tokens until the next token is a closing parenthesis.
4801 If we find the closing `)', and the next token is a `{', then
4802 we are looking at a compound-literal. */
4804 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
4805 /*consume_paren=*/true)
4806 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
4807 /* Roll back the tokens we skipped. */
4808 cp_lexer_rollback_tokens (parser->lexer);
4809 /* If we were looking at a compound-literal, simulate an error
4810 so that the call to cp_parser_parse_definitely below will
4812 if (compound_literal_p)
4813 cp_parser_simulate_error (parser);
4816 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4817 parser->in_type_id_in_expr_p = true;
4818 /* Look for the type-id. */
4819 type = cp_parser_type_id (parser);
4820 /* Look for the closing `)'. */
4821 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4822 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4825 /* Restore the saved message. */
4826 parser->type_definition_forbidden_message = saved_message;
4828 /* If ok so far, parse the dependent expression. We cannot be
4829 sure it is a cast. Consider `(T ())'. It is a parenthesized
4830 ctor of T, but looks like a cast to function returning T
4831 without a dependent expression. */
4832 if (!cp_parser_error_occurred (parser))
4833 expr = cp_parser_simple_cast_expression (parser);
4835 if (cp_parser_parse_definitely (parser))
4837 /* Warn about old-style casts, if so requested. */
4838 if (warn_old_style_cast
4839 && !in_system_header
4840 && !VOID_TYPE_P (type)
4841 && current_lang_name != lang_name_c)
4842 warning ("use of old-style cast");
4844 /* Only type conversions to integral or enumeration types
4845 can be used in constant-expressions. */
4846 if (parser->integral_constant_expression_p
4847 && !dependent_type_p (type)
4848 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type)
4849 && (cp_parser_non_integral_constant_expression
4851 "a casts to a type other than an integral or "
4852 "enumeration type")))
4853 return error_mark_node;
4855 /* Perform the cast. */
4856 expr = build_c_cast (type, expr);
4861 /* If we get here, then it's not a cast, so it must be a
4862 unary-expression. */
4863 return cp_parser_unary_expression (parser, address_p);
4866 /* Parse a pm-expression.
4870 pm-expression .* cast-expression
4871 pm-expression ->* cast-expression
4873 Returns a representation of the expression. */
4876 cp_parser_pm_expression (cp_parser* parser)
4878 static const cp_parser_token_tree_map map = {
4879 { CPP_DEREF_STAR, MEMBER_REF },
4880 { CPP_DOT_STAR, DOTSTAR_EXPR },
4881 { CPP_EOF, ERROR_MARK }
4884 return cp_parser_binary_expression (parser, map,
4885 cp_parser_simple_cast_expression);
4888 /* Parse a multiplicative-expression.
4890 mulitplicative-expression:
4892 multiplicative-expression * pm-expression
4893 multiplicative-expression / pm-expression
4894 multiplicative-expression % pm-expression
4896 Returns a representation of the expression. */
4899 cp_parser_multiplicative_expression (cp_parser* parser)
4901 static const cp_parser_token_tree_map map = {
4902 { CPP_MULT, MULT_EXPR },
4903 { CPP_DIV, TRUNC_DIV_EXPR },
4904 { CPP_MOD, TRUNC_MOD_EXPR },
4905 { CPP_EOF, ERROR_MARK }
4908 return cp_parser_binary_expression (parser,
4910 cp_parser_pm_expression);
4913 /* Parse an additive-expression.
4915 additive-expression:
4916 multiplicative-expression
4917 additive-expression + multiplicative-expression
4918 additive-expression - multiplicative-expression
4920 Returns a representation of the expression. */
4923 cp_parser_additive_expression (cp_parser* parser)
4925 static const cp_parser_token_tree_map map = {
4926 { CPP_PLUS, PLUS_EXPR },
4927 { CPP_MINUS, MINUS_EXPR },
4928 { CPP_EOF, ERROR_MARK }
4931 return cp_parser_binary_expression (parser,
4933 cp_parser_multiplicative_expression);
4936 /* Parse a shift-expression.
4940 shift-expression << additive-expression
4941 shift-expression >> additive-expression
4943 Returns a representation of the expression. */
4946 cp_parser_shift_expression (cp_parser* parser)
4948 static const cp_parser_token_tree_map map = {
4949 { CPP_LSHIFT, LSHIFT_EXPR },
4950 { CPP_RSHIFT, RSHIFT_EXPR },
4951 { CPP_EOF, ERROR_MARK }
4954 return cp_parser_binary_expression (parser,
4956 cp_parser_additive_expression);
4959 /* Parse a relational-expression.
4961 relational-expression:
4963 relational-expression < shift-expression
4964 relational-expression > shift-expression
4965 relational-expression <= shift-expression
4966 relational-expression >= shift-expression
4970 relational-expression:
4971 relational-expression <? shift-expression
4972 relational-expression >? shift-expression
4974 Returns a representation of the expression. */
4977 cp_parser_relational_expression (cp_parser* parser)
4979 static const cp_parser_token_tree_map map = {
4980 { CPP_LESS, LT_EXPR },
4981 { CPP_GREATER, GT_EXPR },
4982 { CPP_LESS_EQ, LE_EXPR },
4983 { CPP_GREATER_EQ, GE_EXPR },
4984 { CPP_MIN, MIN_EXPR },
4985 { CPP_MAX, MAX_EXPR },
4986 { CPP_EOF, ERROR_MARK }
4989 return cp_parser_binary_expression (parser,
4991 cp_parser_shift_expression);
4994 /* Parse an equality-expression.
4996 equality-expression:
4997 relational-expression
4998 equality-expression == relational-expression
4999 equality-expression != relational-expression
5001 Returns a representation of the expression. */
5004 cp_parser_equality_expression (cp_parser* parser)
5006 static const cp_parser_token_tree_map map = {
5007 { CPP_EQ_EQ, EQ_EXPR },
5008 { CPP_NOT_EQ, NE_EXPR },
5009 { CPP_EOF, ERROR_MARK }
5012 return cp_parser_binary_expression (parser,
5014 cp_parser_relational_expression);
5017 /* Parse an and-expression.
5021 and-expression & equality-expression
5023 Returns a representation of the expression. */
5026 cp_parser_and_expression (cp_parser* parser)
5028 static const cp_parser_token_tree_map map = {
5029 { CPP_AND, BIT_AND_EXPR },
5030 { CPP_EOF, ERROR_MARK }
5033 return cp_parser_binary_expression (parser,
5035 cp_parser_equality_expression);
5038 /* Parse an exclusive-or-expression.
5040 exclusive-or-expression:
5042 exclusive-or-expression ^ and-expression
5044 Returns a representation of the expression. */
5047 cp_parser_exclusive_or_expression (cp_parser* parser)
5049 static const cp_parser_token_tree_map map = {
5050 { CPP_XOR, BIT_XOR_EXPR },
5051 { CPP_EOF, ERROR_MARK }
5054 return cp_parser_binary_expression (parser,
5056 cp_parser_and_expression);
5060 /* Parse an inclusive-or-expression.
5062 inclusive-or-expression:
5063 exclusive-or-expression
5064 inclusive-or-expression | exclusive-or-expression
5066 Returns a representation of the expression. */
5069 cp_parser_inclusive_or_expression (cp_parser* parser)
5071 static const cp_parser_token_tree_map map = {
5072 { CPP_OR, BIT_IOR_EXPR },
5073 { CPP_EOF, ERROR_MARK }
5076 return cp_parser_binary_expression (parser,
5078 cp_parser_exclusive_or_expression);
5081 /* Parse a logical-and-expression.
5083 logical-and-expression:
5084 inclusive-or-expression
5085 logical-and-expression && inclusive-or-expression
5087 Returns a representation of the expression. */
5090 cp_parser_logical_and_expression (cp_parser* parser)
5092 static const cp_parser_token_tree_map map = {
5093 { CPP_AND_AND, TRUTH_ANDIF_EXPR },
5094 { CPP_EOF, ERROR_MARK }
5097 return cp_parser_binary_expression (parser,
5099 cp_parser_inclusive_or_expression);
5102 /* Parse a logical-or-expression.
5104 logical-or-expression:
5105 logical-and-expression
5106 logical-or-expression || logical-and-expression
5108 Returns a representation of the expression. */
5111 cp_parser_logical_or_expression (cp_parser* parser)
5113 static const cp_parser_token_tree_map map = {
5114 { CPP_OR_OR, TRUTH_ORIF_EXPR },
5115 { CPP_EOF, ERROR_MARK }
5118 return cp_parser_binary_expression (parser,
5120 cp_parser_logical_and_expression);
5123 /* Parse the `? expression : assignment-expression' part of a
5124 conditional-expression. The LOGICAL_OR_EXPR is the
5125 logical-or-expression that started the conditional-expression.
5126 Returns a representation of the entire conditional-expression.
5128 This routine is used by cp_parser_assignment_expression.
5130 ? expression : assignment-expression
5134 ? : assignment-expression */
5137 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
5140 tree assignment_expr;
5142 /* Consume the `?' token. */
5143 cp_lexer_consume_token (parser->lexer);
5144 if (cp_parser_allow_gnu_extensions_p (parser)
5145 && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
5146 /* Implicit true clause. */
5149 /* Parse the expression. */
5150 expr = cp_parser_expression (parser);
5152 /* The next token should be a `:'. */
5153 cp_parser_require (parser, CPP_COLON, "`:'");
5154 /* Parse the assignment-expression. */
5155 assignment_expr = cp_parser_assignment_expression (parser);
5157 /* Build the conditional-expression. */
5158 return build_x_conditional_expr (logical_or_expr,
5163 /* Parse an assignment-expression.
5165 assignment-expression:
5166 conditional-expression
5167 logical-or-expression assignment-operator assignment_expression
5170 Returns a representation for the expression. */
5173 cp_parser_assignment_expression (cp_parser* parser)
5177 /* If the next token is the `throw' keyword, then we're looking at
5178 a throw-expression. */
5179 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
5180 expr = cp_parser_throw_expression (parser);
5181 /* Otherwise, it must be that we are looking at a
5182 logical-or-expression. */
5185 /* Parse the logical-or-expression. */
5186 expr = cp_parser_logical_or_expression (parser);
5187 /* If the next token is a `?' then we're actually looking at a
5188 conditional-expression. */
5189 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
5190 return cp_parser_question_colon_clause (parser, expr);
5193 enum tree_code assignment_operator;
5195 /* If it's an assignment-operator, we're using the second
5198 = cp_parser_assignment_operator_opt (parser);
5199 if (assignment_operator != ERROR_MARK)
5203 /* Parse the right-hand side of the assignment. */
5204 rhs = cp_parser_assignment_expression (parser);
5205 /* An assignment may not appear in a
5206 constant-expression. */
5207 if (cp_parser_non_integral_constant_expression (parser,
5209 return error_mark_node;
5210 /* Build the assignment expression. */
5211 expr = build_x_modify_expr (expr,
5212 assignment_operator,
5221 /* Parse an (optional) assignment-operator.
5223 assignment-operator: one of
5224 = *= /= %= += -= >>= <<= &= ^= |=
5228 assignment-operator: one of
5231 If the next token is an assignment operator, the corresponding tree
5232 code is returned, and the token is consumed. For example, for
5233 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
5234 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
5235 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
5236 operator, ERROR_MARK is returned. */
5238 static enum tree_code
5239 cp_parser_assignment_operator_opt (cp_parser* parser)
5244 /* Peek at the next toen. */
5245 token = cp_lexer_peek_token (parser->lexer);
5247 switch (token->type)
5258 op = TRUNC_DIV_EXPR;
5262 op = TRUNC_MOD_EXPR;
5302 /* Nothing else is an assignment operator. */
5306 /* If it was an assignment operator, consume it. */
5307 if (op != ERROR_MARK)
5308 cp_lexer_consume_token (parser->lexer);
5313 /* Parse an expression.
5316 assignment-expression
5317 expression , assignment-expression
5319 Returns a representation of the expression. */
5322 cp_parser_expression (cp_parser* parser)
5324 tree expression = NULL_TREE;
5328 tree assignment_expression;
5330 /* Parse the next assignment-expression. */
5331 assignment_expression
5332 = cp_parser_assignment_expression (parser);
5333 /* If this is the first assignment-expression, we can just
5336 expression = assignment_expression;
5338 expression = build_x_compound_expr (expression,
5339 assignment_expression);
5340 /* If the next token is not a comma, then we are done with the
5342 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5344 /* Consume the `,'. */
5345 cp_lexer_consume_token (parser->lexer);
5346 /* A comma operator cannot appear in a constant-expression. */
5347 if (cp_parser_non_integral_constant_expression (parser,
5348 "a comma operator"))
5349 expression = error_mark_node;
5355 /* Parse a constant-expression.
5357 constant-expression:
5358 conditional-expression
5360 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
5361 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
5362 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
5363 is false, NON_CONSTANT_P should be NULL. */
5366 cp_parser_constant_expression (cp_parser* parser,
5367 bool allow_non_constant_p,
5368 bool *non_constant_p)
5370 bool saved_integral_constant_expression_p;
5371 bool saved_allow_non_integral_constant_expression_p;
5372 bool saved_non_integral_constant_expression_p;
5375 /* It might seem that we could simply parse the
5376 conditional-expression, and then check to see if it were
5377 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
5378 one that the compiler can figure out is constant, possibly after
5379 doing some simplifications or optimizations. The standard has a
5380 precise definition of constant-expression, and we must honor
5381 that, even though it is somewhat more restrictive.
5387 is not a legal declaration, because `(2, 3)' is not a
5388 constant-expression. The `,' operator is forbidden in a
5389 constant-expression. However, GCC's constant-folding machinery
5390 will fold this operation to an INTEGER_CST for `3'. */
5392 /* Save the old settings. */
5393 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
5394 saved_allow_non_integral_constant_expression_p
5395 = parser->allow_non_integral_constant_expression_p;
5396 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
5397 /* We are now parsing a constant-expression. */
5398 parser->integral_constant_expression_p = true;
5399 parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
5400 parser->non_integral_constant_expression_p = false;
5401 /* Although the grammar says "conditional-expression", we parse an
5402 "assignment-expression", which also permits "throw-expression"
5403 and the use of assignment operators. In the case that
5404 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
5405 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
5406 actually essential that we look for an assignment-expression.
5407 For example, cp_parser_initializer_clauses uses this function to
5408 determine whether a particular assignment-expression is in fact
5410 expression = cp_parser_assignment_expression (parser);
5411 /* Restore the old settings. */
5412 parser->integral_constant_expression_p = saved_integral_constant_expression_p;
5413 parser->allow_non_integral_constant_expression_p
5414 = saved_allow_non_integral_constant_expression_p;
5415 if (allow_non_constant_p)
5416 *non_constant_p = parser->non_integral_constant_expression_p;
5417 parser->non_integral_constant_expression_p = saved_non_integral_constant_expression_p;
5422 /* Statements [gram.stmt.stmt] */
5424 /* Parse a statement.
5428 expression-statement
5433 declaration-statement
5437 cp_parser_statement (cp_parser* parser, bool in_statement_expr_p)
5441 int statement_line_number;
5443 /* There is no statement yet. */
5444 statement = NULL_TREE;
5445 /* Peek at the next token. */
5446 token = cp_lexer_peek_token (parser->lexer);
5447 /* Remember the line number of the first token in the statement. */
5448 statement_line_number = token->location.line;
5449 /* If this is a keyword, then that will often determine what kind of
5450 statement we have. */
5451 if (token->type == CPP_KEYWORD)
5453 enum rid keyword = token->keyword;
5459 statement = cp_parser_labeled_statement (parser,
5460 in_statement_expr_p);
5465 statement = cp_parser_selection_statement (parser);
5471 statement = cp_parser_iteration_statement (parser);
5478 statement = cp_parser_jump_statement (parser);
5482 statement = cp_parser_try_block (parser);
5486 /* It might be a keyword like `int' that can start a
5487 declaration-statement. */
5491 else if (token->type == CPP_NAME)
5493 /* If the next token is a `:', then we are looking at a
5494 labeled-statement. */
5495 token = cp_lexer_peek_nth_token (parser->lexer, 2);
5496 if (token->type == CPP_COLON)
5497 statement = cp_parser_labeled_statement (parser, in_statement_expr_p);
5499 /* Anything that starts with a `{' must be a compound-statement. */
5500 else if (token->type == CPP_OPEN_BRACE)
5501 statement = cp_parser_compound_statement (parser, false);
5503 /* Everything else must be a declaration-statement or an
5504 expression-statement. Try for the declaration-statement
5505 first, unless we are looking at a `;', in which case we know that
5506 we have an expression-statement. */
5509 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
5511 cp_parser_parse_tentatively (parser);
5512 /* Try to parse the declaration-statement. */
5513 cp_parser_declaration_statement (parser);
5514 /* If that worked, we're done. */
5515 if (cp_parser_parse_definitely (parser))
5518 /* Look for an expression-statement instead. */
5519 statement = cp_parser_expression_statement (parser, in_statement_expr_p);
5522 /* Set the line number for the statement. */
5523 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
5524 STMT_LINENO (statement) = statement_line_number;
5527 /* Parse a labeled-statement.
5530 identifier : statement
5531 case constant-expression : statement
5537 case constant-expression ... constant-expression : statement
5539 Returns the new CASE_LABEL, for a `case' or `default' label. For
5540 an ordinary label, returns a LABEL_STMT. */
5543 cp_parser_labeled_statement (cp_parser* parser, bool in_statement_expr_p)
5546 tree statement = error_mark_node;
5548 /* The next token should be an identifier. */
5549 token = cp_lexer_peek_token (parser->lexer);
5550 if (token->type != CPP_NAME
5551 && token->type != CPP_KEYWORD)
5553 cp_parser_error (parser, "expected labeled-statement");
5554 return error_mark_node;
5557 switch (token->keyword)
5564 /* Consume the `case' token. */
5565 cp_lexer_consume_token (parser->lexer);
5566 /* Parse the constant-expression. */
5567 expr = cp_parser_constant_expression (parser,
5568 /*allow_non_constant_p=*/false,
5571 ellipsis = cp_lexer_peek_token (parser->lexer);
5572 if (ellipsis->type == CPP_ELLIPSIS)
5574 /* Consume the `...' token. */
5575 cp_lexer_consume_token (parser->lexer);
5577 cp_parser_constant_expression (parser,
5578 /*allow_non_constant_p=*/false,
5580 /* We don't need to emit warnings here, as the common code
5581 will do this for us. */
5584 expr_hi = NULL_TREE;
5586 if (!parser->in_switch_statement_p)
5587 error ("case label `%E' not within a switch statement", expr);
5589 statement = finish_case_label (expr, expr_hi);
5594 /* Consume the `default' token. */
5595 cp_lexer_consume_token (parser->lexer);
5596 if (!parser->in_switch_statement_p)
5597 error ("case label not within a switch statement");
5599 statement = finish_case_label (NULL_TREE, NULL_TREE);
5603 /* Anything else must be an ordinary label. */
5604 statement = finish_label_stmt (cp_parser_identifier (parser));
5608 /* Require the `:' token. */
5609 cp_parser_require (parser, CPP_COLON, "`:'");
5610 /* Parse the labeled statement. */
5611 cp_parser_statement (parser, in_statement_expr_p);
5613 /* Return the label, in the case of a `case' or `default' label. */
5617 /* Parse an expression-statement.
5619 expression-statement:
5622 Returns the new EXPR_STMT -- or NULL_TREE if the expression
5623 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
5624 indicates whether this expression-statement is part of an
5625 expression statement. */
5628 cp_parser_expression_statement (cp_parser* parser, bool in_statement_expr_p)
5630 tree statement = NULL_TREE;
5632 /* If the next token is a ';', then there is no expression
5634 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
5635 statement = cp_parser_expression (parser);
5637 /* Consume the final `;'. */
5638 cp_parser_consume_semicolon_at_end_of_statement (parser);
5640 if (in_statement_expr_p
5641 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
5643 /* This is the final expression statement of a statement
5645 statement = finish_stmt_expr_expr (statement);
5648 statement = finish_expr_stmt (statement);
5655 /* Parse a compound-statement.
5658 { statement-seq [opt] }
5660 Returns a COMPOUND_STMT representing the statement. */
5663 cp_parser_compound_statement (cp_parser *parser, bool in_statement_expr_p)
5667 /* Consume the `{'. */
5668 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
5669 return error_mark_node;
5670 /* Begin the compound-statement. */
5671 compound_stmt = begin_compound_stmt (/*has_no_scope=*/false);
5672 /* Parse an (optional) statement-seq. */
5673 cp_parser_statement_seq_opt (parser, in_statement_expr_p);
5674 /* Finish the compound-statement. */
5675 finish_compound_stmt (compound_stmt);
5676 /* Consume the `}'. */
5677 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
5679 return compound_stmt;
5682 /* Parse an (optional) statement-seq.
5686 statement-seq [opt] statement */
5689 cp_parser_statement_seq_opt (cp_parser* parser, bool in_statement_expr_p)
5691 /* Scan statements until there aren't any more. */
5694 /* If we're looking at a `}', then we've run out of statements. */
5695 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE)
5696 || cp_lexer_next_token_is (parser->lexer, CPP_EOF))
5699 /* Parse the statement. */
5700 cp_parser_statement (parser, in_statement_expr_p);
5704 /* Parse a selection-statement.
5706 selection-statement:
5707 if ( condition ) statement
5708 if ( condition ) statement else statement
5709 switch ( condition ) statement
5711 Returns the new IF_STMT or SWITCH_STMT. */
5714 cp_parser_selection_statement (cp_parser* parser)
5719 /* Peek at the next token. */
5720 token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
5722 /* See what kind of keyword it is. */
5723 keyword = token->keyword;
5732 /* Look for the `('. */
5733 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
5735 cp_parser_skip_to_end_of_statement (parser);
5736 return error_mark_node;
5739 /* Begin the selection-statement. */
5740 if (keyword == RID_IF)
5741 statement = begin_if_stmt ();
5743 statement = begin_switch_stmt ();
5745 /* Parse the condition. */
5746 condition = cp_parser_condition (parser);
5747 /* Look for the `)'. */
5748 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
5749 cp_parser_skip_to_closing_parenthesis (parser, true, false,
5750 /*consume_paren=*/true);
5752 if (keyword == RID_IF)
5756 /* Add the condition. */
5757 finish_if_stmt_cond (condition, statement);
5759 /* Parse the then-clause. */
5760 then_stmt = cp_parser_implicitly_scoped_statement (parser);
5761 finish_then_clause (statement);
5763 /* If the next token is `else', parse the else-clause. */
5764 if (cp_lexer_next_token_is_keyword (parser->lexer,
5769 /* Consume the `else' keyword. */
5770 cp_lexer_consume_token (parser->lexer);
5771 /* Parse the else-clause. */
5773 = cp_parser_implicitly_scoped_statement (parser);
5774 finish_else_clause (statement);
5777 /* Now we're all done with the if-statement. */
5783 bool in_switch_statement_p;
5785 /* Add the condition. */
5786 finish_switch_cond (condition, statement);
5788 /* Parse the body of the switch-statement. */
5789 in_switch_statement_p = parser->in_switch_statement_p;
5790 parser->in_switch_statement_p = true;
5791 body = cp_parser_implicitly_scoped_statement (parser);
5792 parser->in_switch_statement_p = in_switch_statement_p;
5794 /* Now we're all done with the switch-statement. */
5795 finish_switch_stmt (statement);
5803 cp_parser_error (parser, "expected selection-statement");
5804 return error_mark_node;
5808 /* Parse a condition.
5812 type-specifier-seq declarator = assignment-expression
5817 type-specifier-seq declarator asm-specification [opt]
5818 attributes [opt] = assignment-expression
5820 Returns the expression that should be tested. */
5823 cp_parser_condition (cp_parser* parser)
5825 tree type_specifiers;
5826 const char *saved_message;
5828 /* Try the declaration first. */
5829 cp_parser_parse_tentatively (parser);
5830 /* New types are not allowed in the type-specifier-seq for a
5832 saved_message = parser->type_definition_forbidden_message;
5833 parser->type_definition_forbidden_message
5834 = "types may not be defined in conditions";
5835 /* Parse the type-specifier-seq. */
5836 type_specifiers = cp_parser_type_specifier_seq (parser);
5837 /* Restore the saved message. */
5838 parser->type_definition_forbidden_message = saved_message;
5839 /* If all is well, we might be looking at a declaration. */
5840 if (!cp_parser_error_occurred (parser))
5843 tree asm_specification;
5846 tree initializer = NULL_TREE;
5848 /* Parse the declarator. */
5849 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
5850 /*ctor_dtor_or_conv_p=*/NULL,
5851 /*parenthesized_p=*/NULL,
5852 /*member_p=*/false);
5853 /* Parse the attributes. */
5854 attributes = cp_parser_attributes_opt (parser);
5855 /* Parse the asm-specification. */
5856 asm_specification = cp_parser_asm_specification_opt (parser);
5857 /* If the next token is not an `=', then we might still be
5858 looking at an expression. For example:
5862 looks like a decl-specifier-seq and a declarator -- but then
5863 there is no `=', so this is an expression. */
5864 cp_parser_require (parser, CPP_EQ, "`='");
5865 /* If we did see an `=', then we are looking at a declaration
5867 if (cp_parser_parse_definitely (parser))
5869 /* Create the declaration. */
5870 decl = start_decl (declarator, type_specifiers,
5871 /*initialized_p=*/true,
5872 attributes, /*prefix_attributes=*/NULL_TREE);
5873 /* Parse the assignment-expression. */
5874 initializer = cp_parser_assignment_expression (parser);
5876 /* Process the initializer. */
5877 cp_finish_decl (decl,
5880 LOOKUP_ONLYCONVERTING);
5882 return convert_from_reference (decl);
5885 /* If we didn't even get past the declarator successfully, we are
5886 definitely not looking at a declaration. */
5888 cp_parser_abort_tentative_parse (parser);
5890 /* Otherwise, we are looking at an expression. */
5891 return cp_parser_expression (parser);
5894 /* Parse an iteration-statement.
5896 iteration-statement:
5897 while ( condition ) statement
5898 do statement while ( expression ) ;
5899 for ( for-init-statement condition [opt] ; expression [opt] )
5902 Returns the new WHILE_STMT, DO_STMT, or FOR_STMT. */
5905 cp_parser_iteration_statement (cp_parser* parser)
5910 bool in_iteration_statement_p;
5913 /* Peek at the next token. */
5914 token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
5916 return error_mark_node;
5918 /* Remember whether or not we are already within an iteration
5920 in_iteration_statement_p = parser->in_iteration_statement_p;
5922 /* See what kind of keyword it is. */
5923 keyword = token->keyword;
5930 /* Begin the while-statement. */
5931 statement = begin_while_stmt ();
5932 /* Look for the `('. */
5933 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
5934 /* Parse the condition. */
5935 condition = cp_parser_condition (parser);
5936 finish_while_stmt_cond (condition, statement);
5937 /* Look for the `)'. */
5938 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5939 /* Parse the dependent statement. */
5940 parser->in_iteration_statement_p = true;
5941 cp_parser_already_scoped_statement (parser);
5942 parser->in_iteration_statement_p = in_iteration_statement_p;
5943 /* We're done with the while-statement. */
5944 finish_while_stmt (statement);
5952 /* Begin the do-statement. */
5953 statement = begin_do_stmt ();
5954 /* Parse the body of the do-statement. */
5955 parser->in_iteration_statement_p = true;
5956 cp_parser_implicitly_scoped_statement (parser);
5957 parser->in_iteration_statement_p = in_iteration_statement_p;
5958 finish_do_body (statement);
5959 /* Look for the `while' keyword. */
5960 cp_parser_require_keyword (parser, RID_WHILE, "`while'");
5961 /* Look for the `('. */
5962 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
5963 /* Parse the expression. */
5964 expression = cp_parser_expression (parser);
5965 /* We're done with the do-statement. */
5966 finish_do_stmt (expression, statement);
5967 /* Look for the `)'. */
5968 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5969 /* Look for the `;'. */
5970 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
5976 tree condition = NULL_TREE;
5977 tree expression = NULL_TREE;
5979 /* Begin the for-statement. */
5980 statement = begin_for_stmt ();
5981 /* Look for the `('. */
5982 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
5983 /* Parse the initialization. */
5984 cp_parser_for_init_statement (parser);
5985 finish_for_init_stmt (statement);
5987 /* If there's a condition, process it. */
5988 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
5989 condition = cp_parser_condition (parser);
5990 finish_for_cond (condition, statement);
5991 /* Look for the `;'. */
5992 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
5994 /* If there's an expression, process it. */
5995 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
5996 expression = cp_parser_expression (parser);
5997 finish_for_expr (expression, statement);
5998 /* Look for the `)'. */
5999 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6001 /* Parse the body of the for-statement. */
6002 parser->in_iteration_statement_p = true;
6003 cp_parser_already_scoped_statement (parser);
6004 parser->in_iteration_statement_p = in_iteration_statement_p;
6006 /* We're done with the for-statement. */
6007 finish_for_stmt (statement);
6012 cp_parser_error (parser, "expected iteration-statement");
6013 statement = error_mark_node;
6020 /* Parse a for-init-statement.
6023 expression-statement
6024 simple-declaration */
6027 cp_parser_for_init_statement (cp_parser* parser)
6029 /* If the next token is a `;', then we have an empty
6030 expression-statement. Grammatically, this is also a
6031 simple-declaration, but an invalid one, because it does not
6032 declare anything. Therefore, if we did not handle this case
6033 specially, we would issue an error message about an invalid
6035 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6037 /* We're going to speculatively look for a declaration, falling back
6038 to an expression, if necessary. */
6039 cp_parser_parse_tentatively (parser);
6040 /* Parse the declaration. */
6041 cp_parser_simple_declaration (parser,
6042 /*function_definition_allowed_p=*/false);
6043 /* If the tentative parse failed, then we shall need to look for an
6044 expression-statement. */
6045 if (cp_parser_parse_definitely (parser))
6049 cp_parser_expression_statement (parser, false);
6052 /* Parse a jump-statement.
6057 return expression [opt] ;
6065 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_STMT, or
6069 cp_parser_jump_statement (cp_parser* parser)
6071 tree statement = error_mark_node;
6075 /* Peek at the next token. */
6076 token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
6078 return error_mark_node;
6080 /* See what kind of keyword it is. */
6081 keyword = token->keyword;
6085 if (!parser->in_switch_statement_p
6086 && !parser->in_iteration_statement_p)
6088 error ("break statement not within loop or switch");
6089 statement = error_mark_node;
6092 statement = finish_break_stmt ();
6093 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6097 if (!parser->in_iteration_statement_p)
6099 error ("continue statement not within a loop");
6100 statement = error_mark_node;
6103 statement = finish_continue_stmt ();
6104 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6111 /* If the next token is a `;', then there is no
6113 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6114 expr = cp_parser_expression (parser);
6117 /* Build the return-statement. */
6118 statement = finish_return_stmt (expr);
6119 /* Look for the final `;'. */
6120 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6125 /* Create the goto-statement. */
6126 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
6128 /* Issue a warning about this use of a GNU extension. */
6130 pedwarn ("ISO C++ forbids computed gotos");
6131 /* Consume the '*' token. */
6132 cp_lexer_consume_token (parser->lexer);
6133 /* Parse the dependent expression. */
6134 finish_goto_stmt (cp_parser_expression (parser));
6137 finish_goto_stmt (cp_parser_identifier (parser));
6138 /* Look for the final `;'. */
6139 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6143 cp_parser_error (parser, "expected jump-statement");
6150 /* Parse a declaration-statement.
6152 declaration-statement:
6153 block-declaration */
6156 cp_parser_declaration_statement (cp_parser* parser)
6158 /* Parse the block-declaration. */
6159 cp_parser_block_declaration (parser, /*statement_p=*/true);
6161 /* Finish off the statement. */
6165 /* Some dependent statements (like `if (cond) statement'), are
6166 implicitly in their own scope. In other words, if the statement is
6167 a single statement (as opposed to a compound-statement), it is
6168 none-the-less treated as if it were enclosed in braces. Any
6169 declarations appearing in the dependent statement are out of scope
6170 after control passes that point. This function parses a statement,
6171 but ensures that is in its own scope, even if it is not a
6174 Returns the new statement. */
6177 cp_parser_implicitly_scoped_statement (cp_parser* parser)
6181 /* If the token is not a `{', then we must take special action. */
6182 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
6184 /* Create a compound-statement. */
6185 statement = begin_compound_stmt (/*has_no_scope=*/false);
6186 /* Parse the dependent-statement. */
6187 cp_parser_statement (parser, false);
6188 /* Finish the dummy compound-statement. */
6189 finish_compound_stmt (statement);
6191 /* Otherwise, we simply parse the statement directly. */
6193 statement = cp_parser_compound_statement (parser, false);
6195 /* Return the statement. */
6199 /* For some dependent statements (like `while (cond) statement'), we
6200 have already created a scope. Therefore, even if the dependent
6201 statement is a compound-statement, we do not want to create another
6205 cp_parser_already_scoped_statement (cp_parser* parser)
6207 /* If the token is not a `{', then we must take special action. */
6208 if (cp_lexer_next_token_is_not(parser->lexer, CPP_OPEN_BRACE))
6212 /* Create a compound-statement. */
6213 statement = begin_compound_stmt (/*has_no_scope=*/true);
6214 /* Parse the dependent-statement. */
6215 cp_parser_statement (parser, false);
6216 /* Finish the dummy compound-statement. */
6217 finish_compound_stmt (statement);
6219 /* Otherwise, we simply parse the statement directly. */
6221 cp_parser_statement (parser, false);
6224 /* Declarations [gram.dcl.dcl] */
6226 /* Parse an optional declaration-sequence.
6230 declaration-seq declaration */
6233 cp_parser_declaration_seq_opt (cp_parser* parser)
6239 token = cp_lexer_peek_token (parser->lexer);
6241 if (token->type == CPP_CLOSE_BRACE
6242 || token->type == CPP_EOF)
6245 if (token->type == CPP_SEMICOLON)
6247 /* A declaration consisting of a single semicolon is
6248 invalid. Allow it unless we're being pedantic. */
6249 if (pedantic && !in_system_header)
6250 pedwarn ("extra `;'");
6251 cp_lexer_consume_token (parser->lexer);
6255 /* The C lexer modifies PENDING_LANG_CHANGE when it wants the
6256 parser to enter or exit implicit `extern "C"' blocks. */
6257 while (pending_lang_change > 0)
6259 push_lang_context (lang_name_c);
6260 --pending_lang_change;
6262 while (pending_lang_change < 0)
6264 pop_lang_context ();
6265 ++pending_lang_change;
6268 /* Parse the declaration itself. */
6269 cp_parser_declaration (parser);
6273 /* Parse a declaration.
6278 template-declaration
6279 explicit-instantiation
6280 explicit-specialization
6281 linkage-specification
6282 namespace-definition
6287 __extension__ declaration */
6290 cp_parser_declaration (cp_parser* parser)
6296 /* Check for the `__extension__' keyword. */
6297 if (cp_parser_extension_opt (parser, &saved_pedantic))
6299 /* Parse the qualified declaration. */
6300 cp_parser_declaration (parser);
6301 /* Restore the PEDANTIC flag. */
6302 pedantic = saved_pedantic;
6307 /* Try to figure out what kind of declaration is present. */
6308 token1 = *cp_lexer_peek_token (parser->lexer);
6309 if (token1.type != CPP_EOF)
6310 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
6312 /* If the next token is `extern' and the following token is a string
6313 literal, then we have a linkage specification. */
6314 if (token1.keyword == RID_EXTERN
6315 && cp_parser_is_string_literal (&token2))
6316 cp_parser_linkage_specification (parser);
6317 /* If the next token is `template', then we have either a template
6318 declaration, an explicit instantiation, or an explicit
6320 else if (token1.keyword == RID_TEMPLATE)
6322 /* `template <>' indicates a template specialization. */
6323 if (token2.type == CPP_LESS
6324 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
6325 cp_parser_explicit_specialization (parser);
6326 /* `template <' indicates a template declaration. */
6327 else if (token2.type == CPP_LESS)
6328 cp_parser_template_declaration (parser, /*member_p=*/false);
6329 /* Anything else must be an explicit instantiation. */
6331 cp_parser_explicit_instantiation (parser);
6333 /* If the next token is `export', then we have a template
6335 else if (token1.keyword == RID_EXPORT)
6336 cp_parser_template_declaration (parser, /*member_p=*/false);
6337 /* If the next token is `extern', 'static' or 'inline' and the one
6338 after that is `template', we have a GNU extended explicit
6339 instantiation directive. */
6340 else if (cp_parser_allow_gnu_extensions_p (parser)
6341 && (token1.keyword == RID_EXTERN
6342 || token1.keyword == RID_STATIC
6343 || token1.keyword == RID_INLINE)
6344 && token2.keyword == RID_TEMPLATE)
6345 cp_parser_explicit_instantiation (parser);
6346 /* If the next token is `namespace', check for a named or unnamed
6347 namespace definition. */
6348 else if (token1.keyword == RID_NAMESPACE
6349 && (/* A named namespace definition. */
6350 (token2.type == CPP_NAME
6351 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
6353 /* An unnamed namespace definition. */
6354 || token2.type == CPP_OPEN_BRACE))
6355 cp_parser_namespace_definition (parser);
6356 /* We must have either a block declaration or a function
6359 /* Try to parse a block-declaration, or a function-definition. */
6360 cp_parser_block_declaration (parser, /*statement_p=*/false);
6363 /* Parse a block-declaration.
6368 namespace-alias-definition
6375 __extension__ block-declaration
6378 If STATEMENT_P is TRUE, then this block-declaration is occurring as
6379 part of a declaration-statement. */
6382 cp_parser_block_declaration (cp_parser *parser,
6388 /* Check for the `__extension__' keyword. */
6389 if (cp_parser_extension_opt (parser, &saved_pedantic))
6391 /* Parse the qualified declaration. */
6392 cp_parser_block_declaration (parser, statement_p);
6393 /* Restore the PEDANTIC flag. */
6394 pedantic = saved_pedantic;
6399 /* Peek at the next token to figure out which kind of declaration is
6401 token1 = cp_lexer_peek_token (parser->lexer);
6403 /* If the next keyword is `asm', we have an asm-definition. */
6404 if (token1->keyword == RID_ASM)
6407 cp_parser_commit_to_tentative_parse (parser);
6408 cp_parser_asm_definition (parser);
6410 /* If the next keyword is `namespace', we have a
6411 namespace-alias-definition. */
6412 else if (token1->keyword == RID_NAMESPACE)
6413 cp_parser_namespace_alias_definition (parser);
6414 /* If the next keyword is `using', we have either a
6415 using-declaration or a using-directive. */
6416 else if (token1->keyword == RID_USING)
6421 cp_parser_commit_to_tentative_parse (parser);
6422 /* If the token after `using' is `namespace', then we have a
6424 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
6425 if (token2->keyword == RID_NAMESPACE)
6426 cp_parser_using_directive (parser);
6427 /* Otherwise, it's a using-declaration. */
6429 cp_parser_using_declaration (parser);
6431 /* If the next keyword is `__label__' we have a label declaration. */
6432 else if (token1->keyword == RID_LABEL)
6435 cp_parser_commit_to_tentative_parse (parser);
6436 cp_parser_label_declaration (parser);
6438 /* Anything else must be a simple-declaration. */
6440 cp_parser_simple_declaration (parser, !statement_p);
6443 /* Parse a simple-declaration.
6446 decl-specifier-seq [opt] init-declarator-list [opt] ;
6448 init-declarator-list:
6450 init-declarator-list , init-declarator
6452 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
6453 function-definition as a simple-declaration. */
6456 cp_parser_simple_declaration (cp_parser* parser,
6457 bool function_definition_allowed_p)
6459 tree decl_specifiers;
6461 int declares_class_or_enum;
6462 bool saw_declarator;
6464 /* Defer access checks until we know what is being declared; the
6465 checks for names appearing in the decl-specifier-seq should be
6466 done as if we were in the scope of the thing being declared. */
6467 push_deferring_access_checks (dk_deferred);
6469 /* Parse the decl-specifier-seq. We have to keep track of whether
6470 or not the decl-specifier-seq declares a named class or
6471 enumeration type, since that is the only case in which the
6472 init-declarator-list is allowed to be empty.
6476 In a simple-declaration, the optional init-declarator-list can be
6477 omitted only when declaring a class or enumeration, that is when
6478 the decl-specifier-seq contains either a class-specifier, an
6479 elaborated-type-specifier, or an enum-specifier. */
6481 = cp_parser_decl_specifier_seq (parser,
6482 CP_PARSER_FLAGS_OPTIONAL,
6484 &declares_class_or_enum);
6485 /* We no longer need to defer access checks. */
6486 stop_deferring_access_checks ();
6488 /* In a block scope, a valid declaration must always have a
6489 decl-specifier-seq. By not trying to parse declarators, we can
6490 resolve the declaration/expression ambiguity more quickly. */
6491 if (!function_definition_allowed_p && !decl_specifiers)
6493 cp_parser_error (parser, "expected declaration");
6497 /* If the next two tokens are both identifiers, the code is
6498 erroneous. The usual cause of this situation is code like:
6502 where "T" should name a type -- but does not. */
6503 if (cp_parser_diagnose_invalid_type_name (parser))
6505 /* If parsing tentatively, we should commit; we really are
6506 looking at a declaration. */
6507 cp_parser_commit_to_tentative_parse (parser);
6512 /* Keep going until we hit the `;' at the end of the simple
6514 saw_declarator = false;
6515 while (cp_lexer_next_token_is_not (parser->lexer,
6519 bool function_definition_p;
6522 saw_declarator = true;
6523 /* Parse the init-declarator. */
6524 decl = cp_parser_init_declarator (parser, decl_specifiers, attributes,
6525 function_definition_allowed_p,
6527 declares_class_or_enum,
6528 &function_definition_p);
6529 /* If an error occurred while parsing tentatively, exit quickly.
6530 (That usually happens when in the body of a function; each
6531 statement is treated as a declaration-statement until proven
6533 if (cp_parser_error_occurred (parser))
6535 /* Handle function definitions specially. */
6536 if (function_definition_p)
6538 /* If the next token is a `,', then we are probably
6539 processing something like:
6543 which is erroneous. */
6544 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
6545 error ("mixing declarations and function-definitions is forbidden");
6546 /* Otherwise, we're done with the list of declarators. */
6549 pop_deferring_access_checks ();
6553 /* The next token should be either a `,' or a `;'. */
6554 token = cp_lexer_peek_token (parser->lexer);
6555 /* If it's a `,', there are more declarators to come. */
6556 if (token->type == CPP_COMMA)
6557 cp_lexer_consume_token (parser->lexer);
6558 /* If it's a `;', we are done. */
6559 else if (token->type == CPP_SEMICOLON)
6561 /* Anything else is an error. */
6564 cp_parser_error (parser, "expected `,' or `;'");
6565 /* Skip tokens until we reach the end of the statement. */
6566 cp_parser_skip_to_end_of_statement (parser);
6567 /* If the next token is now a `;', consume it. */
6568 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
6569 cp_lexer_consume_token (parser->lexer);
6572 /* After the first time around, a function-definition is not
6573 allowed -- even if it was OK at first. For example:
6578 function_definition_allowed_p = false;
6581 /* Issue an error message if no declarators are present, and the
6582 decl-specifier-seq does not itself declare a class or
6584 if (!saw_declarator)
6586 if (cp_parser_declares_only_class_p (parser))
6587 shadow_tag (decl_specifiers);
6588 /* Perform any deferred access checks. */
6589 perform_deferred_access_checks ();
6592 /* Consume the `;'. */
6593 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6596 pop_deferring_access_checks ();
6599 /* Parse a decl-specifier-seq.
6602 decl-specifier-seq [opt] decl-specifier
6605 storage-class-specifier
6616 Returns a TREE_LIST, giving the decl-specifiers in the order they
6617 appear in the source code. The TREE_VALUE of each node is the
6618 decl-specifier. For a keyword (such as `auto' or `friend'), the
6619 TREE_VALUE is simply the corresponding TREE_IDENTIFIER. For the
6620 representation of a type-specifier, see cp_parser_type_specifier.
6622 If there are attributes, they will be stored in *ATTRIBUTES,
6623 represented as described above cp_parser_attributes.
6625 If FRIEND_IS_NOT_CLASS_P is non-NULL, and the `friend' specifier
6626 appears, and the entity that will be a friend is not going to be a
6627 class, then *FRIEND_IS_NOT_CLASS_P will be set to TRUE. Note that
6628 even if *FRIEND_IS_NOT_CLASS_P is FALSE, the entity to which
6629 friendship is granted might not be a class.
6631 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
6634 1: one of the decl-specifiers is an elaborated-type-specifier
6635 (i.e., a type declaration)
6636 2: one of the decl-specifiers is an enum-specifier or a
6637 class-specifier (i.e., a type definition)
6642 cp_parser_decl_specifier_seq (cp_parser* parser,
6643 cp_parser_flags flags,
6645 int* declares_class_or_enum)
6647 tree decl_specs = NULL_TREE;
6648 bool friend_p = false;
6649 bool constructor_possible_p = !parser->in_declarator_p;
6651 /* Assume no class or enumeration type is declared. */
6652 *declares_class_or_enum = 0;
6654 /* Assume there are no attributes. */
6655 *attributes = NULL_TREE;
6657 /* Keep reading specifiers until there are no more to read. */
6660 tree decl_spec = NULL_TREE;
6664 /* Peek at the next token. */
6665 token = cp_lexer_peek_token (parser->lexer);
6666 /* Handle attributes. */
6667 if (token->keyword == RID_ATTRIBUTE)
6669 /* Parse the attributes. */
6670 decl_spec = cp_parser_attributes_opt (parser);
6671 /* Add them to the list. */
6672 *attributes = chainon (*attributes, decl_spec);
6675 /* If the next token is an appropriate keyword, we can simply
6676 add it to the list. */
6677 switch (token->keyword)
6683 error ("duplicate `friend'");
6686 /* The representation of the specifier is simply the
6687 appropriate TREE_IDENTIFIER node. */
6688 decl_spec = token->value;
6689 /* Consume the token. */
6690 cp_lexer_consume_token (parser->lexer);
6693 /* function-specifier:
6700 decl_spec = cp_parser_function_specifier_opt (parser);
6706 /* The representation of the specifier is simply the
6707 appropriate TREE_IDENTIFIER node. */
6708 decl_spec = token->value;
6709 /* Consume the token. */
6710 cp_lexer_consume_token (parser->lexer);
6711 /* A constructor declarator cannot appear in a typedef. */
6712 constructor_possible_p = false;
6713 /* The "typedef" keyword can only occur in a declaration; we
6714 may as well commit at this point. */
6715 cp_parser_commit_to_tentative_parse (parser);
6718 /* storage-class-specifier:
6733 decl_spec = cp_parser_storage_class_specifier_opt (parser);
6740 /* Constructors are a special case. The `S' in `S()' is not a
6741 decl-specifier; it is the beginning of the declarator. */
6742 constructor_p = (!decl_spec
6743 && constructor_possible_p
6744 && cp_parser_constructor_declarator_p (parser,
6747 /* If we don't have a DECL_SPEC yet, then we must be looking at
6748 a type-specifier. */
6749 if (!decl_spec && !constructor_p)
6751 int decl_spec_declares_class_or_enum;
6752 bool is_cv_qualifier;
6755 = cp_parser_type_specifier (parser, flags,
6757 /*is_declaration=*/true,
6758 &decl_spec_declares_class_or_enum,
6761 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
6763 /* If this type-specifier referenced a user-defined type
6764 (a typedef, class-name, etc.), then we can't allow any
6765 more such type-specifiers henceforth.
6769 The longest sequence of decl-specifiers that could
6770 possibly be a type name is taken as the
6771 decl-specifier-seq of a declaration. The sequence shall
6772 be self-consistent as described below.
6776 As a general rule, at most one type-specifier is allowed
6777 in the complete decl-specifier-seq of a declaration. The
6778 only exceptions are the following:
6780 -- const or volatile can be combined with any other
6783 -- signed or unsigned can be combined with char, long,
6791 void g (const int Pc);
6793 Here, Pc is *not* part of the decl-specifier seq; it's
6794 the declarator. Therefore, once we see a type-specifier
6795 (other than a cv-qualifier), we forbid any additional
6796 user-defined types. We *do* still allow things like `int
6797 int' to be considered a decl-specifier-seq, and issue the
6798 error message later. */
6799 if (decl_spec && !is_cv_qualifier)
6800 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
6801 /* A constructor declarator cannot follow a type-specifier. */
6803 constructor_possible_p = false;
6806 /* If we still do not have a DECL_SPEC, then there are no more
6810 /* Issue an error message, unless the entire construct was
6812 if (!(flags & CP_PARSER_FLAGS_OPTIONAL))
6814 cp_parser_error (parser, "expected decl specifier");
6815 return error_mark_node;
6821 /* Add the DECL_SPEC to the list of specifiers. */
6822 if (decl_specs == NULL || TREE_VALUE (decl_specs) != error_mark_node)
6823 decl_specs = tree_cons (NULL_TREE, decl_spec, decl_specs);
6825 /* After we see one decl-specifier, further decl-specifiers are
6827 flags |= CP_PARSER_FLAGS_OPTIONAL;
6830 /* Don't allow a friend specifier with a class definition. */
6831 if (friend_p && (*declares_class_or_enum & 2))
6832 error ("class definition may not be declared a friend");
6834 /* We have built up the DECL_SPECS in reverse order. Return them in
6835 the correct order. */
6836 return nreverse (decl_specs);
6839 /* Parse an (optional) storage-class-specifier.
6841 storage-class-specifier:
6850 storage-class-specifier:
6853 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
6856 cp_parser_storage_class_specifier_opt (cp_parser* parser)
6858 switch (cp_lexer_peek_token (parser->lexer)->keyword)
6866 /* Consume the token. */
6867 return cp_lexer_consume_token (parser->lexer)->value;
6874 /* Parse an (optional) function-specifier.
6881 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
6884 cp_parser_function_specifier_opt (cp_parser* parser)
6886 switch (cp_lexer_peek_token (parser->lexer)->keyword)
6891 /* Consume the token. */
6892 return cp_lexer_consume_token (parser->lexer)->value;
6899 /* Parse a linkage-specification.
6901 linkage-specification:
6902 extern string-literal { declaration-seq [opt] }
6903 extern string-literal declaration */
6906 cp_parser_linkage_specification (cp_parser* parser)
6911 /* Look for the `extern' keyword. */
6912 cp_parser_require_keyword (parser, RID_EXTERN, "`extern'");
6914 /* Peek at the next token. */
6915 token = cp_lexer_peek_token (parser->lexer);
6916 /* If it's not a string-literal, then there's a problem. */
6917 if (!cp_parser_is_string_literal (token))
6919 cp_parser_error (parser, "expected language-name");
6922 /* Consume the token. */
6923 cp_lexer_consume_token (parser->lexer);
6925 /* Transform the literal into an identifier. If the literal is a
6926 wide-character string, or contains embedded NULs, then we can't
6927 handle it as the user wants. */
6928 if (token->type == CPP_WSTRING
6929 || (strlen (TREE_STRING_POINTER (token->value))
6930 != (size_t) (TREE_STRING_LENGTH (token->value) - 1)))
6932 cp_parser_error (parser, "invalid linkage-specification");
6933 /* Assume C++ linkage. */
6934 linkage = get_identifier ("c++");
6936 /* If it's a simple string constant, things are easier. */
6938 linkage = get_identifier (TREE_STRING_POINTER (token->value));
6940 /* We're now using the new linkage. */
6941 push_lang_context (linkage);
6943 /* If the next token is a `{', then we're using the first
6945 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6947 /* Consume the `{' token. */
6948 cp_lexer_consume_token (parser->lexer);
6949 /* Parse the declarations. */
6950 cp_parser_declaration_seq_opt (parser);
6951 /* Look for the closing `}'. */
6952 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
6954 /* Otherwise, there's just one declaration. */
6957 bool saved_in_unbraced_linkage_specification_p;
6959 saved_in_unbraced_linkage_specification_p
6960 = parser->in_unbraced_linkage_specification_p;
6961 parser->in_unbraced_linkage_specification_p = true;
6962 have_extern_spec = true;
6963 cp_parser_declaration (parser);
6964 have_extern_spec = false;
6965 parser->in_unbraced_linkage_specification_p
6966 = saved_in_unbraced_linkage_specification_p;
6969 /* We're done with the linkage-specification. */
6970 pop_lang_context ();
6973 /* Special member functions [gram.special] */
6975 /* Parse a conversion-function-id.
6977 conversion-function-id:
6978 operator conversion-type-id
6980 Returns an IDENTIFIER_NODE representing the operator. */
6983 cp_parser_conversion_function_id (cp_parser* parser)
6987 tree saved_qualifying_scope;
6988 tree saved_object_scope;
6991 /* Look for the `operator' token. */
6992 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
6993 return error_mark_node;
6994 /* When we parse the conversion-type-id, the current scope will be
6995 reset. However, we need that information in able to look up the
6996 conversion function later, so we save it here. */
6997 saved_scope = parser->scope;
6998 saved_qualifying_scope = parser->qualifying_scope;
6999 saved_object_scope = parser->object_scope;
7000 /* We must enter the scope of the class so that the names of
7001 entities declared within the class are available in the
7002 conversion-type-id. For example, consider:
7009 S::operator I() { ... }
7011 In order to see that `I' is a type-name in the definition, we
7012 must be in the scope of `S'. */
7014 pop_p = push_scope (saved_scope);
7015 /* Parse the conversion-type-id. */
7016 type = cp_parser_conversion_type_id (parser);
7017 /* Leave the scope of the class, if any. */
7019 pop_scope (saved_scope);
7020 /* Restore the saved scope. */
7021 parser->scope = saved_scope;
7022 parser->qualifying_scope = saved_qualifying_scope;
7023 parser->object_scope = saved_object_scope;
7024 /* If the TYPE is invalid, indicate failure. */
7025 if (type == error_mark_node)
7026 return error_mark_node;
7027 return mangle_conv_op_name_for_type (type);
7030 /* Parse a conversion-type-id:
7033 type-specifier-seq conversion-declarator [opt]
7035 Returns the TYPE specified. */
7038 cp_parser_conversion_type_id (cp_parser* parser)
7041 tree type_specifiers;
7044 /* Parse the attributes. */
7045 attributes = cp_parser_attributes_opt (parser);
7046 /* Parse the type-specifiers. */
7047 type_specifiers = cp_parser_type_specifier_seq (parser);
7048 /* If that didn't work, stop. */
7049 if (type_specifiers == error_mark_node)
7050 return error_mark_node;
7051 /* Parse the conversion-declarator. */
7052 declarator = cp_parser_conversion_declarator_opt (parser);
7054 return grokdeclarator (declarator, type_specifiers, TYPENAME,
7055 /*initialized=*/0, &attributes);
7058 /* Parse an (optional) conversion-declarator.
7060 conversion-declarator:
7061 ptr-operator conversion-declarator [opt]
7063 Returns a representation of the declarator. See
7064 cp_parser_declarator for details. */
7067 cp_parser_conversion_declarator_opt (cp_parser* parser)
7069 enum tree_code code;
7071 tree cv_qualifier_seq;
7073 /* We don't know if there's a ptr-operator next, or not. */
7074 cp_parser_parse_tentatively (parser);
7075 /* Try the ptr-operator. */
7076 code = cp_parser_ptr_operator (parser, &class_type,
7078 /* If it worked, look for more conversion-declarators. */
7079 if (cp_parser_parse_definitely (parser))
7083 /* Parse another optional declarator. */
7084 declarator = cp_parser_conversion_declarator_opt (parser);
7086 /* Create the representation of the declarator. */
7087 if (code == INDIRECT_REF)
7088 declarator = make_pointer_declarator (cv_qualifier_seq,
7091 declarator = make_reference_declarator (cv_qualifier_seq,
7094 /* Handle the pointer-to-member case. */
7096 declarator = build_nt (SCOPE_REF, class_type, declarator);
7104 /* Parse an (optional) ctor-initializer.
7107 : mem-initializer-list
7109 Returns TRUE iff the ctor-initializer was actually present. */
7112 cp_parser_ctor_initializer_opt (cp_parser* parser)
7114 /* If the next token is not a `:', then there is no
7115 ctor-initializer. */
7116 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
7118 /* Do default initialization of any bases and members. */
7119 if (DECL_CONSTRUCTOR_P (current_function_decl))
7120 finish_mem_initializers (NULL_TREE);
7125 /* Consume the `:' token. */
7126 cp_lexer_consume_token (parser->lexer);
7127 /* And the mem-initializer-list. */
7128 cp_parser_mem_initializer_list (parser);
7133 /* Parse a mem-initializer-list.
7135 mem-initializer-list:
7137 mem-initializer , mem-initializer-list */
7140 cp_parser_mem_initializer_list (cp_parser* parser)
7142 tree mem_initializer_list = NULL_TREE;
7144 /* Let the semantic analysis code know that we are starting the
7145 mem-initializer-list. */
7146 if (!DECL_CONSTRUCTOR_P (current_function_decl))
7147 error ("only constructors take base initializers");
7149 /* Loop through the list. */
7152 tree mem_initializer;
7154 /* Parse the mem-initializer. */
7155 mem_initializer = cp_parser_mem_initializer (parser);
7156 /* Add it to the list, unless it was erroneous. */
7157 if (mem_initializer)
7159 TREE_CHAIN (mem_initializer) = mem_initializer_list;
7160 mem_initializer_list = mem_initializer;
7162 /* If the next token is not a `,', we're done. */
7163 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7165 /* Consume the `,' token. */
7166 cp_lexer_consume_token (parser->lexer);
7169 /* Perform semantic analysis. */
7170 if (DECL_CONSTRUCTOR_P (current_function_decl))
7171 finish_mem_initializers (mem_initializer_list);
7174 /* Parse a mem-initializer.
7177 mem-initializer-id ( expression-list [opt] )
7182 ( expression-list [opt] )
7184 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
7185 class) or FIELD_DECL (for a non-static data member) to initialize;
7186 the TREE_VALUE is the expression-list. */
7189 cp_parser_mem_initializer (cp_parser* parser)
7191 tree mem_initializer_id;
7192 tree expression_list;
7195 /* Find out what is being initialized. */
7196 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7198 pedwarn ("anachronistic old-style base class initializer");
7199 mem_initializer_id = NULL_TREE;
7202 mem_initializer_id = cp_parser_mem_initializer_id (parser);
7203 member = expand_member_init (mem_initializer_id);
7204 if (member && !DECL_P (member))
7205 in_base_initializer = 1;
7208 = cp_parser_parenthesized_expression_list (parser, false,
7209 /*non_constant_p=*/NULL);
7210 if (!expression_list)
7211 expression_list = void_type_node;
7213 in_base_initializer = 0;
7215 return member ? build_tree_list (member, expression_list) : NULL_TREE;
7218 /* Parse a mem-initializer-id.
7221 :: [opt] nested-name-specifier [opt] class-name
7224 Returns a TYPE indicating the class to be initializer for the first
7225 production. Returns an IDENTIFIER_NODE indicating the data member
7226 to be initialized for the second production. */
7229 cp_parser_mem_initializer_id (cp_parser* parser)
7231 bool global_scope_p;
7232 bool nested_name_specifier_p;
7233 bool template_p = false;
7236 /* `typename' is not allowed in this context ([temp.res]). */
7237 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
7239 error ("keyword `typename' not allowed in this context (a qualified "
7240 "member initializer is implicitly a type)");
7241 cp_lexer_consume_token (parser->lexer);
7243 /* Look for the optional `::' operator. */
7245 = (cp_parser_global_scope_opt (parser,
7246 /*current_scope_valid_p=*/false)
7248 /* Look for the optional nested-name-specifier. The simplest way to
7253 The keyword `typename' is not permitted in a base-specifier or
7254 mem-initializer; in these contexts a qualified name that
7255 depends on a template-parameter is implicitly assumed to be a
7258 is to assume that we have seen the `typename' keyword at this
7260 nested_name_specifier_p
7261 = (cp_parser_nested_name_specifier_opt (parser,
7262 /*typename_keyword_p=*/true,
7263 /*check_dependency_p=*/true,
7265 /*is_declaration=*/true)
7267 if (nested_name_specifier_p)
7268 template_p = cp_parser_optional_template_keyword (parser);
7269 /* If there is a `::' operator or a nested-name-specifier, then we
7270 are definitely looking for a class-name. */
7271 if (global_scope_p || nested_name_specifier_p)
7272 return cp_parser_class_name (parser,
7273 /*typename_keyword_p=*/true,
7274 /*template_keyword_p=*/template_p,
7276 /*check_dependency_p=*/true,
7277 /*class_head_p=*/false,
7278 /*is_declaration=*/true);
7279 /* Otherwise, we could also be looking for an ordinary identifier. */
7280 cp_parser_parse_tentatively (parser);
7281 /* Try a class-name. */
7282 id = cp_parser_class_name (parser,
7283 /*typename_keyword_p=*/true,
7284 /*template_keyword_p=*/false,
7286 /*check_dependency_p=*/true,
7287 /*class_head_p=*/false,
7288 /*is_declaration=*/true);
7289 /* If we found one, we're done. */
7290 if (cp_parser_parse_definitely (parser))
7292 /* Otherwise, look for an ordinary identifier. */
7293 return cp_parser_identifier (parser);
7296 /* Overloading [gram.over] */
7298 /* Parse an operator-function-id.
7300 operator-function-id:
7303 Returns an IDENTIFIER_NODE for the operator which is a
7304 human-readable spelling of the identifier, e.g., `operator +'. */
7307 cp_parser_operator_function_id (cp_parser* parser)
7309 /* Look for the `operator' keyword. */
7310 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
7311 return error_mark_node;
7312 /* And then the name of the operator itself. */
7313 return cp_parser_operator (parser);
7316 /* Parse an operator.
7319 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
7320 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
7321 || ++ -- , ->* -> () []
7328 Returns an IDENTIFIER_NODE for the operator which is a
7329 human-readable spelling of the identifier, e.g., `operator +'. */
7332 cp_parser_operator (cp_parser* parser)
7334 tree id = NULL_TREE;
7337 /* Peek at the next token. */
7338 token = cp_lexer_peek_token (parser->lexer);
7339 /* Figure out which operator we have. */
7340 switch (token->type)
7346 /* The keyword should be either `new' or `delete'. */
7347 if (token->keyword == RID_NEW)
7349 else if (token->keyword == RID_DELETE)
7354 /* Consume the `new' or `delete' token. */
7355 cp_lexer_consume_token (parser->lexer);
7357 /* Peek at the next token. */
7358 token = cp_lexer_peek_token (parser->lexer);
7359 /* If it's a `[' token then this is the array variant of the
7361 if (token->type == CPP_OPEN_SQUARE)
7363 /* Consume the `[' token. */
7364 cp_lexer_consume_token (parser->lexer);
7365 /* Look for the `]' token. */
7366 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
7367 id = ansi_opname (op == NEW_EXPR
7368 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
7370 /* Otherwise, we have the non-array variant. */
7372 id = ansi_opname (op);
7378 id = ansi_opname (PLUS_EXPR);
7382 id = ansi_opname (MINUS_EXPR);
7386 id = ansi_opname (MULT_EXPR);
7390 id = ansi_opname (TRUNC_DIV_EXPR);
7394 id = ansi_opname (TRUNC_MOD_EXPR);
7398 id = ansi_opname (BIT_XOR_EXPR);
7402 id = ansi_opname (BIT_AND_EXPR);
7406 id = ansi_opname (BIT_IOR_EXPR);
7410 id = ansi_opname (BIT_NOT_EXPR);
7414 id = ansi_opname (TRUTH_NOT_EXPR);
7418 id = ansi_assopname (NOP_EXPR);
7422 id = ansi_opname (LT_EXPR);
7426 id = ansi_opname (GT_EXPR);
7430 id = ansi_assopname (PLUS_EXPR);
7434 id = ansi_assopname (MINUS_EXPR);
7438 id = ansi_assopname (MULT_EXPR);
7442 id = ansi_assopname (TRUNC_DIV_EXPR);
7446 id = ansi_assopname (TRUNC_MOD_EXPR);
7450 id = ansi_assopname (BIT_XOR_EXPR);
7454 id = ansi_assopname (BIT_AND_EXPR);
7458 id = ansi_assopname (BIT_IOR_EXPR);
7462 id = ansi_opname (LSHIFT_EXPR);
7466 id = ansi_opname (RSHIFT_EXPR);
7470 id = ansi_assopname (LSHIFT_EXPR);
7474 id = ansi_assopname (RSHIFT_EXPR);
7478 id = ansi_opname (EQ_EXPR);
7482 id = ansi_opname (NE_EXPR);
7486 id = ansi_opname (LE_EXPR);
7489 case CPP_GREATER_EQ:
7490 id = ansi_opname (GE_EXPR);
7494 id = ansi_opname (TRUTH_ANDIF_EXPR);
7498 id = ansi_opname (TRUTH_ORIF_EXPR);
7502 id = ansi_opname (POSTINCREMENT_EXPR);
7505 case CPP_MINUS_MINUS:
7506 id = ansi_opname (PREDECREMENT_EXPR);
7510 id = ansi_opname (COMPOUND_EXPR);
7513 case CPP_DEREF_STAR:
7514 id = ansi_opname (MEMBER_REF);
7518 id = ansi_opname (COMPONENT_REF);
7521 case CPP_OPEN_PAREN:
7522 /* Consume the `('. */
7523 cp_lexer_consume_token (parser->lexer);
7524 /* Look for the matching `)'. */
7525 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
7526 return ansi_opname (CALL_EXPR);
7528 case CPP_OPEN_SQUARE:
7529 /* Consume the `['. */
7530 cp_lexer_consume_token (parser->lexer);
7531 /* Look for the matching `]'. */
7532 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
7533 return ansi_opname (ARRAY_REF);
7537 id = ansi_opname (MIN_EXPR);
7541 id = ansi_opname (MAX_EXPR);
7545 id = ansi_assopname (MIN_EXPR);
7549 id = ansi_assopname (MAX_EXPR);
7553 /* Anything else is an error. */
7557 /* If we have selected an identifier, we need to consume the
7560 cp_lexer_consume_token (parser->lexer);
7561 /* Otherwise, no valid operator name was present. */
7564 cp_parser_error (parser, "expected operator");
7565 id = error_mark_node;
7571 /* Parse a template-declaration.
7573 template-declaration:
7574 export [opt] template < template-parameter-list > declaration
7576 If MEMBER_P is TRUE, this template-declaration occurs within a
7579 The grammar rule given by the standard isn't correct. What
7582 template-declaration:
7583 export [opt] template-parameter-list-seq
7584 decl-specifier-seq [opt] init-declarator [opt] ;
7585 export [opt] template-parameter-list-seq
7588 template-parameter-list-seq:
7589 template-parameter-list-seq [opt]
7590 template < template-parameter-list > */
7593 cp_parser_template_declaration (cp_parser* parser, bool member_p)
7595 /* Check for `export'. */
7596 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
7598 /* Consume the `export' token. */
7599 cp_lexer_consume_token (parser->lexer);
7600 /* Warn that we do not support `export'. */
7601 warning ("keyword `export' not implemented, and will be ignored");
7604 cp_parser_template_declaration_after_export (parser, member_p);
7607 /* Parse a template-parameter-list.
7609 template-parameter-list:
7611 template-parameter-list , template-parameter
7613 Returns a TREE_LIST. Each node represents a template parameter.
7614 The nodes are connected via their TREE_CHAINs. */
7617 cp_parser_template_parameter_list (cp_parser* parser)
7619 tree parameter_list = NULL_TREE;
7626 /* Parse the template-parameter. */
7627 parameter = cp_parser_template_parameter (parser);
7628 /* Add it to the list. */
7629 parameter_list = process_template_parm (parameter_list,
7632 /* Peek at the next token. */
7633 token = cp_lexer_peek_token (parser->lexer);
7634 /* If it's not a `,', we're done. */
7635 if (token->type != CPP_COMMA)
7637 /* Otherwise, consume the `,' token. */
7638 cp_lexer_consume_token (parser->lexer);
7641 return parameter_list;
7644 /* Parse a template-parameter.
7648 parameter-declaration
7650 Returns a TREE_LIST. The TREE_VALUE represents the parameter. The
7651 TREE_PURPOSE is the default value, if any. */
7654 cp_parser_template_parameter (cp_parser* parser)
7658 /* Peek at the next token. */
7659 token = cp_lexer_peek_token (parser->lexer);
7660 /* If it is `class' or `template', we have a type-parameter. */
7661 if (token->keyword == RID_TEMPLATE)
7662 return cp_parser_type_parameter (parser);
7663 /* If it is `class' or `typename' we do not know yet whether it is a
7664 type parameter or a non-type parameter. Consider:
7666 template <typename T, typename T::X X> ...
7670 template <class C, class D*> ...
7672 Here, the first parameter is a type parameter, and the second is
7673 a non-type parameter. We can tell by looking at the token after
7674 the identifier -- if it is a `,', `=', or `>' then we have a type
7676 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
7678 /* Peek at the token after `class' or `typename'. */
7679 token = cp_lexer_peek_nth_token (parser->lexer, 2);
7680 /* If it's an identifier, skip it. */
7681 if (token->type == CPP_NAME)
7682 token = cp_lexer_peek_nth_token (parser->lexer, 3);
7683 /* Now, see if the token looks like the end of a template
7685 if (token->type == CPP_COMMA
7686 || token->type == CPP_EQ
7687 || token->type == CPP_GREATER)
7688 return cp_parser_type_parameter (parser);
7691 /* Otherwise, it is a non-type parameter.
7695 When parsing a default template-argument for a non-type
7696 template-parameter, the first non-nested `>' is taken as the end
7697 of the template parameter-list rather than a greater-than
7700 cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
7701 /*parenthesized_p=*/NULL);
7704 /* Parse a type-parameter.
7707 class identifier [opt]
7708 class identifier [opt] = type-id
7709 typename identifier [opt]
7710 typename identifier [opt] = type-id
7711 template < template-parameter-list > class identifier [opt]
7712 template < template-parameter-list > class identifier [opt]
7715 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
7716 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
7717 the declaration of the parameter. */
7720 cp_parser_type_parameter (cp_parser* parser)
7725 /* Look for a keyword to tell us what kind of parameter this is. */
7726 token = cp_parser_require (parser, CPP_KEYWORD,
7727 "`class', `typename', or `template'");
7729 return error_mark_node;
7731 switch (token->keyword)
7737 tree default_argument;
7739 /* If the next token is an identifier, then it names the
7741 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
7742 identifier = cp_parser_identifier (parser);
7744 identifier = NULL_TREE;
7746 /* Create the parameter. */
7747 parameter = finish_template_type_parm (class_type_node, identifier);
7749 /* If the next token is an `=', we have a default argument. */
7750 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7752 /* Consume the `=' token. */
7753 cp_lexer_consume_token (parser->lexer);
7754 /* Parse the default-argument. */
7755 default_argument = cp_parser_type_id (parser);
7758 default_argument = NULL_TREE;
7760 /* Create the combined representation of the parameter and the
7761 default argument. */
7762 parameter = build_tree_list (default_argument, parameter);
7768 tree parameter_list;
7770 tree default_argument;
7772 /* Look for the `<'. */
7773 cp_parser_require (parser, CPP_LESS, "`<'");
7774 /* Parse the template-parameter-list. */
7775 begin_template_parm_list ();
7777 = cp_parser_template_parameter_list (parser);
7778 parameter_list = end_template_parm_list (parameter_list);
7779 /* Look for the `>'. */
7780 cp_parser_require (parser, CPP_GREATER, "`>'");
7781 /* Look for the `class' keyword. */
7782 cp_parser_require_keyword (parser, RID_CLASS, "`class'");
7783 /* If the next token is an `=', then there is a
7784 default-argument. If the next token is a `>', we are at
7785 the end of the parameter-list. If the next token is a `,',
7786 then we are at the end of this parameter. */
7787 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
7788 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
7789 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7790 identifier = cp_parser_identifier (parser);
7792 identifier = NULL_TREE;
7793 /* Create the template parameter. */
7794 parameter = finish_template_template_parm (class_type_node,
7797 /* If the next token is an `=', then there is a
7798 default-argument. */
7799 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7803 /* Consume the `='. */
7804 cp_lexer_consume_token (parser->lexer);
7805 /* Parse the id-expression. */
7807 = cp_parser_id_expression (parser,
7808 /*template_keyword_p=*/false,
7809 /*check_dependency_p=*/true,
7810 /*template_p=*/&is_template,
7811 /*declarator_p=*/false);
7812 if (TREE_CODE (default_argument) == TYPE_DECL)
7813 /* If the id-expression was a template-id that refers to
7814 a template-class, we already have the declaration here,
7815 so no further lookup is needed. */
7818 /* Look up the name. */
7820 = cp_parser_lookup_name (parser, default_argument,
7822 /*is_template=*/is_template,
7823 /*is_namespace=*/false,
7824 /*check_dependency=*/true);
7825 /* See if the default argument is valid. */
7827 = check_template_template_default_arg (default_argument);
7830 default_argument = NULL_TREE;
7832 /* Create the combined representation of the parameter and the
7833 default argument. */
7834 parameter = build_tree_list (default_argument, parameter);
7839 /* Anything else is an error. */
7840 cp_parser_error (parser,
7841 "expected `class', `typename', or `template'");
7842 parameter = error_mark_node;
7848 /* Parse a template-id.
7851 template-name < template-argument-list [opt] >
7853 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
7854 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
7855 returned. Otherwise, if the template-name names a function, or set
7856 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
7857 names a class, returns a TYPE_DECL for the specialization.
7859 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
7860 uninstantiated templates. */
7863 cp_parser_template_id (cp_parser *parser,
7864 bool template_keyword_p,
7865 bool check_dependency_p,
7866 bool is_declaration)
7871 ptrdiff_t start_of_id;
7872 tree access_check = NULL_TREE;
7873 cp_token *next_token, *next_token_2;
7876 /* If the next token corresponds to a template-id, there is no need
7878 next_token = cp_lexer_peek_token (parser->lexer);
7879 if (next_token->type == CPP_TEMPLATE_ID)
7884 /* Get the stored value. */
7885 value = cp_lexer_consume_token (parser->lexer)->value;
7886 /* Perform any access checks that were deferred. */
7887 for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
7888 perform_or_defer_access_check (TREE_PURPOSE (check),
7889 TREE_VALUE (check));
7890 /* Return the stored value. */
7891 return TREE_VALUE (value);
7894 /* Avoid performing name lookup if there is no possibility of
7895 finding a template-id. */
7896 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
7897 || (next_token->type == CPP_NAME
7898 && !cp_parser_nth_token_starts_template_argument_list_p
7901 cp_parser_error (parser, "expected template-id");
7902 return error_mark_node;
7905 /* Remember where the template-id starts. */
7906 if (cp_parser_parsing_tentatively (parser)
7907 && !cp_parser_committed_to_tentative_parse (parser))
7909 next_token = cp_lexer_peek_token (parser->lexer);
7910 start_of_id = cp_lexer_token_difference (parser->lexer,
7911 parser->lexer->first_token,
7917 push_deferring_access_checks (dk_deferred);
7919 /* Parse the template-name. */
7920 is_identifier = false;
7921 template = cp_parser_template_name (parser, template_keyword_p,
7925 if (template == error_mark_node || is_identifier)
7927 pop_deferring_access_checks ();
7931 /* If we find the sequence `[:' after a template-name, it's probably
7932 a digraph-typo for `< ::'. Substitute the tokens and check if we can
7933 parse correctly the argument list. */
7934 next_token = cp_lexer_peek_nth_token (parser->lexer, 1);
7935 next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
7936 if (next_token->type == CPP_OPEN_SQUARE
7937 && next_token->flags & DIGRAPH
7938 && next_token_2->type == CPP_COLON
7939 && !(next_token_2->flags & PREV_WHITE))
7941 cp_parser_parse_tentatively (parser);
7942 /* Change `:' into `::'. */
7943 next_token_2->type = CPP_SCOPE;
7944 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
7946 cp_lexer_consume_token (parser->lexer);
7947 /* Parse the arguments. */
7948 arguments = cp_parser_enclosed_template_argument_list (parser);
7949 if (!cp_parser_parse_definitely (parser))
7951 /* If we couldn't parse an argument list, then we revert our changes
7952 and return simply an error. Maybe this is not a template-id
7954 next_token_2->type = CPP_COLON;
7955 cp_parser_error (parser, "expected `<'");
7956 pop_deferring_access_checks ();
7957 return error_mark_node;
7959 /* Otherwise, emit an error about the invalid digraph, but continue
7960 parsing because we got our argument list. */
7961 pedwarn ("`<::' cannot begin a template-argument list");
7962 inform ("`<:' is an alternate spelling for `['. Insert whitespace "
7963 "between `<' and `::'");
7964 if (!flag_permissive)
7969 inform ("(if you use `-fpermissive' G++ will accept your code)");
7976 /* Look for the `<' that starts the template-argument-list. */
7977 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
7979 pop_deferring_access_checks ();
7980 return error_mark_node;
7982 /* Parse the arguments. */
7983 arguments = cp_parser_enclosed_template_argument_list (parser);
7986 /* Build a representation of the specialization. */
7987 if (TREE_CODE (template) == IDENTIFIER_NODE)
7988 template_id = build_min_nt (TEMPLATE_ID_EXPR, template, arguments);
7989 else if (DECL_CLASS_TEMPLATE_P (template)
7990 || DECL_TEMPLATE_TEMPLATE_PARM_P (template))
7992 = finish_template_type (template, arguments,
7993 cp_lexer_next_token_is (parser->lexer,
7997 /* If it's not a class-template or a template-template, it should be
7998 a function-template. */
7999 my_friendly_assert ((DECL_FUNCTION_TEMPLATE_P (template)
8000 || TREE_CODE (template) == OVERLOAD
8001 || BASELINK_P (template)),
8004 template_id = lookup_template_function (template, arguments);
8007 /* Retrieve any deferred checks. Do not pop this access checks yet
8008 so the memory will not be reclaimed during token replacing below. */
8009 access_check = get_deferred_access_checks ();
8011 /* If parsing tentatively, replace the sequence of tokens that makes
8012 up the template-id with a CPP_TEMPLATE_ID token. That way,
8013 should we re-parse the token stream, we will not have to repeat
8014 the effort required to do the parse, nor will we issue duplicate
8015 error messages about problems during instantiation of the
8017 if (start_of_id >= 0)
8021 /* Find the token that corresponds to the start of the
8023 token = cp_lexer_advance_token (parser->lexer,
8024 parser->lexer->first_token,
8027 /* Reset the contents of the START_OF_ID token. */
8028 token->type = CPP_TEMPLATE_ID;
8029 token->value = build_tree_list (access_check, template_id);
8030 token->keyword = RID_MAX;
8031 /* Purge all subsequent tokens. */
8032 cp_lexer_purge_tokens_after (parser->lexer, token);
8035 pop_deferring_access_checks ();
8039 /* Parse a template-name.
8044 The standard should actually say:
8048 operator-function-id
8050 A defect report has been filed about this issue.
8052 A conversion-function-id cannot be a template name because they cannot
8053 be part of a template-id. In fact, looking at this code:
8057 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
8058 It is impossible to call a templated conversion-function-id with an
8059 explicit argument list, since the only allowed template parameter is
8060 the type to which it is converting.
8062 If TEMPLATE_KEYWORD_P is true, then we have just seen the
8063 `template' keyword, in a construction like:
8067 In that case `f' is taken to be a template-name, even though there
8068 is no way of knowing for sure.
8070 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
8071 name refers to a set of overloaded functions, at least one of which
8072 is a template, or an IDENTIFIER_NODE with the name of the template,
8073 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
8074 names are looked up inside uninstantiated templates. */
8077 cp_parser_template_name (cp_parser* parser,
8078 bool template_keyword_p,
8079 bool check_dependency_p,
8080 bool is_declaration,
8081 bool *is_identifier)
8087 /* If the next token is `operator', then we have either an
8088 operator-function-id or a conversion-function-id. */
8089 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
8091 /* We don't know whether we're looking at an
8092 operator-function-id or a conversion-function-id. */
8093 cp_parser_parse_tentatively (parser);
8094 /* Try an operator-function-id. */
8095 identifier = cp_parser_operator_function_id (parser);
8096 /* If that didn't work, try a conversion-function-id. */
8097 if (!cp_parser_parse_definitely (parser))
8099 cp_parser_error (parser, "expected template-name");
8100 return error_mark_node;
8103 /* Look for the identifier. */
8105 identifier = cp_parser_identifier (parser);
8107 /* If we didn't find an identifier, we don't have a template-id. */
8108 if (identifier == error_mark_node)
8109 return error_mark_node;
8111 /* If the name immediately followed the `template' keyword, then it
8112 is a template-name. However, if the next token is not `<', then
8113 we do not treat it as a template-name, since it is not being used
8114 as part of a template-id. This enables us to handle constructs
8117 template <typename T> struct S { S(); };
8118 template <typename T> S<T>::S();
8120 correctly. We would treat `S' as a template -- if it were `S<T>'
8121 -- but we do not if there is no `<'. */
8123 if (processing_template_decl
8124 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
8126 /* In a declaration, in a dependent context, we pretend that the
8127 "template" keyword was present in order to improve error
8128 recovery. For example, given:
8130 template <typename T> void f(T::X<int>);
8132 we want to treat "X<int>" as a template-id. */
8134 && !template_keyword_p
8135 && parser->scope && TYPE_P (parser->scope)
8136 && check_dependency_p
8137 && dependent_type_p (parser->scope)
8138 /* Do not do this for dtors (or ctors), since they never
8139 need the template keyword before their name. */
8140 && !constructor_name_p (identifier, parser->scope))
8144 /* Explain what went wrong. */
8145 error ("non-template `%D' used as template", identifier);
8146 inform ("use `%T::template %D' to indicate that it is a template",
8147 parser->scope, identifier);
8148 /* If parsing tentatively, find the location of the "<"
8150 if (cp_parser_parsing_tentatively (parser)
8151 && !cp_parser_committed_to_tentative_parse (parser))
8153 cp_parser_simulate_error (parser);
8154 token = cp_lexer_peek_token (parser->lexer);
8155 token = cp_lexer_prev_token (parser->lexer, token);
8156 start = cp_lexer_token_difference (parser->lexer,
8157 parser->lexer->first_token,
8162 /* Parse the template arguments so that we can issue error
8163 messages about them. */
8164 cp_lexer_consume_token (parser->lexer);
8165 cp_parser_enclosed_template_argument_list (parser);
8166 /* Skip tokens until we find a good place from which to
8167 continue parsing. */
8168 cp_parser_skip_to_closing_parenthesis (parser,
8169 /*recovering=*/true,
8171 /*consume_paren=*/false);
8172 /* If parsing tentatively, permanently remove the
8173 template argument list. That will prevent duplicate
8174 error messages from being issued about the missing
8175 "template" keyword. */
8178 token = cp_lexer_advance_token (parser->lexer,
8179 parser->lexer->first_token,
8181 cp_lexer_purge_tokens_after (parser->lexer, token);
8184 *is_identifier = true;
8188 /* If the "template" keyword is present, then there is generally
8189 no point in doing name-lookup, so we just return IDENTIFIER.
8190 But, if the qualifying scope is non-dependent then we can
8191 (and must) do name-lookup normally. */
8192 if (template_keyword_p
8194 || (TYPE_P (parser->scope)
8195 && dependent_type_p (parser->scope))))
8199 /* Look up the name. */
8200 decl = cp_parser_lookup_name (parser, identifier,
8202 /*is_template=*/false,
8203 /*is_namespace=*/false,
8204 check_dependency_p);
8205 decl = maybe_get_template_decl_from_type_decl (decl);
8207 /* If DECL is a template, then the name was a template-name. */
8208 if (TREE_CODE (decl) == TEMPLATE_DECL)
8212 /* The standard does not explicitly indicate whether a name that
8213 names a set of overloaded declarations, some of which are
8214 templates, is a template-name. However, such a name should
8215 be a template-name; otherwise, there is no way to form a
8216 template-id for the overloaded templates. */
8217 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
8218 if (TREE_CODE (fns) == OVERLOAD)
8222 for (fn = fns; fn; fn = OVL_NEXT (fn))
8223 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
8228 /* Otherwise, the name does not name a template. */
8229 cp_parser_error (parser, "expected template-name");
8230 return error_mark_node;
8234 /* If DECL is dependent, and refers to a function, then just return
8235 its name; we will look it up again during template instantiation. */
8236 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
8238 tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
8239 if (TYPE_P (scope) && dependent_type_p (scope))
8246 /* Parse a template-argument-list.
8248 template-argument-list:
8250 template-argument-list , template-argument
8252 Returns a TREE_VEC containing the arguments. */
8255 cp_parser_template_argument_list (cp_parser* parser)
8257 tree fixed_args[10];
8258 unsigned n_args = 0;
8259 unsigned alloced = 10;
8260 tree *arg_ary = fixed_args;
8262 bool saved_in_template_argument_list_p;
8264 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
8265 parser->in_template_argument_list_p = true;
8271 /* Consume the comma. */
8272 cp_lexer_consume_token (parser->lexer);
8274 /* Parse the template-argument. */
8275 argument = cp_parser_template_argument (parser);
8276 if (n_args == alloced)
8280 if (arg_ary == fixed_args)
8282 arg_ary = xmalloc (sizeof (tree) * alloced);
8283 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
8286 arg_ary = xrealloc (arg_ary, sizeof (tree) * alloced);
8288 arg_ary[n_args++] = argument;
8290 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
8292 vec = make_tree_vec (n_args);
8295 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
8297 if (arg_ary != fixed_args)
8299 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
8303 /* Parse a template-argument.
8306 assignment-expression
8310 The representation is that of an assignment-expression, type-id, or
8311 id-expression -- except that the qualified id-expression is
8312 evaluated, so that the value returned is either a DECL or an
8315 Although the standard says "assignment-expression", it forbids
8316 throw-expressions or assignments in the template argument.
8317 Therefore, we use "conditional-expression" instead. */
8320 cp_parser_template_argument (cp_parser* parser)
8325 bool maybe_type_id = false;
8328 tree qualifying_class;
8330 /* There's really no way to know what we're looking at, so we just
8331 try each alternative in order.
8335 In a template-argument, an ambiguity between a type-id and an
8336 expression is resolved to a type-id, regardless of the form of
8337 the corresponding template-parameter.
8339 Therefore, we try a type-id first. */
8340 cp_parser_parse_tentatively (parser);
8341 argument = cp_parser_type_id (parser);
8342 /* If there was no error parsing the type-id but the next token is a '>>',
8343 we probably found a typo for '> >'. But there are type-id which are
8344 also valid expressions. For instance:
8346 struct X { int operator >> (int); };
8347 template <int V> struct Foo {};
8350 Here 'X()' is a valid type-id of a function type, but the user just
8351 wanted to write the expression "X() >> 5". Thus, we remember that we
8352 found a valid type-id, but we still try to parse the argument as an
8353 expression to see what happens. */
8354 if (!cp_parser_error_occurred (parser)
8355 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
8357 maybe_type_id = true;
8358 cp_parser_abort_tentative_parse (parser);
8362 /* If the next token isn't a `,' or a `>', then this argument wasn't
8363 really finished. This means that the argument is not a valid
8365 if (!cp_parser_next_token_ends_template_argument_p (parser))
8366 cp_parser_error (parser, "expected template-argument");
8367 /* If that worked, we're done. */
8368 if (cp_parser_parse_definitely (parser))
8371 /* We're still not sure what the argument will be. */
8372 cp_parser_parse_tentatively (parser);
8373 /* Try a template. */
8374 argument = cp_parser_id_expression (parser,
8375 /*template_keyword_p=*/false,
8376 /*check_dependency_p=*/true,
8378 /*declarator_p=*/false);
8379 /* If the next token isn't a `,' or a `>', then this argument wasn't
8381 if (!cp_parser_next_token_ends_template_argument_p (parser))
8382 cp_parser_error (parser, "expected template-argument");
8383 if (!cp_parser_error_occurred (parser))
8385 /* Figure out what is being referred to. If the id-expression
8386 was for a class template specialization, then we will have a
8387 TYPE_DECL at this point. There is no need to do name lookup
8388 at this point in that case. */
8389 if (TREE_CODE (argument) != TYPE_DECL)
8390 argument = cp_parser_lookup_name (parser, argument,
8392 /*is_template=*/template_p,
8393 /*is_namespace=*/false,
8394 /*check_dependency=*/true);
8395 if (TREE_CODE (argument) != TEMPLATE_DECL
8396 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
8397 cp_parser_error (parser, "expected template-name");
8399 if (cp_parser_parse_definitely (parser))
8401 /* It must be a non-type argument. There permitted cases are given
8402 in [temp.arg.nontype]:
8404 -- an integral constant-expression of integral or enumeration
8407 -- the name of a non-type template-parameter; or
8409 -- the name of an object or function with external linkage...
8411 -- the address of an object or function with external linkage...
8413 -- a pointer to member... */
8414 /* Look for a non-type template parameter. */
8415 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
8417 cp_parser_parse_tentatively (parser);
8418 argument = cp_parser_primary_expression (parser,
8421 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
8422 || !cp_parser_next_token_ends_template_argument_p (parser))
8423 cp_parser_simulate_error (parser);
8424 if (cp_parser_parse_definitely (parser))
8427 /* If the next token is "&", the argument must be the address of an
8428 object or function with external linkage. */
8429 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
8431 cp_lexer_consume_token (parser->lexer);
8432 /* See if we might have an id-expression. */
8433 token = cp_lexer_peek_token (parser->lexer);
8434 if (token->type == CPP_NAME
8435 || token->keyword == RID_OPERATOR
8436 || token->type == CPP_SCOPE
8437 || token->type == CPP_TEMPLATE_ID
8438 || token->type == CPP_NESTED_NAME_SPECIFIER)
8440 cp_parser_parse_tentatively (parser);
8441 argument = cp_parser_primary_expression (parser,
8444 if (cp_parser_error_occurred (parser)
8445 || !cp_parser_next_token_ends_template_argument_p (parser))
8446 cp_parser_abort_tentative_parse (parser);
8449 if (qualifying_class)
8450 argument = finish_qualified_id_expr (qualifying_class,
8454 if (TREE_CODE (argument) == VAR_DECL)
8456 /* A variable without external linkage might still be a
8457 valid constant-expression, so no error is issued here
8458 if the external-linkage check fails. */
8459 if (!DECL_EXTERNAL_LINKAGE_P (argument))
8460 cp_parser_simulate_error (parser);
8462 else if (is_overloaded_fn (argument))
8463 /* All overloaded functions are allowed; if the external
8464 linkage test does not pass, an error will be issued
8468 && (TREE_CODE (argument) == OFFSET_REF
8469 || TREE_CODE (argument) == SCOPE_REF))
8470 /* A pointer-to-member. */
8473 cp_parser_simulate_error (parser);
8475 if (cp_parser_parse_definitely (parser))
8478 argument = build_x_unary_op (ADDR_EXPR, argument);
8483 /* If the argument started with "&", there are no other valid
8484 alternatives at this point. */
8487 cp_parser_error (parser, "invalid non-type template argument");
8488 return error_mark_node;
8490 /* If the argument wasn't successfully parsed as a type-id followed
8491 by '>>', the argument can only be a constant expression now.
8492 Otherwise, we try parsing the constant-expression tentatively,
8493 because the argument could really be a type-id. */
8495 cp_parser_parse_tentatively (parser);
8496 argument = cp_parser_constant_expression (parser,
8497 /*allow_non_constant_p=*/false,
8498 /*non_constant_p=*/NULL);
8499 argument = fold_non_dependent_expr (argument);
8502 if (!cp_parser_next_token_ends_template_argument_p (parser))
8503 cp_parser_error (parser, "expected template-argument");
8504 if (cp_parser_parse_definitely (parser))
8506 /* We did our best to parse the argument as a non type-id, but that
8507 was the only alternative that matched (albeit with a '>' after
8508 it). We can assume it's just a typo from the user, and a
8509 diagnostic will then be issued. */
8510 return cp_parser_type_id (parser);
8513 /* Parse an explicit-instantiation.
8515 explicit-instantiation:
8516 template declaration
8518 Although the standard says `declaration', what it really means is:
8520 explicit-instantiation:
8521 template decl-specifier-seq [opt] declarator [opt] ;
8523 Things like `template int S<int>::i = 5, int S<double>::j;' are not
8524 supposed to be allowed. A defect report has been filed about this
8529 explicit-instantiation:
8530 storage-class-specifier template
8531 decl-specifier-seq [opt] declarator [opt] ;
8532 function-specifier template
8533 decl-specifier-seq [opt] declarator [opt] ; */
8536 cp_parser_explicit_instantiation (cp_parser* parser)
8538 int declares_class_or_enum;
8539 tree decl_specifiers;
8541 tree extension_specifier = NULL_TREE;
8543 /* Look for an (optional) storage-class-specifier or
8544 function-specifier. */
8545 if (cp_parser_allow_gnu_extensions_p (parser))
8548 = cp_parser_storage_class_specifier_opt (parser);
8549 if (!extension_specifier)
8550 extension_specifier = cp_parser_function_specifier_opt (parser);
8553 /* Look for the `template' keyword. */
8554 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
8555 /* Let the front end know that we are processing an explicit
8557 begin_explicit_instantiation ();
8558 /* [temp.explicit] says that we are supposed to ignore access
8559 control while processing explicit instantiation directives. */
8560 push_deferring_access_checks (dk_no_check);
8561 /* Parse a decl-specifier-seq. */
8563 = cp_parser_decl_specifier_seq (parser,
8564 CP_PARSER_FLAGS_OPTIONAL,
8566 &declares_class_or_enum);
8567 /* If there was exactly one decl-specifier, and it declared a class,
8568 and there's no declarator, then we have an explicit type
8570 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
8574 type = check_tag_decl (decl_specifiers);
8575 /* Turn access control back on for names used during
8576 template instantiation. */
8577 pop_deferring_access_checks ();
8579 do_type_instantiation (type, extension_specifier, /*complain=*/1);
8586 /* Parse the declarator. */
8588 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
8589 /*ctor_dtor_or_conv_p=*/NULL,
8590 /*parenthesized_p=*/NULL,
8591 /*member_p=*/false);
8592 cp_parser_check_for_definition_in_return_type (declarator,
8593 declares_class_or_enum);
8594 if (declarator != error_mark_node)
8596 decl = grokdeclarator (declarator, decl_specifiers,
8598 /* Turn access control back on for names used during
8599 template instantiation. */
8600 pop_deferring_access_checks ();
8601 /* Do the explicit instantiation. */
8602 do_decl_instantiation (decl, extension_specifier);
8606 pop_deferring_access_checks ();
8607 /* Skip the body of the explicit instantiation. */
8608 cp_parser_skip_to_end_of_statement (parser);
8611 /* We're done with the instantiation. */
8612 end_explicit_instantiation ();
8614 cp_parser_consume_semicolon_at_end_of_statement (parser);
8617 /* Parse an explicit-specialization.
8619 explicit-specialization:
8620 template < > declaration
8622 Although the standard says `declaration', what it really means is:
8624 explicit-specialization:
8625 template <> decl-specifier [opt] init-declarator [opt] ;
8626 template <> function-definition
8627 template <> explicit-specialization
8628 template <> template-declaration */
8631 cp_parser_explicit_specialization (cp_parser* parser)
8633 /* Look for the `template' keyword. */
8634 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
8635 /* Look for the `<'. */
8636 cp_parser_require (parser, CPP_LESS, "`<'");
8637 /* Look for the `>'. */
8638 cp_parser_require (parser, CPP_GREATER, "`>'");
8639 /* We have processed another parameter list. */
8640 ++parser->num_template_parameter_lists;
8641 /* Let the front end know that we are beginning a specialization. */
8642 begin_specialization ();
8644 /* If the next keyword is `template', we need to figure out whether
8645 or not we're looking a template-declaration. */
8646 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
8648 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
8649 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
8650 cp_parser_template_declaration_after_export (parser,
8651 /*member_p=*/false);
8653 cp_parser_explicit_specialization (parser);
8656 /* Parse the dependent declaration. */
8657 cp_parser_single_declaration (parser,
8661 /* We're done with the specialization. */
8662 end_specialization ();
8663 /* We're done with this parameter list. */
8664 --parser->num_template_parameter_lists;
8667 /* Parse a type-specifier.
8670 simple-type-specifier
8673 elaborated-type-specifier
8681 Returns a representation of the type-specifier. If the
8682 type-specifier is a keyword (like `int' or `const', or
8683 `__complex__') then the corresponding IDENTIFIER_NODE is returned.
8684 For a class-specifier, enum-specifier, or elaborated-type-specifier
8685 a TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
8687 If IS_FRIEND is TRUE then this type-specifier is being declared a
8688 `friend'. If IS_DECLARATION is TRUE, then this type-specifier is
8689 appearing in a decl-specifier-seq.
8691 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
8692 class-specifier, enum-specifier, or elaborated-type-specifier, then
8693 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
8694 if a type is declared; 2 if it is defined. Otherwise, it is set to
8697 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
8698 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
8702 cp_parser_type_specifier (cp_parser* parser,
8703 cp_parser_flags flags,
8705 bool is_declaration,
8706 int* declares_class_or_enum,
8707 bool* is_cv_qualifier)
8709 tree type_spec = NULL_TREE;
8713 /* Assume this type-specifier does not declare a new type. */
8714 if (declares_class_or_enum)
8715 *declares_class_or_enum = 0;
8716 /* And that it does not specify a cv-qualifier. */
8717 if (is_cv_qualifier)
8718 *is_cv_qualifier = false;
8719 /* Peek at the next token. */
8720 token = cp_lexer_peek_token (parser->lexer);
8722 /* If we're looking at a keyword, we can use that to guide the
8723 production we choose. */
8724 keyword = token->keyword;
8727 /* Any of these indicate either a class-specifier, or an
8728 elaborated-type-specifier. */
8733 /* Parse tentatively so that we can back up if we don't find a
8734 class-specifier or enum-specifier. */
8735 cp_parser_parse_tentatively (parser);
8736 /* Look for the class-specifier or enum-specifier. */
8737 if (keyword == RID_ENUM)
8738 type_spec = cp_parser_enum_specifier (parser);
8740 type_spec = cp_parser_class_specifier (parser);
8742 /* If that worked, we're done. */
8743 if (cp_parser_parse_definitely (parser))
8745 if (declares_class_or_enum)
8746 *declares_class_or_enum = 2;
8753 /* Look for an elaborated-type-specifier. */
8754 type_spec = cp_parser_elaborated_type_specifier (parser,
8757 /* We're declaring a class or enum -- unless we're using
8759 if (declares_class_or_enum && keyword != RID_TYPENAME)
8760 *declares_class_or_enum = 1;
8766 type_spec = cp_parser_cv_qualifier_opt (parser);
8767 /* Even though we call a routine that looks for an optional
8768 qualifier, we know that there should be one. */
8769 my_friendly_assert (type_spec != NULL, 20000328);
8770 /* This type-specifier was a cv-qualified. */
8771 if (is_cv_qualifier)
8772 *is_cv_qualifier = true;
8777 /* The `__complex__' keyword is a GNU extension. */
8778 return cp_lexer_consume_token (parser->lexer)->value;
8784 /* If we do not already have a type-specifier, assume we are looking
8785 at a simple-type-specifier. */
8786 type_spec = cp_parser_simple_type_specifier (parser, flags,
8787 /*identifier_p=*/true);
8789 /* If we didn't find a type-specifier, and a type-specifier was not
8790 optional in this context, issue an error message. */
8791 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
8793 cp_parser_error (parser, "expected type specifier");
8794 return error_mark_node;
8800 /* Parse a simple-type-specifier.
8802 simple-type-specifier:
8803 :: [opt] nested-name-specifier [opt] type-name
8804 :: [opt] nested-name-specifier template template-id
8819 simple-type-specifier:
8820 __typeof__ unary-expression
8821 __typeof__ ( type-id )
8823 For the various keywords, the value returned is simply the
8824 TREE_IDENTIFIER representing the keyword if IDENTIFIER_P is true.
8825 For the first two productions, and if IDENTIFIER_P is false, the
8826 value returned is the indicated TYPE_DECL. */
8829 cp_parser_simple_type_specifier (cp_parser* parser, cp_parser_flags flags,
8832 tree type = NULL_TREE;
8835 /* Peek at the next token. */
8836 token = cp_lexer_peek_token (parser->lexer);
8838 /* If we're looking at a keyword, things are easy. */
8839 switch (token->keyword)
8842 type = char_type_node;
8845 type = wchar_type_node;
8848 type = boolean_type_node;
8851 type = short_integer_type_node;
8854 type = integer_type_node;
8857 type = long_integer_type_node;
8860 type = integer_type_node;
8863 type = unsigned_type_node;
8866 type = float_type_node;
8869 type = double_type_node;
8872 type = void_type_node;
8879 /* Consume the `typeof' token. */
8880 cp_lexer_consume_token (parser->lexer);
8881 /* Parse the operand to `typeof'. */
8882 operand = cp_parser_sizeof_operand (parser, RID_TYPEOF);
8883 /* If it is not already a TYPE, take its type. */
8884 if (!TYPE_P (operand))
8885 operand = finish_typeof (operand);
8894 /* If the type-specifier was for a built-in type, we're done. */
8899 /* Consume the token. */
8900 id = cp_lexer_consume_token (parser->lexer)->value;
8902 /* There is no valid C++ program where a non-template type is
8903 followed by a "<". That usually indicates that the user thought
8904 that the type was a template. */
8905 cp_parser_check_for_invalid_template_id (parser, type);
8907 return identifier_p ? id : TYPE_NAME (type);
8910 /* The type-specifier must be a user-defined type. */
8911 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
8916 /* Don't gobble tokens or issue error messages if this is an
8917 optional type-specifier. */
8918 if (flags & CP_PARSER_FLAGS_OPTIONAL)
8919 cp_parser_parse_tentatively (parser);
8921 /* Look for the optional `::' operator. */
8923 = (cp_parser_global_scope_opt (parser,
8924 /*current_scope_valid_p=*/false)
8926 /* Look for the nested-name specifier. */
8928 = (cp_parser_nested_name_specifier_opt (parser,
8929 /*typename_keyword_p=*/false,
8930 /*check_dependency_p=*/true,
8932 /*is_declaration=*/false)
8934 /* If we have seen a nested-name-specifier, and the next token
8935 is `template', then we are using the template-id production. */
8937 && cp_parser_optional_template_keyword (parser))
8939 /* Look for the template-id. */
8940 type = cp_parser_template_id (parser,
8941 /*template_keyword_p=*/true,
8942 /*check_dependency_p=*/true,
8943 /*is_declaration=*/false);
8944 /* If the template-id did not name a type, we are out of
8946 if (TREE_CODE (type) != TYPE_DECL)
8948 cp_parser_error (parser, "expected template-id for type");
8952 /* Otherwise, look for a type-name. */
8954 type = cp_parser_type_name (parser);
8955 /* Keep track of all name-lookups performed in class scopes. */
8959 && TREE_CODE (type) == TYPE_DECL
8960 && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
8961 maybe_note_name_used_in_class (DECL_NAME (type), type);
8962 /* If it didn't work out, we don't have a TYPE. */
8963 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
8964 && !cp_parser_parse_definitely (parser))
8968 /* If we didn't get a type-name, issue an error message. */
8969 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
8971 cp_parser_error (parser, "expected type-name");
8972 return error_mark_node;
8975 /* There is no valid C++ program where a non-template type is
8976 followed by a "<". That usually indicates that the user thought
8977 that the type was a template. */
8978 if (type && type != error_mark_node)
8979 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type));
8984 /* Parse a type-name.
8997 Returns a TYPE_DECL for the the type. */
9000 cp_parser_type_name (cp_parser* parser)
9005 /* We can't know yet whether it is a class-name or not. */
9006 cp_parser_parse_tentatively (parser);
9007 /* Try a class-name. */
9008 type_decl = cp_parser_class_name (parser,
9009 /*typename_keyword_p=*/false,
9010 /*template_keyword_p=*/false,
9012 /*check_dependency_p=*/true,
9013 /*class_head_p=*/false,
9014 /*is_declaration=*/false);
9015 /* If it's not a class-name, keep looking. */
9016 if (!cp_parser_parse_definitely (parser))
9018 /* It must be a typedef-name or an enum-name. */
9019 identifier = cp_parser_identifier (parser);
9020 if (identifier == error_mark_node)
9021 return error_mark_node;
9023 /* Look up the type-name. */
9024 type_decl = cp_parser_lookup_name_simple (parser, identifier);
9025 /* Issue an error if we did not find a type-name. */
9026 if (TREE_CODE (type_decl) != TYPE_DECL)
9028 if (!cp_parser_simulate_error (parser))
9029 cp_parser_name_lookup_error (parser, identifier, type_decl,
9031 type_decl = error_mark_node;
9033 /* Remember that the name was used in the definition of the
9034 current class so that we can check later to see if the
9035 meaning would have been different after the class was
9036 entirely defined. */
9037 else if (type_decl != error_mark_node
9039 maybe_note_name_used_in_class (identifier, type_decl);
9046 /* Parse an elaborated-type-specifier. Note that the grammar given
9047 here incorporates the resolution to DR68.
9049 elaborated-type-specifier:
9050 class-key :: [opt] nested-name-specifier [opt] identifier
9051 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
9052 enum :: [opt] nested-name-specifier [opt] identifier
9053 typename :: [opt] nested-name-specifier identifier
9054 typename :: [opt] nested-name-specifier template [opt]
9059 elaborated-type-specifier:
9060 class-key attributes :: [opt] nested-name-specifier [opt] identifier
9061 class-key attributes :: [opt] nested-name-specifier [opt]
9062 template [opt] template-id
9063 enum attributes :: [opt] nested-name-specifier [opt] identifier
9065 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
9066 declared `friend'. If IS_DECLARATION is TRUE, then this
9067 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
9068 something is being declared.
9070 Returns the TYPE specified. */
9073 cp_parser_elaborated_type_specifier (cp_parser* parser,
9075 bool is_declaration)
9077 enum tag_types tag_type;
9079 tree type = NULL_TREE;
9080 tree attributes = NULL_TREE;
9082 /* See if we're looking at the `enum' keyword. */
9083 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
9085 /* Consume the `enum' token. */
9086 cp_lexer_consume_token (parser->lexer);
9087 /* Remember that it's an enumeration type. */
9088 tag_type = enum_type;
9089 /* Parse the attributes. */
9090 attributes = cp_parser_attributes_opt (parser);
9092 /* Or, it might be `typename'. */
9093 else if (cp_lexer_next_token_is_keyword (parser->lexer,
9096 /* Consume the `typename' token. */
9097 cp_lexer_consume_token (parser->lexer);
9098 /* Remember that it's a `typename' type. */
9099 tag_type = typename_type;
9100 /* The `typename' keyword is only allowed in templates. */
9101 if (!processing_template_decl)
9102 pedwarn ("using `typename' outside of template");
9104 /* Otherwise it must be a class-key. */
9107 tag_type = cp_parser_class_key (parser);
9108 if (tag_type == none_type)
9109 return error_mark_node;
9110 /* Parse the attributes. */
9111 attributes = cp_parser_attributes_opt (parser);
9114 /* Look for the `::' operator. */
9115 cp_parser_global_scope_opt (parser,
9116 /*current_scope_valid_p=*/false);
9117 /* Look for the nested-name-specifier. */
9118 if (tag_type == typename_type)
9120 if (cp_parser_nested_name_specifier (parser,
9121 /*typename_keyword_p=*/true,
9122 /*check_dependency_p=*/true,
9126 return error_mark_node;
9129 /* Even though `typename' is not present, the proposed resolution
9130 to Core Issue 180 says that in `class A<T>::B', `B' should be
9131 considered a type-name, even if `A<T>' is dependent. */
9132 cp_parser_nested_name_specifier_opt (parser,
9133 /*typename_keyword_p=*/true,
9134 /*check_dependency_p=*/true,
9137 /* For everything but enumeration types, consider a template-id. */
9138 if (tag_type != enum_type)
9140 bool template_p = false;
9143 /* Allow the `template' keyword. */
9144 template_p = cp_parser_optional_template_keyword (parser);
9145 /* If we didn't see `template', we don't know if there's a
9146 template-id or not. */
9148 cp_parser_parse_tentatively (parser);
9149 /* Parse the template-id. */
9150 decl = cp_parser_template_id (parser, template_p,
9151 /*check_dependency_p=*/true,
9153 /* If we didn't find a template-id, look for an ordinary
9155 if (!template_p && !cp_parser_parse_definitely (parser))
9157 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
9158 in effect, then we must assume that, upon instantiation, the
9159 template will correspond to a class. */
9160 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
9161 && tag_type == typename_type)
9162 type = make_typename_type (parser->scope, decl,
9165 type = TREE_TYPE (decl);
9168 /* For an enumeration type, consider only a plain identifier. */
9171 identifier = cp_parser_identifier (parser);
9173 if (identifier == error_mark_node)
9175 parser->scope = NULL_TREE;
9176 return error_mark_node;
9179 /* For a `typename', we needn't call xref_tag. */
9180 if (tag_type == typename_type)
9181 return make_typename_type (parser->scope, identifier,
9183 /* Look up a qualified name in the usual way. */
9188 /* In an elaborated-type-specifier, names are assumed to name
9189 types, so we set IS_TYPE to TRUE when calling
9190 cp_parser_lookup_name. */
9191 decl = cp_parser_lookup_name (parser, identifier,
9193 /*is_template=*/false,
9194 /*is_namespace=*/false,
9195 /*check_dependency=*/true);
9197 /* If we are parsing friend declaration, DECL may be a
9198 TEMPLATE_DECL tree node here. However, we need to check
9199 whether this TEMPLATE_DECL results in valid code. Consider
9200 the following example:
9203 template <class T> class C {};
9206 template <class T> friend class N::C; // #1, valid code
9208 template <class T> class Y {
9209 friend class N::C; // #2, invalid code
9212 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
9213 name lookup of `N::C'. We see that friend declaration must
9214 be template for the code to be valid. Note that
9215 processing_template_decl does not work here since it is
9216 always 1 for the above two cases. */
9218 decl = (cp_parser_maybe_treat_template_as_class
9219 (decl, /*tag_name_p=*/is_friend
9220 && parser->num_template_parameter_lists));
9222 if (TREE_CODE (decl) != TYPE_DECL)
9224 error ("expected type-name");
9225 return error_mark_node;
9228 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
9229 check_elaborated_type_specifier
9231 (parser->num_template_parameter_lists
9232 || DECL_SELF_REFERENCE_P (decl)));
9234 type = TREE_TYPE (decl);
9238 /* An elaborated-type-specifier sometimes introduces a new type and
9239 sometimes names an existing type. Normally, the rule is that it
9240 introduces a new type only if there is not an existing type of
9241 the same name already in scope. For example, given:
9244 void f() { struct S s; }
9246 the `struct S' in the body of `f' is the same `struct S' as in
9247 the global scope; the existing definition is used. However, if
9248 there were no global declaration, this would introduce a new
9249 local class named `S'.
9251 An exception to this rule applies to the following code:
9253 namespace N { struct S; }
9255 Here, the elaborated-type-specifier names a new type
9256 unconditionally; even if there is already an `S' in the
9257 containing scope this declaration names a new type.
9258 This exception only applies if the elaborated-type-specifier
9259 forms the complete declaration:
9263 A declaration consisting solely of `class-key identifier ;' is
9264 either a redeclaration of the name in the current scope or a
9265 forward declaration of the identifier as a class name. It
9266 introduces the name into the current scope.
9268 We are in this situation precisely when the next token is a `;'.
9270 An exception to the exception is that a `friend' declaration does
9271 *not* name a new type; i.e., given:
9273 struct S { friend struct T; };
9275 `T' is not a new type in the scope of `S'.
9277 Also, `new struct S' or `sizeof (struct S)' never results in the
9278 definition of a new type; a new type can only be declared in a
9279 declaration context. */
9281 /* Warn about attributes. They are ignored. */
9283 warning ("type attributes are honored only at type definition");
9285 type = xref_tag (tag_type, identifier,
9288 || cp_lexer_next_token_is_not (parser->lexer,
9290 parser->num_template_parameter_lists);
9293 if (tag_type != enum_type)
9294 cp_parser_check_class_key (tag_type, type);
9296 /* A "<" cannot follow an elaborated type specifier. If that
9297 happens, the user was probably trying to form a template-id. */
9298 cp_parser_check_for_invalid_template_id (parser, type);
9303 /* Parse an enum-specifier.
9306 enum identifier [opt] { enumerator-list [opt] }
9308 Returns an ENUM_TYPE representing the enumeration. */
9311 cp_parser_enum_specifier (cp_parser* parser)
9314 tree identifier = NULL_TREE;
9317 /* Look for the `enum' keyword. */
9318 if (!cp_parser_require_keyword (parser, RID_ENUM, "`enum'"))
9319 return error_mark_node;
9320 /* Peek at the next token. */
9321 token = cp_lexer_peek_token (parser->lexer);
9323 /* See if it is an identifier. */
9324 if (token->type == CPP_NAME)
9325 identifier = cp_parser_identifier (parser);
9327 /* Look for the `{'. */
9328 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
9329 return error_mark_node;
9331 /* At this point, we're going ahead with the enum-specifier, even
9332 if some other problem occurs. */
9333 cp_parser_commit_to_tentative_parse (parser);
9335 /* Issue an error message if type-definitions are forbidden here. */
9336 cp_parser_check_type_definition (parser);
9338 /* Create the new type. */
9339 type = start_enum (identifier ? identifier : make_anon_name ());
9341 /* Peek at the next token. */
9342 token = cp_lexer_peek_token (parser->lexer);
9343 /* If it's not a `}', then there are some enumerators. */
9344 if (token->type != CPP_CLOSE_BRACE)
9345 cp_parser_enumerator_list (parser, type);
9346 /* Look for the `}'. */
9347 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
9349 /* Finish up the enumeration. */
9355 /* Parse an enumerator-list. The enumerators all have the indicated
9359 enumerator-definition
9360 enumerator-list , enumerator-definition */
9363 cp_parser_enumerator_list (cp_parser* parser, tree type)
9369 /* Parse an enumerator-definition. */
9370 cp_parser_enumerator_definition (parser, type);
9371 /* Peek at the next token. */
9372 token = cp_lexer_peek_token (parser->lexer);
9373 /* If it's not a `,', then we've reached the end of the
9375 if (token->type != CPP_COMMA)
9377 /* Otherwise, consume the `,' and keep going. */
9378 cp_lexer_consume_token (parser->lexer);
9379 /* If the next token is a `}', there is a trailing comma. */
9380 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
9382 if (pedantic && !in_system_header)
9383 pedwarn ("comma at end of enumerator list");
9389 /* Parse an enumerator-definition. The enumerator has the indicated
9392 enumerator-definition:
9394 enumerator = constant-expression
9400 cp_parser_enumerator_definition (cp_parser* parser, tree type)
9406 /* Look for the identifier. */
9407 identifier = cp_parser_identifier (parser);
9408 if (identifier == error_mark_node)
9411 /* Peek at the next token. */
9412 token = cp_lexer_peek_token (parser->lexer);
9413 /* If it's an `=', then there's an explicit value. */
9414 if (token->type == CPP_EQ)
9416 /* Consume the `=' token. */
9417 cp_lexer_consume_token (parser->lexer);
9418 /* Parse the value. */
9419 value = cp_parser_constant_expression (parser,
9420 /*allow_non_constant_p=*/false,
9426 /* Create the enumerator. */
9427 build_enumerator (identifier, value, type);
9430 /* Parse a namespace-name.
9433 original-namespace-name
9436 Returns the NAMESPACE_DECL for the namespace. */
9439 cp_parser_namespace_name (cp_parser* parser)
9442 tree namespace_decl;
9444 /* Get the name of the namespace. */
9445 identifier = cp_parser_identifier (parser);
9446 if (identifier == error_mark_node)
9447 return error_mark_node;
9449 /* Look up the identifier in the currently active scope. Look only
9450 for namespaces, due to:
9454 When looking up a namespace-name in a using-directive or alias
9455 definition, only namespace names are considered.
9461 During the lookup of a name preceding the :: scope resolution
9462 operator, object, function, and enumerator names are ignored.
9464 (Note that cp_parser_class_or_namespace_name only calls this
9465 function if the token after the name is the scope resolution
9467 namespace_decl = cp_parser_lookup_name (parser, identifier,
9469 /*is_template=*/false,
9470 /*is_namespace=*/true,
9471 /*check_dependency=*/true);
9472 /* If it's not a namespace, issue an error. */
9473 if (namespace_decl == error_mark_node
9474 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
9476 cp_parser_error (parser, "expected namespace-name");
9477 namespace_decl = error_mark_node;
9480 return namespace_decl;
9483 /* Parse a namespace-definition.
9485 namespace-definition:
9486 named-namespace-definition
9487 unnamed-namespace-definition
9489 named-namespace-definition:
9490 original-namespace-definition
9491 extension-namespace-definition
9493 original-namespace-definition:
9494 namespace identifier { namespace-body }
9496 extension-namespace-definition:
9497 namespace original-namespace-name { namespace-body }
9499 unnamed-namespace-definition:
9500 namespace { namespace-body } */
9503 cp_parser_namespace_definition (cp_parser* parser)
9507 /* Look for the `namespace' keyword. */
9508 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
9510 /* Get the name of the namespace. We do not attempt to distinguish
9511 between an original-namespace-definition and an
9512 extension-namespace-definition at this point. The semantic
9513 analysis routines are responsible for that. */
9514 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
9515 identifier = cp_parser_identifier (parser);
9517 identifier = NULL_TREE;
9519 /* Look for the `{' to start the namespace. */
9520 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
9521 /* Start the namespace. */
9522 push_namespace (identifier);
9523 /* Parse the body of the namespace. */
9524 cp_parser_namespace_body (parser);
9525 /* Finish the namespace. */
9527 /* Look for the final `}'. */
9528 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
9531 /* Parse a namespace-body.
9534 declaration-seq [opt] */
9537 cp_parser_namespace_body (cp_parser* parser)
9539 cp_parser_declaration_seq_opt (parser);
9542 /* Parse a namespace-alias-definition.
9544 namespace-alias-definition:
9545 namespace identifier = qualified-namespace-specifier ; */
9548 cp_parser_namespace_alias_definition (cp_parser* parser)
9551 tree namespace_specifier;
9553 /* Look for the `namespace' keyword. */
9554 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
9555 /* Look for the identifier. */
9556 identifier = cp_parser_identifier (parser);
9557 if (identifier == error_mark_node)
9559 /* Look for the `=' token. */
9560 cp_parser_require (parser, CPP_EQ, "`='");
9561 /* Look for the qualified-namespace-specifier. */
9563 = cp_parser_qualified_namespace_specifier (parser);
9564 /* Look for the `;' token. */
9565 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
9567 /* Register the alias in the symbol table. */
9568 do_namespace_alias (identifier, namespace_specifier);
9571 /* Parse a qualified-namespace-specifier.
9573 qualified-namespace-specifier:
9574 :: [opt] nested-name-specifier [opt] namespace-name
9576 Returns a NAMESPACE_DECL corresponding to the specified
9580 cp_parser_qualified_namespace_specifier (cp_parser* parser)
9582 /* Look for the optional `::'. */
9583 cp_parser_global_scope_opt (parser,
9584 /*current_scope_valid_p=*/false);
9586 /* Look for the optional nested-name-specifier. */
9587 cp_parser_nested_name_specifier_opt (parser,
9588 /*typename_keyword_p=*/false,
9589 /*check_dependency_p=*/true,
9591 /*is_declaration=*/true);
9593 return cp_parser_namespace_name (parser);
9596 /* Parse a using-declaration.
9599 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
9600 using :: unqualified-id ; */
9603 cp_parser_using_declaration (cp_parser* parser)
9606 bool typename_p = false;
9607 bool global_scope_p;
9613 /* Look for the `using' keyword. */
9614 cp_parser_require_keyword (parser, RID_USING, "`using'");
9616 /* Peek at the next token. */
9617 token = cp_lexer_peek_token (parser->lexer);
9618 /* See if it's `typename'. */
9619 if (token->keyword == RID_TYPENAME)
9621 /* Remember that we've seen it. */
9623 /* Consume the `typename' token. */
9624 cp_lexer_consume_token (parser->lexer);
9627 /* Look for the optional global scope qualification. */
9629 = (cp_parser_global_scope_opt (parser,
9630 /*current_scope_valid_p=*/false)
9633 /* If we saw `typename', or didn't see `::', then there must be a
9634 nested-name-specifier present. */
9635 if (typename_p || !global_scope_p)
9636 qscope = cp_parser_nested_name_specifier (parser, typename_p,
9637 /*check_dependency_p=*/true,
9639 /*is_declaration=*/true);
9640 /* Otherwise, we could be in either of the two productions. In that
9641 case, treat the nested-name-specifier as optional. */
9643 qscope = cp_parser_nested_name_specifier_opt (parser,
9644 /*typename_keyword_p=*/false,
9645 /*check_dependency_p=*/true,
9647 /*is_declaration=*/true);
9649 qscope = global_namespace;
9651 /* Parse the unqualified-id. */
9652 identifier = cp_parser_unqualified_id (parser,
9653 /*template_keyword_p=*/false,
9654 /*check_dependency_p=*/true,
9655 /*declarator_p=*/true);
9657 /* The function we call to handle a using-declaration is different
9658 depending on what scope we are in. */
9659 if (identifier == error_mark_node)
9661 else if (TREE_CODE (identifier) != IDENTIFIER_NODE
9662 && TREE_CODE (identifier) != BIT_NOT_EXPR)
9663 /* [namespace.udecl]
9665 A using declaration shall not name a template-id. */
9666 error ("a template-id may not appear in a using-declaration");
9669 scope = current_scope ();
9670 if (scope && TYPE_P (scope))
9672 /* Create the USING_DECL. */
9673 decl = do_class_using_decl (build_nt (SCOPE_REF,
9676 /* Add it to the list of members in this class. */
9677 finish_member_declaration (decl);
9681 decl = cp_parser_lookup_name_simple (parser, identifier);
9682 if (decl == error_mark_node)
9683 cp_parser_name_lookup_error (parser, identifier, decl, NULL);
9685 do_local_using_decl (decl, qscope, identifier);
9687 do_toplevel_using_decl (decl, qscope, identifier);
9691 /* Look for the final `;'. */
9692 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
9695 /* Parse a using-directive.
9698 using namespace :: [opt] nested-name-specifier [opt]
9702 cp_parser_using_directive (cp_parser* parser)
9704 tree namespace_decl;
9707 /* Look for the `using' keyword. */
9708 cp_parser_require_keyword (parser, RID_USING, "`using'");
9709 /* And the `namespace' keyword. */
9710 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
9711 /* Look for the optional `::' operator. */
9712 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
9713 /* And the optional nested-name-specifier. */
9714 cp_parser_nested_name_specifier_opt (parser,
9715 /*typename_keyword_p=*/false,
9716 /*check_dependency_p=*/true,
9718 /*is_declaration=*/true);
9719 /* Get the namespace being used. */
9720 namespace_decl = cp_parser_namespace_name (parser);
9721 /* And any specified attributes. */
9722 attribs = cp_parser_attributes_opt (parser);
9723 /* Update the symbol table. */
9724 parse_using_directive (namespace_decl, attribs);
9725 /* Look for the final `;'. */
9726 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
9729 /* Parse an asm-definition.
9732 asm ( string-literal ) ;
9737 asm volatile [opt] ( string-literal ) ;
9738 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
9739 asm volatile [opt] ( string-literal : asm-operand-list [opt]
9740 : asm-operand-list [opt] ) ;
9741 asm volatile [opt] ( string-literal : asm-operand-list [opt]
9742 : asm-operand-list [opt]
9743 : asm-operand-list [opt] ) ; */
9746 cp_parser_asm_definition (cp_parser* parser)
9750 tree outputs = NULL_TREE;
9751 tree inputs = NULL_TREE;
9752 tree clobbers = NULL_TREE;
9754 bool volatile_p = false;
9755 bool extended_p = false;
9757 /* Look for the `asm' keyword. */
9758 cp_parser_require_keyword (parser, RID_ASM, "`asm'");
9759 /* See if the next token is `volatile'. */
9760 if (cp_parser_allow_gnu_extensions_p (parser)
9761 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
9763 /* Remember that we saw the `volatile' keyword. */
9765 /* Consume the token. */
9766 cp_lexer_consume_token (parser->lexer);
9768 /* Look for the opening `('. */
9769 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
9770 /* Look for the string. */
9771 token = cp_parser_require (parser, CPP_STRING, "asm body");
9774 string = token->value;
9775 /* If we're allowing GNU extensions, check for the extended assembly
9776 syntax. Unfortunately, the `:' tokens need not be separated by
9777 a space in C, and so, for compatibility, we tolerate that here
9778 too. Doing that means that we have to treat the `::' operator as
9780 if (cp_parser_allow_gnu_extensions_p (parser)
9781 && at_function_scope_p ()
9782 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
9783 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
9785 bool inputs_p = false;
9786 bool clobbers_p = false;
9788 /* The extended syntax was used. */
9791 /* Look for outputs. */
9792 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
9794 /* Consume the `:'. */
9795 cp_lexer_consume_token (parser->lexer);
9796 /* Parse the output-operands. */
9797 if (cp_lexer_next_token_is_not (parser->lexer,
9799 && cp_lexer_next_token_is_not (parser->lexer,
9801 && cp_lexer_next_token_is_not (parser->lexer,
9803 outputs = cp_parser_asm_operand_list (parser);
9805 /* If the next token is `::', there are no outputs, and the
9806 next token is the beginning of the inputs. */
9807 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
9808 /* The inputs are coming next. */
9811 /* Look for inputs. */
9813 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
9815 /* Consume the `:' or `::'. */
9816 cp_lexer_consume_token (parser->lexer);
9817 /* Parse the output-operands. */
9818 if (cp_lexer_next_token_is_not (parser->lexer,
9820 && cp_lexer_next_token_is_not (parser->lexer,
9822 inputs = cp_parser_asm_operand_list (parser);
9824 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
9825 /* The clobbers are coming next. */
9828 /* Look for clobbers. */
9830 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
9832 /* Consume the `:' or `::'. */
9833 cp_lexer_consume_token (parser->lexer);
9834 /* Parse the clobbers. */
9835 if (cp_lexer_next_token_is_not (parser->lexer,
9837 clobbers = cp_parser_asm_clobber_list (parser);
9840 /* Look for the closing `)'. */
9841 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
9842 cp_parser_skip_to_closing_parenthesis (parser, true, false,
9843 /*consume_paren=*/true);
9844 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
9846 /* Create the ASM_STMT. */
9847 if (at_function_scope_p ())
9850 finish_asm_stmt (volatile_p
9851 ? ridpointers[(int) RID_VOLATILE] : NULL_TREE,
9852 string, outputs, inputs, clobbers);
9853 /* If the extended syntax was not used, mark the ASM_STMT. */
9855 ASM_INPUT_P (asm_stmt) = 1;
9858 assemble_asm (string);
9861 /* Declarators [gram.dcl.decl] */
9863 /* Parse an init-declarator.
9866 declarator initializer [opt]
9871 declarator asm-specification [opt] attributes [opt] initializer [opt]
9873 function-definition:
9874 decl-specifier-seq [opt] declarator ctor-initializer [opt]
9876 decl-specifier-seq [opt] declarator function-try-block
9880 function-definition:
9881 __extension__ function-definition
9883 The DECL_SPECIFIERS and PREFIX_ATTRIBUTES apply to this declarator.
9884 Returns a representation of the entity declared. If MEMBER_P is TRUE,
9885 then this declarator appears in a class scope. The new DECL created
9886 by this declarator is returned.
9888 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
9889 for a function-definition here as well. If the declarator is a
9890 declarator for a function-definition, *FUNCTION_DEFINITION_P will
9891 be TRUE upon return. By that point, the function-definition will
9892 have been completely parsed.
9894 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
9898 cp_parser_init_declarator (cp_parser* parser,
9899 tree decl_specifiers,
9900 tree prefix_attributes,
9901 bool function_definition_allowed_p,
9903 int declares_class_or_enum,
9904 bool* function_definition_p)
9909 tree asm_specification;
9911 tree decl = NULL_TREE;
9913 bool is_initialized;
9914 bool is_parenthesized_init;
9915 bool is_non_constant_init;
9916 int ctor_dtor_or_conv_p;
9920 /* Assume that this is not the declarator for a function
9922 if (function_definition_p)
9923 *function_definition_p = false;
9925 /* Defer access checks while parsing the declarator; we cannot know
9926 what names are accessible until we know what is being
9928 resume_deferring_access_checks ();
9930 /* Parse the declarator. */
9932 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
9933 &ctor_dtor_or_conv_p,
9934 /*parenthesized_p=*/NULL,
9935 /*member_p=*/false);
9936 /* Gather up the deferred checks. */
9937 stop_deferring_access_checks ();
9939 /* If the DECLARATOR was erroneous, there's no need to go
9941 if (declarator == error_mark_node)
9942 return error_mark_node;
9944 cp_parser_check_for_definition_in_return_type (declarator,
9945 declares_class_or_enum);
9947 /* Figure out what scope the entity declared by the DECLARATOR is
9948 located in. `grokdeclarator' sometimes changes the scope, so
9949 we compute it now. */
9950 scope = get_scope_of_declarator (declarator);
9952 /* If we're allowing GNU extensions, look for an asm-specification
9954 if (cp_parser_allow_gnu_extensions_p (parser))
9956 /* Look for an asm-specification. */
9957 asm_specification = cp_parser_asm_specification_opt (parser);
9958 /* And attributes. */
9959 attributes = cp_parser_attributes_opt (parser);
9963 asm_specification = NULL_TREE;
9964 attributes = NULL_TREE;
9967 /* Peek at the next token. */
9968 token = cp_lexer_peek_token (parser->lexer);
9969 /* Check to see if the token indicates the start of a
9970 function-definition. */
9971 if (cp_parser_token_starts_function_definition_p (token))
9973 if (!function_definition_allowed_p)
9975 /* If a function-definition should not appear here, issue an
9977 cp_parser_error (parser,
9978 "a function-definition is not allowed here");
9979 return error_mark_node;
9983 /* Neither attributes nor an asm-specification are allowed
9984 on a function-definition. */
9985 if (asm_specification)
9986 error ("an asm-specification is not allowed on a function-definition");
9988 error ("attributes are not allowed on a function-definition");
9989 /* This is a function-definition. */
9990 *function_definition_p = true;
9992 /* Parse the function definition. */
9994 decl = cp_parser_save_member_function_body (parser,
10000 = (cp_parser_function_definition_from_specifiers_and_declarator
10001 (parser, decl_specifiers, prefix_attributes, declarator));
10009 Only in function declarations for constructors, destructors, and
10010 type conversions can the decl-specifier-seq be omitted.
10012 We explicitly postpone this check past the point where we handle
10013 function-definitions because we tolerate function-definitions
10014 that are missing their return types in some modes. */
10015 if (!decl_specifiers && ctor_dtor_or_conv_p <= 0)
10017 cp_parser_error (parser,
10018 "expected constructor, destructor, or type conversion");
10019 return error_mark_node;
10022 /* An `=' or an `(' indicates an initializer. */
10023 is_initialized = (token->type == CPP_EQ
10024 || token->type == CPP_OPEN_PAREN);
10025 /* If the init-declarator isn't initialized and isn't followed by a
10026 `,' or `;', it's not a valid init-declarator. */
10027 if (!is_initialized
10028 && token->type != CPP_COMMA
10029 && token->type != CPP_SEMICOLON)
10031 cp_parser_error (parser, "expected init-declarator");
10032 return error_mark_node;
10035 /* Because start_decl has side-effects, we should only call it if we
10036 know we're going ahead. By this point, we know that we cannot
10037 possibly be looking at any other construct. */
10038 cp_parser_commit_to_tentative_parse (parser);
10040 /* If the decl specifiers were bad, issue an error now that we're
10041 sure this was intended to be a declarator. Then continue
10042 declaring the variable(s), as int, to try to cut down on further
10044 if (decl_specifiers != NULL
10045 && TREE_VALUE (decl_specifiers) == error_mark_node)
10047 cp_parser_error (parser, "invalid type in declaration");
10048 TREE_VALUE (decl_specifiers) = integer_type_node;
10051 /* Check to see whether or not this declaration is a friend. */
10052 friend_p = cp_parser_friend_p (decl_specifiers);
10054 /* Check that the number of template-parameter-lists is OK. */
10055 if (!cp_parser_check_declarator_template_parameters (parser, declarator))
10056 return error_mark_node;
10058 /* Enter the newly declared entry in the symbol table. If we're
10059 processing a declaration in a class-specifier, we wait until
10060 after processing the initializer. */
10063 if (parser->in_unbraced_linkage_specification_p)
10065 decl_specifiers = tree_cons (error_mark_node,
10066 get_identifier ("extern"),
10068 have_extern_spec = false;
10070 decl = start_decl (declarator, decl_specifiers,
10071 is_initialized, attributes, prefix_attributes);
10074 /* Enter the SCOPE. That way unqualified names appearing in the
10075 initializer will be looked up in SCOPE. */
10077 pop_p = push_scope (scope);
10079 /* Perform deferred access control checks, now that we know in which
10080 SCOPE the declared entity resides. */
10081 if (!member_p && decl)
10083 tree saved_current_function_decl = NULL_TREE;
10085 /* If the entity being declared is a function, pretend that we
10086 are in its scope. If it is a `friend', it may have access to
10087 things that would not otherwise be accessible. */
10088 if (TREE_CODE (decl) == FUNCTION_DECL)
10090 saved_current_function_decl = current_function_decl;
10091 current_function_decl = decl;
10094 /* Perform the access control checks for the declarator and the
10095 the decl-specifiers. */
10096 perform_deferred_access_checks ();
10098 /* Restore the saved value. */
10099 if (TREE_CODE (decl) == FUNCTION_DECL)
10100 current_function_decl = saved_current_function_decl;
10103 /* Parse the initializer. */
10104 if (is_initialized)
10105 initializer = cp_parser_initializer (parser,
10106 &is_parenthesized_init,
10107 &is_non_constant_init);
10110 initializer = NULL_TREE;
10111 is_parenthesized_init = false;
10112 is_non_constant_init = true;
10115 /* The old parser allows attributes to appear after a parenthesized
10116 initializer. Mark Mitchell proposed removing this functionality
10117 on the GCC mailing lists on 2002-08-13. This parser accepts the
10118 attributes -- but ignores them. */
10119 if (cp_parser_allow_gnu_extensions_p (parser) && is_parenthesized_init)
10120 if (cp_parser_attributes_opt (parser))
10121 warning ("attributes after parenthesized initializer ignored");
10123 /* Leave the SCOPE, now that we have processed the initializer. It
10124 is important to do this before calling cp_finish_decl because it
10125 makes decisions about whether to create DECL_STMTs or not based
10126 on the current scope. */
10130 /* For an in-class declaration, use `grokfield' to create the
10134 decl = grokfield (declarator, decl_specifiers,
10135 initializer, /*asmspec=*/NULL_TREE,
10136 /*attributes=*/NULL_TREE);
10137 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
10138 cp_parser_save_default_args (parser, decl);
10141 /* Finish processing the declaration. But, skip friend
10143 if (!friend_p && decl)
10144 cp_finish_decl (decl,
10147 /* If the initializer is in parentheses, then this is
10148 a direct-initialization, which means that an
10149 `explicit' constructor is OK. Otherwise, an
10150 `explicit' constructor cannot be used. */
10151 ((is_parenthesized_init || !is_initialized)
10152 ? 0 : LOOKUP_ONLYCONVERTING));
10154 /* Remember whether or not variables were initialized by
10155 constant-expressions. */
10156 if (decl && TREE_CODE (decl) == VAR_DECL
10157 && is_initialized && !is_non_constant_init)
10158 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = true;
10163 /* Parse a declarator.
10167 ptr-operator declarator
10169 abstract-declarator:
10170 ptr-operator abstract-declarator [opt]
10171 direct-abstract-declarator
10176 attributes [opt] direct-declarator
10177 attributes [opt] ptr-operator declarator
10179 abstract-declarator:
10180 attributes [opt] ptr-operator abstract-declarator [opt]
10181 attributes [opt] direct-abstract-declarator
10183 Returns a representation of the declarator. If the declarator has
10184 the form `* declarator', then an INDIRECT_REF is returned, whose
10185 only operand is the sub-declarator. Analogously, `& declarator' is
10186 represented as an ADDR_EXPR. For `X::* declarator', a SCOPE_REF is
10187 used. The first operand is the TYPE for `X'. The second operand
10188 is an INDIRECT_REF whose operand is the sub-declarator.
10190 Otherwise, the representation is as for a direct-declarator.
10192 (It would be better to define a structure type to represent
10193 declarators, rather than abusing `tree' nodes to represent
10194 declarators. That would be much clearer and save some memory.
10195 There is no reason for declarators to be garbage-collected, for
10196 example; they are created during parser and no longer needed after
10197 `grokdeclarator' has been called.)
10199 For a ptr-operator that has the optional cv-qualifier-seq,
10200 cv-qualifiers will be stored in the TREE_TYPE of the INDIRECT_REF
10203 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
10204 detect constructor, destructor or conversion operators. It is set
10205 to -1 if the declarator is a name, and +1 if it is a
10206 function. Otherwise it is set to zero. Usually you just want to
10207 test for >0, but internally the negative value is used.
10209 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
10210 a decl-specifier-seq unless it declares a constructor, destructor,
10211 or conversion. It might seem that we could check this condition in
10212 semantic analysis, rather than parsing, but that makes it difficult
10213 to handle something like `f()'. We want to notice that there are
10214 no decl-specifiers, and therefore realize that this is an
10215 expression, not a declaration.)
10217 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
10218 the declarator is a direct-declarator of the form "(...)".
10220 MEMBER_P is true iff this declarator is a member-declarator. */
10223 cp_parser_declarator (cp_parser* parser,
10224 cp_parser_declarator_kind dcl_kind,
10225 int* ctor_dtor_or_conv_p,
10226 bool* parenthesized_p,
10231 enum tree_code code;
10232 tree cv_qualifier_seq;
10234 tree attributes = NULL_TREE;
10236 /* Assume this is not a constructor, destructor, or type-conversion
10238 if (ctor_dtor_or_conv_p)
10239 *ctor_dtor_or_conv_p = 0;
10241 if (cp_parser_allow_gnu_extensions_p (parser))
10242 attributes = cp_parser_attributes_opt (parser);
10244 /* Peek at the next token. */
10245 token = cp_lexer_peek_token (parser->lexer);
10247 /* Check for the ptr-operator production. */
10248 cp_parser_parse_tentatively (parser);
10249 /* Parse the ptr-operator. */
10250 code = cp_parser_ptr_operator (parser,
10252 &cv_qualifier_seq);
10253 /* If that worked, then we have a ptr-operator. */
10254 if (cp_parser_parse_definitely (parser))
10256 /* If a ptr-operator was found, then this declarator was not
10258 if (parenthesized_p)
10259 *parenthesized_p = true;
10260 /* The dependent declarator is optional if we are parsing an
10261 abstract-declarator. */
10262 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
10263 cp_parser_parse_tentatively (parser);
10265 /* Parse the dependent declarator. */
10266 declarator = cp_parser_declarator (parser, dcl_kind,
10267 /*ctor_dtor_or_conv_p=*/NULL,
10268 /*parenthesized_p=*/NULL,
10269 /*member_p=*/false);
10271 /* If we are parsing an abstract-declarator, we must handle the
10272 case where the dependent declarator is absent. */
10273 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
10274 && !cp_parser_parse_definitely (parser))
10275 declarator = NULL_TREE;
10277 /* Build the representation of the ptr-operator. */
10278 if (code == INDIRECT_REF)
10279 declarator = make_pointer_declarator (cv_qualifier_seq,
10282 declarator = make_reference_declarator (cv_qualifier_seq,
10284 /* Handle the pointer-to-member case. */
10286 declarator = build_nt (SCOPE_REF, class_type, declarator);
10288 /* Everything else is a direct-declarator. */
10291 if (parenthesized_p)
10292 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
10294 declarator = cp_parser_direct_declarator (parser, dcl_kind,
10295 ctor_dtor_or_conv_p,
10299 if (attributes && declarator != error_mark_node)
10300 declarator = tree_cons (attributes, declarator, NULL_TREE);
10305 /* Parse a direct-declarator or direct-abstract-declarator.
10309 direct-declarator ( parameter-declaration-clause )
10310 cv-qualifier-seq [opt]
10311 exception-specification [opt]
10312 direct-declarator [ constant-expression [opt] ]
10315 direct-abstract-declarator:
10316 direct-abstract-declarator [opt]
10317 ( parameter-declaration-clause )
10318 cv-qualifier-seq [opt]
10319 exception-specification [opt]
10320 direct-abstract-declarator [opt] [ constant-expression [opt] ]
10321 ( abstract-declarator )
10323 Returns a representation of the declarator. DCL_KIND is
10324 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
10325 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
10326 we are parsing a direct-declarator. It is
10327 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
10328 of ambiguity we prefer an abstract declarator, as per
10329 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P is as for
10330 cp_parser_declarator.
10332 For the declarator-id production, the representation is as for an
10333 id-expression, except that a qualified name is represented as a
10334 SCOPE_REF. A function-declarator is represented as a CALL_EXPR;
10335 see the documentation of the FUNCTION_DECLARATOR_* macros for
10336 information about how to find the various declarator components.
10337 An array-declarator is represented as an ARRAY_REF. The
10338 direct-declarator is the first operand; the constant-expression
10339 indicating the size of the array is the second operand. */
10342 cp_parser_direct_declarator (cp_parser* parser,
10343 cp_parser_declarator_kind dcl_kind,
10344 int* ctor_dtor_or_conv_p,
10348 tree declarator = NULL_TREE;
10349 tree scope = NULL_TREE;
10350 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
10351 bool saved_in_declarator_p = parser->in_declarator_p;
10353 bool pop_p = false;
10357 /* Peek at the next token. */
10358 token = cp_lexer_peek_token (parser->lexer);
10359 if (token->type == CPP_OPEN_PAREN)
10361 /* This is either a parameter-declaration-clause, or a
10362 parenthesized declarator. When we know we are parsing a
10363 named declarator, it must be a parenthesized declarator
10364 if FIRST is true. For instance, `(int)' is a
10365 parameter-declaration-clause, with an omitted
10366 direct-abstract-declarator. But `((*))', is a
10367 parenthesized abstract declarator. Finally, when T is a
10368 template parameter `(T)' is a
10369 parameter-declaration-clause, and not a parenthesized
10372 We first try and parse a parameter-declaration-clause,
10373 and then try a nested declarator (if FIRST is true).
10375 It is not an error for it not to be a
10376 parameter-declaration-clause, even when FIRST is
10382 The first is the declaration of a function while the
10383 second is a the definition of a variable, including its
10386 Having seen only the parenthesis, we cannot know which of
10387 these two alternatives should be selected. Even more
10388 complex are examples like:
10393 The former is a function-declaration; the latter is a
10394 variable initialization.
10396 Thus again, we try a parameter-declaration-clause, and if
10397 that fails, we back out and return. */
10399 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
10402 unsigned saved_num_template_parameter_lists;
10404 /* In a member-declarator, the only valid interpretation
10405 of a parenthesis is the start of a
10406 parameter-declaration-clause. (It is invalid to
10407 initialize a static data member with a parenthesized
10408 initializer; only the "=" form of initialization is
10411 cp_parser_parse_tentatively (parser);
10413 /* Consume the `('. */
10414 cp_lexer_consume_token (parser->lexer);
10417 /* If this is going to be an abstract declarator, we're
10418 in a declarator and we can't have default args. */
10419 parser->default_arg_ok_p = false;
10420 parser->in_declarator_p = true;
10423 /* Inside the function parameter list, surrounding
10424 template-parameter-lists do not apply. */
10425 saved_num_template_parameter_lists
10426 = parser->num_template_parameter_lists;
10427 parser->num_template_parameter_lists = 0;
10429 /* Parse the parameter-declaration-clause. */
10430 params = cp_parser_parameter_declaration_clause (parser);
10432 parser->num_template_parameter_lists
10433 = saved_num_template_parameter_lists;
10435 /* If all went well, parse the cv-qualifier-seq and the
10436 exception-specification. */
10437 if (member_p || cp_parser_parse_definitely (parser))
10439 tree cv_qualifiers;
10440 tree exception_specification;
10442 if (ctor_dtor_or_conv_p)
10443 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
10445 /* Consume the `)'. */
10446 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
10448 /* Parse the cv-qualifier-seq. */
10449 cv_qualifiers = cp_parser_cv_qualifier_seq_opt (parser);
10450 /* And the exception-specification. */
10451 exception_specification
10452 = cp_parser_exception_specification_opt (parser);
10454 /* Create the function-declarator. */
10455 declarator = make_call_declarator (declarator,
10458 exception_specification);
10459 /* Any subsequent parameter lists are to do with
10460 return type, so are not those of the declared
10462 parser->default_arg_ok_p = false;
10464 /* Repeat the main loop. */
10469 /* If this is the first, we can try a parenthesized
10473 bool saved_in_type_id_in_expr_p;
10475 parser->default_arg_ok_p = saved_default_arg_ok_p;
10476 parser->in_declarator_p = saved_in_declarator_p;
10478 /* Consume the `('. */
10479 cp_lexer_consume_token (parser->lexer);
10480 /* Parse the nested declarator. */
10481 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
10482 parser->in_type_id_in_expr_p = true;
10484 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
10485 /*parenthesized_p=*/NULL,
10487 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
10489 /* Expect a `)'. */
10490 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
10491 declarator = error_mark_node;
10492 if (declarator == error_mark_node)
10495 goto handle_declarator;
10497 /* Otherwise, we must be done. */
10501 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
10502 && token->type == CPP_OPEN_SQUARE)
10504 /* Parse an array-declarator. */
10507 if (ctor_dtor_or_conv_p)
10508 *ctor_dtor_or_conv_p = 0;
10511 parser->default_arg_ok_p = false;
10512 parser->in_declarator_p = true;
10513 /* Consume the `['. */
10514 cp_lexer_consume_token (parser->lexer);
10515 /* Peek at the next token. */
10516 token = cp_lexer_peek_token (parser->lexer);
10517 /* If the next token is `]', then there is no
10518 constant-expression. */
10519 if (token->type != CPP_CLOSE_SQUARE)
10521 bool non_constant_p;
10524 = cp_parser_constant_expression (parser,
10525 /*allow_non_constant=*/true,
10527 if (!non_constant_p)
10528 bounds = fold_non_dependent_expr (bounds);
10531 bounds = NULL_TREE;
10532 /* Look for the closing `]'. */
10533 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'"))
10535 declarator = error_mark_node;
10539 declarator = build_nt (ARRAY_REF, declarator, bounds);
10541 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
10543 /* Parse a declarator-id */
10544 if (dcl_kind == CP_PARSER_DECLARATOR_EITHER)
10545 cp_parser_parse_tentatively (parser);
10546 declarator = cp_parser_declarator_id (parser);
10547 if (dcl_kind == CP_PARSER_DECLARATOR_EITHER)
10549 if (!cp_parser_parse_definitely (parser))
10550 declarator = error_mark_node;
10551 else if (TREE_CODE (declarator) != IDENTIFIER_NODE)
10553 cp_parser_error (parser, "expected unqualified-id");
10554 declarator = error_mark_node;
10558 if (declarator == error_mark_node)
10561 if (TREE_CODE (declarator) == SCOPE_REF
10562 && !current_scope ())
10564 tree scope = TREE_OPERAND (declarator, 0);
10566 /* In the declaration of a member of a template class
10567 outside of the class itself, the SCOPE will sometimes
10568 be a TYPENAME_TYPE. For example, given:
10570 template <typename T>
10571 int S<T>::R::i = 3;
10573 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
10574 this context, we must resolve S<T>::R to an ordinary
10575 type, rather than a typename type.
10577 The reason we normally avoid resolving TYPENAME_TYPEs
10578 is that a specialization of `S' might render
10579 `S<T>::R' not a type. However, if `S' is
10580 specialized, then this `i' will not be used, so there
10581 is no harm in resolving the types here. */
10582 if (TREE_CODE (scope) == TYPENAME_TYPE)
10586 /* Resolve the TYPENAME_TYPE. */
10587 type = resolve_typename_type (scope,
10588 /*only_current_p=*/false);
10589 /* If that failed, the declarator is invalid. */
10590 if (type == error_mark_node)
10591 error ("`%T::%D' is not a type",
10592 TYPE_CONTEXT (scope),
10593 TYPE_IDENTIFIER (scope));
10594 /* Build a new DECLARATOR. */
10595 declarator = build_nt (SCOPE_REF,
10597 TREE_OPERAND (declarator, 1));
10601 /* Check to see whether the declarator-id names a constructor,
10602 destructor, or conversion. */
10603 if (declarator && ctor_dtor_or_conv_p
10604 && ((TREE_CODE (declarator) == SCOPE_REF
10605 && CLASS_TYPE_P (TREE_OPERAND (declarator, 0)))
10606 || (TREE_CODE (declarator) != SCOPE_REF
10607 && at_class_scope_p ())))
10609 tree unqualified_name;
10612 /* Get the unqualified part of the name. */
10613 if (TREE_CODE (declarator) == SCOPE_REF)
10615 class_type = TREE_OPERAND (declarator, 0);
10616 unqualified_name = TREE_OPERAND (declarator, 1);
10620 class_type = current_class_type;
10621 unqualified_name = declarator;
10624 /* See if it names ctor, dtor or conv. */
10625 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR
10626 || IDENTIFIER_TYPENAME_P (unqualified_name)
10627 || constructor_name_p (unqualified_name, class_type)
10628 || (TREE_CODE (unqualified_name) == TYPE_DECL
10629 && same_type_p (TREE_TYPE (unqualified_name),
10631 *ctor_dtor_or_conv_p = -1;
10634 handle_declarator:;
10635 scope = get_scope_of_declarator (declarator);
10637 /* Any names that appear after the declarator-id for a
10638 member are looked up in the containing scope. */
10639 pop_p = push_scope (scope);
10640 parser->in_declarator_p = true;
10641 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
10643 && (TREE_CODE (declarator) == SCOPE_REF
10644 || TREE_CODE (declarator) == IDENTIFIER_NODE)))
10645 /* Default args are only allowed on function
10647 parser->default_arg_ok_p = saved_default_arg_ok_p;
10649 parser->default_arg_ok_p = false;
10658 /* For an abstract declarator, we might wind up with nothing at this
10659 point. That's an error; the declarator is not optional. */
10661 cp_parser_error (parser, "expected declarator");
10663 /* If we entered a scope, we must exit it now. */
10667 parser->default_arg_ok_p = saved_default_arg_ok_p;
10668 parser->in_declarator_p = saved_in_declarator_p;
10673 /* Parse a ptr-operator.
10676 * cv-qualifier-seq [opt]
10678 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
10683 & cv-qualifier-seq [opt]
10685 Returns INDIRECT_REF if a pointer, or pointer-to-member, was
10686 used. Returns ADDR_EXPR if a reference was used. In the
10687 case of a pointer-to-member, *TYPE is filled in with the
10688 TYPE containing the member. *CV_QUALIFIER_SEQ is filled in
10689 with the cv-qualifier-seq, or NULL_TREE, if there are no
10690 cv-qualifiers. Returns ERROR_MARK if an error occurred. */
10692 static enum tree_code
10693 cp_parser_ptr_operator (cp_parser* parser,
10695 tree* cv_qualifier_seq)
10697 enum tree_code code = ERROR_MARK;
10700 /* Assume that it's not a pointer-to-member. */
10702 /* And that there are no cv-qualifiers. */
10703 *cv_qualifier_seq = NULL_TREE;
10705 /* Peek at the next token. */
10706 token = cp_lexer_peek_token (parser->lexer);
10707 /* If it's a `*' or `&' we have a pointer or reference. */
10708 if (token->type == CPP_MULT || token->type == CPP_AND)
10710 /* Remember which ptr-operator we were processing. */
10711 code = (token->type == CPP_AND ? ADDR_EXPR : INDIRECT_REF);
10713 /* Consume the `*' or `&'. */
10714 cp_lexer_consume_token (parser->lexer);
10716 /* A `*' can be followed by a cv-qualifier-seq, and so can a
10717 `&', if we are allowing GNU extensions. (The only qualifier
10718 that can legally appear after `&' is `restrict', but that is
10719 enforced during semantic analysis. */
10720 if (code == INDIRECT_REF
10721 || cp_parser_allow_gnu_extensions_p (parser))
10722 *cv_qualifier_seq = cp_parser_cv_qualifier_seq_opt (parser);
10726 /* Try the pointer-to-member case. */
10727 cp_parser_parse_tentatively (parser);
10728 /* Look for the optional `::' operator. */
10729 cp_parser_global_scope_opt (parser,
10730 /*current_scope_valid_p=*/false);
10731 /* Look for the nested-name specifier. */
10732 cp_parser_nested_name_specifier (parser,
10733 /*typename_keyword_p=*/false,
10734 /*check_dependency_p=*/true,
10736 /*is_declaration=*/false);
10737 /* If we found it, and the next token is a `*', then we are
10738 indeed looking at a pointer-to-member operator. */
10739 if (!cp_parser_error_occurred (parser)
10740 && cp_parser_require (parser, CPP_MULT, "`*'"))
10742 /* The type of which the member is a member is given by the
10744 *type = parser->scope;
10745 /* The next name will not be qualified. */
10746 parser->scope = NULL_TREE;
10747 parser->qualifying_scope = NULL_TREE;
10748 parser->object_scope = NULL_TREE;
10749 /* Indicate that the `*' operator was used. */
10750 code = INDIRECT_REF;
10751 /* Look for the optional cv-qualifier-seq. */
10752 *cv_qualifier_seq = cp_parser_cv_qualifier_seq_opt (parser);
10754 /* If that didn't work we don't have a ptr-operator. */
10755 if (!cp_parser_parse_definitely (parser))
10756 cp_parser_error (parser, "expected ptr-operator");
10762 /* Parse an (optional) cv-qualifier-seq.
10765 cv-qualifier cv-qualifier-seq [opt]
10767 Returns a TREE_LIST. The TREE_VALUE of each node is the
10768 representation of a cv-qualifier. */
10771 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
10773 tree cv_qualifiers = NULL_TREE;
10779 /* Look for the next cv-qualifier. */
10780 cv_qualifier = cp_parser_cv_qualifier_opt (parser);
10781 /* If we didn't find one, we're done. */
10785 /* Add this cv-qualifier to the list. */
10787 = tree_cons (NULL_TREE, cv_qualifier, cv_qualifiers);
10790 /* We built up the list in reverse order. */
10791 return nreverse (cv_qualifiers);
10794 /* Parse an (optional) cv-qualifier.
10806 cp_parser_cv_qualifier_opt (cp_parser* parser)
10809 tree cv_qualifier = NULL_TREE;
10811 /* Peek at the next token. */
10812 token = cp_lexer_peek_token (parser->lexer);
10813 /* See if it's a cv-qualifier. */
10814 switch (token->keyword)
10819 /* Save the value of the token. */
10820 cv_qualifier = token->value;
10821 /* Consume the token. */
10822 cp_lexer_consume_token (parser->lexer);
10829 return cv_qualifier;
10832 /* Parse a declarator-id.
10836 :: [opt] nested-name-specifier [opt] type-name
10838 In the `id-expression' case, the value returned is as for
10839 cp_parser_id_expression if the id-expression was an unqualified-id.
10840 If the id-expression was a qualified-id, then a SCOPE_REF is
10841 returned. The first operand is the scope (either a NAMESPACE_DECL
10842 or TREE_TYPE), but the second is still just a representation of an
10846 cp_parser_declarator_id (cp_parser* parser)
10848 tree id_expression;
10850 /* The expression must be an id-expression. Assume that qualified
10851 names are the names of types so that:
10854 int S<T>::R::i = 3;
10856 will work; we must treat `S<T>::R' as the name of a type.
10857 Similarly, assume that qualified names are templates, where
10861 int S<T>::R<T>::i = 3;
10864 id_expression = cp_parser_id_expression (parser,
10865 /*template_keyword_p=*/false,
10866 /*check_dependency_p=*/false,
10867 /*template_p=*/NULL,
10868 /*declarator_p=*/true);
10869 /* If the name was qualified, create a SCOPE_REF to represent
10873 id_expression = build_nt (SCOPE_REF, parser->scope, id_expression);
10874 parser->scope = NULL_TREE;
10877 return id_expression;
10880 /* Parse a type-id.
10883 type-specifier-seq abstract-declarator [opt]
10885 Returns the TYPE specified. */
10888 cp_parser_type_id (cp_parser* parser)
10890 tree type_specifier_seq;
10891 tree abstract_declarator;
10893 /* Parse the type-specifier-seq. */
10895 = cp_parser_type_specifier_seq (parser);
10896 if (type_specifier_seq == error_mark_node)
10897 return error_mark_node;
10899 /* There might or might not be an abstract declarator. */
10900 cp_parser_parse_tentatively (parser);
10901 /* Look for the declarator. */
10902 abstract_declarator
10903 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
10904 /*parenthesized_p=*/NULL,
10905 /*member_p=*/false);
10906 /* Check to see if there really was a declarator. */
10907 if (!cp_parser_parse_definitely (parser))
10908 abstract_declarator = NULL_TREE;
10910 return groktypename (build_tree_list (type_specifier_seq,
10911 abstract_declarator));
10914 /* Parse a type-specifier-seq.
10916 type-specifier-seq:
10917 type-specifier type-specifier-seq [opt]
10921 type-specifier-seq:
10922 attributes type-specifier-seq [opt]
10924 Returns a TREE_LIST. Either the TREE_VALUE of each node is a
10925 type-specifier, or the TREE_PURPOSE is a list of attributes. */
10928 cp_parser_type_specifier_seq (cp_parser* parser)
10930 bool seen_type_specifier = false;
10931 tree type_specifier_seq = NULL_TREE;
10933 /* Parse the type-specifiers and attributes. */
10936 tree type_specifier;
10938 /* Check for attributes first. */
10939 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
10941 type_specifier_seq = tree_cons (cp_parser_attributes_opt (parser),
10943 type_specifier_seq);
10947 /* After the first type-specifier, others are optional. */
10948 if (seen_type_specifier)
10949 cp_parser_parse_tentatively (parser);
10950 /* Look for the type-specifier. */
10951 type_specifier = cp_parser_type_specifier (parser,
10952 CP_PARSER_FLAGS_NONE,
10953 /*is_friend=*/false,
10954 /*is_declaration=*/false,
10957 /* If the first type-specifier could not be found, this is not a
10958 type-specifier-seq at all. */
10959 if (!seen_type_specifier && type_specifier == error_mark_node)
10960 return error_mark_node;
10961 /* If subsequent type-specifiers could not be found, the
10962 type-specifier-seq is complete. */
10963 else if (seen_type_specifier && !cp_parser_parse_definitely (parser))
10966 /* Add the new type-specifier to the list. */
10968 = tree_cons (NULL_TREE, type_specifier, type_specifier_seq);
10969 seen_type_specifier = true;
10972 /* We built up the list in reverse order. */
10973 return nreverse (type_specifier_seq);
10976 /* Parse a parameter-declaration-clause.
10978 parameter-declaration-clause:
10979 parameter-declaration-list [opt] ... [opt]
10980 parameter-declaration-list , ...
10982 Returns a representation for the parameter declarations. Each node
10983 is a TREE_LIST. (See cp_parser_parameter_declaration for the exact
10984 representation.) If the parameter-declaration-clause ends with an
10985 ellipsis, PARMLIST_ELLIPSIS_P will hold of the first node in the
10986 list. A return value of NULL_TREE indicates a
10987 parameter-declaration-clause consisting only of an ellipsis. */
10990 cp_parser_parameter_declaration_clause (cp_parser* parser)
10996 /* Peek at the next token. */
10997 token = cp_lexer_peek_token (parser->lexer);
10998 /* Check for trivial parameter-declaration-clauses. */
10999 if (token->type == CPP_ELLIPSIS)
11001 /* Consume the `...' token. */
11002 cp_lexer_consume_token (parser->lexer);
11005 else if (token->type == CPP_CLOSE_PAREN)
11006 /* There are no parameters. */
11008 #ifndef NO_IMPLICIT_EXTERN_C
11009 if (in_system_header && current_class_type == NULL
11010 && current_lang_name == lang_name_c)
11014 return void_list_node;
11016 /* Check for `(void)', too, which is a special case. */
11017 else if (token->keyword == RID_VOID
11018 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
11019 == CPP_CLOSE_PAREN))
11021 /* Consume the `void' token. */
11022 cp_lexer_consume_token (parser->lexer);
11023 /* There are no parameters. */
11024 return void_list_node;
11027 /* Parse the parameter-declaration-list. */
11028 parameters = cp_parser_parameter_declaration_list (parser);
11029 /* If a parse error occurred while parsing the
11030 parameter-declaration-list, then the entire
11031 parameter-declaration-clause is erroneous. */
11032 if (parameters == error_mark_node)
11033 return error_mark_node;
11035 /* Peek at the next token. */
11036 token = cp_lexer_peek_token (parser->lexer);
11037 /* If it's a `,', the clause should terminate with an ellipsis. */
11038 if (token->type == CPP_COMMA)
11040 /* Consume the `,'. */
11041 cp_lexer_consume_token (parser->lexer);
11042 /* Expect an ellipsis. */
11044 = (cp_parser_require (parser, CPP_ELLIPSIS, "`...'") != NULL);
11046 /* It might also be `...' if the optional trailing `,' was
11048 else if (token->type == CPP_ELLIPSIS)
11050 /* Consume the `...' token. */
11051 cp_lexer_consume_token (parser->lexer);
11052 /* And remember that we saw it. */
11056 ellipsis_p = false;
11058 /* Finish the parameter list. */
11059 return finish_parmlist (parameters, ellipsis_p);
11062 /* Parse a parameter-declaration-list.
11064 parameter-declaration-list:
11065 parameter-declaration
11066 parameter-declaration-list , parameter-declaration
11068 Returns a representation of the parameter-declaration-list, as for
11069 cp_parser_parameter_declaration_clause. However, the
11070 `void_list_node' is never appended to the list. */
11073 cp_parser_parameter_declaration_list (cp_parser* parser)
11075 tree parameters = NULL_TREE;
11077 /* Look for more parameters. */
11081 bool parenthesized_p;
11082 /* Parse the parameter. */
11084 = cp_parser_parameter_declaration (parser,
11085 /*template_parm_p=*/false,
11088 /* If a parse error occurred parsing the parameter declaration,
11089 then the entire parameter-declaration-list is erroneous. */
11090 if (parameter == error_mark_node)
11092 parameters = error_mark_node;
11095 /* Add the new parameter to the list. */
11096 TREE_CHAIN (parameter) = parameters;
11097 parameters = parameter;
11099 /* Peek at the next token. */
11100 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
11101 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11102 /* The parameter-declaration-list is complete. */
11104 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
11108 /* Peek at the next token. */
11109 token = cp_lexer_peek_nth_token (parser->lexer, 2);
11110 /* If it's an ellipsis, then the list is complete. */
11111 if (token->type == CPP_ELLIPSIS)
11113 /* Otherwise, there must be more parameters. Consume the
11115 cp_lexer_consume_token (parser->lexer);
11116 /* When parsing something like:
11118 int i(float f, double d)
11120 we can tell after seeing the declaration for "f" that we
11121 are not looking at an initialization of a variable "i",
11122 but rather at the declaration of a function "i".
11124 Due to the fact that the parsing of template arguments
11125 (as specified to a template-id) requires backtracking we
11126 cannot use this technique when inside a template argument
11128 if (!parser->in_template_argument_list_p
11129 && !parser->in_type_id_in_expr_p
11130 && cp_parser_parsing_tentatively (parser)
11131 && !cp_parser_committed_to_tentative_parse (parser)
11132 /* However, a parameter-declaration of the form
11133 "foat(f)" (which is a valid declaration of a
11134 parameter "f") can also be interpreted as an
11135 expression (the conversion of "f" to "float"). */
11136 && !parenthesized_p)
11137 cp_parser_commit_to_tentative_parse (parser);
11141 cp_parser_error (parser, "expected `,' or `...'");
11142 if (!cp_parser_parsing_tentatively (parser)
11143 || cp_parser_committed_to_tentative_parse (parser))
11144 cp_parser_skip_to_closing_parenthesis (parser,
11145 /*recovering=*/true,
11146 /*or_comma=*/false,
11147 /*consume_paren=*/false);
11152 /* We built up the list in reverse order; straighten it out now. */
11153 return nreverse (parameters);
11156 /* Parse a parameter declaration.
11158 parameter-declaration:
11159 decl-specifier-seq declarator
11160 decl-specifier-seq declarator = assignment-expression
11161 decl-specifier-seq abstract-declarator [opt]
11162 decl-specifier-seq abstract-declarator [opt] = assignment-expression
11164 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
11165 declares a template parameter. (In that case, a non-nested `>'
11166 token encountered during the parsing of the assignment-expression
11167 is not interpreted as a greater-than operator.)
11169 Returns a TREE_LIST representing the parameter-declaration. The
11170 TREE_PURPOSE is the default argument expression, or NULL_TREE if
11171 there is no default argument. The TREE_VALUE is a representation
11172 of the decl-specifier-seq and declarator. In particular, the
11173 TREE_VALUE will be a TREE_LIST whose TREE_PURPOSE represents the
11174 decl-specifier-seq and whose TREE_VALUE represents the declarator.
11175 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
11176 the declarator is of the form "(p)". */
11179 cp_parser_parameter_declaration (cp_parser *parser,
11180 bool template_parm_p,
11181 bool *parenthesized_p)
11183 int declares_class_or_enum;
11184 bool greater_than_is_operator_p;
11185 tree decl_specifiers;
11188 tree default_argument;
11191 const char *saved_message;
11193 /* In a template parameter, `>' is not an operator.
11197 When parsing a default template-argument for a non-type
11198 template-parameter, the first non-nested `>' is taken as the end
11199 of the template parameter-list rather than a greater-than
11201 greater_than_is_operator_p = !template_parm_p;
11203 /* Type definitions may not appear in parameter types. */
11204 saved_message = parser->type_definition_forbidden_message;
11205 parser->type_definition_forbidden_message
11206 = "types may not be defined in parameter types";
11208 /* Parse the declaration-specifiers. */
11210 = cp_parser_decl_specifier_seq (parser,
11211 CP_PARSER_FLAGS_NONE,
11213 &declares_class_or_enum);
11214 /* If an error occurred, there's no reason to attempt to parse the
11215 rest of the declaration. */
11216 if (cp_parser_error_occurred (parser))
11218 parser->type_definition_forbidden_message = saved_message;
11219 return error_mark_node;
11222 /* Peek at the next token. */
11223 token = cp_lexer_peek_token (parser->lexer);
11224 /* If the next token is a `)', `,', `=', `>', or `...', then there
11225 is no declarator. */
11226 if (token->type == CPP_CLOSE_PAREN
11227 || token->type == CPP_COMMA
11228 || token->type == CPP_EQ
11229 || token->type == CPP_ELLIPSIS
11230 || token->type == CPP_GREATER)
11232 declarator = NULL_TREE;
11233 if (parenthesized_p)
11234 *parenthesized_p = false;
11236 /* Otherwise, there should be a declarator. */
11239 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
11240 parser->default_arg_ok_p = false;
11242 /* After seeing a decl-specifier-seq, if the next token is not a
11243 "(", there is no possibility that the code is a valid
11244 expression. Therefore, if parsing tentatively, we commit at
11246 if (!parser->in_template_argument_list_p
11247 /* In an expression context, having seen:
11251 we cannot be sure whether we are looking at a
11252 function-type (taking a "char" as a parameter) or a cast
11253 of some object of type "char" to "int". */
11254 && !parser->in_type_id_in_expr_p
11255 && cp_parser_parsing_tentatively (parser)
11256 && !cp_parser_committed_to_tentative_parse (parser)
11257 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
11258 cp_parser_commit_to_tentative_parse (parser);
11259 /* Parse the declarator. */
11260 declarator = cp_parser_declarator (parser,
11261 CP_PARSER_DECLARATOR_EITHER,
11262 /*ctor_dtor_or_conv_p=*/NULL,
11264 /*member_p=*/false);
11265 parser->default_arg_ok_p = saved_default_arg_ok_p;
11266 /* After the declarator, allow more attributes. */
11267 attributes = chainon (attributes, cp_parser_attributes_opt (parser));
11270 /* The restriction on defining new types applies only to the type
11271 of the parameter, not to the default argument. */
11272 parser->type_definition_forbidden_message = saved_message;
11274 /* If the next token is `=', then process a default argument. */
11275 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11277 bool saved_greater_than_is_operator_p;
11278 /* Consume the `='. */
11279 cp_lexer_consume_token (parser->lexer);
11281 /* If we are defining a class, then the tokens that make up the
11282 default argument must be saved and processed later. */
11283 if (!template_parm_p && at_class_scope_p ()
11284 && TYPE_BEING_DEFINED (current_class_type))
11286 unsigned depth = 0;
11288 /* Create a DEFAULT_ARG to represented the unparsed default
11290 default_argument = make_node (DEFAULT_ARG);
11291 DEFARG_TOKENS (default_argument) = cp_token_cache_new ();
11293 /* Add tokens until we have processed the entire default
11300 /* Peek at the next token. */
11301 token = cp_lexer_peek_token (parser->lexer);
11302 /* What we do depends on what token we have. */
11303 switch (token->type)
11305 /* In valid code, a default argument must be
11306 immediately followed by a `,' `)', or `...'. */
11308 case CPP_CLOSE_PAREN:
11310 /* If we run into a non-nested `;', `}', or `]',
11311 then the code is invalid -- but the default
11312 argument is certainly over. */
11313 case CPP_SEMICOLON:
11314 case CPP_CLOSE_BRACE:
11315 case CPP_CLOSE_SQUARE:
11318 /* Update DEPTH, if necessary. */
11319 else if (token->type == CPP_CLOSE_PAREN
11320 || token->type == CPP_CLOSE_BRACE
11321 || token->type == CPP_CLOSE_SQUARE)
11325 case CPP_OPEN_PAREN:
11326 case CPP_OPEN_SQUARE:
11327 case CPP_OPEN_BRACE:
11332 /* If we see a non-nested `>', and `>' is not an
11333 operator, then it marks the end of the default
11335 if (!depth && !greater_than_is_operator_p)
11339 /* If we run out of tokens, issue an error message. */
11341 error ("file ends in default argument");
11347 /* In these cases, we should look for template-ids.
11348 For example, if the default argument is
11349 `X<int, double>()', we need to do name lookup to
11350 figure out whether or not `X' is a template; if
11351 so, the `,' does not end the default argument.
11353 That is not yet done. */
11360 /* If we've reached the end, stop. */
11364 /* Add the token to the token block. */
11365 token = cp_lexer_consume_token (parser->lexer);
11366 cp_token_cache_push_token (DEFARG_TOKENS (default_argument),
11370 /* Outside of a class definition, we can just parse the
11371 assignment-expression. */
11374 bool saved_local_variables_forbidden_p;
11376 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
11378 saved_greater_than_is_operator_p
11379 = parser->greater_than_is_operator_p;
11380 parser->greater_than_is_operator_p = greater_than_is_operator_p;
11381 /* Local variable names (and the `this' keyword) may not
11382 appear in a default argument. */
11383 saved_local_variables_forbidden_p
11384 = parser->local_variables_forbidden_p;
11385 parser->local_variables_forbidden_p = true;
11386 /* Parse the assignment-expression. */
11387 default_argument = cp_parser_assignment_expression (parser);
11388 /* Restore saved state. */
11389 parser->greater_than_is_operator_p
11390 = saved_greater_than_is_operator_p;
11391 parser->local_variables_forbidden_p
11392 = saved_local_variables_forbidden_p;
11394 if (!parser->default_arg_ok_p)
11396 if (!flag_pedantic_errors)
11397 warning ("deprecated use of default argument for parameter of non-function");
11400 error ("default arguments are only permitted for function parameters");
11401 default_argument = NULL_TREE;
11406 default_argument = NULL_TREE;
11408 /* Create the representation of the parameter. */
11410 decl_specifiers = tree_cons (attributes, NULL_TREE, decl_specifiers);
11411 parameter = build_tree_list (default_argument,
11412 build_tree_list (decl_specifiers,
11418 /* Parse a function-body.
11421 compound_statement */
11424 cp_parser_function_body (cp_parser *parser)
11426 cp_parser_compound_statement (parser, false);
11429 /* Parse a ctor-initializer-opt followed by a function-body. Return
11430 true if a ctor-initializer was present. */
11433 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
11436 bool ctor_initializer_p;
11438 /* Begin the function body. */
11439 body = begin_function_body ();
11440 /* Parse the optional ctor-initializer. */
11441 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
11442 /* Parse the function-body. */
11443 cp_parser_function_body (parser);
11444 /* Finish the function body. */
11445 finish_function_body (body);
11447 return ctor_initializer_p;
11450 /* Parse an initializer.
11453 = initializer-clause
11454 ( expression-list )
11456 Returns a expression representing the initializer. If no
11457 initializer is present, NULL_TREE is returned.
11459 *IS_PARENTHESIZED_INIT is set to TRUE if the `( expression-list )'
11460 production is used, and zero otherwise. *IS_PARENTHESIZED_INIT is
11461 set to FALSE if there is no initializer present. If there is an
11462 initializer, and it is not a constant-expression, *NON_CONSTANT_P
11463 is set to true; otherwise it is set to false. */
11466 cp_parser_initializer (cp_parser* parser, bool* is_parenthesized_init,
11467 bool* non_constant_p)
11472 /* Peek at the next token. */
11473 token = cp_lexer_peek_token (parser->lexer);
11475 /* Let our caller know whether or not this initializer was
11477 *is_parenthesized_init = (token->type == CPP_OPEN_PAREN);
11478 /* Assume that the initializer is constant. */
11479 *non_constant_p = false;
11481 if (token->type == CPP_EQ)
11483 /* Consume the `='. */
11484 cp_lexer_consume_token (parser->lexer);
11485 /* Parse the initializer-clause. */
11486 init = cp_parser_initializer_clause (parser, non_constant_p);
11488 else if (token->type == CPP_OPEN_PAREN)
11489 init = cp_parser_parenthesized_expression_list (parser, false,
11493 /* Anything else is an error. */
11494 cp_parser_error (parser, "expected initializer");
11495 init = error_mark_node;
11501 /* Parse an initializer-clause.
11503 initializer-clause:
11504 assignment-expression
11505 { initializer-list , [opt] }
11508 Returns an expression representing the initializer.
11510 If the `assignment-expression' production is used the value
11511 returned is simply a representation for the expression.
11513 Otherwise, a CONSTRUCTOR is returned. The CONSTRUCTOR_ELTS will be
11514 the elements of the initializer-list (or NULL_TREE, if the last
11515 production is used). The TREE_TYPE for the CONSTRUCTOR will be
11516 NULL_TREE. There is no way to detect whether or not the optional
11517 trailing `,' was provided. NON_CONSTANT_P is as for
11518 cp_parser_initializer. */
11521 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
11523 tree initializer = NULL_TREE;
11525 /* If it is not a `{', then we are looking at an
11526 assignment-expression. */
11527 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
11529 /* Speed up common initializers (simply a literal). */
11530 cp_token* token = cp_lexer_peek_token (parser->lexer);
11531 cp_token* token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
11533 if (token2->type == CPP_COMMA)
11534 switch (token->type)
11539 token = cp_lexer_consume_token (parser->lexer);
11540 initializer = token->value;
11545 token = cp_lexer_consume_token (parser->lexer);
11546 if (TREE_CHAIN (token->value))
11547 initializer = TREE_CHAIN (token->value);
11549 initializer = token->value;
11556 /* Otherwise, fall back to the generic assignment expression. */
11560 = cp_parser_constant_expression (parser,
11561 /*allow_non_constant_p=*/true,
11563 if (!*non_constant_p)
11564 initializer = fold_non_dependent_expr (initializer);
11569 /* Consume the `{' token. */
11570 cp_lexer_consume_token (parser->lexer);
11571 /* Create a CONSTRUCTOR to represent the braced-initializer. */
11572 initializer = make_node (CONSTRUCTOR);
11573 /* Mark it with TREE_HAS_CONSTRUCTOR. This should not be
11574 necessary, but check_initializer depends upon it, for
11576 TREE_HAS_CONSTRUCTOR (initializer) = 1;
11577 /* If it's not a `}', then there is a non-trivial initializer. */
11578 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
11580 /* Parse the initializer list. */
11581 CONSTRUCTOR_ELTS (initializer)
11582 = cp_parser_initializer_list (parser, non_constant_p);
11583 /* A trailing `,' token is allowed. */
11584 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
11585 cp_lexer_consume_token (parser->lexer);
11587 /* Now, there should be a trailing `}'. */
11588 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
11591 return initializer;
11594 /* Parse an initializer-list.
11598 initializer-list , initializer-clause
11603 identifier : initializer-clause
11604 initializer-list, identifier : initializer-clause
11606 Returns a TREE_LIST. The TREE_VALUE of each node is an expression
11607 for the initializer. If the TREE_PURPOSE is non-NULL, it is the
11608 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
11609 as for cp_parser_initializer. */
11612 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
11614 tree initializers = NULL_TREE;
11616 /* Assume all of the expressions are constant. */
11617 *non_constant_p = false;
11619 /* Parse the rest of the list. */
11625 bool clause_non_constant_p;
11627 /* If the next token is an identifier and the following one is a
11628 colon, we are looking at the GNU designated-initializer
11630 if (cp_parser_allow_gnu_extensions_p (parser)
11631 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
11632 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
11634 /* Consume the identifier. */
11635 identifier = cp_lexer_consume_token (parser->lexer)->value;
11636 /* Consume the `:'. */
11637 cp_lexer_consume_token (parser->lexer);
11640 identifier = NULL_TREE;
11642 /* Parse the initializer. */
11643 initializer = cp_parser_initializer_clause (parser,
11644 &clause_non_constant_p);
11645 /* If any clause is non-constant, so is the entire initializer. */
11646 if (clause_non_constant_p)
11647 *non_constant_p = true;
11648 /* Add it to the list. */
11649 initializers = tree_cons (identifier, initializer, initializers);
11651 /* If the next token is not a comma, we have reached the end of
11653 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11656 /* Peek at the next token. */
11657 token = cp_lexer_peek_nth_token (parser->lexer, 2);
11658 /* If the next token is a `}', then we're still done. An
11659 initializer-clause can have a trailing `,' after the
11660 initializer-list and before the closing `}'. */
11661 if (token->type == CPP_CLOSE_BRACE)
11664 /* Consume the `,' token. */
11665 cp_lexer_consume_token (parser->lexer);
11668 /* The initializers were built up in reverse order, so we need to
11669 reverse them now. */
11670 return nreverse (initializers);
11673 /* Classes [gram.class] */
11675 /* Parse a class-name.
11681 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
11682 to indicate that names looked up in dependent types should be
11683 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
11684 keyword has been used to indicate that the name that appears next
11685 is a template. TYPE_P is true iff the next name should be treated
11686 as class-name, even if it is declared to be some other kind of name
11687 as well. If CHECK_DEPENDENCY_P is FALSE, names are looked up in
11688 dependent scopes. If CLASS_HEAD_P is TRUE, this class is the class
11689 being defined in a class-head.
11691 Returns the TYPE_DECL representing the class. */
11694 cp_parser_class_name (cp_parser *parser,
11695 bool typename_keyword_p,
11696 bool template_keyword_p,
11698 bool check_dependency_p,
11700 bool is_declaration)
11707 /* All class-names start with an identifier. */
11708 token = cp_lexer_peek_token (parser->lexer);
11709 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
11711 cp_parser_error (parser, "expected class-name");
11712 return error_mark_node;
11715 /* PARSER->SCOPE can be cleared when parsing the template-arguments
11716 to a template-id, so we save it here. */
11717 scope = parser->scope;
11718 if (scope == error_mark_node)
11719 return error_mark_node;
11721 /* Any name names a type if we're following the `typename' keyword
11722 in a qualified name where the enclosing scope is type-dependent. */
11723 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
11724 && dependent_type_p (scope));
11725 /* Handle the common case (an identifier, but not a template-id)
11727 if (token->type == CPP_NAME
11728 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
11732 /* Look for the identifier. */
11733 identifier = cp_parser_identifier (parser);
11734 /* If the next token isn't an identifier, we are certainly not
11735 looking at a class-name. */
11736 if (identifier == error_mark_node)
11737 decl = error_mark_node;
11738 /* If we know this is a type-name, there's no need to look it
11740 else if (typename_p)
11744 /* If the next token is a `::', then the name must be a type
11747 [basic.lookup.qual]
11749 During the lookup for a name preceding the :: scope
11750 resolution operator, object, function, and enumerator
11751 names are ignored. */
11752 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
11754 /* Look up the name. */
11755 decl = cp_parser_lookup_name (parser, identifier,
11757 /*is_template=*/false,
11758 /*is_namespace=*/false,
11759 check_dependency_p);
11764 /* Try a template-id. */
11765 decl = cp_parser_template_id (parser, template_keyword_p,
11766 check_dependency_p,
11768 if (decl == error_mark_node)
11769 return error_mark_node;
11772 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
11774 /* If this is a typename, create a TYPENAME_TYPE. */
11775 if (typename_p && decl != error_mark_node)
11777 decl = make_typename_type (scope, decl, /*complain=*/1);
11778 if (decl != error_mark_node)
11779 decl = TYPE_NAME (decl);
11782 /* Check to see that it is really the name of a class. */
11783 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
11784 && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
11785 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
11786 /* Situations like this:
11788 template <typename T> struct A {
11789 typename T::template X<int>::I i;
11792 are problematic. Is `T::template X<int>' a class-name? The
11793 standard does not seem to be definitive, but there is no other
11794 valid interpretation of the following `::'. Therefore, those
11795 names are considered class-names. */
11796 decl = TYPE_NAME (make_typename_type (scope, decl, tf_error));
11797 else if (decl == error_mark_node
11798 || TREE_CODE (decl) != TYPE_DECL
11799 || !IS_AGGR_TYPE (TREE_TYPE (decl)))
11801 cp_parser_error (parser, "expected class-name");
11802 return error_mark_node;
11808 /* Parse a class-specifier.
11811 class-head { member-specification [opt] }
11813 Returns the TREE_TYPE representing the class. */
11816 cp_parser_class_specifier (cp_parser* parser)
11821 int has_trailing_semicolon;
11822 bool nested_name_specifier_p;
11823 unsigned saved_num_template_parameter_lists;
11824 bool pop_p = false;
11825 tree scope = NULL_TREE;
11827 push_deferring_access_checks (dk_no_deferred);
11829 /* Parse the class-head. */
11830 type = cp_parser_class_head (parser,
11831 &nested_name_specifier_p,
11833 /* If the class-head was a semantic disaster, skip the entire body
11837 cp_parser_skip_to_end_of_block_or_statement (parser);
11838 pop_deferring_access_checks ();
11839 return error_mark_node;
11842 /* Look for the `{'. */
11843 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
11845 pop_deferring_access_checks ();
11846 return error_mark_node;
11849 /* Issue an error message if type-definitions are forbidden here. */
11850 cp_parser_check_type_definition (parser);
11851 /* Remember that we are defining one more class. */
11852 ++parser->num_classes_being_defined;
11853 /* Inside the class, surrounding template-parameter-lists do not
11855 saved_num_template_parameter_lists
11856 = parser->num_template_parameter_lists;
11857 parser->num_template_parameter_lists = 0;
11859 /* Start the class. */
11860 if (nested_name_specifier_p)
11862 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
11863 pop_p = push_scope (scope);
11865 type = begin_class_definition (type);
11866 if (type == error_mark_node)
11867 /* If the type is erroneous, skip the entire body of the class. */
11868 cp_parser_skip_to_closing_brace (parser);
11870 /* Parse the member-specification. */
11871 cp_parser_member_specification_opt (parser);
11872 /* Look for the trailing `}'. */
11873 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
11874 /* We get better error messages by noticing a common problem: a
11875 missing trailing `;'. */
11876 token = cp_lexer_peek_token (parser->lexer);
11877 has_trailing_semicolon = (token->type == CPP_SEMICOLON);
11878 /* Look for trailing attributes to apply to this class. */
11879 if (cp_parser_allow_gnu_extensions_p (parser))
11881 tree sub_attr = cp_parser_attributes_opt (parser);
11882 attributes = chainon (attributes, sub_attr);
11884 if (type != error_mark_node)
11885 type = finish_struct (type, attributes);
11888 /* If this class is not itself within the scope of another class,
11889 then we need to parse the bodies of all of the queued function
11890 definitions. Note that the queued functions defined in a class
11891 are not always processed immediately following the
11892 class-specifier for that class. Consider:
11895 struct B { void f() { sizeof (A); } };
11898 If `f' were processed before the processing of `A' were
11899 completed, there would be no way to compute the size of `A'.
11900 Note that the nesting we are interested in here is lexical --
11901 not the semantic nesting given by TYPE_CONTEXT. In particular,
11904 struct A { struct B; };
11905 struct A::B { void f() { } };
11907 there is no need to delay the parsing of `A::B::f'. */
11908 if (--parser->num_classes_being_defined == 0)
11913 /* In a first pass, parse default arguments to the functions.
11914 Then, in a second pass, parse the bodies of the functions.
11915 This two-phased approach handles cases like:
11923 for (TREE_PURPOSE (parser->unparsed_functions_queues)
11924 = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
11925 (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
11926 TREE_PURPOSE (parser->unparsed_functions_queues)
11927 = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
11929 fn = TREE_VALUE (queue_entry);
11930 /* Make sure that any template parameters are in scope. */
11931 maybe_begin_member_template_processing (fn);
11932 /* If there are default arguments that have not yet been processed,
11933 take care of them now. */
11934 cp_parser_late_parsing_default_args (parser, fn);
11935 /* Remove any template parameters from the symbol table. */
11936 maybe_end_member_template_processing ();
11938 /* Now parse the body of the functions. */
11939 for (TREE_VALUE (parser->unparsed_functions_queues)
11940 = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
11941 (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
11942 TREE_VALUE (parser->unparsed_functions_queues)
11943 = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
11945 /* Figure out which function we need to process. */
11946 fn = TREE_VALUE (queue_entry);
11948 /* A hack to prevent garbage collection. */
11951 /* Parse the function. */
11952 cp_parser_late_parsing_for_member (parser, fn);
11958 /* Put back any saved access checks. */
11959 pop_deferring_access_checks ();
11961 /* Restore the count of active template-parameter-lists. */
11962 parser->num_template_parameter_lists
11963 = saved_num_template_parameter_lists;
11968 /* Parse a class-head.
11971 class-key identifier [opt] base-clause [opt]
11972 class-key nested-name-specifier identifier base-clause [opt]
11973 class-key nested-name-specifier [opt] template-id
11977 class-key attributes identifier [opt] base-clause [opt]
11978 class-key attributes nested-name-specifier identifier base-clause [opt]
11979 class-key attributes nested-name-specifier [opt] template-id
11982 Returns the TYPE of the indicated class. Sets
11983 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
11984 involving a nested-name-specifier was used, and FALSE otherwise.
11986 Returns NULL_TREE if the class-head is syntactically valid, but
11987 semantically invalid in a way that means we should skip the entire
11988 body of the class. */
11991 cp_parser_class_head (cp_parser* parser,
11992 bool* nested_name_specifier_p,
11993 tree *attributes_p)
11996 tree nested_name_specifier;
11997 enum tag_types class_key;
11998 tree id = NULL_TREE;
11999 tree type = NULL_TREE;
12001 bool template_id_p = false;
12002 bool qualified_p = false;
12003 bool invalid_nested_name_p = false;
12004 bool invalid_explicit_specialization_p = false;
12005 bool pop_p = false;
12006 unsigned num_templates;
12008 /* Assume no nested-name-specifier will be present. */
12009 *nested_name_specifier_p = false;
12010 /* Assume no template parameter lists will be used in defining the
12014 /* Look for the class-key. */
12015 class_key = cp_parser_class_key (parser);
12016 if (class_key == none_type)
12017 return error_mark_node;
12019 /* Parse the attributes. */
12020 attributes = cp_parser_attributes_opt (parser);
12022 /* If the next token is `::', that is invalid -- but sometimes
12023 people do try to write:
12027 Handle this gracefully by accepting the extra qualifier, and then
12028 issuing an error about it later if this really is a
12029 class-head. If it turns out just to be an elaborated type
12030 specifier, remain silent. */
12031 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
12032 qualified_p = true;
12034 push_deferring_access_checks (dk_no_check);
12036 /* Determine the name of the class. Begin by looking for an
12037 optional nested-name-specifier. */
12038 nested_name_specifier
12039 = cp_parser_nested_name_specifier_opt (parser,
12040 /*typename_keyword_p=*/false,
12041 /*check_dependency_p=*/false,
12043 /*is_declaration=*/false);
12044 /* If there was a nested-name-specifier, then there *must* be an
12046 if (nested_name_specifier)
12048 /* Although the grammar says `identifier', it really means
12049 `class-name' or `template-name'. You are only allowed to
12050 define a class that has already been declared with this
12053 The proposed resolution for Core Issue 180 says that whever
12054 you see `class T::X' you should treat `X' as a type-name.
12056 It is OK to define an inaccessible class; for example:
12058 class A { class B; };
12061 We do not know if we will see a class-name, or a
12062 template-name. We look for a class-name first, in case the
12063 class-name is a template-id; if we looked for the
12064 template-name first we would stop after the template-name. */
12065 cp_parser_parse_tentatively (parser);
12066 type = cp_parser_class_name (parser,
12067 /*typename_keyword_p=*/false,
12068 /*template_keyword_p=*/false,
12070 /*check_dependency_p=*/false,
12071 /*class_head_p=*/true,
12072 /*is_declaration=*/false);
12073 /* If that didn't work, ignore the nested-name-specifier. */
12074 if (!cp_parser_parse_definitely (parser))
12076 invalid_nested_name_p = true;
12077 id = cp_parser_identifier (parser);
12078 if (id == error_mark_node)
12081 /* If we could not find a corresponding TYPE, treat this
12082 declaration like an unqualified declaration. */
12083 if (type == error_mark_node)
12084 nested_name_specifier = NULL_TREE;
12085 /* Otherwise, count the number of templates used in TYPE and its
12086 containing scopes. */
12091 for (scope = TREE_TYPE (type);
12092 scope && TREE_CODE (scope) != NAMESPACE_DECL;
12093 scope = (TYPE_P (scope)
12094 ? TYPE_CONTEXT (scope)
12095 : DECL_CONTEXT (scope)))
12097 && CLASS_TYPE_P (scope)
12098 && CLASSTYPE_TEMPLATE_INFO (scope)
12099 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
12100 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
12104 /* Otherwise, the identifier is optional. */
12107 /* We don't know whether what comes next is a template-id,
12108 an identifier, or nothing at all. */
12109 cp_parser_parse_tentatively (parser);
12110 /* Check for a template-id. */
12111 id = cp_parser_template_id (parser,
12112 /*template_keyword_p=*/false,
12113 /*check_dependency_p=*/true,
12114 /*is_declaration=*/true);
12115 /* If that didn't work, it could still be an identifier. */
12116 if (!cp_parser_parse_definitely (parser))
12118 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12119 id = cp_parser_identifier (parser);
12125 template_id_p = true;
12130 pop_deferring_access_checks ();
12133 cp_parser_check_for_invalid_template_id (parser, id);
12135 /* If it's not a `:' or a `{' then we can't really be looking at a
12136 class-head, since a class-head only appears as part of a
12137 class-specifier. We have to detect this situation before calling
12138 xref_tag, since that has irreversible side-effects. */
12139 if (!cp_parser_next_token_starts_class_definition_p (parser))
12141 cp_parser_error (parser, "expected `{' or `:'");
12142 return error_mark_node;
12145 /* At this point, we're going ahead with the class-specifier, even
12146 if some other problem occurs. */
12147 cp_parser_commit_to_tentative_parse (parser);
12148 /* Issue the error about the overly-qualified name now. */
12150 cp_parser_error (parser,
12151 "global qualification of class name is invalid");
12152 else if (invalid_nested_name_p)
12153 cp_parser_error (parser,
12154 "qualified name does not name a class");
12155 else if (nested_name_specifier)
12158 /* Figure out in what scope the declaration is being placed. */
12159 scope = current_scope ();
12161 scope = current_namespace;
12162 /* If that scope does not contain the scope in which the
12163 class was originally declared, the program is invalid. */
12164 if (scope && !is_ancestor (scope, nested_name_specifier))
12166 error ("declaration of `%D' in `%D' which does not "
12167 "enclose `%D'", type, scope, nested_name_specifier);
12173 A declarator-id shall not be qualified exception of the
12174 definition of a ... nested class outside of its class
12175 ... [or] a the definition or explicit instantiation of a
12176 class member of a namespace outside of its namespace. */
12177 if (scope == nested_name_specifier)
12179 pedwarn ("extra qualification ignored");
12180 nested_name_specifier = NULL_TREE;
12184 /* An explicit-specialization must be preceded by "template <>". If
12185 it is not, try to recover gracefully. */
12186 if (at_namespace_scope_p ()
12187 && parser->num_template_parameter_lists == 0
12190 error ("an explicit specialization must be preceded by 'template <>'");
12191 invalid_explicit_specialization_p = true;
12192 /* Take the same action that would have been taken by
12193 cp_parser_explicit_specialization. */
12194 ++parser->num_template_parameter_lists;
12195 begin_specialization ();
12197 /* There must be no "return" statements between this point and the
12198 end of this function; set "type "to the correct return value and
12199 use "goto done;" to return. */
12200 /* Make sure that the right number of template parameters were
12202 if (!cp_parser_check_template_parameters (parser, num_templates))
12204 /* If something went wrong, there is no point in even trying to
12205 process the class-definition. */
12210 /* Look up the type. */
12213 type = TREE_TYPE (id);
12214 maybe_process_partial_specialization (type);
12216 else if (!nested_name_specifier)
12218 /* If the class was unnamed, create a dummy name. */
12220 id = make_anon_name ();
12221 type = xref_tag (class_key, id, /*globalize=*/false,
12222 parser->num_template_parameter_lists);
12227 bool pop_p = false;
12231 template <typename T> struct S { struct T };
12232 template <typename T> struct S<T>::T { };
12234 we will get a TYPENAME_TYPE when processing the definition of
12235 `S::T'. We need to resolve it to the actual type before we
12236 try to define it. */
12237 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
12239 class_type = resolve_typename_type (TREE_TYPE (type),
12240 /*only_current_p=*/false);
12241 if (class_type != error_mark_node)
12242 type = TYPE_NAME (class_type);
12245 cp_parser_error (parser, "could not resolve typename type");
12246 type = error_mark_node;
12250 maybe_process_partial_specialization (TREE_TYPE (type));
12251 class_type = current_class_type;
12252 /* Enter the scope indicated by the nested-name-specifier. */
12253 if (nested_name_specifier)
12254 pop_p = push_scope (nested_name_specifier);
12255 /* Get the canonical version of this type. */
12256 type = TYPE_MAIN_DECL (TREE_TYPE (type));
12257 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
12258 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
12259 type = push_template_decl (type);
12260 type = TREE_TYPE (type);
12261 if (nested_name_specifier)
12263 *nested_name_specifier_p = true;
12265 pop_scope (nested_name_specifier);
12268 /* Indicate whether this class was declared as a `class' or as a
12270 if (TREE_CODE (type) == RECORD_TYPE)
12271 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
12272 cp_parser_check_class_key (class_key, type);
12274 /* Enter the scope containing the class; the names of base classes
12275 should be looked up in that context. For example, given:
12277 struct A { struct B {}; struct C; };
12278 struct A::C : B {};
12281 if (nested_name_specifier)
12282 pop_p = push_scope (nested_name_specifier);
12283 /* Now, look for the base-clause. */
12284 token = cp_lexer_peek_token (parser->lexer);
12285 if (token->type == CPP_COLON)
12289 /* Get the list of base-classes. */
12290 bases = cp_parser_base_clause (parser);
12291 /* Process them. */
12292 xref_basetypes (type, bases);
12294 /* Leave the scope given by the nested-name-specifier. We will
12295 enter the class scope itself while processing the members. */
12297 pop_scope (nested_name_specifier);
12300 if (invalid_explicit_specialization_p)
12302 end_specialization ();
12303 --parser->num_template_parameter_lists;
12305 *attributes_p = attributes;
12309 /* Parse a class-key.
12316 Returns the kind of class-key specified, or none_type to indicate
12319 static enum tag_types
12320 cp_parser_class_key (cp_parser* parser)
12323 enum tag_types tag_type;
12325 /* Look for the class-key. */
12326 token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
12330 /* Check to see if the TOKEN is a class-key. */
12331 tag_type = cp_parser_token_is_class_key (token);
12333 cp_parser_error (parser, "expected class-key");
12337 /* Parse an (optional) member-specification.
12339 member-specification:
12340 member-declaration member-specification [opt]
12341 access-specifier : member-specification [opt] */
12344 cp_parser_member_specification_opt (cp_parser* parser)
12351 /* Peek at the next token. */
12352 token = cp_lexer_peek_token (parser->lexer);
12353 /* If it's a `}', or EOF then we've seen all the members. */
12354 if (token->type == CPP_CLOSE_BRACE || token->type == CPP_EOF)
12357 /* See if this token is a keyword. */
12358 keyword = token->keyword;
12362 case RID_PROTECTED:
12364 /* Consume the access-specifier. */
12365 cp_lexer_consume_token (parser->lexer);
12366 /* Remember which access-specifier is active. */
12367 current_access_specifier = token->value;
12368 /* Look for the `:'. */
12369 cp_parser_require (parser, CPP_COLON, "`:'");
12373 /* Otherwise, the next construction must be a
12374 member-declaration. */
12375 cp_parser_member_declaration (parser);
12380 /* Parse a member-declaration.
12382 member-declaration:
12383 decl-specifier-seq [opt] member-declarator-list [opt] ;
12384 function-definition ; [opt]
12385 :: [opt] nested-name-specifier template [opt] unqualified-id ;
12387 template-declaration
12389 member-declarator-list:
12391 member-declarator-list , member-declarator
12394 declarator pure-specifier [opt]
12395 declarator constant-initializer [opt]
12396 identifier [opt] : constant-expression
12400 member-declaration:
12401 __extension__ member-declaration
12404 declarator attributes [opt] pure-specifier [opt]
12405 declarator attributes [opt] constant-initializer [opt]
12406 identifier [opt] attributes [opt] : constant-expression */
12409 cp_parser_member_declaration (cp_parser* parser)
12411 tree decl_specifiers;
12412 tree prefix_attributes;
12414 int declares_class_or_enum;
12417 int saved_pedantic;
12419 /* Check for the `__extension__' keyword. */
12420 if (cp_parser_extension_opt (parser, &saved_pedantic))
12423 cp_parser_member_declaration (parser);
12424 /* Restore the old value of the PEDANTIC flag. */
12425 pedantic = saved_pedantic;
12430 /* Check for a template-declaration. */
12431 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
12433 /* Parse the template-declaration. */
12434 cp_parser_template_declaration (parser, /*member_p=*/true);
12439 /* Check for a using-declaration. */
12440 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
12442 /* Parse the using-declaration. */
12443 cp_parser_using_declaration (parser);
12448 /* Parse the decl-specifier-seq. */
12450 = cp_parser_decl_specifier_seq (parser,
12451 CP_PARSER_FLAGS_OPTIONAL,
12452 &prefix_attributes,
12453 &declares_class_or_enum);
12454 /* Check for an invalid type-name. */
12455 if (cp_parser_diagnose_invalid_type_name (parser))
12457 /* If there is no declarator, then the decl-specifier-seq should
12459 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12461 /* If there was no decl-specifier-seq, and the next token is a
12462 `;', then we have something like:
12468 Each member-declaration shall declare at least one member
12469 name of the class. */
12470 if (!decl_specifiers)
12473 pedwarn ("extra semicolon");
12479 /* See if this declaration is a friend. */
12480 friend_p = cp_parser_friend_p (decl_specifiers);
12481 /* If there were decl-specifiers, check to see if there was
12482 a class-declaration. */
12483 type = check_tag_decl (decl_specifiers);
12484 /* Nested classes have already been added to the class, but
12485 a `friend' needs to be explicitly registered. */
12488 /* If the `friend' keyword was present, the friend must
12489 be introduced with a class-key. */
12490 if (!declares_class_or_enum)
12491 error ("a class-key must be used when declaring a friend");
12494 template <typename T> struct A {
12495 friend struct A<T>::B;
12498 A<T>::B will be represented by a TYPENAME_TYPE, and
12499 therefore not recognized by check_tag_decl. */
12504 for (specifier = decl_specifiers;
12506 specifier = TREE_CHAIN (specifier))
12508 tree s = TREE_VALUE (specifier);
12510 if (TREE_CODE (s) == IDENTIFIER_NODE)
12511 get_global_value_if_present (s, &type);
12512 if (TREE_CODE (s) == TYPE_DECL)
12521 if (!type || !TYPE_P (type))
12522 error ("friend declaration does not name a class or "
12525 make_friend_class (current_class_type, type,
12526 /*complain=*/true);
12528 /* If there is no TYPE, an error message will already have
12532 /* An anonymous aggregate has to be handled specially; such
12533 a declaration really declares a data member (with a
12534 particular type), as opposed to a nested class. */
12535 else if (ANON_AGGR_TYPE_P (type))
12537 /* Remove constructors and such from TYPE, now that we
12538 know it is an anonymous aggregate. */
12539 fixup_anonymous_aggr (type);
12540 /* And make the corresponding data member. */
12541 decl = build_decl (FIELD_DECL, NULL_TREE, type);
12542 /* Add it to the class. */
12543 finish_member_declaration (decl);
12546 cp_parser_check_access_in_redeclaration (TYPE_NAME (type));
12551 /* See if these declarations will be friends. */
12552 friend_p = cp_parser_friend_p (decl_specifiers);
12554 /* Keep going until we hit the `;' at the end of the
12556 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12558 tree attributes = NULL_TREE;
12559 tree first_attribute;
12561 /* Peek at the next token. */
12562 token = cp_lexer_peek_token (parser->lexer);
12564 /* Check for a bitfield declaration. */
12565 if (token->type == CPP_COLON
12566 || (token->type == CPP_NAME
12567 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
12573 /* Get the name of the bitfield. Note that we cannot just
12574 check TOKEN here because it may have been invalidated by
12575 the call to cp_lexer_peek_nth_token above. */
12576 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
12577 identifier = cp_parser_identifier (parser);
12579 identifier = NULL_TREE;
12581 /* Consume the `:' token. */
12582 cp_lexer_consume_token (parser->lexer);
12583 /* Get the width of the bitfield. */
12585 = cp_parser_constant_expression (parser,
12586 /*allow_non_constant=*/false,
12589 /* Look for attributes that apply to the bitfield. */
12590 attributes = cp_parser_attributes_opt (parser);
12591 /* Remember which attributes are prefix attributes and
12593 first_attribute = attributes;
12594 /* Combine the attributes. */
12595 attributes = chainon (prefix_attributes, attributes);
12597 /* Create the bitfield declaration. */
12598 decl = grokbitfield (identifier,
12601 /* Apply the attributes. */
12602 cplus_decl_attributes (&decl, attributes, /*flags=*/0);
12608 tree asm_specification;
12609 int ctor_dtor_or_conv_p;
12611 /* Parse the declarator. */
12613 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
12614 &ctor_dtor_or_conv_p,
12615 /*parenthesized_p=*/NULL,
12616 /*member_p=*/true);
12618 /* If something went wrong parsing the declarator, make sure
12619 that we at least consume some tokens. */
12620 if (declarator == error_mark_node)
12622 /* Skip to the end of the statement. */
12623 cp_parser_skip_to_end_of_statement (parser);
12624 /* If the next token is not a semicolon, that is
12625 probably because we just skipped over the body of
12626 a function. So, we consume a semicolon if
12627 present, but do not issue an error message if it
12629 if (cp_lexer_next_token_is (parser->lexer,
12631 cp_lexer_consume_token (parser->lexer);
12635 cp_parser_check_for_definition_in_return_type
12636 (declarator, declares_class_or_enum);
12638 /* Look for an asm-specification. */
12639 asm_specification = cp_parser_asm_specification_opt (parser);
12640 /* Look for attributes that apply to the declaration. */
12641 attributes = cp_parser_attributes_opt (parser);
12642 /* Remember which attributes are prefix attributes and
12644 first_attribute = attributes;
12645 /* Combine the attributes. */
12646 attributes = chainon (prefix_attributes, attributes);
12648 /* If it's an `=', then we have a constant-initializer or a
12649 pure-specifier. It is not correct to parse the
12650 initializer before registering the member declaration
12651 since the member declaration should be in scope while
12652 its initializer is processed. However, the rest of the
12653 front end does not yet provide an interface that allows
12654 us to handle this correctly. */
12655 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12659 A pure-specifier shall be used only in the declaration of
12660 a virtual function.
12662 A member-declarator can contain a constant-initializer
12663 only if it declares a static member of integral or
12666 Therefore, if the DECLARATOR is for a function, we look
12667 for a pure-specifier; otherwise, we look for a
12668 constant-initializer. When we call `grokfield', it will
12669 perform more stringent semantics checks. */
12670 if (TREE_CODE (declarator) == CALL_EXPR)
12671 initializer = cp_parser_pure_specifier (parser);
12673 /* Parse the initializer. */
12674 initializer = cp_parser_constant_initializer (parser);
12676 /* Otherwise, there is no initializer. */
12678 initializer = NULL_TREE;
12680 /* See if we are probably looking at a function
12681 definition. We are certainly not looking at at a
12682 member-declarator. Calling `grokfield' has
12683 side-effects, so we must not do it unless we are sure
12684 that we are looking at a member-declarator. */
12685 if (cp_parser_token_starts_function_definition_p
12686 (cp_lexer_peek_token (parser->lexer)))
12688 /* The grammar does not allow a pure-specifier to be
12689 used when a member function is defined. (It is
12690 possible that this fact is an oversight in the
12691 standard, since a pure function may be defined
12692 outside of the class-specifier. */
12694 error ("pure-specifier on function-definition");
12695 decl = cp_parser_save_member_function_body (parser,
12699 /* If the member was not a friend, declare it here. */
12701 finish_member_declaration (decl);
12702 /* Peek at the next token. */
12703 token = cp_lexer_peek_token (parser->lexer);
12704 /* If the next token is a semicolon, consume it. */
12705 if (token->type == CPP_SEMICOLON)
12706 cp_lexer_consume_token (parser->lexer);
12711 /* Create the declaration. */
12712 decl = grokfield (declarator, decl_specifiers,
12713 initializer, asm_specification,
12715 /* Any initialization must have been from a
12716 constant-expression. */
12717 if (decl && TREE_CODE (decl) == VAR_DECL && initializer)
12718 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = 1;
12722 /* Reset PREFIX_ATTRIBUTES. */
12723 while (attributes && TREE_CHAIN (attributes) != first_attribute)
12724 attributes = TREE_CHAIN (attributes);
12726 TREE_CHAIN (attributes) = NULL_TREE;
12728 /* If there is any qualification still in effect, clear it
12729 now; we will be starting fresh with the next declarator. */
12730 parser->scope = NULL_TREE;
12731 parser->qualifying_scope = NULL_TREE;
12732 parser->object_scope = NULL_TREE;
12733 /* If it's a `,', then there are more declarators. */
12734 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
12735 cp_lexer_consume_token (parser->lexer);
12736 /* If the next token isn't a `;', then we have a parse error. */
12737 else if (cp_lexer_next_token_is_not (parser->lexer,
12740 cp_parser_error (parser, "expected `;'");
12741 /* Skip tokens until we find a `;'. */
12742 cp_parser_skip_to_end_of_statement (parser);
12749 /* Add DECL to the list of members. */
12751 finish_member_declaration (decl);
12753 if (TREE_CODE (decl) == FUNCTION_DECL)
12754 cp_parser_save_default_args (parser, decl);
12759 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
12762 /* Parse a pure-specifier.
12767 Returns INTEGER_ZERO_NODE if a pure specifier is found.
12768 Otherwise, ERROR_MARK_NODE is returned. */
12771 cp_parser_pure_specifier (cp_parser* parser)
12775 /* Look for the `=' token. */
12776 if (!cp_parser_require (parser, CPP_EQ, "`='"))
12777 return error_mark_node;
12778 /* Look for the `0' token. */
12779 token = cp_parser_require (parser, CPP_NUMBER, "`0'");
12780 /* Unfortunately, this will accept `0L' and `0x00' as well. We need
12781 to get information from the lexer about how the number was
12782 spelled in order to fix this problem. */
12783 if (!token || !integer_zerop (token->value))
12784 return error_mark_node;
12786 return integer_zero_node;
12789 /* Parse a constant-initializer.
12791 constant-initializer:
12792 = constant-expression
12794 Returns a representation of the constant-expression. */
12797 cp_parser_constant_initializer (cp_parser* parser)
12799 /* Look for the `=' token. */
12800 if (!cp_parser_require (parser, CPP_EQ, "`='"))
12801 return error_mark_node;
12803 /* It is invalid to write:
12805 struct S { static const int i = { 7 }; };
12808 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12810 cp_parser_error (parser,
12811 "a brace-enclosed initializer is not allowed here");
12812 /* Consume the opening brace. */
12813 cp_lexer_consume_token (parser->lexer);
12814 /* Skip the initializer. */
12815 cp_parser_skip_to_closing_brace (parser);
12816 /* Look for the trailing `}'. */
12817 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
12819 return error_mark_node;
12822 return cp_parser_constant_expression (parser,
12823 /*allow_non_constant=*/false,
12827 /* Derived classes [gram.class.derived] */
12829 /* Parse a base-clause.
12832 : base-specifier-list
12834 base-specifier-list:
12836 base-specifier-list , base-specifier
12838 Returns a TREE_LIST representing the base-classes, in the order in
12839 which they were declared. The representation of each node is as
12840 described by cp_parser_base_specifier.
12842 In the case that no bases are specified, this function will return
12843 NULL_TREE, not ERROR_MARK_NODE. */
12846 cp_parser_base_clause (cp_parser* parser)
12848 tree bases = NULL_TREE;
12850 /* Look for the `:' that begins the list. */
12851 cp_parser_require (parser, CPP_COLON, "`:'");
12853 /* Scan the base-specifier-list. */
12859 /* Look for the base-specifier. */
12860 base = cp_parser_base_specifier (parser);
12861 /* Add BASE to the front of the list. */
12862 if (base != error_mark_node)
12864 TREE_CHAIN (base) = bases;
12867 /* Peek at the next token. */
12868 token = cp_lexer_peek_token (parser->lexer);
12869 /* If it's not a comma, then the list is complete. */
12870 if (token->type != CPP_COMMA)
12872 /* Consume the `,'. */
12873 cp_lexer_consume_token (parser->lexer);
12876 /* PARSER->SCOPE may still be non-NULL at this point, if the last
12877 base class had a qualified name. However, the next name that
12878 appears is certainly not qualified. */
12879 parser->scope = NULL_TREE;
12880 parser->qualifying_scope = NULL_TREE;
12881 parser->object_scope = NULL_TREE;
12883 return nreverse (bases);
12886 /* Parse a base-specifier.
12889 :: [opt] nested-name-specifier [opt] class-name
12890 virtual access-specifier [opt] :: [opt] nested-name-specifier
12892 access-specifier virtual [opt] :: [opt] nested-name-specifier
12895 Returns a TREE_LIST. The TREE_PURPOSE will be one of
12896 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
12897 indicate the specifiers provided. The TREE_VALUE will be a TYPE
12898 (or the ERROR_MARK_NODE) indicating the type that was specified. */
12901 cp_parser_base_specifier (cp_parser* parser)
12905 bool virtual_p = false;
12906 bool duplicate_virtual_error_issued_p = false;
12907 bool duplicate_access_error_issued_p = false;
12908 bool class_scope_p, template_p;
12909 tree access = access_default_node;
12912 /* Process the optional `virtual' and `access-specifier'. */
12915 /* Peek at the next token. */
12916 token = cp_lexer_peek_token (parser->lexer);
12917 /* Process `virtual'. */
12918 switch (token->keyword)
12921 /* If `virtual' appears more than once, issue an error. */
12922 if (virtual_p && !duplicate_virtual_error_issued_p)
12924 cp_parser_error (parser,
12925 "`virtual' specified more than once in base-specified");
12926 duplicate_virtual_error_issued_p = true;
12931 /* Consume the `virtual' token. */
12932 cp_lexer_consume_token (parser->lexer);
12937 case RID_PROTECTED:
12939 /* If more than one access specifier appears, issue an
12941 if (access != access_default_node
12942 && !duplicate_access_error_issued_p)
12944 cp_parser_error (parser,
12945 "more than one access specifier in base-specified");
12946 duplicate_access_error_issued_p = true;
12949 access = ridpointers[(int) token->keyword];
12951 /* Consume the access-specifier. */
12952 cp_lexer_consume_token (parser->lexer);
12961 /* It is not uncommon to see programs mechanically, errouneously, use
12962 the 'typename' keyword to denote (dependent) qualified types
12963 as base classes. */
12964 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
12966 if (!processing_template_decl)
12967 error ("keyword `typename' not allowed outside of templates");
12969 error ("keyword `typename' not allowed in this context "
12970 "(the base class is implicitly a type)");
12971 cp_lexer_consume_token (parser->lexer);
12974 /* Look for the optional `::' operator. */
12975 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
12976 /* Look for the nested-name-specifier. The simplest way to
12981 The keyword `typename' is not permitted in a base-specifier or
12982 mem-initializer; in these contexts a qualified name that
12983 depends on a template-parameter is implicitly assumed to be a
12986 is to pretend that we have seen the `typename' keyword at this
12988 cp_parser_nested_name_specifier_opt (parser,
12989 /*typename_keyword_p=*/true,
12990 /*check_dependency_p=*/true,
12992 /*is_declaration=*/true);
12993 /* If the base class is given by a qualified name, assume that names
12994 we see are type names or templates, as appropriate. */
12995 class_scope_p = (parser->scope && TYPE_P (parser->scope));
12996 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
12998 /* Finally, look for the class-name. */
12999 type = cp_parser_class_name (parser,
13003 /*check_dependency_p=*/true,
13004 /*class_head_p=*/false,
13005 /*is_declaration=*/true);
13007 if (type == error_mark_node)
13008 return error_mark_node;
13010 return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
13013 /* Exception handling [gram.exception] */
13015 /* Parse an (optional) exception-specification.
13017 exception-specification:
13018 throw ( type-id-list [opt] )
13020 Returns a TREE_LIST representing the exception-specification. The
13021 TREE_VALUE of each node is a type. */
13024 cp_parser_exception_specification_opt (cp_parser* parser)
13029 /* Peek at the next token. */
13030 token = cp_lexer_peek_token (parser->lexer);
13031 /* If it's not `throw', then there's no exception-specification. */
13032 if (!cp_parser_is_keyword (token, RID_THROW))
13035 /* Consume the `throw'. */
13036 cp_lexer_consume_token (parser->lexer);
13038 /* Look for the `('. */
13039 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13041 /* Peek at the next token. */
13042 token = cp_lexer_peek_token (parser->lexer);
13043 /* If it's not a `)', then there is a type-id-list. */
13044 if (token->type != CPP_CLOSE_PAREN)
13046 const char *saved_message;
13048 /* Types may not be defined in an exception-specification. */
13049 saved_message = parser->type_definition_forbidden_message;
13050 parser->type_definition_forbidden_message
13051 = "types may not be defined in an exception-specification";
13052 /* Parse the type-id-list. */
13053 type_id_list = cp_parser_type_id_list (parser);
13054 /* Restore the saved message. */
13055 parser->type_definition_forbidden_message = saved_message;
13058 type_id_list = empty_except_spec;
13060 /* Look for the `)'. */
13061 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13063 return type_id_list;
13066 /* Parse an (optional) type-id-list.
13070 type-id-list , type-id
13072 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
13073 in the order that the types were presented. */
13076 cp_parser_type_id_list (cp_parser* parser)
13078 tree types = NULL_TREE;
13085 /* Get the next type-id. */
13086 type = cp_parser_type_id (parser);
13087 /* Add it to the list. */
13088 types = add_exception_specifier (types, type, /*complain=*/1);
13089 /* Peek at the next token. */
13090 token = cp_lexer_peek_token (parser->lexer);
13091 /* If it is not a `,', we are done. */
13092 if (token->type != CPP_COMMA)
13094 /* Consume the `,'. */
13095 cp_lexer_consume_token (parser->lexer);
13098 return nreverse (types);
13101 /* Parse a try-block.
13104 try compound-statement handler-seq */
13107 cp_parser_try_block (cp_parser* parser)
13111 cp_parser_require_keyword (parser, RID_TRY, "`try'");
13112 try_block = begin_try_block ();
13113 cp_parser_compound_statement (parser, false);
13114 finish_try_block (try_block);
13115 cp_parser_handler_seq (parser);
13116 finish_handler_sequence (try_block);
13121 /* Parse a function-try-block.
13123 function-try-block:
13124 try ctor-initializer [opt] function-body handler-seq */
13127 cp_parser_function_try_block (cp_parser* parser)
13130 bool ctor_initializer_p;
13132 /* Look for the `try' keyword. */
13133 if (!cp_parser_require_keyword (parser, RID_TRY, "`try'"))
13135 /* Let the rest of the front-end know where we are. */
13136 try_block = begin_function_try_block ();
13137 /* Parse the function-body. */
13139 = cp_parser_ctor_initializer_opt_and_function_body (parser);
13140 /* We're done with the `try' part. */
13141 finish_function_try_block (try_block);
13142 /* Parse the handlers. */
13143 cp_parser_handler_seq (parser);
13144 /* We're done with the handlers. */
13145 finish_function_handler_sequence (try_block);
13147 return ctor_initializer_p;
13150 /* Parse a handler-seq.
13153 handler handler-seq [opt] */
13156 cp_parser_handler_seq (cp_parser* parser)
13162 /* Parse the handler. */
13163 cp_parser_handler (parser);
13164 /* Peek at the next token. */
13165 token = cp_lexer_peek_token (parser->lexer);
13166 /* If it's not `catch' then there are no more handlers. */
13167 if (!cp_parser_is_keyword (token, RID_CATCH))
13172 /* Parse a handler.
13175 catch ( exception-declaration ) compound-statement */
13178 cp_parser_handler (cp_parser* parser)
13183 cp_parser_require_keyword (parser, RID_CATCH, "`catch'");
13184 handler = begin_handler ();
13185 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13186 declaration = cp_parser_exception_declaration (parser);
13187 finish_handler_parms (declaration, handler);
13188 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13189 cp_parser_compound_statement (parser, false);
13190 finish_handler (handler);
13193 /* Parse an exception-declaration.
13195 exception-declaration:
13196 type-specifier-seq declarator
13197 type-specifier-seq abstract-declarator
13201 Returns a VAR_DECL for the declaration, or NULL_TREE if the
13202 ellipsis variant is used. */
13205 cp_parser_exception_declaration (cp_parser* parser)
13207 tree type_specifiers;
13209 const char *saved_message;
13211 /* If it's an ellipsis, it's easy to handle. */
13212 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13214 /* Consume the `...' token. */
13215 cp_lexer_consume_token (parser->lexer);
13219 /* Types may not be defined in exception-declarations. */
13220 saved_message = parser->type_definition_forbidden_message;
13221 parser->type_definition_forbidden_message
13222 = "types may not be defined in exception-declarations";
13224 /* Parse the type-specifier-seq. */
13225 type_specifiers = cp_parser_type_specifier_seq (parser);
13226 /* If it's a `)', then there is no declarator. */
13227 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
13228 declarator = NULL_TREE;
13230 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
13231 /*ctor_dtor_or_conv_p=*/NULL,
13232 /*parenthesized_p=*/NULL,
13233 /*member_p=*/false);
13235 /* Restore the saved message. */
13236 parser->type_definition_forbidden_message = saved_message;
13238 return start_handler_parms (type_specifiers, declarator);
13241 /* Parse a throw-expression.
13244 throw assignment-expression [opt]
13246 Returns a THROW_EXPR representing the throw-expression. */
13249 cp_parser_throw_expression (cp_parser* parser)
13254 cp_parser_require_keyword (parser, RID_THROW, "`throw'");
13255 token = cp_lexer_peek_token (parser->lexer);
13256 /* Figure out whether or not there is an assignment-expression
13257 following the "throw" keyword. */
13258 if (token->type == CPP_COMMA
13259 || token->type == CPP_SEMICOLON
13260 || token->type == CPP_CLOSE_PAREN
13261 || token->type == CPP_CLOSE_SQUARE
13262 || token->type == CPP_CLOSE_BRACE
13263 || token->type == CPP_COLON)
13264 expression = NULL_TREE;
13266 expression = cp_parser_assignment_expression (parser);
13268 return build_throw (expression);
13271 /* GNU Extensions */
13273 /* Parse an (optional) asm-specification.
13276 asm ( string-literal )
13278 If the asm-specification is present, returns a STRING_CST
13279 corresponding to the string-literal. Otherwise, returns
13283 cp_parser_asm_specification_opt (cp_parser* parser)
13286 tree asm_specification;
13288 /* Peek at the next token. */
13289 token = cp_lexer_peek_token (parser->lexer);
13290 /* If the next token isn't the `asm' keyword, then there's no
13291 asm-specification. */
13292 if (!cp_parser_is_keyword (token, RID_ASM))
13295 /* Consume the `asm' token. */
13296 cp_lexer_consume_token (parser->lexer);
13297 /* Look for the `('. */
13298 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13300 /* Look for the string-literal. */
13301 token = cp_parser_require (parser, CPP_STRING, "string-literal");
13303 asm_specification = token->value;
13305 asm_specification = NULL_TREE;
13307 /* Look for the `)'. */
13308 cp_parser_require (parser, CPP_CLOSE_PAREN, "`('");
13310 return asm_specification;
13313 /* Parse an asm-operand-list.
13317 asm-operand-list , asm-operand
13320 string-literal ( expression )
13321 [ string-literal ] string-literal ( expression )
13323 Returns a TREE_LIST representing the operands. The TREE_VALUE of
13324 each node is the expression. The TREE_PURPOSE is itself a
13325 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
13326 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
13327 is a STRING_CST for the string literal before the parenthesis. */
13330 cp_parser_asm_operand_list (cp_parser* parser)
13332 tree asm_operands = NULL_TREE;
13336 tree string_literal;
13341 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
13343 /* Consume the `[' token. */
13344 cp_lexer_consume_token (parser->lexer);
13345 /* Read the operand name. */
13346 name = cp_parser_identifier (parser);
13347 if (name != error_mark_node)
13348 name = build_string (IDENTIFIER_LENGTH (name),
13349 IDENTIFIER_POINTER (name));
13350 /* Look for the closing `]'. */
13351 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
13355 /* Look for the string-literal. */
13356 token = cp_parser_require (parser, CPP_STRING, "string-literal");
13357 string_literal = token ? token->value : error_mark_node;
13358 /* Look for the `('. */
13359 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13360 /* Parse the expression. */
13361 expression = cp_parser_expression (parser);
13362 /* Look for the `)'. */
13363 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13364 /* Add this operand to the list. */
13365 asm_operands = tree_cons (build_tree_list (name, string_literal),
13368 /* If the next token is not a `,', there are no more
13370 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13372 /* Consume the `,'. */
13373 cp_lexer_consume_token (parser->lexer);
13376 return nreverse (asm_operands);
13379 /* Parse an asm-clobber-list.
13383 asm-clobber-list , string-literal
13385 Returns a TREE_LIST, indicating the clobbers in the order that they
13386 appeared. The TREE_VALUE of each node is a STRING_CST. */
13389 cp_parser_asm_clobber_list (cp_parser* parser)
13391 tree clobbers = NULL_TREE;
13396 tree string_literal;
13398 /* Look for the string literal. */
13399 token = cp_parser_require (parser, CPP_STRING, "string-literal");
13400 string_literal = token ? token->value : error_mark_node;
13401 /* Add it to the list. */
13402 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
13403 /* If the next token is not a `,', then the list is
13405 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13407 /* Consume the `,' token. */
13408 cp_lexer_consume_token (parser->lexer);
13414 /* Parse an (optional) series of attributes.
13417 attributes attribute
13420 __attribute__ (( attribute-list [opt] ))
13422 The return value is as for cp_parser_attribute_list. */
13425 cp_parser_attributes_opt (cp_parser* parser)
13427 tree attributes = NULL_TREE;
13432 tree attribute_list;
13434 /* Peek at the next token. */
13435 token = cp_lexer_peek_token (parser->lexer);
13436 /* If it's not `__attribute__', then we're done. */
13437 if (token->keyword != RID_ATTRIBUTE)
13440 /* Consume the `__attribute__' keyword. */
13441 cp_lexer_consume_token (parser->lexer);
13442 /* Look for the two `(' tokens. */
13443 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13444 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13446 /* Peek at the next token. */
13447 token = cp_lexer_peek_token (parser->lexer);
13448 if (token->type != CPP_CLOSE_PAREN)
13449 /* Parse the attribute-list. */
13450 attribute_list = cp_parser_attribute_list (parser);
13452 /* If the next token is a `)', then there is no attribute
13454 attribute_list = NULL;
13456 /* Look for the two `)' tokens. */
13457 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13458 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13460 /* Add these new attributes to the list. */
13461 attributes = chainon (attributes, attribute_list);
13467 /* Parse an attribute-list.
13471 attribute-list , attribute
13475 identifier ( identifier )
13476 identifier ( identifier , expression-list )
13477 identifier ( expression-list )
13479 Returns a TREE_LIST. Each node corresponds to an attribute. THe
13480 TREE_PURPOSE of each node is the identifier indicating which
13481 attribute is in use. The TREE_VALUE represents the arguments, if
13485 cp_parser_attribute_list (cp_parser* parser)
13487 tree attribute_list = NULL_TREE;
13495 /* Look for the identifier. We also allow keywords here; for
13496 example `__attribute__ ((const))' is legal. */
13497 token = cp_lexer_peek_token (parser->lexer);
13498 if (token->type != CPP_NAME
13499 && token->type != CPP_KEYWORD)
13500 return error_mark_node;
13501 /* Consume the token. */
13502 token = cp_lexer_consume_token (parser->lexer);
13504 /* Save away the identifier that indicates which attribute this is. */
13505 identifier = token->value;
13506 attribute = build_tree_list (identifier, NULL_TREE);
13508 /* Peek at the next token. */
13509 token = cp_lexer_peek_token (parser->lexer);
13510 /* If it's an `(', then parse the attribute arguments. */
13511 if (token->type == CPP_OPEN_PAREN)
13515 arguments = (cp_parser_parenthesized_expression_list
13516 (parser, true, /*non_constant_p=*/NULL));
13517 /* Save the identifier and arguments away. */
13518 TREE_VALUE (attribute) = arguments;
13521 /* Add this attribute to the list. */
13522 TREE_CHAIN (attribute) = attribute_list;
13523 attribute_list = attribute;
13525 /* Now, look for more attributes. */
13526 token = cp_lexer_peek_token (parser->lexer);
13527 /* If the next token isn't a `,', we're done. */
13528 if (token->type != CPP_COMMA)
13531 /* Consume the comma and keep going. */
13532 cp_lexer_consume_token (parser->lexer);
13535 /* We built up the list in reverse order. */
13536 return nreverse (attribute_list);
13539 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
13540 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
13541 current value of the PEDANTIC flag, regardless of whether or not
13542 the `__extension__' keyword is present. The caller is responsible
13543 for restoring the value of the PEDANTIC flag. */
13546 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
13548 /* Save the old value of the PEDANTIC flag. */
13549 *saved_pedantic = pedantic;
13551 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
13553 /* Consume the `__extension__' token. */
13554 cp_lexer_consume_token (parser->lexer);
13555 /* We're not being pedantic while the `__extension__' keyword is
13565 /* Parse a label declaration.
13568 __label__ label-declarator-seq ;
13570 label-declarator-seq:
13571 identifier , label-declarator-seq
13575 cp_parser_label_declaration (cp_parser* parser)
13577 /* Look for the `__label__' keyword. */
13578 cp_parser_require_keyword (parser, RID_LABEL, "`__label__'");
13584 /* Look for an identifier. */
13585 identifier = cp_parser_identifier (parser);
13586 /* Declare it as a lobel. */
13587 finish_label_decl (identifier);
13588 /* If the next token is a `;', stop. */
13589 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13591 /* Look for the `,' separating the label declarations. */
13592 cp_parser_require (parser, CPP_COMMA, "`,'");
13595 /* Look for the final `;'. */
13596 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
13599 /* Support Functions */
13601 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
13602 NAME should have one of the representations used for an
13603 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
13604 is returned. If PARSER->SCOPE is a dependent type, then a
13605 SCOPE_REF is returned.
13607 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
13608 returned; the name was already resolved when the TEMPLATE_ID_EXPR
13609 was formed. Abstractly, such entities should not be passed to this
13610 function, because they do not need to be looked up, but it is
13611 simpler to check for this special case here, rather than at the
13614 In cases not explicitly covered above, this function returns a
13615 DECL, OVERLOAD, or baselink representing the result of the lookup.
13616 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
13619 If IS_TYPE is TRUE, bindings that do not refer to types are
13622 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
13625 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
13628 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
13632 cp_parser_lookup_name (cp_parser *parser, tree name,
13633 bool is_type, bool is_template, bool is_namespace,
13634 bool check_dependency)
13637 tree object_type = parser->context->object_type;
13639 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
13640 no longer valid. Note that if we are parsing tentatively, and
13641 the parse fails, OBJECT_TYPE will be automatically restored. */
13642 parser->context->object_type = NULL_TREE;
13644 if (name == error_mark_node)
13645 return error_mark_node;
13647 /* A template-id has already been resolved; there is no lookup to
13649 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
13651 if (BASELINK_P (name))
13653 my_friendly_assert ((TREE_CODE (BASELINK_FUNCTIONS (name))
13654 == TEMPLATE_ID_EXPR),
13659 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
13660 it should already have been checked to make sure that the name
13661 used matches the type being destroyed. */
13662 if (TREE_CODE (name) == BIT_NOT_EXPR)
13666 /* Figure out to which type this destructor applies. */
13668 type = parser->scope;
13669 else if (object_type)
13670 type = object_type;
13672 type = current_class_type;
13673 /* If that's not a class type, there is no destructor. */
13674 if (!type || !CLASS_TYPE_P (type))
13675 return error_mark_node;
13676 if (!CLASSTYPE_DESTRUCTORS (type))
13677 return error_mark_node;
13678 /* If it was a class type, return the destructor. */
13679 return CLASSTYPE_DESTRUCTORS (type);
13682 /* By this point, the NAME should be an ordinary identifier. If
13683 the id-expression was a qualified name, the qualifying scope is
13684 stored in PARSER->SCOPE at this point. */
13685 my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE,
13688 /* Perform the lookup. */
13693 if (parser->scope == error_mark_node)
13694 return error_mark_node;
13696 /* If the SCOPE is dependent, the lookup must be deferred until
13697 the template is instantiated -- unless we are explicitly
13698 looking up names in uninstantiated templates. Even then, we
13699 cannot look up the name if the scope is not a class type; it
13700 might, for example, be a template type parameter. */
13701 dependent_p = (TYPE_P (parser->scope)
13702 && !(parser->in_declarator_p
13703 && currently_open_class (parser->scope))
13704 && dependent_type_p (parser->scope));
13705 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
13709 /* The resolution to Core Issue 180 says that `struct A::B'
13710 should be considered a type-name, even if `A' is
13712 decl = TYPE_NAME (make_typename_type (parser->scope,
13715 else if (is_template)
13716 decl = make_unbound_class_template (parser->scope,
13720 decl = build_nt (SCOPE_REF, parser->scope, name);
13724 bool pop_p = false;
13726 /* If PARSER->SCOPE is a dependent type, then it must be a
13727 class type, and we must not be checking dependencies;
13728 otherwise, we would have processed this lookup above. So
13729 that PARSER->SCOPE is not considered a dependent base by
13730 lookup_member, we must enter the scope here. */
13732 pop_p = push_scope (parser->scope);
13733 /* If the PARSER->SCOPE is a a template specialization, it
13734 may be instantiated during name lookup. In that case,
13735 errors may be issued. Even if we rollback the current
13736 tentative parse, those errors are valid. */
13737 decl = lookup_qualified_name (parser->scope, name, is_type,
13738 /*complain=*/true);
13740 pop_scope (parser->scope);
13742 parser->qualifying_scope = parser->scope;
13743 parser->object_scope = NULL_TREE;
13745 else if (object_type)
13747 tree object_decl = NULL_TREE;
13748 /* Look up the name in the scope of the OBJECT_TYPE, unless the
13749 OBJECT_TYPE is not a class. */
13750 if (CLASS_TYPE_P (object_type))
13751 /* If the OBJECT_TYPE is a template specialization, it may
13752 be instantiated during name lookup. In that case, errors
13753 may be issued. Even if we rollback the current tentative
13754 parse, those errors are valid. */
13755 object_decl = lookup_member (object_type,
13757 /*protect=*/0, is_type);
13758 /* Look it up in the enclosing context, too. */
13759 decl = lookup_name_real (name, is_type, /*nonclass=*/0,
13762 parser->object_scope = object_type;
13763 parser->qualifying_scope = NULL_TREE;
13765 decl = object_decl;
13769 decl = lookup_name_real (name, is_type, /*nonclass=*/0,
13772 parser->qualifying_scope = NULL_TREE;
13773 parser->object_scope = NULL_TREE;
13776 /* If the lookup failed, let our caller know. */
13778 || decl == error_mark_node
13779 || (TREE_CODE (decl) == FUNCTION_DECL
13780 && DECL_ANTICIPATED (decl)))
13781 return error_mark_node;
13783 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
13784 if (TREE_CODE (decl) == TREE_LIST)
13786 /* The error message we have to print is too complicated for
13787 cp_parser_error, so we incorporate its actions directly. */
13788 if (!cp_parser_simulate_error (parser))
13790 error ("reference to `%D' is ambiguous", name);
13791 print_candidates (decl);
13793 return error_mark_node;
13796 my_friendly_assert (DECL_P (decl)
13797 || TREE_CODE (decl) == OVERLOAD
13798 || TREE_CODE (decl) == SCOPE_REF
13799 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
13800 || BASELINK_P (decl),
13803 /* If we have resolved the name of a member declaration, check to
13804 see if the declaration is accessible. When the name resolves to
13805 set of overloaded functions, accessibility is checked when
13806 overload resolution is done.
13808 During an explicit instantiation, access is not checked at all,
13809 as per [temp.explicit]. */
13811 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
13816 /* Like cp_parser_lookup_name, but for use in the typical case where
13817 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
13818 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
13821 cp_parser_lookup_name_simple (cp_parser* parser, tree name)
13823 return cp_parser_lookup_name (parser, name,
13825 /*is_template=*/false,
13826 /*is_namespace=*/false,
13827 /*check_dependency=*/true);
13830 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
13831 the current context, return the TYPE_DECL. If TAG_NAME_P is
13832 true, the DECL indicates the class being defined in a class-head,
13833 or declared in an elaborated-type-specifier.
13835 Otherwise, return DECL. */
13838 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
13840 /* If the TEMPLATE_DECL is being declared as part of a class-head,
13841 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
13844 template <typename T> struct B;
13847 template <typename T> struct A::B {};
13849 Similarly, in a elaborated-type-specifier:
13851 namespace N { struct X{}; }
13854 template <typename T> friend struct N::X;
13857 However, if the DECL refers to a class type, and we are in
13858 the scope of the class, then the name lookup automatically
13859 finds the TYPE_DECL created by build_self_reference rather
13860 than a TEMPLATE_DECL. For example, in:
13862 template <class T> struct S {
13866 there is no need to handle such case. */
13868 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
13869 return DECL_TEMPLATE_RESULT (decl);
13874 /* If too many, or too few, template-parameter lists apply to the
13875 declarator, issue an error message. Returns TRUE if all went well,
13876 and FALSE otherwise. */
13879 cp_parser_check_declarator_template_parameters (cp_parser* parser,
13882 unsigned num_templates;
13884 /* We haven't seen any classes that involve template parameters yet. */
13887 switch (TREE_CODE (declarator))
13894 tree main_declarator = TREE_OPERAND (declarator, 0);
13896 cp_parser_check_declarator_template_parameters (parser,
13905 scope = TREE_OPERAND (declarator, 0);
13906 member = TREE_OPERAND (declarator, 1);
13908 /* If this is a pointer-to-member, then we are not interested
13909 in the SCOPE, because it does not qualify the thing that is
13911 if (TREE_CODE (member) == INDIRECT_REF)
13912 return (cp_parser_check_declarator_template_parameters
13915 while (scope && CLASS_TYPE_P (scope))
13917 /* You're supposed to have one `template <...>'
13918 for every template class, but you don't need one
13919 for a full specialization. For example:
13921 template <class T> struct S{};
13922 template <> struct S<int> { void f(); };
13923 void S<int>::f () {}
13925 is correct; there shouldn't be a `template <>' for
13926 the definition of `S<int>::f'. */
13927 if (CLASSTYPE_TEMPLATE_INFO (scope)
13928 && (CLASSTYPE_TEMPLATE_INSTANTIATION (scope)
13929 || uses_template_parms (CLASSTYPE_TI_ARGS (scope)))
13930 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
13933 scope = TYPE_CONTEXT (scope);
13937 /* Fall through. */
13940 /* If the DECLARATOR has the form `X<y>' then it uses one
13941 additional level of template parameters. */
13942 if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
13945 return cp_parser_check_template_parameters (parser,
13950 /* NUM_TEMPLATES were used in the current declaration. If that is
13951 invalid, return FALSE and issue an error messages. Otherwise,
13955 cp_parser_check_template_parameters (cp_parser* parser,
13956 unsigned num_templates)
13958 /* If there are more template classes than parameter lists, we have
13961 template <class T> void S<T>::R<T>::f (); */
13962 if (parser->num_template_parameter_lists < num_templates)
13964 error ("too few template-parameter-lists");
13967 /* If there are the same number of template classes and parameter
13968 lists, that's OK. */
13969 if (parser->num_template_parameter_lists == num_templates)
13971 /* If there are more, but only one more, then we are referring to a
13972 member template. That's OK too. */
13973 if (parser->num_template_parameter_lists == num_templates + 1)
13975 /* Otherwise, there are too many template parameter lists. We have
13978 template <class T> template <class U> void S::f(); */
13979 error ("too many template-parameter-lists");
13983 /* Parse a binary-expression of the general form:
13987 binary-expression <token> <expr>
13989 The TOKEN_TREE_MAP maps <token> types to <expr> codes. FN is used
13990 to parser the <expr>s. If the first production is used, then the
13991 value returned by FN is returned directly. Otherwise, a node with
13992 the indicated EXPR_TYPE is returned, with operands corresponding to
13993 the two sub-expressions. */
13996 cp_parser_binary_expression (cp_parser* parser,
13997 const cp_parser_token_tree_map token_tree_map,
13998 cp_parser_expression_fn fn)
14002 /* Parse the first expression. */
14003 lhs = (*fn) (parser);
14004 /* Now, look for more expressions. */
14008 const cp_parser_token_tree_map_node *map_node;
14011 /* Peek at the next token. */
14012 token = cp_lexer_peek_token (parser->lexer);
14013 /* If the token is `>', and that's not an operator at the
14014 moment, then we're done. */
14015 if (token->type == CPP_GREATER
14016 && !parser->greater_than_is_operator_p)
14018 /* If we find one of the tokens we want, build the corresponding
14019 tree representation. */
14020 for (map_node = token_tree_map;
14021 map_node->token_type != CPP_EOF;
14023 if (map_node->token_type == token->type)
14025 /* Assume that an overloaded operator will not be used. */
14026 bool overloaded_p = false;
14028 /* Consume the operator token. */
14029 cp_lexer_consume_token (parser->lexer);
14030 /* Parse the right-hand side of the expression. */
14031 rhs = (*fn) (parser);
14032 /* Build the binary tree node. */
14033 lhs = build_x_binary_op (map_node->tree_type, lhs, rhs,
14035 /* If the binary operator required the use of an
14036 overloaded operator, then this expression cannot be an
14037 integral constant-expression. An overloaded operator
14038 can be used even if both operands are otherwise
14039 permissible in an integral constant-expression if at
14040 least one of the operands is of enumeration type. */
14042 && (cp_parser_non_integral_constant_expression
14043 (parser, "calls to overloaded operators")))
14044 lhs = error_mark_node;
14048 /* If the token wasn't one of the ones we want, we're done. */
14049 if (map_node->token_type == CPP_EOF)
14056 /* Parse an optional `::' token indicating that the following name is
14057 from the global namespace. If so, PARSER->SCOPE is set to the
14058 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
14059 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
14060 Returns the new value of PARSER->SCOPE, if the `::' token is
14061 present, and NULL_TREE otherwise. */
14064 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
14068 /* Peek at the next token. */
14069 token = cp_lexer_peek_token (parser->lexer);
14070 /* If we're looking at a `::' token then we're starting from the
14071 global namespace, not our current location. */
14072 if (token->type == CPP_SCOPE)
14074 /* Consume the `::' token. */
14075 cp_lexer_consume_token (parser->lexer);
14076 /* Set the SCOPE so that we know where to start the lookup. */
14077 parser->scope = global_namespace;
14078 parser->qualifying_scope = global_namespace;
14079 parser->object_scope = NULL_TREE;
14081 return parser->scope;
14083 else if (!current_scope_valid_p)
14085 parser->scope = NULL_TREE;
14086 parser->qualifying_scope = NULL_TREE;
14087 parser->object_scope = NULL_TREE;
14093 /* Returns TRUE if the upcoming token sequence is the start of a
14094 constructor declarator. If FRIEND_P is true, the declarator is
14095 preceded by the `friend' specifier. */
14098 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
14100 bool constructor_p;
14101 tree type_decl = NULL_TREE;
14102 bool nested_name_p;
14103 cp_token *next_token;
14105 /* The common case is that this is not a constructor declarator, so
14106 try to avoid doing lots of work if at all possible. It's not
14107 valid declare a constructor at function scope. */
14108 if (at_function_scope_p ())
14110 /* And only certain tokens can begin a constructor declarator. */
14111 next_token = cp_lexer_peek_token (parser->lexer);
14112 if (next_token->type != CPP_NAME
14113 && next_token->type != CPP_SCOPE
14114 && next_token->type != CPP_NESTED_NAME_SPECIFIER
14115 && next_token->type != CPP_TEMPLATE_ID)
14118 /* Parse tentatively; we are going to roll back all of the tokens
14120 cp_parser_parse_tentatively (parser);
14121 /* Assume that we are looking at a constructor declarator. */
14122 constructor_p = true;
14124 /* Look for the optional `::' operator. */
14125 cp_parser_global_scope_opt (parser,
14126 /*current_scope_valid_p=*/false);
14127 /* Look for the nested-name-specifier. */
14129 = (cp_parser_nested_name_specifier_opt (parser,
14130 /*typename_keyword_p=*/false,
14131 /*check_dependency_p=*/false,
14133 /*is_declaration=*/false)
14135 /* Outside of a class-specifier, there must be a
14136 nested-name-specifier. */
14137 if (!nested_name_p &&
14138 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
14140 constructor_p = false;
14141 /* If we still think that this might be a constructor-declarator,
14142 look for a class-name. */
14147 template <typename T> struct S { S(); };
14148 template <typename T> S<T>::S ();
14150 we must recognize that the nested `S' names a class.
14153 template <typename T> S<T>::S<T> ();
14155 we must recognize that the nested `S' names a template. */
14156 type_decl = cp_parser_class_name (parser,
14157 /*typename_keyword_p=*/false,
14158 /*template_keyword_p=*/false,
14160 /*check_dependency_p=*/false,
14161 /*class_head_p=*/false,
14162 /*is_declaration=*/false);
14163 /* If there was no class-name, then this is not a constructor. */
14164 constructor_p = !cp_parser_error_occurred (parser);
14167 /* If we're still considering a constructor, we have to see a `(',
14168 to begin the parameter-declaration-clause, followed by either a
14169 `)', an `...', or a decl-specifier. We need to check for a
14170 type-specifier to avoid being fooled into thinking that:
14174 is a constructor. (It is actually a function named `f' that
14175 takes one parameter (of type `int') and returns a value of type
14178 && cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
14180 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
14181 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
14182 /* A parameter declaration begins with a decl-specifier,
14183 which is either the "attribute" keyword, a storage class
14184 specifier, or (usually) a type-specifier. */
14185 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE)
14186 && !cp_parser_storage_class_specifier_opt (parser))
14189 bool pop_p = false;
14190 unsigned saved_num_template_parameter_lists;
14192 /* Names appearing in the type-specifier should be looked up
14193 in the scope of the class. */
14194 if (current_class_type)
14198 type = TREE_TYPE (type_decl);
14199 if (TREE_CODE (type) == TYPENAME_TYPE)
14201 type = resolve_typename_type (type,
14202 /*only_current_p=*/false);
14203 if (type == error_mark_node)
14205 cp_parser_abort_tentative_parse (parser);
14209 pop_p = push_scope (type);
14212 /* Inside the constructor parameter list, surrounding
14213 template-parameter-lists do not apply. */
14214 saved_num_template_parameter_lists
14215 = parser->num_template_parameter_lists;
14216 parser->num_template_parameter_lists = 0;
14218 /* Look for the type-specifier. */
14219 cp_parser_type_specifier (parser,
14220 CP_PARSER_FLAGS_NONE,
14221 /*is_friend=*/false,
14222 /*is_declarator=*/true,
14223 /*declares_class_or_enum=*/NULL,
14224 /*is_cv_qualifier=*/NULL);
14226 parser->num_template_parameter_lists
14227 = saved_num_template_parameter_lists;
14229 /* Leave the scope of the class. */
14233 constructor_p = !cp_parser_error_occurred (parser);
14237 constructor_p = false;
14238 /* We did not really want to consume any tokens. */
14239 cp_parser_abort_tentative_parse (parser);
14241 return constructor_p;
14244 /* Parse the definition of the function given by the DECL_SPECIFIERS,
14245 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
14246 they must be performed once we are in the scope of the function.
14248 Returns the function defined. */
14251 cp_parser_function_definition_from_specifiers_and_declarator
14252 (cp_parser* parser,
14253 tree decl_specifiers,
14260 /* Begin the function-definition. */
14261 success_p = begin_function_definition (decl_specifiers,
14265 /* If there were names looked up in the decl-specifier-seq that we
14266 did not check, check them now. We must wait until we are in the
14267 scope of the function to perform the checks, since the function
14268 might be a friend. */
14269 perform_deferred_access_checks ();
14273 /* If begin_function_definition didn't like the definition, skip
14274 the entire function. */
14275 error ("invalid function declaration");
14276 cp_parser_skip_to_end_of_block_or_statement (parser);
14277 fn = error_mark_node;
14280 fn = cp_parser_function_definition_after_declarator (parser,
14281 /*inline_p=*/false);
14286 /* Parse the part of a function-definition that follows the
14287 declarator. INLINE_P is TRUE iff this function is an inline
14288 function defined with a class-specifier.
14290 Returns the function defined. */
14293 cp_parser_function_definition_after_declarator (cp_parser* parser,
14297 bool ctor_initializer_p = false;
14298 bool saved_in_unbraced_linkage_specification_p;
14299 unsigned saved_num_template_parameter_lists;
14301 /* If the next token is `return', then the code may be trying to
14302 make use of the "named return value" extension that G++ used to
14304 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
14306 /* Consume the `return' keyword. */
14307 cp_lexer_consume_token (parser->lexer);
14308 /* Look for the identifier that indicates what value is to be
14310 cp_parser_identifier (parser);
14311 /* Issue an error message. */
14312 error ("named return values are no longer supported");
14313 /* Skip tokens until we reach the start of the function body. */
14314 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
14315 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
14316 cp_lexer_consume_token (parser->lexer);
14318 /* The `extern' in `extern "C" void f () { ... }' does not apply to
14319 anything declared inside `f'. */
14320 saved_in_unbraced_linkage_specification_p
14321 = parser->in_unbraced_linkage_specification_p;
14322 parser->in_unbraced_linkage_specification_p = false;
14323 /* Inside the function, surrounding template-parameter-lists do not
14325 saved_num_template_parameter_lists
14326 = parser->num_template_parameter_lists;
14327 parser->num_template_parameter_lists = 0;
14328 /* If the next token is `try', then we are looking at a
14329 function-try-block. */
14330 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
14331 ctor_initializer_p = cp_parser_function_try_block (parser);
14332 /* A function-try-block includes the function-body, so we only do
14333 this next part if we're not processing a function-try-block. */
14336 = cp_parser_ctor_initializer_opt_and_function_body (parser);
14338 /* Finish the function. */
14339 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
14340 (inline_p ? 2 : 0));
14341 /* Generate code for it, if necessary. */
14342 expand_or_defer_fn (fn);
14343 /* Restore the saved values. */
14344 parser->in_unbraced_linkage_specification_p
14345 = saved_in_unbraced_linkage_specification_p;
14346 parser->num_template_parameter_lists
14347 = saved_num_template_parameter_lists;
14352 /* Parse a template-declaration, assuming that the `export' (and
14353 `extern') keywords, if present, has already been scanned. MEMBER_P
14354 is as for cp_parser_template_declaration. */
14357 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
14359 tree decl = NULL_TREE;
14360 tree parameter_list;
14361 bool friend_p = false;
14363 /* Look for the `template' keyword. */
14364 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'"))
14368 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
14371 /* If the next token is `>', then we have an invalid
14372 specialization. Rather than complain about an invalid template
14373 parameter, issue an error message here. */
14374 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
14376 cp_parser_error (parser, "invalid explicit specialization");
14377 begin_specialization ();
14378 parameter_list = NULL_TREE;
14382 /* Parse the template parameters. */
14383 begin_template_parm_list ();
14384 parameter_list = cp_parser_template_parameter_list (parser);
14385 parameter_list = end_template_parm_list (parameter_list);
14388 /* Look for the `>'. */
14389 cp_parser_skip_until_found (parser, CPP_GREATER, "`>'");
14390 /* We just processed one more parameter list. */
14391 ++parser->num_template_parameter_lists;
14392 /* If the next token is `template', there are more template
14394 if (cp_lexer_next_token_is_keyword (parser->lexer,
14396 cp_parser_template_declaration_after_export (parser, member_p);
14399 decl = cp_parser_single_declaration (parser,
14403 /* If this is a member template declaration, let the front
14405 if (member_p && !friend_p && decl)
14407 if (TREE_CODE (decl) == TYPE_DECL)
14408 cp_parser_check_access_in_redeclaration (decl);
14410 decl = finish_member_template_decl (decl);
14412 else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
14413 make_friend_class (current_class_type, TREE_TYPE (decl),
14414 /*complain=*/true);
14416 /* We are done with the current parameter list. */
14417 --parser->num_template_parameter_lists;
14420 finish_template_decl (parameter_list);
14422 /* Register member declarations. */
14423 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
14424 finish_member_declaration (decl);
14426 /* If DECL is a function template, we must return to parse it later.
14427 (Even though there is no definition, there might be default
14428 arguments that need handling.) */
14429 if (member_p && decl
14430 && (TREE_CODE (decl) == FUNCTION_DECL
14431 || DECL_FUNCTION_TEMPLATE_P (decl)))
14432 TREE_VALUE (parser->unparsed_functions_queues)
14433 = tree_cons (NULL_TREE, decl,
14434 TREE_VALUE (parser->unparsed_functions_queues));
14437 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
14438 `function-definition' sequence. MEMBER_P is true, this declaration
14439 appears in a class scope.
14441 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
14442 *FRIEND_P is set to TRUE iff the declaration is a friend. */
14445 cp_parser_single_declaration (cp_parser* parser,
14449 int declares_class_or_enum;
14450 tree decl = NULL_TREE;
14451 tree decl_specifiers;
14453 bool function_definition_p = false;
14455 /* Defer access checks until we know what is being declared. */
14456 push_deferring_access_checks (dk_deferred);
14458 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
14461 = cp_parser_decl_specifier_seq (parser,
14462 CP_PARSER_FLAGS_OPTIONAL,
14464 &declares_class_or_enum);
14466 *friend_p = cp_parser_friend_p (decl_specifiers);
14467 /* Gather up the access checks that occurred the
14468 decl-specifier-seq. */
14469 stop_deferring_access_checks ();
14471 /* Check for the declaration of a template class. */
14472 if (declares_class_or_enum)
14474 if (cp_parser_declares_only_class_p (parser))
14476 decl = shadow_tag (decl_specifiers);
14478 decl = TYPE_NAME (decl);
14480 decl = error_mark_node;
14485 /* If it's not a template class, try for a template function. If
14486 the next token is a `;', then this declaration does not declare
14487 anything. But, if there were errors in the decl-specifiers, then
14488 the error might well have come from an attempted class-specifier.
14489 In that case, there's no need to warn about a missing declarator. */
14491 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
14492 || !value_member (error_mark_node, decl_specifiers)))
14493 decl = cp_parser_init_declarator (parser,
14496 /*function_definition_allowed_p=*/true,
14498 declares_class_or_enum,
14499 &function_definition_p);
14501 pop_deferring_access_checks ();
14503 /* Clear any current qualification; whatever comes next is the start
14504 of something new. */
14505 parser->scope = NULL_TREE;
14506 parser->qualifying_scope = NULL_TREE;
14507 parser->object_scope = NULL_TREE;
14508 /* Look for a trailing `;' after the declaration. */
14509 if (!function_definition_p
14510 && !cp_parser_require (parser, CPP_SEMICOLON, "`;'"))
14511 cp_parser_skip_to_end_of_block_or_statement (parser);
14516 /* Parse a cast-expression that is not the operand of a unary "&". */
14519 cp_parser_simple_cast_expression (cp_parser *parser)
14521 return cp_parser_cast_expression (parser, /*address_p=*/false);
14524 /* Parse a functional cast to TYPE. Returns an expression
14525 representing the cast. */
14528 cp_parser_functional_cast (cp_parser* parser, tree type)
14530 tree expression_list;
14534 = cp_parser_parenthesized_expression_list (parser, false,
14535 /*non_constant_p=*/NULL);
14537 cast = build_functional_cast (type, expression_list);
14538 /* [expr.const]/1: In an integral constant expression "only type
14539 conversions to integral or enumeration type can be used". */
14540 if (cast != error_mark_node && !type_dependent_expression_p (type)
14541 && !INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (type)))
14543 if (cp_parser_non_integral_constant_expression
14544 (parser, "a call to a constructor"))
14545 return error_mark_node;
14550 /* Save the tokens that make up the body of a member function defined
14551 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
14552 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
14553 specifiers applied to the declaration. Returns the FUNCTION_DECL
14554 for the member function. */
14557 cp_parser_save_member_function_body (cp_parser* parser,
14558 tree decl_specifiers,
14562 cp_token_cache *cache;
14565 /* Create the function-declaration. */
14566 fn = start_method (decl_specifiers, declarator, attributes);
14567 /* If something went badly wrong, bail out now. */
14568 if (fn == error_mark_node)
14570 /* If there's a function-body, skip it. */
14571 if (cp_parser_token_starts_function_definition_p
14572 (cp_lexer_peek_token (parser->lexer)))
14573 cp_parser_skip_to_end_of_block_or_statement (parser);
14574 return error_mark_node;
14577 /* Remember it, if there default args to post process. */
14578 cp_parser_save_default_args (parser, fn);
14580 /* Create a token cache. */
14581 cache = cp_token_cache_new ();
14582 /* Save away the tokens that make up the body of the
14584 cp_parser_cache_group (parser, cache, CPP_CLOSE_BRACE, /*depth=*/0);
14585 /* Handle function try blocks. */
14586 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
14587 cp_parser_cache_group (parser, cache, CPP_CLOSE_BRACE, /*depth=*/0);
14589 /* Save away the inline definition; we will process it when the
14590 class is complete. */
14591 DECL_PENDING_INLINE_INFO (fn) = cache;
14592 DECL_PENDING_INLINE_P (fn) = 1;
14594 /* We need to know that this was defined in the class, so that
14595 friend templates are handled correctly. */
14596 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
14598 /* We're done with the inline definition. */
14599 finish_method (fn);
14601 /* Add FN to the queue of functions to be parsed later. */
14602 TREE_VALUE (parser->unparsed_functions_queues)
14603 = tree_cons (NULL_TREE, fn,
14604 TREE_VALUE (parser->unparsed_functions_queues));
14609 /* Parse a template-argument-list, as well as the trailing ">" (but
14610 not the opening ">"). See cp_parser_template_argument_list for the
14614 cp_parser_enclosed_template_argument_list (cp_parser* parser)
14618 tree saved_qualifying_scope;
14619 tree saved_object_scope;
14620 bool saved_greater_than_is_operator_p;
14624 When parsing a template-id, the first non-nested `>' is taken as
14625 the end of the template-argument-list rather than a greater-than
14627 saved_greater_than_is_operator_p
14628 = parser->greater_than_is_operator_p;
14629 parser->greater_than_is_operator_p = false;
14630 /* Parsing the argument list may modify SCOPE, so we save it
14632 saved_scope = parser->scope;
14633 saved_qualifying_scope = parser->qualifying_scope;
14634 saved_object_scope = parser->object_scope;
14635 /* Parse the template-argument-list itself. */
14636 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
14637 arguments = NULL_TREE;
14639 arguments = cp_parser_template_argument_list (parser);
14640 /* Look for the `>' that ends the template-argument-list. If we find
14641 a '>>' instead, it's probably just a typo. */
14642 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
14644 if (!saved_greater_than_is_operator_p)
14646 /* If we're in a nested template argument list, the '>>' has to be
14647 a typo for '> >'. We emit the error message, but we continue
14648 parsing and we push a '>' as next token, so that the argument
14649 list will be parsed correctly.. */
14651 error ("`>>' should be `> >' within a nested template argument list");
14652 token = cp_lexer_peek_token (parser->lexer);
14653 token->type = CPP_GREATER;
14657 /* If this is not a nested template argument list, the '>>' is
14658 a typo for '>'. Emit an error message and continue. */
14659 error ("spurious `>>', use `>' to terminate a template argument list");
14660 cp_lexer_consume_token (parser->lexer);
14663 else if (!cp_parser_require (parser, CPP_GREATER, "`>'"))
14664 error ("missing `>' to terminate the template argument list");
14665 /* The `>' token might be a greater-than operator again now. */
14666 parser->greater_than_is_operator_p
14667 = saved_greater_than_is_operator_p;
14668 /* Restore the SAVED_SCOPE. */
14669 parser->scope = saved_scope;
14670 parser->qualifying_scope = saved_qualifying_scope;
14671 parser->object_scope = saved_object_scope;
14676 /* MEMBER_FUNCTION is a member function, or a friend. If default
14677 arguments, or the body of the function have not yet been parsed,
14681 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
14683 cp_lexer *saved_lexer;
14685 /* If this member is a template, get the underlying
14687 if (DECL_FUNCTION_TEMPLATE_P (member_function))
14688 member_function = DECL_TEMPLATE_RESULT (member_function);
14690 /* There should not be any class definitions in progress at this
14691 point; the bodies of members are only parsed outside of all class
14693 my_friendly_assert (parser->num_classes_being_defined == 0, 20010816);
14694 /* While we're parsing the member functions we might encounter more
14695 classes. We want to handle them right away, but we don't want
14696 them getting mixed up with functions that are currently in the
14698 parser->unparsed_functions_queues
14699 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
14701 /* Make sure that any template parameters are in scope. */
14702 maybe_begin_member_template_processing (member_function);
14704 /* If the body of the function has not yet been parsed, parse it
14706 if (DECL_PENDING_INLINE_P (member_function))
14708 tree function_scope;
14709 cp_token_cache *tokens;
14711 /* The function is no longer pending; we are processing it. */
14712 tokens = DECL_PENDING_INLINE_INFO (member_function);
14713 DECL_PENDING_INLINE_INFO (member_function) = NULL;
14714 DECL_PENDING_INLINE_P (member_function) = 0;
14715 /* If this was an inline function in a local class, enter the scope
14716 of the containing function. */
14717 function_scope = decl_function_context (member_function);
14718 if (function_scope)
14719 push_function_context_to (function_scope);
14721 /* Save away the current lexer. */
14722 saved_lexer = parser->lexer;
14723 /* Make a new lexer to feed us the tokens saved for this function. */
14724 parser->lexer = cp_lexer_new_from_tokens (tokens);
14725 parser->lexer->next = saved_lexer;
14727 /* Set the current source position to be the location of the first
14728 token in the saved inline body. */
14729 cp_lexer_peek_token (parser->lexer);
14731 /* Let the front end know that we going to be defining this
14733 start_function (NULL_TREE, member_function, NULL_TREE,
14734 SF_PRE_PARSED | SF_INCLASS_INLINE);
14736 /* Now, parse the body of the function. */
14737 cp_parser_function_definition_after_declarator (parser,
14738 /*inline_p=*/true);
14740 /* Leave the scope of the containing function. */
14741 if (function_scope)
14742 pop_function_context_from (function_scope);
14743 /* Restore the lexer. */
14744 parser->lexer = saved_lexer;
14747 /* Remove any template parameters from the symbol table. */
14748 maybe_end_member_template_processing ();
14750 /* Restore the queue. */
14751 parser->unparsed_functions_queues
14752 = TREE_CHAIN (parser->unparsed_functions_queues);
14755 /* If DECL contains any default args, remember it on the unparsed
14756 functions queue. */
14759 cp_parser_save_default_args (cp_parser* parser, tree decl)
14763 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
14765 probe = TREE_CHAIN (probe))
14766 if (TREE_PURPOSE (probe))
14768 TREE_PURPOSE (parser->unparsed_functions_queues)
14769 = tree_cons (NULL_TREE, decl,
14770 TREE_PURPOSE (parser->unparsed_functions_queues));
14776 /* FN is a FUNCTION_DECL which may contains a parameter with an
14777 unparsed DEFAULT_ARG. Parse the default args now. */
14780 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
14782 cp_lexer *saved_lexer;
14783 cp_token_cache *tokens;
14784 bool saved_local_variables_forbidden_p;
14787 /* While we're parsing the default args, we might (due to the
14788 statement expression extension) encounter more classes. We want
14789 to handle them right away, but we don't want them getting mixed
14790 up with default args that are currently in the queue. */
14791 parser->unparsed_functions_queues
14792 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
14794 for (parameters = TYPE_ARG_TYPES (TREE_TYPE (fn));
14796 parameters = TREE_CHAIN (parameters))
14798 if (!TREE_PURPOSE (parameters)
14799 || TREE_CODE (TREE_PURPOSE (parameters)) != DEFAULT_ARG)
14802 /* Save away the current lexer. */
14803 saved_lexer = parser->lexer;
14804 /* Create a new one, using the tokens we have saved. */
14805 tokens = DEFARG_TOKENS (TREE_PURPOSE (parameters));
14806 parser->lexer = cp_lexer_new_from_tokens (tokens);
14808 /* Set the current source position to be the location of the
14809 first token in the default argument. */
14810 cp_lexer_peek_token (parser->lexer);
14812 /* Local variable names (and the `this' keyword) may not appear
14813 in a default argument. */
14814 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
14815 parser->local_variables_forbidden_p = true;
14816 /* Parse the assignment-expression. */
14817 if (DECL_CLASS_SCOPE_P (fn))
14818 push_nested_class (DECL_CONTEXT (fn));
14819 TREE_PURPOSE (parameters) = cp_parser_assignment_expression (parser);
14820 if (DECL_CLASS_SCOPE_P (fn))
14821 pop_nested_class ();
14823 /* If the token stream has not been completely used up, then
14824 there was extra junk after the end of the default
14826 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
14827 cp_parser_error (parser, "expected `,'");
14829 /* Restore saved state. */
14830 parser->lexer = saved_lexer;
14831 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
14834 /* Restore the queue. */
14835 parser->unparsed_functions_queues
14836 = TREE_CHAIN (parser->unparsed_functions_queues);
14839 /* Parse the operand of `sizeof' (or a similar operator). Returns
14840 either a TYPE or an expression, depending on the form of the
14841 input. The KEYWORD indicates which kind of expression we have
14845 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
14847 static const char *format;
14848 tree expr = NULL_TREE;
14849 const char *saved_message;
14850 bool saved_integral_constant_expression_p;
14852 /* Initialize FORMAT the first time we get here. */
14854 format = "types may not be defined in `%s' expressions";
14856 /* Types cannot be defined in a `sizeof' expression. Save away the
14858 saved_message = parser->type_definition_forbidden_message;
14859 /* And create the new one. */
14860 parser->type_definition_forbidden_message
14861 = xmalloc (strlen (format)
14862 + strlen (IDENTIFIER_POINTER (ridpointers[keyword]))
14864 sprintf ((char *) parser->type_definition_forbidden_message,
14865 format, IDENTIFIER_POINTER (ridpointers[keyword]));
14867 /* The restrictions on constant-expressions do not apply inside
14868 sizeof expressions. */
14869 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
14870 parser->integral_constant_expression_p = false;
14872 /* Do not actually evaluate the expression. */
14874 /* If it's a `(', then we might be looking at the type-id
14876 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
14879 bool saved_in_type_id_in_expr_p;
14881 /* We can't be sure yet whether we're looking at a type-id or an
14883 cp_parser_parse_tentatively (parser);
14884 /* Consume the `('. */
14885 cp_lexer_consume_token (parser->lexer);
14886 /* Parse the type-id. */
14887 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
14888 parser->in_type_id_in_expr_p = true;
14889 type = cp_parser_type_id (parser);
14890 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
14891 /* Now, look for the trailing `)'. */
14892 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14893 /* If all went well, then we're done. */
14894 if (cp_parser_parse_definitely (parser))
14896 /* Build a list of decl-specifiers; right now, we have only
14897 a single type-specifier. */
14898 type = build_tree_list (NULL_TREE,
14901 /* Call grokdeclarator to figure out what type this is. */
14902 expr = grokdeclarator (NULL_TREE,
14906 /*attrlist=*/NULL);
14910 /* If the type-id production did not work out, then we must be
14911 looking at the unary-expression production. */
14913 expr = cp_parser_unary_expression (parser, /*address_p=*/false);
14914 /* Go back to evaluating expressions. */
14917 /* Free the message we created. */
14918 free ((char *) parser->type_definition_forbidden_message);
14919 /* And restore the old one. */
14920 parser->type_definition_forbidden_message = saved_message;
14921 parser->integral_constant_expression_p = saved_integral_constant_expression_p;
14926 /* If the current declaration has no declarator, return true. */
14929 cp_parser_declares_only_class_p (cp_parser *parser)
14931 /* If the next token is a `;' or a `,' then there is no
14933 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
14934 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
14937 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
14938 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
14941 cp_parser_friend_p (tree decl_specifiers)
14943 while (decl_specifiers)
14945 /* See if this decl-specifier is `friend'. */
14946 if (TREE_CODE (TREE_VALUE (decl_specifiers)) == IDENTIFIER_NODE
14947 && C_RID_CODE (TREE_VALUE (decl_specifiers)) == RID_FRIEND)
14950 /* Go on to the next decl-specifier. */
14951 decl_specifiers = TREE_CHAIN (decl_specifiers);
14957 /* If the next token is of the indicated TYPE, consume it. Otherwise,
14958 issue an error message indicating that TOKEN_DESC was expected.
14960 Returns the token consumed, if the token had the appropriate type.
14961 Otherwise, returns NULL. */
14964 cp_parser_require (cp_parser* parser,
14965 enum cpp_ttype type,
14966 const char* token_desc)
14968 if (cp_lexer_next_token_is (parser->lexer, type))
14969 return cp_lexer_consume_token (parser->lexer);
14972 /* Output the MESSAGE -- unless we're parsing tentatively. */
14973 if (!cp_parser_simulate_error (parser))
14975 char *message = concat ("expected ", token_desc, NULL);
14976 cp_parser_error (parser, message);
14983 /* Like cp_parser_require, except that tokens will be skipped until
14984 the desired token is found. An error message is still produced if
14985 the next token is not as expected. */
14988 cp_parser_skip_until_found (cp_parser* parser,
14989 enum cpp_ttype type,
14990 const char* token_desc)
14993 unsigned nesting_depth = 0;
14995 if (cp_parser_require (parser, type, token_desc))
14998 /* Skip tokens until the desired token is found. */
15001 /* Peek at the next token. */
15002 token = cp_lexer_peek_token (parser->lexer);
15003 /* If we've reached the token we want, consume it and
15005 if (token->type == type && !nesting_depth)
15007 cp_lexer_consume_token (parser->lexer);
15010 /* If we've run out of tokens, stop. */
15011 if (token->type == CPP_EOF)
15013 if (token->type == CPP_OPEN_BRACE
15014 || token->type == CPP_OPEN_PAREN
15015 || token->type == CPP_OPEN_SQUARE)
15017 else if (token->type == CPP_CLOSE_BRACE
15018 || token->type == CPP_CLOSE_PAREN
15019 || token->type == CPP_CLOSE_SQUARE)
15021 if (nesting_depth-- == 0)
15024 /* Consume this token. */
15025 cp_lexer_consume_token (parser->lexer);
15029 /* If the next token is the indicated keyword, consume it. Otherwise,
15030 issue an error message indicating that TOKEN_DESC was expected.
15032 Returns the token consumed, if the token had the appropriate type.
15033 Otherwise, returns NULL. */
15036 cp_parser_require_keyword (cp_parser* parser,
15038 const char* token_desc)
15040 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
15042 if (token && token->keyword != keyword)
15044 dyn_string_t error_msg;
15046 /* Format the error message. */
15047 error_msg = dyn_string_new (0);
15048 dyn_string_append_cstr (error_msg, "expected ");
15049 dyn_string_append_cstr (error_msg, token_desc);
15050 cp_parser_error (parser, error_msg->s);
15051 dyn_string_delete (error_msg);
15058 /* Returns TRUE iff TOKEN is a token that can begin the body of a
15059 function-definition. */
15062 cp_parser_token_starts_function_definition_p (cp_token* token)
15064 return (/* An ordinary function-body begins with an `{'. */
15065 token->type == CPP_OPEN_BRACE
15066 /* A ctor-initializer begins with a `:'. */
15067 || token->type == CPP_COLON
15068 /* A function-try-block begins with `try'. */
15069 || token->keyword == RID_TRY
15070 /* The named return value extension begins with `return'. */
15071 || token->keyword == RID_RETURN);
15074 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
15078 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
15082 token = cp_lexer_peek_token (parser->lexer);
15083 return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
15086 /* Returns TRUE iff the next token is the "," or ">" ending a
15087 template-argument. */
15090 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
15094 token = cp_lexer_peek_token (parser->lexer);
15095 return (token->type == CPP_COMMA || token->type == CPP_GREATER);
15098 /* Returns TRUE iff the n-th token is a ">", or the n-th is a "[" and the
15099 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
15102 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
15107 token = cp_lexer_peek_nth_token (parser->lexer, n);
15108 if (token->type == CPP_LESS)
15110 /* Check for the sequence `<::' in the original code. It would be lexed as
15111 `[:', where `[' is a digraph, and there is no whitespace before
15113 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
15116 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
15117 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
15123 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
15124 or none_type otherwise. */
15126 static enum tag_types
15127 cp_parser_token_is_class_key (cp_token* token)
15129 switch (token->keyword)
15134 return record_type;
15143 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
15146 cp_parser_check_class_key (enum tag_types class_key, tree type)
15148 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
15149 pedwarn ("`%s' tag used in naming `%#T'",
15150 class_key == union_type ? "union"
15151 : class_key == record_type ? "struct" : "class",
15155 /* Issue an error message if DECL is redeclared with different
15156 access than its original declaration [class.access.spec/3].
15157 This applies to nested classes and nested class templates.
15160 static void cp_parser_check_access_in_redeclaration (tree decl)
15162 if (!CLASS_TYPE_P (TREE_TYPE (decl)))
15165 if ((TREE_PRIVATE (decl)
15166 != (current_access_specifier == access_private_node))
15167 || (TREE_PROTECTED (decl)
15168 != (current_access_specifier == access_protected_node)))
15169 error ("%D redeclared with different access", decl);
15172 /* Look for the `template' keyword, as a syntactic disambiguator.
15173 Return TRUE iff it is present, in which case it will be
15177 cp_parser_optional_template_keyword (cp_parser *parser)
15179 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
15181 /* The `template' keyword can only be used within templates;
15182 outside templates the parser can always figure out what is a
15183 template and what is not. */
15184 if (!processing_template_decl)
15186 error ("`template' (as a disambiguator) is only allowed "
15187 "within templates");
15188 /* If this part of the token stream is rescanned, the same
15189 error message would be generated. So, we purge the token
15190 from the stream. */
15191 cp_lexer_purge_token (parser->lexer);
15196 /* Consume the `template' keyword. */
15197 cp_lexer_consume_token (parser->lexer);
15205 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
15206 set PARSER->SCOPE, and perform other related actions. */
15209 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
15214 /* Get the stored value. */
15215 value = cp_lexer_consume_token (parser->lexer)->value;
15216 /* Perform any access checks that were deferred. */
15217 for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
15218 perform_or_defer_access_check (TREE_PURPOSE (check), TREE_VALUE (check));
15219 /* Set the scope from the stored value. */
15220 parser->scope = TREE_VALUE (value);
15221 parser->qualifying_scope = TREE_TYPE (value);
15222 parser->object_scope = NULL_TREE;
15225 /* Add tokens to CACHE until an non-nested END token appears. */
15228 cp_parser_cache_group (cp_parser *parser,
15229 cp_token_cache *cache,
15230 enum cpp_ttype end,
15237 /* Abort a parenthesized expression if we encounter a brace. */
15238 if ((end == CPP_CLOSE_PAREN || depth == 0)
15239 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
15241 /* If we've reached the end of the file, stop. */
15242 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
15244 /* Consume the next token. */
15245 token = cp_lexer_consume_token (parser->lexer);
15246 /* Add this token to the tokens we are saving. */
15247 cp_token_cache_push_token (cache, token);
15248 /* See if it starts a new group. */
15249 if (token->type == CPP_OPEN_BRACE)
15251 cp_parser_cache_group (parser, cache, CPP_CLOSE_BRACE, depth + 1);
15255 else if (token->type == CPP_OPEN_PAREN)
15256 cp_parser_cache_group (parser, cache, CPP_CLOSE_PAREN, depth + 1);
15257 else if (token->type == end)
15262 /* Begin parsing tentatively. We always save tokens while parsing
15263 tentatively so that if the tentative parsing fails we can restore the
15267 cp_parser_parse_tentatively (cp_parser* parser)
15269 /* Enter a new parsing context. */
15270 parser->context = cp_parser_context_new (parser->context);
15271 /* Begin saving tokens. */
15272 cp_lexer_save_tokens (parser->lexer);
15273 /* In order to avoid repetitive access control error messages,
15274 access checks are queued up until we are no longer parsing
15276 push_deferring_access_checks (dk_deferred);
15279 /* Commit to the currently active tentative parse. */
15282 cp_parser_commit_to_tentative_parse (cp_parser* parser)
15284 cp_parser_context *context;
15287 /* Mark all of the levels as committed. */
15288 lexer = parser->lexer;
15289 for (context = parser->context; context->next; context = context->next)
15291 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
15293 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
15294 while (!cp_lexer_saving_tokens (lexer))
15295 lexer = lexer->next;
15296 cp_lexer_commit_tokens (lexer);
15300 /* Abort the currently active tentative parse. All consumed tokens
15301 will be rolled back, and no diagnostics will be issued. */
15304 cp_parser_abort_tentative_parse (cp_parser* parser)
15306 cp_parser_simulate_error (parser);
15307 /* Now, pretend that we want to see if the construct was
15308 successfully parsed. */
15309 cp_parser_parse_definitely (parser);
15312 /* Stop parsing tentatively. If a parse error has occurred, restore the
15313 token stream. Otherwise, commit to the tokens we have consumed.
15314 Returns true if no error occurred; false otherwise. */
15317 cp_parser_parse_definitely (cp_parser* parser)
15319 bool error_occurred;
15320 cp_parser_context *context;
15322 /* Remember whether or not an error occurred, since we are about to
15323 destroy that information. */
15324 error_occurred = cp_parser_error_occurred (parser);
15325 /* Remove the topmost context from the stack. */
15326 context = parser->context;
15327 parser->context = context->next;
15328 /* If no parse errors occurred, commit to the tentative parse. */
15329 if (!error_occurred)
15331 /* Commit to the tokens read tentatively, unless that was
15333 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
15334 cp_lexer_commit_tokens (parser->lexer);
15336 pop_to_parent_deferring_access_checks ();
15338 /* Otherwise, if errors occurred, roll back our state so that things
15339 are just as they were before we began the tentative parse. */
15342 cp_lexer_rollback_tokens (parser->lexer);
15343 pop_deferring_access_checks ();
15345 /* Add the context to the front of the free list. */
15346 context->next = cp_parser_context_free_list;
15347 cp_parser_context_free_list = context;
15349 return !error_occurred;
15352 /* Returns true if we are parsing tentatively -- but have decided that
15353 we will stick with this tentative parse, even if errors occur. */
15356 cp_parser_committed_to_tentative_parse (cp_parser* parser)
15358 return (cp_parser_parsing_tentatively (parser)
15359 && parser->context->status == CP_PARSER_STATUS_KIND_COMMITTED);
15362 /* Returns nonzero iff an error has occurred during the most recent
15363 tentative parse. */
15366 cp_parser_error_occurred (cp_parser* parser)
15368 return (cp_parser_parsing_tentatively (parser)
15369 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
15372 /* Returns nonzero if GNU extensions are allowed. */
15375 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
15377 return parser->allow_gnu_extensions_p;
15384 static GTY (()) cp_parser *the_parser;
15386 /* External interface. */
15388 /* Parse one entire translation unit. */
15391 c_parse_file (void)
15393 bool error_occurred;
15395 the_parser = cp_parser_new ();
15396 push_deferring_access_checks (flag_access_control
15397 ? dk_no_deferred : dk_no_check);
15398 error_occurred = cp_parser_translation_unit (the_parser);
15402 /* This variable must be provided by every front end. */
15406 #include "gt-cp-parser.h"