2 Copyright (C) 2000, 2001, 2002, 2003 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"
45 A cp_lexer represents a stream of cp_tokens. It allows arbitrary
51 We use a circular buffer to store incoming tokens.
53 Some artifacts of the C++ language (such as the
54 expression/declaration ambiguity) require arbitrary look-ahead.
55 The strategy we adopt for dealing with these problems is to attempt
56 to parse one construct (e.g., the declaration) and fall back to the
57 other (e.g., the expression) if that attempt does not succeed.
58 Therefore, we must sometimes store an arbitrary number of tokens.
60 The parser routinely peeks at the next token, and then consumes it
61 later. That also requires a buffer in which to store the tokens.
63 In order to easily permit adding tokens to the end of the buffer,
64 while removing them from the beginning of the buffer, we use a
69 typedef struct cp_token GTY (())
71 /* The kind of token. */
73 /* The value associated with this token, if any. */
75 /* If this token is a keyword, this value indicates which keyword.
76 Otherwise, this value is RID_MAX. */
78 /* The file in which this token was found. */
79 const char *file_name;
80 /* The line at which this token was found. */
84 /* The number of tokens in a single token block. */
86 #define CP_TOKEN_BLOCK_NUM_TOKENS 32
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 contraints 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 ()
133 return (cp_token_cache *) 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 = ((cp_token_block *) 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
218 static cp_lexer *cp_lexer_new_from_tokens
219 PARAMS ((struct cp_token_cache *));
220 static int cp_lexer_saving_tokens
221 PARAMS ((const cp_lexer *));
222 static cp_token *cp_lexer_next_token
223 PARAMS ((cp_lexer *, cp_token *));
224 static ptrdiff_t cp_lexer_token_difference
225 PARAMS ((cp_lexer *, cp_token *, cp_token *));
226 static cp_token *cp_lexer_read_token
227 PARAMS ((cp_lexer *));
228 static void cp_lexer_maybe_grow_buffer
229 PARAMS ((cp_lexer *));
230 static void cp_lexer_get_preprocessor_token
231 PARAMS ((cp_lexer *, cp_token *));
232 static cp_token *cp_lexer_peek_token
233 PARAMS ((cp_lexer *));
234 static cp_token *cp_lexer_peek_nth_token
235 PARAMS ((cp_lexer *, size_t));
236 static inline bool cp_lexer_next_token_is
237 PARAMS ((cp_lexer *, enum cpp_ttype));
238 static bool cp_lexer_next_token_is_not
239 PARAMS ((cp_lexer *, enum cpp_ttype));
240 static bool cp_lexer_next_token_is_keyword
241 PARAMS ((cp_lexer *, enum rid));
242 static cp_token *cp_lexer_consume_token
243 PARAMS ((cp_lexer *));
244 static void cp_lexer_purge_token
246 static void cp_lexer_purge_tokens_after
247 (cp_lexer *, cp_token *);
248 static void cp_lexer_save_tokens
249 PARAMS ((cp_lexer *));
250 static void cp_lexer_commit_tokens
251 PARAMS ((cp_lexer *));
252 static void cp_lexer_rollback_tokens
253 PARAMS ((cp_lexer *));
254 static inline void cp_lexer_set_source_position_from_token
255 PARAMS ((cp_lexer *, const cp_token *));
256 static void cp_lexer_print_token
257 PARAMS ((FILE *, cp_token *));
258 static inline bool cp_lexer_debugging_p
259 PARAMS ((cp_lexer *));
260 static void cp_lexer_start_debugging
261 PARAMS ((cp_lexer *)) ATTRIBUTE_UNUSED;
262 static void cp_lexer_stop_debugging
263 PARAMS ((cp_lexer *)) ATTRIBUTE_UNUSED;
265 /* Manifest constants. */
267 #define CP_TOKEN_BUFFER_SIZE 5
268 #define CP_SAVED_TOKENS_SIZE 5
270 /* A token type for keywords, as opposed to ordinary identifiers. */
271 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
273 /* A token type for template-ids. If a template-id is processed while
274 parsing tentatively, it is replaced with a CPP_TEMPLATE_ID token;
275 the value of the CPP_TEMPLATE_ID is whatever was returned by
276 cp_parser_template_id. */
277 #define CPP_TEMPLATE_ID ((enum cpp_ttype) (CPP_KEYWORD + 1))
279 /* A token type for nested-name-specifiers. If a
280 nested-name-specifier is processed while parsing tentatively, it is
281 replaced with a CPP_NESTED_NAME_SPECIFIER token; the value of the
282 CPP_NESTED_NAME_SPECIFIER is whatever was returned by
283 cp_parser_nested_name_specifier_opt. */
284 #define CPP_NESTED_NAME_SPECIFIER ((enum cpp_ttype) (CPP_TEMPLATE_ID + 1))
286 /* A token type for tokens that are not tokens at all; these are used
287 to mark the end of a token block. */
288 #define CPP_NONE (CPP_NESTED_NAME_SPECIFIER + 1)
292 /* The stream to which debugging output should be written. */
293 static FILE *cp_lexer_debug_stream;
295 /* Create a new C++ lexer. If MAIN_LEXER_P is true the new lexer is
296 the main lexer -- i.e, the lexer that gets tokens from the
297 preprocessor. Otherwise, it is a lexer that uses a cache of stored
301 cp_lexer_new (bool main_lexer_p)
305 /* Allocate the memory. */
306 lexer = (cp_lexer *) ggc_alloc_cleared (sizeof (cp_lexer));
308 /* Create the circular buffer. */
309 lexer->buffer = ((cp_token *)
310 ggc_alloc (CP_TOKEN_BUFFER_SIZE * sizeof (cp_token)));
311 lexer->buffer_end = lexer->buffer + CP_TOKEN_BUFFER_SIZE;
313 /* There are no tokens in the buffer. */
314 lexer->last_token = lexer->buffer;
316 /* This lexer obtains more tokens by calling c_lex. */
317 lexer->main_lexer_p = main_lexer_p;
319 /* Create the SAVED_TOKENS stack. */
320 VARRAY_INT_INIT (lexer->saved_tokens, CP_SAVED_TOKENS_SIZE, "saved_tokens");
322 /* Create the STRINGS array. */
323 VARRAY_TREE_INIT (lexer->string_tokens, 32, "strings");
325 /* Assume we are not debugging. */
326 lexer->debugging_p = false;
331 /* Create a new lexer whose token stream is primed with the TOKENS.
332 When these tokens are exhausted, no new tokens will be read. */
335 cp_lexer_new_from_tokens (cp_token_cache *tokens)
339 cp_token_block *block;
340 ptrdiff_t num_tokens;
342 /* Create the lexer. */
343 lexer = cp_lexer_new (/*main_lexer_p=*/false);
345 /* Create a new buffer, appropriately sized. */
347 for (block = tokens->first; block != NULL; block = block->next)
348 num_tokens += block->num_tokens;
349 lexer->buffer = ((cp_token *)
350 ggc_alloc (num_tokens * sizeof (cp_token)));
351 lexer->buffer_end = lexer->buffer + num_tokens;
353 /* Install the tokens. */
354 token = lexer->buffer;
355 for (block = tokens->first; block != NULL; block = block->next)
357 memcpy (token, block->tokens, block->num_tokens * sizeof (cp_token));
358 token += block->num_tokens;
361 /* The FIRST_TOKEN is the beginning of the buffer. */
362 lexer->first_token = lexer->buffer;
363 /* The next available token is also at the beginning of the buffer. */
364 lexer->next_token = lexer->buffer;
365 /* The buffer is full. */
366 lexer->last_token = lexer->first_token;
371 /* Returns non-zero if debugging information should be output. */
374 cp_lexer_debugging_p (cp_lexer *lexer)
376 return lexer->debugging_p;
379 /* Set the current source position from the information stored in
383 cp_lexer_set_source_position_from_token (lexer, token)
384 cp_lexer *lexer ATTRIBUTE_UNUSED;
385 const cp_token *token;
387 /* Ideally, the source position information would not be a global
388 variable, but it is. */
390 /* Update the line number. */
391 if (token->type != CPP_EOF)
393 lineno = token->line_number;
394 input_filename = token->file_name;
398 /* TOKEN points into the circular token buffer. Return a pointer to
399 the next token in the buffer. */
401 static inline cp_token *
402 cp_lexer_next_token (lexer, token)
407 if (token == lexer->buffer_end)
408 token = lexer->buffer;
412 /* Non-zero if we are presently saving tokens. */
415 cp_lexer_saving_tokens (lexer)
416 const cp_lexer *lexer;
418 return VARRAY_ACTIVE_SIZE (lexer->saved_tokens) != 0;
421 /* Return a pointer to the token that is N tokens beyond TOKEN in the
425 cp_lexer_advance_token (cp_lexer *lexer, cp_token *token, ptrdiff_t n)
428 if (token >= lexer->buffer_end)
429 token = lexer->buffer + (token - lexer->buffer_end);
433 /* Returns the number of times that START would have to be incremented
434 to reach FINISH. If START and FINISH are the same, returns zero. */
437 cp_lexer_token_difference (lexer, start, finish)
443 return finish - start;
445 return ((lexer->buffer_end - lexer->buffer)
449 /* Obtain another token from the C preprocessor and add it to the
450 token buffer. Returns the newly read token. */
453 cp_lexer_read_token (lexer)
458 /* Make sure there is room in the buffer. */
459 cp_lexer_maybe_grow_buffer (lexer);
461 /* If there weren't any tokens, then this one will be the first. */
462 if (!lexer->first_token)
463 lexer->first_token = lexer->last_token;
464 /* Similarly, if there were no available tokens, there is one now. */
465 if (!lexer->next_token)
466 lexer->next_token = lexer->last_token;
468 /* Figure out where we're going to store the new token. */
469 token = lexer->last_token;
471 /* Get a new token from the preprocessor. */
472 cp_lexer_get_preprocessor_token (lexer, token);
474 /* Increment LAST_TOKEN. */
475 lexer->last_token = cp_lexer_next_token (lexer, token);
477 /* The preprocessor does not yet do translation phase six, i.e., the
478 combination of adjacent string literals. Therefore, we do it
480 if (token->type == CPP_STRING || token->type == CPP_WSTRING)
485 /* When we grow the buffer, we may invalidate TOKEN. So, save
486 the distance from the beginning of the BUFFER so that we can
488 delta = cp_lexer_token_difference (lexer, lexer->buffer, token);
489 /* Make sure there is room in the buffer for another token. */
490 cp_lexer_maybe_grow_buffer (lexer);
492 token = lexer->buffer;
493 for (i = 0; i < delta; ++i)
494 token = cp_lexer_next_token (lexer, token);
496 VARRAY_PUSH_TREE (lexer->string_tokens, token->value);
499 /* Read the token after TOKEN. */
500 cp_lexer_get_preprocessor_token (lexer, lexer->last_token);
501 /* See whether it's another string constant. */
502 if (lexer->last_token->type != token->type)
504 /* If not, then it will be the next real token. */
505 lexer->last_token = cp_lexer_next_token (lexer,
510 /* Chain the strings together. */
511 VARRAY_PUSH_TREE (lexer->string_tokens,
512 lexer->last_token->value);
515 /* Create a single STRING_CST. Curiously we have to call
516 combine_strings even if there is only a single string in
517 order to get the type set correctly. */
518 token->value = combine_strings (lexer->string_tokens);
519 VARRAY_CLEAR (lexer->string_tokens);
520 token->value = fix_string_type (token->value);
521 /* Strings should have type `const char []'. Right now, we will
522 have an ARRAY_TYPE that is constant rather than an array of
523 constant elements. */
524 if (flag_const_strings)
528 /* Get the current type. It will be an ARRAY_TYPE. */
529 type = TREE_TYPE (token->value);
530 /* Use build_cplus_array_type to rebuild the array, thereby
531 getting the right type. */
532 type = build_cplus_array_type (TREE_TYPE (type),
534 /* Reset the type of the token. */
535 TREE_TYPE (token->value) = type;
542 /* If the circular buffer is full, make it bigger. */
545 cp_lexer_maybe_grow_buffer (lexer)
548 /* If the buffer is full, enlarge it. */
549 if (lexer->last_token == lexer->first_token)
551 cp_token *new_buffer;
552 cp_token *old_buffer;
553 cp_token *new_first_token;
554 ptrdiff_t buffer_length;
555 size_t num_tokens_to_copy;
557 /* Remember the current buffer pointer. It will become invalid,
558 but we will need to do pointer arithmetic involving this
560 old_buffer = lexer->buffer;
561 /* Compute the current buffer size. */
562 buffer_length = lexer->buffer_end - lexer->buffer;
563 /* Allocate a buffer twice as big. */
564 new_buffer = ((cp_token *)
565 ggc_realloc (lexer->buffer,
566 2 * buffer_length * sizeof (cp_token)));
568 /* Because the buffer is circular, logically consecutive tokens
569 are not necessarily placed consecutively in memory.
570 Therefore, we must keep move the tokens that were before
571 FIRST_TOKEN to the second half of the newly allocated
573 num_tokens_to_copy = (lexer->first_token - old_buffer);
574 memcpy (new_buffer + buffer_length,
576 num_tokens_to_copy * sizeof (cp_token));
577 /* Clear the rest of the buffer. We never look at this storage,
578 but the garbage collector may. */
579 memset (new_buffer + buffer_length + num_tokens_to_copy, 0,
580 (buffer_length - num_tokens_to_copy) * sizeof (cp_token));
582 /* Now recompute all of the buffer pointers. */
584 = new_buffer + (lexer->first_token - old_buffer);
585 if (lexer->next_token != NULL)
587 ptrdiff_t next_token_delta;
589 if (lexer->next_token > lexer->first_token)
590 next_token_delta = lexer->next_token - lexer->first_token;
593 buffer_length - (lexer->first_token - lexer->next_token);
594 lexer->next_token = new_first_token + next_token_delta;
596 lexer->last_token = new_first_token + buffer_length;
597 lexer->buffer = new_buffer;
598 lexer->buffer_end = new_buffer + buffer_length * 2;
599 lexer->first_token = new_first_token;
603 /* Store the next token from the preprocessor in *TOKEN. */
606 cp_lexer_get_preprocessor_token (lexer, token)
607 cp_lexer *lexer ATTRIBUTE_UNUSED;
612 /* If this not the main lexer, return a terminating CPP_EOF token. */
613 if (!lexer->main_lexer_p)
615 token->type = CPP_EOF;
616 token->line_number = 0;
617 token->file_name = NULL;
618 token->value = NULL_TREE;
619 token->keyword = RID_MAX;
625 /* Keep going until we get a token we like. */
628 /* Get a new token from the preprocessor. */
629 token->type = c_lex (&token->value);
630 /* Issue messages about tokens we cannot process. */
636 error ("invalid token");
640 /* These tokens are already warned about by c_lex. */
644 /* This is a good token, so we exit the loop. */
649 /* Now we've got our token. */
650 token->line_number = lineno;
651 token->file_name = input_filename;
653 /* Check to see if this token is a keyword. */
654 if (token->type == CPP_NAME
655 && C_IS_RESERVED_WORD (token->value))
657 /* Mark this token as a keyword. */
658 token->type = CPP_KEYWORD;
659 /* Record which keyword. */
660 token->keyword = C_RID_CODE (token->value);
661 /* Update the value. Some keywords are mapped to particular
662 entities, rather than simply having the value of the
663 corresponding IDENTIFIER_NODE. For example, `__const' is
664 mapped to `const'. */
665 token->value = ridpointers[token->keyword];
668 token->keyword = RID_MAX;
671 /* Return a pointer to the next token in the token stream, but do not
675 cp_lexer_peek_token (lexer)
680 /* If there are no tokens, read one now. */
681 if (!lexer->next_token)
682 cp_lexer_read_token (lexer);
684 /* Provide debugging output. */
685 if (cp_lexer_debugging_p (lexer))
687 fprintf (cp_lexer_debug_stream, "cp_lexer: peeking at token: ");
688 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
689 fprintf (cp_lexer_debug_stream, "\n");
692 token = lexer->next_token;
693 cp_lexer_set_source_position_from_token (lexer, token);
697 /* Return true if the next token has the indicated TYPE. */
700 cp_lexer_next_token_is (lexer, type)
706 /* Peek at the next token. */
707 token = cp_lexer_peek_token (lexer);
708 /* Check to see if it has the indicated TYPE. */
709 return token->type == type;
712 /* Return true if the next token does not have the indicated TYPE. */
715 cp_lexer_next_token_is_not (lexer, type)
719 return !cp_lexer_next_token_is (lexer, type);
722 /* Return true if the next token is the indicated KEYWORD. */
725 cp_lexer_next_token_is_keyword (lexer, keyword)
731 /* Peek at the next token. */
732 token = cp_lexer_peek_token (lexer);
733 /* Check to see if it is the indicated keyword. */
734 return token->keyword == keyword;
737 /* Return a pointer to the Nth token in the token stream. If N is 1,
738 then this is precisely equivalent to cp_lexer_peek_token. */
741 cp_lexer_peek_nth_token (lexer, n)
747 /* N is 1-based, not zero-based. */
748 my_friendly_assert (n > 0, 20000224);
750 /* Skip ahead from NEXT_TOKEN, reading more tokens as necessary. */
751 token = lexer->next_token;
752 /* If there are no tokens in the buffer, get one now. */
755 cp_lexer_read_token (lexer);
756 token = lexer->next_token;
759 /* Now, read tokens until we have enough. */
762 /* Advance to the next token. */
763 token = cp_lexer_next_token (lexer, token);
764 /* If that's all the tokens we have, read a new one. */
765 if (token == lexer->last_token)
766 token = cp_lexer_read_token (lexer);
772 /* Consume the next token. The pointer returned is valid only until
773 another token is read. Callers should preserve copy the token
774 explicitly if they will need its value for a longer period of
778 cp_lexer_consume_token (lexer)
783 /* If there are no tokens, read one now. */
784 if (!lexer->next_token)
785 cp_lexer_read_token (lexer);
787 /* Remember the token we'll be returning. */
788 token = lexer->next_token;
790 /* Increment NEXT_TOKEN. */
791 lexer->next_token = cp_lexer_next_token (lexer,
793 /* Check to see if we're all out of tokens. */
794 if (lexer->next_token == lexer->last_token)
795 lexer->next_token = NULL;
797 /* If we're not saving tokens, then move FIRST_TOKEN too. */
798 if (!cp_lexer_saving_tokens (lexer))
800 /* If there are no tokens available, set FIRST_TOKEN to NULL. */
801 if (!lexer->next_token)
802 lexer->first_token = NULL;
804 lexer->first_token = lexer->next_token;
807 /* Provide debugging output. */
808 if (cp_lexer_debugging_p (lexer))
810 fprintf (cp_lexer_debug_stream, "cp_lexer: consuming token: ");
811 cp_lexer_print_token (cp_lexer_debug_stream, token);
812 fprintf (cp_lexer_debug_stream, "\n");
818 /* Permanently remove the next token from the token stream. There
819 must be a valid next token already; this token never reads
820 additional tokens from the preprocessor. */
823 cp_lexer_purge_token (cp_lexer *lexer)
826 cp_token *next_token;
828 token = lexer->next_token;
831 next_token = cp_lexer_next_token (lexer, token);
832 if (next_token == lexer->last_token)
834 *token = *next_token;
838 lexer->last_token = token;
839 /* The token purged may have been the only token remaining; if so,
841 if (lexer->next_token == token)
842 lexer->next_token = NULL;
845 /* Permanently remove all tokens after TOKEN, up to, but not
846 including, the token that will be returned next by
847 cp_lexer_peek_token. */
850 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *token)
856 if (lexer->next_token)
858 /* Copy the tokens that have not yet been read to the location
859 immediately following TOKEN. */
860 t1 = cp_lexer_next_token (lexer, token);
861 t2 = peek = cp_lexer_peek_token (lexer);
862 /* Move tokens into the vacant area between TOKEN and PEEK. */
863 while (t2 != lexer->last_token)
866 t1 = cp_lexer_next_token (lexer, t1);
867 t2 = cp_lexer_next_token (lexer, t2);
869 /* Now, the next available token is right after TOKEN. */
870 lexer->next_token = cp_lexer_next_token (lexer, token);
871 /* And the last token is wherever we ended up. */
872 lexer->last_token = t1;
876 /* There are no tokens in the buffer, so there is nothing to
877 copy. The last token in the buffer is TOKEN itself. */
878 lexer->last_token = cp_lexer_next_token (lexer, token);
882 /* Begin saving tokens. All tokens consumed after this point will be
886 cp_lexer_save_tokens (lexer)
889 /* Provide debugging output. */
890 if (cp_lexer_debugging_p (lexer))
891 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
893 /* Make sure that LEXER->NEXT_TOKEN is non-NULL so that we can
894 restore the tokens if required. */
895 if (!lexer->next_token)
896 cp_lexer_read_token (lexer);
898 VARRAY_PUSH_INT (lexer->saved_tokens,
899 cp_lexer_token_difference (lexer,
904 /* Commit to the portion of the token stream most recently saved. */
907 cp_lexer_commit_tokens (lexer)
910 /* Provide debugging output. */
911 if (cp_lexer_debugging_p (lexer))
912 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
914 VARRAY_POP (lexer->saved_tokens);
917 /* Return all tokens saved since the last call to cp_lexer_save_tokens
918 to the token stream. Stop saving tokens. */
921 cp_lexer_rollback_tokens (lexer)
926 /* Provide debugging output. */
927 if (cp_lexer_debugging_p (lexer))
928 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
930 /* Find the token that was the NEXT_TOKEN when we started saving
932 delta = VARRAY_TOP_INT(lexer->saved_tokens);
933 /* Make it the next token again now. */
934 lexer->next_token = cp_lexer_advance_token (lexer,
937 /* It might be the case that there wer no tokens when we started
938 saving tokens, but that there are some tokens now. */
939 if (!lexer->next_token && lexer->first_token)
940 lexer->next_token = lexer->first_token;
942 /* Stop saving tokens. */
943 VARRAY_POP (lexer->saved_tokens);
946 /* Print a representation of the TOKEN on the STREAM. */
949 cp_lexer_print_token (stream, token)
953 const char *token_type = NULL;
955 /* Figure out what kind of token this is. */
963 token_type = "COMMA";
967 token_type = "OPEN_PAREN";
970 case CPP_CLOSE_PAREN:
971 token_type = "CLOSE_PAREN";
975 token_type = "OPEN_BRACE";
978 case CPP_CLOSE_BRACE:
979 token_type = "CLOSE_BRACE";
983 token_type = "SEMICOLON";
995 token_type = "keyword";
998 /* This is not a token that we know how to handle yet. */
1003 /* If we have a name for the token, print it out. Otherwise, we
1004 simply give the numeric code. */
1006 fprintf (stream, "%s", token_type);
1008 fprintf (stream, "%d", token->type);
1009 /* And, for an identifier, print the identifier name. */
1010 if (token->type == CPP_NAME
1011 /* Some keywords have a value that is not an IDENTIFIER_NODE.
1012 For example, `struct' is mapped to an INTEGER_CST. */
1013 || (token->type == CPP_KEYWORD
1014 && TREE_CODE (token->value) == IDENTIFIER_NODE))
1015 fprintf (stream, " %s", IDENTIFIER_POINTER (token->value));
1018 /* Start emitting debugging information. */
1021 cp_lexer_start_debugging (lexer)
1024 ++lexer->debugging_p;
1027 /* Stop emitting debugging information. */
1030 cp_lexer_stop_debugging (lexer)
1033 --lexer->debugging_p;
1042 A cp_parser parses the token stream as specified by the C++
1043 grammar. Its job is purely parsing, not semantic analysis. For
1044 example, the parser breaks the token stream into declarators,
1045 expressions, statements, and other similar syntactic constructs.
1046 It does not check that the types of the expressions on either side
1047 of an assignment-statement are compatible, or that a function is
1048 not declared with a parameter of type `void'.
1050 The parser invokes routines elsewhere in the compiler to perform
1051 semantic analysis and to build up the abstract syntax tree for the
1054 The parser (and the template instantiation code, which is, in a
1055 way, a close relative of parsing) are the only parts of the
1056 compiler that should be calling push_scope and pop_scope, or
1057 related functions. The parser (and template instantiation code)
1058 keeps track of what scope is presently active; everything else
1059 should simply honor that. (The code that generates static
1060 initializers may also need to set the scope, in order to check
1061 access control correctly when emitting the initializers.)
1066 The parser is of the standard recursive-descent variety. Upcoming
1067 tokens in the token stream are examined in order to determine which
1068 production to use when parsing a non-terminal. Some C++ constructs
1069 require arbitrary look ahead to disambiguate. For example, it is
1070 impossible, in the general case, to tell whether a statement is an
1071 expression or declaration without scanning the entire statement.
1072 Therefore, the parser is capable of "parsing tentatively." When the
1073 parser is not sure what construct comes next, it enters this mode.
1074 Then, while we attempt to parse the construct, the parser queues up
1075 error messages, rather than issuing them immediately, and saves the
1076 tokens it consumes. If the construct is parsed successfully, the
1077 parser "commits", i.e., it issues any queued error messages and
1078 the tokens that were being preserved are permanently discarded.
1079 If, however, the construct is not parsed successfully, the parser
1080 rolls back its state completely so that it can resume parsing using
1081 a different alternative.
1086 The performance of the parser could probably be improved
1087 substantially. Some possible improvements include:
1089 - The expression parser recurses through the various levels of
1090 precedence as specified in the grammar, rather than using an
1091 operator-precedence technique. Therefore, parsing a simple
1092 identifier requires multiple recursive calls.
1094 - We could often eliminate the need to parse tentatively by
1095 looking ahead a little bit. In some places, this approach
1096 might not entirely eliminate the need to parse tentatively, but
1097 it might still speed up the average case. */
1099 /* Flags that are passed to some parsing functions. These values can
1100 be bitwise-ored together. */
1102 typedef enum cp_parser_flags
1105 CP_PARSER_FLAGS_NONE = 0x0,
1106 /* The construct is optional. If it is not present, then no error
1107 should be issued. */
1108 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1109 /* When parsing a type-specifier, do not allow user-defined types. */
1110 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2
1113 /* The different kinds of ids that we ecounter. */
1115 typedef enum cp_parser_id_kind
1117 /* Not an id at all. */
1118 CP_PARSER_ID_KIND_NONE,
1119 /* An unqualified-id that is not a template-id. */
1120 CP_PARSER_ID_KIND_UNQUALIFIED,
1121 /* An unqualified template-id. */
1122 CP_PARSER_ID_KIND_TEMPLATE_ID,
1123 /* A qualified-id. */
1124 CP_PARSER_ID_KIND_QUALIFIED
1125 } cp_parser_id_kind;
1127 /* A mapping from a token type to a corresponding tree node type. */
1129 typedef struct cp_parser_token_tree_map_node
1131 /* The token type. */
1132 enum cpp_ttype token_type;
1133 /* The corresponding tree code. */
1134 enum tree_code tree_type;
1135 } cp_parser_token_tree_map_node;
1137 /* A complete map consists of several ordinary entries, followed by a
1138 terminator. The terminating entry has a token_type of CPP_EOF. */
1140 typedef cp_parser_token_tree_map_node cp_parser_token_tree_map[];
1142 /* The status of a tentative parse. */
1144 typedef enum cp_parser_status_kind
1146 /* No errors have occurred. */
1147 CP_PARSER_STATUS_KIND_NO_ERROR,
1148 /* An error has occurred. */
1149 CP_PARSER_STATUS_KIND_ERROR,
1150 /* We are committed to this tentative parse, whether or not an error
1152 CP_PARSER_STATUS_KIND_COMMITTED
1153 } cp_parser_status_kind;
1155 /* Context that is saved and restored when parsing tentatively. */
1157 typedef struct cp_parser_context GTY (())
1159 /* If this is a tentative parsing context, the status of the
1161 enum cp_parser_status_kind status;
1162 /* If non-NULL, we have just seen a `x->' or `x.' expression. Names
1163 that are looked up in this context must be looked up both in the
1164 scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1165 the context of the containing expression. */
1167 /* A TREE_LIST representing name-lookups for which we have deferred
1168 checking access controls. We cannot check the accessibility of
1169 names used in a decl-specifier-seq until we know what is being
1170 declared because code like:
1177 A::B* A::f() { return 0; }
1179 is valid, even though `A::B' is not generally accessible.
1181 The TREE_PURPOSE of each node is the scope used to qualify the
1182 name being looked up; the TREE_VALUE is the DECL to which the
1183 name was resolved. */
1184 tree deferred_access_checks;
1185 /* TRUE iff we are deferring access checks. */
1186 bool deferring_access_checks_p;
1187 /* The next parsing context in the stack. */
1188 struct cp_parser_context *next;
1189 } cp_parser_context;
1193 /* Constructors and destructors. */
1195 static cp_parser_context *cp_parser_context_new
1196 PARAMS ((cp_parser_context *));
1198 /* Class variables. */
1200 static GTY((deletable (""))) cp_parser_context* cp_parser_context_free_list;
1202 /* Constructors and destructors. */
1204 /* Construct a new context. The context below this one on the stack
1205 is given by NEXT. */
1207 static cp_parser_context *
1208 cp_parser_context_new (next)
1209 cp_parser_context *next;
1211 cp_parser_context *context;
1213 /* Allocate the storage. */
1214 if (cp_parser_context_free_list != NULL)
1216 /* Pull the first entry from the free list. */
1217 context = cp_parser_context_free_list;
1218 cp_parser_context_free_list = context->next;
1219 memset ((char *)context, 0, sizeof (*context));
1222 context = ((cp_parser_context *)
1223 ggc_alloc_cleared (sizeof (cp_parser_context)));
1224 /* No errors have occurred yet in this context. */
1225 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1226 /* If this is not the bottomost context, copy information that we
1227 need from the previous context. */
1230 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1231 expression, then we are parsing one in this context, too. */
1232 context->object_type = next->object_type;
1233 /* We are deferring access checks here if we were in the NEXT
1235 context->deferring_access_checks_p
1236 = next->deferring_access_checks_p;
1237 /* Thread the stack. */
1238 context->next = next;
1244 /* The cp_parser structure represents the C++ parser. */
1246 typedef struct cp_parser GTY(())
1248 /* The lexer from which we are obtaining tokens. */
1251 /* The scope in which names should be looked up. If NULL_TREE, then
1252 we look up names in the scope that is currently open in the
1253 source program. If non-NULL, this is either a TYPE or
1254 NAMESPACE_DECL for the scope in which we should look.
1256 This value is not cleared automatically after a name is looked
1257 up, so we must be careful to clear it before starting a new look
1258 up sequence. (If it is not cleared, then `X::Y' followed by `Z'
1259 will look up `Z' in the scope of `X', rather than the current
1260 scope.) Unfortunately, it is difficult to tell when name lookup
1261 is complete, because we sometimes peek at a token, look it up,
1262 and then decide not to consume it. */
1265 /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1266 last lookup took place. OBJECT_SCOPE is used if an expression
1267 like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1268 respectively. QUALIFYING_SCOPE is used for an expression of the
1269 form "X::Y"; it refers to X. */
1271 tree qualifying_scope;
1273 /* A stack of parsing contexts. All but the bottom entry on the
1274 stack will be tentative contexts.
1276 We parse tentatively in order to determine which construct is in
1277 use in some situations. For example, in order to determine
1278 whether a statement is an expression-statement or a
1279 declaration-statement we parse it tentatively as a
1280 declaration-statement. If that fails, we then reparse the same
1281 token stream as an expression-statement. */
1282 cp_parser_context *context;
1284 /* True if we are parsing GNU C++. If this flag is not set, then
1285 GNU extensions are not recognized. */
1286 bool allow_gnu_extensions_p;
1288 /* TRUE if the `>' token should be interpreted as the greater-than
1289 operator. FALSE if it is the end of a template-id or
1290 template-parameter-list. */
1291 bool greater_than_is_operator_p;
1293 /* TRUE if default arguments are allowed within a parameter list
1294 that starts at this point. FALSE if only a gnu extension makes
1295 them permissable. */
1296 bool default_arg_ok_p;
1298 /* TRUE if we are parsing an integral constant-expression. See
1299 [expr.const] for a precise definition. */
1300 /* FIXME: Need to implement code that checks this flag. */
1301 bool constant_expression_p;
1303 /* TRUE if local variable names and `this' are forbidden in the
1305 bool local_variables_forbidden_p;
1307 /* TRUE if the declaration we are parsing is part of a
1308 linkage-specification of the form `extern string-literal
1310 bool in_unbraced_linkage_specification_p;
1312 /* TRUE if we are presently parsing a declarator, after the
1313 direct-declarator. */
1314 bool in_declarator_p;
1316 /* If non-NULL, then we are parsing a construct where new type
1317 definitions are not permitted. The string stored here will be
1318 issued as an error message if a type is defined. */
1319 const char *type_definition_forbidden_message;
1321 /* List of FUNCTION_TYPEs which contain unprocessed DEFAULT_ARGs
1322 during class parsing, and are not FUNCTION_DECLs. G++ has an
1323 awkward extension allowing default args on pointers to functions
1325 tree default_arg_types;
1327 /* A TREE_LIST of queues of functions whose bodies have been lexed,
1328 but may not have been parsed. These functions are friends of
1329 members defined within a class-specification; they are not
1330 procssed until the class is complete. The active queue is at the
1333 Within each queue, functions appear in the reverse order that
1334 they appeared in the source. The TREE_PURPOSE of each node is
1335 the class in which the function was defined or declared; the
1336 TREE_VALUE is the FUNCTION_DECL itself. */
1337 tree unparsed_functions_queues;
1339 /* The number of classes whose definitions are currently in
1341 unsigned num_classes_being_defined;
1343 /* The number of template parameter lists that apply directly to the
1344 current declaration. */
1345 unsigned num_template_parameter_lists;
1347 /* List of access checks lists, used to prevent GC collection while
1349 tree access_checks_lists;
1352 /* The type of a function that parses some kind of expression */
1353 typedef tree (*cp_parser_expression_fn) PARAMS ((cp_parser *));
1357 /* Constructors and destructors. */
1359 static cp_parser *cp_parser_new
1362 /* Routines to parse various constructs.
1364 Those that return `tree' will return the error_mark_node (rather
1365 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1366 Sometimes, they will return an ordinary node if error-recovery was
1367 attempted, even though a parse error occurrred. So, to check
1368 whether or not a parse error occurred, you should always use
1369 cp_parser_error_occurred. If the construct is optional (indicated
1370 either by an `_opt' in the name of the function that does the
1371 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1372 the construct is not present. */
1374 /* Lexical conventions [gram.lex] */
1376 static tree cp_parser_identifier
1377 PARAMS ((cp_parser *));
1379 /* Basic concepts [gram.basic] */
1381 static bool cp_parser_translation_unit
1382 PARAMS ((cp_parser *));
1384 /* Expressions [gram.expr] */
1386 static tree cp_parser_primary_expression
1387 (cp_parser *, cp_parser_id_kind *, tree *);
1388 static tree cp_parser_id_expression
1389 PARAMS ((cp_parser *, bool, bool, bool *));
1390 static tree cp_parser_unqualified_id
1391 PARAMS ((cp_parser *, bool, bool));
1392 static tree cp_parser_nested_name_specifier_opt
1393 (cp_parser *, bool, bool, bool);
1394 static tree cp_parser_nested_name_specifier
1395 (cp_parser *, bool, bool, bool);
1396 static tree cp_parser_class_or_namespace_name
1397 (cp_parser *, bool, bool, bool, bool);
1398 static tree cp_parser_postfix_expression
1399 (cp_parser *, bool);
1400 static tree cp_parser_expression_list
1401 PARAMS ((cp_parser *));
1402 static void cp_parser_pseudo_destructor_name
1403 PARAMS ((cp_parser *, tree *, tree *));
1404 static tree cp_parser_unary_expression
1405 (cp_parser *, bool);
1406 static enum tree_code cp_parser_unary_operator
1407 PARAMS ((cp_token *));
1408 static tree cp_parser_new_expression
1409 PARAMS ((cp_parser *));
1410 static tree cp_parser_new_placement
1411 PARAMS ((cp_parser *));
1412 static tree cp_parser_new_type_id
1413 PARAMS ((cp_parser *));
1414 static tree cp_parser_new_declarator_opt
1415 PARAMS ((cp_parser *));
1416 static tree cp_parser_direct_new_declarator
1417 PARAMS ((cp_parser *));
1418 static tree cp_parser_new_initializer
1419 PARAMS ((cp_parser *));
1420 static tree cp_parser_delete_expression
1421 PARAMS ((cp_parser *));
1422 static tree cp_parser_cast_expression
1423 (cp_parser *, bool);
1424 static tree cp_parser_pm_expression
1425 PARAMS ((cp_parser *));
1426 static tree cp_parser_multiplicative_expression
1427 PARAMS ((cp_parser *));
1428 static tree cp_parser_additive_expression
1429 PARAMS ((cp_parser *));
1430 static tree cp_parser_shift_expression
1431 PARAMS ((cp_parser *));
1432 static tree cp_parser_relational_expression
1433 PARAMS ((cp_parser *));
1434 static tree cp_parser_equality_expression
1435 PARAMS ((cp_parser *));
1436 static tree cp_parser_and_expression
1437 PARAMS ((cp_parser *));
1438 static tree cp_parser_exclusive_or_expression
1439 PARAMS ((cp_parser *));
1440 static tree cp_parser_inclusive_or_expression
1441 PARAMS ((cp_parser *));
1442 static tree cp_parser_logical_and_expression
1443 PARAMS ((cp_parser *));
1444 static tree cp_parser_logical_or_expression
1445 PARAMS ((cp_parser *));
1446 static tree cp_parser_conditional_expression
1447 PARAMS ((cp_parser *));
1448 static tree cp_parser_question_colon_clause
1449 PARAMS ((cp_parser *, tree));
1450 static tree cp_parser_assignment_expression
1451 PARAMS ((cp_parser *));
1452 static enum tree_code cp_parser_assignment_operator_opt
1453 PARAMS ((cp_parser *));
1454 static tree cp_parser_expression
1455 PARAMS ((cp_parser *));
1456 static tree cp_parser_constant_expression
1457 PARAMS ((cp_parser *));
1459 /* Statements [gram.stmt.stmt] */
1461 static void cp_parser_statement
1462 PARAMS ((cp_parser *));
1463 static tree cp_parser_labeled_statement
1464 PARAMS ((cp_parser *));
1465 static tree cp_parser_expression_statement
1466 PARAMS ((cp_parser *));
1467 static tree cp_parser_compound_statement
1469 static void cp_parser_statement_seq_opt
1470 PARAMS ((cp_parser *));
1471 static tree cp_parser_selection_statement
1472 PARAMS ((cp_parser *));
1473 static tree cp_parser_condition
1474 PARAMS ((cp_parser *));
1475 static tree cp_parser_iteration_statement
1476 PARAMS ((cp_parser *));
1477 static void cp_parser_for_init_statement
1478 PARAMS ((cp_parser *));
1479 static tree cp_parser_jump_statement
1480 PARAMS ((cp_parser *));
1481 static void cp_parser_declaration_statement
1482 PARAMS ((cp_parser *));
1484 static tree cp_parser_implicitly_scoped_statement
1485 PARAMS ((cp_parser *));
1486 static void cp_parser_already_scoped_statement
1487 PARAMS ((cp_parser *));
1489 /* Declarations [gram.dcl.dcl] */
1491 static void cp_parser_declaration_seq_opt
1492 PARAMS ((cp_parser *));
1493 static void cp_parser_declaration
1494 PARAMS ((cp_parser *));
1495 static void cp_parser_block_declaration
1496 PARAMS ((cp_parser *, bool));
1497 static void cp_parser_simple_declaration
1498 PARAMS ((cp_parser *, bool));
1499 static tree cp_parser_decl_specifier_seq
1500 PARAMS ((cp_parser *, cp_parser_flags, tree *, bool *));
1501 static tree cp_parser_storage_class_specifier_opt
1502 PARAMS ((cp_parser *));
1503 static tree cp_parser_function_specifier_opt
1504 PARAMS ((cp_parser *));
1505 static tree cp_parser_type_specifier
1506 (cp_parser *, cp_parser_flags, bool, bool, bool *, bool *);
1507 static tree cp_parser_simple_type_specifier
1508 PARAMS ((cp_parser *, cp_parser_flags));
1509 static tree cp_parser_type_name
1510 PARAMS ((cp_parser *));
1511 static tree cp_parser_elaborated_type_specifier
1512 PARAMS ((cp_parser *, bool, bool));
1513 static tree cp_parser_enum_specifier
1514 PARAMS ((cp_parser *));
1515 static void cp_parser_enumerator_list
1516 PARAMS ((cp_parser *, tree));
1517 static void cp_parser_enumerator_definition
1518 PARAMS ((cp_parser *, tree));
1519 static tree cp_parser_namespace_name
1520 PARAMS ((cp_parser *));
1521 static void cp_parser_namespace_definition
1522 PARAMS ((cp_parser *));
1523 static void cp_parser_namespace_body
1524 PARAMS ((cp_parser *));
1525 static tree cp_parser_qualified_namespace_specifier
1526 PARAMS ((cp_parser *));
1527 static void cp_parser_namespace_alias_definition
1528 PARAMS ((cp_parser *));
1529 static void cp_parser_using_declaration
1530 PARAMS ((cp_parser *));
1531 static void cp_parser_using_directive
1532 PARAMS ((cp_parser *));
1533 static void cp_parser_asm_definition
1534 PARAMS ((cp_parser *));
1535 static void cp_parser_linkage_specification
1536 PARAMS ((cp_parser *));
1538 /* Declarators [gram.dcl.decl] */
1540 static tree cp_parser_init_declarator
1541 PARAMS ((cp_parser *, tree, tree, tree, bool, bool, bool *));
1542 static tree cp_parser_declarator
1543 PARAMS ((cp_parser *, bool, bool *));
1544 static tree cp_parser_direct_declarator
1545 PARAMS ((cp_parser *, bool, bool *));
1546 static enum tree_code cp_parser_ptr_operator
1547 PARAMS ((cp_parser *, tree *, tree *));
1548 static tree cp_parser_cv_qualifier_seq_opt
1549 PARAMS ((cp_parser *));
1550 static tree cp_parser_cv_qualifier_opt
1551 PARAMS ((cp_parser *));
1552 static tree cp_parser_declarator_id
1553 PARAMS ((cp_parser *));
1554 static tree cp_parser_type_id
1555 PARAMS ((cp_parser *));
1556 static tree cp_parser_type_specifier_seq
1557 PARAMS ((cp_parser *));
1558 static tree cp_parser_parameter_declaration_clause
1559 PARAMS ((cp_parser *));
1560 static tree cp_parser_parameter_declaration_list
1561 PARAMS ((cp_parser *));
1562 static tree cp_parser_parameter_declaration
1563 PARAMS ((cp_parser *, bool));
1564 static tree cp_parser_function_definition
1565 PARAMS ((cp_parser *, bool *));
1566 static void cp_parser_function_body
1568 static tree cp_parser_initializer
1569 PARAMS ((cp_parser *, bool *));
1570 static tree cp_parser_initializer_clause
1571 PARAMS ((cp_parser *));
1572 static tree cp_parser_initializer_list
1573 PARAMS ((cp_parser *));
1575 static bool cp_parser_ctor_initializer_opt_and_function_body
1578 /* Classes [gram.class] */
1580 static tree cp_parser_class_name
1581 (cp_parser *, bool, bool, bool, bool, bool, bool);
1582 static tree cp_parser_class_specifier
1583 PARAMS ((cp_parser *));
1584 static tree cp_parser_class_head
1585 PARAMS ((cp_parser *, bool *, bool *, tree *));
1586 static enum tag_types cp_parser_class_key
1587 PARAMS ((cp_parser *));
1588 static void cp_parser_member_specification_opt
1589 PARAMS ((cp_parser *));
1590 static void cp_parser_member_declaration
1591 PARAMS ((cp_parser *));
1592 static tree cp_parser_pure_specifier
1593 PARAMS ((cp_parser *));
1594 static tree cp_parser_constant_initializer
1595 PARAMS ((cp_parser *));
1597 /* Derived classes [gram.class.derived] */
1599 static tree cp_parser_base_clause
1600 PARAMS ((cp_parser *));
1601 static tree cp_parser_base_specifier
1602 PARAMS ((cp_parser *));
1604 /* Special member functions [gram.special] */
1606 static tree cp_parser_conversion_function_id
1607 PARAMS ((cp_parser *));
1608 static tree cp_parser_conversion_type_id
1609 PARAMS ((cp_parser *));
1610 static tree cp_parser_conversion_declarator_opt
1611 PARAMS ((cp_parser *));
1612 static bool cp_parser_ctor_initializer_opt
1613 PARAMS ((cp_parser *));
1614 static void cp_parser_mem_initializer_list
1615 PARAMS ((cp_parser *));
1616 static tree cp_parser_mem_initializer
1617 PARAMS ((cp_parser *));
1618 static tree cp_parser_mem_initializer_id
1619 PARAMS ((cp_parser *));
1621 /* Overloading [gram.over] */
1623 static tree cp_parser_operator_function_id
1624 PARAMS ((cp_parser *));
1625 static tree cp_parser_operator
1626 PARAMS ((cp_parser *));
1628 /* Templates [gram.temp] */
1630 static void cp_parser_template_declaration
1631 PARAMS ((cp_parser *, bool));
1632 static tree cp_parser_template_parameter_list
1633 PARAMS ((cp_parser *));
1634 static tree cp_parser_template_parameter
1635 PARAMS ((cp_parser *));
1636 static tree cp_parser_type_parameter
1637 PARAMS ((cp_parser *));
1638 static tree cp_parser_template_id
1639 PARAMS ((cp_parser *, bool, bool));
1640 static tree cp_parser_template_name
1641 PARAMS ((cp_parser *, bool, bool));
1642 static tree cp_parser_template_argument_list
1643 PARAMS ((cp_parser *));
1644 static tree cp_parser_template_argument
1645 PARAMS ((cp_parser *));
1646 static void cp_parser_explicit_instantiation
1647 PARAMS ((cp_parser *));
1648 static void cp_parser_explicit_specialization
1649 PARAMS ((cp_parser *));
1651 /* Exception handling [gram.exception] */
1653 static tree cp_parser_try_block
1654 PARAMS ((cp_parser *));
1655 static bool cp_parser_function_try_block
1656 PARAMS ((cp_parser *));
1657 static void cp_parser_handler_seq
1658 PARAMS ((cp_parser *));
1659 static void cp_parser_handler
1660 PARAMS ((cp_parser *));
1661 static tree cp_parser_exception_declaration
1662 PARAMS ((cp_parser *));
1663 static tree cp_parser_throw_expression
1664 PARAMS ((cp_parser *));
1665 static tree cp_parser_exception_specification_opt
1666 PARAMS ((cp_parser *));
1667 static tree cp_parser_type_id_list
1668 PARAMS ((cp_parser *));
1670 /* GNU Extensions */
1672 static tree cp_parser_asm_specification_opt
1673 PARAMS ((cp_parser *));
1674 static tree cp_parser_asm_operand_list
1675 PARAMS ((cp_parser *));
1676 static tree cp_parser_asm_clobber_list
1677 PARAMS ((cp_parser *));
1678 static tree cp_parser_attributes_opt
1679 PARAMS ((cp_parser *));
1680 static tree cp_parser_attribute_list
1681 PARAMS ((cp_parser *));
1682 static bool cp_parser_extension_opt
1683 PARAMS ((cp_parser *, int *));
1684 static void cp_parser_label_declaration
1685 PARAMS ((cp_parser *));
1687 /* Utility Routines */
1689 static tree cp_parser_lookup_name
1690 PARAMS ((cp_parser *, tree, bool, bool, bool, bool));
1691 static tree cp_parser_lookup_name_simple
1692 PARAMS ((cp_parser *, tree));
1693 static tree cp_parser_resolve_typename_type
1694 PARAMS ((cp_parser *, tree));
1695 static tree cp_parser_maybe_treat_template_as_class
1697 static bool cp_parser_check_declarator_template_parameters
1698 PARAMS ((cp_parser *, tree));
1699 static bool cp_parser_check_template_parameters
1700 PARAMS ((cp_parser *, unsigned));
1701 static tree cp_parser_binary_expression
1702 PARAMS ((cp_parser *,
1703 cp_parser_token_tree_map,
1704 cp_parser_expression_fn));
1705 static tree cp_parser_global_scope_opt
1706 PARAMS ((cp_parser *, bool));
1707 static bool cp_parser_constructor_declarator_p
1708 (cp_parser *, bool);
1709 static tree cp_parser_function_definition_from_specifiers_and_declarator
1710 PARAMS ((cp_parser *, tree, tree, tree, tree));
1711 static tree cp_parser_function_definition_after_declarator
1712 PARAMS ((cp_parser *, bool));
1713 static void cp_parser_template_declaration_after_export
1714 PARAMS ((cp_parser *, bool));
1715 static tree cp_parser_single_declaration
1716 PARAMS ((cp_parser *, bool, bool *));
1717 static tree cp_parser_functional_cast
1718 PARAMS ((cp_parser *, tree));
1719 static void cp_parser_late_parsing_for_member
1720 PARAMS ((cp_parser *, tree));
1721 static void cp_parser_late_parsing_default_args
1722 (cp_parser *, tree, tree);
1723 static tree cp_parser_sizeof_operand
1724 PARAMS ((cp_parser *, enum rid));
1725 static bool cp_parser_declares_only_class_p
1726 PARAMS ((cp_parser *));
1727 static bool cp_parser_friend_p
1729 static cp_token *cp_parser_require
1730 PARAMS ((cp_parser *, enum cpp_ttype, const char *));
1731 static cp_token *cp_parser_require_keyword
1732 PARAMS ((cp_parser *, enum rid, const char *));
1733 static bool cp_parser_token_starts_function_definition_p
1734 PARAMS ((cp_token *));
1735 static bool cp_parser_next_token_starts_class_definition_p
1737 static enum tag_types cp_parser_token_is_class_key
1738 PARAMS ((cp_token *));
1739 static void cp_parser_check_class_key
1740 (enum tag_types, tree type);
1741 static bool cp_parser_optional_template_keyword
1743 static void cp_parser_cache_group
1744 (cp_parser *, cp_token_cache *, enum cpp_ttype, unsigned);
1745 static void cp_parser_parse_tentatively
1746 PARAMS ((cp_parser *));
1747 static void cp_parser_commit_to_tentative_parse
1748 PARAMS ((cp_parser *));
1749 static void cp_parser_abort_tentative_parse
1750 PARAMS ((cp_parser *));
1751 static bool cp_parser_parse_definitely
1752 PARAMS ((cp_parser *));
1753 static inline bool cp_parser_parsing_tentatively
1754 PARAMS ((cp_parser *));
1755 static bool cp_parser_committed_to_tentative_parse
1756 PARAMS ((cp_parser *));
1757 static void cp_parser_error
1758 PARAMS ((cp_parser *, const char *));
1759 static bool cp_parser_simulate_error
1760 PARAMS ((cp_parser *));
1761 static void cp_parser_check_type_definition
1762 PARAMS ((cp_parser *));
1763 static bool cp_parser_skip_to_closing_parenthesis
1764 PARAMS ((cp_parser *));
1765 static bool cp_parser_skip_to_closing_parenthesis_or_comma
1767 static void cp_parser_skip_to_end_of_statement
1768 PARAMS ((cp_parser *));
1769 static void cp_parser_skip_to_end_of_block_or_statement
1770 PARAMS ((cp_parser *));
1771 static void cp_parser_skip_to_closing_brace
1773 static void cp_parser_skip_until_found
1774 PARAMS ((cp_parser *, enum cpp_ttype, const char *));
1775 static bool cp_parser_error_occurred
1776 PARAMS ((cp_parser *));
1777 static bool cp_parser_allow_gnu_extensions_p
1778 PARAMS ((cp_parser *));
1779 static bool cp_parser_is_string_literal
1780 PARAMS ((cp_token *));
1781 static bool cp_parser_is_keyword
1782 PARAMS ((cp_token *, enum rid));
1783 static bool cp_parser_dependent_type_p
1785 static bool cp_parser_value_dependent_expression_p
1787 static bool cp_parser_type_dependent_expression_p
1789 static bool cp_parser_dependent_template_arg_p
1791 static bool cp_parser_dependent_template_id_p
1793 static bool cp_parser_dependent_template_p
1795 static void cp_parser_defer_access_check
1796 (cp_parser *, tree, tree);
1797 static void cp_parser_start_deferring_access_checks
1799 static tree cp_parser_stop_deferring_access_checks
1800 PARAMS ((cp_parser *));
1801 static void cp_parser_perform_deferred_access_checks
1803 static tree cp_parser_scope_through_which_access_occurs
1806 /* Returns non-zero if we are parsing tentatively. */
1809 cp_parser_parsing_tentatively (parser)
1812 return parser->context->next != NULL;
1815 /* Returns non-zero if TOKEN is a string literal. */
1818 cp_parser_is_string_literal (token)
1821 return (token->type == CPP_STRING || token->type == CPP_WSTRING);
1824 /* Returns non-zero if TOKEN is the indicated KEYWORD. */
1827 cp_parser_is_keyword (token, keyword)
1831 return token->keyword == keyword;
1834 /* Returns TRUE if TYPE is dependent, in the sense of
1838 cp_parser_dependent_type_p (type)
1843 if (!processing_template_decl)
1846 /* If the type is NULL, we have not computed a type for the entity
1847 in question; in that case, the type is dependent. */
1851 /* Erroneous types can be considered non-dependent. */
1852 if (type == error_mark_node)
1857 A type is dependent if it is:
1859 -- a template parameter. */
1860 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
1862 /* -- a qualified-id with a nested-name-specifier which contains a
1863 class-name that names a dependent type or whose unqualified-id
1864 names a dependent type. */
1865 if (TREE_CODE (type) == TYPENAME_TYPE)
1867 /* -- a cv-qualified type where the cv-unqualified type is
1869 type = TYPE_MAIN_VARIANT (type);
1870 /* -- a compound type constructed from any dependent type. */
1871 if (TYPE_PTRMEM_P (type) || TYPE_PTRMEMFUNC_P (type))
1872 return (cp_parser_dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type))
1873 || cp_parser_dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
1875 else if (TREE_CODE (type) == POINTER_TYPE
1876 || TREE_CODE (type) == REFERENCE_TYPE)
1877 return cp_parser_dependent_type_p (TREE_TYPE (type));
1878 else if (TREE_CODE (type) == FUNCTION_TYPE
1879 || TREE_CODE (type) == METHOD_TYPE)
1883 if (cp_parser_dependent_type_p (TREE_TYPE (type)))
1885 for (arg_type = TYPE_ARG_TYPES (type);
1887 arg_type = TREE_CHAIN (arg_type))
1888 if (cp_parser_dependent_type_p (TREE_VALUE (arg_type)))
1892 /* -- an array type constructed from any dependent type or whose
1893 size is specified by a constant expression that is
1895 if (TREE_CODE (type) == ARRAY_TYPE)
1897 if (TYPE_DOMAIN (type)
1898 && ((cp_parser_value_dependent_expression_p
1899 (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
1900 || (cp_parser_type_dependent_expression_p
1901 (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))))
1903 return cp_parser_dependent_type_p (TREE_TYPE (type));
1905 /* -- a template-id in which either the template name is a template
1906 parameter or any of the template arguments is a dependent type or
1907 an expression that is type-dependent or value-dependent.
1909 This language seems somewhat confused; for example, it does not
1910 discuss template template arguments. Therefore, we use the
1911 definition for dependent template arguments in [temp.dep.temp]. */
1912 if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INFO (type)
1913 && (cp_parser_dependent_template_id_p
1914 (CLASSTYPE_TI_TEMPLATE (type),
1915 CLASSTYPE_TI_ARGS (type))))
1917 else if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
1919 /* All TYPEOF_TYPEs are dependent; if the argument of the `typeof'
1920 expression is not type-dependent, then it should already been
1922 if (TREE_CODE (type) == TYPEOF_TYPE)
1924 /* The standard does not specifically mention types that are local
1925 to template functions or local classes, but they should be
1926 considered dependent too. For example:
1928 template <int I> void f() {
1933 The size of `E' cannot be known until the value of `I' has been
1934 determined. Therefore, `E' must be considered dependent. */
1935 scope = TYPE_CONTEXT (type);
1936 if (scope && TYPE_P (scope))
1937 return cp_parser_dependent_type_p (scope);
1938 else if (scope && TREE_CODE (scope) == FUNCTION_DECL)
1939 return cp_parser_type_dependent_expression_p (scope);
1941 /* Other types are non-dependent. */
1945 /* Returns TRUE if the EXPRESSION is value-dependent. */
1948 cp_parser_value_dependent_expression_p (tree expression)
1950 if (!processing_template_decl)
1953 /* A name declared with a dependent type. */
1954 if (DECL_P (expression)
1955 && cp_parser_dependent_type_p (TREE_TYPE (expression)))
1957 /* A non-type template parameter. */
1958 if ((TREE_CODE (expression) == CONST_DECL
1959 && DECL_TEMPLATE_PARM_P (expression))
1960 || TREE_CODE (expression) == TEMPLATE_PARM_INDEX)
1962 /* A constant with integral or enumeration type and is initialized
1963 with an expression that is value-dependent. */
1964 if (TREE_CODE (expression) == VAR_DECL
1965 && DECL_INITIAL (expression)
1966 && (CP_INTEGRAL_TYPE_P (TREE_TYPE (expression))
1967 || TREE_CODE (TREE_TYPE (expression)) == ENUMERAL_TYPE)
1968 && cp_parser_value_dependent_expression_p (DECL_INITIAL (expression)))
1970 /* These expressions are value-dependent if the type to which the
1971 cast occurs is dependent. */
1972 if ((TREE_CODE (expression) == DYNAMIC_CAST_EXPR
1973 || TREE_CODE (expression) == STATIC_CAST_EXPR
1974 || TREE_CODE (expression) == CONST_CAST_EXPR
1975 || TREE_CODE (expression) == REINTERPRET_CAST_EXPR
1976 || TREE_CODE (expression) == CAST_EXPR)
1977 && cp_parser_dependent_type_p (TREE_TYPE (expression)))
1979 /* A `sizeof' expression where the sizeof operand is a type is
1980 value-dependent if the type is dependent. If the type was not
1981 dependent, we would no longer have a SIZEOF_EXPR, so any
1982 SIZEOF_EXPR is dependent. */
1983 if (TREE_CODE (expression) == SIZEOF_EXPR)
1985 /* A constant expression is value-dependent if any subexpression is
1987 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (expression))))
1989 switch (TREE_CODE_CLASS (TREE_CODE (expression)))
1992 return (cp_parser_value_dependent_expression_p
1993 (TREE_OPERAND (expression, 0)));
1996 return ((cp_parser_value_dependent_expression_p
1997 (TREE_OPERAND (expression, 0)))
1998 || (cp_parser_value_dependent_expression_p
1999 (TREE_OPERAND (expression, 1))));
2004 i < TREE_CODE_LENGTH (TREE_CODE (expression));
2006 if (cp_parser_value_dependent_expression_p
2007 (TREE_OPERAND (expression, i)))
2014 /* The expression is not value-dependent. */
2018 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
2022 cp_parser_type_dependent_expression_p (expression)
2025 if (!processing_template_decl)
2028 /* Some expression forms are never type-dependent. */
2029 if (TREE_CODE (expression) == PSEUDO_DTOR_EXPR
2030 || TREE_CODE (expression) == SIZEOF_EXPR
2031 || TREE_CODE (expression) == ALIGNOF_EXPR
2032 || TREE_CODE (expression) == TYPEID_EXPR
2033 || TREE_CODE (expression) == DELETE_EXPR
2034 || TREE_CODE (expression) == VEC_DELETE_EXPR
2035 || TREE_CODE (expression) == THROW_EXPR)
2038 /* The types of these expressions depends only on the type to which
2040 if (TREE_CODE (expression) == DYNAMIC_CAST_EXPR
2041 || TREE_CODE (expression) == STATIC_CAST_EXPR
2042 || TREE_CODE (expression) == CONST_CAST_EXPR
2043 || TREE_CODE (expression) == REINTERPRET_CAST_EXPR
2044 || TREE_CODE (expression) == CAST_EXPR)
2045 return cp_parser_dependent_type_p (TREE_TYPE (expression));
2046 /* The types of these expressions depends only on the type created
2047 by the expression. */
2048 else if (TREE_CODE (expression) == NEW_EXPR
2049 || TREE_CODE (expression) == VEC_NEW_EXPR)
2050 return cp_parser_dependent_type_p (TREE_OPERAND (expression, 1));
2052 if (TREE_CODE (expression) == FUNCTION_DECL
2053 && DECL_LANG_SPECIFIC (expression)
2054 && DECL_TEMPLATE_INFO (expression)
2055 && (cp_parser_dependent_template_id_p
2056 (DECL_TI_TEMPLATE (expression),
2057 INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression)))))
2060 return (cp_parser_dependent_type_p (TREE_TYPE (expression)));
2063 /* Returns TRUE if the ARG (a template argument) is dependent. */
2066 cp_parser_dependent_template_arg_p (tree arg)
2068 if (!processing_template_decl)
2071 if (TREE_CODE (arg) == TEMPLATE_DECL
2072 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
2073 return cp_parser_dependent_template_p (arg);
2074 else if (TYPE_P (arg))
2075 return cp_parser_dependent_type_p (arg);
2077 return (cp_parser_type_dependent_expression_p (arg)
2078 || cp_parser_value_dependent_expression_p (arg));
2081 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
2084 cp_parser_dependent_template_id_p (tree tmpl, tree args)
2088 if (cp_parser_dependent_template_p (tmpl))
2090 for (i = 0; i < TREE_VEC_LENGTH (args); ++i)
2091 if (cp_parser_dependent_template_arg_p (TREE_VEC_ELT (args, i)))
2096 /* Returns TRUE if the template TMPL is dependent. */
2099 cp_parser_dependent_template_p (tree tmpl)
2101 /* Template template parameters are dependent. */
2102 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)
2103 || TREE_CODE (tmpl) == TEMPLATE_TEMPLATE_PARM)
2105 /* So are member templates of dependent classes. */
2106 if (TYPE_P (CP_DECL_CONTEXT (tmpl)))
2107 return cp_parser_dependent_type_p (DECL_CONTEXT (tmpl));
2111 /* Defer checking the accessibility of DECL, when looked up in
2115 cp_parser_defer_access_check (cp_parser *parser,
2121 /* If we are not supposed to defer access checks, just check now. */
2122 if (!parser->context->deferring_access_checks_p)
2124 enforce_access (class_type, decl);
2128 /* See if we are already going to perform this check. */
2129 for (check = parser->context->deferred_access_checks;
2131 check = TREE_CHAIN (check))
2132 if (TREE_VALUE (check) == decl
2133 && same_type_p (TREE_PURPOSE (check), class_type))
2135 /* If not, record the check. */
2136 parser->context->deferred_access_checks
2137 = tree_cons (class_type, decl, parser->context->deferred_access_checks);
2140 /* Start deferring access control checks. */
2143 cp_parser_start_deferring_access_checks (cp_parser *parser)
2145 parser->context->deferring_access_checks_p = true;
2148 /* Stop deferring access control checks. Returns a TREE_LIST
2149 representing the deferred checks. The TREE_PURPOSE of each node is
2150 the type through which the access occurred; the TREE_VALUE is the
2151 declaration named. */
2154 cp_parser_stop_deferring_access_checks (parser)
2159 parser->context->deferring_access_checks_p = false;
2160 access_checks = parser->context->deferred_access_checks;
2161 parser->context->deferred_access_checks = NULL_TREE;
2163 return access_checks;
2166 /* Perform the deferred ACCESS_CHECKS, whose representation is as
2167 documented with cp_parser_stop_deferrring_access_checks. */
2170 cp_parser_perform_deferred_access_checks (access_checks)
2173 tree deferred_check;
2175 /* Look through all the deferred checks. */
2176 for (deferred_check = access_checks;
2178 deferred_check = TREE_CHAIN (deferred_check))
2180 enforce_access (TREE_PURPOSE (deferred_check),
2181 TREE_VALUE (deferred_check));
2184 /* Returns the scope through which DECL is being accessed, or
2185 NULL_TREE if DECL is not a member. If OBJECT_TYPE is non-NULL, we
2186 have just seen `x->' or `x.' and OBJECT_TYPE is the type of `*x',
2187 or `x', respectively. If the DECL was named as `A::B' then
2188 NESTED_NAME_SPECIFIER is `A'. */
2191 cp_parser_scope_through_which_access_occurs (decl,
2193 nested_name_specifier)
2196 tree nested_name_specifier;
2199 tree qualifying_type = NULL_TREE;
2201 /* Determine the SCOPE of DECL. */
2202 scope = context_for_name_lookup (decl);
2203 /* If the SCOPE is not a type, then DECL is not a member. */
2204 if (!TYPE_P (scope))
2206 /* Figure out the type through which DECL is being accessed. */
2207 if (object_type && DERIVED_FROM_P (scope, object_type))
2208 /* If we are processing a `->' or `.' expression, use the type of the
2210 qualifying_type = object_type;
2211 else if (nested_name_specifier)
2213 /* If the reference is to a non-static member of the
2214 current class, treat it as if it were referenced through
2216 if (DECL_NONSTATIC_MEMBER_P (decl)
2217 && current_class_ptr
2218 && DERIVED_FROM_P (scope, current_class_type))
2219 qualifying_type = current_class_type;
2220 /* Otherwise, use the type indicated by the
2221 nested-name-specifier. */
2223 qualifying_type = nested_name_specifier;
2226 /* Otherwise, the name must be from the current class or one of
2228 qualifying_type = currently_open_derived_class (scope);
2230 return qualifying_type;
2233 /* Issue the indicated error MESSAGE. */
2236 cp_parser_error (parser, message)
2238 const char *message;
2240 /* Output the MESSAGE -- unless we're parsing tentatively. */
2241 if (!cp_parser_simulate_error (parser))
2245 /* If we are parsing tentatively, remember that an error has occurred
2246 during this tentative parse. Returns true if the error was
2247 simulated; false if a messgae should be issued by the caller. */
2250 cp_parser_simulate_error (parser)
2253 if (cp_parser_parsing_tentatively (parser)
2254 && !cp_parser_committed_to_tentative_parse (parser))
2256 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2262 /* This function is called when a type is defined. If type
2263 definitions are forbidden at this point, an error message is
2267 cp_parser_check_type_definition (parser)
2270 /* If types are forbidden here, issue a message. */
2271 if (parser->type_definition_forbidden_message)
2272 /* Use `%s' to print the string in case there are any escape
2273 characters in the message. */
2274 error ("%s", parser->type_definition_forbidden_message);
2277 /* Consume tokens up to, and including, the next non-nested closing `)'.
2278 Returns TRUE iff we found a closing `)'. */
2281 cp_parser_skip_to_closing_parenthesis (cp_parser *parser)
2283 unsigned nesting_depth = 0;
2289 /* If we've run out of tokens, then there is no closing `)'. */
2290 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2292 /* Consume the token. */
2293 token = cp_lexer_consume_token (parser->lexer);
2294 /* If it is an `(', we have entered another level of nesting. */
2295 if (token->type == CPP_OPEN_PAREN)
2297 /* If it is a `)', then we might be done. */
2298 else if (token->type == CPP_CLOSE_PAREN && nesting_depth-- == 0)
2303 /* Consume tokens until the next token is a `)', or a `,'. Returns
2304 TRUE if the next token is a `,'. */
2307 cp_parser_skip_to_closing_parenthesis_or_comma (cp_parser *parser)
2309 unsigned nesting_depth = 0;
2313 cp_token *token = cp_lexer_peek_token (parser->lexer);
2315 /* If we've run out of tokens, then there is no closing `)'. */
2316 if (token->type == CPP_EOF)
2318 /* If it is a `,' stop. */
2319 else if (token->type == CPP_COMMA && nesting_depth-- == 0)
2321 /* If it is a `)', stop. */
2322 else if (token->type == CPP_CLOSE_PAREN && nesting_depth-- == 0)
2324 /* If it is an `(', we have entered another level of nesting. */
2325 else if (token->type == CPP_OPEN_PAREN)
2327 /* Consume the token. */
2328 token = cp_lexer_consume_token (parser->lexer);
2332 /* Consume tokens until we reach the end of the current statement.
2333 Normally, that will be just before consuming a `;'. However, if a
2334 non-nested `}' comes first, then we stop before consuming that. */
2337 cp_parser_skip_to_end_of_statement (parser)
2340 unsigned nesting_depth = 0;
2346 /* Peek at the next token. */
2347 token = cp_lexer_peek_token (parser->lexer);
2348 /* If we've run out of tokens, stop. */
2349 if (token->type == CPP_EOF)
2351 /* If the next token is a `;', we have reached the end of the
2353 if (token->type == CPP_SEMICOLON && !nesting_depth)
2355 /* If the next token is a non-nested `}', then we have reached
2356 the end of the current block. */
2357 if (token->type == CPP_CLOSE_BRACE)
2359 /* If this is a non-nested `}', stop before consuming it.
2360 That way, when confronted with something like:
2364 we stop before consuming the closing `}', even though we
2365 have not yet reached a `;'. */
2366 if (nesting_depth == 0)
2368 /* If it is the closing `}' for a block that we have
2369 scanned, stop -- but only after consuming the token.
2375 we will stop after the body of the erroneously declared
2376 function, but before consuming the following `typedef'
2378 if (--nesting_depth == 0)
2380 cp_lexer_consume_token (parser->lexer);
2384 /* If it the next token is a `{', then we are entering a new
2385 block. Consume the entire block. */
2386 else if (token->type == CPP_OPEN_BRACE)
2388 /* Consume the token. */
2389 cp_lexer_consume_token (parser->lexer);
2393 /* Skip tokens until we have consumed an entire block, or until we
2394 have consumed a non-nested `;'. */
2397 cp_parser_skip_to_end_of_block_or_statement (parser)
2400 unsigned nesting_depth = 0;
2406 /* Peek at the next token. */
2407 token = cp_lexer_peek_token (parser->lexer);
2408 /* If we've run out of tokens, stop. */
2409 if (token->type == CPP_EOF)
2411 /* If the next token is a `;', we have reached the end of the
2413 if (token->type == CPP_SEMICOLON && !nesting_depth)
2415 /* Consume the `;'. */
2416 cp_lexer_consume_token (parser->lexer);
2419 /* Consume the token. */
2420 token = cp_lexer_consume_token (parser->lexer);
2421 /* If the next token is a non-nested `}', then we have reached
2422 the end of the current block. */
2423 if (token->type == CPP_CLOSE_BRACE
2424 && (nesting_depth == 0 || --nesting_depth == 0))
2426 /* If it the next token is a `{', then we are entering a new
2427 block. Consume the entire block. */
2428 if (token->type == CPP_OPEN_BRACE)
2433 /* Skip tokens until a non-nested closing curly brace is the next
2437 cp_parser_skip_to_closing_brace (cp_parser *parser)
2439 unsigned nesting_depth = 0;
2445 /* Peek at the next token. */
2446 token = cp_lexer_peek_token (parser->lexer);
2447 /* If we've run out of tokens, stop. */
2448 if (token->type == CPP_EOF)
2450 /* If the next token is a non-nested `}', then we have reached
2451 the end of the current block. */
2452 if (token->type == CPP_CLOSE_BRACE && nesting_depth-- == 0)
2454 /* If it the next token is a `{', then we are entering a new
2455 block. Consume the entire block. */
2456 else if (token->type == CPP_OPEN_BRACE)
2458 /* Consume the token. */
2459 cp_lexer_consume_token (parser->lexer);
2463 /* Create a new C++ parser. */
2470 parser = (cp_parser *) ggc_alloc_cleared (sizeof (cp_parser));
2471 parser->lexer = cp_lexer_new (/*main_lexer_p=*/true);
2472 parser->context = cp_parser_context_new (NULL);
2474 /* For now, we always accept GNU extensions. */
2475 parser->allow_gnu_extensions_p = 1;
2477 /* The `>' token is a greater-than operator, not the end of a
2479 parser->greater_than_is_operator_p = true;
2481 parser->default_arg_ok_p = true;
2483 /* We are not parsing a constant-expression. */
2484 parser->constant_expression_p = false;
2486 /* Local variable names are not forbidden. */
2487 parser->local_variables_forbidden_p = false;
2489 /* We are not procesing an `extern "C"' declaration. */
2490 parser->in_unbraced_linkage_specification_p = false;
2492 /* We are not processing a declarator. */
2493 parser->in_declarator_p = false;
2495 /* There are no default args to process. */
2496 parser->default_arg_types = NULL;
2498 /* The unparsed function queue is empty. */
2499 parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2501 /* There are no classes being defined. */
2502 parser->num_classes_being_defined = 0;
2504 /* No template parameters apply. */
2505 parser->num_template_parameter_lists = 0;
2510 /* Lexical conventions [gram.lex] */
2512 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
2516 cp_parser_identifier (parser)
2521 /* Look for the identifier. */
2522 token = cp_parser_require (parser, CPP_NAME, "identifier");
2523 /* Return the value. */
2524 return token ? token->value : error_mark_node;
2527 /* Basic concepts [gram.basic] */
2529 /* Parse a translation-unit.
2532 declaration-seq [opt]
2534 Returns TRUE if all went well. */
2537 cp_parser_translation_unit (parser)
2542 cp_parser_declaration_seq_opt (parser);
2544 /* If there are no tokens left then all went well. */
2545 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2548 /* Otherwise, issue an error message. */
2549 cp_parser_error (parser, "expected declaration");
2553 /* Consume the EOF token. */
2554 cp_parser_require (parser, CPP_EOF, "end-of-file");
2557 finish_translation_unit ();
2559 /* All went well. */
2563 /* Expressions [gram.expr] */
2565 /* Parse a primary-expression.
2576 ( compound-statement )
2577 __builtin_va_arg ( assignment-expression , type-id )
2582 Returns a representation of the expression.
2584 *IDK indicates what kind of id-expression (if any) was present.
2586 *QUALIFYING_CLASS is set to a non-NULL value if the id-expression can be
2587 used as the operand of a pointer-to-member. In that case,
2588 *QUALIFYING_CLASS gives the class that is used as the qualifying
2589 class in the pointer-to-member. */
2592 cp_parser_primary_expression (cp_parser *parser,
2593 cp_parser_id_kind *idk,
2594 tree *qualifying_class)
2598 /* Assume the primary expression is not an id-expression. */
2599 *idk = CP_PARSER_ID_KIND_NONE;
2600 /* And that it cannot be used as pointer-to-member. */
2601 *qualifying_class = NULL_TREE;
2603 /* Peek at the next token. */
2604 token = cp_lexer_peek_token (parser->lexer);
2605 switch (token->type)
2618 token = cp_lexer_consume_token (parser->lexer);
2619 return token->value;
2621 case CPP_OPEN_PAREN:
2624 bool saved_greater_than_is_operator_p;
2626 /* Consume the `('. */
2627 cp_lexer_consume_token (parser->lexer);
2628 /* Within a parenthesized expression, a `>' token is always
2629 the greater-than operator. */
2630 saved_greater_than_is_operator_p
2631 = parser->greater_than_is_operator_p;
2632 parser->greater_than_is_operator_p = true;
2633 /* If we see `( { ' then we are looking at the beginning of
2634 a GNU statement-expression. */
2635 if (cp_parser_allow_gnu_extensions_p (parser)
2636 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
2638 /* Statement-expressions are not allowed by the standard. */
2640 pedwarn ("ISO C++ forbids braced-groups within expressions");
2642 /* And they're not allowed outside of a function-body; you
2643 cannot, for example, write:
2645 int i = ({ int j = 3; j + 1; });
2647 at class or namespace scope. */
2648 if (!at_function_scope_p ())
2649 error ("statement-expressions are allowed only inside functions");
2650 /* Start the statement-expression. */
2651 expr = begin_stmt_expr ();
2652 /* Parse the compound-statement. */
2653 cp_parser_compound_statement (parser);
2655 expr = finish_stmt_expr (expr);
2659 /* Parse the parenthesized expression. */
2660 expr = cp_parser_expression (parser);
2661 /* Let the front end know that this expression was
2662 enclosed in parentheses. This matters in case, for
2663 example, the expression is of the form `A::B', since
2664 `&A::B' might be a pointer-to-member, but `&(A::B)' is
2666 finish_parenthesized_expr (expr);
2668 /* The `>' token might be the end of a template-id or
2669 template-parameter-list now. */
2670 parser->greater_than_is_operator_p
2671 = saved_greater_than_is_operator_p;
2672 /* Consume the `)'. */
2673 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
2674 cp_parser_skip_to_end_of_statement (parser);
2680 switch (token->keyword)
2682 /* These two are the boolean literals. */
2684 cp_lexer_consume_token (parser->lexer);
2685 return boolean_true_node;
2687 cp_lexer_consume_token (parser->lexer);
2688 return boolean_false_node;
2690 /* The `__null' literal. */
2692 cp_lexer_consume_token (parser->lexer);
2695 /* Recognize the `this' keyword. */
2697 cp_lexer_consume_token (parser->lexer);
2698 if (parser->local_variables_forbidden_p)
2700 error ("`this' may not be used in this context");
2701 return error_mark_node;
2703 return finish_this_expr ();
2705 /* The `operator' keyword can be the beginning of an
2710 case RID_FUNCTION_NAME:
2711 case RID_PRETTY_FUNCTION_NAME:
2712 case RID_C99_FUNCTION_NAME:
2713 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
2714 __func__ are the names of variables -- but they are
2715 treated specially. Therefore, they are handled here,
2716 rather than relying on the generic id-expression logic
2717 below. Gramatically, these names are id-expressions.
2719 Consume the token. */
2720 token = cp_lexer_consume_token (parser->lexer);
2721 /* Look up the name. */
2722 return finish_fname (token->value);
2729 /* The `__builtin_va_arg' construct is used to handle
2730 `va_arg'. Consume the `__builtin_va_arg' token. */
2731 cp_lexer_consume_token (parser->lexer);
2732 /* Look for the opening `('. */
2733 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
2734 /* Now, parse the assignment-expression. */
2735 expression = cp_parser_assignment_expression (parser);
2736 /* Look for the `,'. */
2737 cp_parser_require (parser, CPP_COMMA, "`,'");
2738 /* Parse the type-id. */
2739 type = cp_parser_type_id (parser);
2740 /* Look for the closing `)'. */
2741 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
2743 return build_x_va_arg (expression, type);
2747 cp_parser_error (parser, "expected primary-expression");
2748 return error_mark_node;
2752 /* An id-expression can start with either an identifier, a
2753 `::' as the beginning of a qualified-id, or the "operator"
2757 case CPP_TEMPLATE_ID:
2758 case CPP_NESTED_NAME_SPECIFIER:
2764 /* Parse the id-expression. */
2766 = cp_parser_id_expression (parser,
2767 /*template_keyword_p=*/false,
2768 /*check_dependency_p=*/true,
2769 /*template_p=*/NULL);
2770 if (id_expression == error_mark_node)
2771 return error_mark_node;
2772 /* If we have a template-id, then no further lookup is
2773 required. If the template-id was for a template-class, we
2774 will sometimes have a TYPE_DECL at this point. */
2775 else if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
2776 || TREE_CODE (id_expression) == TYPE_DECL)
2777 decl = id_expression;
2778 /* Look up the name. */
2781 decl = cp_parser_lookup_name_simple (parser, id_expression);
2782 /* If name lookup gives us a SCOPE_REF, then the
2783 qualifying scope was dependent. Just propagate the
2785 if (TREE_CODE (decl) == SCOPE_REF)
2787 if (TYPE_P (TREE_OPERAND (decl, 0)))
2788 *qualifying_class = TREE_OPERAND (decl, 0);
2791 /* Check to see if DECL is a local variable in a context
2792 where that is forbidden. */
2793 if (parser->local_variables_forbidden_p
2794 && local_variable_p (decl))
2796 /* It might be that we only found DECL because we are
2797 trying to be generous with pre-ISO scoping rules.
2798 For example, consider:
2802 for (int i = 0; i < 10; ++i) {}
2803 extern void f(int j = i);
2806 Here, name look up will originally find the out
2807 of scope `i'. We need to issue a warning message,
2808 but then use the global `i'. */
2809 decl = check_for_out_of_scope_variable (decl);
2810 if (local_variable_p (decl))
2812 error ("local variable `%D' may not appear in this context",
2814 return error_mark_node;
2818 /* If unqualified name lookup fails while processing a
2819 template, that just means that we need to do name
2820 lookup again when the template is instantiated. */
2822 && decl == error_mark_node
2823 && processing_template_decl)
2825 *idk = CP_PARSER_ID_KIND_UNQUALIFIED;
2826 return build_min_nt (LOOKUP_EXPR, id_expression);
2828 else if (decl == error_mark_node
2829 && !processing_template_decl)
2833 /* It may be resolvable as a koenig lookup function
2835 *idk = CP_PARSER_ID_KIND_UNQUALIFIED;
2836 return id_expression;
2838 else if (TYPE_P (parser->scope)
2839 && !COMPLETE_TYPE_P (parser->scope))
2840 error ("incomplete type `%T' used in nested name specifier",
2842 else if (parser->scope != global_namespace)
2843 error ("`%D' is not a member of `%D'",
2844 id_expression, parser->scope);
2846 error ("`::%D' has not been declared", id_expression);
2848 /* If DECL is a variable would be out of scope under
2849 ANSI/ISO rules, but in scope in the ARM, name lookup
2850 will succeed. Issue a diagnostic here. */
2852 decl = check_for_out_of_scope_variable (decl);
2854 /* Remember that the name was used in the definition of
2855 the current class so that we can check later to see if
2856 the meaning would have been different after the class
2857 was entirely defined. */
2858 if (!parser->scope && decl != error_mark_node)
2859 maybe_note_name_used_in_class (id_expression, decl);
2862 /* If we didn't find anything, or what we found was a type,
2863 then this wasn't really an id-expression. */
2864 if (TREE_CODE (decl) == TYPE_DECL
2865 || TREE_CODE (decl) == NAMESPACE_DECL
2866 || (TREE_CODE (decl) == TEMPLATE_DECL
2867 && !DECL_FUNCTION_TEMPLATE_P (decl)))
2869 cp_parser_error (parser,
2870 "expected primary-expression");
2871 return error_mark_node;
2874 /* If the name resolved to a template parameter, there is no
2875 need to look it up again later. Similarly, we resolve
2876 enumeration constants to their underlying values. */
2877 if (TREE_CODE (decl) == CONST_DECL)
2879 *idk = CP_PARSER_ID_KIND_NONE;
2880 if (DECL_TEMPLATE_PARM_P (decl) || !processing_template_decl)
2881 return DECL_INITIAL (decl);
2888 /* If the declaration was explicitly qualified indicate
2889 that. The semantics of `A::f(3)' are different than
2890 `f(3)' if `f' is virtual. */
2891 *idk = (parser->scope
2892 ? CP_PARSER_ID_KIND_QUALIFIED
2893 : (TREE_CODE (decl) == TEMPLATE_ID_EXPR
2894 ? CP_PARSER_ID_KIND_TEMPLATE_ID
2895 : CP_PARSER_ID_KIND_UNQUALIFIED));
2900 An id-expression is type-dependent if it contains an
2901 identifier that was declared with a dependent type.
2903 As an optimization, we could choose not to create a
2904 LOOKUP_EXPR for a name that resolved to a local
2905 variable in the template function that we are currently
2906 declaring; such a name cannot ever resolve to anything
2907 else. If we did that we would not have to look up
2908 these names at instantiation time.
2910 The standard is not very specific about an
2911 id-expression that names a set of overloaded functions.
2912 What if some of them have dependent types and some of
2913 them do not? Presumably, such a name should be treated
2914 as a dependent name. */
2915 /* Assume the name is not dependent. */
2916 dependent_p = false;
2917 if (!processing_template_decl)
2918 /* No names are dependent outside a template. */
2920 /* A template-id where the name of the template was not
2921 resolved is definitely dependent. */
2922 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
2923 && (TREE_CODE (TREE_OPERAND (decl, 0))
2924 == IDENTIFIER_NODE))
2926 /* For anything except an overloaded function, just check
2928 else if (!is_overloaded_fn (decl))
2930 = cp_parser_dependent_type_p (TREE_TYPE (decl));
2931 /* For a set of overloaded functions, check each of the
2937 if (BASELINK_P (fns))
2938 fns = BASELINK_FUNCTIONS (fns);
2940 /* For a template-id, check to see if the template
2941 arguments are dependent. */
2942 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
2944 tree args = TREE_OPERAND (fns, 1);
2946 if (args && TREE_CODE (args) == TREE_LIST)
2950 if (cp_parser_dependent_template_arg_p
2951 (TREE_VALUE (args)))
2956 args = TREE_CHAIN (args);
2959 else if (args && TREE_CODE (args) == TREE_VEC)
2962 for (i = 0; i < TREE_VEC_LENGTH (args); ++i)
2963 if (cp_parser_dependent_template_arg_p
2964 (TREE_VEC_ELT (args, i)))
2971 /* The functions are those referred to by the
2973 fns = TREE_OPERAND (fns, 0);
2976 /* If there are no dependent template arguments, go
2977 through the overlaoded functions. */
2978 while (fns && !dependent_p)
2980 tree fn = OVL_CURRENT (fns);
2982 /* Member functions of dependent classes are
2984 if (TREE_CODE (fn) == FUNCTION_DECL
2985 && cp_parser_type_dependent_expression_p (fn))
2987 else if (TREE_CODE (fn) == TEMPLATE_DECL
2988 && cp_parser_dependent_template_p (fn))
2991 fns = OVL_NEXT (fns);
2995 /* If the name was dependent on a template parameter,
2996 we will resolve the name at instantiation time. */
2999 /* Create a SCOPE_REF for qualified names. */
3002 if (TYPE_P (parser->scope))
3003 *qualifying_class = parser->scope;
3004 return build_nt (SCOPE_REF,
3008 /* A TEMPLATE_ID already contains all the information
3010 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR)
3011 return id_expression;
3012 /* Create a LOOKUP_EXPR for other unqualified names. */
3013 return build_min_nt (LOOKUP_EXPR, id_expression);
3018 decl = (adjust_result_of_qualified_name_lookup
3019 (decl, parser->scope, current_class_type));
3020 if (TREE_CODE (decl) == FIELD_DECL || BASELINK_P (decl))
3021 *qualifying_class = parser->scope;
3023 /* Resolve references to variables of anonymous unions
3024 into COMPONENT_REFs. */
3025 else if (TREE_CODE (decl) == ALIAS_DECL)
3026 decl = DECL_INITIAL (decl);
3028 /* Transform references to non-static data members into
3030 decl = hack_identifier (decl, id_expression);
3033 if (TREE_DEPRECATED (decl))
3034 warn_deprecated_use (decl);
3039 /* Anything else is an error. */
3041 cp_parser_error (parser, "expected primary-expression");
3042 return error_mark_node;
3046 /* Parse an id-expression.
3053 :: [opt] nested-name-specifier template [opt] unqualified-id
3055 :: operator-function-id
3058 Return a representation of the unqualified portion of the
3059 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
3060 a `::' or nested-name-specifier.
3062 Often, if the id-expression was a qualified-id, the caller will
3063 want to make a SCOPE_REF to represent the qualified-id. This
3064 function does not do this in order to avoid wastefully creating
3065 SCOPE_REFs when they are not required.
3067 If ASSUME_TYPENAME_P is true then we assume that qualified names
3068 are typenames. This flag is set when parsing a declarator-id;
3074 we are supposed to assume that `S<T>::R' is a class.
3076 If TEMPLATE_KEYWORD_P is true, then we have just seen the
3079 If CHECK_DEPENDENCY_P is false, then names are looked up inside
3080 uninstantiated templates.
3082 If *TEMPLATE_KEYWORD_P is non-NULL, it is set to true iff the
3083 `template' keyword is used to explicitly indicate that the entity
3084 named is a template. */
3087 cp_parser_id_expression (cp_parser *parser,
3088 bool template_keyword_p,
3089 bool check_dependency_p,
3092 bool global_scope_p;
3093 bool nested_name_specifier_p;
3095 /* Assume the `template' keyword was not used. */
3097 *template_p = false;
3099 /* Look for the optional `::' operator. */
3101 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
3103 /* Look for the optional nested-name-specifier. */
3104 nested_name_specifier_p
3105 = (cp_parser_nested_name_specifier_opt (parser,
3106 /*typename_keyword_p=*/false,
3110 /* If there is a nested-name-specifier, then we are looking at
3111 the first qualified-id production. */
3112 if (nested_name_specifier_p)
3115 tree saved_object_scope;
3116 tree saved_qualifying_scope;
3117 tree unqualified_id;
3120 /* See if the next token is the `template' keyword. */
3122 template_p = &is_template;
3123 *template_p = cp_parser_optional_template_keyword (parser);
3124 /* Name lookup we do during the processing of the
3125 unqualified-id might obliterate SCOPE. */
3126 saved_scope = parser->scope;
3127 saved_object_scope = parser->object_scope;
3128 saved_qualifying_scope = parser->qualifying_scope;
3129 /* Process the final unqualified-id. */
3130 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
3131 check_dependency_p);
3132 /* Restore the SAVED_SCOPE for our caller. */
3133 parser->scope = saved_scope;
3134 parser->object_scope = saved_object_scope;
3135 parser->qualifying_scope = saved_qualifying_scope;
3137 return unqualified_id;
3139 /* Otherwise, if we are in global scope, then we are looking at one
3140 of the other qualified-id productions. */
3141 else if (global_scope_p)
3146 /* Peek at the next token. */
3147 token = cp_lexer_peek_token (parser->lexer);
3149 /* If it's an identifier, and the next token is not a "<", then
3150 we can avoid the template-id case. This is an optimization
3151 for this common case. */
3152 if (token->type == CPP_NAME
3153 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_LESS)
3154 return cp_parser_identifier (parser);
3156 cp_parser_parse_tentatively (parser);
3157 /* Try a template-id. */
3158 id = cp_parser_template_id (parser,
3159 /*template_keyword_p=*/false,
3160 /*check_dependency_p=*/true);
3161 /* If that worked, we're done. */
3162 if (cp_parser_parse_definitely (parser))
3165 /* Peek at the next token. (Changes in the token buffer may
3166 have invalidated the pointer obtained above.) */
3167 token = cp_lexer_peek_token (parser->lexer);
3169 switch (token->type)
3172 return cp_parser_identifier (parser);
3175 if (token->keyword == RID_OPERATOR)
3176 return cp_parser_operator_function_id (parser);
3180 cp_parser_error (parser, "expected id-expression");
3181 return error_mark_node;
3185 return cp_parser_unqualified_id (parser, template_keyword_p,
3186 /*check_dependency_p=*/true);
3189 /* Parse an unqualified-id.
3193 operator-function-id
3194 conversion-function-id
3198 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3199 keyword, in a construct like `A::template ...'.
3201 Returns a representation of unqualified-id. For the `identifier'
3202 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
3203 production a BIT_NOT_EXPR is returned; the operand of the
3204 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
3205 other productions, see the documentation accompanying the
3206 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
3207 names are looked up in uninstantiated templates. */
3210 cp_parser_unqualified_id (parser, template_keyword_p,
3213 bool template_keyword_p;
3214 bool check_dependency_p;
3218 /* Peek at the next token. */
3219 token = cp_lexer_peek_token (parser->lexer);
3221 switch (token->type)
3227 /* We don't know yet whether or not this will be a
3229 cp_parser_parse_tentatively (parser);
3230 /* Try a template-id. */
3231 id = cp_parser_template_id (parser, template_keyword_p,
3232 check_dependency_p);
3233 /* If it worked, we're done. */
3234 if (cp_parser_parse_definitely (parser))
3236 /* Otherwise, it's an ordinary identifier. */
3237 return cp_parser_identifier (parser);
3240 case CPP_TEMPLATE_ID:
3241 return cp_parser_template_id (parser, template_keyword_p,
3242 check_dependency_p);
3247 tree qualifying_scope;
3251 /* Consume the `~' token. */
3252 cp_lexer_consume_token (parser->lexer);
3253 /* Parse the class-name. The standard, as written, seems to
3256 template <typename T> struct S { ~S (); };
3257 template <typename T> S<T>::~S() {}
3259 is invalid, since `~' must be followed by a class-name, but
3260 `S<T>' is dependent, and so not known to be a class.
3261 That's not right; we need to look in uninstantiated
3262 templates. A further complication arises from:
3264 template <typename T> void f(T t) {
3268 Here, it is not possible to look up `T' in the scope of `T'
3269 itself. We must look in both the current scope, and the
3270 scope of the containing complete expression.
3272 Yet another issue is:
3281 The standard does not seem to say that the `S' in `~S'
3282 should refer to the type `S' and not the data member
3285 /* DR 244 says that we look up the name after the "~" in the
3286 same scope as we looked up the qualifying name. That idea
3287 isn't fully worked out; it's more complicated than that. */
3288 scope = parser->scope;
3289 object_scope = parser->object_scope;
3290 qualifying_scope = parser->qualifying_scope;
3292 /* If the name is of the form "X::~X" it's OK. */
3293 if (scope && TYPE_P (scope)
3294 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
3295 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3297 && (cp_lexer_peek_token (parser->lexer)->value
3298 == TYPE_IDENTIFIER (scope)))
3300 cp_lexer_consume_token (parser->lexer);
3301 return build_nt (BIT_NOT_EXPR, scope);
3304 /* If there was an explicit qualification (S::~T), first look
3305 in the scope given by the qualification (i.e., S). */
3308 cp_parser_parse_tentatively (parser);
3309 type_decl = cp_parser_class_name (parser,
3310 /*typename_keyword_p=*/false,
3311 /*template_keyword_p=*/false,
3313 /*check_access_p=*/true,
3314 /*check_dependency=*/false,
3315 /*class_head_p=*/false);
3316 if (cp_parser_parse_definitely (parser))
3317 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3319 /* In "N::S::~S", look in "N" as well. */
3320 if (scope && qualifying_scope)
3322 cp_parser_parse_tentatively (parser);
3323 parser->scope = qualifying_scope;
3324 parser->object_scope = NULL_TREE;
3325 parser->qualifying_scope = NULL_TREE;
3327 = cp_parser_class_name (parser,
3328 /*typename_keyword_p=*/false,
3329 /*template_keyword_p=*/false,
3331 /*check_access_p=*/true,
3332 /*check_dependency=*/false,
3333 /*class_head_p=*/false);
3334 if (cp_parser_parse_definitely (parser))
3335 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3337 /* In "p->S::~T", look in the scope given by "*p" as well. */
3338 else if (object_scope)
3340 cp_parser_parse_tentatively (parser);
3341 parser->scope = object_scope;
3342 parser->object_scope = NULL_TREE;
3343 parser->qualifying_scope = NULL_TREE;
3345 = cp_parser_class_name (parser,
3346 /*typename_keyword_p=*/false,
3347 /*template_keyword_p=*/false,
3349 /*check_access_p=*/true,
3350 /*check_dependency=*/false,
3351 /*class_head_p=*/false);
3352 if (cp_parser_parse_definitely (parser))
3353 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3355 /* Look in the surrounding context. */
3356 parser->scope = NULL_TREE;
3357 parser->object_scope = NULL_TREE;
3358 parser->qualifying_scope = NULL_TREE;
3360 = cp_parser_class_name (parser,
3361 /*typename_keyword_p=*/false,
3362 /*template_keyword_p=*/false,
3364 /*check_access_p=*/true,
3365 /*check_dependency=*/false,
3366 /*class_head_p=*/false);
3367 /* If an error occurred, assume that the name of the
3368 destructor is the same as the name of the qualifying
3369 class. That allows us to keep parsing after running
3370 into ill-formed destructor names. */
3371 if (type_decl == error_mark_node && scope && TYPE_P (scope))
3372 return build_nt (BIT_NOT_EXPR, scope);
3373 else if (type_decl == error_mark_node)
3374 return error_mark_node;
3376 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3380 if (token->keyword == RID_OPERATOR)
3384 /* This could be a template-id, so we try that first. */
3385 cp_parser_parse_tentatively (parser);
3386 /* Try a template-id. */
3387 id = cp_parser_template_id (parser, template_keyword_p,
3388 /*check_dependency_p=*/true);
3389 /* If that worked, we're done. */
3390 if (cp_parser_parse_definitely (parser))
3392 /* We still don't know whether we're looking at an
3393 operator-function-id or a conversion-function-id. */
3394 cp_parser_parse_tentatively (parser);
3395 /* Try an operator-function-id. */
3396 id = cp_parser_operator_function_id (parser);
3397 /* If that didn't work, try a conversion-function-id. */
3398 if (!cp_parser_parse_definitely (parser))
3399 id = cp_parser_conversion_function_id (parser);
3406 cp_parser_error (parser, "expected unqualified-id");
3407 return error_mark_node;
3411 /* Parse an (optional) nested-name-specifier.
3413 nested-name-specifier:
3414 class-or-namespace-name :: nested-name-specifier [opt]
3415 class-or-namespace-name :: template nested-name-specifier [opt]
3417 PARSER->SCOPE should be set appropriately before this function is
3418 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
3419 effect. TYPE_P is TRUE if we non-type bindings should be ignored
3422 Sets PARSER->SCOPE to the class (TYPE) or namespace
3423 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
3424 it unchanged if there is no nested-name-specifier. Returns the new
3425 scope iff there is a nested-name-specifier, or NULL_TREE otherwise. */
3428 cp_parser_nested_name_specifier_opt (cp_parser *parser,
3429 bool typename_keyword_p,
3430 bool check_dependency_p,
3433 bool success = false;
3434 tree access_check = NULL_TREE;
3437 /* If the next token corresponds to a nested name specifier, there
3438 is no need to reparse it. */
3439 if (cp_lexer_next_token_is (parser->lexer, CPP_NESTED_NAME_SPECIFIER))
3444 /* Get the stored value. */
3445 value = cp_lexer_consume_token (parser->lexer)->value;
3446 /* Perform any access checks that were deferred. */
3447 for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
3448 cp_parser_defer_access_check (parser,
3449 TREE_PURPOSE (check),
3450 TREE_VALUE (check));
3451 /* Set the scope from the stored value. */
3452 parser->scope = TREE_VALUE (value);
3453 parser->qualifying_scope = TREE_TYPE (value);
3454 parser->object_scope = NULL_TREE;
3455 return parser->scope;
3458 /* Remember where the nested-name-specifier starts. */
3459 if (cp_parser_parsing_tentatively (parser)
3460 && !cp_parser_committed_to_tentative_parse (parser))
3462 cp_token *next_token = cp_lexer_peek_token (parser->lexer);
3463 start = cp_lexer_token_difference (parser->lexer,
3464 parser->lexer->first_token,
3466 access_check = parser->context->deferred_access_checks;
3475 tree saved_qualifying_scope;
3477 bool template_keyword_p;
3479 /* Spot cases that cannot be the beginning of a
3480 nested-name-specifier. On the second and subsequent times
3481 through the loop, we look for the `template' keyword. */
3482 token = cp_lexer_peek_token (parser->lexer);
3483 if (success && token->keyword == RID_TEMPLATE)
3485 /* A template-id can start a nested-name-specifier. */
3486 else if (token->type == CPP_TEMPLATE_ID)
3490 /* If the next token is not an identifier, then it is
3491 definitely not a class-or-namespace-name. */
3492 if (token->type != CPP_NAME)
3494 /* If the following token is neither a `<' (to begin a
3495 template-id), nor a `::', then we are not looking at a
3496 nested-name-specifier. */
3497 token = cp_lexer_peek_nth_token (parser->lexer, 2);
3498 if (token->type != CPP_LESS && token->type != CPP_SCOPE)
3502 /* The nested-name-specifier is optional, so we parse
3504 cp_parser_parse_tentatively (parser);
3506 /* Look for the optional `template' keyword, if this isn't the
3507 first time through the loop. */
3509 template_keyword_p = cp_parser_optional_template_keyword (parser);
3511 template_keyword_p = false;
3513 /* Save the old scope since the name lookup we are about to do
3514 might destroy it. */
3515 old_scope = parser->scope;
3516 saved_qualifying_scope = parser->qualifying_scope;
3517 /* Parse the qualifying entity. */
3519 = cp_parser_class_or_namespace_name (parser,
3524 /* Look for the `::' token. */
3525 cp_parser_require (parser, CPP_SCOPE, "`::'");
3527 /* If we found what we wanted, we keep going; otherwise, we're
3529 if (!cp_parser_parse_definitely (parser))
3531 bool error_p = false;
3533 /* Restore the OLD_SCOPE since it was valid before the
3534 failed attempt at finding the last
3535 class-or-namespace-name. */
3536 parser->scope = old_scope;
3537 parser->qualifying_scope = saved_qualifying_scope;
3538 /* If the next token is an identifier, and the one after
3539 that is a `::', then any valid interpretation would have
3540 found a class-or-namespace-name. */
3541 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
3542 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3544 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
3547 token = cp_lexer_consume_token (parser->lexer);
3552 decl = cp_parser_lookup_name_simple (parser, token->value);
3553 if (TREE_CODE (decl) == TEMPLATE_DECL)
3554 error ("`%D' used without template parameters",
3556 else if (parser->scope)
3558 if (TYPE_P (parser->scope))
3559 error ("`%T::%D' is not a class-name or "
3561 parser->scope, token->value);
3563 error ("`%D::%D' is not a class-name or "
3565 parser->scope, token->value);
3568 error ("`%D' is not a class-name or namespace-name",
3570 parser->scope = NULL_TREE;
3572 /* Treat this as a successful nested-name-specifier
3577 If the name found is not a class-name (clause
3578 _class_) or namespace-name (_namespace.def_), the
3579 program is ill-formed. */
3582 cp_lexer_consume_token (parser->lexer);
3587 /* We've found one valid nested-name-specifier. */
3589 /* Make sure we look in the right scope the next time through
3591 parser->scope = (TREE_CODE (new_scope) == TYPE_DECL
3592 ? TREE_TYPE (new_scope)
3594 /* If it is a class scope, try to complete it; we are about to
3595 be looking up names inside the class. */
3596 if (TYPE_P (parser->scope))
3597 complete_type (parser->scope);
3600 /* If parsing tentatively, replace the sequence of tokens that makes
3601 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
3602 token. That way, should we re-parse the token stream, we will
3603 not have to repeat the effort required to do the parse, nor will
3604 we issue duplicate error messages. */
3605 if (success && start >= 0)
3610 /* Find the token that corresponds to the start of the
3612 token = cp_lexer_advance_token (parser->lexer,
3613 parser->lexer->first_token,
3616 /* Remember the access checks associated with this
3617 nested-name-specifier. */
3618 c = parser->context->deferred_access_checks;
3619 if (c == access_check)
3620 access_check = NULL_TREE;
3623 while (TREE_CHAIN (c) != access_check)
3625 access_check = parser->context->deferred_access_checks;
3626 parser->context->deferred_access_checks = TREE_CHAIN (c);
3627 TREE_CHAIN (c) = NULL_TREE;
3630 /* Reset the contents of the START token. */
3631 token->type = CPP_NESTED_NAME_SPECIFIER;
3632 token->value = build_tree_list (access_check, parser->scope);
3633 TREE_TYPE (token->value) = parser->qualifying_scope;
3634 token->keyword = RID_MAX;
3635 /* Purge all subsequent tokens. */
3636 cp_lexer_purge_tokens_after (parser->lexer, token);
3639 return success ? parser->scope : NULL_TREE;
3642 /* Parse a nested-name-specifier. See
3643 cp_parser_nested_name_specifier_opt for details. This function
3644 behaves identically, except that it will an issue an error if no
3645 nested-name-specifier is present, and it will return
3646 ERROR_MARK_NODE, rather than NULL_TREE, if no nested-name-specifier
3650 cp_parser_nested_name_specifier (cp_parser *parser,
3651 bool typename_keyword_p,
3652 bool check_dependency_p,
3657 /* Look for the nested-name-specifier. */
3658 scope = cp_parser_nested_name_specifier_opt (parser,
3662 /* If it was not present, issue an error message. */
3665 cp_parser_error (parser, "expected nested-name-specifier");
3666 return error_mark_node;
3672 /* Parse a class-or-namespace-name.
3674 class-or-namespace-name:
3678 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
3679 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
3680 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
3681 TYPE_P is TRUE iff the next name should be taken as a class-name,
3682 even the same name is declared to be another entity in the same
3685 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
3686 specified by the class-or-namespace-name. If neither is found the
3687 ERROR_MARK_NODE is returned. */
3690 cp_parser_class_or_namespace_name (cp_parser *parser,
3691 bool typename_keyword_p,
3692 bool template_keyword_p,
3693 bool check_dependency_p,
3697 tree saved_qualifying_scope;
3698 tree saved_object_scope;
3702 /* If the next token is the `template' keyword, we know that we are
3703 looking at a class-name. */
3704 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
3705 return cp_parser_class_name (parser,
3709 /*check_access_p=*/true,
3711 /*class_head_p=*/false);
3712 /* Before we try to parse the class-name, we must save away the
3713 current PARSER->SCOPE since cp_parser_class_name will destroy
3715 saved_scope = parser->scope;
3716 saved_qualifying_scope = parser->qualifying_scope;
3717 saved_object_scope = parser->object_scope;
3718 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
3719 there is no need to look for a namespace-name. */
3720 only_class_p = saved_scope && TYPE_P (saved_scope);
3722 cp_parser_parse_tentatively (parser);
3723 scope = cp_parser_class_name (parser,
3727 /*check_access_p=*/true,
3729 /*class_head_p=*/false);
3730 /* If that didn't work, try for a namespace-name. */
3731 if (!only_class_p && !cp_parser_parse_definitely (parser))
3733 /* Restore the saved scope. */
3734 parser->scope = saved_scope;
3735 parser->qualifying_scope = saved_qualifying_scope;
3736 parser->object_scope = saved_object_scope;
3737 /* If we are not looking at an identifier followed by the scope
3738 resolution operator, then this is not part of a
3739 nested-name-specifier. (Note that this function is only used
3740 to parse the components of a nested-name-specifier.) */
3741 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
3742 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
3743 return error_mark_node;
3744 scope = cp_parser_namespace_name (parser);
3750 /* Parse a postfix-expression.
3754 postfix-expression [ expression ]
3755 postfix-expression ( expression-list [opt] )
3756 simple-type-specifier ( expression-list [opt] )
3757 typename :: [opt] nested-name-specifier identifier
3758 ( expression-list [opt] )
3759 typename :: [opt] nested-name-specifier template [opt] template-id
3760 ( expression-list [opt] )
3761 postfix-expression . template [opt] id-expression
3762 postfix-expression -> template [opt] id-expression
3763 postfix-expression . pseudo-destructor-name
3764 postfix-expression -> pseudo-destructor-name
3765 postfix-expression ++
3766 postfix-expression --
3767 dynamic_cast < type-id > ( expression )
3768 static_cast < type-id > ( expression )
3769 reinterpret_cast < type-id > ( expression )
3770 const_cast < type-id > ( expression )
3771 typeid ( expression )
3777 ( type-id ) { initializer-list , [opt] }
3779 This extension is a GNU version of the C99 compound-literal
3780 construct. (The C99 grammar uses `type-name' instead of `type-id',
3781 but they are essentially the same concept.)
3783 If ADDRESS_P is true, the postfix expression is the operand of the
3786 Returns a representation of the expression. */
3789 cp_parser_postfix_expression (cp_parser *parser, bool address_p)
3793 cp_parser_id_kind idk = CP_PARSER_ID_KIND_NONE;
3794 tree postfix_expression = NULL_TREE;
3795 /* Non-NULL only if the current postfix-expression can be used to
3796 form a pointer-to-member. In that case, QUALIFYING_CLASS is the
3797 class used to qualify the member. */
3798 tree qualifying_class = NULL_TREE;
3801 /* Peek at the next token. */
3802 token = cp_lexer_peek_token (parser->lexer);
3803 /* Some of the productions are determined by keywords. */
3804 keyword = token->keyword;
3814 const char *saved_message;
3816 /* All of these can be handled in the same way from the point
3817 of view of parsing. Begin by consuming the token
3818 identifying the cast. */
3819 cp_lexer_consume_token (parser->lexer);
3821 /* New types cannot be defined in the cast. */
3822 saved_message = parser->type_definition_forbidden_message;
3823 parser->type_definition_forbidden_message
3824 = "types may not be defined in casts";
3826 /* Look for the opening `<'. */
3827 cp_parser_require (parser, CPP_LESS, "`<'");
3828 /* Parse the type to which we are casting. */
3829 type = cp_parser_type_id (parser);
3830 /* Look for the closing `>'. */
3831 cp_parser_require (parser, CPP_GREATER, "`>'");
3832 /* Restore the old message. */
3833 parser->type_definition_forbidden_message = saved_message;
3835 /* And the expression which is being cast. */
3836 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3837 expression = cp_parser_expression (parser);
3838 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3844 = build_dynamic_cast (type, expression);
3848 = build_static_cast (type, expression);
3852 = build_reinterpret_cast (type, expression);
3856 = build_const_cast (type, expression);
3867 const char *saved_message;
3869 /* Consume the `typeid' token. */
3870 cp_lexer_consume_token (parser->lexer);
3871 /* Look for the `(' token. */
3872 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3873 /* Types cannot be defined in a `typeid' expression. */
3874 saved_message = parser->type_definition_forbidden_message;
3875 parser->type_definition_forbidden_message
3876 = "types may not be defined in a `typeid\' expression";
3877 /* We can't be sure yet whether we're looking at a type-id or an
3879 cp_parser_parse_tentatively (parser);
3880 /* Try a type-id first. */
3881 type = cp_parser_type_id (parser);
3882 /* Look for the `)' token. Otherwise, we can't be sure that
3883 we're not looking at an expression: consider `typeid (int
3884 (3))', for example. */
3885 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3886 /* If all went well, simply lookup the type-id. */
3887 if (cp_parser_parse_definitely (parser))
3888 postfix_expression = get_typeid (type);
3889 /* Otherwise, fall back to the expression variant. */
3894 /* Look for an expression. */
3895 expression = cp_parser_expression (parser);
3896 /* Compute its typeid. */
3897 postfix_expression = build_typeid (expression);
3898 /* Look for the `)' token. */
3899 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3902 /* Restore the saved message. */
3903 parser->type_definition_forbidden_message = saved_message;
3909 bool template_p = false;
3913 /* Consume the `typename' token. */
3914 cp_lexer_consume_token (parser->lexer);
3915 /* Look for the optional `::' operator. */
3916 cp_parser_global_scope_opt (parser,
3917 /*current_scope_valid_p=*/false);
3918 /* Look for the nested-name-specifier. */
3919 cp_parser_nested_name_specifier (parser,
3920 /*typename_keyword_p=*/true,
3921 /*check_dependency_p=*/true,
3923 /* Look for the optional `template' keyword. */
3924 template_p = cp_parser_optional_template_keyword (parser);
3925 /* We don't know whether we're looking at a template-id or an
3927 cp_parser_parse_tentatively (parser);
3928 /* Try a template-id. */
3929 id = cp_parser_template_id (parser, template_p,
3930 /*check_dependency_p=*/true);
3931 /* If that didn't work, try an identifier. */
3932 if (!cp_parser_parse_definitely (parser))
3933 id = cp_parser_identifier (parser);
3934 /* Create a TYPENAME_TYPE to represent the type to which the
3935 functional cast is being performed. */
3936 type = make_typename_type (parser->scope, id,
3939 postfix_expression = cp_parser_functional_cast (parser, type);
3947 /* If the next thing is a simple-type-specifier, we may be
3948 looking at a functional cast. We could also be looking at
3949 an id-expression. So, we try the functional cast, and if
3950 that doesn't work we fall back to the primary-expression. */
3951 cp_parser_parse_tentatively (parser);
3952 /* Look for the simple-type-specifier. */
3953 type = cp_parser_simple_type_specifier (parser,
3954 CP_PARSER_FLAGS_NONE);
3955 /* Parse the cast itself. */
3956 if (!cp_parser_error_occurred (parser))
3958 = cp_parser_functional_cast (parser, type);
3959 /* If that worked, we're done. */
3960 if (cp_parser_parse_definitely (parser))
3963 /* If the functional-cast didn't work out, try a
3964 compound-literal. */
3965 if (cp_parser_allow_gnu_extensions_p (parser))
3967 tree initializer_list = NULL_TREE;
3969 cp_parser_parse_tentatively (parser);
3970 /* Look for the `('. */
3971 if (cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
3973 type = cp_parser_type_id (parser);
3974 /* Look for the `)'. */
3975 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3976 /* Look for the `{'. */
3977 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
3978 /* If things aren't going well, there's no need to
3980 if (!cp_parser_error_occurred (parser))
3982 /* Parse the initializer-list. */
3984 = cp_parser_initializer_list (parser);
3985 /* Allow a trailing `,'. */
3986 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
3987 cp_lexer_consume_token (parser->lexer);
3988 /* Look for the final `}'. */
3989 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
3992 /* If that worked, we're definitely looking at a
3993 compound-literal expression. */
3994 if (cp_parser_parse_definitely (parser))
3996 /* Warn the user that a compound literal is not
3997 allowed in standard C++. */
3999 pedwarn ("ISO C++ forbids compound-literals");
4000 /* Form the representation of the compound-literal. */
4002 = finish_compound_literal (type, initializer_list);
4007 /* It must be a primary-expression. */
4008 postfix_expression = cp_parser_primary_expression (parser,
4015 /* Peek at the next token. */
4016 token = cp_lexer_peek_token (parser->lexer);
4017 done = (token->type != CPP_OPEN_SQUARE
4018 && token->type != CPP_OPEN_PAREN
4019 && token->type != CPP_DOT
4020 && token->type != CPP_DEREF
4021 && token->type != CPP_PLUS_PLUS
4022 && token->type != CPP_MINUS_MINUS);
4024 /* If the postfix expression is complete, finish up. */
4025 if (address_p && qualifying_class && done)
4027 if (TREE_CODE (postfix_expression) == SCOPE_REF)
4028 postfix_expression = TREE_OPERAND (postfix_expression, 1);
4030 = build_offset_ref (qualifying_class, postfix_expression);
4031 return postfix_expression;
4034 /* Otherwise, if we were avoiding committing until we knew
4035 whether or not we had a pointer-to-member, we now know that
4036 the expression is an ordinary reference to a qualified name. */
4037 if (qualifying_class && !processing_template_decl)
4039 if (TREE_CODE (postfix_expression) == FIELD_DECL)
4041 = finish_non_static_data_member (postfix_expression,
4043 else if (BASELINK_P (postfix_expression))
4048 /* See if any of the functions are non-static members. */
4049 fns = BASELINK_FUNCTIONS (postfix_expression);
4050 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
4051 fns = TREE_OPERAND (fns, 0);
4052 for (fn = fns; fn; fn = OVL_NEXT (fn))
4053 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn))
4055 /* If so, the expression may be relative to the current
4057 if (fn && current_class_type
4058 && DERIVED_FROM_P (qualifying_class, current_class_type))
4060 = (build_class_member_access_expr
4061 (maybe_dummy_object (qualifying_class, NULL),
4063 BASELINK_ACCESS_BINFO (postfix_expression),
4064 /*preserve_reference=*/false));
4066 return build_offset_ref (qualifying_class,
4067 postfix_expression);
4071 /* Remember that there was a reference to this entity. */
4072 if (DECL_P (postfix_expression))
4073 mark_used (postfix_expression);
4075 /* Keep looping until the postfix-expression is complete. */
4078 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE
4079 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
4081 /* It is not a Koenig lookup function call. */
4082 unqualified_name_lookup_error (postfix_expression);
4083 postfix_expression = error_mark_node;
4086 /* Peek at the next token. */
4087 token = cp_lexer_peek_token (parser->lexer);
4089 switch (token->type)
4091 case CPP_OPEN_SQUARE:
4092 /* postfix-expression [ expression ] */
4096 /* Consume the `[' token. */
4097 cp_lexer_consume_token (parser->lexer);
4098 /* Parse the index expression. */
4099 index = cp_parser_expression (parser);
4100 /* Look for the closing `]'. */
4101 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4103 /* Build the ARRAY_REF. */
4105 = grok_array_decl (postfix_expression, index);
4106 idk = CP_PARSER_ID_KIND_NONE;
4110 case CPP_OPEN_PAREN:
4111 /* postfix-expression ( expression-list [opt] ) */
4115 /* Consume the `(' token. */
4116 cp_lexer_consume_token (parser->lexer);
4117 /* If the next token is not a `)', then there are some
4119 if (cp_lexer_next_token_is_not (parser->lexer,
4121 args = cp_parser_expression_list (parser);
4124 /* Look for the closing `)'. */
4125 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4127 if (idk == CP_PARSER_ID_KIND_UNQUALIFIED
4128 && (is_overloaded_fn (postfix_expression)
4129 || DECL_P (postfix_expression)
4130 || TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4134 tree identifier = NULL_TREE;
4135 tree functions = NULL_TREE;
4137 /* Find the name of the overloaded function. */
4138 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4139 identifier = postfix_expression;
4140 else if (is_overloaded_fn (postfix_expression))
4142 functions = postfix_expression;
4143 identifier = DECL_NAME (get_first_fn (functions));
4145 else if (DECL_P (postfix_expression))
4147 functions = postfix_expression;
4148 identifier = DECL_NAME (postfix_expression);
4151 /* A call to a namespace-scope function using an
4154 Do Koenig lookup -- unless any of the arguments are
4156 for (arg = args; arg; arg = TREE_CHAIN (arg))
4157 if (cp_parser_type_dependent_expression_p (TREE_VALUE (arg)))
4162 = lookup_arg_dependent(identifier, functions, args);
4163 if (!postfix_expression)
4165 /* The unqualified name could not be resolved. */
4166 unqualified_name_lookup_error (identifier);
4167 postfix_expression = error_mark_node;
4170 = build_call_from_tree (postfix_expression, args,
4171 /*diallow_virtual=*/false);
4174 postfix_expression = build_min_nt (LOOKUP_EXPR,
4177 else if (idk == CP_PARSER_ID_KIND_UNQUALIFIED
4178 && TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4180 /* The unqualified name could not be resolved. */
4181 unqualified_name_lookup_error (postfix_expression);
4182 postfix_expression = error_mark_node;
4186 /* In the body of a template, no further processing is
4188 if (processing_template_decl)
4190 postfix_expression = build_nt (CALL_EXPR,
4196 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
4198 = (build_new_method_call
4199 (TREE_OPERAND (postfix_expression, 0),
4200 TREE_OPERAND (postfix_expression, 1),
4202 (idk == CP_PARSER_ID_KIND_QUALIFIED
4203 ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL)));
4204 else if (TREE_CODE (postfix_expression) == OFFSET_REF)
4205 postfix_expression = (build_offset_ref_call_from_tree
4206 (postfix_expression, args));
4207 else if (idk == CP_PARSER_ID_KIND_QUALIFIED)
4209 /* A call to a static class member, or a
4210 namespace-scope function. */
4212 = finish_call_expr (postfix_expression, args,
4213 /*disallow_virtual=*/true);
4217 /* All other function calls. */
4219 = finish_call_expr (postfix_expression, args,
4220 /*disallow_virtual=*/false);
4223 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
4224 idk = CP_PARSER_ID_KIND_NONE;
4230 /* postfix-expression . template [opt] id-expression
4231 postfix-expression . pseudo-destructor-name
4232 postfix-expression -> template [opt] id-expression
4233 postfix-expression -> pseudo-destructor-name */
4238 tree scope = NULL_TREE;
4240 /* If this is a `->' operator, dereference the pointer. */
4241 if (token->type == CPP_DEREF)
4242 postfix_expression = build_x_arrow (postfix_expression);
4243 /* Check to see whether or not the expression is
4245 dependent_p = (cp_parser_type_dependent_expression_p
4246 (postfix_expression));
4247 /* The identifier following the `->' or `.' is not
4249 parser->scope = NULL_TREE;
4250 parser->qualifying_scope = NULL_TREE;
4251 parser->object_scope = NULL_TREE;
4252 /* Enter the scope corresponding to the type of the object
4253 given by the POSTFIX_EXPRESSION. */
4255 && TREE_TYPE (postfix_expression) != NULL_TREE)
4257 scope = TREE_TYPE (postfix_expression);
4258 /* According to the standard, no expression should
4259 ever have reference type. Unfortunately, we do not
4260 currently match the standard in this respect in
4261 that our internal representation of an expression
4262 may have reference type even when the standard says
4263 it does not. Therefore, we have to manually obtain
4264 the underlying type here. */
4265 if (TREE_CODE (scope) == REFERENCE_TYPE)
4266 scope = TREE_TYPE (scope);
4267 /* If the SCOPE is an OFFSET_TYPE, then we grab the
4268 type of the field. We get an OFFSET_TYPE for
4273 Probably, we should not get an OFFSET_TYPE here;
4274 that transformation should be made only if `&S::T'
4276 if (TREE_CODE (scope) == OFFSET_TYPE)
4277 scope = TREE_TYPE (scope);
4278 /* The type of the POSTFIX_EXPRESSION must be
4280 scope = complete_type_or_else (scope, NULL_TREE);
4281 /* Let the name lookup machinery know that we are
4282 processing a class member access expression. */
4283 parser->context->object_type = scope;
4284 /* If something went wrong, we want to be able to
4285 discern that case, as opposed to the case where
4286 there was no SCOPE due to the type of expression
4289 scope = error_mark_node;
4292 /* Consume the `.' or `->' operator. */
4293 cp_lexer_consume_token (parser->lexer);
4294 /* If the SCOPE is not a scalar type, we are looking at an
4295 ordinary class member access expression, rather than a
4296 pseudo-destructor-name. */
4297 if (!scope || !SCALAR_TYPE_P (scope))
4299 template_p = cp_parser_optional_template_keyword (parser);
4300 /* Parse the id-expression. */
4301 name = cp_parser_id_expression (parser,
4303 /*check_dependency_p=*/true,
4304 /*template_p=*/NULL);
4305 /* In general, build a SCOPE_REF if the member name is
4306 qualified. However, if the name was not dependent
4307 and has already been resolved; there is no need to
4308 build the SCOPE_REF. For example;
4310 struct X { void f(); };
4311 template <typename T> void f(T* t) { t->X::f(); }
4313 Even though "t" is dependent, "X::f" is not and has
4314 except that for a BASELINK there is no need to
4315 include scope information. */
4316 if (name != error_mark_node
4317 && !BASELINK_P (name)
4320 name = build_nt (SCOPE_REF, parser->scope, name);
4321 parser->scope = NULL_TREE;
4322 parser->qualifying_scope = NULL_TREE;
4323 parser->object_scope = NULL_TREE;
4326 = finish_class_member_access_expr (postfix_expression, name);
4328 /* Otherwise, try the pseudo-destructor-name production. */
4334 /* Parse the pseudo-destructor-name. */
4335 cp_parser_pseudo_destructor_name (parser, &s, &type);
4336 /* Form the call. */
4338 = finish_pseudo_destructor_expr (postfix_expression,
4339 s, TREE_TYPE (type));
4342 /* We no longer need to look up names in the scope of the
4343 object on the left-hand side of the `.' or `->'
4345 parser->context->object_type = NULL_TREE;
4346 idk = CP_PARSER_ID_KIND_NONE;
4351 /* postfix-expression ++ */
4352 /* Consume the `++' token. */
4353 cp_lexer_consume_token (parser->lexer);
4354 /* Generate a reprsentation for the complete expression. */
4356 = finish_increment_expr (postfix_expression,
4357 POSTINCREMENT_EXPR);
4358 idk = CP_PARSER_ID_KIND_NONE;
4361 case CPP_MINUS_MINUS:
4362 /* postfix-expression -- */
4363 /* Consume the `--' token. */
4364 cp_lexer_consume_token (parser->lexer);
4365 /* Generate a reprsentation for the complete expression. */
4367 = finish_increment_expr (postfix_expression,
4368 POSTDECREMENT_EXPR);
4369 idk = CP_PARSER_ID_KIND_NONE;
4373 return postfix_expression;
4377 /* We should never get here. */
4379 return error_mark_node;
4382 /* Parse an expression-list.
4385 assignment-expression
4386 expression-list, assignment-expression
4388 Returns a TREE_LIST. The TREE_VALUE of each node is a
4389 representation of an assignment-expression. Note that a TREE_LIST
4390 is returned even if there is only a single expression in the list. */
4393 cp_parser_expression_list (parser)
4396 tree expression_list = NULL_TREE;
4398 /* Consume expressions until there are no more. */
4403 /* Parse the next assignment-expression. */
4404 expr = cp_parser_assignment_expression (parser);
4405 /* Add it to the list. */
4406 expression_list = tree_cons (NULL_TREE, expr, expression_list);
4408 /* If the next token isn't a `,', then we are done. */
4409 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
4411 /* All uses of expression-list in the grammar are followed
4412 by a `)'. Therefore, if the next token is not a `)' an
4413 error will be issued, unless we are parsing tentatively.
4414 Skip ahead to see if there is another `,' before the `)';
4415 if so, we can go there and recover. */
4416 if (cp_parser_parsing_tentatively (parser)
4417 || cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
4418 || !cp_parser_skip_to_closing_parenthesis_or_comma (parser))
4422 /* Otherwise, consume the `,' and keep going. */
4423 cp_lexer_consume_token (parser->lexer);
4426 /* We built up the list in reverse order so we must reverse it now. */
4427 return nreverse (expression_list);
4430 /* Parse a pseudo-destructor-name.
4432 pseudo-destructor-name:
4433 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
4434 :: [opt] nested-name-specifier template template-id :: ~ type-name
4435 :: [opt] nested-name-specifier [opt] ~ type-name
4437 If either of the first two productions is used, sets *SCOPE to the
4438 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
4439 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
4440 or ERROR_MARK_NODE if no type-name is present. */
4443 cp_parser_pseudo_destructor_name (parser, scope, type)
4448 bool nested_name_specifier_p;
4450 /* Look for the optional `::' operator. */
4451 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
4452 /* Look for the optional nested-name-specifier. */
4453 nested_name_specifier_p
4454 = (cp_parser_nested_name_specifier_opt (parser,
4455 /*typename_keyword_p=*/false,
4456 /*check_dependency_p=*/true,
4459 /* Now, if we saw a nested-name-specifier, we might be doing the
4460 second production. */
4461 if (nested_name_specifier_p
4462 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
4464 /* Consume the `template' keyword. */
4465 cp_lexer_consume_token (parser->lexer);
4466 /* Parse the template-id. */
4467 cp_parser_template_id (parser,
4468 /*template_keyword_p=*/true,
4469 /*check_dependency_p=*/false);
4470 /* Look for the `::' token. */
4471 cp_parser_require (parser, CPP_SCOPE, "`::'");
4473 /* If the next token is not a `~', then there might be some
4474 additional qualification. */
4475 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
4477 /* Look for the type-name. */
4478 *scope = TREE_TYPE (cp_parser_type_name (parser));
4479 /* Look for the `::' token. */
4480 cp_parser_require (parser, CPP_SCOPE, "`::'");
4485 /* Look for the `~'. */
4486 cp_parser_require (parser, CPP_COMPL, "`~'");
4487 /* Look for the type-name again. We are not responsible for
4488 checking that it matches the first type-name. */
4489 *type = cp_parser_type_name (parser);
4492 /* Parse a unary-expression.
4498 unary-operator cast-expression
4499 sizeof unary-expression
4507 __extension__ cast-expression
4508 __alignof__ unary-expression
4509 __alignof__ ( type-id )
4510 __real__ cast-expression
4511 __imag__ cast-expression
4514 ADDRESS_P is true iff the unary-expression is appearing as the
4515 operand of the `&' operator.
4517 Returns a representation of the expresion. */
4520 cp_parser_unary_expression (cp_parser *parser, bool address_p)
4523 enum tree_code unary_operator;
4525 /* Peek at the next token. */
4526 token = cp_lexer_peek_token (parser->lexer);
4527 /* Some keywords give away the kind of expression. */
4528 if (token->type == CPP_KEYWORD)
4530 enum rid keyword = token->keyword;
4536 /* Consume the `alignof' token. */
4537 cp_lexer_consume_token (parser->lexer);
4538 /* Parse the operand. */
4539 return finish_alignof (cp_parser_sizeof_operand
4547 /* Consume the `sizeof' token. */
4548 cp_lexer_consume_token (parser->lexer);
4549 /* Parse the operand. */
4550 operand = cp_parser_sizeof_operand (parser, keyword);
4552 /* If the type of the operand cannot be determined build a
4554 if (TYPE_P (operand)
4555 ? cp_parser_dependent_type_p (operand)
4556 : cp_parser_type_dependent_expression_p (operand))
4557 return build_min (SIZEOF_EXPR, size_type_node, operand);
4558 /* Otherwise, compute the constant value. */
4560 return finish_sizeof (operand);
4564 return cp_parser_new_expression (parser);
4567 return cp_parser_delete_expression (parser);
4571 /* The saved value of the PEDANTIC flag. */
4575 /* Save away the PEDANTIC flag. */
4576 cp_parser_extension_opt (parser, &saved_pedantic);
4577 /* Parse the cast-expression. */
4578 expr = cp_parser_cast_expression (parser, /*address_p=*/false);
4579 /* Restore the PEDANTIC flag. */
4580 pedantic = saved_pedantic;
4590 /* Consume the `__real__' or `__imag__' token. */
4591 cp_lexer_consume_token (parser->lexer);
4592 /* Parse the cast-expression. */
4593 expression = cp_parser_cast_expression (parser,
4594 /*address_p=*/false);
4595 /* Create the complete representation. */
4596 return build_x_unary_op ((keyword == RID_REALPART
4597 ? REALPART_EXPR : IMAGPART_EXPR),
4607 /* Look for the `:: new' and `:: delete', which also signal the
4608 beginning of a new-expression, or delete-expression,
4609 respectively. If the next token is `::', then it might be one of
4611 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
4615 /* See if the token after the `::' is one of the keywords in
4616 which we're interested. */
4617 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
4618 /* If it's `new', we have a new-expression. */
4619 if (keyword == RID_NEW)
4620 return cp_parser_new_expression (parser);
4621 /* Similarly, for `delete'. */
4622 else if (keyword == RID_DELETE)
4623 return cp_parser_delete_expression (parser);
4626 /* Look for a unary operator. */
4627 unary_operator = cp_parser_unary_operator (token);
4628 /* The `++' and `--' operators can be handled similarly, even though
4629 they are not technically unary-operators in the grammar. */
4630 if (unary_operator == ERROR_MARK)
4632 if (token->type == CPP_PLUS_PLUS)
4633 unary_operator = PREINCREMENT_EXPR;
4634 else if (token->type == CPP_MINUS_MINUS)
4635 unary_operator = PREDECREMENT_EXPR;
4636 /* Handle the GNU address-of-label extension. */
4637 else if (cp_parser_allow_gnu_extensions_p (parser)
4638 && token->type == CPP_AND_AND)
4642 /* Consume the '&&' token. */
4643 cp_lexer_consume_token (parser->lexer);
4644 /* Look for the identifier. */
4645 identifier = cp_parser_identifier (parser);
4646 /* Create an expression representing the address. */
4647 return finish_label_address_expr (identifier);
4650 if (unary_operator != ERROR_MARK)
4652 tree cast_expression;
4654 /* Consume the operator token. */
4655 token = cp_lexer_consume_token (parser->lexer);
4656 /* Parse the cast-expression. */
4658 = cp_parser_cast_expression (parser, unary_operator == ADDR_EXPR);
4659 /* Now, build an appropriate representation. */
4660 switch (unary_operator)
4663 return build_x_indirect_ref (cast_expression, "unary *");
4666 return build_x_unary_op (ADDR_EXPR, cast_expression);
4670 case TRUTH_NOT_EXPR:
4671 case PREINCREMENT_EXPR:
4672 case PREDECREMENT_EXPR:
4673 return finish_unary_op_expr (unary_operator, cast_expression);
4676 return build_x_unary_op (BIT_NOT_EXPR, cast_expression);
4680 return error_mark_node;
4684 return cp_parser_postfix_expression (parser, address_p);
4687 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
4688 unary-operator, the corresponding tree code is returned. */
4690 static enum tree_code
4691 cp_parser_unary_operator (token)
4694 switch (token->type)
4697 return INDIRECT_REF;
4703 return CONVERT_EXPR;
4709 return TRUTH_NOT_EXPR;
4712 return BIT_NOT_EXPR;
4719 /* Parse a new-expression.
4721 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
4722 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
4724 Returns a representation of the expression. */
4727 cp_parser_new_expression (parser)
4730 bool global_scope_p;
4735 /* Look for the optional `::' operator. */
4737 = (cp_parser_global_scope_opt (parser,
4738 /*current_scope_valid_p=*/false)
4740 /* Look for the `new' operator. */
4741 cp_parser_require_keyword (parser, RID_NEW, "`new'");
4742 /* There's no easy way to tell a new-placement from the
4743 `( type-id )' construct. */
4744 cp_parser_parse_tentatively (parser);
4745 /* Look for a new-placement. */
4746 placement = cp_parser_new_placement (parser);
4747 /* If that didn't work out, there's no new-placement. */
4748 if (!cp_parser_parse_definitely (parser))
4749 placement = NULL_TREE;
4751 /* If the next token is a `(', then we have a parenthesized
4753 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4755 /* Consume the `('. */
4756 cp_lexer_consume_token (parser->lexer);
4757 /* Parse the type-id. */
4758 type = cp_parser_type_id (parser);
4759 /* Look for the closing `)'. */
4760 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4762 /* Otherwise, there must be a new-type-id. */
4764 type = cp_parser_new_type_id (parser);
4766 /* If the next token is a `(', then we have a new-initializer. */
4767 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4768 initializer = cp_parser_new_initializer (parser);
4770 initializer = NULL_TREE;
4772 /* Create a representation of the new-expression. */
4773 return build_new (placement, type, initializer, global_scope_p);
4776 /* Parse a new-placement.
4781 Returns the same representation as for an expression-list. */
4784 cp_parser_new_placement (parser)
4787 tree expression_list;
4789 /* Look for the opening `('. */
4790 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
4791 return error_mark_node;
4792 /* Parse the expression-list. */
4793 expression_list = cp_parser_expression_list (parser);
4794 /* Look for the closing `)'. */
4795 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4797 return expression_list;
4800 /* Parse a new-type-id.
4803 type-specifier-seq new-declarator [opt]
4805 Returns a TREE_LIST whose TREE_PURPOSE is the type-specifier-seq,
4806 and whose TREE_VALUE is the new-declarator. */
4809 cp_parser_new_type_id (parser)
4812 tree type_specifier_seq;
4814 const char *saved_message;
4816 /* The type-specifier sequence must not contain type definitions.
4817 (It cannot contain declarations of new types either, but if they
4818 are not definitions we will catch that because they are not
4820 saved_message = parser->type_definition_forbidden_message;
4821 parser->type_definition_forbidden_message
4822 = "types may not be defined in a new-type-id";
4823 /* Parse the type-specifier-seq. */
4824 type_specifier_seq = cp_parser_type_specifier_seq (parser);
4825 /* Restore the old message. */
4826 parser->type_definition_forbidden_message = saved_message;
4827 /* Parse the new-declarator. */
4828 declarator = cp_parser_new_declarator_opt (parser);
4830 return build_tree_list (type_specifier_seq, declarator);
4833 /* Parse an (optional) new-declarator.
4836 ptr-operator new-declarator [opt]
4837 direct-new-declarator
4839 Returns a representation of the declarator. See
4840 cp_parser_declarator for the representations used. */
4843 cp_parser_new_declarator_opt (parser)
4846 enum tree_code code;
4848 tree cv_qualifier_seq;
4850 /* We don't know if there's a ptr-operator next, or not. */
4851 cp_parser_parse_tentatively (parser);
4852 /* Look for a ptr-operator. */
4853 code = cp_parser_ptr_operator (parser, &type, &cv_qualifier_seq);
4854 /* If that worked, look for more new-declarators. */
4855 if (cp_parser_parse_definitely (parser))
4859 /* Parse another optional declarator. */
4860 declarator = cp_parser_new_declarator_opt (parser);
4862 /* Create the representation of the declarator. */
4863 if (code == INDIRECT_REF)
4864 declarator = make_pointer_declarator (cv_qualifier_seq,
4867 declarator = make_reference_declarator (cv_qualifier_seq,
4870 /* Handle the pointer-to-member case. */
4872 declarator = build_nt (SCOPE_REF, type, declarator);
4877 /* If the next token is a `[', there is a direct-new-declarator. */
4878 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
4879 return cp_parser_direct_new_declarator (parser);
4884 /* Parse a direct-new-declarator.
4886 direct-new-declarator:
4888 direct-new-declarator [constant-expression]
4890 Returns an ARRAY_REF, following the same conventions as are
4891 documented for cp_parser_direct_declarator. */
4894 cp_parser_direct_new_declarator (parser)
4897 tree declarator = NULL_TREE;
4903 /* Look for the opening `['. */
4904 cp_parser_require (parser, CPP_OPEN_SQUARE, "`['");
4905 /* The first expression is not required to be constant. */
4908 expression = cp_parser_expression (parser);
4909 /* The standard requires that the expression have integral
4910 type. DR 74 adds enumeration types. We believe that the
4911 real intent is that these expressions be handled like the
4912 expression in a `switch' condition, which also allows
4913 classes with a single conversion to integral or
4914 enumeration type. */
4915 if (!processing_template_decl)
4918 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
4923 error ("expression in new-declarator must have integral or enumeration type");
4924 expression = error_mark_node;
4928 /* But all the other expressions must be. */
4930 expression = cp_parser_constant_expression (parser);
4931 /* Look for the closing `]'. */
4932 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4934 /* Add this bound to the declarator. */
4935 declarator = build_nt (ARRAY_REF, declarator, expression);
4937 /* If the next token is not a `[', then there are no more
4939 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
4946 /* Parse a new-initializer.
4949 ( expression-list [opt] )
4951 Returns a reprsentation of the expression-list. If there is no
4952 expression-list, VOID_ZERO_NODE is returned. */
4955 cp_parser_new_initializer (parser)
4958 tree expression_list;
4960 /* Look for the opening parenthesis. */
4961 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
4962 /* If the next token is not a `)', then there is an
4964 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
4965 expression_list = cp_parser_expression_list (parser);
4967 expression_list = void_zero_node;
4968 /* Look for the closing parenthesis. */
4969 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4971 return expression_list;
4974 /* Parse a delete-expression.
4977 :: [opt] delete cast-expression
4978 :: [opt] delete [ ] cast-expression
4980 Returns a representation of the expression. */
4983 cp_parser_delete_expression (parser)
4986 bool global_scope_p;
4990 /* Look for the optional `::' operator. */
4992 = (cp_parser_global_scope_opt (parser,
4993 /*current_scope_valid_p=*/false)
4995 /* Look for the `delete' keyword. */
4996 cp_parser_require_keyword (parser, RID_DELETE, "`delete'");
4997 /* See if the array syntax is in use. */
4998 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5000 /* Consume the `[' token. */
5001 cp_lexer_consume_token (parser->lexer);
5002 /* Look for the `]' token. */
5003 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5004 /* Remember that this is the `[]' construct. */
5010 /* Parse the cast-expression. */
5011 expression = cp_parser_cast_expression (parser, /*address_p=*/false);
5013 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
5016 /* Parse a cast-expression.
5020 ( type-id ) cast-expression
5022 Returns a representation of the expression. */
5025 cp_parser_cast_expression (cp_parser *parser, bool address_p)
5027 /* If it's a `(', then we might be looking at a cast. */
5028 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5030 tree type = NULL_TREE;
5031 tree expr = NULL_TREE;
5032 bool compound_literal_p;
5033 const char *saved_message;
5035 /* There's no way to know yet whether or not this is a cast.
5036 For example, `(int (3))' is a unary-expression, while `(int)
5037 3' is a cast. So, we resort to parsing tentatively. */
5038 cp_parser_parse_tentatively (parser);
5039 /* Types may not be defined in a cast. */
5040 saved_message = parser->type_definition_forbidden_message;
5041 parser->type_definition_forbidden_message
5042 = "types may not be defined in casts";
5043 /* Consume the `('. */
5044 cp_lexer_consume_token (parser->lexer);
5045 /* A very tricky bit is that `(struct S) { 3 }' is a
5046 compound-literal (which we permit in C++ as an extension).
5047 But, that construct is not a cast-expression -- it is a
5048 postfix-expression. (The reason is that `(struct S) { 3 }.i'
5049 is legal; if the compound-literal were a cast-expression,
5050 you'd need an extra set of parentheses.) But, if we parse
5051 the type-id, and it happens to be a class-specifier, then we
5052 will commit to the parse at that point, because we cannot
5053 undo the action that is done when creating a new class. So,
5054 then we cannot back up and do a postfix-expression.
5056 Therefore, we scan ahead to the closing `)', and check to see
5057 if the token after the `)' is a `{'. If so, we are not
5058 looking at a cast-expression.
5060 Save tokens so that we can put them back. */
5061 cp_lexer_save_tokens (parser->lexer);
5062 /* Skip tokens until the next token is a closing parenthesis.
5063 If we find the closing `)', and the next token is a `{', then
5064 we are looking at a compound-literal. */
5066 = (cp_parser_skip_to_closing_parenthesis (parser)
5067 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
5068 /* Roll back the tokens we skipped. */
5069 cp_lexer_rollback_tokens (parser->lexer);
5070 /* If we were looking at a compound-literal, simulate an error
5071 so that the call to cp_parser_parse_definitely below will
5073 if (compound_literal_p)
5074 cp_parser_simulate_error (parser);
5077 /* Look for the type-id. */
5078 type = cp_parser_type_id (parser);
5079 /* Look for the closing `)'. */
5080 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5083 /* Restore the saved message. */
5084 parser->type_definition_forbidden_message = saved_message;
5086 /* If all went well, this is a cast. */
5087 if (cp_parser_parse_definitely (parser))
5089 /* Parse the dependent expression. */
5090 expr = cp_parser_cast_expression (parser, /*address_p=*/false);
5091 /* Warn about old-style casts, if so requested. */
5092 if (warn_old_style_cast
5093 && !in_system_header
5094 && !VOID_TYPE_P (type)
5095 && current_lang_name != lang_name_c)
5096 warning ("use of old-style cast");
5097 /* Perform the cast. */
5098 expr = build_c_cast (type, expr);
5105 /* If we get here, then it's not a cast, so it must be a
5106 unary-expression. */
5107 return cp_parser_unary_expression (parser, address_p);
5110 /* Parse a pm-expression.
5114 pm-expression .* cast-expression
5115 pm-expression ->* cast-expression
5117 Returns a representation of the expression. */
5120 cp_parser_pm_expression (parser)
5126 /* Parse the cast-expresion. */
5127 cast_expr = cp_parser_cast_expression (parser, /*address_p=*/false);
5128 pm_expr = cast_expr;
5129 /* Now look for pointer-to-member operators. */
5133 enum cpp_ttype token_type;
5135 /* Peek at the next token. */
5136 token = cp_lexer_peek_token (parser->lexer);
5137 token_type = token->type;
5138 /* If it's not `.*' or `->*' there's no pointer-to-member
5140 if (token_type != CPP_DOT_STAR
5141 && token_type != CPP_DEREF_STAR)
5144 /* Consume the token. */
5145 cp_lexer_consume_token (parser->lexer);
5147 /* Parse another cast-expression. */
5148 cast_expr = cp_parser_cast_expression (parser, /*address_p=*/false);
5150 /* Build the representation of the pointer-to-member
5152 if (token_type == CPP_DEREF_STAR)
5153 pm_expr = build_x_binary_op (MEMBER_REF, pm_expr, cast_expr);
5155 pm_expr = build_m_component_ref (pm_expr, cast_expr);
5161 /* Parse a multiplicative-expression.
5163 mulitplicative-expression:
5165 multiplicative-expression * pm-expression
5166 multiplicative-expression / pm-expression
5167 multiplicative-expression % pm-expression
5169 Returns a representation of the expression. */
5172 cp_parser_multiplicative_expression (parser)
5175 static cp_parser_token_tree_map map = {
5176 { CPP_MULT, MULT_EXPR },
5177 { CPP_DIV, TRUNC_DIV_EXPR },
5178 { CPP_MOD, TRUNC_MOD_EXPR },
5179 { CPP_EOF, ERROR_MARK }
5182 return cp_parser_binary_expression (parser,
5184 cp_parser_pm_expression);
5187 /* Parse an additive-expression.
5189 additive-expression:
5190 multiplicative-expression
5191 additive-expression + multiplicative-expression
5192 additive-expression - multiplicative-expression
5194 Returns a representation of the expression. */
5197 cp_parser_additive_expression (parser)
5200 static cp_parser_token_tree_map map = {
5201 { CPP_PLUS, PLUS_EXPR },
5202 { CPP_MINUS, MINUS_EXPR },
5203 { CPP_EOF, ERROR_MARK }
5206 return cp_parser_binary_expression (parser,
5208 cp_parser_multiplicative_expression);
5211 /* Parse a shift-expression.
5215 shift-expression << additive-expression
5216 shift-expression >> additive-expression
5218 Returns a representation of the expression. */
5221 cp_parser_shift_expression (parser)
5224 static cp_parser_token_tree_map map = {
5225 { CPP_LSHIFT, LSHIFT_EXPR },
5226 { CPP_RSHIFT, RSHIFT_EXPR },
5227 { CPP_EOF, ERROR_MARK }
5230 return cp_parser_binary_expression (parser,
5232 cp_parser_additive_expression);
5235 /* Parse a relational-expression.
5237 relational-expression:
5239 relational-expression < shift-expression
5240 relational-expression > shift-expression
5241 relational-expression <= shift-expression
5242 relational-expression >= shift-expression
5246 relational-expression:
5247 relational-expression <? shift-expression
5248 relational-expression >? shift-expression
5250 Returns a representation of the expression. */
5253 cp_parser_relational_expression (parser)
5256 static cp_parser_token_tree_map map = {
5257 { CPP_LESS, LT_EXPR },
5258 { CPP_GREATER, GT_EXPR },
5259 { CPP_LESS_EQ, LE_EXPR },
5260 { CPP_GREATER_EQ, GE_EXPR },
5261 { CPP_MIN, MIN_EXPR },
5262 { CPP_MAX, MAX_EXPR },
5263 { CPP_EOF, ERROR_MARK }
5266 return cp_parser_binary_expression (parser,
5268 cp_parser_shift_expression);
5271 /* Parse an equality-expression.
5273 equality-expression:
5274 relational-expression
5275 equality-expression == relational-expression
5276 equality-expression != relational-expression
5278 Returns a representation of the expression. */
5281 cp_parser_equality_expression (parser)
5284 static cp_parser_token_tree_map map = {
5285 { CPP_EQ_EQ, EQ_EXPR },
5286 { CPP_NOT_EQ, NE_EXPR },
5287 { CPP_EOF, ERROR_MARK }
5290 return cp_parser_binary_expression (parser,
5292 cp_parser_relational_expression);
5295 /* Parse an and-expression.
5299 and-expression & equality-expression
5301 Returns a representation of the expression. */
5304 cp_parser_and_expression (parser)
5307 static cp_parser_token_tree_map map = {
5308 { CPP_AND, BIT_AND_EXPR },
5309 { CPP_EOF, ERROR_MARK }
5312 return cp_parser_binary_expression (parser,
5314 cp_parser_equality_expression);
5317 /* Parse an exclusive-or-expression.
5319 exclusive-or-expression:
5321 exclusive-or-expression ^ and-expression
5323 Returns a representation of the expression. */
5326 cp_parser_exclusive_or_expression (parser)
5329 static cp_parser_token_tree_map map = {
5330 { CPP_XOR, BIT_XOR_EXPR },
5331 { CPP_EOF, ERROR_MARK }
5334 return cp_parser_binary_expression (parser,
5336 cp_parser_and_expression);
5340 /* Parse an inclusive-or-expression.
5342 inclusive-or-expression:
5343 exclusive-or-expression
5344 inclusive-or-expression | exclusive-or-expression
5346 Returns a representation of the expression. */
5349 cp_parser_inclusive_or_expression (parser)
5352 static cp_parser_token_tree_map map = {
5353 { CPP_OR, BIT_IOR_EXPR },
5354 { CPP_EOF, ERROR_MARK }
5357 return cp_parser_binary_expression (parser,
5359 cp_parser_exclusive_or_expression);
5362 /* Parse a logical-and-expression.
5364 logical-and-expression:
5365 inclusive-or-expression
5366 logical-and-expression && inclusive-or-expression
5368 Returns a representation of the expression. */
5371 cp_parser_logical_and_expression (parser)
5374 static cp_parser_token_tree_map map = {
5375 { CPP_AND_AND, TRUTH_ANDIF_EXPR },
5376 { CPP_EOF, ERROR_MARK }
5379 return cp_parser_binary_expression (parser,
5381 cp_parser_inclusive_or_expression);
5384 /* Parse a logical-or-expression.
5386 logical-or-expression:
5387 logical-and-expresion
5388 logical-or-expression || logical-and-expression
5390 Returns a representation of the expression. */
5393 cp_parser_logical_or_expression (parser)
5396 static cp_parser_token_tree_map map = {
5397 { CPP_OR_OR, TRUTH_ORIF_EXPR },
5398 { CPP_EOF, ERROR_MARK }
5401 return cp_parser_binary_expression (parser,
5403 cp_parser_logical_and_expression);
5406 /* Parse a conditional-expression.
5408 conditional-expression:
5409 logical-or-expression
5410 logical-or-expression ? expression : assignment-expression
5414 conditional-expression:
5415 logical-or-expression ? : assignment-expression
5417 Returns a representation of the expression. */
5420 cp_parser_conditional_expression (parser)
5423 tree logical_or_expr;
5425 /* Parse the logical-or-expression. */
5426 logical_or_expr = cp_parser_logical_or_expression (parser);
5427 /* If the next token is a `?', then we have a real conditional
5429 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
5430 return cp_parser_question_colon_clause (parser, logical_or_expr);
5431 /* Otherwise, the value is simply the logical-or-expression. */
5433 return logical_or_expr;
5436 /* Parse the `? expression : assignment-expression' part of a
5437 conditional-expression. The LOGICAL_OR_EXPR is the
5438 logical-or-expression that started the conditional-expression.
5439 Returns a representation of the entire conditional-expression.
5441 This routine exists only so that it can be shared between
5442 cp_parser_conditional_expression and
5443 cp_parser_assignment_expression.
5445 ? expression : assignment-expression
5449 ? : assignment-expression */
5452 cp_parser_question_colon_clause (parser, logical_or_expr)
5454 tree logical_or_expr;
5457 tree assignment_expr;
5459 /* Consume the `?' token. */
5460 cp_lexer_consume_token (parser->lexer);
5461 if (cp_parser_allow_gnu_extensions_p (parser)
5462 && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
5463 /* Implicit true clause. */
5466 /* Parse the expression. */
5467 expr = cp_parser_expression (parser);
5469 /* The next token should be a `:'. */
5470 cp_parser_require (parser, CPP_COLON, "`:'");
5471 /* Parse the assignment-expression. */
5472 assignment_expr = cp_parser_assignment_expression (parser);
5474 /* Build the conditional-expression. */
5475 return build_x_conditional_expr (logical_or_expr,
5480 /* Parse an assignment-expression.
5482 assignment-expression:
5483 conditional-expression
5484 logical-or-expression assignment-operator assignment_expression
5487 Returns a representation for the expression. */
5490 cp_parser_assignment_expression (parser)
5495 /* If the next token is the `throw' keyword, then we're looking at
5496 a throw-expression. */
5497 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
5498 expr = cp_parser_throw_expression (parser);
5499 /* Otherwise, it must be that we are looking at a
5500 logical-or-expression. */
5503 /* Parse the logical-or-expression. */
5504 expr = cp_parser_logical_or_expression (parser);
5505 /* If the next token is a `?' then we're actually looking at a
5506 conditional-expression. */
5507 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
5508 return cp_parser_question_colon_clause (parser, expr);
5511 enum tree_code assignment_operator;
5513 /* If it's an assignment-operator, we're using the second
5516 = cp_parser_assignment_operator_opt (parser);
5517 if (assignment_operator != ERROR_MARK)
5521 /* Parse the right-hand side of the assignment. */
5522 rhs = cp_parser_assignment_expression (parser);
5523 /* Build the asignment expression. */
5524 expr = build_x_modify_expr (expr,
5525 assignment_operator,
5534 /* Parse an (optional) assignment-operator.
5536 assignment-operator: one of
5537 = *= /= %= += -= >>= <<= &= ^= |=
5541 assignment-operator: one of
5544 If the next token is an assignment operator, the corresponding tree
5545 code is returned, and the token is consumed. For example, for
5546 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
5547 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
5548 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
5549 operator, ERROR_MARK is returned. */
5551 static enum tree_code
5552 cp_parser_assignment_operator_opt (parser)
5558 /* Peek at the next toen. */
5559 token = cp_lexer_peek_token (parser->lexer);
5561 switch (token->type)
5572 op = TRUNC_DIV_EXPR;
5576 op = TRUNC_MOD_EXPR;
5616 /* Nothing else is an assignment operator. */
5620 /* If it was an assignment operator, consume it. */
5621 if (op != ERROR_MARK)
5622 cp_lexer_consume_token (parser->lexer);
5627 /* Parse an expression.
5630 assignment-expression
5631 expression , assignment-expression
5633 Returns a representation of the expression. */
5636 cp_parser_expression (parser)
5639 tree expression = NULL_TREE;
5640 bool saw_comma_p = false;
5644 tree assignment_expression;
5646 /* Parse the next assignment-expression. */
5647 assignment_expression
5648 = cp_parser_assignment_expression (parser);
5649 /* If this is the first assignment-expression, we can just
5652 expression = assignment_expression;
5653 /* Otherwise, chain the expressions together. It is unclear why
5654 we do not simply build COMPOUND_EXPRs as we go. */
5656 expression = tree_cons (NULL_TREE,
5657 assignment_expression,
5659 /* If the next token is not a comma, then we are done with the
5661 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5663 /* Consume the `,'. */
5664 cp_lexer_consume_token (parser->lexer);
5665 /* The first time we see a `,', we must take special action
5666 because the representation used for a single expression is
5667 different from that used for a list containing the single
5671 /* Remember that this expression has a `,' in it. */
5673 /* Turn the EXPRESSION into a TREE_LIST so that we can link
5674 additional expressions to it. */
5675 expression = build_tree_list (NULL_TREE, expression);
5679 /* Build a COMPOUND_EXPR to represent the entire expression, if
5680 necessary. We built up the list in reverse order, so we must
5681 straighten it out here. */
5683 expression = build_x_compound_expr (nreverse (expression));
5688 /* Parse a constant-expression.
5690 constant-expression:
5691 conditional-expression */
5694 cp_parser_constant_expression (parser)
5697 bool saved_constant_expression_p;
5700 /* It might seem that we could simply parse the
5701 conditional-expression, and then check to see if it were
5702 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
5703 one that the compiler can figure out is constant, possibly after
5704 doing some simplifications or optimizations. The standard has a
5705 precise definition of constant-expression, and we must honor
5706 that, even though it is somewhat more restrictive.
5712 is not a legal declaration, because `(2, 3)' is not a
5713 constant-expression. The `,' operator is forbidden in a
5714 constant-expression. However, GCC's constant-folding machinery
5715 will fold this operation to an INTEGER_CST for `3'. */
5717 /* Save the old setting of CONSTANT_EXPRESSION_P. */
5718 saved_constant_expression_p = parser->constant_expression_p;
5719 /* We are now parsing a constant-expression. */
5720 parser->constant_expression_p = true;
5721 /* Parse the conditional-expression. */
5722 expression = cp_parser_conditional_expression (parser);
5723 /* Restore the old setting of CONSTANT_EXPRESSION_P. */
5724 parser->constant_expression_p = saved_constant_expression_p;
5729 /* Statements [gram.stmt.stmt] */
5731 /* Parse a statement.
5735 expression-statement
5740 declaration-statement
5744 cp_parser_statement (parser)
5749 int statement_line_number;
5751 /* There is no statement yet. */
5752 statement = NULL_TREE;
5753 /* Peek at the next token. */
5754 token = cp_lexer_peek_token (parser->lexer);
5755 /* Remember the line number of the first token in the statement. */
5756 statement_line_number = token->line_number;
5757 /* If this is a keyword, then that will often determine what kind of
5758 statement we have. */
5759 if (token->type == CPP_KEYWORD)
5761 enum rid keyword = token->keyword;
5767 statement = cp_parser_labeled_statement (parser);
5772 statement = cp_parser_selection_statement (parser);
5778 statement = cp_parser_iteration_statement (parser);
5785 statement = cp_parser_jump_statement (parser);
5789 statement = cp_parser_try_block (parser);
5793 /* It might be a keyword like `int' that can start a
5794 declaration-statement. */
5798 else if (token->type == CPP_NAME)
5800 /* If the next token is a `:', then we are looking at a
5801 labeled-statement. */
5802 token = cp_lexer_peek_nth_token (parser->lexer, 2);
5803 if (token->type == CPP_COLON)
5804 statement = cp_parser_labeled_statement (parser);
5806 /* Anything that starts with a `{' must be a compound-statement. */
5807 else if (token->type == CPP_OPEN_BRACE)
5808 statement = cp_parser_compound_statement (parser);
5810 /* Everything else must be a declaration-statement or an
5811 expression-statement. Try for the declaration-statement
5812 first, unless we are looking at a `;', in which case we know that
5813 we have an expression-statement. */
5816 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
5818 cp_parser_parse_tentatively (parser);
5819 /* Try to parse the declaration-statement. */
5820 cp_parser_declaration_statement (parser);
5821 /* If that worked, we're done. */
5822 if (cp_parser_parse_definitely (parser))
5825 /* Look for an expression-statement instead. */
5826 statement = cp_parser_expression_statement (parser);
5829 /* Set the line number for the statement. */
5830 if (statement && statement_code_p (TREE_CODE (statement)))
5831 STMT_LINENO (statement) = statement_line_number;
5834 /* Parse a labeled-statement.
5837 identifier : statement
5838 case constant-expression : statement
5841 Returns the new CASE_LABEL, for a `case' or `default' label. For
5842 an ordinary label, returns a LABEL_STMT. */
5845 cp_parser_labeled_statement (parser)
5849 tree statement = NULL_TREE;
5851 /* The next token should be an identifier. */
5852 token = cp_lexer_peek_token (parser->lexer);
5853 if (token->type != CPP_NAME
5854 && token->type != CPP_KEYWORD)
5856 cp_parser_error (parser, "expected labeled-statement");
5857 return error_mark_node;
5860 switch (token->keyword)
5866 /* Consume the `case' token. */
5867 cp_lexer_consume_token (parser->lexer);
5868 /* Parse the constant-expression. */
5869 expr = cp_parser_constant_expression (parser);
5870 /* Create the label. */
5871 statement = finish_case_label (expr, NULL_TREE);
5876 /* Consume the `default' token. */
5877 cp_lexer_consume_token (parser->lexer);
5878 /* Create the label. */
5879 statement = finish_case_label (NULL_TREE, NULL_TREE);
5883 /* Anything else must be an ordinary label. */
5884 statement = finish_label_stmt (cp_parser_identifier (parser));
5888 /* Require the `:' token. */
5889 cp_parser_require (parser, CPP_COLON, "`:'");
5890 /* Parse the labeled statement. */
5891 cp_parser_statement (parser);
5893 /* Return the label, in the case of a `case' or `default' label. */
5897 /* Parse an expression-statement.
5899 expression-statement:
5902 Returns the new EXPR_STMT -- or NULL_TREE if the expression
5903 statement consists of nothing more than an `;'. */
5906 cp_parser_expression_statement (parser)
5911 /* If the next token is not a `;', then there is an expression to parse. */
5912 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
5913 statement = finish_expr_stmt (cp_parser_expression (parser));
5914 /* Otherwise, we do not even bother to build an EXPR_STMT. */
5918 statement = NULL_TREE;
5920 /* Consume the final `;'. */
5921 if (!cp_parser_require (parser, CPP_SEMICOLON, "`;'"))
5923 /* If there is additional (erroneous) input, skip to the end of
5925 cp_parser_skip_to_end_of_statement (parser);
5926 /* If the next token is now a `;', consume it. */
5927 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
5928 cp_lexer_consume_token (parser->lexer);
5934 /* Parse a compound-statement.
5937 { statement-seq [opt] }
5939 Returns a COMPOUND_STMT representing the statement. */
5942 cp_parser_compound_statement (cp_parser *parser)
5946 /* Consume the `{'. */
5947 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
5948 return error_mark_node;
5949 /* Begin the compound-statement. */
5950 compound_stmt = begin_compound_stmt (/*has_no_scope=*/0);
5951 /* Parse an (optional) statement-seq. */
5952 cp_parser_statement_seq_opt (parser);
5953 /* Finish the compound-statement. */
5954 finish_compound_stmt (/*has_no_scope=*/0, compound_stmt);
5955 /* Consume the `}'. */
5956 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
5958 return compound_stmt;
5961 /* Parse an (optional) statement-seq.
5965 statement-seq [opt] statement */
5968 cp_parser_statement_seq_opt (parser)
5971 /* Scan statements until there aren't any more. */
5974 /* If we're looking at a `}', then we've run out of statements. */
5975 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE)
5976 || cp_lexer_next_token_is (parser->lexer, CPP_EOF))
5979 /* Parse the statement. */
5980 cp_parser_statement (parser);
5984 /* Parse a selection-statement.
5986 selection-statement:
5987 if ( condition ) statement
5988 if ( condition ) statement else statement
5989 switch ( condition ) statement
5991 Returns the new IF_STMT or SWITCH_STMT. */
5994 cp_parser_selection_statement (parser)
6000 /* Peek at the next token. */
6001 token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
6003 /* See what kind of keyword it is. */
6004 keyword = token->keyword;
6013 /* Look for the `('. */
6014 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
6016 cp_parser_skip_to_end_of_statement (parser);
6017 return error_mark_node;
6020 /* Begin the selection-statement. */
6021 if (keyword == RID_IF)
6022 statement = begin_if_stmt ();
6024 statement = begin_switch_stmt ();
6026 /* Parse the condition. */
6027 condition = cp_parser_condition (parser);
6028 /* Look for the `)'. */
6029 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
6030 cp_parser_skip_to_closing_parenthesis (parser);
6032 if (keyword == RID_IF)
6036 /* Add the condition. */
6037 finish_if_stmt_cond (condition, statement);
6039 /* Parse the then-clause. */
6040 then_stmt = cp_parser_implicitly_scoped_statement (parser);
6041 finish_then_clause (statement);
6043 /* If the next token is `else', parse the else-clause. */
6044 if (cp_lexer_next_token_is_keyword (parser->lexer,
6049 /* Consume the `else' keyword. */
6050 cp_lexer_consume_token (parser->lexer);
6051 /* Parse the else-clause. */
6053 = cp_parser_implicitly_scoped_statement (parser);
6054 finish_else_clause (statement);
6057 /* Now we're all done with the if-statement. */
6064 /* Add the condition. */
6065 finish_switch_cond (condition, statement);
6067 /* Parse the body of the switch-statement. */
6068 body = cp_parser_implicitly_scoped_statement (parser);
6070 /* Now we're all done with the switch-statement. */
6071 finish_switch_stmt (statement);
6079 cp_parser_error (parser, "expected selection-statement");
6080 return error_mark_node;
6084 /* Parse a condition.
6088 type-specifier-seq declarator = assignment-expression
6093 type-specifier-seq declarator asm-specification [opt]
6094 attributes [opt] = assignment-expression
6096 Returns the expression that should be tested. */
6099 cp_parser_condition (parser)
6102 tree type_specifiers;
6103 const char *saved_message;
6105 /* Try the declaration first. */
6106 cp_parser_parse_tentatively (parser);
6107 /* New types are not allowed in the type-specifier-seq for a
6109 saved_message = parser->type_definition_forbidden_message;
6110 parser->type_definition_forbidden_message
6111 = "types may not be defined in conditions";
6112 /* Parse the type-specifier-seq. */
6113 type_specifiers = cp_parser_type_specifier_seq (parser);
6114 /* Restore the saved message. */
6115 parser->type_definition_forbidden_message = saved_message;
6116 /* If all is well, we might be looking at a declaration. */
6117 if (!cp_parser_error_occurred (parser))
6120 tree asm_specification;
6123 tree initializer = NULL_TREE;
6125 /* Parse the declarator. */
6126 declarator = cp_parser_declarator (parser,
6127 /*abstract_p=*/false,
6128 /*ctor_dtor_or_conv_p=*/NULL);
6129 /* Parse the attributes. */
6130 attributes = cp_parser_attributes_opt (parser);
6131 /* Parse the asm-specification. */
6132 asm_specification = cp_parser_asm_specification_opt (parser);
6133 /* If the next token is not an `=', then we might still be
6134 looking at an expression. For example:
6138 looks like a decl-specifier-seq and a declarator -- but then
6139 there is no `=', so this is an expression. */
6140 cp_parser_require (parser, CPP_EQ, "`='");
6141 /* If we did see an `=', then we are looking at a declaration
6143 if (cp_parser_parse_definitely (parser))
6145 /* Create the declaration. */
6146 decl = start_decl (declarator, type_specifiers,
6147 /*initialized_p=*/true,
6148 attributes, /*prefix_attributes=*/NULL_TREE);
6149 /* Parse the assignment-expression. */
6150 initializer = cp_parser_assignment_expression (parser);
6152 /* Process the initializer. */
6153 cp_finish_decl (decl,
6156 LOOKUP_ONLYCONVERTING);
6158 return convert_from_reference (decl);
6161 /* If we didn't even get past the declarator successfully, we are
6162 definitely not looking at a declaration. */
6164 cp_parser_abort_tentative_parse (parser);
6166 /* Otherwise, we are looking at an expression. */
6167 return cp_parser_expression (parser);
6170 /* Parse an iteration-statement.
6172 iteration-statement:
6173 while ( condition ) statement
6174 do statement while ( expression ) ;
6175 for ( for-init-statement condition [opt] ; expression [opt] )
6178 Returns the new WHILE_STMT, DO_STMT, or FOR_STMT. */
6181 cp_parser_iteration_statement (parser)
6188 /* Peek at the next token. */
6189 token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
6191 return error_mark_node;
6193 /* See what kind of keyword it is. */
6194 keyword = token->keyword;
6201 /* Begin the while-statement. */
6202 statement = begin_while_stmt ();
6203 /* Look for the `('. */
6204 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6205 /* Parse the condition. */
6206 condition = cp_parser_condition (parser);
6207 finish_while_stmt_cond (condition, statement);
6208 /* Look for the `)'. */
6209 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6210 /* Parse the dependent statement. */
6211 cp_parser_already_scoped_statement (parser);
6212 /* We're done with the while-statement. */
6213 finish_while_stmt (statement);
6221 /* Begin the do-statement. */
6222 statement = begin_do_stmt ();
6223 /* Parse the body of the do-statement. */
6224 cp_parser_implicitly_scoped_statement (parser);
6225 finish_do_body (statement);
6226 /* Look for the `while' keyword. */
6227 cp_parser_require_keyword (parser, RID_WHILE, "`while'");
6228 /* Look for the `('. */
6229 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6230 /* Parse the expression. */
6231 expression = cp_parser_expression (parser);
6232 /* We're done with the do-statement. */
6233 finish_do_stmt (expression, statement);
6234 /* Look for the `)'. */
6235 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6236 /* Look for the `;'. */
6237 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6243 tree condition = NULL_TREE;
6244 tree expression = NULL_TREE;
6246 /* Begin the for-statement. */
6247 statement = begin_for_stmt ();
6248 /* Look for the `('. */
6249 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6250 /* Parse the initialization. */
6251 cp_parser_for_init_statement (parser);
6252 finish_for_init_stmt (statement);
6254 /* If there's a condition, process it. */
6255 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6256 condition = cp_parser_condition (parser);
6257 finish_for_cond (condition, statement);
6258 /* Look for the `;'. */
6259 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6261 /* If there's an expression, process it. */
6262 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
6263 expression = cp_parser_expression (parser);
6264 finish_for_expr (expression, statement);
6265 /* Look for the `)'. */
6266 cp_parser_require (parser, CPP_CLOSE_PAREN, "`;'");
6268 /* Parse the body of the for-statement. */
6269 cp_parser_already_scoped_statement (parser);
6271 /* We're done with the for-statement. */
6272 finish_for_stmt (statement);
6277 cp_parser_error (parser, "expected iteration-statement");
6278 statement = error_mark_node;
6285 /* Parse a for-init-statement.
6288 expression-statement
6289 simple-declaration */
6292 cp_parser_for_init_statement (parser)
6295 /* If the next token is a `;', then we have an empty
6296 expression-statement. Gramatically, this is also a
6297 simple-declaration, but an invalid one, because it does not
6298 declare anything. Therefore, if we did not handle this case
6299 specially, we would issue an error message about an invalid
6301 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6303 /* We're going to speculatively look for a declaration, falling back
6304 to an expression, if necessary. */
6305 cp_parser_parse_tentatively (parser);
6306 /* Parse the declaration. */
6307 cp_parser_simple_declaration (parser,
6308 /*function_definition_allowed_p=*/false);
6309 /* If the tentative parse failed, then we shall need to look for an
6310 expression-statement. */
6311 if (cp_parser_parse_definitely (parser))
6315 cp_parser_expression_statement (parser);
6318 /* Parse a jump-statement.
6323 return expression [opt] ;
6331 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_STMT, or
6335 cp_parser_jump_statement (parser)
6338 tree statement = error_mark_node;
6342 /* Peek at the next token. */
6343 token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
6345 return error_mark_node;
6347 /* See what kind of keyword it is. */
6348 keyword = token->keyword;
6352 statement = finish_break_stmt ();
6353 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6357 statement = finish_continue_stmt ();
6358 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6365 /* If the next token is a `;', then there is no
6367 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6368 expr = cp_parser_expression (parser);
6371 /* Build the return-statement. */
6372 statement = finish_return_stmt (expr);
6373 /* Look for the final `;'. */
6374 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6379 /* Create the goto-statement. */
6380 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
6382 /* Issue a warning about this use of a GNU extension. */
6384 pedwarn ("ISO C++ forbids computed gotos");
6385 /* Consume the '*' token. */
6386 cp_lexer_consume_token (parser->lexer);
6387 /* Parse the dependent expression. */
6388 finish_goto_stmt (cp_parser_expression (parser));
6391 finish_goto_stmt (cp_parser_identifier (parser));
6392 /* Look for the final `;'. */
6393 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6397 cp_parser_error (parser, "expected jump-statement");
6404 /* Parse a declaration-statement.
6406 declaration-statement:
6407 block-declaration */
6410 cp_parser_declaration_statement (parser)
6413 /* Parse the block-declaration. */
6414 cp_parser_block_declaration (parser, /*statement_p=*/true);
6416 /* Finish off the statement. */
6420 /* Some dependent statements (like `if (cond) statement'), are
6421 implicitly in their own scope. In other words, if the statement is
6422 a single statement (as opposed to a compound-statement), it is
6423 none-the-less treated as if it were enclosed in braces. Any
6424 declarations appearing in the dependent statement are out of scope
6425 after control passes that point. This function parses a statement,
6426 but ensures that is in its own scope, even if it is not a
6429 Returns the new statement. */
6432 cp_parser_implicitly_scoped_statement (parser)
6437 /* If the token is not a `{', then we must take special action. */
6438 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
6440 /* Create a compound-statement. */
6441 statement = begin_compound_stmt (/*has_no_scope=*/0);
6442 /* Parse the dependent-statement. */
6443 cp_parser_statement (parser);
6444 /* Finish the dummy compound-statement. */
6445 finish_compound_stmt (/*has_no_scope=*/0, statement);
6447 /* Otherwise, we simply parse the statement directly. */
6449 statement = cp_parser_compound_statement (parser);
6451 /* Return the statement. */
6455 /* For some dependent statements (like `while (cond) statement'), we
6456 have already created a scope. Therefore, even if the dependent
6457 statement is a compound-statement, we do not want to create another
6461 cp_parser_already_scoped_statement (parser)
6464 /* If the token is not a `{', then we must take special action. */
6465 if (cp_lexer_next_token_is_not(parser->lexer, CPP_OPEN_BRACE))
6469 /* Create a compound-statement. */
6470 statement = begin_compound_stmt (/*has_no_scope=*/1);
6471 /* Parse the dependent-statement. */
6472 cp_parser_statement (parser);
6473 /* Finish the dummy compound-statement. */
6474 finish_compound_stmt (/*has_no_scope=*/1, statement);
6476 /* Otherwise, we simply parse the statement directly. */
6478 cp_parser_statement (parser);
6481 /* Declarations [gram.dcl.dcl] */
6483 /* Parse an optional declaration-sequence.
6487 declaration-seq declaration */
6490 cp_parser_declaration_seq_opt (parser)
6497 token = cp_lexer_peek_token (parser->lexer);
6499 if (token->type == CPP_CLOSE_BRACE
6500 || token->type == CPP_EOF)
6503 if (token->type == CPP_SEMICOLON)
6505 /* A declaration consisting of a single semicolon is
6506 invalid. Allow it unless we're being pedantic. */
6508 pedwarn ("extra `;'");
6509 cp_lexer_consume_token (parser->lexer);
6513 /* The C lexer modifies PENDING_LANG_CHANGE when it wants the
6514 parser to enter or exit implict `extern "C"' blocks. */
6515 while (pending_lang_change > 0)
6517 push_lang_context (lang_name_c);
6518 --pending_lang_change;
6520 while (pending_lang_change < 0)
6522 pop_lang_context ();
6523 ++pending_lang_change;
6526 /* Parse the declaration itself. */
6527 cp_parser_declaration (parser);
6531 /* Parse a declaration.
6536 template-declaration
6537 explicit-instantiation
6538 explicit-specialization
6539 linkage-specification
6540 namespace-definition
6545 __extension__ declaration */
6548 cp_parser_declaration (parser)
6555 /* Check for the `__extension__' keyword. */
6556 if (cp_parser_extension_opt (parser, &saved_pedantic))
6558 /* Parse the qualified declaration. */
6559 cp_parser_declaration (parser);
6560 /* Restore the PEDANTIC flag. */
6561 pedantic = saved_pedantic;
6566 /* Try to figure out what kind of declaration is present. */
6567 token1 = *cp_lexer_peek_token (parser->lexer);
6568 if (token1.type != CPP_EOF)
6569 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
6571 /* If the next token is `extern' and the following token is a string
6572 literal, then we have a linkage specification. */
6573 if (token1.keyword == RID_EXTERN
6574 && cp_parser_is_string_literal (&token2))
6575 cp_parser_linkage_specification (parser);
6576 /* If the next token is `template', then we have either a template
6577 declaration, an explicit instantiation, or an explicit
6579 else if (token1.keyword == RID_TEMPLATE)
6581 /* `template <>' indicates a template specialization. */
6582 if (token2.type == CPP_LESS
6583 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
6584 cp_parser_explicit_specialization (parser);
6585 /* `template <' indicates a template declaration. */
6586 else if (token2.type == CPP_LESS)
6587 cp_parser_template_declaration (parser, /*member_p=*/false);
6588 /* Anything else must be an explicit instantiation. */
6590 cp_parser_explicit_instantiation (parser);
6592 /* If the next token is `export', then we have a template
6594 else if (token1.keyword == RID_EXPORT)
6595 cp_parser_template_declaration (parser, /*member_p=*/false);
6596 /* If the next token is `extern', 'static' or 'inline' and the one
6597 after that is `template', we have a GNU extended explicit
6598 instantiation directive. */
6599 else if (cp_parser_allow_gnu_extensions_p (parser)
6600 && (token1.keyword == RID_EXTERN
6601 || token1.keyword == RID_STATIC
6602 || token1.keyword == RID_INLINE)
6603 && token2.keyword == RID_TEMPLATE)
6604 cp_parser_explicit_instantiation (parser);
6605 /* If the next token is `namespace', check for a named or unnamed
6606 namespace definition. */
6607 else if (token1.keyword == RID_NAMESPACE
6608 && (/* A named namespace definition. */
6609 (token2.type == CPP_NAME
6610 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
6612 /* An unnamed namespace definition. */
6613 || token2.type == CPP_OPEN_BRACE))
6614 cp_parser_namespace_definition (parser);
6615 /* We must have either a block declaration or a function
6618 /* Try to parse a block-declaration, or a function-definition. */
6619 cp_parser_block_declaration (parser, /*statement_p=*/false);
6622 /* Parse a block-declaration.
6627 namespace-alias-definition
6634 __extension__ block-declaration
6637 If STATEMENT_P is TRUE, then this block-declaration is ocurring as
6638 part of a declaration-statement. */
6641 cp_parser_block_declaration (cp_parser *parser,
6647 /* Check for the `__extension__' keyword. */
6648 if (cp_parser_extension_opt (parser, &saved_pedantic))
6650 /* Parse the qualified declaration. */
6651 cp_parser_block_declaration (parser, statement_p);
6652 /* Restore the PEDANTIC flag. */
6653 pedantic = saved_pedantic;
6658 /* Peek at the next token to figure out which kind of declaration is
6660 token1 = cp_lexer_peek_token (parser->lexer);
6662 /* If the next keyword is `asm', we have an asm-definition. */
6663 if (token1->keyword == RID_ASM)
6666 cp_parser_commit_to_tentative_parse (parser);
6667 cp_parser_asm_definition (parser);
6669 /* If the next keyword is `namespace', we have a
6670 namespace-alias-definition. */
6671 else if (token1->keyword == RID_NAMESPACE)
6672 cp_parser_namespace_alias_definition (parser);
6673 /* If the next keyword is `using', we have either a
6674 using-declaration or a using-directive. */
6675 else if (token1->keyword == RID_USING)
6680 cp_parser_commit_to_tentative_parse (parser);
6681 /* If the token after `using' is `namespace', then we have a
6683 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
6684 if (token2->keyword == RID_NAMESPACE)
6685 cp_parser_using_directive (parser);
6686 /* Otherwise, it's a using-declaration. */
6688 cp_parser_using_declaration (parser);
6690 /* If the next keyword is `__label__' we have a label declaration. */
6691 else if (token1->keyword == RID_LABEL)
6694 cp_parser_commit_to_tentative_parse (parser);
6695 cp_parser_label_declaration (parser);
6697 /* Anything else must be a simple-declaration. */
6699 cp_parser_simple_declaration (parser, !statement_p);
6702 /* Parse a simple-declaration.
6705 decl-specifier-seq [opt] init-declarator-list [opt] ;
6707 init-declarator-list:
6709 init-declarator-list , init-declarator
6711 If FUNCTION_DEFINTION_ALLOWED_P is TRUE, then we also recognize a
6712 function-definition as a simple-declaration. */
6715 cp_parser_simple_declaration (parser, function_definition_allowed_p)
6717 bool function_definition_allowed_p;
6719 tree decl_specifiers;
6722 bool declares_class_or_enum;
6723 bool saw_declarator;
6725 /* Defer access checks until we know what is being declared; the
6726 checks for names appearing in the decl-specifier-seq should be
6727 done as if we were in the scope of the thing being declared. */
6728 cp_parser_start_deferring_access_checks (parser);
6729 /* Parse the decl-specifier-seq. We have to keep track of whether
6730 or not the decl-specifier-seq declares a named class or
6731 enumeration type, since that is the only case in which the
6732 init-declarator-list is allowed to be empty.
6736 In a simple-declaration, the optional init-declarator-list can be
6737 omitted only when declaring a class or enumeration, that is when
6738 the decl-specifier-seq contains either a class-specifier, an
6739 elaborated-type-specifier, or an enum-specifier. */
6741 = cp_parser_decl_specifier_seq (parser,
6742 CP_PARSER_FLAGS_OPTIONAL,
6744 &declares_class_or_enum);
6745 /* We no longer need to defer access checks. */
6746 access_checks = cp_parser_stop_deferring_access_checks (parser);
6748 /* Prevent access checks from being reclaimed by GC. */
6749 parser->access_checks_lists = tree_cons (NULL_TREE, access_checks,
6750 parser->access_checks_lists);
6752 /* Keep going until we hit the `;' at the end of the simple
6754 saw_declarator = false;
6755 while (cp_lexer_next_token_is_not (parser->lexer,
6759 bool function_definition_p;
6761 saw_declarator = true;
6762 /* Parse the init-declarator. */
6763 cp_parser_init_declarator (parser, decl_specifiers, attributes,
6765 function_definition_allowed_p,
6767 &function_definition_p);
6768 /* Handle function definitions specially. */
6769 if (function_definition_p)
6771 /* If the next token is a `,', then we are probably
6772 processing something like:
6776 which is erroneous. */
6777 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
6778 error ("mixing declarations and function-definitions is forbidden");
6779 /* Otherwise, we're done with the list of declarators. */
6782 /* Discard access checks no longer in use. */
6783 parser->access_checks_lists
6784 = TREE_CHAIN (parser->access_checks_lists);
6788 /* The next token should be either a `,' or a `;'. */
6789 token = cp_lexer_peek_token (parser->lexer);
6790 /* If it's a `,', there are more declarators to come. */
6791 if (token->type == CPP_COMMA)
6792 cp_lexer_consume_token (parser->lexer);
6793 /* If it's a `;', we are done. */
6794 else if (token->type == CPP_SEMICOLON)
6796 /* Anything else is an error. */
6799 cp_parser_error (parser, "expected `,' or `;'");
6800 /* Skip tokens until we reach the end of the statement. */
6801 cp_parser_skip_to_end_of_statement (parser);
6802 /* Discard access checks no longer in use. */
6803 parser->access_checks_lists
6804 = TREE_CHAIN (parser->access_checks_lists);
6807 /* After the first time around, a function-definition is not
6808 allowed -- even if it was OK at first. For example:
6813 function_definition_allowed_p = false;
6816 /* Issue an error message if no declarators are present, and the
6817 decl-specifier-seq does not itself declare a class or
6819 if (!saw_declarator)
6821 if (cp_parser_declares_only_class_p (parser))
6822 shadow_tag (decl_specifiers);
6823 /* Perform any deferred access checks. */
6824 cp_parser_perform_deferred_access_checks (access_checks);
6827 /* Consume the `;'. */
6828 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6830 /* Mark all the classes that appeared in the decl-specifier-seq as
6831 having received a `;'. */
6832 note_list_got_semicolon (decl_specifiers);
6834 /* Discard access checks no longer in use. */
6835 parser->access_checks_lists = TREE_CHAIN (parser->access_checks_lists);
6838 /* Parse a decl-specifier-seq.
6841 decl-specifier-seq [opt] decl-specifier
6844 storage-class-specifier
6853 decl-specifier-seq [opt] attributes
6855 Returns a TREE_LIST, giving the decl-specifiers in the order they
6856 appear in the source code. The TREE_VALUE of each node is the
6857 decl-specifier. For a keyword (such as `auto' or `friend'), the
6858 TREE_VALUE is simply the correspoding TREE_IDENTIFIER. For the
6859 representation of a type-specifier, see cp_parser_type_specifier.
6861 If there are attributes, they will be stored in *ATTRIBUTES,
6862 represented as described above cp_parser_attributes.
6864 If FRIEND_IS_NOT_CLASS_P is non-NULL, and the `friend' specifier
6865 appears, and the entity that will be a friend is not going to be a
6866 class, then *FRIEND_IS_NOT_CLASS_P will be set to TRUE. Note that
6867 even if *FRIEND_IS_NOT_CLASS_P is FALSE, the entity to which
6868 friendship is granted might not be a class. */
6871 cp_parser_decl_specifier_seq (parser, flags, attributes,
6872 declares_class_or_enum)
6874 cp_parser_flags flags;
6876 bool *declares_class_or_enum;
6878 tree decl_specs = NULL_TREE;
6879 bool friend_p = false;
6881 /* Assume no class or enumeration type is declared. */
6882 *declares_class_or_enum = false;
6884 /* Assume there are no attributes. */
6885 *attributes = NULL_TREE;
6887 /* Keep reading specifiers until there are no more to read. */
6890 tree decl_spec = NULL_TREE;
6894 /* Peek at the next token. */
6895 token = cp_lexer_peek_token (parser->lexer);
6896 /* Handle attributes. */
6897 if (token->keyword == RID_ATTRIBUTE)
6899 /* Parse the attributes. */
6900 decl_spec = cp_parser_attributes_opt (parser);
6901 /* Add them to the list. */
6902 *attributes = chainon (*attributes, decl_spec);
6905 /* If the next token is an appropriate keyword, we can simply
6906 add it to the list. */
6907 switch (token->keyword)
6913 /* The representation of the specifier is simply the
6914 appropriate TREE_IDENTIFIER node. */
6915 decl_spec = token->value;
6916 /* Consume the token. */
6917 cp_lexer_consume_token (parser->lexer);
6920 /* function-specifier:
6927 decl_spec = cp_parser_function_specifier_opt (parser);
6933 /* The representation of the specifier is simply the
6934 appropriate TREE_IDENTIFIER node. */
6935 decl_spec = token->value;
6936 /* Consume the token. */
6937 cp_lexer_consume_token (parser->lexer);
6940 /* storage-class-specifier:
6955 decl_spec = cp_parser_storage_class_specifier_opt (parser);
6962 /* Constructors are a special case. The `S' in `S()' is not a
6963 decl-specifier; it is the beginning of the declarator. */
6964 constructor_p = (!decl_spec
6965 && cp_parser_constructor_declarator_p (parser,
6968 /* If we don't have a DECL_SPEC yet, then we must be looking at
6969 a type-specifier. */
6970 if (!decl_spec && !constructor_p)
6972 bool decl_spec_declares_class_or_enum;
6973 bool is_cv_qualifier;
6976 = cp_parser_type_specifier (parser, flags,
6978 /*is_declaration=*/true,
6979 &decl_spec_declares_class_or_enum,
6982 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
6984 /* If this type-specifier referenced a user-defined type
6985 (a typedef, class-name, etc.), then we can't allow any
6986 more such type-specifiers henceforth.
6990 The longest sequence of decl-specifiers that could
6991 possibly be a type name is taken as the
6992 decl-specifier-seq of a declaration. The sequence shall
6993 be self-consistent as described below.
6997 As a general rule, at most one type-specifier is allowed
6998 in the complete decl-specifier-seq of a declaration. The
6999 only exceptions are the following:
7001 -- const or volatile can be combined with any other
7004 -- signed or unsigned can be combined with char, long,
7012 void g (const int Pc);
7014 Here, Pc is *not* part of the decl-specifier seq; it's
7015 the declarator. Therefore, once we see a type-specifier
7016 (other than a cv-qualifier), we forbid any additional
7017 user-defined types. We *do* still allow things like `int
7018 int' to be considered a decl-specifier-seq, and issue the
7019 error message later. */
7020 if (decl_spec && !is_cv_qualifier)
7021 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
7024 /* If we still do not have a DECL_SPEC, then there are no more
7028 /* Issue an error message, unless the entire construct was
7030 if (!(flags & CP_PARSER_FLAGS_OPTIONAL))
7032 cp_parser_error (parser, "expected decl specifier");
7033 return error_mark_node;
7039 /* Add the DECL_SPEC to the list of specifiers. */
7040 decl_specs = tree_cons (NULL_TREE, decl_spec, decl_specs);
7042 /* After we see one decl-specifier, further decl-specifiers are
7044 flags |= CP_PARSER_FLAGS_OPTIONAL;
7047 /* We have built up the DECL_SPECS in reverse order. Return them in
7048 the correct order. */
7049 return nreverse (decl_specs);
7052 /* Parse an (optional) storage-class-specifier.
7054 storage-class-specifier:
7063 storage-class-specifier:
7066 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
7069 cp_parser_storage_class_specifier_opt (parser)
7072 switch (cp_lexer_peek_token (parser->lexer)->keyword)
7080 /* Consume the token. */
7081 return cp_lexer_consume_token (parser->lexer)->value;
7088 /* Parse an (optional) function-specifier.
7095 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
7098 cp_parser_function_specifier_opt (parser)
7101 switch (cp_lexer_peek_token (parser->lexer)->keyword)
7106 /* Consume the token. */
7107 return cp_lexer_consume_token (parser->lexer)->value;
7114 /* Parse a linkage-specification.
7116 linkage-specification:
7117 extern string-literal { declaration-seq [opt] }
7118 extern string-literal declaration */
7121 cp_parser_linkage_specification (parser)
7127 /* Look for the `extern' keyword. */
7128 cp_parser_require_keyword (parser, RID_EXTERN, "`extern'");
7130 /* Peek at the next token. */
7131 token = cp_lexer_peek_token (parser->lexer);
7132 /* If it's not a string-literal, then there's a problem. */
7133 if (!cp_parser_is_string_literal (token))
7135 cp_parser_error (parser, "expected language-name");
7138 /* Consume the token. */
7139 cp_lexer_consume_token (parser->lexer);
7141 /* Transform the literal into an identifier. If the literal is a
7142 wide-character string, or contains embedded NULs, then we can't
7143 handle it as the user wants. */
7144 if (token->type == CPP_WSTRING
7145 || (strlen (TREE_STRING_POINTER (token->value))
7146 != (size_t) (TREE_STRING_LENGTH (token->value) - 1)))
7148 cp_parser_error (parser, "invalid linkage-specification");
7149 /* Assume C++ linkage. */
7150 linkage = get_identifier ("c++");
7152 /* If it's a simple string constant, things are easier. */
7154 linkage = get_identifier (TREE_STRING_POINTER (token->value));
7156 /* We're now using the new linkage. */
7157 push_lang_context (linkage);
7159 /* If the next token is a `{', then we're using the first
7161 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7163 /* Consume the `{' token. */
7164 cp_lexer_consume_token (parser->lexer);
7165 /* Parse the declarations. */
7166 cp_parser_declaration_seq_opt (parser);
7167 /* Look for the closing `}'. */
7168 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
7170 /* Otherwise, there's just one declaration. */
7173 bool saved_in_unbraced_linkage_specification_p;
7175 saved_in_unbraced_linkage_specification_p
7176 = parser->in_unbraced_linkage_specification_p;
7177 parser->in_unbraced_linkage_specification_p = true;
7178 have_extern_spec = true;
7179 cp_parser_declaration (parser);
7180 have_extern_spec = false;
7181 parser->in_unbraced_linkage_specification_p
7182 = saved_in_unbraced_linkage_specification_p;
7185 /* We're done with the linkage-specification. */
7186 pop_lang_context ();
7189 /* Special member functions [gram.special] */
7191 /* Parse a conversion-function-id.
7193 conversion-function-id:
7194 operator conversion-type-id
7196 Returns an IDENTIFIER_NODE representing the operator. */
7199 cp_parser_conversion_function_id (parser)
7204 tree saved_qualifying_scope;
7205 tree saved_object_scope;
7207 /* Look for the `operator' token. */
7208 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
7209 return error_mark_node;
7210 /* When we parse the conversion-type-id, the current scope will be
7211 reset. However, we need that information in able to look up the
7212 conversion function later, so we save it here. */
7213 saved_scope = parser->scope;
7214 saved_qualifying_scope = parser->qualifying_scope;
7215 saved_object_scope = parser->object_scope;
7216 /* We must enter the scope of the class so that the names of
7217 entities declared within the class are available in the
7218 conversion-type-id. For example, consider:
7225 S::operator I() { ... }
7227 In order to see that `I' is a type-name in the definition, we
7228 must be in the scope of `S'. */
7230 push_scope (saved_scope);
7231 /* Parse the conversion-type-id. */
7232 type = cp_parser_conversion_type_id (parser);
7233 /* Leave the scope of the class, if any. */
7235 pop_scope (saved_scope);
7236 /* Restore the saved scope. */
7237 parser->scope = saved_scope;
7238 parser->qualifying_scope = saved_qualifying_scope;
7239 parser->object_scope = saved_object_scope;
7240 /* If the TYPE is invalid, indicate failure. */
7241 if (type == error_mark_node)
7242 return error_mark_node;
7243 return mangle_conv_op_name_for_type (type);
7246 /* Parse a conversion-type-id:
7249 type-specifier-seq conversion-declarator [opt]
7251 Returns the TYPE specified. */
7254 cp_parser_conversion_type_id (parser)
7258 tree type_specifiers;
7261 /* Parse the attributes. */
7262 attributes = cp_parser_attributes_opt (parser);
7263 /* Parse the type-specifiers. */
7264 type_specifiers = cp_parser_type_specifier_seq (parser);
7265 /* If that didn't work, stop. */
7266 if (type_specifiers == error_mark_node)
7267 return error_mark_node;
7268 /* Parse the conversion-declarator. */
7269 declarator = cp_parser_conversion_declarator_opt (parser);
7271 return grokdeclarator (declarator, type_specifiers, TYPENAME,
7272 /*initialized=*/0, &attributes);
7275 /* Parse an (optional) conversion-declarator.
7277 conversion-declarator:
7278 ptr-operator conversion-declarator [opt]
7280 Returns a representation of the declarator. See
7281 cp_parser_declarator for details. */
7284 cp_parser_conversion_declarator_opt (parser)
7287 enum tree_code code;
7289 tree cv_qualifier_seq;
7291 /* We don't know if there's a ptr-operator next, or not. */
7292 cp_parser_parse_tentatively (parser);
7293 /* Try the ptr-operator. */
7294 code = cp_parser_ptr_operator (parser, &class_type,
7296 /* If it worked, look for more conversion-declarators. */
7297 if (cp_parser_parse_definitely (parser))
7301 /* Parse another optional declarator. */
7302 declarator = cp_parser_conversion_declarator_opt (parser);
7304 /* Create the representation of the declarator. */
7305 if (code == INDIRECT_REF)
7306 declarator = make_pointer_declarator (cv_qualifier_seq,
7309 declarator = make_reference_declarator (cv_qualifier_seq,
7312 /* Handle the pointer-to-member case. */
7314 declarator = build_nt (SCOPE_REF, class_type, declarator);
7322 /* Parse an (optional) ctor-initializer.
7325 : mem-initializer-list
7327 Returns TRUE iff the ctor-initializer was actually present. */
7330 cp_parser_ctor_initializer_opt (parser)
7333 /* If the next token is not a `:', then there is no
7334 ctor-initializer. */
7335 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
7337 /* Do default initialization of any bases and members. */
7338 if (DECL_CONSTRUCTOR_P (current_function_decl))
7339 finish_mem_initializers (NULL_TREE);
7344 /* Consume the `:' token. */
7345 cp_lexer_consume_token (parser->lexer);
7346 /* And the mem-initializer-list. */
7347 cp_parser_mem_initializer_list (parser);
7352 /* Parse a mem-initializer-list.
7354 mem-initializer-list:
7356 mem-initializer , mem-initializer-list */
7359 cp_parser_mem_initializer_list (parser)
7362 tree mem_initializer_list = NULL_TREE;
7364 /* Let the semantic analysis code know that we are starting the
7365 mem-initializer-list. */
7366 begin_mem_initializers ();
7368 /* Loop through the list. */
7371 tree mem_initializer;
7373 /* Parse the mem-initializer. */
7374 mem_initializer = cp_parser_mem_initializer (parser);
7375 /* Add it to the list, unless it was erroneous. */
7376 if (mem_initializer)
7378 TREE_CHAIN (mem_initializer) = mem_initializer_list;
7379 mem_initializer_list = mem_initializer;
7381 /* If the next token is not a `,', we're done. */
7382 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7384 /* Consume the `,' token. */
7385 cp_lexer_consume_token (parser->lexer);
7388 /* Perform semantic analysis. */
7389 finish_mem_initializers (mem_initializer_list);
7392 /* Parse a mem-initializer.
7395 mem-initializer-id ( expression-list [opt] )
7400 ( expresion-list [opt] )
7402 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
7403 class) or FIELD_DECL (for a non-static data member) to initialize;
7404 the TREE_VALUE is the expression-list. */
7407 cp_parser_mem_initializer (parser)
7410 tree mem_initializer_id;
7411 tree expression_list;
7413 /* Find out what is being initialized. */
7414 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7416 pedwarn ("anachronistic old-style base class initializer");
7417 mem_initializer_id = NULL_TREE;
7420 mem_initializer_id = cp_parser_mem_initializer_id (parser);
7421 /* Look for the opening `('. */
7422 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
7423 /* Parse the expression-list. */
7424 if (cp_lexer_next_token_is_not (parser->lexer,
7426 expression_list = cp_parser_expression_list (parser);
7428 expression_list = void_type_node;
7429 /* Look for the closing `)'. */
7430 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
7432 return expand_member_init (mem_initializer_id,
7436 /* Parse a mem-initializer-id.
7439 :: [opt] nested-name-specifier [opt] class-name
7442 Returns a TYPE indicating the class to be initializer for the first
7443 production. Returns an IDENTIFIER_NODE indicating the data member
7444 to be initialized for the second production. */
7447 cp_parser_mem_initializer_id (parser)
7450 bool global_scope_p;
7451 bool nested_name_specifier_p;
7454 /* Look for the optional `::' operator. */
7456 = (cp_parser_global_scope_opt (parser,
7457 /*current_scope_valid_p=*/false)
7459 /* Look for the optional nested-name-specifier. The simplest way to
7464 The keyword `typename' is not permitted in a base-specifier or
7465 mem-initializer; in these contexts a qualified name that
7466 depends on a template-parameter is implicitly assumed to be a
7469 is to assume that we have seen the `typename' keyword at this
7471 nested_name_specifier_p
7472 = (cp_parser_nested_name_specifier_opt (parser,
7473 /*typename_keyword_p=*/true,
7474 /*check_dependency_p=*/true,
7477 /* If there is a `::' operator or a nested-name-specifier, then we
7478 are definitely looking for a class-name. */
7479 if (global_scope_p || nested_name_specifier_p)
7480 return cp_parser_class_name (parser,
7481 /*typename_keyword_p=*/true,
7482 /*template_keyword_p=*/false,
7484 /*check_access_p=*/true,
7485 /*check_dependency_p=*/true,
7486 /*class_head_p=*/false);
7487 /* Otherwise, we could also be looking for an ordinary identifier. */
7488 cp_parser_parse_tentatively (parser);
7489 /* Try a class-name. */
7490 id = cp_parser_class_name (parser,
7491 /*typename_keyword_p=*/true,
7492 /*template_keyword_p=*/false,
7494 /*check_access_p=*/true,
7495 /*check_dependency_p=*/true,
7496 /*class_head_p=*/false);
7497 /* If we found one, we're done. */
7498 if (cp_parser_parse_definitely (parser))
7500 /* Otherwise, look for an ordinary identifier. */
7501 return cp_parser_identifier (parser);
7504 /* Overloading [gram.over] */
7506 /* Parse an operator-function-id.
7508 operator-function-id:
7511 Returns an IDENTIFIER_NODE for the operator which is a
7512 human-readable spelling of the identifier, e.g., `operator +'. */
7515 cp_parser_operator_function_id (parser)
7518 /* Look for the `operator' keyword. */
7519 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
7520 return error_mark_node;
7521 /* And then the name of the operator itself. */
7522 return cp_parser_operator (parser);
7525 /* Parse an operator.
7528 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
7529 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
7530 || ++ -- , ->* -> () []
7537 Returns an IDENTIFIER_NODE for the operator which is a
7538 human-readable spelling of the identifier, e.g., `operator +'. */
7541 cp_parser_operator (parser)
7544 tree id = NULL_TREE;
7547 /* Peek at the next token. */
7548 token = cp_lexer_peek_token (parser->lexer);
7549 /* Figure out which operator we have. */
7550 switch (token->type)
7556 /* The keyword should be either `new' or `delete'. */
7557 if (token->keyword == RID_NEW)
7559 else if (token->keyword == RID_DELETE)
7564 /* Consume the `new' or `delete' token. */
7565 cp_lexer_consume_token (parser->lexer);
7567 /* Peek at the next token. */
7568 token = cp_lexer_peek_token (parser->lexer);
7569 /* If it's a `[' token then this is the array variant of the
7571 if (token->type == CPP_OPEN_SQUARE)
7573 /* Consume the `[' token. */
7574 cp_lexer_consume_token (parser->lexer);
7575 /* Look for the `]' token. */
7576 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
7577 id = ansi_opname (op == NEW_EXPR
7578 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
7580 /* Otherwise, we have the non-array variant. */
7582 id = ansi_opname (op);
7588 id = ansi_opname (PLUS_EXPR);
7592 id = ansi_opname (MINUS_EXPR);
7596 id = ansi_opname (MULT_EXPR);
7600 id = ansi_opname (TRUNC_DIV_EXPR);
7604 id = ansi_opname (TRUNC_MOD_EXPR);
7608 id = ansi_opname (BIT_XOR_EXPR);
7612 id = ansi_opname (BIT_AND_EXPR);
7616 id = ansi_opname (BIT_IOR_EXPR);
7620 id = ansi_opname (BIT_NOT_EXPR);
7624 id = ansi_opname (TRUTH_NOT_EXPR);
7628 id = ansi_assopname (NOP_EXPR);
7632 id = ansi_opname (LT_EXPR);
7636 id = ansi_opname (GT_EXPR);
7640 id = ansi_assopname (PLUS_EXPR);
7644 id = ansi_assopname (MINUS_EXPR);
7648 id = ansi_assopname (MULT_EXPR);
7652 id = ansi_assopname (TRUNC_DIV_EXPR);
7656 id = ansi_assopname (TRUNC_MOD_EXPR);
7660 id = ansi_assopname (BIT_XOR_EXPR);
7664 id = ansi_assopname (BIT_AND_EXPR);
7668 id = ansi_assopname (BIT_IOR_EXPR);
7672 id = ansi_opname (LSHIFT_EXPR);
7676 id = ansi_opname (RSHIFT_EXPR);
7680 id = ansi_assopname (LSHIFT_EXPR);
7684 id = ansi_assopname (RSHIFT_EXPR);
7688 id = ansi_opname (EQ_EXPR);
7692 id = ansi_opname (NE_EXPR);
7696 id = ansi_opname (LE_EXPR);
7699 case CPP_GREATER_EQ:
7700 id = ansi_opname (GE_EXPR);
7704 id = ansi_opname (TRUTH_ANDIF_EXPR);
7708 id = ansi_opname (TRUTH_ORIF_EXPR);
7712 id = ansi_opname (POSTINCREMENT_EXPR);
7715 case CPP_MINUS_MINUS:
7716 id = ansi_opname (PREDECREMENT_EXPR);
7720 id = ansi_opname (COMPOUND_EXPR);
7723 case CPP_DEREF_STAR:
7724 id = ansi_opname (MEMBER_REF);
7728 id = ansi_opname (COMPONENT_REF);
7731 case CPP_OPEN_PAREN:
7732 /* Consume the `('. */
7733 cp_lexer_consume_token (parser->lexer);
7734 /* Look for the matching `)'. */
7735 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
7736 return ansi_opname (CALL_EXPR);
7738 case CPP_OPEN_SQUARE:
7739 /* Consume the `['. */
7740 cp_lexer_consume_token (parser->lexer);
7741 /* Look for the matching `]'. */
7742 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
7743 return ansi_opname (ARRAY_REF);
7747 id = ansi_opname (MIN_EXPR);
7751 id = ansi_opname (MAX_EXPR);
7755 id = ansi_assopname (MIN_EXPR);
7759 id = ansi_assopname (MAX_EXPR);
7763 /* Anything else is an error. */
7767 /* If we have selected an identifier, we need to consume the
7770 cp_lexer_consume_token (parser->lexer);
7771 /* Otherwise, no valid operator name was present. */
7774 cp_parser_error (parser, "expected operator");
7775 id = error_mark_node;
7781 /* Parse a template-declaration.
7783 template-declaration:
7784 export [opt] template < template-parameter-list > declaration
7786 If MEMBER_P is TRUE, this template-declaration occurs within a
7789 The grammar rule given by the standard isn't correct. What
7792 template-declaration:
7793 export [opt] template-parameter-list-seq
7794 decl-specifier-seq [opt] init-declarator [opt] ;
7795 export [opt] template-parameter-list-seq
7798 template-parameter-list-seq:
7799 template-parameter-list-seq [opt]
7800 template < template-parameter-list > */
7803 cp_parser_template_declaration (parser, member_p)
7807 /* Check for `export'. */
7808 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
7810 /* Consume the `export' token. */
7811 cp_lexer_consume_token (parser->lexer);
7812 /* Warn that we do not support `export'. */
7813 warning ("keyword `export' not implemented, and will be ignored");
7816 cp_parser_template_declaration_after_export (parser, member_p);
7819 /* Parse a template-parameter-list.
7821 template-parameter-list:
7823 template-parameter-list , template-parameter
7825 Returns a TREE_LIST. Each node represents a template parameter.
7826 The nodes are connected via their TREE_CHAINs. */
7829 cp_parser_template_parameter_list (parser)
7832 tree parameter_list = NULL_TREE;
7839 /* Parse the template-parameter. */
7840 parameter = cp_parser_template_parameter (parser);
7841 /* Add it to the list. */
7842 parameter_list = process_template_parm (parameter_list,
7845 /* Peek at the next token. */
7846 token = cp_lexer_peek_token (parser->lexer);
7847 /* If it's not a `,', we're done. */
7848 if (token->type != CPP_COMMA)
7850 /* Otherwise, consume the `,' token. */
7851 cp_lexer_consume_token (parser->lexer);
7854 return parameter_list;
7857 /* Parse a template-parameter.
7861 parameter-declaration
7863 Returns a TREE_LIST. The TREE_VALUE represents the parameter. The
7864 TREE_PURPOSE is the default value, if any. */
7867 cp_parser_template_parameter (parser)
7872 /* Peek at the next token. */
7873 token = cp_lexer_peek_token (parser->lexer);
7874 /* If it is `class' or `template', we have a type-parameter. */
7875 if (token->keyword == RID_TEMPLATE)
7876 return cp_parser_type_parameter (parser);
7877 /* If it is `class' or `typename' we do not know yet whether it is a
7878 type parameter or a non-type parameter. Consider:
7880 template <typename T, typename T::X X> ...
7884 template <class C, class D*> ...
7886 Here, the first parameter is a type parameter, and the second is
7887 a non-type parameter. We can tell by looking at the token after
7888 the identifier -- if it is a `,', `=', or `>' then we have a type
7890 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
7892 /* Peek at the token after `class' or `typename'. */
7893 token = cp_lexer_peek_nth_token (parser->lexer, 2);
7894 /* If it's an identifier, skip it. */
7895 if (token->type == CPP_NAME)
7896 token = cp_lexer_peek_nth_token (parser->lexer, 3);
7897 /* Now, see if the token looks like the end of a template
7899 if (token->type == CPP_COMMA
7900 || token->type == CPP_EQ
7901 || token->type == CPP_GREATER)
7902 return cp_parser_type_parameter (parser);
7905 /* Otherwise, it is a non-type parameter.
7909 When parsing a default template-argument for a non-type
7910 template-parameter, the first non-nested `>' is taken as the end
7911 of the template parameter-list rather than a greater-than
7914 cp_parser_parameter_declaration (parser,
7915 /*greater_than_is_operator_p=*/false);
7918 /* Parse a type-parameter.
7921 class identifier [opt]
7922 class identifier [opt] = type-id
7923 typename identifier [opt]
7924 typename identifier [opt] = type-id
7925 template < template-parameter-list > class identifier [opt]
7926 template < template-parameter-list > class identifier [opt]
7929 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
7930 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
7931 the declaration of the parameter. */
7934 cp_parser_type_parameter (parser)
7940 /* Look for a keyword to tell us what kind of parameter this is. */
7941 token = cp_parser_require (parser, CPP_KEYWORD,
7942 "expected `class', `typename', or `template'");
7944 return error_mark_node;
7946 switch (token->keyword)
7952 tree default_argument;
7954 /* If the next token is an identifier, then it names the
7956 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
7957 identifier = cp_parser_identifier (parser);
7959 identifier = NULL_TREE;
7961 /* Create the parameter. */
7962 parameter = finish_template_type_parm (class_type_node, identifier);
7964 /* If the next token is an `=', we have a default argument. */
7965 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7967 /* Consume the `=' token. */
7968 cp_lexer_consume_token (parser->lexer);
7969 /* Parse the default-argumen. */
7970 default_argument = cp_parser_type_id (parser);
7973 default_argument = NULL_TREE;
7975 /* Create the combined representation of the parameter and the
7976 default argument. */
7977 parameter = build_tree_list (default_argument,
7984 tree parameter_list;
7986 tree default_argument;
7988 /* Look for the `<'. */
7989 cp_parser_require (parser, CPP_LESS, "`<'");
7990 /* Parse the template-parameter-list. */
7991 begin_template_parm_list ();
7993 = cp_parser_template_parameter_list (parser);
7994 parameter_list = end_template_parm_list (parameter_list);
7995 /* Look for the `>'. */
7996 cp_parser_require (parser, CPP_GREATER, "`>'");
7997 /* Look for the `class' keyword. */
7998 cp_parser_require_keyword (parser, RID_CLASS, "`class'");
7999 /* If the next token is an `=', then there is a
8000 default-argument. If the next token is a `>', we are at
8001 the end of the parameter-list. If the next token is a `,',
8002 then we are at the end of this parameter. */
8003 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
8004 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
8005 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8006 identifier = cp_parser_identifier (parser);
8008 identifier = NULL_TREE;
8009 /* Create the template parameter. */
8010 parameter = finish_template_template_parm (class_type_node,
8013 /* If the next token is an `=', then there is a
8014 default-argument. */
8015 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8017 /* Consume the `='. */
8018 cp_lexer_consume_token (parser->lexer);
8019 /* Parse the id-expression. */
8021 = cp_parser_id_expression (parser,
8022 /*template_keyword_p=*/false,
8023 /*check_dependency_p=*/true,
8024 /*template_p=*/NULL);
8025 /* Look up the name. */
8027 = cp_parser_lookup_name_simple (parser, default_argument);
8028 /* See if the default argument is valid. */
8030 = check_template_template_default_arg (default_argument);
8033 default_argument = NULL_TREE;
8035 /* Create the combined representation of the parameter and the
8036 default argument. */
8037 parameter = build_tree_list (default_argument,
8043 /* Anything else is an error. */
8044 cp_parser_error (parser,
8045 "expected `class', `typename', or `template'");
8046 parameter = error_mark_node;
8052 /* Parse a template-id.
8055 template-name < template-argument-list [opt] >
8057 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
8058 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
8059 returned. Otherwise, if the template-name names a function, or set
8060 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
8061 names a class, returns a TYPE_DECL for the specialization.
8063 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
8064 uninstantiated templates. */
8067 cp_parser_template_id (cp_parser *parser,
8068 bool template_keyword_p,
8069 bool check_dependency_p)
8074 tree saved_qualifying_scope;
8075 tree saved_object_scope;
8077 bool saved_greater_than_is_operator_p;
8078 ptrdiff_t start_of_id;
8079 tree access_check = NULL_TREE;
8081 /* If the next token corresponds to a template-id, there is no need
8083 if (cp_lexer_next_token_is (parser->lexer, CPP_TEMPLATE_ID))
8088 /* Get the stored value. */
8089 value = cp_lexer_consume_token (parser->lexer)->value;
8090 /* Perform any access checks that were deferred. */
8091 for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
8092 cp_parser_defer_access_check (parser,
8093 TREE_PURPOSE (check),
8094 TREE_VALUE (check));
8095 /* Return the stored value. */
8096 return TREE_VALUE (value);
8099 /* Remember where the template-id starts. */
8100 if (cp_parser_parsing_tentatively (parser)
8101 && !cp_parser_committed_to_tentative_parse (parser))
8103 cp_token *next_token = cp_lexer_peek_token (parser->lexer);
8104 start_of_id = cp_lexer_token_difference (parser->lexer,
8105 parser->lexer->first_token,
8107 access_check = parser->context->deferred_access_checks;
8112 /* Parse the template-name. */
8113 template = cp_parser_template_name (parser, template_keyword_p,
8114 check_dependency_p);
8115 if (template == error_mark_node)
8116 return error_mark_node;
8118 /* Look for the `<' that starts the template-argument-list. */
8119 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
8120 return error_mark_node;
8124 When parsing a template-id, the first non-nested `>' is taken as
8125 the end of the template-argument-list rather than a greater-than
8127 saved_greater_than_is_operator_p
8128 = parser->greater_than_is_operator_p;
8129 parser->greater_than_is_operator_p = false;
8130 /* Parsing the argument list may modify SCOPE, so we save it
8132 saved_scope = parser->scope;
8133 saved_qualifying_scope = parser->qualifying_scope;
8134 saved_object_scope = parser->object_scope;
8135 /* Parse the template-argument-list itself. */
8136 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
8137 arguments = NULL_TREE;
8139 arguments = cp_parser_template_argument_list (parser);
8140 /* Look for the `>' that ends the template-argument-list. */
8141 cp_parser_require (parser, CPP_GREATER, "`>'");
8142 /* The `>' token might be a greater-than operator again now. */
8143 parser->greater_than_is_operator_p
8144 = saved_greater_than_is_operator_p;
8145 /* Restore the SAVED_SCOPE. */
8146 parser->scope = saved_scope;
8147 parser->qualifying_scope = saved_qualifying_scope;
8148 parser->object_scope = saved_object_scope;
8150 /* Build a representation of the specialization. */
8151 if (TREE_CODE (template) == IDENTIFIER_NODE)
8152 template_id = build_min_nt (TEMPLATE_ID_EXPR, template, arguments);
8153 else if (DECL_CLASS_TEMPLATE_P (template)
8154 || DECL_TEMPLATE_TEMPLATE_PARM_P (template))
8156 = finish_template_type (template, arguments,
8157 cp_lexer_next_token_is (parser->lexer,
8161 /* If it's not a class-template or a template-template, it should be
8162 a function-template. */
8163 my_friendly_assert ((DECL_FUNCTION_TEMPLATE_P (template)
8164 || TREE_CODE (template) == OVERLOAD
8165 || BASELINK_P (template)),
8168 template_id = lookup_template_function (template, arguments);
8171 /* If parsing tentatively, replace the sequence of tokens that makes
8172 up the template-id with a CPP_TEMPLATE_ID token. That way,
8173 should we re-parse the token stream, we will not have to repeat
8174 the effort required to do the parse, nor will we issue duplicate
8175 error messages about problems during instantiation of the
8177 if (start_of_id >= 0)
8182 /* Find the token that corresponds to the start of the
8184 token = cp_lexer_advance_token (parser->lexer,
8185 parser->lexer->first_token,
8188 /* Remember the access checks associated with this
8189 nested-name-specifier. */
8190 c = parser->context->deferred_access_checks;
8191 if (c == access_check)
8192 access_check = NULL_TREE;
8195 while (TREE_CHAIN (c) != access_check)
8197 access_check = parser->context->deferred_access_checks;
8198 parser->context->deferred_access_checks = TREE_CHAIN (c);
8199 TREE_CHAIN (c) = NULL_TREE;
8202 /* Reset the contents of the START_OF_ID token. */
8203 token->type = CPP_TEMPLATE_ID;
8204 token->value = build_tree_list (access_check, template_id);
8205 token->keyword = RID_MAX;
8206 /* Purge all subsequent tokens. */
8207 cp_lexer_purge_tokens_after (parser->lexer, token);
8213 /* Parse a template-name.
8218 The standard should actually say:
8222 operator-function-id
8223 conversion-function-id
8225 A defect report has been filed about this issue.
8227 If TEMPLATE_KEYWORD_P is true, then we have just seen the
8228 `template' keyword, in a construction like:
8232 In that case `f' is taken to be a template-name, even though there
8233 is no way of knowing for sure.
8235 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
8236 name refers to a set of overloaded functions, at least one of which
8237 is a template, or an IDENTIFIER_NODE with the name of the template,
8238 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
8239 names are looked up inside uninstantiated templates. */
8242 cp_parser_template_name (parser, template_keyword_p, check_dependency_p)
8244 bool template_keyword_p;
8245 bool check_dependency_p;
8251 /* If the next token is `operator', then we have either an
8252 operator-function-id or a conversion-function-id. */
8253 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
8255 /* We don't know whether we're looking at an
8256 operator-function-id or a conversion-function-id. */
8257 cp_parser_parse_tentatively (parser);
8258 /* Try an operator-function-id. */
8259 identifier = cp_parser_operator_function_id (parser);
8260 /* If that didn't work, try a conversion-function-id. */
8261 if (!cp_parser_parse_definitely (parser))
8262 identifier = cp_parser_conversion_function_id (parser);
8264 /* Look for the identifier. */
8266 identifier = cp_parser_identifier (parser);
8268 /* If we didn't find an identifier, we don't have a template-id. */
8269 if (identifier == error_mark_node)
8270 return error_mark_node;
8272 /* If the name immediately followed the `template' keyword, then it
8273 is a template-name. However, if the next token is not `<', then
8274 we do not treat it as a template-name, since it is not being used
8275 as part of a template-id. This enables us to handle constructs
8278 template <typename T> struct S { S(); };
8279 template <typename T> S<T>::S();
8281 correctly. We would treat `S' as a template -- if it were `S<T>'
8282 -- but we do not if there is no `<'. */
8283 if (template_keyword_p && processing_template_decl
8284 && cp_lexer_next_token_is (parser->lexer, CPP_LESS))
8287 /* Look up the name. */
8288 decl = cp_parser_lookup_name (parser, identifier,
8289 /*check_access=*/true,
8291 /*is_namespace=*/false,
8292 check_dependency_p);
8293 decl = maybe_get_template_decl_from_type_decl (decl);
8295 /* If DECL is a template, then the name was a template-name. */
8296 if (TREE_CODE (decl) == TEMPLATE_DECL)
8300 /* The standard does not explicitly indicate whether a name that
8301 names a set of overloaded declarations, some of which are
8302 templates, is a template-name. However, such a name should
8303 be a template-name; otherwise, there is no way to form a
8304 template-id for the overloaded templates. */
8305 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
8306 if (TREE_CODE (fns) == OVERLOAD)
8310 for (fn = fns; fn; fn = OVL_NEXT (fn))
8311 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
8316 /* Otherwise, the name does not name a template. */
8317 cp_parser_error (parser, "expected template-name");
8318 return error_mark_node;
8322 /* If DECL is dependent, and refers to a function, then just return
8323 its name; we will look it up again during template instantiation. */
8324 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
8326 tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
8327 if (TYPE_P (scope) && cp_parser_dependent_type_p (scope))
8334 /* Parse a template-argument-list.
8336 template-argument-list:
8338 template-argument-list , template-argument
8340 Returns a TREE_LIST representing the arguments, in the order they
8341 appeared. The TREE_VALUE of each node is a representation of the
8345 cp_parser_template_argument_list (parser)
8348 tree arguments = NULL_TREE;
8354 /* Parse the template-argument. */
8355 argument = cp_parser_template_argument (parser);
8356 /* Add it to the list. */
8357 arguments = tree_cons (NULL_TREE, argument, arguments);
8358 /* If it is not a `,', then there are no more arguments. */
8359 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8361 /* Otherwise, consume the ','. */
8362 cp_lexer_consume_token (parser->lexer);
8365 /* We built up the arguments in reverse order. */
8366 return nreverse (arguments);
8369 /* Parse a template-argument.
8372 assignment-expression
8376 The representation is that of an assignment-expression, type-id, or
8377 id-expression -- except that the qualified id-expression is
8378 evaluated, so that the value returned is either a DECL or an
8382 cp_parser_template_argument (parser)
8388 /* There's really no way to know what we're looking at, so we just
8389 try each alternative in order.
8393 In a template-argument, an ambiguity between a type-id and an
8394 expression is resolved to a type-id, regardless of the form of
8395 the corresponding template-parameter.
8397 Therefore, we try a type-id first. */
8398 cp_parser_parse_tentatively (parser);
8399 argument = cp_parser_type_id (parser);
8400 /* If the next token isn't a `,' or a `>', then this argument wasn't
8402 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
8403 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER))
8404 cp_parser_error (parser, "expected template-argument");
8405 /* If that worked, we're done. */
8406 if (cp_parser_parse_definitely (parser))
8408 /* We're still not sure what the argument will be. */
8409 cp_parser_parse_tentatively (parser);
8410 /* Try a template. */
8411 argument = cp_parser_id_expression (parser,
8412 /*template_keyword_p=*/false,
8413 /*check_dependency_p=*/true,
8415 /* If the next token isn't a `,' or a `>', then this argument wasn't
8417 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
8418 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER))
8419 cp_parser_error (parser, "expected template-argument");
8420 if (!cp_parser_error_occurred (parser))
8422 /* Figure out what is being referred to. */
8423 argument = cp_parser_lookup_name_simple (parser, argument);
8425 argument = make_unbound_class_template (TREE_OPERAND (argument, 0),
8426 TREE_OPERAND (argument, 1),
8427 tf_error | tf_parsing);
8428 else if (TREE_CODE (argument) != TEMPLATE_DECL)
8429 cp_parser_error (parser, "expected template-name");
8431 if (cp_parser_parse_definitely (parser))
8433 /* It must be an assignment-expression. */
8434 return cp_parser_assignment_expression (parser);
8437 /* Parse an explicit-instantiation.
8439 explicit-instantiation:
8440 template declaration
8442 Although the standard says `declaration', what it really means is:
8444 explicit-instantiation:
8445 template decl-specifier-seq [opt] declarator [opt] ;
8447 Things like `template int S<int>::i = 5, int S<double>::j;' are not
8448 supposed to be allowed. A defect report has been filed about this
8453 explicit-instantiation:
8454 storage-class-specifier template
8455 decl-specifier-seq [opt] declarator [opt] ;
8456 function-specifier template
8457 decl-specifier-seq [opt] declarator [opt] ; */
8460 cp_parser_explicit_instantiation (parser)
8463 bool declares_class_or_enum;
8464 tree decl_specifiers;
8466 tree extension_specifier = NULL_TREE;
8468 /* Look for an (optional) storage-class-specifier or
8469 function-specifier. */
8470 if (cp_parser_allow_gnu_extensions_p (parser))
8473 = cp_parser_storage_class_specifier_opt (parser);
8474 if (!extension_specifier)
8475 extension_specifier = cp_parser_function_specifier_opt (parser);
8478 /* Look for the `template' keyword. */
8479 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
8480 /* Let the front end know that we are processing an explicit
8482 begin_explicit_instantiation ();
8483 /* [temp.explicit] says that we are supposed to ignore access
8484 control while processing explicit instantiation directives. */
8485 scope_chain->check_access = 0;
8486 /* Parse a decl-specifier-seq. */
8488 = cp_parser_decl_specifier_seq (parser,
8489 CP_PARSER_FLAGS_OPTIONAL,
8491 &declares_class_or_enum);
8492 /* If there was exactly one decl-specifier, and it declared a class,
8493 and there's no declarator, then we have an explicit type
8495 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
8499 type = check_tag_decl (decl_specifiers);
8501 do_type_instantiation (type, extension_specifier, /*complain=*/1);
8508 /* Parse the declarator. */
8510 = cp_parser_declarator (parser,
8511 /*abstract_p=*/false,
8512 /*ctor_dtor_or_conv_p=*/NULL);
8513 decl = grokdeclarator (declarator, decl_specifiers,
8515 /* Do the explicit instantiation. */
8516 do_decl_instantiation (decl, extension_specifier);
8518 /* We're done with the instantiation. */
8519 end_explicit_instantiation ();
8520 /* Trun access control back on. */
8521 scope_chain->check_access = flag_access_control;
8523 /* Look for the trailing `;'. */
8524 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
8527 /* Parse an explicit-specialization.
8529 explicit-specialization:
8530 template < > declaration
8532 Although the standard says `declaration', what it really means is:
8534 explicit-specialization:
8535 template <> decl-specifier [opt] init-declarator [opt] ;
8536 template <> function-definition
8537 template <> explicit-specialization
8538 template <> template-declaration */
8541 cp_parser_explicit_specialization (parser)
8544 /* Look for the `template' keyword. */
8545 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
8546 /* Look for the `<'. */
8547 cp_parser_require (parser, CPP_LESS, "`<'");
8548 /* Look for the `>'. */
8549 cp_parser_require (parser, CPP_GREATER, "`>'");
8550 /* We have processed another parameter list. */
8551 ++parser->num_template_parameter_lists;
8552 /* Let the front end know that we are beginning a specialization. */
8553 begin_specialization ();
8555 /* If the next keyword is `template', we need to figure out whether
8556 or not we're looking a template-declaration. */
8557 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
8559 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
8560 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
8561 cp_parser_template_declaration_after_export (parser,
8562 /*member_p=*/false);
8564 cp_parser_explicit_specialization (parser);
8567 /* Parse the dependent declaration. */
8568 cp_parser_single_declaration (parser,
8572 /* We're done with the specialization. */
8573 end_specialization ();
8574 /* We're done with this parameter list. */
8575 --parser->num_template_parameter_lists;
8578 /* Parse a type-specifier.
8581 simple-type-specifier
8584 elaborated-type-specifier
8592 Returns a representation of the type-specifier. If the
8593 type-specifier is a keyword (like `int' or `const', or
8594 `__complex__') then the correspoding IDENTIFIER_NODE is returned.
8595 For a class-specifier, enum-specifier, or elaborated-type-specifier
8596 a TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
8598 If IS_FRIEND is TRUE then this type-specifier is being declared a
8599 `friend'. If IS_DECLARATION is TRUE, then this type-specifier is
8600 appearing in a decl-specifier-seq.
8602 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
8603 class-specifier, enum-specifier, or elaborated-type-specifier, then
8604 *DECLARES_CLASS_OR_ENUM is set to TRUE. Otherwise, it is set to
8607 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
8608 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
8612 cp_parser_type_specifier (parser,
8616 declares_class_or_enum,
8619 cp_parser_flags flags;
8621 bool is_declaration;
8622 bool *declares_class_or_enum;
8623 bool *is_cv_qualifier;
8625 tree type_spec = NULL_TREE;
8629 /* Assume this type-specifier does not declare a new type. */
8630 if (declares_class_or_enum)
8631 *declares_class_or_enum = false;
8632 /* And that it does not specify a cv-qualifier. */
8633 if (is_cv_qualifier)
8634 *is_cv_qualifier = false;
8635 /* Peek at the next token. */
8636 token = cp_lexer_peek_token (parser->lexer);
8638 /* If we're looking at a keyword, we can use that to guide the
8639 production we choose. */
8640 keyword = token->keyword;
8643 /* Any of these indicate either a class-specifier, or an
8644 elaborated-type-specifier. */
8649 /* Parse tentatively so that we can back up if we don't find a
8650 class-specifier or enum-specifier. */
8651 cp_parser_parse_tentatively (parser);
8652 /* Look for the class-specifier or enum-specifier. */
8653 if (keyword == RID_ENUM)
8654 type_spec = cp_parser_enum_specifier (parser);
8656 type_spec = cp_parser_class_specifier (parser);
8658 /* If that worked, we're done. */
8659 if (cp_parser_parse_definitely (parser))
8661 if (declares_class_or_enum)
8662 *declares_class_or_enum = true;
8669 /* Look for an elaborated-type-specifier. */
8670 type_spec = cp_parser_elaborated_type_specifier (parser,
8673 /* We're declaring a class or enum -- unless we're using
8675 if (declares_class_or_enum && keyword != RID_TYPENAME)
8676 *declares_class_or_enum = true;
8682 type_spec = cp_parser_cv_qualifier_opt (parser);
8683 /* Even though we call a routine that looks for an optional
8684 qualifier, we know that there should be one. */
8685 my_friendly_assert (type_spec != NULL, 20000328);
8686 /* This type-specifier was a cv-qualified. */
8687 if (is_cv_qualifier)
8688 *is_cv_qualifier = true;
8693 /* The `__complex__' keyword is a GNU extension. */
8694 return cp_lexer_consume_token (parser->lexer)->value;
8700 /* If we do not already have a type-specifier, assume we are looking
8701 at a simple-type-specifier. */
8702 type_spec = cp_parser_simple_type_specifier (parser, flags);
8704 /* If we didn't find a type-specifier, and a type-specifier was not
8705 optional in this context, issue an error message. */
8706 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
8708 cp_parser_error (parser, "expected type specifier");
8709 return error_mark_node;
8715 /* Parse a simple-type-specifier.
8717 simple-type-specifier:
8718 :: [opt] nested-name-specifier [opt] type-name
8719 :: [opt] nested-name-specifier template template-id
8734 simple-type-specifier:
8735 __typeof__ unary-expression
8736 __typeof__ ( type-id )
8738 For the various keywords, the value returned is simply the
8739 TREE_IDENTIFIER representing the keyword. For the first two
8740 productions, the value returned is the indicated TYPE_DECL. */
8743 cp_parser_simple_type_specifier (parser, flags)
8745 cp_parser_flags flags;
8747 tree type = NULL_TREE;
8750 /* Peek at the next token. */
8751 token = cp_lexer_peek_token (parser->lexer);
8753 /* If we're looking at a keyword, things are easy. */
8754 switch (token->keyword)
8767 /* Consume the token. */
8768 return cp_lexer_consume_token (parser->lexer)->value;
8774 /* Consume the `typeof' token. */
8775 cp_lexer_consume_token (parser->lexer);
8776 /* Parse the operand to `typeof' */
8777 operand = cp_parser_sizeof_operand (parser, RID_TYPEOF);
8778 /* If it is not already a TYPE, take its type. */
8779 if (!TYPE_P (operand))
8780 operand = finish_typeof (operand);
8789 /* The type-specifier must be a user-defined type. */
8790 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
8792 /* Don't gobble tokens or issue error messages if this is an
8793 optional type-specifier. */
8794 if (flags & CP_PARSER_FLAGS_OPTIONAL)
8795 cp_parser_parse_tentatively (parser);
8797 /* Look for the optional `::' operator. */
8798 cp_parser_global_scope_opt (parser,
8799 /*current_scope_valid_p=*/false);
8800 /* Look for the nested-name specifier. */
8801 cp_parser_nested_name_specifier_opt (parser,
8802 /*typename_keyword_p=*/false,
8803 /*check_dependency_p=*/true,
8805 /* If we have seen a nested-name-specifier, and the next token
8806 is `template', then we are using the template-id production. */
8808 && cp_parser_optional_template_keyword (parser))
8810 /* Look for the template-id. */
8811 type = cp_parser_template_id (parser,
8812 /*template_keyword_p=*/true,
8813 /*check_dependency_p=*/true);
8814 /* If the template-id did not name a type, we are out of
8816 if (TREE_CODE (type) != TYPE_DECL)
8818 cp_parser_error (parser, "expected template-id for type");
8822 /* Otherwise, look for a type-name. */
8825 type = cp_parser_type_name (parser);
8826 if (type == error_mark_node)
8830 /* If it didn't work out, we don't have a TYPE. */
8831 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
8832 && !cp_parser_parse_definitely (parser))
8836 /* If we didn't get a type-name, issue an error message. */
8837 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
8839 cp_parser_error (parser, "expected type-name");
8840 return error_mark_node;
8846 /* Parse a type-name.
8859 Returns a TYPE_DECL for the the type. */
8862 cp_parser_type_name (parser)
8868 /* We can't know yet whether it is a class-name or not. */
8869 cp_parser_parse_tentatively (parser);
8870 /* Try a class-name. */
8871 type_decl = cp_parser_class_name (parser,
8872 /*typename_keyword_p=*/false,
8873 /*template_keyword_p=*/false,
8875 /*check_access_p=*/true,
8876 /*check_dependency_p=*/true,
8877 /*class_head_p=*/false);
8878 /* If it's not a class-name, keep looking. */
8879 if (!cp_parser_parse_definitely (parser))
8881 /* It must be a typedef-name or an enum-name. */
8882 identifier = cp_parser_identifier (parser);
8883 if (identifier == error_mark_node)
8884 return error_mark_node;
8886 /* Look up the type-name. */
8887 type_decl = cp_parser_lookup_name_simple (parser, identifier);
8888 /* Issue an error if we did not find a type-name. */
8889 if (TREE_CODE (type_decl) != TYPE_DECL)
8891 cp_parser_error (parser, "expected type-name");
8892 type_decl = error_mark_node;
8894 /* Remember that the name was used in the definition of the
8895 current class so that we can check later to see if the
8896 meaning would have been different after the class was
8897 entirely defined. */
8898 else if (type_decl != error_mark_node
8900 maybe_note_name_used_in_class (identifier, type_decl);
8907 /* Parse an elaborated-type-specifier. Note that the grammar given
8908 here incorporates the resolution to DR68.
8910 elaborated-type-specifier:
8911 class-key :: [opt] nested-name-specifier [opt] identifier
8912 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
8913 enum :: [opt] nested-name-specifier [opt] identifier
8914 typename :: [opt] nested-name-specifier identifier
8915 typename :: [opt] nested-name-specifier template [opt]
8918 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
8919 declared `friend'. If IS_DECLARATION is TRUE, then this
8920 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
8921 something is being declared.
8923 Returns the TYPE specified. */
8926 cp_parser_elaborated_type_specifier (parser, is_friend, is_declaration)
8929 bool is_declaration;
8931 enum tag_types tag_type;
8933 tree type = NULL_TREE;
8935 /* See if we're looking at the `enum' keyword. */
8936 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
8938 /* Consume the `enum' token. */
8939 cp_lexer_consume_token (parser->lexer);
8940 /* Remember that it's an enumeration type. */
8941 tag_type = enum_type;
8943 /* Or, it might be `typename'. */
8944 else if (cp_lexer_next_token_is_keyword (parser->lexer,
8947 /* Consume the `typename' token. */
8948 cp_lexer_consume_token (parser->lexer);
8949 /* Remember that it's a `typename' type. */
8950 tag_type = typename_type;
8951 /* The `typename' keyword is only allowed in templates. */
8952 if (!processing_template_decl)
8953 pedwarn ("using `typename' outside of template");
8955 /* Otherwise it must be a class-key. */
8958 tag_type = cp_parser_class_key (parser);
8959 if (tag_type == none_type)
8960 return error_mark_node;
8963 /* Look for the `::' operator. */
8964 cp_parser_global_scope_opt (parser,
8965 /*current_scope_valid_p=*/false);
8966 /* Look for the nested-name-specifier. */
8967 if (tag_type == typename_type)
8968 cp_parser_nested_name_specifier (parser,
8969 /*typename_keyword_p=*/true,
8970 /*check_dependency_p=*/true,
8973 /* Even though `typename' is not present, the proposed resolution
8974 to Core Issue 180 says that in `class A<T>::B', `B' should be
8975 considered a type-name, even if `A<T>' is dependent. */
8976 cp_parser_nested_name_specifier_opt (parser,
8977 /*typename_keyword_p=*/true,
8978 /*check_dependency_p=*/true,
8980 /* For everything but enumeration types, consider a template-id. */
8981 if (tag_type != enum_type)
8983 bool template_p = false;
8986 /* Allow the `template' keyword. */
8987 template_p = cp_parser_optional_template_keyword (parser);
8988 /* If we didn't see `template', we don't know if there's a
8989 template-id or not. */
8991 cp_parser_parse_tentatively (parser);
8992 /* Parse the template-id. */
8993 decl = cp_parser_template_id (parser, template_p,
8994 /*check_dependency_p=*/true);
8995 /* If we didn't find a template-id, look for an ordinary
8997 if (!template_p && !cp_parser_parse_definitely (parser))
8999 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
9000 in effect, then we must assume that, upon instantiation, the
9001 template will correspond to a class. */
9002 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
9003 && tag_type == typename_type)
9004 type = make_typename_type (parser->scope, decl,
9007 type = TREE_TYPE (decl);
9010 /* For an enumeration type, consider only a plain identifier. */
9013 identifier = cp_parser_identifier (parser);
9015 if (identifier == error_mark_node)
9016 return error_mark_node;
9018 /* For a `typename', we needn't call xref_tag. */
9019 if (tag_type == typename_type)
9020 return make_typename_type (parser->scope, identifier,
9022 /* Look up a qualified name in the usual way. */
9027 /* In an elaborated-type-specifier, names are assumed to name
9028 types, so we set IS_TYPE to TRUE when calling
9029 cp_parser_lookup_name. */
9030 decl = cp_parser_lookup_name (parser, identifier,
9031 /*check_access=*/true,
9033 /*is_namespace=*/false,
9034 /*check_dependency=*/true);
9035 decl = (cp_parser_maybe_treat_template_as_class
9036 (decl, /*tag_name_p=*/is_friend));
9038 if (TREE_CODE (decl) != TYPE_DECL)
9040 error ("expected type-name");
9041 return error_mark_node;
9043 else if (TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE
9044 && tag_type != enum_type)
9045 error ("`%T' referred to as `%s'", TREE_TYPE (decl),
9046 tag_type == record_type ? "struct" : "class");
9047 else if (TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE
9048 && tag_type == enum_type)
9049 error ("`%T' referred to as enum", TREE_TYPE (decl));
9051 type = TREE_TYPE (decl);
9055 /* An elaborated-type-specifier sometimes introduces a new type and
9056 sometimes names an existing type. Normally, the rule is that it
9057 introduces a new type only if there is not an existing type of
9058 the same name already in scope. For example, given:
9061 void f() { struct S s; }
9063 the `struct S' in the body of `f' is the same `struct S' as in
9064 the global scope; the existing definition is used. However, if
9065 there were no global declaration, this would introduce a new
9066 local class named `S'.
9068 An exception to this rule applies to the following code:
9070 namespace N { struct S; }
9072 Here, the elaborated-type-specifier names a new type
9073 unconditionally; even if there is already an `S' in the
9074 containing scope this declaration names a new type.
9075 This exception only applies if the elaborated-type-specifier
9076 forms the complete declaration:
9080 A declaration consisting solely of `class-key identifier ;' is
9081 either a redeclaration of the name in the current scope or a
9082 forward declaration of the identifier as a class name. It
9083 introduces the name into the current scope.
9085 We are in this situation precisely when the next token is a `;'.
9087 An exception to the exception is that a `friend' declaration does
9088 *not* name a new type; i.e., given:
9090 struct S { friend struct T; };
9092 `T' is not a new type in the scope of `S'.
9094 Also, `new struct S' or `sizeof (struct S)' never results in the
9095 definition of a new type; a new type can only be declared in a
9096 declaration context. */
9098 type = xref_tag (tag_type, identifier,
9099 /*attributes=*/NULL_TREE,
9102 || cp_lexer_next_token_is_not (parser->lexer,
9106 if (tag_type != enum_type)
9107 cp_parser_check_class_key (tag_type, type);
9111 /* Parse an enum-specifier.
9114 enum identifier [opt] { enumerator-list [opt] }
9116 Returns an ENUM_TYPE representing the enumeration. */
9119 cp_parser_enum_specifier (parser)
9123 tree identifier = NULL_TREE;
9126 /* Look for the `enum' keyword. */
9127 if (!cp_parser_require_keyword (parser, RID_ENUM, "`enum'"))
9128 return error_mark_node;
9129 /* Peek at the next token. */
9130 token = cp_lexer_peek_token (parser->lexer);
9132 /* See if it is an identifier. */
9133 if (token->type == CPP_NAME)
9134 identifier = cp_parser_identifier (parser);
9136 /* Look for the `{'. */
9137 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
9138 return error_mark_node;
9140 /* At this point, we're going ahead with the enum-specifier, even
9141 if some other problem occurs. */
9142 cp_parser_commit_to_tentative_parse (parser);
9144 /* Issue an error message if type-definitions are forbidden here. */
9145 cp_parser_check_type_definition (parser);
9147 /* Create the new type. */
9148 type = start_enum (identifier ? identifier : make_anon_name ());
9150 /* Peek at the next token. */
9151 token = cp_lexer_peek_token (parser->lexer);
9152 /* If it's not a `}', then there are some enumerators. */
9153 if (token->type != CPP_CLOSE_BRACE)
9154 cp_parser_enumerator_list (parser, type);
9155 /* Look for the `}'. */
9156 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
9158 /* Finish up the enumeration. */
9164 /* Parse an enumerator-list. The enumerators all have the indicated
9168 enumerator-definition
9169 enumerator-list , enumerator-definition */
9172 cp_parser_enumerator_list (parser, type)
9180 /* Parse an enumerator-definition. */
9181 cp_parser_enumerator_definition (parser, type);
9182 /* Peek at the next token. */
9183 token = cp_lexer_peek_token (parser->lexer);
9184 /* If it's not a `,', then we've reached the end of the
9186 if (token->type != CPP_COMMA)
9188 /* Otherwise, consume the `,' and keep going. */
9189 cp_lexer_consume_token (parser->lexer);
9190 /* If the next token is a `}', there is a trailing comma. */
9191 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
9193 if (pedantic && !in_system_header)
9194 pedwarn ("comma at end of enumerator list");
9200 /* Parse an enumerator-definition. The enumerator has the indicated
9203 enumerator-definition:
9205 enumerator = constant-expression
9211 cp_parser_enumerator_definition (parser, type)
9219 /* Look for the identifier. */
9220 identifier = cp_parser_identifier (parser);
9221 if (identifier == error_mark_node)
9224 /* Peek at the next token. */
9225 token = cp_lexer_peek_token (parser->lexer);
9226 /* If it's an `=', then there's an explicit value. */
9227 if (token->type == CPP_EQ)
9229 /* Consume the `=' token. */
9230 cp_lexer_consume_token (parser->lexer);
9231 /* Parse the value. */
9232 value = cp_parser_constant_expression (parser);
9237 /* Create the enumerator. */
9238 build_enumerator (identifier, value, type);
9241 /* Parse a namespace-name.
9244 original-namespace-name
9247 Returns the NAMESPACE_DECL for the namespace. */
9250 cp_parser_namespace_name (parser)
9254 tree namespace_decl;
9256 /* Get the name of the namespace. */
9257 identifier = cp_parser_identifier (parser);
9258 if (identifier == error_mark_node)
9259 return error_mark_node;
9261 /* Look up the identifier in the currently active scope. Look only
9262 for namespaces, due to:
9266 When looking up a namespace-name in a using-directive or alias
9267 definition, only namespace names are considered.
9273 During the lookup of a name preceding the :: scope resolution
9274 operator, object, function, and enumerator names are ignored.
9276 (Note that cp_parser_class_or_namespace_name only calls this
9277 function if the token after the name is the scope resolution
9279 namespace_decl = cp_parser_lookup_name (parser, identifier,
9280 /*check_access=*/true,
9282 /*is_namespace=*/true,
9283 /*check_dependency=*/true);
9284 /* If it's not a namespace, issue an error. */
9285 if (namespace_decl == error_mark_node
9286 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
9288 cp_parser_error (parser, "expected namespace-name");
9289 namespace_decl = error_mark_node;
9292 return namespace_decl;
9295 /* Parse a namespace-definition.
9297 namespace-definition:
9298 named-namespace-definition
9299 unnamed-namespace-definition
9301 named-namespace-definition:
9302 original-namespace-definition
9303 extension-namespace-definition
9305 original-namespace-definition:
9306 namespace identifier { namespace-body }
9308 extension-namespace-definition:
9309 namespace original-namespace-name { namespace-body }
9311 unnamed-namespace-definition:
9312 namespace { namespace-body } */
9315 cp_parser_namespace_definition (parser)
9320 /* Look for the `namespace' keyword. */
9321 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
9323 /* Get the name of the namespace. We do not attempt to distinguish
9324 between an original-namespace-definition and an
9325 extension-namespace-definition at this point. The semantic
9326 analysis routines are responsible for that. */
9327 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
9328 identifier = cp_parser_identifier (parser);
9330 identifier = NULL_TREE;
9332 /* Look for the `{' to start the namespace. */
9333 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
9334 /* Start the namespace. */
9335 push_namespace (identifier);
9336 /* Parse the body of the namespace. */
9337 cp_parser_namespace_body (parser);
9338 /* Finish the namespace. */
9340 /* Look for the final `}'. */
9341 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
9344 /* Parse a namespace-body.
9347 declaration-seq [opt] */
9350 cp_parser_namespace_body (parser)
9353 cp_parser_declaration_seq_opt (parser);
9356 /* Parse a namespace-alias-definition.
9358 namespace-alias-definition:
9359 namespace identifier = qualified-namespace-specifier ; */
9362 cp_parser_namespace_alias_definition (parser)
9366 tree namespace_specifier;
9368 /* Look for the `namespace' keyword. */
9369 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
9370 /* Look for the identifier. */
9371 identifier = cp_parser_identifier (parser);
9372 if (identifier == error_mark_node)
9374 /* Look for the `=' token. */
9375 cp_parser_require (parser, CPP_EQ, "`='");
9376 /* Look for the qualified-namespace-specifier. */
9378 = cp_parser_qualified_namespace_specifier (parser);
9379 /* Look for the `;' token. */
9380 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
9382 /* Register the alias in the symbol table. */
9383 do_namespace_alias (identifier, namespace_specifier);
9386 /* Parse a qualified-namespace-specifier.
9388 qualified-namespace-specifier:
9389 :: [opt] nested-name-specifier [opt] namespace-name
9391 Returns a NAMESPACE_DECL corresponding to the specified
9395 cp_parser_qualified_namespace_specifier (parser)
9398 /* Look for the optional `::'. */
9399 cp_parser_global_scope_opt (parser,
9400 /*current_scope_valid_p=*/false);
9402 /* Look for the optional nested-name-specifier. */
9403 cp_parser_nested_name_specifier_opt (parser,
9404 /*typename_keyword_p=*/false,
9405 /*check_dependency_p=*/true,
9408 return cp_parser_namespace_name (parser);
9411 /* Parse a using-declaration.
9414 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
9415 using :: unqualified-id ; */
9418 cp_parser_using_declaration (parser)
9422 bool typename_p = false;
9423 bool global_scope_p;
9428 /* Look for the `using' keyword. */
9429 cp_parser_require_keyword (parser, RID_USING, "`using'");
9431 /* Peek at the next token. */
9432 token = cp_lexer_peek_token (parser->lexer);
9433 /* See if it's `typename'. */
9434 if (token->keyword == RID_TYPENAME)
9436 /* Remember that we've seen it. */
9438 /* Consume the `typename' token. */
9439 cp_lexer_consume_token (parser->lexer);
9442 /* Look for the optional global scope qualification. */
9444 = (cp_parser_global_scope_opt (parser,
9445 /*current_scope_valid_p=*/false)
9448 /* If we saw `typename', or didn't see `::', then there must be a
9449 nested-name-specifier present. */
9450 if (typename_p || !global_scope_p)
9451 cp_parser_nested_name_specifier (parser, typename_p,
9452 /*check_dependency_p=*/true,
9454 /* Otherwise, we could be in either of the two productions. In that
9455 case, treat the nested-name-specifier as optional. */
9457 cp_parser_nested_name_specifier_opt (parser,
9458 /*typename_keyword_p=*/false,
9459 /*check_dependency_p=*/true,
9462 /* Parse the unqualified-id. */
9463 identifier = cp_parser_unqualified_id (parser,
9464 /*template_keyword_p=*/false,
9465 /*check_dependency_p=*/true);
9467 /* The function we call to handle a using-declaration is different
9468 depending on what scope we are in. */
9469 scope = current_scope ();
9470 if (scope && TYPE_P (scope))
9472 /* Create the USING_DECL. */
9473 decl = do_class_using_decl (build_nt (SCOPE_REF,
9476 /* Add it to the list of members in this class. */
9477 finish_member_declaration (decl);
9481 decl = cp_parser_lookup_name_simple (parser, identifier);
9483 do_local_using_decl (decl);
9485 do_toplevel_using_decl (decl);
9488 /* Look for the final `;'. */
9489 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
9492 /* Parse a using-directive.
9495 using namespace :: [opt] nested-name-specifier [opt]
9499 cp_parser_using_directive (parser)
9502 tree namespace_decl;
9504 /* Look for the `using' keyword. */
9505 cp_parser_require_keyword (parser, RID_USING, "`using'");
9506 /* And the `namespace' keyword. */
9507 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
9508 /* Look for the optional `::' operator. */
9509 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
9510 /* And the optional nested-name-sepcifier. */
9511 cp_parser_nested_name_specifier_opt (parser,
9512 /*typename_keyword_p=*/false,
9513 /*check_dependency_p=*/true,
9515 /* Get the namespace being used. */
9516 namespace_decl = cp_parser_namespace_name (parser);
9517 /* Update the symbol table. */
9518 do_using_directive (namespace_decl);
9519 /* Look for the final `;'. */
9520 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
9523 /* Parse an asm-definition.
9526 asm ( string-literal ) ;
9531 asm volatile [opt] ( string-literal ) ;
9532 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
9533 asm volatile [opt] ( string-literal : asm-operand-list [opt]
9534 : asm-operand-list [opt] ) ;
9535 asm volatile [opt] ( string-literal : asm-operand-list [opt]
9536 : asm-operand-list [opt]
9537 : asm-operand-list [opt] ) ; */
9540 cp_parser_asm_definition (parser)
9545 tree outputs = NULL_TREE;
9546 tree inputs = NULL_TREE;
9547 tree clobbers = NULL_TREE;
9549 bool volatile_p = false;
9550 bool extended_p = false;
9552 /* Look for the `asm' keyword. */
9553 cp_parser_require_keyword (parser, RID_ASM, "`asm'");
9554 /* See if the next token is `volatile'. */
9555 if (cp_parser_allow_gnu_extensions_p (parser)
9556 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
9558 /* Remember that we saw the `volatile' keyword. */
9560 /* Consume the token. */
9561 cp_lexer_consume_token (parser->lexer);
9563 /* Look for the opening `('. */
9564 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
9565 /* Look for the string. */
9566 token = cp_parser_require (parser, CPP_STRING, "asm body");
9569 string = token->value;
9570 /* If we're allowing GNU extensions, check for the extended assembly
9571 syntax. Unfortunately, the `:' tokens need not be separated by
9572 a space in C, and so, for compatibility, we tolerate that here
9573 too. Doing that means that we have to treat the `::' operator as
9575 if (cp_parser_allow_gnu_extensions_p (parser)
9576 && at_function_scope_p ()
9577 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
9578 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
9580 bool inputs_p = false;
9581 bool clobbers_p = false;
9583 /* The extended syntax was used. */
9586 /* Look for outputs. */
9587 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
9589 /* Consume the `:'. */
9590 cp_lexer_consume_token (parser->lexer);
9591 /* Parse the output-operands. */
9592 if (cp_lexer_next_token_is_not (parser->lexer,
9594 && cp_lexer_next_token_is_not (parser->lexer,
9596 outputs = cp_parser_asm_operand_list (parser);
9598 /* If the next token is `::', there are no outputs, and the
9599 next token is the beginning of the inputs. */
9600 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
9602 /* Consume the `::' token. */
9603 cp_lexer_consume_token (parser->lexer);
9604 /* The inputs are coming next. */
9608 /* Look for inputs. */
9610 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
9613 /* Consume the `:'. */
9614 cp_lexer_consume_token (parser->lexer);
9615 /* Parse the output-operands. */
9616 if (cp_lexer_next_token_is_not (parser->lexer,
9618 && cp_lexer_next_token_is_not (parser->lexer,
9620 inputs = cp_parser_asm_operand_list (parser);
9622 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
9623 /* The clobbers are coming next. */
9626 /* Look for clobbers. */
9628 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
9631 /* Consume the `:'. */
9632 cp_lexer_consume_token (parser->lexer);
9633 /* Parse the clobbers. */
9634 clobbers = cp_parser_asm_clobber_list (parser);
9637 /* Look for the closing `)'. */
9638 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
9639 cp_parser_skip_to_closing_parenthesis (parser);
9640 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
9642 /* Create the ASM_STMT. */
9643 if (at_function_scope_p ())
9646 finish_asm_stmt (volatile_p
9647 ? ridpointers[(int) RID_VOLATILE] : NULL_TREE,
9648 string, outputs, inputs, clobbers);
9649 /* If the extended syntax was not used, mark the ASM_STMT. */
9651 ASM_INPUT_P (asm_stmt) = 1;
9654 assemble_asm (string);
9657 /* Declarators [gram.dcl.decl] */
9659 /* Parse an init-declarator.
9662 declarator initializer [opt]
9667 declarator asm-specification [opt] attributes [opt] initializer [opt]
9669 The DECL_SPECIFIERS and PREFIX_ATTRIBUTES apply to this declarator.
9670 Returns a reprsentation of the entity declared. The ACCESS_CHECKS
9671 represent deferred access checks from the decl-specifier-seq. If
9672 MEMBER_P is TRUE, then this declarator appears in a class scope.
9673 The new DECL created by this declarator is returned.
9675 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
9676 for a function-definition here as well. If the declarator is a
9677 declarator for a function-definition, *FUNCTION_DEFINITION_P will
9678 be TRUE upon return. By that point, the function-definition will
9679 have been completely parsed.
9681 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
9685 cp_parser_init_declarator (parser,
9689 function_definition_allowed_p,
9691 function_definition_p)
9693 tree decl_specifiers;
9694 tree prefix_attributes;
9696 bool function_definition_allowed_p;
9698 bool *function_definition_p;
9703 tree asm_specification;
9705 tree decl = NULL_TREE;
9707 tree declarator_access_checks;
9708 bool is_initialized;
9709 bool is_parenthesized_init;
9710 bool ctor_dtor_or_conv_p;
9713 /* Assume that this is not the declarator for a function
9715 if (function_definition_p)
9716 *function_definition_p = false;
9718 /* Defer access checks while parsing the declarator; we cannot know
9719 what names are accessible until we know what is being
9721 cp_parser_start_deferring_access_checks (parser);
9722 /* Parse the declarator. */
9724 = cp_parser_declarator (parser,
9725 /*abstract_p=*/false,
9726 &ctor_dtor_or_conv_p);
9727 /* Gather up the deferred checks. */
9728 declarator_access_checks
9729 = cp_parser_stop_deferring_access_checks (parser);
9731 /* Prevent the access checks from being reclaimed by GC. */
9732 parser->access_checks_lists
9733 = tree_cons (NULL_TREE, declarator_access_checks,
9734 parser->access_checks_lists);
9736 /* If the DECLARATOR was erroneous, there's no need to go
9738 if (declarator == error_mark_node)
9740 /* Discard access checks no longer in use. */
9741 parser->access_checks_lists
9742 = TREE_CHAIN (parser->access_checks_lists);
9743 return error_mark_node;
9746 /* Figure out what scope the entity declared by the DECLARATOR is
9747 located in. `grokdeclarator' sometimes changes the scope, so
9748 we compute it now. */
9749 scope = get_scope_of_declarator (declarator);
9751 /* If we're allowing GNU extensions, look for an asm-specification
9753 if (cp_parser_allow_gnu_extensions_p (parser))
9755 /* Look for an asm-specification. */
9756 asm_specification = cp_parser_asm_specification_opt (parser);
9757 /* And attributes. */
9758 attributes = cp_parser_attributes_opt (parser);
9762 asm_specification = NULL_TREE;
9763 attributes = NULL_TREE;
9766 /* Peek at the next token. */
9767 token = cp_lexer_peek_token (parser->lexer);
9768 /* Check to see if the token indicates the start of a
9769 function-definition. */
9770 if (cp_parser_token_starts_function_definition_p (token))
9772 if (!function_definition_allowed_p)
9774 /* If a function-definition should not appear here, issue an
9776 cp_parser_error (parser,
9777 "a function-definition is not allowed here");
9778 /* Discard access checks no longer in use. */
9779 parser->access_checks_lists
9780 = TREE_CHAIN (parser->access_checks_lists);
9781 return error_mark_node;
9787 /* Neither attributes nor an asm-specification are allowed
9788 on a function-definition. */
9789 if (asm_specification)
9790 error ("an asm-specification is not allowed on a function-definition");
9792 error ("attributes are not allowed on a function-definition");
9793 /* This is a function-definition. */
9794 *function_definition_p = true;
9796 /* Thread the access checks together. */
9797 ac = &access_checks;
9799 ac = &TREE_CHAIN (*ac);
9800 *ac = declarator_access_checks;
9802 /* Parse the function definition. */
9803 decl = (cp_parser_function_definition_from_specifiers_and_declarator
9804 (parser, decl_specifiers, prefix_attributes, declarator,
9807 /* Pull the access-checks apart again. */
9810 /* Discard access checks no longer in use. */
9811 parser->access_checks_lists
9812 = TREE_CHAIN (parser->access_checks_lists);
9820 Only in function declarations for constructors, destructors, and
9821 type conversions can the decl-specifier-seq be omitted.
9823 We explicitly postpone this check past the point where we handle
9824 function-definitions because we tolerate function-definitions
9825 that are missing their return types in some modes. */
9826 if (!decl_specifiers && !ctor_dtor_or_conv_p)
9828 cp_parser_error (parser,
9829 "expected constructor, destructor, or type conversion");
9830 /* Discard access checks no longer in use. */
9831 parser->access_checks_lists
9832 = TREE_CHAIN (parser->access_checks_lists);
9833 return error_mark_node;
9836 /* An `=' or an `(' indicates an initializer. */
9837 is_initialized = (token->type == CPP_EQ
9838 || token->type == CPP_OPEN_PAREN);
9839 /* If the init-declarator isn't initialized and isn't followed by a
9840 `,' or `;', it's not a valid init-declarator. */
9842 && token->type != CPP_COMMA
9843 && token->type != CPP_SEMICOLON)
9845 cp_parser_error (parser, "expected init-declarator");
9846 /* Discard access checks no longer in use. */
9847 parser->access_checks_lists
9848 = TREE_CHAIN (parser->access_checks_lists);
9849 return error_mark_node;
9852 /* Because start_decl has side-effects, we should only call it if we
9853 know we're going ahead. By this point, we know that we cannot
9854 possibly be looking at any other construct. */
9855 cp_parser_commit_to_tentative_parse (parser);
9857 /* Check to see whether or not this declaration is a friend. */
9858 friend_p = cp_parser_friend_p (decl_specifiers);
9860 /* Check that the number of template-parameter-lists is OK. */
9861 if (!cp_parser_check_declarator_template_parameters (parser,
9864 /* Discard access checks no longer in use. */
9865 parser->access_checks_lists
9866 = TREE_CHAIN (parser->access_checks_lists);
9867 return error_mark_node;
9870 /* Enter the newly declared entry in the symbol table. If we're
9871 processing a declaration in a class-specifier, we wait until
9872 after processing the initializer. */
9875 if (parser->in_unbraced_linkage_specification_p)
9877 decl_specifiers = tree_cons (error_mark_node,
9878 get_identifier ("extern"),
9880 have_extern_spec = false;
9882 decl = start_decl (declarator,
9889 /* Enter the SCOPE. That way unqualified names appearing in the
9890 initializer will be looked up in SCOPE. */
9894 /* Perform deferred access control checks, now that we know in which
9895 SCOPE the declared entity resides. */
9896 if (!member_p && decl)
9898 tree saved_current_function_decl = NULL_TREE;
9900 /* If the entity being declared is a function, pretend that we
9901 are in its scope. If it is a `friend', it may have access to
9902 things that would not otherwise be accessible. */
9903 if (TREE_CODE (decl) == FUNCTION_DECL)
9905 saved_current_function_decl = current_function_decl;
9906 current_function_decl = decl;
9909 /* Perform the access control checks for the decl-specifiers. */
9910 cp_parser_perform_deferred_access_checks (access_checks);
9911 /* And for the declarator. */
9912 cp_parser_perform_deferred_access_checks (declarator_access_checks);
9914 /* Restore the saved value. */
9915 if (TREE_CODE (decl) == FUNCTION_DECL)
9916 current_function_decl = saved_current_function_decl;
9919 /* Parse the initializer. */
9921 initializer = cp_parser_initializer (parser,
9922 &is_parenthesized_init);
9925 initializer = NULL_TREE;
9926 is_parenthesized_init = false;
9929 /* The old parser allows attributes to appear after a parenthesized
9930 initializer. Mark Mitchell proposed removing this functionality
9931 on the GCC mailing lists on 2002-08-13. This parser accepts the
9932 attributes -- but ignores them. */
9933 if (cp_parser_allow_gnu_extensions_p (parser) && is_parenthesized_init)
9934 if (cp_parser_attributes_opt (parser))
9935 warning ("attributes after parenthesized initializer ignored");
9937 /* Leave the SCOPE, now that we have processed the initializer. It
9938 is important to do this before calling cp_finish_decl because it
9939 makes decisions about whether to create DECL_STMTs or not based
9940 on the current scope. */
9944 /* For an in-class declaration, use `grokfield' to create the
9947 decl = grokfield (declarator, decl_specifiers,
9948 initializer, /*asmspec=*/NULL_TREE,
9949 /*attributes=*/NULL_TREE);
9951 /* Finish processing the declaration. But, skip friend
9953 if (!friend_p && decl)
9954 cp_finish_decl (decl,
9957 /* If the initializer is in parentheses, then this is
9958 a direct-initialization, which means that an
9959 `explicit' constructor is OK. Otherwise, an
9960 `explicit' constructor cannot be used. */
9961 ((is_parenthesized_init || !is_initialized)
9962 ? 0 : LOOKUP_ONLYCONVERTING));
9964 /* Discard access checks no longer in use. */
9965 parser->access_checks_lists
9966 = TREE_CHAIN (parser->access_checks_lists);
9971 /* Parse a declarator.
9975 ptr-operator declarator
9977 abstract-declarator:
9978 ptr-operator abstract-declarator [opt]
9979 direct-abstract-declarator
9984 attributes [opt] direct-declarator
9985 attributes [opt] ptr-operator declarator
9987 abstract-declarator:
9988 attributes [opt] ptr-operator abstract-declarator [opt]
9989 attributes [opt] direct-abstract-declarator
9991 Returns a representation of the declarator. If the declarator has
9992 the form `* declarator', then an INDIRECT_REF is returned, whose
9993 only operand is the sub-declarator. Analagously, `& declarator' is
9994 represented as an ADDR_EXPR. For `X::* declarator', a SCOPE_REF is
9995 used. The first operand is the TYPE for `X'. The second operand
9996 is an INDIRECT_REF whose operand is the sub-declarator.
9998 Otherwise, the reprsentation is as for a direct-declarator.
10000 (It would be better to define a structure type to represent
10001 declarators, rather than abusing `tree' nodes to represent
10002 declarators. That would be much clearer and save some memory.
10003 There is no reason for declarators to be garbage-collected, for
10004 example; they are created during parser and no longer needed after
10005 `grokdeclarator' has been called.)
10007 For a ptr-operator that has the optional cv-qualifier-seq,
10008 cv-qualifiers will be stored in the TREE_TYPE of the INDIRECT_REF
10011 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is set to
10012 true if this declarator represents a constructor, destructor, or
10013 type conversion operator. Otherwise, it is set to false.
10015 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
10016 a decl-specifier-seq unless it declares a constructor, destructor,
10017 or conversion. It might seem that we could check this condition in
10018 semantic analysis, rather than parsing, but that makes it difficult
10019 to handle something like `f()'. We want to notice that there are
10020 no decl-specifiers, and therefore realize that this is an
10021 expression, not a declaration.) */
10024 cp_parser_declarator (parser, abstract_p, ctor_dtor_or_conv_p)
10027 bool *ctor_dtor_or_conv_p;
10031 enum tree_code code;
10032 tree cv_qualifier_seq;
10034 tree attributes = NULL_TREE;
10036 /* Assume this is not a constructor, destructor, or type-conversion
10038 if (ctor_dtor_or_conv_p)
10039 *ctor_dtor_or_conv_p = false;
10041 if (cp_parser_allow_gnu_extensions_p (parser))
10042 attributes = cp_parser_attributes_opt (parser);
10044 /* Peek at the next token. */
10045 token = cp_lexer_peek_token (parser->lexer);
10047 /* Check for the ptr-operator production. */
10048 cp_parser_parse_tentatively (parser);
10049 /* Parse the ptr-operator. */
10050 code = cp_parser_ptr_operator (parser,
10052 &cv_qualifier_seq);
10053 /* If that worked, then we have a ptr-operator. */
10054 if (cp_parser_parse_definitely (parser))
10056 /* The dependent declarator is optional if we are parsing an
10057 abstract-declarator. */
10059 cp_parser_parse_tentatively (parser);
10061 /* Parse the dependent declarator. */
10062 declarator = cp_parser_declarator (parser, abstract_p,
10063 /*ctor_dtor_or_conv_p=*/NULL);
10065 /* If we are parsing an abstract-declarator, we must handle the
10066 case where the dependent declarator is absent. */
10067 if (abstract_p && !cp_parser_parse_definitely (parser))
10068 declarator = NULL_TREE;
10070 /* Build the representation of the ptr-operator. */
10071 if (code == INDIRECT_REF)
10072 declarator = make_pointer_declarator (cv_qualifier_seq,
10075 declarator = make_reference_declarator (cv_qualifier_seq,
10077 /* Handle the pointer-to-member case. */
10079 declarator = build_nt (SCOPE_REF, class_type, declarator);
10081 /* Everything else is a direct-declarator. */
10083 declarator = cp_parser_direct_declarator (parser,
10085 ctor_dtor_or_conv_p);
10087 if (attributes && declarator != error_mark_node)
10088 declarator = tree_cons (attributes, declarator, NULL_TREE);
10093 /* Parse a direct-declarator or direct-abstract-declarator.
10097 direct-declarator ( parameter-declaration-clause )
10098 cv-qualifier-seq [opt]
10099 exception-specification [opt]
10100 direct-declarator [ constant-expression [opt] ]
10103 direct-abstract-declarator:
10104 direct-abstract-declarator [opt]
10105 ( parameter-declaration-clause )
10106 cv-qualifier-seq [opt]
10107 exception-specification [opt]
10108 direct-abstract-declarator [opt] [ constant-expression [opt] ]
10109 ( abstract-declarator )
10111 Returns a representation of the declarator. ABSTRACT_P is TRUE if
10112 we are parsing a direct-abstract-declarator; FALSE if we are
10113 parsing a direct-declarator. CTOR_DTOR_OR_CONV_P is as for
10114 cp_parser_declarator.
10116 For the declarator-id production, the representation is as for an
10117 id-expression, except that a qualified name is represented as a
10118 SCOPE_REF. A function-declarator is represented as a CALL_EXPR;
10119 see the documentation of the FUNCTION_DECLARATOR_* macros for
10120 information about how to find the various declarator components.
10121 An array-declarator is represented as an ARRAY_REF. The
10122 direct-declarator is the first operand; the constant-expression
10123 indicating the size of the array is the second operand. */
10126 cp_parser_direct_declarator (parser, abstract_p, ctor_dtor_or_conv_p)
10129 bool *ctor_dtor_or_conv_p;
10133 tree scope = NULL_TREE;
10134 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
10135 bool saved_in_declarator_p = parser->in_declarator_p;
10137 /* Peek at the next token. */
10138 token = cp_lexer_peek_token (parser->lexer);
10139 /* Find the initial direct-declarator. It might be a parenthesized
10141 if (token->type == CPP_OPEN_PAREN)
10145 /* For an abstract declarator we do not know whether we are
10146 looking at the beginning of a parameter-declaration-clause,
10147 or at a parenthesized abstract declarator. For example, if
10148 we see `(int)', we are looking at a
10149 parameter-declaration-clause, and the
10150 direct-abstract-declarator has been omitted. If, on the
10151 other hand we are looking at `((*))' then we are looking at a
10152 parenthesized abstract-declarator. There is no easy way to
10153 tell which situation we are in. */
10155 cp_parser_parse_tentatively (parser);
10157 /* Consume the `('. */
10158 cp_lexer_consume_token (parser->lexer);
10159 /* Parse the nested declarator. */
10161 = cp_parser_declarator (parser, abstract_p, ctor_dtor_or_conv_p);
10162 /* Expect a `)'. */
10163 error_p = !cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
10165 /* If parsing a parenthesized abstract declarator didn't work,
10166 try a parameter-declaration-clause. */
10167 if (abstract_p && !cp_parser_parse_definitely (parser))
10168 declarator = NULL_TREE;
10169 /* If we were not parsing an abstract declarator, but failed to
10170 find a satisfactory nested declarator, then an error has
10172 else if (!abstract_p
10173 && (declarator == error_mark_node || error_p))
10174 return error_mark_node;
10175 /* Default args cannot appear in an abstract decl. */
10176 parser->default_arg_ok_p = false;
10178 /* Otherwise, for a non-abstract declarator, there should be a
10180 else if (!abstract_p)
10182 declarator = cp_parser_declarator_id (parser);
10184 if (TREE_CODE (declarator) == SCOPE_REF)
10186 scope = TREE_OPERAND (declarator, 0);
10188 /* In the declaration of a member of a template class
10189 outside of the class itself, the SCOPE will sometimes be
10190 a TYPENAME_TYPE. For example, given:
10192 template <typename T>
10193 int S<T>::R::i = 3;
10195 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In this
10196 context, we must resolve S<T>::R to an ordinary type,
10197 rather than a typename type.
10199 The reason we normally avoid resolving TYPENAME_TYPEs is
10200 that a specialization of `S' might render `S<T>::R' not a
10201 type. However, if `S' is specialized, then this `i' will
10202 not be used, so there is no harm in resolving the types
10204 if (TREE_CODE (scope) == TYPENAME_TYPE)
10206 /* Resolve the TYPENAME_TYPE. */
10207 scope = cp_parser_resolve_typename_type (parser, scope);
10208 /* If that failed, the declarator is invalid. */
10209 if (scope == error_mark_node)
10210 return error_mark_node;
10211 /* Build a new DECLARATOR. */
10212 declarator = build_nt (SCOPE_REF,
10214 TREE_OPERAND (declarator, 1));
10217 else if (TREE_CODE (declarator) != IDENTIFIER_NODE)
10218 /* Default args can only appear for a function decl. */
10219 parser->default_arg_ok_p = false;
10221 /* Check to see whether the declarator-id names a constructor,
10222 destructor, or conversion. */
10223 if (ctor_dtor_or_conv_p
10224 && ((TREE_CODE (declarator) == SCOPE_REF
10225 && CLASS_TYPE_P (TREE_OPERAND (declarator, 0)))
10226 || (TREE_CODE (declarator) != SCOPE_REF
10227 && at_class_scope_p ())))
10229 tree unqualified_name;
10232 /* Get the unqualified part of the name. */
10233 if (TREE_CODE (declarator) == SCOPE_REF)
10235 class_type = TREE_OPERAND (declarator, 0);
10236 unqualified_name = TREE_OPERAND (declarator, 1);
10240 class_type = current_class_type;
10241 unqualified_name = declarator;
10244 /* See if it names ctor, dtor or conv. */
10245 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR
10246 || IDENTIFIER_TYPENAME_P (unqualified_name)
10247 || constructor_name_p (unqualified_name, class_type))
10249 *ctor_dtor_or_conv_p = true;
10250 /* We would have cleared the default arg flag above, but
10252 parser->default_arg_ok_p = saved_default_arg_ok_p;
10256 /* But for an abstract declarator, the initial direct-declarator can
10260 declarator = NULL_TREE;
10261 parser->default_arg_ok_p = false;
10264 scope = get_scope_of_declarator (declarator);
10266 /* Any names that appear after the declarator-id for a member
10267 are looked up in the containing scope. */
10268 push_scope (scope);
10271 parser->in_declarator_p = true;
10273 /* Now, parse function-declarators and array-declarators until there
10277 /* Peek at the next token. */
10278 token = cp_lexer_peek_token (parser->lexer);
10279 /* If it's a `[', we're looking at an array-declarator. */
10280 if (token->type == CPP_OPEN_SQUARE)
10284 /* Consume the `['. */
10285 cp_lexer_consume_token (parser->lexer);
10286 /* Peek at the next token. */
10287 token = cp_lexer_peek_token (parser->lexer);
10288 /* If the next token is `]', then there is no
10289 constant-expression. */
10290 if (token->type != CPP_CLOSE_SQUARE)
10291 bounds = cp_parser_constant_expression (parser);
10293 bounds = NULL_TREE;
10294 /* Look for the closing `]'. */
10295 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
10297 declarator = build_nt (ARRAY_REF, declarator, bounds);
10299 /* If it's a `(', we're looking at a function-declarator. */
10300 else if (token->type == CPP_OPEN_PAREN)
10302 /* A function-declarator. Or maybe not. Consider, for
10308 The first is the declaration of a function while the
10309 second is a the definition of a variable, including its
10312 Having seen only the parenthesis, we cannot know which of
10313 these two alternatives should be selected. Even more
10314 complex are examples like:
10319 The former is a function-declaration; the latter is a
10320 variable initialization.
10322 First, we attempt to parse a parameter-declaration
10323 clause. If this works, then we continue; otherwise, we
10324 replace the tokens consumed in the process and continue. */
10327 /* We are now parsing tentatively. */
10328 cp_parser_parse_tentatively (parser);
10330 /* Consume the `('. */
10331 cp_lexer_consume_token (parser->lexer);
10332 /* Parse the parameter-declaration-clause. */
10333 params = cp_parser_parameter_declaration_clause (parser);
10335 /* If all went well, parse the cv-qualifier-seq and the
10336 exception-specification. */
10337 if (cp_parser_parse_definitely (parser))
10339 tree cv_qualifiers;
10340 tree exception_specification;
10342 /* Consume the `)'. */
10343 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
10345 /* Parse the cv-qualifier-seq. */
10346 cv_qualifiers = cp_parser_cv_qualifier_seq_opt (parser);
10347 /* And the exception-specification. */
10348 exception_specification
10349 = cp_parser_exception_specification_opt (parser);
10351 /* Create the function-declarator. */
10352 declarator = make_call_declarator (declarator,
10355 exception_specification);
10357 /* Otherwise, we must be done with the declarator. */
10361 /* Otherwise, we're done with the declarator. */
10364 /* Any subsequent parameter lists are to do with return type, so
10365 are not those of the declared function. */
10366 parser->default_arg_ok_p = false;
10369 /* For an abstract declarator, we might wind up with nothing at this
10370 point. That's an error; the declarator is not optional. */
10372 cp_parser_error (parser, "expected declarator");
10374 /* If we entered a scope, we must exit it now. */
10378 parser->default_arg_ok_p = saved_default_arg_ok_p;
10379 parser->in_declarator_p = saved_in_declarator_p;
10384 /* Parse a ptr-operator.
10387 * cv-qualifier-seq [opt]
10389 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
10394 & cv-qualifier-seq [opt]
10396 Returns INDIRECT_REF if a pointer, or pointer-to-member, was
10397 used. Returns ADDR_EXPR if a reference was used. In the
10398 case of a pointer-to-member, *TYPE is filled in with the
10399 TYPE containing the member. *CV_QUALIFIER_SEQ is filled in
10400 with the cv-qualifier-seq, or NULL_TREE, if there are no
10401 cv-qualifiers. Returns ERROR_MARK if an error occurred. */
10403 static enum tree_code
10404 cp_parser_ptr_operator (parser, type, cv_qualifier_seq)
10407 tree *cv_qualifier_seq;
10409 enum tree_code code = ERROR_MARK;
10412 /* Assume that it's not a pointer-to-member. */
10414 /* And that there are no cv-qualifiers. */
10415 *cv_qualifier_seq = NULL_TREE;
10417 /* Peek at the next token. */
10418 token = cp_lexer_peek_token (parser->lexer);
10419 /* If it's a `*' or `&' we have a pointer or reference. */
10420 if (token->type == CPP_MULT || token->type == CPP_AND)
10422 /* Remember which ptr-operator we were processing. */
10423 code = (token->type == CPP_AND ? ADDR_EXPR : INDIRECT_REF);
10425 /* Consume the `*' or `&'. */
10426 cp_lexer_consume_token (parser->lexer);
10428 /* A `*' can be followed by a cv-qualifier-seq, and so can a
10429 `&', if we are allowing GNU extensions. (The only qualifier
10430 that can legally appear after `&' is `restrict', but that is
10431 enforced during semantic analysis. */
10432 if (code == INDIRECT_REF
10433 || cp_parser_allow_gnu_extensions_p (parser))
10434 *cv_qualifier_seq = cp_parser_cv_qualifier_seq_opt (parser);
10438 /* Try the pointer-to-member case. */
10439 cp_parser_parse_tentatively (parser);
10440 /* Look for the optional `::' operator. */
10441 cp_parser_global_scope_opt (parser,
10442 /*current_scope_valid_p=*/false);
10443 /* Look for the nested-name specifier. */
10444 cp_parser_nested_name_specifier (parser,
10445 /*typename_keyword_p=*/false,
10446 /*check_dependency_p=*/true,
10448 /* If we found it, and the next token is a `*', then we are
10449 indeed looking at a pointer-to-member operator. */
10450 if (!cp_parser_error_occurred (parser)
10451 && cp_parser_require (parser, CPP_MULT, "`*'"))
10453 /* The type of which the member is a member is given by the
10455 *type = parser->scope;
10456 /* The next name will not be qualified. */
10457 parser->scope = NULL_TREE;
10458 parser->qualifying_scope = NULL_TREE;
10459 parser->object_scope = NULL_TREE;
10460 /* Indicate that the `*' operator was used. */
10461 code = INDIRECT_REF;
10462 /* Look for the optional cv-qualifier-seq. */
10463 *cv_qualifier_seq = cp_parser_cv_qualifier_seq_opt (parser);
10465 /* If that didn't work we don't have a ptr-operator. */
10466 if (!cp_parser_parse_definitely (parser))
10467 cp_parser_error (parser, "expected ptr-operator");
10473 /* Parse an (optional) cv-qualifier-seq.
10476 cv-qualifier cv-qualifier-seq [opt]
10478 Returns a TREE_LIST. The TREE_VALUE of each node is the
10479 representation of a cv-qualifier. */
10482 cp_parser_cv_qualifier_seq_opt (parser)
10485 tree cv_qualifiers = NULL_TREE;
10491 /* Look for the next cv-qualifier. */
10492 cv_qualifier = cp_parser_cv_qualifier_opt (parser);
10493 /* If we didn't find one, we're done. */
10497 /* Add this cv-qualifier to the list. */
10499 = tree_cons (NULL_TREE, cv_qualifier, cv_qualifiers);
10502 /* We built up the list in reverse order. */
10503 return nreverse (cv_qualifiers);
10506 /* Parse an (optional) cv-qualifier.
10518 cp_parser_cv_qualifier_opt (parser)
10522 tree cv_qualifier = NULL_TREE;
10524 /* Peek at the next token. */
10525 token = cp_lexer_peek_token (parser->lexer);
10526 /* See if it's a cv-qualifier. */
10527 switch (token->keyword)
10532 /* Save the value of the token. */
10533 cv_qualifier = token->value;
10534 /* Consume the token. */
10535 cp_lexer_consume_token (parser->lexer);
10542 return cv_qualifier;
10545 /* Parse a declarator-id.
10549 :: [opt] nested-name-specifier [opt] type-name
10551 In the `id-expression' case, the value returned is as for
10552 cp_parser_id_expression if the id-expression was an unqualified-id.
10553 If the id-expression was a qualified-id, then a SCOPE_REF is
10554 returned. The first operand is the scope (either a NAMESPACE_DECL
10555 or TREE_TYPE), but the second is still just a representation of an
10559 cp_parser_declarator_id (parser)
10562 tree id_expression;
10564 /* The expression must be an id-expression. Assume that qualified
10565 names are the names of types so that:
10568 int S<T>::R::i = 3;
10570 will work; we must treat `S<T>::R' as the name of a type.
10571 Similarly, assume that qualified names are templates, where
10575 int S<T>::R<T>::i = 3;
10578 id_expression = cp_parser_id_expression (parser,
10579 /*template_keyword_p=*/false,
10580 /*check_dependency_p=*/false,
10581 /*template_p=*/NULL);
10582 /* If the name was qualified, create a SCOPE_REF to represent
10585 id_expression = build_nt (SCOPE_REF, parser->scope, id_expression);
10587 return id_expression;
10590 /* Parse a type-id.
10593 type-specifier-seq abstract-declarator [opt]
10595 Returns the TYPE specified. */
10598 cp_parser_type_id (parser)
10601 tree type_specifier_seq;
10602 tree abstract_declarator;
10604 /* Parse the type-specifier-seq. */
10606 = cp_parser_type_specifier_seq (parser);
10607 if (type_specifier_seq == error_mark_node)
10608 return error_mark_node;
10610 /* There might or might not be an abstract declarator. */
10611 cp_parser_parse_tentatively (parser);
10612 /* Look for the declarator. */
10613 abstract_declarator
10614 = cp_parser_declarator (parser, /*abstract_p=*/true, NULL);
10615 /* Check to see if there really was a declarator. */
10616 if (!cp_parser_parse_definitely (parser))
10617 abstract_declarator = NULL_TREE;
10619 return groktypename (build_tree_list (type_specifier_seq,
10620 abstract_declarator));
10623 /* Parse a type-specifier-seq.
10625 type-specifier-seq:
10626 type-specifier type-specifier-seq [opt]
10630 type-specifier-seq:
10631 attributes type-specifier-seq [opt]
10633 Returns a TREE_LIST. Either the TREE_VALUE of each node is a
10634 type-specifier, or the TREE_PURPOSE is a list of attributes. */
10637 cp_parser_type_specifier_seq (parser)
10640 bool seen_type_specifier = false;
10641 tree type_specifier_seq = NULL_TREE;
10643 /* Parse the type-specifiers and attributes. */
10646 tree type_specifier;
10648 /* Check for attributes first. */
10649 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
10651 type_specifier_seq = tree_cons (cp_parser_attributes_opt (parser),
10653 type_specifier_seq);
10657 /* After the first type-specifier, others are optional. */
10658 if (seen_type_specifier)
10659 cp_parser_parse_tentatively (parser);
10660 /* Look for the type-specifier. */
10661 type_specifier = cp_parser_type_specifier (parser,
10662 CP_PARSER_FLAGS_NONE,
10663 /*is_friend=*/false,
10664 /*is_declaration=*/false,
10667 /* If the first type-specifier could not be found, this is not a
10668 type-specifier-seq at all. */
10669 if (!seen_type_specifier && type_specifier == error_mark_node)
10670 return error_mark_node;
10671 /* If subsequent type-specifiers could not be found, the
10672 type-specifier-seq is complete. */
10673 else if (seen_type_specifier && !cp_parser_parse_definitely (parser))
10676 /* Add the new type-specifier to the list. */
10678 = tree_cons (NULL_TREE, type_specifier, type_specifier_seq);
10679 seen_type_specifier = true;
10682 /* We built up the list in reverse order. */
10683 return nreverse (type_specifier_seq);
10686 /* Parse a parameter-declaration-clause.
10688 parameter-declaration-clause:
10689 parameter-declaration-list [opt] ... [opt]
10690 parameter-declaration-list , ...
10692 Returns a representation for the parameter declarations. Each node
10693 is a TREE_LIST. (See cp_parser_parameter_declaration for the exact
10694 representation.) If the parameter-declaration-clause ends with an
10695 ellipsis, PARMLIST_ELLIPSIS_P will hold of the first node in the
10696 list. A return value of NULL_TREE indicates a
10697 parameter-declaration-clause consisting only of an ellipsis. */
10700 cp_parser_parameter_declaration_clause (parser)
10707 /* Peek at the next token. */
10708 token = cp_lexer_peek_token (parser->lexer);
10709 /* Check for trivial parameter-declaration-clauses. */
10710 if (token->type == CPP_ELLIPSIS)
10712 /* Consume the `...' token. */
10713 cp_lexer_consume_token (parser->lexer);
10716 else if (token->type == CPP_CLOSE_PAREN)
10717 /* There are no parameters. */
10719 #ifndef NO_IMPLICIT_EXTERN_C
10720 if (in_system_header && current_class_type == NULL
10721 && current_lang_name == lang_name_c)
10725 return void_list_node;
10727 /* Check for `(void)', too, which is a special case. */
10728 else if (token->keyword == RID_VOID
10729 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
10730 == CPP_CLOSE_PAREN))
10732 /* Consume the `void' token. */
10733 cp_lexer_consume_token (parser->lexer);
10734 /* There are no parameters. */
10735 return void_list_node;
10738 /* Parse the parameter-declaration-list. */
10739 parameters = cp_parser_parameter_declaration_list (parser);
10740 /* If a parse error occurred while parsing the
10741 parameter-declaration-list, then the entire
10742 parameter-declaration-clause is erroneous. */
10743 if (parameters == error_mark_node)
10744 return error_mark_node;
10746 /* Peek at the next token. */
10747 token = cp_lexer_peek_token (parser->lexer);
10748 /* If it's a `,', the clause should terminate with an ellipsis. */
10749 if (token->type == CPP_COMMA)
10751 /* Consume the `,'. */
10752 cp_lexer_consume_token (parser->lexer);
10753 /* Expect an ellipsis. */
10755 = (cp_parser_require (parser, CPP_ELLIPSIS, "`...'") != NULL);
10757 /* It might also be `...' if the optional trailing `,' was
10759 else if (token->type == CPP_ELLIPSIS)
10761 /* Consume the `...' token. */
10762 cp_lexer_consume_token (parser->lexer);
10763 /* And remember that we saw it. */
10767 ellipsis_p = false;
10769 /* Finish the parameter list. */
10770 return finish_parmlist (parameters, ellipsis_p);
10773 /* Parse a parameter-declaration-list.
10775 parameter-declaration-list:
10776 parameter-declaration
10777 parameter-declaration-list , parameter-declaration
10779 Returns a representation of the parameter-declaration-list, as for
10780 cp_parser_parameter_declaration_clause. However, the
10781 `void_list_node' is never appended to the list. */
10784 cp_parser_parameter_declaration_list (parser)
10787 tree parameters = NULL_TREE;
10789 /* Look for more parameters. */
10793 /* Parse the parameter. */
10795 = cp_parser_parameter_declaration (parser,
10796 /*greater_than_is_operator_p=*/true);
10797 /* If a parse error ocurred parsing the parameter declaration,
10798 then the entire parameter-declaration-list is erroneous. */
10799 if (parameter == error_mark_node)
10801 parameters = error_mark_node;
10804 /* Add the new parameter to the list. */
10805 TREE_CHAIN (parameter) = parameters;
10806 parameters = parameter;
10808 /* Peek at the next token. */
10809 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
10810 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10811 /* The parameter-declaration-list is complete. */
10813 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
10817 /* Peek at the next token. */
10818 token = cp_lexer_peek_nth_token (parser->lexer, 2);
10819 /* If it's an ellipsis, then the list is complete. */
10820 if (token->type == CPP_ELLIPSIS)
10822 /* Otherwise, there must be more parameters. Consume the
10824 cp_lexer_consume_token (parser->lexer);
10828 cp_parser_error (parser, "expected `,' or `...'");
10833 /* We built up the list in reverse order; straighten it out now. */
10834 return nreverse (parameters);
10837 /* Parse a parameter declaration.
10839 parameter-declaration:
10840 decl-specifier-seq declarator
10841 decl-specifier-seq declarator = assignment-expression
10842 decl-specifier-seq abstract-declarator [opt]
10843 decl-specifier-seq abstract-declarator [opt] = assignment-expression
10845 If GREATER_THAN_IS_OPERATOR_P is FALSE, then a non-nested `>' token
10846 encountered during the parsing of the assignment-expression is not
10847 interpreted as a greater-than operator.
10849 Returns a TREE_LIST representing the parameter-declaration. The
10850 TREE_VALUE is a representation of the decl-specifier-seq and
10851 declarator. In particular, the TREE_VALUE will be a TREE_LIST
10852 whose TREE_PURPOSE represents the decl-specifier-seq and whose
10853 TREE_VALUE represents the declarator. */
10856 cp_parser_parameter_declaration (parser, greater_than_is_operator_p)
10858 bool greater_than_is_operator_p;
10860 bool declares_class_or_enum;
10861 tree decl_specifiers;
10864 tree default_argument;
10867 const char *saved_message;
10869 /* Type definitions may not appear in parameter types. */
10870 saved_message = parser->type_definition_forbidden_message;
10871 parser->type_definition_forbidden_message
10872 = "types may not be defined in parameter types";
10874 /* Parse the declaration-specifiers. */
10876 = cp_parser_decl_specifier_seq (parser,
10877 CP_PARSER_FLAGS_NONE,
10879 &declares_class_or_enum);
10880 /* If an error occurred, there's no reason to attempt to parse the
10881 rest of the declaration. */
10882 if (cp_parser_error_occurred (parser))
10884 parser->type_definition_forbidden_message = saved_message;
10885 return error_mark_node;
10888 /* Peek at the next token. */
10889 token = cp_lexer_peek_token (parser->lexer);
10890 /* If the next token is a `)', `,', `=', `>', or `...', then there
10891 is no declarator. */
10892 if (token->type == CPP_CLOSE_PAREN
10893 || token->type == CPP_COMMA
10894 || token->type == CPP_EQ
10895 || token->type == CPP_ELLIPSIS
10896 || token->type == CPP_GREATER)
10897 declarator = NULL_TREE;
10898 /* Otherwise, there should be a declarator. */
10901 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
10902 parser->default_arg_ok_p = false;
10904 /* We don't know whether the declarator will be abstract or
10905 not. So, first we try an ordinary declarator. */
10906 cp_parser_parse_tentatively (parser);
10907 declarator = cp_parser_declarator (parser,
10908 /*abstract_p=*/false,
10909 /*ctor_dtor_or_conv_p=*/NULL);
10910 /* If that didn't work, look for an abstract declarator. */
10911 if (!cp_parser_parse_definitely (parser))
10912 declarator = cp_parser_declarator (parser,
10913 /*abstract_p=*/true,
10914 /*ctor_dtor_or_conv_p=*/NULL);
10915 parser->default_arg_ok_p = saved_default_arg_ok_p;
10916 /* After the declarator, allow more attributes. */
10917 attributes = chainon (attributes, cp_parser_attributes_opt (parser));
10920 /* The restriction on definining new types applies only to the type
10921 of the parameter, not to the default argument. */
10922 parser->type_definition_forbidden_message = saved_message;
10924 /* If the next token is `=', then process a default argument. */
10925 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10927 bool saved_greater_than_is_operator_p;
10928 /* Consume the `='. */
10929 cp_lexer_consume_token (parser->lexer);
10931 /* If we are defining a class, then the tokens that make up the
10932 default argument must be saved and processed later. */
10933 if (at_class_scope_p () && TYPE_BEING_DEFINED (current_class_type))
10935 unsigned depth = 0;
10937 /* Create a DEFAULT_ARG to represented the unparsed default
10939 default_argument = make_node (DEFAULT_ARG);
10940 DEFARG_TOKENS (default_argument) = cp_token_cache_new ();
10942 /* Add tokens until we have processed the entire default
10949 /* Peek at the next token. */
10950 token = cp_lexer_peek_token (parser->lexer);
10951 /* What we do depends on what token we have. */
10952 switch (token->type)
10954 /* In valid code, a default argument must be
10955 immediately followed by a `,' `)', or `...'. */
10957 case CPP_CLOSE_PAREN:
10959 /* If we run into a non-nested `;', `}', or `]',
10960 then the code is invalid -- but the default
10961 argument is certainly over. */
10962 case CPP_SEMICOLON:
10963 case CPP_CLOSE_BRACE:
10964 case CPP_CLOSE_SQUARE:
10967 /* Update DEPTH, if necessary. */
10968 else if (token->type == CPP_CLOSE_PAREN
10969 || token->type == CPP_CLOSE_BRACE
10970 || token->type == CPP_CLOSE_SQUARE)
10974 case CPP_OPEN_PAREN:
10975 case CPP_OPEN_SQUARE:
10976 case CPP_OPEN_BRACE:
10981 /* If we see a non-nested `>', and `>' is not an
10982 operator, then it marks the end of the default
10984 if (!depth && !greater_than_is_operator_p)
10988 /* If we run out of tokens, issue an error message. */
10990 error ("file ends in default argument");
10996 /* In these cases, we should look for template-ids.
10997 For example, if the default argument is
10998 `X<int, double>()', we need to do name lookup to
10999 figure out whether or not `X' is a template; if
11000 so, the `,' does not end the deault argument.
11002 That is not yet done. */
11009 /* If we've reached the end, stop. */
11013 /* Add the token to the token block. */
11014 token = cp_lexer_consume_token (parser->lexer);
11015 cp_token_cache_push_token (DEFARG_TOKENS (default_argument),
11019 /* Outside of a class definition, we can just parse the
11020 assignment-expression. */
11023 bool saved_local_variables_forbidden_p;
11025 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
11027 saved_greater_than_is_operator_p
11028 = parser->greater_than_is_operator_p;
11029 parser->greater_than_is_operator_p = greater_than_is_operator_p;
11030 /* Local variable names (and the `this' keyword) may not
11031 appear in a default argument. */
11032 saved_local_variables_forbidden_p
11033 = parser->local_variables_forbidden_p;
11034 parser->local_variables_forbidden_p = true;
11035 /* Parse the assignment-expression. */
11036 default_argument = cp_parser_assignment_expression (parser);
11037 /* Restore saved state. */
11038 parser->greater_than_is_operator_p
11039 = saved_greater_than_is_operator_p;
11040 parser->local_variables_forbidden_p
11041 = saved_local_variables_forbidden_p;
11043 if (!parser->default_arg_ok_p)
11045 pedwarn ("default arguments are only permitted on functions");
11046 if (flag_pedantic_errors)
11047 default_argument = NULL_TREE;
11051 default_argument = NULL_TREE;
11053 /* Create the representation of the parameter. */
11055 decl_specifiers = tree_cons (attributes, NULL_TREE, decl_specifiers);
11056 parameter = build_tree_list (default_argument,
11057 build_tree_list (decl_specifiers,
11063 /* Parse a function-definition.
11065 function-definition:
11066 decl-specifier-seq [opt] declarator ctor-initializer [opt]
11068 decl-specifier-seq [opt] declarator function-try-block
11072 function-definition:
11073 __extension__ function-definition
11075 Returns the FUNCTION_DECL for the function. If FRIEND_P is
11076 non-NULL, *FRIEND_P is set to TRUE iff the function was declared to
11080 cp_parser_function_definition (parser, friend_p)
11084 tree decl_specifiers;
11088 tree access_checks;
11090 bool declares_class_or_enum;
11092 /* The saved value of the PEDANTIC flag. */
11093 int saved_pedantic;
11095 /* Any pending qualification must be cleared by our caller. It is
11096 more robust to force the callers to clear PARSER->SCOPE than to
11097 do it here since if the qualification is in effect here, it might
11098 also end up in effect elsewhere that it is not intended. */
11099 my_friendly_assert (!parser->scope, 20010821);
11101 /* Handle `__extension__'. */
11102 if (cp_parser_extension_opt (parser, &saved_pedantic))
11104 /* Parse the function-definition. */
11105 fn = cp_parser_function_definition (parser, friend_p);
11106 /* Restore the PEDANTIC flag. */
11107 pedantic = saved_pedantic;
11112 /* Check to see if this definition appears in a class-specifier. */
11113 member_p = (at_class_scope_p ()
11114 && TYPE_BEING_DEFINED (current_class_type));
11115 /* Defer access checks in the decl-specifier-seq until we know what
11116 function is being defined. There is no need to do this for the
11117 definition of member functions; we cannot be defining a member
11118 from another class. */
11120 cp_parser_start_deferring_access_checks (parser);
11121 /* Parse the decl-specifier-seq. */
11123 = cp_parser_decl_specifier_seq (parser,
11124 CP_PARSER_FLAGS_OPTIONAL,
11126 &declares_class_or_enum);
11127 /* Figure out whether this declaration is a `friend'. */
11129 *friend_p = cp_parser_friend_p (decl_specifiers);
11131 /* Parse the declarator. */
11132 declarator = cp_parser_declarator (parser,
11133 /*abstract_p=*/false,
11134 /*ctor_dtor_or_conv_p=*/NULL);
11136 /* Gather up any access checks that occurred. */
11138 access_checks = cp_parser_stop_deferring_access_checks (parser);
11140 access_checks = NULL_TREE;
11142 /* If something has already gone wrong, we may as well stop now. */
11143 if (declarator == error_mark_node)
11145 /* Skip to the end of the function, or if this wasn't anything
11146 like a function-definition, to a `;' in the hopes of finding
11147 a sensible place from which to continue parsing. */
11148 cp_parser_skip_to_end_of_block_or_statement (parser);
11149 return error_mark_node;
11152 /* The next character should be a `{' (for a simple function
11153 definition), a `:' (for a ctor-initializer), or `try' (for a
11154 function-try block). */
11155 token = cp_lexer_peek_token (parser->lexer);
11156 if (!cp_parser_token_starts_function_definition_p (token))
11158 /* Issue the error-message. */
11159 cp_parser_error (parser, "expected function-definition");
11160 /* Skip to the next `;'. */
11161 cp_parser_skip_to_end_of_block_or_statement (parser);
11163 return error_mark_node;
11166 /* If we are in a class scope, then we must handle
11167 function-definitions specially. In particular, we save away the
11168 tokens that make up the function body, and parse them again
11169 later, in order to handle code like:
11172 int f () { return i; }
11176 Here, we cannot parse the body of `f' until after we have seen
11177 the declaration of `i'. */
11180 cp_token_cache *cache;
11182 /* Create the function-declaration. */
11183 fn = start_method (decl_specifiers, declarator, attributes);
11184 /* If something went badly wrong, bail out now. */
11185 if (fn == error_mark_node)
11187 /* If there's a function-body, skip it. */
11188 if (cp_parser_token_starts_function_definition_p
11189 (cp_lexer_peek_token (parser->lexer)))
11190 cp_parser_skip_to_end_of_block_or_statement (parser);
11191 return error_mark_node;
11194 /* Create a token cache. */
11195 cache = cp_token_cache_new ();
11196 /* Save away the tokens that make up the body of the
11198 cp_parser_cache_group (parser, cache, CPP_CLOSE_BRACE, /*depth=*/0);
11199 /* Handle function try blocks. */
11200 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
11201 cp_parser_cache_group (parser, cache, CPP_CLOSE_BRACE, /*depth=*/0);
11203 /* Save away the inline definition; we will process it when the
11204 class is complete. */
11205 DECL_PENDING_INLINE_INFO (fn) = cache;
11206 DECL_PENDING_INLINE_P (fn) = 1;
11208 /* We're done with the inline definition. */
11209 finish_method (fn);
11211 /* Add FN to the queue of functions to be parsed later. */
11212 TREE_VALUE (parser->unparsed_functions_queues)
11213 = tree_cons (current_class_type, fn,
11214 TREE_VALUE (parser->unparsed_functions_queues));
11219 /* Check that the number of template-parameter-lists is OK. */
11220 if (!cp_parser_check_declarator_template_parameters (parser,
11223 cp_parser_skip_to_end_of_block_or_statement (parser);
11224 return error_mark_node;
11227 return (cp_parser_function_definition_from_specifiers_and_declarator
11228 (parser, decl_specifiers, attributes, declarator, access_checks));
11231 /* Parse a function-body.
11234 compound_statement */
11237 cp_parser_function_body (cp_parser *parser)
11239 cp_parser_compound_statement (parser);
11242 /* Parse a ctor-initializer-opt followed by a function-body. Return
11243 true if a ctor-initializer was present. */
11246 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
11249 bool ctor_initializer_p;
11251 /* Begin the function body. */
11252 body = begin_function_body ();
11253 /* Parse the optional ctor-initializer. */
11254 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
11255 /* Parse the function-body. */
11256 cp_parser_function_body (parser);
11257 /* Finish the function body. */
11258 finish_function_body (body);
11260 return ctor_initializer_p;
11263 /* Parse an initializer.
11266 = initializer-clause
11267 ( expression-list )
11269 Returns a expression representing the initializer. If no
11270 initializer is present, NULL_TREE is returned.
11272 *IS_PARENTHESIZED_INIT is set to TRUE if the `( expression-list )'
11273 production is used, and zero otherwise. *IS_PARENTHESIZED_INIT is
11274 set to FALSE if there is no initializer present. */
11277 cp_parser_initializer (parser, is_parenthesized_init)
11279 bool *is_parenthesized_init;
11284 /* Peek at the next token. */
11285 token = cp_lexer_peek_token (parser->lexer);
11287 /* Let our caller know whether or not this initializer was
11289 *is_parenthesized_init = (token->type == CPP_OPEN_PAREN);
11291 if (token->type == CPP_EQ)
11293 /* Consume the `='. */
11294 cp_lexer_consume_token (parser->lexer);
11295 /* Parse the initializer-clause. */
11296 init = cp_parser_initializer_clause (parser);
11298 else if (token->type == CPP_OPEN_PAREN)
11300 /* Consume the `('. */
11301 cp_lexer_consume_token (parser->lexer);
11302 /* Parse the expression-list. */
11303 init = cp_parser_expression_list (parser);
11304 /* Consume the `)' token. */
11305 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
11306 cp_parser_skip_to_closing_parenthesis (parser);
11310 /* Anything else is an error. */
11311 cp_parser_error (parser, "expected initializer");
11312 init = error_mark_node;
11318 /* Parse an initializer-clause.
11320 initializer-clause:
11321 assignment-expression
11322 { initializer-list , [opt] }
11325 Returns an expression representing the initializer.
11327 If the `assignment-expression' production is used the value
11328 returned is simply a reprsentation for the expression.
11330 Otherwise, a CONSTRUCTOR is returned. The CONSTRUCTOR_ELTS will be
11331 the elements of the initializer-list (or NULL_TREE, if the last
11332 production is used). The TREE_TYPE for the CONSTRUCTOR will be
11333 NULL_TREE. There is no way to detect whether or not the optional
11334 trailing `,' was provided. */
11337 cp_parser_initializer_clause (parser)
11342 /* If it is not a `{', then we are looking at an
11343 assignment-expression. */
11344 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
11345 initializer = cp_parser_assignment_expression (parser);
11348 /* Consume the `{' token. */
11349 cp_lexer_consume_token (parser->lexer);
11350 /* Create a CONSTRUCTOR to represent the braced-initializer. */
11351 initializer = make_node (CONSTRUCTOR);
11352 /* Mark it with TREE_HAS_CONSTRUCTOR. This should not be
11353 necessary, but check_initializer depends upon it, for
11355 TREE_HAS_CONSTRUCTOR (initializer) = 1;
11356 /* If it's not a `}', then there is a non-trivial initializer. */
11357 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
11359 /* Parse the initializer list. */
11360 CONSTRUCTOR_ELTS (initializer)
11361 = cp_parser_initializer_list (parser);
11362 /* A trailing `,' token is allowed. */
11363 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
11364 cp_lexer_consume_token (parser->lexer);
11367 /* Now, there should be a trailing `}'. */
11368 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
11371 return initializer;
11374 /* Parse an initializer-list.
11378 initializer-list , initializer-clause
11383 identifier : initializer-clause
11384 initializer-list, identifier : initializer-clause
11386 Returns a TREE_LIST. The TREE_VALUE of each node is an expression
11387 for the initializer. If the TREE_PURPOSE is non-NULL, it is the
11388 IDENTIFIER_NODE naming the field to initialize. */
11391 cp_parser_initializer_list (parser)
11394 tree initializers = NULL_TREE;
11396 /* Parse the rest of the list. */
11403 /* If the next token is an identifier and the following one is a
11404 colon, we are looking at the GNU designated-initializer
11406 if (cp_parser_allow_gnu_extensions_p (parser)
11407 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
11408 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
11410 /* Consume the identifier. */
11411 identifier = cp_lexer_consume_token (parser->lexer)->value;
11412 /* Consume the `:'. */
11413 cp_lexer_consume_token (parser->lexer);
11416 identifier = NULL_TREE;
11418 /* Parse the initializer. */
11419 initializer = cp_parser_initializer_clause (parser);
11421 /* Add it to the list. */
11422 initializers = tree_cons (identifier, initializer, initializers);
11424 /* If the next token is not a comma, we have reached the end of
11426 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11429 /* Peek at the next token. */
11430 token = cp_lexer_peek_nth_token (parser->lexer, 2);
11431 /* If the next token is a `}', then we're still done. An
11432 initializer-clause can have a trailing `,' after the
11433 initializer-list and before the closing `}'. */
11434 if (token->type == CPP_CLOSE_BRACE)
11437 /* Consume the `,' token. */
11438 cp_lexer_consume_token (parser->lexer);
11441 /* The initializers were built up in reverse order, so we need to
11442 reverse them now. */
11443 return nreverse (initializers);
11446 /* Classes [gram.class] */
11448 /* Parse a class-name.
11454 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
11455 to indicate that names looked up in dependent types should be
11456 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
11457 keyword has been used to indicate that the name that appears next
11458 is a template. TYPE_P is true iff the next name should be treated
11459 as class-name, even if it is declared to be some other kind of name
11460 as well. The accessibility of the class-name is checked iff
11461 CHECK_ACCESS_P is true. If CHECK_DEPENDENCY_P is FALSE, names are
11462 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
11463 is the class being defined in a class-head.
11465 Returns the TYPE_DECL representing the class. */
11468 cp_parser_class_name (cp_parser *parser,
11469 bool typename_keyword_p,
11470 bool template_keyword_p,
11472 bool check_access_p,
11473 bool check_dependency_p,
11481 /* All class-names start with an identifier. */
11482 token = cp_lexer_peek_token (parser->lexer);
11483 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
11485 cp_parser_error (parser, "expected class-name");
11486 return error_mark_node;
11489 /* PARSER->SCOPE can be cleared when parsing the template-arguments
11490 to a template-id, so we save it here. */
11491 scope = parser->scope;
11492 /* Any name names a type if we're following the `typename' keyword
11493 in a qualified name where the enclosing scope is type-dependent. */
11494 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
11495 && cp_parser_dependent_type_p (scope));
11496 /* Handle the common case (an identifier, but not a template-id)
11498 if (token->type == CPP_NAME
11499 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_LESS)
11503 /* Look for the identifier. */
11504 identifier = cp_parser_identifier (parser);
11505 /* If the next token isn't an identifier, we are certainly not
11506 looking at a class-name. */
11507 if (identifier == error_mark_node)
11508 decl = error_mark_node;
11509 /* If we know this is a type-name, there's no need to look it
11511 else if (typename_p)
11515 /* If the next token is a `::', then the name must be a type
11518 [basic.lookup.qual]
11520 During the lookup for a name preceding the :: scope
11521 resolution operator, object, function, and enumerator
11522 names are ignored. */
11523 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
11525 /* Look up the name. */
11526 decl = cp_parser_lookup_name (parser, identifier,
11529 /*is_namespace=*/false,
11530 check_dependency_p);
11535 /* Try a template-id. */
11536 decl = cp_parser_template_id (parser, template_keyword_p,
11537 check_dependency_p);
11538 if (decl == error_mark_node)
11539 return error_mark_node;
11542 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
11544 /* If this is a typename, create a TYPENAME_TYPE. */
11545 if (typename_p && decl != error_mark_node)
11546 decl = TYPE_NAME (make_typename_type (scope, decl,
11549 /* Check to see that it is really the name of a class. */
11550 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
11551 && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
11552 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
11553 /* Situations like this:
11555 template <typename T> struct A {
11556 typename T::template X<int>::I i;
11559 are problematic. Is `T::template X<int>' a class-name? The
11560 standard does not seem to be definitive, but there is no other
11561 valid interpretation of the following `::'. Therefore, those
11562 names are considered class-names. */
11563 decl = TYPE_NAME (make_typename_type (scope, decl,
11564 tf_error | tf_parsing));
11565 else if (decl == error_mark_node
11566 || TREE_CODE (decl) != TYPE_DECL
11567 || !IS_AGGR_TYPE (TREE_TYPE (decl)))
11569 cp_parser_error (parser, "expected class-name");
11570 return error_mark_node;
11576 /* Parse a class-specifier.
11579 class-head { member-specification [opt] }
11581 Returns the TREE_TYPE representing the class. */
11584 cp_parser_class_specifier (parser)
11589 tree attributes = NULL_TREE;
11590 int has_trailing_semicolon;
11591 bool nested_name_specifier_p;
11592 bool deferring_access_checks_p;
11593 tree saved_access_checks;
11594 unsigned saved_num_template_parameter_lists;
11596 /* Parse the class-head. */
11597 type = cp_parser_class_head (parser,
11598 &nested_name_specifier_p,
11599 &deferring_access_checks_p,
11600 &saved_access_checks);
11601 /* If the class-head was a semantic disaster, skip the entire body
11605 cp_parser_skip_to_end_of_block_or_statement (parser);
11606 return error_mark_node;
11608 /* Look for the `{'. */
11609 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
11610 return error_mark_node;
11611 /* Issue an error message if type-definitions are forbidden here. */
11612 cp_parser_check_type_definition (parser);
11613 /* Remember that we are defining one more class. */
11614 ++parser->num_classes_being_defined;
11615 /* Inside the class, surrounding template-parameter-lists do not
11617 saved_num_template_parameter_lists
11618 = parser->num_template_parameter_lists;
11619 parser->num_template_parameter_lists = 0;
11620 /* Start the class. */
11621 type = begin_class_definition (type);
11622 if (type == error_mark_node)
11623 /* If the type is erroneous, skip the entire body of the class. */
11624 cp_parser_skip_to_closing_brace (parser);
11626 /* Parse the member-specification. */
11627 cp_parser_member_specification_opt (parser);
11628 /* Look for the trailing `}'. */
11629 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
11630 /* We get better error messages by noticing a common problem: a
11631 missing trailing `;'. */
11632 token = cp_lexer_peek_token (parser->lexer);
11633 has_trailing_semicolon = (token->type == CPP_SEMICOLON);
11634 /* Look for attributes to apply to this class. */
11635 if (cp_parser_allow_gnu_extensions_p (parser))
11636 attributes = cp_parser_attributes_opt (parser);
11637 /* Finish the class definition. */
11638 type = finish_class_definition (type,
11640 has_trailing_semicolon,
11641 nested_name_specifier_p);
11642 /* If this class is not itself within the scope of another class,
11643 then we need to parse the bodies of all of the queued function
11644 definitions. Note that the queued functions defined in a class
11645 are not always processed immediately following the
11646 class-specifier for that class. Consider:
11649 struct B { void f() { sizeof (A); } };
11652 If `f' were processed before the processing of `A' were
11653 completed, there would be no way to compute the size of `A'.
11654 Note that the nesting we are interested in here is lexical --
11655 not the semantic nesting given by TYPE_CONTEXT. In particular,
11658 struct A { struct B; };
11659 struct A::B { void f() { } };
11661 there is no need to delay the parsing of `A::B::f'. */
11662 if (--parser->num_classes_being_defined == 0)
11664 tree last_scope = NULL_TREE;
11666 /* Process non FUNCTION_DECL related DEFAULT_ARGs. */
11667 for (parser->default_arg_types = nreverse (parser->default_arg_types);
11668 parser->default_arg_types;
11669 parser->default_arg_types = TREE_CHAIN (parser->default_arg_types))
11670 cp_parser_late_parsing_default_args
11671 (parser, TREE_PURPOSE (parser->default_arg_types), NULL_TREE);
11673 /* Reverse the queue, so that we process it in the order the
11674 functions were declared. */
11675 TREE_VALUE (parser->unparsed_functions_queues)
11676 = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
11677 /* Loop through all of the functions. */
11678 while (TREE_VALUE (parser->unparsed_functions_queues))
11685 /* Figure out which function we need to process. */
11686 queue_entry = TREE_VALUE (parser->unparsed_functions_queues);
11687 fn_scope = TREE_PURPOSE (queue_entry);
11688 fn = TREE_VALUE (queue_entry);
11690 /* Parse the function. */
11691 cp_parser_late_parsing_for_member (parser, fn);
11693 TREE_VALUE (parser->unparsed_functions_queues)
11694 = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues));
11697 /* If LAST_SCOPE is non-NULL, then we have pushed scopes one
11698 more time than we have popped, so me must pop here. */
11700 pop_scope (last_scope);
11703 /* Put back any saved access checks. */
11704 if (deferring_access_checks_p)
11706 cp_parser_start_deferring_access_checks (parser);
11707 parser->context->deferred_access_checks = saved_access_checks;
11710 /* Restore the count of active template-parameter-lists. */
11711 parser->num_template_parameter_lists
11712 = saved_num_template_parameter_lists;
11717 /* Parse a class-head.
11720 class-key identifier [opt] base-clause [opt]
11721 class-key nested-name-specifier identifier base-clause [opt]
11722 class-key nested-name-specifier [opt] template-id
11726 class-key attributes identifier [opt] base-clause [opt]
11727 class-key attributes nested-name-specifier identifier base-clause [opt]
11728 class-key attributes nested-name-specifier [opt] template-id
11731 Returns the TYPE of the indicated class. Sets
11732 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
11733 involving a nested-name-specifier was used, and FALSE otherwise.
11734 Sets *DEFERRING_ACCESS_CHECKS_P to TRUE iff we were deferring
11735 access checks before this class-head. In that case,
11736 *SAVED_ACCESS_CHECKS is set to the current list of deferred access
11739 Returns NULL_TREE if the class-head is syntactically valid, but
11740 semantically invalid in a way that means we should skip the entire
11741 body of the class. */
11744 cp_parser_class_head (parser,
11745 nested_name_specifier_p,
11746 deferring_access_checks_p,
11747 saved_access_checks)
11749 bool *nested_name_specifier_p;
11750 bool *deferring_access_checks_p;
11751 tree *saved_access_checks;
11754 tree nested_name_specifier;
11755 enum tag_types class_key;
11756 tree id = NULL_TREE;
11757 tree type = NULL_TREE;
11759 bool template_id_p = false;
11760 bool qualified_p = false;
11761 bool invalid_nested_name_p = false;
11762 unsigned num_templates;
11764 /* Assume no nested-name-specifier will be present. */
11765 *nested_name_specifier_p = false;
11766 /* Assume no template parameter lists will be used in defining the
11770 /* Look for the class-key. */
11771 class_key = cp_parser_class_key (parser);
11772 if (class_key == none_type)
11773 return error_mark_node;
11775 /* Parse the attributes. */
11776 attributes = cp_parser_attributes_opt (parser);
11778 /* If the next token is `::', that is invalid -- but sometimes
11779 people do try to write:
11783 Handle this gracefully by accepting the extra qualifier, and then
11784 issuing an error about it later if this really is a
11785 class-header. If it turns out just to be an elaborated type
11786 specifier, remain silent. */
11787 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
11788 qualified_p = true;
11790 /* Determine the name of the class. Begin by looking for an
11791 optional nested-name-specifier. */
11792 nested_name_specifier
11793 = cp_parser_nested_name_specifier_opt (parser,
11794 /*typename_keyword_p=*/false,
11795 /*check_dependency_p=*/true,
11797 /* If there was a nested-name-specifier, then there *must* be an
11799 if (nested_name_specifier)
11801 /* Although the grammar says `identifier', it really means
11802 `class-name' or `template-name'. You are only allowed to
11803 define a class that has already been declared with this
11806 The proposed resolution for Core Issue 180 says that whever
11807 you see `class T::X' you should treat `X' as a type-name.
11809 It is OK to define an inaccessible class; for example:
11811 class A { class B; };
11814 So, we ask cp_parser_class_name not to check accessibility.
11816 We do not know if we will see a class-name, or a
11817 template-name. We look for a class-name first, in case the
11818 class-name is a template-id; if we looked for the
11819 template-name first we would stop after the template-name. */
11820 cp_parser_parse_tentatively (parser);
11821 type = cp_parser_class_name (parser,
11822 /*typename_keyword_p=*/false,
11823 /*template_keyword_p=*/false,
11825 /*check_access_p=*/false,
11826 /*check_dependency_p=*/false,
11827 /*class_head_p=*/true);
11828 /* If that didn't work, ignore the nested-name-specifier. */
11829 if (!cp_parser_parse_definitely (parser))
11831 invalid_nested_name_p = true;
11832 id = cp_parser_identifier (parser);
11833 if (id == error_mark_node)
11836 /* If we could not find a corresponding TYPE, treat this
11837 declaration like an unqualified declaration. */
11838 if (type == error_mark_node)
11839 nested_name_specifier = NULL_TREE;
11840 /* Otherwise, count the number of templates used in TYPE and its
11841 containing scopes. */
11846 for (scope = TREE_TYPE (type);
11847 scope && TREE_CODE (scope) != NAMESPACE_DECL;
11848 scope = (TYPE_P (scope)
11849 ? TYPE_CONTEXT (scope)
11850 : DECL_CONTEXT (scope)))
11852 && CLASS_TYPE_P (scope)
11853 && CLASSTYPE_TEMPLATE_INFO (scope)
11854 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
11858 /* Otherwise, the identifier is optional. */
11861 /* We don't know whether what comes next is a template-id,
11862 an identifier, or nothing at all. */
11863 cp_parser_parse_tentatively (parser);
11864 /* Check for a template-id. */
11865 id = cp_parser_template_id (parser,
11866 /*template_keyword_p=*/false,
11867 /*check_dependency_p=*/true);
11868 /* If that didn't work, it could still be an identifier. */
11869 if (!cp_parser_parse_definitely (parser))
11871 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
11872 id = cp_parser_identifier (parser);
11878 template_id_p = true;
11883 /* If it's not a `:' or a `{' then we can't really be looking at a
11884 class-head, since a class-head only appears as part of a
11885 class-specifier. We have to detect this situation before calling
11886 xref_tag, since that has irreversible side-effects. */
11887 if (!cp_parser_next_token_starts_class_definition_p (parser))
11889 cp_parser_error (parser, "expected `{' or `:'");
11890 return error_mark_node;
11893 /* At this point, we're going ahead with the class-specifier, even
11894 if some other problem occurs. */
11895 cp_parser_commit_to_tentative_parse (parser);
11896 /* Issue the error about the overly-qualified name now. */
11898 cp_parser_error (parser,
11899 "global qualification of class name is invalid");
11900 else if (invalid_nested_name_p)
11901 cp_parser_error (parser,
11902 "qualified name does not name a class");
11903 /* Make sure that the right number of template parameters were
11905 if (!cp_parser_check_template_parameters (parser, num_templates))
11906 /* If something went wrong, there is no point in even trying to
11907 process the class-definition. */
11910 /* We do not need to defer access checks for entities declared
11911 within the class. But, we do need to save any access checks that
11912 are currently deferred and restore them later, in case we are in
11913 the middle of something else. */
11914 *deferring_access_checks_p = parser->context->deferring_access_checks_p;
11915 if (*deferring_access_checks_p)
11916 *saved_access_checks = cp_parser_stop_deferring_access_checks (parser);
11918 /* Look up the type. */
11921 type = TREE_TYPE (id);
11922 maybe_process_partial_specialization (type);
11924 else if (!nested_name_specifier)
11926 /* If the class was unnamed, create a dummy name. */
11928 id = make_anon_name ();
11929 type = xref_tag (class_key, id, attributes, /*globalize=*/0);
11938 template <typename T> struct S { struct T };
11939 template <typename T> struct S::T { };
11941 we will get a TYPENAME_TYPE when processing the definition of
11942 `S::T'. We need to resolve it to the actual type before we
11943 try to define it. */
11944 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
11946 type = cp_parser_resolve_typename_type (parser, TREE_TYPE (type));
11947 if (type != error_mark_node)
11948 type = TYPE_NAME (type);
11951 maybe_process_partial_specialization (TREE_TYPE (type));
11952 class_type = current_class_type;
11953 type = TREE_TYPE (handle_class_head (class_key,
11954 nested_name_specifier,
11959 if (type != error_mark_node)
11961 if (!class_type && TYPE_CONTEXT (type))
11962 *nested_name_specifier_p = true;
11963 else if (class_type && !same_type_p (TYPE_CONTEXT (type),
11965 *nested_name_specifier_p = true;
11968 /* Indicate whether this class was declared as a `class' or as a
11970 if (TREE_CODE (type) == RECORD_TYPE)
11971 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
11972 cp_parser_check_class_key (class_key, type);
11974 /* Enter the scope containing the class; the names of base classes
11975 should be looked up in that context. For example, given:
11977 struct A { struct B {}; struct C; };
11978 struct A::C : B {};
11981 if (nested_name_specifier)
11982 push_scope (nested_name_specifier);
11983 /* Now, look for the base-clause. */
11984 token = cp_lexer_peek_token (parser->lexer);
11985 if (token->type == CPP_COLON)
11989 /* Get the list of base-classes. */
11990 bases = cp_parser_base_clause (parser);
11991 /* Process them. */
11992 xref_basetypes (type, bases);
11994 /* Leave the scope given by the nested-name-specifier. We will
11995 enter the class scope itself while processing the members. */
11996 if (nested_name_specifier)
11997 pop_scope (nested_name_specifier);
12002 /* Parse a class-key.
12009 Returns the kind of class-key specified, or none_type to indicate
12012 static enum tag_types
12013 cp_parser_class_key (parser)
12017 enum tag_types tag_type;
12019 /* Look for the class-key. */
12020 token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
12024 /* Check to see if the TOKEN is a class-key. */
12025 tag_type = cp_parser_token_is_class_key (token);
12027 cp_parser_error (parser, "expected class-key");
12031 /* Parse an (optional) member-specification.
12033 member-specification:
12034 member-declaration member-specification [opt]
12035 access-specifier : member-specification [opt] */
12038 cp_parser_member_specification_opt (parser)
12046 /* Peek at the next token. */
12047 token = cp_lexer_peek_token (parser->lexer);
12048 /* If it's a `}', or EOF then we've seen all the members. */
12049 if (token->type == CPP_CLOSE_BRACE || token->type == CPP_EOF)
12052 /* See if this token is a keyword. */
12053 keyword = token->keyword;
12057 case RID_PROTECTED:
12059 /* Consume the access-specifier. */
12060 cp_lexer_consume_token (parser->lexer);
12061 /* Remember which access-specifier is active. */
12062 current_access_specifier = token->value;
12063 /* Look for the `:'. */
12064 cp_parser_require (parser, CPP_COLON, "`:'");
12068 /* Otherwise, the next construction must be a
12069 member-declaration. */
12070 cp_parser_member_declaration (parser);
12071 reset_type_access_control ();
12076 /* Parse a member-declaration.
12078 member-declaration:
12079 decl-specifier-seq [opt] member-declarator-list [opt] ;
12080 function-definition ; [opt]
12081 :: [opt] nested-name-specifier template [opt] unqualified-id ;
12083 template-declaration
12085 member-declarator-list:
12087 member-declarator-list , member-declarator
12090 declarator pure-specifier [opt]
12091 declarator constant-initializer [opt]
12092 identifier [opt] : constant-expression
12096 member-declaration:
12097 __extension__ member-declaration
12100 declarator attributes [opt] pure-specifier [opt]
12101 declarator attributes [opt] constant-initializer [opt]
12102 identifier [opt] attributes [opt] : constant-expression */
12105 cp_parser_member_declaration (parser)
12108 tree decl_specifiers;
12109 tree prefix_attributes;
12111 bool declares_class_or_enum;
12114 int saved_pedantic;
12116 /* Check for the `__extension__' keyword. */
12117 if (cp_parser_extension_opt (parser, &saved_pedantic))
12120 cp_parser_member_declaration (parser);
12121 /* Restore the old value of the PEDANTIC flag. */
12122 pedantic = saved_pedantic;
12127 /* Check for a template-declaration. */
12128 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
12130 /* Parse the template-declaration. */
12131 cp_parser_template_declaration (parser, /*member_p=*/true);
12136 /* Check for a using-declaration. */
12137 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
12139 /* Parse the using-declaration. */
12140 cp_parser_using_declaration (parser);
12145 /* We can't tell whether we're looking at a declaration or a
12146 function-definition. */
12147 cp_parser_parse_tentatively (parser);
12149 /* Parse the decl-specifier-seq. */
12151 = cp_parser_decl_specifier_seq (parser,
12152 CP_PARSER_FLAGS_OPTIONAL,
12153 &prefix_attributes,
12154 &declares_class_or_enum);
12155 /* If there is no declarator, then the decl-specifier-seq should
12157 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12159 /* If there was no decl-specifier-seq, and the next token is a
12160 `;', then we have something like:
12166 Each member-declaration shall declare at least one member
12167 name of the class. */
12168 if (!decl_specifiers)
12171 pedwarn ("extra semicolon");
12177 /* See if this declaration is a friend. */
12178 friend_p = cp_parser_friend_p (decl_specifiers);
12179 /* If there were decl-specifiers, check to see if there was
12180 a class-declaration. */
12181 type = check_tag_decl (decl_specifiers);
12182 /* Nested classes have already been added to the class, but
12183 a `friend' needs to be explicitly registered. */
12186 /* If the `friend' keyword was present, the friend must
12187 be introduced with a class-key. */
12188 if (!declares_class_or_enum)
12189 error ("a class-key must be used when declaring a friend");
12192 template <typename T> struct A {
12193 friend struct A<T>::B;
12196 A<T>::B will be represented by a TYPENAME_TYPE, and
12197 therefore not recognized by check_tag_decl. */
12202 for (specifier = decl_specifiers;
12204 specifier = TREE_CHAIN (specifier))
12206 tree s = TREE_VALUE (specifier);
12208 if (TREE_CODE (s) == IDENTIFIER_NODE
12209 && IDENTIFIER_GLOBAL_VALUE (s))
12210 type = IDENTIFIER_GLOBAL_VALUE (s);
12211 if (TREE_CODE (s) == TYPE_DECL)
12221 error ("friend declaration does not name a class or "
12224 make_friend_class (current_class_type, type);
12226 /* If there is no TYPE, an error message will already have
12230 /* An anonymous aggregate has to be handled specially; such
12231 a declaration really declares a data member (with a
12232 particular type), as opposed to a nested class. */
12233 else if (ANON_AGGR_TYPE_P (type))
12235 /* Remove constructors and such from TYPE, now that we
12236 know it is an anoymous aggregate. */
12237 fixup_anonymous_aggr (type);
12238 /* And make the corresponding data member. */
12239 decl = build_decl (FIELD_DECL, NULL_TREE, type);
12240 /* Add it to the class. */
12241 finish_member_declaration (decl);
12247 /* See if these declarations will be friends. */
12248 friend_p = cp_parser_friend_p (decl_specifiers);
12250 /* Keep going until we hit the `;' at the end of the
12252 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12254 tree attributes = NULL_TREE;
12255 tree first_attribute;
12257 /* Peek at the next token. */
12258 token = cp_lexer_peek_token (parser->lexer);
12260 /* Check for a bitfield declaration. */
12261 if (token->type == CPP_COLON
12262 || (token->type == CPP_NAME
12263 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
12269 /* Get the name of the bitfield. Note that we cannot just
12270 check TOKEN here because it may have been invalidated by
12271 the call to cp_lexer_peek_nth_token above. */
12272 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
12273 identifier = cp_parser_identifier (parser);
12275 identifier = NULL_TREE;
12277 /* Consume the `:' token. */
12278 cp_lexer_consume_token (parser->lexer);
12279 /* Get the width of the bitfield. */
12280 width = cp_parser_constant_expression (parser);
12282 /* Look for attributes that apply to the bitfield. */
12283 attributes = cp_parser_attributes_opt (parser);
12284 /* Remember which attributes are prefix attributes and
12286 first_attribute = attributes;
12287 /* Combine the attributes. */
12288 attributes = chainon (prefix_attributes, attributes);
12290 /* Create the bitfield declaration. */
12291 decl = grokbitfield (identifier,
12294 /* Apply the attributes. */
12295 cplus_decl_attributes (&decl, attributes, /*flags=*/0);
12301 tree asm_specification;
12302 bool ctor_dtor_or_conv_p;
12304 /* Parse the declarator. */
12306 = cp_parser_declarator (parser,
12307 /*abstract_p=*/false,
12308 &ctor_dtor_or_conv_p);
12310 /* If something went wrong parsing the declarator, make sure
12311 that we at least consume some tokens. */
12312 if (declarator == error_mark_node)
12314 /* Skip to the end of the statement. */
12315 cp_parser_skip_to_end_of_statement (parser);
12319 /* Look for an asm-specification. */
12320 asm_specification = cp_parser_asm_specification_opt (parser);
12321 /* Look for attributes that apply to the declaration. */
12322 attributes = cp_parser_attributes_opt (parser);
12323 /* Remember which attributes are prefix attributes and
12325 first_attribute = attributes;
12326 /* Combine the attributes. */
12327 attributes = chainon (prefix_attributes, attributes);
12329 /* If it's an `=', then we have a constant-initializer or a
12330 pure-specifier. It is not correct to parse the
12331 initializer before registering the member declaration
12332 since the member declaration should be in scope while
12333 its initializer is processed. However, the rest of the
12334 front end does not yet provide an interface that allows
12335 us to handle this correctly. */
12336 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12340 A pure-specifier shall be used only in the declaration of
12341 a virtual function.
12343 A member-declarator can contain a constant-initializer
12344 only if it declares a static member of integral or
12347 Therefore, if the DECLARATOR is for a function, we look
12348 for a pure-specifier; otherwise, we look for a
12349 constant-initializer. When we call `grokfield', it will
12350 perform more stringent semantics checks. */
12351 if (TREE_CODE (declarator) == CALL_EXPR)
12352 initializer = cp_parser_pure_specifier (parser);
12355 /* This declaration cannot be a function
12357 cp_parser_commit_to_tentative_parse (parser);
12358 /* Parse the initializer. */
12359 initializer = cp_parser_constant_initializer (parser);
12362 /* Otherwise, there is no initializer. */
12364 initializer = NULL_TREE;
12366 /* See if we are probably looking at a function
12367 definition. We are certainly not looking at at a
12368 member-declarator. Calling `grokfield' has
12369 side-effects, so we must not do it unless we are sure
12370 that we are looking at a member-declarator. */
12371 if (cp_parser_token_starts_function_definition_p
12372 (cp_lexer_peek_token (parser->lexer)))
12373 decl = error_mark_node;
12375 /* Create the declaration. */
12376 decl = grokfield (declarator,
12383 /* Reset PREFIX_ATTRIBUTES. */
12384 while (attributes && TREE_CHAIN (attributes) != first_attribute)
12385 attributes = TREE_CHAIN (attributes);
12387 TREE_CHAIN (attributes) = NULL_TREE;
12389 /* If there is any qualification still in effect, clear it
12390 now; we will be starting fresh with the next declarator. */
12391 parser->scope = NULL_TREE;
12392 parser->qualifying_scope = NULL_TREE;
12393 parser->object_scope = NULL_TREE;
12394 /* If it's a `,', then there are more declarators. */
12395 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
12396 cp_lexer_consume_token (parser->lexer);
12397 /* If the next token isn't a `;', then we have a parse error. */
12398 else if (cp_lexer_next_token_is_not (parser->lexer,
12401 cp_parser_error (parser, "expected `;'");
12402 /* Skip tokens until we find a `;' */
12403 cp_parser_skip_to_end_of_statement (parser);
12410 /* Add DECL to the list of members. */
12412 finish_member_declaration (decl);
12414 /* If DECL is a function, we must return
12415 to parse it later. (Even though there is no definition,
12416 there might be default arguments that need handling.) */
12417 if (TREE_CODE (decl) == FUNCTION_DECL)
12418 TREE_VALUE (parser->unparsed_functions_queues)
12419 = tree_cons (current_class_type, decl,
12420 TREE_VALUE (parser->unparsed_functions_queues));
12425 /* If everything went well, look for the `;'. */
12426 if (cp_parser_parse_definitely (parser))
12428 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
12432 /* Parse the function-definition. */
12433 decl = cp_parser_function_definition (parser, &friend_p);
12434 /* If the member was not a friend, declare it here. */
12436 finish_member_declaration (decl);
12437 /* Peek at the next token. */
12438 token = cp_lexer_peek_token (parser->lexer);
12439 /* If the next token is a semicolon, consume it. */
12440 if (token->type == CPP_SEMICOLON)
12441 cp_lexer_consume_token (parser->lexer);
12444 /* Parse a pure-specifier.
12449 Returns INTEGER_ZERO_NODE if a pure specifier is found.
12450 Otherwiser, ERROR_MARK_NODE is returned. */
12453 cp_parser_pure_specifier (parser)
12458 /* Look for the `=' token. */
12459 if (!cp_parser_require (parser, CPP_EQ, "`='"))
12460 return error_mark_node;
12461 /* Look for the `0' token. */
12462 token = cp_parser_require (parser, CPP_NUMBER, "`0'");
12463 /* Unfortunately, this will accept `0L' and `0x00' as well. We need
12464 to get information from the lexer about how the number was
12465 spelled in order to fix this problem. */
12466 if (!token || !integer_zerop (token->value))
12467 return error_mark_node;
12469 return integer_zero_node;
12472 /* Parse a constant-initializer.
12474 constant-initializer:
12475 = constant-expression
12477 Returns a representation of the constant-expression. */
12480 cp_parser_constant_initializer (parser)
12483 /* Look for the `=' token. */
12484 if (!cp_parser_require (parser, CPP_EQ, "`='"))
12485 return error_mark_node;
12487 /* It is invalid to write:
12489 struct S { static const int i = { 7 }; };
12492 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12494 cp_parser_error (parser,
12495 "a brace-enclosed initializer is not allowed here");
12496 /* Consume the opening brace. */
12497 cp_lexer_consume_token (parser->lexer);
12498 /* Skip the initializer. */
12499 cp_parser_skip_to_closing_brace (parser);
12500 /* Look for the trailing `}'. */
12501 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
12503 return error_mark_node;
12506 return cp_parser_constant_expression (parser);
12509 /* Derived classes [gram.class.derived] */
12511 /* Parse a base-clause.
12514 : base-specifier-list
12516 base-specifier-list:
12518 base-specifier-list , base-specifier
12520 Returns a TREE_LIST representing the base-classes, in the order in
12521 which they were declared. The representation of each node is as
12522 described by cp_parser_base_specifier.
12524 In the case that no bases are specified, this function will return
12525 NULL_TREE, not ERROR_MARK_NODE. */
12528 cp_parser_base_clause (parser)
12531 tree bases = NULL_TREE;
12533 /* Look for the `:' that begins the list. */
12534 cp_parser_require (parser, CPP_COLON, "`:'");
12536 /* Scan the base-specifier-list. */
12542 /* Look for the base-specifier. */
12543 base = cp_parser_base_specifier (parser);
12544 /* Add BASE to the front of the list. */
12545 if (base != error_mark_node)
12547 TREE_CHAIN (base) = bases;
12550 /* Peek at the next token. */
12551 token = cp_lexer_peek_token (parser->lexer);
12552 /* If it's not a comma, then the list is complete. */
12553 if (token->type != CPP_COMMA)
12555 /* Consume the `,'. */
12556 cp_lexer_consume_token (parser->lexer);
12559 /* PARSER->SCOPE may still be non-NULL at this point, if the last
12560 base class had a qualified name. However, the next name that
12561 appears is certainly not qualified. */
12562 parser->scope = NULL_TREE;
12563 parser->qualifying_scope = NULL_TREE;
12564 parser->object_scope = NULL_TREE;
12566 return nreverse (bases);
12569 /* Parse a base-specifier.
12572 :: [opt] nested-name-specifier [opt] class-name
12573 virtual access-specifier [opt] :: [opt] nested-name-specifier
12575 access-specifier virtual [opt] :: [opt] nested-name-specifier
12578 Returns a TREE_LIST. The TREE_PURPOSE will be one of
12579 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
12580 indicate the specifiers provided. The TREE_VALUE will be a TYPE
12581 (or the ERROR_MARK_NODE) indicating the type that was specified. */
12584 cp_parser_base_specifier (parser)
12589 bool virtual_p = false;
12590 bool duplicate_virtual_error_issued_p = false;
12591 bool duplicate_access_error_issued_p = false;
12592 bool class_scope_p;
12593 access_kind access = ak_none;
12597 /* Process the optional `virtual' and `access-specifier'. */
12600 /* Peek at the next token. */
12601 token = cp_lexer_peek_token (parser->lexer);
12602 /* Process `virtual'. */
12603 switch (token->keyword)
12606 /* If `virtual' appears more than once, issue an error. */
12607 if (virtual_p && !duplicate_virtual_error_issued_p)
12609 cp_parser_error (parser,
12610 "`virtual' specified more than once in base-specified");
12611 duplicate_virtual_error_issued_p = true;
12616 /* Consume the `virtual' token. */
12617 cp_lexer_consume_token (parser->lexer);
12622 case RID_PROTECTED:
12624 /* If more than one access specifier appears, issue an
12626 if (access != ak_none && !duplicate_access_error_issued_p)
12628 cp_parser_error (parser,
12629 "more than one access specifier in base-specified");
12630 duplicate_access_error_issued_p = true;
12633 access = ((access_kind)
12634 tree_low_cst (ridpointers[(int) token->keyword],
12637 /* Consume the access-specifier. */
12638 cp_lexer_consume_token (parser->lexer);
12648 /* Map `virtual_p' and `access' onto one of the access
12654 access_node = access_default_node;
12657 access_node = access_public_node;
12660 access_node = access_protected_node;
12663 access_node = access_private_node;
12672 access_node = access_default_virtual_node;
12675 access_node = access_public_virtual_node;
12678 access_node = access_protected_virtual_node;
12681 access_node = access_private_virtual_node;
12687 /* Look for the optional `::' operator. */
12688 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
12689 /* Look for the nested-name-specifier. The simplest way to
12694 The keyword `typename' is not permitted in a base-specifier or
12695 mem-initializer; in these contexts a qualified name that
12696 depends on a template-parameter is implicitly assumed to be a
12699 is to pretend that we have seen the `typename' keyword at this
12701 cp_parser_nested_name_specifier_opt (parser,
12702 /*typename_keyword_p=*/true,
12703 /*check_dependency_p=*/true,
12705 /* If the base class is given by a qualified name, assume that names
12706 we see are type names or templates, as appropriate. */
12707 class_scope_p = (parser->scope && TYPE_P (parser->scope));
12708 /* Finally, look for the class-name. */
12709 type = cp_parser_class_name (parser,
12713 /*check_access=*/true,
12714 /*check_dependency_p=*/true,
12715 /*class_head_p=*/false);
12717 if (type == error_mark_node)
12718 return error_mark_node;
12720 return finish_base_specifier (access_node, TREE_TYPE (type));
12723 /* Exception handling [gram.exception] */
12725 /* Parse an (optional) exception-specification.
12727 exception-specification:
12728 throw ( type-id-list [opt] )
12730 Returns a TREE_LIST representing the exception-specification. The
12731 TREE_VALUE of each node is a type. */
12734 cp_parser_exception_specification_opt (parser)
12740 /* Peek at the next token. */
12741 token = cp_lexer_peek_token (parser->lexer);
12742 /* If it's not `throw', then there's no exception-specification. */
12743 if (!cp_parser_is_keyword (token, RID_THROW))
12746 /* Consume the `throw'. */
12747 cp_lexer_consume_token (parser->lexer);
12749 /* Look for the `('. */
12750 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
12752 /* Peek at the next token. */
12753 token = cp_lexer_peek_token (parser->lexer);
12754 /* If it's not a `)', then there is a type-id-list. */
12755 if (token->type != CPP_CLOSE_PAREN)
12757 const char *saved_message;
12759 /* Types may not be defined in an exception-specification. */
12760 saved_message = parser->type_definition_forbidden_message;
12761 parser->type_definition_forbidden_message
12762 = "types may not be defined in an exception-specification";
12763 /* Parse the type-id-list. */
12764 type_id_list = cp_parser_type_id_list (parser);
12765 /* Restore the saved message. */
12766 parser->type_definition_forbidden_message = saved_message;
12769 type_id_list = empty_except_spec;
12771 /* Look for the `)'. */
12772 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
12774 return type_id_list;
12777 /* Parse an (optional) type-id-list.
12781 type-id-list , type-id
12783 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
12784 in the order that the types were presented. */
12787 cp_parser_type_id_list (parser)
12790 tree types = NULL_TREE;
12797 /* Get the next type-id. */
12798 type = cp_parser_type_id (parser);
12799 /* Add it to the list. */
12800 types = add_exception_specifier (types, type, /*complain=*/1);
12801 /* Peek at the next token. */
12802 token = cp_lexer_peek_token (parser->lexer);
12803 /* If it is not a `,', we are done. */
12804 if (token->type != CPP_COMMA)
12806 /* Consume the `,'. */
12807 cp_lexer_consume_token (parser->lexer);
12810 return nreverse (types);
12813 /* Parse a try-block.
12816 try compound-statement handler-seq */
12819 cp_parser_try_block (parser)
12824 cp_parser_require_keyword (parser, RID_TRY, "`try'");
12825 try_block = begin_try_block ();
12826 cp_parser_compound_statement (parser);
12827 finish_try_block (try_block);
12828 cp_parser_handler_seq (parser);
12829 finish_handler_sequence (try_block);
12834 /* Parse a function-try-block.
12836 function-try-block:
12837 try ctor-initializer [opt] function-body handler-seq */
12840 cp_parser_function_try_block (parser)
12844 bool ctor_initializer_p;
12846 /* Look for the `try' keyword. */
12847 if (!cp_parser_require_keyword (parser, RID_TRY, "`try'"))
12849 /* Let the rest of the front-end know where we are. */
12850 try_block = begin_function_try_block ();
12851 /* Parse the function-body. */
12853 = cp_parser_ctor_initializer_opt_and_function_body (parser);
12854 /* We're done with the `try' part. */
12855 finish_function_try_block (try_block);
12856 /* Parse the handlers. */
12857 cp_parser_handler_seq (parser);
12858 /* We're done with the handlers. */
12859 finish_function_handler_sequence (try_block);
12861 return ctor_initializer_p;
12864 /* Parse a handler-seq.
12867 handler handler-seq [opt] */
12870 cp_parser_handler_seq (parser)
12877 /* Parse the handler. */
12878 cp_parser_handler (parser);
12879 /* Peek at the next token. */
12880 token = cp_lexer_peek_token (parser->lexer);
12881 /* If it's not `catch' then there are no more handlers. */
12882 if (!cp_parser_is_keyword (token, RID_CATCH))
12887 /* Parse a handler.
12890 catch ( exception-declaration ) compound-statement */
12893 cp_parser_handler (parser)
12899 cp_parser_require_keyword (parser, RID_CATCH, "`catch'");
12900 handler = begin_handler ();
12901 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
12902 declaration = cp_parser_exception_declaration (parser);
12903 finish_handler_parms (declaration, handler);
12904 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
12905 cp_parser_compound_statement (parser);
12906 finish_handler (handler);
12909 /* Parse an exception-declaration.
12911 exception-declaration:
12912 type-specifier-seq declarator
12913 type-specifier-seq abstract-declarator
12917 Returns a VAR_DECL for the declaration, or NULL_TREE if the
12918 ellipsis variant is used. */
12921 cp_parser_exception_declaration (parser)
12924 tree type_specifiers;
12926 const char *saved_message;
12928 /* If it's an ellipsis, it's easy to handle. */
12929 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
12931 /* Consume the `...' token. */
12932 cp_lexer_consume_token (parser->lexer);
12936 /* Types may not be defined in exception-declarations. */
12937 saved_message = parser->type_definition_forbidden_message;
12938 parser->type_definition_forbidden_message
12939 = "types may not be defined in exception-declarations";
12941 /* Parse the type-specifier-seq. */
12942 type_specifiers = cp_parser_type_specifier_seq (parser);
12943 /* If it's a `)', then there is no declarator. */
12944 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
12945 declarator = NULL_TREE;
12948 /* Otherwise, we can't be sure whether we are looking at a
12949 direct, or an abstract, declarator. */
12950 cp_parser_parse_tentatively (parser);
12951 /* Try an ordinary declarator. */
12952 declarator = cp_parser_declarator (parser,
12953 /*abstract_p=*/false,
12954 /*ctor_dtor_or_conv_p=*/NULL);
12955 /* If that didn't work, try an abstract declarator. */
12956 if (!cp_parser_parse_definitely (parser))
12957 declarator = cp_parser_declarator (parser,
12958 /*abstract_p=*/true,
12959 /*ctor_dtor_or_conv_p=*/NULL);
12962 /* Restore the saved message. */
12963 parser->type_definition_forbidden_message = saved_message;
12965 return start_handler_parms (type_specifiers, declarator);
12968 /* Parse a throw-expression.
12971 throw assignment-expresion [opt]
12973 Returns a THROW_EXPR representing the throw-expression. */
12976 cp_parser_throw_expression (parser)
12981 cp_parser_require_keyword (parser, RID_THROW, "`throw'");
12982 /* We can't be sure if there is an assignment-expression or not. */
12983 cp_parser_parse_tentatively (parser);
12985 expression = cp_parser_assignment_expression (parser);
12986 /* If it didn't work, this is just a rethrow. */
12987 if (!cp_parser_parse_definitely (parser))
12988 expression = NULL_TREE;
12990 return build_throw (expression);
12993 /* GNU Extensions */
12995 /* Parse an (optional) asm-specification.
12998 asm ( string-literal )
13000 If the asm-specification is present, returns a STRING_CST
13001 corresponding to the string-literal. Otherwise, returns
13005 cp_parser_asm_specification_opt (parser)
13009 tree asm_specification;
13011 /* Peek at the next token. */
13012 token = cp_lexer_peek_token (parser->lexer);
13013 /* If the next token isn't the `asm' keyword, then there's no
13014 asm-specification. */
13015 if (!cp_parser_is_keyword (token, RID_ASM))
13018 /* Consume the `asm' token. */
13019 cp_lexer_consume_token (parser->lexer);
13020 /* Look for the `('. */
13021 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13023 /* Look for the string-literal. */
13024 token = cp_parser_require (parser, CPP_STRING, "string-literal");
13026 asm_specification = token->value;
13028 asm_specification = NULL_TREE;
13030 /* Look for the `)'. */
13031 cp_parser_require (parser, CPP_CLOSE_PAREN, "`('");
13033 return asm_specification;
13036 /* Parse an asm-operand-list.
13040 asm-operand-list , asm-operand
13043 string-literal ( expression )
13044 [ string-literal ] string-literal ( expression )
13046 Returns a TREE_LIST representing the operands. The TREE_VALUE of
13047 each node is the expression. The TREE_PURPOSE is itself a
13048 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
13049 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
13050 is a STRING_CST for the string literal before the parenthesis. */
13053 cp_parser_asm_operand_list (parser)
13056 tree asm_operands = NULL_TREE;
13060 tree string_literal;
13065 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
13067 /* Consume the `[' token. */
13068 cp_lexer_consume_token (parser->lexer);
13069 /* Read the operand name. */
13070 name = cp_parser_identifier (parser);
13071 if (name != error_mark_node)
13072 name = build_string (IDENTIFIER_LENGTH (name),
13073 IDENTIFIER_POINTER (name));
13074 /* Look for the closing `]'. */
13075 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
13079 /* Look for the string-literal. */
13080 token = cp_parser_require (parser, CPP_STRING, "string-literal");
13081 string_literal = token ? token->value : error_mark_node;
13082 /* Look for the `('. */
13083 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13084 /* Parse the expression. */
13085 expression = cp_parser_expression (parser);
13086 /* Look for the `)'. */
13087 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13088 /* Add this operand to the list. */
13089 asm_operands = tree_cons (build_tree_list (name, string_literal),
13092 /* If the next token is not a `,', there are no more
13094 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13096 /* Consume the `,'. */
13097 cp_lexer_consume_token (parser->lexer);
13100 return nreverse (asm_operands);
13103 /* Parse an asm-clobber-list.
13107 asm-clobber-list , string-literal
13109 Returns a TREE_LIST, indicating the clobbers in the order that they
13110 appeared. The TREE_VALUE of each node is a STRING_CST. */
13113 cp_parser_asm_clobber_list (parser)
13116 tree clobbers = NULL_TREE;
13121 tree string_literal;
13123 /* Look for the string literal. */
13124 token = cp_parser_require (parser, CPP_STRING, "string-literal");
13125 string_literal = token ? token->value : error_mark_node;
13126 /* Add it to the list. */
13127 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
13128 /* If the next token is not a `,', then the list is
13130 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13132 /* Consume the `,' token. */
13133 cp_lexer_consume_token (parser->lexer);
13139 /* Parse an (optional) series of attributes.
13142 attributes attribute
13145 __attribute__ (( attribute-list [opt] ))
13147 The return value is as for cp_parser_attribute_list. */
13150 cp_parser_attributes_opt (parser)
13153 tree attributes = NULL_TREE;
13158 tree attribute_list;
13160 /* Peek at the next token. */
13161 token = cp_lexer_peek_token (parser->lexer);
13162 /* If it's not `__attribute__', then we're done. */
13163 if (token->keyword != RID_ATTRIBUTE)
13166 /* Consume the `__attribute__' keyword. */
13167 cp_lexer_consume_token (parser->lexer);
13168 /* Look for the two `(' tokens. */
13169 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13170 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13172 /* Peek at the next token. */
13173 token = cp_lexer_peek_token (parser->lexer);
13174 if (token->type != CPP_CLOSE_PAREN)
13175 /* Parse the attribute-list. */
13176 attribute_list = cp_parser_attribute_list (parser);
13178 /* If the next token is a `)', then there is no attribute
13180 attribute_list = NULL;
13182 /* Look for the two `)' tokens. */
13183 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13184 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13186 /* Add these new attributes to the list. */
13187 attributes = chainon (attributes, attribute_list);
13193 /* Parse an attribute-list.
13197 attribute-list , attribute
13201 identifier ( identifier )
13202 identifier ( identifier , expression-list )
13203 identifier ( expression-list )
13205 Returns a TREE_LIST. Each node corresponds to an attribute. THe
13206 TREE_PURPOSE of each node is the identifier indicating which
13207 attribute is in use. The TREE_VALUE represents the arguments, if
13211 cp_parser_attribute_list (parser)
13214 tree attribute_list = NULL_TREE;
13222 /* Look for the identifier. We also allow keywords here; for
13223 example `__attribute__ ((const))' is legal. */
13224 token = cp_lexer_peek_token (parser->lexer);
13225 if (token->type != CPP_NAME
13226 && token->type != CPP_KEYWORD)
13227 return error_mark_node;
13228 /* Consume the token. */
13229 token = cp_lexer_consume_token (parser->lexer);
13231 /* Save away the identifier that indicates which attribute this is. */
13232 identifier = token->value;
13233 attribute = build_tree_list (identifier, NULL_TREE);
13235 /* Peek at the next token. */
13236 token = cp_lexer_peek_token (parser->lexer);
13237 /* If it's an `(', then parse the attribute arguments. */
13238 if (token->type == CPP_OPEN_PAREN)
13241 int arguments_allowed_p = 1;
13243 /* Consume the `('. */
13244 cp_lexer_consume_token (parser->lexer);
13245 /* Peek at the next token. */
13246 token = cp_lexer_peek_token (parser->lexer);
13247 /* Check to see if the next token is an identifier. */
13248 if (token->type == CPP_NAME)
13250 /* Save the identifier. */
13251 identifier = token->value;
13252 /* Consume the identifier. */
13253 cp_lexer_consume_token (parser->lexer);
13254 /* Peek at the next token. */
13255 token = cp_lexer_peek_token (parser->lexer);
13256 /* If the next token is a `,', then there are some other
13257 expressions as well. */
13258 if (token->type == CPP_COMMA)
13259 /* Consume the comma. */
13260 cp_lexer_consume_token (parser->lexer);
13262 arguments_allowed_p = 0;
13265 identifier = NULL_TREE;
13267 /* If there are arguments, parse them too. */
13268 if (arguments_allowed_p)
13269 arguments = cp_parser_expression_list (parser);
13271 arguments = NULL_TREE;
13273 /* Combine the identifier and the arguments. */
13275 arguments = tree_cons (NULL_TREE, identifier, arguments);
13277 /* Save the identifier and arguments away. */
13278 TREE_VALUE (attribute) = arguments;
13280 /* Look for the closing `)'. */
13281 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13284 /* Add this attribute to the list. */
13285 TREE_CHAIN (attribute) = attribute_list;
13286 attribute_list = attribute;
13288 /* Now, look for more attributes. */
13289 token = cp_lexer_peek_token (parser->lexer);
13290 /* If the next token isn't a `,', we're done. */
13291 if (token->type != CPP_COMMA)
13294 /* Consume the commma and keep going. */
13295 cp_lexer_consume_token (parser->lexer);
13298 /* We built up the list in reverse order. */
13299 return nreverse (attribute_list);
13302 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
13303 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
13304 current value of the PEDANTIC flag, regardless of whether or not
13305 the `__extension__' keyword is present. The caller is responsible
13306 for restoring the value of the PEDANTIC flag. */
13309 cp_parser_extension_opt (parser, saved_pedantic)
13311 int *saved_pedantic;
13313 /* Save the old value of the PEDANTIC flag. */
13314 *saved_pedantic = pedantic;
13316 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
13318 /* Consume the `__extension__' token. */
13319 cp_lexer_consume_token (parser->lexer);
13320 /* We're not being pedantic while the `__extension__' keyword is
13330 /* Parse a label declaration.
13333 __label__ label-declarator-seq ;
13335 label-declarator-seq:
13336 identifier , label-declarator-seq
13340 cp_parser_label_declaration (parser)
13343 /* Look for the `__label__' keyword. */
13344 cp_parser_require_keyword (parser, RID_LABEL, "`__label__'");
13350 /* Look for an identifier. */
13351 identifier = cp_parser_identifier (parser);
13352 /* Declare it as a lobel. */
13353 finish_label_decl (identifier);
13354 /* If the next token is a `;', stop. */
13355 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13357 /* Look for the `,' separating the label declarations. */
13358 cp_parser_require (parser, CPP_COMMA, "`,'");
13361 /* Look for the final `;'. */
13362 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
13365 /* Support Functions */
13367 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
13368 NAME should have one of the representations used for an
13369 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
13370 is returned. If PARSER->SCOPE is a dependent type, then a
13371 SCOPE_REF is returned.
13373 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
13374 returned; the name was already resolved when the TEMPLATE_ID_EXPR
13375 was formed. Abstractly, such entities should not be passed to this
13376 function, because they do not need to be looked up, but it is
13377 simpler to check for this special case here, rather than at the
13380 In cases not explicitly covered above, this function returns a
13381 DECL, OVERLOAD, or baselink representing the result of the lookup.
13382 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
13385 If CHECK_ACCESS is TRUE, then access control is performed on the
13386 declaration to which the name resolves, and an error message is
13387 issued if the declaration is inaccessible.
13389 If IS_TYPE is TRUE, bindings that do not refer to types are
13392 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
13395 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
13399 cp_parser_lookup_name (cp_parser *parser, tree name, bool check_access,
13400 bool is_type, bool is_namespace, bool check_dependency)
13403 tree object_type = parser->context->object_type;
13405 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
13406 no longer valid. Note that if we are parsing tentatively, and
13407 the parse fails, OBJECT_TYPE will be automatically restored. */
13408 parser->context->object_type = NULL_TREE;
13410 if (name == error_mark_node)
13411 return error_mark_node;
13413 /* A template-id has already been resolved; there is no lookup to
13415 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
13417 if (BASELINK_P (name))
13419 my_friendly_assert ((TREE_CODE (BASELINK_FUNCTIONS (name))
13420 == TEMPLATE_ID_EXPR),
13425 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
13426 it should already have been checked to make sure that the name
13427 used matches the type being destroyed. */
13428 if (TREE_CODE (name) == BIT_NOT_EXPR)
13432 /* Figure out to which type this destructor applies. */
13434 type = parser->scope;
13435 else if (object_type)
13436 type = object_type;
13438 type = current_class_type;
13439 /* If that's not a class type, there is no destructor. */
13440 if (!type || !CLASS_TYPE_P (type))
13441 return error_mark_node;
13442 /* If it was a class type, return the destructor. */
13443 return CLASSTYPE_DESTRUCTORS (type);
13446 /* By this point, the NAME should be an ordinary identifier. If
13447 the id-expression was a qualified name, the qualifying scope is
13448 stored in PARSER->SCOPE at this point. */
13449 my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE,
13452 /* Perform the lookup. */
13455 bool dependent_type_p;
13457 if (parser->scope == error_mark_node)
13458 return error_mark_node;
13460 /* If the SCOPE is dependent, the lookup must be deferred until
13461 the template is instantiated -- unless we are explicitly
13462 looking up names in uninstantiated templates. Even then, we
13463 cannot look up the name if the scope is not a class type; it
13464 might, for example, be a template type parameter. */
13465 dependent_type_p = (TYPE_P (parser->scope)
13466 && !(parser->in_declarator_p
13467 && currently_open_class (parser->scope))
13468 && cp_parser_dependent_type_p (parser->scope));
13469 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
13470 && dependent_type_p)
13473 decl = build_nt (SCOPE_REF, parser->scope, name);
13475 /* The resolution to Core Issue 180 says that `struct A::B'
13476 should be considered a type-name, even if `A' is
13478 decl = TYPE_NAME (make_typename_type (parser->scope,
13484 /* If PARSER->SCOPE is a dependent type, then it must be a
13485 class type, and we must not be checking dependencies;
13486 otherwise, we would have processed this lookup above. So
13487 that PARSER->SCOPE is not considered a dependent base by
13488 lookup_member, we must enter the scope here. */
13489 if (dependent_type_p)
13490 push_scope (parser->scope);
13491 /* If the PARSER->SCOPE is a a template specialization, it
13492 may be instantiated during name lookup. In that case,
13493 errors may be issued. Even if we rollback the current
13494 tentative parse, those errors are valid. */
13495 decl = lookup_qualified_name (parser->scope, name, is_type,
13497 if (dependent_type_p)
13498 pop_scope (parser->scope);
13500 parser->qualifying_scope = parser->scope;
13501 parser->object_scope = NULL_TREE;
13503 else if (object_type)
13505 tree object_decl = NULL_TREE;
13506 /* Look up the name in the scope of the OBJECT_TYPE, unless the
13507 OBJECT_TYPE is not a class. */
13508 if (CLASS_TYPE_P (object_type))
13509 /* If the OBJECT_TYPE is a template specialization, it may
13510 be instantiated during name lookup. In that case, errors
13511 may be issued. Even if we rollback the current tentative
13512 parse, those errors are valid. */
13513 object_decl = lookup_member (object_type,
13515 /*protect=*/0, is_type);
13516 /* Look it up in the enclosing context, too. */
13517 decl = lookup_name_real (name, is_type, /*nonclass=*/0,
13520 parser->object_scope = object_type;
13521 parser->qualifying_scope = NULL_TREE;
13523 decl = object_decl;
13527 decl = lookup_name_real (name, is_type, /*nonclass=*/0,
13530 parser->qualifying_scope = NULL_TREE;
13531 parser->object_scope = NULL_TREE;
13534 /* If the lookup failed, let our caller know. */
13536 || decl == error_mark_node
13537 || (TREE_CODE (decl) == FUNCTION_DECL
13538 && DECL_ANTICIPATED (decl)))
13539 return error_mark_node;
13541 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
13542 if (TREE_CODE (decl) == TREE_LIST)
13544 /* The error message we have to print is too complicated for
13545 cp_parser_error, so we incorporate its actions directly. */
13546 if (!cp_parser_simulate_error (parser))
13548 error ("reference to `%D' is ambiguous", name);
13549 print_candidates (decl);
13551 return error_mark_node;
13554 my_friendly_assert (DECL_P (decl)
13555 || TREE_CODE (decl) == OVERLOAD
13556 || TREE_CODE (decl) == SCOPE_REF
13557 || BASELINK_P (decl),
13560 /* If we have resolved the name of a member declaration, check to
13561 see if the declaration is accessible. When the name resolves to
13562 set of overloaded functions, accesibility is checked when
13563 overload resolution is done.
13565 During an explicit instantiation, access is not checked at all,
13566 as per [temp.explicit]. */
13567 if (check_access && scope_chain->check_access && DECL_P (decl))
13569 tree qualifying_type;
13571 /* Figure out the type through which DECL is being
13574 = cp_parser_scope_through_which_access_occurs (decl,
13577 if (qualifying_type)
13579 /* If we are supposed to defer access checks, just record
13580 the information for later. */
13581 if (parser->context->deferring_access_checks_p)
13582 cp_parser_defer_access_check (parser, qualifying_type, decl);
13583 /* Otherwise, check accessibility now. */
13585 enforce_access (qualifying_type, decl);
13592 /* Like cp_parser_lookup_name, but for use in the typical case where
13593 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, and CHECK_DEPENDENCY is
13597 cp_parser_lookup_name_simple (parser, name)
13601 return cp_parser_lookup_name (parser, name,
13602 /*check_access=*/true,
13604 /*is_namespace=*/false,
13605 /*check_dependency=*/true);
13608 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
13609 TYPENAME_TYPE corresponds. Note that this function peers inside
13610 uninstantiated templates and therefore should be used only in
13611 extremely limited situations. */
13614 cp_parser_resolve_typename_type (parser, type)
13622 my_friendly_assert (TREE_CODE (type) == TYPENAME_TYPE,
13625 scope = TYPE_CONTEXT (type);
13626 name = DECL_NAME (TYPE_NAME (type));
13628 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
13629 it first before we can figure out what NAME refers to. */
13630 if (TREE_CODE (scope) == TYPENAME_TYPE)
13631 scope = cp_parser_resolve_typename_type (parser, scope);
13632 /* If we don't know what SCOPE refers to, then we cannot resolve the
13634 if (scope == error_mark_node)
13635 return error_mark_node;
13636 /* If the SCOPE is a template type parameter, we have no way of
13637 resolving the name. */
13638 if (TREE_CODE (scope) == TEMPLATE_TYPE_PARM)
13640 /* Enter the SCOPE so that name lookup will be resolved as if we
13641 were in the class definition. In particular, SCOPE will no
13642 longer be considered a dependent type. */
13643 push_scope (scope);
13644 /* Look up the declaration. */
13645 decl = lookup_member (scope, name, /*protect=*/0, /*want_type=*/1);
13646 /* If all went well, we got a TYPE_DECL for a non-typename. */
13648 || TREE_CODE (decl) != TYPE_DECL
13649 || TREE_CODE (TREE_TYPE (decl)) == TYPENAME_TYPE)
13651 cp_parser_error (parser, "could not resolve typename type");
13652 type = error_mark_node;
13655 type = TREE_TYPE (decl);
13656 /* Leave the SCOPE. */
13662 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
13663 the current context, return the TYPE_DECL. If TAG_NAME_P is
13664 true, the DECL indicates the class being defined in a class-head,
13665 or declared in an elaborated-type-specifier.
13667 Otherwise, return DECL. */
13670 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
13672 /* If the DECL is a TEMPLATE_DECL for a class type, and we are in
13673 the scope of the class, then treat the TEMPLATE_DECL as a
13674 class-name. For example, in:
13676 template <class T> struct S {
13682 If the TEMPLATE_DECL is being declared as part of a class-head,
13683 the same translation occurs:
13686 template <typename T> struct B;
13689 template <typename T> struct A::B {};
13691 Similarly, in a elaborated-type-specifier:
13693 namespace N { struct X{}; }
13696 template <typename T> friend struct N::X;
13700 if (DECL_CLASS_TEMPLATE_P (decl)
13702 || (current_class_type
13703 && same_type_p (TREE_TYPE (DECL_TEMPLATE_RESULT (decl)),
13704 current_class_type))))
13705 return DECL_TEMPLATE_RESULT (decl);
13710 /* If too many, or too few, template-parameter lists apply to the
13711 declarator, issue an error message. Returns TRUE if all went well,
13712 and FALSE otherwise. */
13715 cp_parser_check_declarator_template_parameters (parser, declarator)
13719 unsigned num_templates;
13721 /* We haven't seen any classes that involve template parameters yet. */
13724 switch (TREE_CODE (declarator))
13731 tree main_declarator = TREE_OPERAND (declarator, 0);
13733 cp_parser_check_declarator_template_parameters (parser,
13742 scope = TREE_OPERAND (declarator, 0);
13743 member = TREE_OPERAND (declarator, 1);
13745 /* If this is a pointer-to-member, then we are not interested
13746 in the SCOPE, because it does not qualify the thing that is
13748 if (TREE_CODE (member) == INDIRECT_REF)
13749 return (cp_parser_check_declarator_template_parameters
13752 while (scope && CLASS_TYPE_P (scope))
13754 /* You're supposed to have one `template <...>'
13755 for every template class, but you don't need one
13756 for a full specialization. For example:
13758 template <class T> struct S{};
13759 template <> struct S<int> { void f(); };
13760 void S<int>::f () {}
13762 is correct; there shouldn't be a `template <>' for
13763 the definition of `S<int>::f'. */
13764 if (CLASSTYPE_TEMPLATE_INFO (scope)
13765 && (CLASSTYPE_TEMPLATE_INSTANTIATION (scope)
13766 || uses_template_parms (CLASSTYPE_TI_ARGS (scope)))
13767 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
13770 scope = TYPE_CONTEXT (scope);
13774 /* Fall through. */
13777 /* If the DECLARATOR has the form `X<y>' then it uses one
13778 additional level of template parameters. */
13779 if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
13782 return cp_parser_check_template_parameters (parser,
13787 /* NUM_TEMPLATES were used in the current declaration. If that is
13788 invalid, return FALSE and issue an error messages. Otherwise,
13792 cp_parser_check_template_parameters (parser, num_templates)
13794 unsigned num_templates;
13796 /* If there are more template classes than parameter lists, we have
13799 template <class T> void S<T>::R<T>::f (); */
13800 if (parser->num_template_parameter_lists < num_templates)
13802 error ("too few template-parameter-lists");
13805 /* If there are the same number of template classes and parameter
13806 lists, that's OK. */
13807 if (parser->num_template_parameter_lists == num_templates)
13809 /* If there are more, but only one more, then we are referring to a
13810 member template. That's OK too. */
13811 if (parser->num_template_parameter_lists == num_templates + 1)
13813 /* Otherwise, there are too many template parameter lists. We have
13816 template <class T> template <class U> void S::f(); */
13817 error ("too many template-parameter-lists");
13821 /* Parse a binary-expression of the general form:
13825 binary-expression <token> <expr>
13827 The TOKEN_TREE_MAP maps <token> types to <expr> codes. FN is used
13828 to parser the <expr>s. If the first production is used, then the
13829 value returned by FN is returned directly. Otherwise, a node with
13830 the indicated EXPR_TYPE is returned, with operands corresponding to
13831 the two sub-expressions. */
13834 cp_parser_binary_expression (parser, token_tree_map, fn)
13836 cp_parser_token_tree_map token_tree_map;
13837 cp_parser_expression_fn fn;
13841 /* Parse the first expression. */
13842 lhs = (*fn) (parser);
13843 /* Now, look for more expressions. */
13847 cp_parser_token_tree_map_node *map_node;
13850 /* Peek at the next token. */
13851 token = cp_lexer_peek_token (parser->lexer);
13852 /* If the token is `>', and that's not an operator at the
13853 moment, then we're done. */
13854 if (token->type == CPP_GREATER
13855 && !parser->greater_than_is_operator_p)
13857 /* If we find one of the tokens we want, build the correspoding
13858 tree representation. */
13859 for (map_node = token_tree_map;
13860 map_node->token_type != CPP_EOF;
13862 if (map_node->token_type == token->type)
13864 /* Consume the operator token. */
13865 cp_lexer_consume_token (parser->lexer);
13866 /* Parse the right-hand side of the expression. */
13867 rhs = (*fn) (parser);
13868 /* Build the binary tree node. */
13869 lhs = build_x_binary_op (map_node->tree_type, lhs, rhs);
13873 /* If the token wasn't one of the ones we want, we're done. */
13874 if (map_node->token_type == CPP_EOF)
13881 /* Parse an optional `::' token indicating that the following name is
13882 from the global namespace. If so, PARSER->SCOPE is set to the
13883 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
13884 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
13885 Returns the new value of PARSER->SCOPE, if the `::' token is
13886 present, and NULL_TREE otherwise. */
13889 cp_parser_global_scope_opt (parser, current_scope_valid_p)
13891 bool current_scope_valid_p;
13895 /* Peek at the next token. */
13896 token = cp_lexer_peek_token (parser->lexer);
13897 /* If we're looking at a `::' token then we're starting from the
13898 global namespace, not our current location. */
13899 if (token->type == CPP_SCOPE)
13901 /* Consume the `::' token. */
13902 cp_lexer_consume_token (parser->lexer);
13903 /* Set the SCOPE so that we know where to start the lookup. */
13904 parser->scope = global_namespace;
13905 parser->qualifying_scope = global_namespace;
13906 parser->object_scope = NULL_TREE;
13908 return parser->scope;
13910 else if (!current_scope_valid_p)
13912 parser->scope = NULL_TREE;
13913 parser->qualifying_scope = NULL_TREE;
13914 parser->object_scope = NULL_TREE;
13920 /* Returns TRUE if the upcoming token sequence is the start of a
13921 constructor declarator. If FRIEND_P is true, the declarator is
13922 preceded by the `friend' specifier. */
13925 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
13927 bool constructor_p;
13928 tree type_decl = NULL_TREE;
13929 bool nested_name_p;
13931 /* Parse tentatively; we are going to roll back all of the tokens
13933 cp_parser_parse_tentatively (parser);
13934 /* Assume that we are looking at a constructor declarator. */
13935 constructor_p = true;
13936 /* Look for the optional `::' operator. */
13937 cp_parser_global_scope_opt (parser,
13938 /*current_scope_valid_p=*/false);
13939 /* Look for the nested-name-specifier. */
13941 = (cp_parser_nested_name_specifier_opt (parser,
13942 /*typename_keyword_p=*/false,
13943 /*check_dependency_p=*/false,
13946 /* Outside of a class-specifier, there must be a
13947 nested-name-specifier. */
13948 if (!nested_name_p &&
13949 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
13951 constructor_p = false;
13952 /* If we still think that this might be a constructor-declarator,
13953 look for a class-name. */
13958 template <typename T> struct S { S(); }
13959 template <typename T> S<T>::S ();
13961 we must recognize that the nested `S' names a class.
13964 template <typename T> S<T>::S<T> ();
13966 we must recognize that the nested `S' names a template. */
13967 type_decl = cp_parser_class_name (parser,
13968 /*typename_keyword_p=*/false,
13969 /*template_keyword_p=*/false,
13971 /*check_access_p=*/false,
13972 /*check_dependency_p=*/false,
13973 /*class_head_p=*/false);
13974 /* If there was no class-name, then this is not a constructor. */
13975 constructor_p = !cp_parser_error_occurred (parser);
13977 /* If we're still considering a constructor, we have to see a `(',
13978 to begin the parameter-declaration-clause, followed by either a
13979 `)', an `...', or a decl-specifier. We need to check for a
13980 type-specifier to avoid being fooled into thinking that:
13984 is a constructor. (It is actually a function named `f' that
13985 takes one parameter (of type `int') and returns a value of type
13988 && cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
13990 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
13991 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
13992 && !cp_parser_storage_class_specifier_opt (parser))
13994 if (current_class_type
13995 && !same_type_p (current_class_type, TREE_TYPE (type_decl)))
13996 /* The constructor for one class cannot be declared inside
13998 constructor_p = false;
14003 /* Names appearing in the type-specifier should be looked up
14004 in the scope of the class. */
14005 if (current_class_type)
14009 type = TREE_TYPE (type_decl);
14010 if (TREE_CODE (type) == TYPENAME_TYPE)
14011 type = cp_parser_resolve_typename_type (parser, type);
14014 /* Look for the type-specifier. */
14015 cp_parser_type_specifier (parser,
14016 CP_PARSER_FLAGS_NONE,
14017 /*is_friend=*/false,
14018 /*is_declarator=*/true,
14019 /*declares_class_or_enum=*/NULL,
14020 /*is_cv_qualifier=*/NULL);
14021 /* Leave the scope of the class. */
14025 constructor_p = !cp_parser_error_occurred (parser);
14030 constructor_p = false;
14031 /* We did not really want to consume any tokens. */
14032 cp_parser_abort_tentative_parse (parser);
14034 return constructor_p;
14037 /* Parse the definition of the function given by the DECL_SPECIFIERS,
14038 ATTRIBUTES, and DECLARATOR. The ACCESS_CHECKS have been deferred;
14039 they must be performed once we are in the scope of the function.
14041 Returns the function defined. */
14044 cp_parser_function_definition_from_specifiers_and_declarator
14045 (parser, decl_specifiers, attributes, declarator, access_checks)
14047 tree decl_specifiers;
14050 tree access_checks;
14055 /* Begin the function-definition. */
14056 success_p = begin_function_definition (decl_specifiers,
14060 /* If there were names looked up in the decl-specifier-seq that we
14061 did not check, check them now. We must wait until we are in the
14062 scope of the function to perform the checks, since the function
14063 might be a friend. */
14064 cp_parser_perform_deferred_access_checks (access_checks);
14068 /* If begin_function_definition didn't like the definition, skip
14069 the entire function. */
14070 error ("invalid function declaration");
14071 cp_parser_skip_to_end_of_block_or_statement (parser);
14072 fn = error_mark_node;
14075 fn = cp_parser_function_definition_after_declarator (parser,
14076 /*inline_p=*/false);
14081 /* Parse the part of a function-definition that follows the
14082 declarator. INLINE_P is TRUE iff this function is an inline
14083 function defined with a class-specifier.
14085 Returns the function defined. */
14088 cp_parser_function_definition_after_declarator (parser,
14094 bool ctor_initializer_p = false;
14095 bool saved_in_unbraced_linkage_specification_p;
14096 unsigned saved_num_template_parameter_lists;
14098 /* If the next token is `return', then the code may be trying to
14099 make use of the "named return value" extension that G++ used to
14101 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
14103 /* Consume the `return' keyword. */
14104 cp_lexer_consume_token (parser->lexer);
14105 /* Look for the identifier that indicates what value is to be
14107 cp_parser_identifier (parser);
14108 /* Issue an error message. */
14109 error ("named return values are no longer supported");
14110 /* Skip tokens until we reach the start of the function body. */
14111 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
14112 cp_lexer_consume_token (parser->lexer);
14114 /* The `extern' in `extern "C" void f () { ... }' does not apply to
14115 anything declared inside `f'. */
14116 saved_in_unbraced_linkage_specification_p
14117 = parser->in_unbraced_linkage_specification_p;
14118 parser->in_unbraced_linkage_specification_p = false;
14119 /* Inside the function, surrounding template-parameter-lists do not
14121 saved_num_template_parameter_lists
14122 = parser->num_template_parameter_lists;
14123 parser->num_template_parameter_lists = 0;
14124 /* If the next token is `try', then we are looking at a
14125 function-try-block. */
14126 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
14127 ctor_initializer_p = cp_parser_function_try_block (parser);
14128 /* A function-try-block includes the function-body, so we only do
14129 this next part if we're not processing a function-try-block. */
14132 = cp_parser_ctor_initializer_opt_and_function_body (parser);
14134 /* Finish the function. */
14135 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
14136 (inline_p ? 2 : 0));
14137 /* Generate code for it, if necessary. */
14139 /* Restore the saved values. */
14140 parser->in_unbraced_linkage_specification_p
14141 = saved_in_unbraced_linkage_specification_p;
14142 parser->num_template_parameter_lists
14143 = saved_num_template_parameter_lists;
14148 /* Parse a template-declaration, assuming that the `export' (and
14149 `extern') keywords, if present, has already been scanned. MEMBER_P
14150 is as for cp_parser_template_declaration. */
14153 cp_parser_template_declaration_after_export (parser, member_p)
14157 tree decl = NULL_TREE;
14158 tree parameter_list;
14159 bool friend_p = false;
14161 /* Look for the `template' keyword. */
14162 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'"))
14166 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
14169 /* Parse the template parameters. */
14170 begin_template_parm_list ();
14171 /* If the next token is `>', then we have an invalid
14172 specialization. Rather than complain about an invalid template
14173 parameter, issue an error message here. */
14174 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
14176 cp_parser_error (parser, "invalid explicit specialization");
14177 parameter_list = NULL_TREE;
14180 parameter_list = cp_parser_template_parameter_list (parser);
14181 parameter_list = end_template_parm_list (parameter_list);
14182 /* Look for the `>'. */
14183 cp_parser_skip_until_found (parser, CPP_GREATER, "`>'");
14184 /* We just processed one more parameter list. */
14185 ++parser->num_template_parameter_lists;
14186 /* If the next token is `template', there are more template
14188 if (cp_lexer_next_token_is_keyword (parser->lexer,
14190 cp_parser_template_declaration_after_export (parser, member_p);
14193 decl = cp_parser_single_declaration (parser,
14197 /* If this is a member template declaration, let the front
14199 if (member_p && !friend_p && decl)
14200 decl = finish_member_template_decl (decl);
14201 else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
14202 make_friend_class (current_class_type, TREE_TYPE (decl));
14204 /* We are done with the current parameter list. */
14205 --parser->num_template_parameter_lists;
14208 finish_template_decl (parameter_list);
14210 /* Register member declarations. */
14211 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
14212 finish_member_declaration (decl);
14214 /* If DECL is a function template, we must return to parse it later.
14215 (Even though there is no definition, there might be default
14216 arguments that need handling.) */
14217 if (member_p && decl
14218 && (TREE_CODE (decl) == FUNCTION_DECL
14219 || DECL_FUNCTION_TEMPLATE_P (decl)))
14220 TREE_VALUE (parser->unparsed_functions_queues)
14221 = tree_cons (current_class_type, decl,
14222 TREE_VALUE (parser->unparsed_functions_queues));
14225 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
14226 `function-definition' sequence. MEMBER_P is true, this declaration
14227 appears in a class scope.
14229 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
14230 *FRIEND_P is set to TRUE iff the declaration is a friend. */
14233 cp_parser_single_declaration (parser,
14240 bool declares_class_or_enum;
14241 tree decl = NULL_TREE;
14242 tree decl_specifiers;
14244 tree access_checks;
14246 /* Parse the dependent declaration. We don't know yet
14247 whether it will be a function-definition. */
14248 cp_parser_parse_tentatively (parser);
14249 /* Defer access checks until we know what is being declared. */
14250 cp_parser_start_deferring_access_checks (parser);
14251 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
14254 = cp_parser_decl_specifier_seq (parser,
14255 CP_PARSER_FLAGS_OPTIONAL,
14257 &declares_class_or_enum);
14258 /* Gather up the access checks that occurred the
14259 decl-specifier-seq. */
14260 access_checks = cp_parser_stop_deferring_access_checks (parser);
14261 /* Check for the declaration of a template class. */
14262 if (declares_class_or_enum)
14264 if (cp_parser_declares_only_class_p (parser))
14266 decl = shadow_tag (decl_specifiers);
14268 decl = TYPE_NAME (decl);
14270 decl = error_mark_node;
14275 /* If it's not a template class, try for a template function. If
14276 the next token is a `;', then this declaration does not declare
14277 anything. But, if there were errors in the decl-specifiers, then
14278 the error might well have come from an attempted class-specifier.
14279 In that case, there's no need to warn about a missing declarator. */
14281 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
14282 || !value_member (error_mark_node, decl_specifiers)))
14283 decl = cp_parser_init_declarator (parser,
14287 /*function_definition_allowed_p=*/false,
14289 /*function_definition_p=*/NULL);
14290 /* Clear any current qualification; whatever comes next is the start
14291 of something new. */
14292 parser->scope = NULL_TREE;
14293 parser->qualifying_scope = NULL_TREE;
14294 parser->object_scope = NULL_TREE;
14295 /* Look for a trailing `;' after the declaration. */
14296 if (!cp_parser_require (parser, CPP_SEMICOLON, "expected `;'")
14297 && cp_parser_committed_to_tentative_parse (parser))
14298 cp_parser_skip_to_end_of_block_or_statement (parser);
14299 /* If it worked, set *FRIEND_P based on the DECL_SPECIFIERS. */
14300 if (cp_parser_parse_definitely (parser))
14303 *friend_p = cp_parser_friend_p (decl_specifiers);
14305 /* Otherwise, try a function-definition. */
14307 decl = cp_parser_function_definition (parser, friend_p);
14312 /* Parse a functional cast to TYPE. Returns an expression
14313 representing the cast. */
14316 cp_parser_functional_cast (parser, type)
14320 tree expression_list;
14322 /* Look for the opening `('. */
14323 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
14324 return error_mark_node;
14325 /* If the next token is not an `)', there are arguments to the
14327 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
14328 expression_list = cp_parser_expression_list (parser);
14330 expression_list = NULL_TREE;
14331 /* Look for the closing `)'. */
14332 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14334 return build_functional_cast (type, expression_list);
14337 /* MEMBER_FUNCTION is a member function, or a friend. If default
14338 arguments, or the body of the function have not yet been parsed,
14342 cp_parser_late_parsing_for_member (parser, member_function)
14344 tree member_function;
14346 cp_lexer *saved_lexer;
14348 /* If this member is a template, get the underlying
14350 if (DECL_FUNCTION_TEMPLATE_P (member_function))
14351 member_function = DECL_TEMPLATE_RESULT (member_function);
14353 /* There should not be any class definitions in progress at this
14354 point; the bodies of members are only parsed outside of all class
14356 my_friendly_assert (parser->num_classes_being_defined == 0, 20010816);
14357 /* While we're parsing the member functions we might encounter more
14358 classes. We want to handle them right away, but we don't want
14359 them getting mixed up with functions that are currently in the
14361 parser->unparsed_functions_queues
14362 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
14364 /* Make sure that any template parameters are in scope. */
14365 maybe_begin_member_template_processing (member_function);
14367 /* If there are default arguments that have not yet been processed,
14368 take care of them now. */
14369 cp_parser_late_parsing_default_args (parser, TREE_TYPE (member_function),
14370 DECL_FUNCTION_MEMBER_P (member_function)
14371 ? DECL_CONTEXT (member_function)
14374 /* If the body of the function has not yet been parsed, parse it
14376 if (DECL_PENDING_INLINE_P (member_function))
14378 tree function_scope;
14379 cp_token_cache *tokens;
14381 /* The function is no longer pending; we are processing it. */
14382 tokens = DECL_PENDING_INLINE_INFO (member_function);
14383 DECL_PENDING_INLINE_INFO (member_function) = NULL;
14384 DECL_PENDING_INLINE_P (member_function) = 0;
14385 /* If this was an inline function in a local class, enter the scope
14386 of the containing function. */
14387 function_scope = decl_function_context (member_function);
14388 if (function_scope)
14389 push_function_context_to (function_scope);
14391 /* Save away the current lexer. */
14392 saved_lexer = parser->lexer;
14393 /* Make a new lexer to feed us the tokens saved for this function. */
14394 parser->lexer = cp_lexer_new_from_tokens (tokens);
14395 parser->lexer->next = saved_lexer;
14397 /* Set the current source position to be the location of the first
14398 token in the saved inline body. */
14399 cp_lexer_set_source_position_from_token
14401 cp_lexer_peek_token (parser->lexer));
14403 /* Let the front end know that we going to be defining this
14405 start_function (NULL_TREE, member_function, NULL_TREE,
14406 SF_PRE_PARSED | SF_INCLASS_INLINE);
14408 /* Now, parse the body of the function. */
14409 cp_parser_function_definition_after_declarator (parser,
14410 /*inline_p=*/true);
14412 /* Leave the scope of the containing function. */
14413 if (function_scope)
14414 pop_function_context_from (function_scope);
14415 /* Restore the lexer. */
14416 parser->lexer = saved_lexer;
14419 /* Remove any template parameters from the symbol table. */
14420 maybe_end_member_template_processing ();
14422 /* Restore the queue. */
14423 parser->unparsed_functions_queues
14424 = TREE_CHAIN (parser->unparsed_functions_queues);
14427 /* TYPE is a FUNCTION_TYPE or METHOD_TYPE which contains a parameter
14428 with an unparsed DEFAULT_ARG. If non-NULL, SCOPE is the class in
14429 whose context name lookups in the default argument should occur.
14430 Parse the default args now. */
14433 cp_parser_late_parsing_default_args (cp_parser *parser, tree type, tree scope)
14435 cp_lexer *saved_lexer;
14436 cp_token_cache *tokens;
14437 bool saved_local_variables_forbidden_p;
14440 for (parameters = TYPE_ARG_TYPES (type);
14442 parameters = TREE_CHAIN (parameters))
14444 if (!TREE_PURPOSE (parameters)
14445 || TREE_CODE (TREE_PURPOSE (parameters)) != DEFAULT_ARG)
14448 /* Save away the current lexer. */
14449 saved_lexer = parser->lexer;
14450 /* Create a new one, using the tokens we have saved. */
14451 tokens = DEFARG_TOKENS (TREE_PURPOSE (parameters));
14452 parser->lexer = cp_lexer_new_from_tokens (tokens);
14454 /* Set the current source position to be the location of the
14455 first token in the default argument. */
14456 cp_lexer_set_source_position_from_token
14457 (parser->lexer, cp_lexer_peek_token (parser->lexer));
14459 /* Local variable names (and the `this' keyword) may not appear
14460 in a default argument. */
14461 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
14462 parser->local_variables_forbidden_p = true;
14463 /* Parse the assignment-expression. */
14465 push_nested_class (scope, 1);
14466 TREE_PURPOSE (parameters) = cp_parser_assignment_expression (parser);
14468 pop_nested_class ();
14470 /* Restore saved state. */
14471 parser->lexer = saved_lexer;
14472 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
14476 /* Parse the operand of `sizeof' (or a similar operator). Returns
14477 either a TYPE or an expression, depending on the form of the
14478 input. The KEYWORD indicates which kind of expression we have
14482 cp_parser_sizeof_operand (parser, keyword)
14486 static const char *format;
14487 tree expr = NULL_TREE;
14488 const char *saved_message;
14489 bool saved_constant_expression_p;
14491 /* Initialize FORMAT the first time we get here. */
14493 format = "types may not be defined in `%s' expressions";
14495 /* Types cannot be defined in a `sizeof' expression. Save away the
14497 saved_message = parser->type_definition_forbidden_message;
14498 /* And create the new one. */
14499 parser->type_definition_forbidden_message
14501 xmalloc (strlen (format)
14502 + strlen (IDENTIFIER_POINTER (ridpointers[keyword]))
14504 sprintf ((char *) parser->type_definition_forbidden_message,
14505 format, IDENTIFIER_POINTER (ridpointers[keyword]));
14507 /* The restrictions on constant-expressions do not apply inside
14508 sizeof expressions. */
14509 saved_constant_expression_p = parser->constant_expression_p;
14510 parser->constant_expression_p = false;
14512 /* Do not actually evaluate the expression. */
14514 /* If it's a `(', then we might be looking at the type-id
14516 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
14520 /* We can't be sure yet whether we're looking at a type-id or an
14522 cp_parser_parse_tentatively (parser);
14523 /* Consume the `('. */
14524 cp_lexer_consume_token (parser->lexer);
14525 /* Parse the type-id. */
14526 type = cp_parser_type_id (parser);
14527 /* Now, look for the trailing `)'. */
14528 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14529 /* If all went well, then we're done. */
14530 if (cp_parser_parse_definitely (parser))
14532 /* Build a list of decl-specifiers; right now, we have only
14533 a single type-specifier. */
14534 type = build_tree_list (NULL_TREE,
14537 /* Call grokdeclarator to figure out what type this is. */
14538 expr = grokdeclarator (NULL_TREE,
14542 /*attrlist=*/NULL);
14546 /* If the type-id production did not work out, then we must be
14547 looking at the unary-expression production. */
14549 expr = cp_parser_unary_expression (parser, /*address_p=*/false);
14550 /* Go back to evaluating expressions. */
14553 /* Free the message we created. */
14554 free ((char *) parser->type_definition_forbidden_message);
14555 /* And restore the old one. */
14556 parser->type_definition_forbidden_message = saved_message;
14557 parser->constant_expression_p = saved_constant_expression_p;
14562 /* If the current declaration has no declarator, return true. */
14565 cp_parser_declares_only_class_p (cp_parser *parser)
14567 /* If the next token is a `;' or a `,' then there is no
14569 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
14570 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
14573 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
14574 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
14577 cp_parser_friend_p (decl_specifiers)
14578 tree decl_specifiers;
14580 while (decl_specifiers)
14582 /* See if this decl-specifier is `friend'. */
14583 if (TREE_CODE (TREE_VALUE (decl_specifiers)) == IDENTIFIER_NODE
14584 && C_RID_CODE (TREE_VALUE (decl_specifiers)) == RID_FRIEND)
14587 /* Go on to the next decl-specifier. */
14588 decl_specifiers = TREE_CHAIN (decl_specifiers);
14594 /* If the next token is of the indicated TYPE, consume it. Otherwise,
14595 issue an error message indicating that TOKEN_DESC was expected.
14597 Returns the token consumed, if the token had the appropriate type.
14598 Otherwise, returns NULL. */
14601 cp_parser_require (parser, type, token_desc)
14603 enum cpp_ttype type;
14604 const char *token_desc;
14606 if (cp_lexer_next_token_is (parser->lexer, type))
14607 return cp_lexer_consume_token (parser->lexer);
14610 /* Output the MESSAGE -- unless we're parsing tentatively. */
14611 if (!cp_parser_simulate_error (parser))
14612 error ("expected %s", token_desc);
14617 /* Like cp_parser_require, except that tokens will be skipped until
14618 the desired token is found. An error message is still produced if
14619 the next token is not as expected. */
14622 cp_parser_skip_until_found (parser, type, token_desc)
14624 enum cpp_ttype type;
14625 const char *token_desc;
14628 unsigned nesting_depth = 0;
14630 if (cp_parser_require (parser, type, token_desc))
14633 /* Skip tokens until the desired token is found. */
14636 /* Peek at the next token. */
14637 token = cp_lexer_peek_token (parser->lexer);
14638 /* If we've reached the token we want, consume it and
14640 if (token->type == type && !nesting_depth)
14642 cp_lexer_consume_token (parser->lexer);
14645 /* If we've run out of tokens, stop. */
14646 if (token->type == CPP_EOF)
14648 if (token->type == CPP_OPEN_BRACE
14649 || token->type == CPP_OPEN_PAREN
14650 || token->type == CPP_OPEN_SQUARE)
14652 else if (token->type == CPP_CLOSE_BRACE
14653 || token->type == CPP_CLOSE_PAREN
14654 || token->type == CPP_CLOSE_SQUARE)
14656 if (nesting_depth-- == 0)
14659 /* Consume this token. */
14660 cp_lexer_consume_token (parser->lexer);
14664 /* If the next token is the indicated keyword, consume it. Otherwise,
14665 issue an error message indicating that TOKEN_DESC was expected.
14667 Returns the token consumed, if the token had the appropriate type.
14668 Otherwise, returns NULL. */
14671 cp_parser_require_keyword (parser, keyword, token_desc)
14674 const char *token_desc;
14676 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
14678 if (token && token->keyword != keyword)
14680 dyn_string_t error_msg;
14682 /* Format the error message. */
14683 error_msg = dyn_string_new (0);
14684 dyn_string_append_cstr (error_msg, "expected ");
14685 dyn_string_append_cstr (error_msg, token_desc);
14686 cp_parser_error (parser, error_msg->s);
14687 dyn_string_delete (error_msg);
14694 /* Returns TRUE iff TOKEN is a token that can begin the body of a
14695 function-definition. */
14698 cp_parser_token_starts_function_definition_p (token)
14701 return (/* An ordinary function-body begins with an `{'. */
14702 token->type == CPP_OPEN_BRACE
14703 /* A ctor-initializer begins with a `:'. */
14704 || token->type == CPP_COLON
14705 /* A function-try-block begins with `try'. */
14706 || token->keyword == RID_TRY
14707 /* The named return value extension begins with `return'. */
14708 || token->keyword == RID_RETURN);
14711 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
14715 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
14719 token = cp_lexer_peek_token (parser->lexer);
14720 return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
14723 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
14724 or none_type otherwise. */
14726 static enum tag_types
14727 cp_parser_token_is_class_key (token)
14730 switch (token->keyword)
14735 return record_type;
14744 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
14747 cp_parser_check_class_key (enum tag_types class_key, tree type)
14749 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
14750 pedwarn ("`%s' tag used in naming `%#T'",
14751 class_key == union_type ? "union"
14752 : class_key == record_type ? "struct" : "class",
14756 /* Look for the `template' keyword, as a syntactic disambiguator.
14757 Return TRUE iff it is present, in which case it will be
14761 cp_parser_optional_template_keyword (cp_parser *parser)
14763 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
14765 /* The `template' keyword can only be used within templates;
14766 outside templates the parser can always figure out what is a
14767 template and what is not. */
14768 if (!processing_template_decl)
14770 error ("`template' (as a disambiguator) is only allowed "
14771 "within templates");
14772 /* If this part of the token stream is rescanned, the same
14773 error message would be generated. So, we purge the token
14774 from the stream. */
14775 cp_lexer_purge_token (parser->lexer);
14780 /* Consume the `template' keyword. */
14781 cp_lexer_consume_token (parser->lexer);
14789 /* Add tokens to CACHE until an non-nested END token appears. */
14792 cp_parser_cache_group (cp_parser *parser,
14793 cp_token_cache *cache,
14794 enum cpp_ttype end,
14801 /* Abort a parenthesized expression if we encounter a brace. */
14802 if ((end == CPP_CLOSE_PAREN || depth == 0)
14803 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
14805 /* Consume the next token. */
14806 token = cp_lexer_consume_token (parser->lexer);
14807 /* If we've reached the end of the file, stop. */
14808 if (token->type == CPP_EOF)
14810 /* Add this token to the tokens we are saving. */
14811 cp_token_cache_push_token (cache, token);
14812 /* See if it starts a new group. */
14813 if (token->type == CPP_OPEN_BRACE)
14815 cp_parser_cache_group (parser, cache, CPP_CLOSE_BRACE, depth + 1);
14819 else if (token->type == CPP_OPEN_PAREN)
14820 cp_parser_cache_group (parser, cache, CPP_CLOSE_PAREN, depth + 1);
14821 else if (token->type == end)
14826 /* Begin parsing tentatively. We always save tokens while parsing
14827 tentatively so that if the tentative parsing fails we can restore the
14831 cp_parser_parse_tentatively (parser)
14834 /* Enter a new parsing context. */
14835 parser->context = cp_parser_context_new (parser->context);
14836 /* Begin saving tokens. */
14837 cp_lexer_save_tokens (parser->lexer);
14838 /* In order to avoid repetitive access control error messages,
14839 access checks are queued up until we are no longer parsing
14841 cp_parser_start_deferring_access_checks (parser);
14844 /* Commit to the currently active tentative parse. */
14847 cp_parser_commit_to_tentative_parse (parser)
14850 cp_parser_context *context;
14853 /* Mark all of the levels as committed. */
14854 lexer = parser->lexer;
14855 for (context = parser->context; context->next; context = context->next)
14857 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
14859 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
14860 while (!cp_lexer_saving_tokens (lexer))
14861 lexer = lexer->next;
14862 cp_lexer_commit_tokens (lexer);
14866 /* Abort the currently active tentative parse. All consumed tokens
14867 will be rolled back, and no diagnostics will be issued. */
14870 cp_parser_abort_tentative_parse (parser)
14873 cp_parser_simulate_error (parser);
14874 /* Now, pretend that we want to see if the construct was
14875 successfully parsed. */
14876 cp_parser_parse_definitely (parser);
14879 /* Stop parsing tentatively. If a parse error has ocurred, restore the
14880 token stream. Otherwise, commit to the tokens we have consumed.
14881 Returns true if no error occurred; false otherwise. */
14884 cp_parser_parse_definitely (parser)
14887 bool error_occurred;
14888 cp_parser_context *context;
14890 /* Remember whether or not an error ocurred, since we are about to
14891 destroy that information. */
14892 error_occurred = cp_parser_error_occurred (parser);
14893 /* Remove the topmost context from the stack. */
14894 context = parser->context;
14895 parser->context = context->next;
14896 /* If no parse errors occurred, commit to the tentative parse. */
14897 if (!error_occurred)
14899 /* Commit to the tokens read tentatively, unless that was
14901 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
14902 cp_lexer_commit_tokens (parser->lexer);
14903 if (!parser->context->deferring_access_checks_p)
14904 /* If in the parent context we are not deferring checks, then
14905 these perform these checks now. */
14906 (cp_parser_perform_deferred_access_checks
14907 (context->deferred_access_checks));
14909 /* Any lookups that were deferred during the tentative parse are
14911 parser->context->deferred_access_checks
14912 = chainon (parser->context->deferred_access_checks,
14913 context->deferred_access_checks);
14915 /* Otherwise, if errors occurred, roll back our state so that things
14916 are just as they were before we began the tentative parse. */
14918 cp_lexer_rollback_tokens (parser->lexer);
14919 /* Add the context to the front of the free list. */
14920 context->next = cp_parser_context_free_list;
14921 cp_parser_context_free_list = context;
14923 return !error_occurred;
14926 /* Returns true if we are parsing tentatively -- but have decided that
14927 we will stick with this tentative parse, even if errors occur. */
14930 cp_parser_committed_to_tentative_parse (parser)
14933 return (cp_parser_parsing_tentatively (parser)
14934 && parser->context->status == CP_PARSER_STATUS_KIND_COMMITTED);
14937 /* Returns non-zero iff an error has occurred during the most recent
14938 tentative parse. */
14941 cp_parser_error_occurred (parser)
14944 return (cp_parser_parsing_tentatively (parser)
14945 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
14948 /* Returns non-zero if GNU extensions are allowed. */
14951 cp_parser_allow_gnu_extensions_p (parser)
14954 return parser->allow_gnu_extensions_p;
14961 static GTY (()) cp_parser *the_parser;
14963 /* External interface. */
14965 /* Parse the entire translation unit. */
14970 bool error_occurred;
14972 the_parser = cp_parser_new ();
14973 error_occurred = cp_parser_translation_unit (the_parser);
14976 return error_occurred;
14979 /* Clean up after parsing the entire translation unit. */
14982 free_parser_stacks ()
14984 /* Nothing to do. */
14987 /* This variable must be provided by every front end. */
14991 #include "gt-cp-parser.h"