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 /* The different kinds of declarators we want to parse. */
1129 typedef enum cp_parser_declarator_kind
1131 /* We want an abstract declartor. */
1132 CP_PARSER_DECLARATOR_ABSTRACT,
1133 /* We want a named declarator. */
1134 CP_PARSER_DECLARATOR_NAMED,
1135 /* We don't mind. */
1136 CP_PARSER_DECLARATOR_EITHER
1137 } cp_parser_declarator_kind;
1139 /* A mapping from a token type to a corresponding tree node type. */
1141 typedef struct cp_parser_token_tree_map_node
1143 /* The token type. */
1144 enum cpp_ttype token_type;
1145 /* The corresponding tree code. */
1146 enum tree_code tree_type;
1147 } cp_parser_token_tree_map_node;
1149 /* A complete map consists of several ordinary entries, followed by a
1150 terminator. The terminating entry has a token_type of CPP_EOF. */
1152 typedef cp_parser_token_tree_map_node cp_parser_token_tree_map[];
1154 /* The status of a tentative parse. */
1156 typedef enum cp_parser_status_kind
1158 /* No errors have occurred. */
1159 CP_PARSER_STATUS_KIND_NO_ERROR,
1160 /* An error has occurred. */
1161 CP_PARSER_STATUS_KIND_ERROR,
1162 /* We are committed to this tentative parse, whether or not an error
1164 CP_PARSER_STATUS_KIND_COMMITTED
1165 } cp_parser_status_kind;
1167 /* Context that is saved and restored when parsing tentatively. */
1169 typedef struct cp_parser_context GTY (())
1171 /* If this is a tentative parsing context, the status of the
1173 enum cp_parser_status_kind status;
1174 /* If non-NULL, we have just seen a `x->' or `x.' expression. Names
1175 that are looked up in this context must be looked up both in the
1176 scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1177 the context of the containing expression. */
1179 /* A TREE_LIST representing name-lookups for which we have deferred
1180 checking access controls. We cannot check the accessibility of
1181 names used in a decl-specifier-seq until we know what is being
1182 declared because code like:
1189 A::B* A::f() { return 0; }
1191 is valid, even though `A::B' is not generally accessible.
1193 The TREE_PURPOSE of each node is the scope used to qualify the
1194 name being looked up; the TREE_VALUE is the DECL to which the
1195 name was resolved. */
1196 tree deferred_access_checks;
1197 /* TRUE iff we are deferring access checks. */
1198 bool deferring_access_checks_p;
1199 /* The next parsing context in the stack. */
1200 struct cp_parser_context *next;
1201 } cp_parser_context;
1205 /* Constructors and destructors. */
1207 static cp_parser_context *cp_parser_context_new
1208 PARAMS ((cp_parser_context *));
1210 /* Class variables. */
1212 static GTY((deletable (""))) cp_parser_context* cp_parser_context_free_list;
1214 /* Constructors and destructors. */
1216 /* Construct a new context. The context below this one on the stack
1217 is given by NEXT. */
1219 static cp_parser_context *
1220 cp_parser_context_new (next)
1221 cp_parser_context *next;
1223 cp_parser_context *context;
1225 /* Allocate the storage. */
1226 if (cp_parser_context_free_list != NULL)
1228 /* Pull the first entry from the free list. */
1229 context = cp_parser_context_free_list;
1230 cp_parser_context_free_list = context->next;
1231 memset ((char *)context, 0, sizeof (*context));
1234 context = ((cp_parser_context *)
1235 ggc_alloc_cleared (sizeof (cp_parser_context)));
1236 /* No errors have occurred yet in this context. */
1237 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1238 /* If this is not the bottomost context, copy information that we
1239 need from the previous context. */
1242 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1243 expression, then we are parsing one in this context, too. */
1244 context->object_type = next->object_type;
1245 /* We are deferring access checks here if we were in the NEXT
1247 context->deferring_access_checks_p
1248 = next->deferring_access_checks_p;
1249 /* Thread the stack. */
1250 context->next = next;
1256 /* The cp_parser structure represents the C++ parser. */
1258 typedef struct cp_parser GTY(())
1260 /* The lexer from which we are obtaining tokens. */
1263 /* The scope in which names should be looked up. If NULL_TREE, then
1264 we look up names in the scope that is currently open in the
1265 source program. If non-NULL, this is either a TYPE or
1266 NAMESPACE_DECL for the scope in which we should look.
1268 This value is not cleared automatically after a name is looked
1269 up, so we must be careful to clear it before starting a new look
1270 up sequence. (If it is not cleared, then `X::Y' followed by `Z'
1271 will look up `Z' in the scope of `X', rather than the current
1272 scope.) Unfortunately, it is difficult to tell when name lookup
1273 is complete, because we sometimes peek at a token, look it up,
1274 and then decide not to consume it. */
1277 /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1278 last lookup took place. OBJECT_SCOPE is used if an expression
1279 like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1280 respectively. QUALIFYING_SCOPE is used for an expression of the
1281 form "X::Y"; it refers to X. */
1283 tree qualifying_scope;
1285 /* A stack of parsing contexts. All but the bottom entry on the
1286 stack will be tentative contexts.
1288 We parse tentatively in order to determine which construct is in
1289 use in some situations. For example, in order to determine
1290 whether a statement is an expression-statement or a
1291 declaration-statement we parse it tentatively as a
1292 declaration-statement. If that fails, we then reparse the same
1293 token stream as an expression-statement. */
1294 cp_parser_context *context;
1296 /* True if we are parsing GNU C++. If this flag is not set, then
1297 GNU extensions are not recognized. */
1298 bool allow_gnu_extensions_p;
1300 /* TRUE if the `>' token should be interpreted as the greater-than
1301 operator. FALSE if it is the end of a template-id or
1302 template-parameter-list. */
1303 bool greater_than_is_operator_p;
1305 /* TRUE if default arguments are allowed within a parameter list
1306 that starts at this point. FALSE if only a gnu extension makes
1307 them permissable. */
1308 bool default_arg_ok_p;
1310 /* TRUE if we are parsing an integral constant-expression. See
1311 [expr.const] for a precise definition. */
1312 /* FIXME: Need to implement code that checks this flag. */
1313 bool constant_expression_p;
1315 /* TRUE if local variable names and `this' are forbidden in the
1317 bool local_variables_forbidden_p;
1319 /* TRUE if the declaration we are parsing is part of a
1320 linkage-specification of the form `extern string-literal
1322 bool in_unbraced_linkage_specification_p;
1324 /* TRUE if we are presently parsing a declarator, after the
1325 direct-declarator. */
1326 bool in_declarator_p;
1328 /* If non-NULL, then we are parsing a construct where new type
1329 definitions are not permitted. The string stored here will be
1330 issued as an error message if a type is defined. */
1331 const char *type_definition_forbidden_message;
1333 /* A TREE_LIST of queues of functions whose bodies have been lexed,
1334 but may not have been parsed. These functions are friends of
1335 members defined within a class-specification; they are not
1336 procssed until the class is complete. The active queue is at the
1339 Within each queue, functions appear in the reverse order that
1340 they appeared in the source. Each TREE_VALUE is a
1341 FUNCTION_DECL of TEMPLATE_DECL corresponding to a member
1343 tree unparsed_functions_queues;
1345 /* The number of classes whose definitions are currently in
1347 unsigned num_classes_being_defined;
1349 /* The number of template parameter lists that apply directly to the
1350 current declaration. */
1351 unsigned num_template_parameter_lists;
1353 /* List of access checks lists, used to prevent GC collection while
1355 tree access_checks_lists;
1358 /* The type of a function that parses some kind of expression */
1359 typedef tree (*cp_parser_expression_fn) PARAMS ((cp_parser *));
1363 /* Constructors and destructors. */
1365 static cp_parser *cp_parser_new
1368 /* Routines to parse various constructs.
1370 Those that return `tree' will return the error_mark_node (rather
1371 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1372 Sometimes, they will return an ordinary node if error-recovery was
1373 attempted, even though a parse error occurrred. So, to check
1374 whether or not a parse error occurred, you should always use
1375 cp_parser_error_occurred. If the construct is optional (indicated
1376 either by an `_opt' in the name of the function that does the
1377 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1378 the construct is not present. */
1380 /* Lexical conventions [gram.lex] */
1382 static tree cp_parser_identifier
1383 PARAMS ((cp_parser *));
1385 /* Basic concepts [gram.basic] */
1387 static bool cp_parser_translation_unit
1388 PARAMS ((cp_parser *));
1390 /* Expressions [gram.expr] */
1392 static tree cp_parser_primary_expression
1393 (cp_parser *, cp_parser_id_kind *, tree *);
1394 static tree cp_parser_id_expression
1395 PARAMS ((cp_parser *, bool, bool, bool *));
1396 static tree cp_parser_unqualified_id
1397 PARAMS ((cp_parser *, bool, bool));
1398 static tree cp_parser_nested_name_specifier_opt
1399 (cp_parser *, bool, bool, bool);
1400 static tree cp_parser_nested_name_specifier
1401 (cp_parser *, bool, bool, bool);
1402 static tree cp_parser_class_or_namespace_name
1403 (cp_parser *, bool, bool, bool, bool);
1404 static tree cp_parser_postfix_expression
1405 (cp_parser *, bool);
1406 static tree cp_parser_expression_list
1407 PARAMS ((cp_parser *));
1408 static void cp_parser_pseudo_destructor_name
1409 PARAMS ((cp_parser *, tree *, tree *));
1410 static tree cp_parser_unary_expression
1411 (cp_parser *, bool);
1412 static enum tree_code cp_parser_unary_operator
1413 PARAMS ((cp_token *));
1414 static tree cp_parser_new_expression
1415 PARAMS ((cp_parser *));
1416 static tree cp_parser_new_placement
1417 PARAMS ((cp_parser *));
1418 static tree cp_parser_new_type_id
1419 PARAMS ((cp_parser *));
1420 static tree cp_parser_new_declarator_opt
1421 PARAMS ((cp_parser *));
1422 static tree cp_parser_direct_new_declarator
1423 PARAMS ((cp_parser *));
1424 static tree cp_parser_new_initializer
1425 PARAMS ((cp_parser *));
1426 static tree cp_parser_delete_expression
1427 PARAMS ((cp_parser *));
1428 static tree cp_parser_cast_expression
1429 (cp_parser *, bool);
1430 static tree cp_parser_pm_expression
1431 PARAMS ((cp_parser *));
1432 static tree cp_parser_multiplicative_expression
1433 PARAMS ((cp_parser *));
1434 static tree cp_parser_additive_expression
1435 PARAMS ((cp_parser *));
1436 static tree cp_parser_shift_expression
1437 PARAMS ((cp_parser *));
1438 static tree cp_parser_relational_expression
1439 PARAMS ((cp_parser *));
1440 static tree cp_parser_equality_expression
1441 PARAMS ((cp_parser *));
1442 static tree cp_parser_and_expression
1443 PARAMS ((cp_parser *));
1444 static tree cp_parser_exclusive_or_expression
1445 PARAMS ((cp_parser *));
1446 static tree cp_parser_inclusive_or_expression
1447 PARAMS ((cp_parser *));
1448 static tree cp_parser_logical_and_expression
1449 PARAMS ((cp_parser *));
1450 static tree cp_parser_logical_or_expression
1451 PARAMS ((cp_parser *));
1452 static tree cp_parser_conditional_expression
1453 PARAMS ((cp_parser *));
1454 static tree cp_parser_question_colon_clause
1455 PARAMS ((cp_parser *, tree));
1456 static tree cp_parser_assignment_expression
1457 PARAMS ((cp_parser *));
1458 static enum tree_code cp_parser_assignment_operator_opt
1459 PARAMS ((cp_parser *));
1460 static tree cp_parser_expression
1461 PARAMS ((cp_parser *));
1462 static tree cp_parser_constant_expression
1463 PARAMS ((cp_parser *));
1465 /* Statements [gram.stmt.stmt] */
1467 static void cp_parser_statement
1468 PARAMS ((cp_parser *));
1469 static tree cp_parser_labeled_statement
1470 PARAMS ((cp_parser *));
1471 static tree cp_parser_expression_statement
1472 PARAMS ((cp_parser *));
1473 static tree cp_parser_compound_statement
1475 static void cp_parser_statement_seq_opt
1476 PARAMS ((cp_parser *));
1477 static tree cp_parser_selection_statement
1478 PARAMS ((cp_parser *));
1479 static tree cp_parser_condition
1480 PARAMS ((cp_parser *));
1481 static tree cp_parser_iteration_statement
1482 PARAMS ((cp_parser *));
1483 static void cp_parser_for_init_statement
1484 PARAMS ((cp_parser *));
1485 static tree cp_parser_jump_statement
1486 PARAMS ((cp_parser *));
1487 static void cp_parser_declaration_statement
1488 PARAMS ((cp_parser *));
1490 static tree cp_parser_implicitly_scoped_statement
1491 PARAMS ((cp_parser *));
1492 static void cp_parser_already_scoped_statement
1493 PARAMS ((cp_parser *));
1495 /* Declarations [gram.dcl.dcl] */
1497 static void cp_parser_declaration_seq_opt
1498 PARAMS ((cp_parser *));
1499 static void cp_parser_declaration
1500 PARAMS ((cp_parser *));
1501 static void cp_parser_block_declaration
1502 PARAMS ((cp_parser *, bool));
1503 static void cp_parser_simple_declaration
1504 PARAMS ((cp_parser *, bool));
1505 static tree cp_parser_decl_specifier_seq
1506 PARAMS ((cp_parser *, cp_parser_flags, tree *, bool *));
1507 static tree cp_parser_storage_class_specifier_opt
1508 PARAMS ((cp_parser *));
1509 static tree cp_parser_function_specifier_opt
1510 PARAMS ((cp_parser *));
1511 static tree cp_parser_type_specifier
1512 (cp_parser *, cp_parser_flags, bool, bool, bool *, bool *);
1513 static tree cp_parser_simple_type_specifier
1514 PARAMS ((cp_parser *, cp_parser_flags));
1515 static tree cp_parser_type_name
1516 PARAMS ((cp_parser *));
1517 static tree cp_parser_elaborated_type_specifier
1518 PARAMS ((cp_parser *, bool, bool));
1519 static tree cp_parser_enum_specifier
1520 PARAMS ((cp_parser *));
1521 static void cp_parser_enumerator_list
1522 PARAMS ((cp_parser *, tree));
1523 static void cp_parser_enumerator_definition
1524 PARAMS ((cp_parser *, tree));
1525 static tree cp_parser_namespace_name
1526 PARAMS ((cp_parser *));
1527 static void cp_parser_namespace_definition
1528 PARAMS ((cp_parser *));
1529 static void cp_parser_namespace_body
1530 PARAMS ((cp_parser *));
1531 static tree cp_parser_qualified_namespace_specifier
1532 PARAMS ((cp_parser *));
1533 static void cp_parser_namespace_alias_definition
1534 PARAMS ((cp_parser *));
1535 static void cp_parser_using_declaration
1536 PARAMS ((cp_parser *));
1537 static void cp_parser_using_directive
1538 PARAMS ((cp_parser *));
1539 static void cp_parser_asm_definition
1540 PARAMS ((cp_parser *));
1541 static void cp_parser_linkage_specification
1542 PARAMS ((cp_parser *));
1544 /* Declarators [gram.dcl.decl] */
1546 static tree cp_parser_init_declarator
1547 PARAMS ((cp_parser *, tree, tree, tree, bool, bool, bool *));
1548 static tree cp_parser_declarator
1549 PARAMS ((cp_parser *, cp_parser_declarator_kind, bool *));
1550 static tree cp_parser_direct_declarator
1551 PARAMS ((cp_parser *, cp_parser_declarator_kind, bool *));
1552 static enum tree_code cp_parser_ptr_operator
1553 PARAMS ((cp_parser *, tree *, tree *));
1554 static tree cp_parser_cv_qualifier_seq_opt
1555 PARAMS ((cp_parser *));
1556 static tree cp_parser_cv_qualifier_opt
1557 PARAMS ((cp_parser *));
1558 static tree cp_parser_declarator_id
1559 PARAMS ((cp_parser *));
1560 static tree cp_parser_type_id
1561 PARAMS ((cp_parser *));
1562 static tree cp_parser_type_specifier_seq
1563 PARAMS ((cp_parser *));
1564 static tree cp_parser_parameter_declaration_clause
1565 PARAMS ((cp_parser *));
1566 static tree cp_parser_parameter_declaration_list
1567 PARAMS ((cp_parser *));
1568 static tree cp_parser_parameter_declaration
1569 PARAMS ((cp_parser *, bool));
1570 static tree cp_parser_function_definition
1571 PARAMS ((cp_parser *, bool *));
1572 static void cp_parser_function_body
1574 static tree cp_parser_initializer
1575 PARAMS ((cp_parser *, bool *));
1576 static tree cp_parser_initializer_clause
1577 PARAMS ((cp_parser *));
1578 static tree cp_parser_initializer_list
1579 PARAMS ((cp_parser *));
1581 static bool cp_parser_ctor_initializer_opt_and_function_body
1584 /* Classes [gram.class] */
1586 static tree cp_parser_class_name
1587 (cp_parser *, bool, bool, bool, bool, bool, bool);
1588 static tree cp_parser_class_specifier
1589 PARAMS ((cp_parser *));
1590 static tree cp_parser_class_head
1591 PARAMS ((cp_parser *, bool *, bool *, tree *));
1592 static enum tag_types cp_parser_class_key
1593 PARAMS ((cp_parser *));
1594 static void cp_parser_member_specification_opt
1595 PARAMS ((cp_parser *));
1596 static void cp_parser_member_declaration
1597 PARAMS ((cp_parser *));
1598 static tree cp_parser_pure_specifier
1599 PARAMS ((cp_parser *));
1600 static tree cp_parser_constant_initializer
1601 PARAMS ((cp_parser *));
1603 /* Derived classes [gram.class.derived] */
1605 static tree cp_parser_base_clause
1606 PARAMS ((cp_parser *));
1607 static tree cp_parser_base_specifier
1608 PARAMS ((cp_parser *));
1610 /* Special member functions [gram.special] */
1612 static tree cp_parser_conversion_function_id
1613 PARAMS ((cp_parser *));
1614 static tree cp_parser_conversion_type_id
1615 PARAMS ((cp_parser *));
1616 static tree cp_parser_conversion_declarator_opt
1617 PARAMS ((cp_parser *));
1618 static bool cp_parser_ctor_initializer_opt
1619 PARAMS ((cp_parser *));
1620 static void cp_parser_mem_initializer_list
1621 PARAMS ((cp_parser *));
1622 static tree cp_parser_mem_initializer
1623 PARAMS ((cp_parser *));
1624 static tree cp_parser_mem_initializer_id
1625 PARAMS ((cp_parser *));
1627 /* Overloading [gram.over] */
1629 static tree cp_parser_operator_function_id
1630 PARAMS ((cp_parser *));
1631 static tree cp_parser_operator
1632 PARAMS ((cp_parser *));
1634 /* Templates [gram.temp] */
1636 static void cp_parser_template_declaration
1637 PARAMS ((cp_parser *, bool));
1638 static tree cp_parser_template_parameter_list
1639 PARAMS ((cp_parser *));
1640 static tree cp_parser_template_parameter
1641 PARAMS ((cp_parser *));
1642 static tree cp_parser_type_parameter
1643 PARAMS ((cp_parser *));
1644 static tree cp_parser_template_id
1645 PARAMS ((cp_parser *, bool, bool));
1646 static tree cp_parser_template_name
1647 PARAMS ((cp_parser *, bool, bool));
1648 static tree cp_parser_template_argument_list
1649 PARAMS ((cp_parser *));
1650 static tree cp_parser_template_argument
1651 PARAMS ((cp_parser *));
1652 static void cp_parser_explicit_instantiation
1653 PARAMS ((cp_parser *));
1654 static void cp_parser_explicit_specialization
1655 PARAMS ((cp_parser *));
1657 /* Exception handling [gram.exception] */
1659 static tree cp_parser_try_block
1660 PARAMS ((cp_parser *));
1661 static bool cp_parser_function_try_block
1662 PARAMS ((cp_parser *));
1663 static void cp_parser_handler_seq
1664 PARAMS ((cp_parser *));
1665 static void cp_parser_handler
1666 PARAMS ((cp_parser *));
1667 static tree cp_parser_exception_declaration
1668 PARAMS ((cp_parser *));
1669 static tree cp_parser_throw_expression
1670 PARAMS ((cp_parser *));
1671 static tree cp_parser_exception_specification_opt
1672 PARAMS ((cp_parser *));
1673 static tree cp_parser_type_id_list
1674 PARAMS ((cp_parser *));
1676 /* GNU Extensions */
1678 static tree cp_parser_asm_specification_opt
1679 PARAMS ((cp_parser *));
1680 static tree cp_parser_asm_operand_list
1681 PARAMS ((cp_parser *));
1682 static tree cp_parser_asm_clobber_list
1683 PARAMS ((cp_parser *));
1684 static tree cp_parser_attributes_opt
1685 PARAMS ((cp_parser *));
1686 static tree cp_parser_attribute_list
1687 PARAMS ((cp_parser *));
1688 static bool cp_parser_extension_opt
1689 PARAMS ((cp_parser *, int *));
1690 static void cp_parser_label_declaration
1691 PARAMS ((cp_parser *));
1693 /* Utility Routines */
1695 static tree cp_parser_lookup_name
1696 PARAMS ((cp_parser *, tree, bool, bool, bool, bool));
1697 static tree cp_parser_lookup_name_simple
1698 PARAMS ((cp_parser *, tree));
1699 static tree cp_parser_resolve_typename_type
1700 PARAMS ((cp_parser *, tree));
1701 static tree cp_parser_maybe_treat_template_as_class
1703 static bool cp_parser_check_declarator_template_parameters
1704 PARAMS ((cp_parser *, tree));
1705 static bool cp_parser_check_template_parameters
1706 PARAMS ((cp_parser *, unsigned));
1707 static tree cp_parser_binary_expression
1708 PARAMS ((cp_parser *,
1709 const cp_parser_token_tree_map,
1710 cp_parser_expression_fn));
1711 static tree cp_parser_global_scope_opt
1712 PARAMS ((cp_parser *, bool));
1713 static bool cp_parser_constructor_declarator_p
1714 (cp_parser *, bool);
1715 static tree cp_parser_function_definition_from_specifiers_and_declarator
1716 PARAMS ((cp_parser *, tree, tree, tree, tree));
1717 static tree cp_parser_function_definition_after_declarator
1718 PARAMS ((cp_parser *, bool));
1719 static void cp_parser_template_declaration_after_export
1720 PARAMS ((cp_parser *, bool));
1721 static tree cp_parser_single_declaration
1722 PARAMS ((cp_parser *, bool, bool *));
1723 static tree cp_parser_functional_cast
1724 PARAMS ((cp_parser *, tree));
1725 static void cp_parser_late_parsing_for_member
1726 PARAMS ((cp_parser *, tree));
1727 static void cp_parser_late_parsing_default_args
1728 (cp_parser *, tree);
1729 static tree cp_parser_sizeof_operand
1730 PARAMS ((cp_parser *, enum rid));
1731 static bool cp_parser_declares_only_class_p
1732 PARAMS ((cp_parser *));
1733 static bool cp_parser_friend_p
1735 static cp_token *cp_parser_require
1736 PARAMS ((cp_parser *, enum cpp_ttype, const char *));
1737 static cp_token *cp_parser_require_keyword
1738 PARAMS ((cp_parser *, enum rid, const char *));
1739 static bool cp_parser_token_starts_function_definition_p
1740 PARAMS ((cp_token *));
1741 static bool cp_parser_next_token_starts_class_definition_p
1743 static enum tag_types cp_parser_token_is_class_key
1744 PARAMS ((cp_token *));
1745 static void cp_parser_check_class_key
1746 (enum tag_types, tree type);
1747 static bool cp_parser_optional_template_keyword
1749 static void cp_parser_cache_group
1750 (cp_parser *, cp_token_cache *, enum cpp_ttype, unsigned);
1751 static void cp_parser_parse_tentatively
1752 PARAMS ((cp_parser *));
1753 static void cp_parser_commit_to_tentative_parse
1754 PARAMS ((cp_parser *));
1755 static void cp_parser_abort_tentative_parse
1756 PARAMS ((cp_parser *));
1757 static bool cp_parser_parse_definitely
1758 PARAMS ((cp_parser *));
1759 static inline bool cp_parser_parsing_tentatively
1760 PARAMS ((cp_parser *));
1761 static bool cp_parser_committed_to_tentative_parse
1762 PARAMS ((cp_parser *));
1763 static void cp_parser_error
1764 PARAMS ((cp_parser *, const char *));
1765 static bool cp_parser_simulate_error
1766 PARAMS ((cp_parser *));
1767 static void cp_parser_check_type_definition
1768 PARAMS ((cp_parser *));
1769 static bool cp_parser_skip_to_closing_parenthesis
1770 PARAMS ((cp_parser *));
1771 static bool cp_parser_skip_to_closing_parenthesis_or_comma
1773 static void cp_parser_skip_to_end_of_statement
1774 PARAMS ((cp_parser *));
1775 static void cp_parser_skip_to_end_of_block_or_statement
1776 PARAMS ((cp_parser *));
1777 static void cp_parser_skip_to_closing_brace
1779 static void cp_parser_skip_until_found
1780 PARAMS ((cp_parser *, enum cpp_ttype, const char *));
1781 static bool cp_parser_error_occurred
1782 PARAMS ((cp_parser *));
1783 static bool cp_parser_allow_gnu_extensions_p
1784 PARAMS ((cp_parser *));
1785 static bool cp_parser_is_string_literal
1786 PARAMS ((cp_token *));
1787 static bool cp_parser_is_keyword
1788 PARAMS ((cp_token *, enum rid));
1789 static bool cp_parser_dependent_type_p
1791 static bool cp_parser_value_dependent_expression_p
1793 static bool cp_parser_type_dependent_expression_p
1795 static bool cp_parser_dependent_template_arg_p
1797 static bool cp_parser_dependent_template_id_p
1799 static bool cp_parser_dependent_template_p
1801 static void cp_parser_defer_access_check
1802 (cp_parser *, tree, tree);
1803 static void cp_parser_start_deferring_access_checks
1805 static tree cp_parser_stop_deferring_access_checks
1806 PARAMS ((cp_parser *));
1807 static void cp_parser_perform_deferred_access_checks
1809 static tree cp_parser_scope_through_which_access_occurs
1812 /* Returns non-zero if we are parsing tentatively. */
1815 cp_parser_parsing_tentatively (parser)
1818 return parser->context->next != NULL;
1821 /* Returns non-zero if TOKEN is a string literal. */
1824 cp_parser_is_string_literal (token)
1827 return (token->type == CPP_STRING || token->type == CPP_WSTRING);
1830 /* Returns non-zero if TOKEN is the indicated KEYWORD. */
1833 cp_parser_is_keyword (token, keyword)
1837 return token->keyword == keyword;
1840 /* Returns TRUE if TYPE is dependent, in the sense of
1844 cp_parser_dependent_type_p (type)
1849 if (!processing_template_decl)
1852 /* If the type is NULL, we have not computed a type for the entity
1853 in question; in that case, the type is dependent. */
1857 /* Erroneous types can be considered non-dependent. */
1858 if (type == error_mark_node)
1863 A type is dependent if it is:
1865 -- a template parameter. */
1866 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
1868 /* -- a qualified-id with a nested-name-specifier which contains a
1869 class-name that names a dependent type or whose unqualified-id
1870 names a dependent type. */
1871 if (TREE_CODE (type) == TYPENAME_TYPE)
1873 /* -- a cv-qualified type where the cv-unqualified type is
1875 type = TYPE_MAIN_VARIANT (type);
1876 /* -- a compound type constructed from any dependent type. */
1877 if (TYPE_PTRMEM_P (type) || TYPE_PTRMEMFUNC_P (type))
1878 return (cp_parser_dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type))
1879 || cp_parser_dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
1881 else if (TREE_CODE (type) == POINTER_TYPE
1882 || TREE_CODE (type) == REFERENCE_TYPE)
1883 return cp_parser_dependent_type_p (TREE_TYPE (type));
1884 else if (TREE_CODE (type) == FUNCTION_TYPE
1885 || TREE_CODE (type) == METHOD_TYPE)
1889 if (cp_parser_dependent_type_p (TREE_TYPE (type)))
1891 for (arg_type = TYPE_ARG_TYPES (type);
1893 arg_type = TREE_CHAIN (arg_type))
1894 if (cp_parser_dependent_type_p (TREE_VALUE (arg_type)))
1898 /* -- an array type constructed from any dependent type or whose
1899 size is specified by a constant expression that is
1901 if (TREE_CODE (type) == ARRAY_TYPE)
1903 if (TYPE_DOMAIN (type)
1904 && ((cp_parser_value_dependent_expression_p
1905 (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
1906 || (cp_parser_type_dependent_expression_p
1907 (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))))
1909 return cp_parser_dependent_type_p (TREE_TYPE (type));
1911 /* -- a template-id in which either the template name is a template
1912 parameter or any of the template arguments is a dependent type or
1913 an expression that is type-dependent or value-dependent.
1915 This language seems somewhat confused; for example, it does not
1916 discuss template template arguments. Therefore, we use the
1917 definition for dependent template arguments in [temp.dep.temp]. */
1918 if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INFO (type)
1919 && (cp_parser_dependent_template_id_p
1920 (CLASSTYPE_TI_TEMPLATE (type),
1921 CLASSTYPE_TI_ARGS (type))))
1923 else if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
1925 /* All TYPEOF_TYPEs are dependent; if the argument of the `typeof'
1926 expression is not type-dependent, then it should already been
1928 if (TREE_CODE (type) == TYPEOF_TYPE)
1930 /* The standard does not specifically mention types that are local
1931 to template functions or local classes, but they should be
1932 considered dependent too. For example:
1934 template <int I> void f() {
1939 The size of `E' cannot be known until the value of `I' has been
1940 determined. Therefore, `E' must be considered dependent. */
1941 scope = TYPE_CONTEXT (type);
1942 if (scope && TYPE_P (scope))
1943 return cp_parser_dependent_type_p (scope);
1944 else if (scope && TREE_CODE (scope) == FUNCTION_DECL)
1945 return cp_parser_type_dependent_expression_p (scope);
1947 /* Other types are non-dependent. */
1951 /* Returns TRUE if the EXPRESSION is value-dependent. */
1954 cp_parser_value_dependent_expression_p (tree expression)
1956 if (!processing_template_decl)
1959 /* A name declared with a dependent type. */
1960 if (DECL_P (expression)
1961 && cp_parser_dependent_type_p (TREE_TYPE (expression)))
1963 /* A non-type template parameter. */
1964 if ((TREE_CODE (expression) == CONST_DECL
1965 && DECL_TEMPLATE_PARM_P (expression))
1966 || TREE_CODE (expression) == TEMPLATE_PARM_INDEX)
1968 /* A constant with integral or enumeration type and is initialized
1969 with an expression that is value-dependent. */
1970 if (TREE_CODE (expression) == VAR_DECL
1971 && DECL_INITIAL (expression)
1972 && (CP_INTEGRAL_TYPE_P (TREE_TYPE (expression))
1973 || TREE_CODE (TREE_TYPE (expression)) == ENUMERAL_TYPE)
1974 && cp_parser_value_dependent_expression_p (DECL_INITIAL (expression)))
1976 /* These expressions are value-dependent if the type to which the
1977 cast occurs is dependent. */
1978 if ((TREE_CODE (expression) == DYNAMIC_CAST_EXPR
1979 || TREE_CODE (expression) == STATIC_CAST_EXPR
1980 || TREE_CODE (expression) == CONST_CAST_EXPR
1981 || TREE_CODE (expression) == REINTERPRET_CAST_EXPR
1982 || TREE_CODE (expression) == CAST_EXPR)
1983 && cp_parser_dependent_type_p (TREE_TYPE (expression)))
1985 /* A `sizeof' expression where the sizeof operand is a type is
1986 value-dependent if the type is dependent. If the type was not
1987 dependent, we would no longer have a SIZEOF_EXPR, so any
1988 SIZEOF_EXPR is dependent. */
1989 if (TREE_CODE (expression) == SIZEOF_EXPR)
1991 /* A constant expression is value-dependent if any subexpression is
1993 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (expression))))
1995 switch (TREE_CODE_CLASS (TREE_CODE (expression)))
1998 return (cp_parser_value_dependent_expression_p
1999 (TREE_OPERAND (expression, 0)));
2002 return ((cp_parser_value_dependent_expression_p
2003 (TREE_OPERAND (expression, 0)))
2004 || (cp_parser_value_dependent_expression_p
2005 (TREE_OPERAND (expression, 1))));
2010 i < TREE_CODE_LENGTH (TREE_CODE (expression));
2012 if (cp_parser_value_dependent_expression_p
2013 (TREE_OPERAND (expression, i)))
2020 /* The expression is not value-dependent. */
2024 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
2028 cp_parser_type_dependent_expression_p (expression)
2031 if (!processing_template_decl)
2034 /* Some expression forms are never type-dependent. */
2035 if (TREE_CODE (expression) == PSEUDO_DTOR_EXPR
2036 || TREE_CODE (expression) == SIZEOF_EXPR
2037 || TREE_CODE (expression) == ALIGNOF_EXPR
2038 || TREE_CODE (expression) == TYPEID_EXPR
2039 || TREE_CODE (expression) == DELETE_EXPR
2040 || TREE_CODE (expression) == VEC_DELETE_EXPR
2041 || TREE_CODE (expression) == THROW_EXPR)
2044 /* The types of these expressions depends only on the type to which
2046 if (TREE_CODE (expression) == DYNAMIC_CAST_EXPR
2047 || TREE_CODE (expression) == STATIC_CAST_EXPR
2048 || TREE_CODE (expression) == CONST_CAST_EXPR
2049 || TREE_CODE (expression) == REINTERPRET_CAST_EXPR
2050 || TREE_CODE (expression) == CAST_EXPR)
2051 return cp_parser_dependent_type_p (TREE_TYPE (expression));
2052 /* The types of these expressions depends only on the type created
2053 by the expression. */
2054 else if (TREE_CODE (expression) == NEW_EXPR
2055 || TREE_CODE (expression) == VEC_NEW_EXPR)
2056 return cp_parser_dependent_type_p (TREE_OPERAND (expression, 1));
2058 if (TREE_CODE (expression) == FUNCTION_DECL
2059 && DECL_LANG_SPECIFIC (expression)
2060 && DECL_TEMPLATE_INFO (expression)
2061 && (cp_parser_dependent_template_id_p
2062 (DECL_TI_TEMPLATE (expression),
2063 INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression)))))
2066 return (cp_parser_dependent_type_p (TREE_TYPE (expression)));
2069 /* Returns TRUE if the ARG (a template argument) is dependent. */
2072 cp_parser_dependent_template_arg_p (tree arg)
2074 if (!processing_template_decl)
2077 if (TREE_CODE (arg) == TEMPLATE_DECL
2078 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
2079 return cp_parser_dependent_template_p (arg);
2080 else if (TYPE_P (arg))
2081 return cp_parser_dependent_type_p (arg);
2083 return (cp_parser_type_dependent_expression_p (arg)
2084 || cp_parser_value_dependent_expression_p (arg));
2087 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
2090 cp_parser_dependent_template_id_p (tree tmpl, tree args)
2094 if (cp_parser_dependent_template_p (tmpl))
2096 for (i = 0; i < TREE_VEC_LENGTH (args); ++i)
2097 if (cp_parser_dependent_template_arg_p (TREE_VEC_ELT (args, i)))
2102 /* Returns TRUE if the template TMPL is dependent. */
2105 cp_parser_dependent_template_p (tree tmpl)
2107 /* Template template parameters are dependent. */
2108 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)
2109 || TREE_CODE (tmpl) == TEMPLATE_TEMPLATE_PARM)
2111 /* So are member templates of dependent classes. */
2112 if (TYPE_P (CP_DECL_CONTEXT (tmpl)))
2113 return cp_parser_dependent_type_p (DECL_CONTEXT (tmpl));
2117 /* Defer checking the accessibility of DECL, when looked up in
2121 cp_parser_defer_access_check (cp_parser *parser,
2127 /* If we are not supposed to defer access checks, just check now. */
2128 if (!parser->context->deferring_access_checks_p)
2130 enforce_access (class_type, decl);
2134 /* See if we are already going to perform this check. */
2135 for (check = parser->context->deferred_access_checks;
2137 check = TREE_CHAIN (check))
2138 if (TREE_VALUE (check) == decl
2139 && same_type_p (TREE_PURPOSE (check), class_type))
2141 /* If not, record the check. */
2142 parser->context->deferred_access_checks
2143 = tree_cons (class_type, decl, parser->context->deferred_access_checks);
2146 /* Start deferring access control checks. */
2149 cp_parser_start_deferring_access_checks (cp_parser *parser)
2151 parser->context->deferring_access_checks_p = true;
2154 /* Stop deferring access control checks. Returns a TREE_LIST
2155 representing the deferred checks. The TREE_PURPOSE of each node is
2156 the type through which the access occurred; the TREE_VALUE is the
2157 declaration named. */
2160 cp_parser_stop_deferring_access_checks (parser)
2165 parser->context->deferring_access_checks_p = false;
2166 access_checks = parser->context->deferred_access_checks;
2167 parser->context->deferred_access_checks = NULL_TREE;
2169 return access_checks;
2172 /* Perform the deferred ACCESS_CHECKS, whose representation is as
2173 documented with cp_parser_stop_deferrring_access_checks. */
2176 cp_parser_perform_deferred_access_checks (access_checks)
2179 tree deferred_check;
2181 /* Look through all the deferred checks. */
2182 for (deferred_check = access_checks;
2184 deferred_check = TREE_CHAIN (deferred_check))
2186 enforce_access (TREE_PURPOSE (deferred_check),
2187 TREE_VALUE (deferred_check));
2190 /* Returns the scope through which DECL is being accessed, or
2191 NULL_TREE if DECL is not a member. If OBJECT_TYPE is non-NULL, we
2192 have just seen `x->' or `x.' and OBJECT_TYPE is the type of `*x',
2193 or `x', respectively. If the DECL was named as `A::B' then
2194 NESTED_NAME_SPECIFIER is `A'. */
2197 cp_parser_scope_through_which_access_occurs (decl,
2199 nested_name_specifier)
2202 tree nested_name_specifier;
2205 tree qualifying_type = NULL_TREE;
2207 /* Determine the SCOPE of DECL. */
2208 scope = context_for_name_lookup (decl);
2209 /* If the SCOPE is not a type, then DECL is not a member. */
2210 if (!TYPE_P (scope))
2212 /* Figure out the type through which DECL is being accessed. */
2213 if (object_type && DERIVED_FROM_P (scope, object_type))
2214 /* If we are processing a `->' or `.' expression, use the type of the
2216 qualifying_type = object_type;
2217 else if (nested_name_specifier)
2219 /* If the reference is to a non-static member of the
2220 current class, treat it as if it were referenced through
2222 if (DECL_NONSTATIC_MEMBER_P (decl)
2223 && current_class_ptr
2224 && DERIVED_FROM_P (scope, current_class_type))
2225 qualifying_type = current_class_type;
2226 /* Otherwise, use the type indicated by the
2227 nested-name-specifier. */
2229 qualifying_type = nested_name_specifier;
2232 /* Otherwise, the name must be from the current class or one of
2234 qualifying_type = currently_open_derived_class (scope);
2236 return qualifying_type;
2239 /* Issue the indicated error MESSAGE. */
2242 cp_parser_error (parser, message)
2244 const char *message;
2246 /* Output the MESSAGE -- unless we're parsing tentatively. */
2247 if (!cp_parser_simulate_error (parser))
2251 /* If we are parsing tentatively, remember that an error has occurred
2252 during this tentative parse. Returns true if the error was
2253 simulated; false if a messgae should be issued by the caller. */
2256 cp_parser_simulate_error (parser)
2259 if (cp_parser_parsing_tentatively (parser)
2260 && !cp_parser_committed_to_tentative_parse (parser))
2262 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2268 /* This function is called when a type is defined. If type
2269 definitions are forbidden at this point, an error message is
2273 cp_parser_check_type_definition (parser)
2276 /* If types are forbidden here, issue a message. */
2277 if (parser->type_definition_forbidden_message)
2278 /* Use `%s' to print the string in case there are any escape
2279 characters in the message. */
2280 error ("%s", parser->type_definition_forbidden_message);
2283 /* Consume tokens up to, and including, the next non-nested closing `)'.
2284 Returns TRUE iff we found a closing `)'. */
2287 cp_parser_skip_to_closing_parenthesis (cp_parser *parser)
2289 unsigned nesting_depth = 0;
2295 /* If we've run out of tokens, then there is no closing `)'. */
2296 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2298 /* Consume the token. */
2299 token = cp_lexer_consume_token (parser->lexer);
2300 /* If it is an `(', we have entered another level of nesting. */
2301 if (token->type == CPP_OPEN_PAREN)
2303 /* If it is a `)', then we might be done. */
2304 else if (token->type == CPP_CLOSE_PAREN && nesting_depth-- == 0)
2309 /* Consume tokens until the next token is a `)', or a `,'. Returns
2310 TRUE if the next token is a `,'. */
2313 cp_parser_skip_to_closing_parenthesis_or_comma (cp_parser *parser)
2315 unsigned nesting_depth = 0;
2319 cp_token *token = cp_lexer_peek_token (parser->lexer);
2321 /* If we've run out of tokens, then there is no closing `)'. */
2322 if (token->type == CPP_EOF)
2324 /* If it is a `,' stop. */
2325 else if (token->type == CPP_COMMA && nesting_depth-- == 0)
2327 /* If it is a `)', stop. */
2328 else if (token->type == CPP_CLOSE_PAREN && nesting_depth-- == 0)
2330 /* If it is an `(', we have entered another level of nesting. */
2331 else if (token->type == CPP_OPEN_PAREN)
2333 /* Consume the token. */
2334 token = cp_lexer_consume_token (parser->lexer);
2338 /* Consume tokens until we reach the end of the current statement.
2339 Normally, that will be just before consuming a `;'. However, if a
2340 non-nested `}' comes first, then we stop before consuming that. */
2343 cp_parser_skip_to_end_of_statement (parser)
2346 unsigned nesting_depth = 0;
2352 /* Peek at the next token. */
2353 token = cp_lexer_peek_token (parser->lexer);
2354 /* If we've run out of tokens, stop. */
2355 if (token->type == CPP_EOF)
2357 /* If the next token is a `;', we have reached the end of the
2359 if (token->type == CPP_SEMICOLON && !nesting_depth)
2361 /* If the next token is a non-nested `}', then we have reached
2362 the end of the current block. */
2363 if (token->type == CPP_CLOSE_BRACE)
2365 /* If this is a non-nested `}', stop before consuming it.
2366 That way, when confronted with something like:
2370 we stop before consuming the closing `}', even though we
2371 have not yet reached a `;'. */
2372 if (nesting_depth == 0)
2374 /* If it is the closing `}' for a block that we have
2375 scanned, stop -- but only after consuming the token.
2381 we will stop after the body of the erroneously declared
2382 function, but before consuming the following `typedef'
2384 if (--nesting_depth == 0)
2386 cp_lexer_consume_token (parser->lexer);
2390 /* If it the next token is a `{', then we are entering a new
2391 block. Consume the entire block. */
2392 else if (token->type == CPP_OPEN_BRACE)
2394 /* Consume the token. */
2395 cp_lexer_consume_token (parser->lexer);
2399 /* Skip tokens until we have consumed an entire block, or until we
2400 have consumed a non-nested `;'. */
2403 cp_parser_skip_to_end_of_block_or_statement (parser)
2406 unsigned nesting_depth = 0;
2412 /* Peek at the next token. */
2413 token = cp_lexer_peek_token (parser->lexer);
2414 /* If we've run out of tokens, stop. */
2415 if (token->type == CPP_EOF)
2417 /* If the next token is a `;', we have reached the end of the
2419 if (token->type == CPP_SEMICOLON && !nesting_depth)
2421 /* Consume the `;'. */
2422 cp_lexer_consume_token (parser->lexer);
2425 /* Consume the token. */
2426 token = cp_lexer_consume_token (parser->lexer);
2427 /* If the next token is a non-nested `}', then we have reached
2428 the end of the current block. */
2429 if (token->type == CPP_CLOSE_BRACE
2430 && (nesting_depth == 0 || --nesting_depth == 0))
2432 /* If it the next token is a `{', then we are entering a new
2433 block. Consume the entire block. */
2434 if (token->type == CPP_OPEN_BRACE)
2439 /* Skip tokens until a non-nested closing curly brace is the next
2443 cp_parser_skip_to_closing_brace (cp_parser *parser)
2445 unsigned nesting_depth = 0;
2451 /* Peek at the next token. */
2452 token = cp_lexer_peek_token (parser->lexer);
2453 /* If we've run out of tokens, stop. */
2454 if (token->type == CPP_EOF)
2456 /* If the next token is a non-nested `}', then we have reached
2457 the end of the current block. */
2458 if (token->type == CPP_CLOSE_BRACE && nesting_depth-- == 0)
2460 /* If it the next token is a `{', then we are entering a new
2461 block. Consume the entire block. */
2462 else if (token->type == CPP_OPEN_BRACE)
2464 /* Consume the token. */
2465 cp_lexer_consume_token (parser->lexer);
2469 /* Create a new C++ parser. */
2476 parser = (cp_parser *) ggc_alloc_cleared (sizeof (cp_parser));
2477 parser->lexer = cp_lexer_new (/*main_lexer_p=*/true);
2478 parser->context = cp_parser_context_new (NULL);
2480 /* For now, we always accept GNU extensions. */
2481 parser->allow_gnu_extensions_p = 1;
2483 /* The `>' token is a greater-than operator, not the end of a
2485 parser->greater_than_is_operator_p = true;
2487 parser->default_arg_ok_p = true;
2489 /* We are not parsing a constant-expression. */
2490 parser->constant_expression_p = false;
2492 /* Local variable names are not forbidden. */
2493 parser->local_variables_forbidden_p = false;
2495 /* We are not procesing an `extern "C"' declaration. */
2496 parser->in_unbraced_linkage_specification_p = false;
2498 /* We are not processing a declarator. */
2499 parser->in_declarator_p = false;
2501 /* The unparsed function queue is empty. */
2502 parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2504 /* There are no classes being defined. */
2505 parser->num_classes_being_defined = 0;
2507 /* No template parameters apply. */
2508 parser->num_template_parameter_lists = 0;
2513 /* Lexical conventions [gram.lex] */
2515 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
2519 cp_parser_identifier (parser)
2524 /* Look for the identifier. */
2525 token = cp_parser_require (parser, CPP_NAME, "identifier");
2526 /* Return the value. */
2527 return token ? token->value : error_mark_node;
2530 /* Basic concepts [gram.basic] */
2532 /* Parse a translation-unit.
2535 declaration-seq [opt]
2537 Returns TRUE if all went well. */
2540 cp_parser_translation_unit (parser)
2545 cp_parser_declaration_seq_opt (parser);
2547 /* If there are no tokens left then all went well. */
2548 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2551 /* Otherwise, issue an error message. */
2552 cp_parser_error (parser, "expected declaration");
2556 /* Consume the EOF token. */
2557 cp_parser_require (parser, CPP_EOF, "end-of-file");
2560 finish_translation_unit ();
2562 /* All went well. */
2566 /* Expressions [gram.expr] */
2568 /* Parse a primary-expression.
2579 ( compound-statement )
2580 __builtin_va_arg ( assignment-expression , type-id )
2585 Returns a representation of the expression.
2587 *IDK indicates what kind of id-expression (if any) was present.
2589 *QUALIFYING_CLASS is set to a non-NULL value if the id-expression can be
2590 used as the operand of a pointer-to-member. In that case,
2591 *QUALIFYING_CLASS gives the class that is used as the qualifying
2592 class in the pointer-to-member. */
2595 cp_parser_primary_expression (cp_parser *parser,
2596 cp_parser_id_kind *idk,
2597 tree *qualifying_class)
2601 /* Assume the primary expression is not an id-expression. */
2602 *idk = CP_PARSER_ID_KIND_NONE;
2603 /* And that it cannot be used as pointer-to-member. */
2604 *qualifying_class = NULL_TREE;
2606 /* Peek at the next token. */
2607 token = cp_lexer_peek_token (parser->lexer);
2608 switch (token->type)
2621 token = cp_lexer_consume_token (parser->lexer);
2622 return token->value;
2624 case CPP_OPEN_PAREN:
2627 bool saved_greater_than_is_operator_p;
2629 /* Consume the `('. */
2630 cp_lexer_consume_token (parser->lexer);
2631 /* Within a parenthesized expression, a `>' token is always
2632 the greater-than operator. */
2633 saved_greater_than_is_operator_p
2634 = parser->greater_than_is_operator_p;
2635 parser->greater_than_is_operator_p = true;
2636 /* If we see `( { ' then we are looking at the beginning of
2637 a GNU statement-expression. */
2638 if (cp_parser_allow_gnu_extensions_p (parser)
2639 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
2641 /* Statement-expressions are not allowed by the standard. */
2643 pedwarn ("ISO C++ forbids braced-groups within expressions");
2645 /* And they're not allowed outside of a function-body; you
2646 cannot, for example, write:
2648 int i = ({ int j = 3; j + 1; });
2650 at class or namespace scope. */
2651 if (!at_function_scope_p ())
2652 error ("statement-expressions are allowed only inside functions");
2653 /* Start the statement-expression. */
2654 expr = begin_stmt_expr ();
2655 /* Parse the compound-statement. */
2656 cp_parser_compound_statement (parser);
2658 expr = finish_stmt_expr (expr);
2662 /* Parse the parenthesized expression. */
2663 expr = cp_parser_expression (parser);
2664 /* Let the front end know that this expression was
2665 enclosed in parentheses. This matters in case, for
2666 example, the expression is of the form `A::B', since
2667 `&A::B' might be a pointer-to-member, but `&(A::B)' is
2669 finish_parenthesized_expr (expr);
2671 /* The `>' token might be the end of a template-id or
2672 template-parameter-list now. */
2673 parser->greater_than_is_operator_p
2674 = saved_greater_than_is_operator_p;
2675 /* Consume the `)'. */
2676 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
2677 cp_parser_skip_to_end_of_statement (parser);
2683 switch (token->keyword)
2685 /* These two are the boolean literals. */
2687 cp_lexer_consume_token (parser->lexer);
2688 return boolean_true_node;
2690 cp_lexer_consume_token (parser->lexer);
2691 return boolean_false_node;
2693 /* The `__null' literal. */
2695 cp_lexer_consume_token (parser->lexer);
2698 /* Recognize the `this' keyword. */
2700 cp_lexer_consume_token (parser->lexer);
2701 if (parser->local_variables_forbidden_p)
2703 error ("`this' may not be used in this context");
2704 return error_mark_node;
2706 return finish_this_expr ();
2708 /* The `operator' keyword can be the beginning of an
2713 case RID_FUNCTION_NAME:
2714 case RID_PRETTY_FUNCTION_NAME:
2715 case RID_C99_FUNCTION_NAME:
2716 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
2717 __func__ are the names of variables -- but they are
2718 treated specially. Therefore, they are handled here,
2719 rather than relying on the generic id-expression logic
2720 below. Gramatically, these names are id-expressions.
2722 Consume the token. */
2723 token = cp_lexer_consume_token (parser->lexer);
2724 /* Look up the name. */
2725 return finish_fname (token->value);
2732 /* The `__builtin_va_arg' construct is used to handle
2733 `va_arg'. Consume the `__builtin_va_arg' token. */
2734 cp_lexer_consume_token (parser->lexer);
2735 /* Look for the opening `('. */
2736 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
2737 /* Now, parse the assignment-expression. */
2738 expression = cp_parser_assignment_expression (parser);
2739 /* Look for the `,'. */
2740 cp_parser_require (parser, CPP_COMMA, "`,'");
2741 /* Parse the type-id. */
2742 type = cp_parser_type_id (parser);
2743 /* Look for the closing `)'. */
2744 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
2746 return build_x_va_arg (expression, type);
2750 cp_parser_error (parser, "expected primary-expression");
2751 return error_mark_node;
2755 /* An id-expression can start with either an identifier, a
2756 `::' as the beginning of a qualified-id, or the "operator"
2760 case CPP_TEMPLATE_ID:
2761 case CPP_NESTED_NAME_SPECIFIER:
2767 /* Parse the id-expression. */
2769 = cp_parser_id_expression (parser,
2770 /*template_keyword_p=*/false,
2771 /*check_dependency_p=*/true,
2772 /*template_p=*/NULL);
2773 if (id_expression == error_mark_node)
2774 return error_mark_node;
2775 /* If we have a template-id, then no further lookup is
2776 required. If the template-id was for a template-class, we
2777 will sometimes have a TYPE_DECL at this point. */
2778 else if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
2779 || TREE_CODE (id_expression) == TYPE_DECL)
2780 decl = id_expression;
2781 /* Look up the name. */
2784 decl = cp_parser_lookup_name_simple (parser, id_expression);
2785 /* If name lookup gives us a SCOPE_REF, then the
2786 qualifying scope was dependent. Just propagate the
2788 if (TREE_CODE (decl) == SCOPE_REF)
2790 if (TYPE_P (TREE_OPERAND (decl, 0)))
2791 *qualifying_class = TREE_OPERAND (decl, 0);
2794 /* Check to see if DECL is a local variable in a context
2795 where that is forbidden. */
2796 if (parser->local_variables_forbidden_p
2797 && local_variable_p (decl))
2799 /* It might be that we only found DECL because we are
2800 trying to be generous with pre-ISO scoping rules.
2801 For example, consider:
2805 for (int i = 0; i < 10; ++i) {}
2806 extern void f(int j = i);
2809 Here, name look up will originally find the out
2810 of scope `i'. We need to issue a warning message,
2811 but then use the global `i'. */
2812 decl = check_for_out_of_scope_variable (decl);
2813 if (local_variable_p (decl))
2815 error ("local variable `%D' may not appear in this context",
2817 return error_mark_node;
2821 /* If unqualified name lookup fails while processing a
2822 template, that just means that we need to do name
2823 lookup again when the template is instantiated. */
2825 && decl == error_mark_node
2826 && processing_template_decl)
2828 *idk = CP_PARSER_ID_KIND_UNQUALIFIED;
2829 return build_min_nt (LOOKUP_EXPR, id_expression);
2831 else if (decl == error_mark_node
2832 && !processing_template_decl)
2836 /* It may be resolvable as a koenig lookup function
2838 *idk = CP_PARSER_ID_KIND_UNQUALIFIED;
2839 return id_expression;
2841 else if (TYPE_P (parser->scope)
2842 && !COMPLETE_TYPE_P (parser->scope))
2843 error ("incomplete type `%T' used in nested name specifier",
2845 else if (parser->scope != global_namespace)
2846 error ("`%D' is not a member of `%D'",
2847 id_expression, parser->scope);
2849 error ("`::%D' has not been declared", id_expression);
2851 /* If DECL is a variable would be out of scope under
2852 ANSI/ISO rules, but in scope in the ARM, name lookup
2853 will succeed. Issue a diagnostic here. */
2855 decl = check_for_out_of_scope_variable (decl);
2857 /* Remember that the name was used in the definition of
2858 the current class so that we can check later to see if
2859 the meaning would have been different after the class
2860 was entirely defined. */
2861 if (!parser->scope && decl != error_mark_node)
2862 maybe_note_name_used_in_class (id_expression, decl);
2865 /* If we didn't find anything, or what we found was a type,
2866 then this wasn't really an id-expression. */
2867 if (TREE_CODE (decl) == TYPE_DECL
2868 || TREE_CODE (decl) == NAMESPACE_DECL
2869 || (TREE_CODE (decl) == TEMPLATE_DECL
2870 && !DECL_FUNCTION_TEMPLATE_P (decl)))
2872 cp_parser_error (parser,
2873 "expected primary-expression");
2874 return error_mark_node;
2877 /* If the name resolved to a template parameter, there is no
2878 need to look it up again later. Similarly, we resolve
2879 enumeration constants to their underlying values. */
2880 if (TREE_CODE (decl) == CONST_DECL)
2882 *idk = CP_PARSER_ID_KIND_NONE;
2883 if (DECL_TEMPLATE_PARM_P (decl) || !processing_template_decl)
2884 return DECL_INITIAL (decl);
2891 /* If the declaration was explicitly qualified indicate
2892 that. The semantics of `A::f(3)' are different than
2893 `f(3)' if `f' is virtual. */
2894 *idk = (parser->scope
2895 ? CP_PARSER_ID_KIND_QUALIFIED
2896 : (TREE_CODE (decl) == TEMPLATE_ID_EXPR
2897 ? CP_PARSER_ID_KIND_TEMPLATE_ID
2898 : CP_PARSER_ID_KIND_UNQUALIFIED));
2903 An id-expression is type-dependent if it contains an
2904 identifier that was declared with a dependent type.
2906 As an optimization, we could choose not to create a
2907 LOOKUP_EXPR for a name that resolved to a local
2908 variable in the template function that we are currently
2909 declaring; such a name cannot ever resolve to anything
2910 else. If we did that we would not have to look up
2911 these names at instantiation time.
2913 The standard is not very specific about an
2914 id-expression that names a set of overloaded functions.
2915 What if some of them have dependent types and some of
2916 them do not? Presumably, such a name should be treated
2917 as a dependent name. */
2918 /* Assume the name is not dependent. */
2919 dependent_p = false;
2920 if (!processing_template_decl)
2921 /* No names are dependent outside a template. */
2923 /* A template-id where the name of the template was not
2924 resolved is definitely dependent. */
2925 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
2926 && (TREE_CODE (TREE_OPERAND (decl, 0))
2927 == IDENTIFIER_NODE))
2929 /* For anything except an overloaded function, just check
2931 else if (!is_overloaded_fn (decl))
2933 = cp_parser_dependent_type_p (TREE_TYPE (decl));
2934 /* For a set of overloaded functions, check each of the
2940 if (BASELINK_P (fns))
2941 fns = BASELINK_FUNCTIONS (fns);
2943 /* For a template-id, check to see if the template
2944 arguments are dependent. */
2945 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
2947 tree args = TREE_OPERAND (fns, 1);
2949 if (args && TREE_CODE (args) == TREE_LIST)
2953 if (cp_parser_dependent_template_arg_p
2954 (TREE_VALUE (args)))
2959 args = TREE_CHAIN (args);
2962 else if (args && TREE_CODE (args) == TREE_VEC)
2965 for (i = 0; i < TREE_VEC_LENGTH (args); ++i)
2966 if (cp_parser_dependent_template_arg_p
2967 (TREE_VEC_ELT (args, i)))
2974 /* The functions are those referred to by the
2976 fns = TREE_OPERAND (fns, 0);
2979 /* If there are no dependent template arguments, go
2980 through the overlaoded functions. */
2981 while (fns && !dependent_p)
2983 tree fn = OVL_CURRENT (fns);
2985 /* Member functions of dependent classes are
2987 if (TREE_CODE (fn) == FUNCTION_DECL
2988 && cp_parser_type_dependent_expression_p (fn))
2990 else if (TREE_CODE (fn) == TEMPLATE_DECL
2991 && cp_parser_dependent_template_p (fn))
2994 fns = OVL_NEXT (fns);
2998 /* If the name was dependent on a template parameter,
2999 we will resolve the name at instantiation time. */
3002 /* Create a SCOPE_REF for qualified names. */
3005 if (TYPE_P (parser->scope))
3006 *qualifying_class = parser->scope;
3007 return build_nt (SCOPE_REF,
3011 /* A TEMPLATE_ID already contains all the information
3013 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR)
3014 return id_expression;
3015 /* Create a LOOKUP_EXPR for other unqualified names. */
3016 return build_min_nt (LOOKUP_EXPR, id_expression);
3021 decl = (adjust_result_of_qualified_name_lookup
3022 (decl, parser->scope, current_class_type));
3023 if (TREE_CODE (decl) == FIELD_DECL || BASELINK_P (decl))
3024 *qualifying_class = parser->scope;
3027 /* Transform references to non-static data members into
3029 decl = hack_identifier (decl, id_expression);
3031 /* Resolve references to variables of anonymous unions
3032 into COMPONENT_REFs. */
3033 if (TREE_CODE (decl) == ALIAS_DECL)
3034 decl = DECL_INITIAL (decl);
3037 if (TREE_DEPRECATED (decl))
3038 warn_deprecated_use (decl);
3043 /* Anything else is an error. */
3045 cp_parser_error (parser, "expected primary-expression");
3046 return error_mark_node;
3050 /* Parse an id-expression.
3057 :: [opt] nested-name-specifier template [opt] unqualified-id
3059 :: operator-function-id
3062 Return a representation of the unqualified portion of the
3063 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
3064 a `::' or nested-name-specifier.
3066 Often, if the id-expression was a qualified-id, the caller will
3067 want to make a SCOPE_REF to represent the qualified-id. This
3068 function does not do this in order to avoid wastefully creating
3069 SCOPE_REFs when they are not required.
3071 If ASSUME_TYPENAME_P is true then we assume that qualified names
3072 are typenames. This flag is set when parsing a declarator-id;
3078 we are supposed to assume that `S<T>::R' is a class.
3080 If TEMPLATE_KEYWORD_P is true, then we have just seen the
3083 If CHECK_DEPENDENCY_P is false, then names are looked up inside
3084 uninstantiated templates.
3086 If *TEMPLATE_KEYWORD_P is non-NULL, it is set to true iff the
3087 `template' keyword is used to explicitly indicate that the entity
3088 named is a template. */
3091 cp_parser_id_expression (cp_parser *parser,
3092 bool template_keyword_p,
3093 bool check_dependency_p,
3096 bool global_scope_p;
3097 bool nested_name_specifier_p;
3099 /* Assume the `template' keyword was not used. */
3101 *template_p = false;
3103 /* Look for the optional `::' operator. */
3105 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
3107 /* Look for the optional nested-name-specifier. */
3108 nested_name_specifier_p
3109 = (cp_parser_nested_name_specifier_opt (parser,
3110 /*typename_keyword_p=*/false,
3114 /* If there is a nested-name-specifier, then we are looking at
3115 the first qualified-id production. */
3116 if (nested_name_specifier_p)
3119 tree saved_object_scope;
3120 tree saved_qualifying_scope;
3121 tree unqualified_id;
3124 /* See if the next token is the `template' keyword. */
3126 template_p = &is_template;
3127 *template_p = cp_parser_optional_template_keyword (parser);
3128 /* Name lookup we do during the processing of the
3129 unqualified-id might obliterate SCOPE. */
3130 saved_scope = parser->scope;
3131 saved_object_scope = parser->object_scope;
3132 saved_qualifying_scope = parser->qualifying_scope;
3133 /* Process the final unqualified-id. */
3134 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
3135 check_dependency_p);
3136 /* Restore the SAVED_SCOPE for our caller. */
3137 parser->scope = saved_scope;
3138 parser->object_scope = saved_object_scope;
3139 parser->qualifying_scope = saved_qualifying_scope;
3141 return unqualified_id;
3143 /* Otherwise, if we are in global scope, then we are looking at one
3144 of the other qualified-id productions. */
3145 else if (global_scope_p)
3150 /* Peek at the next token. */
3151 token = cp_lexer_peek_token (parser->lexer);
3153 /* If it's an identifier, and the next token is not a "<", then
3154 we can avoid the template-id case. This is an optimization
3155 for this common case. */
3156 if (token->type == CPP_NAME
3157 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_LESS)
3158 return cp_parser_identifier (parser);
3160 cp_parser_parse_tentatively (parser);
3161 /* Try a template-id. */
3162 id = cp_parser_template_id (parser,
3163 /*template_keyword_p=*/false,
3164 /*check_dependency_p=*/true);
3165 /* If that worked, we're done. */
3166 if (cp_parser_parse_definitely (parser))
3169 /* Peek at the next token. (Changes in the token buffer may
3170 have invalidated the pointer obtained above.) */
3171 token = cp_lexer_peek_token (parser->lexer);
3173 switch (token->type)
3176 return cp_parser_identifier (parser);
3179 if (token->keyword == RID_OPERATOR)
3180 return cp_parser_operator_function_id (parser);
3184 cp_parser_error (parser, "expected id-expression");
3185 return error_mark_node;
3189 return cp_parser_unqualified_id (parser, template_keyword_p,
3190 /*check_dependency_p=*/true);
3193 /* Parse an unqualified-id.
3197 operator-function-id
3198 conversion-function-id
3202 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3203 keyword, in a construct like `A::template ...'.
3205 Returns a representation of unqualified-id. For the `identifier'
3206 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
3207 production a BIT_NOT_EXPR is returned; the operand of the
3208 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
3209 other productions, see the documentation accompanying the
3210 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
3211 names are looked up in uninstantiated templates. */
3214 cp_parser_unqualified_id (parser, template_keyword_p,
3217 bool template_keyword_p;
3218 bool check_dependency_p;
3222 /* Peek at the next token. */
3223 token = cp_lexer_peek_token (parser->lexer);
3225 switch (token->type)
3231 /* We don't know yet whether or not this will be a
3233 cp_parser_parse_tentatively (parser);
3234 /* Try a template-id. */
3235 id = cp_parser_template_id (parser, template_keyword_p,
3236 check_dependency_p);
3237 /* If it worked, we're done. */
3238 if (cp_parser_parse_definitely (parser))
3240 /* Otherwise, it's an ordinary identifier. */
3241 return cp_parser_identifier (parser);
3244 case CPP_TEMPLATE_ID:
3245 return cp_parser_template_id (parser, template_keyword_p,
3246 check_dependency_p);
3251 tree qualifying_scope;
3255 /* Consume the `~' token. */
3256 cp_lexer_consume_token (parser->lexer);
3257 /* Parse the class-name. The standard, as written, seems to
3260 template <typename T> struct S { ~S (); };
3261 template <typename T> S<T>::~S() {}
3263 is invalid, since `~' must be followed by a class-name, but
3264 `S<T>' is dependent, and so not known to be a class.
3265 That's not right; we need to look in uninstantiated
3266 templates. A further complication arises from:
3268 template <typename T> void f(T t) {
3272 Here, it is not possible to look up `T' in the scope of `T'
3273 itself. We must look in both the current scope, and the
3274 scope of the containing complete expression.
3276 Yet another issue is:
3285 The standard does not seem to say that the `S' in `~S'
3286 should refer to the type `S' and not the data member
3289 /* DR 244 says that we look up the name after the "~" in the
3290 same scope as we looked up the qualifying name. That idea
3291 isn't fully worked out; it's more complicated than that. */
3292 scope = parser->scope;
3293 object_scope = parser->object_scope;
3294 qualifying_scope = parser->qualifying_scope;
3296 /* If the name is of the form "X::~X" it's OK. */
3297 if (scope && TYPE_P (scope)
3298 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
3299 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3301 && (cp_lexer_peek_token (parser->lexer)->value
3302 == TYPE_IDENTIFIER (scope)))
3304 cp_lexer_consume_token (parser->lexer);
3305 return build_nt (BIT_NOT_EXPR, scope);
3308 /* If there was an explicit qualification (S::~T), first look
3309 in the scope given by the qualification (i.e., S). */
3312 cp_parser_parse_tentatively (parser);
3313 type_decl = cp_parser_class_name (parser,
3314 /*typename_keyword_p=*/false,
3315 /*template_keyword_p=*/false,
3317 /*check_access_p=*/true,
3318 /*check_dependency=*/false,
3319 /*class_head_p=*/false);
3320 if (cp_parser_parse_definitely (parser))
3321 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3323 /* In "N::S::~S", look in "N" as well. */
3324 if (scope && qualifying_scope)
3326 cp_parser_parse_tentatively (parser);
3327 parser->scope = qualifying_scope;
3328 parser->object_scope = NULL_TREE;
3329 parser->qualifying_scope = NULL_TREE;
3331 = cp_parser_class_name (parser,
3332 /*typename_keyword_p=*/false,
3333 /*template_keyword_p=*/false,
3335 /*check_access_p=*/true,
3336 /*check_dependency=*/false,
3337 /*class_head_p=*/false);
3338 if (cp_parser_parse_definitely (parser))
3339 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3341 /* In "p->S::~T", look in the scope given by "*p" as well. */
3342 else if (object_scope)
3344 cp_parser_parse_tentatively (parser);
3345 parser->scope = object_scope;
3346 parser->object_scope = NULL_TREE;
3347 parser->qualifying_scope = NULL_TREE;
3349 = cp_parser_class_name (parser,
3350 /*typename_keyword_p=*/false,
3351 /*template_keyword_p=*/false,
3353 /*check_access_p=*/true,
3354 /*check_dependency=*/false,
3355 /*class_head_p=*/false);
3356 if (cp_parser_parse_definitely (parser))
3357 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3359 /* Look in the surrounding context. */
3360 parser->scope = NULL_TREE;
3361 parser->object_scope = NULL_TREE;
3362 parser->qualifying_scope = NULL_TREE;
3364 = cp_parser_class_name (parser,
3365 /*typename_keyword_p=*/false,
3366 /*template_keyword_p=*/false,
3368 /*check_access_p=*/true,
3369 /*check_dependency=*/false,
3370 /*class_head_p=*/false);
3371 /* If an error occurred, assume that the name of the
3372 destructor is the same as the name of the qualifying
3373 class. That allows us to keep parsing after running
3374 into ill-formed destructor names. */
3375 if (type_decl == error_mark_node && scope && TYPE_P (scope))
3376 return build_nt (BIT_NOT_EXPR, scope);
3377 else if (type_decl == error_mark_node)
3378 return error_mark_node;
3380 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3384 if (token->keyword == RID_OPERATOR)
3388 /* This could be a template-id, so we try that first. */
3389 cp_parser_parse_tentatively (parser);
3390 /* Try a template-id. */
3391 id = cp_parser_template_id (parser, template_keyword_p,
3392 /*check_dependency_p=*/true);
3393 /* If that worked, we're done. */
3394 if (cp_parser_parse_definitely (parser))
3396 /* We still don't know whether we're looking at an
3397 operator-function-id or a conversion-function-id. */
3398 cp_parser_parse_tentatively (parser);
3399 /* Try an operator-function-id. */
3400 id = cp_parser_operator_function_id (parser);
3401 /* If that didn't work, try a conversion-function-id. */
3402 if (!cp_parser_parse_definitely (parser))
3403 id = cp_parser_conversion_function_id (parser);
3410 cp_parser_error (parser, "expected unqualified-id");
3411 return error_mark_node;
3415 /* Parse an (optional) nested-name-specifier.
3417 nested-name-specifier:
3418 class-or-namespace-name :: nested-name-specifier [opt]
3419 class-or-namespace-name :: template nested-name-specifier [opt]
3421 PARSER->SCOPE should be set appropriately before this function is
3422 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
3423 effect. TYPE_P is TRUE if we non-type bindings should be ignored
3426 Sets PARSER->SCOPE to the class (TYPE) or namespace
3427 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
3428 it unchanged if there is no nested-name-specifier. Returns the new
3429 scope iff there is a nested-name-specifier, or NULL_TREE otherwise. */
3432 cp_parser_nested_name_specifier_opt (cp_parser *parser,
3433 bool typename_keyword_p,
3434 bool check_dependency_p,
3437 bool success = false;
3438 tree access_check = NULL_TREE;
3441 /* If the next token corresponds to a nested name specifier, there
3442 is no need to reparse it. */
3443 if (cp_lexer_next_token_is (parser->lexer, CPP_NESTED_NAME_SPECIFIER))
3448 /* Get the stored value. */
3449 value = cp_lexer_consume_token (parser->lexer)->value;
3450 /* Perform any access checks that were deferred. */
3451 for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
3452 cp_parser_defer_access_check (parser,
3453 TREE_PURPOSE (check),
3454 TREE_VALUE (check));
3455 /* Set the scope from the stored value. */
3456 parser->scope = TREE_VALUE (value);
3457 parser->qualifying_scope = TREE_TYPE (value);
3458 parser->object_scope = NULL_TREE;
3459 return parser->scope;
3462 /* Remember where the nested-name-specifier starts. */
3463 if (cp_parser_parsing_tentatively (parser)
3464 && !cp_parser_committed_to_tentative_parse (parser))
3466 cp_token *next_token = cp_lexer_peek_token (parser->lexer);
3467 start = cp_lexer_token_difference (parser->lexer,
3468 parser->lexer->first_token,
3470 access_check = parser->context->deferred_access_checks;
3479 tree saved_qualifying_scope;
3481 bool template_keyword_p;
3483 /* Spot cases that cannot be the beginning of a
3484 nested-name-specifier. On the second and subsequent times
3485 through the loop, we look for the `template' keyword. */
3486 token = cp_lexer_peek_token (parser->lexer);
3487 if (success && token->keyword == RID_TEMPLATE)
3489 /* A template-id can start a nested-name-specifier. */
3490 else if (token->type == CPP_TEMPLATE_ID)
3494 /* If the next token is not an identifier, then it is
3495 definitely not a class-or-namespace-name. */
3496 if (token->type != CPP_NAME)
3498 /* If the following token is neither a `<' (to begin a
3499 template-id), nor a `::', then we are not looking at a
3500 nested-name-specifier. */
3501 token = cp_lexer_peek_nth_token (parser->lexer, 2);
3502 if (token->type != CPP_LESS && token->type != CPP_SCOPE)
3506 /* The nested-name-specifier is optional, so we parse
3508 cp_parser_parse_tentatively (parser);
3510 /* Look for the optional `template' keyword, if this isn't the
3511 first time through the loop. */
3513 template_keyword_p = cp_parser_optional_template_keyword (parser);
3515 template_keyword_p = false;
3517 /* Save the old scope since the name lookup we are about to do
3518 might destroy it. */
3519 old_scope = parser->scope;
3520 saved_qualifying_scope = parser->qualifying_scope;
3521 /* Parse the qualifying entity. */
3523 = cp_parser_class_or_namespace_name (parser,
3528 /* Look for the `::' token. */
3529 cp_parser_require (parser, CPP_SCOPE, "`::'");
3531 /* If we found what we wanted, we keep going; otherwise, we're
3533 if (!cp_parser_parse_definitely (parser))
3535 bool error_p = false;
3537 /* Restore the OLD_SCOPE since it was valid before the
3538 failed attempt at finding the last
3539 class-or-namespace-name. */
3540 parser->scope = old_scope;
3541 parser->qualifying_scope = saved_qualifying_scope;
3542 /* If the next token is an identifier, and the one after
3543 that is a `::', then any valid interpretation would have
3544 found a class-or-namespace-name. */
3545 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
3546 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3548 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
3551 token = cp_lexer_consume_token (parser->lexer);
3556 decl = cp_parser_lookup_name_simple (parser, token->value);
3557 if (TREE_CODE (decl) == TEMPLATE_DECL)
3558 error ("`%D' used without template parameters",
3560 else if (parser->scope)
3562 if (TYPE_P (parser->scope))
3563 error ("`%T::%D' is not a class-name or "
3565 parser->scope, token->value);
3567 error ("`%D::%D' is not a class-name or "
3569 parser->scope, token->value);
3572 error ("`%D' is not a class-name or namespace-name",
3574 parser->scope = NULL_TREE;
3576 /* Treat this as a successful nested-name-specifier
3581 If the name found is not a class-name (clause
3582 _class_) or namespace-name (_namespace.def_), the
3583 program is ill-formed. */
3586 cp_lexer_consume_token (parser->lexer);
3591 /* We've found one valid nested-name-specifier. */
3593 /* Make sure we look in the right scope the next time through
3595 parser->scope = (TREE_CODE (new_scope) == TYPE_DECL
3596 ? TREE_TYPE (new_scope)
3598 /* If it is a class scope, try to complete it; we are about to
3599 be looking up names inside the class. */
3600 if (TYPE_P (parser->scope))
3601 complete_type (parser->scope);
3604 /* If parsing tentatively, replace the sequence of tokens that makes
3605 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
3606 token. That way, should we re-parse the token stream, we will
3607 not have to repeat the effort required to do the parse, nor will
3608 we issue duplicate error messages. */
3609 if (success && start >= 0)
3614 /* Find the token that corresponds to the start of the
3616 token = cp_lexer_advance_token (parser->lexer,
3617 parser->lexer->first_token,
3620 /* Remember the access checks associated with this
3621 nested-name-specifier. */
3622 c = parser->context->deferred_access_checks;
3623 if (c == access_check)
3624 access_check = NULL_TREE;
3627 while (TREE_CHAIN (c) != access_check)
3629 access_check = parser->context->deferred_access_checks;
3630 parser->context->deferred_access_checks = TREE_CHAIN (c);
3631 TREE_CHAIN (c) = NULL_TREE;
3634 /* Reset the contents of the START token. */
3635 token->type = CPP_NESTED_NAME_SPECIFIER;
3636 token->value = build_tree_list (access_check, parser->scope);
3637 TREE_TYPE (token->value) = parser->qualifying_scope;
3638 token->keyword = RID_MAX;
3639 /* Purge all subsequent tokens. */
3640 cp_lexer_purge_tokens_after (parser->lexer, token);
3643 return success ? parser->scope : NULL_TREE;
3646 /* Parse a nested-name-specifier. See
3647 cp_parser_nested_name_specifier_opt for details. This function
3648 behaves identically, except that it will an issue an error if no
3649 nested-name-specifier is present, and it will return
3650 ERROR_MARK_NODE, rather than NULL_TREE, if no nested-name-specifier
3654 cp_parser_nested_name_specifier (cp_parser *parser,
3655 bool typename_keyword_p,
3656 bool check_dependency_p,
3661 /* Look for the nested-name-specifier. */
3662 scope = cp_parser_nested_name_specifier_opt (parser,
3666 /* If it was not present, issue an error message. */
3669 cp_parser_error (parser, "expected nested-name-specifier");
3670 return error_mark_node;
3676 /* Parse a class-or-namespace-name.
3678 class-or-namespace-name:
3682 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
3683 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
3684 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
3685 TYPE_P is TRUE iff the next name should be taken as a class-name,
3686 even the same name is declared to be another entity in the same
3689 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
3690 specified by the class-or-namespace-name. If neither is found the
3691 ERROR_MARK_NODE is returned. */
3694 cp_parser_class_or_namespace_name (cp_parser *parser,
3695 bool typename_keyword_p,
3696 bool template_keyword_p,
3697 bool check_dependency_p,
3701 tree saved_qualifying_scope;
3702 tree saved_object_scope;
3706 /* If the next token is the `template' keyword, we know that we are
3707 looking at a class-name. */
3708 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
3709 return cp_parser_class_name (parser,
3713 /*check_access_p=*/true,
3715 /*class_head_p=*/false);
3716 /* Before we try to parse the class-name, we must save away the
3717 current PARSER->SCOPE since cp_parser_class_name will destroy
3719 saved_scope = parser->scope;
3720 saved_qualifying_scope = parser->qualifying_scope;
3721 saved_object_scope = parser->object_scope;
3722 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
3723 there is no need to look for a namespace-name. */
3724 only_class_p = saved_scope && TYPE_P (saved_scope);
3726 cp_parser_parse_tentatively (parser);
3727 scope = cp_parser_class_name (parser,
3731 /*check_access_p=*/true,
3733 /*class_head_p=*/false);
3734 /* If that didn't work, try for a namespace-name. */
3735 if (!only_class_p && !cp_parser_parse_definitely (parser))
3737 /* Restore the saved scope. */
3738 parser->scope = saved_scope;
3739 parser->qualifying_scope = saved_qualifying_scope;
3740 parser->object_scope = saved_object_scope;
3741 /* If we are not looking at an identifier followed by the scope
3742 resolution operator, then this is not part of a
3743 nested-name-specifier. (Note that this function is only used
3744 to parse the components of a nested-name-specifier.) */
3745 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
3746 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
3747 return error_mark_node;
3748 scope = cp_parser_namespace_name (parser);
3754 /* Parse a postfix-expression.
3758 postfix-expression [ expression ]
3759 postfix-expression ( expression-list [opt] )
3760 simple-type-specifier ( expression-list [opt] )
3761 typename :: [opt] nested-name-specifier identifier
3762 ( expression-list [opt] )
3763 typename :: [opt] nested-name-specifier template [opt] template-id
3764 ( expression-list [opt] )
3765 postfix-expression . template [opt] id-expression
3766 postfix-expression -> template [opt] id-expression
3767 postfix-expression . pseudo-destructor-name
3768 postfix-expression -> pseudo-destructor-name
3769 postfix-expression ++
3770 postfix-expression --
3771 dynamic_cast < type-id > ( expression )
3772 static_cast < type-id > ( expression )
3773 reinterpret_cast < type-id > ( expression )
3774 const_cast < type-id > ( expression )
3775 typeid ( expression )
3781 ( type-id ) { initializer-list , [opt] }
3783 This extension is a GNU version of the C99 compound-literal
3784 construct. (The C99 grammar uses `type-name' instead of `type-id',
3785 but they are essentially the same concept.)
3787 If ADDRESS_P is true, the postfix expression is the operand of the
3790 Returns a representation of the expression. */
3793 cp_parser_postfix_expression (cp_parser *parser, bool address_p)
3797 cp_parser_id_kind idk = CP_PARSER_ID_KIND_NONE;
3798 tree postfix_expression = NULL_TREE;
3799 /* Non-NULL only if the current postfix-expression can be used to
3800 form a pointer-to-member. In that case, QUALIFYING_CLASS is the
3801 class used to qualify the member. */
3802 tree qualifying_class = NULL_TREE;
3805 /* Peek at the next token. */
3806 token = cp_lexer_peek_token (parser->lexer);
3807 /* Some of the productions are determined by keywords. */
3808 keyword = token->keyword;
3818 const char *saved_message;
3820 /* All of these can be handled in the same way from the point
3821 of view of parsing. Begin by consuming the token
3822 identifying the cast. */
3823 cp_lexer_consume_token (parser->lexer);
3825 /* New types cannot be defined in the cast. */
3826 saved_message = parser->type_definition_forbidden_message;
3827 parser->type_definition_forbidden_message
3828 = "types may not be defined in casts";
3830 /* Look for the opening `<'. */
3831 cp_parser_require (parser, CPP_LESS, "`<'");
3832 /* Parse the type to which we are casting. */
3833 type = cp_parser_type_id (parser);
3834 /* Look for the closing `>'. */
3835 cp_parser_require (parser, CPP_GREATER, "`>'");
3836 /* Restore the old message. */
3837 parser->type_definition_forbidden_message = saved_message;
3839 /* And the expression which is being cast. */
3840 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3841 expression = cp_parser_expression (parser);
3842 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3848 = build_dynamic_cast (type, expression);
3852 = build_static_cast (type, expression);
3856 = build_reinterpret_cast (type, expression);
3860 = build_const_cast (type, expression);
3871 const char *saved_message;
3873 /* Consume the `typeid' token. */
3874 cp_lexer_consume_token (parser->lexer);
3875 /* Look for the `(' token. */
3876 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3877 /* Types cannot be defined in a `typeid' expression. */
3878 saved_message = parser->type_definition_forbidden_message;
3879 parser->type_definition_forbidden_message
3880 = "types may not be defined in a `typeid\' expression";
3881 /* We can't be sure yet whether we're looking at a type-id or an
3883 cp_parser_parse_tentatively (parser);
3884 /* Try a type-id first. */
3885 type = cp_parser_type_id (parser);
3886 /* Look for the `)' token. Otherwise, we can't be sure that
3887 we're not looking at an expression: consider `typeid (int
3888 (3))', for example. */
3889 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3890 /* If all went well, simply lookup the type-id. */
3891 if (cp_parser_parse_definitely (parser))
3892 postfix_expression = get_typeid (type);
3893 /* Otherwise, fall back to the expression variant. */
3898 /* Look for an expression. */
3899 expression = cp_parser_expression (parser);
3900 /* Compute its typeid. */
3901 postfix_expression = build_typeid (expression);
3902 /* Look for the `)' token. */
3903 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3906 /* Restore the saved message. */
3907 parser->type_definition_forbidden_message = saved_message;
3913 bool template_p = false;
3917 /* Consume the `typename' token. */
3918 cp_lexer_consume_token (parser->lexer);
3919 /* Look for the optional `::' operator. */
3920 cp_parser_global_scope_opt (parser,
3921 /*current_scope_valid_p=*/false);
3922 /* Look for the nested-name-specifier. */
3923 cp_parser_nested_name_specifier (parser,
3924 /*typename_keyword_p=*/true,
3925 /*check_dependency_p=*/true,
3927 /* Look for the optional `template' keyword. */
3928 template_p = cp_parser_optional_template_keyword (parser);
3929 /* We don't know whether we're looking at a template-id or an
3931 cp_parser_parse_tentatively (parser);
3932 /* Try a template-id. */
3933 id = cp_parser_template_id (parser, template_p,
3934 /*check_dependency_p=*/true);
3935 /* If that didn't work, try an identifier. */
3936 if (!cp_parser_parse_definitely (parser))
3937 id = cp_parser_identifier (parser);
3938 /* Create a TYPENAME_TYPE to represent the type to which the
3939 functional cast is being performed. */
3940 type = make_typename_type (parser->scope, id,
3943 postfix_expression = cp_parser_functional_cast (parser, type);
3951 /* If the next thing is a simple-type-specifier, we may be
3952 looking at a functional cast. We could also be looking at
3953 an id-expression. So, we try the functional cast, and if
3954 that doesn't work we fall back to the primary-expression. */
3955 cp_parser_parse_tentatively (parser);
3956 /* Look for the simple-type-specifier. */
3957 type = cp_parser_simple_type_specifier (parser,
3958 CP_PARSER_FLAGS_NONE);
3959 /* Parse the cast itself. */
3960 if (!cp_parser_error_occurred (parser))
3962 = cp_parser_functional_cast (parser, type);
3963 /* If that worked, we're done. */
3964 if (cp_parser_parse_definitely (parser))
3967 /* If the functional-cast didn't work out, try a
3968 compound-literal. */
3969 if (cp_parser_allow_gnu_extensions_p (parser))
3971 tree initializer_list = NULL_TREE;
3973 cp_parser_parse_tentatively (parser);
3974 /* Look for the `('. */
3975 if (cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
3977 type = cp_parser_type_id (parser);
3978 /* Look for the `)'. */
3979 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3980 /* Look for the `{'. */
3981 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
3982 /* If things aren't going well, there's no need to
3984 if (!cp_parser_error_occurred (parser))
3986 /* Parse the initializer-list. */
3988 = cp_parser_initializer_list (parser);
3989 /* Allow a trailing `,'. */
3990 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
3991 cp_lexer_consume_token (parser->lexer);
3992 /* Look for the final `}'. */
3993 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
3996 /* If that worked, we're definitely looking at a
3997 compound-literal expression. */
3998 if (cp_parser_parse_definitely (parser))
4000 /* Warn the user that a compound literal is not
4001 allowed in standard C++. */
4003 pedwarn ("ISO C++ forbids compound-literals");
4004 /* Form the representation of the compound-literal. */
4006 = finish_compound_literal (type, initializer_list);
4011 /* It must be a primary-expression. */
4012 postfix_expression = cp_parser_primary_expression (parser,
4019 /* Peek at the next token. */
4020 token = cp_lexer_peek_token (parser->lexer);
4021 done = (token->type != CPP_OPEN_SQUARE
4022 && token->type != CPP_OPEN_PAREN
4023 && token->type != CPP_DOT
4024 && token->type != CPP_DEREF
4025 && token->type != CPP_PLUS_PLUS
4026 && token->type != CPP_MINUS_MINUS);
4028 /* If the postfix expression is complete, finish up. */
4029 if (address_p && qualifying_class && done)
4031 if (TREE_CODE (postfix_expression) == SCOPE_REF)
4032 postfix_expression = TREE_OPERAND (postfix_expression, 1);
4034 = build_offset_ref (qualifying_class, postfix_expression);
4035 return postfix_expression;
4038 /* Otherwise, if we were avoiding committing until we knew
4039 whether or not we had a pointer-to-member, we now know that
4040 the expression is an ordinary reference to a qualified name. */
4041 if (qualifying_class && !processing_template_decl)
4043 if (TREE_CODE (postfix_expression) == FIELD_DECL)
4045 = finish_non_static_data_member (postfix_expression,
4047 else if (BASELINK_P (postfix_expression))
4052 /* See if any of the functions are non-static members. */
4053 fns = BASELINK_FUNCTIONS (postfix_expression);
4054 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
4055 fns = TREE_OPERAND (fns, 0);
4056 for (fn = fns; fn; fn = OVL_NEXT (fn))
4057 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn))
4059 /* If so, the expression may be relative to the current
4061 if (fn && current_class_type
4062 && DERIVED_FROM_P (qualifying_class, current_class_type))
4064 = (build_class_member_access_expr
4065 (maybe_dummy_object (qualifying_class, NULL),
4067 BASELINK_ACCESS_BINFO (postfix_expression),
4068 /*preserve_reference=*/false));
4070 return build_offset_ref (qualifying_class,
4071 postfix_expression);
4075 /* Remember that there was a reference to this entity. */
4076 if (DECL_P (postfix_expression))
4077 mark_used (postfix_expression);
4079 /* Keep looping until the postfix-expression is complete. */
4082 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE
4083 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
4085 /* It is not a Koenig lookup function call. */
4086 unqualified_name_lookup_error (postfix_expression);
4087 postfix_expression = error_mark_node;
4090 /* Peek at the next token. */
4091 token = cp_lexer_peek_token (parser->lexer);
4093 switch (token->type)
4095 case CPP_OPEN_SQUARE:
4096 /* postfix-expression [ expression ] */
4100 /* Consume the `[' token. */
4101 cp_lexer_consume_token (parser->lexer);
4102 /* Parse the index expression. */
4103 index = cp_parser_expression (parser);
4104 /* Look for the closing `]'. */
4105 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4107 /* Build the ARRAY_REF. */
4109 = grok_array_decl (postfix_expression, index);
4110 idk = CP_PARSER_ID_KIND_NONE;
4114 case CPP_OPEN_PAREN:
4115 /* postfix-expression ( expression-list [opt] ) */
4119 /* Consume the `(' token. */
4120 cp_lexer_consume_token (parser->lexer);
4121 /* If the next token is not a `)', then there are some
4123 if (cp_lexer_next_token_is_not (parser->lexer,
4125 args = cp_parser_expression_list (parser);
4128 /* Look for the closing `)'. */
4129 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4131 if (idk == CP_PARSER_ID_KIND_UNQUALIFIED
4132 && (is_overloaded_fn (postfix_expression)
4133 || DECL_P (postfix_expression)
4134 || TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4138 tree identifier = NULL_TREE;
4139 tree functions = NULL_TREE;
4141 /* Find the name of the overloaded function. */
4142 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4143 identifier = postfix_expression;
4144 else if (is_overloaded_fn (postfix_expression))
4146 functions = postfix_expression;
4147 identifier = DECL_NAME (get_first_fn (functions));
4149 else if (DECL_P (postfix_expression))
4151 functions = postfix_expression;
4152 identifier = DECL_NAME (postfix_expression);
4155 /* A call to a namespace-scope function using an
4158 Do Koenig lookup -- unless any of the arguments are
4160 for (arg = args; arg; arg = TREE_CHAIN (arg))
4161 if (cp_parser_type_dependent_expression_p (TREE_VALUE (arg)))
4166 = lookup_arg_dependent(identifier, functions, args);
4167 if (!postfix_expression)
4169 /* The unqualified name could not be resolved. */
4170 unqualified_name_lookup_error (identifier);
4171 postfix_expression = error_mark_node;
4174 = build_call_from_tree (postfix_expression, args,
4175 /*diallow_virtual=*/false);
4178 postfix_expression = build_min_nt (LOOKUP_EXPR,
4181 else if (idk == CP_PARSER_ID_KIND_UNQUALIFIED
4182 && TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4184 /* The unqualified name could not be resolved. */
4185 unqualified_name_lookup_error (postfix_expression);
4186 postfix_expression = error_mark_node;
4190 /* In the body of a template, no further processing is
4192 if (processing_template_decl)
4194 postfix_expression = build_nt (CALL_EXPR,
4200 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
4202 = (build_new_method_call
4203 (TREE_OPERAND (postfix_expression, 0),
4204 TREE_OPERAND (postfix_expression, 1),
4206 (idk == CP_PARSER_ID_KIND_QUALIFIED
4207 ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL)));
4208 else if (TREE_CODE (postfix_expression) == OFFSET_REF)
4209 postfix_expression = (build_offset_ref_call_from_tree
4210 (postfix_expression, args));
4211 else if (idk == CP_PARSER_ID_KIND_QUALIFIED)
4213 /* A call to a static class member, or a
4214 namespace-scope function. */
4216 = finish_call_expr (postfix_expression, args,
4217 /*disallow_virtual=*/true);
4221 /* All other function calls. */
4223 = finish_call_expr (postfix_expression, args,
4224 /*disallow_virtual=*/false);
4227 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
4228 idk = CP_PARSER_ID_KIND_NONE;
4234 /* postfix-expression . template [opt] id-expression
4235 postfix-expression . pseudo-destructor-name
4236 postfix-expression -> template [opt] id-expression
4237 postfix-expression -> pseudo-destructor-name */
4242 tree scope = NULL_TREE;
4244 /* If this is a `->' operator, dereference the pointer. */
4245 if (token->type == CPP_DEREF)
4246 postfix_expression = build_x_arrow (postfix_expression);
4247 /* Check to see whether or not the expression is
4249 dependent_p = (cp_parser_type_dependent_expression_p
4250 (postfix_expression));
4251 /* The identifier following the `->' or `.' is not
4253 parser->scope = NULL_TREE;
4254 parser->qualifying_scope = NULL_TREE;
4255 parser->object_scope = NULL_TREE;
4256 /* Enter the scope corresponding to the type of the object
4257 given by the POSTFIX_EXPRESSION. */
4259 && TREE_TYPE (postfix_expression) != NULL_TREE)
4261 scope = TREE_TYPE (postfix_expression);
4262 /* According to the standard, no expression should
4263 ever have reference type. Unfortunately, we do not
4264 currently match the standard in this respect in
4265 that our internal representation of an expression
4266 may have reference type even when the standard says
4267 it does not. Therefore, we have to manually obtain
4268 the underlying type here. */
4269 if (TREE_CODE (scope) == REFERENCE_TYPE)
4270 scope = TREE_TYPE (scope);
4271 /* If the SCOPE is an OFFSET_TYPE, then we grab the
4272 type of the field. We get an OFFSET_TYPE for
4277 Probably, we should not get an OFFSET_TYPE here;
4278 that transformation should be made only if `&S::T'
4280 if (TREE_CODE (scope) == OFFSET_TYPE)
4281 scope = TREE_TYPE (scope);
4282 /* The type of the POSTFIX_EXPRESSION must be
4284 scope = complete_type_or_else (scope, NULL_TREE);
4285 /* Let the name lookup machinery know that we are
4286 processing a class member access expression. */
4287 parser->context->object_type = scope;
4288 /* If something went wrong, we want to be able to
4289 discern that case, as opposed to the case where
4290 there was no SCOPE due to the type of expression
4293 scope = error_mark_node;
4296 /* Consume the `.' or `->' operator. */
4297 cp_lexer_consume_token (parser->lexer);
4298 /* If the SCOPE is not a scalar type, we are looking at an
4299 ordinary class member access expression, rather than a
4300 pseudo-destructor-name. */
4301 if (!scope || !SCALAR_TYPE_P (scope))
4303 template_p = cp_parser_optional_template_keyword (parser);
4304 /* Parse the id-expression. */
4305 name = cp_parser_id_expression (parser,
4307 /*check_dependency_p=*/true,
4308 /*template_p=*/NULL);
4309 /* In general, build a SCOPE_REF if the member name is
4310 qualified. However, if the name was not dependent
4311 and has already been resolved; there is no need to
4312 build the SCOPE_REF. For example;
4314 struct X { void f(); };
4315 template <typename T> void f(T* t) { t->X::f(); }
4317 Even though "t" is dependent, "X::f" is not and has
4318 except that for a BASELINK there is no need to
4319 include scope information. */
4320 if (name != error_mark_node
4321 && !BASELINK_P (name)
4324 name = build_nt (SCOPE_REF, parser->scope, name);
4325 parser->scope = NULL_TREE;
4326 parser->qualifying_scope = NULL_TREE;
4327 parser->object_scope = NULL_TREE;
4330 = finish_class_member_access_expr (postfix_expression, name);
4332 /* Otherwise, try the pseudo-destructor-name production. */
4338 /* Parse the pseudo-destructor-name. */
4339 cp_parser_pseudo_destructor_name (parser, &s, &type);
4340 /* Form the call. */
4342 = finish_pseudo_destructor_expr (postfix_expression,
4343 s, TREE_TYPE (type));
4346 /* We no longer need to look up names in the scope of the
4347 object on the left-hand side of the `.' or `->'
4349 parser->context->object_type = NULL_TREE;
4350 idk = CP_PARSER_ID_KIND_NONE;
4355 /* postfix-expression ++ */
4356 /* Consume the `++' token. */
4357 cp_lexer_consume_token (parser->lexer);
4358 /* Generate a reprsentation for the complete expression. */
4360 = finish_increment_expr (postfix_expression,
4361 POSTINCREMENT_EXPR);
4362 idk = CP_PARSER_ID_KIND_NONE;
4365 case CPP_MINUS_MINUS:
4366 /* postfix-expression -- */
4367 /* Consume the `--' token. */
4368 cp_lexer_consume_token (parser->lexer);
4369 /* Generate a reprsentation for the complete expression. */
4371 = finish_increment_expr (postfix_expression,
4372 POSTDECREMENT_EXPR);
4373 idk = CP_PARSER_ID_KIND_NONE;
4377 return postfix_expression;
4381 /* We should never get here. */
4383 return error_mark_node;
4386 /* Parse an expression-list.
4389 assignment-expression
4390 expression-list, assignment-expression
4392 Returns a TREE_LIST. The TREE_VALUE of each node is a
4393 representation of an assignment-expression. Note that a TREE_LIST
4394 is returned even if there is only a single expression in the list. */
4397 cp_parser_expression_list (parser)
4400 tree expression_list = NULL_TREE;
4402 /* Consume expressions until there are no more. */
4407 /* Parse the next assignment-expression. */
4408 expr = cp_parser_assignment_expression (parser);
4409 /* Add it to the list. */
4410 expression_list = tree_cons (NULL_TREE, expr, expression_list);
4412 /* If the next token isn't a `,', then we are done. */
4413 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
4415 /* All uses of expression-list in the grammar are followed
4416 by a `)'. Therefore, if the next token is not a `)' an
4417 error will be issued, unless we are parsing tentatively.
4418 Skip ahead to see if there is another `,' before the `)';
4419 if so, we can go there and recover. */
4420 if (cp_parser_parsing_tentatively (parser)
4421 || cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
4422 || !cp_parser_skip_to_closing_parenthesis_or_comma (parser))
4426 /* Otherwise, consume the `,' and keep going. */
4427 cp_lexer_consume_token (parser->lexer);
4430 /* We built up the list in reverse order so we must reverse it now. */
4431 return nreverse (expression_list);
4434 /* Parse a pseudo-destructor-name.
4436 pseudo-destructor-name:
4437 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
4438 :: [opt] nested-name-specifier template template-id :: ~ type-name
4439 :: [opt] nested-name-specifier [opt] ~ type-name
4441 If either of the first two productions is used, sets *SCOPE to the
4442 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
4443 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
4444 or ERROR_MARK_NODE if no type-name is present. */
4447 cp_parser_pseudo_destructor_name (parser, scope, type)
4452 bool nested_name_specifier_p;
4454 /* Look for the optional `::' operator. */
4455 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
4456 /* Look for the optional nested-name-specifier. */
4457 nested_name_specifier_p
4458 = (cp_parser_nested_name_specifier_opt (parser,
4459 /*typename_keyword_p=*/false,
4460 /*check_dependency_p=*/true,
4463 /* Now, if we saw a nested-name-specifier, we might be doing the
4464 second production. */
4465 if (nested_name_specifier_p
4466 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
4468 /* Consume the `template' keyword. */
4469 cp_lexer_consume_token (parser->lexer);
4470 /* Parse the template-id. */
4471 cp_parser_template_id (parser,
4472 /*template_keyword_p=*/true,
4473 /*check_dependency_p=*/false);
4474 /* Look for the `::' token. */
4475 cp_parser_require (parser, CPP_SCOPE, "`::'");
4477 /* If the next token is not a `~', then there might be some
4478 additional qualification. */
4479 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
4481 /* Look for the type-name. */
4482 *scope = TREE_TYPE (cp_parser_type_name (parser));
4483 /* Look for the `::' token. */
4484 cp_parser_require (parser, CPP_SCOPE, "`::'");
4489 /* Look for the `~'. */
4490 cp_parser_require (parser, CPP_COMPL, "`~'");
4491 /* Look for the type-name again. We are not responsible for
4492 checking that it matches the first type-name. */
4493 *type = cp_parser_type_name (parser);
4496 /* Parse a unary-expression.
4502 unary-operator cast-expression
4503 sizeof unary-expression
4511 __extension__ cast-expression
4512 __alignof__ unary-expression
4513 __alignof__ ( type-id )
4514 __real__ cast-expression
4515 __imag__ cast-expression
4518 ADDRESS_P is true iff the unary-expression is appearing as the
4519 operand of the `&' operator.
4521 Returns a representation of the expresion. */
4524 cp_parser_unary_expression (cp_parser *parser, bool address_p)
4527 enum tree_code unary_operator;
4529 /* Peek at the next token. */
4530 token = cp_lexer_peek_token (parser->lexer);
4531 /* Some keywords give away the kind of expression. */
4532 if (token->type == CPP_KEYWORD)
4534 enum rid keyword = token->keyword;
4540 /* Consume the `alignof' token. */
4541 cp_lexer_consume_token (parser->lexer);
4542 /* Parse the operand. */
4543 return finish_alignof (cp_parser_sizeof_operand
4551 /* Consume the `sizeof' token. */
4552 cp_lexer_consume_token (parser->lexer);
4553 /* Parse the operand. */
4554 operand = cp_parser_sizeof_operand (parser, keyword);
4556 /* If the type of the operand cannot be determined build a
4558 if (TYPE_P (operand)
4559 ? cp_parser_dependent_type_p (operand)
4560 : cp_parser_type_dependent_expression_p (operand))
4561 return build_min (SIZEOF_EXPR, size_type_node, operand);
4562 /* Otherwise, compute the constant value. */
4564 return finish_sizeof (operand);
4568 return cp_parser_new_expression (parser);
4571 return cp_parser_delete_expression (parser);
4575 /* The saved value of the PEDANTIC flag. */
4579 /* Save away the PEDANTIC flag. */
4580 cp_parser_extension_opt (parser, &saved_pedantic);
4581 /* Parse the cast-expression. */
4582 expr = cp_parser_cast_expression (parser, /*address_p=*/false);
4583 /* Restore the PEDANTIC flag. */
4584 pedantic = saved_pedantic;
4594 /* Consume the `__real__' or `__imag__' token. */
4595 cp_lexer_consume_token (parser->lexer);
4596 /* Parse the cast-expression. */
4597 expression = cp_parser_cast_expression (parser,
4598 /*address_p=*/false);
4599 /* Create the complete representation. */
4600 return build_x_unary_op ((keyword == RID_REALPART
4601 ? REALPART_EXPR : IMAGPART_EXPR),
4611 /* Look for the `:: new' and `:: delete', which also signal the
4612 beginning of a new-expression, or delete-expression,
4613 respectively. If the next token is `::', then it might be one of
4615 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
4619 /* See if the token after the `::' is one of the keywords in
4620 which we're interested. */
4621 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
4622 /* If it's `new', we have a new-expression. */
4623 if (keyword == RID_NEW)
4624 return cp_parser_new_expression (parser);
4625 /* Similarly, for `delete'. */
4626 else if (keyword == RID_DELETE)
4627 return cp_parser_delete_expression (parser);
4630 /* Look for a unary operator. */
4631 unary_operator = cp_parser_unary_operator (token);
4632 /* The `++' and `--' operators can be handled similarly, even though
4633 they are not technically unary-operators in the grammar. */
4634 if (unary_operator == ERROR_MARK)
4636 if (token->type == CPP_PLUS_PLUS)
4637 unary_operator = PREINCREMENT_EXPR;
4638 else if (token->type == CPP_MINUS_MINUS)
4639 unary_operator = PREDECREMENT_EXPR;
4640 /* Handle the GNU address-of-label extension. */
4641 else if (cp_parser_allow_gnu_extensions_p (parser)
4642 && token->type == CPP_AND_AND)
4646 /* Consume the '&&' token. */
4647 cp_lexer_consume_token (parser->lexer);
4648 /* Look for the identifier. */
4649 identifier = cp_parser_identifier (parser);
4650 /* Create an expression representing the address. */
4651 return finish_label_address_expr (identifier);
4654 if (unary_operator != ERROR_MARK)
4656 tree cast_expression;
4658 /* Consume the operator token. */
4659 token = cp_lexer_consume_token (parser->lexer);
4660 /* Parse the cast-expression. */
4662 = cp_parser_cast_expression (parser, unary_operator == ADDR_EXPR);
4663 /* Now, build an appropriate representation. */
4664 switch (unary_operator)
4667 return build_x_indirect_ref (cast_expression, "unary *");
4670 return build_x_unary_op (ADDR_EXPR, cast_expression);
4674 case TRUTH_NOT_EXPR:
4675 case PREINCREMENT_EXPR:
4676 case PREDECREMENT_EXPR:
4677 return finish_unary_op_expr (unary_operator, cast_expression);
4680 return build_x_unary_op (BIT_NOT_EXPR, cast_expression);
4684 return error_mark_node;
4688 return cp_parser_postfix_expression (parser, address_p);
4691 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
4692 unary-operator, the corresponding tree code is returned. */
4694 static enum tree_code
4695 cp_parser_unary_operator (token)
4698 switch (token->type)
4701 return INDIRECT_REF;
4707 return CONVERT_EXPR;
4713 return TRUTH_NOT_EXPR;
4716 return BIT_NOT_EXPR;
4723 /* Parse a new-expression.
4725 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
4726 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
4728 Returns a representation of the expression. */
4731 cp_parser_new_expression (parser)
4734 bool global_scope_p;
4739 /* Look for the optional `::' operator. */
4741 = (cp_parser_global_scope_opt (parser,
4742 /*current_scope_valid_p=*/false)
4744 /* Look for the `new' operator. */
4745 cp_parser_require_keyword (parser, RID_NEW, "`new'");
4746 /* There's no easy way to tell a new-placement from the
4747 `( type-id )' construct. */
4748 cp_parser_parse_tentatively (parser);
4749 /* Look for a new-placement. */
4750 placement = cp_parser_new_placement (parser);
4751 /* If that didn't work out, there's no new-placement. */
4752 if (!cp_parser_parse_definitely (parser))
4753 placement = NULL_TREE;
4755 /* If the next token is a `(', then we have a parenthesized
4757 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4759 /* Consume the `('. */
4760 cp_lexer_consume_token (parser->lexer);
4761 /* Parse the type-id. */
4762 type = cp_parser_type_id (parser);
4763 /* Look for the closing `)'. */
4764 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4766 /* Otherwise, there must be a new-type-id. */
4768 type = cp_parser_new_type_id (parser);
4770 /* If the next token is a `(', then we have a new-initializer. */
4771 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4772 initializer = cp_parser_new_initializer (parser);
4774 initializer = NULL_TREE;
4776 /* Create a representation of the new-expression. */
4777 return build_new (placement, type, initializer, global_scope_p);
4780 /* Parse a new-placement.
4785 Returns the same representation as for an expression-list. */
4788 cp_parser_new_placement (parser)
4791 tree expression_list;
4793 /* Look for the opening `('. */
4794 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
4795 return error_mark_node;
4796 /* Parse the expression-list. */
4797 expression_list = cp_parser_expression_list (parser);
4798 /* Look for the closing `)'. */
4799 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4801 return expression_list;
4804 /* Parse a new-type-id.
4807 type-specifier-seq new-declarator [opt]
4809 Returns a TREE_LIST whose TREE_PURPOSE is the type-specifier-seq,
4810 and whose TREE_VALUE is the new-declarator. */
4813 cp_parser_new_type_id (parser)
4816 tree type_specifier_seq;
4818 const char *saved_message;
4820 /* The type-specifier sequence must not contain type definitions.
4821 (It cannot contain declarations of new types either, but if they
4822 are not definitions we will catch that because they are not
4824 saved_message = parser->type_definition_forbidden_message;
4825 parser->type_definition_forbidden_message
4826 = "types may not be defined in a new-type-id";
4827 /* Parse the type-specifier-seq. */
4828 type_specifier_seq = cp_parser_type_specifier_seq (parser);
4829 /* Restore the old message. */
4830 parser->type_definition_forbidden_message = saved_message;
4831 /* Parse the new-declarator. */
4832 declarator = cp_parser_new_declarator_opt (parser);
4834 return build_tree_list (type_specifier_seq, declarator);
4837 /* Parse an (optional) new-declarator.
4840 ptr-operator new-declarator [opt]
4841 direct-new-declarator
4843 Returns a representation of the declarator. See
4844 cp_parser_declarator for the representations used. */
4847 cp_parser_new_declarator_opt (parser)
4850 enum tree_code code;
4852 tree cv_qualifier_seq;
4854 /* We don't know if there's a ptr-operator next, or not. */
4855 cp_parser_parse_tentatively (parser);
4856 /* Look for a ptr-operator. */
4857 code = cp_parser_ptr_operator (parser, &type, &cv_qualifier_seq);
4858 /* If that worked, look for more new-declarators. */
4859 if (cp_parser_parse_definitely (parser))
4863 /* Parse another optional declarator. */
4864 declarator = cp_parser_new_declarator_opt (parser);
4866 /* Create the representation of the declarator. */
4867 if (code == INDIRECT_REF)
4868 declarator = make_pointer_declarator (cv_qualifier_seq,
4871 declarator = make_reference_declarator (cv_qualifier_seq,
4874 /* Handle the pointer-to-member case. */
4876 declarator = build_nt (SCOPE_REF, type, declarator);
4881 /* If the next token is a `[', there is a direct-new-declarator. */
4882 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
4883 return cp_parser_direct_new_declarator (parser);
4888 /* Parse a direct-new-declarator.
4890 direct-new-declarator:
4892 direct-new-declarator [constant-expression]
4894 Returns an ARRAY_REF, following the same conventions as are
4895 documented for cp_parser_direct_declarator. */
4898 cp_parser_direct_new_declarator (parser)
4901 tree declarator = NULL_TREE;
4907 /* Look for the opening `['. */
4908 cp_parser_require (parser, CPP_OPEN_SQUARE, "`['");
4909 /* The first expression is not required to be constant. */
4912 expression = cp_parser_expression (parser);
4913 /* The standard requires that the expression have integral
4914 type. DR 74 adds enumeration types. We believe that the
4915 real intent is that these expressions be handled like the
4916 expression in a `switch' condition, which also allows
4917 classes with a single conversion to integral or
4918 enumeration type. */
4919 if (!processing_template_decl)
4922 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
4927 error ("expression in new-declarator must have integral or enumeration type");
4928 expression = error_mark_node;
4932 /* But all the other expressions must be. */
4934 expression = cp_parser_constant_expression (parser);
4935 /* Look for the closing `]'. */
4936 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4938 /* Add this bound to the declarator. */
4939 declarator = build_nt (ARRAY_REF, declarator, expression);
4941 /* If the next token is not a `[', then there are no more
4943 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
4950 /* Parse a new-initializer.
4953 ( expression-list [opt] )
4955 Returns a reprsentation of the expression-list. If there is no
4956 expression-list, VOID_ZERO_NODE is returned. */
4959 cp_parser_new_initializer (parser)
4962 tree expression_list;
4964 /* Look for the opening parenthesis. */
4965 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
4966 /* If the next token is not a `)', then there is an
4968 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
4969 expression_list = cp_parser_expression_list (parser);
4971 expression_list = void_zero_node;
4972 /* Look for the closing parenthesis. */
4973 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4975 return expression_list;
4978 /* Parse a delete-expression.
4981 :: [opt] delete cast-expression
4982 :: [opt] delete [ ] cast-expression
4984 Returns a representation of the expression. */
4987 cp_parser_delete_expression (parser)
4990 bool global_scope_p;
4994 /* Look for the optional `::' operator. */
4996 = (cp_parser_global_scope_opt (parser,
4997 /*current_scope_valid_p=*/false)
4999 /* Look for the `delete' keyword. */
5000 cp_parser_require_keyword (parser, RID_DELETE, "`delete'");
5001 /* See if the array syntax is in use. */
5002 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5004 /* Consume the `[' token. */
5005 cp_lexer_consume_token (parser->lexer);
5006 /* Look for the `]' token. */
5007 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5008 /* Remember that this is the `[]' construct. */
5014 /* Parse the cast-expression. */
5015 expression = cp_parser_cast_expression (parser, /*address_p=*/false);
5017 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
5020 /* Parse a cast-expression.
5024 ( type-id ) cast-expression
5026 Returns a representation of the expression. */
5029 cp_parser_cast_expression (cp_parser *parser, bool address_p)
5031 /* If it's a `(', then we might be looking at a cast. */
5032 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5034 tree type = NULL_TREE;
5035 tree expr = NULL_TREE;
5036 bool compound_literal_p;
5037 const char *saved_message;
5039 /* There's no way to know yet whether or not this is a cast.
5040 For example, `(int (3))' is a unary-expression, while `(int)
5041 3' is a cast. So, we resort to parsing tentatively. */
5042 cp_parser_parse_tentatively (parser);
5043 /* Types may not be defined in a cast. */
5044 saved_message = parser->type_definition_forbidden_message;
5045 parser->type_definition_forbidden_message
5046 = "types may not be defined in casts";
5047 /* Consume the `('. */
5048 cp_lexer_consume_token (parser->lexer);
5049 /* A very tricky bit is that `(struct S) { 3 }' is a
5050 compound-literal (which we permit in C++ as an extension).
5051 But, that construct is not a cast-expression -- it is a
5052 postfix-expression. (The reason is that `(struct S) { 3 }.i'
5053 is legal; if the compound-literal were a cast-expression,
5054 you'd need an extra set of parentheses.) But, if we parse
5055 the type-id, and it happens to be a class-specifier, then we
5056 will commit to the parse at that point, because we cannot
5057 undo the action that is done when creating a new class. So,
5058 then we cannot back up and do a postfix-expression.
5060 Therefore, we scan ahead to the closing `)', and check to see
5061 if the token after the `)' is a `{'. If so, we are not
5062 looking at a cast-expression.
5064 Save tokens so that we can put them back. */
5065 cp_lexer_save_tokens (parser->lexer);
5066 /* Skip tokens until the next token is a closing parenthesis.
5067 If we find the closing `)', and the next token is a `{', then
5068 we are looking at a compound-literal. */
5070 = (cp_parser_skip_to_closing_parenthesis (parser)
5071 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
5072 /* Roll back the tokens we skipped. */
5073 cp_lexer_rollback_tokens (parser->lexer);
5074 /* If we were looking at a compound-literal, simulate an error
5075 so that the call to cp_parser_parse_definitely below will
5077 if (compound_literal_p)
5078 cp_parser_simulate_error (parser);
5081 /* Look for the type-id. */
5082 type = cp_parser_type_id (parser);
5083 /* Look for the closing `)'. */
5084 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5087 /* Restore the saved message. */
5088 parser->type_definition_forbidden_message = saved_message;
5090 /* If all went well, this is a cast. */
5091 if (cp_parser_parse_definitely (parser))
5093 /* Parse the dependent expression. */
5094 expr = cp_parser_cast_expression (parser, /*address_p=*/false);
5095 /* Warn about old-style casts, if so requested. */
5096 if (warn_old_style_cast
5097 && !in_system_header
5098 && !VOID_TYPE_P (type)
5099 && current_lang_name != lang_name_c)
5100 warning ("use of old-style cast");
5101 /* Perform the cast. */
5102 expr = build_c_cast (type, expr);
5109 /* If we get here, then it's not a cast, so it must be a
5110 unary-expression. */
5111 return cp_parser_unary_expression (parser, address_p);
5114 /* Parse a pm-expression.
5118 pm-expression .* cast-expression
5119 pm-expression ->* cast-expression
5121 Returns a representation of the expression. */
5124 cp_parser_pm_expression (parser)
5130 /* Parse the cast-expresion. */
5131 cast_expr = cp_parser_cast_expression (parser, /*address_p=*/false);
5132 pm_expr = cast_expr;
5133 /* Now look for pointer-to-member operators. */
5137 enum cpp_ttype token_type;
5139 /* Peek at the next token. */
5140 token = cp_lexer_peek_token (parser->lexer);
5141 token_type = token->type;
5142 /* If it's not `.*' or `->*' there's no pointer-to-member
5144 if (token_type != CPP_DOT_STAR
5145 && token_type != CPP_DEREF_STAR)
5148 /* Consume the token. */
5149 cp_lexer_consume_token (parser->lexer);
5151 /* Parse another cast-expression. */
5152 cast_expr = cp_parser_cast_expression (parser, /*address_p=*/false);
5154 /* Build the representation of the pointer-to-member
5156 if (token_type == CPP_DEREF_STAR)
5157 pm_expr = build_x_binary_op (MEMBER_REF, pm_expr, cast_expr);
5159 pm_expr = build_m_component_ref (pm_expr, cast_expr);
5165 /* Parse a multiplicative-expression.
5167 mulitplicative-expression:
5169 multiplicative-expression * pm-expression
5170 multiplicative-expression / pm-expression
5171 multiplicative-expression % pm-expression
5173 Returns a representation of the expression. */
5176 cp_parser_multiplicative_expression (parser)
5179 static const cp_parser_token_tree_map map = {
5180 { CPP_MULT, MULT_EXPR },
5181 { CPP_DIV, TRUNC_DIV_EXPR },
5182 { CPP_MOD, TRUNC_MOD_EXPR },
5183 { CPP_EOF, ERROR_MARK }
5186 return cp_parser_binary_expression (parser,
5188 cp_parser_pm_expression);
5191 /* Parse an additive-expression.
5193 additive-expression:
5194 multiplicative-expression
5195 additive-expression + multiplicative-expression
5196 additive-expression - multiplicative-expression
5198 Returns a representation of the expression. */
5201 cp_parser_additive_expression (parser)
5204 static const cp_parser_token_tree_map map = {
5205 { CPP_PLUS, PLUS_EXPR },
5206 { CPP_MINUS, MINUS_EXPR },
5207 { CPP_EOF, ERROR_MARK }
5210 return cp_parser_binary_expression (parser,
5212 cp_parser_multiplicative_expression);
5215 /* Parse a shift-expression.
5219 shift-expression << additive-expression
5220 shift-expression >> additive-expression
5222 Returns a representation of the expression. */
5225 cp_parser_shift_expression (parser)
5228 static const cp_parser_token_tree_map map = {
5229 { CPP_LSHIFT, LSHIFT_EXPR },
5230 { CPP_RSHIFT, RSHIFT_EXPR },
5231 { CPP_EOF, ERROR_MARK }
5234 return cp_parser_binary_expression (parser,
5236 cp_parser_additive_expression);
5239 /* Parse a relational-expression.
5241 relational-expression:
5243 relational-expression < shift-expression
5244 relational-expression > shift-expression
5245 relational-expression <= shift-expression
5246 relational-expression >= shift-expression
5250 relational-expression:
5251 relational-expression <? shift-expression
5252 relational-expression >? shift-expression
5254 Returns a representation of the expression. */
5257 cp_parser_relational_expression (parser)
5260 static const cp_parser_token_tree_map map = {
5261 { CPP_LESS, LT_EXPR },
5262 { CPP_GREATER, GT_EXPR },
5263 { CPP_LESS_EQ, LE_EXPR },
5264 { CPP_GREATER_EQ, GE_EXPR },
5265 { CPP_MIN, MIN_EXPR },
5266 { CPP_MAX, MAX_EXPR },
5267 { CPP_EOF, ERROR_MARK }
5270 return cp_parser_binary_expression (parser,
5272 cp_parser_shift_expression);
5275 /* Parse an equality-expression.
5277 equality-expression:
5278 relational-expression
5279 equality-expression == relational-expression
5280 equality-expression != relational-expression
5282 Returns a representation of the expression. */
5285 cp_parser_equality_expression (parser)
5288 static const cp_parser_token_tree_map map = {
5289 { CPP_EQ_EQ, EQ_EXPR },
5290 { CPP_NOT_EQ, NE_EXPR },
5291 { CPP_EOF, ERROR_MARK }
5294 return cp_parser_binary_expression (parser,
5296 cp_parser_relational_expression);
5299 /* Parse an and-expression.
5303 and-expression & equality-expression
5305 Returns a representation of the expression. */
5308 cp_parser_and_expression (parser)
5311 static const cp_parser_token_tree_map map = {
5312 { CPP_AND, BIT_AND_EXPR },
5313 { CPP_EOF, ERROR_MARK }
5316 return cp_parser_binary_expression (parser,
5318 cp_parser_equality_expression);
5321 /* Parse an exclusive-or-expression.
5323 exclusive-or-expression:
5325 exclusive-or-expression ^ and-expression
5327 Returns a representation of the expression. */
5330 cp_parser_exclusive_or_expression (parser)
5333 static const cp_parser_token_tree_map map = {
5334 { CPP_XOR, BIT_XOR_EXPR },
5335 { CPP_EOF, ERROR_MARK }
5338 return cp_parser_binary_expression (parser,
5340 cp_parser_and_expression);
5344 /* Parse an inclusive-or-expression.
5346 inclusive-or-expression:
5347 exclusive-or-expression
5348 inclusive-or-expression | exclusive-or-expression
5350 Returns a representation of the expression. */
5353 cp_parser_inclusive_or_expression (parser)
5356 static const cp_parser_token_tree_map map = {
5357 { CPP_OR, BIT_IOR_EXPR },
5358 { CPP_EOF, ERROR_MARK }
5361 return cp_parser_binary_expression (parser,
5363 cp_parser_exclusive_or_expression);
5366 /* Parse a logical-and-expression.
5368 logical-and-expression:
5369 inclusive-or-expression
5370 logical-and-expression && inclusive-or-expression
5372 Returns a representation of the expression. */
5375 cp_parser_logical_and_expression (parser)
5378 static const cp_parser_token_tree_map map = {
5379 { CPP_AND_AND, TRUTH_ANDIF_EXPR },
5380 { CPP_EOF, ERROR_MARK }
5383 return cp_parser_binary_expression (parser,
5385 cp_parser_inclusive_or_expression);
5388 /* Parse a logical-or-expression.
5390 logical-or-expression:
5391 logical-and-expresion
5392 logical-or-expression || logical-and-expression
5394 Returns a representation of the expression. */
5397 cp_parser_logical_or_expression (parser)
5400 static const cp_parser_token_tree_map map = {
5401 { CPP_OR_OR, TRUTH_ORIF_EXPR },
5402 { CPP_EOF, ERROR_MARK }
5405 return cp_parser_binary_expression (parser,
5407 cp_parser_logical_and_expression);
5410 /* Parse a conditional-expression.
5412 conditional-expression:
5413 logical-or-expression
5414 logical-or-expression ? expression : assignment-expression
5418 conditional-expression:
5419 logical-or-expression ? : assignment-expression
5421 Returns a representation of the expression. */
5424 cp_parser_conditional_expression (parser)
5427 tree logical_or_expr;
5429 /* Parse the logical-or-expression. */
5430 logical_or_expr = cp_parser_logical_or_expression (parser);
5431 /* If the next token is a `?', then we have a real conditional
5433 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
5434 return cp_parser_question_colon_clause (parser, logical_or_expr);
5435 /* Otherwise, the value is simply the logical-or-expression. */
5437 return logical_or_expr;
5440 /* Parse the `? expression : assignment-expression' part of a
5441 conditional-expression. The LOGICAL_OR_EXPR is the
5442 logical-or-expression that started the conditional-expression.
5443 Returns a representation of the entire conditional-expression.
5445 This routine exists only so that it can be shared between
5446 cp_parser_conditional_expression and
5447 cp_parser_assignment_expression.
5449 ? expression : assignment-expression
5453 ? : assignment-expression */
5456 cp_parser_question_colon_clause (parser, logical_or_expr)
5458 tree logical_or_expr;
5461 tree assignment_expr;
5463 /* Consume the `?' token. */
5464 cp_lexer_consume_token (parser->lexer);
5465 if (cp_parser_allow_gnu_extensions_p (parser)
5466 && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
5467 /* Implicit true clause. */
5470 /* Parse the expression. */
5471 expr = cp_parser_expression (parser);
5473 /* The next token should be a `:'. */
5474 cp_parser_require (parser, CPP_COLON, "`:'");
5475 /* Parse the assignment-expression. */
5476 assignment_expr = cp_parser_assignment_expression (parser);
5478 /* Build the conditional-expression. */
5479 return build_x_conditional_expr (logical_or_expr,
5484 /* Parse an assignment-expression.
5486 assignment-expression:
5487 conditional-expression
5488 logical-or-expression assignment-operator assignment_expression
5491 Returns a representation for the expression. */
5494 cp_parser_assignment_expression (parser)
5499 /* If the next token is the `throw' keyword, then we're looking at
5500 a throw-expression. */
5501 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
5502 expr = cp_parser_throw_expression (parser);
5503 /* Otherwise, it must be that we are looking at a
5504 logical-or-expression. */
5507 /* Parse the logical-or-expression. */
5508 expr = cp_parser_logical_or_expression (parser);
5509 /* If the next token is a `?' then we're actually looking at a
5510 conditional-expression. */
5511 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
5512 return cp_parser_question_colon_clause (parser, expr);
5515 enum tree_code assignment_operator;
5517 /* If it's an assignment-operator, we're using the second
5520 = cp_parser_assignment_operator_opt (parser);
5521 if (assignment_operator != ERROR_MARK)
5525 /* Parse the right-hand side of the assignment. */
5526 rhs = cp_parser_assignment_expression (parser);
5527 /* Build the asignment expression. */
5528 expr = build_x_modify_expr (expr,
5529 assignment_operator,
5538 /* Parse an (optional) assignment-operator.
5540 assignment-operator: one of
5541 = *= /= %= += -= >>= <<= &= ^= |=
5545 assignment-operator: one of
5548 If the next token is an assignment operator, the corresponding tree
5549 code is returned, and the token is consumed. For example, for
5550 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
5551 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
5552 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
5553 operator, ERROR_MARK is returned. */
5555 static enum tree_code
5556 cp_parser_assignment_operator_opt (parser)
5562 /* Peek at the next toen. */
5563 token = cp_lexer_peek_token (parser->lexer);
5565 switch (token->type)
5576 op = TRUNC_DIV_EXPR;
5580 op = TRUNC_MOD_EXPR;
5620 /* Nothing else is an assignment operator. */
5624 /* If it was an assignment operator, consume it. */
5625 if (op != ERROR_MARK)
5626 cp_lexer_consume_token (parser->lexer);
5631 /* Parse an expression.
5634 assignment-expression
5635 expression , assignment-expression
5637 Returns a representation of the expression. */
5640 cp_parser_expression (parser)
5643 tree expression = NULL_TREE;
5644 bool saw_comma_p = false;
5648 tree assignment_expression;
5650 /* Parse the next assignment-expression. */
5651 assignment_expression
5652 = cp_parser_assignment_expression (parser);
5653 /* If this is the first assignment-expression, we can just
5656 expression = assignment_expression;
5657 /* Otherwise, chain the expressions together. It is unclear why
5658 we do not simply build COMPOUND_EXPRs as we go. */
5660 expression = tree_cons (NULL_TREE,
5661 assignment_expression,
5663 /* If the next token is not a comma, then we are done with the
5665 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5667 /* Consume the `,'. */
5668 cp_lexer_consume_token (parser->lexer);
5669 /* The first time we see a `,', we must take special action
5670 because the representation used for a single expression is
5671 different from that used for a list containing the single
5675 /* Remember that this expression has a `,' in it. */
5677 /* Turn the EXPRESSION into a TREE_LIST so that we can link
5678 additional expressions to it. */
5679 expression = build_tree_list (NULL_TREE, expression);
5683 /* Build a COMPOUND_EXPR to represent the entire expression, if
5684 necessary. We built up the list in reverse order, so we must
5685 straighten it out here. */
5687 expression = build_x_compound_expr (nreverse (expression));
5692 /* Parse a constant-expression.
5694 constant-expression:
5695 conditional-expression */
5698 cp_parser_constant_expression (parser)
5701 bool saved_constant_expression_p;
5704 /* It might seem that we could simply parse the
5705 conditional-expression, and then check to see if it were
5706 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
5707 one that the compiler can figure out is constant, possibly after
5708 doing some simplifications or optimizations. The standard has a
5709 precise definition of constant-expression, and we must honor
5710 that, even though it is somewhat more restrictive.
5716 is not a legal declaration, because `(2, 3)' is not a
5717 constant-expression. The `,' operator is forbidden in a
5718 constant-expression. However, GCC's constant-folding machinery
5719 will fold this operation to an INTEGER_CST for `3'. */
5721 /* Save the old setting of CONSTANT_EXPRESSION_P. */
5722 saved_constant_expression_p = parser->constant_expression_p;
5723 /* We are now parsing a constant-expression. */
5724 parser->constant_expression_p = true;
5725 /* Parse the conditional-expression. */
5726 expression = cp_parser_conditional_expression (parser);
5727 /* Restore the old setting of CONSTANT_EXPRESSION_P. */
5728 parser->constant_expression_p = saved_constant_expression_p;
5733 /* Statements [gram.stmt.stmt] */
5735 /* Parse a statement.
5739 expression-statement
5744 declaration-statement
5748 cp_parser_statement (parser)
5753 int statement_line_number;
5755 /* There is no statement yet. */
5756 statement = NULL_TREE;
5757 /* Peek at the next token. */
5758 token = cp_lexer_peek_token (parser->lexer);
5759 /* Remember the line number of the first token in the statement. */
5760 statement_line_number = token->line_number;
5761 /* If this is a keyword, then that will often determine what kind of
5762 statement we have. */
5763 if (token->type == CPP_KEYWORD)
5765 enum rid keyword = token->keyword;
5771 statement = cp_parser_labeled_statement (parser);
5776 statement = cp_parser_selection_statement (parser);
5782 statement = cp_parser_iteration_statement (parser);
5789 statement = cp_parser_jump_statement (parser);
5793 statement = cp_parser_try_block (parser);
5797 /* It might be a keyword like `int' that can start a
5798 declaration-statement. */
5802 else if (token->type == CPP_NAME)
5804 /* If the next token is a `:', then we are looking at a
5805 labeled-statement. */
5806 token = cp_lexer_peek_nth_token (parser->lexer, 2);
5807 if (token->type == CPP_COLON)
5808 statement = cp_parser_labeled_statement (parser);
5810 /* Anything that starts with a `{' must be a compound-statement. */
5811 else if (token->type == CPP_OPEN_BRACE)
5812 statement = cp_parser_compound_statement (parser);
5814 /* Everything else must be a declaration-statement or an
5815 expression-statement. Try for the declaration-statement
5816 first, unless we are looking at a `;', in which case we know that
5817 we have an expression-statement. */
5820 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
5822 cp_parser_parse_tentatively (parser);
5823 /* Try to parse the declaration-statement. */
5824 cp_parser_declaration_statement (parser);
5825 /* If that worked, we're done. */
5826 if (cp_parser_parse_definitely (parser))
5829 /* Look for an expression-statement instead. */
5830 statement = cp_parser_expression_statement (parser);
5833 /* Set the line number for the statement. */
5834 if (statement && statement_code_p (TREE_CODE (statement)))
5835 STMT_LINENO (statement) = statement_line_number;
5838 /* Parse a labeled-statement.
5841 identifier : statement
5842 case constant-expression : statement
5845 Returns the new CASE_LABEL, for a `case' or `default' label. For
5846 an ordinary label, returns a LABEL_STMT. */
5849 cp_parser_labeled_statement (parser)
5853 tree statement = NULL_TREE;
5855 /* The next token should be an identifier. */
5856 token = cp_lexer_peek_token (parser->lexer);
5857 if (token->type != CPP_NAME
5858 && token->type != CPP_KEYWORD)
5860 cp_parser_error (parser, "expected labeled-statement");
5861 return error_mark_node;
5864 switch (token->keyword)
5870 /* Consume the `case' token. */
5871 cp_lexer_consume_token (parser->lexer);
5872 /* Parse the constant-expression. */
5873 expr = cp_parser_constant_expression (parser);
5874 /* Create the label. */
5875 statement = finish_case_label (expr, NULL_TREE);
5880 /* Consume the `default' token. */
5881 cp_lexer_consume_token (parser->lexer);
5882 /* Create the label. */
5883 statement = finish_case_label (NULL_TREE, NULL_TREE);
5887 /* Anything else must be an ordinary label. */
5888 statement = finish_label_stmt (cp_parser_identifier (parser));
5892 /* Require the `:' token. */
5893 cp_parser_require (parser, CPP_COLON, "`:'");
5894 /* Parse the labeled statement. */
5895 cp_parser_statement (parser);
5897 /* Return the label, in the case of a `case' or `default' label. */
5901 /* Parse an expression-statement.
5903 expression-statement:
5906 Returns the new EXPR_STMT -- or NULL_TREE if the expression
5907 statement consists of nothing more than an `;'. */
5910 cp_parser_expression_statement (parser)
5915 /* If the next token is not a `;', then there is an expression to parse. */
5916 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
5917 statement = finish_expr_stmt (cp_parser_expression (parser));
5918 /* Otherwise, we do not even bother to build an EXPR_STMT. */
5922 statement = NULL_TREE;
5924 /* Consume the final `;'. */
5925 if (!cp_parser_require (parser, CPP_SEMICOLON, "`;'"))
5927 /* If there is additional (erroneous) input, skip to the end of
5929 cp_parser_skip_to_end_of_statement (parser);
5930 /* If the next token is now a `;', consume it. */
5931 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
5932 cp_lexer_consume_token (parser->lexer);
5938 /* Parse a compound-statement.
5941 { statement-seq [opt] }
5943 Returns a COMPOUND_STMT representing the statement. */
5946 cp_parser_compound_statement (cp_parser *parser)
5950 /* Consume the `{'. */
5951 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
5952 return error_mark_node;
5953 /* Begin the compound-statement. */
5954 compound_stmt = begin_compound_stmt (/*has_no_scope=*/0);
5955 /* Parse an (optional) statement-seq. */
5956 cp_parser_statement_seq_opt (parser);
5957 /* Finish the compound-statement. */
5958 finish_compound_stmt (/*has_no_scope=*/0, compound_stmt);
5959 /* Consume the `}'. */
5960 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
5962 return compound_stmt;
5965 /* Parse an (optional) statement-seq.
5969 statement-seq [opt] statement */
5972 cp_parser_statement_seq_opt (parser)
5975 /* Scan statements until there aren't any more. */
5978 /* If we're looking at a `}', then we've run out of statements. */
5979 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE)
5980 || cp_lexer_next_token_is (parser->lexer, CPP_EOF))
5983 /* Parse the statement. */
5984 cp_parser_statement (parser);
5988 /* Parse a selection-statement.
5990 selection-statement:
5991 if ( condition ) statement
5992 if ( condition ) statement else statement
5993 switch ( condition ) statement
5995 Returns the new IF_STMT or SWITCH_STMT. */
5998 cp_parser_selection_statement (parser)
6004 /* Peek at the next token. */
6005 token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
6007 /* See what kind of keyword it is. */
6008 keyword = token->keyword;
6017 /* Look for the `('. */
6018 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
6020 cp_parser_skip_to_end_of_statement (parser);
6021 return error_mark_node;
6024 /* Begin the selection-statement. */
6025 if (keyword == RID_IF)
6026 statement = begin_if_stmt ();
6028 statement = begin_switch_stmt ();
6030 /* Parse the condition. */
6031 condition = cp_parser_condition (parser);
6032 /* Look for the `)'. */
6033 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
6034 cp_parser_skip_to_closing_parenthesis (parser);
6036 if (keyword == RID_IF)
6040 /* Add the condition. */
6041 finish_if_stmt_cond (condition, statement);
6043 /* Parse the then-clause. */
6044 then_stmt = cp_parser_implicitly_scoped_statement (parser);
6045 finish_then_clause (statement);
6047 /* If the next token is `else', parse the else-clause. */
6048 if (cp_lexer_next_token_is_keyword (parser->lexer,
6053 /* Consume the `else' keyword. */
6054 cp_lexer_consume_token (parser->lexer);
6055 /* Parse the else-clause. */
6057 = cp_parser_implicitly_scoped_statement (parser);
6058 finish_else_clause (statement);
6061 /* Now we're all done with the if-statement. */
6068 /* Add the condition. */
6069 finish_switch_cond (condition, statement);
6071 /* Parse the body of the switch-statement. */
6072 body = cp_parser_implicitly_scoped_statement (parser);
6074 /* Now we're all done with the switch-statement. */
6075 finish_switch_stmt (statement);
6083 cp_parser_error (parser, "expected selection-statement");
6084 return error_mark_node;
6088 /* Parse a condition.
6092 type-specifier-seq declarator = assignment-expression
6097 type-specifier-seq declarator asm-specification [opt]
6098 attributes [opt] = assignment-expression
6100 Returns the expression that should be tested. */
6103 cp_parser_condition (parser)
6106 tree type_specifiers;
6107 const char *saved_message;
6109 /* Try the declaration first. */
6110 cp_parser_parse_tentatively (parser);
6111 /* New types are not allowed in the type-specifier-seq for a
6113 saved_message = parser->type_definition_forbidden_message;
6114 parser->type_definition_forbidden_message
6115 = "types may not be defined in conditions";
6116 /* Parse the type-specifier-seq. */
6117 type_specifiers = cp_parser_type_specifier_seq (parser);
6118 /* Restore the saved message. */
6119 parser->type_definition_forbidden_message = saved_message;
6120 /* If all is well, we might be looking at a declaration. */
6121 if (!cp_parser_error_occurred (parser))
6124 tree asm_specification;
6127 tree initializer = NULL_TREE;
6129 /* Parse the declarator. */
6130 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
6131 /*ctor_dtor_or_conv_p=*/NULL);
6132 /* Parse the attributes. */
6133 attributes = cp_parser_attributes_opt (parser);
6134 /* Parse the asm-specification. */
6135 asm_specification = cp_parser_asm_specification_opt (parser);
6136 /* If the next token is not an `=', then we might still be
6137 looking at an expression. For example:
6141 looks like a decl-specifier-seq and a declarator -- but then
6142 there is no `=', so this is an expression. */
6143 cp_parser_require (parser, CPP_EQ, "`='");
6144 /* If we did see an `=', then we are looking at a declaration
6146 if (cp_parser_parse_definitely (parser))
6148 /* Create the declaration. */
6149 decl = start_decl (declarator, type_specifiers,
6150 /*initialized_p=*/true,
6151 attributes, /*prefix_attributes=*/NULL_TREE);
6152 /* Parse the assignment-expression. */
6153 initializer = cp_parser_assignment_expression (parser);
6155 /* Process the initializer. */
6156 cp_finish_decl (decl,
6159 LOOKUP_ONLYCONVERTING);
6161 return convert_from_reference (decl);
6164 /* If we didn't even get past the declarator successfully, we are
6165 definitely not looking at a declaration. */
6167 cp_parser_abort_tentative_parse (parser);
6169 /* Otherwise, we are looking at an expression. */
6170 return cp_parser_expression (parser);
6173 /* Parse an iteration-statement.
6175 iteration-statement:
6176 while ( condition ) statement
6177 do statement while ( expression ) ;
6178 for ( for-init-statement condition [opt] ; expression [opt] )
6181 Returns the new WHILE_STMT, DO_STMT, or FOR_STMT. */
6184 cp_parser_iteration_statement (parser)
6191 /* Peek at the next token. */
6192 token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
6194 return error_mark_node;
6196 /* See what kind of keyword it is. */
6197 keyword = token->keyword;
6204 /* Begin the while-statement. */
6205 statement = begin_while_stmt ();
6206 /* Look for the `('. */
6207 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6208 /* Parse the condition. */
6209 condition = cp_parser_condition (parser);
6210 finish_while_stmt_cond (condition, statement);
6211 /* Look for the `)'. */
6212 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6213 /* Parse the dependent statement. */
6214 cp_parser_already_scoped_statement (parser);
6215 /* We're done with the while-statement. */
6216 finish_while_stmt (statement);
6224 /* Begin the do-statement. */
6225 statement = begin_do_stmt ();
6226 /* Parse the body of the do-statement. */
6227 cp_parser_implicitly_scoped_statement (parser);
6228 finish_do_body (statement);
6229 /* Look for the `while' keyword. */
6230 cp_parser_require_keyword (parser, RID_WHILE, "`while'");
6231 /* Look for the `('. */
6232 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6233 /* Parse the expression. */
6234 expression = cp_parser_expression (parser);
6235 /* We're done with the do-statement. */
6236 finish_do_stmt (expression, statement);
6237 /* Look for the `)'. */
6238 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6239 /* Look for the `;'. */
6240 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6246 tree condition = NULL_TREE;
6247 tree expression = NULL_TREE;
6249 /* Begin the for-statement. */
6250 statement = begin_for_stmt ();
6251 /* Look for the `('. */
6252 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6253 /* Parse the initialization. */
6254 cp_parser_for_init_statement (parser);
6255 finish_for_init_stmt (statement);
6257 /* If there's a condition, process it. */
6258 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6259 condition = cp_parser_condition (parser);
6260 finish_for_cond (condition, statement);
6261 /* Look for the `;'. */
6262 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6264 /* If there's an expression, process it. */
6265 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
6266 expression = cp_parser_expression (parser);
6267 finish_for_expr (expression, statement);
6268 /* Look for the `)'. */
6269 cp_parser_require (parser, CPP_CLOSE_PAREN, "`;'");
6271 /* Parse the body of the for-statement. */
6272 cp_parser_already_scoped_statement (parser);
6274 /* We're done with the for-statement. */
6275 finish_for_stmt (statement);
6280 cp_parser_error (parser, "expected iteration-statement");
6281 statement = error_mark_node;
6288 /* Parse a for-init-statement.
6291 expression-statement
6292 simple-declaration */
6295 cp_parser_for_init_statement (parser)
6298 /* If the next token is a `;', then we have an empty
6299 expression-statement. Gramatically, this is also a
6300 simple-declaration, but an invalid one, because it does not
6301 declare anything. Therefore, if we did not handle this case
6302 specially, we would issue an error message about an invalid
6304 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6306 /* We're going to speculatively look for a declaration, falling back
6307 to an expression, if necessary. */
6308 cp_parser_parse_tentatively (parser);
6309 /* Parse the declaration. */
6310 cp_parser_simple_declaration (parser,
6311 /*function_definition_allowed_p=*/false);
6312 /* If the tentative parse failed, then we shall need to look for an
6313 expression-statement. */
6314 if (cp_parser_parse_definitely (parser))
6318 cp_parser_expression_statement (parser);
6321 /* Parse a jump-statement.
6326 return expression [opt] ;
6334 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_STMT, or
6338 cp_parser_jump_statement (parser)
6341 tree statement = error_mark_node;
6345 /* Peek at the next token. */
6346 token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
6348 return error_mark_node;
6350 /* See what kind of keyword it is. */
6351 keyword = token->keyword;
6355 statement = finish_break_stmt ();
6356 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6360 statement = finish_continue_stmt ();
6361 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6368 /* If the next token is a `;', then there is no
6370 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6371 expr = cp_parser_expression (parser);
6374 /* Build the return-statement. */
6375 statement = finish_return_stmt (expr);
6376 /* Look for the final `;'. */
6377 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6382 /* Create the goto-statement. */
6383 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
6385 /* Issue a warning about this use of a GNU extension. */
6387 pedwarn ("ISO C++ forbids computed gotos");
6388 /* Consume the '*' token. */
6389 cp_lexer_consume_token (parser->lexer);
6390 /* Parse the dependent expression. */
6391 finish_goto_stmt (cp_parser_expression (parser));
6394 finish_goto_stmt (cp_parser_identifier (parser));
6395 /* Look for the final `;'. */
6396 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6400 cp_parser_error (parser, "expected jump-statement");
6407 /* Parse a declaration-statement.
6409 declaration-statement:
6410 block-declaration */
6413 cp_parser_declaration_statement (parser)
6416 /* Parse the block-declaration. */
6417 cp_parser_block_declaration (parser, /*statement_p=*/true);
6419 /* Finish off the statement. */
6423 /* Some dependent statements (like `if (cond) statement'), are
6424 implicitly in their own scope. In other words, if the statement is
6425 a single statement (as opposed to a compound-statement), it is
6426 none-the-less treated as if it were enclosed in braces. Any
6427 declarations appearing in the dependent statement are out of scope
6428 after control passes that point. This function parses a statement,
6429 but ensures that is in its own scope, even if it is not a
6432 Returns the new statement. */
6435 cp_parser_implicitly_scoped_statement (parser)
6440 /* If the token is not a `{', then we must take special action. */
6441 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
6443 /* Create a compound-statement. */
6444 statement = begin_compound_stmt (/*has_no_scope=*/0);
6445 /* Parse the dependent-statement. */
6446 cp_parser_statement (parser);
6447 /* Finish the dummy compound-statement. */
6448 finish_compound_stmt (/*has_no_scope=*/0, statement);
6450 /* Otherwise, we simply parse the statement directly. */
6452 statement = cp_parser_compound_statement (parser);
6454 /* Return the statement. */
6458 /* For some dependent statements (like `while (cond) statement'), we
6459 have already created a scope. Therefore, even if the dependent
6460 statement is a compound-statement, we do not want to create another
6464 cp_parser_already_scoped_statement (parser)
6467 /* If the token is not a `{', then we must take special action. */
6468 if (cp_lexer_next_token_is_not(parser->lexer, CPP_OPEN_BRACE))
6472 /* Create a compound-statement. */
6473 statement = begin_compound_stmt (/*has_no_scope=*/1);
6474 /* Parse the dependent-statement. */
6475 cp_parser_statement (parser);
6476 /* Finish the dummy compound-statement. */
6477 finish_compound_stmt (/*has_no_scope=*/1, statement);
6479 /* Otherwise, we simply parse the statement directly. */
6481 cp_parser_statement (parser);
6484 /* Declarations [gram.dcl.dcl] */
6486 /* Parse an optional declaration-sequence.
6490 declaration-seq declaration */
6493 cp_parser_declaration_seq_opt (parser)
6500 token = cp_lexer_peek_token (parser->lexer);
6502 if (token->type == CPP_CLOSE_BRACE
6503 || token->type == CPP_EOF)
6506 if (token->type == CPP_SEMICOLON)
6508 /* A declaration consisting of a single semicolon is
6509 invalid. Allow it unless we're being pedantic. */
6511 pedwarn ("extra `;'");
6512 cp_lexer_consume_token (parser->lexer);
6516 /* The C lexer modifies PENDING_LANG_CHANGE when it wants the
6517 parser to enter or exit implict `extern "C"' blocks. */
6518 while (pending_lang_change > 0)
6520 push_lang_context (lang_name_c);
6521 --pending_lang_change;
6523 while (pending_lang_change < 0)
6525 pop_lang_context ();
6526 ++pending_lang_change;
6529 /* Parse the declaration itself. */
6530 cp_parser_declaration (parser);
6534 /* Parse a declaration.
6539 template-declaration
6540 explicit-instantiation
6541 explicit-specialization
6542 linkage-specification
6543 namespace-definition
6548 __extension__ declaration */
6551 cp_parser_declaration (parser)
6558 /* Check for the `__extension__' keyword. */
6559 if (cp_parser_extension_opt (parser, &saved_pedantic))
6561 /* Parse the qualified declaration. */
6562 cp_parser_declaration (parser);
6563 /* Restore the PEDANTIC flag. */
6564 pedantic = saved_pedantic;
6569 /* Try to figure out what kind of declaration is present. */
6570 token1 = *cp_lexer_peek_token (parser->lexer);
6571 if (token1.type != CPP_EOF)
6572 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
6574 /* If the next token is `extern' and the following token is a string
6575 literal, then we have a linkage specification. */
6576 if (token1.keyword == RID_EXTERN
6577 && cp_parser_is_string_literal (&token2))
6578 cp_parser_linkage_specification (parser);
6579 /* If the next token is `template', then we have either a template
6580 declaration, an explicit instantiation, or an explicit
6582 else if (token1.keyword == RID_TEMPLATE)
6584 /* `template <>' indicates a template specialization. */
6585 if (token2.type == CPP_LESS
6586 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
6587 cp_parser_explicit_specialization (parser);
6588 /* `template <' indicates a template declaration. */
6589 else if (token2.type == CPP_LESS)
6590 cp_parser_template_declaration (parser, /*member_p=*/false);
6591 /* Anything else must be an explicit instantiation. */
6593 cp_parser_explicit_instantiation (parser);
6595 /* If the next token is `export', then we have a template
6597 else if (token1.keyword == RID_EXPORT)
6598 cp_parser_template_declaration (parser, /*member_p=*/false);
6599 /* If the next token is `extern', 'static' or 'inline' and the one
6600 after that is `template', we have a GNU extended explicit
6601 instantiation directive. */
6602 else if (cp_parser_allow_gnu_extensions_p (parser)
6603 && (token1.keyword == RID_EXTERN
6604 || token1.keyword == RID_STATIC
6605 || token1.keyword == RID_INLINE)
6606 && token2.keyword == RID_TEMPLATE)
6607 cp_parser_explicit_instantiation (parser);
6608 /* If the next token is `namespace', check for a named or unnamed
6609 namespace definition. */
6610 else if (token1.keyword == RID_NAMESPACE
6611 && (/* A named namespace definition. */
6612 (token2.type == CPP_NAME
6613 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
6615 /* An unnamed namespace definition. */
6616 || token2.type == CPP_OPEN_BRACE))
6617 cp_parser_namespace_definition (parser);
6618 /* We must have either a block declaration or a function
6621 /* Try to parse a block-declaration, or a function-definition. */
6622 cp_parser_block_declaration (parser, /*statement_p=*/false);
6625 /* Parse a block-declaration.
6630 namespace-alias-definition
6637 __extension__ block-declaration
6640 If STATEMENT_P is TRUE, then this block-declaration is ocurring as
6641 part of a declaration-statement. */
6644 cp_parser_block_declaration (cp_parser *parser,
6650 /* Check for the `__extension__' keyword. */
6651 if (cp_parser_extension_opt (parser, &saved_pedantic))
6653 /* Parse the qualified declaration. */
6654 cp_parser_block_declaration (parser, statement_p);
6655 /* Restore the PEDANTIC flag. */
6656 pedantic = saved_pedantic;
6661 /* Peek at the next token to figure out which kind of declaration is
6663 token1 = cp_lexer_peek_token (parser->lexer);
6665 /* If the next keyword is `asm', we have an asm-definition. */
6666 if (token1->keyword == RID_ASM)
6669 cp_parser_commit_to_tentative_parse (parser);
6670 cp_parser_asm_definition (parser);
6672 /* If the next keyword is `namespace', we have a
6673 namespace-alias-definition. */
6674 else if (token1->keyword == RID_NAMESPACE)
6675 cp_parser_namespace_alias_definition (parser);
6676 /* If the next keyword is `using', we have either a
6677 using-declaration or a using-directive. */
6678 else if (token1->keyword == RID_USING)
6683 cp_parser_commit_to_tentative_parse (parser);
6684 /* If the token after `using' is `namespace', then we have a
6686 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
6687 if (token2->keyword == RID_NAMESPACE)
6688 cp_parser_using_directive (parser);
6689 /* Otherwise, it's a using-declaration. */
6691 cp_parser_using_declaration (parser);
6693 /* If the next keyword is `__label__' we have a label declaration. */
6694 else if (token1->keyword == RID_LABEL)
6697 cp_parser_commit_to_tentative_parse (parser);
6698 cp_parser_label_declaration (parser);
6700 /* Anything else must be a simple-declaration. */
6702 cp_parser_simple_declaration (parser, !statement_p);
6705 /* Parse a simple-declaration.
6708 decl-specifier-seq [opt] init-declarator-list [opt] ;
6710 init-declarator-list:
6712 init-declarator-list , init-declarator
6714 If FUNCTION_DEFINTION_ALLOWED_P is TRUE, then we also recognize a
6715 function-definition as a simple-declaration. */
6718 cp_parser_simple_declaration (parser, function_definition_allowed_p)
6720 bool function_definition_allowed_p;
6722 tree decl_specifiers;
6725 bool declares_class_or_enum;
6726 bool saw_declarator;
6728 /* Defer access checks until we know what is being declared; the
6729 checks for names appearing in the decl-specifier-seq should be
6730 done as if we were in the scope of the thing being declared. */
6731 cp_parser_start_deferring_access_checks (parser);
6732 /* Parse the decl-specifier-seq. We have to keep track of whether
6733 or not the decl-specifier-seq declares a named class or
6734 enumeration type, since that is the only case in which the
6735 init-declarator-list is allowed to be empty.
6739 In a simple-declaration, the optional init-declarator-list can be
6740 omitted only when declaring a class or enumeration, that is when
6741 the decl-specifier-seq contains either a class-specifier, an
6742 elaborated-type-specifier, or an enum-specifier. */
6744 = cp_parser_decl_specifier_seq (parser,
6745 CP_PARSER_FLAGS_OPTIONAL,
6747 &declares_class_or_enum);
6748 /* We no longer need to defer access checks. */
6749 access_checks = cp_parser_stop_deferring_access_checks (parser);
6751 /* Prevent access checks from being reclaimed by GC. */
6752 parser->access_checks_lists = tree_cons (NULL_TREE, access_checks,
6753 parser->access_checks_lists);
6755 /* Keep going until we hit the `;' at the end of the simple
6757 saw_declarator = false;
6758 while (cp_lexer_next_token_is_not (parser->lexer,
6762 bool function_definition_p;
6764 saw_declarator = true;
6765 /* Parse the init-declarator. */
6766 cp_parser_init_declarator (parser, decl_specifiers, attributes,
6768 function_definition_allowed_p,
6770 &function_definition_p);
6771 /* Handle function definitions specially. */
6772 if (function_definition_p)
6774 /* If the next token is a `,', then we are probably
6775 processing something like:
6779 which is erroneous. */
6780 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
6781 error ("mixing declarations and function-definitions is forbidden");
6782 /* Otherwise, we're done with the list of declarators. */
6785 /* Discard access checks no longer in use. */
6786 parser->access_checks_lists
6787 = TREE_CHAIN (parser->access_checks_lists);
6791 /* The next token should be either a `,' or a `;'. */
6792 token = cp_lexer_peek_token (parser->lexer);
6793 /* If it's a `,', there are more declarators to come. */
6794 if (token->type == CPP_COMMA)
6795 cp_lexer_consume_token (parser->lexer);
6796 /* If it's a `;', we are done. */
6797 else if (token->type == CPP_SEMICOLON)
6799 /* Anything else is an error. */
6802 cp_parser_error (parser, "expected `,' or `;'");
6803 /* Skip tokens until we reach the end of the statement. */
6804 cp_parser_skip_to_end_of_statement (parser);
6805 /* Discard access checks no longer in use. */
6806 parser->access_checks_lists
6807 = TREE_CHAIN (parser->access_checks_lists);
6810 /* After the first time around, a function-definition is not
6811 allowed -- even if it was OK at first. For example:
6816 function_definition_allowed_p = false;
6819 /* Issue an error message if no declarators are present, and the
6820 decl-specifier-seq does not itself declare a class or
6822 if (!saw_declarator)
6824 if (cp_parser_declares_only_class_p (parser))
6825 shadow_tag (decl_specifiers);
6826 /* Perform any deferred access checks. */
6827 cp_parser_perform_deferred_access_checks (access_checks);
6830 /* Consume the `;'. */
6831 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6833 /* Mark all the classes that appeared in the decl-specifier-seq as
6834 having received a `;'. */
6835 note_list_got_semicolon (decl_specifiers);
6837 /* Discard access checks no longer in use. */
6838 parser->access_checks_lists = TREE_CHAIN (parser->access_checks_lists);
6841 /* Parse a decl-specifier-seq.
6844 decl-specifier-seq [opt] decl-specifier
6847 storage-class-specifier
6856 decl-specifier-seq [opt] attributes
6858 Returns a TREE_LIST, giving the decl-specifiers in the order they
6859 appear in the source code. The TREE_VALUE of each node is the
6860 decl-specifier. For a keyword (such as `auto' or `friend'), the
6861 TREE_VALUE is simply the correspoding TREE_IDENTIFIER. For the
6862 representation of a type-specifier, see cp_parser_type_specifier.
6864 If there are attributes, they will be stored in *ATTRIBUTES,
6865 represented as described above cp_parser_attributes.
6867 If FRIEND_IS_NOT_CLASS_P is non-NULL, and the `friend' specifier
6868 appears, and the entity that will be a friend is not going to be a
6869 class, then *FRIEND_IS_NOT_CLASS_P will be set to TRUE. Note that
6870 even if *FRIEND_IS_NOT_CLASS_P is FALSE, the entity to which
6871 friendship is granted might not be a class. */
6874 cp_parser_decl_specifier_seq (parser, flags, attributes,
6875 declares_class_or_enum)
6877 cp_parser_flags flags;
6879 bool *declares_class_or_enum;
6881 tree decl_specs = NULL_TREE;
6882 bool friend_p = false;
6884 /* Assume no class or enumeration type is declared. */
6885 *declares_class_or_enum = false;
6887 /* Assume there are no attributes. */
6888 *attributes = NULL_TREE;
6890 /* Keep reading specifiers until there are no more to read. */
6893 tree decl_spec = NULL_TREE;
6897 /* Peek at the next token. */
6898 token = cp_lexer_peek_token (parser->lexer);
6899 /* Handle attributes. */
6900 if (token->keyword == RID_ATTRIBUTE)
6902 /* Parse the attributes. */
6903 decl_spec = cp_parser_attributes_opt (parser);
6904 /* Add them to the list. */
6905 *attributes = chainon (*attributes, decl_spec);
6908 /* If the next token is an appropriate keyword, we can simply
6909 add it to the list. */
6910 switch (token->keyword)
6916 /* The representation of the specifier is simply the
6917 appropriate TREE_IDENTIFIER node. */
6918 decl_spec = token->value;
6919 /* Consume the token. */
6920 cp_lexer_consume_token (parser->lexer);
6923 /* function-specifier:
6930 decl_spec = cp_parser_function_specifier_opt (parser);
6936 /* The representation of the specifier is simply the
6937 appropriate TREE_IDENTIFIER node. */
6938 decl_spec = token->value;
6939 /* Consume the token. */
6940 cp_lexer_consume_token (parser->lexer);
6943 /* storage-class-specifier:
6958 decl_spec = cp_parser_storage_class_specifier_opt (parser);
6965 /* Constructors are a special case. The `S' in `S()' is not a
6966 decl-specifier; it is the beginning of the declarator. */
6967 constructor_p = (!decl_spec
6968 && cp_parser_constructor_declarator_p (parser,
6971 /* If we don't have a DECL_SPEC yet, then we must be looking at
6972 a type-specifier. */
6973 if (!decl_spec && !constructor_p)
6975 bool decl_spec_declares_class_or_enum;
6976 bool is_cv_qualifier;
6979 = cp_parser_type_specifier (parser, flags,
6981 /*is_declaration=*/true,
6982 &decl_spec_declares_class_or_enum,
6985 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
6987 /* If this type-specifier referenced a user-defined type
6988 (a typedef, class-name, etc.), then we can't allow any
6989 more such type-specifiers henceforth.
6993 The longest sequence of decl-specifiers that could
6994 possibly be a type name is taken as the
6995 decl-specifier-seq of a declaration. The sequence shall
6996 be self-consistent as described below.
7000 As a general rule, at most one type-specifier is allowed
7001 in the complete decl-specifier-seq of a declaration. The
7002 only exceptions are the following:
7004 -- const or volatile can be combined with any other
7007 -- signed or unsigned can be combined with char, long,
7015 void g (const int Pc);
7017 Here, Pc is *not* part of the decl-specifier seq; it's
7018 the declarator. Therefore, once we see a type-specifier
7019 (other than a cv-qualifier), we forbid any additional
7020 user-defined types. We *do* still allow things like `int
7021 int' to be considered a decl-specifier-seq, and issue the
7022 error message later. */
7023 if (decl_spec && !is_cv_qualifier)
7024 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
7027 /* If we still do not have a DECL_SPEC, then there are no more
7031 /* Issue an error message, unless the entire construct was
7033 if (!(flags & CP_PARSER_FLAGS_OPTIONAL))
7035 cp_parser_error (parser, "expected decl specifier");
7036 return error_mark_node;
7042 /* Add the DECL_SPEC to the list of specifiers. */
7043 decl_specs = tree_cons (NULL_TREE, decl_spec, decl_specs);
7045 /* After we see one decl-specifier, further decl-specifiers are
7047 flags |= CP_PARSER_FLAGS_OPTIONAL;
7050 /* We have built up the DECL_SPECS in reverse order. Return them in
7051 the correct order. */
7052 return nreverse (decl_specs);
7055 /* Parse an (optional) storage-class-specifier.
7057 storage-class-specifier:
7066 storage-class-specifier:
7069 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
7072 cp_parser_storage_class_specifier_opt (parser)
7075 switch (cp_lexer_peek_token (parser->lexer)->keyword)
7083 /* Consume the token. */
7084 return cp_lexer_consume_token (parser->lexer)->value;
7091 /* Parse an (optional) function-specifier.
7098 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
7101 cp_parser_function_specifier_opt (parser)
7104 switch (cp_lexer_peek_token (parser->lexer)->keyword)
7109 /* Consume the token. */
7110 return cp_lexer_consume_token (parser->lexer)->value;
7117 /* Parse a linkage-specification.
7119 linkage-specification:
7120 extern string-literal { declaration-seq [opt] }
7121 extern string-literal declaration */
7124 cp_parser_linkage_specification (parser)
7130 /* Look for the `extern' keyword. */
7131 cp_parser_require_keyword (parser, RID_EXTERN, "`extern'");
7133 /* Peek at the next token. */
7134 token = cp_lexer_peek_token (parser->lexer);
7135 /* If it's not a string-literal, then there's a problem. */
7136 if (!cp_parser_is_string_literal (token))
7138 cp_parser_error (parser, "expected language-name");
7141 /* Consume the token. */
7142 cp_lexer_consume_token (parser->lexer);
7144 /* Transform the literal into an identifier. If the literal is a
7145 wide-character string, or contains embedded NULs, then we can't
7146 handle it as the user wants. */
7147 if (token->type == CPP_WSTRING
7148 || (strlen (TREE_STRING_POINTER (token->value))
7149 != (size_t) (TREE_STRING_LENGTH (token->value) - 1)))
7151 cp_parser_error (parser, "invalid linkage-specification");
7152 /* Assume C++ linkage. */
7153 linkage = get_identifier ("c++");
7155 /* If it's a simple string constant, things are easier. */
7157 linkage = get_identifier (TREE_STRING_POINTER (token->value));
7159 /* We're now using the new linkage. */
7160 push_lang_context (linkage);
7162 /* If the next token is a `{', then we're using the first
7164 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7166 /* Consume the `{' token. */
7167 cp_lexer_consume_token (parser->lexer);
7168 /* Parse the declarations. */
7169 cp_parser_declaration_seq_opt (parser);
7170 /* Look for the closing `}'. */
7171 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
7173 /* Otherwise, there's just one declaration. */
7176 bool saved_in_unbraced_linkage_specification_p;
7178 saved_in_unbraced_linkage_specification_p
7179 = parser->in_unbraced_linkage_specification_p;
7180 parser->in_unbraced_linkage_specification_p = true;
7181 have_extern_spec = true;
7182 cp_parser_declaration (parser);
7183 have_extern_spec = false;
7184 parser->in_unbraced_linkage_specification_p
7185 = saved_in_unbraced_linkage_specification_p;
7188 /* We're done with the linkage-specification. */
7189 pop_lang_context ();
7192 /* Special member functions [gram.special] */
7194 /* Parse a conversion-function-id.
7196 conversion-function-id:
7197 operator conversion-type-id
7199 Returns an IDENTIFIER_NODE representing the operator. */
7202 cp_parser_conversion_function_id (parser)
7207 tree saved_qualifying_scope;
7208 tree saved_object_scope;
7210 /* Look for the `operator' token. */
7211 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
7212 return error_mark_node;
7213 /* When we parse the conversion-type-id, the current scope will be
7214 reset. However, we need that information in able to look up the
7215 conversion function later, so we save it here. */
7216 saved_scope = parser->scope;
7217 saved_qualifying_scope = parser->qualifying_scope;
7218 saved_object_scope = parser->object_scope;
7219 /* We must enter the scope of the class so that the names of
7220 entities declared within the class are available in the
7221 conversion-type-id. For example, consider:
7228 S::operator I() { ... }
7230 In order to see that `I' is a type-name in the definition, we
7231 must be in the scope of `S'. */
7233 push_scope (saved_scope);
7234 /* Parse the conversion-type-id. */
7235 type = cp_parser_conversion_type_id (parser);
7236 /* Leave the scope of the class, if any. */
7238 pop_scope (saved_scope);
7239 /* Restore the saved scope. */
7240 parser->scope = saved_scope;
7241 parser->qualifying_scope = saved_qualifying_scope;
7242 parser->object_scope = saved_object_scope;
7243 /* If the TYPE is invalid, indicate failure. */
7244 if (type == error_mark_node)
7245 return error_mark_node;
7246 return mangle_conv_op_name_for_type (type);
7249 /* Parse a conversion-type-id:
7252 type-specifier-seq conversion-declarator [opt]
7254 Returns the TYPE specified. */
7257 cp_parser_conversion_type_id (parser)
7261 tree type_specifiers;
7264 /* Parse the attributes. */
7265 attributes = cp_parser_attributes_opt (parser);
7266 /* Parse the type-specifiers. */
7267 type_specifiers = cp_parser_type_specifier_seq (parser);
7268 /* If that didn't work, stop. */
7269 if (type_specifiers == error_mark_node)
7270 return error_mark_node;
7271 /* Parse the conversion-declarator. */
7272 declarator = cp_parser_conversion_declarator_opt (parser);
7274 return grokdeclarator (declarator, type_specifiers, TYPENAME,
7275 /*initialized=*/0, &attributes);
7278 /* Parse an (optional) conversion-declarator.
7280 conversion-declarator:
7281 ptr-operator conversion-declarator [opt]
7283 Returns a representation of the declarator. See
7284 cp_parser_declarator for details. */
7287 cp_parser_conversion_declarator_opt (parser)
7290 enum tree_code code;
7292 tree cv_qualifier_seq;
7294 /* We don't know if there's a ptr-operator next, or not. */
7295 cp_parser_parse_tentatively (parser);
7296 /* Try the ptr-operator. */
7297 code = cp_parser_ptr_operator (parser, &class_type,
7299 /* If it worked, look for more conversion-declarators. */
7300 if (cp_parser_parse_definitely (parser))
7304 /* Parse another optional declarator. */
7305 declarator = cp_parser_conversion_declarator_opt (parser);
7307 /* Create the representation of the declarator. */
7308 if (code == INDIRECT_REF)
7309 declarator = make_pointer_declarator (cv_qualifier_seq,
7312 declarator = make_reference_declarator (cv_qualifier_seq,
7315 /* Handle the pointer-to-member case. */
7317 declarator = build_nt (SCOPE_REF, class_type, declarator);
7325 /* Parse an (optional) ctor-initializer.
7328 : mem-initializer-list
7330 Returns TRUE iff the ctor-initializer was actually present. */
7333 cp_parser_ctor_initializer_opt (parser)
7336 /* If the next token is not a `:', then there is no
7337 ctor-initializer. */
7338 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
7340 /* Do default initialization of any bases and members. */
7341 if (DECL_CONSTRUCTOR_P (current_function_decl))
7342 finish_mem_initializers (NULL_TREE);
7347 /* Consume the `:' token. */
7348 cp_lexer_consume_token (parser->lexer);
7349 /* And the mem-initializer-list. */
7350 cp_parser_mem_initializer_list (parser);
7355 /* Parse a mem-initializer-list.
7357 mem-initializer-list:
7359 mem-initializer , mem-initializer-list */
7362 cp_parser_mem_initializer_list (parser)
7365 tree mem_initializer_list = NULL_TREE;
7367 /* Let the semantic analysis code know that we are starting the
7368 mem-initializer-list. */
7369 begin_mem_initializers ();
7371 /* Loop through the list. */
7374 tree mem_initializer;
7376 /* Parse the mem-initializer. */
7377 mem_initializer = cp_parser_mem_initializer (parser);
7378 /* Add it to the list, unless it was erroneous. */
7379 if (mem_initializer)
7381 TREE_CHAIN (mem_initializer) = mem_initializer_list;
7382 mem_initializer_list = mem_initializer;
7384 /* If the next token is not a `,', we're done. */
7385 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7387 /* Consume the `,' token. */
7388 cp_lexer_consume_token (parser->lexer);
7391 /* Perform semantic analysis. */
7392 finish_mem_initializers (mem_initializer_list);
7395 /* Parse a mem-initializer.
7398 mem-initializer-id ( expression-list [opt] )
7403 ( expresion-list [opt] )
7405 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
7406 class) or FIELD_DECL (for a non-static data member) to initialize;
7407 the TREE_VALUE is the expression-list. */
7410 cp_parser_mem_initializer (parser)
7413 tree mem_initializer_id;
7414 tree expression_list;
7416 /* Find out what is being initialized. */
7417 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7419 pedwarn ("anachronistic old-style base class initializer");
7420 mem_initializer_id = NULL_TREE;
7423 mem_initializer_id = cp_parser_mem_initializer_id (parser);
7424 /* Look for the opening `('. */
7425 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
7426 /* Parse the expression-list. */
7427 if (cp_lexer_next_token_is_not (parser->lexer,
7429 expression_list = cp_parser_expression_list (parser);
7431 expression_list = void_type_node;
7432 /* Look for the closing `)'. */
7433 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
7435 return expand_member_init (mem_initializer_id,
7439 /* Parse a mem-initializer-id.
7442 :: [opt] nested-name-specifier [opt] class-name
7445 Returns a TYPE indicating the class to be initializer for the first
7446 production. Returns an IDENTIFIER_NODE indicating the data member
7447 to be initialized for the second production. */
7450 cp_parser_mem_initializer_id (parser)
7453 bool global_scope_p;
7454 bool nested_name_specifier_p;
7457 /* Look for the optional `::' operator. */
7459 = (cp_parser_global_scope_opt (parser,
7460 /*current_scope_valid_p=*/false)
7462 /* Look for the optional nested-name-specifier. The simplest way to
7467 The keyword `typename' is not permitted in a base-specifier or
7468 mem-initializer; in these contexts a qualified name that
7469 depends on a template-parameter is implicitly assumed to be a
7472 is to assume that we have seen the `typename' keyword at this
7474 nested_name_specifier_p
7475 = (cp_parser_nested_name_specifier_opt (parser,
7476 /*typename_keyword_p=*/true,
7477 /*check_dependency_p=*/true,
7480 /* If there is a `::' operator or a nested-name-specifier, then we
7481 are definitely looking for a class-name. */
7482 if (global_scope_p || nested_name_specifier_p)
7483 return cp_parser_class_name (parser,
7484 /*typename_keyword_p=*/true,
7485 /*template_keyword_p=*/false,
7487 /*check_access_p=*/true,
7488 /*check_dependency_p=*/true,
7489 /*class_head_p=*/false);
7490 /* Otherwise, we could also be looking for an ordinary identifier. */
7491 cp_parser_parse_tentatively (parser);
7492 /* Try a class-name. */
7493 id = cp_parser_class_name (parser,
7494 /*typename_keyword_p=*/true,
7495 /*template_keyword_p=*/false,
7497 /*check_access_p=*/true,
7498 /*check_dependency_p=*/true,
7499 /*class_head_p=*/false);
7500 /* If we found one, we're done. */
7501 if (cp_parser_parse_definitely (parser))
7503 /* Otherwise, look for an ordinary identifier. */
7504 return cp_parser_identifier (parser);
7507 /* Overloading [gram.over] */
7509 /* Parse an operator-function-id.
7511 operator-function-id:
7514 Returns an IDENTIFIER_NODE for the operator which is a
7515 human-readable spelling of the identifier, e.g., `operator +'. */
7518 cp_parser_operator_function_id (parser)
7521 /* Look for the `operator' keyword. */
7522 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
7523 return error_mark_node;
7524 /* And then the name of the operator itself. */
7525 return cp_parser_operator (parser);
7528 /* Parse an operator.
7531 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
7532 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
7533 || ++ -- , ->* -> () []
7540 Returns an IDENTIFIER_NODE for the operator which is a
7541 human-readable spelling of the identifier, e.g., `operator +'. */
7544 cp_parser_operator (parser)
7547 tree id = NULL_TREE;
7550 /* Peek at the next token. */
7551 token = cp_lexer_peek_token (parser->lexer);
7552 /* Figure out which operator we have. */
7553 switch (token->type)
7559 /* The keyword should be either `new' or `delete'. */
7560 if (token->keyword == RID_NEW)
7562 else if (token->keyword == RID_DELETE)
7567 /* Consume the `new' or `delete' token. */
7568 cp_lexer_consume_token (parser->lexer);
7570 /* Peek at the next token. */
7571 token = cp_lexer_peek_token (parser->lexer);
7572 /* If it's a `[' token then this is the array variant of the
7574 if (token->type == CPP_OPEN_SQUARE)
7576 /* Consume the `[' token. */
7577 cp_lexer_consume_token (parser->lexer);
7578 /* Look for the `]' token. */
7579 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
7580 id = ansi_opname (op == NEW_EXPR
7581 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
7583 /* Otherwise, we have the non-array variant. */
7585 id = ansi_opname (op);
7591 id = ansi_opname (PLUS_EXPR);
7595 id = ansi_opname (MINUS_EXPR);
7599 id = ansi_opname (MULT_EXPR);
7603 id = ansi_opname (TRUNC_DIV_EXPR);
7607 id = ansi_opname (TRUNC_MOD_EXPR);
7611 id = ansi_opname (BIT_XOR_EXPR);
7615 id = ansi_opname (BIT_AND_EXPR);
7619 id = ansi_opname (BIT_IOR_EXPR);
7623 id = ansi_opname (BIT_NOT_EXPR);
7627 id = ansi_opname (TRUTH_NOT_EXPR);
7631 id = ansi_assopname (NOP_EXPR);
7635 id = ansi_opname (LT_EXPR);
7639 id = ansi_opname (GT_EXPR);
7643 id = ansi_assopname (PLUS_EXPR);
7647 id = ansi_assopname (MINUS_EXPR);
7651 id = ansi_assopname (MULT_EXPR);
7655 id = ansi_assopname (TRUNC_DIV_EXPR);
7659 id = ansi_assopname (TRUNC_MOD_EXPR);
7663 id = ansi_assopname (BIT_XOR_EXPR);
7667 id = ansi_assopname (BIT_AND_EXPR);
7671 id = ansi_assopname (BIT_IOR_EXPR);
7675 id = ansi_opname (LSHIFT_EXPR);
7679 id = ansi_opname (RSHIFT_EXPR);
7683 id = ansi_assopname (LSHIFT_EXPR);
7687 id = ansi_assopname (RSHIFT_EXPR);
7691 id = ansi_opname (EQ_EXPR);
7695 id = ansi_opname (NE_EXPR);
7699 id = ansi_opname (LE_EXPR);
7702 case CPP_GREATER_EQ:
7703 id = ansi_opname (GE_EXPR);
7707 id = ansi_opname (TRUTH_ANDIF_EXPR);
7711 id = ansi_opname (TRUTH_ORIF_EXPR);
7715 id = ansi_opname (POSTINCREMENT_EXPR);
7718 case CPP_MINUS_MINUS:
7719 id = ansi_opname (PREDECREMENT_EXPR);
7723 id = ansi_opname (COMPOUND_EXPR);
7726 case CPP_DEREF_STAR:
7727 id = ansi_opname (MEMBER_REF);
7731 id = ansi_opname (COMPONENT_REF);
7734 case CPP_OPEN_PAREN:
7735 /* Consume the `('. */
7736 cp_lexer_consume_token (parser->lexer);
7737 /* Look for the matching `)'. */
7738 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
7739 return ansi_opname (CALL_EXPR);
7741 case CPP_OPEN_SQUARE:
7742 /* Consume the `['. */
7743 cp_lexer_consume_token (parser->lexer);
7744 /* Look for the matching `]'. */
7745 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
7746 return ansi_opname (ARRAY_REF);
7750 id = ansi_opname (MIN_EXPR);
7754 id = ansi_opname (MAX_EXPR);
7758 id = ansi_assopname (MIN_EXPR);
7762 id = ansi_assopname (MAX_EXPR);
7766 /* Anything else is an error. */
7770 /* If we have selected an identifier, we need to consume the
7773 cp_lexer_consume_token (parser->lexer);
7774 /* Otherwise, no valid operator name was present. */
7777 cp_parser_error (parser, "expected operator");
7778 id = error_mark_node;
7784 /* Parse a template-declaration.
7786 template-declaration:
7787 export [opt] template < template-parameter-list > declaration
7789 If MEMBER_P is TRUE, this template-declaration occurs within a
7792 The grammar rule given by the standard isn't correct. What
7795 template-declaration:
7796 export [opt] template-parameter-list-seq
7797 decl-specifier-seq [opt] init-declarator [opt] ;
7798 export [opt] template-parameter-list-seq
7801 template-parameter-list-seq:
7802 template-parameter-list-seq [opt]
7803 template < template-parameter-list > */
7806 cp_parser_template_declaration (parser, member_p)
7810 /* Check for `export'. */
7811 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
7813 /* Consume the `export' token. */
7814 cp_lexer_consume_token (parser->lexer);
7815 /* Warn that we do not support `export'. */
7816 warning ("keyword `export' not implemented, and will be ignored");
7819 cp_parser_template_declaration_after_export (parser, member_p);
7822 /* Parse a template-parameter-list.
7824 template-parameter-list:
7826 template-parameter-list , template-parameter
7828 Returns a TREE_LIST. Each node represents a template parameter.
7829 The nodes are connected via their TREE_CHAINs. */
7832 cp_parser_template_parameter_list (parser)
7835 tree parameter_list = NULL_TREE;
7842 /* Parse the template-parameter. */
7843 parameter = cp_parser_template_parameter (parser);
7844 /* Add it to the list. */
7845 parameter_list = process_template_parm (parameter_list,
7848 /* Peek at the next token. */
7849 token = cp_lexer_peek_token (parser->lexer);
7850 /* If it's not a `,', we're done. */
7851 if (token->type != CPP_COMMA)
7853 /* Otherwise, consume the `,' token. */
7854 cp_lexer_consume_token (parser->lexer);
7857 return parameter_list;
7860 /* Parse a template-parameter.
7864 parameter-declaration
7866 Returns a TREE_LIST. The TREE_VALUE represents the parameter. The
7867 TREE_PURPOSE is the default value, if any. */
7870 cp_parser_template_parameter (parser)
7875 /* Peek at the next token. */
7876 token = cp_lexer_peek_token (parser->lexer);
7877 /* If it is `class' or `template', we have a type-parameter. */
7878 if (token->keyword == RID_TEMPLATE)
7879 return cp_parser_type_parameter (parser);
7880 /* If it is `class' or `typename' we do not know yet whether it is a
7881 type parameter or a non-type parameter. Consider:
7883 template <typename T, typename T::X X> ...
7887 template <class C, class D*> ...
7889 Here, the first parameter is a type parameter, and the second is
7890 a non-type parameter. We can tell by looking at the token after
7891 the identifier -- if it is a `,', `=', or `>' then we have a type
7893 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
7895 /* Peek at the token after `class' or `typename'. */
7896 token = cp_lexer_peek_nth_token (parser->lexer, 2);
7897 /* If it's an identifier, skip it. */
7898 if (token->type == CPP_NAME)
7899 token = cp_lexer_peek_nth_token (parser->lexer, 3);
7900 /* Now, see if the token looks like the end of a template
7902 if (token->type == CPP_COMMA
7903 || token->type == CPP_EQ
7904 || token->type == CPP_GREATER)
7905 return cp_parser_type_parameter (parser);
7908 /* Otherwise, it is a non-type parameter.
7912 When parsing a default template-argument for a non-type
7913 template-parameter, the first non-nested `>' is taken as the end
7914 of the template parameter-list rather than a greater-than
7917 cp_parser_parameter_declaration (parser, /*template_parm_p=*/true);
7920 /* Parse a type-parameter.
7923 class identifier [opt]
7924 class identifier [opt] = type-id
7925 typename identifier [opt]
7926 typename identifier [opt] = type-id
7927 template < template-parameter-list > class identifier [opt]
7928 template < template-parameter-list > class identifier [opt]
7931 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
7932 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
7933 the declaration of the parameter. */
7936 cp_parser_type_parameter (parser)
7942 /* Look for a keyword to tell us what kind of parameter this is. */
7943 token = cp_parser_require (parser, CPP_KEYWORD,
7944 "expected `class', `typename', or `template'");
7946 return error_mark_node;
7948 switch (token->keyword)
7954 tree default_argument;
7956 /* If the next token is an identifier, then it names the
7958 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
7959 identifier = cp_parser_identifier (parser);
7961 identifier = NULL_TREE;
7963 /* Create the parameter. */
7964 parameter = finish_template_type_parm (class_type_node, identifier);
7966 /* If the next token is an `=', we have a default argument. */
7967 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7969 /* Consume the `=' token. */
7970 cp_lexer_consume_token (parser->lexer);
7971 /* Parse the default-argumen. */
7972 default_argument = cp_parser_type_id (parser);
7975 default_argument = NULL_TREE;
7977 /* Create the combined representation of the parameter and the
7978 default argument. */
7979 parameter = build_tree_list (default_argument,
7986 tree parameter_list;
7988 tree default_argument;
7990 /* Look for the `<'. */
7991 cp_parser_require (parser, CPP_LESS, "`<'");
7992 /* Parse the template-parameter-list. */
7993 begin_template_parm_list ();
7995 = cp_parser_template_parameter_list (parser);
7996 parameter_list = end_template_parm_list (parameter_list);
7997 /* Look for the `>'. */
7998 cp_parser_require (parser, CPP_GREATER, "`>'");
7999 /* Look for the `class' keyword. */
8000 cp_parser_require_keyword (parser, RID_CLASS, "`class'");
8001 /* If the next token is an `=', then there is a
8002 default-argument. If the next token is a `>', we are at
8003 the end of the parameter-list. If the next token is a `,',
8004 then we are at the end of this parameter. */
8005 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
8006 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
8007 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8008 identifier = cp_parser_identifier (parser);
8010 identifier = NULL_TREE;
8011 /* Create the template parameter. */
8012 parameter = finish_template_template_parm (class_type_node,
8015 /* If the next token is an `=', then there is a
8016 default-argument. */
8017 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8019 /* Consume the `='. */
8020 cp_lexer_consume_token (parser->lexer);
8021 /* Parse the id-expression. */
8023 = cp_parser_id_expression (parser,
8024 /*template_keyword_p=*/false,
8025 /*check_dependency_p=*/true,
8026 /*template_p=*/NULL);
8027 /* Look up the name. */
8029 = cp_parser_lookup_name_simple (parser, default_argument);
8030 /* See if the default argument is valid. */
8032 = check_template_template_default_arg (default_argument);
8035 default_argument = NULL_TREE;
8037 /* Create the combined representation of the parameter and the
8038 default argument. */
8039 parameter = build_tree_list (default_argument,
8045 /* Anything else is an error. */
8046 cp_parser_error (parser,
8047 "expected `class', `typename', or `template'");
8048 parameter = error_mark_node;
8054 /* Parse a template-id.
8057 template-name < template-argument-list [opt] >
8059 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
8060 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
8061 returned. Otherwise, if the template-name names a function, or set
8062 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
8063 names a class, returns a TYPE_DECL for the specialization.
8065 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
8066 uninstantiated templates. */
8069 cp_parser_template_id (cp_parser *parser,
8070 bool template_keyword_p,
8071 bool check_dependency_p)
8076 tree saved_qualifying_scope;
8077 tree saved_object_scope;
8079 bool saved_greater_than_is_operator_p;
8080 ptrdiff_t start_of_id;
8081 tree access_check = NULL_TREE;
8083 /* If the next token corresponds to a template-id, there is no need
8085 if (cp_lexer_next_token_is (parser->lexer, CPP_TEMPLATE_ID))
8090 /* Get the stored value. */
8091 value = cp_lexer_consume_token (parser->lexer)->value;
8092 /* Perform any access checks that were deferred. */
8093 for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
8094 cp_parser_defer_access_check (parser,
8095 TREE_PURPOSE (check),
8096 TREE_VALUE (check));
8097 /* Return the stored value. */
8098 return TREE_VALUE (value);
8101 /* Remember where the template-id starts. */
8102 if (cp_parser_parsing_tentatively (parser)
8103 && !cp_parser_committed_to_tentative_parse (parser))
8105 cp_token *next_token = cp_lexer_peek_token (parser->lexer);
8106 start_of_id = cp_lexer_token_difference (parser->lexer,
8107 parser->lexer->first_token,
8109 access_check = parser->context->deferred_access_checks;
8114 /* Parse the template-name. */
8115 template = cp_parser_template_name (parser, template_keyword_p,
8116 check_dependency_p);
8117 if (template == error_mark_node)
8118 return error_mark_node;
8120 /* Look for the `<' that starts the template-argument-list. */
8121 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
8122 return error_mark_node;
8126 When parsing a template-id, the first non-nested `>' is taken as
8127 the end of the template-argument-list rather than a greater-than
8129 saved_greater_than_is_operator_p
8130 = parser->greater_than_is_operator_p;
8131 parser->greater_than_is_operator_p = false;
8132 /* Parsing the argument list may modify SCOPE, so we save it
8134 saved_scope = parser->scope;
8135 saved_qualifying_scope = parser->qualifying_scope;
8136 saved_object_scope = parser->object_scope;
8137 /* Parse the template-argument-list itself. */
8138 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
8139 arguments = NULL_TREE;
8141 arguments = cp_parser_template_argument_list (parser);
8142 /* Look for the `>' that ends the template-argument-list. */
8143 cp_parser_require (parser, CPP_GREATER, "`>'");
8144 /* The `>' token might be a greater-than operator again now. */
8145 parser->greater_than_is_operator_p
8146 = saved_greater_than_is_operator_p;
8147 /* Restore the SAVED_SCOPE. */
8148 parser->scope = saved_scope;
8149 parser->qualifying_scope = saved_qualifying_scope;
8150 parser->object_scope = saved_object_scope;
8152 /* Build a representation of the specialization. */
8153 if (TREE_CODE (template) == IDENTIFIER_NODE)
8154 template_id = build_min_nt (TEMPLATE_ID_EXPR, template, arguments);
8155 else if (DECL_CLASS_TEMPLATE_P (template)
8156 || DECL_TEMPLATE_TEMPLATE_PARM_P (template))
8158 = finish_template_type (template, arguments,
8159 cp_lexer_next_token_is (parser->lexer,
8163 /* If it's not a class-template or a template-template, it should be
8164 a function-template. */
8165 my_friendly_assert ((DECL_FUNCTION_TEMPLATE_P (template)
8166 || TREE_CODE (template) == OVERLOAD
8167 || BASELINK_P (template)),
8170 template_id = lookup_template_function (template, arguments);
8173 /* If parsing tentatively, replace the sequence of tokens that makes
8174 up the template-id with a CPP_TEMPLATE_ID token. That way,
8175 should we re-parse the token stream, we will not have to repeat
8176 the effort required to do the parse, nor will we issue duplicate
8177 error messages about problems during instantiation of the
8179 if (start_of_id >= 0)
8184 /* Find the token that corresponds to the start of the
8186 token = cp_lexer_advance_token (parser->lexer,
8187 parser->lexer->first_token,
8190 /* Remember the access checks associated with this
8191 nested-name-specifier. */
8192 c = parser->context->deferred_access_checks;
8193 if (c == access_check)
8194 access_check = NULL_TREE;
8197 while (TREE_CHAIN (c) != access_check)
8199 access_check = parser->context->deferred_access_checks;
8200 parser->context->deferred_access_checks = TREE_CHAIN (c);
8201 TREE_CHAIN (c) = NULL_TREE;
8204 /* Reset the contents of the START_OF_ID token. */
8205 token->type = CPP_TEMPLATE_ID;
8206 token->value = build_tree_list (access_check, template_id);
8207 token->keyword = RID_MAX;
8208 /* Purge all subsequent tokens. */
8209 cp_lexer_purge_tokens_after (parser->lexer, token);
8215 /* Parse a template-name.
8220 The standard should actually say:
8224 operator-function-id
8225 conversion-function-id
8227 A defect report has been filed about this issue.
8229 If TEMPLATE_KEYWORD_P is true, then we have just seen the
8230 `template' keyword, in a construction like:
8234 In that case `f' is taken to be a template-name, even though there
8235 is no way of knowing for sure.
8237 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
8238 name refers to a set of overloaded functions, at least one of which
8239 is a template, or an IDENTIFIER_NODE with the name of the template,
8240 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
8241 names are looked up inside uninstantiated templates. */
8244 cp_parser_template_name (parser, template_keyword_p, check_dependency_p)
8246 bool template_keyword_p;
8247 bool check_dependency_p;
8253 /* If the next token is `operator', then we have either an
8254 operator-function-id or a conversion-function-id. */
8255 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
8257 /* We don't know whether we're looking at an
8258 operator-function-id or a conversion-function-id. */
8259 cp_parser_parse_tentatively (parser);
8260 /* Try an operator-function-id. */
8261 identifier = cp_parser_operator_function_id (parser);
8262 /* If that didn't work, try a conversion-function-id. */
8263 if (!cp_parser_parse_definitely (parser))
8264 identifier = cp_parser_conversion_function_id (parser);
8266 /* Look for the identifier. */
8268 identifier = cp_parser_identifier (parser);
8270 /* If we didn't find an identifier, we don't have a template-id. */
8271 if (identifier == error_mark_node)
8272 return error_mark_node;
8274 /* If the name immediately followed the `template' keyword, then it
8275 is a template-name. However, if the next token is not `<', then
8276 we do not treat it as a template-name, since it is not being used
8277 as part of a template-id. This enables us to handle constructs
8280 template <typename T> struct S { S(); };
8281 template <typename T> S<T>::S();
8283 correctly. We would treat `S' as a template -- if it were `S<T>'
8284 -- but we do not if there is no `<'. */
8285 if (template_keyword_p && processing_template_decl
8286 && cp_lexer_next_token_is (parser->lexer, CPP_LESS))
8289 /* Look up the name. */
8290 decl = cp_parser_lookup_name (parser, identifier,
8291 /*check_access=*/true,
8293 /*is_namespace=*/false,
8294 check_dependency_p);
8295 decl = maybe_get_template_decl_from_type_decl (decl);
8297 /* If DECL is a template, then the name was a template-name. */
8298 if (TREE_CODE (decl) == TEMPLATE_DECL)
8302 /* The standard does not explicitly indicate whether a name that
8303 names a set of overloaded declarations, some of which are
8304 templates, is a template-name. However, such a name should
8305 be a template-name; otherwise, there is no way to form a
8306 template-id for the overloaded templates. */
8307 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
8308 if (TREE_CODE (fns) == OVERLOAD)
8312 for (fn = fns; fn; fn = OVL_NEXT (fn))
8313 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
8318 /* Otherwise, the name does not name a template. */
8319 cp_parser_error (parser, "expected template-name");
8320 return error_mark_node;
8324 /* If DECL is dependent, and refers to a function, then just return
8325 its name; we will look it up again during template instantiation. */
8326 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
8328 tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
8329 if (TYPE_P (scope) && cp_parser_dependent_type_p (scope))
8336 /* Parse a template-argument-list.
8338 template-argument-list:
8340 template-argument-list , template-argument
8342 Returns a TREE_LIST representing the arguments, in the order they
8343 appeared. The TREE_VALUE of each node is a representation of the
8347 cp_parser_template_argument_list (parser)
8350 tree arguments = NULL_TREE;
8356 /* Parse the template-argument. */
8357 argument = cp_parser_template_argument (parser);
8358 /* Add it to the list. */
8359 arguments = tree_cons (NULL_TREE, argument, arguments);
8360 /* If it is not a `,', then there are no more arguments. */
8361 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8363 /* Otherwise, consume the ','. */
8364 cp_lexer_consume_token (parser->lexer);
8367 /* We built up the arguments in reverse order. */
8368 return nreverse (arguments);
8371 /* Parse a template-argument.
8374 assignment-expression
8378 The representation is that of an assignment-expression, type-id, or
8379 id-expression -- except that the qualified id-expression is
8380 evaluated, so that the value returned is either a DECL or an
8384 cp_parser_template_argument (parser)
8390 /* There's really no way to know what we're looking at, so we just
8391 try each alternative in order.
8395 In a template-argument, an ambiguity between a type-id and an
8396 expression is resolved to a type-id, regardless of the form of
8397 the corresponding template-parameter.
8399 Therefore, we try a type-id first. */
8400 cp_parser_parse_tentatively (parser);
8401 argument = cp_parser_type_id (parser);
8402 /* If the next token isn't a `,' or a `>', then this argument wasn't
8404 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
8405 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER))
8406 cp_parser_error (parser, "expected template-argument");
8407 /* If that worked, we're done. */
8408 if (cp_parser_parse_definitely (parser))
8410 /* We're still not sure what the argument will be. */
8411 cp_parser_parse_tentatively (parser);
8412 /* Try a template. */
8413 argument = cp_parser_id_expression (parser,
8414 /*template_keyword_p=*/false,
8415 /*check_dependency_p=*/true,
8417 /* If the next token isn't a `,' or a `>', then this argument wasn't
8419 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
8420 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER))
8421 cp_parser_error (parser, "expected template-argument");
8422 if (!cp_parser_error_occurred (parser))
8424 /* Figure out what is being referred to. */
8425 argument = cp_parser_lookup_name_simple (parser, argument);
8427 argument = make_unbound_class_template (TREE_OPERAND (argument, 0),
8428 TREE_OPERAND (argument, 1),
8429 tf_error | tf_parsing);
8430 else if (TREE_CODE (argument) != TEMPLATE_DECL)
8431 cp_parser_error (parser, "expected template-name");
8433 if (cp_parser_parse_definitely (parser))
8435 /* It must be an assignment-expression. */
8436 return cp_parser_assignment_expression (parser);
8439 /* Parse an explicit-instantiation.
8441 explicit-instantiation:
8442 template declaration
8444 Although the standard says `declaration', what it really means is:
8446 explicit-instantiation:
8447 template decl-specifier-seq [opt] declarator [opt] ;
8449 Things like `template int S<int>::i = 5, int S<double>::j;' are not
8450 supposed to be allowed. A defect report has been filed about this
8455 explicit-instantiation:
8456 storage-class-specifier template
8457 decl-specifier-seq [opt] declarator [opt] ;
8458 function-specifier template
8459 decl-specifier-seq [opt] declarator [opt] ; */
8462 cp_parser_explicit_instantiation (parser)
8465 bool declares_class_or_enum;
8466 tree decl_specifiers;
8468 tree extension_specifier = NULL_TREE;
8470 /* Look for an (optional) storage-class-specifier or
8471 function-specifier. */
8472 if (cp_parser_allow_gnu_extensions_p (parser))
8475 = cp_parser_storage_class_specifier_opt (parser);
8476 if (!extension_specifier)
8477 extension_specifier = cp_parser_function_specifier_opt (parser);
8480 /* Look for the `template' keyword. */
8481 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
8482 /* Let the front end know that we are processing an explicit
8484 begin_explicit_instantiation ();
8485 /* [temp.explicit] says that we are supposed to ignore access
8486 control while processing explicit instantiation directives. */
8487 scope_chain->check_access = 0;
8488 /* Parse a decl-specifier-seq. */
8490 = cp_parser_decl_specifier_seq (parser,
8491 CP_PARSER_FLAGS_OPTIONAL,
8493 &declares_class_or_enum);
8494 /* If there was exactly one decl-specifier, and it declared a class,
8495 and there's no declarator, then we have an explicit type
8497 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
8501 type = check_tag_decl (decl_specifiers);
8503 do_type_instantiation (type, extension_specifier, /*complain=*/1);
8510 /* Parse the declarator. */
8512 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
8513 /*ctor_dtor_or_conv_p=*/NULL);
8514 decl = grokdeclarator (declarator, decl_specifiers,
8516 /* Do the explicit instantiation. */
8517 do_decl_instantiation (decl, extension_specifier);
8519 /* We're done with the instantiation. */
8520 end_explicit_instantiation ();
8521 /* Trun access control back on. */
8522 scope_chain->check_access = flag_access_control;
8524 /* Look for the trailing `;'. */
8525 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
8528 /* Parse an explicit-specialization.
8530 explicit-specialization:
8531 template < > declaration
8533 Although the standard says `declaration', what it really means is:
8535 explicit-specialization:
8536 template <> decl-specifier [opt] init-declarator [opt] ;
8537 template <> function-definition
8538 template <> explicit-specialization
8539 template <> template-declaration */
8542 cp_parser_explicit_specialization (parser)
8545 /* Look for the `template' keyword. */
8546 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
8547 /* Look for the `<'. */
8548 cp_parser_require (parser, CPP_LESS, "`<'");
8549 /* Look for the `>'. */
8550 cp_parser_require (parser, CPP_GREATER, "`>'");
8551 /* We have processed another parameter list. */
8552 ++parser->num_template_parameter_lists;
8553 /* Let the front end know that we are beginning a specialization. */
8554 begin_specialization ();
8556 /* If the next keyword is `template', we need to figure out whether
8557 or not we're looking a template-declaration. */
8558 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
8560 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
8561 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
8562 cp_parser_template_declaration_after_export (parser,
8563 /*member_p=*/false);
8565 cp_parser_explicit_specialization (parser);
8568 /* Parse the dependent declaration. */
8569 cp_parser_single_declaration (parser,
8573 /* We're done with the specialization. */
8574 end_specialization ();
8575 /* We're done with this parameter list. */
8576 --parser->num_template_parameter_lists;
8579 /* Parse a type-specifier.
8582 simple-type-specifier
8585 elaborated-type-specifier
8593 Returns a representation of the type-specifier. If the
8594 type-specifier is a keyword (like `int' or `const', or
8595 `__complex__') then the correspoding IDENTIFIER_NODE is returned.
8596 For a class-specifier, enum-specifier, or elaborated-type-specifier
8597 a TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
8599 If IS_FRIEND is TRUE then this type-specifier is being declared a
8600 `friend'. If IS_DECLARATION is TRUE, then this type-specifier is
8601 appearing in a decl-specifier-seq.
8603 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
8604 class-specifier, enum-specifier, or elaborated-type-specifier, then
8605 *DECLARES_CLASS_OR_ENUM is set to TRUE. Otherwise, it is set to
8608 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
8609 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
8613 cp_parser_type_specifier (parser,
8617 declares_class_or_enum,
8620 cp_parser_flags flags;
8622 bool is_declaration;
8623 bool *declares_class_or_enum;
8624 bool *is_cv_qualifier;
8626 tree type_spec = NULL_TREE;
8630 /* Assume this type-specifier does not declare a new type. */
8631 if (declares_class_or_enum)
8632 *declares_class_or_enum = false;
8633 /* And that it does not specify a cv-qualifier. */
8634 if (is_cv_qualifier)
8635 *is_cv_qualifier = false;
8636 /* Peek at the next token. */
8637 token = cp_lexer_peek_token (parser->lexer);
8639 /* If we're looking at a keyword, we can use that to guide the
8640 production we choose. */
8641 keyword = token->keyword;
8644 /* Any of these indicate either a class-specifier, or an
8645 elaborated-type-specifier. */
8650 /* Parse tentatively so that we can back up if we don't find a
8651 class-specifier or enum-specifier. */
8652 cp_parser_parse_tentatively (parser);
8653 /* Look for the class-specifier or enum-specifier. */
8654 if (keyword == RID_ENUM)
8655 type_spec = cp_parser_enum_specifier (parser);
8657 type_spec = cp_parser_class_specifier (parser);
8659 /* If that worked, we're done. */
8660 if (cp_parser_parse_definitely (parser))
8662 if (declares_class_or_enum)
8663 *declares_class_or_enum = true;
8670 /* Look for an elaborated-type-specifier. */
8671 type_spec = cp_parser_elaborated_type_specifier (parser,
8674 /* We're declaring a class or enum -- unless we're using
8676 if (declares_class_or_enum && keyword != RID_TYPENAME)
8677 *declares_class_or_enum = true;
8683 type_spec = cp_parser_cv_qualifier_opt (parser);
8684 /* Even though we call a routine that looks for an optional
8685 qualifier, we know that there should be one. */
8686 my_friendly_assert (type_spec != NULL, 20000328);
8687 /* This type-specifier was a cv-qualified. */
8688 if (is_cv_qualifier)
8689 *is_cv_qualifier = true;
8694 /* The `__complex__' keyword is a GNU extension. */
8695 return cp_lexer_consume_token (parser->lexer)->value;
8701 /* If we do not already have a type-specifier, assume we are looking
8702 at a simple-type-specifier. */
8703 type_spec = cp_parser_simple_type_specifier (parser, flags);
8705 /* If we didn't find a type-specifier, and a type-specifier was not
8706 optional in this context, issue an error message. */
8707 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
8709 cp_parser_error (parser, "expected type specifier");
8710 return error_mark_node;
8716 /* Parse a simple-type-specifier.
8718 simple-type-specifier:
8719 :: [opt] nested-name-specifier [opt] type-name
8720 :: [opt] nested-name-specifier template template-id
8735 simple-type-specifier:
8736 __typeof__ unary-expression
8737 __typeof__ ( type-id )
8739 For the various keywords, the value returned is simply the
8740 TREE_IDENTIFIER representing the keyword. For the first two
8741 productions, the value returned is the indicated TYPE_DECL. */
8744 cp_parser_simple_type_specifier (parser, flags)
8746 cp_parser_flags flags;
8748 tree type = NULL_TREE;
8751 /* Peek at the next token. */
8752 token = cp_lexer_peek_token (parser->lexer);
8754 /* If we're looking at a keyword, things are easy. */
8755 switch (token->keyword)
8768 /* Consume the token. */
8769 return cp_lexer_consume_token (parser->lexer)->value;
8775 /* Consume the `typeof' token. */
8776 cp_lexer_consume_token (parser->lexer);
8777 /* Parse the operand to `typeof' */
8778 operand = cp_parser_sizeof_operand (parser, RID_TYPEOF);
8779 /* If it is not already a TYPE, take its type. */
8780 if (!TYPE_P (operand))
8781 operand = finish_typeof (operand);
8790 /* The type-specifier must be a user-defined type. */
8791 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
8793 /* Don't gobble tokens or issue error messages if this is an
8794 optional type-specifier. */
8795 if (flags & CP_PARSER_FLAGS_OPTIONAL)
8796 cp_parser_parse_tentatively (parser);
8798 /* Look for the optional `::' operator. */
8799 cp_parser_global_scope_opt (parser,
8800 /*current_scope_valid_p=*/false);
8801 /* Look for the nested-name specifier. */
8802 cp_parser_nested_name_specifier_opt (parser,
8803 /*typename_keyword_p=*/false,
8804 /*check_dependency_p=*/true,
8806 /* If we have seen a nested-name-specifier, and the next token
8807 is `template', then we are using the template-id production. */
8809 && cp_parser_optional_template_keyword (parser))
8811 /* Look for the template-id. */
8812 type = cp_parser_template_id (parser,
8813 /*template_keyword_p=*/true,
8814 /*check_dependency_p=*/true);
8815 /* If the template-id did not name a type, we are out of
8817 if (TREE_CODE (type) != TYPE_DECL)
8819 cp_parser_error (parser, "expected template-id for type");
8823 /* Otherwise, look for a type-name. */
8826 type = cp_parser_type_name (parser);
8827 if (type == error_mark_node)
8831 /* If it didn't work out, we don't have a TYPE. */
8832 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
8833 && !cp_parser_parse_definitely (parser))
8837 /* If we didn't get a type-name, issue an error message. */
8838 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
8840 cp_parser_error (parser, "expected type-name");
8841 return error_mark_node;
8847 /* Parse a type-name.
8860 Returns a TYPE_DECL for the the type. */
8863 cp_parser_type_name (parser)
8869 /* We can't know yet whether it is a class-name or not. */
8870 cp_parser_parse_tentatively (parser);
8871 /* Try a class-name. */
8872 type_decl = cp_parser_class_name (parser,
8873 /*typename_keyword_p=*/false,
8874 /*template_keyword_p=*/false,
8876 /*check_access_p=*/true,
8877 /*check_dependency_p=*/true,
8878 /*class_head_p=*/false);
8879 /* If it's not a class-name, keep looking. */
8880 if (!cp_parser_parse_definitely (parser))
8882 /* It must be a typedef-name or an enum-name. */
8883 identifier = cp_parser_identifier (parser);
8884 if (identifier == error_mark_node)
8885 return error_mark_node;
8887 /* Look up the type-name. */
8888 type_decl = cp_parser_lookup_name_simple (parser, identifier);
8889 /* Issue an error if we did not find a type-name. */
8890 if (TREE_CODE (type_decl) != TYPE_DECL)
8892 cp_parser_error (parser, "expected type-name");
8893 type_decl = error_mark_node;
8895 /* Remember that the name was used in the definition of the
8896 current class so that we can check later to see if the
8897 meaning would have been different after the class was
8898 entirely defined. */
8899 else if (type_decl != error_mark_node
8901 maybe_note_name_used_in_class (identifier, type_decl);
8908 /* Parse an elaborated-type-specifier. Note that the grammar given
8909 here incorporates the resolution to DR68.
8911 elaborated-type-specifier:
8912 class-key :: [opt] nested-name-specifier [opt] identifier
8913 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
8914 enum :: [opt] nested-name-specifier [opt] identifier
8915 typename :: [opt] nested-name-specifier identifier
8916 typename :: [opt] nested-name-specifier template [opt]
8919 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
8920 declared `friend'. If IS_DECLARATION is TRUE, then this
8921 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
8922 something is being declared.
8924 Returns the TYPE specified. */
8927 cp_parser_elaborated_type_specifier (parser, is_friend, is_declaration)
8930 bool is_declaration;
8932 enum tag_types tag_type;
8934 tree type = NULL_TREE;
8936 /* See if we're looking at the `enum' keyword. */
8937 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
8939 /* Consume the `enum' token. */
8940 cp_lexer_consume_token (parser->lexer);
8941 /* Remember that it's an enumeration type. */
8942 tag_type = enum_type;
8944 /* Or, it might be `typename'. */
8945 else if (cp_lexer_next_token_is_keyword (parser->lexer,
8948 /* Consume the `typename' token. */
8949 cp_lexer_consume_token (parser->lexer);
8950 /* Remember that it's a `typename' type. */
8951 tag_type = typename_type;
8952 /* The `typename' keyword is only allowed in templates. */
8953 if (!processing_template_decl)
8954 pedwarn ("using `typename' outside of template");
8956 /* Otherwise it must be a class-key. */
8959 tag_type = cp_parser_class_key (parser);
8960 if (tag_type == none_type)
8961 return error_mark_node;
8964 /* Look for the `::' operator. */
8965 cp_parser_global_scope_opt (parser,
8966 /*current_scope_valid_p=*/false);
8967 /* Look for the nested-name-specifier. */
8968 if (tag_type == typename_type)
8969 cp_parser_nested_name_specifier (parser,
8970 /*typename_keyword_p=*/true,
8971 /*check_dependency_p=*/true,
8974 /* Even though `typename' is not present, the proposed resolution
8975 to Core Issue 180 says that in `class A<T>::B', `B' should be
8976 considered a type-name, even if `A<T>' is dependent. */
8977 cp_parser_nested_name_specifier_opt (parser,
8978 /*typename_keyword_p=*/true,
8979 /*check_dependency_p=*/true,
8981 /* For everything but enumeration types, consider a template-id. */
8982 if (tag_type != enum_type)
8984 bool template_p = false;
8987 /* Allow the `template' keyword. */
8988 template_p = cp_parser_optional_template_keyword (parser);
8989 /* If we didn't see `template', we don't know if there's a
8990 template-id or not. */
8992 cp_parser_parse_tentatively (parser);
8993 /* Parse the template-id. */
8994 decl = cp_parser_template_id (parser, template_p,
8995 /*check_dependency_p=*/true);
8996 /* If we didn't find a template-id, look for an ordinary
8998 if (!template_p && !cp_parser_parse_definitely (parser))
9000 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
9001 in effect, then we must assume that, upon instantiation, the
9002 template will correspond to a class. */
9003 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
9004 && tag_type == typename_type)
9005 type = make_typename_type (parser->scope, decl,
9008 type = TREE_TYPE (decl);
9011 /* For an enumeration type, consider only a plain identifier. */
9014 identifier = cp_parser_identifier (parser);
9016 if (identifier == error_mark_node)
9017 return error_mark_node;
9019 /* For a `typename', we needn't call xref_tag. */
9020 if (tag_type == typename_type)
9021 return make_typename_type (parser->scope, identifier,
9023 /* Look up a qualified name in the usual way. */
9028 /* In an elaborated-type-specifier, names are assumed to name
9029 types, so we set IS_TYPE to TRUE when calling
9030 cp_parser_lookup_name. */
9031 decl = cp_parser_lookup_name (parser, identifier,
9032 /*check_access=*/true,
9034 /*is_namespace=*/false,
9035 /*check_dependency=*/true);
9036 decl = (cp_parser_maybe_treat_template_as_class
9037 (decl, /*tag_name_p=*/is_friend));
9039 if (TREE_CODE (decl) != TYPE_DECL)
9041 error ("expected type-name");
9042 return error_mark_node;
9044 else if (TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE
9045 && tag_type != enum_type)
9046 error ("`%T' referred to as `%s'", TREE_TYPE (decl),
9047 tag_type == record_type ? "struct" : "class");
9048 else if (TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE
9049 && tag_type == enum_type)
9050 error ("`%T' referred to as enum", TREE_TYPE (decl));
9052 type = TREE_TYPE (decl);
9056 /* An elaborated-type-specifier sometimes introduces a new type and
9057 sometimes names an existing type. Normally, the rule is that it
9058 introduces a new type only if there is not an existing type of
9059 the same name already in scope. For example, given:
9062 void f() { struct S s; }
9064 the `struct S' in the body of `f' is the same `struct S' as in
9065 the global scope; the existing definition is used. However, if
9066 there were no global declaration, this would introduce a new
9067 local class named `S'.
9069 An exception to this rule applies to the following code:
9071 namespace N { struct S; }
9073 Here, the elaborated-type-specifier names a new type
9074 unconditionally; even if there is already an `S' in the
9075 containing scope this declaration names a new type.
9076 This exception only applies if the elaborated-type-specifier
9077 forms the complete declaration:
9081 A declaration consisting solely of `class-key identifier ;' is
9082 either a redeclaration of the name in the current scope or a
9083 forward declaration of the identifier as a class name. It
9084 introduces the name into the current scope.
9086 We are in this situation precisely when the next token is a `;'.
9088 An exception to the exception is that a `friend' declaration does
9089 *not* name a new type; i.e., given:
9091 struct S { friend struct T; };
9093 `T' is not a new type in the scope of `S'.
9095 Also, `new struct S' or `sizeof (struct S)' never results in the
9096 definition of a new type; a new type can only be declared in a
9097 declaration context. */
9099 type = xref_tag (tag_type, identifier,
9100 /*attributes=*/NULL_TREE,
9103 || cp_lexer_next_token_is_not (parser->lexer,
9107 if (tag_type != enum_type)
9108 cp_parser_check_class_key (tag_type, type);
9112 /* Parse an enum-specifier.
9115 enum identifier [opt] { enumerator-list [opt] }
9117 Returns an ENUM_TYPE representing the enumeration. */
9120 cp_parser_enum_specifier (parser)
9124 tree identifier = NULL_TREE;
9127 /* Look for the `enum' keyword. */
9128 if (!cp_parser_require_keyword (parser, RID_ENUM, "`enum'"))
9129 return error_mark_node;
9130 /* Peek at the next token. */
9131 token = cp_lexer_peek_token (parser->lexer);
9133 /* See if it is an identifier. */
9134 if (token->type == CPP_NAME)
9135 identifier = cp_parser_identifier (parser);
9137 /* Look for the `{'. */
9138 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
9139 return error_mark_node;
9141 /* At this point, we're going ahead with the enum-specifier, even
9142 if some other problem occurs. */
9143 cp_parser_commit_to_tentative_parse (parser);
9145 /* Issue an error message if type-definitions are forbidden here. */
9146 cp_parser_check_type_definition (parser);
9148 /* Create the new type. */
9149 type = start_enum (identifier ? identifier : make_anon_name ());
9151 /* Peek at the next token. */
9152 token = cp_lexer_peek_token (parser->lexer);
9153 /* If it's not a `}', then there are some enumerators. */
9154 if (token->type != CPP_CLOSE_BRACE)
9155 cp_parser_enumerator_list (parser, type);
9156 /* Look for the `}'. */
9157 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
9159 /* Finish up the enumeration. */
9165 /* Parse an enumerator-list. The enumerators all have the indicated
9169 enumerator-definition
9170 enumerator-list , enumerator-definition */
9173 cp_parser_enumerator_list (parser, type)
9181 /* Parse an enumerator-definition. */
9182 cp_parser_enumerator_definition (parser, type);
9183 /* Peek at the next token. */
9184 token = cp_lexer_peek_token (parser->lexer);
9185 /* If it's not a `,', then we've reached the end of the
9187 if (token->type != CPP_COMMA)
9189 /* Otherwise, consume the `,' and keep going. */
9190 cp_lexer_consume_token (parser->lexer);
9191 /* If the next token is a `}', there is a trailing comma. */
9192 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
9194 if (pedantic && !in_system_header)
9195 pedwarn ("comma at end of enumerator list");
9201 /* Parse an enumerator-definition. The enumerator has the indicated
9204 enumerator-definition:
9206 enumerator = constant-expression
9212 cp_parser_enumerator_definition (parser, type)
9220 /* Look for the identifier. */
9221 identifier = cp_parser_identifier (parser);
9222 if (identifier == error_mark_node)
9225 /* Peek at the next token. */
9226 token = cp_lexer_peek_token (parser->lexer);
9227 /* If it's an `=', then there's an explicit value. */
9228 if (token->type == CPP_EQ)
9230 /* Consume the `=' token. */
9231 cp_lexer_consume_token (parser->lexer);
9232 /* Parse the value. */
9233 value = cp_parser_constant_expression (parser);
9238 /* Create the enumerator. */
9239 build_enumerator (identifier, value, type);
9242 /* Parse a namespace-name.
9245 original-namespace-name
9248 Returns the NAMESPACE_DECL for the namespace. */
9251 cp_parser_namespace_name (parser)
9255 tree namespace_decl;
9257 /* Get the name of the namespace. */
9258 identifier = cp_parser_identifier (parser);
9259 if (identifier == error_mark_node)
9260 return error_mark_node;
9262 /* Look up the identifier in the currently active scope. Look only
9263 for namespaces, due to:
9267 When looking up a namespace-name in a using-directive or alias
9268 definition, only namespace names are considered.
9274 During the lookup of a name preceding the :: scope resolution
9275 operator, object, function, and enumerator names are ignored.
9277 (Note that cp_parser_class_or_namespace_name only calls this
9278 function if the token after the name is the scope resolution
9280 namespace_decl = cp_parser_lookup_name (parser, identifier,
9281 /*check_access=*/true,
9283 /*is_namespace=*/true,
9284 /*check_dependency=*/true);
9285 /* If it's not a namespace, issue an error. */
9286 if (namespace_decl == error_mark_node
9287 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
9289 cp_parser_error (parser, "expected namespace-name");
9290 namespace_decl = error_mark_node;
9293 return namespace_decl;
9296 /* Parse a namespace-definition.
9298 namespace-definition:
9299 named-namespace-definition
9300 unnamed-namespace-definition
9302 named-namespace-definition:
9303 original-namespace-definition
9304 extension-namespace-definition
9306 original-namespace-definition:
9307 namespace identifier { namespace-body }
9309 extension-namespace-definition:
9310 namespace original-namespace-name { namespace-body }
9312 unnamed-namespace-definition:
9313 namespace { namespace-body } */
9316 cp_parser_namespace_definition (parser)
9321 /* Look for the `namespace' keyword. */
9322 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
9324 /* Get the name of the namespace. We do not attempt to distinguish
9325 between an original-namespace-definition and an
9326 extension-namespace-definition at this point. The semantic
9327 analysis routines are responsible for that. */
9328 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
9329 identifier = cp_parser_identifier (parser);
9331 identifier = NULL_TREE;
9333 /* Look for the `{' to start the namespace. */
9334 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
9335 /* Start the namespace. */
9336 push_namespace (identifier);
9337 /* Parse the body of the namespace. */
9338 cp_parser_namespace_body (parser);
9339 /* Finish the namespace. */
9341 /* Look for the final `}'. */
9342 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
9345 /* Parse a namespace-body.
9348 declaration-seq [opt] */
9351 cp_parser_namespace_body (parser)
9354 cp_parser_declaration_seq_opt (parser);
9357 /* Parse a namespace-alias-definition.
9359 namespace-alias-definition:
9360 namespace identifier = qualified-namespace-specifier ; */
9363 cp_parser_namespace_alias_definition (parser)
9367 tree namespace_specifier;
9369 /* Look for the `namespace' keyword. */
9370 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
9371 /* Look for the identifier. */
9372 identifier = cp_parser_identifier (parser);
9373 if (identifier == error_mark_node)
9375 /* Look for the `=' token. */
9376 cp_parser_require (parser, CPP_EQ, "`='");
9377 /* Look for the qualified-namespace-specifier. */
9379 = cp_parser_qualified_namespace_specifier (parser);
9380 /* Look for the `;' token. */
9381 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
9383 /* Register the alias in the symbol table. */
9384 do_namespace_alias (identifier, namespace_specifier);
9387 /* Parse a qualified-namespace-specifier.
9389 qualified-namespace-specifier:
9390 :: [opt] nested-name-specifier [opt] namespace-name
9392 Returns a NAMESPACE_DECL corresponding to the specified
9396 cp_parser_qualified_namespace_specifier (parser)
9399 /* Look for the optional `::'. */
9400 cp_parser_global_scope_opt (parser,
9401 /*current_scope_valid_p=*/false);
9403 /* Look for the optional nested-name-specifier. */
9404 cp_parser_nested_name_specifier_opt (parser,
9405 /*typename_keyword_p=*/false,
9406 /*check_dependency_p=*/true,
9409 return cp_parser_namespace_name (parser);
9412 /* Parse a using-declaration.
9415 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
9416 using :: unqualified-id ; */
9419 cp_parser_using_declaration (parser)
9423 bool typename_p = false;
9424 bool global_scope_p;
9429 /* Look for the `using' keyword. */
9430 cp_parser_require_keyword (parser, RID_USING, "`using'");
9432 /* Peek at the next token. */
9433 token = cp_lexer_peek_token (parser->lexer);
9434 /* See if it's `typename'. */
9435 if (token->keyword == RID_TYPENAME)
9437 /* Remember that we've seen it. */
9439 /* Consume the `typename' token. */
9440 cp_lexer_consume_token (parser->lexer);
9443 /* Look for the optional global scope qualification. */
9445 = (cp_parser_global_scope_opt (parser,
9446 /*current_scope_valid_p=*/false)
9449 /* If we saw `typename', or didn't see `::', then there must be a
9450 nested-name-specifier present. */
9451 if (typename_p || !global_scope_p)
9452 cp_parser_nested_name_specifier (parser, typename_p,
9453 /*check_dependency_p=*/true,
9455 /* Otherwise, we could be in either of the two productions. In that
9456 case, treat the nested-name-specifier as optional. */
9458 cp_parser_nested_name_specifier_opt (parser,
9459 /*typename_keyword_p=*/false,
9460 /*check_dependency_p=*/true,
9463 /* Parse the unqualified-id. */
9464 identifier = cp_parser_unqualified_id (parser,
9465 /*template_keyword_p=*/false,
9466 /*check_dependency_p=*/true);
9468 /* The function we call to handle a using-declaration is different
9469 depending on what scope we are in. */
9470 scope = current_scope ();
9471 if (scope && TYPE_P (scope))
9473 /* Create the USING_DECL. */
9474 decl = do_class_using_decl (build_nt (SCOPE_REF,
9477 /* Add it to the list of members in this class. */
9478 finish_member_declaration (decl);
9482 decl = cp_parser_lookup_name_simple (parser, identifier);
9484 do_local_using_decl (decl);
9486 do_toplevel_using_decl (decl);
9489 /* Look for the final `;'. */
9490 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
9493 /* Parse a using-directive.
9496 using namespace :: [opt] nested-name-specifier [opt]
9500 cp_parser_using_directive (parser)
9503 tree namespace_decl;
9505 /* Look for the `using' keyword. */
9506 cp_parser_require_keyword (parser, RID_USING, "`using'");
9507 /* And the `namespace' keyword. */
9508 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
9509 /* Look for the optional `::' operator. */
9510 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
9511 /* And the optional nested-name-sepcifier. */
9512 cp_parser_nested_name_specifier_opt (parser,
9513 /*typename_keyword_p=*/false,
9514 /*check_dependency_p=*/true,
9516 /* Get the namespace being used. */
9517 namespace_decl = cp_parser_namespace_name (parser);
9518 /* Update the symbol table. */
9519 do_using_directive (namespace_decl);
9520 /* Look for the final `;'. */
9521 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
9524 /* Parse an asm-definition.
9527 asm ( string-literal ) ;
9532 asm volatile [opt] ( string-literal ) ;
9533 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
9534 asm volatile [opt] ( string-literal : asm-operand-list [opt]
9535 : asm-operand-list [opt] ) ;
9536 asm volatile [opt] ( string-literal : asm-operand-list [opt]
9537 : asm-operand-list [opt]
9538 : asm-operand-list [opt] ) ; */
9541 cp_parser_asm_definition (parser)
9546 tree outputs = NULL_TREE;
9547 tree inputs = NULL_TREE;
9548 tree clobbers = NULL_TREE;
9550 bool volatile_p = false;
9551 bool extended_p = false;
9553 /* Look for the `asm' keyword. */
9554 cp_parser_require_keyword (parser, RID_ASM, "`asm'");
9555 /* See if the next token is `volatile'. */
9556 if (cp_parser_allow_gnu_extensions_p (parser)
9557 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
9559 /* Remember that we saw the `volatile' keyword. */
9561 /* Consume the token. */
9562 cp_lexer_consume_token (parser->lexer);
9564 /* Look for the opening `('. */
9565 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
9566 /* Look for the string. */
9567 token = cp_parser_require (parser, CPP_STRING, "asm body");
9570 string = token->value;
9571 /* If we're allowing GNU extensions, check for the extended assembly
9572 syntax. Unfortunately, the `:' tokens need not be separated by
9573 a space in C, and so, for compatibility, we tolerate that here
9574 too. Doing that means that we have to treat the `::' operator as
9576 if (cp_parser_allow_gnu_extensions_p (parser)
9577 && at_function_scope_p ()
9578 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
9579 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
9581 bool inputs_p = false;
9582 bool clobbers_p = false;
9584 /* The extended syntax was used. */
9587 /* Look for outputs. */
9588 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
9590 /* Consume the `:'. */
9591 cp_lexer_consume_token (parser->lexer);
9592 /* Parse the output-operands. */
9593 if (cp_lexer_next_token_is_not (parser->lexer,
9595 && cp_lexer_next_token_is_not (parser->lexer,
9597 && cp_lexer_next_token_is_not (parser->lexer,
9599 outputs = cp_parser_asm_operand_list (parser);
9601 /* If the next token is `::', there are no outputs, and the
9602 next token is the beginning of the inputs. */
9603 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
9605 /* Consume the `::' token. */
9606 cp_lexer_consume_token (parser->lexer);
9607 /* The inputs are coming next. */
9611 /* Look for inputs. */
9613 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
9616 /* Consume the `:'. */
9617 cp_lexer_consume_token (parser->lexer);
9618 /* Parse the output-operands. */
9619 if (cp_lexer_next_token_is_not (parser->lexer,
9621 && cp_lexer_next_token_is_not (parser->lexer,
9623 && cp_lexer_next_token_is_not (parser->lexer,
9625 inputs = cp_parser_asm_operand_list (parser);
9627 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
9628 /* The clobbers are coming next. */
9631 /* Look for clobbers. */
9633 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
9636 /* Consume the `:'. */
9637 cp_lexer_consume_token (parser->lexer);
9638 /* Parse the clobbers. */
9639 if (cp_lexer_next_token_is_not (parser->lexer,
9641 clobbers = cp_parser_asm_clobber_list (parser);
9644 /* Look for the closing `)'. */
9645 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
9646 cp_parser_skip_to_closing_parenthesis (parser);
9647 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
9649 /* Create the ASM_STMT. */
9650 if (at_function_scope_p ())
9653 finish_asm_stmt (volatile_p
9654 ? ridpointers[(int) RID_VOLATILE] : NULL_TREE,
9655 string, outputs, inputs, clobbers);
9656 /* If the extended syntax was not used, mark the ASM_STMT. */
9658 ASM_INPUT_P (asm_stmt) = 1;
9661 assemble_asm (string);
9664 /* Declarators [gram.dcl.decl] */
9666 /* Parse an init-declarator.
9669 declarator initializer [opt]
9674 declarator asm-specification [opt] attributes [opt] initializer [opt]
9676 The DECL_SPECIFIERS and PREFIX_ATTRIBUTES apply to this declarator.
9677 Returns a reprsentation of the entity declared. The ACCESS_CHECKS
9678 represent deferred access checks from the decl-specifier-seq. If
9679 MEMBER_P is TRUE, then this declarator appears in a class scope.
9680 The new DECL created by this declarator is returned.
9682 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
9683 for a function-definition here as well. If the declarator is a
9684 declarator for a function-definition, *FUNCTION_DEFINITION_P will
9685 be TRUE upon return. By that point, the function-definition will
9686 have been completely parsed.
9688 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
9692 cp_parser_init_declarator (parser,
9696 function_definition_allowed_p,
9698 function_definition_p)
9700 tree decl_specifiers;
9701 tree prefix_attributes;
9703 bool function_definition_allowed_p;
9705 bool *function_definition_p;
9710 tree asm_specification;
9712 tree decl = NULL_TREE;
9714 tree declarator_access_checks;
9715 bool is_initialized;
9716 bool is_parenthesized_init;
9717 bool ctor_dtor_or_conv_p;
9720 /* Assume that this is not the declarator for a function
9722 if (function_definition_p)
9723 *function_definition_p = false;
9725 /* Defer access checks while parsing the declarator; we cannot know
9726 what names are accessible until we know what is being
9728 cp_parser_start_deferring_access_checks (parser);
9729 /* Parse the declarator. */
9731 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
9732 &ctor_dtor_or_conv_p);
9733 /* Gather up the deferred checks. */
9734 declarator_access_checks
9735 = cp_parser_stop_deferring_access_checks (parser);
9737 /* Prevent the access checks from being reclaimed by GC. */
9738 parser->access_checks_lists
9739 = tree_cons (NULL_TREE, declarator_access_checks,
9740 parser->access_checks_lists);
9742 /* If the DECLARATOR was erroneous, there's no need to go
9744 if (declarator == error_mark_node)
9746 /* Discard access checks no longer in use. */
9747 parser->access_checks_lists
9748 = TREE_CHAIN (parser->access_checks_lists);
9749 return error_mark_node;
9752 /* Figure out what scope the entity declared by the DECLARATOR is
9753 located in. `grokdeclarator' sometimes changes the scope, so
9754 we compute it now. */
9755 scope = get_scope_of_declarator (declarator);
9757 /* If we're allowing GNU extensions, look for an asm-specification
9759 if (cp_parser_allow_gnu_extensions_p (parser))
9761 /* Look for an asm-specification. */
9762 asm_specification = cp_parser_asm_specification_opt (parser);
9763 /* And attributes. */
9764 attributes = cp_parser_attributes_opt (parser);
9768 asm_specification = NULL_TREE;
9769 attributes = NULL_TREE;
9772 /* Peek at the next token. */
9773 token = cp_lexer_peek_token (parser->lexer);
9774 /* Check to see if the token indicates the start of a
9775 function-definition. */
9776 if (cp_parser_token_starts_function_definition_p (token))
9778 if (!function_definition_allowed_p)
9780 /* If a function-definition should not appear here, issue an
9782 cp_parser_error (parser,
9783 "a function-definition is not allowed here");
9784 /* Discard access checks no longer in use. */
9785 parser->access_checks_lists
9786 = TREE_CHAIN (parser->access_checks_lists);
9787 return error_mark_node;
9793 /* Neither attributes nor an asm-specification are allowed
9794 on a function-definition. */
9795 if (asm_specification)
9796 error ("an asm-specification is not allowed on a function-definition");
9798 error ("attributes are not allowed on a function-definition");
9799 /* This is a function-definition. */
9800 *function_definition_p = true;
9802 /* Thread the access checks together. */
9803 ac = &access_checks;
9805 ac = &TREE_CHAIN (*ac);
9806 *ac = declarator_access_checks;
9808 /* Parse the function definition. */
9809 decl = (cp_parser_function_definition_from_specifiers_and_declarator
9810 (parser, decl_specifiers, prefix_attributes, declarator,
9813 /* Pull the access-checks apart again. */
9816 /* Discard access checks no longer in use. */
9817 parser->access_checks_lists
9818 = TREE_CHAIN (parser->access_checks_lists);
9826 Only in function declarations for constructors, destructors, and
9827 type conversions can the decl-specifier-seq be omitted.
9829 We explicitly postpone this check past the point where we handle
9830 function-definitions because we tolerate function-definitions
9831 that are missing their return types in some modes. */
9832 if (!decl_specifiers && !ctor_dtor_or_conv_p)
9834 cp_parser_error (parser,
9835 "expected constructor, destructor, or type conversion");
9836 /* Discard access checks no longer in use. */
9837 parser->access_checks_lists
9838 = TREE_CHAIN (parser->access_checks_lists);
9839 return error_mark_node;
9842 /* An `=' or an `(' indicates an initializer. */
9843 is_initialized = (token->type == CPP_EQ
9844 || token->type == CPP_OPEN_PAREN);
9845 /* If the init-declarator isn't initialized and isn't followed by a
9846 `,' or `;', it's not a valid init-declarator. */
9848 && token->type != CPP_COMMA
9849 && token->type != CPP_SEMICOLON)
9851 cp_parser_error (parser, "expected init-declarator");
9852 /* Discard access checks no longer in use. */
9853 parser->access_checks_lists
9854 = TREE_CHAIN (parser->access_checks_lists);
9855 return error_mark_node;
9858 /* Because start_decl has side-effects, we should only call it if we
9859 know we're going ahead. By this point, we know that we cannot
9860 possibly be looking at any other construct. */
9861 cp_parser_commit_to_tentative_parse (parser);
9863 /* Check to see whether or not this declaration is a friend. */
9864 friend_p = cp_parser_friend_p (decl_specifiers);
9866 /* Check that the number of template-parameter-lists is OK. */
9867 if (!cp_parser_check_declarator_template_parameters (parser,
9870 /* Discard access checks no longer in use. */
9871 parser->access_checks_lists
9872 = TREE_CHAIN (parser->access_checks_lists);
9873 return error_mark_node;
9876 /* Enter the newly declared entry in the symbol table. If we're
9877 processing a declaration in a class-specifier, we wait until
9878 after processing the initializer. */
9881 if (parser->in_unbraced_linkage_specification_p)
9883 decl_specifiers = tree_cons (error_mark_node,
9884 get_identifier ("extern"),
9886 have_extern_spec = false;
9888 decl = start_decl (declarator,
9895 /* Enter the SCOPE. That way unqualified names appearing in the
9896 initializer will be looked up in SCOPE. */
9900 /* Perform deferred access control checks, now that we know in which
9901 SCOPE the declared entity resides. */
9902 if (!member_p && decl)
9904 tree saved_current_function_decl = NULL_TREE;
9906 /* If the entity being declared is a function, pretend that we
9907 are in its scope. If it is a `friend', it may have access to
9908 things that would not otherwise be accessible. */
9909 if (TREE_CODE (decl) == FUNCTION_DECL)
9911 saved_current_function_decl = current_function_decl;
9912 current_function_decl = decl;
9915 /* Perform the access control checks for the decl-specifiers. */
9916 cp_parser_perform_deferred_access_checks (access_checks);
9917 /* And for the declarator. */
9918 cp_parser_perform_deferred_access_checks (declarator_access_checks);
9920 /* Restore the saved value. */
9921 if (TREE_CODE (decl) == FUNCTION_DECL)
9922 current_function_decl = saved_current_function_decl;
9925 /* Parse the initializer. */
9927 initializer = cp_parser_initializer (parser,
9928 &is_parenthesized_init);
9931 initializer = NULL_TREE;
9932 is_parenthesized_init = false;
9935 /* The old parser allows attributes to appear after a parenthesized
9936 initializer. Mark Mitchell proposed removing this functionality
9937 on the GCC mailing lists on 2002-08-13. This parser accepts the
9938 attributes -- but ignores them. */
9939 if (cp_parser_allow_gnu_extensions_p (parser) && is_parenthesized_init)
9940 if (cp_parser_attributes_opt (parser))
9941 warning ("attributes after parenthesized initializer ignored");
9943 /* Leave the SCOPE, now that we have processed the initializer. It
9944 is important to do this before calling cp_finish_decl because it
9945 makes decisions about whether to create DECL_STMTs or not based
9946 on the current scope. */
9950 /* For an in-class declaration, use `grokfield' to create the
9953 decl = grokfield (declarator, decl_specifiers,
9954 initializer, /*asmspec=*/NULL_TREE,
9955 /*attributes=*/NULL_TREE);
9957 /* Finish processing the declaration. But, skip friend
9959 if (!friend_p && decl)
9960 cp_finish_decl (decl,
9963 /* If the initializer is in parentheses, then this is
9964 a direct-initialization, which means that an
9965 `explicit' constructor is OK. Otherwise, an
9966 `explicit' constructor cannot be used. */
9967 ((is_parenthesized_init || !is_initialized)
9968 ? 0 : LOOKUP_ONLYCONVERTING));
9970 /* Discard access checks no longer in use. */
9971 parser->access_checks_lists
9972 = TREE_CHAIN (parser->access_checks_lists);
9977 /* Parse a declarator.
9981 ptr-operator declarator
9983 abstract-declarator:
9984 ptr-operator abstract-declarator [opt]
9985 direct-abstract-declarator
9990 attributes [opt] direct-declarator
9991 attributes [opt] ptr-operator declarator
9993 abstract-declarator:
9994 attributes [opt] ptr-operator abstract-declarator [opt]
9995 attributes [opt] direct-abstract-declarator
9997 Returns a representation of the declarator. If the declarator has
9998 the form `* declarator', then an INDIRECT_REF is returned, whose
9999 only operand is the sub-declarator. Analagously, `& declarator' is
10000 represented as an ADDR_EXPR. For `X::* declarator', a SCOPE_REF is
10001 used. The first operand is the TYPE for `X'. The second operand
10002 is an INDIRECT_REF whose operand is the sub-declarator.
10004 Otherwise, the reprsentation is as for a direct-declarator.
10006 (It would be better to define a structure type to represent
10007 declarators, rather than abusing `tree' nodes to represent
10008 declarators. That would be much clearer and save some memory.
10009 There is no reason for declarators to be garbage-collected, for
10010 example; they are created during parser and no longer needed after
10011 `grokdeclarator' has been called.)
10013 For a ptr-operator that has the optional cv-qualifier-seq,
10014 cv-qualifiers will be stored in the TREE_TYPE of the INDIRECT_REF
10017 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is set to
10018 true if this declarator represents a constructor, destructor, or
10019 type conversion operator. Otherwise, it is set to false.
10021 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
10022 a decl-specifier-seq unless it declares a constructor, destructor,
10023 or conversion. It might seem that we could check this condition in
10024 semantic analysis, rather than parsing, but that makes it difficult
10025 to handle something like `f()'. We want to notice that there are
10026 no decl-specifiers, and therefore realize that this is an
10027 expression, not a declaration.) */
10030 cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p)
10032 cp_parser_declarator_kind dcl_kind;
10033 bool *ctor_dtor_or_conv_p;
10037 enum tree_code code;
10038 tree cv_qualifier_seq;
10040 tree attributes = NULL_TREE;
10042 /* Assume this is not a constructor, destructor, or type-conversion
10044 if (ctor_dtor_or_conv_p)
10045 *ctor_dtor_or_conv_p = false;
10047 if (cp_parser_allow_gnu_extensions_p (parser))
10048 attributes = cp_parser_attributes_opt (parser);
10050 /* Peek at the next token. */
10051 token = cp_lexer_peek_token (parser->lexer);
10053 /* Check for the ptr-operator production. */
10054 cp_parser_parse_tentatively (parser);
10055 /* Parse the ptr-operator. */
10056 code = cp_parser_ptr_operator (parser,
10058 &cv_qualifier_seq);
10059 /* If that worked, then we have a ptr-operator. */
10060 if (cp_parser_parse_definitely (parser))
10062 /* The dependent declarator is optional if we are parsing an
10063 abstract-declarator. */
10064 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
10065 cp_parser_parse_tentatively (parser);
10067 /* Parse the dependent declarator. */
10068 declarator = cp_parser_declarator (parser, dcl_kind,
10069 /*ctor_dtor_or_conv_p=*/NULL);
10071 /* If we are parsing an abstract-declarator, we must handle the
10072 case where the dependent declarator is absent. */
10073 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
10074 && !cp_parser_parse_definitely (parser))
10075 declarator = NULL_TREE;
10077 /* Build the representation of the ptr-operator. */
10078 if (code == INDIRECT_REF)
10079 declarator = make_pointer_declarator (cv_qualifier_seq,
10082 declarator = make_reference_declarator (cv_qualifier_seq,
10084 /* Handle the pointer-to-member case. */
10086 declarator = build_nt (SCOPE_REF, class_type, declarator);
10088 /* Everything else is a direct-declarator. */
10090 declarator = cp_parser_direct_declarator (parser,
10092 ctor_dtor_or_conv_p);
10094 if (attributes && declarator != error_mark_node)
10095 declarator = tree_cons (attributes, declarator, NULL_TREE);
10100 /* Parse a direct-declarator or direct-abstract-declarator.
10104 direct-declarator ( parameter-declaration-clause )
10105 cv-qualifier-seq [opt]
10106 exception-specification [opt]
10107 direct-declarator [ constant-expression [opt] ]
10110 direct-abstract-declarator:
10111 direct-abstract-declarator [opt]
10112 ( parameter-declaration-clause )
10113 cv-qualifier-seq [opt]
10114 exception-specification [opt]
10115 direct-abstract-declarator [opt] [ constant-expression [opt] ]
10116 ( abstract-declarator )
10118 Returns a representation of the declarator. DCL_KIND is
10119 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
10120 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
10121 we are parsing a direct-declarator. It is
10122 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
10123 of ambiguity we prefer an abstract declarator, as per
10124 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P is as for
10125 cp_parser_declarator.
10127 For the declarator-id production, the representation is as for an
10128 id-expression, except that a qualified name is represented as a
10129 SCOPE_REF. A function-declarator is represented as a CALL_EXPR;
10130 see the documentation of the FUNCTION_DECLARATOR_* macros for
10131 information about how to find the various declarator components.
10132 An array-declarator is represented as an ARRAY_REF. The
10133 direct-declarator is the first operand; the constant-expression
10134 indicating the size of the array is the second operand. */
10137 cp_parser_direct_declarator (parser, dcl_kind, ctor_dtor_or_conv_p)
10139 cp_parser_declarator_kind dcl_kind;
10140 bool *ctor_dtor_or_conv_p;
10143 tree declarator = NULL_TREE;
10144 tree scope = NULL_TREE;
10145 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
10146 bool saved_in_declarator_p = parser->in_declarator_p;
10151 /* Peek at the next token. */
10152 token = cp_lexer_peek_token (parser->lexer);
10153 if (token->type == CPP_OPEN_PAREN)
10155 /* This is either a parameter-declaration-clause, or a
10156 parenthesized declarator. When we know we are parsing a
10157 named declaratory, it must be a paranthesized declarator
10158 if FIRST is true. For instance, `(int)' is a
10159 parameter-declaration-clause, with an omitted
10160 direct-abstract-declarator. But `((*))', is a
10161 parenthesized abstract declarator. Finally, when T is a
10162 template parameter `(T)' is a
10163 paremeter-declaration-clause, and not a parenthesized
10166 We first try and parse a parameter-declaration-clause,
10167 and then try a nested declarator (if FIRST is true).
10169 It is not an error for it not to be a
10170 parameter-declaration-clause, even when FIRST is
10176 The first is the declaration of a function while the
10177 second is a the definition of a variable, including its
10180 Having seen only the parenthesis, we cannot know which of
10181 these two alternatives should be selected. Even more
10182 complex are examples like:
10187 The former is a function-declaration; the latter is a
10188 variable initialization.
10190 Thus again, we try a parameter-declation-clause, and if
10191 that fails, we back out and return. */
10193 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
10197 cp_parser_parse_tentatively (parser);
10199 /* Consume the `('. */
10200 cp_lexer_consume_token (parser->lexer);
10203 /* If this is going to be an abstract declarator, we're
10204 in a declarator and we can't have default args. */
10205 parser->default_arg_ok_p = false;
10206 parser->in_declarator_p = true;
10209 /* Parse the parameter-declaration-clause. */
10210 params = cp_parser_parameter_declaration_clause (parser);
10212 /* If all went well, parse the cv-qualifier-seq and the
10213 exception-specfication. */
10214 if (cp_parser_parse_definitely (parser))
10216 tree cv_qualifiers;
10217 tree exception_specification;
10220 /* Consume the `)'. */
10221 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
10223 /* Parse the cv-qualifier-seq. */
10224 cv_qualifiers = cp_parser_cv_qualifier_seq_opt (parser);
10225 /* And the exception-specification. */
10226 exception_specification
10227 = cp_parser_exception_specification_opt (parser);
10229 /* Create the function-declarator. */
10230 declarator = make_call_declarator (declarator,
10233 exception_specification);
10234 /* Any subsequent parameter lists are to do with
10235 return type, so are not those of the declared
10237 parser->default_arg_ok_p = false;
10239 /* Repeat the main loop. */
10244 /* If this is the first, we can try a parenthesized
10248 parser->default_arg_ok_p = saved_default_arg_ok_p;
10249 parser->in_declarator_p = saved_in_declarator_p;
10251 /* Consume the `('. */
10252 cp_lexer_consume_token (parser->lexer);
10253 /* Parse the nested declarator. */
10255 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p);
10257 /* Expect a `)'. */
10258 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
10259 declarator = error_mark_node;
10260 if (declarator == error_mark_node)
10263 goto handle_declarator;
10265 /* Otherwise, we must be done. */
10269 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
10270 && token->type == CPP_OPEN_SQUARE)
10272 /* Parse an array-declarator. */
10276 parser->default_arg_ok_p = false;
10277 parser->in_declarator_p = true;
10278 /* Consume the `['. */
10279 cp_lexer_consume_token (parser->lexer);
10280 /* Peek at the next token. */
10281 token = cp_lexer_peek_token (parser->lexer);
10282 /* If the next token is `]', then there is no
10283 constant-expression. */
10284 if (token->type != CPP_CLOSE_SQUARE)
10285 bounds = cp_parser_constant_expression (parser);
10287 bounds = NULL_TREE;
10288 /* Look for the closing `]'. */
10289 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'"))
10291 declarator = error_mark_node;
10295 declarator = build_nt (ARRAY_REF, declarator, bounds);
10297 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
10299 /* Parse a declarator_id */
10300 if (dcl_kind == CP_PARSER_DECLARATOR_EITHER)
10301 cp_parser_parse_tentatively (parser);
10302 declarator = cp_parser_declarator_id (parser);
10303 if (dcl_kind == CP_PARSER_DECLARATOR_EITHER
10304 && !cp_parser_parse_definitely (parser))
10305 declarator = error_mark_node;
10306 if (declarator == error_mark_node)
10309 if (TREE_CODE (declarator) == SCOPE_REF)
10311 tree scope = TREE_OPERAND (declarator, 0);
10313 /* In the declaration of a member of a template class
10314 outside of the class itself, the SCOPE will sometimes
10315 be a TYPENAME_TYPE. For example, given:
10317 template <typename T>
10318 int S<T>::R::i = 3;
10320 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
10321 this context, we must resolve S<T>::R to an ordinary
10322 type, rather than a typename type.
10324 The reason we normally avoid resolving TYPENAME_TYPEs
10325 is that a specialization of `S' might render
10326 `S<T>::R' not a type. However, if `S' is
10327 specialized, then this `i' will not be used, so there
10328 is no harm in resolving the types here. */
10329 if (TREE_CODE (scope) == TYPENAME_TYPE)
10331 /* Resolve the TYPENAME_TYPE. */
10332 scope = cp_parser_resolve_typename_type (parser, scope);
10333 /* If that failed, the declarator is invalid. */
10334 if (scope == error_mark_node)
10335 return error_mark_node;
10336 /* Build a new DECLARATOR. */
10337 declarator = build_nt (SCOPE_REF,
10339 TREE_OPERAND (declarator, 1));
10343 /* Check to see whether the declarator-id names a constructor,
10344 destructor, or conversion. */
10345 if (declarator && ctor_dtor_or_conv_p
10346 && ((TREE_CODE (declarator) == SCOPE_REF
10347 && CLASS_TYPE_P (TREE_OPERAND (declarator, 0)))
10348 || (TREE_CODE (declarator) != SCOPE_REF
10349 && at_class_scope_p ())))
10351 tree unqualified_name;
10354 /* Get the unqualified part of the name. */
10355 if (TREE_CODE (declarator) == SCOPE_REF)
10357 class_type = TREE_OPERAND (declarator, 0);
10358 unqualified_name = TREE_OPERAND (declarator, 1);
10362 class_type = current_class_type;
10363 unqualified_name = declarator;
10366 /* See if it names ctor, dtor or conv. */
10367 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR
10368 || IDENTIFIER_TYPENAME_P (unqualified_name)
10369 || constructor_name_p (unqualified_name, class_type))
10370 *ctor_dtor_or_conv_p = true;
10373 handle_declarator:;
10374 scope = get_scope_of_declarator (declarator);
10376 /* Any names that appear after the declarator-id for a member
10377 are looked up in the containing scope. */
10378 push_scope (scope);
10379 parser->in_declarator_p = true;
10380 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
10382 && (TREE_CODE (declarator) == SCOPE_REF
10383 || TREE_CODE (declarator) == IDENTIFIER_NODE)))
10384 /* Default args are only allowed on function
10386 parser->default_arg_ok_p = saved_default_arg_ok_p;
10388 parser->default_arg_ok_p = false;
10397 /* For an abstract declarator, we might wind up with nothing at this
10398 point. That's an error; the declarator is not optional. */
10400 cp_parser_error (parser, "expected declarator");
10402 /* If we entered a scope, we must exit it now. */
10406 parser->default_arg_ok_p = saved_default_arg_ok_p;
10407 parser->in_declarator_p = saved_in_declarator_p;
10412 /* Parse a ptr-operator.
10415 * cv-qualifier-seq [opt]
10417 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
10422 & cv-qualifier-seq [opt]
10424 Returns INDIRECT_REF if a pointer, or pointer-to-member, was
10425 used. Returns ADDR_EXPR if a reference was used. In the
10426 case of a pointer-to-member, *TYPE is filled in with the
10427 TYPE containing the member. *CV_QUALIFIER_SEQ is filled in
10428 with the cv-qualifier-seq, or NULL_TREE, if there are no
10429 cv-qualifiers. Returns ERROR_MARK if an error occurred. */
10431 static enum tree_code
10432 cp_parser_ptr_operator (parser, type, cv_qualifier_seq)
10435 tree *cv_qualifier_seq;
10437 enum tree_code code = ERROR_MARK;
10440 /* Assume that it's not a pointer-to-member. */
10442 /* And that there are no cv-qualifiers. */
10443 *cv_qualifier_seq = NULL_TREE;
10445 /* Peek at the next token. */
10446 token = cp_lexer_peek_token (parser->lexer);
10447 /* If it's a `*' or `&' we have a pointer or reference. */
10448 if (token->type == CPP_MULT || token->type == CPP_AND)
10450 /* Remember which ptr-operator we were processing. */
10451 code = (token->type == CPP_AND ? ADDR_EXPR : INDIRECT_REF);
10453 /* Consume the `*' or `&'. */
10454 cp_lexer_consume_token (parser->lexer);
10456 /* A `*' can be followed by a cv-qualifier-seq, and so can a
10457 `&', if we are allowing GNU extensions. (The only qualifier
10458 that can legally appear after `&' is `restrict', but that is
10459 enforced during semantic analysis. */
10460 if (code == INDIRECT_REF
10461 || cp_parser_allow_gnu_extensions_p (parser))
10462 *cv_qualifier_seq = cp_parser_cv_qualifier_seq_opt (parser);
10466 /* Try the pointer-to-member case. */
10467 cp_parser_parse_tentatively (parser);
10468 /* Look for the optional `::' operator. */
10469 cp_parser_global_scope_opt (parser,
10470 /*current_scope_valid_p=*/false);
10471 /* Look for the nested-name specifier. */
10472 cp_parser_nested_name_specifier (parser,
10473 /*typename_keyword_p=*/false,
10474 /*check_dependency_p=*/true,
10476 /* If we found it, and the next token is a `*', then we are
10477 indeed looking at a pointer-to-member operator. */
10478 if (!cp_parser_error_occurred (parser)
10479 && cp_parser_require (parser, CPP_MULT, "`*'"))
10481 /* The type of which the member is a member is given by the
10483 *type = parser->scope;
10484 /* The next name will not be qualified. */
10485 parser->scope = NULL_TREE;
10486 parser->qualifying_scope = NULL_TREE;
10487 parser->object_scope = NULL_TREE;
10488 /* Indicate that the `*' operator was used. */
10489 code = INDIRECT_REF;
10490 /* Look for the optional cv-qualifier-seq. */
10491 *cv_qualifier_seq = cp_parser_cv_qualifier_seq_opt (parser);
10493 /* If that didn't work we don't have a ptr-operator. */
10494 if (!cp_parser_parse_definitely (parser))
10495 cp_parser_error (parser, "expected ptr-operator");
10501 /* Parse an (optional) cv-qualifier-seq.
10504 cv-qualifier cv-qualifier-seq [opt]
10506 Returns a TREE_LIST. The TREE_VALUE of each node is the
10507 representation of a cv-qualifier. */
10510 cp_parser_cv_qualifier_seq_opt (parser)
10513 tree cv_qualifiers = NULL_TREE;
10519 /* Look for the next cv-qualifier. */
10520 cv_qualifier = cp_parser_cv_qualifier_opt (parser);
10521 /* If we didn't find one, we're done. */
10525 /* Add this cv-qualifier to the list. */
10527 = tree_cons (NULL_TREE, cv_qualifier, cv_qualifiers);
10530 /* We built up the list in reverse order. */
10531 return nreverse (cv_qualifiers);
10534 /* Parse an (optional) cv-qualifier.
10546 cp_parser_cv_qualifier_opt (parser)
10550 tree cv_qualifier = NULL_TREE;
10552 /* Peek at the next token. */
10553 token = cp_lexer_peek_token (parser->lexer);
10554 /* See if it's a cv-qualifier. */
10555 switch (token->keyword)
10560 /* Save the value of the token. */
10561 cv_qualifier = token->value;
10562 /* Consume the token. */
10563 cp_lexer_consume_token (parser->lexer);
10570 return cv_qualifier;
10573 /* Parse a declarator-id.
10577 :: [opt] nested-name-specifier [opt] type-name
10579 In the `id-expression' case, the value returned is as for
10580 cp_parser_id_expression if the id-expression was an unqualified-id.
10581 If the id-expression was a qualified-id, then a SCOPE_REF is
10582 returned. The first operand is the scope (either a NAMESPACE_DECL
10583 or TREE_TYPE), but the second is still just a representation of an
10587 cp_parser_declarator_id (parser)
10590 tree id_expression;
10592 /* The expression must be an id-expression. Assume that qualified
10593 names are the names of types so that:
10596 int S<T>::R::i = 3;
10598 will work; we must treat `S<T>::R' as the name of a type.
10599 Similarly, assume that qualified names are templates, where
10603 int S<T>::R<T>::i = 3;
10606 id_expression = cp_parser_id_expression (parser,
10607 /*template_keyword_p=*/false,
10608 /*check_dependency_p=*/false,
10609 /*template_p=*/NULL);
10610 /* If the name was qualified, create a SCOPE_REF to represent
10613 id_expression = build_nt (SCOPE_REF, parser->scope, id_expression);
10615 return id_expression;
10618 /* Parse a type-id.
10621 type-specifier-seq abstract-declarator [opt]
10623 Returns the TYPE specified. */
10626 cp_parser_type_id (parser)
10629 tree type_specifier_seq;
10630 tree abstract_declarator;
10632 /* Parse the type-specifier-seq. */
10634 = cp_parser_type_specifier_seq (parser);
10635 if (type_specifier_seq == error_mark_node)
10636 return error_mark_node;
10638 /* There might or might not be an abstract declarator. */
10639 cp_parser_parse_tentatively (parser);
10640 /* Look for the declarator. */
10641 abstract_declarator
10642 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL);
10643 /* Check to see if there really was a declarator. */
10644 if (!cp_parser_parse_definitely (parser))
10645 abstract_declarator = NULL_TREE;
10647 return groktypename (build_tree_list (type_specifier_seq,
10648 abstract_declarator));
10651 /* Parse a type-specifier-seq.
10653 type-specifier-seq:
10654 type-specifier type-specifier-seq [opt]
10658 type-specifier-seq:
10659 attributes type-specifier-seq [opt]
10661 Returns a TREE_LIST. Either the TREE_VALUE of each node is a
10662 type-specifier, or the TREE_PURPOSE is a list of attributes. */
10665 cp_parser_type_specifier_seq (parser)
10668 bool seen_type_specifier = false;
10669 tree type_specifier_seq = NULL_TREE;
10671 /* Parse the type-specifiers and attributes. */
10674 tree type_specifier;
10676 /* Check for attributes first. */
10677 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
10679 type_specifier_seq = tree_cons (cp_parser_attributes_opt (parser),
10681 type_specifier_seq);
10685 /* After the first type-specifier, others are optional. */
10686 if (seen_type_specifier)
10687 cp_parser_parse_tentatively (parser);
10688 /* Look for the type-specifier. */
10689 type_specifier = cp_parser_type_specifier (parser,
10690 CP_PARSER_FLAGS_NONE,
10691 /*is_friend=*/false,
10692 /*is_declaration=*/false,
10695 /* If the first type-specifier could not be found, this is not a
10696 type-specifier-seq at all. */
10697 if (!seen_type_specifier && type_specifier == error_mark_node)
10698 return error_mark_node;
10699 /* If subsequent type-specifiers could not be found, the
10700 type-specifier-seq is complete. */
10701 else if (seen_type_specifier && !cp_parser_parse_definitely (parser))
10704 /* Add the new type-specifier to the list. */
10706 = tree_cons (NULL_TREE, type_specifier, type_specifier_seq);
10707 seen_type_specifier = true;
10710 /* We built up the list in reverse order. */
10711 return nreverse (type_specifier_seq);
10714 /* Parse a parameter-declaration-clause.
10716 parameter-declaration-clause:
10717 parameter-declaration-list [opt] ... [opt]
10718 parameter-declaration-list , ...
10720 Returns a representation for the parameter declarations. Each node
10721 is a TREE_LIST. (See cp_parser_parameter_declaration for the exact
10722 representation.) If the parameter-declaration-clause ends with an
10723 ellipsis, PARMLIST_ELLIPSIS_P will hold of the first node in the
10724 list. A return value of NULL_TREE indicates a
10725 parameter-declaration-clause consisting only of an ellipsis. */
10728 cp_parser_parameter_declaration_clause (parser)
10735 /* Peek at the next token. */
10736 token = cp_lexer_peek_token (parser->lexer);
10737 /* Check for trivial parameter-declaration-clauses. */
10738 if (token->type == CPP_ELLIPSIS)
10740 /* Consume the `...' token. */
10741 cp_lexer_consume_token (parser->lexer);
10744 else if (token->type == CPP_CLOSE_PAREN)
10745 /* There are no parameters. */
10747 #ifndef NO_IMPLICIT_EXTERN_C
10748 if (in_system_header && current_class_type == NULL
10749 && current_lang_name == lang_name_c)
10753 return void_list_node;
10755 /* Check for `(void)', too, which is a special case. */
10756 else if (token->keyword == RID_VOID
10757 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
10758 == CPP_CLOSE_PAREN))
10760 /* Consume the `void' token. */
10761 cp_lexer_consume_token (parser->lexer);
10762 /* There are no parameters. */
10763 return void_list_node;
10766 /* Parse the parameter-declaration-list. */
10767 parameters = cp_parser_parameter_declaration_list (parser);
10768 /* If a parse error occurred while parsing the
10769 parameter-declaration-list, then the entire
10770 parameter-declaration-clause is erroneous. */
10771 if (parameters == error_mark_node)
10772 return error_mark_node;
10774 /* Peek at the next token. */
10775 token = cp_lexer_peek_token (parser->lexer);
10776 /* If it's a `,', the clause should terminate with an ellipsis. */
10777 if (token->type == CPP_COMMA)
10779 /* Consume the `,'. */
10780 cp_lexer_consume_token (parser->lexer);
10781 /* Expect an ellipsis. */
10783 = (cp_parser_require (parser, CPP_ELLIPSIS, "`...'") != NULL);
10785 /* It might also be `...' if the optional trailing `,' was
10787 else if (token->type == CPP_ELLIPSIS)
10789 /* Consume the `...' token. */
10790 cp_lexer_consume_token (parser->lexer);
10791 /* And remember that we saw it. */
10795 ellipsis_p = false;
10797 /* Finish the parameter list. */
10798 return finish_parmlist (parameters, ellipsis_p);
10801 /* Parse a parameter-declaration-list.
10803 parameter-declaration-list:
10804 parameter-declaration
10805 parameter-declaration-list , parameter-declaration
10807 Returns a representation of the parameter-declaration-list, as for
10808 cp_parser_parameter_declaration_clause. However, the
10809 `void_list_node' is never appended to the list. */
10812 cp_parser_parameter_declaration_list (parser)
10815 tree parameters = NULL_TREE;
10817 /* Look for more parameters. */
10821 /* Parse the parameter. */
10823 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/false);
10825 /* If a parse error ocurred parsing the parameter declaration,
10826 then the entire parameter-declaration-list is erroneous. */
10827 if (parameter == error_mark_node)
10829 parameters = error_mark_node;
10832 /* Add the new parameter to the list. */
10833 TREE_CHAIN (parameter) = parameters;
10834 parameters = parameter;
10836 /* Peek at the next token. */
10837 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
10838 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10839 /* The parameter-declaration-list is complete. */
10841 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
10845 /* Peek at the next token. */
10846 token = cp_lexer_peek_nth_token (parser->lexer, 2);
10847 /* If it's an ellipsis, then the list is complete. */
10848 if (token->type == CPP_ELLIPSIS)
10850 /* Otherwise, there must be more parameters. Consume the
10852 cp_lexer_consume_token (parser->lexer);
10856 cp_parser_error (parser, "expected `,' or `...'");
10861 /* We built up the list in reverse order; straighten it out now. */
10862 return nreverse (parameters);
10865 /* Parse a parameter declaration.
10867 parameter-declaration:
10868 decl-specifier-seq declarator
10869 decl-specifier-seq declarator = assignment-expression
10870 decl-specifier-seq abstract-declarator [opt]
10871 decl-specifier-seq abstract-declarator [opt] = assignment-expression
10873 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
10874 declares a template parameter. (In that case, a non-nested `>'
10875 token encountered during the parsing of the assignment-expression
10876 is not interpreted as a greater-than operator.)
10878 Returns a TREE_LIST representing the parameter-declaration. The
10879 TREE_VALUE is a representation of the decl-specifier-seq and
10880 declarator. In particular, the TREE_VALUE will be a TREE_LIST
10881 whose TREE_PURPOSE represents the decl-specifier-seq and whose
10882 TREE_VALUE represents the declarator. */
10885 cp_parser_parameter_declaration (cp_parser *parser,
10886 bool template_parm_p)
10888 bool declares_class_or_enum;
10889 bool greater_than_is_operator_p;
10890 tree decl_specifiers;
10893 tree default_argument;
10896 const char *saved_message;
10898 /* In a template parameter, `>' is not an operator.
10902 When parsing a default template-argument for a non-type
10903 template-parameter, the first non-nested `>' is taken as the end
10904 of the template parameter-list rather than a greater-than
10906 greater_than_is_operator_p = !template_parm_p;
10908 /* Type definitions may not appear in parameter types. */
10909 saved_message = parser->type_definition_forbidden_message;
10910 parser->type_definition_forbidden_message
10911 = "types may not be defined in parameter types";
10913 /* Parse the declaration-specifiers. */
10915 = cp_parser_decl_specifier_seq (parser,
10916 CP_PARSER_FLAGS_NONE,
10918 &declares_class_or_enum);
10919 /* If an error occurred, there's no reason to attempt to parse the
10920 rest of the declaration. */
10921 if (cp_parser_error_occurred (parser))
10923 parser->type_definition_forbidden_message = saved_message;
10924 return error_mark_node;
10927 /* Peek at the next token. */
10928 token = cp_lexer_peek_token (parser->lexer);
10929 /* If the next token is a `)', `,', `=', `>', or `...', then there
10930 is no declarator. */
10931 if (token->type == CPP_CLOSE_PAREN
10932 || token->type == CPP_COMMA
10933 || token->type == CPP_EQ
10934 || token->type == CPP_ELLIPSIS
10935 || token->type == CPP_GREATER)
10936 declarator = NULL_TREE;
10937 /* Otherwise, there should be a declarator. */
10940 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
10941 parser->default_arg_ok_p = false;
10943 declarator = cp_parser_declarator (parser,
10944 CP_PARSER_DECLARATOR_EITHER,
10945 /*ctor_dtor_or_conv_p=*/NULL);
10946 parser->default_arg_ok_p = saved_default_arg_ok_p;
10947 /* After the declarator, allow more attributes. */
10948 attributes = chainon (attributes, cp_parser_attributes_opt (parser));
10951 /* The restriction on defining new types applies only to the type
10952 of the parameter, not to the default argument. */
10953 parser->type_definition_forbidden_message = saved_message;
10955 /* If the next token is `=', then process a default argument. */
10956 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10958 bool saved_greater_than_is_operator_p;
10959 /* Consume the `='. */
10960 cp_lexer_consume_token (parser->lexer);
10962 /* If we are defining a class, then the tokens that make up the
10963 default argument must be saved and processed later. */
10964 if (!template_parm_p && at_class_scope_p ()
10965 && TYPE_BEING_DEFINED (current_class_type))
10967 unsigned depth = 0;
10969 /* Create a DEFAULT_ARG to represented the unparsed default
10971 default_argument = make_node (DEFAULT_ARG);
10972 DEFARG_TOKENS (default_argument) = cp_token_cache_new ();
10974 /* Add tokens until we have processed the entire default
10981 /* Peek at the next token. */
10982 token = cp_lexer_peek_token (parser->lexer);
10983 /* What we do depends on what token we have. */
10984 switch (token->type)
10986 /* In valid code, a default argument must be
10987 immediately followed by a `,' `)', or `...'. */
10989 case CPP_CLOSE_PAREN:
10991 /* If we run into a non-nested `;', `}', or `]',
10992 then the code is invalid -- but the default
10993 argument is certainly over. */
10994 case CPP_SEMICOLON:
10995 case CPP_CLOSE_BRACE:
10996 case CPP_CLOSE_SQUARE:
10999 /* Update DEPTH, if necessary. */
11000 else if (token->type == CPP_CLOSE_PAREN
11001 || token->type == CPP_CLOSE_BRACE
11002 || token->type == CPP_CLOSE_SQUARE)
11006 case CPP_OPEN_PAREN:
11007 case CPP_OPEN_SQUARE:
11008 case CPP_OPEN_BRACE:
11013 /* If we see a non-nested `>', and `>' is not an
11014 operator, then it marks the end of the default
11016 if (!depth && !greater_than_is_operator_p)
11020 /* If we run out of tokens, issue an error message. */
11022 error ("file ends in default argument");
11028 /* In these cases, we should look for template-ids.
11029 For example, if the default argument is
11030 `X<int, double>()', we need to do name lookup to
11031 figure out whether or not `X' is a template; if
11032 so, the `,' does not end the deault argument.
11034 That is not yet done. */
11041 /* If we've reached the end, stop. */
11045 /* Add the token to the token block. */
11046 token = cp_lexer_consume_token (parser->lexer);
11047 cp_token_cache_push_token (DEFARG_TOKENS (default_argument),
11051 /* Outside of a class definition, we can just parse the
11052 assignment-expression. */
11055 bool saved_local_variables_forbidden_p;
11057 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
11059 saved_greater_than_is_operator_p
11060 = parser->greater_than_is_operator_p;
11061 parser->greater_than_is_operator_p = greater_than_is_operator_p;
11062 /* Local variable names (and the `this' keyword) may not
11063 appear in a default argument. */
11064 saved_local_variables_forbidden_p
11065 = parser->local_variables_forbidden_p;
11066 parser->local_variables_forbidden_p = true;
11067 /* Parse the assignment-expression. */
11068 default_argument = cp_parser_assignment_expression (parser);
11069 /* Restore saved state. */
11070 parser->greater_than_is_operator_p
11071 = saved_greater_than_is_operator_p;
11072 parser->local_variables_forbidden_p
11073 = saved_local_variables_forbidden_p;
11075 if (!parser->default_arg_ok_p)
11077 pedwarn ("default arguments are only permitted on functions");
11078 if (flag_pedantic_errors)
11079 default_argument = NULL_TREE;
11083 default_argument = NULL_TREE;
11085 /* Create the representation of the parameter. */
11087 decl_specifiers = tree_cons (attributes, NULL_TREE, decl_specifiers);
11088 parameter = build_tree_list (default_argument,
11089 build_tree_list (decl_specifiers,
11095 /* Parse a function-definition.
11097 function-definition:
11098 decl-specifier-seq [opt] declarator ctor-initializer [opt]
11100 decl-specifier-seq [opt] declarator function-try-block
11104 function-definition:
11105 __extension__ function-definition
11107 Returns the FUNCTION_DECL for the function. If FRIEND_P is
11108 non-NULL, *FRIEND_P is set to TRUE iff the function was declared to
11112 cp_parser_function_definition (parser, friend_p)
11116 tree decl_specifiers;
11120 tree access_checks;
11122 bool declares_class_or_enum;
11124 /* The saved value of the PEDANTIC flag. */
11125 int saved_pedantic;
11127 /* Any pending qualification must be cleared by our caller. It is
11128 more robust to force the callers to clear PARSER->SCOPE than to
11129 do it here since if the qualification is in effect here, it might
11130 also end up in effect elsewhere that it is not intended. */
11131 my_friendly_assert (!parser->scope, 20010821);
11133 /* Handle `__extension__'. */
11134 if (cp_parser_extension_opt (parser, &saved_pedantic))
11136 /* Parse the function-definition. */
11137 fn = cp_parser_function_definition (parser, friend_p);
11138 /* Restore the PEDANTIC flag. */
11139 pedantic = saved_pedantic;
11144 /* Check to see if this definition appears in a class-specifier. */
11145 member_p = (at_class_scope_p ()
11146 && TYPE_BEING_DEFINED (current_class_type));
11147 /* Defer access checks in the decl-specifier-seq until we know what
11148 function is being defined. There is no need to do this for the
11149 definition of member functions; we cannot be defining a member
11150 from another class. */
11152 cp_parser_start_deferring_access_checks (parser);
11153 /* Parse the decl-specifier-seq. */
11155 = cp_parser_decl_specifier_seq (parser,
11156 CP_PARSER_FLAGS_OPTIONAL,
11158 &declares_class_or_enum);
11159 /* Figure out whether this declaration is a `friend'. */
11161 *friend_p = cp_parser_friend_p (decl_specifiers);
11163 /* Parse the declarator. */
11164 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
11165 /*ctor_dtor_or_conv_p=*/NULL);
11167 /* Gather up any access checks that occurred. */
11169 access_checks = cp_parser_stop_deferring_access_checks (parser);
11171 access_checks = NULL_TREE;
11173 /* If something has already gone wrong, we may as well stop now. */
11174 if (declarator == error_mark_node)
11176 /* Skip to the end of the function, or if this wasn't anything
11177 like a function-definition, to a `;' in the hopes of finding
11178 a sensible place from which to continue parsing. */
11179 cp_parser_skip_to_end_of_block_or_statement (parser);
11180 return error_mark_node;
11183 /* The next character should be a `{' (for a simple function
11184 definition), a `:' (for a ctor-initializer), or `try' (for a
11185 function-try block). */
11186 token = cp_lexer_peek_token (parser->lexer);
11187 if (!cp_parser_token_starts_function_definition_p (token))
11189 /* Issue the error-message. */
11190 cp_parser_error (parser, "expected function-definition");
11191 /* Skip to the next `;'. */
11192 cp_parser_skip_to_end_of_block_or_statement (parser);
11194 return error_mark_node;
11197 /* If we are in a class scope, then we must handle
11198 function-definitions specially. In particular, we save away the
11199 tokens that make up the function body, and parse them again
11200 later, in order to handle code like:
11203 int f () { return i; }
11207 Here, we cannot parse the body of `f' until after we have seen
11208 the declaration of `i'. */
11211 cp_token_cache *cache;
11213 /* Create the function-declaration. */
11214 fn = start_method (decl_specifiers, declarator, attributes);
11215 /* If something went badly wrong, bail out now. */
11216 if (fn == error_mark_node)
11218 /* If there's a function-body, skip it. */
11219 if (cp_parser_token_starts_function_definition_p
11220 (cp_lexer_peek_token (parser->lexer)))
11221 cp_parser_skip_to_end_of_block_or_statement (parser);
11222 return error_mark_node;
11225 /* Create a token cache. */
11226 cache = cp_token_cache_new ();
11227 /* Save away the tokens that make up the body of the
11229 cp_parser_cache_group (parser, cache, CPP_CLOSE_BRACE, /*depth=*/0);
11230 /* Handle function try blocks. */
11231 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
11232 cp_parser_cache_group (parser, cache, CPP_CLOSE_BRACE, /*depth=*/0);
11234 /* Save away the inline definition; we will process it when the
11235 class is complete. */
11236 DECL_PENDING_INLINE_INFO (fn) = cache;
11237 DECL_PENDING_INLINE_P (fn) = 1;
11239 /* We're done with the inline definition. */
11240 finish_method (fn);
11242 /* Add FN to the queue of functions to be parsed later. */
11243 TREE_VALUE (parser->unparsed_functions_queues)
11244 = tree_cons (NULL_TREE, fn,
11245 TREE_VALUE (parser->unparsed_functions_queues));
11250 /* Check that the number of template-parameter-lists is OK. */
11251 if (!cp_parser_check_declarator_template_parameters (parser,
11254 cp_parser_skip_to_end_of_block_or_statement (parser);
11255 return error_mark_node;
11258 return (cp_parser_function_definition_from_specifiers_and_declarator
11259 (parser, decl_specifiers, attributes, declarator, access_checks));
11262 /* Parse a function-body.
11265 compound_statement */
11268 cp_parser_function_body (cp_parser *parser)
11270 cp_parser_compound_statement (parser);
11273 /* Parse a ctor-initializer-opt followed by a function-body. Return
11274 true if a ctor-initializer was present. */
11277 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
11280 bool ctor_initializer_p;
11282 /* Begin the function body. */
11283 body = begin_function_body ();
11284 /* Parse the optional ctor-initializer. */
11285 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
11286 /* Parse the function-body. */
11287 cp_parser_function_body (parser);
11288 /* Finish the function body. */
11289 finish_function_body (body);
11291 return ctor_initializer_p;
11294 /* Parse an initializer.
11297 = initializer-clause
11298 ( expression-list )
11300 Returns a expression representing the initializer. If no
11301 initializer is present, NULL_TREE is returned.
11303 *IS_PARENTHESIZED_INIT is set to TRUE if the `( expression-list )'
11304 production is used, and zero otherwise. *IS_PARENTHESIZED_INIT is
11305 set to FALSE if there is no initializer present. */
11308 cp_parser_initializer (parser, is_parenthesized_init)
11310 bool *is_parenthesized_init;
11315 /* Peek at the next token. */
11316 token = cp_lexer_peek_token (parser->lexer);
11318 /* Let our caller know whether or not this initializer was
11320 *is_parenthesized_init = (token->type == CPP_OPEN_PAREN);
11322 if (token->type == CPP_EQ)
11324 /* Consume the `='. */
11325 cp_lexer_consume_token (parser->lexer);
11326 /* Parse the initializer-clause. */
11327 init = cp_parser_initializer_clause (parser);
11329 else if (token->type == CPP_OPEN_PAREN)
11331 /* Consume the `('. */
11332 cp_lexer_consume_token (parser->lexer);
11333 /* Parse the expression-list. */
11334 init = cp_parser_expression_list (parser);
11335 /* Consume the `)' token. */
11336 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
11337 cp_parser_skip_to_closing_parenthesis (parser);
11341 /* Anything else is an error. */
11342 cp_parser_error (parser, "expected initializer");
11343 init = error_mark_node;
11349 /* Parse an initializer-clause.
11351 initializer-clause:
11352 assignment-expression
11353 { initializer-list , [opt] }
11356 Returns an expression representing the initializer.
11358 If the `assignment-expression' production is used the value
11359 returned is simply a reprsentation for the expression.
11361 Otherwise, a CONSTRUCTOR is returned. The CONSTRUCTOR_ELTS will be
11362 the elements of the initializer-list (or NULL_TREE, if the last
11363 production is used). The TREE_TYPE for the CONSTRUCTOR will be
11364 NULL_TREE. There is no way to detect whether or not the optional
11365 trailing `,' was provided. */
11368 cp_parser_initializer_clause (parser)
11373 /* If it is not a `{', then we are looking at an
11374 assignment-expression. */
11375 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
11376 initializer = cp_parser_assignment_expression (parser);
11379 /* Consume the `{' token. */
11380 cp_lexer_consume_token (parser->lexer);
11381 /* Create a CONSTRUCTOR to represent the braced-initializer. */
11382 initializer = make_node (CONSTRUCTOR);
11383 /* Mark it with TREE_HAS_CONSTRUCTOR. This should not be
11384 necessary, but check_initializer depends upon it, for
11386 TREE_HAS_CONSTRUCTOR (initializer) = 1;
11387 /* If it's not a `}', then there is a non-trivial initializer. */
11388 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
11390 /* Parse the initializer list. */
11391 CONSTRUCTOR_ELTS (initializer)
11392 = cp_parser_initializer_list (parser);
11393 /* A trailing `,' token is allowed. */
11394 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
11395 cp_lexer_consume_token (parser->lexer);
11398 /* Now, there should be a trailing `}'. */
11399 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
11402 return initializer;
11405 /* Parse an initializer-list.
11409 initializer-list , initializer-clause
11414 identifier : initializer-clause
11415 initializer-list, identifier : initializer-clause
11417 Returns a TREE_LIST. The TREE_VALUE of each node is an expression
11418 for the initializer. If the TREE_PURPOSE is non-NULL, it is the
11419 IDENTIFIER_NODE naming the field to initialize. */
11422 cp_parser_initializer_list (parser)
11425 tree initializers = NULL_TREE;
11427 /* Parse the rest of the list. */
11434 /* If the next token is an identifier and the following one is a
11435 colon, we are looking at the GNU designated-initializer
11437 if (cp_parser_allow_gnu_extensions_p (parser)
11438 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
11439 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
11441 /* Consume the identifier. */
11442 identifier = cp_lexer_consume_token (parser->lexer)->value;
11443 /* Consume the `:'. */
11444 cp_lexer_consume_token (parser->lexer);
11447 identifier = NULL_TREE;
11449 /* Parse the initializer. */
11450 initializer = cp_parser_initializer_clause (parser);
11452 /* Add it to the list. */
11453 initializers = tree_cons (identifier, initializer, initializers);
11455 /* If the next token is not a comma, we have reached the end of
11457 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11460 /* Peek at the next token. */
11461 token = cp_lexer_peek_nth_token (parser->lexer, 2);
11462 /* If the next token is a `}', then we're still done. An
11463 initializer-clause can have a trailing `,' after the
11464 initializer-list and before the closing `}'. */
11465 if (token->type == CPP_CLOSE_BRACE)
11468 /* Consume the `,' token. */
11469 cp_lexer_consume_token (parser->lexer);
11472 /* The initializers were built up in reverse order, so we need to
11473 reverse them now. */
11474 return nreverse (initializers);
11477 /* Classes [gram.class] */
11479 /* Parse a class-name.
11485 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
11486 to indicate that names looked up in dependent types should be
11487 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
11488 keyword has been used to indicate that the name that appears next
11489 is a template. TYPE_P is true iff the next name should be treated
11490 as class-name, even if it is declared to be some other kind of name
11491 as well. The accessibility of the class-name is checked iff
11492 CHECK_ACCESS_P is true. If CHECK_DEPENDENCY_P is FALSE, names are
11493 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
11494 is the class being defined in a class-head.
11496 Returns the TYPE_DECL representing the class. */
11499 cp_parser_class_name (cp_parser *parser,
11500 bool typename_keyword_p,
11501 bool template_keyword_p,
11503 bool check_access_p,
11504 bool check_dependency_p,
11512 /* All class-names start with an identifier. */
11513 token = cp_lexer_peek_token (parser->lexer);
11514 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
11516 cp_parser_error (parser, "expected class-name");
11517 return error_mark_node;
11520 /* PARSER->SCOPE can be cleared when parsing the template-arguments
11521 to a template-id, so we save it here. */
11522 scope = parser->scope;
11523 /* Any name names a type if we're following the `typename' keyword
11524 in a qualified name where the enclosing scope is type-dependent. */
11525 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
11526 && cp_parser_dependent_type_p (scope));
11527 /* Handle the common case (an identifier, but not a template-id)
11529 if (token->type == CPP_NAME
11530 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_LESS)
11534 /* Look for the identifier. */
11535 identifier = cp_parser_identifier (parser);
11536 /* If the next token isn't an identifier, we are certainly not
11537 looking at a class-name. */
11538 if (identifier == error_mark_node)
11539 decl = error_mark_node;
11540 /* If we know this is a type-name, there's no need to look it
11542 else if (typename_p)
11546 /* If the next token is a `::', then the name must be a type
11549 [basic.lookup.qual]
11551 During the lookup for a name preceding the :: scope
11552 resolution operator, object, function, and enumerator
11553 names are ignored. */
11554 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
11556 /* Look up the name. */
11557 decl = cp_parser_lookup_name (parser, identifier,
11560 /*is_namespace=*/false,
11561 check_dependency_p);
11566 /* Try a template-id. */
11567 decl = cp_parser_template_id (parser, template_keyword_p,
11568 check_dependency_p);
11569 if (decl == error_mark_node)
11570 return error_mark_node;
11573 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
11575 /* If this is a typename, create a TYPENAME_TYPE. */
11576 if (typename_p && decl != error_mark_node)
11577 decl = TYPE_NAME (make_typename_type (scope, decl,
11580 /* Check to see that it is really the name of a class. */
11581 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
11582 && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
11583 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
11584 /* Situations like this:
11586 template <typename T> struct A {
11587 typename T::template X<int>::I i;
11590 are problematic. Is `T::template X<int>' a class-name? The
11591 standard does not seem to be definitive, but there is no other
11592 valid interpretation of the following `::'. Therefore, those
11593 names are considered class-names. */
11594 decl = TYPE_NAME (make_typename_type (scope, decl,
11595 tf_error | tf_parsing));
11596 else if (decl == error_mark_node
11597 || TREE_CODE (decl) != TYPE_DECL
11598 || !IS_AGGR_TYPE (TREE_TYPE (decl)))
11600 cp_parser_error (parser, "expected class-name");
11601 return error_mark_node;
11607 /* Parse a class-specifier.
11610 class-head { member-specification [opt] }
11612 Returns the TREE_TYPE representing the class. */
11615 cp_parser_class_specifier (parser)
11620 tree attributes = NULL_TREE;
11621 int has_trailing_semicolon;
11622 bool nested_name_specifier_p;
11623 bool deferring_access_checks_p;
11624 tree saved_access_checks;
11625 unsigned saved_num_template_parameter_lists;
11627 /* Parse the class-head. */
11628 type = cp_parser_class_head (parser,
11629 &nested_name_specifier_p,
11630 &deferring_access_checks_p,
11631 &saved_access_checks);
11632 /* If the class-head was a semantic disaster, skip the entire body
11636 cp_parser_skip_to_end_of_block_or_statement (parser);
11637 return error_mark_node;
11639 /* Look for the `{'. */
11640 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
11641 return error_mark_node;
11642 /* Issue an error message if type-definitions are forbidden here. */
11643 cp_parser_check_type_definition (parser);
11644 /* Remember that we are defining one more class. */
11645 ++parser->num_classes_being_defined;
11646 /* Inside the class, surrounding template-parameter-lists do not
11648 saved_num_template_parameter_lists
11649 = parser->num_template_parameter_lists;
11650 parser->num_template_parameter_lists = 0;
11651 /* Start the class. */
11652 type = begin_class_definition (type);
11653 if (type == error_mark_node)
11654 /* If the type is erroneous, skip the entire body of the class. */
11655 cp_parser_skip_to_closing_brace (parser);
11657 /* Parse the member-specification. */
11658 cp_parser_member_specification_opt (parser);
11659 /* Look for the trailing `}'. */
11660 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
11661 /* We get better error messages by noticing a common problem: a
11662 missing trailing `;'. */
11663 token = cp_lexer_peek_token (parser->lexer);
11664 has_trailing_semicolon = (token->type == CPP_SEMICOLON);
11665 /* Look for attributes to apply to this class. */
11666 if (cp_parser_allow_gnu_extensions_p (parser))
11667 attributes = cp_parser_attributes_opt (parser);
11668 /* Finish the class definition. */
11669 type = finish_class_definition (type,
11671 has_trailing_semicolon,
11672 nested_name_specifier_p);
11673 /* If this class is not itself within the scope of another class,
11674 then we need to parse the bodies of all of the queued function
11675 definitions. Note that the queued functions defined in a class
11676 are not always processed immediately following the
11677 class-specifier for that class. Consider:
11680 struct B { void f() { sizeof (A); } };
11683 If `f' were processed before the processing of `A' were
11684 completed, there would be no way to compute the size of `A'.
11685 Note that the nesting we are interested in here is lexical --
11686 not the semantic nesting given by TYPE_CONTEXT. In particular,
11689 struct A { struct B; };
11690 struct A::B { void f() { } };
11692 there is no need to delay the parsing of `A::B::f'. */
11693 if (--parser->num_classes_being_defined == 0)
11695 tree last_scope = NULL_TREE;
11699 /* Reverse the queue, so that we process it in the order the
11700 functions were declared. */
11701 TREE_VALUE (parser->unparsed_functions_queues)
11702 = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
11703 /* In a first pass, parse default arguments to the functions.
11704 Then, in a second pass, parse the bodies of the functions.
11705 This two-phased approach handles cases like:
11713 for (queue_entry = TREE_VALUE (parser->unparsed_functions_queues);
11715 queue_entry = TREE_CHAIN (queue_entry))
11717 fn = TREE_VALUE (queue_entry);
11718 if (DECL_FUNCTION_TEMPLATE_P (fn))
11719 fn = DECL_TEMPLATE_RESULT (fn);
11720 /* Make sure that any template parameters are in scope. */
11721 maybe_begin_member_template_processing (fn);
11722 /* If there are default arguments that have not yet been processed,
11723 take care of them now. */
11724 cp_parser_late_parsing_default_args (parser, fn);
11725 /* Remove any template parameters from the symbol table. */
11726 maybe_end_member_template_processing ();
11728 /* Now parse the body of the functions. */
11729 while (TREE_VALUE (parser->unparsed_functions_queues))
11732 /* Figure out which function we need to process. */
11733 queue_entry = TREE_VALUE (parser->unparsed_functions_queues);
11734 fn = TREE_VALUE (queue_entry);
11736 /* Parse the function. */
11737 cp_parser_late_parsing_for_member (parser, fn);
11739 TREE_VALUE (parser->unparsed_functions_queues)
11740 = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues));
11743 /* If LAST_SCOPE is non-NULL, then we have pushed scopes one
11744 more time than we have popped, so me must pop here. */
11746 pop_scope (last_scope);
11749 /* Put back any saved access checks. */
11750 if (deferring_access_checks_p)
11752 cp_parser_start_deferring_access_checks (parser);
11753 parser->context->deferred_access_checks = saved_access_checks;
11756 /* Restore the count of active template-parameter-lists. */
11757 parser->num_template_parameter_lists
11758 = saved_num_template_parameter_lists;
11763 /* Parse a class-head.
11766 class-key identifier [opt] base-clause [opt]
11767 class-key nested-name-specifier identifier base-clause [opt]
11768 class-key nested-name-specifier [opt] template-id
11772 class-key attributes identifier [opt] base-clause [opt]
11773 class-key attributes nested-name-specifier identifier base-clause [opt]
11774 class-key attributes nested-name-specifier [opt] template-id
11777 Returns the TYPE of the indicated class. Sets
11778 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
11779 involving a nested-name-specifier was used, and FALSE otherwise.
11780 Sets *DEFERRING_ACCESS_CHECKS_P to TRUE iff we were deferring
11781 access checks before this class-head. In that case,
11782 *SAVED_ACCESS_CHECKS is set to the current list of deferred access
11785 Returns NULL_TREE if the class-head is syntactically valid, but
11786 semantically invalid in a way that means we should skip the entire
11787 body of the class. */
11790 cp_parser_class_head (parser,
11791 nested_name_specifier_p,
11792 deferring_access_checks_p,
11793 saved_access_checks)
11795 bool *nested_name_specifier_p;
11796 bool *deferring_access_checks_p;
11797 tree *saved_access_checks;
11800 tree nested_name_specifier;
11801 enum tag_types class_key;
11802 tree id = NULL_TREE;
11803 tree type = NULL_TREE;
11805 bool template_id_p = false;
11806 bool qualified_p = false;
11807 bool invalid_nested_name_p = false;
11808 unsigned num_templates;
11810 /* Assume no nested-name-specifier will be present. */
11811 *nested_name_specifier_p = false;
11812 /* Assume no template parameter lists will be used in defining the
11816 /* Look for the class-key. */
11817 class_key = cp_parser_class_key (parser);
11818 if (class_key == none_type)
11819 return error_mark_node;
11821 /* Parse the attributes. */
11822 attributes = cp_parser_attributes_opt (parser);
11824 /* If the next token is `::', that is invalid -- but sometimes
11825 people do try to write:
11829 Handle this gracefully by accepting the extra qualifier, and then
11830 issuing an error about it later if this really is a
11831 class-header. If it turns out just to be an elaborated type
11832 specifier, remain silent. */
11833 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
11834 qualified_p = true;
11836 /* Determine the name of the class. Begin by looking for an
11837 optional nested-name-specifier. */
11838 nested_name_specifier
11839 = cp_parser_nested_name_specifier_opt (parser,
11840 /*typename_keyword_p=*/false,
11841 /*check_dependency_p=*/true,
11843 /* If there was a nested-name-specifier, then there *must* be an
11845 if (nested_name_specifier)
11847 /* Although the grammar says `identifier', it really means
11848 `class-name' or `template-name'. You are only allowed to
11849 define a class that has already been declared with this
11852 The proposed resolution for Core Issue 180 says that whever
11853 you see `class T::X' you should treat `X' as a type-name.
11855 It is OK to define an inaccessible class; for example:
11857 class A { class B; };
11860 So, we ask cp_parser_class_name not to check accessibility.
11862 We do not know if we will see a class-name, or a
11863 template-name. We look for a class-name first, in case the
11864 class-name is a template-id; if we looked for the
11865 template-name first we would stop after the template-name. */
11866 cp_parser_parse_tentatively (parser);
11867 type = cp_parser_class_name (parser,
11868 /*typename_keyword_p=*/false,
11869 /*template_keyword_p=*/false,
11871 /*check_access_p=*/false,
11872 /*check_dependency_p=*/false,
11873 /*class_head_p=*/true);
11874 /* If that didn't work, ignore the nested-name-specifier. */
11875 if (!cp_parser_parse_definitely (parser))
11877 invalid_nested_name_p = true;
11878 id = cp_parser_identifier (parser);
11879 if (id == error_mark_node)
11882 /* If we could not find a corresponding TYPE, treat this
11883 declaration like an unqualified declaration. */
11884 if (type == error_mark_node)
11885 nested_name_specifier = NULL_TREE;
11886 /* Otherwise, count the number of templates used in TYPE and its
11887 containing scopes. */
11892 for (scope = TREE_TYPE (type);
11893 scope && TREE_CODE (scope) != NAMESPACE_DECL;
11894 scope = (TYPE_P (scope)
11895 ? TYPE_CONTEXT (scope)
11896 : DECL_CONTEXT (scope)))
11898 && CLASS_TYPE_P (scope)
11899 && CLASSTYPE_TEMPLATE_INFO (scope)
11900 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
11904 /* Otherwise, the identifier is optional. */
11907 /* We don't know whether what comes next is a template-id,
11908 an identifier, or nothing at all. */
11909 cp_parser_parse_tentatively (parser);
11910 /* Check for a template-id. */
11911 id = cp_parser_template_id (parser,
11912 /*template_keyword_p=*/false,
11913 /*check_dependency_p=*/true);
11914 /* If that didn't work, it could still be an identifier. */
11915 if (!cp_parser_parse_definitely (parser))
11917 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
11918 id = cp_parser_identifier (parser);
11924 template_id_p = true;
11929 /* If it's not a `:' or a `{' then we can't really be looking at a
11930 class-head, since a class-head only appears as part of a
11931 class-specifier. We have to detect this situation before calling
11932 xref_tag, since that has irreversible side-effects. */
11933 if (!cp_parser_next_token_starts_class_definition_p (parser))
11935 cp_parser_error (parser, "expected `{' or `:'");
11936 return error_mark_node;
11939 /* At this point, we're going ahead with the class-specifier, even
11940 if some other problem occurs. */
11941 cp_parser_commit_to_tentative_parse (parser);
11942 /* Issue the error about the overly-qualified name now. */
11944 cp_parser_error (parser,
11945 "global qualification of class name is invalid");
11946 else if (invalid_nested_name_p)
11947 cp_parser_error (parser,
11948 "qualified name does not name a class");
11949 /* Make sure that the right number of template parameters were
11951 if (!cp_parser_check_template_parameters (parser, num_templates))
11952 /* If something went wrong, there is no point in even trying to
11953 process the class-definition. */
11956 /* We do not need to defer access checks for entities declared
11957 within the class. But, we do need to save any access checks that
11958 are currently deferred and restore them later, in case we are in
11959 the middle of something else. */
11960 *deferring_access_checks_p = parser->context->deferring_access_checks_p;
11961 if (*deferring_access_checks_p)
11962 *saved_access_checks = cp_parser_stop_deferring_access_checks (parser);
11964 /* Look up the type. */
11967 type = TREE_TYPE (id);
11968 maybe_process_partial_specialization (type);
11970 else if (!nested_name_specifier)
11972 /* If the class was unnamed, create a dummy name. */
11974 id = make_anon_name ();
11975 type = xref_tag (class_key, id, attributes, /*globalize=*/0);
11984 template <typename T> struct S { struct T };
11985 template <typename T> struct S::T { };
11987 we will get a TYPENAME_TYPE when processing the definition of
11988 `S::T'. We need to resolve it to the actual type before we
11989 try to define it. */
11990 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
11992 type = cp_parser_resolve_typename_type (parser, TREE_TYPE (type));
11993 if (type != error_mark_node)
11994 type = TYPE_NAME (type);
11997 maybe_process_partial_specialization (TREE_TYPE (type));
11998 class_type = current_class_type;
11999 type = TREE_TYPE (handle_class_head (class_key,
12000 nested_name_specifier,
12005 if (type != error_mark_node)
12007 if (!class_type && TYPE_CONTEXT (type))
12008 *nested_name_specifier_p = true;
12009 else if (class_type && !same_type_p (TYPE_CONTEXT (type),
12011 *nested_name_specifier_p = true;
12014 /* Indicate whether this class was declared as a `class' or as a
12016 if (TREE_CODE (type) == RECORD_TYPE)
12017 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
12018 cp_parser_check_class_key (class_key, type);
12020 /* Enter the scope containing the class; the names of base classes
12021 should be looked up in that context. For example, given:
12023 struct A { struct B {}; struct C; };
12024 struct A::C : B {};
12027 if (nested_name_specifier)
12028 push_scope (nested_name_specifier);
12029 /* Now, look for the base-clause. */
12030 token = cp_lexer_peek_token (parser->lexer);
12031 if (token->type == CPP_COLON)
12035 /* Get the list of base-classes. */
12036 bases = cp_parser_base_clause (parser);
12037 /* Process them. */
12038 xref_basetypes (type, bases);
12040 /* Leave the scope given by the nested-name-specifier. We will
12041 enter the class scope itself while processing the members. */
12042 if (nested_name_specifier)
12043 pop_scope (nested_name_specifier);
12048 /* Parse a class-key.
12055 Returns the kind of class-key specified, or none_type to indicate
12058 static enum tag_types
12059 cp_parser_class_key (parser)
12063 enum tag_types tag_type;
12065 /* Look for the class-key. */
12066 token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
12070 /* Check to see if the TOKEN is a class-key. */
12071 tag_type = cp_parser_token_is_class_key (token);
12073 cp_parser_error (parser, "expected class-key");
12077 /* Parse an (optional) member-specification.
12079 member-specification:
12080 member-declaration member-specification [opt]
12081 access-specifier : member-specification [opt] */
12084 cp_parser_member_specification_opt (parser)
12092 /* Peek at the next token. */
12093 token = cp_lexer_peek_token (parser->lexer);
12094 /* If it's a `}', or EOF then we've seen all the members. */
12095 if (token->type == CPP_CLOSE_BRACE || token->type == CPP_EOF)
12098 /* See if this token is a keyword. */
12099 keyword = token->keyword;
12103 case RID_PROTECTED:
12105 /* Consume the access-specifier. */
12106 cp_lexer_consume_token (parser->lexer);
12107 /* Remember which access-specifier is active. */
12108 current_access_specifier = token->value;
12109 /* Look for the `:'. */
12110 cp_parser_require (parser, CPP_COLON, "`:'");
12114 /* Otherwise, the next construction must be a
12115 member-declaration. */
12116 cp_parser_member_declaration (parser);
12117 reset_type_access_control ();
12122 /* Parse a member-declaration.
12124 member-declaration:
12125 decl-specifier-seq [opt] member-declarator-list [opt] ;
12126 function-definition ; [opt]
12127 :: [opt] nested-name-specifier template [opt] unqualified-id ;
12129 template-declaration
12131 member-declarator-list:
12133 member-declarator-list , member-declarator
12136 declarator pure-specifier [opt]
12137 declarator constant-initializer [opt]
12138 identifier [opt] : constant-expression
12142 member-declaration:
12143 __extension__ member-declaration
12146 declarator attributes [opt] pure-specifier [opt]
12147 declarator attributes [opt] constant-initializer [opt]
12148 identifier [opt] attributes [opt] : constant-expression */
12151 cp_parser_member_declaration (parser)
12154 tree decl_specifiers;
12155 tree prefix_attributes;
12157 bool declares_class_or_enum;
12160 int saved_pedantic;
12162 /* Check for the `__extension__' keyword. */
12163 if (cp_parser_extension_opt (parser, &saved_pedantic))
12166 cp_parser_member_declaration (parser);
12167 /* Restore the old value of the PEDANTIC flag. */
12168 pedantic = saved_pedantic;
12173 /* Check for a template-declaration. */
12174 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
12176 /* Parse the template-declaration. */
12177 cp_parser_template_declaration (parser, /*member_p=*/true);
12182 /* Check for a using-declaration. */
12183 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
12185 /* Parse the using-declaration. */
12186 cp_parser_using_declaration (parser);
12191 /* We can't tell whether we're looking at a declaration or a
12192 function-definition. */
12193 cp_parser_parse_tentatively (parser);
12195 /* Parse the decl-specifier-seq. */
12197 = cp_parser_decl_specifier_seq (parser,
12198 CP_PARSER_FLAGS_OPTIONAL,
12199 &prefix_attributes,
12200 &declares_class_or_enum);
12201 /* If there is no declarator, then the decl-specifier-seq should
12203 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12205 /* If there was no decl-specifier-seq, and the next token is a
12206 `;', then we have something like:
12212 Each member-declaration shall declare at least one member
12213 name of the class. */
12214 if (!decl_specifiers)
12217 pedwarn ("extra semicolon");
12223 /* See if this declaration is a friend. */
12224 friend_p = cp_parser_friend_p (decl_specifiers);
12225 /* If there were decl-specifiers, check to see if there was
12226 a class-declaration. */
12227 type = check_tag_decl (decl_specifiers);
12228 /* Nested classes have already been added to the class, but
12229 a `friend' needs to be explicitly registered. */
12232 /* If the `friend' keyword was present, the friend must
12233 be introduced with a class-key. */
12234 if (!declares_class_or_enum)
12235 error ("a class-key must be used when declaring a friend");
12238 template <typename T> struct A {
12239 friend struct A<T>::B;
12242 A<T>::B will be represented by a TYPENAME_TYPE, and
12243 therefore not recognized by check_tag_decl. */
12248 for (specifier = decl_specifiers;
12250 specifier = TREE_CHAIN (specifier))
12252 tree s = TREE_VALUE (specifier);
12254 if (TREE_CODE (s) == IDENTIFIER_NODE
12255 && IDENTIFIER_GLOBAL_VALUE (s))
12256 type = IDENTIFIER_GLOBAL_VALUE (s);
12257 if (TREE_CODE (s) == TYPE_DECL)
12267 error ("friend declaration does not name a class or "
12270 make_friend_class (current_class_type, type);
12272 /* If there is no TYPE, an error message will already have
12276 /* An anonymous aggregate has to be handled specially; such
12277 a declaration really declares a data member (with a
12278 particular type), as opposed to a nested class. */
12279 else if (ANON_AGGR_TYPE_P (type))
12281 /* Remove constructors and such from TYPE, now that we
12282 know it is an anoymous aggregate. */
12283 fixup_anonymous_aggr (type);
12284 /* And make the corresponding data member. */
12285 decl = build_decl (FIELD_DECL, NULL_TREE, type);
12286 /* Add it to the class. */
12287 finish_member_declaration (decl);
12293 /* See if these declarations will be friends. */
12294 friend_p = cp_parser_friend_p (decl_specifiers);
12296 /* Keep going until we hit the `;' at the end of the
12298 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12300 tree attributes = NULL_TREE;
12301 tree first_attribute;
12303 /* Peek at the next token. */
12304 token = cp_lexer_peek_token (parser->lexer);
12306 /* Check for a bitfield declaration. */
12307 if (token->type == CPP_COLON
12308 || (token->type == CPP_NAME
12309 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
12315 /* Get the name of the bitfield. Note that we cannot just
12316 check TOKEN here because it may have been invalidated by
12317 the call to cp_lexer_peek_nth_token above. */
12318 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
12319 identifier = cp_parser_identifier (parser);
12321 identifier = NULL_TREE;
12323 /* Consume the `:' token. */
12324 cp_lexer_consume_token (parser->lexer);
12325 /* Get the width of the bitfield. */
12326 width = cp_parser_constant_expression (parser);
12328 /* Look for attributes that apply to the bitfield. */
12329 attributes = cp_parser_attributes_opt (parser);
12330 /* Remember which attributes are prefix attributes and
12332 first_attribute = attributes;
12333 /* Combine the attributes. */
12334 attributes = chainon (prefix_attributes, attributes);
12336 /* Create the bitfield declaration. */
12337 decl = grokbitfield (identifier,
12340 /* Apply the attributes. */
12341 cplus_decl_attributes (&decl, attributes, /*flags=*/0);
12347 tree asm_specification;
12348 bool ctor_dtor_or_conv_p;
12350 /* Parse the declarator. */
12352 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
12353 &ctor_dtor_or_conv_p);
12355 /* If something went wrong parsing the declarator, make sure
12356 that we at least consume some tokens. */
12357 if (declarator == error_mark_node)
12359 /* Skip to the end of the statement. */
12360 cp_parser_skip_to_end_of_statement (parser);
12364 /* Look for an asm-specification. */
12365 asm_specification = cp_parser_asm_specification_opt (parser);
12366 /* Look for attributes that apply to the declaration. */
12367 attributes = cp_parser_attributes_opt (parser);
12368 /* Remember which attributes are prefix attributes and
12370 first_attribute = attributes;
12371 /* Combine the attributes. */
12372 attributes = chainon (prefix_attributes, attributes);
12374 /* If it's an `=', then we have a constant-initializer or a
12375 pure-specifier. It is not correct to parse the
12376 initializer before registering the member declaration
12377 since the member declaration should be in scope while
12378 its initializer is processed. However, the rest of the
12379 front end does not yet provide an interface that allows
12380 us to handle this correctly. */
12381 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12385 A pure-specifier shall be used only in the declaration of
12386 a virtual function.
12388 A member-declarator can contain a constant-initializer
12389 only if it declares a static member of integral or
12392 Therefore, if the DECLARATOR is for a function, we look
12393 for a pure-specifier; otherwise, we look for a
12394 constant-initializer. When we call `grokfield', it will
12395 perform more stringent semantics checks. */
12396 if (TREE_CODE (declarator) == CALL_EXPR)
12397 initializer = cp_parser_pure_specifier (parser);
12400 /* This declaration cannot be a function
12402 cp_parser_commit_to_tentative_parse (parser);
12403 /* Parse the initializer. */
12404 initializer = cp_parser_constant_initializer (parser);
12407 /* Otherwise, there is no initializer. */
12409 initializer = NULL_TREE;
12411 /* See if we are probably looking at a function
12412 definition. We are certainly not looking at at a
12413 member-declarator. Calling `grokfield' has
12414 side-effects, so we must not do it unless we are sure
12415 that we are looking at a member-declarator. */
12416 if (cp_parser_token_starts_function_definition_p
12417 (cp_lexer_peek_token (parser->lexer)))
12418 decl = error_mark_node;
12420 /* Create the declaration. */
12421 decl = grokfield (declarator,
12428 /* Reset PREFIX_ATTRIBUTES. */
12429 while (attributes && TREE_CHAIN (attributes) != first_attribute)
12430 attributes = TREE_CHAIN (attributes);
12432 TREE_CHAIN (attributes) = NULL_TREE;
12434 /* If there is any qualification still in effect, clear it
12435 now; we will be starting fresh with the next declarator. */
12436 parser->scope = NULL_TREE;
12437 parser->qualifying_scope = NULL_TREE;
12438 parser->object_scope = NULL_TREE;
12439 /* If it's a `,', then there are more declarators. */
12440 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
12441 cp_lexer_consume_token (parser->lexer);
12442 /* If the next token isn't a `;', then we have a parse error. */
12443 else if (cp_lexer_next_token_is_not (parser->lexer,
12446 cp_parser_error (parser, "expected `;'");
12447 /* Skip tokens until we find a `;' */
12448 cp_parser_skip_to_end_of_statement (parser);
12455 /* Add DECL to the list of members. */
12457 finish_member_declaration (decl);
12459 /* If DECL is a function, we must return
12460 to parse it later. (Even though there is no definition,
12461 there might be default arguments that need handling.) */
12462 if (TREE_CODE (decl) == FUNCTION_DECL)
12463 TREE_VALUE (parser->unparsed_functions_queues)
12464 = tree_cons (NULL_TREE, decl,
12465 TREE_VALUE (parser->unparsed_functions_queues));
12470 /* If everything went well, look for the `;'. */
12471 if (cp_parser_parse_definitely (parser))
12473 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
12477 /* Parse the function-definition. */
12478 decl = cp_parser_function_definition (parser, &friend_p);
12479 /* If the member was not a friend, declare it here. */
12481 finish_member_declaration (decl);
12482 /* Peek at the next token. */
12483 token = cp_lexer_peek_token (parser->lexer);
12484 /* If the next token is a semicolon, consume it. */
12485 if (token->type == CPP_SEMICOLON)
12486 cp_lexer_consume_token (parser->lexer);
12489 /* Parse a pure-specifier.
12494 Returns INTEGER_ZERO_NODE if a pure specifier is found.
12495 Otherwiser, ERROR_MARK_NODE is returned. */
12498 cp_parser_pure_specifier (parser)
12503 /* Look for the `=' token. */
12504 if (!cp_parser_require (parser, CPP_EQ, "`='"))
12505 return error_mark_node;
12506 /* Look for the `0' token. */
12507 token = cp_parser_require (parser, CPP_NUMBER, "`0'");
12508 /* Unfortunately, this will accept `0L' and `0x00' as well. We need
12509 to get information from the lexer about how the number was
12510 spelled in order to fix this problem. */
12511 if (!token || !integer_zerop (token->value))
12512 return error_mark_node;
12514 return integer_zero_node;
12517 /* Parse a constant-initializer.
12519 constant-initializer:
12520 = constant-expression
12522 Returns a representation of the constant-expression. */
12525 cp_parser_constant_initializer (parser)
12528 /* Look for the `=' token. */
12529 if (!cp_parser_require (parser, CPP_EQ, "`='"))
12530 return error_mark_node;
12532 /* It is invalid to write:
12534 struct S { static const int i = { 7 }; };
12537 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12539 cp_parser_error (parser,
12540 "a brace-enclosed initializer is not allowed here");
12541 /* Consume the opening brace. */
12542 cp_lexer_consume_token (parser->lexer);
12543 /* Skip the initializer. */
12544 cp_parser_skip_to_closing_brace (parser);
12545 /* Look for the trailing `}'. */
12546 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
12548 return error_mark_node;
12551 return cp_parser_constant_expression (parser);
12554 /* Derived classes [gram.class.derived] */
12556 /* Parse a base-clause.
12559 : base-specifier-list
12561 base-specifier-list:
12563 base-specifier-list , base-specifier
12565 Returns a TREE_LIST representing the base-classes, in the order in
12566 which they were declared. The representation of each node is as
12567 described by cp_parser_base_specifier.
12569 In the case that no bases are specified, this function will return
12570 NULL_TREE, not ERROR_MARK_NODE. */
12573 cp_parser_base_clause (parser)
12576 tree bases = NULL_TREE;
12578 /* Look for the `:' that begins the list. */
12579 cp_parser_require (parser, CPP_COLON, "`:'");
12581 /* Scan the base-specifier-list. */
12587 /* Look for the base-specifier. */
12588 base = cp_parser_base_specifier (parser);
12589 /* Add BASE to the front of the list. */
12590 if (base != error_mark_node)
12592 TREE_CHAIN (base) = bases;
12595 /* Peek at the next token. */
12596 token = cp_lexer_peek_token (parser->lexer);
12597 /* If it's not a comma, then the list is complete. */
12598 if (token->type != CPP_COMMA)
12600 /* Consume the `,'. */
12601 cp_lexer_consume_token (parser->lexer);
12604 /* PARSER->SCOPE may still be non-NULL at this point, if the last
12605 base class had a qualified name. However, the next name that
12606 appears is certainly not qualified. */
12607 parser->scope = NULL_TREE;
12608 parser->qualifying_scope = NULL_TREE;
12609 parser->object_scope = NULL_TREE;
12611 return nreverse (bases);
12614 /* Parse a base-specifier.
12617 :: [opt] nested-name-specifier [opt] class-name
12618 virtual access-specifier [opt] :: [opt] nested-name-specifier
12620 access-specifier virtual [opt] :: [opt] nested-name-specifier
12623 Returns a TREE_LIST. The TREE_PURPOSE will be one of
12624 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
12625 indicate the specifiers provided. The TREE_VALUE will be a TYPE
12626 (or the ERROR_MARK_NODE) indicating the type that was specified. */
12629 cp_parser_base_specifier (parser)
12634 bool virtual_p = false;
12635 bool duplicate_virtual_error_issued_p = false;
12636 bool duplicate_access_error_issued_p = false;
12637 bool class_scope_p;
12638 access_kind access = ak_none;
12642 /* Process the optional `virtual' and `access-specifier'. */
12645 /* Peek at the next token. */
12646 token = cp_lexer_peek_token (parser->lexer);
12647 /* Process `virtual'. */
12648 switch (token->keyword)
12651 /* If `virtual' appears more than once, issue an error. */
12652 if (virtual_p && !duplicate_virtual_error_issued_p)
12654 cp_parser_error (parser,
12655 "`virtual' specified more than once in base-specified");
12656 duplicate_virtual_error_issued_p = true;
12661 /* Consume the `virtual' token. */
12662 cp_lexer_consume_token (parser->lexer);
12667 case RID_PROTECTED:
12669 /* If more than one access specifier appears, issue an
12671 if (access != ak_none && !duplicate_access_error_issued_p)
12673 cp_parser_error (parser,
12674 "more than one access specifier in base-specified");
12675 duplicate_access_error_issued_p = true;
12678 access = ((access_kind)
12679 tree_low_cst (ridpointers[(int) token->keyword],
12682 /* Consume the access-specifier. */
12683 cp_lexer_consume_token (parser->lexer);
12693 /* Map `virtual_p' and `access' onto one of the access
12699 access_node = access_default_node;
12702 access_node = access_public_node;
12705 access_node = access_protected_node;
12708 access_node = access_private_node;
12717 access_node = access_default_virtual_node;
12720 access_node = access_public_virtual_node;
12723 access_node = access_protected_virtual_node;
12726 access_node = access_private_virtual_node;
12732 /* Look for the optional `::' operator. */
12733 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
12734 /* Look for the nested-name-specifier. The simplest way to
12739 The keyword `typename' is not permitted in a base-specifier or
12740 mem-initializer; in these contexts a qualified name that
12741 depends on a template-parameter is implicitly assumed to be a
12744 is to pretend that we have seen the `typename' keyword at this
12746 cp_parser_nested_name_specifier_opt (parser,
12747 /*typename_keyword_p=*/true,
12748 /*check_dependency_p=*/true,
12750 /* If the base class is given by a qualified name, assume that names
12751 we see are type names or templates, as appropriate. */
12752 class_scope_p = (parser->scope && TYPE_P (parser->scope));
12753 /* Finally, look for the class-name. */
12754 type = cp_parser_class_name (parser,
12758 /*check_access=*/true,
12759 /*check_dependency_p=*/true,
12760 /*class_head_p=*/false);
12762 if (type == error_mark_node)
12763 return error_mark_node;
12765 return finish_base_specifier (access_node, TREE_TYPE (type));
12768 /* Exception handling [gram.exception] */
12770 /* Parse an (optional) exception-specification.
12772 exception-specification:
12773 throw ( type-id-list [opt] )
12775 Returns a TREE_LIST representing the exception-specification. The
12776 TREE_VALUE of each node is a type. */
12779 cp_parser_exception_specification_opt (parser)
12785 /* Peek at the next token. */
12786 token = cp_lexer_peek_token (parser->lexer);
12787 /* If it's not `throw', then there's no exception-specification. */
12788 if (!cp_parser_is_keyword (token, RID_THROW))
12791 /* Consume the `throw'. */
12792 cp_lexer_consume_token (parser->lexer);
12794 /* Look for the `('. */
12795 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
12797 /* Peek at the next token. */
12798 token = cp_lexer_peek_token (parser->lexer);
12799 /* If it's not a `)', then there is a type-id-list. */
12800 if (token->type != CPP_CLOSE_PAREN)
12802 const char *saved_message;
12804 /* Types may not be defined in an exception-specification. */
12805 saved_message = parser->type_definition_forbidden_message;
12806 parser->type_definition_forbidden_message
12807 = "types may not be defined in an exception-specification";
12808 /* Parse the type-id-list. */
12809 type_id_list = cp_parser_type_id_list (parser);
12810 /* Restore the saved message. */
12811 parser->type_definition_forbidden_message = saved_message;
12814 type_id_list = empty_except_spec;
12816 /* Look for the `)'. */
12817 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
12819 return type_id_list;
12822 /* Parse an (optional) type-id-list.
12826 type-id-list , type-id
12828 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
12829 in the order that the types were presented. */
12832 cp_parser_type_id_list (parser)
12835 tree types = NULL_TREE;
12842 /* Get the next type-id. */
12843 type = cp_parser_type_id (parser);
12844 /* Add it to the list. */
12845 types = add_exception_specifier (types, type, /*complain=*/1);
12846 /* Peek at the next token. */
12847 token = cp_lexer_peek_token (parser->lexer);
12848 /* If it is not a `,', we are done. */
12849 if (token->type != CPP_COMMA)
12851 /* Consume the `,'. */
12852 cp_lexer_consume_token (parser->lexer);
12855 return nreverse (types);
12858 /* Parse a try-block.
12861 try compound-statement handler-seq */
12864 cp_parser_try_block (parser)
12869 cp_parser_require_keyword (parser, RID_TRY, "`try'");
12870 try_block = begin_try_block ();
12871 cp_parser_compound_statement (parser);
12872 finish_try_block (try_block);
12873 cp_parser_handler_seq (parser);
12874 finish_handler_sequence (try_block);
12879 /* Parse a function-try-block.
12881 function-try-block:
12882 try ctor-initializer [opt] function-body handler-seq */
12885 cp_parser_function_try_block (parser)
12889 bool ctor_initializer_p;
12891 /* Look for the `try' keyword. */
12892 if (!cp_parser_require_keyword (parser, RID_TRY, "`try'"))
12894 /* Let the rest of the front-end know where we are. */
12895 try_block = begin_function_try_block ();
12896 /* Parse the function-body. */
12898 = cp_parser_ctor_initializer_opt_and_function_body (parser);
12899 /* We're done with the `try' part. */
12900 finish_function_try_block (try_block);
12901 /* Parse the handlers. */
12902 cp_parser_handler_seq (parser);
12903 /* We're done with the handlers. */
12904 finish_function_handler_sequence (try_block);
12906 return ctor_initializer_p;
12909 /* Parse a handler-seq.
12912 handler handler-seq [opt] */
12915 cp_parser_handler_seq (parser)
12922 /* Parse the handler. */
12923 cp_parser_handler (parser);
12924 /* Peek at the next token. */
12925 token = cp_lexer_peek_token (parser->lexer);
12926 /* If it's not `catch' then there are no more handlers. */
12927 if (!cp_parser_is_keyword (token, RID_CATCH))
12932 /* Parse a handler.
12935 catch ( exception-declaration ) compound-statement */
12938 cp_parser_handler (parser)
12944 cp_parser_require_keyword (parser, RID_CATCH, "`catch'");
12945 handler = begin_handler ();
12946 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
12947 declaration = cp_parser_exception_declaration (parser);
12948 finish_handler_parms (declaration, handler);
12949 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
12950 cp_parser_compound_statement (parser);
12951 finish_handler (handler);
12954 /* Parse an exception-declaration.
12956 exception-declaration:
12957 type-specifier-seq declarator
12958 type-specifier-seq abstract-declarator
12962 Returns a VAR_DECL for the declaration, or NULL_TREE if the
12963 ellipsis variant is used. */
12966 cp_parser_exception_declaration (parser)
12969 tree type_specifiers;
12971 const char *saved_message;
12973 /* If it's an ellipsis, it's easy to handle. */
12974 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
12976 /* Consume the `...' token. */
12977 cp_lexer_consume_token (parser->lexer);
12981 /* Types may not be defined in exception-declarations. */
12982 saved_message = parser->type_definition_forbidden_message;
12983 parser->type_definition_forbidden_message
12984 = "types may not be defined in exception-declarations";
12986 /* Parse the type-specifier-seq. */
12987 type_specifiers = cp_parser_type_specifier_seq (parser);
12988 /* If it's a `)', then there is no declarator. */
12989 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
12990 declarator = NULL_TREE;
12992 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
12993 /*ctor_dtor_or_conv_p=*/NULL);
12995 /* Restore the saved message. */
12996 parser->type_definition_forbidden_message = saved_message;
12998 return start_handler_parms (type_specifiers, declarator);
13001 /* Parse a throw-expression.
13004 throw assignment-expresion [opt]
13006 Returns a THROW_EXPR representing the throw-expression. */
13009 cp_parser_throw_expression (parser)
13014 cp_parser_require_keyword (parser, RID_THROW, "`throw'");
13015 /* We can't be sure if there is an assignment-expression or not. */
13016 cp_parser_parse_tentatively (parser);
13018 expression = cp_parser_assignment_expression (parser);
13019 /* If it didn't work, this is just a rethrow. */
13020 if (!cp_parser_parse_definitely (parser))
13021 expression = NULL_TREE;
13023 return build_throw (expression);
13026 /* GNU Extensions */
13028 /* Parse an (optional) asm-specification.
13031 asm ( string-literal )
13033 If the asm-specification is present, returns a STRING_CST
13034 corresponding to the string-literal. Otherwise, returns
13038 cp_parser_asm_specification_opt (parser)
13042 tree asm_specification;
13044 /* Peek at the next token. */
13045 token = cp_lexer_peek_token (parser->lexer);
13046 /* If the next token isn't the `asm' keyword, then there's no
13047 asm-specification. */
13048 if (!cp_parser_is_keyword (token, RID_ASM))
13051 /* Consume the `asm' token. */
13052 cp_lexer_consume_token (parser->lexer);
13053 /* Look for the `('. */
13054 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13056 /* Look for the string-literal. */
13057 token = cp_parser_require (parser, CPP_STRING, "string-literal");
13059 asm_specification = token->value;
13061 asm_specification = NULL_TREE;
13063 /* Look for the `)'. */
13064 cp_parser_require (parser, CPP_CLOSE_PAREN, "`('");
13066 return asm_specification;
13069 /* Parse an asm-operand-list.
13073 asm-operand-list , asm-operand
13076 string-literal ( expression )
13077 [ string-literal ] string-literal ( expression )
13079 Returns a TREE_LIST representing the operands. The TREE_VALUE of
13080 each node is the expression. The TREE_PURPOSE is itself a
13081 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
13082 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
13083 is a STRING_CST for the string literal before the parenthesis. */
13086 cp_parser_asm_operand_list (parser)
13089 tree asm_operands = NULL_TREE;
13093 tree string_literal;
13098 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
13100 /* Consume the `[' token. */
13101 cp_lexer_consume_token (parser->lexer);
13102 /* Read the operand name. */
13103 name = cp_parser_identifier (parser);
13104 if (name != error_mark_node)
13105 name = build_string (IDENTIFIER_LENGTH (name),
13106 IDENTIFIER_POINTER (name));
13107 /* Look for the closing `]'. */
13108 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
13112 /* Look for the string-literal. */
13113 token = cp_parser_require (parser, CPP_STRING, "string-literal");
13114 string_literal = token ? token->value : error_mark_node;
13115 /* Look for the `('. */
13116 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13117 /* Parse the expression. */
13118 expression = cp_parser_expression (parser);
13119 /* Look for the `)'. */
13120 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13121 /* Add this operand to the list. */
13122 asm_operands = tree_cons (build_tree_list (name, string_literal),
13125 /* If the next token is not a `,', there are no more
13127 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13129 /* Consume the `,'. */
13130 cp_lexer_consume_token (parser->lexer);
13133 return nreverse (asm_operands);
13136 /* Parse an asm-clobber-list.
13140 asm-clobber-list , string-literal
13142 Returns a TREE_LIST, indicating the clobbers in the order that they
13143 appeared. The TREE_VALUE of each node is a STRING_CST. */
13146 cp_parser_asm_clobber_list (parser)
13149 tree clobbers = NULL_TREE;
13154 tree string_literal;
13156 /* Look for the string literal. */
13157 token = cp_parser_require (parser, CPP_STRING, "string-literal");
13158 string_literal = token ? token->value : error_mark_node;
13159 /* Add it to the list. */
13160 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
13161 /* If the next token is not a `,', then the list is
13163 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13165 /* Consume the `,' token. */
13166 cp_lexer_consume_token (parser->lexer);
13172 /* Parse an (optional) series of attributes.
13175 attributes attribute
13178 __attribute__ (( attribute-list [opt] ))
13180 The return value is as for cp_parser_attribute_list. */
13183 cp_parser_attributes_opt (parser)
13186 tree attributes = NULL_TREE;
13191 tree attribute_list;
13193 /* Peek at the next token. */
13194 token = cp_lexer_peek_token (parser->lexer);
13195 /* If it's not `__attribute__', then we're done. */
13196 if (token->keyword != RID_ATTRIBUTE)
13199 /* Consume the `__attribute__' keyword. */
13200 cp_lexer_consume_token (parser->lexer);
13201 /* Look for the two `(' tokens. */
13202 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13203 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13205 /* Peek at the next token. */
13206 token = cp_lexer_peek_token (parser->lexer);
13207 if (token->type != CPP_CLOSE_PAREN)
13208 /* Parse the attribute-list. */
13209 attribute_list = cp_parser_attribute_list (parser);
13211 /* If the next token is a `)', then there is no attribute
13213 attribute_list = NULL;
13215 /* Look for the two `)' tokens. */
13216 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13217 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13219 /* Add these new attributes to the list. */
13220 attributes = chainon (attributes, attribute_list);
13226 /* Parse an attribute-list.
13230 attribute-list , attribute
13234 identifier ( identifier )
13235 identifier ( identifier , expression-list )
13236 identifier ( expression-list )
13238 Returns a TREE_LIST. Each node corresponds to an attribute. THe
13239 TREE_PURPOSE of each node is the identifier indicating which
13240 attribute is in use. The TREE_VALUE represents the arguments, if
13244 cp_parser_attribute_list (parser)
13247 tree attribute_list = NULL_TREE;
13255 /* Look for the identifier. We also allow keywords here; for
13256 example `__attribute__ ((const))' is legal. */
13257 token = cp_lexer_peek_token (parser->lexer);
13258 if (token->type != CPP_NAME
13259 && token->type != CPP_KEYWORD)
13260 return error_mark_node;
13261 /* Consume the token. */
13262 token = cp_lexer_consume_token (parser->lexer);
13264 /* Save away the identifier that indicates which attribute this is. */
13265 identifier = token->value;
13266 attribute = build_tree_list (identifier, NULL_TREE);
13268 /* Peek at the next token. */
13269 token = cp_lexer_peek_token (parser->lexer);
13270 /* If it's an `(', then parse the attribute arguments. */
13271 if (token->type == CPP_OPEN_PAREN)
13274 int arguments_allowed_p = 1;
13276 /* Consume the `('. */
13277 cp_lexer_consume_token (parser->lexer);
13278 /* Peek at the next token. */
13279 token = cp_lexer_peek_token (parser->lexer);
13280 /* Check to see if the next token is an identifier. */
13281 if (token->type == CPP_NAME)
13283 /* Save the identifier. */
13284 identifier = token->value;
13285 /* Consume the identifier. */
13286 cp_lexer_consume_token (parser->lexer);
13287 /* Peek at the next token. */
13288 token = cp_lexer_peek_token (parser->lexer);
13289 /* If the next token is a `,', then there are some other
13290 expressions as well. */
13291 if (token->type == CPP_COMMA)
13292 /* Consume the comma. */
13293 cp_lexer_consume_token (parser->lexer);
13295 arguments_allowed_p = 0;
13298 identifier = NULL_TREE;
13300 /* If there are arguments, parse them too. */
13301 if (arguments_allowed_p)
13302 arguments = cp_parser_expression_list (parser);
13304 arguments = NULL_TREE;
13306 /* Combine the identifier and the arguments. */
13308 arguments = tree_cons (NULL_TREE, identifier, arguments);
13310 /* Save the identifier and arguments away. */
13311 TREE_VALUE (attribute) = arguments;
13313 /* Look for the closing `)'. */
13314 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13317 /* Add this attribute to the list. */
13318 TREE_CHAIN (attribute) = attribute_list;
13319 attribute_list = attribute;
13321 /* Now, look for more attributes. */
13322 token = cp_lexer_peek_token (parser->lexer);
13323 /* If the next token isn't a `,', we're done. */
13324 if (token->type != CPP_COMMA)
13327 /* Consume the commma and keep going. */
13328 cp_lexer_consume_token (parser->lexer);
13331 /* We built up the list in reverse order. */
13332 return nreverse (attribute_list);
13335 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
13336 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
13337 current value of the PEDANTIC flag, regardless of whether or not
13338 the `__extension__' keyword is present. The caller is responsible
13339 for restoring the value of the PEDANTIC flag. */
13342 cp_parser_extension_opt (parser, saved_pedantic)
13344 int *saved_pedantic;
13346 /* Save the old value of the PEDANTIC flag. */
13347 *saved_pedantic = pedantic;
13349 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
13351 /* Consume the `__extension__' token. */
13352 cp_lexer_consume_token (parser->lexer);
13353 /* We're not being pedantic while the `__extension__' keyword is
13363 /* Parse a label declaration.
13366 __label__ label-declarator-seq ;
13368 label-declarator-seq:
13369 identifier , label-declarator-seq
13373 cp_parser_label_declaration (parser)
13376 /* Look for the `__label__' keyword. */
13377 cp_parser_require_keyword (parser, RID_LABEL, "`__label__'");
13383 /* Look for an identifier. */
13384 identifier = cp_parser_identifier (parser);
13385 /* Declare it as a lobel. */
13386 finish_label_decl (identifier);
13387 /* If the next token is a `;', stop. */
13388 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13390 /* Look for the `,' separating the label declarations. */
13391 cp_parser_require (parser, CPP_COMMA, "`,'");
13394 /* Look for the final `;'. */
13395 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
13398 /* Support Functions */
13400 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
13401 NAME should have one of the representations used for an
13402 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
13403 is returned. If PARSER->SCOPE is a dependent type, then a
13404 SCOPE_REF is returned.
13406 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
13407 returned; the name was already resolved when the TEMPLATE_ID_EXPR
13408 was formed. Abstractly, such entities should not be passed to this
13409 function, because they do not need to be looked up, but it is
13410 simpler to check for this special case here, rather than at the
13413 In cases not explicitly covered above, this function returns a
13414 DECL, OVERLOAD, or baselink representing the result of the lookup.
13415 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
13418 If CHECK_ACCESS is TRUE, then access control is performed on the
13419 declaration to which the name resolves, and an error message is
13420 issued if the declaration is inaccessible.
13422 If IS_TYPE is TRUE, bindings that do not refer to types are
13425 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
13428 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
13432 cp_parser_lookup_name (cp_parser *parser, tree name, bool check_access,
13433 bool is_type, bool is_namespace, bool check_dependency)
13436 tree object_type = parser->context->object_type;
13438 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
13439 no longer valid. Note that if we are parsing tentatively, and
13440 the parse fails, OBJECT_TYPE will be automatically restored. */
13441 parser->context->object_type = NULL_TREE;
13443 if (name == error_mark_node)
13444 return error_mark_node;
13446 /* A template-id has already been resolved; there is no lookup to
13448 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
13450 if (BASELINK_P (name))
13452 my_friendly_assert ((TREE_CODE (BASELINK_FUNCTIONS (name))
13453 == TEMPLATE_ID_EXPR),
13458 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
13459 it should already have been checked to make sure that the name
13460 used matches the type being destroyed. */
13461 if (TREE_CODE (name) == BIT_NOT_EXPR)
13465 /* Figure out to which type this destructor applies. */
13467 type = parser->scope;
13468 else if (object_type)
13469 type = object_type;
13471 type = current_class_type;
13472 /* If that's not a class type, there is no destructor. */
13473 if (!type || !CLASS_TYPE_P (type))
13474 return error_mark_node;
13475 /* If it was a class type, return the destructor. */
13476 return CLASSTYPE_DESTRUCTORS (type);
13479 /* By this point, the NAME should be an ordinary identifier. If
13480 the id-expression was a qualified name, the qualifying scope is
13481 stored in PARSER->SCOPE at this point. */
13482 my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE,
13485 /* Perform the lookup. */
13488 bool dependent_type_p;
13490 if (parser->scope == error_mark_node)
13491 return error_mark_node;
13493 /* If the SCOPE is dependent, the lookup must be deferred until
13494 the template is instantiated -- unless we are explicitly
13495 looking up names in uninstantiated templates. Even then, we
13496 cannot look up the name if the scope is not a class type; it
13497 might, for example, be a template type parameter. */
13498 dependent_type_p = (TYPE_P (parser->scope)
13499 && !(parser->in_declarator_p
13500 && currently_open_class (parser->scope))
13501 && cp_parser_dependent_type_p (parser->scope));
13502 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
13503 && dependent_type_p)
13506 decl = build_nt (SCOPE_REF, parser->scope, name);
13508 /* The resolution to Core Issue 180 says that `struct A::B'
13509 should be considered a type-name, even if `A' is
13511 decl = TYPE_NAME (make_typename_type (parser->scope,
13517 /* If PARSER->SCOPE is a dependent type, then it must be a
13518 class type, and we must not be checking dependencies;
13519 otherwise, we would have processed this lookup above. So
13520 that PARSER->SCOPE is not considered a dependent base by
13521 lookup_member, we must enter the scope here. */
13522 if (dependent_type_p)
13523 push_scope (parser->scope);
13524 /* If the PARSER->SCOPE is a a template specialization, it
13525 may be instantiated during name lookup. In that case,
13526 errors may be issued. Even if we rollback the current
13527 tentative parse, those errors are valid. */
13528 decl = lookup_qualified_name (parser->scope, name, is_type,
13530 if (dependent_type_p)
13531 pop_scope (parser->scope);
13533 parser->qualifying_scope = parser->scope;
13534 parser->object_scope = NULL_TREE;
13536 else if (object_type)
13538 tree object_decl = NULL_TREE;
13539 /* Look up the name in the scope of the OBJECT_TYPE, unless the
13540 OBJECT_TYPE is not a class. */
13541 if (CLASS_TYPE_P (object_type))
13542 /* If the OBJECT_TYPE is a template specialization, it may
13543 be instantiated during name lookup. In that case, errors
13544 may be issued. Even if we rollback the current tentative
13545 parse, those errors are valid. */
13546 object_decl = lookup_member (object_type,
13548 /*protect=*/0, is_type);
13549 /* Look it up in the enclosing context, too. */
13550 decl = lookup_name_real (name, is_type, /*nonclass=*/0,
13553 parser->object_scope = object_type;
13554 parser->qualifying_scope = NULL_TREE;
13556 decl = object_decl;
13560 decl = lookup_name_real (name, is_type, /*nonclass=*/0,
13563 parser->qualifying_scope = NULL_TREE;
13564 parser->object_scope = NULL_TREE;
13567 /* If the lookup failed, let our caller know. */
13569 || decl == error_mark_node
13570 || (TREE_CODE (decl) == FUNCTION_DECL
13571 && DECL_ANTICIPATED (decl)))
13572 return error_mark_node;
13574 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
13575 if (TREE_CODE (decl) == TREE_LIST)
13577 /* The error message we have to print is too complicated for
13578 cp_parser_error, so we incorporate its actions directly. */
13579 if (!cp_parser_simulate_error (parser))
13581 error ("reference to `%D' is ambiguous", name);
13582 print_candidates (decl);
13584 return error_mark_node;
13587 my_friendly_assert (DECL_P (decl)
13588 || TREE_CODE (decl) == OVERLOAD
13589 || TREE_CODE (decl) == SCOPE_REF
13590 || BASELINK_P (decl),
13593 /* If we have resolved the name of a member declaration, check to
13594 see if the declaration is accessible. When the name resolves to
13595 set of overloaded functions, accesibility is checked when
13596 overload resolution is done.
13598 During an explicit instantiation, access is not checked at all,
13599 as per [temp.explicit]. */
13600 if (check_access && scope_chain->check_access && DECL_P (decl))
13602 tree qualifying_type;
13604 /* Figure out the type through which DECL is being
13607 = cp_parser_scope_through_which_access_occurs (decl,
13610 if (qualifying_type)
13612 /* If we are supposed to defer access checks, just record
13613 the information for later. */
13614 if (parser->context->deferring_access_checks_p)
13615 cp_parser_defer_access_check (parser, qualifying_type, decl);
13616 /* Otherwise, check accessibility now. */
13618 enforce_access (qualifying_type, decl);
13625 /* Like cp_parser_lookup_name, but for use in the typical case where
13626 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, and CHECK_DEPENDENCY is
13630 cp_parser_lookup_name_simple (parser, name)
13634 return cp_parser_lookup_name (parser, name,
13635 /*check_access=*/true,
13637 /*is_namespace=*/false,
13638 /*check_dependency=*/true);
13641 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
13642 TYPENAME_TYPE corresponds. Note that this function peers inside
13643 uninstantiated templates and therefore should be used only in
13644 extremely limited situations. */
13647 cp_parser_resolve_typename_type (parser, type)
13655 my_friendly_assert (TREE_CODE (type) == TYPENAME_TYPE,
13658 scope = TYPE_CONTEXT (type);
13659 name = DECL_NAME (TYPE_NAME (type));
13661 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
13662 it first before we can figure out what NAME refers to. */
13663 if (TREE_CODE (scope) == TYPENAME_TYPE)
13664 scope = cp_parser_resolve_typename_type (parser, scope);
13665 /* If we don't know what SCOPE refers to, then we cannot resolve the
13667 if (scope == error_mark_node)
13668 return error_mark_node;
13669 /* If the SCOPE is a template type parameter, we have no way of
13670 resolving the name. */
13671 if (TREE_CODE (scope) == TEMPLATE_TYPE_PARM)
13673 /* Enter the SCOPE so that name lookup will be resolved as if we
13674 were in the class definition. In particular, SCOPE will no
13675 longer be considered a dependent type. */
13676 push_scope (scope);
13677 /* Look up the declaration. */
13678 decl = lookup_member (scope, name, /*protect=*/0, /*want_type=*/1);
13679 /* If all went well, we got a TYPE_DECL for a non-typename. */
13681 || TREE_CODE (decl) != TYPE_DECL
13682 || TREE_CODE (TREE_TYPE (decl)) == TYPENAME_TYPE)
13684 cp_parser_error (parser, "could not resolve typename type");
13685 type = error_mark_node;
13688 type = TREE_TYPE (decl);
13689 /* Leave the SCOPE. */
13695 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
13696 the current context, return the TYPE_DECL. If TAG_NAME_P is
13697 true, the DECL indicates the class being defined in a class-head,
13698 or declared in an elaborated-type-specifier.
13700 Otherwise, return DECL. */
13703 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
13705 /* If the DECL is a TEMPLATE_DECL for a class type, and we are in
13706 the scope of the class, then treat the TEMPLATE_DECL as a
13707 class-name. For example, in:
13709 template <class T> struct S {
13715 If the TEMPLATE_DECL is being declared as part of a class-head,
13716 the same translation occurs:
13719 template <typename T> struct B;
13722 template <typename T> struct A::B {};
13724 Similarly, in a elaborated-type-specifier:
13726 namespace N { struct X{}; }
13729 template <typename T> friend struct N::X;
13733 if (DECL_CLASS_TEMPLATE_P (decl)
13735 || (current_class_type
13736 && same_type_p (TREE_TYPE (DECL_TEMPLATE_RESULT (decl)),
13737 current_class_type))))
13738 return DECL_TEMPLATE_RESULT (decl);
13743 /* If too many, or too few, template-parameter lists apply to the
13744 declarator, issue an error message. Returns TRUE if all went well,
13745 and FALSE otherwise. */
13748 cp_parser_check_declarator_template_parameters (parser, declarator)
13752 unsigned num_templates;
13754 /* We haven't seen any classes that involve template parameters yet. */
13757 switch (TREE_CODE (declarator))
13764 tree main_declarator = TREE_OPERAND (declarator, 0);
13766 cp_parser_check_declarator_template_parameters (parser,
13775 scope = TREE_OPERAND (declarator, 0);
13776 member = TREE_OPERAND (declarator, 1);
13778 /* If this is a pointer-to-member, then we are not interested
13779 in the SCOPE, because it does not qualify the thing that is
13781 if (TREE_CODE (member) == INDIRECT_REF)
13782 return (cp_parser_check_declarator_template_parameters
13785 while (scope && CLASS_TYPE_P (scope))
13787 /* You're supposed to have one `template <...>'
13788 for every template class, but you don't need one
13789 for a full specialization. For example:
13791 template <class T> struct S{};
13792 template <> struct S<int> { void f(); };
13793 void S<int>::f () {}
13795 is correct; there shouldn't be a `template <>' for
13796 the definition of `S<int>::f'. */
13797 if (CLASSTYPE_TEMPLATE_INFO (scope)
13798 && (CLASSTYPE_TEMPLATE_INSTANTIATION (scope)
13799 || uses_template_parms (CLASSTYPE_TI_ARGS (scope)))
13800 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
13803 scope = TYPE_CONTEXT (scope);
13807 /* Fall through. */
13810 /* If the DECLARATOR has the form `X<y>' then it uses one
13811 additional level of template parameters. */
13812 if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
13815 return cp_parser_check_template_parameters (parser,
13820 /* NUM_TEMPLATES were used in the current declaration. If that is
13821 invalid, return FALSE and issue an error messages. Otherwise,
13825 cp_parser_check_template_parameters (parser, num_templates)
13827 unsigned num_templates;
13829 /* If there are more template classes than parameter lists, we have
13832 template <class T> void S<T>::R<T>::f (); */
13833 if (parser->num_template_parameter_lists < num_templates)
13835 error ("too few template-parameter-lists");
13838 /* If there are the same number of template classes and parameter
13839 lists, that's OK. */
13840 if (parser->num_template_parameter_lists == num_templates)
13842 /* If there are more, but only one more, then we are referring to a
13843 member template. That's OK too. */
13844 if (parser->num_template_parameter_lists == num_templates + 1)
13846 /* Otherwise, there are too many template parameter lists. We have
13849 template <class T> template <class U> void S::f(); */
13850 error ("too many template-parameter-lists");
13854 /* Parse a binary-expression of the general form:
13858 binary-expression <token> <expr>
13860 The TOKEN_TREE_MAP maps <token> types to <expr> codes. FN is used
13861 to parser the <expr>s. If the first production is used, then the
13862 value returned by FN is returned directly. Otherwise, a node with
13863 the indicated EXPR_TYPE is returned, with operands corresponding to
13864 the two sub-expressions. */
13867 cp_parser_binary_expression (parser, token_tree_map, fn)
13869 const cp_parser_token_tree_map token_tree_map;
13870 cp_parser_expression_fn fn;
13874 /* Parse the first expression. */
13875 lhs = (*fn) (parser);
13876 /* Now, look for more expressions. */
13880 const cp_parser_token_tree_map_node *map_node;
13883 /* Peek at the next token. */
13884 token = cp_lexer_peek_token (parser->lexer);
13885 /* If the token is `>', and that's not an operator at the
13886 moment, then we're done. */
13887 if (token->type == CPP_GREATER
13888 && !parser->greater_than_is_operator_p)
13890 /* If we find one of the tokens we want, build the correspoding
13891 tree representation. */
13892 for (map_node = token_tree_map;
13893 map_node->token_type != CPP_EOF;
13895 if (map_node->token_type == token->type)
13897 /* Consume the operator token. */
13898 cp_lexer_consume_token (parser->lexer);
13899 /* Parse the right-hand side of the expression. */
13900 rhs = (*fn) (parser);
13901 /* Build the binary tree node. */
13902 lhs = build_x_binary_op (map_node->tree_type, lhs, rhs);
13906 /* If the token wasn't one of the ones we want, we're done. */
13907 if (map_node->token_type == CPP_EOF)
13914 /* Parse an optional `::' token indicating that the following name is
13915 from the global namespace. If so, PARSER->SCOPE is set to the
13916 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
13917 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
13918 Returns the new value of PARSER->SCOPE, if the `::' token is
13919 present, and NULL_TREE otherwise. */
13922 cp_parser_global_scope_opt (parser, current_scope_valid_p)
13924 bool current_scope_valid_p;
13928 /* Peek at the next token. */
13929 token = cp_lexer_peek_token (parser->lexer);
13930 /* If we're looking at a `::' token then we're starting from the
13931 global namespace, not our current location. */
13932 if (token->type == CPP_SCOPE)
13934 /* Consume the `::' token. */
13935 cp_lexer_consume_token (parser->lexer);
13936 /* Set the SCOPE so that we know where to start the lookup. */
13937 parser->scope = global_namespace;
13938 parser->qualifying_scope = global_namespace;
13939 parser->object_scope = NULL_TREE;
13941 return parser->scope;
13943 else if (!current_scope_valid_p)
13945 parser->scope = NULL_TREE;
13946 parser->qualifying_scope = NULL_TREE;
13947 parser->object_scope = NULL_TREE;
13953 /* Returns TRUE if the upcoming token sequence is the start of a
13954 constructor declarator. If FRIEND_P is true, the declarator is
13955 preceded by the `friend' specifier. */
13958 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
13960 bool constructor_p;
13961 tree type_decl = NULL_TREE;
13962 bool nested_name_p;
13964 /* Parse tentatively; we are going to roll back all of the tokens
13966 cp_parser_parse_tentatively (parser);
13967 /* Assume that we are looking at a constructor declarator. */
13968 constructor_p = true;
13969 /* Look for the optional `::' operator. */
13970 cp_parser_global_scope_opt (parser,
13971 /*current_scope_valid_p=*/false);
13972 /* Look for the nested-name-specifier. */
13974 = (cp_parser_nested_name_specifier_opt (parser,
13975 /*typename_keyword_p=*/false,
13976 /*check_dependency_p=*/false,
13979 /* Outside of a class-specifier, there must be a
13980 nested-name-specifier. */
13981 if (!nested_name_p &&
13982 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
13984 constructor_p = false;
13985 /* If we still think that this might be a constructor-declarator,
13986 look for a class-name. */
13991 template <typename T> struct S { S(); }
13992 template <typename T> S<T>::S ();
13994 we must recognize that the nested `S' names a class.
13997 template <typename T> S<T>::S<T> ();
13999 we must recognize that the nested `S' names a template. */
14000 type_decl = cp_parser_class_name (parser,
14001 /*typename_keyword_p=*/false,
14002 /*template_keyword_p=*/false,
14004 /*check_access_p=*/false,
14005 /*check_dependency_p=*/false,
14006 /*class_head_p=*/false);
14007 /* If there was no class-name, then this is not a constructor. */
14008 constructor_p = !cp_parser_error_occurred (parser);
14010 /* If we're still considering a constructor, we have to see a `(',
14011 to begin the parameter-declaration-clause, followed by either a
14012 `)', an `...', or a decl-specifier. We need to check for a
14013 type-specifier to avoid being fooled into thinking that:
14017 is a constructor. (It is actually a function named `f' that
14018 takes one parameter (of type `int') and returns a value of type
14021 && cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
14023 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
14024 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
14025 && !cp_parser_storage_class_specifier_opt (parser))
14027 if (current_class_type
14028 && !same_type_p (current_class_type, TREE_TYPE (type_decl)))
14029 /* The constructor for one class cannot be declared inside
14031 constructor_p = false;
14036 /* Names appearing in the type-specifier should be looked up
14037 in the scope of the class. */
14038 if (current_class_type)
14042 type = TREE_TYPE (type_decl);
14043 if (TREE_CODE (type) == TYPENAME_TYPE)
14044 type = cp_parser_resolve_typename_type (parser, type);
14047 /* Look for the type-specifier. */
14048 cp_parser_type_specifier (parser,
14049 CP_PARSER_FLAGS_NONE,
14050 /*is_friend=*/false,
14051 /*is_declarator=*/true,
14052 /*declares_class_or_enum=*/NULL,
14053 /*is_cv_qualifier=*/NULL);
14054 /* Leave the scope of the class. */
14058 constructor_p = !cp_parser_error_occurred (parser);
14063 constructor_p = false;
14064 /* We did not really want to consume any tokens. */
14065 cp_parser_abort_tentative_parse (parser);
14067 return constructor_p;
14070 /* Parse the definition of the function given by the DECL_SPECIFIERS,
14071 ATTRIBUTES, and DECLARATOR. The ACCESS_CHECKS have been deferred;
14072 they must be performed once we are in the scope of the function.
14074 Returns the function defined. */
14077 cp_parser_function_definition_from_specifiers_and_declarator
14078 (parser, decl_specifiers, attributes, declarator, access_checks)
14080 tree decl_specifiers;
14083 tree access_checks;
14088 /* Begin the function-definition. */
14089 success_p = begin_function_definition (decl_specifiers,
14093 /* If there were names looked up in the decl-specifier-seq that we
14094 did not check, check them now. We must wait until we are in the
14095 scope of the function to perform the checks, since the function
14096 might be a friend. */
14097 cp_parser_perform_deferred_access_checks (access_checks);
14101 /* If begin_function_definition didn't like the definition, skip
14102 the entire function. */
14103 error ("invalid function declaration");
14104 cp_parser_skip_to_end_of_block_or_statement (parser);
14105 fn = error_mark_node;
14108 fn = cp_parser_function_definition_after_declarator (parser,
14109 /*inline_p=*/false);
14114 /* Parse the part of a function-definition that follows the
14115 declarator. INLINE_P is TRUE iff this function is an inline
14116 function defined with a class-specifier.
14118 Returns the function defined. */
14121 cp_parser_function_definition_after_declarator (parser,
14127 bool ctor_initializer_p = false;
14128 bool saved_in_unbraced_linkage_specification_p;
14129 unsigned saved_num_template_parameter_lists;
14131 /* If the next token is `return', then the code may be trying to
14132 make use of the "named return value" extension that G++ used to
14134 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
14136 /* Consume the `return' keyword. */
14137 cp_lexer_consume_token (parser->lexer);
14138 /* Look for the identifier that indicates what value is to be
14140 cp_parser_identifier (parser);
14141 /* Issue an error message. */
14142 error ("named return values are no longer supported");
14143 /* Skip tokens until we reach the start of the function body. */
14144 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
14145 cp_lexer_consume_token (parser->lexer);
14147 /* The `extern' in `extern "C" void f () { ... }' does not apply to
14148 anything declared inside `f'. */
14149 saved_in_unbraced_linkage_specification_p
14150 = parser->in_unbraced_linkage_specification_p;
14151 parser->in_unbraced_linkage_specification_p = false;
14152 /* Inside the function, surrounding template-parameter-lists do not
14154 saved_num_template_parameter_lists
14155 = parser->num_template_parameter_lists;
14156 parser->num_template_parameter_lists = 0;
14157 /* If the next token is `try', then we are looking at a
14158 function-try-block. */
14159 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
14160 ctor_initializer_p = cp_parser_function_try_block (parser);
14161 /* A function-try-block includes the function-body, so we only do
14162 this next part if we're not processing a function-try-block. */
14165 = cp_parser_ctor_initializer_opt_and_function_body (parser);
14167 /* Finish the function. */
14168 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
14169 (inline_p ? 2 : 0));
14170 /* Generate code for it, if necessary. */
14172 /* Restore the saved values. */
14173 parser->in_unbraced_linkage_specification_p
14174 = saved_in_unbraced_linkage_specification_p;
14175 parser->num_template_parameter_lists
14176 = saved_num_template_parameter_lists;
14181 /* Parse a template-declaration, assuming that the `export' (and
14182 `extern') keywords, if present, has already been scanned. MEMBER_P
14183 is as for cp_parser_template_declaration. */
14186 cp_parser_template_declaration_after_export (parser, member_p)
14190 tree decl = NULL_TREE;
14191 tree parameter_list;
14192 bool friend_p = false;
14194 /* Look for the `template' keyword. */
14195 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'"))
14199 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
14202 /* Parse the template parameters. */
14203 begin_template_parm_list ();
14204 /* If the next token is `>', then we have an invalid
14205 specialization. Rather than complain about an invalid template
14206 parameter, issue an error message here. */
14207 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
14209 cp_parser_error (parser, "invalid explicit specialization");
14210 parameter_list = NULL_TREE;
14213 parameter_list = cp_parser_template_parameter_list (parser);
14214 parameter_list = end_template_parm_list (parameter_list);
14215 /* Look for the `>'. */
14216 cp_parser_skip_until_found (parser, CPP_GREATER, "`>'");
14217 /* We just processed one more parameter list. */
14218 ++parser->num_template_parameter_lists;
14219 /* If the next token is `template', there are more template
14221 if (cp_lexer_next_token_is_keyword (parser->lexer,
14223 cp_parser_template_declaration_after_export (parser, member_p);
14226 decl = cp_parser_single_declaration (parser,
14230 /* If this is a member template declaration, let the front
14232 if (member_p && !friend_p && decl)
14233 decl = finish_member_template_decl (decl);
14234 else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
14235 make_friend_class (current_class_type, TREE_TYPE (decl));
14237 /* We are done with the current parameter list. */
14238 --parser->num_template_parameter_lists;
14241 finish_template_decl (parameter_list);
14243 /* Register member declarations. */
14244 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
14245 finish_member_declaration (decl);
14247 /* If DECL is a function template, we must return to parse it later.
14248 (Even though there is no definition, there might be default
14249 arguments that need handling.) */
14250 if (member_p && decl
14251 && (TREE_CODE (decl) == FUNCTION_DECL
14252 || DECL_FUNCTION_TEMPLATE_P (decl)))
14253 TREE_VALUE (parser->unparsed_functions_queues)
14254 = tree_cons (NULL_TREE, decl,
14255 TREE_VALUE (parser->unparsed_functions_queues));
14258 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
14259 `function-definition' sequence. MEMBER_P is true, this declaration
14260 appears in a class scope.
14262 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
14263 *FRIEND_P is set to TRUE iff the declaration is a friend. */
14266 cp_parser_single_declaration (parser,
14273 bool declares_class_or_enum;
14274 tree decl = NULL_TREE;
14275 tree decl_specifiers;
14277 tree access_checks;
14279 /* Parse the dependent declaration. We don't know yet
14280 whether it will be a function-definition. */
14281 cp_parser_parse_tentatively (parser);
14282 /* Defer access checks until we know what is being declared. */
14283 cp_parser_start_deferring_access_checks (parser);
14284 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
14287 = cp_parser_decl_specifier_seq (parser,
14288 CP_PARSER_FLAGS_OPTIONAL,
14290 &declares_class_or_enum);
14291 /* Gather up the access checks that occurred the
14292 decl-specifier-seq. */
14293 access_checks = cp_parser_stop_deferring_access_checks (parser);
14294 /* Check for the declaration of a template class. */
14295 if (declares_class_or_enum)
14297 if (cp_parser_declares_only_class_p (parser))
14299 decl = shadow_tag (decl_specifiers);
14301 decl = TYPE_NAME (decl);
14303 decl = error_mark_node;
14308 /* If it's not a template class, try for a template function. If
14309 the next token is a `;', then this declaration does not declare
14310 anything. But, if there were errors in the decl-specifiers, then
14311 the error might well have come from an attempted class-specifier.
14312 In that case, there's no need to warn about a missing declarator. */
14314 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
14315 || !value_member (error_mark_node, decl_specifiers)))
14316 decl = cp_parser_init_declarator (parser,
14320 /*function_definition_allowed_p=*/false,
14322 /*function_definition_p=*/NULL);
14323 /* Clear any current qualification; whatever comes next is the start
14324 of something new. */
14325 parser->scope = NULL_TREE;
14326 parser->qualifying_scope = NULL_TREE;
14327 parser->object_scope = NULL_TREE;
14328 /* Look for a trailing `;' after the declaration. */
14329 if (!cp_parser_require (parser, CPP_SEMICOLON, "expected `;'")
14330 && cp_parser_committed_to_tentative_parse (parser))
14331 cp_parser_skip_to_end_of_block_or_statement (parser);
14332 /* If it worked, set *FRIEND_P based on the DECL_SPECIFIERS. */
14333 if (cp_parser_parse_definitely (parser))
14336 *friend_p = cp_parser_friend_p (decl_specifiers);
14338 /* Otherwise, try a function-definition. */
14340 decl = cp_parser_function_definition (parser, friend_p);
14345 /* Parse a functional cast to TYPE. Returns an expression
14346 representing the cast. */
14349 cp_parser_functional_cast (parser, type)
14353 tree expression_list;
14355 /* Look for the opening `('. */
14356 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
14357 return error_mark_node;
14358 /* If the next token is not an `)', there are arguments to the
14360 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
14361 expression_list = cp_parser_expression_list (parser);
14363 expression_list = NULL_TREE;
14364 /* Look for the closing `)'. */
14365 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14367 return build_functional_cast (type, expression_list);
14370 /* MEMBER_FUNCTION is a member function, or a friend. If default
14371 arguments, or the body of the function have not yet been parsed,
14375 cp_parser_late_parsing_for_member (parser, member_function)
14377 tree member_function;
14379 cp_lexer *saved_lexer;
14381 /* If this member is a template, get the underlying
14383 if (DECL_FUNCTION_TEMPLATE_P (member_function))
14384 member_function = DECL_TEMPLATE_RESULT (member_function);
14386 /* There should not be any class definitions in progress at this
14387 point; the bodies of members are only parsed outside of all class
14389 my_friendly_assert (parser->num_classes_being_defined == 0, 20010816);
14390 /* While we're parsing the member functions we might encounter more
14391 classes. We want to handle them right away, but we don't want
14392 them getting mixed up with functions that are currently in the
14394 parser->unparsed_functions_queues
14395 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
14397 /* Make sure that any template parameters are in scope. */
14398 maybe_begin_member_template_processing (member_function);
14400 /* If the body of the function has not yet been parsed, parse it
14402 if (DECL_PENDING_INLINE_P (member_function))
14404 tree function_scope;
14405 cp_token_cache *tokens;
14407 /* The function is no longer pending; we are processing it. */
14408 tokens = DECL_PENDING_INLINE_INFO (member_function);
14409 DECL_PENDING_INLINE_INFO (member_function) = NULL;
14410 DECL_PENDING_INLINE_P (member_function) = 0;
14411 /* If this was an inline function in a local class, enter the scope
14412 of the containing function. */
14413 function_scope = decl_function_context (member_function);
14414 if (function_scope)
14415 push_function_context_to (function_scope);
14417 /* Save away the current lexer. */
14418 saved_lexer = parser->lexer;
14419 /* Make a new lexer to feed us the tokens saved for this function. */
14420 parser->lexer = cp_lexer_new_from_tokens (tokens);
14421 parser->lexer->next = saved_lexer;
14423 /* Set the current source position to be the location of the first
14424 token in the saved inline body. */
14425 cp_lexer_set_source_position_from_token
14427 cp_lexer_peek_token (parser->lexer));
14429 /* Let the front end know that we going to be defining this
14431 start_function (NULL_TREE, member_function, NULL_TREE,
14432 SF_PRE_PARSED | SF_INCLASS_INLINE);
14434 /* Now, parse the body of the function. */
14435 cp_parser_function_definition_after_declarator (parser,
14436 /*inline_p=*/true);
14438 /* Leave the scope of the containing function. */
14439 if (function_scope)
14440 pop_function_context_from (function_scope);
14441 /* Restore the lexer. */
14442 parser->lexer = saved_lexer;
14445 /* Remove any template parameters from the symbol table. */
14446 maybe_end_member_template_processing ();
14448 /* Restore the queue. */
14449 parser->unparsed_functions_queues
14450 = TREE_CHAIN (parser->unparsed_functions_queues);
14453 /* FN is a FUNCTION_DECL which may contains a parameter with an
14454 unparsed DEFAULT_ARG. Parse the default args now. */
14457 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
14459 cp_lexer *saved_lexer;
14460 cp_token_cache *tokens;
14461 bool saved_local_variables_forbidden_p;
14464 for (parameters = TYPE_ARG_TYPES (TREE_TYPE (fn));
14466 parameters = TREE_CHAIN (parameters))
14468 if (!TREE_PURPOSE (parameters)
14469 || TREE_CODE (TREE_PURPOSE (parameters)) != DEFAULT_ARG)
14472 /* Save away the current lexer. */
14473 saved_lexer = parser->lexer;
14474 /* Create a new one, using the tokens we have saved. */
14475 tokens = DEFARG_TOKENS (TREE_PURPOSE (parameters));
14476 parser->lexer = cp_lexer_new_from_tokens (tokens);
14478 /* Set the current source position to be the location of the
14479 first token in the default argument. */
14480 cp_lexer_set_source_position_from_token
14481 (parser->lexer, cp_lexer_peek_token (parser->lexer));
14483 /* Local variable names (and the `this' keyword) may not appear
14484 in a default argument. */
14485 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
14486 parser->local_variables_forbidden_p = true;
14487 /* Parse the assignment-expression. */
14488 if (DECL_CONTEXT (fn))
14489 push_nested_class (DECL_CONTEXT (fn), 1);
14490 TREE_PURPOSE (parameters) = cp_parser_assignment_expression (parser);
14491 if (DECL_CONTEXT (fn))
14492 pop_nested_class ();
14494 /* Restore saved state. */
14495 parser->lexer = saved_lexer;
14496 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
14500 /* Parse the operand of `sizeof' (or a similar operator). Returns
14501 either a TYPE or an expression, depending on the form of the
14502 input. The KEYWORD indicates which kind of expression we have
14506 cp_parser_sizeof_operand (parser, keyword)
14510 static const char *format;
14511 tree expr = NULL_TREE;
14512 const char *saved_message;
14513 bool saved_constant_expression_p;
14515 /* Initialize FORMAT the first time we get here. */
14517 format = "types may not be defined in `%s' expressions";
14519 /* Types cannot be defined in a `sizeof' expression. Save away the
14521 saved_message = parser->type_definition_forbidden_message;
14522 /* And create the new one. */
14523 parser->type_definition_forbidden_message
14525 xmalloc (strlen (format)
14526 + strlen (IDENTIFIER_POINTER (ridpointers[keyword]))
14528 sprintf ((char *) parser->type_definition_forbidden_message,
14529 format, IDENTIFIER_POINTER (ridpointers[keyword]));
14531 /* The restrictions on constant-expressions do not apply inside
14532 sizeof expressions. */
14533 saved_constant_expression_p = parser->constant_expression_p;
14534 parser->constant_expression_p = false;
14536 /* Do not actually evaluate the expression. */
14538 /* If it's a `(', then we might be looking at the type-id
14540 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
14544 /* We can't be sure yet whether we're looking at a type-id or an
14546 cp_parser_parse_tentatively (parser);
14547 /* Consume the `('. */
14548 cp_lexer_consume_token (parser->lexer);
14549 /* Parse the type-id. */
14550 type = cp_parser_type_id (parser);
14551 /* Now, look for the trailing `)'. */
14552 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14553 /* If all went well, then we're done. */
14554 if (cp_parser_parse_definitely (parser))
14556 /* Build a list of decl-specifiers; right now, we have only
14557 a single type-specifier. */
14558 type = build_tree_list (NULL_TREE,
14561 /* Call grokdeclarator to figure out what type this is. */
14562 expr = grokdeclarator (NULL_TREE,
14566 /*attrlist=*/NULL);
14570 /* If the type-id production did not work out, then we must be
14571 looking at the unary-expression production. */
14573 expr = cp_parser_unary_expression (parser, /*address_p=*/false);
14574 /* Go back to evaluating expressions. */
14577 /* Free the message we created. */
14578 free ((char *) parser->type_definition_forbidden_message);
14579 /* And restore the old one. */
14580 parser->type_definition_forbidden_message = saved_message;
14581 parser->constant_expression_p = saved_constant_expression_p;
14586 /* If the current declaration has no declarator, return true. */
14589 cp_parser_declares_only_class_p (cp_parser *parser)
14591 /* If the next token is a `;' or a `,' then there is no
14593 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
14594 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
14597 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
14598 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
14601 cp_parser_friend_p (decl_specifiers)
14602 tree decl_specifiers;
14604 while (decl_specifiers)
14606 /* See if this decl-specifier is `friend'. */
14607 if (TREE_CODE (TREE_VALUE (decl_specifiers)) == IDENTIFIER_NODE
14608 && C_RID_CODE (TREE_VALUE (decl_specifiers)) == RID_FRIEND)
14611 /* Go on to the next decl-specifier. */
14612 decl_specifiers = TREE_CHAIN (decl_specifiers);
14618 /* If the next token is of the indicated TYPE, consume it. Otherwise,
14619 issue an error message indicating that TOKEN_DESC was expected.
14621 Returns the token consumed, if the token had the appropriate type.
14622 Otherwise, returns NULL. */
14625 cp_parser_require (parser, type, token_desc)
14627 enum cpp_ttype type;
14628 const char *token_desc;
14630 if (cp_lexer_next_token_is (parser->lexer, type))
14631 return cp_lexer_consume_token (parser->lexer);
14634 /* Output the MESSAGE -- unless we're parsing tentatively. */
14635 if (!cp_parser_simulate_error (parser))
14636 error ("expected %s", token_desc);
14641 /* Like cp_parser_require, except that tokens will be skipped until
14642 the desired token is found. An error message is still produced if
14643 the next token is not as expected. */
14646 cp_parser_skip_until_found (parser, type, token_desc)
14648 enum cpp_ttype type;
14649 const char *token_desc;
14652 unsigned nesting_depth = 0;
14654 if (cp_parser_require (parser, type, token_desc))
14657 /* Skip tokens until the desired token is found. */
14660 /* Peek at the next token. */
14661 token = cp_lexer_peek_token (parser->lexer);
14662 /* If we've reached the token we want, consume it and
14664 if (token->type == type && !nesting_depth)
14666 cp_lexer_consume_token (parser->lexer);
14669 /* If we've run out of tokens, stop. */
14670 if (token->type == CPP_EOF)
14672 if (token->type == CPP_OPEN_BRACE
14673 || token->type == CPP_OPEN_PAREN
14674 || token->type == CPP_OPEN_SQUARE)
14676 else if (token->type == CPP_CLOSE_BRACE
14677 || token->type == CPP_CLOSE_PAREN
14678 || token->type == CPP_CLOSE_SQUARE)
14680 if (nesting_depth-- == 0)
14683 /* Consume this token. */
14684 cp_lexer_consume_token (parser->lexer);
14688 /* If the next token is the indicated keyword, consume it. Otherwise,
14689 issue an error message indicating that TOKEN_DESC was expected.
14691 Returns the token consumed, if the token had the appropriate type.
14692 Otherwise, returns NULL. */
14695 cp_parser_require_keyword (parser, keyword, token_desc)
14698 const char *token_desc;
14700 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
14702 if (token && token->keyword != keyword)
14704 dyn_string_t error_msg;
14706 /* Format the error message. */
14707 error_msg = dyn_string_new (0);
14708 dyn_string_append_cstr (error_msg, "expected ");
14709 dyn_string_append_cstr (error_msg, token_desc);
14710 cp_parser_error (parser, error_msg->s);
14711 dyn_string_delete (error_msg);
14718 /* Returns TRUE iff TOKEN is a token that can begin the body of a
14719 function-definition. */
14722 cp_parser_token_starts_function_definition_p (token)
14725 return (/* An ordinary function-body begins with an `{'. */
14726 token->type == CPP_OPEN_BRACE
14727 /* A ctor-initializer begins with a `:'. */
14728 || token->type == CPP_COLON
14729 /* A function-try-block begins with `try'. */
14730 || token->keyword == RID_TRY
14731 /* The named return value extension begins with `return'. */
14732 || token->keyword == RID_RETURN);
14735 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
14739 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
14743 token = cp_lexer_peek_token (parser->lexer);
14744 return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
14747 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
14748 or none_type otherwise. */
14750 static enum tag_types
14751 cp_parser_token_is_class_key (token)
14754 switch (token->keyword)
14759 return record_type;
14768 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
14771 cp_parser_check_class_key (enum tag_types class_key, tree type)
14773 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
14774 pedwarn ("`%s' tag used in naming `%#T'",
14775 class_key == union_type ? "union"
14776 : class_key == record_type ? "struct" : "class",
14780 /* Look for the `template' keyword, as a syntactic disambiguator.
14781 Return TRUE iff it is present, in which case it will be
14785 cp_parser_optional_template_keyword (cp_parser *parser)
14787 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
14789 /* The `template' keyword can only be used within templates;
14790 outside templates the parser can always figure out what is a
14791 template and what is not. */
14792 if (!processing_template_decl)
14794 error ("`template' (as a disambiguator) is only allowed "
14795 "within templates");
14796 /* If this part of the token stream is rescanned, the same
14797 error message would be generated. So, we purge the token
14798 from the stream. */
14799 cp_lexer_purge_token (parser->lexer);
14804 /* Consume the `template' keyword. */
14805 cp_lexer_consume_token (parser->lexer);
14813 /* Add tokens to CACHE until an non-nested END token appears. */
14816 cp_parser_cache_group (cp_parser *parser,
14817 cp_token_cache *cache,
14818 enum cpp_ttype end,
14825 /* Abort a parenthesized expression if we encounter a brace. */
14826 if ((end == CPP_CLOSE_PAREN || depth == 0)
14827 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
14829 /* Consume the next token. */
14830 token = cp_lexer_consume_token (parser->lexer);
14831 /* If we've reached the end of the file, stop. */
14832 if (token->type == CPP_EOF)
14834 /* Add this token to the tokens we are saving. */
14835 cp_token_cache_push_token (cache, token);
14836 /* See if it starts a new group. */
14837 if (token->type == CPP_OPEN_BRACE)
14839 cp_parser_cache_group (parser, cache, CPP_CLOSE_BRACE, depth + 1);
14843 else if (token->type == CPP_OPEN_PAREN)
14844 cp_parser_cache_group (parser, cache, CPP_CLOSE_PAREN, depth + 1);
14845 else if (token->type == end)
14850 /* Begin parsing tentatively. We always save tokens while parsing
14851 tentatively so that if the tentative parsing fails we can restore the
14855 cp_parser_parse_tentatively (parser)
14858 /* Enter a new parsing context. */
14859 parser->context = cp_parser_context_new (parser->context);
14860 /* Begin saving tokens. */
14861 cp_lexer_save_tokens (parser->lexer);
14862 /* In order to avoid repetitive access control error messages,
14863 access checks are queued up until we are no longer parsing
14865 cp_parser_start_deferring_access_checks (parser);
14868 /* Commit to the currently active tentative parse. */
14871 cp_parser_commit_to_tentative_parse (parser)
14874 cp_parser_context *context;
14877 /* Mark all of the levels as committed. */
14878 lexer = parser->lexer;
14879 for (context = parser->context; context->next; context = context->next)
14881 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
14883 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
14884 while (!cp_lexer_saving_tokens (lexer))
14885 lexer = lexer->next;
14886 cp_lexer_commit_tokens (lexer);
14890 /* Abort the currently active tentative parse. All consumed tokens
14891 will be rolled back, and no diagnostics will be issued. */
14894 cp_parser_abort_tentative_parse (parser)
14897 cp_parser_simulate_error (parser);
14898 /* Now, pretend that we want to see if the construct was
14899 successfully parsed. */
14900 cp_parser_parse_definitely (parser);
14903 /* Stop parsing tentatively. If a parse error has ocurred, restore the
14904 token stream. Otherwise, commit to the tokens we have consumed.
14905 Returns true if no error occurred; false otherwise. */
14908 cp_parser_parse_definitely (parser)
14911 bool error_occurred;
14912 cp_parser_context *context;
14914 /* Remember whether or not an error ocurred, since we are about to
14915 destroy that information. */
14916 error_occurred = cp_parser_error_occurred (parser);
14917 /* Remove the topmost context from the stack. */
14918 context = parser->context;
14919 parser->context = context->next;
14920 /* If no parse errors occurred, commit to the tentative parse. */
14921 if (!error_occurred)
14923 /* Commit to the tokens read tentatively, unless that was
14925 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
14926 cp_lexer_commit_tokens (parser->lexer);
14927 if (!parser->context->deferring_access_checks_p)
14928 /* If in the parent context we are not deferring checks, then
14929 these perform these checks now. */
14930 (cp_parser_perform_deferred_access_checks
14931 (context->deferred_access_checks));
14933 /* Any lookups that were deferred during the tentative parse are
14935 parser->context->deferred_access_checks
14936 = chainon (parser->context->deferred_access_checks,
14937 context->deferred_access_checks);
14939 /* Otherwise, if errors occurred, roll back our state so that things
14940 are just as they were before we began the tentative parse. */
14942 cp_lexer_rollback_tokens (parser->lexer);
14943 /* Add the context to the front of the free list. */
14944 context->next = cp_parser_context_free_list;
14945 cp_parser_context_free_list = context;
14947 return !error_occurred;
14950 /* Returns true if we are parsing tentatively -- but have decided that
14951 we will stick with this tentative parse, even if errors occur. */
14954 cp_parser_committed_to_tentative_parse (parser)
14957 return (cp_parser_parsing_tentatively (parser)
14958 && parser->context->status == CP_PARSER_STATUS_KIND_COMMITTED);
14961 /* Returns non-zero iff an error has occurred during the most recent
14962 tentative parse. */
14965 cp_parser_error_occurred (parser)
14968 return (cp_parser_parsing_tentatively (parser)
14969 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
14972 /* Returns non-zero if GNU extensions are allowed. */
14975 cp_parser_allow_gnu_extensions_p (parser)
14978 return parser->allow_gnu_extensions_p;
14985 static GTY (()) cp_parser *the_parser;
14987 /* External interface. */
14989 /* Parse the entire translation unit. */
14994 bool error_occurred;
14996 the_parser = cp_parser_new ();
14997 error_occurred = cp_parser_translation_unit (the_parser);
15000 return error_occurred;
15003 /* Clean up after parsing the entire translation unit. */
15006 free_parser_stacks ()
15008 /* Nothing to do. */
15011 /* This variable must be provided by every front end. */
15015 #include "gt-cp-parser.h"