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;
1348 /* The type of a function that parses some kind of expression */
1349 typedef tree (*cp_parser_expression_fn) PARAMS ((cp_parser *));
1353 /* Constructors and destructors. */
1355 static cp_parser *cp_parser_new
1358 /* Routines to parse various constructs.
1360 Those that return `tree' will return the error_mark_node (rather
1361 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1362 Sometimes, they will return an ordinary node if error-recovery was
1363 attempted, even though a parse error occurrred. So, to check
1364 whether or not a parse error occurred, you should always use
1365 cp_parser_error_occurred. If the construct is optional (indicated
1366 either by an `_opt' in the name of the function that does the
1367 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1368 the construct is not present. */
1370 /* Lexical conventions [gram.lex] */
1372 static tree cp_parser_identifier
1373 PARAMS ((cp_parser *));
1375 /* Basic concepts [gram.basic] */
1377 static bool cp_parser_translation_unit
1378 PARAMS ((cp_parser *));
1380 /* Expressions [gram.expr] */
1382 static tree cp_parser_primary_expression
1383 (cp_parser *, cp_parser_id_kind *, tree *);
1384 static tree cp_parser_id_expression
1385 PARAMS ((cp_parser *, bool, bool, bool *));
1386 static tree cp_parser_unqualified_id
1387 PARAMS ((cp_parser *, bool, bool));
1388 static tree cp_parser_nested_name_specifier_opt
1389 (cp_parser *, bool, bool, bool);
1390 static tree cp_parser_nested_name_specifier
1391 (cp_parser *, bool, bool, bool);
1392 static tree cp_parser_class_or_namespace_name
1393 (cp_parser *, bool, bool, bool, bool);
1394 static tree cp_parser_postfix_expression
1395 (cp_parser *, bool);
1396 static tree cp_parser_expression_list
1397 PARAMS ((cp_parser *));
1398 static void cp_parser_pseudo_destructor_name
1399 PARAMS ((cp_parser *, tree *, tree *));
1400 static tree cp_parser_unary_expression
1401 (cp_parser *, bool);
1402 static enum tree_code cp_parser_unary_operator
1403 PARAMS ((cp_token *));
1404 static tree cp_parser_new_expression
1405 PARAMS ((cp_parser *));
1406 static tree cp_parser_new_placement
1407 PARAMS ((cp_parser *));
1408 static tree cp_parser_new_type_id
1409 PARAMS ((cp_parser *));
1410 static tree cp_parser_new_declarator_opt
1411 PARAMS ((cp_parser *));
1412 static tree cp_parser_direct_new_declarator
1413 PARAMS ((cp_parser *));
1414 static tree cp_parser_new_initializer
1415 PARAMS ((cp_parser *));
1416 static tree cp_parser_delete_expression
1417 PARAMS ((cp_parser *));
1418 static tree cp_parser_cast_expression
1419 (cp_parser *, bool);
1420 static tree cp_parser_pm_expression
1421 PARAMS ((cp_parser *));
1422 static tree cp_parser_multiplicative_expression
1423 PARAMS ((cp_parser *));
1424 static tree cp_parser_additive_expression
1425 PARAMS ((cp_parser *));
1426 static tree cp_parser_shift_expression
1427 PARAMS ((cp_parser *));
1428 static tree cp_parser_relational_expression
1429 PARAMS ((cp_parser *));
1430 static tree cp_parser_equality_expression
1431 PARAMS ((cp_parser *));
1432 static tree cp_parser_and_expression
1433 PARAMS ((cp_parser *));
1434 static tree cp_parser_exclusive_or_expression
1435 PARAMS ((cp_parser *));
1436 static tree cp_parser_inclusive_or_expression
1437 PARAMS ((cp_parser *));
1438 static tree cp_parser_logical_and_expression
1439 PARAMS ((cp_parser *));
1440 static tree cp_parser_logical_or_expression
1441 PARAMS ((cp_parser *));
1442 static tree cp_parser_conditional_expression
1443 PARAMS ((cp_parser *));
1444 static tree cp_parser_question_colon_clause
1445 PARAMS ((cp_parser *, tree));
1446 static tree cp_parser_assignment_expression
1447 PARAMS ((cp_parser *));
1448 static enum tree_code cp_parser_assignment_operator_opt
1449 PARAMS ((cp_parser *));
1450 static tree cp_parser_expression
1451 PARAMS ((cp_parser *));
1452 static tree cp_parser_constant_expression
1453 PARAMS ((cp_parser *));
1455 /* Statements [gram.stmt.stmt] */
1457 static void cp_parser_statement
1458 PARAMS ((cp_parser *));
1459 static tree cp_parser_labeled_statement
1460 PARAMS ((cp_parser *));
1461 static tree cp_parser_expression_statement
1462 PARAMS ((cp_parser *));
1463 static tree cp_parser_compound_statement
1465 static void cp_parser_statement_seq_opt
1466 PARAMS ((cp_parser *));
1467 static tree cp_parser_selection_statement
1468 PARAMS ((cp_parser *));
1469 static tree cp_parser_condition
1470 PARAMS ((cp_parser *));
1471 static tree cp_parser_iteration_statement
1472 PARAMS ((cp_parser *));
1473 static void cp_parser_for_init_statement
1474 PARAMS ((cp_parser *));
1475 static tree cp_parser_jump_statement
1476 PARAMS ((cp_parser *));
1477 static void cp_parser_declaration_statement
1478 PARAMS ((cp_parser *));
1480 static tree cp_parser_implicitly_scoped_statement
1481 PARAMS ((cp_parser *));
1482 static void cp_parser_already_scoped_statement
1483 PARAMS ((cp_parser *));
1485 /* Declarations [gram.dcl.dcl] */
1487 static void cp_parser_declaration_seq_opt
1488 PARAMS ((cp_parser *));
1489 static void cp_parser_declaration
1490 PARAMS ((cp_parser *));
1491 static void cp_parser_block_declaration
1492 PARAMS ((cp_parser *, bool));
1493 static void cp_parser_simple_declaration
1494 PARAMS ((cp_parser *, bool));
1495 static tree cp_parser_decl_specifier_seq
1496 PARAMS ((cp_parser *, cp_parser_flags, tree *, bool *));
1497 static tree cp_parser_storage_class_specifier_opt
1498 PARAMS ((cp_parser *));
1499 static tree cp_parser_function_specifier_opt
1500 PARAMS ((cp_parser *));
1501 static tree cp_parser_type_specifier
1502 (cp_parser *, cp_parser_flags, bool, bool, bool *, bool *);
1503 static tree cp_parser_simple_type_specifier
1504 PARAMS ((cp_parser *, cp_parser_flags));
1505 static tree cp_parser_type_name
1506 PARAMS ((cp_parser *));
1507 static tree cp_parser_elaborated_type_specifier
1508 PARAMS ((cp_parser *, bool, bool));
1509 static tree cp_parser_enum_specifier
1510 PARAMS ((cp_parser *));
1511 static void cp_parser_enumerator_list
1512 PARAMS ((cp_parser *, tree));
1513 static void cp_parser_enumerator_definition
1514 PARAMS ((cp_parser *, tree));
1515 static tree cp_parser_namespace_name
1516 PARAMS ((cp_parser *));
1517 static void cp_parser_namespace_definition
1518 PARAMS ((cp_parser *));
1519 static void cp_parser_namespace_body
1520 PARAMS ((cp_parser *));
1521 static tree cp_parser_qualified_namespace_specifier
1522 PARAMS ((cp_parser *));
1523 static void cp_parser_namespace_alias_definition
1524 PARAMS ((cp_parser *));
1525 static void cp_parser_using_declaration
1526 PARAMS ((cp_parser *));
1527 static void cp_parser_using_directive
1528 PARAMS ((cp_parser *));
1529 static void cp_parser_asm_definition
1530 PARAMS ((cp_parser *));
1531 static void cp_parser_linkage_specification
1532 PARAMS ((cp_parser *));
1534 /* Declarators [gram.dcl.decl] */
1536 static tree cp_parser_init_declarator
1537 PARAMS ((cp_parser *, tree, tree, tree, bool, bool, bool *));
1538 static tree cp_parser_declarator
1539 PARAMS ((cp_parser *, bool, bool *));
1540 static tree cp_parser_direct_declarator
1541 PARAMS ((cp_parser *, bool, bool *));
1542 static enum tree_code cp_parser_ptr_operator
1543 PARAMS ((cp_parser *, tree *, tree *));
1544 static tree cp_parser_cv_qualifier_seq_opt
1545 PARAMS ((cp_parser *));
1546 static tree cp_parser_cv_qualifier_opt
1547 PARAMS ((cp_parser *));
1548 static tree cp_parser_declarator_id
1549 PARAMS ((cp_parser *));
1550 static tree cp_parser_type_id
1551 PARAMS ((cp_parser *));
1552 static tree cp_parser_type_specifier_seq
1553 PARAMS ((cp_parser *));
1554 static tree cp_parser_parameter_declaration_clause
1555 PARAMS ((cp_parser *));
1556 static tree cp_parser_parameter_declaration_list
1557 PARAMS ((cp_parser *));
1558 static tree cp_parser_parameter_declaration
1559 PARAMS ((cp_parser *, bool));
1560 static tree cp_parser_function_definition
1561 PARAMS ((cp_parser *, bool *));
1562 static void cp_parser_function_body
1564 static tree cp_parser_initializer
1565 PARAMS ((cp_parser *, bool *));
1566 static tree cp_parser_initializer_clause
1567 PARAMS ((cp_parser *));
1568 static tree cp_parser_initializer_list
1569 PARAMS ((cp_parser *));
1571 static bool cp_parser_ctor_initializer_opt_and_function_body
1574 /* Classes [gram.class] */
1576 static tree cp_parser_class_name
1577 (cp_parser *, bool, bool, bool, bool, bool, bool);
1578 static tree cp_parser_class_specifier
1579 PARAMS ((cp_parser *));
1580 static tree cp_parser_class_head
1581 PARAMS ((cp_parser *, bool *, bool *, tree *));
1582 static enum tag_types cp_parser_class_key
1583 PARAMS ((cp_parser *));
1584 static void cp_parser_member_specification_opt
1585 PARAMS ((cp_parser *));
1586 static void cp_parser_member_declaration
1587 PARAMS ((cp_parser *));
1588 static tree cp_parser_pure_specifier
1589 PARAMS ((cp_parser *));
1590 static tree cp_parser_constant_initializer
1591 PARAMS ((cp_parser *));
1593 /* Derived classes [gram.class.derived] */
1595 static tree cp_parser_base_clause
1596 PARAMS ((cp_parser *));
1597 static tree cp_parser_base_specifier
1598 PARAMS ((cp_parser *));
1600 /* Special member functions [gram.special] */
1602 static tree cp_parser_conversion_function_id
1603 PARAMS ((cp_parser *));
1604 static tree cp_parser_conversion_type_id
1605 PARAMS ((cp_parser *));
1606 static tree cp_parser_conversion_declarator_opt
1607 PARAMS ((cp_parser *));
1608 static bool cp_parser_ctor_initializer_opt
1609 PARAMS ((cp_parser *));
1610 static void cp_parser_mem_initializer_list
1611 PARAMS ((cp_parser *));
1612 static tree cp_parser_mem_initializer
1613 PARAMS ((cp_parser *));
1614 static tree cp_parser_mem_initializer_id
1615 PARAMS ((cp_parser *));
1617 /* Overloading [gram.over] */
1619 static tree cp_parser_operator_function_id
1620 PARAMS ((cp_parser *));
1621 static tree cp_parser_operator
1622 PARAMS ((cp_parser *));
1624 /* Templates [gram.temp] */
1626 static void cp_parser_template_declaration
1627 PARAMS ((cp_parser *, bool));
1628 static tree cp_parser_template_parameter_list
1629 PARAMS ((cp_parser *));
1630 static tree cp_parser_template_parameter
1631 PARAMS ((cp_parser *));
1632 static tree cp_parser_type_parameter
1633 PARAMS ((cp_parser *));
1634 static tree cp_parser_template_id
1635 PARAMS ((cp_parser *, bool, bool));
1636 static tree cp_parser_template_name
1637 PARAMS ((cp_parser *, bool, bool));
1638 static tree cp_parser_template_argument_list
1639 PARAMS ((cp_parser *));
1640 static tree cp_parser_template_argument
1641 PARAMS ((cp_parser *));
1642 static void cp_parser_explicit_instantiation
1643 PARAMS ((cp_parser *));
1644 static void cp_parser_explicit_specialization
1645 PARAMS ((cp_parser *));
1647 /* Exception handling [gram.exception] */
1649 static tree cp_parser_try_block
1650 PARAMS ((cp_parser *));
1651 static bool cp_parser_function_try_block
1652 PARAMS ((cp_parser *));
1653 static void cp_parser_handler_seq
1654 PARAMS ((cp_parser *));
1655 static void cp_parser_handler
1656 PARAMS ((cp_parser *));
1657 static tree cp_parser_exception_declaration
1658 PARAMS ((cp_parser *));
1659 static tree cp_parser_throw_expression
1660 PARAMS ((cp_parser *));
1661 static tree cp_parser_exception_specification_opt
1662 PARAMS ((cp_parser *));
1663 static tree cp_parser_type_id_list
1664 PARAMS ((cp_parser *));
1666 /* GNU Extensions */
1668 static tree cp_parser_asm_specification_opt
1669 PARAMS ((cp_parser *));
1670 static tree cp_parser_asm_operand_list
1671 PARAMS ((cp_parser *));
1672 static tree cp_parser_asm_clobber_list
1673 PARAMS ((cp_parser *));
1674 static tree cp_parser_attributes_opt
1675 PARAMS ((cp_parser *));
1676 static tree cp_parser_attribute_list
1677 PARAMS ((cp_parser *));
1678 static bool cp_parser_extension_opt
1679 PARAMS ((cp_parser *, int *));
1680 static void cp_parser_label_declaration
1681 PARAMS ((cp_parser *));
1683 /* Utility Routines */
1685 static tree cp_parser_lookup_name
1686 PARAMS ((cp_parser *, tree, bool, bool, bool, bool));
1687 static tree cp_parser_lookup_name_simple
1688 PARAMS ((cp_parser *, tree));
1689 static tree cp_parser_resolve_typename_type
1690 PARAMS ((cp_parser *, tree));
1691 static tree cp_parser_maybe_treat_template_as_class
1693 static bool cp_parser_check_declarator_template_parameters
1694 PARAMS ((cp_parser *, tree));
1695 static bool cp_parser_check_template_parameters
1696 PARAMS ((cp_parser *, unsigned));
1697 static tree cp_parser_binary_expression
1698 PARAMS ((cp_parser *,
1699 cp_parser_token_tree_map,
1700 cp_parser_expression_fn));
1701 static tree cp_parser_global_scope_opt
1702 PARAMS ((cp_parser *, bool));
1703 static bool cp_parser_constructor_declarator_p
1704 (cp_parser *, bool);
1705 static tree cp_parser_function_definition_from_specifiers_and_declarator
1706 PARAMS ((cp_parser *, tree, tree, tree, tree));
1707 static tree cp_parser_function_definition_after_declarator
1708 PARAMS ((cp_parser *, bool));
1709 static void cp_parser_template_declaration_after_export
1710 PARAMS ((cp_parser *, bool));
1711 static tree cp_parser_single_declaration
1712 PARAMS ((cp_parser *, bool, bool *));
1713 static tree cp_parser_functional_cast
1714 PARAMS ((cp_parser *, tree));
1715 static void cp_parser_late_parsing_for_member
1716 PARAMS ((cp_parser *, tree));
1717 static void cp_parser_late_parsing_default_args
1718 (cp_parser *, tree, tree);
1719 static tree cp_parser_sizeof_operand
1720 PARAMS ((cp_parser *, enum rid));
1721 static bool cp_parser_declares_only_class_p
1722 PARAMS ((cp_parser *));
1723 static bool cp_parser_friend_p
1725 static cp_token *cp_parser_require
1726 PARAMS ((cp_parser *, enum cpp_ttype, const char *));
1727 static cp_token *cp_parser_require_keyword
1728 PARAMS ((cp_parser *, enum rid, const char *));
1729 static bool cp_parser_token_starts_function_definition_p
1730 PARAMS ((cp_token *));
1731 static bool cp_parser_next_token_starts_class_definition_p
1733 static enum tag_types cp_parser_token_is_class_key
1734 PARAMS ((cp_token *));
1735 static void cp_parser_check_class_key
1736 (enum tag_types, tree type);
1737 static bool cp_parser_optional_template_keyword
1739 static void cp_parser_cache_group
1740 (cp_parser *, cp_token_cache *, enum cpp_ttype, unsigned);
1741 static void cp_parser_parse_tentatively
1742 PARAMS ((cp_parser *));
1743 static void cp_parser_commit_to_tentative_parse
1744 PARAMS ((cp_parser *));
1745 static void cp_parser_abort_tentative_parse
1746 PARAMS ((cp_parser *));
1747 static bool cp_parser_parse_definitely
1748 PARAMS ((cp_parser *));
1749 static inline bool cp_parser_parsing_tentatively
1750 PARAMS ((cp_parser *));
1751 static bool cp_parser_committed_to_tentative_parse
1752 PARAMS ((cp_parser *));
1753 static void cp_parser_error
1754 PARAMS ((cp_parser *, const char *));
1755 static bool cp_parser_simulate_error
1756 PARAMS ((cp_parser *));
1757 static void cp_parser_check_type_definition
1758 PARAMS ((cp_parser *));
1759 static bool cp_parser_skip_to_closing_parenthesis
1760 PARAMS ((cp_parser *));
1761 static bool cp_parser_skip_to_closing_parenthesis_or_comma
1763 static void cp_parser_skip_to_end_of_statement
1764 PARAMS ((cp_parser *));
1765 static void cp_parser_skip_to_end_of_block_or_statement
1766 PARAMS ((cp_parser *));
1767 static void cp_parser_skip_to_closing_brace
1769 static void cp_parser_skip_until_found
1770 PARAMS ((cp_parser *, enum cpp_ttype, const char *));
1771 static bool cp_parser_error_occurred
1772 PARAMS ((cp_parser *));
1773 static bool cp_parser_allow_gnu_extensions_p
1774 PARAMS ((cp_parser *));
1775 static bool cp_parser_is_string_literal
1776 PARAMS ((cp_token *));
1777 static bool cp_parser_is_keyword
1778 PARAMS ((cp_token *, enum rid));
1779 static bool cp_parser_dependent_type_p
1781 static bool cp_parser_value_dependent_expression_p
1783 static bool cp_parser_type_dependent_expression_p
1785 static bool cp_parser_dependent_template_arg_p
1787 static bool cp_parser_dependent_template_id_p
1789 static bool cp_parser_dependent_template_p
1791 static void cp_parser_defer_access_check
1792 (cp_parser *, tree, tree);
1793 static void cp_parser_start_deferring_access_checks
1795 static tree cp_parser_stop_deferring_access_checks
1796 PARAMS ((cp_parser *));
1797 static void cp_parser_perform_deferred_access_checks
1799 static tree cp_parser_scope_through_which_access_occurs
1802 /* Returns non-zero if we are parsing tentatively. */
1805 cp_parser_parsing_tentatively (parser)
1808 return parser->context->next != NULL;
1811 /* Returns non-zero if TOKEN is a string literal. */
1814 cp_parser_is_string_literal (token)
1817 return (token->type == CPP_STRING || token->type == CPP_WSTRING);
1820 /* Returns non-zero if TOKEN is the indicated KEYWORD. */
1823 cp_parser_is_keyword (token, keyword)
1827 return token->keyword == keyword;
1830 /* Returns TRUE if TYPE is dependent, in the sense of
1834 cp_parser_dependent_type_p (type)
1839 if (!processing_template_decl)
1842 /* If the type is NULL, we have not computed a type for the entity
1843 in question; in that case, the type is dependent. */
1847 /* Erroneous types can be considered non-dependent. */
1848 if (type == error_mark_node)
1853 A type is dependent if it is:
1855 -- a template parameter. */
1856 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
1858 /* -- a qualified-id with a nested-name-specifier which contains a
1859 class-name that names a dependent type or whose unqualified-id
1860 names a dependent type. */
1861 if (TREE_CODE (type) == TYPENAME_TYPE)
1863 /* -- a cv-qualified type where the cv-unqualified type is
1865 type = TYPE_MAIN_VARIANT (type);
1866 /* -- a compound type constructed from any dependent type. */
1867 if (TYPE_PTRMEM_P (type) || TYPE_PTRMEMFUNC_P (type))
1868 return (cp_parser_dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type))
1869 || cp_parser_dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
1871 else if (TREE_CODE (type) == POINTER_TYPE
1872 || TREE_CODE (type) == REFERENCE_TYPE)
1873 return cp_parser_dependent_type_p (TREE_TYPE (type));
1874 else if (TREE_CODE (type) == FUNCTION_TYPE
1875 || TREE_CODE (type) == METHOD_TYPE)
1879 if (cp_parser_dependent_type_p (TREE_TYPE (type)))
1881 for (arg_type = TYPE_ARG_TYPES (type);
1883 arg_type = TREE_CHAIN (arg_type))
1884 if (cp_parser_dependent_type_p (TREE_VALUE (arg_type)))
1888 /* -- an array type constructed from any dependent type or whose
1889 size is specified by a constant expression that is
1891 if (TREE_CODE (type) == ARRAY_TYPE)
1893 if (TYPE_DOMAIN (type)
1894 && ((cp_parser_value_dependent_expression_p
1895 (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
1896 || (cp_parser_type_dependent_expression_p
1897 (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))))
1899 return cp_parser_dependent_type_p (TREE_TYPE (type));
1901 /* -- a template-id in which either the template name is a template
1902 parameter or any of the template arguments is a dependent type or
1903 an expression that is type-dependent or value-dependent.
1905 This language seems somewhat confused; for example, it does not
1906 discuss template template arguments. Therefore, we use the
1907 definition for dependent template arguments in [temp.dep.temp]. */
1908 if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INFO (type)
1909 && (cp_parser_dependent_template_id_p
1910 (CLASSTYPE_TI_TEMPLATE (type),
1911 CLASSTYPE_TI_ARGS (type))))
1913 else if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
1915 /* All TYPEOF_TYPEs are dependent; if the argument of the `typeof'
1916 expression is not type-dependent, then it should already been
1918 if (TREE_CODE (type) == TYPEOF_TYPE)
1920 /* The standard does not specifically mention types that are local
1921 to template functions or local classes, but they should be
1922 considered dependent too. For example:
1924 template <int I> void f() {
1929 The size of `E' cannot be known until the value of `I' has been
1930 determined. Therefore, `E' must be considered dependent. */
1931 scope = TYPE_CONTEXT (type);
1932 if (scope && TYPE_P (scope))
1933 return cp_parser_dependent_type_p (scope);
1934 else if (scope && TREE_CODE (scope) == FUNCTION_DECL)
1935 return cp_parser_type_dependent_expression_p (scope);
1937 /* Other types are non-dependent. */
1941 /* Returns TRUE if the EXPRESSION is value-dependent. */
1944 cp_parser_value_dependent_expression_p (tree expression)
1946 if (!processing_template_decl)
1949 /* A name declared with a dependent type. */
1950 if (DECL_P (expression)
1951 && cp_parser_dependent_type_p (TREE_TYPE (expression)))
1953 /* A non-type template parameter. */
1954 if ((TREE_CODE (expression) == CONST_DECL
1955 && DECL_TEMPLATE_PARM_P (expression))
1956 || TREE_CODE (expression) == TEMPLATE_PARM_INDEX)
1958 /* A constant with integral or enumeration type and is initialized
1959 with an expression that is value-dependent. */
1960 if (TREE_CODE (expression) == VAR_DECL
1961 && DECL_INITIAL (expression)
1962 && (CP_INTEGRAL_TYPE_P (TREE_TYPE (expression))
1963 || TREE_CODE (TREE_TYPE (expression)) == ENUMERAL_TYPE)
1964 && cp_parser_value_dependent_expression_p (DECL_INITIAL (expression)))
1966 /* These expressions are value-dependent if the type to which the
1967 cast occurs is dependent. */
1968 if ((TREE_CODE (expression) == DYNAMIC_CAST_EXPR
1969 || TREE_CODE (expression) == STATIC_CAST_EXPR
1970 || TREE_CODE (expression) == CONST_CAST_EXPR
1971 || TREE_CODE (expression) == REINTERPRET_CAST_EXPR
1972 || TREE_CODE (expression) == CAST_EXPR)
1973 && cp_parser_dependent_type_p (TREE_TYPE (expression)))
1975 /* A `sizeof' expression where the sizeof operand is a type is
1976 value-dependent if the type is dependent. If the type was not
1977 dependent, we would no longer have a SIZEOF_EXPR, so any
1978 SIZEOF_EXPR is dependent. */
1979 if (TREE_CODE (expression) == SIZEOF_EXPR)
1981 /* A constant expression is value-dependent if any subexpression is
1983 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (expression))))
1985 switch (TREE_CODE_CLASS (TREE_CODE (expression)))
1988 return (cp_parser_value_dependent_expression_p
1989 (TREE_OPERAND (expression, 0)));
1992 return ((cp_parser_value_dependent_expression_p
1993 (TREE_OPERAND (expression, 0)))
1994 || (cp_parser_value_dependent_expression_p
1995 (TREE_OPERAND (expression, 1))));
2000 i < TREE_CODE_LENGTH (TREE_CODE (expression));
2002 if (cp_parser_value_dependent_expression_p
2003 (TREE_OPERAND (expression, i)))
2010 /* The expression is not value-dependent. */
2014 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
2018 cp_parser_type_dependent_expression_p (expression)
2021 if (!processing_template_decl)
2024 /* Some expression forms are never type-dependent. */
2025 if (TREE_CODE (expression) == PSEUDO_DTOR_EXPR
2026 || TREE_CODE (expression) == SIZEOF_EXPR
2027 || TREE_CODE (expression) == ALIGNOF_EXPR
2028 || TREE_CODE (expression) == TYPEID_EXPR
2029 || TREE_CODE (expression) == DELETE_EXPR
2030 || TREE_CODE (expression) == VEC_DELETE_EXPR
2031 || TREE_CODE (expression) == THROW_EXPR)
2034 /* The types of these expressions depends only on the type to which
2036 if (TREE_CODE (expression) == DYNAMIC_CAST_EXPR
2037 || TREE_CODE (expression) == STATIC_CAST_EXPR
2038 || TREE_CODE (expression) == CONST_CAST_EXPR
2039 || TREE_CODE (expression) == REINTERPRET_CAST_EXPR
2040 || TREE_CODE (expression) == CAST_EXPR)
2041 return cp_parser_dependent_type_p (TREE_TYPE (expression));
2042 /* The types of these expressions depends only on the type created
2043 by the expression. */
2044 else if (TREE_CODE (expression) == NEW_EXPR
2045 || TREE_CODE (expression) == VEC_NEW_EXPR)
2046 return cp_parser_dependent_type_p (TREE_OPERAND (expression, 1));
2048 if (TREE_CODE (expression) == FUNCTION_DECL
2049 && DECL_LANG_SPECIFIC (expression)
2050 && DECL_TEMPLATE_INFO (expression)
2051 && (cp_parser_dependent_template_id_p
2052 (DECL_TI_TEMPLATE (expression),
2053 INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression)))))
2056 return (cp_parser_dependent_type_p (TREE_TYPE (expression)));
2059 /* Returns TRUE if the ARG (a template argument) is dependent. */
2062 cp_parser_dependent_template_arg_p (tree arg)
2064 if (!processing_template_decl)
2067 if (TREE_CODE (arg) == TEMPLATE_DECL
2068 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
2069 return cp_parser_dependent_template_p (arg);
2070 else if (TYPE_P (arg))
2071 return cp_parser_dependent_type_p (arg);
2073 return (cp_parser_type_dependent_expression_p (arg)
2074 || cp_parser_value_dependent_expression_p (arg));
2077 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
2080 cp_parser_dependent_template_id_p (tree tmpl, tree args)
2084 if (cp_parser_dependent_template_p (tmpl))
2086 for (i = 0; i < TREE_VEC_LENGTH (args); ++i)
2087 if (cp_parser_dependent_template_arg_p (TREE_VEC_ELT (args, i)))
2092 /* Returns TRUE if the template TMPL is dependent. */
2095 cp_parser_dependent_template_p (tree tmpl)
2097 /* Template template parameters are dependent. */
2098 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)
2099 || TREE_CODE (tmpl) == TEMPLATE_TEMPLATE_PARM)
2101 /* So are member templates of dependent classes. */
2102 if (TYPE_P (CP_DECL_CONTEXT (tmpl)))
2103 return cp_parser_dependent_type_p (DECL_CONTEXT (tmpl));
2107 /* Defer checking the accessibility of DECL, when looked up in
2111 cp_parser_defer_access_check (cp_parser *parser,
2117 /* If we are not supposed to defer access checks, just check now. */
2118 if (!parser->context->deferring_access_checks_p)
2120 enforce_access (class_type, decl);
2124 /* See if we are already going to perform this check. */
2125 for (check = parser->context->deferred_access_checks;
2127 check = TREE_CHAIN (check))
2128 if (TREE_VALUE (check) == decl
2129 && same_type_p (TREE_PURPOSE (check), class_type))
2131 /* If not, record the check. */
2132 parser->context->deferred_access_checks
2133 = tree_cons (class_type, decl, parser->context->deferred_access_checks);
2136 /* Start deferring access control checks. */
2139 cp_parser_start_deferring_access_checks (cp_parser *parser)
2141 parser->context->deferring_access_checks_p = true;
2144 /* Stop deferring access control checks. Returns a TREE_LIST
2145 representing the deferred checks. The TREE_PURPOSE of each node is
2146 the type through which the access occurred; the TREE_VALUE is the
2147 declaration named. */
2150 cp_parser_stop_deferring_access_checks (parser)
2155 parser->context->deferring_access_checks_p = false;
2156 access_checks = parser->context->deferred_access_checks;
2157 parser->context->deferred_access_checks = NULL_TREE;
2159 return access_checks;
2162 /* Perform the deferred ACCESS_CHECKS, whose representation is as
2163 documented with cp_parser_stop_deferrring_access_checks. */
2166 cp_parser_perform_deferred_access_checks (access_checks)
2169 tree deferred_check;
2171 /* Look through all the deferred checks. */
2172 for (deferred_check = access_checks;
2174 deferred_check = TREE_CHAIN (deferred_check))
2176 enforce_access (TREE_PURPOSE (deferred_check),
2177 TREE_VALUE (deferred_check));
2180 /* Returns the scope through which DECL is being accessed, or
2181 NULL_TREE if DECL is not a member. If OBJECT_TYPE is non-NULL, we
2182 have just seen `x->' or `x.' and OBJECT_TYPE is the type of `*x',
2183 or `x', respectively. If the DECL was named as `A::B' then
2184 NESTED_NAME_SPECIFIER is `A'. */
2187 cp_parser_scope_through_which_access_occurs (decl,
2189 nested_name_specifier)
2192 tree nested_name_specifier;
2195 tree qualifying_type = NULL_TREE;
2197 /* Determine the SCOPE of DECL. */
2198 scope = context_for_name_lookup (decl);
2199 /* If the SCOPE is not a type, then DECL is not a member. */
2200 if (!TYPE_P (scope))
2202 /* Figure out the type through which DECL is being accessed. */
2203 if (object_type && DERIVED_FROM_P (scope, object_type))
2204 /* If we are processing a `->' or `.' expression, use the type of the
2206 qualifying_type = object_type;
2207 else if (nested_name_specifier)
2209 /* If the reference is to a non-static member of the
2210 current class, treat it as if it were referenced through
2212 if (DECL_NONSTATIC_MEMBER_P (decl)
2213 && current_class_ptr
2214 && DERIVED_FROM_P (scope, current_class_type))
2215 qualifying_type = current_class_type;
2216 /* Otherwise, use the type indicated by the
2217 nested-name-specifier. */
2219 qualifying_type = nested_name_specifier;
2222 /* Otherwise, the name must be from the current class or one of
2224 qualifying_type = currently_open_derived_class (scope);
2226 return qualifying_type;
2229 /* Issue the indicated error MESSAGE. */
2232 cp_parser_error (parser, message)
2234 const char *message;
2236 /* Output the MESSAGE -- unless we're parsing tentatively. */
2237 if (!cp_parser_simulate_error (parser))
2241 /* If we are parsing tentatively, remember that an error has occurred
2242 during this tentative parse. Returns true if the error was
2243 simulated; false if a messgae should be issued by the caller. */
2246 cp_parser_simulate_error (parser)
2249 if (cp_parser_parsing_tentatively (parser)
2250 && !cp_parser_committed_to_tentative_parse (parser))
2252 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2258 /* This function is called when a type is defined. If type
2259 definitions are forbidden at this point, an error message is
2263 cp_parser_check_type_definition (parser)
2266 /* If types are forbidden here, issue a message. */
2267 if (parser->type_definition_forbidden_message)
2268 /* Use `%s' to print the string in case there are any escape
2269 characters in the message. */
2270 error ("%s", parser->type_definition_forbidden_message);
2273 /* Consume tokens up to, and including, the next non-nested closing `)'.
2274 Returns TRUE iff we found a closing `)'. */
2277 cp_parser_skip_to_closing_parenthesis (cp_parser *parser)
2279 unsigned nesting_depth = 0;
2285 /* If we've run out of tokens, then there is no closing `)'. */
2286 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2288 /* Consume the token. */
2289 token = cp_lexer_consume_token (parser->lexer);
2290 /* If it is an `(', we have entered another level of nesting. */
2291 if (token->type == CPP_OPEN_PAREN)
2293 /* If it is a `)', then we might be done. */
2294 else if (token->type == CPP_CLOSE_PAREN && nesting_depth-- == 0)
2299 /* Consume tokens until the next token is a `)', or a `,'. Returns
2300 TRUE if the next token is a `,'. */
2303 cp_parser_skip_to_closing_parenthesis_or_comma (cp_parser *parser)
2305 unsigned nesting_depth = 0;
2309 cp_token *token = cp_lexer_peek_token (parser->lexer);
2311 /* If we've run out of tokens, then there is no closing `)'. */
2312 if (token->type == CPP_EOF)
2314 /* If it is a `,' stop. */
2315 else if (token->type == CPP_COMMA && nesting_depth-- == 0)
2317 /* If it is a `)', stop. */
2318 else if (token->type == CPP_CLOSE_PAREN && nesting_depth-- == 0)
2320 /* If it is an `(', we have entered another level of nesting. */
2321 else if (token->type == CPP_OPEN_PAREN)
2323 /* Consume the token. */
2324 token = cp_lexer_consume_token (parser->lexer);
2328 /* Consume tokens until we reach the end of the current statement.
2329 Normally, that will be just before consuming a `;'. However, if a
2330 non-nested `}' comes first, then we stop before consuming that. */
2333 cp_parser_skip_to_end_of_statement (parser)
2336 unsigned nesting_depth = 0;
2342 /* Peek at the next token. */
2343 token = cp_lexer_peek_token (parser->lexer);
2344 /* If we've run out of tokens, stop. */
2345 if (token->type == CPP_EOF)
2347 /* If the next token is a `;', we have reached the end of the
2349 if (token->type == CPP_SEMICOLON && !nesting_depth)
2351 /* If the next token is a non-nested `}', then we have reached
2352 the end of the current block. */
2353 if (token->type == CPP_CLOSE_BRACE)
2355 /* If this is a non-nested `}', stop before consuming it.
2356 That way, when confronted with something like:
2360 we stop before consuming the closing `}', even though we
2361 have not yet reached a `;'. */
2362 if (nesting_depth == 0)
2364 /* If it is the closing `}' for a block that we have
2365 scanned, stop -- but only after consuming the token.
2371 we will stop after the body of the erroneously declared
2372 function, but before consuming the following `typedef'
2374 if (--nesting_depth == 0)
2376 cp_lexer_consume_token (parser->lexer);
2380 /* If it the next token is a `{', then we are entering a new
2381 block. Consume the entire block. */
2382 else if (token->type == CPP_OPEN_BRACE)
2384 /* Consume the token. */
2385 cp_lexer_consume_token (parser->lexer);
2389 /* Skip tokens until we have consumed an entire block, or until we
2390 have consumed a non-nested `;'. */
2393 cp_parser_skip_to_end_of_block_or_statement (parser)
2396 unsigned nesting_depth = 0;
2402 /* Peek at the next token. */
2403 token = cp_lexer_peek_token (parser->lexer);
2404 /* If we've run out of tokens, stop. */
2405 if (token->type == CPP_EOF)
2407 /* If the next token is a `;', we have reached the end of the
2409 if (token->type == CPP_SEMICOLON && !nesting_depth)
2411 /* Consume the `;'. */
2412 cp_lexer_consume_token (parser->lexer);
2415 /* Consume the token. */
2416 token = cp_lexer_consume_token (parser->lexer);
2417 /* If the next token is a non-nested `}', then we have reached
2418 the end of the current block. */
2419 if (token->type == CPP_CLOSE_BRACE
2420 && (nesting_depth == 0 || --nesting_depth == 0))
2422 /* If it the next token is a `{', then we are entering a new
2423 block. Consume the entire block. */
2424 if (token->type == CPP_OPEN_BRACE)
2429 /* Skip tokens until a non-nested closing curly brace is the next
2433 cp_parser_skip_to_closing_brace (cp_parser *parser)
2435 unsigned nesting_depth = 0;
2441 /* Peek at the next token. */
2442 token = cp_lexer_peek_token (parser->lexer);
2443 /* If we've run out of tokens, stop. */
2444 if (token->type == CPP_EOF)
2446 /* If the next token is a non-nested `}', then we have reached
2447 the end of the current block. */
2448 if (token->type == CPP_CLOSE_BRACE && nesting_depth-- == 0)
2450 /* If it the next token is a `{', then we are entering a new
2451 block. Consume the entire block. */
2452 else if (token->type == CPP_OPEN_BRACE)
2454 /* Consume the token. */
2455 cp_lexer_consume_token (parser->lexer);
2459 /* Create a new C++ parser. */
2466 parser = (cp_parser *) ggc_alloc_cleared (sizeof (cp_parser));
2467 parser->lexer = cp_lexer_new (/*main_lexer_p=*/true);
2468 parser->context = cp_parser_context_new (NULL);
2470 /* For now, we always accept GNU extensions. */
2471 parser->allow_gnu_extensions_p = 1;
2473 /* The `>' token is a greater-than operator, not the end of a
2475 parser->greater_than_is_operator_p = true;
2477 parser->default_arg_ok_p = true;
2479 /* We are not parsing a constant-expression. */
2480 parser->constant_expression_p = false;
2482 /* Local variable names are not forbidden. */
2483 parser->local_variables_forbidden_p = false;
2485 /* We are not procesing an `extern "C"' declaration. */
2486 parser->in_unbraced_linkage_specification_p = false;
2488 /* We are not processing a declarator. */
2489 parser->in_declarator_p = false;
2491 /* There are no default args to process. */
2492 parser->default_arg_types = NULL;
2494 /* The unparsed function queue is empty. */
2495 parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2497 /* There are no classes being defined. */
2498 parser->num_classes_being_defined = 0;
2500 /* No template parameters apply. */
2501 parser->num_template_parameter_lists = 0;
2506 /* Lexical conventions [gram.lex] */
2508 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
2512 cp_parser_identifier (parser)
2517 /* Look for the identifier. */
2518 token = cp_parser_require (parser, CPP_NAME, "identifier");
2519 /* Return the value. */
2520 return token ? token->value : error_mark_node;
2523 /* Basic concepts [gram.basic] */
2525 /* Parse a translation-unit.
2528 declaration-seq [opt]
2530 Returns TRUE if all went well. */
2533 cp_parser_translation_unit (parser)
2538 cp_parser_declaration_seq_opt (parser);
2540 /* If there are no tokens left then all went well. */
2541 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2544 /* Otherwise, issue an error message. */
2545 cp_parser_error (parser, "expected declaration");
2549 /* Consume the EOF token. */
2550 cp_parser_require (parser, CPP_EOF, "end-of-file");
2553 finish_translation_unit ();
2555 /* All went well. */
2559 /* Expressions [gram.expr] */
2561 /* Parse a primary-expression.
2572 ( compound-statement )
2573 __builtin_va_arg ( assignment-expression , type-id )
2578 Returns a representation of the expression.
2580 *IDK indicates what kind of id-expression (if any) was present.
2582 *QUALIFYING_CLASS is set to a non-NULL value if the id-expression can be
2583 used as the operand of a pointer-to-member. In that case,
2584 *QUALIFYING_CLASS gives the class that is used as the qualifying
2585 class in the pointer-to-member. */
2588 cp_parser_primary_expression (cp_parser *parser,
2589 cp_parser_id_kind *idk,
2590 tree *qualifying_class)
2594 /* Assume the primary expression is not an id-expression. */
2595 *idk = CP_PARSER_ID_KIND_NONE;
2596 /* And that it cannot be used as pointer-to-member. */
2597 *qualifying_class = NULL_TREE;
2599 /* Peek at the next token. */
2600 token = cp_lexer_peek_token (parser->lexer);
2601 switch (token->type)
2614 token = cp_lexer_consume_token (parser->lexer);
2615 return token->value;
2617 case CPP_OPEN_PAREN:
2620 bool saved_greater_than_is_operator_p;
2622 /* Consume the `('. */
2623 cp_lexer_consume_token (parser->lexer);
2624 /* Within a parenthesized expression, a `>' token is always
2625 the greater-than operator. */
2626 saved_greater_than_is_operator_p
2627 = parser->greater_than_is_operator_p;
2628 parser->greater_than_is_operator_p = true;
2629 /* If we see `( { ' then we are looking at the beginning of
2630 a GNU statement-expression. */
2631 if (cp_parser_allow_gnu_extensions_p (parser)
2632 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
2634 /* Statement-expressions are not allowed by the standard. */
2636 pedwarn ("ISO C++ forbids braced-groups within expressions");
2638 /* And they're not allowed outside of a function-body; you
2639 cannot, for example, write:
2641 int i = ({ int j = 3; j + 1; });
2643 at class or namespace scope. */
2644 if (!at_function_scope_p ())
2645 error ("statement-expressions are allowed only inside functions");
2646 /* Start the statement-expression. */
2647 expr = begin_stmt_expr ();
2648 /* Parse the compound-statement. */
2649 cp_parser_compound_statement (parser);
2651 expr = finish_stmt_expr (expr);
2655 /* Parse the parenthesized expression. */
2656 expr = cp_parser_expression (parser);
2657 /* Let the front end know that this expression was
2658 enclosed in parentheses. This matters in case, for
2659 example, the expression is of the form `A::B', since
2660 `&A::B' might be a pointer-to-member, but `&(A::B)' is
2662 finish_parenthesized_expr (expr);
2664 /* The `>' token might be the end of a template-id or
2665 template-parameter-list now. */
2666 parser->greater_than_is_operator_p
2667 = saved_greater_than_is_operator_p;
2668 /* Consume the `)'. */
2669 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
2670 cp_parser_skip_to_end_of_statement (parser);
2676 switch (token->keyword)
2678 /* These two are the boolean literals. */
2680 cp_lexer_consume_token (parser->lexer);
2681 return boolean_true_node;
2683 cp_lexer_consume_token (parser->lexer);
2684 return boolean_false_node;
2686 /* The `__null' literal. */
2688 cp_lexer_consume_token (parser->lexer);
2691 /* Recognize the `this' keyword. */
2693 cp_lexer_consume_token (parser->lexer);
2694 if (parser->local_variables_forbidden_p)
2696 error ("`this' may not be used in this context");
2697 return error_mark_node;
2699 return finish_this_expr ();
2701 /* The `operator' keyword can be the beginning of an
2706 case RID_FUNCTION_NAME:
2707 case RID_PRETTY_FUNCTION_NAME:
2708 case RID_C99_FUNCTION_NAME:
2709 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
2710 __func__ are the names of variables -- but they are
2711 treated specially. Therefore, they are handled here,
2712 rather than relying on the generic id-expression logic
2713 below. Gramatically, these names are id-expressions.
2715 Consume the token. */
2716 token = cp_lexer_consume_token (parser->lexer);
2717 /* Look up the name. */
2718 return finish_fname (token->value);
2725 /* The `__builtin_va_arg' construct is used to handle
2726 `va_arg'. Consume the `__builtin_va_arg' token. */
2727 cp_lexer_consume_token (parser->lexer);
2728 /* Look for the opening `('. */
2729 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
2730 /* Now, parse the assignment-expression. */
2731 expression = cp_parser_assignment_expression (parser);
2732 /* Look for the `,'. */
2733 cp_parser_require (parser, CPP_COMMA, "`,'");
2734 /* Parse the type-id. */
2735 type = cp_parser_type_id (parser);
2736 /* Look for the closing `)'. */
2737 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
2739 return build_x_va_arg (expression, type);
2743 cp_parser_error (parser, "expected primary-expression");
2744 return error_mark_node;
2748 /* An id-expression can start with either an identifier, a
2749 `::' as the beginning of a qualified-id, or the "operator"
2753 case CPP_TEMPLATE_ID:
2754 case CPP_NESTED_NAME_SPECIFIER:
2760 /* Parse the id-expression. */
2762 = cp_parser_id_expression (parser,
2763 /*template_keyword_p=*/false,
2764 /*check_dependency_p=*/true,
2765 /*template_p=*/NULL);
2766 if (id_expression == error_mark_node)
2767 return error_mark_node;
2768 /* If we have a template-id, then no further lookup is
2769 required. If the template-id was for a template-class, we
2770 will sometimes have a TYPE_DECL at this point. */
2771 else if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
2772 || TREE_CODE (id_expression) == TYPE_DECL)
2773 decl = id_expression;
2774 /* Look up the name. */
2777 decl = cp_parser_lookup_name_simple (parser, id_expression);
2778 /* If name lookup gives us a SCOPE_REF, then the
2779 qualifying scope was dependent. Just propagate the
2781 if (TREE_CODE (decl) == SCOPE_REF)
2783 if (TYPE_P (TREE_OPERAND (decl, 0)))
2784 *qualifying_class = TREE_OPERAND (decl, 0);
2787 /* Check to see if DECL is a local variable in a context
2788 where that is forbidden. */
2789 if (parser->local_variables_forbidden_p
2790 && local_variable_p (decl))
2792 /* It might be that we only found DECL because we are
2793 trying to be generous with pre-ISO scoping rules.
2794 For example, consider:
2798 for (int i = 0; i < 10; ++i) {}
2799 extern void f(int j = i);
2802 Here, name look up will originally find the out
2803 of scope `i'. We need to issue a warning message,
2804 but then use the global `i'. */
2805 decl = check_for_out_of_scope_variable (decl);
2806 if (local_variable_p (decl))
2808 error ("local variable `%D' may not appear in this context",
2810 return error_mark_node;
2814 /* If unqualified name lookup fails while processing a
2815 template, that just means that we need to do name
2816 lookup again when the template is instantiated. */
2818 && decl == error_mark_node
2819 && processing_template_decl)
2821 *idk = CP_PARSER_ID_KIND_UNQUALIFIED;
2822 return build_min_nt (LOOKUP_EXPR, id_expression);
2824 else if (decl == error_mark_node
2825 && !processing_template_decl)
2829 /* It may be resolvable as a koenig lookup function
2831 *idk = CP_PARSER_ID_KIND_UNQUALIFIED;
2832 return id_expression;
2834 else if (TYPE_P (parser->scope)
2835 && !COMPLETE_TYPE_P (parser->scope))
2836 error ("incomplete type `%T' used in nested name specifier",
2838 else if (parser->scope != global_namespace)
2839 error ("`%D' is not a member of `%D'",
2840 id_expression, parser->scope);
2842 error ("`::%D' has not been declared", id_expression);
2844 /* If DECL is a variable would be out of scope under
2845 ANSI/ISO rules, but in scope in the ARM, name lookup
2846 will succeed. Issue a diagnostic here. */
2848 decl = check_for_out_of_scope_variable (decl);
2850 /* Remember that the name was used in the definition of
2851 the current class so that we can check later to see if
2852 the meaning would have been different after the class
2853 was entirely defined. */
2854 if (!parser->scope && decl != error_mark_node)
2855 maybe_note_name_used_in_class (id_expression, decl);
2858 /* If we didn't find anything, or what we found was a type,
2859 then this wasn't really an id-expression. */
2860 if (TREE_CODE (decl) == TYPE_DECL
2861 || TREE_CODE (decl) == NAMESPACE_DECL
2862 || (TREE_CODE (decl) == TEMPLATE_DECL
2863 && !DECL_FUNCTION_TEMPLATE_P (decl)))
2865 cp_parser_error (parser,
2866 "expected primary-expression");
2867 return error_mark_node;
2870 /* If the name resolved to a template parameter, there is no
2871 need to look it up again later. Similarly, we resolve
2872 enumeration constants to their underlying values. */
2873 if (TREE_CODE (decl) == CONST_DECL)
2875 *idk = CP_PARSER_ID_KIND_NONE;
2876 if (DECL_TEMPLATE_PARM_P (decl) || !processing_template_decl)
2877 return DECL_INITIAL (decl);
2884 /* If the declaration was explicitly qualified indicate
2885 that. The semantics of `A::f(3)' are different than
2886 `f(3)' if `f' is virtual. */
2887 *idk = (parser->scope
2888 ? CP_PARSER_ID_KIND_QUALIFIED
2889 : (TREE_CODE (decl) == TEMPLATE_ID_EXPR
2890 ? CP_PARSER_ID_KIND_TEMPLATE_ID
2891 : CP_PARSER_ID_KIND_UNQUALIFIED));
2896 An id-expression is type-dependent if it contains an
2897 identifier that was declared with a dependent type.
2899 As an optimization, we could choose not to create a
2900 LOOKUP_EXPR for a name that resolved to a local
2901 variable in the template function that we are currently
2902 declaring; such a name cannot ever resolve to anything
2903 else. If we did that we would not have to look up
2904 these names at instantiation time.
2906 The standard is not very specific about an
2907 id-expression that names a set of overloaded functions.
2908 What if some of them have dependent types and some of
2909 them do not? Presumably, such a name should be treated
2910 as a dependent name. */
2911 /* Assume the name is not dependent. */
2912 dependent_p = false;
2913 if (!processing_template_decl)
2914 /* No names are dependent outside a template. */
2916 /* A template-id where the name of the template was not
2917 resolved is definitely dependent. */
2918 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
2919 && (TREE_CODE (TREE_OPERAND (decl, 0))
2920 == IDENTIFIER_NODE))
2922 /* For anything except an overloaded function, just check
2924 else if (!is_overloaded_fn (decl))
2926 = cp_parser_dependent_type_p (TREE_TYPE (decl));
2927 /* For a set of overloaded functions, check each of the
2933 if (BASELINK_P (fns))
2934 fns = BASELINK_FUNCTIONS (fns);
2936 /* For a template-id, check to see if the template
2937 arguments are dependent. */
2938 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
2940 tree args = TREE_OPERAND (fns, 1);
2942 if (args && TREE_CODE (args) == TREE_LIST)
2946 if (cp_parser_dependent_template_arg_p
2947 (TREE_VALUE (args)))
2952 args = TREE_CHAIN (args);
2955 else if (args && TREE_CODE (args) == TREE_VEC)
2958 for (i = 0; i < TREE_VEC_LENGTH (args); ++i)
2959 if (cp_parser_dependent_template_arg_p
2960 (TREE_VEC_ELT (args, i)))
2967 /* The functions are those referred to by the
2969 fns = TREE_OPERAND (fns, 0);
2972 /* If there are no dependent template arguments, go
2973 through the overlaoded functions. */
2974 while (fns && !dependent_p)
2976 tree fn = OVL_CURRENT (fns);
2978 /* Member functions of dependent classes are
2980 if (TREE_CODE (fn) == FUNCTION_DECL
2981 && cp_parser_type_dependent_expression_p (fn))
2983 else if (TREE_CODE (fn) == TEMPLATE_DECL
2984 && cp_parser_dependent_template_p (fn))
2987 fns = OVL_NEXT (fns);
2991 /* If the name was dependent on a template parameter,
2992 we will resolve the name at instantiation time. */
2995 /* Create a SCOPE_REF for qualified names. */
2998 if (TYPE_P (parser->scope))
2999 *qualifying_class = parser->scope;
3000 return build_nt (SCOPE_REF,
3004 /* A TEMPLATE_ID already contains all the information
3006 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR)
3007 return id_expression;
3008 /* Create a LOOKUP_EXPR for other unqualified names. */
3009 return build_min_nt (LOOKUP_EXPR, id_expression);
3014 decl = (adjust_result_of_qualified_name_lookup
3015 (decl, parser->scope, current_class_type));
3016 if (TREE_CODE (decl) == FIELD_DECL || BASELINK_P (decl))
3017 *qualifying_class = parser->scope;
3019 /* Resolve references to variables of anonymous unions
3020 into COMPONENT_REFs. */
3021 else if (TREE_CODE (decl) == ALIAS_DECL)
3022 decl = DECL_INITIAL (decl);
3024 /* Transform references to non-static data members into
3026 decl = hack_identifier (decl, id_expression);
3029 if (TREE_DEPRECATED (decl))
3030 warn_deprecated_use (decl);
3035 /* Anything else is an error. */
3037 cp_parser_error (parser, "expected primary-expression");
3038 return error_mark_node;
3042 /* Parse an id-expression.
3049 :: [opt] nested-name-specifier template [opt] unqualified-id
3051 :: operator-function-id
3054 Return a representation of the unqualified portion of the
3055 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
3056 a `::' or nested-name-specifier.
3058 Often, if the id-expression was a qualified-id, the caller will
3059 want to make a SCOPE_REF to represent the qualified-id. This
3060 function does not do this in order to avoid wastefully creating
3061 SCOPE_REFs when they are not required.
3063 If ASSUME_TYPENAME_P is true then we assume that qualified names
3064 are typenames. This flag is set when parsing a declarator-id;
3070 we are supposed to assume that `S<T>::R' is a class.
3072 If TEMPLATE_KEYWORD_P is true, then we have just seen the
3075 If CHECK_DEPENDENCY_P is false, then names are looked up inside
3076 uninstantiated templates.
3078 If *TEMPLATE_KEYWORD_P is non-NULL, it is set to true iff the
3079 `template' keyword is used to explicitly indicate that the entity
3080 named is a template. */
3083 cp_parser_id_expression (cp_parser *parser,
3084 bool template_keyword_p,
3085 bool check_dependency_p,
3088 bool global_scope_p;
3089 bool nested_name_specifier_p;
3091 /* Assume the `template' keyword was not used. */
3093 *template_p = false;
3095 /* Look for the optional `::' operator. */
3097 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
3099 /* Look for the optional nested-name-specifier. */
3100 nested_name_specifier_p
3101 = (cp_parser_nested_name_specifier_opt (parser,
3102 /*typename_keyword_p=*/false,
3106 /* If there is a nested-name-specifier, then we are looking at
3107 the first qualified-id production. */
3108 if (nested_name_specifier_p)
3111 tree saved_object_scope;
3112 tree saved_qualifying_scope;
3113 tree unqualified_id;
3116 /* See if the next token is the `template' keyword. */
3118 template_p = &is_template;
3119 *template_p = cp_parser_optional_template_keyword (parser);
3120 /* Name lookup we do during the processing of the
3121 unqualified-id might obliterate SCOPE. */
3122 saved_scope = parser->scope;
3123 saved_object_scope = parser->object_scope;
3124 saved_qualifying_scope = parser->qualifying_scope;
3125 /* Process the final unqualified-id. */
3126 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
3127 check_dependency_p);
3128 /* Restore the SAVED_SCOPE for our caller. */
3129 parser->scope = saved_scope;
3130 parser->object_scope = saved_object_scope;
3131 parser->qualifying_scope = saved_qualifying_scope;
3133 return unqualified_id;
3135 /* Otherwise, if we are in global scope, then we are looking at one
3136 of the other qualified-id productions. */
3137 else if (global_scope_p)
3142 /* Peek at the next token. */
3143 token = cp_lexer_peek_token (parser->lexer);
3145 /* If it's an identifier, and the next token is not a "<", then
3146 we can avoid the template-id case. This is an optimization
3147 for this common case. */
3148 if (token->type == CPP_NAME
3149 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_LESS)
3150 return cp_parser_identifier (parser);
3152 cp_parser_parse_tentatively (parser);
3153 /* Try a template-id. */
3154 id = cp_parser_template_id (parser,
3155 /*template_keyword_p=*/false,
3156 /*check_dependency_p=*/true);
3157 /* If that worked, we're done. */
3158 if (cp_parser_parse_definitely (parser))
3161 /* Peek at the next token. (Changes in the token buffer may
3162 have invalidated the pointer obtained above.) */
3163 token = cp_lexer_peek_token (parser->lexer);
3165 switch (token->type)
3168 return cp_parser_identifier (parser);
3171 if (token->keyword == RID_OPERATOR)
3172 return cp_parser_operator_function_id (parser);
3176 cp_parser_error (parser, "expected id-expression");
3177 return error_mark_node;
3181 return cp_parser_unqualified_id (parser, template_keyword_p,
3182 /*check_dependency_p=*/true);
3185 /* Parse an unqualified-id.
3189 operator-function-id
3190 conversion-function-id
3194 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3195 keyword, in a construct like `A::template ...'.
3197 Returns a representation of unqualified-id. For the `identifier'
3198 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
3199 production a BIT_NOT_EXPR is returned; the operand of the
3200 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
3201 other productions, see the documentation accompanying the
3202 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
3203 names are looked up in uninstantiated templates. */
3206 cp_parser_unqualified_id (parser, template_keyword_p,
3209 bool template_keyword_p;
3210 bool check_dependency_p;
3214 /* Peek at the next token. */
3215 token = cp_lexer_peek_token (parser->lexer);
3217 switch (token->type)
3223 /* We don't know yet whether or not this will be a
3225 cp_parser_parse_tentatively (parser);
3226 /* Try a template-id. */
3227 id = cp_parser_template_id (parser, template_keyword_p,
3228 check_dependency_p);
3229 /* If it worked, we're done. */
3230 if (cp_parser_parse_definitely (parser))
3232 /* Otherwise, it's an ordinary identifier. */
3233 return cp_parser_identifier (parser);
3236 case CPP_TEMPLATE_ID:
3237 return cp_parser_template_id (parser, template_keyword_p,
3238 check_dependency_p);
3243 tree qualifying_scope;
3247 /* Consume the `~' token. */
3248 cp_lexer_consume_token (parser->lexer);
3249 /* Parse the class-name. The standard, as written, seems to
3252 template <typename T> struct S { ~S (); };
3253 template <typename T> S<T>::~S() {}
3255 is invalid, since `~' must be followed by a class-name, but
3256 `S<T>' is dependent, and so not known to be a class.
3257 That's not right; we need to look in uninstantiated
3258 templates. A further complication arises from:
3260 template <typename T> void f(T t) {
3264 Here, it is not possible to look up `T' in the scope of `T'
3265 itself. We must look in both the current scope, and the
3266 scope of the containing complete expression.
3268 Yet another issue is:
3277 The standard does not seem to say that the `S' in `~S'
3278 should refer to the type `S' and not the data member
3281 /* DR 244 says that we look up the name after the "~" in the
3282 same scope as we looked up the qualifying name. That idea
3283 isn't fully worked out; it's more complicated than that. */
3284 scope = parser->scope;
3285 object_scope = parser->object_scope;
3286 qualifying_scope = parser->qualifying_scope;
3288 /* If the name is of the form "X::~X" it's OK. */
3289 if (scope && TYPE_P (scope)
3290 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
3291 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3293 && (cp_lexer_peek_token (parser->lexer)->value
3294 == TYPE_IDENTIFIER (scope)))
3296 cp_lexer_consume_token (parser->lexer);
3297 return build_nt (BIT_NOT_EXPR, scope);
3300 /* If there was an explicit qualification (S::~T), first look
3301 in the scope given by the qualification (i.e., S). */
3304 cp_parser_parse_tentatively (parser);
3305 type_decl = cp_parser_class_name (parser,
3306 /*typename_keyword_p=*/false,
3307 /*template_keyword_p=*/false,
3309 /*check_access_p=*/true,
3310 /*check_dependency=*/false,
3311 /*class_head_p=*/false);
3312 if (cp_parser_parse_definitely (parser))
3313 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3315 /* In "N::S::~S", look in "N" as well. */
3316 if (scope && qualifying_scope)
3318 cp_parser_parse_tentatively (parser);
3319 parser->scope = qualifying_scope;
3320 parser->object_scope = NULL_TREE;
3321 parser->qualifying_scope = NULL_TREE;
3323 = cp_parser_class_name (parser,
3324 /*typename_keyword_p=*/false,
3325 /*template_keyword_p=*/false,
3327 /*check_access_p=*/true,
3328 /*check_dependency=*/false,
3329 /*class_head_p=*/false);
3330 if (cp_parser_parse_definitely (parser))
3331 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3333 /* In "p->S::~T", look in the scope given by "*p" as well. */
3334 else if (object_scope)
3336 cp_parser_parse_tentatively (parser);
3337 parser->scope = object_scope;
3338 parser->object_scope = NULL_TREE;
3339 parser->qualifying_scope = NULL_TREE;
3341 = cp_parser_class_name (parser,
3342 /*typename_keyword_p=*/false,
3343 /*template_keyword_p=*/false,
3345 /*check_access_p=*/true,
3346 /*check_dependency=*/false,
3347 /*class_head_p=*/false);
3348 if (cp_parser_parse_definitely (parser))
3349 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3351 /* Look in the surrounding context. */
3352 parser->scope = NULL_TREE;
3353 parser->object_scope = NULL_TREE;
3354 parser->qualifying_scope = NULL_TREE;
3356 = cp_parser_class_name (parser,
3357 /*typename_keyword_p=*/false,
3358 /*template_keyword_p=*/false,
3360 /*check_access_p=*/true,
3361 /*check_dependency=*/false,
3362 /*class_head_p=*/false);
3363 /* If an error occurred, assume that the name of the
3364 destructor is the same as the name of the qualifying
3365 class. That allows us to keep parsing after running
3366 into ill-formed destructor names. */
3367 if (type_decl == error_mark_node && scope && TYPE_P (scope))
3368 return build_nt (BIT_NOT_EXPR, scope);
3369 else if (type_decl == error_mark_node)
3370 return error_mark_node;
3372 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3376 if (token->keyword == RID_OPERATOR)
3380 /* This could be a template-id, so we try that first. */
3381 cp_parser_parse_tentatively (parser);
3382 /* Try a template-id. */
3383 id = cp_parser_template_id (parser, template_keyword_p,
3384 /*check_dependency_p=*/true);
3385 /* If that worked, we're done. */
3386 if (cp_parser_parse_definitely (parser))
3388 /* We still don't know whether we're looking at an
3389 operator-function-id or a conversion-function-id. */
3390 cp_parser_parse_tentatively (parser);
3391 /* Try an operator-function-id. */
3392 id = cp_parser_operator_function_id (parser);
3393 /* If that didn't work, try a conversion-function-id. */
3394 if (!cp_parser_parse_definitely (parser))
3395 id = cp_parser_conversion_function_id (parser);
3402 cp_parser_error (parser, "expected unqualified-id");
3403 return error_mark_node;
3407 /* Parse an (optional) nested-name-specifier.
3409 nested-name-specifier:
3410 class-or-namespace-name :: nested-name-specifier [opt]
3411 class-or-namespace-name :: template nested-name-specifier [opt]
3413 PARSER->SCOPE should be set appropriately before this function is
3414 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
3415 effect. TYPE_P is TRUE if we non-type bindings should be ignored
3418 Sets PARSER->SCOPE to the class (TYPE) or namespace
3419 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
3420 it unchanged if there is no nested-name-specifier. Returns the new
3421 scope iff there is a nested-name-specifier, or NULL_TREE otherwise. */
3424 cp_parser_nested_name_specifier_opt (cp_parser *parser,
3425 bool typename_keyword_p,
3426 bool check_dependency_p,
3429 bool success = false;
3430 tree access_check = NULL_TREE;
3433 /* If the next token corresponds to a nested name specifier, there
3434 is no need to reparse it. */
3435 if (cp_lexer_next_token_is (parser->lexer, CPP_NESTED_NAME_SPECIFIER))
3440 /* Get the stored value. */
3441 value = cp_lexer_consume_token (parser->lexer)->value;
3442 /* Perform any access checks that were deferred. */
3443 for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
3444 cp_parser_defer_access_check (parser,
3445 TREE_PURPOSE (check),
3446 TREE_VALUE (check));
3447 /* Set the scope from the stored value. */
3448 parser->scope = TREE_VALUE (value);
3449 parser->qualifying_scope = TREE_TYPE (value);
3450 parser->object_scope = NULL_TREE;
3451 return parser->scope;
3454 /* Remember where the nested-name-specifier starts. */
3455 if (cp_parser_parsing_tentatively (parser)
3456 && !cp_parser_committed_to_tentative_parse (parser))
3458 cp_token *next_token = cp_lexer_peek_token (parser->lexer);
3459 start = cp_lexer_token_difference (parser->lexer,
3460 parser->lexer->first_token,
3462 access_check = parser->context->deferred_access_checks;
3471 tree saved_qualifying_scope;
3473 bool template_keyword_p;
3475 /* Spot cases that cannot be the beginning of a
3476 nested-name-specifier. On the second and subsequent times
3477 through the loop, we look for the `template' keyword. */
3478 token = cp_lexer_peek_token (parser->lexer);
3479 if (success && token->keyword == RID_TEMPLATE)
3481 /* A template-id can start a nested-name-specifier. */
3482 else if (token->type == CPP_TEMPLATE_ID)
3486 /* If the next token is not an identifier, then it is
3487 definitely not a class-or-namespace-name. */
3488 if (token->type != CPP_NAME)
3490 /* If the following token is neither a `<' (to begin a
3491 template-id), nor a `::', then we are not looking at a
3492 nested-name-specifier. */
3493 token = cp_lexer_peek_nth_token (parser->lexer, 2);
3494 if (token->type != CPP_LESS && token->type != CPP_SCOPE)
3498 /* The nested-name-specifier is optional, so we parse
3500 cp_parser_parse_tentatively (parser);
3502 /* Look for the optional `template' keyword, if this isn't the
3503 first time through the loop. */
3505 template_keyword_p = cp_parser_optional_template_keyword (parser);
3507 template_keyword_p = false;
3509 /* Save the old scope since the name lookup we are about to do
3510 might destroy it. */
3511 old_scope = parser->scope;
3512 saved_qualifying_scope = parser->qualifying_scope;
3513 /* Parse the qualifying entity. */
3515 = cp_parser_class_or_namespace_name (parser,
3520 /* Look for the `::' token. */
3521 cp_parser_require (parser, CPP_SCOPE, "`::'");
3523 /* If we found what we wanted, we keep going; otherwise, we're
3525 if (!cp_parser_parse_definitely (parser))
3527 bool error_p = false;
3529 /* Restore the OLD_SCOPE since it was valid before the
3530 failed attempt at finding the last
3531 class-or-namespace-name. */
3532 parser->scope = old_scope;
3533 parser->qualifying_scope = saved_qualifying_scope;
3534 /* If the next token is an identifier, and the one after
3535 that is a `::', then any valid interpretation would have
3536 found a class-or-namespace-name. */
3537 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
3538 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3540 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
3543 token = cp_lexer_consume_token (parser->lexer);
3548 decl = cp_parser_lookup_name_simple (parser, token->value);
3549 if (TREE_CODE (decl) == TEMPLATE_DECL)
3550 error ("`%D' used without template parameters",
3552 else if (parser->scope)
3554 if (TYPE_P (parser->scope))
3555 error ("`%T::%D' is not a class-name or "
3557 parser->scope, token->value);
3559 error ("`%D::%D' is not a class-name or "
3561 parser->scope, token->value);
3564 error ("`%D' is not a class-name or namespace-name",
3566 parser->scope = NULL_TREE;
3568 /* Treat this as a successful nested-name-specifier
3573 If the name found is not a class-name (clause
3574 _class_) or namespace-name (_namespace.def_), the
3575 program is ill-formed. */
3578 cp_lexer_consume_token (parser->lexer);
3583 /* We've found one valid nested-name-specifier. */
3585 /* Make sure we look in the right scope the next time through
3587 parser->scope = (TREE_CODE (new_scope) == TYPE_DECL
3588 ? TREE_TYPE (new_scope)
3590 /* If it is a class scope, try to complete it; we are about to
3591 be looking up names inside the class. */
3592 if (TYPE_P (parser->scope))
3593 complete_type (parser->scope);
3596 /* If parsing tentatively, replace the sequence of tokens that makes
3597 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
3598 token. That way, should we re-parse the token stream, we will
3599 not have to repeat the effort required to do the parse, nor will
3600 we issue duplicate error messages. */
3601 if (success && start >= 0)
3606 /* Find the token that corresponds to the start of the
3608 token = cp_lexer_advance_token (parser->lexer,
3609 parser->lexer->first_token,
3612 /* Remember the access checks associated with this
3613 nested-name-specifier. */
3614 c = parser->context->deferred_access_checks;
3615 if (c == access_check)
3616 access_check = NULL_TREE;
3619 while (TREE_CHAIN (c) != access_check)
3621 access_check = parser->context->deferred_access_checks;
3622 parser->context->deferred_access_checks = TREE_CHAIN (c);
3623 TREE_CHAIN (c) = NULL_TREE;
3626 /* Reset the contents of the START token. */
3627 token->type = CPP_NESTED_NAME_SPECIFIER;
3628 token->value = build_tree_list (access_check, parser->scope);
3629 TREE_TYPE (token->value) = parser->qualifying_scope;
3630 token->keyword = RID_MAX;
3631 /* Purge all subsequent tokens. */
3632 cp_lexer_purge_tokens_after (parser->lexer, token);
3635 return success ? parser->scope : NULL_TREE;
3638 /* Parse a nested-name-specifier. See
3639 cp_parser_nested_name_specifier_opt for details. This function
3640 behaves identically, except that it will an issue an error if no
3641 nested-name-specifier is present, and it will return
3642 ERROR_MARK_NODE, rather than NULL_TREE, if no nested-name-specifier
3646 cp_parser_nested_name_specifier (cp_parser *parser,
3647 bool typename_keyword_p,
3648 bool check_dependency_p,
3653 /* Look for the nested-name-specifier. */
3654 scope = cp_parser_nested_name_specifier_opt (parser,
3658 /* If it was not present, issue an error message. */
3661 cp_parser_error (parser, "expected nested-name-specifier");
3662 return error_mark_node;
3668 /* Parse a class-or-namespace-name.
3670 class-or-namespace-name:
3674 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
3675 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
3676 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
3677 TYPE_P is TRUE iff the next name should be taken as a class-name,
3678 even the same name is declared to be another entity in the same
3681 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
3682 specified by the class-or-namespace-name. If neither is found the
3683 ERROR_MARK_NODE is returned. */
3686 cp_parser_class_or_namespace_name (cp_parser *parser,
3687 bool typename_keyword_p,
3688 bool template_keyword_p,
3689 bool check_dependency_p,
3693 tree saved_qualifying_scope;
3694 tree saved_object_scope;
3698 /* If the next token is the `template' keyword, we know that we are
3699 looking at a class-name. */
3700 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
3701 return cp_parser_class_name (parser,
3705 /*check_access_p=*/true,
3707 /*class_head_p=*/false);
3708 /* Before we try to parse the class-name, we must save away the
3709 current PARSER->SCOPE since cp_parser_class_name will destroy
3711 saved_scope = parser->scope;
3712 saved_qualifying_scope = parser->qualifying_scope;
3713 saved_object_scope = parser->object_scope;
3714 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
3715 there is no need to look for a namespace-name. */
3716 only_class_p = saved_scope && TYPE_P (saved_scope);
3718 cp_parser_parse_tentatively (parser);
3719 scope = cp_parser_class_name (parser,
3723 /*check_access_p=*/true,
3725 /*class_head_p=*/false);
3726 /* If that didn't work, try for a namespace-name. */
3727 if (!only_class_p && !cp_parser_parse_definitely (parser))
3729 /* Restore the saved scope. */
3730 parser->scope = saved_scope;
3731 parser->qualifying_scope = saved_qualifying_scope;
3732 parser->object_scope = saved_object_scope;
3733 /* If we are not looking at an identifier followed by the scope
3734 resolution operator, then this is not part of a
3735 nested-name-specifier. (Note that this function is only used
3736 to parse the components of a nested-name-specifier.) */
3737 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
3738 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
3739 return error_mark_node;
3740 scope = cp_parser_namespace_name (parser);
3746 /* Parse a postfix-expression.
3750 postfix-expression [ expression ]
3751 postfix-expression ( expression-list [opt] )
3752 simple-type-specifier ( expression-list [opt] )
3753 typename :: [opt] nested-name-specifier identifier
3754 ( expression-list [opt] )
3755 typename :: [opt] nested-name-specifier template [opt] template-id
3756 ( expression-list [opt] )
3757 postfix-expression . template [opt] id-expression
3758 postfix-expression -> template [opt] id-expression
3759 postfix-expression . pseudo-destructor-name
3760 postfix-expression -> pseudo-destructor-name
3761 postfix-expression ++
3762 postfix-expression --
3763 dynamic_cast < type-id > ( expression )
3764 static_cast < type-id > ( expression )
3765 reinterpret_cast < type-id > ( expression )
3766 const_cast < type-id > ( expression )
3767 typeid ( expression )
3773 ( type-id ) { initializer-list , [opt] }
3775 This extension is a GNU version of the C99 compound-literal
3776 construct. (The C99 grammar uses `type-name' instead of `type-id',
3777 but they are essentially the same concept.)
3779 If ADDRESS_P is true, the postfix expression is the operand of the
3782 Returns a representation of the expression. */
3785 cp_parser_postfix_expression (cp_parser *parser, bool address_p)
3789 cp_parser_id_kind idk = CP_PARSER_ID_KIND_NONE;
3790 tree postfix_expression = NULL_TREE;
3791 /* Non-NULL only if the current postfix-expression can be used to
3792 form a pointer-to-member. In that case, QUALIFYING_CLASS is the
3793 class used to qualify the member. */
3794 tree qualifying_class = NULL_TREE;
3797 /* Peek at the next token. */
3798 token = cp_lexer_peek_token (parser->lexer);
3799 /* Some of the productions are determined by keywords. */
3800 keyword = token->keyword;
3810 const char *saved_message;
3812 /* All of these can be handled in the same way from the point
3813 of view of parsing. Begin by consuming the token
3814 identifying the cast. */
3815 cp_lexer_consume_token (parser->lexer);
3817 /* New types cannot be defined in the cast. */
3818 saved_message = parser->type_definition_forbidden_message;
3819 parser->type_definition_forbidden_message
3820 = "types may not be defined in casts";
3822 /* Look for the opening `<'. */
3823 cp_parser_require (parser, CPP_LESS, "`<'");
3824 /* Parse the type to which we are casting. */
3825 type = cp_parser_type_id (parser);
3826 /* Look for the closing `>'. */
3827 cp_parser_require (parser, CPP_GREATER, "`>'");
3828 /* Restore the old message. */
3829 parser->type_definition_forbidden_message = saved_message;
3831 /* And the expression which is being cast. */
3832 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3833 expression = cp_parser_expression (parser);
3834 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3840 = build_dynamic_cast (type, expression);
3844 = build_static_cast (type, expression);
3848 = build_reinterpret_cast (type, expression);
3852 = build_const_cast (type, expression);
3863 const char *saved_message;
3865 /* Consume the `typeid' token. */
3866 cp_lexer_consume_token (parser->lexer);
3867 /* Look for the `(' token. */
3868 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3869 /* Types cannot be defined in a `typeid' expression. */
3870 saved_message = parser->type_definition_forbidden_message;
3871 parser->type_definition_forbidden_message
3872 = "types may not be defined in a `typeid\' expression";
3873 /* We can't be sure yet whether we're looking at a type-id or an
3875 cp_parser_parse_tentatively (parser);
3876 /* Try a type-id first. */
3877 type = cp_parser_type_id (parser);
3878 /* Look for the `)' token. Otherwise, we can't be sure that
3879 we're not looking at an expression: consider `typeid (int
3880 (3))', for example. */
3881 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3882 /* If all went well, simply lookup the type-id. */
3883 if (cp_parser_parse_definitely (parser))
3884 postfix_expression = get_typeid (type);
3885 /* Otherwise, fall back to the expression variant. */
3890 /* Look for an expression. */
3891 expression = cp_parser_expression (parser);
3892 /* Compute its typeid. */
3893 postfix_expression = build_typeid (expression);
3894 /* Look for the `)' token. */
3895 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3898 /* Restore the saved message. */
3899 parser->type_definition_forbidden_message = saved_message;
3905 bool template_p = false;
3909 /* Consume the `typename' token. */
3910 cp_lexer_consume_token (parser->lexer);
3911 /* Look for the optional `::' operator. */
3912 cp_parser_global_scope_opt (parser,
3913 /*current_scope_valid_p=*/false);
3914 /* Look for the nested-name-specifier. */
3915 cp_parser_nested_name_specifier (parser,
3916 /*typename_keyword_p=*/true,
3917 /*check_dependency_p=*/true,
3919 /* Look for the optional `template' keyword. */
3920 template_p = cp_parser_optional_template_keyword (parser);
3921 /* We don't know whether we're looking at a template-id or an
3923 cp_parser_parse_tentatively (parser);
3924 /* Try a template-id. */
3925 id = cp_parser_template_id (parser, template_p,
3926 /*check_dependency_p=*/true);
3927 /* If that didn't work, try an identifier. */
3928 if (!cp_parser_parse_definitely (parser))
3929 id = cp_parser_identifier (parser);
3930 /* Create a TYPENAME_TYPE to represent the type to which the
3931 functional cast is being performed. */
3932 type = make_typename_type (parser->scope, id,
3935 postfix_expression = cp_parser_functional_cast (parser, type);
3943 /* If the next thing is a simple-type-specifier, we may be
3944 looking at a functional cast. We could also be looking at
3945 an id-expression. So, we try the functional cast, and if
3946 that doesn't work we fall back to the primary-expression. */
3947 cp_parser_parse_tentatively (parser);
3948 /* Look for the simple-type-specifier. */
3949 type = cp_parser_simple_type_specifier (parser,
3950 CP_PARSER_FLAGS_NONE);
3951 /* Parse the cast itself. */
3952 if (!cp_parser_error_occurred (parser))
3954 = cp_parser_functional_cast (parser, type);
3955 /* If that worked, we're done. */
3956 if (cp_parser_parse_definitely (parser))
3959 /* If the functional-cast didn't work out, try a
3960 compound-literal. */
3961 if (cp_parser_allow_gnu_extensions_p (parser))
3963 tree initializer_list = NULL_TREE;
3965 cp_parser_parse_tentatively (parser);
3966 /* Look for the `('. */
3967 if (cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
3969 type = cp_parser_type_id (parser);
3970 /* Look for the `)'. */
3971 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3972 /* Look for the `{'. */
3973 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
3974 /* If things aren't going well, there's no need to
3976 if (!cp_parser_error_occurred (parser))
3978 /* Parse the initializer-list. */
3980 = cp_parser_initializer_list (parser);
3981 /* Allow a trailing `,'. */
3982 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
3983 cp_lexer_consume_token (parser->lexer);
3984 /* Look for the final `}'. */
3985 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
3988 /* If that worked, we're definitely looking at a
3989 compound-literal expression. */
3990 if (cp_parser_parse_definitely (parser))
3992 /* Warn the user that a compound literal is not
3993 allowed in standard C++. */
3995 pedwarn ("ISO C++ forbids compound-literals");
3996 /* Form the representation of the compound-literal. */
3998 = finish_compound_literal (type, initializer_list);
4003 /* It must be a primary-expression. */
4004 postfix_expression = cp_parser_primary_expression (parser,
4011 /* Peek at the next token. */
4012 token = cp_lexer_peek_token (parser->lexer);
4013 done = (token->type != CPP_OPEN_SQUARE
4014 && token->type != CPP_OPEN_PAREN
4015 && token->type != CPP_DOT
4016 && token->type != CPP_DEREF
4017 && token->type != CPP_PLUS_PLUS
4018 && token->type != CPP_MINUS_MINUS);
4020 /* If the postfix expression is complete, finish up. */
4021 if (address_p && qualifying_class && done)
4023 if (TREE_CODE (postfix_expression) == SCOPE_REF)
4024 postfix_expression = TREE_OPERAND (postfix_expression, 1);
4026 = build_offset_ref (qualifying_class, postfix_expression);
4027 return postfix_expression;
4030 /* Otherwise, if we were avoiding committing until we knew
4031 whether or not we had a pointer-to-member, we now know that
4032 the expression is an ordinary reference to a qualified name. */
4033 if (qualifying_class && !processing_template_decl)
4035 if (TREE_CODE (postfix_expression) == FIELD_DECL)
4037 = finish_non_static_data_member (postfix_expression,
4039 else if (BASELINK_P (postfix_expression))
4044 /* See if any of the functions are non-static members. */
4045 fns = BASELINK_FUNCTIONS (postfix_expression);
4046 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
4047 fns = TREE_OPERAND (fns, 0);
4048 for (fn = fns; fn; fn = OVL_NEXT (fn))
4049 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn))
4051 /* If so, the expression may be relative to the current
4053 if (fn && current_class_type
4054 && DERIVED_FROM_P (qualifying_class, current_class_type))
4056 = (build_class_member_access_expr
4057 (maybe_dummy_object (qualifying_class, NULL),
4059 BASELINK_ACCESS_BINFO (postfix_expression),
4060 /*preserve_reference=*/false));
4062 return build_offset_ref (qualifying_class,
4063 postfix_expression);
4067 /* Remember that there was a reference to this entity. */
4068 if (DECL_P (postfix_expression))
4069 mark_used (postfix_expression);
4071 /* Keep looping until the postfix-expression is complete. */
4074 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE
4075 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
4077 /* It is not a Koenig lookup function call. */
4078 unqualified_name_lookup_error (postfix_expression);
4079 postfix_expression = error_mark_node;
4082 /* Peek at the next token. */
4083 token = cp_lexer_peek_token (parser->lexer);
4085 switch (token->type)
4087 case CPP_OPEN_SQUARE:
4088 /* postfix-expression [ expression ] */
4092 /* Consume the `[' token. */
4093 cp_lexer_consume_token (parser->lexer);
4094 /* Parse the index expression. */
4095 index = cp_parser_expression (parser);
4096 /* Look for the closing `]'. */
4097 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4099 /* Build the ARRAY_REF. */
4101 = grok_array_decl (postfix_expression, index);
4102 idk = CP_PARSER_ID_KIND_NONE;
4106 case CPP_OPEN_PAREN:
4107 /* postfix-expression ( expression-list [opt] ) */
4111 /* Consume the `(' token. */
4112 cp_lexer_consume_token (parser->lexer);
4113 /* If the next token is not a `)', then there are some
4115 if (cp_lexer_next_token_is_not (parser->lexer,
4117 args = cp_parser_expression_list (parser);
4120 /* Look for the closing `)'. */
4121 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4123 if (idk == CP_PARSER_ID_KIND_UNQUALIFIED
4124 && (is_overloaded_fn (postfix_expression)
4125 || DECL_P (postfix_expression)
4126 || TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4130 tree identifier = NULL_TREE;
4131 tree functions = NULL_TREE;
4133 /* Find the name of the overloaded function. */
4134 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4135 identifier = postfix_expression;
4136 else if (is_overloaded_fn (postfix_expression))
4138 functions = postfix_expression;
4139 identifier = DECL_NAME (get_first_fn (functions));
4141 else if (DECL_P (postfix_expression))
4143 functions = postfix_expression;
4144 identifier = DECL_NAME (postfix_expression);
4147 /* A call to a namespace-scope function using an
4150 Do Koenig lookup -- unless any of the arguments are
4152 for (arg = args; arg; arg = TREE_CHAIN (arg))
4153 if (cp_parser_type_dependent_expression_p (TREE_VALUE (arg)))
4158 = lookup_arg_dependent(identifier, functions, args);
4159 if (!postfix_expression)
4161 /* The unqualified name could not be resolved. */
4162 unqualified_name_lookup_error (identifier);
4163 postfix_expression = error_mark_node;
4166 = build_call_from_tree (postfix_expression, args,
4167 /*diallow_virtual=*/false);
4170 postfix_expression = build_min_nt (LOOKUP_EXPR,
4173 else if (idk == CP_PARSER_ID_KIND_UNQUALIFIED
4174 && TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4176 /* The unqualified name could not be resolved. */
4177 unqualified_name_lookup_error (postfix_expression);
4178 postfix_expression = error_mark_node;
4182 /* In the body of a template, no further processing is
4184 if (processing_template_decl)
4186 postfix_expression = build_nt (CALL_EXPR,
4192 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
4194 = (build_new_method_call
4195 (TREE_OPERAND (postfix_expression, 0),
4196 TREE_OPERAND (postfix_expression, 1),
4198 (idk == CP_PARSER_ID_KIND_QUALIFIED
4199 ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL)));
4200 else if (TREE_CODE (postfix_expression) == OFFSET_REF)
4201 postfix_expression = (build_offset_ref_call_from_tree
4202 (postfix_expression, args));
4203 else if (idk == CP_PARSER_ID_KIND_QUALIFIED)
4205 /* A call to a static class member, or a
4206 namespace-scope function. */
4208 = finish_call_expr (postfix_expression, args,
4209 /*disallow_virtual=*/true);
4213 /* All other function calls. */
4215 = finish_call_expr (postfix_expression, args,
4216 /*disallow_virtual=*/false);
4219 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
4220 idk = CP_PARSER_ID_KIND_NONE;
4226 /* postfix-expression . template [opt] id-expression
4227 postfix-expression . pseudo-destructor-name
4228 postfix-expression -> template [opt] id-expression
4229 postfix-expression -> pseudo-destructor-name */
4234 tree scope = NULL_TREE;
4236 /* If this is a `->' operator, dereference the pointer. */
4237 if (token->type == CPP_DEREF)
4238 postfix_expression = build_x_arrow (postfix_expression);
4239 /* Check to see whether or not the expression is
4241 dependent_p = (cp_parser_type_dependent_expression_p
4242 (postfix_expression));
4243 /* The identifier following the `->' or `.' is not
4245 parser->scope = NULL_TREE;
4246 parser->qualifying_scope = NULL_TREE;
4247 parser->object_scope = NULL_TREE;
4248 /* Enter the scope corresponding to the type of the object
4249 given by the POSTFIX_EXPRESSION. */
4251 && TREE_TYPE (postfix_expression) != NULL_TREE)
4253 scope = TREE_TYPE (postfix_expression);
4254 /* According to the standard, no expression should
4255 ever have reference type. Unfortunately, we do not
4256 currently match the standard in this respect in
4257 that our internal representation of an expression
4258 may have reference type even when the standard says
4259 it does not. Therefore, we have to manually obtain
4260 the underlying type here. */
4261 if (TREE_CODE (scope) == REFERENCE_TYPE)
4262 scope = TREE_TYPE (scope);
4263 /* If the SCOPE is an OFFSET_TYPE, then we grab the
4264 type of the field. We get an OFFSET_TYPE for
4269 Probably, we should not get an OFFSET_TYPE here;
4270 that transformation should be made only if `&S::T'
4272 if (TREE_CODE (scope) == OFFSET_TYPE)
4273 scope = TREE_TYPE (scope);
4274 /* The type of the POSTFIX_EXPRESSION must be
4276 scope = complete_type_or_else (scope, NULL_TREE);
4277 /* Let the name lookup machinery know that we are
4278 processing a class member access expression. */
4279 parser->context->object_type = scope;
4280 /* If something went wrong, we want to be able to
4281 discern that case, as opposed to the case where
4282 there was no SCOPE due to the type of expression
4285 scope = error_mark_node;
4288 /* Consume the `.' or `->' operator. */
4289 cp_lexer_consume_token (parser->lexer);
4290 /* If the SCOPE is not a scalar type, we are looking at an
4291 ordinary class member access expression, rather than a
4292 pseudo-destructor-name. */
4293 if (!scope || !SCALAR_TYPE_P (scope))
4295 template_p = cp_parser_optional_template_keyword (parser);
4296 /* Parse the id-expression. */
4297 name = cp_parser_id_expression (parser,
4299 /*check_dependency_p=*/true,
4300 /*template_p=*/NULL);
4301 /* In general, build a SCOPE_REF if the member name is
4302 qualified. However, if the name was not dependent
4303 and has already been resolved; there is no need to
4304 build the SCOPE_REF. For example;
4306 struct X { void f(); };
4307 template <typename T> void f(T* t) { t->X::f(); }
4309 Even though "t" is dependent, "X::f" is not and has
4310 except that for a BASELINK there is no need to
4311 include scope information. */
4312 if (name != error_mark_node
4313 && !BASELINK_P (name)
4316 name = build_nt (SCOPE_REF, parser->scope, name);
4317 parser->scope = NULL_TREE;
4318 parser->qualifying_scope = NULL_TREE;
4319 parser->object_scope = NULL_TREE;
4322 = finish_class_member_access_expr (postfix_expression, name);
4324 /* Otherwise, try the pseudo-destructor-name production. */
4330 /* Parse the pseudo-destructor-name. */
4331 cp_parser_pseudo_destructor_name (parser, &s, &type);
4332 /* Form the call. */
4334 = finish_pseudo_destructor_expr (postfix_expression,
4335 s, TREE_TYPE (type));
4338 /* We no longer need to look up names in the scope of the
4339 object on the left-hand side of the `.' or `->'
4341 parser->context->object_type = NULL_TREE;
4342 idk = CP_PARSER_ID_KIND_NONE;
4347 /* postfix-expression ++ */
4348 /* Consume the `++' token. */
4349 cp_lexer_consume_token (parser->lexer);
4350 /* Generate a reprsentation for the complete expression. */
4352 = finish_increment_expr (postfix_expression,
4353 POSTINCREMENT_EXPR);
4354 idk = CP_PARSER_ID_KIND_NONE;
4357 case CPP_MINUS_MINUS:
4358 /* postfix-expression -- */
4359 /* Consume the `--' token. */
4360 cp_lexer_consume_token (parser->lexer);
4361 /* Generate a reprsentation for the complete expression. */
4363 = finish_increment_expr (postfix_expression,
4364 POSTDECREMENT_EXPR);
4365 idk = CP_PARSER_ID_KIND_NONE;
4369 return postfix_expression;
4373 /* We should never get here. */
4375 return error_mark_node;
4378 /* Parse an expression-list.
4381 assignment-expression
4382 expression-list, assignment-expression
4384 Returns a TREE_LIST. The TREE_VALUE of each node is a
4385 representation of an assignment-expression. Note that a TREE_LIST
4386 is returned even if there is only a single expression in the list. */
4389 cp_parser_expression_list (parser)
4392 tree expression_list = NULL_TREE;
4394 /* Consume expressions until there are no more. */
4399 /* Parse the next assignment-expression. */
4400 expr = cp_parser_assignment_expression (parser);
4401 /* Add it to the list. */
4402 expression_list = tree_cons (NULL_TREE, expr, expression_list);
4404 /* If the next token isn't a `,', then we are done. */
4405 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
4407 /* All uses of expression-list in the grammar are followed
4408 by a `)'. Therefore, if the next token is not a `)' an
4409 error will be issued, unless we are parsing tentatively.
4410 Skip ahead to see if there is another `,' before the `)';
4411 if so, we can go there and recover. */
4412 if (cp_parser_parsing_tentatively (parser)
4413 || cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
4414 || !cp_parser_skip_to_closing_parenthesis_or_comma (parser))
4418 /* Otherwise, consume the `,' and keep going. */
4419 cp_lexer_consume_token (parser->lexer);
4422 /* We built up the list in reverse order so we must reverse it now. */
4423 return nreverse (expression_list);
4426 /* Parse a pseudo-destructor-name.
4428 pseudo-destructor-name:
4429 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
4430 :: [opt] nested-name-specifier template template-id :: ~ type-name
4431 :: [opt] nested-name-specifier [opt] ~ type-name
4433 If either of the first two productions is used, sets *SCOPE to the
4434 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
4435 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
4436 or ERROR_MARK_NODE if no type-name is present. */
4439 cp_parser_pseudo_destructor_name (parser, scope, type)
4444 bool nested_name_specifier_p;
4446 /* Look for the optional `::' operator. */
4447 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
4448 /* Look for the optional nested-name-specifier. */
4449 nested_name_specifier_p
4450 = (cp_parser_nested_name_specifier_opt (parser,
4451 /*typename_keyword_p=*/false,
4452 /*check_dependency_p=*/true,
4455 /* Now, if we saw a nested-name-specifier, we might be doing the
4456 second production. */
4457 if (nested_name_specifier_p
4458 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
4460 /* Consume the `template' keyword. */
4461 cp_lexer_consume_token (parser->lexer);
4462 /* Parse the template-id. */
4463 cp_parser_template_id (parser,
4464 /*template_keyword_p=*/true,
4465 /*check_dependency_p=*/false);
4466 /* Look for the `::' token. */
4467 cp_parser_require (parser, CPP_SCOPE, "`::'");
4469 /* If the next token is not a `~', then there might be some
4470 additional qualification. */
4471 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
4473 /* Look for the type-name. */
4474 *scope = TREE_TYPE (cp_parser_type_name (parser));
4475 /* Look for the `::' token. */
4476 cp_parser_require (parser, CPP_SCOPE, "`::'");
4481 /* Look for the `~'. */
4482 cp_parser_require (parser, CPP_COMPL, "`~'");
4483 /* Look for the type-name again. We are not responsible for
4484 checking that it matches the first type-name. */
4485 *type = cp_parser_type_name (parser);
4488 /* Parse a unary-expression.
4494 unary-operator cast-expression
4495 sizeof unary-expression
4503 __extension__ cast-expression
4504 __alignof__ unary-expression
4505 __alignof__ ( type-id )
4506 __real__ cast-expression
4507 __imag__ cast-expression
4510 ADDRESS_P is true iff the unary-expression is appearing as the
4511 operand of the `&' operator.
4513 Returns a representation of the expresion. */
4516 cp_parser_unary_expression (cp_parser *parser, bool address_p)
4519 enum tree_code unary_operator;
4521 /* Peek at the next token. */
4522 token = cp_lexer_peek_token (parser->lexer);
4523 /* Some keywords give away the kind of expression. */
4524 if (token->type == CPP_KEYWORD)
4526 enum rid keyword = token->keyword;
4532 /* Consume the `alignof' token. */
4533 cp_lexer_consume_token (parser->lexer);
4534 /* Parse the operand. */
4535 return finish_alignof (cp_parser_sizeof_operand
4543 /* Consume the `sizeof' token. */
4544 cp_lexer_consume_token (parser->lexer);
4545 /* Parse the operand. */
4546 operand = cp_parser_sizeof_operand (parser, keyword);
4548 /* If the type of the operand cannot be determined build a
4550 if (TYPE_P (operand)
4551 ? cp_parser_dependent_type_p (operand)
4552 : cp_parser_type_dependent_expression_p (operand))
4553 return build_min (SIZEOF_EXPR, size_type_node, operand);
4554 /* Otherwise, compute the constant value. */
4556 return finish_sizeof (operand);
4560 return cp_parser_new_expression (parser);
4563 return cp_parser_delete_expression (parser);
4567 /* The saved value of the PEDANTIC flag. */
4571 /* Save away the PEDANTIC flag. */
4572 cp_parser_extension_opt (parser, &saved_pedantic);
4573 /* Parse the cast-expression. */
4574 expr = cp_parser_cast_expression (parser, /*address_p=*/false);
4575 /* Restore the PEDANTIC flag. */
4576 pedantic = saved_pedantic;
4586 /* Consume the `__real__' or `__imag__' token. */
4587 cp_lexer_consume_token (parser->lexer);
4588 /* Parse the cast-expression. */
4589 expression = cp_parser_cast_expression (parser,
4590 /*address_p=*/false);
4591 /* Create the complete representation. */
4592 return build_x_unary_op ((keyword == RID_REALPART
4593 ? REALPART_EXPR : IMAGPART_EXPR),
4603 /* Look for the `:: new' and `:: delete', which also signal the
4604 beginning of a new-expression, or delete-expression,
4605 respectively. If the next token is `::', then it might be one of
4607 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
4611 /* See if the token after the `::' is one of the keywords in
4612 which we're interested. */
4613 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
4614 /* If it's `new', we have a new-expression. */
4615 if (keyword == RID_NEW)
4616 return cp_parser_new_expression (parser);
4617 /* Similarly, for `delete'. */
4618 else if (keyword == RID_DELETE)
4619 return cp_parser_delete_expression (parser);
4622 /* Look for a unary operator. */
4623 unary_operator = cp_parser_unary_operator (token);
4624 /* The `++' and `--' operators can be handled similarly, even though
4625 they are not technically unary-operators in the grammar. */
4626 if (unary_operator == ERROR_MARK)
4628 if (token->type == CPP_PLUS_PLUS)
4629 unary_operator = PREINCREMENT_EXPR;
4630 else if (token->type == CPP_MINUS_MINUS)
4631 unary_operator = PREDECREMENT_EXPR;
4632 /* Handle the GNU address-of-label extension. */
4633 else if (cp_parser_allow_gnu_extensions_p (parser)
4634 && token->type == CPP_AND_AND)
4638 /* Consume the '&&' token. */
4639 cp_lexer_consume_token (parser->lexer);
4640 /* Look for the identifier. */
4641 identifier = cp_parser_identifier (parser);
4642 /* Create an expression representing the address. */
4643 return finish_label_address_expr (identifier);
4646 if (unary_operator != ERROR_MARK)
4648 tree cast_expression;
4650 /* Consume the operator token. */
4651 token = cp_lexer_consume_token (parser->lexer);
4652 /* Parse the cast-expression. */
4654 = cp_parser_cast_expression (parser, unary_operator == ADDR_EXPR);
4655 /* Now, build an appropriate representation. */
4656 switch (unary_operator)
4659 return build_x_indirect_ref (cast_expression, "unary *");
4662 return build_x_unary_op (ADDR_EXPR, cast_expression);
4666 case TRUTH_NOT_EXPR:
4667 case PREINCREMENT_EXPR:
4668 case PREDECREMENT_EXPR:
4669 return finish_unary_op_expr (unary_operator, cast_expression);
4672 return build_x_unary_op (BIT_NOT_EXPR, cast_expression);
4676 return error_mark_node;
4680 return cp_parser_postfix_expression (parser, address_p);
4683 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
4684 unary-operator, the corresponding tree code is returned. */
4686 static enum tree_code
4687 cp_parser_unary_operator (token)
4690 switch (token->type)
4693 return INDIRECT_REF;
4699 return CONVERT_EXPR;
4705 return TRUTH_NOT_EXPR;
4708 return BIT_NOT_EXPR;
4715 /* Parse a new-expression.
4717 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
4718 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
4720 Returns a representation of the expression. */
4723 cp_parser_new_expression (parser)
4726 bool global_scope_p;
4731 /* Look for the optional `::' operator. */
4733 = (cp_parser_global_scope_opt (parser,
4734 /*current_scope_valid_p=*/false)
4736 /* Look for the `new' operator. */
4737 cp_parser_require_keyword (parser, RID_NEW, "`new'");
4738 /* There's no easy way to tell a new-placement from the
4739 `( type-id )' construct. */
4740 cp_parser_parse_tentatively (parser);
4741 /* Look for a new-placement. */
4742 placement = cp_parser_new_placement (parser);
4743 /* If that didn't work out, there's no new-placement. */
4744 if (!cp_parser_parse_definitely (parser))
4745 placement = NULL_TREE;
4747 /* If the next token is a `(', then we have a parenthesized
4749 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4751 /* Consume the `('. */
4752 cp_lexer_consume_token (parser->lexer);
4753 /* Parse the type-id. */
4754 type = cp_parser_type_id (parser);
4755 /* Look for the closing `)'. */
4756 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4758 /* Otherwise, there must be a new-type-id. */
4760 type = cp_parser_new_type_id (parser);
4762 /* If the next token is a `(', then we have a new-initializer. */
4763 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4764 initializer = cp_parser_new_initializer (parser);
4766 initializer = NULL_TREE;
4768 /* Create a representation of the new-expression. */
4769 return build_new (placement, type, initializer, global_scope_p);
4772 /* Parse a new-placement.
4777 Returns the same representation as for an expression-list. */
4780 cp_parser_new_placement (parser)
4783 tree expression_list;
4785 /* Look for the opening `('. */
4786 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
4787 return error_mark_node;
4788 /* Parse the expression-list. */
4789 expression_list = cp_parser_expression_list (parser);
4790 /* Look for the closing `)'. */
4791 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4793 return expression_list;
4796 /* Parse a new-type-id.
4799 type-specifier-seq new-declarator [opt]
4801 Returns a TREE_LIST whose TREE_PURPOSE is the type-specifier-seq,
4802 and whose TREE_VALUE is the new-declarator. */
4805 cp_parser_new_type_id (parser)
4808 tree type_specifier_seq;
4810 const char *saved_message;
4812 /* The type-specifier sequence must not contain type definitions.
4813 (It cannot contain declarations of new types either, but if they
4814 are not definitions we will catch that because they are not
4816 saved_message = parser->type_definition_forbidden_message;
4817 parser->type_definition_forbidden_message
4818 = "types may not be defined in a new-type-id";
4819 /* Parse the type-specifier-seq. */
4820 type_specifier_seq = cp_parser_type_specifier_seq (parser);
4821 /* Restore the old message. */
4822 parser->type_definition_forbidden_message = saved_message;
4823 /* Parse the new-declarator. */
4824 declarator = cp_parser_new_declarator_opt (parser);
4826 return build_tree_list (type_specifier_seq, declarator);
4829 /* Parse an (optional) new-declarator.
4832 ptr-operator new-declarator [opt]
4833 direct-new-declarator
4835 Returns a representation of the declarator. See
4836 cp_parser_declarator for the representations used. */
4839 cp_parser_new_declarator_opt (parser)
4842 enum tree_code code;
4844 tree cv_qualifier_seq;
4846 /* We don't know if there's a ptr-operator next, or not. */
4847 cp_parser_parse_tentatively (parser);
4848 /* Look for a ptr-operator. */
4849 code = cp_parser_ptr_operator (parser, &type, &cv_qualifier_seq);
4850 /* If that worked, look for more new-declarators. */
4851 if (cp_parser_parse_definitely (parser))
4855 /* Parse another optional declarator. */
4856 declarator = cp_parser_new_declarator_opt (parser);
4858 /* Create the representation of the declarator. */
4859 if (code == INDIRECT_REF)
4860 declarator = make_pointer_declarator (cv_qualifier_seq,
4863 declarator = make_reference_declarator (cv_qualifier_seq,
4866 /* Handle the pointer-to-member case. */
4868 declarator = build_nt (SCOPE_REF, type, declarator);
4873 /* If the next token is a `[', there is a direct-new-declarator. */
4874 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
4875 return cp_parser_direct_new_declarator (parser);
4880 /* Parse a direct-new-declarator.
4882 direct-new-declarator:
4884 direct-new-declarator [constant-expression]
4886 Returns an ARRAY_REF, following the same conventions as are
4887 documented for cp_parser_direct_declarator. */
4890 cp_parser_direct_new_declarator (parser)
4893 tree declarator = NULL_TREE;
4899 /* Look for the opening `['. */
4900 cp_parser_require (parser, CPP_OPEN_SQUARE, "`['");
4901 /* The first expression is not required to be constant. */
4904 expression = cp_parser_expression (parser);
4905 /* The standard requires that the expression have integral
4906 type. DR 74 adds enumeration types. We believe that the
4907 real intent is that these expressions be handled like the
4908 expression in a `switch' condition, which also allows
4909 classes with a single conversion to integral or
4910 enumeration type. */
4911 if (!processing_template_decl)
4914 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
4919 error ("expression in new-declarator must have integral or enumeration type");
4920 expression = error_mark_node;
4924 /* But all the other expressions must be. */
4926 expression = cp_parser_constant_expression (parser);
4927 /* Look for the closing `]'. */
4928 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4930 /* Add this bound to the declarator. */
4931 declarator = build_nt (ARRAY_REF, declarator, expression);
4933 /* If the next token is not a `[', then there are no more
4935 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
4942 /* Parse a new-initializer.
4945 ( expression-list [opt] )
4947 Returns a reprsentation of the expression-list. If there is no
4948 expression-list, VOID_ZERO_NODE is returned. */
4951 cp_parser_new_initializer (parser)
4954 tree expression_list;
4956 /* Look for the opening parenthesis. */
4957 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
4958 /* If the next token is not a `)', then there is an
4960 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
4961 expression_list = cp_parser_expression_list (parser);
4963 expression_list = void_zero_node;
4964 /* Look for the closing parenthesis. */
4965 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4967 return expression_list;
4970 /* Parse a delete-expression.
4973 :: [opt] delete cast-expression
4974 :: [opt] delete [ ] cast-expression
4976 Returns a representation of the expression. */
4979 cp_parser_delete_expression (parser)
4982 bool global_scope_p;
4986 /* Look for the optional `::' operator. */
4988 = (cp_parser_global_scope_opt (parser,
4989 /*current_scope_valid_p=*/false)
4991 /* Look for the `delete' keyword. */
4992 cp_parser_require_keyword (parser, RID_DELETE, "`delete'");
4993 /* See if the array syntax is in use. */
4994 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
4996 /* Consume the `[' token. */
4997 cp_lexer_consume_token (parser->lexer);
4998 /* Look for the `]' token. */
4999 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5000 /* Remember that this is the `[]' construct. */
5006 /* Parse the cast-expression. */
5007 expression = cp_parser_cast_expression (parser, /*address_p=*/false);
5009 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
5012 /* Parse a cast-expression.
5016 ( type-id ) cast-expression
5018 Returns a representation of the expression. */
5021 cp_parser_cast_expression (cp_parser *parser, bool address_p)
5023 /* If it's a `(', then we might be looking at a cast. */
5024 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5026 tree type = NULL_TREE;
5027 tree expr = NULL_TREE;
5028 bool compound_literal_p;
5029 const char *saved_message;
5031 /* There's no way to know yet whether or not this is a cast.
5032 For example, `(int (3))' is a unary-expression, while `(int)
5033 3' is a cast. So, we resort to parsing tentatively. */
5034 cp_parser_parse_tentatively (parser);
5035 /* Types may not be defined in a cast. */
5036 saved_message = parser->type_definition_forbidden_message;
5037 parser->type_definition_forbidden_message
5038 = "types may not be defined in casts";
5039 /* Consume the `('. */
5040 cp_lexer_consume_token (parser->lexer);
5041 /* A very tricky bit is that `(struct S) { 3 }' is a
5042 compound-literal (which we permit in C++ as an extension).
5043 But, that construct is not a cast-expression -- it is a
5044 postfix-expression. (The reason is that `(struct S) { 3 }.i'
5045 is legal; if the compound-literal were a cast-expression,
5046 you'd need an extra set of parentheses.) But, if we parse
5047 the type-id, and it happens to be a class-specifier, then we
5048 will commit to the parse at that point, because we cannot
5049 undo the action that is done when creating a new class. So,
5050 then we cannot back up and do a postfix-expression.
5052 Therefore, we scan ahead to the closing `)', and check to see
5053 if the token after the `)' is a `{'. If so, we are not
5054 looking at a cast-expression.
5056 Save tokens so that we can put them back. */
5057 cp_lexer_save_tokens (parser->lexer);
5058 /* Skip tokens until the next token is a closing parenthesis.
5059 If we find the closing `)', and the next token is a `{', then
5060 we are looking at a compound-literal. */
5062 = (cp_parser_skip_to_closing_parenthesis (parser)
5063 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
5064 /* Roll back the tokens we skipped. */
5065 cp_lexer_rollback_tokens (parser->lexer);
5066 /* If we were looking at a compound-literal, simulate an error
5067 so that the call to cp_parser_parse_definitely below will
5069 if (compound_literal_p)
5070 cp_parser_simulate_error (parser);
5073 /* Look for the type-id. */
5074 type = cp_parser_type_id (parser);
5075 /* Look for the closing `)'. */
5076 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5079 /* Restore the saved message. */
5080 parser->type_definition_forbidden_message = saved_message;
5082 /* If all went well, this is a cast. */
5083 if (cp_parser_parse_definitely (parser))
5085 /* Parse the dependent expression. */
5086 expr = cp_parser_cast_expression (parser, /*address_p=*/false);
5087 /* Warn about old-style casts, if so requested. */
5088 if (warn_old_style_cast
5089 && !in_system_header
5090 && !VOID_TYPE_P (type)
5091 && current_lang_name != lang_name_c)
5092 warning ("use of old-style cast");
5093 /* Perform the cast. */
5094 expr = build_c_cast (type, expr);
5101 /* If we get here, then it's not a cast, so it must be a
5102 unary-expression. */
5103 return cp_parser_unary_expression (parser, address_p);
5106 /* Parse a pm-expression.
5110 pm-expression .* cast-expression
5111 pm-expression ->* cast-expression
5113 Returns a representation of the expression. */
5116 cp_parser_pm_expression (parser)
5122 /* Parse the cast-expresion. */
5123 cast_expr = cp_parser_cast_expression (parser, /*address_p=*/false);
5124 pm_expr = cast_expr;
5125 /* Now look for pointer-to-member operators. */
5129 enum cpp_ttype token_type;
5131 /* Peek at the next token. */
5132 token = cp_lexer_peek_token (parser->lexer);
5133 token_type = token->type;
5134 /* If it's not `.*' or `->*' there's no pointer-to-member
5136 if (token_type != CPP_DOT_STAR
5137 && token_type != CPP_DEREF_STAR)
5140 /* Consume the token. */
5141 cp_lexer_consume_token (parser->lexer);
5143 /* Parse another cast-expression. */
5144 cast_expr = cp_parser_cast_expression (parser, /*address_p=*/false);
5146 /* Build the representation of the pointer-to-member
5148 if (token_type == CPP_DEREF_STAR)
5149 pm_expr = build_x_binary_op (MEMBER_REF, pm_expr, cast_expr);
5151 pm_expr = build_m_component_ref (pm_expr, cast_expr);
5157 /* Parse a multiplicative-expression.
5159 mulitplicative-expression:
5161 multiplicative-expression * pm-expression
5162 multiplicative-expression / pm-expression
5163 multiplicative-expression % pm-expression
5165 Returns a representation of the expression. */
5168 cp_parser_multiplicative_expression (parser)
5171 static cp_parser_token_tree_map map = {
5172 { CPP_MULT, MULT_EXPR },
5173 { CPP_DIV, TRUNC_DIV_EXPR },
5174 { CPP_MOD, TRUNC_MOD_EXPR },
5175 { CPP_EOF, ERROR_MARK }
5178 return cp_parser_binary_expression (parser,
5180 cp_parser_pm_expression);
5183 /* Parse an additive-expression.
5185 additive-expression:
5186 multiplicative-expression
5187 additive-expression + multiplicative-expression
5188 additive-expression - multiplicative-expression
5190 Returns a representation of the expression. */
5193 cp_parser_additive_expression (parser)
5196 static cp_parser_token_tree_map map = {
5197 { CPP_PLUS, PLUS_EXPR },
5198 { CPP_MINUS, MINUS_EXPR },
5199 { CPP_EOF, ERROR_MARK }
5202 return cp_parser_binary_expression (parser,
5204 cp_parser_multiplicative_expression);
5207 /* Parse a shift-expression.
5211 shift-expression << additive-expression
5212 shift-expression >> additive-expression
5214 Returns a representation of the expression. */
5217 cp_parser_shift_expression (parser)
5220 static cp_parser_token_tree_map map = {
5221 { CPP_LSHIFT, LSHIFT_EXPR },
5222 { CPP_RSHIFT, RSHIFT_EXPR },
5223 { CPP_EOF, ERROR_MARK }
5226 return cp_parser_binary_expression (parser,
5228 cp_parser_additive_expression);
5231 /* Parse a relational-expression.
5233 relational-expression:
5235 relational-expression < shift-expression
5236 relational-expression > shift-expression
5237 relational-expression <= shift-expression
5238 relational-expression >= shift-expression
5242 relational-expression:
5243 relational-expression <? shift-expression
5244 relational-expression >? shift-expression
5246 Returns a representation of the expression. */
5249 cp_parser_relational_expression (parser)
5252 static cp_parser_token_tree_map map = {
5253 { CPP_LESS, LT_EXPR },
5254 { CPP_GREATER, GT_EXPR },
5255 { CPP_LESS_EQ, LE_EXPR },
5256 { CPP_GREATER_EQ, GE_EXPR },
5257 { CPP_MIN, MIN_EXPR },
5258 { CPP_MAX, MAX_EXPR },
5259 { CPP_EOF, ERROR_MARK }
5262 return cp_parser_binary_expression (parser,
5264 cp_parser_shift_expression);
5267 /* Parse an equality-expression.
5269 equality-expression:
5270 relational-expression
5271 equality-expression == relational-expression
5272 equality-expression != relational-expression
5274 Returns a representation of the expression. */
5277 cp_parser_equality_expression (parser)
5280 static cp_parser_token_tree_map map = {
5281 { CPP_EQ_EQ, EQ_EXPR },
5282 { CPP_NOT_EQ, NE_EXPR },
5283 { CPP_EOF, ERROR_MARK }
5286 return cp_parser_binary_expression (parser,
5288 cp_parser_relational_expression);
5291 /* Parse an and-expression.
5295 and-expression & equality-expression
5297 Returns a representation of the expression. */
5300 cp_parser_and_expression (parser)
5303 static cp_parser_token_tree_map map = {
5304 { CPP_AND, BIT_AND_EXPR },
5305 { CPP_EOF, ERROR_MARK }
5308 return cp_parser_binary_expression (parser,
5310 cp_parser_equality_expression);
5313 /* Parse an exclusive-or-expression.
5315 exclusive-or-expression:
5317 exclusive-or-expression ^ and-expression
5319 Returns a representation of the expression. */
5322 cp_parser_exclusive_or_expression (parser)
5325 static cp_parser_token_tree_map map = {
5326 { CPP_XOR, BIT_XOR_EXPR },
5327 { CPP_EOF, ERROR_MARK }
5330 return cp_parser_binary_expression (parser,
5332 cp_parser_and_expression);
5336 /* Parse an inclusive-or-expression.
5338 inclusive-or-expression:
5339 exclusive-or-expression
5340 inclusive-or-expression | exclusive-or-expression
5342 Returns a representation of the expression. */
5345 cp_parser_inclusive_or_expression (parser)
5348 static cp_parser_token_tree_map map = {
5349 { CPP_OR, BIT_IOR_EXPR },
5350 { CPP_EOF, ERROR_MARK }
5353 return cp_parser_binary_expression (parser,
5355 cp_parser_exclusive_or_expression);
5358 /* Parse a logical-and-expression.
5360 logical-and-expression:
5361 inclusive-or-expression
5362 logical-and-expression && inclusive-or-expression
5364 Returns a representation of the expression. */
5367 cp_parser_logical_and_expression (parser)
5370 static cp_parser_token_tree_map map = {
5371 { CPP_AND_AND, TRUTH_ANDIF_EXPR },
5372 { CPP_EOF, ERROR_MARK }
5375 return cp_parser_binary_expression (parser,
5377 cp_parser_inclusive_or_expression);
5380 /* Parse a logical-or-expression.
5382 logical-or-expression:
5383 logical-and-expresion
5384 logical-or-expression || logical-and-expression
5386 Returns a representation of the expression. */
5389 cp_parser_logical_or_expression (parser)
5392 static cp_parser_token_tree_map map = {
5393 { CPP_OR_OR, TRUTH_ORIF_EXPR },
5394 { CPP_EOF, ERROR_MARK }
5397 return cp_parser_binary_expression (parser,
5399 cp_parser_logical_and_expression);
5402 /* Parse a conditional-expression.
5404 conditional-expression:
5405 logical-or-expression
5406 logical-or-expression ? expression : assignment-expression
5410 conditional-expression:
5411 logical-or-expression ? : assignment-expression
5413 Returns a representation of the expression. */
5416 cp_parser_conditional_expression (parser)
5419 tree logical_or_expr;
5421 /* Parse the logical-or-expression. */
5422 logical_or_expr = cp_parser_logical_or_expression (parser);
5423 /* If the next token is a `?', then we have a real conditional
5425 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
5426 return cp_parser_question_colon_clause (parser, logical_or_expr);
5427 /* Otherwise, the value is simply the logical-or-expression. */
5429 return logical_or_expr;
5432 /* Parse the `? expression : assignment-expression' part of a
5433 conditional-expression. The LOGICAL_OR_EXPR is the
5434 logical-or-expression that started the conditional-expression.
5435 Returns a representation of the entire conditional-expression.
5437 This routine exists only so that it can be shared between
5438 cp_parser_conditional_expression and
5439 cp_parser_assignment_expression.
5441 ? expression : assignment-expression
5445 ? : assignment-expression */
5448 cp_parser_question_colon_clause (parser, logical_or_expr)
5450 tree logical_or_expr;
5453 tree assignment_expr;
5455 /* Consume the `?' token. */
5456 cp_lexer_consume_token (parser->lexer);
5457 if (cp_parser_allow_gnu_extensions_p (parser)
5458 && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
5459 /* Implicit true clause. */
5462 /* Parse the expression. */
5463 expr = cp_parser_expression (parser);
5465 /* The next token should be a `:'. */
5466 cp_parser_require (parser, CPP_COLON, "`:'");
5467 /* Parse the assignment-expression. */
5468 assignment_expr = cp_parser_assignment_expression (parser);
5470 /* Build the conditional-expression. */
5471 return build_x_conditional_expr (logical_or_expr,
5476 /* Parse an assignment-expression.
5478 assignment-expression:
5479 conditional-expression
5480 logical-or-expression assignment-operator assignment_expression
5483 Returns a representation for the expression. */
5486 cp_parser_assignment_expression (parser)
5491 /* If the next token is the `throw' keyword, then we're looking at
5492 a throw-expression. */
5493 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
5494 expr = cp_parser_throw_expression (parser);
5495 /* Otherwise, it must be that we are looking at a
5496 logical-or-expression. */
5499 /* Parse the logical-or-expression. */
5500 expr = cp_parser_logical_or_expression (parser);
5501 /* If the next token is a `?' then we're actually looking at a
5502 conditional-expression. */
5503 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
5504 return cp_parser_question_colon_clause (parser, expr);
5507 enum tree_code assignment_operator;
5509 /* If it's an assignment-operator, we're using the second
5512 = cp_parser_assignment_operator_opt (parser);
5513 if (assignment_operator != ERROR_MARK)
5517 /* Parse the right-hand side of the assignment. */
5518 rhs = cp_parser_assignment_expression (parser);
5519 /* Build the asignment expression. */
5520 expr = build_x_modify_expr (expr,
5521 assignment_operator,
5530 /* Parse an (optional) assignment-operator.
5532 assignment-operator: one of
5533 = *= /= %= += -= >>= <<= &= ^= |=
5537 assignment-operator: one of
5540 If the next token is an assignment operator, the corresponding tree
5541 code is returned, and the token is consumed. For example, for
5542 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
5543 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
5544 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
5545 operator, ERROR_MARK is returned. */
5547 static enum tree_code
5548 cp_parser_assignment_operator_opt (parser)
5554 /* Peek at the next toen. */
5555 token = cp_lexer_peek_token (parser->lexer);
5557 switch (token->type)
5568 op = TRUNC_DIV_EXPR;
5572 op = TRUNC_MOD_EXPR;
5612 /* Nothing else is an assignment operator. */
5616 /* If it was an assignment operator, consume it. */
5617 if (op != ERROR_MARK)
5618 cp_lexer_consume_token (parser->lexer);
5623 /* Parse an expression.
5626 assignment-expression
5627 expression , assignment-expression
5629 Returns a representation of the expression. */
5632 cp_parser_expression (parser)
5635 tree expression = NULL_TREE;
5636 bool saw_comma_p = false;
5640 tree assignment_expression;
5642 /* Parse the next assignment-expression. */
5643 assignment_expression
5644 = cp_parser_assignment_expression (parser);
5645 /* If this is the first assignment-expression, we can just
5648 expression = assignment_expression;
5649 /* Otherwise, chain the expressions together. It is unclear why
5650 we do not simply build COMPOUND_EXPRs as we go. */
5652 expression = tree_cons (NULL_TREE,
5653 assignment_expression,
5655 /* If the next token is not a comma, then we are done with the
5657 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5659 /* Consume the `,'. */
5660 cp_lexer_consume_token (parser->lexer);
5661 /* The first time we see a `,', we must take special action
5662 because the representation used for a single expression is
5663 different from that used for a list containing the single
5667 /* Remember that this expression has a `,' in it. */
5669 /* Turn the EXPRESSION into a TREE_LIST so that we can link
5670 additional expressions to it. */
5671 expression = build_tree_list (NULL_TREE, expression);
5675 /* Build a COMPOUND_EXPR to represent the entire expression, if
5676 necessary. We built up the list in reverse order, so we must
5677 straighten it out here. */
5679 expression = build_x_compound_expr (nreverse (expression));
5684 /* Parse a constant-expression.
5686 constant-expression:
5687 conditional-expression */
5690 cp_parser_constant_expression (parser)
5693 bool saved_constant_expression_p;
5696 /* It might seem that we could simply parse the
5697 conditional-expression, and then check to see if it were
5698 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
5699 one that the compiler can figure out is constant, possibly after
5700 doing some simplifications or optimizations. The standard has a
5701 precise definition of constant-expression, and we must honor
5702 that, even though it is somewhat more restrictive.
5708 is not a legal declaration, because `(2, 3)' is not a
5709 constant-expression. The `,' operator is forbidden in a
5710 constant-expression. However, GCC's constant-folding machinery
5711 will fold this operation to an INTEGER_CST for `3'. */
5713 /* Save the old setting of CONSTANT_EXPRESSION_P. */
5714 saved_constant_expression_p = parser->constant_expression_p;
5715 /* We are now parsing a constant-expression. */
5716 parser->constant_expression_p = true;
5717 /* Parse the conditional-expression. */
5718 expression = cp_parser_conditional_expression (parser);
5719 /* Restore the old setting of CONSTANT_EXPRESSION_P. */
5720 parser->constant_expression_p = saved_constant_expression_p;
5725 /* Statements [gram.stmt.stmt] */
5727 /* Parse a statement.
5731 expression-statement
5736 declaration-statement
5740 cp_parser_statement (parser)
5745 int statement_line_number;
5747 /* There is no statement yet. */
5748 statement = NULL_TREE;
5749 /* Peek at the next token. */
5750 token = cp_lexer_peek_token (parser->lexer);
5751 /* Remember the line number of the first token in the statement. */
5752 statement_line_number = token->line_number;
5753 /* If this is a keyword, then that will often determine what kind of
5754 statement we have. */
5755 if (token->type == CPP_KEYWORD)
5757 enum rid keyword = token->keyword;
5763 statement = cp_parser_labeled_statement (parser);
5768 statement = cp_parser_selection_statement (parser);
5774 statement = cp_parser_iteration_statement (parser);
5781 statement = cp_parser_jump_statement (parser);
5785 statement = cp_parser_try_block (parser);
5789 /* It might be a keyword like `int' that can start a
5790 declaration-statement. */
5794 else if (token->type == CPP_NAME)
5796 /* If the next token is a `:', then we are looking at a
5797 labeled-statement. */
5798 token = cp_lexer_peek_nth_token (parser->lexer, 2);
5799 if (token->type == CPP_COLON)
5800 statement = cp_parser_labeled_statement (parser);
5802 /* Anything that starts with a `{' must be a compound-statement. */
5803 else if (token->type == CPP_OPEN_BRACE)
5804 statement = cp_parser_compound_statement (parser);
5806 /* Everything else must be a declaration-statement or an
5807 expression-statement. Try for the declaration-statement
5808 first, unless we are looking at a `;', in which case we know that
5809 we have an expression-statement. */
5812 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
5814 cp_parser_parse_tentatively (parser);
5815 /* Try to parse the declaration-statement. */
5816 cp_parser_declaration_statement (parser);
5817 /* If that worked, we're done. */
5818 if (cp_parser_parse_definitely (parser))
5821 /* Look for an expression-statement instead. */
5822 statement = cp_parser_expression_statement (parser);
5825 /* Set the line number for the statement. */
5826 if (statement && statement_code_p (TREE_CODE (statement)))
5827 STMT_LINENO (statement) = statement_line_number;
5830 /* Parse a labeled-statement.
5833 identifier : statement
5834 case constant-expression : statement
5837 Returns the new CASE_LABEL, for a `case' or `default' label. For
5838 an ordinary label, returns a LABEL_STMT. */
5841 cp_parser_labeled_statement (parser)
5845 tree statement = NULL_TREE;
5847 /* The next token should be an identifier. */
5848 token = cp_lexer_peek_token (parser->lexer);
5849 if (token->type != CPP_NAME
5850 && token->type != CPP_KEYWORD)
5852 cp_parser_error (parser, "expected labeled-statement");
5853 return error_mark_node;
5856 switch (token->keyword)
5862 /* Consume the `case' token. */
5863 cp_lexer_consume_token (parser->lexer);
5864 /* Parse the constant-expression. */
5865 expr = cp_parser_constant_expression (parser);
5866 /* Create the label. */
5867 statement = finish_case_label (expr, NULL_TREE);
5872 /* Consume the `default' token. */
5873 cp_lexer_consume_token (parser->lexer);
5874 /* Create the label. */
5875 statement = finish_case_label (NULL_TREE, NULL_TREE);
5879 /* Anything else must be an ordinary label. */
5880 statement = finish_label_stmt (cp_parser_identifier (parser));
5884 /* Require the `:' token. */
5885 cp_parser_require (parser, CPP_COLON, "`:'");
5886 /* Parse the labeled statement. */
5887 cp_parser_statement (parser);
5889 /* Return the label, in the case of a `case' or `default' label. */
5893 /* Parse an expression-statement.
5895 expression-statement:
5898 Returns the new EXPR_STMT -- or NULL_TREE if the expression
5899 statement consists of nothing more than an `;'. */
5902 cp_parser_expression_statement (parser)
5907 /* If the next token is not a `;', then there is an expression to parse. */
5908 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
5909 statement = finish_expr_stmt (cp_parser_expression (parser));
5910 /* Otherwise, we do not even bother to build an EXPR_STMT. */
5914 statement = NULL_TREE;
5916 /* Consume the final `;'. */
5917 if (!cp_parser_require (parser, CPP_SEMICOLON, "`;'"))
5919 /* If there is additional (erroneous) input, skip to the end of
5921 cp_parser_skip_to_end_of_statement (parser);
5922 /* If the next token is now a `;', consume it. */
5923 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
5924 cp_lexer_consume_token (parser->lexer);
5930 /* Parse a compound-statement.
5933 { statement-seq [opt] }
5935 Returns a COMPOUND_STMT representing the statement. */
5938 cp_parser_compound_statement (cp_parser *parser)
5942 /* Consume the `{'. */
5943 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
5944 return error_mark_node;
5945 /* Begin the compound-statement. */
5946 compound_stmt = begin_compound_stmt (/*has_no_scope=*/0);
5947 /* Parse an (optional) statement-seq. */
5948 cp_parser_statement_seq_opt (parser);
5949 /* Finish the compound-statement. */
5950 finish_compound_stmt (/*has_no_scope=*/0, compound_stmt);
5951 /* Consume the `}'. */
5952 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
5954 return compound_stmt;
5957 /* Parse an (optional) statement-seq.
5961 statement-seq [opt] statement */
5964 cp_parser_statement_seq_opt (parser)
5967 /* Scan statements until there aren't any more. */
5970 /* If we're looking at a `}', then we've run out of statements. */
5971 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE)
5972 || cp_lexer_next_token_is (parser->lexer, CPP_EOF))
5975 /* Parse the statement. */
5976 cp_parser_statement (parser);
5980 /* Parse a selection-statement.
5982 selection-statement:
5983 if ( condition ) statement
5984 if ( condition ) statement else statement
5985 switch ( condition ) statement
5987 Returns the new IF_STMT or SWITCH_STMT. */
5990 cp_parser_selection_statement (parser)
5996 /* Peek at the next token. */
5997 token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
5999 /* See what kind of keyword it is. */
6000 keyword = token->keyword;
6009 /* Look for the `('. */
6010 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
6012 cp_parser_skip_to_end_of_statement (parser);
6013 return error_mark_node;
6016 /* Begin the selection-statement. */
6017 if (keyword == RID_IF)
6018 statement = begin_if_stmt ();
6020 statement = begin_switch_stmt ();
6022 /* Parse the condition. */
6023 condition = cp_parser_condition (parser);
6024 /* Look for the `)'. */
6025 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
6026 cp_parser_skip_to_closing_parenthesis (parser);
6028 if (keyword == RID_IF)
6032 /* Add the condition. */
6033 finish_if_stmt_cond (condition, statement);
6035 /* Parse the then-clause. */
6036 then_stmt = cp_parser_implicitly_scoped_statement (parser);
6037 finish_then_clause (statement);
6039 /* If the next token is `else', parse the else-clause. */
6040 if (cp_lexer_next_token_is_keyword (parser->lexer,
6045 /* Consume the `else' keyword. */
6046 cp_lexer_consume_token (parser->lexer);
6047 /* Parse the else-clause. */
6049 = cp_parser_implicitly_scoped_statement (parser);
6050 finish_else_clause (statement);
6053 /* Now we're all done with the if-statement. */
6060 /* Add the condition. */
6061 finish_switch_cond (condition, statement);
6063 /* Parse the body of the switch-statement. */
6064 body = cp_parser_implicitly_scoped_statement (parser);
6066 /* Now we're all done with the switch-statement. */
6067 finish_switch_stmt (statement);
6075 cp_parser_error (parser, "expected selection-statement");
6076 return error_mark_node;
6080 /* Parse a condition.
6084 type-specifier-seq declarator = assignment-expression
6089 type-specifier-seq declarator asm-specification [opt]
6090 attributes [opt] = assignment-expression
6092 Returns the expression that should be tested. */
6095 cp_parser_condition (parser)
6098 tree type_specifiers;
6099 const char *saved_message;
6101 /* Try the declaration first. */
6102 cp_parser_parse_tentatively (parser);
6103 /* New types are not allowed in the type-specifier-seq for a
6105 saved_message = parser->type_definition_forbidden_message;
6106 parser->type_definition_forbidden_message
6107 = "types may not be defined in conditions";
6108 /* Parse the type-specifier-seq. */
6109 type_specifiers = cp_parser_type_specifier_seq (parser);
6110 /* Restore the saved message. */
6111 parser->type_definition_forbidden_message = saved_message;
6112 /* If all is well, we might be looking at a declaration. */
6113 if (!cp_parser_error_occurred (parser))
6116 tree asm_specification;
6119 tree initializer = NULL_TREE;
6121 /* Parse the declarator. */
6122 declarator = cp_parser_declarator (parser,
6123 /*abstract_p=*/false,
6124 /*ctor_dtor_or_conv_p=*/NULL);
6125 /* Parse the attributes. */
6126 attributes = cp_parser_attributes_opt (parser);
6127 /* Parse the asm-specification. */
6128 asm_specification = cp_parser_asm_specification_opt (parser);
6129 /* If the next token is not an `=', then we might still be
6130 looking at an expression. For example:
6134 looks like a decl-specifier-seq and a declarator -- but then
6135 there is no `=', so this is an expression. */
6136 cp_parser_require (parser, CPP_EQ, "`='");
6137 /* If we did see an `=', then we are looking at a declaration
6139 if (cp_parser_parse_definitely (parser))
6141 /* Create the declaration. */
6142 decl = start_decl (declarator, type_specifiers,
6143 /*initialized_p=*/true,
6144 attributes, /*prefix_attributes=*/NULL_TREE);
6145 /* Parse the assignment-expression. */
6146 initializer = cp_parser_assignment_expression (parser);
6148 /* Process the initializer. */
6149 cp_finish_decl (decl,
6152 LOOKUP_ONLYCONVERTING);
6154 return convert_from_reference (decl);
6157 /* If we didn't even get past the declarator successfully, we are
6158 definitely not looking at a declaration. */
6160 cp_parser_abort_tentative_parse (parser);
6162 /* Otherwise, we are looking at an expression. */
6163 return cp_parser_expression (parser);
6166 /* Parse an iteration-statement.
6168 iteration-statement:
6169 while ( condition ) statement
6170 do statement while ( expression ) ;
6171 for ( for-init-statement condition [opt] ; expression [opt] )
6174 Returns the new WHILE_STMT, DO_STMT, or FOR_STMT. */
6177 cp_parser_iteration_statement (parser)
6184 /* Peek at the next token. */
6185 token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
6187 return error_mark_node;
6189 /* See what kind of keyword it is. */
6190 keyword = token->keyword;
6197 /* Begin the while-statement. */
6198 statement = begin_while_stmt ();
6199 /* Look for the `('. */
6200 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6201 /* Parse the condition. */
6202 condition = cp_parser_condition (parser);
6203 finish_while_stmt_cond (condition, statement);
6204 /* Look for the `)'. */
6205 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6206 /* Parse the dependent statement. */
6207 cp_parser_already_scoped_statement (parser);
6208 /* We're done with the while-statement. */
6209 finish_while_stmt (statement);
6217 /* Begin the do-statement. */
6218 statement = begin_do_stmt ();
6219 /* Parse the body of the do-statement. */
6220 cp_parser_implicitly_scoped_statement (parser);
6221 finish_do_body (statement);
6222 /* Look for the `while' keyword. */
6223 cp_parser_require_keyword (parser, RID_WHILE, "`while'");
6224 /* Look for the `('. */
6225 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6226 /* Parse the expression. */
6227 expression = cp_parser_expression (parser);
6228 /* We're done with the do-statement. */
6229 finish_do_stmt (expression, statement);
6230 /* Look for the `)'. */
6231 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6232 /* Look for the `;'. */
6233 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6239 tree condition = NULL_TREE;
6240 tree expression = NULL_TREE;
6242 /* Begin the for-statement. */
6243 statement = begin_for_stmt ();
6244 /* Look for the `('. */
6245 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6246 /* Parse the initialization. */
6247 cp_parser_for_init_statement (parser);
6248 finish_for_init_stmt (statement);
6250 /* If there's a condition, process it. */
6251 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6252 condition = cp_parser_condition (parser);
6253 finish_for_cond (condition, statement);
6254 /* Look for the `;'. */
6255 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6257 /* If there's an expression, process it. */
6258 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
6259 expression = cp_parser_expression (parser);
6260 finish_for_expr (expression, statement);
6261 /* Look for the `)'. */
6262 cp_parser_require (parser, CPP_CLOSE_PAREN, "`;'");
6264 /* Parse the body of the for-statement. */
6265 cp_parser_already_scoped_statement (parser);
6267 /* We're done with the for-statement. */
6268 finish_for_stmt (statement);
6273 cp_parser_error (parser, "expected iteration-statement");
6274 statement = error_mark_node;
6281 /* Parse a for-init-statement.
6284 expression-statement
6285 simple-declaration */
6288 cp_parser_for_init_statement (parser)
6291 /* If the next token is a `;', then we have an empty
6292 expression-statement. Gramatically, this is also a
6293 simple-declaration, but an invalid one, because it does not
6294 declare anything. Therefore, if we did not handle this case
6295 specially, we would issue an error message about an invalid
6297 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6299 /* We're going to speculatively look for a declaration, falling back
6300 to an expression, if necessary. */
6301 cp_parser_parse_tentatively (parser);
6302 /* Parse the declaration. */
6303 cp_parser_simple_declaration (parser,
6304 /*function_definition_allowed_p=*/false);
6305 /* If the tentative parse failed, then we shall need to look for an
6306 expression-statement. */
6307 if (cp_parser_parse_definitely (parser))
6311 cp_parser_expression_statement (parser);
6314 /* Parse a jump-statement.
6319 return expression [opt] ;
6327 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_STMT, or
6331 cp_parser_jump_statement (parser)
6334 tree statement = error_mark_node;
6338 /* Peek at the next token. */
6339 token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
6341 return error_mark_node;
6343 /* See what kind of keyword it is. */
6344 keyword = token->keyword;
6348 statement = finish_break_stmt ();
6349 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6353 statement = finish_continue_stmt ();
6354 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6361 /* If the next token is a `;', then there is no
6363 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6364 expr = cp_parser_expression (parser);
6367 /* Build the return-statement. */
6368 statement = finish_return_stmt (expr);
6369 /* Look for the final `;'. */
6370 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6375 /* Create the goto-statement. */
6376 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
6378 /* Issue a warning about this use of a GNU extension. */
6380 pedwarn ("ISO C++ forbids computed gotos");
6381 /* Consume the '*' token. */
6382 cp_lexer_consume_token (parser->lexer);
6383 /* Parse the dependent expression. */
6384 finish_goto_stmt (cp_parser_expression (parser));
6387 finish_goto_stmt (cp_parser_identifier (parser));
6388 /* Look for the final `;'. */
6389 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6393 cp_parser_error (parser, "expected jump-statement");
6400 /* Parse a declaration-statement.
6402 declaration-statement:
6403 block-declaration */
6406 cp_parser_declaration_statement (parser)
6409 /* Parse the block-declaration. */
6410 cp_parser_block_declaration (parser, /*statement_p=*/true);
6412 /* Finish off the statement. */
6416 /* Some dependent statements (like `if (cond) statement'), are
6417 implicitly in their own scope. In other words, if the statement is
6418 a single statement (as opposed to a compound-statement), it is
6419 none-the-less treated as if it were enclosed in braces. Any
6420 declarations appearing in the dependent statement are out of scope
6421 after control passes that point. This function parses a statement,
6422 but ensures that is in its own scope, even if it is not a
6425 Returns the new statement. */
6428 cp_parser_implicitly_scoped_statement (parser)
6433 /* If the token is not a `{', then we must take special action. */
6434 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
6436 /* Create a compound-statement. */
6437 statement = begin_compound_stmt (/*has_no_scope=*/0);
6438 /* Parse the dependent-statement. */
6439 cp_parser_statement (parser);
6440 /* Finish the dummy compound-statement. */
6441 finish_compound_stmt (/*has_no_scope=*/0, statement);
6443 /* Otherwise, we simply parse the statement directly. */
6445 statement = cp_parser_compound_statement (parser);
6447 /* Return the statement. */
6451 /* For some dependent statements (like `while (cond) statement'), we
6452 have already created a scope. Therefore, even if the dependent
6453 statement is a compound-statement, we do not want to create another
6457 cp_parser_already_scoped_statement (parser)
6460 /* If the token is not a `{', then we must take special action. */
6461 if (cp_lexer_next_token_is_not(parser->lexer, CPP_OPEN_BRACE))
6465 /* Create a compound-statement. */
6466 statement = begin_compound_stmt (/*has_no_scope=*/1);
6467 /* Parse the dependent-statement. */
6468 cp_parser_statement (parser);
6469 /* Finish the dummy compound-statement. */
6470 finish_compound_stmt (/*has_no_scope=*/1, statement);
6472 /* Otherwise, we simply parse the statement directly. */
6474 cp_parser_statement (parser);
6477 /* Declarations [gram.dcl.dcl] */
6479 /* Parse an optional declaration-sequence.
6483 declaration-seq declaration */
6486 cp_parser_declaration_seq_opt (parser)
6493 token = cp_lexer_peek_token (parser->lexer);
6495 if (token->type == CPP_CLOSE_BRACE
6496 || token->type == CPP_EOF)
6499 if (token->type == CPP_SEMICOLON)
6501 /* A declaration consisting of a single semicolon is
6502 invalid. Allow it unless we're being pedantic. */
6504 pedwarn ("extra `;'");
6505 cp_lexer_consume_token (parser->lexer);
6509 /* The C lexer modifies PENDING_LANG_CHANGE when it wants the
6510 parser to enter or exit implict `extern "C"' blocks. */
6511 while (pending_lang_change > 0)
6513 push_lang_context (lang_name_c);
6514 --pending_lang_change;
6516 while (pending_lang_change < 0)
6518 pop_lang_context ();
6519 ++pending_lang_change;
6522 /* Parse the declaration itself. */
6523 cp_parser_declaration (parser);
6527 /* Parse a declaration.
6532 template-declaration
6533 explicit-instantiation
6534 explicit-specialization
6535 linkage-specification
6536 namespace-definition
6541 __extension__ declaration */
6544 cp_parser_declaration (parser)
6551 /* Check for the `__extension__' keyword. */
6552 if (cp_parser_extension_opt (parser, &saved_pedantic))
6554 /* Parse the qualified declaration. */
6555 cp_parser_declaration (parser);
6556 /* Restore the PEDANTIC flag. */
6557 pedantic = saved_pedantic;
6562 /* Try to figure out what kind of declaration is present. */
6563 token1 = *cp_lexer_peek_token (parser->lexer);
6564 if (token1.type != CPP_EOF)
6565 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
6567 /* If the next token is `extern' and the following token is a string
6568 literal, then we have a linkage specification. */
6569 if (token1.keyword == RID_EXTERN
6570 && cp_parser_is_string_literal (&token2))
6571 cp_parser_linkage_specification (parser);
6572 /* If the next token is `template', then we have either a template
6573 declaration, an explicit instantiation, or an explicit
6575 else if (token1.keyword == RID_TEMPLATE)
6577 /* `template <>' indicates a template specialization. */
6578 if (token2.type == CPP_LESS
6579 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
6580 cp_parser_explicit_specialization (parser);
6581 /* `template <' indicates a template declaration. */
6582 else if (token2.type == CPP_LESS)
6583 cp_parser_template_declaration (parser, /*member_p=*/false);
6584 /* Anything else must be an explicit instantiation. */
6586 cp_parser_explicit_instantiation (parser);
6588 /* If the next token is `export', then we have a template
6590 else if (token1.keyword == RID_EXPORT)
6591 cp_parser_template_declaration (parser, /*member_p=*/false);
6592 /* If the next token is `extern', 'static' or 'inline' and the one
6593 after that is `template', we have a GNU extended explicit
6594 instantiation directive. */
6595 else if (cp_parser_allow_gnu_extensions_p (parser)
6596 && (token1.keyword == RID_EXTERN
6597 || token1.keyword == RID_STATIC
6598 || token1.keyword == RID_INLINE)
6599 && token2.keyword == RID_TEMPLATE)
6600 cp_parser_explicit_instantiation (parser);
6601 /* If the next token is `namespace', check for a named or unnamed
6602 namespace definition. */
6603 else if (token1.keyword == RID_NAMESPACE
6604 && (/* A named namespace definition. */
6605 (token2.type == CPP_NAME
6606 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
6608 /* An unnamed namespace definition. */
6609 || token2.type == CPP_OPEN_BRACE))
6610 cp_parser_namespace_definition (parser);
6611 /* We must have either a block declaration or a function
6614 /* Try to parse a block-declaration, or a function-definition. */
6615 cp_parser_block_declaration (parser, /*statement_p=*/false);
6618 /* Parse a block-declaration.
6623 namespace-alias-definition
6630 __extension__ block-declaration
6633 If STATEMENT_P is TRUE, then this block-declaration is ocurring as
6634 part of a declaration-statement. */
6637 cp_parser_block_declaration (cp_parser *parser,
6643 /* Check for the `__extension__' keyword. */
6644 if (cp_parser_extension_opt (parser, &saved_pedantic))
6646 /* Parse the qualified declaration. */
6647 cp_parser_block_declaration (parser, statement_p);
6648 /* Restore the PEDANTIC flag. */
6649 pedantic = saved_pedantic;
6654 /* Peek at the next token to figure out which kind of declaration is
6656 token1 = cp_lexer_peek_token (parser->lexer);
6658 /* If the next keyword is `asm', we have an asm-definition. */
6659 if (token1->keyword == RID_ASM)
6662 cp_parser_commit_to_tentative_parse (parser);
6663 cp_parser_asm_definition (parser);
6665 /* If the next keyword is `namespace', we have a
6666 namespace-alias-definition. */
6667 else if (token1->keyword == RID_NAMESPACE)
6668 cp_parser_namespace_alias_definition (parser);
6669 /* If the next keyword is `using', we have either a
6670 using-declaration or a using-directive. */
6671 else if (token1->keyword == RID_USING)
6676 cp_parser_commit_to_tentative_parse (parser);
6677 /* If the token after `using' is `namespace', then we have a
6679 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
6680 if (token2->keyword == RID_NAMESPACE)
6681 cp_parser_using_directive (parser);
6682 /* Otherwise, it's a using-declaration. */
6684 cp_parser_using_declaration (parser);
6686 /* If the next keyword is `__label__' we have a label declaration. */
6687 else if (token1->keyword == RID_LABEL)
6690 cp_parser_commit_to_tentative_parse (parser);
6691 cp_parser_label_declaration (parser);
6693 /* Anything else must be a simple-declaration. */
6695 cp_parser_simple_declaration (parser, !statement_p);
6698 /* Parse a simple-declaration.
6701 decl-specifier-seq [opt] init-declarator-list [opt] ;
6703 init-declarator-list:
6705 init-declarator-list , init-declarator
6707 If FUNCTION_DEFINTION_ALLOWED_P is TRUE, then we also recognize a
6708 function-definition as a simple-declaration. */
6711 cp_parser_simple_declaration (parser, function_definition_allowed_p)
6713 bool function_definition_allowed_p;
6715 tree decl_specifiers;
6718 bool declares_class_or_enum;
6719 bool saw_declarator;
6721 /* Defer access checks until we know what is being declared; the
6722 checks for names appearing in the decl-specifier-seq should be
6723 done as if we were in the scope of the thing being declared. */
6724 cp_parser_start_deferring_access_checks (parser);
6725 /* Parse the decl-specifier-seq. We have to keep track of whether
6726 or not the decl-specifier-seq declares a named class or
6727 enumeration type, since that is the only case in which the
6728 init-declarator-list is allowed to be empty.
6732 In a simple-declaration, the optional init-declarator-list can be
6733 omitted only when declaring a class or enumeration, that is when
6734 the decl-specifier-seq contains either a class-specifier, an
6735 elaborated-type-specifier, or an enum-specifier. */
6737 = cp_parser_decl_specifier_seq (parser,
6738 CP_PARSER_FLAGS_OPTIONAL,
6740 &declares_class_or_enum);
6741 /* We no longer need to defer access checks. */
6742 access_checks = cp_parser_stop_deferring_access_checks (parser);
6744 /* Keep going until we hit the `;' at the end of the simple
6746 saw_declarator = false;
6747 while (cp_lexer_next_token_is_not (parser->lexer,
6751 bool function_definition_p;
6753 saw_declarator = true;
6754 /* Parse the init-declarator. */
6755 cp_parser_init_declarator (parser, decl_specifiers, attributes,
6757 function_definition_allowed_p,
6759 &function_definition_p);
6760 /* Handle function definitions specially. */
6761 if (function_definition_p)
6763 /* If the next token is a `,', then we are probably
6764 processing something like:
6768 which is erroneous. */
6769 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
6770 error ("mixing declarations and function-definitions is forbidden");
6771 /* Otherwise, we're done with the list of declarators. */
6775 /* The next token should be either a `,' or a `;'. */
6776 token = cp_lexer_peek_token (parser->lexer);
6777 /* If it's a `,', there are more declarators to come. */
6778 if (token->type == CPP_COMMA)
6779 cp_lexer_consume_token (parser->lexer);
6780 /* If it's a `;', we are done. */
6781 else if (token->type == CPP_SEMICOLON)
6783 /* Anything else is an error. */
6786 cp_parser_error (parser, "expected `,' or `;'");
6787 /* Skip tokens until we reach the end of the statement. */
6788 cp_parser_skip_to_end_of_statement (parser);
6791 /* After the first time around, a function-definition is not
6792 allowed -- even if it was OK at first. For example:
6797 function_definition_allowed_p = false;
6800 /* Issue an error message if no declarators are present, and the
6801 decl-specifier-seq does not itself declare a class or
6803 if (!saw_declarator)
6805 if (cp_parser_declares_only_class_p (parser))
6806 shadow_tag (decl_specifiers);
6807 /* Perform any deferred access checks. */
6808 cp_parser_perform_deferred_access_checks (access_checks);
6811 /* Consume the `;'. */
6812 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6814 /* Mark all the classes that appeared in the decl-specifier-seq as
6815 having received a `;'. */
6816 note_list_got_semicolon (decl_specifiers);
6819 /* Parse a decl-specifier-seq.
6822 decl-specifier-seq [opt] decl-specifier
6825 storage-class-specifier
6834 decl-specifier-seq [opt] attributes
6836 Returns a TREE_LIST, giving the decl-specifiers in the order they
6837 appear in the source code. The TREE_VALUE of each node is the
6838 decl-specifier. For a keyword (such as `auto' or `friend'), the
6839 TREE_VALUE is simply the correspoding TREE_IDENTIFIER. For the
6840 representation of a type-specifier, see cp_parser_type_specifier.
6842 If there are attributes, they will be stored in *ATTRIBUTES,
6843 represented as described above cp_parser_attributes.
6845 If FRIEND_IS_NOT_CLASS_P is non-NULL, and the `friend' specifier
6846 appears, and the entity that will be a friend is not going to be a
6847 class, then *FRIEND_IS_NOT_CLASS_P will be set to TRUE. Note that
6848 even if *FRIEND_IS_NOT_CLASS_P is FALSE, the entity to which
6849 friendship is granted might not be a class. */
6852 cp_parser_decl_specifier_seq (parser, flags, attributes,
6853 declares_class_or_enum)
6855 cp_parser_flags flags;
6857 bool *declares_class_or_enum;
6859 tree decl_specs = NULL_TREE;
6860 bool friend_p = false;
6862 /* Assume no class or enumeration type is declared. */
6863 *declares_class_or_enum = false;
6865 /* Assume there are no attributes. */
6866 *attributes = NULL_TREE;
6868 /* Keep reading specifiers until there are no more to read. */
6871 tree decl_spec = NULL_TREE;
6875 /* Peek at the next token. */
6876 token = cp_lexer_peek_token (parser->lexer);
6877 /* Handle attributes. */
6878 if (token->keyword == RID_ATTRIBUTE)
6880 /* Parse the attributes. */
6881 decl_spec = cp_parser_attributes_opt (parser);
6882 /* Add them to the list. */
6883 *attributes = chainon (*attributes, decl_spec);
6886 /* If the next token is an appropriate keyword, we can simply
6887 add it to the list. */
6888 switch (token->keyword)
6894 /* The representation of the specifier is simply the
6895 appropriate TREE_IDENTIFIER node. */
6896 decl_spec = token->value;
6897 /* Consume the token. */
6898 cp_lexer_consume_token (parser->lexer);
6901 /* function-specifier:
6908 decl_spec = cp_parser_function_specifier_opt (parser);
6914 /* The representation of the specifier is simply the
6915 appropriate TREE_IDENTIFIER node. */
6916 decl_spec = token->value;
6917 /* Consume the token. */
6918 cp_lexer_consume_token (parser->lexer);
6921 /* storage-class-specifier:
6936 decl_spec = cp_parser_storage_class_specifier_opt (parser);
6943 /* Constructors are a special case. The `S' in `S()' is not a
6944 decl-specifier; it is the beginning of the declarator. */
6945 constructor_p = (!decl_spec
6946 && cp_parser_constructor_declarator_p (parser,
6949 /* If we don't have a DECL_SPEC yet, then we must be looking at
6950 a type-specifier. */
6951 if (!decl_spec && !constructor_p)
6953 bool decl_spec_declares_class_or_enum;
6954 bool is_cv_qualifier;
6957 = cp_parser_type_specifier (parser, flags,
6959 /*is_declaration=*/true,
6960 &decl_spec_declares_class_or_enum,
6963 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
6965 /* If this type-specifier referenced a user-defined type
6966 (a typedef, class-name, etc.), then we can't allow any
6967 more such type-specifiers henceforth.
6971 The longest sequence of decl-specifiers that could
6972 possibly be a type name is taken as the
6973 decl-specifier-seq of a declaration. The sequence shall
6974 be self-consistent as described below.
6978 As a general rule, at most one type-specifier is allowed
6979 in the complete decl-specifier-seq of a declaration. The
6980 only exceptions are the following:
6982 -- const or volatile can be combined with any other
6985 -- signed or unsigned can be combined with char, long,
6993 void g (const int Pc);
6995 Here, Pc is *not* part of the decl-specifier seq; it's
6996 the declarator. Therefore, once we see a type-specifier
6997 (other than a cv-qualifier), we forbid any additional
6998 user-defined types. We *do* still allow things like `int
6999 int' to be considered a decl-specifier-seq, and issue the
7000 error message later. */
7001 if (decl_spec && !is_cv_qualifier)
7002 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
7005 /* If we still do not have a DECL_SPEC, then there are no more
7009 /* Issue an error message, unless the entire construct was
7011 if (!(flags & CP_PARSER_FLAGS_OPTIONAL))
7013 cp_parser_error (parser, "expected decl specifier");
7014 return error_mark_node;
7020 /* Add the DECL_SPEC to the list of specifiers. */
7021 decl_specs = tree_cons (NULL_TREE, decl_spec, decl_specs);
7023 /* After we see one decl-specifier, further decl-specifiers are
7025 flags |= CP_PARSER_FLAGS_OPTIONAL;
7028 /* We have built up the DECL_SPECS in reverse order. Return them in
7029 the correct order. */
7030 return nreverse (decl_specs);
7033 /* Parse an (optional) storage-class-specifier.
7035 storage-class-specifier:
7044 storage-class-specifier:
7047 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
7050 cp_parser_storage_class_specifier_opt (parser)
7053 switch (cp_lexer_peek_token (parser->lexer)->keyword)
7061 /* Consume the token. */
7062 return cp_lexer_consume_token (parser->lexer)->value;
7069 /* Parse an (optional) function-specifier.
7076 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
7079 cp_parser_function_specifier_opt (parser)
7082 switch (cp_lexer_peek_token (parser->lexer)->keyword)
7087 /* Consume the token. */
7088 return cp_lexer_consume_token (parser->lexer)->value;
7095 /* Parse a linkage-specification.
7097 linkage-specification:
7098 extern string-literal { declaration-seq [opt] }
7099 extern string-literal declaration */
7102 cp_parser_linkage_specification (parser)
7108 /* Look for the `extern' keyword. */
7109 cp_parser_require_keyword (parser, RID_EXTERN, "`extern'");
7111 /* Peek at the next token. */
7112 token = cp_lexer_peek_token (parser->lexer);
7113 /* If it's not a string-literal, then there's a problem. */
7114 if (!cp_parser_is_string_literal (token))
7116 cp_parser_error (parser, "expected language-name");
7119 /* Consume the token. */
7120 cp_lexer_consume_token (parser->lexer);
7122 /* Transform the literal into an identifier. If the literal is a
7123 wide-character string, or contains embedded NULs, then we can't
7124 handle it as the user wants. */
7125 if (token->type == CPP_WSTRING
7126 || (strlen (TREE_STRING_POINTER (token->value))
7127 != (size_t) (TREE_STRING_LENGTH (token->value) - 1)))
7129 cp_parser_error (parser, "invalid linkage-specification");
7130 /* Assume C++ linkage. */
7131 linkage = get_identifier ("c++");
7133 /* If it's a simple string constant, things are easier. */
7135 linkage = get_identifier (TREE_STRING_POINTER (token->value));
7137 /* We're now using the new linkage. */
7138 push_lang_context (linkage);
7140 /* If the next token is a `{', then we're using the first
7142 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7144 /* Consume the `{' token. */
7145 cp_lexer_consume_token (parser->lexer);
7146 /* Parse the declarations. */
7147 cp_parser_declaration_seq_opt (parser);
7148 /* Look for the closing `}'. */
7149 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
7151 /* Otherwise, there's just one declaration. */
7154 bool saved_in_unbraced_linkage_specification_p;
7156 saved_in_unbraced_linkage_specification_p
7157 = parser->in_unbraced_linkage_specification_p;
7158 parser->in_unbraced_linkage_specification_p = true;
7159 have_extern_spec = true;
7160 cp_parser_declaration (parser);
7161 have_extern_spec = false;
7162 parser->in_unbraced_linkage_specification_p
7163 = saved_in_unbraced_linkage_specification_p;
7166 /* We're done with the linkage-specification. */
7167 pop_lang_context ();
7170 /* Special member functions [gram.special] */
7172 /* Parse a conversion-function-id.
7174 conversion-function-id:
7175 operator conversion-type-id
7177 Returns an IDENTIFIER_NODE representing the operator. */
7180 cp_parser_conversion_function_id (parser)
7185 tree saved_qualifying_scope;
7186 tree saved_object_scope;
7188 /* Look for the `operator' token. */
7189 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
7190 return error_mark_node;
7191 /* When we parse the conversion-type-id, the current scope will be
7192 reset. However, we need that information in able to look up the
7193 conversion function later, so we save it here. */
7194 saved_scope = parser->scope;
7195 saved_qualifying_scope = parser->qualifying_scope;
7196 saved_object_scope = parser->object_scope;
7197 /* We must enter the scope of the class so that the names of
7198 entities declared within the class are available in the
7199 conversion-type-id. For example, consider:
7206 S::operator I() { ... }
7208 In order to see that `I' is a type-name in the definition, we
7209 must be in the scope of `S'. */
7211 push_scope (saved_scope);
7212 /* Parse the conversion-type-id. */
7213 type = cp_parser_conversion_type_id (parser);
7214 /* Leave the scope of the class, if any. */
7216 pop_scope (saved_scope);
7217 /* Restore the saved scope. */
7218 parser->scope = saved_scope;
7219 parser->qualifying_scope = saved_qualifying_scope;
7220 parser->object_scope = saved_object_scope;
7221 /* If the TYPE is invalid, indicate failure. */
7222 if (type == error_mark_node)
7223 return error_mark_node;
7224 return mangle_conv_op_name_for_type (type);
7227 /* Parse a conversion-type-id:
7230 type-specifier-seq conversion-declarator [opt]
7232 Returns the TYPE specified. */
7235 cp_parser_conversion_type_id (parser)
7239 tree type_specifiers;
7242 /* Parse the attributes. */
7243 attributes = cp_parser_attributes_opt (parser);
7244 /* Parse the type-specifiers. */
7245 type_specifiers = cp_parser_type_specifier_seq (parser);
7246 /* If that didn't work, stop. */
7247 if (type_specifiers == error_mark_node)
7248 return error_mark_node;
7249 /* Parse the conversion-declarator. */
7250 declarator = cp_parser_conversion_declarator_opt (parser);
7252 return grokdeclarator (declarator, type_specifiers, TYPENAME,
7253 /*initialized=*/0, &attributes);
7256 /* Parse an (optional) conversion-declarator.
7258 conversion-declarator:
7259 ptr-operator conversion-declarator [opt]
7261 Returns a representation of the declarator. See
7262 cp_parser_declarator for details. */
7265 cp_parser_conversion_declarator_opt (parser)
7268 enum tree_code code;
7270 tree cv_qualifier_seq;
7272 /* We don't know if there's a ptr-operator next, or not. */
7273 cp_parser_parse_tentatively (parser);
7274 /* Try the ptr-operator. */
7275 code = cp_parser_ptr_operator (parser, &class_type,
7277 /* If it worked, look for more conversion-declarators. */
7278 if (cp_parser_parse_definitely (parser))
7282 /* Parse another optional declarator. */
7283 declarator = cp_parser_conversion_declarator_opt (parser);
7285 /* Create the representation of the declarator. */
7286 if (code == INDIRECT_REF)
7287 declarator = make_pointer_declarator (cv_qualifier_seq,
7290 declarator = make_reference_declarator (cv_qualifier_seq,
7293 /* Handle the pointer-to-member case. */
7295 declarator = build_nt (SCOPE_REF, class_type, declarator);
7303 /* Parse an (optional) ctor-initializer.
7306 : mem-initializer-list
7308 Returns TRUE iff the ctor-initializer was actually present. */
7311 cp_parser_ctor_initializer_opt (parser)
7314 /* If the next token is not a `:', then there is no
7315 ctor-initializer. */
7316 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
7318 /* Do default initialization of any bases and members. */
7319 if (DECL_CONSTRUCTOR_P (current_function_decl))
7320 finish_mem_initializers (NULL_TREE);
7325 /* Consume the `:' token. */
7326 cp_lexer_consume_token (parser->lexer);
7327 /* And the mem-initializer-list. */
7328 cp_parser_mem_initializer_list (parser);
7333 /* Parse a mem-initializer-list.
7335 mem-initializer-list:
7337 mem-initializer , mem-initializer-list */
7340 cp_parser_mem_initializer_list (parser)
7343 tree mem_initializer_list = NULL_TREE;
7345 /* Let the semantic analysis code know that we are starting the
7346 mem-initializer-list. */
7347 begin_mem_initializers ();
7349 /* Loop through the list. */
7352 tree mem_initializer;
7354 /* Parse the mem-initializer. */
7355 mem_initializer = cp_parser_mem_initializer (parser);
7356 /* Add it to the list, unless it was erroneous. */
7357 if (mem_initializer)
7359 TREE_CHAIN (mem_initializer) = mem_initializer_list;
7360 mem_initializer_list = mem_initializer;
7362 /* If the next token is not a `,', we're done. */
7363 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7365 /* Consume the `,' token. */
7366 cp_lexer_consume_token (parser->lexer);
7369 /* Perform semantic analysis. */
7370 finish_mem_initializers (mem_initializer_list);
7373 /* Parse a mem-initializer.
7376 mem-initializer-id ( expression-list [opt] )
7381 ( expresion-list [opt] )
7383 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
7384 class) or FIELD_DECL (for a non-static data member) to initialize;
7385 the TREE_VALUE is the expression-list. */
7388 cp_parser_mem_initializer (parser)
7391 tree mem_initializer_id;
7392 tree expression_list;
7394 /* Find out what is being initialized. */
7395 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7397 pedwarn ("anachronistic old-style base class initializer");
7398 mem_initializer_id = NULL_TREE;
7401 mem_initializer_id = cp_parser_mem_initializer_id (parser);
7402 /* Look for the opening `('. */
7403 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
7404 /* Parse the expression-list. */
7405 if (cp_lexer_next_token_is_not (parser->lexer,
7407 expression_list = cp_parser_expression_list (parser);
7409 expression_list = void_type_node;
7410 /* Look for the closing `)'. */
7411 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
7413 return expand_member_init (mem_initializer_id,
7417 /* Parse a mem-initializer-id.
7420 :: [opt] nested-name-specifier [opt] class-name
7423 Returns a TYPE indicating the class to be initializer for the first
7424 production. Returns an IDENTIFIER_NODE indicating the data member
7425 to be initialized for the second production. */
7428 cp_parser_mem_initializer_id (parser)
7431 bool global_scope_p;
7432 bool nested_name_specifier_p;
7435 /* Look for the optional `::' operator. */
7437 = (cp_parser_global_scope_opt (parser,
7438 /*current_scope_valid_p=*/false)
7440 /* Look for the optional nested-name-specifier. The simplest way to
7445 The keyword `typename' is not permitted in a base-specifier or
7446 mem-initializer; in these contexts a qualified name that
7447 depends on a template-parameter is implicitly assumed to be a
7450 is to assume that we have seen the `typename' keyword at this
7452 nested_name_specifier_p
7453 = (cp_parser_nested_name_specifier_opt (parser,
7454 /*typename_keyword_p=*/true,
7455 /*check_dependency_p=*/true,
7458 /* If there is a `::' operator or a nested-name-specifier, then we
7459 are definitely looking for a class-name. */
7460 if (global_scope_p || nested_name_specifier_p)
7461 return cp_parser_class_name (parser,
7462 /*typename_keyword_p=*/true,
7463 /*template_keyword_p=*/false,
7465 /*check_access_p=*/true,
7466 /*check_dependency_p=*/true,
7467 /*class_head_p=*/false);
7468 /* Otherwise, we could also be looking for an ordinary identifier. */
7469 cp_parser_parse_tentatively (parser);
7470 /* Try a class-name. */
7471 id = cp_parser_class_name (parser,
7472 /*typename_keyword_p=*/true,
7473 /*template_keyword_p=*/false,
7475 /*check_access_p=*/true,
7476 /*check_dependency_p=*/true,
7477 /*class_head_p=*/false);
7478 /* If we found one, we're done. */
7479 if (cp_parser_parse_definitely (parser))
7481 /* Otherwise, look for an ordinary identifier. */
7482 return cp_parser_identifier (parser);
7485 /* Overloading [gram.over] */
7487 /* Parse an operator-function-id.
7489 operator-function-id:
7492 Returns an IDENTIFIER_NODE for the operator which is a
7493 human-readable spelling of the identifier, e.g., `operator +'. */
7496 cp_parser_operator_function_id (parser)
7499 /* Look for the `operator' keyword. */
7500 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
7501 return error_mark_node;
7502 /* And then the name of the operator itself. */
7503 return cp_parser_operator (parser);
7506 /* Parse an operator.
7509 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
7510 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
7511 || ++ -- , ->* -> () []
7518 Returns an IDENTIFIER_NODE for the operator which is a
7519 human-readable spelling of the identifier, e.g., `operator +'. */
7522 cp_parser_operator (parser)
7525 tree id = NULL_TREE;
7528 /* Peek at the next token. */
7529 token = cp_lexer_peek_token (parser->lexer);
7530 /* Figure out which operator we have. */
7531 switch (token->type)
7537 /* The keyword should be either `new' or `delete'. */
7538 if (token->keyword == RID_NEW)
7540 else if (token->keyword == RID_DELETE)
7545 /* Consume the `new' or `delete' token. */
7546 cp_lexer_consume_token (parser->lexer);
7548 /* Peek at the next token. */
7549 token = cp_lexer_peek_token (parser->lexer);
7550 /* If it's a `[' token then this is the array variant of the
7552 if (token->type == CPP_OPEN_SQUARE)
7554 /* Consume the `[' token. */
7555 cp_lexer_consume_token (parser->lexer);
7556 /* Look for the `]' token. */
7557 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
7558 id = ansi_opname (op == NEW_EXPR
7559 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
7561 /* Otherwise, we have the non-array variant. */
7563 id = ansi_opname (op);
7569 id = ansi_opname (PLUS_EXPR);
7573 id = ansi_opname (MINUS_EXPR);
7577 id = ansi_opname (MULT_EXPR);
7581 id = ansi_opname (TRUNC_DIV_EXPR);
7585 id = ansi_opname (TRUNC_MOD_EXPR);
7589 id = ansi_opname (BIT_XOR_EXPR);
7593 id = ansi_opname (BIT_AND_EXPR);
7597 id = ansi_opname (BIT_IOR_EXPR);
7601 id = ansi_opname (BIT_NOT_EXPR);
7605 id = ansi_opname (TRUTH_NOT_EXPR);
7609 id = ansi_assopname (NOP_EXPR);
7613 id = ansi_opname (LT_EXPR);
7617 id = ansi_opname (GT_EXPR);
7621 id = ansi_assopname (PLUS_EXPR);
7625 id = ansi_assopname (MINUS_EXPR);
7629 id = ansi_assopname (MULT_EXPR);
7633 id = ansi_assopname (TRUNC_DIV_EXPR);
7637 id = ansi_assopname (TRUNC_MOD_EXPR);
7641 id = ansi_assopname (BIT_XOR_EXPR);
7645 id = ansi_assopname (BIT_AND_EXPR);
7649 id = ansi_assopname (BIT_IOR_EXPR);
7653 id = ansi_opname (LSHIFT_EXPR);
7657 id = ansi_opname (RSHIFT_EXPR);
7661 id = ansi_assopname (LSHIFT_EXPR);
7665 id = ansi_assopname (RSHIFT_EXPR);
7669 id = ansi_opname (EQ_EXPR);
7673 id = ansi_opname (NE_EXPR);
7677 id = ansi_opname (LE_EXPR);
7680 case CPP_GREATER_EQ:
7681 id = ansi_opname (GE_EXPR);
7685 id = ansi_opname (TRUTH_ANDIF_EXPR);
7689 id = ansi_opname (TRUTH_ORIF_EXPR);
7693 id = ansi_opname (POSTINCREMENT_EXPR);
7696 case CPP_MINUS_MINUS:
7697 id = ansi_opname (PREDECREMENT_EXPR);
7701 id = ansi_opname (COMPOUND_EXPR);
7704 case CPP_DEREF_STAR:
7705 id = ansi_opname (MEMBER_REF);
7709 id = ansi_opname (COMPONENT_REF);
7712 case CPP_OPEN_PAREN:
7713 /* Consume the `('. */
7714 cp_lexer_consume_token (parser->lexer);
7715 /* Look for the matching `)'. */
7716 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
7717 return ansi_opname (CALL_EXPR);
7719 case CPP_OPEN_SQUARE:
7720 /* Consume the `['. */
7721 cp_lexer_consume_token (parser->lexer);
7722 /* Look for the matching `]'. */
7723 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
7724 return ansi_opname (ARRAY_REF);
7728 id = ansi_opname (MIN_EXPR);
7732 id = ansi_opname (MAX_EXPR);
7736 id = ansi_assopname (MIN_EXPR);
7740 id = ansi_assopname (MAX_EXPR);
7744 /* Anything else is an error. */
7748 /* If we have selected an identifier, we need to consume the
7751 cp_lexer_consume_token (parser->lexer);
7752 /* Otherwise, no valid operator name was present. */
7755 cp_parser_error (parser, "expected operator");
7756 id = error_mark_node;
7762 /* Parse a template-declaration.
7764 template-declaration:
7765 export [opt] template < template-parameter-list > declaration
7767 If MEMBER_P is TRUE, this template-declaration occurs within a
7770 The grammar rule given by the standard isn't correct. What
7773 template-declaration:
7774 export [opt] template-parameter-list-seq
7775 decl-specifier-seq [opt] init-declarator [opt] ;
7776 export [opt] template-parameter-list-seq
7779 template-parameter-list-seq:
7780 template-parameter-list-seq [opt]
7781 template < template-parameter-list > */
7784 cp_parser_template_declaration (parser, member_p)
7788 /* Check for `export'. */
7789 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
7791 /* Consume the `export' token. */
7792 cp_lexer_consume_token (parser->lexer);
7793 /* Warn that we do not support `export'. */
7794 warning ("keyword `export' not implemented, and will be ignored");
7797 cp_parser_template_declaration_after_export (parser, member_p);
7800 /* Parse a template-parameter-list.
7802 template-parameter-list:
7804 template-parameter-list , template-parameter
7806 Returns a TREE_LIST. Each node represents a template parameter.
7807 The nodes are connected via their TREE_CHAINs. */
7810 cp_parser_template_parameter_list (parser)
7813 tree parameter_list = NULL_TREE;
7820 /* Parse the template-parameter. */
7821 parameter = cp_parser_template_parameter (parser);
7822 /* Add it to the list. */
7823 parameter_list = process_template_parm (parameter_list,
7826 /* Peek at the next token. */
7827 token = cp_lexer_peek_token (parser->lexer);
7828 /* If it's not a `,', we're done. */
7829 if (token->type != CPP_COMMA)
7831 /* Otherwise, consume the `,' token. */
7832 cp_lexer_consume_token (parser->lexer);
7835 return parameter_list;
7838 /* Parse a template-parameter.
7842 parameter-declaration
7844 Returns a TREE_LIST. The TREE_VALUE represents the parameter. The
7845 TREE_PURPOSE is the default value, if any. */
7848 cp_parser_template_parameter (parser)
7853 /* Peek at the next token. */
7854 token = cp_lexer_peek_token (parser->lexer);
7855 /* If it is `class' or `template', we have a type-parameter. */
7856 if (token->keyword == RID_TEMPLATE)
7857 return cp_parser_type_parameter (parser);
7858 /* If it is `class' or `typename' we do not know yet whether it is a
7859 type parameter or a non-type parameter. Consider:
7861 template <typename T, typename T::X X> ...
7865 template <class C, class D*> ...
7867 Here, the first parameter is a type parameter, and the second is
7868 a non-type parameter. We can tell by looking at the token after
7869 the identifier -- if it is a `,', `=', or `>' then we have a type
7871 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
7873 /* Peek at the token after `class' or `typename'. */
7874 token = cp_lexer_peek_nth_token (parser->lexer, 2);
7875 /* If it's an identifier, skip it. */
7876 if (token->type == CPP_NAME)
7877 token = cp_lexer_peek_nth_token (parser->lexer, 3);
7878 /* Now, see if the token looks like the end of a template
7880 if (token->type == CPP_COMMA
7881 || token->type == CPP_EQ
7882 || token->type == CPP_GREATER)
7883 return cp_parser_type_parameter (parser);
7886 /* Otherwise, it is a non-type parameter.
7890 When parsing a default template-argument for a non-type
7891 template-parameter, the first non-nested `>' is taken as the end
7892 of the template parameter-list rather than a greater-than
7895 cp_parser_parameter_declaration (parser,
7896 /*greater_than_is_operator_p=*/false);
7899 /* Parse a type-parameter.
7902 class identifier [opt]
7903 class identifier [opt] = type-id
7904 typename identifier [opt]
7905 typename identifier [opt] = type-id
7906 template < template-parameter-list > class identifier [opt]
7907 template < template-parameter-list > class identifier [opt]
7910 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
7911 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
7912 the declaration of the parameter. */
7915 cp_parser_type_parameter (parser)
7921 /* Look for a keyword to tell us what kind of parameter this is. */
7922 token = cp_parser_require (parser, CPP_KEYWORD,
7923 "expected `class', `typename', or `template'");
7925 return error_mark_node;
7927 switch (token->keyword)
7933 tree default_argument;
7935 /* If the next token is an identifier, then it names the
7937 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
7938 identifier = cp_parser_identifier (parser);
7940 identifier = NULL_TREE;
7942 /* Create the parameter. */
7943 parameter = finish_template_type_parm (class_type_node, identifier);
7945 /* If the next token is an `=', we have a default argument. */
7946 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7948 /* Consume the `=' token. */
7949 cp_lexer_consume_token (parser->lexer);
7950 /* Parse the default-argumen. */
7951 default_argument = cp_parser_type_id (parser);
7954 default_argument = NULL_TREE;
7956 /* Create the combined representation of the parameter and the
7957 default argument. */
7958 parameter = build_tree_list (default_argument,
7965 tree parameter_list;
7967 tree default_argument;
7969 /* Look for the `<'. */
7970 cp_parser_require (parser, CPP_LESS, "`<'");
7971 /* Parse the template-parameter-list. */
7972 begin_template_parm_list ();
7974 = cp_parser_template_parameter_list (parser);
7975 parameter_list = end_template_parm_list (parameter_list);
7976 /* Look for the `>'. */
7977 cp_parser_require (parser, CPP_GREATER, "`>'");
7978 /* Look for the `class' keyword. */
7979 cp_parser_require_keyword (parser, RID_CLASS, "`class'");
7980 /* If the next token is an `=', then there is a
7981 default-argument. If the next token is a `>', we are at
7982 the end of the parameter-list. If the next token is a `,',
7983 then we are at the end of this parameter. */
7984 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
7985 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
7986 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7987 identifier = cp_parser_identifier (parser);
7989 identifier = NULL_TREE;
7990 /* Create the template parameter. */
7991 parameter = finish_template_template_parm (class_type_node,
7994 /* If the next token is an `=', then there is a
7995 default-argument. */
7996 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7998 /* Consume the `='. */
7999 cp_lexer_consume_token (parser->lexer);
8000 /* Parse the id-expression. */
8002 = cp_parser_id_expression (parser,
8003 /*template_keyword_p=*/false,
8004 /*check_dependency_p=*/true,
8005 /*template_p=*/NULL);
8006 /* Look up the name. */
8008 = cp_parser_lookup_name_simple (parser, default_argument);
8009 /* See if the default argument is valid. */
8011 = check_template_template_default_arg (default_argument);
8014 default_argument = NULL_TREE;
8016 /* Create the combined representation of the parameter and the
8017 default argument. */
8018 parameter = build_tree_list (default_argument,
8024 /* Anything else is an error. */
8025 cp_parser_error (parser,
8026 "expected `class', `typename', or `template'");
8027 parameter = error_mark_node;
8033 /* Parse a template-id.
8036 template-name < template-argument-list [opt] >
8038 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
8039 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
8040 returned. Otherwise, if the template-name names a function, or set
8041 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
8042 names a class, returns a TYPE_DECL for the specialization.
8044 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
8045 uninstantiated templates. */
8048 cp_parser_template_id (cp_parser *parser,
8049 bool template_keyword_p,
8050 bool check_dependency_p)
8055 tree saved_qualifying_scope;
8056 tree saved_object_scope;
8058 bool saved_greater_than_is_operator_p;
8059 ptrdiff_t start_of_id;
8060 tree access_check = NULL_TREE;
8062 /* If the next token corresponds to a template-id, there is no need
8064 if (cp_lexer_next_token_is (parser->lexer, CPP_TEMPLATE_ID))
8069 /* Get the stored value. */
8070 value = cp_lexer_consume_token (parser->lexer)->value;
8071 /* Perform any access checks that were deferred. */
8072 for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
8073 cp_parser_defer_access_check (parser,
8074 TREE_PURPOSE (check),
8075 TREE_VALUE (check));
8076 /* Return the stored value. */
8077 return TREE_VALUE (value);
8080 /* Remember where the template-id starts. */
8081 if (cp_parser_parsing_tentatively (parser)
8082 && !cp_parser_committed_to_tentative_parse (parser))
8084 cp_token *next_token = cp_lexer_peek_token (parser->lexer);
8085 start_of_id = cp_lexer_token_difference (parser->lexer,
8086 parser->lexer->first_token,
8088 access_check = parser->context->deferred_access_checks;
8093 /* Parse the template-name. */
8094 template = cp_parser_template_name (parser, template_keyword_p,
8095 check_dependency_p);
8096 if (template == error_mark_node)
8097 return error_mark_node;
8099 /* Look for the `<' that starts the template-argument-list. */
8100 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
8101 return error_mark_node;
8105 When parsing a template-id, the first non-nested `>' is taken as
8106 the end of the template-argument-list rather than a greater-than
8108 saved_greater_than_is_operator_p
8109 = parser->greater_than_is_operator_p;
8110 parser->greater_than_is_operator_p = false;
8111 /* Parsing the argument list may modify SCOPE, so we save it
8113 saved_scope = parser->scope;
8114 saved_qualifying_scope = parser->qualifying_scope;
8115 saved_object_scope = parser->object_scope;
8116 /* Parse the template-argument-list itself. */
8117 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
8118 arguments = NULL_TREE;
8120 arguments = cp_parser_template_argument_list (parser);
8121 /* Look for the `>' that ends the template-argument-list. */
8122 cp_parser_require (parser, CPP_GREATER, "`>'");
8123 /* The `>' token might be a greater-than operator again now. */
8124 parser->greater_than_is_operator_p
8125 = saved_greater_than_is_operator_p;
8126 /* Restore the SAVED_SCOPE. */
8127 parser->scope = saved_scope;
8128 parser->qualifying_scope = saved_qualifying_scope;
8129 parser->object_scope = saved_object_scope;
8131 /* Build a representation of the specialization. */
8132 if (TREE_CODE (template) == IDENTIFIER_NODE)
8133 template_id = build_min_nt (TEMPLATE_ID_EXPR, template, arguments);
8134 else if (DECL_CLASS_TEMPLATE_P (template)
8135 || DECL_TEMPLATE_TEMPLATE_PARM_P (template))
8137 = finish_template_type (template, arguments,
8138 cp_lexer_next_token_is (parser->lexer,
8142 /* If it's not a class-template or a template-template, it should be
8143 a function-template. */
8144 my_friendly_assert ((DECL_FUNCTION_TEMPLATE_P (template)
8145 || TREE_CODE (template) == OVERLOAD
8146 || BASELINK_P (template)),
8149 template_id = lookup_template_function (template, arguments);
8152 /* If parsing tentatively, replace the sequence of tokens that makes
8153 up the template-id with a CPP_TEMPLATE_ID token. That way,
8154 should we re-parse the token stream, we will not have to repeat
8155 the effort required to do the parse, nor will we issue duplicate
8156 error messages about problems during instantiation of the
8158 if (start_of_id >= 0)
8163 /* Find the token that corresponds to the start of the
8165 token = cp_lexer_advance_token (parser->lexer,
8166 parser->lexer->first_token,
8169 /* Remember the access checks associated with this
8170 nested-name-specifier. */
8171 c = parser->context->deferred_access_checks;
8172 if (c == access_check)
8173 access_check = NULL_TREE;
8176 while (TREE_CHAIN (c) != access_check)
8178 access_check = parser->context->deferred_access_checks;
8179 parser->context->deferred_access_checks = TREE_CHAIN (c);
8180 TREE_CHAIN (c) = NULL_TREE;
8183 /* Reset the contents of the START_OF_ID token. */
8184 token->type = CPP_TEMPLATE_ID;
8185 token->value = build_tree_list (access_check, template_id);
8186 token->keyword = RID_MAX;
8187 /* Purge all subsequent tokens. */
8188 cp_lexer_purge_tokens_after (parser->lexer, token);
8194 /* Parse a template-name.
8199 The standard should actually say:
8203 operator-function-id
8204 conversion-function-id
8206 A defect report has been filed about this issue.
8208 If TEMPLATE_KEYWORD_P is true, then we have just seen the
8209 `template' keyword, in a construction like:
8213 In that case `f' is taken to be a template-name, even though there
8214 is no way of knowing for sure.
8216 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
8217 name refers to a set of overloaded functions, at least one of which
8218 is a template, or an IDENTIFIER_NODE with the name of the template,
8219 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
8220 names are looked up inside uninstantiated templates. */
8223 cp_parser_template_name (parser, template_keyword_p, check_dependency_p)
8225 bool template_keyword_p;
8226 bool check_dependency_p;
8232 /* If the next token is `operator', then we have either an
8233 operator-function-id or a conversion-function-id. */
8234 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
8236 /* We don't know whether we're looking at an
8237 operator-function-id or a conversion-function-id. */
8238 cp_parser_parse_tentatively (parser);
8239 /* Try an operator-function-id. */
8240 identifier = cp_parser_operator_function_id (parser);
8241 /* If that didn't work, try a conversion-function-id. */
8242 if (!cp_parser_parse_definitely (parser))
8243 identifier = cp_parser_conversion_function_id (parser);
8245 /* Look for the identifier. */
8247 identifier = cp_parser_identifier (parser);
8249 /* If we didn't find an identifier, we don't have a template-id. */
8250 if (identifier == error_mark_node)
8251 return error_mark_node;
8253 /* If the name immediately followed the `template' keyword, then it
8254 is a template-name. However, if the next token is not `<', then
8255 we do not treat it as a template-name, since it is not being used
8256 as part of a template-id. This enables us to handle constructs
8259 template <typename T> struct S { S(); };
8260 template <typename T> S<T>::S();
8262 correctly. We would treat `S' as a template -- if it were `S<T>'
8263 -- but we do not if there is no `<'. */
8264 if (template_keyword_p && processing_template_decl
8265 && cp_lexer_next_token_is (parser->lexer, CPP_LESS))
8268 /* Look up the name. */
8269 decl = cp_parser_lookup_name (parser, identifier,
8270 /*check_access=*/true,
8272 /*is_namespace=*/false,
8273 check_dependency_p);
8274 decl = maybe_get_template_decl_from_type_decl (decl);
8276 /* If DECL is a template, then the name was a template-name. */
8277 if (TREE_CODE (decl) == TEMPLATE_DECL)
8281 /* The standard does not explicitly indicate whether a name that
8282 names a set of overloaded declarations, some of which are
8283 templates, is a template-name. However, such a name should
8284 be a template-name; otherwise, there is no way to form a
8285 template-id for the overloaded templates. */
8286 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
8287 if (TREE_CODE (fns) == OVERLOAD)
8291 for (fn = fns; fn; fn = OVL_NEXT (fn))
8292 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
8297 /* Otherwise, the name does not name a template. */
8298 cp_parser_error (parser, "expected template-name");
8299 return error_mark_node;
8303 /* If DECL is dependent, and refers to a function, then just return
8304 its name; we will look it up again during template instantiation. */
8305 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
8307 tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
8308 if (TYPE_P (scope) && cp_parser_dependent_type_p (scope))
8315 /* Parse a template-argument-list.
8317 template-argument-list:
8319 template-argument-list , template-argument
8321 Returns a TREE_LIST representing the arguments, in the order they
8322 appeared. The TREE_VALUE of each node is a representation of the
8326 cp_parser_template_argument_list (parser)
8329 tree arguments = NULL_TREE;
8335 /* Parse the template-argument. */
8336 argument = cp_parser_template_argument (parser);
8337 /* Add it to the list. */
8338 arguments = tree_cons (NULL_TREE, argument, arguments);
8339 /* If it is not a `,', then there are no more arguments. */
8340 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8342 /* Otherwise, consume the ','. */
8343 cp_lexer_consume_token (parser->lexer);
8346 /* We built up the arguments in reverse order. */
8347 return nreverse (arguments);
8350 /* Parse a template-argument.
8353 assignment-expression
8357 The representation is that of an assignment-expression, type-id, or
8358 id-expression -- except that the qualified id-expression is
8359 evaluated, so that the value returned is either a DECL or an
8363 cp_parser_template_argument (parser)
8369 /* There's really no way to know what we're looking at, so we just
8370 try each alternative in order.
8374 In a template-argument, an ambiguity between a type-id and an
8375 expression is resolved to a type-id, regardless of the form of
8376 the corresponding template-parameter.
8378 Therefore, we try a type-id first. */
8379 cp_parser_parse_tentatively (parser);
8380 argument = cp_parser_type_id (parser);
8381 /* If the next token isn't a `,' or a `>', then this argument wasn't
8383 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
8384 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER))
8385 cp_parser_error (parser, "expected template-argument");
8386 /* If that worked, we're done. */
8387 if (cp_parser_parse_definitely (parser))
8389 /* We're still not sure what the argument will be. */
8390 cp_parser_parse_tentatively (parser);
8391 /* Try a template. */
8392 argument = cp_parser_id_expression (parser,
8393 /*template_keyword_p=*/false,
8394 /*check_dependency_p=*/true,
8396 /* If the next token isn't a `,' or a `>', then this argument wasn't
8398 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
8399 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER))
8400 cp_parser_error (parser, "expected template-argument");
8401 if (!cp_parser_error_occurred (parser))
8403 /* Figure out what is being referred to. */
8404 argument = cp_parser_lookup_name_simple (parser, argument);
8406 argument = make_unbound_class_template (TREE_OPERAND (argument, 0),
8407 TREE_OPERAND (argument, 1),
8408 tf_error | tf_parsing);
8409 else if (TREE_CODE (argument) != TEMPLATE_DECL)
8410 cp_parser_error (parser, "expected template-name");
8412 if (cp_parser_parse_definitely (parser))
8414 /* It must be an assignment-expression. */
8415 return cp_parser_assignment_expression (parser);
8418 /* Parse an explicit-instantiation.
8420 explicit-instantiation:
8421 template declaration
8423 Although the standard says `declaration', what it really means is:
8425 explicit-instantiation:
8426 template decl-specifier-seq [opt] declarator [opt] ;
8428 Things like `template int S<int>::i = 5, int S<double>::j;' are not
8429 supposed to be allowed. A defect report has been filed about this
8434 explicit-instantiation:
8435 storage-class-specifier template
8436 decl-specifier-seq [opt] declarator [opt] ;
8437 function-specifier template
8438 decl-specifier-seq [opt] declarator [opt] ; */
8441 cp_parser_explicit_instantiation (parser)
8444 bool declares_class_or_enum;
8445 tree decl_specifiers;
8447 tree extension_specifier = NULL_TREE;
8449 /* Look for an (optional) storage-class-specifier or
8450 function-specifier. */
8451 if (cp_parser_allow_gnu_extensions_p (parser))
8454 = cp_parser_storage_class_specifier_opt (parser);
8455 if (!extension_specifier)
8456 extension_specifier = cp_parser_function_specifier_opt (parser);
8459 /* Look for the `template' keyword. */
8460 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
8461 /* Let the front end know that we are processing an explicit
8463 begin_explicit_instantiation ();
8464 /* [temp.explicit] says that we are supposed to ignore access
8465 control while processing explicit instantiation directives. */
8466 scope_chain->check_access = 0;
8467 /* Parse a decl-specifier-seq. */
8469 = cp_parser_decl_specifier_seq (parser,
8470 CP_PARSER_FLAGS_OPTIONAL,
8472 &declares_class_or_enum);
8473 /* If there was exactly one decl-specifier, and it declared a class,
8474 and there's no declarator, then we have an explicit type
8476 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
8480 type = check_tag_decl (decl_specifiers);
8482 do_type_instantiation (type, extension_specifier, /*complain=*/1);
8489 /* Parse the declarator. */
8491 = cp_parser_declarator (parser,
8492 /*abstract_p=*/false,
8493 /*ctor_dtor_or_conv_p=*/NULL);
8494 decl = grokdeclarator (declarator, decl_specifiers,
8496 /* Do the explicit instantiation. */
8497 do_decl_instantiation (decl, extension_specifier);
8499 /* We're done with the instantiation. */
8500 end_explicit_instantiation ();
8501 /* Trun access control back on. */
8502 scope_chain->check_access = flag_access_control;
8504 /* Look for the trailing `;'. */
8505 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
8508 /* Parse an explicit-specialization.
8510 explicit-specialization:
8511 template < > declaration
8513 Although the standard says `declaration', what it really means is:
8515 explicit-specialization:
8516 template <> decl-specifier [opt] init-declarator [opt] ;
8517 template <> function-definition
8518 template <> explicit-specialization
8519 template <> template-declaration */
8522 cp_parser_explicit_specialization (parser)
8525 /* Look for the `template' keyword. */
8526 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
8527 /* Look for the `<'. */
8528 cp_parser_require (parser, CPP_LESS, "`<'");
8529 /* Look for the `>'. */
8530 cp_parser_require (parser, CPP_GREATER, "`>'");
8531 /* We have processed another parameter list. */
8532 ++parser->num_template_parameter_lists;
8533 /* Let the front end know that we are beginning a specialization. */
8534 begin_specialization ();
8536 /* If the next keyword is `template', we need to figure out whether
8537 or not we're looking a template-declaration. */
8538 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
8540 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
8541 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
8542 cp_parser_template_declaration_after_export (parser,
8543 /*member_p=*/false);
8545 cp_parser_explicit_specialization (parser);
8548 /* Parse the dependent declaration. */
8549 cp_parser_single_declaration (parser,
8553 /* We're done with the specialization. */
8554 end_specialization ();
8555 /* We're done with this parameter list. */
8556 --parser->num_template_parameter_lists;
8559 /* Parse a type-specifier.
8562 simple-type-specifier
8565 elaborated-type-specifier
8573 Returns a representation of the type-specifier. If the
8574 type-specifier is a keyword (like `int' or `const', or
8575 `__complex__') then the correspoding IDENTIFIER_NODE is returned.
8576 For a class-specifier, enum-specifier, or elaborated-type-specifier
8577 a TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
8579 If IS_FRIEND is TRUE then this type-specifier is being declared a
8580 `friend'. If IS_DECLARATION is TRUE, then this type-specifier is
8581 appearing in a decl-specifier-seq.
8583 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
8584 class-specifier, enum-specifier, or elaborated-type-specifier, then
8585 *DECLARES_CLASS_OR_ENUM is set to TRUE. Otherwise, it is set to
8588 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
8589 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
8593 cp_parser_type_specifier (parser,
8597 declares_class_or_enum,
8600 cp_parser_flags flags;
8602 bool is_declaration;
8603 bool *declares_class_or_enum;
8604 bool *is_cv_qualifier;
8606 tree type_spec = NULL_TREE;
8610 /* Assume this type-specifier does not declare a new type. */
8611 if (declares_class_or_enum)
8612 *declares_class_or_enum = false;
8613 /* And that it does not specify a cv-qualifier. */
8614 if (is_cv_qualifier)
8615 *is_cv_qualifier = false;
8616 /* Peek at the next token. */
8617 token = cp_lexer_peek_token (parser->lexer);
8619 /* If we're looking at a keyword, we can use that to guide the
8620 production we choose. */
8621 keyword = token->keyword;
8624 /* Any of these indicate either a class-specifier, or an
8625 elaborated-type-specifier. */
8630 /* Parse tentatively so that we can back up if we don't find a
8631 class-specifier or enum-specifier. */
8632 cp_parser_parse_tentatively (parser);
8633 /* Look for the class-specifier or enum-specifier. */
8634 if (keyword == RID_ENUM)
8635 type_spec = cp_parser_enum_specifier (parser);
8637 type_spec = cp_parser_class_specifier (parser);
8639 /* If that worked, we're done. */
8640 if (cp_parser_parse_definitely (parser))
8642 if (declares_class_or_enum)
8643 *declares_class_or_enum = true;
8650 /* Look for an elaborated-type-specifier. */
8651 type_spec = cp_parser_elaborated_type_specifier (parser,
8654 /* We're declaring a class or enum -- unless we're using
8656 if (declares_class_or_enum && keyword != RID_TYPENAME)
8657 *declares_class_or_enum = true;
8663 type_spec = cp_parser_cv_qualifier_opt (parser);
8664 /* Even though we call a routine that looks for an optional
8665 qualifier, we know that there should be one. */
8666 my_friendly_assert (type_spec != NULL, 20000328);
8667 /* This type-specifier was a cv-qualified. */
8668 if (is_cv_qualifier)
8669 *is_cv_qualifier = true;
8674 /* The `__complex__' keyword is a GNU extension. */
8675 return cp_lexer_consume_token (parser->lexer)->value;
8681 /* If we do not already have a type-specifier, assume we are looking
8682 at a simple-type-specifier. */
8683 type_spec = cp_parser_simple_type_specifier (parser, flags);
8685 /* If we didn't find a type-specifier, and a type-specifier was not
8686 optional in this context, issue an error message. */
8687 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
8689 cp_parser_error (parser, "expected type specifier");
8690 return error_mark_node;
8696 /* Parse a simple-type-specifier.
8698 simple-type-specifier:
8699 :: [opt] nested-name-specifier [opt] type-name
8700 :: [opt] nested-name-specifier template template-id
8715 simple-type-specifier:
8716 __typeof__ unary-expression
8717 __typeof__ ( type-id )
8719 For the various keywords, the value returned is simply the
8720 TREE_IDENTIFIER representing the keyword. For the first two
8721 productions, the value returned is the indicated TYPE_DECL. */
8724 cp_parser_simple_type_specifier (parser, flags)
8726 cp_parser_flags flags;
8728 tree type = NULL_TREE;
8731 /* Peek at the next token. */
8732 token = cp_lexer_peek_token (parser->lexer);
8734 /* If we're looking at a keyword, things are easy. */
8735 switch (token->keyword)
8748 /* Consume the token. */
8749 return cp_lexer_consume_token (parser->lexer)->value;
8755 /* Consume the `typeof' token. */
8756 cp_lexer_consume_token (parser->lexer);
8757 /* Parse the operand to `typeof' */
8758 operand = cp_parser_sizeof_operand (parser, RID_TYPEOF);
8759 /* If it is not already a TYPE, take its type. */
8760 if (!TYPE_P (operand))
8761 operand = finish_typeof (operand);
8770 /* The type-specifier must be a user-defined type. */
8771 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
8773 /* Don't gobble tokens or issue error messages if this is an
8774 optional type-specifier. */
8775 if (flags & CP_PARSER_FLAGS_OPTIONAL)
8776 cp_parser_parse_tentatively (parser);
8778 /* Look for the optional `::' operator. */
8779 cp_parser_global_scope_opt (parser,
8780 /*current_scope_valid_p=*/false);
8781 /* Look for the nested-name specifier. */
8782 cp_parser_nested_name_specifier_opt (parser,
8783 /*typename_keyword_p=*/false,
8784 /*check_dependency_p=*/true,
8786 /* If we have seen a nested-name-specifier, and the next token
8787 is `template', then we are using the template-id production. */
8789 && cp_parser_optional_template_keyword (parser))
8791 /* Look for the template-id. */
8792 type = cp_parser_template_id (parser,
8793 /*template_keyword_p=*/true,
8794 /*check_dependency_p=*/true);
8795 /* If the template-id did not name a type, we are out of
8797 if (TREE_CODE (type) != TYPE_DECL)
8799 cp_parser_error (parser, "expected template-id for type");
8803 /* Otherwise, look for a type-name. */
8806 type = cp_parser_type_name (parser);
8807 if (type == error_mark_node)
8811 /* If it didn't work out, we don't have a TYPE. */
8812 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
8813 && !cp_parser_parse_definitely (parser))
8817 /* If we didn't get a type-name, issue an error message. */
8818 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
8820 cp_parser_error (parser, "expected type-name");
8821 return error_mark_node;
8827 /* Parse a type-name.
8840 Returns a TYPE_DECL for the the type. */
8843 cp_parser_type_name (parser)
8849 /* We can't know yet whether it is a class-name or not. */
8850 cp_parser_parse_tentatively (parser);
8851 /* Try a class-name. */
8852 type_decl = cp_parser_class_name (parser,
8853 /*typename_keyword_p=*/false,
8854 /*template_keyword_p=*/false,
8856 /*check_access_p=*/true,
8857 /*check_dependency_p=*/true,
8858 /*class_head_p=*/false);
8859 /* If it's not a class-name, keep looking. */
8860 if (!cp_parser_parse_definitely (parser))
8862 /* It must be a typedef-name or an enum-name. */
8863 identifier = cp_parser_identifier (parser);
8864 if (identifier == error_mark_node)
8865 return error_mark_node;
8867 /* Look up the type-name. */
8868 type_decl = cp_parser_lookup_name_simple (parser, identifier);
8869 /* Issue an error if we did not find a type-name. */
8870 if (TREE_CODE (type_decl) != TYPE_DECL)
8872 cp_parser_error (parser, "expected type-name");
8873 type_decl = error_mark_node;
8875 /* Remember that the name was used in the definition of the
8876 current class so that we can check later to see if the
8877 meaning would have been different after the class was
8878 entirely defined. */
8879 else if (type_decl != error_mark_node
8881 maybe_note_name_used_in_class (identifier, type_decl);
8888 /* Parse an elaborated-type-specifier. Note that the grammar given
8889 here incorporates the resolution to DR68.
8891 elaborated-type-specifier:
8892 class-key :: [opt] nested-name-specifier [opt] identifier
8893 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
8894 enum :: [opt] nested-name-specifier [opt] identifier
8895 typename :: [opt] nested-name-specifier identifier
8896 typename :: [opt] nested-name-specifier template [opt]
8899 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
8900 declared `friend'. If IS_DECLARATION is TRUE, then this
8901 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
8902 something is being declared.
8904 Returns the TYPE specified. */
8907 cp_parser_elaborated_type_specifier (parser, is_friend, is_declaration)
8910 bool is_declaration;
8912 enum tag_types tag_type;
8914 tree type = NULL_TREE;
8916 /* See if we're looking at the `enum' keyword. */
8917 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
8919 /* Consume the `enum' token. */
8920 cp_lexer_consume_token (parser->lexer);
8921 /* Remember that it's an enumeration type. */
8922 tag_type = enum_type;
8924 /* Or, it might be `typename'. */
8925 else if (cp_lexer_next_token_is_keyword (parser->lexer,
8928 /* Consume the `typename' token. */
8929 cp_lexer_consume_token (parser->lexer);
8930 /* Remember that it's a `typename' type. */
8931 tag_type = typename_type;
8932 /* The `typename' keyword is only allowed in templates. */
8933 if (!processing_template_decl)
8934 pedwarn ("using `typename' outside of template");
8936 /* Otherwise it must be a class-key. */
8939 tag_type = cp_parser_class_key (parser);
8940 if (tag_type == none_type)
8941 return error_mark_node;
8944 /* Look for the `::' operator. */
8945 cp_parser_global_scope_opt (parser,
8946 /*current_scope_valid_p=*/false);
8947 /* Look for the nested-name-specifier. */
8948 if (tag_type == typename_type)
8949 cp_parser_nested_name_specifier (parser,
8950 /*typename_keyword_p=*/true,
8951 /*check_dependency_p=*/true,
8954 /* Even though `typename' is not present, the proposed resolution
8955 to Core Issue 180 says that in `class A<T>::B', `B' should be
8956 considered a type-name, even if `A<T>' is dependent. */
8957 cp_parser_nested_name_specifier_opt (parser,
8958 /*typename_keyword_p=*/true,
8959 /*check_dependency_p=*/true,
8961 /* For everything but enumeration types, consider a template-id. */
8962 if (tag_type != enum_type)
8964 bool template_p = false;
8967 /* Allow the `template' keyword. */
8968 template_p = cp_parser_optional_template_keyword (parser);
8969 /* If we didn't see `template', we don't know if there's a
8970 template-id or not. */
8972 cp_parser_parse_tentatively (parser);
8973 /* Parse the template-id. */
8974 decl = cp_parser_template_id (parser, template_p,
8975 /*check_dependency_p=*/true);
8976 /* If we didn't find a template-id, look for an ordinary
8978 if (!template_p && !cp_parser_parse_definitely (parser))
8980 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
8981 in effect, then we must assume that, upon instantiation, the
8982 template will correspond to a class. */
8983 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
8984 && tag_type == typename_type)
8985 type = make_typename_type (parser->scope, decl,
8988 type = TREE_TYPE (decl);
8991 /* For an enumeration type, consider only a plain identifier. */
8994 identifier = cp_parser_identifier (parser);
8996 if (identifier == error_mark_node)
8997 return error_mark_node;
8999 /* For a `typename', we needn't call xref_tag. */
9000 if (tag_type == typename_type)
9001 return make_typename_type (parser->scope, identifier,
9003 /* Look up a qualified name in the usual way. */
9008 /* In an elaborated-type-specifier, names are assumed to name
9009 types, so we set IS_TYPE to TRUE when calling
9010 cp_parser_lookup_name. */
9011 decl = cp_parser_lookup_name (parser, identifier,
9012 /*check_access=*/true,
9014 /*is_namespace=*/false,
9015 /*check_dependency=*/true);
9016 decl = (cp_parser_maybe_treat_template_as_class
9017 (decl, /*tag_name_p=*/is_friend));
9019 if (TREE_CODE (decl) != TYPE_DECL)
9021 error ("expected type-name");
9022 return error_mark_node;
9024 else if (TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE
9025 && tag_type != enum_type)
9026 error ("`%T' referred to as `%s'", TREE_TYPE (decl),
9027 tag_type == record_type ? "struct" : "class");
9028 else if (TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE
9029 && tag_type == enum_type)
9030 error ("`%T' referred to as enum", TREE_TYPE (decl));
9032 type = TREE_TYPE (decl);
9036 /* An elaborated-type-specifier sometimes introduces a new type and
9037 sometimes names an existing type. Normally, the rule is that it
9038 introduces a new type only if there is not an existing type of
9039 the same name already in scope. For example, given:
9042 void f() { struct S s; }
9044 the `struct S' in the body of `f' is the same `struct S' as in
9045 the global scope; the existing definition is used. However, if
9046 there were no global declaration, this would introduce a new
9047 local class named `S'.
9049 An exception to this rule applies to the following code:
9051 namespace N { struct S; }
9053 Here, the elaborated-type-specifier names a new type
9054 unconditionally; even if there is already an `S' in the
9055 containing scope this declaration names a new type.
9056 This exception only applies if the elaborated-type-specifier
9057 forms the complete declaration:
9061 A declaration consisting solely of `class-key identifier ;' is
9062 either a redeclaration of the name in the current scope or a
9063 forward declaration of the identifier as a class name. It
9064 introduces the name into the current scope.
9066 We are in this situation precisely when the next token is a `;'.
9068 An exception to the exception is that a `friend' declaration does
9069 *not* name a new type; i.e., given:
9071 struct S { friend struct T; };
9073 `T' is not a new type in the scope of `S'.
9075 Also, `new struct S' or `sizeof (struct S)' never results in the
9076 definition of a new type; a new type can only be declared in a
9077 declaration context. */
9079 type = xref_tag (tag_type, identifier,
9080 /*attributes=*/NULL_TREE,
9083 || cp_lexer_next_token_is_not (parser->lexer,
9087 if (tag_type != enum_type)
9088 cp_parser_check_class_key (tag_type, type);
9092 /* Parse an enum-specifier.
9095 enum identifier [opt] { enumerator-list [opt] }
9097 Returns an ENUM_TYPE representing the enumeration. */
9100 cp_parser_enum_specifier (parser)
9104 tree identifier = NULL_TREE;
9107 /* Look for the `enum' keyword. */
9108 if (!cp_parser_require_keyword (parser, RID_ENUM, "`enum'"))
9109 return error_mark_node;
9110 /* Peek at the next token. */
9111 token = cp_lexer_peek_token (parser->lexer);
9113 /* See if it is an identifier. */
9114 if (token->type == CPP_NAME)
9115 identifier = cp_parser_identifier (parser);
9117 /* Look for the `{'. */
9118 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
9119 return error_mark_node;
9121 /* At this point, we're going ahead with the enum-specifier, even
9122 if some other problem occurs. */
9123 cp_parser_commit_to_tentative_parse (parser);
9125 /* Issue an error message if type-definitions are forbidden here. */
9126 cp_parser_check_type_definition (parser);
9128 /* Create the new type. */
9129 type = start_enum (identifier ? identifier : make_anon_name ());
9131 /* Peek at the next token. */
9132 token = cp_lexer_peek_token (parser->lexer);
9133 /* If it's not a `}', then there are some enumerators. */
9134 if (token->type != CPP_CLOSE_BRACE)
9135 cp_parser_enumerator_list (parser, type);
9136 /* Look for the `}'. */
9137 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
9139 /* Finish up the enumeration. */
9145 /* Parse an enumerator-list. The enumerators all have the indicated
9149 enumerator-definition
9150 enumerator-list , enumerator-definition */
9153 cp_parser_enumerator_list (parser, type)
9161 /* Parse an enumerator-definition. */
9162 cp_parser_enumerator_definition (parser, type);
9163 /* Peek at the next token. */
9164 token = cp_lexer_peek_token (parser->lexer);
9165 /* If it's not a `,', then we've reached the end of the
9167 if (token->type != CPP_COMMA)
9169 /* Otherwise, consume the `,' and keep going. */
9170 cp_lexer_consume_token (parser->lexer);
9171 /* If the next token is a `}', there is a trailing comma. */
9172 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
9174 if (pedantic && !in_system_header)
9175 pedwarn ("comma at end of enumerator list");
9181 /* Parse an enumerator-definition. The enumerator has the indicated
9184 enumerator-definition:
9186 enumerator = constant-expression
9192 cp_parser_enumerator_definition (parser, type)
9200 /* Look for the identifier. */
9201 identifier = cp_parser_identifier (parser);
9202 if (identifier == error_mark_node)
9205 /* Peek at the next token. */
9206 token = cp_lexer_peek_token (parser->lexer);
9207 /* If it's an `=', then there's an explicit value. */
9208 if (token->type == CPP_EQ)
9210 /* Consume the `=' token. */
9211 cp_lexer_consume_token (parser->lexer);
9212 /* Parse the value. */
9213 value = cp_parser_constant_expression (parser);
9218 /* Create the enumerator. */
9219 build_enumerator (identifier, value, type);
9222 /* Parse a namespace-name.
9225 original-namespace-name
9228 Returns the NAMESPACE_DECL for the namespace. */
9231 cp_parser_namespace_name (parser)
9235 tree namespace_decl;
9237 /* Get the name of the namespace. */
9238 identifier = cp_parser_identifier (parser);
9239 if (identifier == error_mark_node)
9240 return error_mark_node;
9242 /* Look up the identifier in the currently active scope. Look only
9243 for namespaces, due to:
9247 When looking up a namespace-name in a using-directive or alias
9248 definition, only namespace names are considered.
9254 During the lookup of a name preceding the :: scope resolution
9255 operator, object, function, and enumerator names are ignored.
9257 (Note that cp_parser_class_or_namespace_name only calls this
9258 function if the token after the name is the scope resolution
9260 namespace_decl = cp_parser_lookup_name (parser, identifier,
9261 /*check_access=*/true,
9263 /*is_namespace=*/true,
9264 /*check_dependency=*/true);
9265 /* If it's not a namespace, issue an error. */
9266 if (namespace_decl == error_mark_node
9267 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
9269 cp_parser_error (parser, "expected namespace-name");
9270 namespace_decl = error_mark_node;
9273 return namespace_decl;
9276 /* Parse a namespace-definition.
9278 namespace-definition:
9279 named-namespace-definition
9280 unnamed-namespace-definition
9282 named-namespace-definition:
9283 original-namespace-definition
9284 extension-namespace-definition
9286 original-namespace-definition:
9287 namespace identifier { namespace-body }
9289 extension-namespace-definition:
9290 namespace original-namespace-name { namespace-body }
9292 unnamed-namespace-definition:
9293 namespace { namespace-body } */
9296 cp_parser_namespace_definition (parser)
9301 /* Look for the `namespace' keyword. */
9302 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
9304 /* Get the name of the namespace. We do not attempt to distinguish
9305 between an original-namespace-definition and an
9306 extension-namespace-definition at this point. The semantic
9307 analysis routines are responsible for that. */
9308 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
9309 identifier = cp_parser_identifier (parser);
9311 identifier = NULL_TREE;
9313 /* Look for the `{' to start the namespace. */
9314 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
9315 /* Start the namespace. */
9316 push_namespace (identifier);
9317 /* Parse the body of the namespace. */
9318 cp_parser_namespace_body (parser);
9319 /* Finish the namespace. */
9321 /* Look for the final `}'. */
9322 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
9325 /* Parse a namespace-body.
9328 declaration-seq [opt] */
9331 cp_parser_namespace_body (parser)
9334 cp_parser_declaration_seq_opt (parser);
9337 /* Parse a namespace-alias-definition.
9339 namespace-alias-definition:
9340 namespace identifier = qualified-namespace-specifier ; */
9343 cp_parser_namespace_alias_definition (parser)
9347 tree namespace_specifier;
9349 /* Look for the `namespace' keyword. */
9350 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
9351 /* Look for the identifier. */
9352 identifier = cp_parser_identifier (parser);
9353 if (identifier == error_mark_node)
9355 /* Look for the `=' token. */
9356 cp_parser_require (parser, CPP_EQ, "`='");
9357 /* Look for the qualified-namespace-specifier. */
9359 = cp_parser_qualified_namespace_specifier (parser);
9360 /* Look for the `;' token. */
9361 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
9363 /* Register the alias in the symbol table. */
9364 do_namespace_alias (identifier, namespace_specifier);
9367 /* Parse a qualified-namespace-specifier.
9369 qualified-namespace-specifier:
9370 :: [opt] nested-name-specifier [opt] namespace-name
9372 Returns a NAMESPACE_DECL corresponding to the specified
9376 cp_parser_qualified_namespace_specifier (parser)
9379 /* Look for the optional `::'. */
9380 cp_parser_global_scope_opt (parser,
9381 /*current_scope_valid_p=*/false);
9383 /* Look for the optional nested-name-specifier. */
9384 cp_parser_nested_name_specifier_opt (parser,
9385 /*typename_keyword_p=*/false,
9386 /*check_dependency_p=*/true,
9389 return cp_parser_namespace_name (parser);
9392 /* Parse a using-declaration.
9395 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
9396 using :: unqualified-id ; */
9399 cp_parser_using_declaration (parser)
9403 bool typename_p = false;
9404 bool global_scope_p;
9409 /* Look for the `using' keyword. */
9410 cp_parser_require_keyword (parser, RID_USING, "`using'");
9412 /* Peek at the next token. */
9413 token = cp_lexer_peek_token (parser->lexer);
9414 /* See if it's `typename'. */
9415 if (token->keyword == RID_TYPENAME)
9417 /* Remember that we've seen it. */
9419 /* Consume the `typename' token. */
9420 cp_lexer_consume_token (parser->lexer);
9423 /* Look for the optional global scope qualification. */
9425 = (cp_parser_global_scope_opt (parser,
9426 /*current_scope_valid_p=*/false)
9429 /* If we saw `typename', or didn't see `::', then there must be a
9430 nested-name-specifier present. */
9431 if (typename_p || !global_scope_p)
9432 cp_parser_nested_name_specifier (parser, typename_p,
9433 /*check_dependency_p=*/true,
9435 /* Otherwise, we could be in either of the two productions. In that
9436 case, treat the nested-name-specifier as optional. */
9438 cp_parser_nested_name_specifier_opt (parser,
9439 /*typename_keyword_p=*/false,
9440 /*check_dependency_p=*/true,
9443 /* Parse the unqualified-id. */
9444 identifier = cp_parser_unqualified_id (parser,
9445 /*template_keyword_p=*/false,
9446 /*check_dependency_p=*/true);
9448 /* The function we call to handle a using-declaration is different
9449 depending on what scope we are in. */
9450 scope = current_scope ();
9451 if (scope && TYPE_P (scope))
9453 /* Create the USING_DECL. */
9454 decl = do_class_using_decl (build_nt (SCOPE_REF,
9457 /* Add it to the list of members in this class. */
9458 finish_member_declaration (decl);
9462 decl = cp_parser_lookup_name_simple (parser, identifier);
9464 do_local_using_decl (decl);
9466 do_toplevel_using_decl (decl);
9469 /* Look for the final `;'. */
9470 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
9473 /* Parse a using-directive.
9476 using namespace :: [opt] nested-name-specifier [opt]
9480 cp_parser_using_directive (parser)
9483 tree namespace_decl;
9485 /* Look for the `using' keyword. */
9486 cp_parser_require_keyword (parser, RID_USING, "`using'");
9487 /* And the `namespace' keyword. */
9488 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
9489 /* Look for the optional `::' operator. */
9490 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
9491 /* And the optional nested-name-sepcifier. */
9492 cp_parser_nested_name_specifier_opt (parser,
9493 /*typename_keyword_p=*/false,
9494 /*check_dependency_p=*/true,
9496 /* Get the namespace being used. */
9497 namespace_decl = cp_parser_namespace_name (parser);
9498 /* Update the symbol table. */
9499 do_using_directive (namespace_decl);
9500 /* Look for the final `;'. */
9501 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
9504 /* Parse an asm-definition.
9507 asm ( string-literal ) ;
9512 asm volatile [opt] ( string-literal ) ;
9513 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
9514 asm volatile [opt] ( string-literal : asm-operand-list [opt]
9515 : asm-operand-list [opt] ) ;
9516 asm volatile [opt] ( string-literal : asm-operand-list [opt]
9517 : asm-operand-list [opt]
9518 : asm-operand-list [opt] ) ; */
9521 cp_parser_asm_definition (parser)
9526 tree outputs = NULL_TREE;
9527 tree inputs = NULL_TREE;
9528 tree clobbers = NULL_TREE;
9530 bool volatile_p = false;
9531 bool extended_p = false;
9533 /* Look for the `asm' keyword. */
9534 cp_parser_require_keyword (parser, RID_ASM, "`asm'");
9535 /* See if the next token is `volatile'. */
9536 if (cp_parser_allow_gnu_extensions_p (parser)
9537 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
9539 /* Remember that we saw the `volatile' keyword. */
9541 /* Consume the token. */
9542 cp_lexer_consume_token (parser->lexer);
9544 /* Look for the opening `('. */
9545 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
9546 /* Look for the string. */
9547 token = cp_parser_require (parser, CPP_STRING, "asm body");
9550 string = token->value;
9551 /* If we're allowing GNU extensions, check for the extended assembly
9552 syntax. Unfortunately, the `:' tokens need not be separated by
9553 a space in C, and so, for compatibility, we tolerate that here
9554 too. Doing that means that we have to treat the `::' operator as
9556 if (cp_parser_allow_gnu_extensions_p (parser)
9557 && at_function_scope_p ()
9558 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
9559 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
9561 bool inputs_p = false;
9562 bool clobbers_p = false;
9564 /* The extended syntax was used. */
9567 /* Look for outputs. */
9568 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
9570 /* Consume the `:'. */
9571 cp_lexer_consume_token (parser->lexer);
9572 /* Parse the output-operands. */
9573 if (cp_lexer_next_token_is_not (parser->lexer,
9575 && cp_lexer_next_token_is_not (parser->lexer,
9577 outputs = cp_parser_asm_operand_list (parser);
9579 /* If the next token is `::', there are no outputs, and the
9580 next token is the beginning of the inputs. */
9581 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
9583 /* Consume the `::' token. */
9584 cp_lexer_consume_token (parser->lexer);
9585 /* The inputs are coming next. */
9589 /* Look for inputs. */
9591 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
9594 /* Consume the `:'. */
9595 cp_lexer_consume_token (parser->lexer);
9596 /* Parse the output-operands. */
9597 if (cp_lexer_next_token_is_not (parser->lexer,
9599 && cp_lexer_next_token_is_not (parser->lexer,
9601 inputs = cp_parser_asm_operand_list (parser);
9603 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
9604 /* The clobbers are coming next. */
9607 /* Look for clobbers. */
9609 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
9612 /* Consume the `:'. */
9613 cp_lexer_consume_token (parser->lexer);
9614 /* Parse the clobbers. */
9615 clobbers = cp_parser_asm_clobber_list (parser);
9618 /* Look for the closing `)'. */
9619 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
9620 cp_parser_skip_to_closing_parenthesis (parser);
9621 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
9623 /* Create the ASM_STMT. */
9624 if (at_function_scope_p ())
9627 finish_asm_stmt (volatile_p
9628 ? ridpointers[(int) RID_VOLATILE] : NULL_TREE,
9629 string, outputs, inputs, clobbers);
9630 /* If the extended syntax was not used, mark the ASM_STMT. */
9632 ASM_INPUT_P (asm_stmt) = 1;
9635 assemble_asm (string);
9638 /* Declarators [gram.dcl.decl] */
9640 /* Parse an init-declarator.
9643 declarator initializer [opt]
9648 declarator asm-specification [opt] attributes [opt] initializer [opt]
9650 The DECL_SPECIFIERS and PREFIX_ATTRIBUTES apply to this declarator.
9651 Returns a reprsentation of the entity declared. The ACCESS_CHECKS
9652 represent deferred access checks from the decl-specifier-seq. If
9653 MEMBER_P is TRUE, then this declarator appears in a class scope.
9654 The new DECL created by this declarator is returned.
9656 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
9657 for a function-definition here as well. If the declarator is a
9658 declarator for a function-definition, *FUNCTION_DEFINITION_P will
9659 be TRUE upon return. By that point, the function-definition will
9660 have been completely parsed.
9662 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
9666 cp_parser_init_declarator (parser,
9670 function_definition_allowed_p,
9672 function_definition_p)
9674 tree decl_specifiers;
9675 tree prefix_attributes;
9677 bool function_definition_allowed_p;
9679 bool *function_definition_p;
9684 tree asm_specification;
9686 tree decl = NULL_TREE;
9688 tree declarator_access_checks;
9689 bool is_initialized;
9690 bool is_parenthesized_init;
9691 bool ctor_dtor_or_conv_p;
9694 /* Assume that this is not the declarator for a function
9696 if (function_definition_p)
9697 *function_definition_p = false;
9699 /* Defer access checks while parsing the declarator; we cannot know
9700 what names are accessible until we know what is being
9702 cp_parser_start_deferring_access_checks (parser);
9703 /* Parse the declarator. */
9705 = cp_parser_declarator (parser,
9706 /*abstract_p=*/false,
9707 &ctor_dtor_or_conv_p);
9708 /* Gather up the deferred checks. */
9709 declarator_access_checks
9710 = cp_parser_stop_deferring_access_checks (parser);
9712 /* If the DECLARATOR was erroneous, there's no need to go
9714 if (declarator == error_mark_node)
9715 return error_mark_node;
9717 /* Figure out what scope the entity declared by the DECLARATOR is
9718 located in. `grokdeclarator' sometimes changes the scope, so
9719 we compute it now. */
9720 scope = get_scope_of_declarator (declarator);
9722 /* If we're allowing GNU extensions, look for an asm-specification
9724 if (cp_parser_allow_gnu_extensions_p (parser))
9726 /* Look for an asm-specification. */
9727 asm_specification = cp_parser_asm_specification_opt (parser);
9728 /* And attributes. */
9729 attributes = cp_parser_attributes_opt (parser);
9733 asm_specification = NULL_TREE;
9734 attributes = NULL_TREE;
9737 /* Peek at the next token. */
9738 token = cp_lexer_peek_token (parser->lexer);
9739 /* Check to see if the token indicates the start of a
9740 function-definition. */
9741 if (cp_parser_token_starts_function_definition_p (token))
9743 if (!function_definition_allowed_p)
9745 /* If a function-definition should not appear here, issue an
9747 cp_parser_error (parser,
9748 "a function-definition is not allowed here");
9749 return error_mark_node;
9755 /* Neither attributes nor an asm-specification are allowed
9756 on a function-definition. */
9757 if (asm_specification)
9758 error ("an asm-specification is not allowed on a function-definition");
9760 error ("attributes are not allowed on a function-definition");
9761 /* This is a function-definition. */
9762 *function_definition_p = true;
9764 /* Thread the access checks together. */
9765 ac = &access_checks;
9767 ac = &TREE_CHAIN (*ac);
9768 *ac = declarator_access_checks;
9770 /* Parse the function definition. */
9771 decl = (cp_parser_function_definition_from_specifiers_and_declarator
9772 (parser, decl_specifiers, prefix_attributes, declarator,
9775 /* Pull the access-checks apart again. */
9784 Only in function declarations for constructors, destructors, and
9785 type conversions can the decl-specifier-seq be omitted.
9787 We explicitly postpone this check past the point where we handle
9788 function-definitions because we tolerate function-definitions
9789 that are missing their return types in some modes. */
9790 if (!decl_specifiers && !ctor_dtor_or_conv_p)
9792 cp_parser_error (parser,
9793 "expected constructor, destructor, or type conversion");
9794 return error_mark_node;
9797 /* An `=' or an `(' indicates an initializer. */
9798 is_initialized = (token->type == CPP_EQ
9799 || token->type == CPP_OPEN_PAREN);
9800 /* If the init-declarator isn't initialized and isn't followed by a
9801 `,' or `;', it's not a valid init-declarator. */
9803 && token->type != CPP_COMMA
9804 && token->type != CPP_SEMICOLON)
9806 cp_parser_error (parser, "expected init-declarator");
9807 return error_mark_node;
9810 /* Because start_decl has side-effects, we should only call it if we
9811 know we're going ahead. By this point, we know that we cannot
9812 possibly be looking at any other construct. */
9813 cp_parser_commit_to_tentative_parse (parser);
9815 /* Check to see whether or not this declaration is a friend. */
9816 friend_p = cp_parser_friend_p (decl_specifiers);
9818 /* Check that the number of template-parameter-lists is OK. */
9819 if (!cp_parser_check_declarator_template_parameters (parser,
9821 return error_mark_node;
9823 /* Enter the newly declared entry in the symbol table. If we're
9824 processing a declaration in a class-specifier, we wait until
9825 after processing the initializer. */
9828 if (parser->in_unbraced_linkage_specification_p)
9830 decl_specifiers = tree_cons (error_mark_node,
9831 get_identifier ("extern"),
9833 have_extern_spec = false;
9835 decl = start_decl (declarator,
9842 /* Enter the SCOPE. That way unqualified names appearing in the
9843 initializer will be looked up in SCOPE. */
9847 /* Perform deferred access control checks, now that we know in which
9848 SCOPE the declared entity resides. */
9849 if (!member_p && decl)
9851 tree saved_current_function_decl = NULL_TREE;
9853 /* If the entity being declared is a function, pretend that we
9854 are in its scope. If it is a `friend', it may have access to
9855 things that would not otherwise be accessible. */
9856 if (TREE_CODE (decl) == FUNCTION_DECL)
9858 saved_current_function_decl = current_function_decl;
9859 current_function_decl = decl;
9862 /* Perform the access control checks for the decl-specifiers. */
9863 cp_parser_perform_deferred_access_checks (access_checks);
9864 /* And for the declarator. */
9865 cp_parser_perform_deferred_access_checks (declarator_access_checks);
9867 /* Restore the saved value. */
9868 if (TREE_CODE (decl) == FUNCTION_DECL)
9869 current_function_decl = saved_current_function_decl;
9872 /* Parse the initializer. */
9874 initializer = cp_parser_initializer (parser,
9875 &is_parenthesized_init);
9878 initializer = NULL_TREE;
9879 is_parenthesized_init = false;
9882 /* The old parser allows attributes to appear after a parenthesized
9883 initializer. Mark Mitchell proposed removing this functionality
9884 on the GCC mailing lists on 2002-08-13. This parser accepts the
9885 attributes -- but ignores them. */
9886 if (cp_parser_allow_gnu_extensions_p (parser) && is_parenthesized_init)
9887 if (cp_parser_attributes_opt (parser))
9888 warning ("attributes after parenthesized initializer ignored");
9890 /* Leave the SCOPE, now that we have processed the initializer. It
9891 is important to do this before calling cp_finish_decl because it
9892 makes decisions about whether to create DECL_STMTs or not based
9893 on the current scope. */
9897 /* For an in-class declaration, use `grokfield' to create the
9900 decl = grokfield (declarator, decl_specifiers,
9901 initializer, /*asmspec=*/NULL_TREE,
9902 /*attributes=*/NULL_TREE);
9904 /* Finish processing the declaration. But, skip friend
9906 if (!friend_p && decl)
9907 cp_finish_decl (decl,
9910 /* If the initializer is in parentheses, then this is
9911 a direct-initialization, which means that an
9912 `explicit' constructor is OK. Otherwise, an
9913 `explicit' constructor cannot be used. */
9914 ((is_parenthesized_init || !is_initialized)
9915 ? 0 : LOOKUP_ONLYCONVERTING));
9920 /* Parse a declarator.
9924 ptr-operator declarator
9926 abstract-declarator:
9927 ptr-operator abstract-declarator [opt]
9928 direct-abstract-declarator
9933 attributes [opt] direct-declarator
9934 attributes [opt] ptr-operator declarator
9936 abstract-declarator:
9937 attributes [opt] ptr-operator abstract-declarator [opt]
9938 attributes [opt] direct-abstract-declarator
9940 Returns a representation of the declarator. If the declarator has
9941 the form `* declarator', then an INDIRECT_REF is returned, whose
9942 only operand is the sub-declarator. Analagously, `& declarator' is
9943 represented as an ADDR_EXPR. For `X::* declarator', a SCOPE_REF is
9944 used. The first operand is the TYPE for `X'. The second operand
9945 is an INDIRECT_REF whose operand is the sub-declarator.
9947 Otherwise, the reprsentation is as for a direct-declarator.
9949 (It would be better to define a structure type to represent
9950 declarators, rather than abusing `tree' nodes to represent
9951 declarators. That would be much clearer and save some memory.
9952 There is no reason for declarators to be garbage-collected, for
9953 example; they are created during parser and no longer needed after
9954 `grokdeclarator' has been called.)
9956 For a ptr-operator that has the optional cv-qualifier-seq,
9957 cv-qualifiers will be stored in the TREE_TYPE of the INDIRECT_REF
9960 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is set to
9961 true if this declarator represents a constructor, destructor, or
9962 type conversion operator. Otherwise, it is set to false.
9964 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
9965 a decl-specifier-seq unless it declares a constructor, destructor,
9966 or conversion. It might seem that we could check this condition in
9967 semantic analysis, rather than parsing, but that makes it difficult
9968 to handle something like `f()'. We want to notice that there are
9969 no decl-specifiers, and therefore realize that this is an
9970 expression, not a declaration.) */
9973 cp_parser_declarator (parser, abstract_p, ctor_dtor_or_conv_p)
9976 bool *ctor_dtor_or_conv_p;
9980 enum tree_code code;
9981 tree cv_qualifier_seq;
9983 tree attributes = NULL_TREE;
9985 /* Assume this is not a constructor, destructor, or type-conversion
9987 if (ctor_dtor_or_conv_p)
9988 *ctor_dtor_or_conv_p = false;
9990 if (cp_parser_allow_gnu_extensions_p (parser))
9991 attributes = cp_parser_attributes_opt (parser);
9993 /* Peek at the next token. */
9994 token = cp_lexer_peek_token (parser->lexer);
9996 /* Check for the ptr-operator production. */
9997 cp_parser_parse_tentatively (parser);
9998 /* Parse the ptr-operator. */
9999 code = cp_parser_ptr_operator (parser,
10001 &cv_qualifier_seq);
10002 /* If that worked, then we have a ptr-operator. */
10003 if (cp_parser_parse_definitely (parser))
10005 /* The dependent declarator is optional if we are parsing an
10006 abstract-declarator. */
10008 cp_parser_parse_tentatively (parser);
10010 /* Parse the dependent declarator. */
10011 declarator = cp_parser_declarator (parser, abstract_p,
10012 /*ctor_dtor_or_conv_p=*/NULL);
10014 /* If we are parsing an abstract-declarator, we must handle the
10015 case where the dependent declarator is absent. */
10016 if (abstract_p && !cp_parser_parse_definitely (parser))
10017 declarator = NULL_TREE;
10019 /* Build the representation of the ptr-operator. */
10020 if (code == INDIRECT_REF)
10021 declarator = make_pointer_declarator (cv_qualifier_seq,
10024 declarator = make_reference_declarator (cv_qualifier_seq,
10026 /* Handle the pointer-to-member case. */
10028 declarator = build_nt (SCOPE_REF, class_type, declarator);
10030 /* Everything else is a direct-declarator. */
10032 declarator = cp_parser_direct_declarator (parser,
10034 ctor_dtor_or_conv_p);
10036 if (attributes && declarator != error_mark_node)
10037 declarator = tree_cons (attributes, declarator, NULL_TREE);
10042 /* Parse a direct-declarator or direct-abstract-declarator.
10046 direct-declarator ( parameter-declaration-clause )
10047 cv-qualifier-seq [opt]
10048 exception-specification [opt]
10049 direct-declarator [ constant-expression [opt] ]
10052 direct-abstract-declarator:
10053 direct-abstract-declarator [opt]
10054 ( parameter-declaration-clause )
10055 cv-qualifier-seq [opt]
10056 exception-specification [opt]
10057 direct-abstract-declarator [opt] [ constant-expression [opt] ]
10058 ( abstract-declarator )
10060 Returns a representation of the declarator. ABSTRACT_P is TRUE if
10061 we are parsing a direct-abstract-declarator; FALSE if we are
10062 parsing a direct-declarator. CTOR_DTOR_OR_CONV_P is as for
10063 cp_parser_declarator.
10065 For the declarator-id production, the representation is as for an
10066 id-expression, except that a qualified name is represented as a
10067 SCOPE_REF. A function-declarator is represented as a CALL_EXPR;
10068 see the documentation of the FUNCTION_DECLARATOR_* macros for
10069 information about how to find the various declarator components.
10070 An array-declarator is represented as an ARRAY_REF. The
10071 direct-declarator is the first operand; the constant-expression
10072 indicating the size of the array is the second operand. */
10075 cp_parser_direct_declarator (parser, abstract_p, ctor_dtor_or_conv_p)
10078 bool *ctor_dtor_or_conv_p;
10082 tree scope = NULL_TREE;
10083 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
10084 bool saved_in_declarator_p = parser->in_declarator_p;
10086 /* Peek at the next token. */
10087 token = cp_lexer_peek_token (parser->lexer);
10088 /* Find the initial direct-declarator. It might be a parenthesized
10090 if (token->type == CPP_OPEN_PAREN)
10094 /* For an abstract declarator we do not know whether we are
10095 looking at the beginning of a parameter-declaration-clause,
10096 or at a parenthesized abstract declarator. For example, if
10097 we see `(int)', we are looking at a
10098 parameter-declaration-clause, and the
10099 direct-abstract-declarator has been omitted. If, on the
10100 other hand we are looking at `((*))' then we are looking at a
10101 parenthesized abstract-declarator. There is no easy way to
10102 tell which situation we are in. */
10104 cp_parser_parse_tentatively (parser);
10106 /* Consume the `('. */
10107 cp_lexer_consume_token (parser->lexer);
10108 /* Parse the nested declarator. */
10110 = cp_parser_declarator (parser, abstract_p, ctor_dtor_or_conv_p);
10111 /* Expect a `)'. */
10112 error_p = !cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
10114 /* If parsing a parenthesized abstract declarator didn't work,
10115 try a parameter-declaration-clause. */
10116 if (abstract_p && !cp_parser_parse_definitely (parser))
10117 declarator = NULL_TREE;
10118 /* If we were not parsing an abstract declarator, but failed to
10119 find a satisfactory nested declarator, then an error has
10121 else if (!abstract_p
10122 && (declarator == error_mark_node || error_p))
10123 return error_mark_node;
10124 /* Default args cannot appear in an abstract decl. */
10125 parser->default_arg_ok_p = false;
10127 /* Otherwise, for a non-abstract declarator, there should be a
10129 else if (!abstract_p)
10131 declarator = cp_parser_declarator_id (parser);
10133 if (TREE_CODE (declarator) == SCOPE_REF)
10135 scope = TREE_OPERAND (declarator, 0);
10137 /* In the declaration of a member of a template class
10138 outside of the class itself, the SCOPE will sometimes be
10139 a TYPENAME_TYPE. For example, given:
10141 template <typename T>
10142 int S<T>::R::i = 3;
10144 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In this
10145 context, we must resolve S<T>::R to an ordinary type,
10146 rather than a typename type.
10148 The reason we normally avoid resolving TYPENAME_TYPEs is
10149 that a specialization of `S' might render `S<T>::R' not a
10150 type. However, if `S' is specialized, then this `i' will
10151 not be used, so there is no harm in resolving the types
10153 if (TREE_CODE (scope) == TYPENAME_TYPE)
10155 /* Resolve the TYPENAME_TYPE. */
10156 scope = cp_parser_resolve_typename_type (parser, scope);
10157 /* If that failed, the declarator is invalid. */
10158 if (scope == error_mark_node)
10159 return error_mark_node;
10160 /* Build a new DECLARATOR. */
10161 declarator = build_nt (SCOPE_REF,
10163 TREE_OPERAND (declarator, 1));
10166 else if (TREE_CODE (declarator) != IDENTIFIER_NODE)
10167 /* Default args can only appear for a function decl. */
10168 parser->default_arg_ok_p = false;
10170 /* Check to see whether the declarator-id names a constructor,
10171 destructor, or conversion. */
10172 if (ctor_dtor_or_conv_p
10173 && ((TREE_CODE (declarator) == SCOPE_REF
10174 && CLASS_TYPE_P (TREE_OPERAND (declarator, 0)))
10175 || (TREE_CODE (declarator) != SCOPE_REF
10176 && at_class_scope_p ())))
10178 tree unqualified_name;
10181 /* Get the unqualified part of the name. */
10182 if (TREE_CODE (declarator) == SCOPE_REF)
10184 class_type = TREE_OPERAND (declarator, 0);
10185 unqualified_name = TREE_OPERAND (declarator, 1);
10189 class_type = current_class_type;
10190 unqualified_name = declarator;
10193 /* See if it names ctor, dtor or conv. */
10194 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR
10195 || IDENTIFIER_TYPENAME_P (unqualified_name)
10196 || constructor_name_p (unqualified_name, class_type))
10198 *ctor_dtor_or_conv_p = true;
10199 /* We would have cleared the default arg flag above, but
10201 parser->default_arg_ok_p = saved_default_arg_ok_p;
10205 /* But for an abstract declarator, the initial direct-declarator can
10209 declarator = NULL_TREE;
10210 parser->default_arg_ok_p = false;
10213 scope = get_scope_of_declarator (declarator);
10215 /* Any names that appear after the declarator-id for a member
10216 are looked up in the containing scope. */
10217 push_scope (scope);
10220 parser->in_declarator_p = true;
10222 /* Now, parse function-declarators and array-declarators until there
10226 /* Peek at the next token. */
10227 token = cp_lexer_peek_token (parser->lexer);
10228 /* If it's a `[', we're looking at an array-declarator. */
10229 if (token->type == CPP_OPEN_SQUARE)
10233 /* Consume the `['. */
10234 cp_lexer_consume_token (parser->lexer);
10235 /* Peek at the next token. */
10236 token = cp_lexer_peek_token (parser->lexer);
10237 /* If the next token is `]', then there is no
10238 constant-expression. */
10239 if (token->type != CPP_CLOSE_SQUARE)
10240 bounds = cp_parser_constant_expression (parser);
10242 bounds = NULL_TREE;
10243 /* Look for the closing `]'. */
10244 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
10246 declarator = build_nt (ARRAY_REF, declarator, bounds);
10248 /* If it's a `(', we're looking at a function-declarator. */
10249 else if (token->type == CPP_OPEN_PAREN)
10251 /* A function-declarator. Or maybe not. Consider, for
10257 The first is the declaration of a function while the
10258 second is a the definition of a variable, including its
10261 Having seen only the parenthesis, we cannot know which of
10262 these two alternatives should be selected. Even more
10263 complex are examples like:
10268 The former is a function-declaration; the latter is a
10269 variable initialization.
10271 First, we attempt to parse a parameter-declaration
10272 clause. If this works, then we continue; otherwise, we
10273 replace the tokens consumed in the process and continue. */
10276 /* We are now parsing tentatively. */
10277 cp_parser_parse_tentatively (parser);
10279 /* Consume the `('. */
10280 cp_lexer_consume_token (parser->lexer);
10281 /* Parse the parameter-declaration-clause. */
10282 params = cp_parser_parameter_declaration_clause (parser);
10284 /* If all went well, parse the cv-qualifier-seq and the
10285 exception-specification. */
10286 if (cp_parser_parse_definitely (parser))
10288 tree cv_qualifiers;
10289 tree exception_specification;
10291 /* Consume the `)'. */
10292 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
10294 /* Parse the cv-qualifier-seq. */
10295 cv_qualifiers = cp_parser_cv_qualifier_seq_opt (parser);
10296 /* And the exception-specification. */
10297 exception_specification
10298 = cp_parser_exception_specification_opt (parser);
10300 /* Create the function-declarator. */
10301 declarator = make_call_declarator (declarator,
10304 exception_specification);
10306 /* Otherwise, we must be done with the declarator. */
10310 /* Otherwise, we're done with the declarator. */
10313 /* Any subsequent parameter lists are to do with return type, so
10314 are not those of the declared function. */
10315 parser->default_arg_ok_p = false;
10318 /* For an abstract declarator, we might wind up with nothing at this
10319 point. That's an error; the declarator is not optional. */
10321 cp_parser_error (parser, "expected declarator");
10323 /* If we entered a scope, we must exit it now. */
10327 parser->default_arg_ok_p = saved_default_arg_ok_p;
10328 parser->in_declarator_p = saved_in_declarator_p;
10333 /* Parse a ptr-operator.
10336 * cv-qualifier-seq [opt]
10338 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
10343 & cv-qualifier-seq [opt]
10345 Returns INDIRECT_REF if a pointer, or pointer-to-member, was
10346 used. Returns ADDR_EXPR if a reference was used. In the
10347 case of a pointer-to-member, *TYPE is filled in with the
10348 TYPE containing the member. *CV_QUALIFIER_SEQ is filled in
10349 with the cv-qualifier-seq, or NULL_TREE, if there are no
10350 cv-qualifiers. Returns ERROR_MARK if an error occurred. */
10352 static enum tree_code
10353 cp_parser_ptr_operator (parser, type, cv_qualifier_seq)
10356 tree *cv_qualifier_seq;
10358 enum tree_code code = ERROR_MARK;
10361 /* Assume that it's not a pointer-to-member. */
10363 /* And that there are no cv-qualifiers. */
10364 *cv_qualifier_seq = NULL_TREE;
10366 /* Peek at the next token. */
10367 token = cp_lexer_peek_token (parser->lexer);
10368 /* If it's a `*' or `&' we have a pointer or reference. */
10369 if (token->type == CPP_MULT || token->type == CPP_AND)
10371 /* Remember which ptr-operator we were processing. */
10372 code = (token->type == CPP_AND ? ADDR_EXPR : INDIRECT_REF);
10374 /* Consume the `*' or `&'. */
10375 cp_lexer_consume_token (parser->lexer);
10377 /* A `*' can be followed by a cv-qualifier-seq, and so can a
10378 `&', if we are allowing GNU extensions. (The only qualifier
10379 that can legally appear after `&' is `restrict', but that is
10380 enforced during semantic analysis. */
10381 if (code == INDIRECT_REF
10382 || cp_parser_allow_gnu_extensions_p (parser))
10383 *cv_qualifier_seq = cp_parser_cv_qualifier_seq_opt (parser);
10387 /* Try the pointer-to-member case. */
10388 cp_parser_parse_tentatively (parser);
10389 /* Look for the optional `::' operator. */
10390 cp_parser_global_scope_opt (parser,
10391 /*current_scope_valid_p=*/false);
10392 /* Look for the nested-name specifier. */
10393 cp_parser_nested_name_specifier (parser,
10394 /*typename_keyword_p=*/false,
10395 /*check_dependency_p=*/true,
10397 /* If we found it, and the next token is a `*', then we are
10398 indeed looking at a pointer-to-member operator. */
10399 if (!cp_parser_error_occurred (parser)
10400 && cp_parser_require (parser, CPP_MULT, "`*'"))
10402 /* The type of which the member is a member is given by the
10404 *type = parser->scope;
10405 /* The next name will not be qualified. */
10406 parser->scope = NULL_TREE;
10407 parser->qualifying_scope = NULL_TREE;
10408 parser->object_scope = NULL_TREE;
10409 /* Indicate that the `*' operator was used. */
10410 code = INDIRECT_REF;
10411 /* Look for the optional cv-qualifier-seq. */
10412 *cv_qualifier_seq = cp_parser_cv_qualifier_seq_opt (parser);
10414 /* If that didn't work we don't have a ptr-operator. */
10415 if (!cp_parser_parse_definitely (parser))
10416 cp_parser_error (parser, "expected ptr-operator");
10422 /* Parse an (optional) cv-qualifier-seq.
10425 cv-qualifier cv-qualifier-seq [opt]
10427 Returns a TREE_LIST. The TREE_VALUE of each node is the
10428 representation of a cv-qualifier. */
10431 cp_parser_cv_qualifier_seq_opt (parser)
10434 tree cv_qualifiers = NULL_TREE;
10440 /* Look for the next cv-qualifier. */
10441 cv_qualifier = cp_parser_cv_qualifier_opt (parser);
10442 /* If we didn't find one, we're done. */
10446 /* Add this cv-qualifier to the list. */
10448 = tree_cons (NULL_TREE, cv_qualifier, cv_qualifiers);
10451 /* We built up the list in reverse order. */
10452 return nreverse (cv_qualifiers);
10455 /* Parse an (optional) cv-qualifier.
10467 cp_parser_cv_qualifier_opt (parser)
10471 tree cv_qualifier = NULL_TREE;
10473 /* Peek at the next token. */
10474 token = cp_lexer_peek_token (parser->lexer);
10475 /* See if it's a cv-qualifier. */
10476 switch (token->keyword)
10481 /* Save the value of the token. */
10482 cv_qualifier = token->value;
10483 /* Consume the token. */
10484 cp_lexer_consume_token (parser->lexer);
10491 return cv_qualifier;
10494 /* Parse a declarator-id.
10498 :: [opt] nested-name-specifier [opt] type-name
10500 In the `id-expression' case, the value returned is as for
10501 cp_parser_id_expression if the id-expression was an unqualified-id.
10502 If the id-expression was a qualified-id, then a SCOPE_REF is
10503 returned. The first operand is the scope (either a NAMESPACE_DECL
10504 or TREE_TYPE), but the second is still just a representation of an
10508 cp_parser_declarator_id (parser)
10511 tree id_expression;
10513 /* The expression must be an id-expression. Assume that qualified
10514 names are the names of types so that:
10517 int S<T>::R::i = 3;
10519 will work; we must treat `S<T>::R' as the name of a type.
10520 Similarly, assume that qualified names are templates, where
10524 int S<T>::R<T>::i = 3;
10527 id_expression = cp_parser_id_expression (parser,
10528 /*template_keyword_p=*/false,
10529 /*check_dependency_p=*/false,
10530 /*template_p=*/NULL);
10531 /* If the name was qualified, create a SCOPE_REF to represent
10534 id_expression = build_nt (SCOPE_REF, parser->scope, id_expression);
10536 return id_expression;
10539 /* Parse a type-id.
10542 type-specifier-seq abstract-declarator [opt]
10544 Returns the TYPE specified. */
10547 cp_parser_type_id (parser)
10550 tree type_specifier_seq;
10551 tree abstract_declarator;
10553 /* Parse the type-specifier-seq. */
10555 = cp_parser_type_specifier_seq (parser);
10556 if (type_specifier_seq == error_mark_node)
10557 return error_mark_node;
10559 /* There might or might not be an abstract declarator. */
10560 cp_parser_parse_tentatively (parser);
10561 /* Look for the declarator. */
10562 abstract_declarator
10563 = cp_parser_declarator (parser, /*abstract_p=*/true, NULL);
10564 /* Check to see if there really was a declarator. */
10565 if (!cp_parser_parse_definitely (parser))
10566 abstract_declarator = NULL_TREE;
10568 return groktypename (build_tree_list (type_specifier_seq,
10569 abstract_declarator));
10572 /* Parse a type-specifier-seq.
10574 type-specifier-seq:
10575 type-specifier type-specifier-seq [opt]
10579 type-specifier-seq:
10580 attributes type-specifier-seq [opt]
10582 Returns a TREE_LIST. Either the TREE_VALUE of each node is a
10583 type-specifier, or the TREE_PURPOSE is a list of attributes. */
10586 cp_parser_type_specifier_seq (parser)
10589 bool seen_type_specifier = false;
10590 tree type_specifier_seq = NULL_TREE;
10592 /* Parse the type-specifiers and attributes. */
10595 tree type_specifier;
10597 /* Check for attributes first. */
10598 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
10600 type_specifier_seq = tree_cons (cp_parser_attributes_opt (parser),
10602 type_specifier_seq);
10606 /* After the first type-specifier, others are optional. */
10607 if (seen_type_specifier)
10608 cp_parser_parse_tentatively (parser);
10609 /* Look for the type-specifier. */
10610 type_specifier = cp_parser_type_specifier (parser,
10611 CP_PARSER_FLAGS_NONE,
10612 /*is_friend=*/false,
10613 /*is_declaration=*/false,
10616 /* If the first type-specifier could not be found, this is not a
10617 type-specifier-seq at all. */
10618 if (!seen_type_specifier && type_specifier == error_mark_node)
10619 return error_mark_node;
10620 /* If subsequent type-specifiers could not be found, the
10621 type-specifier-seq is complete. */
10622 else if (seen_type_specifier && !cp_parser_parse_definitely (parser))
10625 /* Add the new type-specifier to the list. */
10627 = tree_cons (NULL_TREE, type_specifier, type_specifier_seq);
10628 seen_type_specifier = true;
10631 /* We built up the list in reverse order. */
10632 return nreverse (type_specifier_seq);
10635 /* Parse a parameter-declaration-clause.
10637 parameter-declaration-clause:
10638 parameter-declaration-list [opt] ... [opt]
10639 parameter-declaration-list , ...
10641 Returns a representation for the parameter declarations. Each node
10642 is a TREE_LIST. (See cp_parser_parameter_declaration for the exact
10643 representation.) If the parameter-declaration-clause ends with an
10644 ellipsis, PARMLIST_ELLIPSIS_P will hold of the first node in the
10645 list. A return value of NULL_TREE indicates a
10646 parameter-declaration-clause consisting only of an ellipsis. */
10649 cp_parser_parameter_declaration_clause (parser)
10656 /* Peek at the next token. */
10657 token = cp_lexer_peek_token (parser->lexer);
10658 /* Check for trivial parameter-declaration-clauses. */
10659 if (token->type == CPP_ELLIPSIS)
10661 /* Consume the `...' token. */
10662 cp_lexer_consume_token (parser->lexer);
10665 else if (token->type == CPP_CLOSE_PAREN)
10666 /* There are no parameters. */
10668 #ifndef NO_IMPLICIT_EXTERN_C
10669 if (in_system_header && current_class_type == NULL
10670 && current_lang_name == lang_name_c)
10674 return void_list_node;
10676 /* Check for `(void)', too, which is a special case. */
10677 else if (token->keyword == RID_VOID
10678 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
10679 == CPP_CLOSE_PAREN))
10681 /* Consume the `void' token. */
10682 cp_lexer_consume_token (parser->lexer);
10683 /* There are no parameters. */
10684 return void_list_node;
10687 /* Parse the parameter-declaration-list. */
10688 parameters = cp_parser_parameter_declaration_list (parser);
10689 /* If a parse error occurred while parsing the
10690 parameter-declaration-list, then the entire
10691 parameter-declaration-clause is erroneous. */
10692 if (parameters == error_mark_node)
10693 return error_mark_node;
10695 /* Peek at the next token. */
10696 token = cp_lexer_peek_token (parser->lexer);
10697 /* If it's a `,', the clause should terminate with an ellipsis. */
10698 if (token->type == CPP_COMMA)
10700 /* Consume the `,'. */
10701 cp_lexer_consume_token (parser->lexer);
10702 /* Expect an ellipsis. */
10704 = (cp_parser_require (parser, CPP_ELLIPSIS, "`...'") != NULL);
10706 /* It might also be `...' if the optional trailing `,' was
10708 else if (token->type == CPP_ELLIPSIS)
10710 /* Consume the `...' token. */
10711 cp_lexer_consume_token (parser->lexer);
10712 /* And remember that we saw it. */
10716 ellipsis_p = false;
10718 /* Finish the parameter list. */
10719 return finish_parmlist (parameters, ellipsis_p);
10722 /* Parse a parameter-declaration-list.
10724 parameter-declaration-list:
10725 parameter-declaration
10726 parameter-declaration-list , parameter-declaration
10728 Returns a representation of the parameter-declaration-list, as for
10729 cp_parser_parameter_declaration_clause. However, the
10730 `void_list_node' is never appended to the list. */
10733 cp_parser_parameter_declaration_list (parser)
10736 tree parameters = NULL_TREE;
10738 /* Look for more parameters. */
10742 /* Parse the parameter. */
10744 = cp_parser_parameter_declaration (parser,
10745 /*greater_than_is_operator_p=*/true);
10746 /* If a parse error ocurred parsing the parameter declaration,
10747 then the entire parameter-declaration-list is erroneous. */
10748 if (parameter == error_mark_node)
10750 parameters = error_mark_node;
10753 /* Add the new parameter to the list. */
10754 TREE_CHAIN (parameter) = parameters;
10755 parameters = parameter;
10757 /* Peek at the next token. */
10758 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
10759 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10760 /* The parameter-declaration-list is complete. */
10762 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
10766 /* Peek at the next token. */
10767 token = cp_lexer_peek_nth_token (parser->lexer, 2);
10768 /* If it's an ellipsis, then the list is complete. */
10769 if (token->type == CPP_ELLIPSIS)
10771 /* Otherwise, there must be more parameters. Consume the
10773 cp_lexer_consume_token (parser->lexer);
10777 cp_parser_error (parser, "expected `,' or `...'");
10782 /* We built up the list in reverse order; straighten it out now. */
10783 return nreverse (parameters);
10786 /* Parse a parameter declaration.
10788 parameter-declaration:
10789 decl-specifier-seq declarator
10790 decl-specifier-seq declarator = assignment-expression
10791 decl-specifier-seq abstract-declarator [opt]
10792 decl-specifier-seq abstract-declarator [opt] = assignment-expression
10794 If GREATER_THAN_IS_OPERATOR_P is FALSE, then a non-nested `>' token
10795 encountered during the parsing of the assignment-expression is not
10796 interpreted as a greater-than operator.
10798 Returns a TREE_LIST representing the parameter-declaration. The
10799 TREE_VALUE is a representation of the decl-specifier-seq and
10800 declarator. In particular, the TREE_VALUE will be a TREE_LIST
10801 whose TREE_PURPOSE represents the decl-specifier-seq and whose
10802 TREE_VALUE represents the declarator. */
10805 cp_parser_parameter_declaration (parser, greater_than_is_operator_p)
10807 bool greater_than_is_operator_p;
10809 bool declares_class_or_enum;
10810 tree decl_specifiers;
10813 tree default_argument;
10816 const char *saved_message;
10818 /* Type definitions may not appear in parameter types. */
10819 saved_message = parser->type_definition_forbidden_message;
10820 parser->type_definition_forbidden_message
10821 = "types may not be defined in parameter types";
10823 /* Parse the declaration-specifiers. */
10825 = cp_parser_decl_specifier_seq (parser,
10826 CP_PARSER_FLAGS_NONE,
10828 &declares_class_or_enum);
10829 /* If an error occurred, there's no reason to attempt to parse the
10830 rest of the declaration. */
10831 if (cp_parser_error_occurred (parser))
10833 parser->type_definition_forbidden_message = saved_message;
10834 return error_mark_node;
10837 /* Peek at the next token. */
10838 token = cp_lexer_peek_token (parser->lexer);
10839 /* If the next token is a `)', `,', `=', `>', or `...', then there
10840 is no declarator. */
10841 if (token->type == CPP_CLOSE_PAREN
10842 || token->type == CPP_COMMA
10843 || token->type == CPP_EQ
10844 || token->type == CPP_ELLIPSIS
10845 || token->type == CPP_GREATER)
10846 declarator = NULL_TREE;
10847 /* Otherwise, there should be a declarator. */
10850 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
10851 parser->default_arg_ok_p = false;
10853 /* We don't know whether the declarator will be abstract or
10854 not. So, first we try an ordinary declarator. */
10855 cp_parser_parse_tentatively (parser);
10856 declarator = cp_parser_declarator (parser,
10857 /*abstract_p=*/false,
10858 /*ctor_dtor_or_conv_p=*/NULL);
10859 /* If that didn't work, look for an abstract declarator. */
10860 if (!cp_parser_parse_definitely (parser))
10861 declarator = cp_parser_declarator (parser,
10862 /*abstract_p=*/true,
10863 /*ctor_dtor_or_conv_p=*/NULL);
10864 parser->default_arg_ok_p = saved_default_arg_ok_p;
10865 /* After the declarator, allow more attributes. */
10866 attributes = chainon (attributes, cp_parser_attributes_opt (parser));
10869 /* The restriction on definining new types applies only to the type
10870 of the parameter, not to the default argument. */
10871 parser->type_definition_forbidden_message = saved_message;
10873 /* If the next token is `=', then process a default argument. */
10874 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10876 bool saved_greater_than_is_operator_p;
10877 /* Consume the `='. */
10878 cp_lexer_consume_token (parser->lexer);
10880 /* If we are defining a class, then the tokens that make up the
10881 default argument must be saved and processed later. */
10882 if (at_class_scope_p () && TYPE_BEING_DEFINED (current_class_type))
10884 unsigned depth = 0;
10886 /* Create a DEFAULT_ARG to represented the unparsed default
10888 default_argument = make_node (DEFAULT_ARG);
10889 DEFARG_TOKENS (default_argument) = cp_token_cache_new ();
10891 /* Add tokens until we have processed the entire default
10898 /* Peek at the next token. */
10899 token = cp_lexer_peek_token (parser->lexer);
10900 /* What we do depends on what token we have. */
10901 switch (token->type)
10903 /* In valid code, a default argument must be
10904 immediately followed by a `,' `)', or `...'. */
10906 case CPP_CLOSE_PAREN:
10908 /* If we run into a non-nested `;', `}', or `]',
10909 then the code is invalid -- but the default
10910 argument is certainly over. */
10911 case CPP_SEMICOLON:
10912 case CPP_CLOSE_BRACE:
10913 case CPP_CLOSE_SQUARE:
10916 /* Update DEPTH, if necessary. */
10917 else if (token->type == CPP_CLOSE_PAREN
10918 || token->type == CPP_CLOSE_BRACE
10919 || token->type == CPP_CLOSE_SQUARE)
10923 case CPP_OPEN_PAREN:
10924 case CPP_OPEN_SQUARE:
10925 case CPP_OPEN_BRACE:
10930 /* If we see a non-nested `>', and `>' is not an
10931 operator, then it marks the end of the default
10933 if (!depth && !greater_than_is_operator_p)
10937 /* If we run out of tokens, issue an error message. */
10939 error ("file ends in default argument");
10945 /* In these cases, we should look for template-ids.
10946 For example, if the default argument is
10947 `X<int, double>()', we need to do name lookup to
10948 figure out whether or not `X' is a template; if
10949 so, the `,' does not end the deault argument.
10951 That is not yet done. */
10958 /* If we've reached the end, stop. */
10962 /* Add the token to the token block. */
10963 token = cp_lexer_consume_token (parser->lexer);
10964 cp_token_cache_push_token (DEFARG_TOKENS (default_argument),
10968 /* Outside of a class definition, we can just parse the
10969 assignment-expression. */
10972 bool saved_local_variables_forbidden_p;
10974 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
10976 saved_greater_than_is_operator_p
10977 = parser->greater_than_is_operator_p;
10978 parser->greater_than_is_operator_p = greater_than_is_operator_p;
10979 /* Local variable names (and the `this' keyword) may not
10980 appear in a default argument. */
10981 saved_local_variables_forbidden_p
10982 = parser->local_variables_forbidden_p;
10983 parser->local_variables_forbidden_p = true;
10984 /* Parse the assignment-expression. */
10985 default_argument = cp_parser_assignment_expression (parser);
10986 /* Restore saved state. */
10987 parser->greater_than_is_operator_p
10988 = saved_greater_than_is_operator_p;
10989 parser->local_variables_forbidden_p
10990 = saved_local_variables_forbidden_p;
10992 if (!parser->default_arg_ok_p)
10994 pedwarn ("default arguments are only permitted on functions");
10995 if (flag_pedantic_errors)
10996 default_argument = NULL_TREE;
11000 default_argument = NULL_TREE;
11002 /* Create the representation of the parameter. */
11004 decl_specifiers = tree_cons (attributes, NULL_TREE, decl_specifiers);
11005 parameter = build_tree_list (default_argument,
11006 build_tree_list (decl_specifiers,
11012 /* Parse a function-definition.
11014 function-definition:
11015 decl-specifier-seq [opt] declarator ctor-initializer [opt]
11017 decl-specifier-seq [opt] declarator function-try-block
11021 function-definition:
11022 __extension__ function-definition
11024 Returns the FUNCTION_DECL for the function. If FRIEND_P is
11025 non-NULL, *FRIEND_P is set to TRUE iff the function was declared to
11029 cp_parser_function_definition (parser, friend_p)
11033 tree decl_specifiers;
11037 tree access_checks;
11039 bool declares_class_or_enum;
11041 /* The saved value of the PEDANTIC flag. */
11042 int saved_pedantic;
11044 /* Any pending qualification must be cleared by our caller. It is
11045 more robust to force the callers to clear PARSER->SCOPE than to
11046 do it here since if the qualification is in effect here, it might
11047 also end up in effect elsewhere that it is not intended. */
11048 my_friendly_assert (!parser->scope, 20010821);
11050 /* Handle `__extension__'. */
11051 if (cp_parser_extension_opt (parser, &saved_pedantic))
11053 /* Parse the function-definition. */
11054 fn = cp_parser_function_definition (parser, friend_p);
11055 /* Restore the PEDANTIC flag. */
11056 pedantic = saved_pedantic;
11061 /* Check to see if this definition appears in a class-specifier. */
11062 member_p = (at_class_scope_p ()
11063 && TYPE_BEING_DEFINED (current_class_type));
11064 /* Defer access checks in the decl-specifier-seq until we know what
11065 function is being defined. There is no need to do this for the
11066 definition of member functions; we cannot be defining a member
11067 from another class. */
11069 cp_parser_start_deferring_access_checks (parser);
11070 /* Parse the decl-specifier-seq. */
11072 = cp_parser_decl_specifier_seq (parser,
11073 CP_PARSER_FLAGS_OPTIONAL,
11075 &declares_class_or_enum);
11076 /* Figure out whether this declaration is a `friend'. */
11078 *friend_p = cp_parser_friend_p (decl_specifiers);
11080 /* Parse the declarator. */
11081 declarator = cp_parser_declarator (parser,
11082 /*abstract_p=*/false,
11083 /*ctor_dtor_or_conv_p=*/NULL);
11085 /* Gather up any access checks that occurred. */
11087 access_checks = cp_parser_stop_deferring_access_checks (parser);
11089 access_checks = NULL_TREE;
11091 /* If something has already gone wrong, we may as well stop now. */
11092 if (declarator == error_mark_node)
11094 /* Skip to the end of the function, or if this wasn't anything
11095 like a function-definition, to a `;' in the hopes of finding
11096 a sensible place from which to continue parsing. */
11097 cp_parser_skip_to_end_of_block_or_statement (parser);
11098 return error_mark_node;
11101 /* The next character should be a `{' (for a simple function
11102 definition), a `:' (for a ctor-initializer), or `try' (for a
11103 function-try block). */
11104 token = cp_lexer_peek_token (parser->lexer);
11105 if (!cp_parser_token_starts_function_definition_p (token))
11107 /* Issue the error-message. */
11108 cp_parser_error (parser, "expected function-definition");
11109 /* Skip to the next `;'. */
11110 cp_parser_skip_to_end_of_block_or_statement (parser);
11112 return error_mark_node;
11115 /* If we are in a class scope, then we must handle
11116 function-definitions specially. In particular, we save away the
11117 tokens that make up the function body, and parse them again
11118 later, in order to handle code like:
11121 int f () { return i; }
11125 Here, we cannot parse the body of `f' until after we have seen
11126 the declaration of `i'. */
11129 cp_token_cache *cache;
11131 /* Create the function-declaration. */
11132 fn = start_method (decl_specifiers, declarator, attributes);
11133 /* If something went badly wrong, bail out now. */
11134 if (fn == error_mark_node)
11136 /* If there's a function-body, skip it. */
11137 if (cp_parser_token_starts_function_definition_p
11138 (cp_lexer_peek_token (parser->lexer)))
11139 cp_parser_skip_to_end_of_block_or_statement (parser);
11140 return error_mark_node;
11143 /* Create a token cache. */
11144 cache = cp_token_cache_new ();
11145 /* Save away the tokens that make up the body of the
11147 cp_parser_cache_group (parser, cache, CPP_CLOSE_BRACE, /*depth=*/0);
11148 /* Handle function try blocks. */
11149 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
11150 cp_parser_cache_group (parser, cache, CPP_CLOSE_BRACE, /*depth=*/0);
11152 /* Save away the inline definition; we will process it when the
11153 class is complete. */
11154 DECL_PENDING_INLINE_INFO (fn) = cache;
11155 DECL_PENDING_INLINE_P (fn) = 1;
11157 /* We're done with the inline definition. */
11158 finish_method (fn);
11160 /* Add FN to the queue of functions to be parsed later. */
11161 TREE_VALUE (parser->unparsed_functions_queues)
11162 = tree_cons (current_class_type, fn,
11163 TREE_VALUE (parser->unparsed_functions_queues));
11168 /* Check that the number of template-parameter-lists is OK. */
11169 if (!cp_parser_check_declarator_template_parameters (parser,
11172 cp_parser_skip_to_end_of_block_or_statement (parser);
11173 return error_mark_node;
11176 return (cp_parser_function_definition_from_specifiers_and_declarator
11177 (parser, decl_specifiers, attributes, declarator, access_checks));
11180 /* Parse a function-body.
11183 compound_statement */
11186 cp_parser_function_body (cp_parser *parser)
11188 cp_parser_compound_statement (parser);
11191 /* Parse a ctor-initializer-opt followed by a function-body. Return
11192 true if a ctor-initializer was present. */
11195 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
11198 bool ctor_initializer_p;
11200 /* Begin the function body. */
11201 body = begin_function_body ();
11202 /* Parse the optional ctor-initializer. */
11203 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
11204 /* Parse the function-body. */
11205 cp_parser_function_body (parser);
11206 /* Finish the function body. */
11207 finish_function_body (body);
11209 return ctor_initializer_p;
11212 /* Parse an initializer.
11215 = initializer-clause
11216 ( expression-list )
11218 Returns a expression representing the initializer. If no
11219 initializer is present, NULL_TREE is returned.
11221 *IS_PARENTHESIZED_INIT is set to TRUE if the `( expression-list )'
11222 production is used, and zero otherwise. *IS_PARENTHESIZED_INIT is
11223 set to FALSE if there is no initializer present. */
11226 cp_parser_initializer (parser, is_parenthesized_init)
11228 bool *is_parenthesized_init;
11233 /* Peek at the next token. */
11234 token = cp_lexer_peek_token (parser->lexer);
11236 /* Let our caller know whether or not this initializer was
11238 *is_parenthesized_init = (token->type == CPP_OPEN_PAREN);
11240 if (token->type == CPP_EQ)
11242 /* Consume the `='. */
11243 cp_lexer_consume_token (parser->lexer);
11244 /* Parse the initializer-clause. */
11245 init = cp_parser_initializer_clause (parser);
11247 else if (token->type == CPP_OPEN_PAREN)
11249 /* Consume the `('. */
11250 cp_lexer_consume_token (parser->lexer);
11251 /* Parse the expression-list. */
11252 init = cp_parser_expression_list (parser);
11253 /* Consume the `)' token. */
11254 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
11255 cp_parser_skip_to_closing_parenthesis (parser);
11259 /* Anything else is an error. */
11260 cp_parser_error (parser, "expected initializer");
11261 init = error_mark_node;
11267 /* Parse an initializer-clause.
11269 initializer-clause:
11270 assignment-expression
11271 { initializer-list , [opt] }
11274 Returns an expression representing the initializer.
11276 If the `assignment-expression' production is used the value
11277 returned is simply a reprsentation for the expression.
11279 Otherwise, a CONSTRUCTOR is returned. The CONSTRUCTOR_ELTS will be
11280 the elements of the initializer-list (or NULL_TREE, if the last
11281 production is used). The TREE_TYPE for the CONSTRUCTOR will be
11282 NULL_TREE. There is no way to detect whether or not the optional
11283 trailing `,' was provided. */
11286 cp_parser_initializer_clause (parser)
11291 /* If it is not a `{', then we are looking at an
11292 assignment-expression. */
11293 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
11294 initializer = cp_parser_assignment_expression (parser);
11297 /* Consume the `{' token. */
11298 cp_lexer_consume_token (parser->lexer);
11299 /* Create a CONSTRUCTOR to represent the braced-initializer. */
11300 initializer = make_node (CONSTRUCTOR);
11301 /* Mark it with TREE_HAS_CONSTRUCTOR. This should not be
11302 necessary, but check_initializer depends upon it, for
11304 TREE_HAS_CONSTRUCTOR (initializer) = 1;
11305 /* If it's not a `}', then there is a non-trivial initializer. */
11306 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
11308 /* Parse the initializer list. */
11309 CONSTRUCTOR_ELTS (initializer)
11310 = cp_parser_initializer_list (parser);
11311 /* A trailing `,' token is allowed. */
11312 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
11313 cp_lexer_consume_token (parser->lexer);
11316 /* Now, there should be a trailing `}'. */
11317 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
11320 return initializer;
11323 /* Parse an initializer-list.
11327 initializer-list , initializer-clause
11332 identifier : initializer-clause
11333 initializer-list, identifier : initializer-clause
11335 Returns a TREE_LIST. The TREE_VALUE of each node is an expression
11336 for the initializer. If the TREE_PURPOSE is non-NULL, it is the
11337 IDENTIFIER_NODE naming the field to initialize. */
11340 cp_parser_initializer_list (parser)
11343 tree initializers = NULL_TREE;
11345 /* Parse the rest of the list. */
11352 /* If the next token is an identifier and the following one is a
11353 colon, we are looking at the GNU designated-initializer
11355 if (cp_parser_allow_gnu_extensions_p (parser)
11356 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
11357 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
11359 /* Consume the identifier. */
11360 identifier = cp_lexer_consume_token (parser->lexer)->value;
11361 /* Consume the `:'. */
11362 cp_lexer_consume_token (parser->lexer);
11365 identifier = NULL_TREE;
11367 /* Parse the initializer. */
11368 initializer = cp_parser_initializer_clause (parser);
11370 /* Add it to the list. */
11371 initializers = tree_cons (identifier, initializer, initializers);
11373 /* If the next token is not a comma, we have reached the end of
11375 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11378 /* Peek at the next token. */
11379 token = cp_lexer_peek_nth_token (parser->lexer, 2);
11380 /* If the next token is a `}', then we're still done. An
11381 initializer-clause can have a trailing `,' after the
11382 initializer-list and before the closing `}'. */
11383 if (token->type == CPP_CLOSE_BRACE)
11386 /* Consume the `,' token. */
11387 cp_lexer_consume_token (parser->lexer);
11390 /* The initializers were built up in reverse order, so we need to
11391 reverse them now. */
11392 return nreverse (initializers);
11395 /* Classes [gram.class] */
11397 /* Parse a class-name.
11403 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
11404 to indicate that names looked up in dependent types should be
11405 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
11406 keyword has been used to indicate that the name that appears next
11407 is a template. TYPE_P is true iff the next name should be treated
11408 as class-name, even if it is declared to be some other kind of name
11409 as well. The accessibility of the class-name is checked iff
11410 CHECK_ACCESS_P is true. If CHECK_DEPENDENCY_P is FALSE, names are
11411 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
11412 is the class being defined in a class-head.
11414 Returns the TYPE_DECL representing the class. */
11417 cp_parser_class_name (cp_parser *parser,
11418 bool typename_keyword_p,
11419 bool template_keyword_p,
11421 bool check_access_p,
11422 bool check_dependency_p,
11430 /* All class-names start with an identifier. */
11431 token = cp_lexer_peek_token (parser->lexer);
11432 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
11434 cp_parser_error (parser, "expected class-name");
11435 return error_mark_node;
11438 /* PARSER->SCOPE can be cleared when parsing the template-arguments
11439 to a template-id, so we save it here. */
11440 scope = parser->scope;
11441 /* Any name names a type if we're following the `typename' keyword
11442 in a qualified name where the enclosing scope is type-dependent. */
11443 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
11444 && cp_parser_dependent_type_p (scope));
11445 /* Handle the common case (an identifier, but not a template-id)
11447 if (token->type == CPP_NAME
11448 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_LESS)
11452 /* Look for the identifier. */
11453 identifier = cp_parser_identifier (parser);
11454 /* If the next token isn't an identifier, we are certainly not
11455 looking at a class-name. */
11456 if (identifier == error_mark_node)
11457 decl = error_mark_node;
11458 /* If we know this is a type-name, there's no need to look it
11460 else if (typename_p)
11464 /* If the next token is a `::', then the name must be a type
11467 [basic.lookup.qual]
11469 During the lookup for a name preceding the :: scope
11470 resolution operator, object, function, and enumerator
11471 names are ignored. */
11472 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
11474 /* Look up the name. */
11475 decl = cp_parser_lookup_name (parser, identifier,
11478 /*is_namespace=*/false,
11479 check_dependency_p);
11484 /* Try a template-id. */
11485 decl = cp_parser_template_id (parser, template_keyword_p,
11486 check_dependency_p);
11487 if (decl == error_mark_node)
11488 return error_mark_node;
11491 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
11493 /* If this is a typename, create a TYPENAME_TYPE. */
11494 if (typename_p && decl != error_mark_node)
11495 decl = TYPE_NAME (make_typename_type (scope, decl,
11498 /* Check to see that it is really the name of a class. */
11499 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
11500 && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
11501 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
11502 /* Situations like this:
11504 template <typename T> struct A {
11505 typename T::template X<int>::I i;
11508 are problematic. Is `T::template X<int>' a class-name? The
11509 standard does not seem to be definitive, but there is no other
11510 valid interpretation of the following `::'. Therefore, those
11511 names are considered class-names. */
11512 decl = TYPE_NAME (make_typename_type (scope, decl,
11513 tf_error | tf_parsing));
11514 else if (decl == error_mark_node
11515 || TREE_CODE (decl) != TYPE_DECL
11516 || !IS_AGGR_TYPE (TREE_TYPE (decl)))
11518 cp_parser_error (parser, "expected class-name");
11519 return error_mark_node;
11525 /* Parse a class-specifier.
11528 class-head { member-specification [opt] }
11530 Returns the TREE_TYPE representing the class. */
11533 cp_parser_class_specifier (parser)
11538 tree attributes = NULL_TREE;
11539 int has_trailing_semicolon;
11540 bool nested_name_specifier_p;
11541 bool deferring_access_checks_p;
11542 tree saved_access_checks;
11543 unsigned saved_num_template_parameter_lists;
11545 /* Parse the class-head. */
11546 type = cp_parser_class_head (parser,
11547 &nested_name_specifier_p,
11548 &deferring_access_checks_p,
11549 &saved_access_checks);
11550 /* If the class-head was a semantic disaster, skip the entire body
11554 cp_parser_skip_to_end_of_block_or_statement (parser);
11555 return error_mark_node;
11557 /* Look for the `{'. */
11558 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
11559 return error_mark_node;
11560 /* Issue an error message if type-definitions are forbidden here. */
11561 cp_parser_check_type_definition (parser);
11562 /* Remember that we are defining one more class. */
11563 ++parser->num_classes_being_defined;
11564 /* Inside the class, surrounding template-parameter-lists do not
11566 saved_num_template_parameter_lists
11567 = parser->num_template_parameter_lists;
11568 parser->num_template_parameter_lists = 0;
11569 /* Start the class. */
11570 type = begin_class_definition (type);
11571 if (type == error_mark_node)
11572 /* If the type is erroneous, skip the entire body of the class. */
11573 cp_parser_skip_to_closing_brace (parser);
11575 /* Parse the member-specification. */
11576 cp_parser_member_specification_opt (parser);
11577 /* Look for the trailing `}'. */
11578 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
11579 /* We get better error messages by noticing a common problem: a
11580 missing trailing `;'. */
11581 token = cp_lexer_peek_token (parser->lexer);
11582 has_trailing_semicolon = (token->type == CPP_SEMICOLON);
11583 /* Look for attributes to apply to this class. */
11584 if (cp_parser_allow_gnu_extensions_p (parser))
11585 attributes = cp_parser_attributes_opt (parser);
11586 /* Finish the class definition. */
11587 type = finish_class_definition (type,
11589 has_trailing_semicolon,
11590 nested_name_specifier_p);
11591 /* If this class is not itself within the scope of another class,
11592 then we need to parse the bodies of all of the queued function
11593 definitions. Note that the queued functions defined in a class
11594 are not always processed immediately following the
11595 class-specifier for that class. Consider:
11598 struct B { void f() { sizeof (A); } };
11601 If `f' were processed before the processing of `A' were
11602 completed, there would be no way to compute the size of `A'.
11603 Note that the nesting we are interested in here is lexical --
11604 not the semantic nesting given by TYPE_CONTEXT. In particular,
11607 struct A { struct B; };
11608 struct A::B { void f() { } };
11610 there is no need to delay the parsing of `A::B::f'. */
11611 if (--parser->num_classes_being_defined == 0)
11613 tree last_scope = NULL_TREE;
11615 /* Process non FUNCTION_DECL related DEFAULT_ARGs. */
11616 for (parser->default_arg_types = nreverse (parser->default_arg_types);
11617 parser->default_arg_types;
11618 parser->default_arg_types = TREE_CHAIN (parser->default_arg_types))
11619 cp_parser_late_parsing_default_args
11620 (parser, TREE_PURPOSE (parser->default_arg_types), NULL_TREE);
11622 /* Reverse the queue, so that we process it in the order the
11623 functions were declared. */
11624 TREE_VALUE (parser->unparsed_functions_queues)
11625 = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
11626 /* Loop through all of the functions. */
11627 while (TREE_VALUE (parser->unparsed_functions_queues))
11634 /* Figure out which function we need to process. */
11635 queue_entry = TREE_VALUE (parser->unparsed_functions_queues);
11636 fn_scope = TREE_PURPOSE (queue_entry);
11637 fn = TREE_VALUE (queue_entry);
11639 /* Parse the function. */
11640 cp_parser_late_parsing_for_member (parser, fn);
11642 TREE_VALUE (parser->unparsed_functions_queues)
11643 = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues));
11646 /* If LAST_SCOPE is non-NULL, then we have pushed scopes one
11647 more time than we have popped, so me must pop here. */
11649 pop_scope (last_scope);
11652 /* Put back any saved access checks. */
11653 if (deferring_access_checks_p)
11655 cp_parser_start_deferring_access_checks (parser);
11656 parser->context->deferred_access_checks = saved_access_checks;
11659 /* Restore the count of active template-parameter-lists. */
11660 parser->num_template_parameter_lists
11661 = saved_num_template_parameter_lists;
11666 /* Parse a class-head.
11669 class-key identifier [opt] base-clause [opt]
11670 class-key nested-name-specifier identifier base-clause [opt]
11671 class-key nested-name-specifier [opt] template-id
11675 class-key attributes identifier [opt] base-clause [opt]
11676 class-key attributes nested-name-specifier identifier base-clause [opt]
11677 class-key attributes nested-name-specifier [opt] template-id
11680 Returns the TYPE of the indicated class. Sets
11681 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
11682 involving a nested-name-specifier was used, and FALSE otherwise.
11683 Sets *DEFERRING_ACCESS_CHECKS_P to TRUE iff we were deferring
11684 access checks before this class-head. In that case,
11685 *SAVED_ACCESS_CHECKS is set to the current list of deferred access
11688 Returns NULL_TREE if the class-head is syntactically valid, but
11689 semantically invalid in a way that means we should skip the entire
11690 body of the class. */
11693 cp_parser_class_head (parser,
11694 nested_name_specifier_p,
11695 deferring_access_checks_p,
11696 saved_access_checks)
11698 bool *nested_name_specifier_p;
11699 bool *deferring_access_checks_p;
11700 tree *saved_access_checks;
11703 tree nested_name_specifier;
11704 enum tag_types class_key;
11705 tree id = NULL_TREE;
11706 tree type = NULL_TREE;
11708 bool template_id_p = false;
11709 bool qualified_p = false;
11710 bool invalid_nested_name_p = false;
11711 unsigned num_templates;
11713 /* Assume no nested-name-specifier will be present. */
11714 *nested_name_specifier_p = false;
11715 /* Assume no template parameter lists will be used in defining the
11719 /* Look for the class-key. */
11720 class_key = cp_parser_class_key (parser);
11721 if (class_key == none_type)
11722 return error_mark_node;
11724 /* Parse the attributes. */
11725 attributes = cp_parser_attributes_opt (parser);
11727 /* If the next token is `::', that is invalid -- but sometimes
11728 people do try to write:
11732 Handle this gracefully by accepting the extra qualifier, and then
11733 issuing an error about it later if this really is a
11734 class-header. If it turns out just to be an elaborated type
11735 specifier, remain silent. */
11736 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
11737 qualified_p = true;
11739 /* Determine the name of the class. Begin by looking for an
11740 optional nested-name-specifier. */
11741 nested_name_specifier
11742 = cp_parser_nested_name_specifier_opt (parser,
11743 /*typename_keyword_p=*/false,
11744 /*check_dependency_p=*/true,
11746 /* If there was a nested-name-specifier, then there *must* be an
11748 if (nested_name_specifier)
11750 /* Although the grammar says `identifier', it really means
11751 `class-name' or `template-name'. You are only allowed to
11752 define a class that has already been declared with this
11755 The proposed resolution for Core Issue 180 says that whever
11756 you see `class T::X' you should treat `X' as a type-name.
11758 It is OK to define an inaccessible class; for example:
11760 class A { class B; };
11763 So, we ask cp_parser_class_name not to check accessibility.
11765 We do not know if we will see a class-name, or a
11766 template-name. We look for a class-name first, in case the
11767 class-name is a template-id; if we looked for the
11768 template-name first we would stop after the template-name. */
11769 cp_parser_parse_tentatively (parser);
11770 type = cp_parser_class_name (parser,
11771 /*typename_keyword_p=*/false,
11772 /*template_keyword_p=*/false,
11774 /*check_access_p=*/false,
11775 /*check_dependency_p=*/false,
11776 /*class_head_p=*/true);
11777 /* If that didn't work, ignore the nested-name-specifier. */
11778 if (!cp_parser_parse_definitely (parser))
11780 invalid_nested_name_p = true;
11781 id = cp_parser_identifier (parser);
11782 if (id == error_mark_node)
11785 /* If we could not find a corresponding TYPE, treat this
11786 declaration like an unqualified declaration. */
11787 if (type == error_mark_node)
11788 nested_name_specifier = NULL_TREE;
11789 /* Otherwise, count the number of templates used in TYPE and its
11790 containing scopes. */
11795 for (scope = TREE_TYPE (type);
11796 scope && TREE_CODE (scope) != NAMESPACE_DECL;
11797 scope = (TYPE_P (scope)
11798 ? TYPE_CONTEXT (scope)
11799 : DECL_CONTEXT (scope)))
11801 && CLASS_TYPE_P (scope)
11802 && CLASSTYPE_TEMPLATE_INFO (scope)
11803 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
11807 /* Otherwise, the identifier is optional. */
11810 /* We don't know whether what comes next is a template-id,
11811 an identifier, or nothing at all. */
11812 cp_parser_parse_tentatively (parser);
11813 /* Check for a template-id. */
11814 id = cp_parser_template_id (parser,
11815 /*template_keyword_p=*/false,
11816 /*check_dependency_p=*/true);
11817 /* If that didn't work, it could still be an identifier. */
11818 if (!cp_parser_parse_definitely (parser))
11820 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
11821 id = cp_parser_identifier (parser);
11827 template_id_p = true;
11832 /* If it's not a `:' or a `{' then we can't really be looking at a
11833 class-head, since a class-head only appears as part of a
11834 class-specifier. We have to detect this situation before calling
11835 xref_tag, since that has irreversible side-effects. */
11836 if (!cp_parser_next_token_starts_class_definition_p (parser))
11838 cp_parser_error (parser, "expected `{' or `:'");
11839 return error_mark_node;
11842 /* At this point, we're going ahead with the class-specifier, even
11843 if some other problem occurs. */
11844 cp_parser_commit_to_tentative_parse (parser);
11845 /* Issue the error about the overly-qualified name now. */
11847 cp_parser_error (parser,
11848 "global qualification of class name is invalid");
11849 else if (invalid_nested_name_p)
11850 cp_parser_error (parser,
11851 "qualified name does not name a class");
11852 /* Make sure that the right number of template parameters were
11854 if (!cp_parser_check_template_parameters (parser, num_templates))
11855 /* If something went wrong, there is no point in even trying to
11856 process the class-definition. */
11859 /* We do not need to defer access checks for entities declared
11860 within the class. But, we do need to save any access checks that
11861 are currently deferred and restore them later, in case we are in
11862 the middle of something else. */
11863 *deferring_access_checks_p = parser->context->deferring_access_checks_p;
11864 if (*deferring_access_checks_p)
11865 *saved_access_checks = cp_parser_stop_deferring_access_checks (parser);
11867 /* Look up the type. */
11870 type = TREE_TYPE (id);
11871 maybe_process_partial_specialization (type);
11873 else if (!nested_name_specifier)
11875 /* If the class was unnamed, create a dummy name. */
11877 id = make_anon_name ();
11878 type = xref_tag (class_key, id, attributes, /*globalize=*/0);
11887 template <typename T> struct S { struct T };
11888 template <typename T> struct S::T { };
11890 we will get a TYPENAME_TYPE when processing the definition of
11891 `S::T'. We need to resolve it to the actual type before we
11892 try to define it. */
11893 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
11895 type = cp_parser_resolve_typename_type (parser, TREE_TYPE (type));
11896 if (type != error_mark_node)
11897 type = TYPE_NAME (type);
11900 maybe_process_partial_specialization (TREE_TYPE (type));
11901 class_type = current_class_type;
11902 type = TREE_TYPE (handle_class_head (class_key,
11903 nested_name_specifier,
11908 if (type != error_mark_node)
11910 if (!class_type && TYPE_CONTEXT (type))
11911 *nested_name_specifier_p = true;
11912 else if (class_type && !same_type_p (TYPE_CONTEXT (type),
11914 *nested_name_specifier_p = true;
11917 /* Indicate whether this class was declared as a `class' or as a
11919 if (TREE_CODE (type) == RECORD_TYPE)
11920 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
11921 cp_parser_check_class_key (class_key, type);
11923 /* Enter the scope containing the class; the names of base classes
11924 should be looked up in that context. For example, given:
11926 struct A { struct B {}; struct C; };
11927 struct A::C : B {};
11930 if (nested_name_specifier)
11931 push_scope (nested_name_specifier);
11932 /* Now, look for the base-clause. */
11933 token = cp_lexer_peek_token (parser->lexer);
11934 if (token->type == CPP_COLON)
11938 /* Get the list of base-classes. */
11939 bases = cp_parser_base_clause (parser);
11940 /* Process them. */
11941 xref_basetypes (type, bases);
11943 /* Leave the scope given by the nested-name-specifier. We will
11944 enter the class scope itself while processing the members. */
11945 if (nested_name_specifier)
11946 pop_scope (nested_name_specifier);
11951 /* Parse a class-key.
11958 Returns the kind of class-key specified, or none_type to indicate
11961 static enum tag_types
11962 cp_parser_class_key (parser)
11966 enum tag_types tag_type;
11968 /* Look for the class-key. */
11969 token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
11973 /* Check to see if the TOKEN is a class-key. */
11974 tag_type = cp_parser_token_is_class_key (token);
11976 cp_parser_error (parser, "expected class-key");
11980 /* Parse an (optional) member-specification.
11982 member-specification:
11983 member-declaration member-specification [opt]
11984 access-specifier : member-specification [opt] */
11987 cp_parser_member_specification_opt (parser)
11995 /* Peek at the next token. */
11996 token = cp_lexer_peek_token (parser->lexer);
11997 /* If it's a `}', or EOF then we've seen all the members. */
11998 if (token->type == CPP_CLOSE_BRACE || token->type == CPP_EOF)
12001 /* See if this token is a keyword. */
12002 keyword = token->keyword;
12006 case RID_PROTECTED:
12008 /* Consume the access-specifier. */
12009 cp_lexer_consume_token (parser->lexer);
12010 /* Remember which access-specifier is active. */
12011 current_access_specifier = token->value;
12012 /* Look for the `:'. */
12013 cp_parser_require (parser, CPP_COLON, "`:'");
12017 /* Otherwise, the next construction must be a
12018 member-declaration. */
12019 cp_parser_member_declaration (parser);
12020 reset_type_access_control ();
12025 /* Parse a member-declaration.
12027 member-declaration:
12028 decl-specifier-seq [opt] member-declarator-list [opt] ;
12029 function-definition ; [opt]
12030 :: [opt] nested-name-specifier template [opt] unqualified-id ;
12032 template-declaration
12034 member-declarator-list:
12036 member-declarator-list , member-declarator
12039 declarator pure-specifier [opt]
12040 declarator constant-initializer [opt]
12041 identifier [opt] : constant-expression
12045 member-declaration:
12046 __extension__ member-declaration
12049 declarator attributes [opt] pure-specifier [opt]
12050 declarator attributes [opt] constant-initializer [opt]
12051 identifier [opt] attributes [opt] : constant-expression */
12054 cp_parser_member_declaration (parser)
12057 tree decl_specifiers;
12058 tree prefix_attributes;
12060 bool declares_class_or_enum;
12063 int saved_pedantic;
12065 /* Check for the `__extension__' keyword. */
12066 if (cp_parser_extension_opt (parser, &saved_pedantic))
12069 cp_parser_member_declaration (parser);
12070 /* Restore the old value of the PEDANTIC flag. */
12071 pedantic = saved_pedantic;
12076 /* Check for a template-declaration. */
12077 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
12079 /* Parse the template-declaration. */
12080 cp_parser_template_declaration (parser, /*member_p=*/true);
12085 /* Check for a using-declaration. */
12086 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
12088 /* Parse the using-declaration. */
12089 cp_parser_using_declaration (parser);
12094 /* We can't tell whether we're looking at a declaration or a
12095 function-definition. */
12096 cp_parser_parse_tentatively (parser);
12098 /* Parse the decl-specifier-seq. */
12100 = cp_parser_decl_specifier_seq (parser,
12101 CP_PARSER_FLAGS_OPTIONAL,
12102 &prefix_attributes,
12103 &declares_class_or_enum);
12104 /* If there is no declarator, then the decl-specifier-seq should
12106 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12108 /* If there was no decl-specifier-seq, and the next token is a
12109 `;', then we have something like:
12115 Each member-declaration shall declare at least one member
12116 name of the class. */
12117 if (!decl_specifiers)
12120 pedwarn ("extra semicolon");
12126 /* See if this declaration is a friend. */
12127 friend_p = cp_parser_friend_p (decl_specifiers);
12128 /* If there were decl-specifiers, check to see if there was
12129 a class-declaration. */
12130 type = check_tag_decl (decl_specifiers);
12131 /* Nested classes have already been added to the class, but
12132 a `friend' needs to be explicitly registered. */
12135 /* If the `friend' keyword was present, the friend must
12136 be introduced with a class-key. */
12137 if (!declares_class_or_enum)
12138 error ("a class-key must be used when declaring a friend");
12141 template <typename T> struct A {
12142 friend struct A<T>::B;
12145 A<T>::B will be represented by a TYPENAME_TYPE, and
12146 therefore not recognized by check_tag_decl. */
12151 for (specifier = decl_specifiers;
12153 specifier = TREE_CHAIN (specifier))
12155 tree s = TREE_VALUE (specifier);
12157 if (TREE_CODE (s) == IDENTIFIER_NODE
12158 && IDENTIFIER_GLOBAL_VALUE (s))
12159 type = IDENTIFIER_GLOBAL_VALUE (s);
12160 if (TREE_CODE (s) == TYPE_DECL)
12170 error ("friend declaration does not name a class or "
12173 make_friend_class (current_class_type, type);
12175 /* If there is no TYPE, an error message will already have
12179 /* An anonymous aggregate has to be handled specially; such
12180 a declaration really declares a data member (with a
12181 particular type), as opposed to a nested class. */
12182 else if (ANON_AGGR_TYPE_P (type))
12184 /* Remove constructors and such from TYPE, now that we
12185 know it is an anoymous aggregate. */
12186 fixup_anonymous_aggr (type);
12187 /* And make the corresponding data member. */
12188 decl = build_decl (FIELD_DECL, NULL_TREE, type);
12189 /* Add it to the class. */
12190 finish_member_declaration (decl);
12196 /* See if these declarations will be friends. */
12197 friend_p = cp_parser_friend_p (decl_specifiers);
12199 /* Keep going until we hit the `;' at the end of the
12201 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12203 tree attributes = NULL_TREE;
12204 tree first_attribute;
12206 /* Peek at the next token. */
12207 token = cp_lexer_peek_token (parser->lexer);
12209 /* Check for a bitfield declaration. */
12210 if (token->type == CPP_COLON
12211 || (token->type == CPP_NAME
12212 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
12218 /* Get the name of the bitfield. Note that we cannot just
12219 check TOKEN here because it may have been invalidated by
12220 the call to cp_lexer_peek_nth_token above. */
12221 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
12222 identifier = cp_parser_identifier (parser);
12224 identifier = NULL_TREE;
12226 /* Consume the `:' token. */
12227 cp_lexer_consume_token (parser->lexer);
12228 /* Get the width of the bitfield. */
12229 width = cp_parser_constant_expression (parser);
12231 /* Look for attributes that apply to the bitfield. */
12232 attributes = cp_parser_attributes_opt (parser);
12233 /* Remember which attributes are prefix attributes and
12235 first_attribute = attributes;
12236 /* Combine the attributes. */
12237 attributes = chainon (prefix_attributes, attributes);
12239 /* Create the bitfield declaration. */
12240 decl = grokbitfield (identifier,
12243 /* Apply the attributes. */
12244 cplus_decl_attributes (&decl, attributes, /*flags=*/0);
12250 tree asm_specification;
12251 bool ctor_dtor_or_conv_p;
12253 /* Parse the declarator. */
12255 = cp_parser_declarator (parser,
12256 /*abstract_p=*/false,
12257 &ctor_dtor_or_conv_p);
12259 /* If something went wrong parsing the declarator, make sure
12260 that we at least consume some tokens. */
12261 if (declarator == error_mark_node)
12263 /* Skip to the end of the statement. */
12264 cp_parser_skip_to_end_of_statement (parser);
12268 /* Look for an asm-specification. */
12269 asm_specification = cp_parser_asm_specification_opt (parser);
12270 /* Look for attributes that apply to the declaration. */
12271 attributes = cp_parser_attributes_opt (parser);
12272 /* Remember which attributes are prefix attributes and
12274 first_attribute = attributes;
12275 /* Combine the attributes. */
12276 attributes = chainon (prefix_attributes, attributes);
12278 /* If it's an `=', then we have a constant-initializer or a
12279 pure-specifier. It is not correct to parse the
12280 initializer before registering the member declaration
12281 since the member declaration should be in scope while
12282 its initializer is processed. However, the rest of the
12283 front end does not yet provide an interface that allows
12284 us to handle this correctly. */
12285 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12289 A pure-specifier shall be used only in the declaration of
12290 a virtual function.
12292 A member-declarator can contain a constant-initializer
12293 only if it declares a static member of integral or
12296 Therefore, if the DECLARATOR is for a function, we look
12297 for a pure-specifier; otherwise, we look for a
12298 constant-initializer. When we call `grokfield', it will
12299 perform more stringent semantics checks. */
12300 if (TREE_CODE (declarator) == CALL_EXPR)
12301 initializer = cp_parser_pure_specifier (parser);
12304 /* This declaration cannot be a function
12306 cp_parser_commit_to_tentative_parse (parser);
12307 /* Parse the initializer. */
12308 initializer = cp_parser_constant_initializer (parser);
12311 /* Otherwise, there is no initializer. */
12313 initializer = NULL_TREE;
12315 /* See if we are probably looking at a function
12316 definition. We are certainly not looking at at a
12317 member-declarator. Calling `grokfield' has
12318 side-effects, so we must not do it unless we are sure
12319 that we are looking at a member-declarator. */
12320 if (cp_parser_token_starts_function_definition_p
12321 (cp_lexer_peek_token (parser->lexer)))
12322 decl = error_mark_node;
12324 /* Create the declaration. */
12325 decl = grokfield (declarator,
12332 /* Reset PREFIX_ATTRIBUTES. */
12333 while (attributes && TREE_CHAIN (attributes) != first_attribute)
12334 attributes = TREE_CHAIN (attributes);
12336 TREE_CHAIN (attributes) = NULL_TREE;
12338 /* If there is any qualification still in effect, clear it
12339 now; we will be starting fresh with the next declarator. */
12340 parser->scope = NULL_TREE;
12341 parser->qualifying_scope = NULL_TREE;
12342 parser->object_scope = NULL_TREE;
12343 /* If it's a `,', then there are more declarators. */
12344 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
12345 cp_lexer_consume_token (parser->lexer);
12346 /* If the next token isn't a `;', then we have a parse error. */
12347 else if (cp_lexer_next_token_is_not (parser->lexer,
12350 cp_parser_error (parser, "expected `;'");
12351 /* Skip tokens until we find a `;' */
12352 cp_parser_skip_to_end_of_statement (parser);
12359 /* Add DECL to the list of members. */
12361 finish_member_declaration (decl);
12363 /* If DECL is a function, we must return
12364 to parse it later. (Even though there is no definition,
12365 there might be default arguments that need handling.) */
12366 if (TREE_CODE (decl) == FUNCTION_DECL)
12367 TREE_VALUE (parser->unparsed_functions_queues)
12368 = tree_cons (current_class_type, decl,
12369 TREE_VALUE (parser->unparsed_functions_queues));
12374 /* If everything went well, look for the `;'. */
12375 if (cp_parser_parse_definitely (parser))
12377 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
12381 /* Parse the function-definition. */
12382 decl = cp_parser_function_definition (parser, &friend_p);
12383 /* If the member was not a friend, declare it here. */
12385 finish_member_declaration (decl);
12386 /* Peek at the next token. */
12387 token = cp_lexer_peek_token (parser->lexer);
12388 /* If the next token is a semicolon, consume it. */
12389 if (token->type == CPP_SEMICOLON)
12390 cp_lexer_consume_token (parser->lexer);
12393 /* Parse a pure-specifier.
12398 Returns INTEGER_ZERO_NODE if a pure specifier is found.
12399 Otherwiser, ERROR_MARK_NODE is returned. */
12402 cp_parser_pure_specifier (parser)
12407 /* Look for the `=' token. */
12408 if (!cp_parser_require (parser, CPP_EQ, "`='"))
12409 return error_mark_node;
12410 /* Look for the `0' token. */
12411 token = cp_parser_require (parser, CPP_NUMBER, "`0'");
12412 /* Unfortunately, this will accept `0L' and `0x00' as well. We need
12413 to get information from the lexer about how the number was
12414 spelled in order to fix this problem. */
12415 if (!token || !integer_zerop (token->value))
12416 return error_mark_node;
12418 return integer_zero_node;
12421 /* Parse a constant-initializer.
12423 constant-initializer:
12424 = constant-expression
12426 Returns a representation of the constant-expression. */
12429 cp_parser_constant_initializer (parser)
12432 /* Look for the `=' token. */
12433 if (!cp_parser_require (parser, CPP_EQ, "`='"))
12434 return error_mark_node;
12436 /* It is invalid to write:
12438 struct S { static const int i = { 7 }; };
12441 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12443 cp_parser_error (parser,
12444 "a brace-enclosed initializer is not allowed here");
12445 /* Consume the opening brace. */
12446 cp_lexer_consume_token (parser->lexer);
12447 /* Skip the initializer. */
12448 cp_parser_skip_to_closing_brace (parser);
12449 /* Look for the trailing `}'. */
12450 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
12452 return error_mark_node;
12455 return cp_parser_constant_expression (parser);
12458 /* Derived classes [gram.class.derived] */
12460 /* Parse a base-clause.
12463 : base-specifier-list
12465 base-specifier-list:
12467 base-specifier-list , base-specifier
12469 Returns a TREE_LIST representing the base-classes, in the order in
12470 which they were declared. The representation of each node is as
12471 described by cp_parser_base_specifier.
12473 In the case that no bases are specified, this function will return
12474 NULL_TREE, not ERROR_MARK_NODE. */
12477 cp_parser_base_clause (parser)
12480 tree bases = NULL_TREE;
12482 /* Look for the `:' that begins the list. */
12483 cp_parser_require (parser, CPP_COLON, "`:'");
12485 /* Scan the base-specifier-list. */
12491 /* Look for the base-specifier. */
12492 base = cp_parser_base_specifier (parser);
12493 /* Add BASE to the front of the list. */
12494 if (base != error_mark_node)
12496 TREE_CHAIN (base) = bases;
12499 /* Peek at the next token. */
12500 token = cp_lexer_peek_token (parser->lexer);
12501 /* If it's not a comma, then the list is complete. */
12502 if (token->type != CPP_COMMA)
12504 /* Consume the `,'. */
12505 cp_lexer_consume_token (parser->lexer);
12508 /* PARSER->SCOPE may still be non-NULL at this point, if the last
12509 base class had a qualified name. However, the next name that
12510 appears is certainly not qualified. */
12511 parser->scope = NULL_TREE;
12512 parser->qualifying_scope = NULL_TREE;
12513 parser->object_scope = NULL_TREE;
12515 return nreverse (bases);
12518 /* Parse a base-specifier.
12521 :: [opt] nested-name-specifier [opt] class-name
12522 virtual access-specifier [opt] :: [opt] nested-name-specifier
12524 access-specifier virtual [opt] :: [opt] nested-name-specifier
12527 Returns a TREE_LIST. The TREE_PURPOSE will be one of
12528 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
12529 indicate the specifiers provided. The TREE_VALUE will be a TYPE
12530 (or the ERROR_MARK_NODE) indicating the type that was specified. */
12533 cp_parser_base_specifier (parser)
12538 bool virtual_p = false;
12539 bool duplicate_virtual_error_issued_p = false;
12540 bool duplicate_access_error_issued_p = false;
12541 bool class_scope_p;
12542 access_kind access = ak_none;
12546 /* Process the optional `virtual' and `access-specifier'. */
12549 /* Peek at the next token. */
12550 token = cp_lexer_peek_token (parser->lexer);
12551 /* Process `virtual'. */
12552 switch (token->keyword)
12555 /* If `virtual' appears more than once, issue an error. */
12556 if (virtual_p && !duplicate_virtual_error_issued_p)
12558 cp_parser_error (parser,
12559 "`virtual' specified more than once in base-specified");
12560 duplicate_virtual_error_issued_p = true;
12565 /* Consume the `virtual' token. */
12566 cp_lexer_consume_token (parser->lexer);
12571 case RID_PROTECTED:
12573 /* If more than one access specifier appears, issue an
12575 if (access != ak_none && !duplicate_access_error_issued_p)
12577 cp_parser_error (parser,
12578 "more than one access specifier in base-specified");
12579 duplicate_access_error_issued_p = true;
12582 access = ((access_kind)
12583 tree_low_cst (ridpointers[(int) token->keyword],
12586 /* Consume the access-specifier. */
12587 cp_lexer_consume_token (parser->lexer);
12597 /* Map `virtual_p' and `access' onto one of the access
12603 access_node = access_default_node;
12606 access_node = access_public_node;
12609 access_node = access_protected_node;
12612 access_node = access_private_node;
12621 access_node = access_default_virtual_node;
12624 access_node = access_public_virtual_node;
12627 access_node = access_protected_virtual_node;
12630 access_node = access_private_virtual_node;
12636 /* Look for the optional `::' operator. */
12637 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
12638 /* Look for the nested-name-specifier. The simplest way to
12643 The keyword `typename' is not permitted in a base-specifier or
12644 mem-initializer; in these contexts a qualified name that
12645 depends on a template-parameter is implicitly assumed to be a
12648 is to pretend that we have seen the `typename' keyword at this
12650 cp_parser_nested_name_specifier_opt (parser,
12651 /*typename_keyword_p=*/true,
12652 /*check_dependency_p=*/true,
12654 /* If the base class is given by a qualified name, assume that names
12655 we see are type names or templates, as appropriate. */
12656 class_scope_p = (parser->scope && TYPE_P (parser->scope));
12657 /* Finally, look for the class-name. */
12658 type = cp_parser_class_name (parser,
12662 /*check_access=*/true,
12663 /*check_dependency_p=*/true,
12664 /*class_head_p=*/false);
12666 if (type == error_mark_node)
12667 return error_mark_node;
12669 return finish_base_specifier (access_node, TREE_TYPE (type));
12672 /* Exception handling [gram.exception] */
12674 /* Parse an (optional) exception-specification.
12676 exception-specification:
12677 throw ( type-id-list [opt] )
12679 Returns a TREE_LIST representing the exception-specification. The
12680 TREE_VALUE of each node is a type. */
12683 cp_parser_exception_specification_opt (parser)
12689 /* Peek at the next token. */
12690 token = cp_lexer_peek_token (parser->lexer);
12691 /* If it's not `throw', then there's no exception-specification. */
12692 if (!cp_parser_is_keyword (token, RID_THROW))
12695 /* Consume the `throw'. */
12696 cp_lexer_consume_token (parser->lexer);
12698 /* Look for the `('. */
12699 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
12701 /* Peek at the next token. */
12702 token = cp_lexer_peek_token (parser->lexer);
12703 /* If it's not a `)', then there is a type-id-list. */
12704 if (token->type != CPP_CLOSE_PAREN)
12706 const char *saved_message;
12708 /* Types may not be defined in an exception-specification. */
12709 saved_message = parser->type_definition_forbidden_message;
12710 parser->type_definition_forbidden_message
12711 = "types may not be defined in an exception-specification";
12712 /* Parse the type-id-list. */
12713 type_id_list = cp_parser_type_id_list (parser);
12714 /* Restore the saved message. */
12715 parser->type_definition_forbidden_message = saved_message;
12718 type_id_list = empty_except_spec;
12720 /* Look for the `)'. */
12721 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
12723 return type_id_list;
12726 /* Parse an (optional) type-id-list.
12730 type-id-list , type-id
12732 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
12733 in the order that the types were presented. */
12736 cp_parser_type_id_list (parser)
12739 tree types = NULL_TREE;
12746 /* Get the next type-id. */
12747 type = cp_parser_type_id (parser);
12748 /* Add it to the list. */
12749 types = add_exception_specifier (types, type, /*complain=*/1);
12750 /* Peek at the next token. */
12751 token = cp_lexer_peek_token (parser->lexer);
12752 /* If it is not a `,', we are done. */
12753 if (token->type != CPP_COMMA)
12755 /* Consume the `,'. */
12756 cp_lexer_consume_token (parser->lexer);
12759 return nreverse (types);
12762 /* Parse a try-block.
12765 try compound-statement handler-seq */
12768 cp_parser_try_block (parser)
12773 cp_parser_require_keyword (parser, RID_TRY, "`try'");
12774 try_block = begin_try_block ();
12775 cp_parser_compound_statement (parser);
12776 finish_try_block (try_block);
12777 cp_parser_handler_seq (parser);
12778 finish_handler_sequence (try_block);
12783 /* Parse a function-try-block.
12785 function-try-block:
12786 try ctor-initializer [opt] function-body handler-seq */
12789 cp_parser_function_try_block (parser)
12793 bool ctor_initializer_p;
12795 /* Look for the `try' keyword. */
12796 if (!cp_parser_require_keyword (parser, RID_TRY, "`try'"))
12798 /* Let the rest of the front-end know where we are. */
12799 try_block = begin_function_try_block ();
12800 /* Parse the function-body. */
12802 = cp_parser_ctor_initializer_opt_and_function_body (parser);
12803 /* We're done with the `try' part. */
12804 finish_function_try_block (try_block);
12805 /* Parse the handlers. */
12806 cp_parser_handler_seq (parser);
12807 /* We're done with the handlers. */
12808 finish_function_handler_sequence (try_block);
12810 return ctor_initializer_p;
12813 /* Parse a handler-seq.
12816 handler handler-seq [opt] */
12819 cp_parser_handler_seq (parser)
12826 /* Parse the handler. */
12827 cp_parser_handler (parser);
12828 /* Peek at the next token. */
12829 token = cp_lexer_peek_token (parser->lexer);
12830 /* If it's not `catch' then there are no more handlers. */
12831 if (!cp_parser_is_keyword (token, RID_CATCH))
12836 /* Parse a handler.
12839 catch ( exception-declaration ) compound-statement */
12842 cp_parser_handler (parser)
12848 cp_parser_require_keyword (parser, RID_CATCH, "`catch'");
12849 handler = begin_handler ();
12850 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
12851 declaration = cp_parser_exception_declaration (parser);
12852 finish_handler_parms (declaration, handler);
12853 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
12854 cp_parser_compound_statement (parser);
12855 finish_handler (handler);
12858 /* Parse an exception-declaration.
12860 exception-declaration:
12861 type-specifier-seq declarator
12862 type-specifier-seq abstract-declarator
12866 Returns a VAR_DECL for the declaration, or NULL_TREE if the
12867 ellipsis variant is used. */
12870 cp_parser_exception_declaration (parser)
12873 tree type_specifiers;
12875 const char *saved_message;
12877 /* If it's an ellipsis, it's easy to handle. */
12878 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
12880 /* Consume the `...' token. */
12881 cp_lexer_consume_token (parser->lexer);
12885 /* Types may not be defined in exception-declarations. */
12886 saved_message = parser->type_definition_forbidden_message;
12887 parser->type_definition_forbidden_message
12888 = "types may not be defined in exception-declarations";
12890 /* Parse the type-specifier-seq. */
12891 type_specifiers = cp_parser_type_specifier_seq (parser);
12892 /* If it's a `)', then there is no declarator. */
12893 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
12894 declarator = NULL_TREE;
12897 /* Otherwise, we can't be sure whether we are looking at a
12898 direct, or an abstract, declarator. */
12899 cp_parser_parse_tentatively (parser);
12900 /* Try an ordinary declarator. */
12901 declarator = cp_parser_declarator (parser,
12902 /*abstract_p=*/false,
12903 /*ctor_dtor_or_conv_p=*/NULL);
12904 /* If that didn't work, try an abstract declarator. */
12905 if (!cp_parser_parse_definitely (parser))
12906 declarator = cp_parser_declarator (parser,
12907 /*abstract_p=*/true,
12908 /*ctor_dtor_or_conv_p=*/NULL);
12911 /* Restore the saved message. */
12912 parser->type_definition_forbidden_message = saved_message;
12914 return start_handler_parms (type_specifiers, declarator);
12917 /* Parse a throw-expression.
12920 throw assignment-expresion [opt]
12922 Returns a THROW_EXPR representing the throw-expression. */
12925 cp_parser_throw_expression (parser)
12930 cp_parser_require_keyword (parser, RID_THROW, "`throw'");
12931 /* We can't be sure if there is an assignment-expression or not. */
12932 cp_parser_parse_tentatively (parser);
12934 expression = cp_parser_assignment_expression (parser);
12935 /* If it didn't work, this is just a rethrow. */
12936 if (!cp_parser_parse_definitely (parser))
12937 expression = NULL_TREE;
12939 return build_throw (expression);
12942 /* GNU Extensions */
12944 /* Parse an (optional) asm-specification.
12947 asm ( string-literal )
12949 If the asm-specification is present, returns a STRING_CST
12950 corresponding to the string-literal. Otherwise, returns
12954 cp_parser_asm_specification_opt (parser)
12958 tree asm_specification;
12960 /* Peek at the next token. */
12961 token = cp_lexer_peek_token (parser->lexer);
12962 /* If the next token isn't the `asm' keyword, then there's no
12963 asm-specification. */
12964 if (!cp_parser_is_keyword (token, RID_ASM))
12967 /* Consume the `asm' token. */
12968 cp_lexer_consume_token (parser->lexer);
12969 /* Look for the `('. */
12970 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
12972 /* Look for the string-literal. */
12973 token = cp_parser_require (parser, CPP_STRING, "string-literal");
12975 asm_specification = token->value;
12977 asm_specification = NULL_TREE;
12979 /* Look for the `)'. */
12980 cp_parser_require (parser, CPP_CLOSE_PAREN, "`('");
12982 return asm_specification;
12985 /* Parse an asm-operand-list.
12989 asm-operand-list , asm-operand
12992 string-literal ( expression )
12993 [ string-literal ] string-literal ( expression )
12995 Returns a TREE_LIST representing the operands. The TREE_VALUE of
12996 each node is the expression. The TREE_PURPOSE is itself a
12997 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
12998 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
12999 is a STRING_CST for the string literal before the parenthesis. */
13002 cp_parser_asm_operand_list (parser)
13005 tree asm_operands = NULL_TREE;
13009 tree string_literal;
13014 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
13016 /* Consume the `[' token. */
13017 cp_lexer_consume_token (parser->lexer);
13018 /* Read the operand name. */
13019 name = cp_parser_identifier (parser);
13020 if (name != error_mark_node)
13021 name = build_string (IDENTIFIER_LENGTH (name),
13022 IDENTIFIER_POINTER (name));
13023 /* Look for the closing `]'. */
13024 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
13028 /* Look for the string-literal. */
13029 token = cp_parser_require (parser, CPP_STRING, "string-literal");
13030 string_literal = token ? token->value : error_mark_node;
13031 /* Look for the `('. */
13032 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13033 /* Parse the expression. */
13034 expression = cp_parser_expression (parser);
13035 /* Look for the `)'. */
13036 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13037 /* Add this operand to the list. */
13038 asm_operands = tree_cons (build_tree_list (name, string_literal),
13041 /* If the next token is not a `,', there are no more
13043 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13045 /* Consume the `,'. */
13046 cp_lexer_consume_token (parser->lexer);
13049 return nreverse (asm_operands);
13052 /* Parse an asm-clobber-list.
13056 asm-clobber-list , string-literal
13058 Returns a TREE_LIST, indicating the clobbers in the order that they
13059 appeared. The TREE_VALUE of each node is a STRING_CST. */
13062 cp_parser_asm_clobber_list (parser)
13065 tree clobbers = NULL_TREE;
13070 tree string_literal;
13072 /* Look for the string literal. */
13073 token = cp_parser_require (parser, CPP_STRING, "string-literal");
13074 string_literal = token ? token->value : error_mark_node;
13075 /* Add it to the list. */
13076 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
13077 /* If the next token is not a `,', then the list is
13079 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13081 /* Consume the `,' token. */
13082 cp_lexer_consume_token (parser->lexer);
13088 /* Parse an (optional) series of attributes.
13091 attributes attribute
13094 __attribute__ (( attribute-list [opt] ))
13096 The return value is as for cp_parser_attribute_list. */
13099 cp_parser_attributes_opt (parser)
13102 tree attributes = NULL_TREE;
13107 tree attribute_list;
13109 /* Peek at the next token. */
13110 token = cp_lexer_peek_token (parser->lexer);
13111 /* If it's not `__attribute__', then we're done. */
13112 if (token->keyword != RID_ATTRIBUTE)
13115 /* Consume the `__attribute__' keyword. */
13116 cp_lexer_consume_token (parser->lexer);
13117 /* Look for the two `(' tokens. */
13118 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13119 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13121 /* Peek at the next token. */
13122 token = cp_lexer_peek_token (parser->lexer);
13123 if (token->type != CPP_CLOSE_PAREN)
13124 /* Parse the attribute-list. */
13125 attribute_list = cp_parser_attribute_list (parser);
13127 /* If the next token is a `)', then there is no attribute
13129 attribute_list = NULL;
13131 /* Look for the two `)' tokens. */
13132 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13133 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13135 /* Add these new attributes to the list. */
13136 attributes = chainon (attributes, attribute_list);
13142 /* Parse an attribute-list.
13146 attribute-list , attribute
13150 identifier ( identifier )
13151 identifier ( identifier , expression-list )
13152 identifier ( expression-list )
13154 Returns a TREE_LIST. Each node corresponds to an attribute. THe
13155 TREE_PURPOSE of each node is the identifier indicating which
13156 attribute is in use. The TREE_VALUE represents the arguments, if
13160 cp_parser_attribute_list (parser)
13163 tree attribute_list = NULL_TREE;
13171 /* Look for the identifier. We also allow keywords here; for
13172 example `__attribute__ ((const))' is legal. */
13173 token = cp_lexer_peek_token (parser->lexer);
13174 if (token->type != CPP_NAME
13175 && token->type != CPP_KEYWORD)
13176 return error_mark_node;
13177 /* Consume the token. */
13178 token = cp_lexer_consume_token (parser->lexer);
13180 /* Save away the identifier that indicates which attribute this is. */
13181 identifier = token->value;
13182 attribute = build_tree_list (identifier, NULL_TREE);
13184 /* Peek at the next token. */
13185 token = cp_lexer_peek_token (parser->lexer);
13186 /* If it's an `(', then parse the attribute arguments. */
13187 if (token->type == CPP_OPEN_PAREN)
13190 int arguments_allowed_p = 1;
13192 /* Consume the `('. */
13193 cp_lexer_consume_token (parser->lexer);
13194 /* Peek at the next token. */
13195 token = cp_lexer_peek_token (parser->lexer);
13196 /* Check to see if the next token is an identifier. */
13197 if (token->type == CPP_NAME)
13199 /* Save the identifier. */
13200 identifier = token->value;
13201 /* Consume the identifier. */
13202 cp_lexer_consume_token (parser->lexer);
13203 /* Peek at the next token. */
13204 token = cp_lexer_peek_token (parser->lexer);
13205 /* If the next token is a `,', then there are some other
13206 expressions as well. */
13207 if (token->type == CPP_COMMA)
13208 /* Consume the comma. */
13209 cp_lexer_consume_token (parser->lexer);
13211 arguments_allowed_p = 0;
13214 identifier = NULL_TREE;
13216 /* If there are arguments, parse them too. */
13217 if (arguments_allowed_p)
13218 arguments = cp_parser_expression_list (parser);
13220 arguments = NULL_TREE;
13222 /* Combine the identifier and the arguments. */
13224 arguments = tree_cons (NULL_TREE, identifier, arguments);
13226 /* Save the identifier and arguments away. */
13227 TREE_VALUE (attribute) = arguments;
13229 /* Look for the closing `)'. */
13230 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13233 /* Add this attribute to the list. */
13234 TREE_CHAIN (attribute) = attribute_list;
13235 attribute_list = attribute;
13237 /* Now, look for more attributes. */
13238 token = cp_lexer_peek_token (parser->lexer);
13239 /* If the next token isn't a `,', we're done. */
13240 if (token->type != CPP_COMMA)
13243 /* Consume the commma and keep going. */
13244 cp_lexer_consume_token (parser->lexer);
13247 /* We built up the list in reverse order. */
13248 return nreverse (attribute_list);
13251 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
13252 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
13253 current value of the PEDANTIC flag, regardless of whether or not
13254 the `__extension__' keyword is present. The caller is responsible
13255 for restoring the value of the PEDANTIC flag. */
13258 cp_parser_extension_opt (parser, saved_pedantic)
13260 int *saved_pedantic;
13262 /* Save the old value of the PEDANTIC flag. */
13263 *saved_pedantic = pedantic;
13265 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
13267 /* Consume the `__extension__' token. */
13268 cp_lexer_consume_token (parser->lexer);
13269 /* We're not being pedantic while the `__extension__' keyword is
13279 /* Parse a label declaration.
13282 __label__ label-declarator-seq ;
13284 label-declarator-seq:
13285 identifier , label-declarator-seq
13289 cp_parser_label_declaration (parser)
13292 /* Look for the `__label__' keyword. */
13293 cp_parser_require_keyword (parser, RID_LABEL, "`__label__'");
13299 /* Look for an identifier. */
13300 identifier = cp_parser_identifier (parser);
13301 /* Declare it as a lobel. */
13302 finish_label_decl (identifier);
13303 /* If the next token is a `;', stop. */
13304 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13306 /* Look for the `,' separating the label declarations. */
13307 cp_parser_require (parser, CPP_COMMA, "`,'");
13310 /* Look for the final `;'. */
13311 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
13314 /* Support Functions */
13316 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
13317 NAME should have one of the representations used for an
13318 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
13319 is returned. If PARSER->SCOPE is a dependent type, then a
13320 SCOPE_REF is returned.
13322 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
13323 returned; the name was already resolved when the TEMPLATE_ID_EXPR
13324 was formed. Abstractly, such entities should not be passed to this
13325 function, because they do not need to be looked up, but it is
13326 simpler to check for this special case here, rather than at the
13329 In cases not explicitly covered above, this function returns a
13330 DECL, OVERLOAD, or baselink representing the result of the lookup.
13331 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
13334 If CHECK_ACCESS is TRUE, then access control is performed on the
13335 declaration to which the name resolves, and an error message is
13336 issued if the declaration is inaccessible.
13338 If IS_TYPE is TRUE, bindings that do not refer to types are
13341 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
13344 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
13348 cp_parser_lookup_name (cp_parser *parser, tree name, bool check_access,
13349 bool is_type, bool is_namespace, bool check_dependency)
13352 tree object_type = parser->context->object_type;
13354 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
13355 no longer valid. Note that if we are parsing tentatively, and
13356 the parse fails, OBJECT_TYPE will be automatically restored. */
13357 parser->context->object_type = NULL_TREE;
13359 if (name == error_mark_node)
13360 return error_mark_node;
13362 /* A template-id has already been resolved; there is no lookup to
13364 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
13366 if (BASELINK_P (name))
13368 my_friendly_assert ((TREE_CODE (BASELINK_FUNCTIONS (name))
13369 == TEMPLATE_ID_EXPR),
13374 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
13375 it should already have been checked to make sure that the name
13376 used matches the type being destroyed. */
13377 if (TREE_CODE (name) == BIT_NOT_EXPR)
13381 /* Figure out to which type this destructor applies. */
13383 type = parser->scope;
13384 else if (object_type)
13385 type = object_type;
13387 type = current_class_type;
13388 /* If that's not a class type, there is no destructor. */
13389 if (!type || !CLASS_TYPE_P (type))
13390 return error_mark_node;
13391 /* If it was a class type, return the destructor. */
13392 return CLASSTYPE_DESTRUCTORS (type);
13395 /* By this point, the NAME should be an ordinary identifier. If
13396 the id-expression was a qualified name, the qualifying scope is
13397 stored in PARSER->SCOPE at this point. */
13398 my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE,
13401 /* Perform the lookup. */
13404 bool dependent_type_p;
13406 if (parser->scope == error_mark_node)
13407 return error_mark_node;
13409 /* If the SCOPE is dependent, the lookup must be deferred until
13410 the template is instantiated -- unless we are explicitly
13411 looking up names in uninstantiated templates. Even then, we
13412 cannot look up the name if the scope is not a class type; it
13413 might, for example, be a template type parameter. */
13414 dependent_type_p = (TYPE_P (parser->scope)
13415 && !(parser->in_declarator_p
13416 && currently_open_class (parser->scope))
13417 && cp_parser_dependent_type_p (parser->scope));
13418 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
13419 && dependent_type_p)
13422 decl = build_nt (SCOPE_REF, parser->scope, name);
13424 /* The resolution to Core Issue 180 says that `struct A::B'
13425 should be considered a type-name, even if `A' is
13427 decl = TYPE_NAME (make_typename_type (parser->scope,
13433 /* If PARSER->SCOPE is a dependent type, then it must be a
13434 class type, and we must not be checking dependencies;
13435 otherwise, we would have processed this lookup above. So
13436 that PARSER->SCOPE is not considered a dependent base by
13437 lookup_member, we must enter the scope here. */
13438 if (dependent_type_p)
13439 push_scope (parser->scope);
13440 /* If the PARSER->SCOPE is a a template specialization, it
13441 may be instantiated during name lookup. In that case,
13442 errors may be issued. Even if we rollback the current
13443 tentative parse, those errors are valid. */
13444 decl = lookup_qualified_name (parser->scope, name, is_type,
13446 if (dependent_type_p)
13447 pop_scope (parser->scope);
13449 parser->qualifying_scope = parser->scope;
13450 parser->object_scope = NULL_TREE;
13452 else if (object_type)
13454 tree object_decl = NULL_TREE;
13455 /* Look up the name in the scope of the OBJECT_TYPE, unless the
13456 OBJECT_TYPE is not a class. */
13457 if (CLASS_TYPE_P (object_type))
13458 /* If the OBJECT_TYPE is a template specialization, it may
13459 be instantiated during name lookup. In that case, errors
13460 may be issued. Even if we rollback the current tentative
13461 parse, those errors are valid. */
13462 object_decl = lookup_member (object_type,
13464 /*protect=*/0, is_type);
13465 /* Look it up in the enclosing context, too. */
13466 decl = lookup_name_real (name, is_type, /*nonclass=*/0,
13469 parser->object_scope = object_type;
13470 parser->qualifying_scope = NULL_TREE;
13472 decl = object_decl;
13476 decl = lookup_name_real (name, is_type, /*nonclass=*/0,
13479 parser->qualifying_scope = NULL_TREE;
13480 parser->object_scope = NULL_TREE;
13483 /* If the lookup failed, let our caller know. */
13485 || decl == error_mark_node
13486 || (TREE_CODE (decl) == FUNCTION_DECL
13487 && DECL_ANTICIPATED (decl)))
13488 return error_mark_node;
13490 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
13491 if (TREE_CODE (decl) == TREE_LIST)
13493 /* The error message we have to print is too complicated for
13494 cp_parser_error, so we incorporate its actions directly. */
13495 if (!cp_parser_simulate_error (parser))
13497 error ("reference to `%D' is ambiguous", name);
13498 print_candidates (decl);
13500 return error_mark_node;
13503 my_friendly_assert (DECL_P (decl)
13504 || TREE_CODE (decl) == OVERLOAD
13505 || TREE_CODE (decl) == SCOPE_REF
13506 || BASELINK_P (decl),
13509 /* If we have resolved the name of a member declaration, check to
13510 see if the declaration is accessible. When the name resolves to
13511 set of overloaded functions, accesibility is checked when
13512 overload resolution is done.
13514 During an explicit instantiation, access is not checked at all,
13515 as per [temp.explicit]. */
13516 if (check_access && scope_chain->check_access && DECL_P (decl))
13518 tree qualifying_type;
13520 /* Figure out the type through which DECL is being
13523 = cp_parser_scope_through_which_access_occurs (decl,
13526 if (qualifying_type)
13528 /* If we are supposed to defer access checks, just record
13529 the information for later. */
13530 if (parser->context->deferring_access_checks_p)
13531 cp_parser_defer_access_check (parser, qualifying_type, decl);
13532 /* Otherwise, check accessibility now. */
13534 enforce_access (qualifying_type, decl);
13541 /* Like cp_parser_lookup_name, but for use in the typical case where
13542 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, and CHECK_DEPENDENCY is
13546 cp_parser_lookup_name_simple (parser, name)
13550 return cp_parser_lookup_name (parser, name,
13551 /*check_access=*/true,
13553 /*is_namespace=*/false,
13554 /*check_dependency=*/true);
13557 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
13558 TYPENAME_TYPE corresponds. Note that this function peers inside
13559 uninstantiated templates and therefore should be used only in
13560 extremely limited situations. */
13563 cp_parser_resolve_typename_type (parser, type)
13571 my_friendly_assert (TREE_CODE (type) == TYPENAME_TYPE,
13574 scope = TYPE_CONTEXT (type);
13575 name = DECL_NAME (TYPE_NAME (type));
13577 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
13578 it first before we can figure out what NAME refers to. */
13579 if (TREE_CODE (scope) == TYPENAME_TYPE)
13580 scope = cp_parser_resolve_typename_type (parser, scope);
13581 /* If we don't know what SCOPE refers to, then we cannot resolve the
13583 if (scope == error_mark_node)
13584 return error_mark_node;
13585 /* If the SCOPE is a template type parameter, we have no way of
13586 resolving the name. */
13587 if (TREE_CODE (scope) == TEMPLATE_TYPE_PARM)
13589 /* Enter the SCOPE so that name lookup will be resolved as if we
13590 were in the class definition. In particular, SCOPE will no
13591 longer be considered a dependent type. */
13592 push_scope (scope);
13593 /* Look up the declaration. */
13594 decl = lookup_member (scope, name, /*protect=*/0, /*want_type=*/1);
13595 /* If all went well, we got a TYPE_DECL for a non-typename. */
13597 || TREE_CODE (decl) != TYPE_DECL
13598 || TREE_CODE (TREE_TYPE (decl)) == TYPENAME_TYPE)
13600 cp_parser_error (parser, "could not resolve typename type");
13601 type = error_mark_node;
13604 type = TREE_TYPE (decl);
13605 /* Leave the SCOPE. */
13611 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
13612 the current context, return the TYPE_DECL. If TAG_NAME_P is
13613 true, the DECL indicates the class being defined in a class-head,
13614 or declared in an elaborated-type-specifier.
13616 Otherwise, return DECL. */
13619 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
13621 /* If the DECL is a TEMPLATE_DECL for a class type, and we are in
13622 the scope of the class, then treat the TEMPLATE_DECL as a
13623 class-name. For example, in:
13625 template <class T> struct S {
13631 If the TEMPLATE_DECL is being declared as part of a class-head,
13632 the same translation occurs:
13635 template <typename T> struct B;
13638 template <typename T> struct A::B {};
13640 Similarly, in a elaborated-type-specifier:
13642 namespace N { struct X{}; }
13645 template <typename T> friend struct N::X;
13649 if (DECL_CLASS_TEMPLATE_P (decl)
13651 || (current_class_type
13652 && same_type_p (TREE_TYPE (DECL_TEMPLATE_RESULT (decl)),
13653 current_class_type))))
13654 return DECL_TEMPLATE_RESULT (decl);
13659 /* If too many, or too few, template-parameter lists apply to the
13660 declarator, issue an error message. Returns TRUE if all went well,
13661 and FALSE otherwise. */
13664 cp_parser_check_declarator_template_parameters (parser, declarator)
13668 unsigned num_templates;
13670 /* We haven't seen any classes that involve template parameters yet. */
13673 switch (TREE_CODE (declarator))
13680 tree main_declarator = TREE_OPERAND (declarator, 0);
13682 cp_parser_check_declarator_template_parameters (parser,
13691 scope = TREE_OPERAND (declarator, 0);
13692 member = TREE_OPERAND (declarator, 1);
13694 /* If this is a pointer-to-member, then we are not interested
13695 in the SCOPE, because it does not qualify the thing that is
13697 if (TREE_CODE (member) == INDIRECT_REF)
13698 return (cp_parser_check_declarator_template_parameters
13701 while (scope && CLASS_TYPE_P (scope))
13703 /* You're supposed to have one `template <...>'
13704 for every template class, but you don't need one
13705 for a full specialization. For example:
13707 template <class T> struct S{};
13708 template <> struct S<int> { void f(); };
13709 void S<int>::f () {}
13711 is correct; there shouldn't be a `template <>' for
13712 the definition of `S<int>::f'. */
13713 if (CLASSTYPE_TEMPLATE_INFO (scope)
13714 && (CLASSTYPE_TEMPLATE_INSTANTIATION (scope)
13715 || uses_template_parms (CLASSTYPE_TI_ARGS (scope)))
13716 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
13719 scope = TYPE_CONTEXT (scope);
13723 /* Fall through. */
13726 /* If the DECLARATOR has the form `X<y>' then it uses one
13727 additional level of template parameters. */
13728 if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
13731 return cp_parser_check_template_parameters (parser,
13736 /* NUM_TEMPLATES were used in the current declaration. If that is
13737 invalid, return FALSE and issue an error messages. Otherwise,
13741 cp_parser_check_template_parameters (parser, num_templates)
13743 unsigned num_templates;
13745 /* If there are more template classes than parameter lists, we have
13748 template <class T> void S<T>::R<T>::f (); */
13749 if (parser->num_template_parameter_lists < num_templates)
13751 error ("too few template-parameter-lists");
13754 /* If there are the same number of template classes and parameter
13755 lists, that's OK. */
13756 if (parser->num_template_parameter_lists == num_templates)
13758 /* If there are more, but only one more, then we are referring to a
13759 member template. That's OK too. */
13760 if (parser->num_template_parameter_lists == num_templates + 1)
13762 /* Otherwise, there are too many template parameter lists. We have
13765 template <class T> template <class U> void S::f(); */
13766 error ("too many template-parameter-lists");
13770 /* Parse a binary-expression of the general form:
13774 binary-expression <token> <expr>
13776 The TOKEN_TREE_MAP maps <token> types to <expr> codes. FN is used
13777 to parser the <expr>s. If the first production is used, then the
13778 value returned by FN is returned directly. Otherwise, a node with
13779 the indicated EXPR_TYPE is returned, with operands corresponding to
13780 the two sub-expressions. */
13783 cp_parser_binary_expression (parser, token_tree_map, fn)
13785 cp_parser_token_tree_map token_tree_map;
13786 cp_parser_expression_fn fn;
13790 /* Parse the first expression. */
13791 lhs = (*fn) (parser);
13792 /* Now, look for more expressions. */
13796 cp_parser_token_tree_map_node *map_node;
13799 /* Peek at the next token. */
13800 token = cp_lexer_peek_token (parser->lexer);
13801 /* If the token is `>', and that's not an operator at the
13802 moment, then we're done. */
13803 if (token->type == CPP_GREATER
13804 && !parser->greater_than_is_operator_p)
13806 /* If we find one of the tokens we want, build the correspoding
13807 tree representation. */
13808 for (map_node = token_tree_map;
13809 map_node->token_type != CPP_EOF;
13811 if (map_node->token_type == token->type)
13813 /* Consume the operator token. */
13814 cp_lexer_consume_token (parser->lexer);
13815 /* Parse the right-hand side of the expression. */
13816 rhs = (*fn) (parser);
13817 /* Build the binary tree node. */
13818 lhs = build_x_binary_op (map_node->tree_type, lhs, rhs);
13822 /* If the token wasn't one of the ones we want, we're done. */
13823 if (map_node->token_type == CPP_EOF)
13830 /* Parse an optional `::' token indicating that the following name is
13831 from the global namespace. If so, PARSER->SCOPE is set to the
13832 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
13833 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
13834 Returns the new value of PARSER->SCOPE, if the `::' token is
13835 present, and NULL_TREE otherwise. */
13838 cp_parser_global_scope_opt (parser, current_scope_valid_p)
13840 bool current_scope_valid_p;
13844 /* Peek at the next token. */
13845 token = cp_lexer_peek_token (parser->lexer);
13846 /* If we're looking at a `::' token then we're starting from the
13847 global namespace, not our current location. */
13848 if (token->type == CPP_SCOPE)
13850 /* Consume the `::' token. */
13851 cp_lexer_consume_token (parser->lexer);
13852 /* Set the SCOPE so that we know where to start the lookup. */
13853 parser->scope = global_namespace;
13854 parser->qualifying_scope = global_namespace;
13855 parser->object_scope = NULL_TREE;
13857 return parser->scope;
13859 else if (!current_scope_valid_p)
13861 parser->scope = NULL_TREE;
13862 parser->qualifying_scope = NULL_TREE;
13863 parser->object_scope = NULL_TREE;
13869 /* Returns TRUE if the upcoming token sequence is the start of a
13870 constructor declarator. If FRIEND_P is true, the declarator is
13871 preceded by the `friend' specifier. */
13874 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
13876 bool constructor_p;
13877 tree type_decl = NULL_TREE;
13878 bool nested_name_p;
13880 /* Parse tentatively; we are going to roll back all of the tokens
13882 cp_parser_parse_tentatively (parser);
13883 /* Assume that we are looking at a constructor declarator. */
13884 constructor_p = true;
13885 /* Look for the optional `::' operator. */
13886 cp_parser_global_scope_opt (parser,
13887 /*current_scope_valid_p=*/false);
13888 /* Look for the nested-name-specifier. */
13890 = (cp_parser_nested_name_specifier_opt (parser,
13891 /*typename_keyword_p=*/false,
13892 /*check_dependency_p=*/false,
13895 /* Outside of a class-specifier, there must be a
13896 nested-name-specifier. */
13897 if (!nested_name_p &&
13898 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
13900 constructor_p = false;
13901 /* If we still think that this might be a constructor-declarator,
13902 look for a class-name. */
13907 template <typename T> struct S { S(); }
13908 template <typename T> S<T>::S ();
13910 we must recognize that the nested `S' names a class.
13913 template <typename T> S<T>::S<T> ();
13915 we must recognize that the nested `S' names a template. */
13916 type_decl = cp_parser_class_name (parser,
13917 /*typename_keyword_p=*/false,
13918 /*template_keyword_p=*/false,
13920 /*check_access_p=*/false,
13921 /*check_dependency_p=*/false,
13922 /*class_head_p=*/false);
13923 /* If there was no class-name, then this is not a constructor. */
13924 constructor_p = !cp_parser_error_occurred (parser);
13926 /* If we're still considering a constructor, we have to see a `(',
13927 to begin the parameter-declaration-clause, followed by either a
13928 `)', an `...', or a decl-specifier. We need to check for a
13929 type-specifier to avoid being fooled into thinking that:
13933 is a constructor. (It is actually a function named `f' that
13934 takes one parameter (of type `int') and returns a value of type
13937 && cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
13939 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
13940 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
13941 && !cp_parser_storage_class_specifier_opt (parser))
13943 if (current_class_type
13944 && !same_type_p (current_class_type, TREE_TYPE (type_decl)))
13945 /* The constructor for one class cannot be declared inside
13947 constructor_p = false;
13952 /* Names appearing in the type-specifier should be looked up
13953 in the scope of the class. */
13954 if (current_class_type)
13958 type = TREE_TYPE (type_decl);
13959 if (TREE_CODE (type) == TYPENAME_TYPE)
13960 type = cp_parser_resolve_typename_type (parser, type);
13963 /* Look for the type-specifier. */
13964 cp_parser_type_specifier (parser,
13965 CP_PARSER_FLAGS_NONE,
13966 /*is_friend=*/false,
13967 /*is_declarator=*/true,
13968 /*declares_class_or_enum=*/NULL,
13969 /*is_cv_qualifier=*/NULL);
13970 /* Leave the scope of the class. */
13974 constructor_p = !cp_parser_error_occurred (parser);
13979 constructor_p = false;
13980 /* We did not really want to consume any tokens. */
13981 cp_parser_abort_tentative_parse (parser);
13983 return constructor_p;
13986 /* Parse the definition of the function given by the DECL_SPECIFIERS,
13987 ATTRIBUTES, and DECLARATOR. The ACCESS_CHECKS have been deferred;
13988 they must be performed once we are in the scope of the function.
13990 Returns the function defined. */
13993 cp_parser_function_definition_from_specifiers_and_declarator
13994 (parser, decl_specifiers, attributes, declarator, access_checks)
13996 tree decl_specifiers;
13999 tree access_checks;
14004 /* Begin the function-definition. */
14005 success_p = begin_function_definition (decl_specifiers,
14009 /* If there were names looked up in the decl-specifier-seq that we
14010 did not check, check them now. We must wait until we are in the
14011 scope of the function to perform the checks, since the function
14012 might be a friend. */
14013 cp_parser_perform_deferred_access_checks (access_checks);
14017 /* If begin_function_definition didn't like the definition, skip
14018 the entire function. */
14019 error ("invalid function declaration");
14020 cp_parser_skip_to_end_of_block_or_statement (parser);
14021 fn = error_mark_node;
14024 fn = cp_parser_function_definition_after_declarator (parser,
14025 /*inline_p=*/false);
14030 /* Parse the part of a function-definition that follows the
14031 declarator. INLINE_P is TRUE iff this function is an inline
14032 function defined with a class-specifier.
14034 Returns the function defined. */
14037 cp_parser_function_definition_after_declarator (parser,
14043 bool ctor_initializer_p = false;
14044 bool saved_in_unbraced_linkage_specification_p;
14045 unsigned saved_num_template_parameter_lists;
14047 /* If the next token is `return', then the code may be trying to
14048 make use of the "named return value" extension that G++ used to
14050 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
14052 /* Consume the `return' keyword. */
14053 cp_lexer_consume_token (parser->lexer);
14054 /* Look for the identifier that indicates what value is to be
14056 cp_parser_identifier (parser);
14057 /* Issue an error message. */
14058 error ("named return values are no longer supported");
14059 /* Skip tokens until we reach the start of the function body. */
14060 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
14061 cp_lexer_consume_token (parser->lexer);
14063 /* The `extern' in `extern "C" void f () { ... }' does not apply to
14064 anything declared inside `f'. */
14065 saved_in_unbraced_linkage_specification_p
14066 = parser->in_unbraced_linkage_specification_p;
14067 parser->in_unbraced_linkage_specification_p = false;
14068 /* Inside the function, surrounding template-parameter-lists do not
14070 saved_num_template_parameter_lists
14071 = parser->num_template_parameter_lists;
14072 parser->num_template_parameter_lists = 0;
14073 /* If the next token is `try', then we are looking at a
14074 function-try-block. */
14075 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
14076 ctor_initializer_p = cp_parser_function_try_block (parser);
14077 /* A function-try-block includes the function-body, so we only do
14078 this next part if we're not processing a function-try-block. */
14081 = cp_parser_ctor_initializer_opt_and_function_body (parser);
14083 /* Finish the function. */
14084 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
14085 (inline_p ? 2 : 0));
14086 /* Generate code for it, if necessary. */
14088 /* Restore the saved values. */
14089 parser->in_unbraced_linkage_specification_p
14090 = saved_in_unbraced_linkage_specification_p;
14091 parser->num_template_parameter_lists
14092 = saved_num_template_parameter_lists;
14097 /* Parse a template-declaration, assuming that the `export' (and
14098 `extern') keywords, if present, has already been scanned. MEMBER_P
14099 is as for cp_parser_template_declaration. */
14102 cp_parser_template_declaration_after_export (parser, member_p)
14106 tree decl = NULL_TREE;
14107 tree parameter_list;
14108 bool friend_p = false;
14110 /* Look for the `template' keyword. */
14111 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'"))
14115 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
14118 /* Parse the template parameters. */
14119 begin_template_parm_list ();
14120 /* If the next token is `>', then we have an invalid
14121 specialization. Rather than complain about an invalid template
14122 parameter, issue an error message here. */
14123 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
14125 cp_parser_error (parser, "invalid explicit specialization");
14126 parameter_list = NULL_TREE;
14129 parameter_list = cp_parser_template_parameter_list (parser);
14130 parameter_list = end_template_parm_list (parameter_list);
14131 /* Look for the `>'. */
14132 cp_parser_skip_until_found (parser, CPP_GREATER, "`>'");
14133 /* We just processed one more parameter list. */
14134 ++parser->num_template_parameter_lists;
14135 /* If the next token is `template', there are more template
14137 if (cp_lexer_next_token_is_keyword (parser->lexer,
14139 cp_parser_template_declaration_after_export (parser, member_p);
14142 decl = cp_parser_single_declaration (parser,
14146 /* If this is a member template declaration, let the front
14148 if (member_p && !friend_p && decl)
14149 decl = finish_member_template_decl (decl);
14150 else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
14151 make_friend_class (current_class_type, TREE_TYPE (decl));
14153 /* We are done with the current parameter list. */
14154 --parser->num_template_parameter_lists;
14157 finish_template_decl (parameter_list);
14159 /* Register member declarations. */
14160 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
14161 finish_member_declaration (decl);
14163 /* If DECL is a function template, we must return to parse it later.
14164 (Even though there is no definition, there might be default
14165 arguments that need handling.) */
14166 if (member_p && decl
14167 && (TREE_CODE (decl) == FUNCTION_DECL
14168 || DECL_FUNCTION_TEMPLATE_P (decl)))
14169 TREE_VALUE (parser->unparsed_functions_queues)
14170 = tree_cons (current_class_type, decl,
14171 TREE_VALUE (parser->unparsed_functions_queues));
14174 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
14175 `function-definition' sequence. MEMBER_P is true, this declaration
14176 appears in a class scope.
14178 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
14179 *FRIEND_P is set to TRUE iff the declaration is a friend. */
14182 cp_parser_single_declaration (parser,
14189 bool declares_class_or_enum;
14190 tree decl = NULL_TREE;
14191 tree decl_specifiers;
14193 tree access_checks;
14195 /* Parse the dependent declaration. We don't know yet
14196 whether it will be a function-definition. */
14197 cp_parser_parse_tentatively (parser);
14198 /* Defer access checks until we know what is being declared. */
14199 cp_parser_start_deferring_access_checks (parser);
14200 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
14203 = cp_parser_decl_specifier_seq (parser,
14204 CP_PARSER_FLAGS_OPTIONAL,
14206 &declares_class_or_enum);
14207 /* Gather up the access checks that occurred the
14208 decl-specifier-seq. */
14209 access_checks = cp_parser_stop_deferring_access_checks (parser);
14210 /* Check for the declaration of a template class. */
14211 if (declares_class_or_enum)
14213 if (cp_parser_declares_only_class_p (parser))
14215 decl = shadow_tag (decl_specifiers);
14217 decl = TYPE_NAME (decl);
14219 decl = error_mark_node;
14224 /* If it's not a template class, try for a template function. If
14225 the next token is a `;', then this declaration does not declare
14226 anything. But, if there were errors in the decl-specifiers, then
14227 the error might well have come from an attempted class-specifier.
14228 In that case, there's no need to warn about a missing declarator. */
14230 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
14231 || !value_member (error_mark_node, decl_specifiers)))
14232 decl = cp_parser_init_declarator (parser,
14236 /*function_definition_allowed_p=*/false,
14238 /*function_definition_p=*/NULL);
14239 /* Clear any current qualification; whatever comes next is the start
14240 of something new. */
14241 parser->scope = NULL_TREE;
14242 parser->qualifying_scope = NULL_TREE;
14243 parser->object_scope = NULL_TREE;
14244 /* Look for a trailing `;' after the declaration. */
14245 if (!cp_parser_require (parser, CPP_SEMICOLON, "expected `;'")
14246 && cp_parser_committed_to_tentative_parse (parser))
14247 cp_parser_skip_to_end_of_block_or_statement (parser);
14248 /* If it worked, set *FRIEND_P based on the DECL_SPECIFIERS. */
14249 if (cp_parser_parse_definitely (parser))
14252 *friend_p = cp_parser_friend_p (decl_specifiers);
14254 /* Otherwise, try a function-definition. */
14256 decl = cp_parser_function_definition (parser, friend_p);
14261 /* Parse a functional cast to TYPE. Returns an expression
14262 representing the cast. */
14265 cp_parser_functional_cast (parser, type)
14269 tree expression_list;
14271 /* Look for the opening `('. */
14272 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
14273 return error_mark_node;
14274 /* If the next token is not an `)', there are arguments to the
14276 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
14277 expression_list = cp_parser_expression_list (parser);
14279 expression_list = NULL_TREE;
14280 /* Look for the closing `)'. */
14281 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14283 return build_functional_cast (type, expression_list);
14286 /* MEMBER_FUNCTION is a member function, or a friend. If default
14287 arguments, or the body of the function have not yet been parsed,
14291 cp_parser_late_parsing_for_member (parser, member_function)
14293 tree member_function;
14295 cp_lexer *saved_lexer;
14297 /* If this member is a template, get the underlying
14299 if (DECL_FUNCTION_TEMPLATE_P (member_function))
14300 member_function = DECL_TEMPLATE_RESULT (member_function);
14302 /* There should not be any class definitions in progress at this
14303 point; the bodies of members are only parsed outside of all class
14305 my_friendly_assert (parser->num_classes_being_defined == 0, 20010816);
14306 /* While we're parsing the member functions we might encounter more
14307 classes. We want to handle them right away, but we don't want
14308 them getting mixed up with functions that are currently in the
14310 parser->unparsed_functions_queues
14311 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
14313 /* Make sure that any template parameters are in scope. */
14314 maybe_begin_member_template_processing (member_function);
14316 /* If there are default arguments that have not yet been processed,
14317 take care of them now. */
14318 cp_parser_late_parsing_default_args (parser, TREE_TYPE (member_function),
14319 DECL_FUNCTION_MEMBER_P (member_function)
14320 ? DECL_CONTEXT (member_function)
14323 /* If the body of the function has not yet been parsed, parse it
14325 if (DECL_PENDING_INLINE_P (member_function))
14327 tree function_scope;
14328 cp_token_cache *tokens;
14330 /* The function is no longer pending; we are processing it. */
14331 tokens = DECL_PENDING_INLINE_INFO (member_function);
14332 DECL_PENDING_INLINE_INFO (member_function) = NULL;
14333 DECL_PENDING_INLINE_P (member_function) = 0;
14334 /* If this was an inline function in a local class, enter the scope
14335 of the containing function. */
14336 function_scope = decl_function_context (member_function);
14337 if (function_scope)
14338 push_function_context_to (function_scope);
14340 /* Save away the current lexer. */
14341 saved_lexer = parser->lexer;
14342 /* Make a new lexer to feed us the tokens saved for this function. */
14343 parser->lexer = cp_lexer_new_from_tokens (tokens);
14344 parser->lexer->next = saved_lexer;
14346 /* Set the current source position to be the location of the first
14347 token in the saved inline body. */
14348 cp_lexer_set_source_position_from_token
14350 cp_lexer_peek_token (parser->lexer));
14352 /* Let the front end know that we going to be defining this
14354 start_function (NULL_TREE, member_function, NULL_TREE,
14355 SF_PRE_PARSED | SF_INCLASS_INLINE);
14357 /* Now, parse the body of the function. */
14358 cp_parser_function_definition_after_declarator (parser,
14359 /*inline_p=*/true);
14361 /* Leave the scope of the containing function. */
14362 if (function_scope)
14363 pop_function_context_from (function_scope);
14364 /* Restore the lexer. */
14365 parser->lexer = saved_lexer;
14368 /* Remove any template parameters from the symbol table. */
14369 maybe_end_member_template_processing ();
14371 /* Restore the queue. */
14372 parser->unparsed_functions_queues
14373 = TREE_CHAIN (parser->unparsed_functions_queues);
14376 /* TYPE is a FUNCTION_TYPE or METHOD_TYPE which contains a parameter
14377 with an unparsed DEFAULT_ARG. If non-NULL, SCOPE is the class in
14378 whose context name lookups in the default argument should occur.
14379 Parse the default args now. */
14382 cp_parser_late_parsing_default_args (cp_parser *parser, tree type, tree scope)
14384 cp_lexer *saved_lexer;
14385 cp_token_cache *tokens;
14386 bool saved_local_variables_forbidden_p;
14389 for (parameters = TYPE_ARG_TYPES (type);
14391 parameters = TREE_CHAIN (parameters))
14393 if (!TREE_PURPOSE (parameters)
14394 || TREE_CODE (TREE_PURPOSE (parameters)) != DEFAULT_ARG)
14397 /* Save away the current lexer. */
14398 saved_lexer = parser->lexer;
14399 /* Create a new one, using the tokens we have saved. */
14400 tokens = DEFARG_TOKENS (TREE_PURPOSE (parameters));
14401 parser->lexer = cp_lexer_new_from_tokens (tokens);
14403 /* Set the current source position to be the location of the
14404 first token in the default argument. */
14405 cp_lexer_set_source_position_from_token
14406 (parser->lexer, cp_lexer_peek_token (parser->lexer));
14408 /* Local variable names (and the `this' keyword) may not appear
14409 in a default argument. */
14410 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
14411 parser->local_variables_forbidden_p = true;
14412 /* Parse the assignment-expression. */
14414 push_nested_class (scope, 1);
14415 TREE_PURPOSE (parameters) = cp_parser_assignment_expression (parser);
14417 pop_nested_class ();
14419 /* Restore saved state. */
14420 parser->lexer = saved_lexer;
14421 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
14425 /* Parse the operand of `sizeof' (or a similar operator). Returns
14426 either a TYPE or an expression, depending on the form of the
14427 input. The KEYWORD indicates which kind of expression we have
14431 cp_parser_sizeof_operand (parser, keyword)
14435 static const char *format;
14436 tree expr = NULL_TREE;
14437 const char *saved_message;
14438 bool saved_constant_expression_p;
14440 /* Initialize FORMAT the first time we get here. */
14442 format = "types may not be defined in `%s' expressions";
14444 /* Types cannot be defined in a `sizeof' expression. Save away the
14446 saved_message = parser->type_definition_forbidden_message;
14447 /* And create the new one. */
14448 parser->type_definition_forbidden_message
14450 xmalloc (strlen (format)
14451 + strlen (IDENTIFIER_POINTER (ridpointers[keyword]))
14453 sprintf ((char *) parser->type_definition_forbidden_message,
14454 format, IDENTIFIER_POINTER (ridpointers[keyword]));
14456 /* The restrictions on constant-expressions do not apply inside
14457 sizeof expressions. */
14458 saved_constant_expression_p = parser->constant_expression_p;
14459 parser->constant_expression_p = false;
14461 /* Do not actually evaluate the expression. */
14463 /* If it's a `(', then we might be looking at the type-id
14465 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
14469 /* We can't be sure yet whether we're looking at a type-id or an
14471 cp_parser_parse_tentatively (parser);
14472 /* Consume the `('. */
14473 cp_lexer_consume_token (parser->lexer);
14474 /* Parse the type-id. */
14475 type = cp_parser_type_id (parser);
14476 /* Now, look for the trailing `)'. */
14477 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14478 /* If all went well, then we're done. */
14479 if (cp_parser_parse_definitely (parser))
14481 /* Build a list of decl-specifiers; right now, we have only
14482 a single type-specifier. */
14483 type = build_tree_list (NULL_TREE,
14486 /* Call grokdeclarator to figure out what type this is. */
14487 expr = grokdeclarator (NULL_TREE,
14491 /*attrlist=*/NULL);
14495 /* If the type-id production did not work out, then we must be
14496 looking at the unary-expression production. */
14498 expr = cp_parser_unary_expression (parser, /*address_p=*/false);
14499 /* Go back to evaluating expressions. */
14502 /* Free the message we created. */
14503 free ((char *) parser->type_definition_forbidden_message);
14504 /* And restore the old one. */
14505 parser->type_definition_forbidden_message = saved_message;
14506 parser->constant_expression_p = saved_constant_expression_p;
14511 /* If the current declaration has no declarator, return true. */
14514 cp_parser_declares_only_class_p (cp_parser *parser)
14516 /* If the next token is a `;' or a `,' then there is no
14518 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
14519 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
14522 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
14523 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
14526 cp_parser_friend_p (decl_specifiers)
14527 tree decl_specifiers;
14529 while (decl_specifiers)
14531 /* See if this decl-specifier is `friend'. */
14532 if (TREE_CODE (TREE_VALUE (decl_specifiers)) == IDENTIFIER_NODE
14533 && C_RID_CODE (TREE_VALUE (decl_specifiers)) == RID_FRIEND)
14536 /* Go on to the next decl-specifier. */
14537 decl_specifiers = TREE_CHAIN (decl_specifiers);
14543 /* If the next token is of the indicated TYPE, consume it. Otherwise,
14544 issue an error message indicating that TOKEN_DESC was expected.
14546 Returns the token consumed, if the token had the appropriate type.
14547 Otherwise, returns NULL. */
14550 cp_parser_require (parser, type, token_desc)
14552 enum cpp_ttype type;
14553 const char *token_desc;
14555 if (cp_lexer_next_token_is (parser->lexer, type))
14556 return cp_lexer_consume_token (parser->lexer);
14559 /* Output the MESSAGE -- unless we're parsing tentatively. */
14560 if (!cp_parser_simulate_error (parser))
14561 error ("expected %s", token_desc);
14566 /* Like cp_parser_require, except that tokens will be skipped until
14567 the desired token is found. An error message is still produced if
14568 the next token is not as expected. */
14571 cp_parser_skip_until_found (parser, type, token_desc)
14573 enum cpp_ttype type;
14574 const char *token_desc;
14577 unsigned nesting_depth = 0;
14579 if (cp_parser_require (parser, type, token_desc))
14582 /* Skip tokens until the desired token is found. */
14585 /* Peek at the next token. */
14586 token = cp_lexer_peek_token (parser->lexer);
14587 /* If we've reached the token we want, consume it and
14589 if (token->type == type && !nesting_depth)
14591 cp_lexer_consume_token (parser->lexer);
14594 /* If we've run out of tokens, stop. */
14595 if (token->type == CPP_EOF)
14597 if (token->type == CPP_OPEN_BRACE
14598 || token->type == CPP_OPEN_PAREN
14599 || token->type == CPP_OPEN_SQUARE)
14601 else if (token->type == CPP_CLOSE_BRACE
14602 || token->type == CPP_CLOSE_PAREN
14603 || token->type == CPP_CLOSE_SQUARE)
14605 if (nesting_depth-- == 0)
14608 /* Consume this token. */
14609 cp_lexer_consume_token (parser->lexer);
14613 /* If the next token is the indicated keyword, consume it. Otherwise,
14614 issue an error message indicating that TOKEN_DESC was expected.
14616 Returns the token consumed, if the token had the appropriate type.
14617 Otherwise, returns NULL. */
14620 cp_parser_require_keyword (parser, keyword, token_desc)
14623 const char *token_desc;
14625 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
14627 if (token && token->keyword != keyword)
14629 dyn_string_t error_msg;
14631 /* Format the error message. */
14632 error_msg = dyn_string_new (0);
14633 dyn_string_append_cstr (error_msg, "expected ");
14634 dyn_string_append_cstr (error_msg, token_desc);
14635 cp_parser_error (parser, error_msg->s);
14636 dyn_string_delete (error_msg);
14643 /* Returns TRUE iff TOKEN is a token that can begin the body of a
14644 function-definition. */
14647 cp_parser_token_starts_function_definition_p (token)
14650 return (/* An ordinary function-body begins with an `{'. */
14651 token->type == CPP_OPEN_BRACE
14652 /* A ctor-initializer begins with a `:'. */
14653 || token->type == CPP_COLON
14654 /* A function-try-block begins with `try'. */
14655 || token->keyword == RID_TRY
14656 /* The named return value extension begins with `return'. */
14657 || token->keyword == RID_RETURN);
14660 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
14664 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
14668 token = cp_lexer_peek_token (parser->lexer);
14669 return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
14672 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
14673 or none_type otherwise. */
14675 static enum tag_types
14676 cp_parser_token_is_class_key (token)
14679 switch (token->keyword)
14684 return record_type;
14693 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
14696 cp_parser_check_class_key (enum tag_types class_key, tree type)
14698 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
14699 pedwarn ("`%s' tag used in naming `%#T'",
14700 class_key == union_type ? "union"
14701 : class_key == record_type ? "struct" : "class",
14705 /* Look for the `template' keyword, as a syntactic disambiguator.
14706 Return TRUE iff it is present, in which case it will be
14710 cp_parser_optional_template_keyword (cp_parser *parser)
14712 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
14714 /* The `template' keyword can only be used within templates;
14715 outside templates the parser can always figure out what is a
14716 template and what is not. */
14717 if (!processing_template_decl)
14719 error ("`template' (as a disambiguator) is only allowed "
14720 "within templates");
14721 /* If this part of the token stream is rescanned, the same
14722 error message would be generated. So, we purge the token
14723 from the stream. */
14724 cp_lexer_purge_token (parser->lexer);
14729 /* Consume the `template' keyword. */
14730 cp_lexer_consume_token (parser->lexer);
14738 /* Add tokens to CACHE until an non-nested END token appears. */
14741 cp_parser_cache_group (cp_parser *parser,
14742 cp_token_cache *cache,
14743 enum cpp_ttype end,
14750 /* Abort a parenthesized expression if we encounter a brace. */
14751 if ((end == CPP_CLOSE_PAREN || depth == 0)
14752 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
14754 /* Consume the next token. */
14755 token = cp_lexer_consume_token (parser->lexer);
14756 /* If we've reached the end of the file, stop. */
14757 if (token->type == CPP_EOF)
14759 /* Add this token to the tokens we are saving. */
14760 cp_token_cache_push_token (cache, token);
14761 /* See if it starts a new group. */
14762 if (token->type == CPP_OPEN_BRACE)
14764 cp_parser_cache_group (parser, cache, CPP_CLOSE_BRACE, depth + 1);
14768 else if (token->type == CPP_OPEN_PAREN)
14769 cp_parser_cache_group (parser, cache, CPP_CLOSE_PAREN, depth + 1);
14770 else if (token->type == end)
14775 /* Begin parsing tentatively. We always save tokens while parsing
14776 tentatively so that if the tentative parsing fails we can restore the
14780 cp_parser_parse_tentatively (parser)
14783 /* Enter a new parsing context. */
14784 parser->context = cp_parser_context_new (parser->context);
14785 /* Begin saving tokens. */
14786 cp_lexer_save_tokens (parser->lexer);
14787 /* In order to avoid repetitive access control error messages,
14788 access checks are queued up until we are no longer parsing
14790 cp_parser_start_deferring_access_checks (parser);
14793 /* Commit to the currently active tentative parse. */
14796 cp_parser_commit_to_tentative_parse (parser)
14799 cp_parser_context *context;
14802 /* Mark all of the levels as committed. */
14803 lexer = parser->lexer;
14804 for (context = parser->context; context->next; context = context->next)
14806 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
14808 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
14809 while (!cp_lexer_saving_tokens (lexer))
14810 lexer = lexer->next;
14811 cp_lexer_commit_tokens (lexer);
14815 /* Abort the currently active tentative parse. All consumed tokens
14816 will be rolled back, and no diagnostics will be issued. */
14819 cp_parser_abort_tentative_parse (parser)
14822 cp_parser_simulate_error (parser);
14823 /* Now, pretend that we want to see if the construct was
14824 successfully parsed. */
14825 cp_parser_parse_definitely (parser);
14828 /* Stop parsing tentatively. If a parse error has ocurred, restore the
14829 token stream. Otherwise, commit to the tokens we have consumed.
14830 Returns true if no error occurred; false otherwise. */
14833 cp_parser_parse_definitely (parser)
14836 bool error_occurred;
14837 cp_parser_context *context;
14839 /* Remember whether or not an error ocurred, since we are about to
14840 destroy that information. */
14841 error_occurred = cp_parser_error_occurred (parser);
14842 /* Remove the topmost context from the stack. */
14843 context = parser->context;
14844 parser->context = context->next;
14845 /* If no parse errors occurred, commit to the tentative parse. */
14846 if (!error_occurred)
14848 /* Commit to the tokens read tentatively, unless that was
14850 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
14851 cp_lexer_commit_tokens (parser->lexer);
14852 if (!parser->context->deferring_access_checks_p)
14853 /* If in the parent context we are not deferring checks, then
14854 these perform these checks now. */
14855 (cp_parser_perform_deferred_access_checks
14856 (context->deferred_access_checks));
14858 /* Any lookups that were deferred during the tentative parse are
14860 parser->context->deferred_access_checks
14861 = chainon (parser->context->deferred_access_checks,
14862 context->deferred_access_checks);
14864 /* Otherwise, if errors occurred, roll back our state so that things
14865 are just as they were before we began the tentative parse. */
14867 cp_lexer_rollback_tokens (parser->lexer);
14868 /* Add the context to the front of the free list. */
14869 context->next = cp_parser_context_free_list;
14870 cp_parser_context_free_list = context;
14872 return !error_occurred;
14875 /* Returns true if we are parsing tentatively -- but have decided that
14876 we will stick with this tentative parse, even if errors occur. */
14879 cp_parser_committed_to_tentative_parse (parser)
14882 return (cp_parser_parsing_tentatively (parser)
14883 && parser->context->status == CP_PARSER_STATUS_KIND_COMMITTED);
14886 /* Returns non-zero iff an error has occurred during the most recent
14887 tentative parse. */
14890 cp_parser_error_occurred (parser)
14893 return (cp_parser_parsing_tentatively (parser)
14894 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
14897 /* Returns non-zero if GNU extensions are allowed. */
14900 cp_parser_allow_gnu_extensions_p (parser)
14903 return parser->allow_gnu_extensions_p;
14910 static GTY (()) cp_parser *the_parser;
14912 /* External interface. */
14914 /* Parse the entire translation unit. */
14919 bool error_occurred;
14921 the_parser = cp_parser_new ();
14922 error_occurred = cp_parser_translation_unit (the_parser);
14925 return error_occurred;
14928 /* Clean up after parsing the entire translation unit. */
14931 free_parser_stacks ()
14933 /* Nothing to do. */
14936 /* This variable must be provided by every front end. */
14940 #include "gt-cp-parser.h"