2 Copyright (C) 2000, 2001, 2002 Free Software Foundation, Inc.
3 Written by Mark Mitchell <mark@codesourcery.com>.
5 This file is part of GNU CC.
7 GNU CC 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 GNU CC 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 GNU CC; 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 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 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 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 /* Non-zero if we are presently saving tokens. */
374 cp_lexer_saving_tokens (lexer)
375 const cp_lexer *lexer;
377 return VARRAY_ACTIVE_SIZE (lexer->saved_tokens) != 0;
380 /* TOKEN points into the circular token buffer. Return a pointer to
381 the next token in the buffer. */
384 cp_lexer_next_token (lexer, token)
389 if (token == lexer->buffer_end)
390 token = lexer->buffer;
394 /* Return a pointer to the token that is N tokens beyond TOKEN in the
398 cp_lexer_advance_token (cp_lexer *lexer, cp_token *token, ptrdiff_t n)
401 if (token >= lexer->buffer_end)
402 token = lexer->buffer + (token - lexer->buffer_end);
406 /* Returns the number of times that START would have to be incremented
407 to reach FINISH. If START and FINISH are the same, returns zero. */
410 cp_lexer_token_difference (lexer, start, finish)
416 return finish - start;
418 return ((lexer->buffer_end - lexer->buffer)
422 /* Obtain another token from the C preprocessor and add it to the
423 token buffer. Returns the newly read token. */
426 cp_lexer_read_token (lexer)
431 /* Make sure there is room in the buffer. */
432 cp_lexer_maybe_grow_buffer (lexer);
434 /* If there weren't any tokens, then this one will be the first. */
435 if (!lexer->first_token)
436 lexer->first_token = lexer->last_token;
437 /* Similarly, if there were no available tokens, there is one now. */
438 if (!lexer->next_token)
439 lexer->next_token = lexer->last_token;
441 /* Figure out where we're going to store the new token. */
442 token = lexer->last_token;
444 /* Get a new token from the preprocessor. */
445 cp_lexer_get_preprocessor_token (lexer, token);
447 /* Increment LAST_TOKEN. */
448 lexer->last_token = cp_lexer_next_token (lexer, token);
450 /* The preprocessor does not yet do translation phase six, i.e., the
451 combination of adjacent string literals. Therefore, we do it
453 if (token->type == CPP_STRING || token->type == CPP_WSTRING)
458 /* When we grow the buffer, we may invalidate TOKEN. So, save
459 the distance from the beginning of the BUFFER so that we can
461 delta = cp_lexer_token_difference (lexer, lexer->buffer, token);
462 /* Make sure there is room in the buffer for another token. */
463 cp_lexer_maybe_grow_buffer (lexer);
465 token = lexer->buffer;
466 for (i = 0; i < delta; ++i)
467 token = cp_lexer_next_token (lexer, token);
469 VARRAY_PUSH_TREE (lexer->string_tokens, token->value);
472 /* Read the token after TOKEN. */
473 cp_lexer_get_preprocessor_token (lexer, lexer->last_token);
474 /* See whether it's another string constant. */
475 if (lexer->last_token->type != token->type)
477 /* If not, then it will be the next real token. */
478 lexer->last_token = cp_lexer_next_token (lexer,
483 /* Chain the strings together. */
484 VARRAY_PUSH_TREE (lexer->string_tokens,
485 lexer->last_token->value);
488 /* Create a single STRING_CST. Curiously we have to call
489 combine_strings even if there is only a single string in
490 order to get the type set correctly. */
491 token->value = combine_strings (lexer->string_tokens);
492 VARRAY_CLEAR (lexer->string_tokens);
493 token->value = fix_string_type (token->value);
494 /* Strings should have type `const char []'. Right now, we will
495 have an ARRAY_TYPE that is constant rather than an array of
496 constant elements. */
497 if (flag_const_strings)
501 /* Get the current type. It will be an ARRAY_TYPE. */
502 type = TREE_TYPE (token->value);
503 /* Use build_cplus_array_type to rebuild the array, thereby
504 getting the right type. */
505 type = build_cplus_array_type (TREE_TYPE (type),
507 /* Reset the type of the token. */
508 TREE_TYPE (token->value) = type;
515 /* If the circular buffer is full, make it bigger. */
518 cp_lexer_maybe_grow_buffer (lexer)
521 /* If the buffer is full, enlarge it. */
522 if (lexer->last_token == lexer->first_token)
524 cp_token *new_buffer;
525 cp_token *old_buffer;
526 cp_token *new_first_token;
527 ptrdiff_t buffer_length;
528 size_t num_tokens_to_copy;
530 /* Remember the current buffer pointer. It will become invalid,
531 but we will need to do pointer arithmetic involving this
533 old_buffer = lexer->buffer;
534 /* Compute the current buffer size. */
535 buffer_length = lexer->buffer_end - lexer->buffer;
536 /* Allocate a buffer twice as big. */
537 new_buffer = ((cp_token *)
538 ggc_realloc (lexer->buffer,
539 2 * buffer_length * sizeof (cp_token)));
541 /* Because the buffer is circular, logically consecutive tokens
542 are not necessarily placed consecutively in memory.
543 Therefore, we must keep move the tokens that were before
544 FIRST_TOKEN to the second half of the newly allocated
546 num_tokens_to_copy = (lexer->first_token - old_buffer);
547 memcpy (new_buffer + buffer_length,
549 num_tokens_to_copy * sizeof (cp_token));
550 /* Clear the rest of the buffer. We never look at this storage,
551 but the garbage collector may. */
552 memset (new_buffer + buffer_length + num_tokens_to_copy, 0,
553 (buffer_length - num_tokens_to_copy) * sizeof (cp_token));
555 /* Now recompute all of the buffer pointers. */
557 = new_buffer + (lexer->first_token - old_buffer);
558 if (lexer->next_token != NULL)
560 ptrdiff_t next_token_delta;
562 if (lexer->next_token > lexer->first_token)
563 next_token_delta = lexer->next_token - lexer->first_token;
566 buffer_length - (lexer->first_token - lexer->next_token);
567 lexer->next_token = new_first_token + next_token_delta;
569 lexer->last_token = new_first_token + buffer_length;
570 lexer->buffer = new_buffer;
571 lexer->buffer_end = new_buffer + buffer_length * 2;
572 lexer->first_token = new_first_token;
576 /* Store the next token from the preprocessor in *TOKEN. */
579 cp_lexer_get_preprocessor_token (lexer, token)
580 cp_lexer *lexer ATTRIBUTE_UNUSED;
585 /* If this not the main lexer, return a terminating CPP_EOF token. */
586 if (!lexer->main_lexer_p)
588 token->type = CPP_EOF;
589 token->line_number = 0;
590 token->file_name = NULL;
591 token->value = NULL_TREE;
592 token->keyword = RID_MAX;
598 /* Keep going until we get a token we like. */
601 /* Get a new token from the preprocessor. */
602 token->type = c_lex (&token->value);
603 /* Issue messages about tokens we cannot process. */
609 error ("invalid token");
613 /* These tokens are already warned about by c_lex. */
617 /* This is a good token, so we exit the loop. */
622 /* Now we've got our token. */
623 token->line_number = lineno;
624 token->file_name = input_filename;
626 /* Check to see if this token is a keyword. */
627 if (token->type == CPP_NAME
628 && C_IS_RESERVED_WORD (token->value))
630 /* Mark this token as a keyword. */
631 token->type = CPP_KEYWORD;
632 /* Record which keyword. */
633 token->keyword = C_RID_CODE (token->value);
634 /* Update the value. Some keywords are mapped to particular
635 entities, rather than simply having the value of the
636 corresponding IDENTIFIER_NODE. For example, `__const' is
637 mapped to `const'. */
638 token->value = ridpointers[token->keyword];
641 token->keyword = RID_MAX;
644 /* Return a pointer to the next token in the token stream, but do not
648 cp_lexer_peek_token (lexer)
653 /* If there are no tokens, read one now. */
654 if (!lexer->next_token)
655 cp_lexer_read_token (lexer);
657 /* Provide debugging output. */
658 if (cp_lexer_debugging_p (lexer))
660 fprintf (cp_lexer_debug_stream, "cp_lexer: peeking at token: ");
661 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
662 fprintf (cp_lexer_debug_stream, "\n");
665 token = lexer->next_token;
666 cp_lexer_set_source_position_from_token (lexer, token);
670 /* Return true if the next token has the indicated TYPE. */
673 cp_lexer_next_token_is (lexer, type)
679 /* Peek at the next token. */
680 token = cp_lexer_peek_token (lexer);
681 /* Check to see if it has the indicated TYPE. */
682 return token->type == type;
685 /* Return true if the next token does not have the indicated TYPE. */
688 cp_lexer_next_token_is_not (lexer, type)
692 return !cp_lexer_next_token_is (lexer, type);
695 /* Return true if the next token is the indicated KEYWORD. */
698 cp_lexer_next_token_is_keyword (lexer, keyword)
704 /* Peek at the next token. */
705 token = cp_lexer_peek_token (lexer);
706 /* Check to see if it is the indicated keyword. */
707 return token->keyword == keyword;
710 /* Return a pointer to the Nth token in the token stream. If N is 1,
711 then this is precisely equivalent to cp_lexer_peek_token. */
714 cp_lexer_peek_nth_token (lexer, n)
720 /* N is 1-based, not zero-based. */
721 my_friendly_assert (n > 0, 20000224);
723 /* Skip ahead from NEXT_TOKEN, reading more tokens as necessary. */
724 token = lexer->next_token;
725 /* If there are no tokens in the buffer, get one now. */
728 cp_lexer_read_token (lexer);
729 token = lexer->next_token;
732 /* Now, read tokens until we have enough. */
735 /* Advance to the next token. */
736 token = cp_lexer_next_token (lexer, token);
737 /* If that's all the tokens we have, read a new one. */
738 if (token == lexer->last_token)
739 token = cp_lexer_read_token (lexer);
745 /* Consume the next token. The pointer returned is valid only until
746 another token is read. Callers should preserve copy the token
747 explicitly if they will need its value for a longer period of
751 cp_lexer_consume_token (lexer)
756 /* If there are no tokens, read one now. */
757 if (!lexer->next_token)
758 cp_lexer_read_token (lexer);
760 /* Remember the token we'll be returning. */
761 token = lexer->next_token;
763 /* Increment NEXT_TOKEN. */
764 lexer->next_token = cp_lexer_next_token (lexer,
766 /* Check to see if we're all out of tokens. */
767 if (lexer->next_token == lexer->last_token)
768 lexer->next_token = NULL;
770 /* If we're not saving tokens, then move FIRST_TOKEN too. */
771 if (!cp_lexer_saving_tokens (lexer))
773 /* If there are no tokens available, set FIRST_TOKEN to NULL. */
774 if (!lexer->next_token)
775 lexer->first_token = NULL;
777 lexer->first_token = lexer->next_token;
780 /* Provide debugging output. */
781 if (cp_lexer_debugging_p (lexer))
783 fprintf (cp_lexer_debug_stream, "cp_lexer: consuming token: ");
784 cp_lexer_print_token (cp_lexer_debug_stream, token);
785 fprintf (cp_lexer_debug_stream, "\n");
791 /* Permanently remove the next token from the token stream. There
792 must be a valid next token already; this token never reads
793 additional tokens from the preprocessor. */
796 cp_lexer_purge_token (cp_lexer *lexer)
799 cp_token *next_token;
801 token = lexer->next_token;
804 next_token = cp_lexer_next_token (lexer, token);
805 if (next_token == lexer->last_token)
807 *token = *next_token;
811 lexer->last_token = token;
812 /* The token purged may have been the only token remaining; if so,
814 if (lexer->next_token == token)
815 lexer->next_token = NULL;
818 /* Permanently remove all tokens after TOKEN, up to, but not
819 including, the token that will be returned next by
820 cp_lexer_peek_token. */
823 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *token)
829 if (lexer->next_token)
831 /* Copy the tokens that have not yet been read to the location
832 immediately following TOKEN. */
833 t1 = cp_lexer_next_token (lexer, token);
834 t2 = peek = cp_lexer_peek_token (lexer);
835 /* Move tokens into the vacant area between TOKEN and PEEK. */
836 while (t2 != lexer->last_token)
839 t1 = cp_lexer_next_token (lexer, t1);
840 t2 = cp_lexer_next_token (lexer, t2);
842 /* Now, the next available token is right after TOKEN. */
843 lexer->next_token = cp_lexer_next_token (lexer, token);
844 /* And the last token is wherever we ended up. */
845 lexer->last_token = t1;
849 /* There are no tokens in the buffer, so there is nothing to
850 copy. The last token in the buffer is TOKEN itself. */
851 lexer->last_token = cp_lexer_next_token (lexer, token);
855 /* Begin saving tokens. All tokens consumed after this point will be
859 cp_lexer_save_tokens (lexer)
862 /* Provide debugging output. */
863 if (cp_lexer_debugging_p (lexer))
864 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
866 /* Make sure that LEXER->NEXT_TOKEN is non-NULL so that we can
867 restore the tokens if required. */
868 if (!lexer->next_token)
869 cp_lexer_read_token (lexer);
871 VARRAY_PUSH_INT (lexer->saved_tokens,
872 cp_lexer_token_difference (lexer,
877 /* Commit to the portion of the token stream most recently saved. */
880 cp_lexer_commit_tokens (lexer)
883 /* Provide debugging output. */
884 if (cp_lexer_debugging_p (lexer))
885 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
887 VARRAY_POP (lexer->saved_tokens);
890 /* Return all tokens saved since the last call to cp_lexer_save_tokens
891 to the token stream. Stop saving tokens. */
894 cp_lexer_rollback_tokens (lexer)
899 /* Provide debugging output. */
900 if (cp_lexer_debugging_p (lexer))
901 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
903 /* Find the token that was the NEXT_TOKEN when we started saving
905 delta = VARRAY_TOP_INT(lexer->saved_tokens);
906 /* Make it the next token again now. */
907 lexer->next_token = cp_lexer_advance_token (lexer,
910 /* It might be the case that there wer no tokens when we started
911 saving tokens, but that there are some tokens now. */
912 if (!lexer->next_token && lexer->first_token)
913 lexer->next_token = lexer->first_token;
915 /* Stop saving tokens. */
916 VARRAY_POP (lexer->saved_tokens);
919 /* Set the current source position from the information stored in
923 cp_lexer_set_source_position_from_token (lexer, token)
924 cp_lexer *lexer ATTRIBUTE_UNUSED;
925 const cp_token *token;
927 /* Ideally, the source position information would not be a global
928 variable, but it is. */
930 /* Update the line number. */
931 if (token->type != CPP_EOF)
933 lineno = token->line_number;
934 input_filename = token->file_name;
938 /* Print a representation of the TOKEN on the STREAM. */
941 cp_lexer_print_token (stream, token)
945 const char *token_type = NULL;
947 /* Figure out what kind of token this is. */
955 token_type = "COMMA";
959 token_type = "OPEN_PAREN";
962 case CPP_CLOSE_PAREN:
963 token_type = "CLOSE_PAREN";
967 token_type = "OPEN_BRACE";
970 case CPP_CLOSE_BRACE:
971 token_type = "CLOSE_BRACE";
975 token_type = "SEMICOLON";
987 token_type = "keyword";
990 /* This is not a token that we know how to handle yet. */
995 /* If we have a name for the token, print it out. Otherwise, we
996 simply give the numeric code. */
998 fprintf (stream, "%s", token_type);
1000 fprintf (stream, "%d", token->type);
1001 /* And, for an identifier, print the identifier name. */
1002 if (token->type == CPP_NAME
1003 /* Some keywords have a value that is not an IDENTIFIER_NODE.
1004 For example, `struct' is mapped to an INTEGER_CST. */
1005 || (token->type == CPP_KEYWORD
1006 && TREE_CODE (token->value) == IDENTIFIER_NODE))
1007 fprintf (stream, " %s", IDENTIFIER_POINTER (token->value));
1010 /* Returns non-zero if debugging information should be output. */
1013 cp_lexer_debugging_p (lexer)
1016 return lexer->debugging_p;
1019 /* Start emitting debugging information. */
1022 cp_lexer_start_debugging (lexer)
1025 ++lexer->debugging_p;
1028 /* Stop emitting debugging information. */
1031 cp_lexer_stop_debugging (lexer)
1034 --lexer->debugging_p;
1043 A cp_parser parses the token stream as specified by the C++
1044 grammar. Its job is purely parsing, not semantic analysis. For
1045 example, the parser breaks the token stream into declarators,
1046 expressions, statements, and other similar syntactic constructs.
1047 It does not check that the types of the expressions on either side
1048 of an assignment-statement are compatible, or that a function is
1049 not declared with a parameter of type `void'.
1051 The parser invokes routines elsewhere in the compiler to perform
1052 semantic analysis and to build up the abstract syntax tree for the
1055 The parser (and the template instantiation code, which is, in a
1056 way, a close relative of parsing) are the only parts of the
1057 compiler that should be calling push_scope and pop_scope, or
1058 related functions. The parser (and template instantiation code)
1059 keeps track of what scope is presently active; everything else
1060 should simply honor that. (The code that generates static
1061 initializers may also need to set the scope, in order to check
1062 access control correctly when emitting the initializers.)
1067 The parser is of the standard recursive-descent variety. Upcoming
1068 tokens in the token stream are examined in order to determine which
1069 production to use when parsing a non-terminal. Some C++ constructs
1070 require arbitrary look ahead to disambiguate. For example, it is
1071 impossible, in the general case, to tell whether a statement is an
1072 expression or declaration without scanning the entire statement.
1073 Therefore, the parser is capable of "parsing tentatively." When the
1074 parser is not sure what construct comes next, it enters this mode.
1075 Then, while we attempt to parse the construct, the parser queues up
1076 error messages, rather than issuing them immediately, and saves the
1077 tokens it consumes. If the construct is parsed successfully, the
1078 parser "commits", i.e., it issues any queued error messages and
1079 the tokens that were being preserved are permanently discarded.
1080 If, however, the construct is not parsed successfully, the parser
1081 rolls back its state completely so that it can resume parsing using
1082 a different alternative.
1087 The performance of the parser could probably be improved
1088 substantially. Some possible improvements include:
1090 - The expression parser recurses through the various levels of
1091 precedence as specified in the grammar, rather than using an
1092 operator-precedence technique. Therefore, parsing a simple
1093 identifier requires multiple recursive calls.
1095 - We could often eliminate the need to parse tentatively by
1096 looking ahead a little bit. In some places, this approach
1097 might not entirely eliminate the need to parse tentatively, but
1098 it might still speed up the average case. */
1100 /* Flags that are passed to some parsing functions. These values can
1101 be bitwise-ored together. */
1103 typedef enum cp_parser_flags
1106 CP_PARSER_FLAGS_NONE = 0x0,
1107 /* The construct is optional. If it is not present, then no error
1108 should be issued. */
1109 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1110 /* When parsing a type-specifier, do not allow user-defined types. */
1111 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2
1114 /* The different kinds of ids that we ecounter. */
1116 typedef enum cp_parser_id_kind
1118 /* Not an id at all. */
1119 CP_PARSER_ID_KIND_NONE,
1120 /* An unqualified-id that is not a template-id. */
1121 CP_PARSER_ID_KIND_UNQUALIFIED,
1122 /* An unqualified template-id. */
1123 CP_PARSER_ID_KIND_TEMPLATE_ID,
1124 /* A qualified-id. */
1125 CP_PARSER_ID_KIND_QUALIFIED
1126 } cp_parser_id_kind;
1128 /* A mapping from a token type to a corresponding tree node type. */
1130 typedef struct cp_parser_token_tree_map_node
1132 /* The token type. */
1133 enum cpp_ttype token_type;
1134 /* The corresponding tree code. */
1135 enum tree_code tree_type;
1136 } cp_parser_token_tree_map_node;
1138 /* A complete map consists of several ordinary entries, followed by a
1139 terminator. The terminating entry has a token_type of CPP_EOF. */
1141 typedef cp_parser_token_tree_map_node cp_parser_token_tree_map[];
1143 /* The status of a tentative parse. */
1145 typedef enum cp_parser_status_kind
1147 /* No errors have occurred. */
1148 CP_PARSER_STATUS_KIND_NO_ERROR,
1149 /* An error has occurred. */
1150 CP_PARSER_STATUS_KIND_ERROR,
1151 /* We are committed to this tentative parse, whether or not an error
1153 CP_PARSER_STATUS_KIND_COMMITTED
1154 } cp_parser_status_kind;
1156 /* Context that is saved and restored when parsing tentatively. */
1158 typedef struct cp_parser_context GTY (())
1160 /* If this is a tentative parsing context, the status of the
1162 enum cp_parser_status_kind status;
1163 /* If non-NULL, we have just seen a `x->' or `x.' expression. Names
1164 that are looked up in this context must be looked up both in the
1165 scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1166 the context of the containing expression. */
1168 /* A TREE_LIST representing name-lookups for which we have deferred
1169 checking access controls. We cannot check the accessibility of
1170 names used in a decl-specifier-seq until we know what is being
1171 declared because code like:
1178 A::B* A::f() { return 0; }
1180 is valid, even though `A::B' is not generally accessible.
1182 The TREE_PURPOSE of each node is the scope used to qualify the
1183 name being looked up; the TREE_VALUE is the DECL to which the
1184 name was resolved. */
1185 tree deferred_access_checks;
1186 /* TRUE iff we are deferring access checks. */
1187 bool deferring_access_checks_p;
1188 /* The next parsing context in the stack. */
1189 struct cp_parser_context *next;
1190 } cp_parser_context;
1194 /* Constructors and destructors. */
1196 static cp_parser_context *cp_parser_context_new
1197 PARAMS ((cp_parser_context *));
1199 /* Class variables. */
1201 static GTY((deletable (""))) cp_parser_context* cp_parser_context_free_list;
1203 /* Constructors and destructors. */
1205 /* Construct a new context. The context below this one on the stack
1206 is given by NEXT. */
1208 static cp_parser_context *
1209 cp_parser_context_new (next)
1210 cp_parser_context *next;
1212 cp_parser_context *context;
1214 /* Allocate the storage. */
1215 if (cp_parser_context_free_list != NULL)
1217 /* Pull the first entry from the free list. */
1218 context = cp_parser_context_free_list;
1219 cp_parser_context_free_list = context->next;
1220 memset ((char *)context, 0, sizeof (*context));
1223 context = ((cp_parser_context *)
1224 ggc_alloc_cleared (sizeof (cp_parser_context)));
1225 /* No errors have occurred yet in this context. */
1226 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1227 /* If this is not the bottomost context, copy information that we
1228 need from the previous context. */
1231 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1232 expression, then we are parsing one in this context, too. */
1233 context->object_type = next->object_type;
1234 /* We are deferring access checks here if we were in the NEXT
1236 context->deferring_access_checks_p
1237 = next->deferring_access_checks_p;
1238 /* Thread the stack. */
1239 context->next = next;
1245 /* The cp_parser structure represents the C++ parser. */
1247 typedef struct cp_parser GTY(())
1249 /* The lexer from which we are obtaining tokens. */
1252 /* The scope in which names should be looked up. If NULL_TREE, then
1253 we look up names in the scope that is currently open in the
1254 source program. If non-NULL, this is either a TYPE or
1255 NAMESPACE_DECL for the scope in which we should look.
1257 This value is not cleared automatically after a name is looked
1258 up, so we must be careful to clear it before starting a new look
1259 up sequence. (If it is not cleared, then `X::Y' followed by `Z'
1260 will look up `Z' in the scope of `X', rather than the current
1261 scope.) Unfortunately, it is difficult to tell when name lookup
1262 is complete, because we sometimes peek at a token, look it up,
1263 and then decide not to consume it. */
1266 /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1267 last lookup took place. OBJECT_SCOPE is used if an expression
1268 like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1269 respectively. QUALIFYING_SCOPE is used for an expression of the
1270 form "X::Y"; it refers to X. */
1272 tree qualifying_scope;
1274 /* A stack of parsing contexts. All but the bottom entry on the
1275 stack will be tentative contexts.
1277 We parse tentatively in order to determine which construct is in
1278 use in some situations. For example, in order to determine
1279 whether a statement is an expression-statement or a
1280 declaration-statement we parse it tentatively as a
1281 declaration-statement. If that fails, we then reparse the same
1282 token stream as an expression-statement. */
1283 cp_parser_context *context;
1285 /* True if we are parsing GNU C++. If this flag is not set, then
1286 GNU extensions are not recognized. */
1287 bool allow_gnu_extensions_p;
1289 /* TRUE if the `>' token should be interpreted as the greater-than
1290 operator. FALSE if it is the end of a template-id or
1291 template-parameter-list. */
1292 bool greater_than_is_operator_p;
1294 /* TRUE if default arguments are allowed within a parameter list
1295 that starts at this point. FALSE if only a gnu extension makes
1296 them permissable. */
1297 bool default_arg_ok_p;
1299 /* TRUE if we are parsing an integral constant-expression. See
1300 [expr.const] for a precise definition. */
1301 /* FIXME: Need to implement code that checks this flag. */
1302 bool constant_expression_p;
1304 /* TRUE if local variable names and `this' are forbidden in the
1306 bool local_variables_forbidden_p;
1308 /* TRUE if the declaration we are parsing is part of a
1309 linkage-specification of the form `extern string-literal
1311 bool in_unbraced_linkage_specification_p;
1313 /* TRUE if we are presently parsing a declarator, after the
1314 direct-declarator. */
1315 bool in_declarator_p;
1317 /* If non-NULL, then we are parsing a construct where new type
1318 definitions are not permitted. The string stored here will be
1319 issued as an error message if a type is defined. */
1320 const char *type_definition_forbidden_message;
1322 /* List of FUNCTION_TYPEs which contain unprocessed DEFAULT_ARGs
1323 during class parsing, and are not FUNCTION_DECLs. G++ has an
1324 awkward extension allowing default args on pointers to functions
1326 tree default_arg_types;
1328 /* A TREE_LIST of queues of functions whose bodies have been lexed,
1329 but may not have been parsed. These functions are friends of
1330 members defined within a class-specification; they are not
1331 procssed until the class is complete. The active queue is at the
1334 Within each queue, functions appear in the reverse order that
1335 they appeared in the source. The TREE_PURPOSE of each node is
1336 the class in which the function was defined or declared; the
1337 TREE_VALUE is the FUNCTION_DECL itself. */
1338 tree unparsed_functions_queues;
1340 /* The number of classes whose definitions are currently in
1342 unsigned num_classes_being_defined;
1344 /* The number of template parameter lists that apply directly to the
1345 current declaration. */
1346 unsigned num_template_parameter_lists;
1349 /* The type of a function that parses some kind of expression */
1350 typedef tree (*cp_parser_expression_fn) PARAMS ((cp_parser *));
1354 /* Constructors and destructors. */
1356 static cp_parser *cp_parser_new
1359 /* Routines to parse various constructs.
1361 Those that return `tree' will return the error_mark_node (rather
1362 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1363 Sometimes, they will return an ordinary node if error-recovery was
1364 attempted, even though a parse error occurrred. So, to check
1365 whether or not a parse error occurred, you should always use
1366 cp_parser_error_occurred. If the construct is optional (indicated
1367 either by an `_opt' in the name of the function that does the
1368 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1369 the construct is not present. */
1371 /* Lexical conventions [gram.lex] */
1373 static tree cp_parser_identifier
1374 PARAMS ((cp_parser *));
1376 /* Basic concepts [gram.basic] */
1378 static bool cp_parser_translation_unit
1379 PARAMS ((cp_parser *));
1381 /* Expressions [gram.expr] */
1383 static tree cp_parser_primary_expression
1384 (cp_parser *, cp_parser_id_kind *, tree *);
1385 static tree cp_parser_id_expression
1386 PARAMS ((cp_parser *, bool, bool, bool *));
1387 static tree cp_parser_unqualified_id
1388 PARAMS ((cp_parser *, bool, bool));
1389 static tree cp_parser_nested_name_specifier_opt
1390 (cp_parser *, bool, bool, bool);
1391 static tree cp_parser_nested_name_specifier
1392 (cp_parser *, bool, bool, bool);
1393 static tree cp_parser_class_or_namespace_name
1394 (cp_parser *, bool, bool, bool, bool);
1395 static tree cp_parser_postfix_expression
1396 (cp_parser *, bool);
1397 static tree cp_parser_expression_list
1398 PARAMS ((cp_parser *));
1399 static void cp_parser_pseudo_destructor_name
1400 PARAMS ((cp_parser *, tree *, tree *));
1401 static tree cp_parser_unary_expression
1402 (cp_parser *, bool);
1403 static enum tree_code cp_parser_unary_operator
1404 PARAMS ((cp_token *));
1405 static tree cp_parser_new_expression
1406 PARAMS ((cp_parser *));
1407 static tree cp_parser_new_placement
1408 PARAMS ((cp_parser *));
1409 static tree cp_parser_new_type_id
1410 PARAMS ((cp_parser *));
1411 static tree cp_parser_new_declarator_opt
1412 PARAMS ((cp_parser *));
1413 static tree cp_parser_direct_new_declarator
1414 PARAMS ((cp_parser *));
1415 static tree cp_parser_new_initializer
1416 PARAMS ((cp_parser *));
1417 static tree cp_parser_delete_expression
1418 PARAMS ((cp_parser *));
1419 static tree cp_parser_cast_expression
1420 (cp_parser *, bool);
1421 static tree cp_parser_pm_expression
1422 PARAMS ((cp_parser *));
1423 static tree cp_parser_multiplicative_expression
1424 PARAMS ((cp_parser *));
1425 static tree cp_parser_additive_expression
1426 PARAMS ((cp_parser *));
1427 static tree cp_parser_shift_expression
1428 PARAMS ((cp_parser *));
1429 static tree cp_parser_relational_expression
1430 PARAMS ((cp_parser *));
1431 static tree cp_parser_equality_expression
1432 PARAMS ((cp_parser *));
1433 static tree cp_parser_and_expression
1434 PARAMS ((cp_parser *));
1435 static tree cp_parser_exclusive_or_expression
1436 PARAMS ((cp_parser *));
1437 static tree cp_parser_inclusive_or_expression
1438 PARAMS ((cp_parser *));
1439 static tree cp_parser_logical_and_expression
1440 PARAMS ((cp_parser *));
1441 static tree cp_parser_logical_or_expression
1442 PARAMS ((cp_parser *));
1443 static tree cp_parser_conditional_expression
1444 PARAMS ((cp_parser *));
1445 static tree cp_parser_question_colon_clause
1446 PARAMS ((cp_parser *, tree));
1447 static tree cp_parser_assignment_expression
1448 PARAMS ((cp_parser *));
1449 static enum tree_code cp_parser_assignment_operator_opt
1450 PARAMS ((cp_parser *));
1451 static tree cp_parser_expression
1452 PARAMS ((cp_parser *));
1453 static tree cp_parser_constant_expression
1454 PARAMS ((cp_parser *));
1456 /* Statements [gram.stmt.stmt] */
1458 static void cp_parser_statement
1459 PARAMS ((cp_parser *));
1460 static tree cp_parser_labeled_statement
1461 PARAMS ((cp_parser *));
1462 static tree cp_parser_expression_statement
1463 PARAMS ((cp_parser *));
1464 static tree cp_parser_compound_statement
1466 static void cp_parser_statement_seq_opt
1467 PARAMS ((cp_parser *));
1468 static tree cp_parser_selection_statement
1469 PARAMS ((cp_parser *));
1470 static tree cp_parser_condition
1471 PARAMS ((cp_parser *));
1472 static tree cp_parser_iteration_statement
1473 PARAMS ((cp_parser *));
1474 static void cp_parser_for_init_statement
1475 PARAMS ((cp_parser *));
1476 static tree cp_parser_jump_statement
1477 PARAMS ((cp_parser *));
1478 static void cp_parser_declaration_statement
1479 PARAMS ((cp_parser *));
1481 static tree cp_parser_implicitly_scoped_statement
1482 PARAMS ((cp_parser *));
1483 static void cp_parser_already_scoped_statement
1484 PARAMS ((cp_parser *));
1486 /* Declarations [gram.dcl.dcl] */
1488 static void cp_parser_declaration_seq_opt
1489 PARAMS ((cp_parser *));
1490 static void cp_parser_declaration
1491 PARAMS ((cp_parser *));
1492 static void cp_parser_block_declaration
1493 PARAMS ((cp_parser *, bool));
1494 static void cp_parser_simple_declaration
1495 PARAMS ((cp_parser *, bool));
1496 static tree cp_parser_decl_specifier_seq
1497 PARAMS ((cp_parser *, cp_parser_flags, tree *, bool *));
1498 static tree cp_parser_storage_class_specifier_opt
1499 PARAMS ((cp_parser *));
1500 static tree cp_parser_function_specifier_opt
1501 PARAMS ((cp_parser *));
1502 static tree cp_parser_type_specifier
1503 (cp_parser *, cp_parser_flags, bool, bool, bool *, bool *);
1504 static tree cp_parser_simple_type_specifier
1505 PARAMS ((cp_parser *, cp_parser_flags));
1506 static tree cp_parser_type_name
1507 PARAMS ((cp_parser *));
1508 static tree cp_parser_elaborated_type_specifier
1509 PARAMS ((cp_parser *, bool, bool));
1510 static tree cp_parser_enum_specifier
1511 PARAMS ((cp_parser *));
1512 static void cp_parser_enumerator_list
1513 PARAMS ((cp_parser *, tree));
1514 static void cp_parser_enumerator_definition
1515 PARAMS ((cp_parser *, tree));
1516 static tree cp_parser_namespace_name
1517 PARAMS ((cp_parser *));
1518 static void cp_parser_namespace_definition
1519 PARAMS ((cp_parser *));
1520 static void cp_parser_namespace_body
1521 PARAMS ((cp_parser *));
1522 static tree cp_parser_qualified_namespace_specifier
1523 PARAMS ((cp_parser *));
1524 static void cp_parser_namespace_alias_definition
1525 PARAMS ((cp_parser *));
1526 static void cp_parser_using_declaration
1527 PARAMS ((cp_parser *));
1528 static void cp_parser_using_directive
1529 PARAMS ((cp_parser *));
1530 static void cp_parser_asm_definition
1531 PARAMS ((cp_parser *));
1532 static void cp_parser_linkage_specification
1533 PARAMS ((cp_parser *));
1535 /* Declarators [gram.dcl.decl] */
1537 static tree cp_parser_init_declarator
1538 PARAMS ((cp_parser *, tree, tree, tree, bool, bool, bool *));
1539 static tree cp_parser_declarator
1540 PARAMS ((cp_parser *, bool, bool *));
1541 static tree cp_parser_direct_declarator
1542 PARAMS ((cp_parser *, bool, bool *));
1543 static enum tree_code cp_parser_ptr_operator
1544 PARAMS ((cp_parser *, tree *, tree *));
1545 static tree cp_parser_cv_qualifier_seq_opt
1546 PARAMS ((cp_parser *));
1547 static tree cp_parser_cv_qualifier_opt
1548 PARAMS ((cp_parser *));
1549 static tree cp_parser_declarator_id
1550 PARAMS ((cp_parser *));
1551 static tree cp_parser_type_id
1552 PARAMS ((cp_parser *));
1553 static tree cp_parser_type_specifier_seq
1554 PARAMS ((cp_parser *));
1555 static tree cp_parser_parameter_declaration_clause
1556 PARAMS ((cp_parser *));
1557 static tree cp_parser_parameter_declaration_list
1558 PARAMS ((cp_parser *));
1559 static tree cp_parser_parameter_declaration
1560 PARAMS ((cp_parser *, bool));
1561 static tree cp_parser_function_definition
1562 PARAMS ((cp_parser *, bool *));
1563 static void cp_parser_function_body
1565 static tree cp_parser_initializer
1566 PARAMS ((cp_parser *, bool *));
1567 static tree cp_parser_initializer_clause
1568 PARAMS ((cp_parser *));
1569 static tree cp_parser_initializer_list
1570 PARAMS ((cp_parser *));
1572 static bool cp_parser_ctor_initializer_opt_and_function_body
1575 /* Classes [gram.class] */
1577 static tree cp_parser_class_name
1578 (cp_parser *, bool, bool, bool, bool, bool, bool);
1579 static tree cp_parser_class_specifier
1580 PARAMS ((cp_parser *));
1581 static tree cp_parser_class_head
1582 PARAMS ((cp_parser *, bool *, bool *, tree *));
1583 static enum tag_types cp_parser_class_key
1584 PARAMS ((cp_parser *));
1585 static void cp_parser_member_specification_opt
1586 PARAMS ((cp_parser *));
1587 static void cp_parser_member_declaration
1588 PARAMS ((cp_parser *));
1589 static tree cp_parser_pure_specifier
1590 PARAMS ((cp_parser *));
1591 static tree cp_parser_constant_initializer
1592 PARAMS ((cp_parser *));
1594 /* Derived classes [gram.class.derived] */
1596 static tree cp_parser_base_clause
1597 PARAMS ((cp_parser *));
1598 static tree cp_parser_base_specifier
1599 PARAMS ((cp_parser *));
1601 /* Special member functions [gram.special] */
1603 static tree cp_parser_conversion_function_id
1604 PARAMS ((cp_parser *));
1605 static tree cp_parser_conversion_type_id
1606 PARAMS ((cp_parser *));
1607 static tree cp_parser_conversion_declarator_opt
1608 PARAMS ((cp_parser *));
1609 static bool cp_parser_ctor_initializer_opt
1610 PARAMS ((cp_parser *));
1611 static void cp_parser_mem_initializer_list
1612 PARAMS ((cp_parser *));
1613 static tree cp_parser_mem_initializer
1614 PARAMS ((cp_parser *));
1615 static tree cp_parser_mem_initializer_id
1616 PARAMS ((cp_parser *));
1618 /* Overloading [gram.over] */
1620 static tree cp_parser_operator_function_id
1621 PARAMS ((cp_parser *));
1622 static tree cp_parser_operator
1623 PARAMS ((cp_parser *));
1625 /* Templates [gram.temp] */
1627 static void cp_parser_template_declaration
1628 PARAMS ((cp_parser *, bool));
1629 static tree cp_parser_template_parameter_list
1630 PARAMS ((cp_parser *));
1631 static tree cp_parser_template_parameter
1632 PARAMS ((cp_parser *));
1633 static tree cp_parser_type_parameter
1634 PARAMS ((cp_parser *));
1635 static tree cp_parser_template_id
1636 PARAMS ((cp_parser *, bool, bool));
1637 static tree cp_parser_template_name
1638 PARAMS ((cp_parser *, bool, bool));
1639 static tree cp_parser_template_argument_list
1640 PARAMS ((cp_parser *));
1641 static tree cp_parser_template_argument
1642 PARAMS ((cp_parser *));
1643 static void cp_parser_explicit_instantiation
1644 PARAMS ((cp_parser *));
1645 static void cp_parser_explicit_specialization
1646 PARAMS ((cp_parser *));
1648 /* Exception handling [gram.exception] */
1650 static tree cp_parser_try_block
1651 PARAMS ((cp_parser *));
1652 static bool cp_parser_function_try_block
1653 PARAMS ((cp_parser *));
1654 static void cp_parser_handler_seq
1655 PARAMS ((cp_parser *));
1656 static void cp_parser_handler
1657 PARAMS ((cp_parser *));
1658 static tree cp_parser_exception_declaration
1659 PARAMS ((cp_parser *));
1660 static tree cp_parser_throw_expression
1661 PARAMS ((cp_parser *));
1662 static tree cp_parser_exception_specification_opt
1663 PARAMS ((cp_parser *));
1664 static tree cp_parser_type_id_list
1665 PARAMS ((cp_parser *));
1667 /* GNU Extensions */
1669 static tree cp_parser_asm_specification_opt
1670 PARAMS ((cp_parser *));
1671 static tree cp_parser_asm_operand_list
1672 PARAMS ((cp_parser *));
1673 static tree cp_parser_asm_clobber_list
1674 PARAMS ((cp_parser *));
1675 static tree cp_parser_attributes_opt
1676 PARAMS ((cp_parser *));
1677 static tree cp_parser_attribute_list
1678 PARAMS ((cp_parser *));
1679 static bool cp_parser_extension_opt
1680 PARAMS ((cp_parser *, int *));
1681 static void cp_parser_label_declaration
1682 PARAMS ((cp_parser *));
1684 /* Utility Routines */
1686 static tree cp_parser_lookup_name
1687 PARAMS ((cp_parser *, tree, bool, bool, bool));
1688 static tree cp_parser_lookup_name_simple
1689 PARAMS ((cp_parser *, tree));
1690 static tree cp_parser_resolve_typename_type
1691 PARAMS ((cp_parser *, tree));
1692 static tree cp_parser_maybe_treat_template_as_class
1694 static bool cp_parser_check_declarator_template_parameters
1695 PARAMS ((cp_parser *, tree));
1696 static bool cp_parser_check_template_parameters
1697 PARAMS ((cp_parser *, unsigned));
1698 static tree cp_parser_binary_expression
1699 PARAMS ((cp_parser *,
1700 cp_parser_token_tree_map,
1701 cp_parser_expression_fn));
1702 static tree cp_parser_global_scope_opt
1703 PARAMS ((cp_parser *, bool));
1704 static bool cp_parser_constructor_declarator_p
1705 (cp_parser *, bool);
1706 static tree cp_parser_function_definition_from_specifiers_and_declarator
1707 PARAMS ((cp_parser *, tree, tree, tree, tree));
1708 static tree cp_parser_function_definition_after_declarator
1709 PARAMS ((cp_parser *, bool));
1710 static void cp_parser_template_declaration_after_export
1711 PARAMS ((cp_parser *, bool));
1712 static tree cp_parser_single_declaration
1713 PARAMS ((cp_parser *, bool, bool *));
1714 static tree cp_parser_functional_cast
1715 PARAMS ((cp_parser *, tree));
1716 static void cp_parser_late_parsing_for_member
1717 PARAMS ((cp_parser *, tree));
1718 static void cp_parser_late_parsing_default_args
1719 (cp_parser *, tree, tree);
1720 static tree cp_parser_sizeof_operand
1721 PARAMS ((cp_parser *, enum rid));
1722 static bool cp_parser_declares_only_class_p
1723 PARAMS ((cp_parser *));
1724 static bool cp_parser_friend_p
1726 static cp_token *cp_parser_require
1727 PARAMS ((cp_parser *, enum cpp_ttype, const char *));
1728 static cp_token *cp_parser_require_keyword
1729 PARAMS ((cp_parser *, enum rid, const char *));
1730 static bool cp_parser_token_starts_function_definition_p
1731 PARAMS ((cp_token *));
1732 static bool cp_parser_next_token_starts_class_definition_p
1734 static enum tag_types cp_parser_token_is_class_key
1735 PARAMS ((cp_token *));
1736 static void cp_parser_check_class_key
1737 (enum tag_types, tree type);
1738 static bool cp_parser_optional_template_keyword
1740 static void cp_parser_cache_group
1741 (cp_parser *, cp_token_cache *, enum cpp_ttype, unsigned);
1742 static void cp_parser_parse_tentatively
1743 PARAMS ((cp_parser *));
1744 static void cp_parser_commit_to_tentative_parse
1745 PARAMS ((cp_parser *));
1746 static void cp_parser_abort_tentative_parse
1747 PARAMS ((cp_parser *));
1748 static bool cp_parser_parse_definitely
1749 PARAMS ((cp_parser *));
1750 static bool cp_parser_parsing_tentatively
1751 PARAMS ((cp_parser *));
1752 static bool cp_parser_committed_to_tentative_parse
1753 PARAMS ((cp_parser *));
1754 static void cp_parser_error
1755 PARAMS ((cp_parser *, const char *));
1756 static bool cp_parser_simulate_error
1757 PARAMS ((cp_parser *));
1758 static void cp_parser_check_type_definition
1759 PARAMS ((cp_parser *));
1760 static bool cp_parser_skip_to_closing_parenthesis
1761 PARAMS ((cp_parser *));
1762 static bool cp_parser_skip_to_closing_parenthesis_or_comma
1764 static void cp_parser_skip_to_end_of_statement
1765 PARAMS ((cp_parser *));
1766 static void cp_parser_skip_to_end_of_block_or_statement
1767 PARAMS ((cp_parser *));
1768 static void cp_parser_skip_to_closing_brace
1770 static void cp_parser_skip_until_found
1771 PARAMS ((cp_parser *, enum cpp_ttype, const char *));
1772 static bool cp_parser_error_occurred
1773 PARAMS ((cp_parser *));
1774 static bool cp_parser_allow_gnu_extensions_p
1775 PARAMS ((cp_parser *));
1776 static bool cp_parser_is_string_literal
1777 PARAMS ((cp_token *));
1778 static bool cp_parser_is_keyword
1779 PARAMS ((cp_token *, enum rid));
1780 static bool cp_parser_dependent_type_p
1782 static bool cp_parser_value_dependent_expression_p
1784 static bool cp_parser_type_dependent_expression_p
1786 static bool cp_parser_dependent_template_arg_p
1788 static bool cp_parser_dependent_template_id_p
1790 static bool cp_parser_dependent_template_p
1792 static void cp_parser_defer_access_check
1793 (cp_parser *, tree, tree);
1794 static void cp_parser_start_deferring_access_checks
1796 static tree cp_parser_stop_deferring_access_checks
1797 PARAMS ((cp_parser *));
1798 static void cp_parser_perform_deferred_access_checks
1800 static tree cp_parser_scope_through_which_access_occurs
1803 /* Returns non-zero if TOKEN is a string literal. */
1806 cp_parser_is_string_literal (token)
1809 return (token->type == CPP_STRING || token->type == CPP_WSTRING);
1812 /* Returns non-zero if TOKEN is the indicated KEYWORD. */
1815 cp_parser_is_keyword (token, keyword)
1819 return token->keyword == keyword;
1822 /* Returns TRUE if TYPE is dependent, in the sense of
1826 cp_parser_dependent_type_p (type)
1831 if (!processing_template_decl)
1834 /* If the type is NULL, we have not computed a type for the entity
1835 in question; in that case, the type is dependent. */
1839 /* Erroneous types can be considered non-dependent. */
1840 if (type == error_mark_node)
1845 A type is dependent if it is:
1847 -- a template parameter. */
1848 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
1850 /* -- a qualified-id with a nested-name-specifier which contains a
1851 class-name that names a dependent type or whose unqualified-id
1852 names a dependent type. */
1853 if (TREE_CODE (type) == TYPENAME_TYPE)
1855 /* -- a cv-qualified type where the cv-unqualified type is
1857 type = TYPE_MAIN_VARIANT (type);
1858 /* -- a compound type constructed from any dependent type. */
1859 if (TYPE_PTRMEM_P (type) || TYPE_PTRMEMFUNC_P (type))
1860 return (cp_parser_dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type))
1861 || cp_parser_dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
1863 else if (TREE_CODE (type) == POINTER_TYPE
1864 || TREE_CODE (type) == REFERENCE_TYPE)
1865 return cp_parser_dependent_type_p (TREE_TYPE (type));
1866 else if (TREE_CODE (type) == FUNCTION_TYPE
1867 || TREE_CODE (type) == METHOD_TYPE)
1871 if (cp_parser_dependent_type_p (TREE_TYPE (type)))
1873 for (arg_type = TYPE_ARG_TYPES (type);
1875 arg_type = TREE_CHAIN (arg_type))
1876 if (cp_parser_dependent_type_p (TREE_VALUE (arg_type)))
1880 /* -- an array type constructed from any dependent type or whose
1881 size is specified by a constant expression that is
1883 if (TREE_CODE (type) == ARRAY_TYPE)
1885 if (TYPE_DOMAIN (TREE_TYPE (type))
1886 && ((cp_parser_value_dependent_expression_p
1887 (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
1888 || (cp_parser_type_dependent_expression_p
1889 (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))))
1891 return cp_parser_dependent_type_p (TREE_TYPE (type));
1893 /* -- a template-id in which either the template name is a template
1894 parameter or any of the template arguments is a dependent type or
1895 an expression that is type-dependent or value-dependent.
1897 This language seems somewhat confused; for example, it does not
1898 discuss template template arguments. Therefore, we use the
1899 definition for dependent template arguments in [temp.dep.temp]. */
1900 if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INFO (type)
1901 && (cp_parser_dependent_template_id_p
1902 (CLASSTYPE_TI_TEMPLATE (type),
1903 CLASSTYPE_TI_ARGS (type))))
1905 else if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
1907 /* All TYPEOF_TYPEs are dependent; if the argument of the `typeof'
1908 expression is not type-dependent, then it should already been
1910 if (TREE_CODE (type) == TYPEOF_TYPE)
1912 /* The standard does not specifically mention types that are local
1913 to template functions or local classes, but they should be
1914 considered dependent too. For example:
1916 template <int I> void f() {
1921 The size of `E' cannot be known until the value of `I' has been
1922 determined. Therefore, `E' must be considered dependent. */
1923 scope = TYPE_CONTEXT (type);
1924 if (scope && TYPE_P (scope))
1925 return cp_parser_dependent_type_p (scope);
1926 else if (scope && TREE_CODE (scope) == FUNCTION_DECL)
1927 return cp_parser_type_dependent_expression_p (scope);
1929 /* Other types are non-dependent. */
1933 /* Returns TRUE if the EXPRESSION is value-dependent. */
1936 cp_parser_value_dependent_expression_p (tree expression)
1938 if (!processing_template_decl)
1941 /* A name declared with a dependent type. */
1942 if (DECL_P (expression)
1943 && cp_parser_dependent_type_p (TREE_TYPE (expression)))
1945 /* A non-type template parameter. */
1946 if ((TREE_CODE (expression) == CONST_DECL
1947 && DECL_TEMPLATE_PARM_P (expression))
1948 || TREE_CODE (expression) == TEMPLATE_PARM_INDEX)
1950 /* A constant with integral or enumeration type and is initialized
1951 with an expression that is value-dependent. */
1952 if (TREE_CODE (expression) == VAR_DECL
1953 && DECL_INITIAL (expression)
1954 && (CP_INTEGRAL_TYPE_P (TREE_TYPE (expression))
1955 || TREE_CODE (TREE_TYPE (expression)) == ENUMERAL_TYPE)
1956 && cp_parser_value_dependent_expression_p (DECL_INITIAL (expression)))
1958 /* These expressions are value-dependent if the type to which the
1959 cast occurs is dependent. */
1960 if ((TREE_CODE (expression) == DYNAMIC_CAST_EXPR
1961 || TREE_CODE (expression) == STATIC_CAST_EXPR
1962 || TREE_CODE (expression) == CONST_CAST_EXPR
1963 || TREE_CODE (expression) == REINTERPRET_CAST_EXPR
1964 || TREE_CODE (expression) == CAST_EXPR)
1965 && cp_parser_dependent_type_p (TREE_TYPE (expression)))
1967 /* A `sizeof' expression where the sizeof operand is a type is
1968 value-dependent if the type is dependent. If the type was not
1969 dependent, we would no longer have a SIZEOF_EXPR, so any
1970 SIZEOF_EXPR is dependent. */
1971 if (TREE_CODE (expression) == SIZEOF_EXPR)
1973 /* A constant expression is value-dependent if any subexpression is
1975 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (expression))))
1977 switch (TREE_CODE_CLASS (TREE_CODE (expression)))
1980 return (cp_parser_value_dependent_expression_p
1981 (TREE_OPERAND (expression, 0)));
1984 return ((cp_parser_value_dependent_expression_p
1985 (TREE_OPERAND (expression, 0)))
1986 || (cp_parser_value_dependent_expression_p
1987 (TREE_OPERAND (expression, 1))));
1992 i < TREE_CODE_LENGTH (TREE_CODE (expression));
1994 if (cp_parser_value_dependent_expression_p
1995 (TREE_OPERAND (expression, i)))
2002 /* The expression is not value-dependent. */
2006 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
2010 cp_parser_type_dependent_expression_p (expression)
2013 if (!processing_template_decl)
2016 /* Some expression forms are never type-dependent. */
2017 if (TREE_CODE (expression) == PSEUDO_DTOR_EXPR
2018 || TREE_CODE (expression) == SIZEOF_EXPR
2019 || TREE_CODE (expression) == ALIGNOF_EXPR
2020 || TREE_CODE (expression) == TYPEID_EXPR
2021 || TREE_CODE (expression) == DELETE_EXPR
2022 || TREE_CODE (expression) == VEC_DELETE_EXPR
2023 || TREE_CODE (expression) == THROW_EXPR)
2026 /* The types of these expressions depends only on the type to which
2028 if (TREE_CODE (expression) == DYNAMIC_CAST_EXPR
2029 || TREE_CODE (expression) == STATIC_CAST_EXPR
2030 || TREE_CODE (expression) == CONST_CAST_EXPR
2031 || TREE_CODE (expression) == REINTERPRET_CAST_EXPR
2032 || TREE_CODE (expression) == CAST_EXPR)
2033 return cp_parser_dependent_type_p (TREE_TYPE (expression));
2034 /* The types of these expressions depends only on the type created
2035 by the expression. */
2036 else if (TREE_CODE (expression) == NEW_EXPR
2037 || TREE_CODE (expression) == VEC_NEW_EXPR)
2038 return cp_parser_dependent_type_p (TREE_OPERAND (expression, 1));
2040 if (TREE_CODE (expression) == FUNCTION_DECL
2041 && DECL_LANG_SPECIFIC (expression)
2042 && DECL_TEMPLATE_INFO (expression)
2043 && (cp_parser_dependent_template_id_p
2044 (DECL_TI_TEMPLATE (expression),
2045 INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression)))))
2048 return (cp_parser_dependent_type_p (TREE_TYPE (expression)));
2051 /* Returns TRUE if the ARG (a template argument) is dependent. */
2054 cp_parser_dependent_template_arg_p (tree arg)
2056 if (!processing_template_decl)
2059 if (TREE_CODE (arg) == TEMPLATE_DECL
2060 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
2061 return cp_parser_dependent_template_p (arg);
2062 else if (TYPE_P (arg))
2063 return cp_parser_dependent_type_p (arg);
2065 return (cp_parser_type_dependent_expression_p (arg)
2066 || cp_parser_value_dependent_expression_p (arg));
2069 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
2072 cp_parser_dependent_template_id_p (tree tmpl, tree args)
2076 if (cp_parser_dependent_template_p (tmpl))
2078 for (i = 0; i < TREE_VEC_LENGTH (args); ++i)
2079 if (cp_parser_dependent_template_arg_p (TREE_VEC_ELT (args, i)))
2084 /* Returns TRUE if the template TMPL is dependent. */
2087 cp_parser_dependent_template_p (tree tmpl)
2089 /* Template template parameters are dependent. */
2090 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)
2091 || TREE_CODE (tmpl) == TEMPLATE_TEMPLATE_PARM)
2093 /* So are member templates of dependent classes. */
2094 if (TYPE_P (CP_DECL_CONTEXT (tmpl)))
2095 return cp_parser_dependent_type_p (DECL_CONTEXT (tmpl));
2099 /* Defer checking the accessibility of DECL, when looked up in
2103 cp_parser_defer_access_check (cp_parser *parser,
2109 /* If we are not supposed to defer access checks, just check now. */
2110 if (!parser->context->deferring_access_checks_p)
2112 enforce_access (class_type, decl);
2116 /* See if we are already going to perform this check. */
2117 for (check = parser->context->deferred_access_checks;
2119 check = TREE_CHAIN (check))
2120 if (TREE_VALUE (check) == decl
2121 && same_type_p (TREE_PURPOSE (check), class_type))
2123 /* If not, record the check. */
2124 parser->context->deferred_access_checks
2125 = tree_cons (class_type, decl, parser->context->deferred_access_checks);
2128 /* Start deferring access control checks. */
2131 cp_parser_start_deferring_access_checks (cp_parser *parser)
2133 parser->context->deferring_access_checks_p = true;
2136 /* Stop deferring access control checks. Returns a TREE_LIST
2137 representing the deferred checks. The TREE_PURPOSE of each node is
2138 the type through which the access occurred; the TREE_VALUE is the
2139 declaration named. */
2142 cp_parser_stop_deferring_access_checks (parser)
2147 parser->context->deferring_access_checks_p = false;
2148 access_checks = parser->context->deferred_access_checks;
2149 parser->context->deferred_access_checks = NULL_TREE;
2151 return access_checks;
2154 /* Perform the deferred ACCESS_CHECKS, whose representation is as
2155 documented with cp_parser_stop_deferrring_access_checks. */
2158 cp_parser_perform_deferred_access_checks (access_checks)
2161 tree deferred_check;
2163 /* Look through all the deferred checks. */
2164 for (deferred_check = access_checks;
2166 deferred_check = TREE_CHAIN (deferred_check))
2168 enforce_access (TREE_PURPOSE (deferred_check),
2169 TREE_VALUE (deferred_check));
2172 /* Returns the scope through which DECL is being accessed, or
2173 NULL_TREE if DECL is not a member. If OBJECT_TYPE is non-NULL, we
2174 have just seen `x->' or `x.' and OBJECT_TYPE is the type of `*x',
2175 or `x', respectively. If the DECL was named as `A::B' then
2176 NESTED_NAME_SPECIFIER is `A'. */
2179 cp_parser_scope_through_which_access_occurs (decl,
2181 nested_name_specifier)
2184 tree nested_name_specifier;
2187 tree qualifying_type = NULL_TREE;
2189 /* Determine the SCOPE of DECL. */
2190 scope = context_for_name_lookup (decl);
2191 /* If the SCOPE is not a type, then DECL is not a member. */
2192 if (!TYPE_P (scope))
2194 /* Figure out the type through which DECL is being accessed. */
2195 if (object_type && DERIVED_FROM_P (scope, object_type))
2196 /* If we are processing a `->' or `.' expression, use the type of the
2198 qualifying_type = object_type;
2199 else if (nested_name_specifier)
2201 /* If the reference is to a non-static member of the
2202 current class, treat it as if it were referenced through
2204 if (DECL_NONSTATIC_MEMBER_P (decl)
2205 && current_class_ptr
2206 && DERIVED_FROM_P (scope, current_class_type))
2207 qualifying_type = current_class_type;
2208 /* Otherwise, use the type indicated by the
2209 nested-name-specifier. */
2211 qualifying_type = nested_name_specifier;
2214 /* Otherwise, the name must be from the current class or one of
2216 qualifying_type = currently_open_derived_class (scope);
2218 return qualifying_type;
2221 /* Issue the indicated error MESSAGE. */
2224 cp_parser_error (parser, message)
2226 const char *message;
2228 /* Output the MESSAGE -- unless we're parsing tentatively. */
2229 if (!cp_parser_simulate_error (parser))
2233 /* If we are parsing tentatively, remember that an error has occurred
2234 during this tentative parse. Returns true if the error was
2235 simulated; false if a messgae should be issued by the caller. */
2238 cp_parser_simulate_error (parser)
2241 if (cp_parser_parsing_tentatively (parser)
2242 && !cp_parser_committed_to_tentative_parse (parser))
2244 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2250 /* This function is called when a type is defined. If type
2251 definitions are forbidden at this point, an error message is
2255 cp_parser_check_type_definition (parser)
2258 /* If types are forbidden here, issue a message. */
2259 if (parser->type_definition_forbidden_message)
2260 /* Use `%s' to print the string in case there are any escape
2261 characters in the message. */
2262 error ("%s", parser->type_definition_forbidden_message);
2265 /* Consume tokens up to, and including, the next non-nested closing `)'.
2266 Returns TRUE iff we found a closing `)'. */
2269 cp_parser_skip_to_closing_parenthesis (cp_parser *parser)
2271 unsigned nesting_depth = 0;
2277 /* If we've run out of tokens, then there is no closing `)'. */
2278 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2280 /* Consume the token. */
2281 token = cp_lexer_consume_token (parser->lexer);
2282 /* If it is an `(', we have entered another level of nesting. */
2283 if (token->type == CPP_OPEN_PAREN)
2285 /* If it is a `)', then we might be done. */
2286 else if (token->type == CPP_CLOSE_PAREN && nesting_depth-- == 0)
2291 /* Consume tokens until the next token is a `)', or a `,'. Returns
2292 TRUE if the next token is a `,'. */
2295 cp_parser_skip_to_closing_parenthesis_or_comma (cp_parser *parser)
2297 unsigned nesting_depth = 0;
2301 cp_token *token = cp_lexer_peek_token (parser->lexer);
2303 /* If we've run out of tokens, then there is no closing `)'. */
2304 if (token->type == CPP_EOF)
2306 /* If it is a `,' stop. */
2307 else if (token->type == CPP_COMMA && nesting_depth-- == 0)
2309 /* If it is a `)', stop. */
2310 else if (token->type == CPP_CLOSE_PAREN && nesting_depth-- == 0)
2312 /* If it is an `(', we have entered another level of nesting. */
2313 else if (token->type == CPP_OPEN_PAREN)
2315 /* Consume the token. */
2316 token = cp_lexer_consume_token (parser->lexer);
2320 /* Consume tokens until we reach the end of the current statement.
2321 Normally, that will be just before consuming a `;'. However, if a
2322 non-nested `}' comes first, then we stop before consuming that. */
2325 cp_parser_skip_to_end_of_statement (parser)
2328 unsigned nesting_depth = 0;
2334 /* Peek at the next token. */
2335 token = cp_lexer_peek_token (parser->lexer);
2336 /* If we've run out of tokens, stop. */
2337 if (token->type == CPP_EOF)
2339 /* If the next token is a `;', we have reached the end of the
2341 if (token->type == CPP_SEMICOLON && !nesting_depth)
2343 /* If the next token is a non-nested `}', then we have reached
2344 the end of the current block. */
2345 if (token->type == CPP_CLOSE_BRACE)
2347 /* If this is a non-nested `}', stop before consuming it.
2348 That way, when confronted with something like:
2352 we stop before consuming the closing `}', even though we
2353 have not yet reached a `;'. */
2354 if (nesting_depth == 0)
2356 /* If it is the closing `}' for a block that we have
2357 scanned, stop -- but only after consuming the token.
2363 we will stop after the body of the erroneously declared
2364 function, but before consuming the following `typedef'
2366 if (--nesting_depth == 0)
2368 cp_lexer_consume_token (parser->lexer);
2372 /* If it the next token is a `{', then we are entering a new
2373 block. Consume the entire block. */
2374 else if (token->type == CPP_OPEN_BRACE)
2376 /* Consume the token. */
2377 cp_lexer_consume_token (parser->lexer);
2381 /* Skip tokens until we have consumed an entire block, or until we
2382 have consumed a non-nested `;'. */
2385 cp_parser_skip_to_end_of_block_or_statement (parser)
2388 unsigned nesting_depth = 0;
2394 /* Peek at the next token. */
2395 token = cp_lexer_peek_token (parser->lexer);
2396 /* If we've run out of tokens, stop. */
2397 if (token->type == CPP_EOF)
2399 /* If the next token is a `;', we have reached the end of the
2401 if (token->type == CPP_SEMICOLON && !nesting_depth)
2403 /* Consume the `;'. */
2404 cp_lexer_consume_token (parser->lexer);
2407 /* Consume the token. */
2408 token = cp_lexer_consume_token (parser->lexer);
2409 /* If the next token is a non-nested `}', then we have reached
2410 the end of the current block. */
2411 if (token->type == CPP_CLOSE_BRACE
2412 && (nesting_depth == 0 || --nesting_depth == 0))
2414 /* If it the next token is a `{', then we are entering a new
2415 block. Consume the entire block. */
2416 if (token->type == CPP_OPEN_BRACE)
2421 /* Skip tokens until a non-nested closing curly brace is the next
2425 cp_parser_skip_to_closing_brace (cp_parser *parser)
2427 unsigned nesting_depth = 0;
2433 /* Peek at the next token. */
2434 token = cp_lexer_peek_token (parser->lexer);
2435 /* If we've run out of tokens, stop. */
2436 if (token->type == CPP_EOF)
2438 /* If the next token is a non-nested `}', then we have reached
2439 the end of the current block. */
2440 if (token->type == CPP_CLOSE_BRACE && nesting_depth-- == 0)
2442 /* If it the next token is a `{', then we are entering a new
2443 block. Consume the entire block. */
2444 else if (token->type == CPP_OPEN_BRACE)
2446 /* Consume the token. */
2447 cp_lexer_consume_token (parser->lexer);
2451 /* Create a new C++ parser. */
2458 parser = (cp_parser *) ggc_alloc_cleared (sizeof (cp_parser));
2459 parser->lexer = cp_lexer_new (/*main_lexer_p=*/true);
2460 parser->context = cp_parser_context_new (NULL);
2462 /* For now, we always accept GNU extensions. */
2463 parser->allow_gnu_extensions_p = 1;
2465 /* The `>' token is a greater-than operator, not the end of a
2467 parser->greater_than_is_operator_p = true;
2469 parser->default_arg_ok_p = true;
2471 /* We are not parsing a constant-expression. */
2472 parser->constant_expression_p = false;
2474 /* Local variable names are not forbidden. */
2475 parser->local_variables_forbidden_p = false;
2477 /* We are not procesing an `extern "C"' declaration. */
2478 parser->in_unbraced_linkage_specification_p = false;
2480 /* We are not processing a declarator. */
2481 parser->in_declarator_p = false;
2483 /* There are no default args to process. */
2484 parser->default_arg_types = NULL;
2486 /* The unparsed function queue is empty. */
2487 parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2489 /* There are no classes being defined. */
2490 parser->num_classes_being_defined = 0;
2492 /* No template parameters apply. */
2493 parser->num_template_parameter_lists = 0;
2498 /* Lexical conventions [gram.lex] */
2500 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
2504 cp_parser_identifier (parser)
2509 /* Look for the identifier. */
2510 token = cp_parser_require (parser, CPP_NAME, "identifier");
2511 /* Return the value. */
2512 return token ? token->value : error_mark_node;
2515 /* Basic concepts [gram.basic] */
2517 /* Parse a translation-unit.
2520 declaration-seq [opt]
2522 Returns TRUE if all went well. */
2525 cp_parser_translation_unit (parser)
2530 cp_parser_declaration_seq_opt (parser);
2532 /* If there are no tokens left then all went well. */
2533 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2536 /* Otherwise, issue an error message. */
2537 cp_parser_error (parser, "expected declaration");
2541 /* Consume the EOF token. */
2542 cp_parser_require (parser, CPP_EOF, "end-of-file");
2545 finish_translation_unit ();
2547 /* All went well. */
2551 /* Expressions [gram.expr] */
2553 /* Parse a primary-expression.
2564 ( compound-statement )
2565 __builtin_va_arg ( assignment-expression , type-id )
2570 Returns a representation of the expression.
2572 *IDK indicates what kind of id-expression (if any) was present.
2574 *QUALIFYING_CLASS is set to a non-NULL value if the id-expression can be
2575 used as the operand of a pointer-to-member. In that case,
2576 *QUALIFYING_CLASS gives the class that is used as the qualifying
2577 class in the pointer-to-member. */
2580 cp_parser_primary_expression (cp_parser *parser,
2581 cp_parser_id_kind *idk,
2582 tree *qualifying_class)
2586 /* Assume the primary expression is not an id-expression. */
2587 *idk = CP_PARSER_ID_KIND_NONE;
2588 /* And that it cannot be used as pointer-to-member. */
2589 *qualifying_class = NULL_TREE;
2591 /* Peek at the next token. */
2592 token = cp_lexer_peek_token (parser->lexer);
2593 switch (token->type)
2606 token = cp_lexer_consume_token (parser->lexer);
2607 return token->value;
2609 case CPP_OPEN_PAREN:
2612 bool saved_greater_than_is_operator_p;
2614 /* Consume the `('. */
2615 cp_lexer_consume_token (parser->lexer);
2616 /* Within a parenthesized expression, a `>' token is always
2617 the greater-than operator. */
2618 saved_greater_than_is_operator_p
2619 = parser->greater_than_is_operator_p;
2620 parser->greater_than_is_operator_p = true;
2621 /* If we see `( { ' then we are looking at the beginning of
2622 a GNU statement-expression. */
2623 if (cp_parser_allow_gnu_extensions_p (parser)
2624 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
2626 /* Statement-expressions are not allowed by the standard. */
2628 pedwarn ("ISO C++ forbids braced-groups within expressions");
2630 /* And they're not allowed outside of a function-body; you
2631 cannot, for example, write:
2633 int i = ({ int j = 3; j + 1; });
2635 at class or namespace scope. */
2636 if (!at_function_scope_p ())
2637 error ("statement-expressions are allowed only inside functions");
2638 /* Start the statement-expression. */
2639 expr = begin_stmt_expr ();
2640 /* Parse the compound-statement. */
2641 cp_parser_compound_statement (parser);
2643 expr = finish_stmt_expr (expr);
2647 /* Parse the parenthesized expression. */
2648 expr = cp_parser_expression (parser);
2649 /* Let the front end know that this expression was
2650 enclosed in parentheses. This matters in case, for
2651 example, the expression is of the form `A::B', since
2652 `&A::B' might be a pointer-to-member, but `&(A::B)' is
2654 finish_parenthesized_expr (expr);
2656 /* The `>' token might be the end of a template-id or
2657 template-parameter-list now. */
2658 parser->greater_than_is_operator_p
2659 = saved_greater_than_is_operator_p;
2660 /* Consume the `)'. */
2661 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
2662 cp_parser_skip_to_end_of_statement (parser);
2668 switch (token->keyword)
2670 /* These two are the boolean literals. */
2672 cp_lexer_consume_token (parser->lexer);
2673 return boolean_true_node;
2675 cp_lexer_consume_token (parser->lexer);
2676 return boolean_false_node;
2678 /* The `__null' literal. */
2680 cp_lexer_consume_token (parser->lexer);
2683 /* Recognize the `this' keyword. */
2685 cp_lexer_consume_token (parser->lexer);
2686 if (parser->local_variables_forbidden_p)
2688 error ("`this' may not be used in this context");
2689 return error_mark_node;
2691 return finish_this_expr ();
2693 /* The `operator' keyword can be the beginning of an
2698 case RID_FUNCTION_NAME:
2699 case RID_PRETTY_FUNCTION_NAME:
2700 case RID_C99_FUNCTION_NAME:
2701 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
2702 __func__ are the names of variables -- but they are
2703 treated specially. Therefore, they are handled here,
2704 rather than relying on the generic id-expression logic
2705 below. Gramatically, these names are id-expressions.
2707 Consume the token. */
2708 token = cp_lexer_consume_token (parser->lexer);
2709 /* Look up the name. */
2710 return finish_fname (token->value);
2717 /* The `__builtin_va_arg' construct is used to handle
2718 `va_arg'. Consume the `__builtin_va_arg' token. */
2719 cp_lexer_consume_token (parser->lexer);
2720 /* Look for the opening `('. */
2721 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
2722 /* Now, parse the assignment-expression. */
2723 expression = cp_parser_assignment_expression (parser);
2724 /* Look for the `,'. */
2725 cp_parser_require (parser, CPP_COMMA, "`,'");
2726 /* Parse the type-id. */
2727 type = cp_parser_type_id (parser);
2728 /* Look for the closing `)'. */
2729 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
2731 return build_x_va_arg (expression, type);
2735 cp_parser_error (parser, "expected primary-expression");
2736 return error_mark_node;
2740 /* An id-expression can start with either an identifier, a
2741 `::' as the beginning of a qualified-id, or the "operator"
2745 case CPP_TEMPLATE_ID:
2746 case CPP_NESTED_NAME_SPECIFIER:
2752 /* Parse the id-expression. */
2754 = cp_parser_id_expression (parser,
2755 /*template_keyword_p=*/false,
2756 /*check_dependency_p=*/true,
2757 /*template_p=*/NULL);
2758 if (id_expression == error_mark_node)
2759 return error_mark_node;
2760 /* If we have a template-id, then no further lookup is
2761 required. If the template-id was for a template-class, we
2762 will sometimes have a TYPE_DECL at this point. */
2763 else if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
2764 || TREE_CODE (id_expression) == TYPE_DECL)
2765 decl = id_expression;
2766 /* Look up the name. */
2769 decl = cp_parser_lookup_name_simple (parser, id_expression);
2770 /* If name lookup gives us a SCOPE_REF, then the
2771 qualifying scope was dependent. Just propagate the
2773 if (TREE_CODE (decl) == SCOPE_REF)
2775 if (TYPE_P (TREE_OPERAND (decl, 0)))
2776 *qualifying_class = TREE_OPERAND (decl, 0);
2779 /* Check to see if DECL is a local variable in a context
2780 where that is forbidden. */
2781 if (parser->local_variables_forbidden_p
2782 && local_variable_p (decl))
2784 /* It might be that we only found DECL because we are
2785 trying to be generous with pre-ISO scoping rules.
2786 For example, consider:
2790 for (int i = 0; i < 10; ++i) {}
2791 extern void f(int j = i);
2794 Here, name look up will originally find the out
2795 of scope `i'. We need to issue a warning message,
2796 but then use the global `i'. */
2797 decl = check_for_out_of_scope_variable (decl);
2798 if (local_variable_p (decl))
2800 error ("local variable `%D' may not appear in this context",
2802 return error_mark_node;
2806 /* If unqualified name lookup fails while processing a
2807 template, that just means that we need to do name
2808 lookup again when the template is instantiated. */
2810 && decl == error_mark_node
2811 && processing_template_decl)
2813 *idk = CP_PARSER_ID_KIND_UNQUALIFIED;
2814 return build_min_nt (LOOKUP_EXPR, id_expression);
2816 else if (decl == error_mark_node
2817 && !processing_template_decl)
2821 /* It may be resolvable as a koenig lookup function
2823 *idk = CP_PARSER_ID_KIND_UNQUALIFIED;
2824 return id_expression;
2826 else if (TYPE_P (parser->scope)
2827 && !COMPLETE_TYPE_P (parser->scope))
2828 error ("incomplete type `%T' used in nested name specifier",
2830 else if (parser->scope != global_namespace)
2831 error ("`%D' is not a member of `%D'",
2832 id_expression, parser->scope);
2834 error ("`::%D' has not been declared", id_expression);
2836 /* If DECL is a variable would be out of scope under
2837 ANSI/ISO rules, but in scope in the ARM, name lookup
2838 will succeed. Issue a diagnostic here. */
2840 decl = check_for_out_of_scope_variable (decl);
2842 /* Remember that the name was used in the definition of
2843 the current class so that we can check later to see if
2844 the meaning would have been different after the class
2845 was entirely defined. */
2846 if (!parser->scope && decl != error_mark_node)
2847 maybe_note_name_used_in_class (id_expression, decl);
2850 /* If we didn't find anything, or what we found was a type,
2851 then this wasn't really an id-expression. */
2852 if (TREE_CODE (decl) == TYPE_DECL
2853 || TREE_CODE (decl) == NAMESPACE_DECL
2854 || (TREE_CODE (decl) == TEMPLATE_DECL
2855 && !DECL_FUNCTION_TEMPLATE_P (decl)))
2857 cp_parser_error (parser,
2858 "expected primary-expression");
2859 return error_mark_node;
2862 /* If the name resolved to a template parameter, there is no
2863 need to look it up again later. Similarly, we resolve
2864 enumeration constants to their underlying values. */
2865 if (TREE_CODE (decl) == CONST_DECL)
2867 *idk = CP_PARSER_ID_KIND_NONE;
2868 if (DECL_TEMPLATE_PARM_P (decl) || !processing_template_decl)
2869 return DECL_INITIAL (decl);
2876 /* If the declaration was explicitly qualified indicate
2877 that. The semantics of `A::f(3)' are different than
2878 `f(3)' if `f' is virtual. */
2879 *idk = (parser->scope
2880 ? CP_PARSER_ID_KIND_QUALIFIED
2881 : (TREE_CODE (decl) == TEMPLATE_ID_EXPR
2882 ? CP_PARSER_ID_KIND_TEMPLATE_ID
2883 : CP_PARSER_ID_KIND_UNQUALIFIED));
2888 An id-expression is type-dependent if it contains an
2889 identifier that was declared with a dependent type.
2891 As an optimization, we could choose not to create a
2892 LOOKUP_EXPR for a name that resolved to a local
2893 variable in the template function that we are currently
2894 declaring; such a name cannot ever resolve to anything
2895 else. If we did that we would not have to look up
2896 these names at instantiation time.
2898 The standard is not very specific about an
2899 id-expression that names a set of overloaded functions.
2900 What if some of them have dependent types and some of
2901 them do not? Presumably, such a name should be treated
2902 as a dependent name. */
2903 /* Assume the name is not dependent. */
2904 dependent_p = false;
2905 if (!processing_template_decl)
2906 /* No names are dependent outside a template. */
2908 /* A template-id where the name of the template was not
2909 resolved is definitely dependent. */
2910 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
2911 && (TREE_CODE (TREE_OPERAND (decl, 0))
2912 == IDENTIFIER_NODE))
2914 /* For anything except an overloaded function, just check
2916 else if (!is_overloaded_fn (decl))
2918 = cp_parser_dependent_type_p (TREE_TYPE (decl));
2919 /* For a set of overloaded functions, check each of the
2925 if (BASELINK_P (fns))
2926 fns = BASELINK_FUNCTIONS (fns);
2928 /* For a template-id, check to see if the template
2929 arguments are dependent. */
2930 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
2932 tree args = TREE_OPERAND (fns, 1);
2934 if (args && TREE_CODE (args) == TREE_LIST)
2938 if (cp_parser_dependent_template_arg_p
2939 (TREE_VALUE (args)))
2944 args = TREE_CHAIN (args);
2947 else if (args && TREE_CODE (args) == TREE_VEC)
2950 for (i = 0; i < TREE_VEC_LENGTH (args); ++i)
2951 if (cp_parser_dependent_template_arg_p
2952 (TREE_VEC_ELT (args, i)))
2959 /* The functions are those referred to by the
2961 fns = TREE_OPERAND (fns, 0);
2964 /* If there are no dependent template arguments, go
2965 through the overlaoded functions. */
2966 while (fns && !dependent_p)
2968 tree fn = OVL_CURRENT (fns);
2970 /* Member functions of dependent classes are
2972 if (TREE_CODE (fn) == FUNCTION_DECL
2973 && cp_parser_type_dependent_expression_p (fn))
2975 else if (TREE_CODE (fn) == TEMPLATE_DECL
2976 && cp_parser_dependent_template_p (fn))
2979 fns = OVL_NEXT (fns);
2983 /* If the name was dependent on a template parameter,
2984 we will resolve the name at instantiation time. */
2987 /* Create a SCOPE_REF for qualified names. */
2990 if (TYPE_P (parser->scope))
2991 *qualifying_class = parser->scope;
2992 return build_nt (SCOPE_REF,
2996 /* A TEMPLATE_ID already contains all the information
2998 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR)
2999 return id_expression;
3000 /* Create a LOOKUP_EXPR for other unqualified names. */
3001 return build_min_nt (LOOKUP_EXPR, id_expression);
3006 decl = (adjust_result_of_qualified_name_lookup
3007 (decl, parser->scope, current_class_type));
3008 if (TREE_CODE (decl) == FIELD_DECL || BASELINK_P (decl))
3009 *qualifying_class = parser->scope;
3011 /* Resolve references to variables of anonymous unions
3012 into COMPONENT_REFs. */
3013 else if (TREE_CODE (decl) == ALIAS_DECL)
3014 decl = DECL_INITIAL (decl);
3016 /* Transform references to non-static data members into
3018 decl = hack_identifier (decl, id_expression);
3021 if (TREE_DEPRECATED (decl))
3022 warn_deprecated_use (decl);
3027 /* Anything else is an error. */
3029 cp_parser_error (parser, "expected primary-expression");
3030 return error_mark_node;
3034 /* Parse an id-expression.
3041 :: [opt] nested-name-specifier template [opt] unqualified-id
3043 :: operator-function-id
3046 Return a representation of the unqualified portion of the
3047 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
3048 a `::' or nested-name-specifier.
3050 Often, if the id-expression was a qualified-id, the caller will
3051 want to make a SCOPE_REF to represent the qualified-id. This
3052 function does not do this in order to avoid wastefully creating
3053 SCOPE_REFs when they are not required.
3055 If ASSUME_TYPENAME_P is true then we assume that qualified names
3056 are typenames. This flag is set when parsing a declarator-id;
3062 we are supposed to assume that `S<T>::R' is a class.
3064 If TEMPLATE_KEYWORD_P is true, then we have just seen the
3067 If CHECK_DEPENDENCY_P is false, then names are looked up inside
3068 uninstantiated templates.
3070 If *TEMPLATE_KEYWORD_P is non-NULL, it is set to true iff the
3071 `template' keyword is used to explicitly indicate that the entity
3072 named is a template. */
3075 cp_parser_id_expression (cp_parser *parser,
3076 bool template_keyword_p,
3077 bool check_dependency_p,
3080 bool global_scope_p;
3081 bool nested_name_specifier_p;
3083 /* Assume the `template' keyword was not used. */
3085 *template_p = false;
3087 /* Look for the optional `::' operator. */
3089 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
3091 /* Look for the optional nested-name-specifier. */
3092 nested_name_specifier_p
3093 = (cp_parser_nested_name_specifier_opt (parser,
3094 /*typename_keyword_p=*/false,
3098 /* If there is a nested-name-specifier, then we are looking at
3099 the first qualified-id production. */
3100 if (nested_name_specifier_p)
3103 tree saved_object_scope;
3104 tree saved_qualifying_scope;
3105 tree unqualified_id;
3108 /* See if the next token is the `template' keyword. */
3110 template_p = &is_template;
3111 *template_p = cp_parser_optional_template_keyword (parser);
3112 /* Name lookup we do during the processing of the
3113 unqualified-id might obliterate SCOPE. */
3114 saved_scope = parser->scope;
3115 saved_object_scope = parser->object_scope;
3116 saved_qualifying_scope = parser->qualifying_scope;
3117 /* Process the final unqualified-id. */
3118 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
3119 check_dependency_p);
3120 /* Restore the SAVED_SCOPE for our caller. */
3121 parser->scope = saved_scope;
3122 parser->object_scope = saved_object_scope;
3123 parser->qualifying_scope = saved_qualifying_scope;
3125 return unqualified_id;
3127 /* Otherwise, if we are in global scope, then we are looking at one
3128 of the other qualified-id productions. */
3129 else if (global_scope_p)
3134 /* Peek at the next token. */
3135 token = cp_lexer_peek_token (parser->lexer);
3137 /* If it's an identifier, and the next token is not a "<", then
3138 we can avoid the template-id case. This is an optimization
3139 for this common case. */
3140 if (token->type == CPP_NAME
3141 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_LESS)
3142 return cp_parser_identifier (parser);
3144 cp_parser_parse_tentatively (parser);
3145 /* Try a template-id. */
3146 id = cp_parser_template_id (parser,
3147 /*template_keyword_p=*/false,
3148 /*check_dependency_p=*/true);
3149 /* If that worked, we're done. */
3150 if (cp_parser_parse_definitely (parser))
3153 /* Peek at the next token. (Changes in the token buffer may
3154 have invalidated the pointer obtained above.) */
3155 token = cp_lexer_peek_token (parser->lexer);
3157 switch (token->type)
3160 return cp_parser_identifier (parser);
3163 if (token->keyword == RID_OPERATOR)
3164 return cp_parser_operator_function_id (parser);
3168 cp_parser_error (parser, "expected id-expression");
3169 return error_mark_node;
3173 return cp_parser_unqualified_id (parser, template_keyword_p,
3174 /*check_dependency_p=*/true);
3177 /* Parse an unqualified-id.
3181 operator-function-id
3182 conversion-function-id
3186 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3187 keyword, in a construct like `A::template ...'.
3189 Returns a representation of unqualified-id. For the `identifier'
3190 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
3191 production a BIT_NOT_EXPR is returned; the operand of the
3192 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
3193 other productions, see the documentation accompanying the
3194 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
3195 names are looked up in uninstantiated templates. */
3198 cp_parser_unqualified_id (parser, template_keyword_p,
3201 bool template_keyword_p;
3202 bool check_dependency_p;
3206 /* Peek at the next token. */
3207 token = cp_lexer_peek_token (parser->lexer);
3209 switch (token->type)
3215 /* We don't know yet whether or not this will be a
3217 cp_parser_parse_tentatively (parser);
3218 /* Try a template-id. */
3219 id = cp_parser_template_id (parser, template_keyword_p,
3220 check_dependency_p);
3221 /* If it worked, we're done. */
3222 if (cp_parser_parse_definitely (parser))
3224 /* Otherwise, it's an ordinary identifier. */
3225 return cp_parser_identifier (parser);
3228 case CPP_TEMPLATE_ID:
3229 return cp_parser_template_id (parser, template_keyword_p,
3230 check_dependency_p);
3235 tree qualifying_scope;
3239 /* Consume the `~' token. */
3240 cp_lexer_consume_token (parser->lexer);
3241 /* Parse the class-name. The standard, as written, seems to
3244 template <typename T> struct S { ~S (); };
3245 template <typename T> S<T>::~S() {}
3247 is invalid, since `~' must be followed by a class-name, but
3248 `S<T>' is dependent, and so not known to be a class.
3249 That's not right; we need to look in uninstantiated
3250 templates. A further complication arises from:
3252 template <typename T> void f(T t) {
3256 Here, it is not possible to look up `T' in the scope of `T'
3257 itself. We must look in both the current scope, and the
3258 scope of the containing complete expression.
3260 Yet another issue is:
3269 The standard does not seem to say that the `S' in `~S'
3270 should refer to the type `S' and not the data member
3273 /* DR 244 says that we look up the name after the "~" in the
3274 same scope as we looked up the qualifying name. That idea
3275 isn't fully worked out; it's more complicated than that. */
3276 scope = parser->scope;
3277 object_scope = parser->object_scope;
3278 qualifying_scope = parser->qualifying_scope;
3280 /* If the name is of the form "X::~X" it's OK. */
3281 if (scope && TYPE_P (scope)
3282 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
3283 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3285 && (cp_lexer_peek_token (parser->lexer)->value
3286 == TYPE_IDENTIFIER (scope)))
3288 cp_lexer_consume_token (parser->lexer);
3289 return build_nt (BIT_NOT_EXPR, scope);
3292 /* If there was an explicit qualification (S::~T), first look
3293 in the scope given by the qualification (i.e., S). */
3296 cp_parser_parse_tentatively (parser);
3297 type_decl = cp_parser_class_name (parser,
3298 /*typename_keyword_p=*/false,
3299 /*template_keyword_p=*/false,
3301 /*check_access_p=*/true,
3302 /*check_dependency=*/false,
3303 /*class_head_p=*/false);
3304 if (cp_parser_parse_definitely (parser))
3305 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3307 /* In "N::S::~S", look in "N" as well. */
3308 if (scope && qualifying_scope)
3310 cp_parser_parse_tentatively (parser);
3311 parser->scope = qualifying_scope;
3312 parser->object_scope = NULL_TREE;
3313 parser->qualifying_scope = NULL_TREE;
3315 = cp_parser_class_name (parser,
3316 /*typename_keyword_p=*/false,
3317 /*template_keyword_p=*/false,
3319 /*check_access_p=*/true,
3320 /*check_dependency=*/false,
3321 /*class_head_p=*/false);
3322 if (cp_parser_parse_definitely (parser))
3323 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3325 /* In "p->S::~T", look in the scope given by "*p" as well. */
3326 else if (object_scope)
3328 cp_parser_parse_tentatively (parser);
3329 parser->scope = object_scope;
3330 parser->object_scope = NULL_TREE;
3331 parser->qualifying_scope = NULL_TREE;
3333 = cp_parser_class_name (parser,
3334 /*typename_keyword_p=*/false,
3335 /*template_keyword_p=*/false,
3337 /*check_access_p=*/true,
3338 /*check_dependency=*/false,
3339 /*class_head_p=*/false);
3340 if (cp_parser_parse_definitely (parser))
3341 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3343 /* Look in the surrounding context. */
3344 parser->scope = NULL_TREE;
3345 parser->object_scope = NULL_TREE;
3346 parser->qualifying_scope = NULL_TREE;
3348 = cp_parser_class_name (parser,
3349 /*typename_keyword_p=*/false,
3350 /*template_keyword_p=*/false,
3352 /*check_access_p=*/true,
3353 /*check_dependency=*/false,
3354 /*class_head_p=*/false);
3355 /* If an error occurred, assume that the name of the
3356 destructor is the same as the name of the qualifying
3357 class. That allows us to keep parsing after running
3358 into ill-formed destructor names. */
3359 if (type_decl == error_mark_node && scope && TYPE_P (scope))
3360 return build_nt (BIT_NOT_EXPR, scope);
3361 else if (type_decl == error_mark_node)
3362 return error_mark_node;
3364 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3368 if (token->keyword == RID_OPERATOR)
3372 /* This could be a template-id, so we try that first. */
3373 cp_parser_parse_tentatively (parser);
3374 /* Try a template-id. */
3375 id = cp_parser_template_id (parser, template_keyword_p,
3376 /*check_dependency_p=*/true);
3377 /* If that worked, we're done. */
3378 if (cp_parser_parse_definitely (parser))
3380 /* We still don't know whether we're looking at an
3381 operator-function-id or a conversion-function-id. */
3382 cp_parser_parse_tentatively (parser);
3383 /* Try an operator-function-id. */
3384 id = cp_parser_operator_function_id (parser);
3385 /* If that didn't work, try a conversion-function-id. */
3386 if (!cp_parser_parse_definitely (parser))
3387 id = cp_parser_conversion_function_id (parser);
3394 cp_parser_error (parser, "expected unqualified-id");
3395 return error_mark_node;
3399 /* Parse an (optional) nested-name-specifier.
3401 nested-name-specifier:
3402 class-or-namespace-name :: nested-name-specifier [opt]
3403 class-or-namespace-name :: template nested-name-specifier [opt]
3405 PARSER->SCOPE should be set appropriately before this function is
3406 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
3407 effect. TYPE_P is TRUE if we non-type bindings should be ignored
3410 Sets PARSER->SCOPE to the class (TYPE) or namespace
3411 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
3412 it unchanged if there is no nested-name-specifier. Returns the new
3413 scope iff there is a nested-name-specifier, or NULL_TREE otherwise. */
3416 cp_parser_nested_name_specifier_opt (cp_parser *parser,
3417 bool typename_keyword_p,
3418 bool check_dependency_p,
3421 bool success = false;
3422 tree access_check = NULL_TREE;
3425 /* If the next token corresponds to a nested name specifier, there
3426 is no need to reparse it. */
3427 if (cp_lexer_next_token_is (parser->lexer, CPP_NESTED_NAME_SPECIFIER))
3432 /* Get the stored value. */
3433 value = cp_lexer_consume_token (parser->lexer)->value;
3434 /* Perform any access checks that were deferred. */
3435 for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
3436 cp_parser_defer_access_check (parser,
3437 TREE_PURPOSE (check),
3438 TREE_VALUE (check));
3439 /* Set the scope from the stored value. */
3440 parser->scope = TREE_VALUE (value);
3441 parser->qualifying_scope = TREE_TYPE (value);
3442 parser->object_scope = NULL_TREE;
3443 return parser->scope;
3446 /* Remember where the nested-name-specifier starts. */
3447 if (cp_parser_parsing_tentatively (parser)
3448 && !cp_parser_committed_to_tentative_parse (parser))
3450 cp_token *next_token = cp_lexer_peek_token (parser->lexer);
3451 start = cp_lexer_token_difference (parser->lexer,
3452 parser->lexer->first_token,
3454 access_check = parser->context->deferred_access_checks;
3463 tree saved_qualifying_scope;
3465 bool template_keyword_p;
3467 /* Spot cases that cannot be the beginning of a
3468 nested-name-specifier. On the second and subsequent times
3469 through the loop, we look for the `template' keyword. */
3471 && cp_lexer_next_token_is_keyword (parser->lexer,
3474 /* A template-id can start a nested-name-specifier. */
3475 else if (cp_lexer_next_token_is (parser->lexer, CPP_TEMPLATE_ID))
3479 /* If the next token is not an identifier, then it is
3480 definitely not a class-or-namespace-name. */
3481 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
3483 /* If the following token is neither a `<' (to begin a
3484 template-id), nor a `::', then we are not looking at a
3485 nested-name-specifier. */
3486 token = cp_lexer_peek_nth_token (parser->lexer, 2);
3487 if (token->type != CPP_LESS && token->type != CPP_SCOPE)
3491 /* The nested-name-specifier is optional, so we parse
3493 cp_parser_parse_tentatively (parser);
3495 /* Look for the optional `template' keyword, if this isn't the
3496 first time through the loop. */
3498 template_keyword_p = cp_parser_optional_template_keyword (parser);
3500 template_keyword_p = false;
3502 /* Save the old scope since the name lookup we are about to do
3503 might destroy it. */
3504 old_scope = parser->scope;
3505 saved_qualifying_scope = parser->qualifying_scope;
3506 /* Parse the qualifying entity. */
3508 = cp_parser_class_or_namespace_name (parser,
3513 /* Look for the `::' token. */
3514 cp_parser_require (parser, CPP_SCOPE, "`::'");
3516 /* If we found what we wanted, we keep going; otherwise, we're
3518 if (!cp_parser_parse_definitely (parser))
3520 bool error_p = false;
3522 /* Restore the OLD_SCOPE since it was valid before the
3523 failed attempt at finding the last
3524 class-or-namespace-name. */
3525 parser->scope = old_scope;
3526 parser->qualifying_scope = saved_qualifying_scope;
3527 /* If the next token is an identifier, and the one after
3528 that is a `::', then any valid interpretation would have
3529 found a class-or-namespace-name. */
3530 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
3531 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3533 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
3536 token = cp_lexer_consume_token (parser->lexer);
3541 decl = cp_parser_lookup_name_simple (parser, token->value);
3542 if (TREE_CODE (decl) == TEMPLATE_DECL)
3543 error ("`%D' used without template parameters",
3545 else if (parser->scope)
3547 if (TYPE_P (parser->scope))
3548 error ("`%T::%D' is not a class-name or "
3550 parser->scope, token->value);
3552 error ("`%D::%D' is not a class-name or "
3554 parser->scope, token->value);
3557 error ("`%D' is not a class-name or namespace-name",
3559 parser->scope = NULL_TREE;
3562 cp_lexer_consume_token (parser->lexer);
3567 /* We've found one valid nested-name-specifier. */
3569 /* Make sure we look in the right scope the next time through
3571 parser->scope = (TREE_CODE (new_scope) == TYPE_DECL
3572 ? TREE_TYPE (new_scope)
3574 /* If it is a class scope, try to complete it; we are about to
3575 be looking up names inside the class. */
3576 if (TYPE_P (parser->scope))
3577 complete_type (parser->scope);
3580 /* If parsing tentatively, replace the sequence of tokens that makes
3581 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
3582 token. That way, should we re-parse the token stream, we will
3583 not have to repeat the effort required to do the parse, nor will
3584 we issue duplicate error messages. */
3585 if (success && start >= 0)
3590 /* Find the token that corresponds to the start of the
3592 token = cp_lexer_advance_token (parser->lexer,
3593 parser->lexer->first_token,
3596 /* Remember the access checks associated with this
3597 nested-name-specifier. */
3598 c = parser->context->deferred_access_checks;
3599 if (c == access_check)
3600 access_check = NULL_TREE;
3603 while (TREE_CHAIN (c) != access_check)
3605 access_check = parser->context->deferred_access_checks;
3606 parser->context->deferred_access_checks = TREE_CHAIN (c);
3607 TREE_CHAIN (c) = NULL_TREE;
3610 /* Reset the contents of the START token. */
3611 token->type = CPP_NESTED_NAME_SPECIFIER;
3612 token->value = build_tree_list (access_check, parser->scope);
3613 TREE_TYPE (token->value) = parser->qualifying_scope;
3614 token->keyword = RID_MAX;
3615 /* Purge all subsequent tokens. */
3616 cp_lexer_purge_tokens_after (parser->lexer, token);
3619 return success ? parser->scope : NULL_TREE;
3622 /* Parse a nested-name-specifier. See
3623 cp_parser_nested_name_specifier_opt for details. This function
3624 behaves identically, except that it will an issue an error if no
3625 nested-name-specifier is present, and it will return
3626 ERROR_MARK_NODE, rather than NULL_TREE, if no nested-name-specifier
3630 cp_parser_nested_name_specifier (cp_parser *parser,
3631 bool typename_keyword_p,
3632 bool check_dependency_p,
3637 /* Look for the nested-name-specifier. */
3638 scope = cp_parser_nested_name_specifier_opt (parser,
3642 /* If it was not present, issue an error message. */
3645 cp_parser_error (parser, "expected nested-name-specifier");
3646 return error_mark_node;
3652 /* Parse a class-or-namespace-name.
3654 class-or-namespace-name:
3658 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
3659 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
3660 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
3661 TYPE_P is TRUE iff the next name should be taken as a class-name,
3662 even the same name is declared to be another entity in the same
3665 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
3666 specified by the class-or-namespace-name. */
3669 cp_parser_class_or_namespace_name (cp_parser *parser,
3670 bool typename_keyword_p,
3671 bool template_keyword_p,
3672 bool check_dependency_p,
3676 tree saved_qualifying_scope;
3677 tree saved_object_scope;
3680 /* If the next token is the `template' keyword, we know that we are
3681 looking at a class-name. */
3682 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
3683 return cp_parser_class_name (parser,
3687 /*check_access_p=*/true,
3689 /*class_head_p=*/false);
3690 /* Before we try to parse the class-name, we must save away the
3691 current PARSER->SCOPE since cp_parser_class_name will destroy
3693 saved_scope = parser->scope;
3694 saved_qualifying_scope = parser->qualifying_scope;
3695 saved_object_scope = parser->object_scope;
3696 /* Try for a class-name first. */
3697 cp_parser_parse_tentatively (parser);
3698 scope = cp_parser_class_name (parser,
3702 /*check_access_p=*/true,
3704 /*class_head_p=*/false);
3705 /* If that didn't work, try for a namespace-name. */
3706 if (!cp_parser_parse_definitely (parser))
3708 /* Restore the saved scope. */
3709 parser->scope = saved_scope;
3710 parser->qualifying_scope = saved_qualifying_scope;
3711 parser->object_scope = saved_object_scope;
3712 /* Now look for a namespace-name. */
3713 scope = cp_parser_namespace_name (parser);
3719 /* Parse a postfix-expression.
3723 postfix-expression [ expression ]
3724 postfix-expression ( expression-list [opt] )
3725 simple-type-specifier ( expression-list [opt] )
3726 typename :: [opt] nested-name-specifier identifier
3727 ( expression-list [opt] )
3728 typename :: [opt] nested-name-specifier template [opt] template-id
3729 ( expression-list [opt] )
3730 postfix-expression . template [opt] id-expression
3731 postfix-expression -> template [opt] id-expression
3732 postfix-expression . pseudo-destructor-name
3733 postfix-expression -> pseudo-destructor-name
3734 postfix-expression ++
3735 postfix-expression --
3736 dynamic_cast < type-id > ( expression )
3737 static_cast < type-id > ( expression )
3738 reinterpret_cast < type-id > ( expression )
3739 const_cast < type-id > ( expression )
3740 typeid ( expression )
3746 ( type-id ) { initializer-list , [opt] }
3748 This extension is a GNU version of the C99 compound-literal
3749 construct. (The C99 grammar uses `type-name' instead of `type-id',
3750 but they are essentially the same concept.)
3752 If ADDRESS_P is true, the postfix expression is the operand of the
3755 Returns a representation of the expression. */
3758 cp_parser_postfix_expression (cp_parser *parser, bool address_p)
3762 cp_parser_id_kind idk = CP_PARSER_ID_KIND_NONE;
3763 tree postfix_expression = NULL_TREE;
3764 /* Non-NULL only if the current postfix-expression can be used to
3765 form a pointer-to-member. In that case, QUALIFYING_CLASS is the
3766 class used to qualify the member. */
3767 tree qualifying_class = NULL_TREE;
3770 /* Peek at the next token. */
3771 token = cp_lexer_peek_token (parser->lexer);
3772 /* Some of the productions are determined by keywords. */
3773 keyword = token->keyword;
3783 const char *saved_message;
3785 /* All of these can be handled in the same way from the point
3786 of view of parsing. Begin by consuming the token
3787 identifying the cast. */
3788 cp_lexer_consume_token (parser->lexer);
3790 /* New types cannot be defined in the cast. */
3791 saved_message = parser->type_definition_forbidden_message;
3792 parser->type_definition_forbidden_message
3793 = "types may not be defined in casts";
3795 /* Look for the opening `<'. */
3796 cp_parser_require (parser, CPP_LESS, "`<'");
3797 /* Parse the type to which we are casting. */
3798 type = cp_parser_type_id (parser);
3799 /* Look for the closing `>'. */
3800 cp_parser_require (parser, CPP_GREATER, "`>'");
3801 /* Restore the old message. */
3802 parser->type_definition_forbidden_message = saved_message;
3804 /* And the expression which is being cast. */
3805 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3806 expression = cp_parser_expression (parser);
3807 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3813 = build_dynamic_cast (type, expression);
3817 = build_static_cast (type, expression);
3821 = build_reinterpret_cast (type, expression);
3825 = build_const_cast (type, expression);
3836 const char *saved_message;
3838 /* Consume the `typeid' token. */
3839 cp_lexer_consume_token (parser->lexer);
3840 /* Look for the `(' token. */
3841 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3842 /* Types cannot be defined in a `typeid' expression. */
3843 saved_message = parser->type_definition_forbidden_message;
3844 parser->type_definition_forbidden_message
3845 = "types may not be defined in a `typeid\' expression";
3846 /* We can't be sure yet whether we're looking at a type-id or an
3848 cp_parser_parse_tentatively (parser);
3849 /* Try a type-id first. */
3850 type = cp_parser_type_id (parser);
3851 /* Look for the `)' token. Otherwise, we can't be sure that
3852 we're not looking at an expression: consider `typeid (int
3853 (3))', for example. */
3854 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3855 /* If all went well, simply lookup the type-id. */
3856 if (cp_parser_parse_definitely (parser))
3857 postfix_expression = get_typeid (type);
3858 /* Otherwise, fall back to the expression variant. */
3863 /* Look for an expression. */
3864 expression = cp_parser_expression (parser);
3865 /* Compute its typeid. */
3866 postfix_expression = build_typeid (expression);
3867 /* Look for the `)' token. */
3868 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3871 /* Restore the saved message. */
3872 parser->type_definition_forbidden_message = saved_message;
3878 bool template_p = false;
3882 /* Consume the `typename' token. */
3883 cp_lexer_consume_token (parser->lexer);
3884 /* Look for the optional `::' operator. */
3885 cp_parser_global_scope_opt (parser,
3886 /*current_scope_valid_p=*/false);
3887 /* Look for the nested-name-specifier. */
3888 cp_parser_nested_name_specifier (parser,
3889 /*typename_keyword_p=*/true,
3890 /*check_dependency_p=*/true,
3892 /* Look for the optional `template' keyword. */
3893 template_p = cp_parser_optional_template_keyword (parser);
3894 /* We don't know whether we're looking at a template-id or an
3896 cp_parser_parse_tentatively (parser);
3897 /* Try a template-id. */
3898 id = cp_parser_template_id (parser, template_p,
3899 /*check_dependency_p=*/true);
3900 /* If that didn't work, try an identifier. */
3901 if (!cp_parser_parse_definitely (parser))
3902 id = cp_parser_identifier (parser);
3903 /* Create a TYPENAME_TYPE to represent the type to which the
3904 functional cast is being performed. */
3905 type = make_typename_type (parser->scope, id,
3908 postfix_expression = cp_parser_functional_cast (parser, type);
3916 /* If the next thing is a simple-type-specifier, we may be
3917 looking at a functional cast. We could also be looking at
3918 an id-expression. So, we try the functional cast, and if
3919 that doesn't work we fall back to the primary-expression. */
3920 cp_parser_parse_tentatively (parser);
3921 /* Look for the simple-type-specifier. */
3922 type = cp_parser_simple_type_specifier (parser,
3923 CP_PARSER_FLAGS_NONE);
3924 /* Parse the cast itself. */
3925 if (!cp_parser_error_occurred (parser))
3927 = cp_parser_functional_cast (parser, type);
3928 /* If that worked, we're done. */
3929 if (cp_parser_parse_definitely (parser))
3932 /* If the functional-cast didn't work out, try a
3933 compound-literal. */
3934 if (cp_parser_allow_gnu_extensions_p (parser))
3936 tree initializer_list = NULL_TREE;
3938 cp_parser_parse_tentatively (parser);
3939 /* Look for the `('. */
3940 if (cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
3942 type = cp_parser_type_id (parser);
3943 /* Look for the `)'. */
3944 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3945 /* Look for the `{'. */
3946 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
3947 /* If things aren't going well, there's no need to
3949 if (!cp_parser_error_occurred (parser))
3951 /* Parse the initializer-list. */
3953 = cp_parser_initializer_list (parser);
3954 /* Allow a trailing `,'. */
3955 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
3956 cp_lexer_consume_token (parser->lexer);
3957 /* Look for the final `}'. */
3958 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
3961 /* If that worked, we're definitely looking at a
3962 compound-literal expression. */
3963 if (cp_parser_parse_definitely (parser))
3965 /* Warn the user that a compound literal is not
3966 allowed in standard C++. */
3968 pedwarn ("ISO C++ forbids compound-literals");
3969 /* Form the representation of the compound-literal. */
3971 = finish_compound_literal (type, initializer_list);
3976 /* It must be a primary-expression. */
3977 postfix_expression = cp_parser_primary_expression (parser,
3984 /* Peek at the next token. */
3985 token = cp_lexer_peek_token (parser->lexer);
3986 done = (token->type != CPP_OPEN_SQUARE
3987 && token->type != CPP_OPEN_PAREN
3988 && token->type != CPP_DOT
3989 && token->type != CPP_DEREF
3990 && token->type != CPP_PLUS_PLUS
3991 && token->type != CPP_MINUS_MINUS);
3993 /* If the postfix expression is complete, finish up. */
3994 if (address_p && qualifying_class && done)
3996 if (TREE_CODE (postfix_expression) == SCOPE_REF)
3997 postfix_expression = TREE_OPERAND (postfix_expression, 1);
3999 = build_offset_ref (qualifying_class, postfix_expression);
4000 return postfix_expression;
4003 /* Otherwise, if we were avoiding committing until we knew
4004 whether or not we had a pointer-to-member, we now know that
4005 the expression is an ordinary reference to a qualified name. */
4006 if (qualifying_class && !processing_template_decl)
4008 if (TREE_CODE (postfix_expression) == FIELD_DECL)
4010 = finish_non_static_data_member (postfix_expression,
4012 else if (BASELINK_P (postfix_expression))
4017 /* See if any of the functions are non-static members. */
4018 fns = BASELINK_FUNCTIONS (postfix_expression);
4019 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
4020 fns = TREE_OPERAND (fns, 0);
4021 for (fn = fns; fn; fn = OVL_NEXT (fn))
4022 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn))
4024 /* If so, the expression may be relative to the current
4026 if (fn && current_class_type
4027 && DERIVED_FROM_P (qualifying_class, current_class_type))
4029 = (build_class_member_access_expr
4030 (maybe_dummy_object (qualifying_class, NULL),
4032 BASELINK_ACCESS_BINFO (postfix_expression),
4033 /*preserve_reference=*/false));
4035 return build_offset_ref (qualifying_class,
4036 postfix_expression);
4040 /* Remember that there was a reference to this entity. */
4041 if (DECL_P (postfix_expression))
4042 mark_used (postfix_expression);
4044 /* Keep looping until the postfix-expression is complete. */
4047 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE
4048 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
4050 /* It is not a Koenig lookup function call. */
4051 unqualified_name_lookup_error (postfix_expression);
4052 postfix_expression = error_mark_node;
4055 /* Peek at the next token. */
4056 token = cp_lexer_peek_token (parser->lexer);
4058 switch (token->type)
4060 case CPP_OPEN_SQUARE:
4061 /* postfix-expression [ expression ] */
4065 /* Consume the `[' token. */
4066 cp_lexer_consume_token (parser->lexer);
4067 /* Parse the index expression. */
4068 index = cp_parser_expression (parser);
4069 /* Look for the closing `]'. */
4070 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4072 /* Build the ARRAY_REF. */
4074 = grok_array_decl (postfix_expression, index);
4075 idk = CP_PARSER_ID_KIND_NONE;
4079 case CPP_OPEN_PAREN:
4080 /* postfix-expression ( expression-list [opt] ) */
4084 /* Consume the `(' token. */
4085 cp_lexer_consume_token (parser->lexer);
4086 /* If the next token is not a `)', then there are some
4088 if (cp_lexer_next_token_is_not (parser->lexer,
4090 args = cp_parser_expression_list (parser);
4093 /* Look for the closing `)'. */
4094 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4096 if (idk == CP_PARSER_ID_KIND_UNQUALIFIED
4097 && (is_overloaded_fn (postfix_expression)
4098 || DECL_P (postfix_expression)
4099 || TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4103 tree identifier = NULL_TREE;
4104 tree functions = NULL_TREE;
4106 /* Find the name of the overloaded function. */
4107 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4108 identifier = postfix_expression;
4109 else if (is_overloaded_fn (postfix_expression))
4111 functions = postfix_expression;
4112 identifier = DECL_NAME (get_first_fn (functions));
4114 else if (DECL_P (postfix_expression))
4116 functions = postfix_expression;
4117 identifier = DECL_NAME (postfix_expression);
4120 /* A call to a namespace-scope function using an
4123 Do Koenig lookup -- unless any of the arguments are
4125 for (arg = args; arg; arg = TREE_CHAIN (arg))
4126 if (cp_parser_type_dependent_expression_p (TREE_VALUE (arg)))
4131 = lookup_arg_dependent(identifier, functions, args);
4132 if (!postfix_expression)
4134 /* The unqualified name could not be resolved. */
4135 unqualified_name_lookup_error (identifier);
4136 postfix_expression = error_mark_node;
4139 = build_call_from_tree (postfix_expression, args,
4140 /*diallow_virtual=*/false);
4143 postfix_expression = build_min_nt (LOOKUP_EXPR,
4146 else if (idk == CP_PARSER_ID_KIND_UNQUALIFIED
4147 && TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4149 /* The unqualified name could not be resolved. */
4150 unqualified_name_lookup_error (postfix_expression);
4151 postfix_expression = error_mark_node;
4155 /* In the body of a template, no further processing is
4157 if (processing_template_decl)
4159 postfix_expression = build_nt (CALL_EXPR,
4165 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
4167 = (build_new_method_call
4168 (TREE_OPERAND (postfix_expression, 0),
4169 TREE_OPERAND (postfix_expression, 1),
4171 (idk == CP_PARSER_ID_KIND_QUALIFIED
4172 ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL)));
4173 else if (TREE_CODE (postfix_expression) == OFFSET_REF)
4174 postfix_expression = (build_offset_ref_call_from_tree
4175 (postfix_expression, args));
4176 else if (idk == CP_PARSER_ID_KIND_QUALIFIED)
4178 /* A call to a static class member, or a
4179 namespace-scope function. */
4181 = finish_call_expr (postfix_expression, args,
4182 /*disallow_virtual=*/true);
4186 /* All other function calls. */
4188 = finish_call_expr (postfix_expression, args,
4189 /*disallow_virtual=*/false);
4192 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
4193 idk = CP_PARSER_ID_KIND_NONE;
4199 /* postfix-expression . template [opt] id-expression
4200 postfix-expression . pseudo-destructor-name
4201 postfix-expression -> template [opt] id-expression
4202 postfix-expression -> pseudo-destructor-name */
4207 tree scope = NULL_TREE;
4209 /* If this is a `->' operator, dereference the pointer. */
4210 if (token->type == CPP_DEREF)
4211 postfix_expression = build_x_arrow (postfix_expression);
4212 /* Check to see whether or not the expression is
4214 dependent_p = (cp_parser_type_dependent_expression_p
4215 (postfix_expression));
4216 /* The identifier following the `->' or `.' is not
4218 parser->scope = NULL_TREE;
4219 parser->qualifying_scope = NULL_TREE;
4220 parser->object_scope = NULL_TREE;
4221 /* Enter the scope corresponding to the type of the object
4222 given by the POSTFIX_EXPRESSION. */
4224 && TREE_TYPE (postfix_expression) != NULL_TREE)
4226 scope = TREE_TYPE (postfix_expression);
4227 /* According to the standard, no expression should
4228 ever have reference type. Unfortunately, we do not
4229 currently match the standard in this respect in
4230 that our internal representation of an expression
4231 may have reference type even when the standard says
4232 it does not. Therefore, we have to manually obtain
4233 the underlying type here. */
4234 if (TREE_CODE (scope) == REFERENCE_TYPE)
4235 scope = TREE_TYPE (scope);
4236 /* If the SCOPE is an OFFSET_TYPE, then we grab the
4237 type of the field. We get an OFFSET_TYPE for
4242 Probably, we should not get an OFFSET_TYPE here;
4243 that transformation should be made only if `&S::T'
4245 if (TREE_CODE (scope) == OFFSET_TYPE)
4246 scope = TREE_TYPE (scope);
4247 /* The type of the POSTFIX_EXPRESSION must be
4249 scope = complete_type_or_else (scope, NULL_TREE);
4250 /* Let the name lookup machinery know that we are
4251 processing a class member access expression. */
4252 parser->context->object_type = scope;
4253 /* If something went wrong, we want to be able to
4254 discern that case, as opposed to the case where
4255 there was no SCOPE due to the type of expression
4258 scope = error_mark_node;
4261 /* Consume the `.' or `->' operator. */
4262 cp_lexer_consume_token (parser->lexer);
4263 /* If the SCOPE is not a scalar type, we are looking at an
4264 ordinary class member access expression, rather than a
4265 pseudo-destructor-name. */
4266 if (!scope || !SCALAR_TYPE_P (scope))
4268 template_p = cp_parser_optional_template_keyword (parser);
4269 /* Parse the id-expression. */
4270 name = cp_parser_id_expression (parser,
4272 /*check_dependency_p=*/true,
4273 /*template_p=*/NULL);
4274 /* In general, build a SCOPE_REF if the member name is
4275 qualified. However, if the name was not dependent
4276 and has already been resolved; there is no need to
4277 build the SCOPE_REF. For example;
4279 struct X { void f(); };
4280 template <typename T> void f(T* t) { t->X::f(); }
4282 Even though "t" is dependent, "X::f" is not and has
4283 except that for a BASELINK there is no need to
4284 include scope information. */
4285 if (name != error_mark_node
4286 && !BASELINK_P (name)
4289 name = build_nt (SCOPE_REF, parser->scope, name);
4290 parser->scope = NULL_TREE;
4291 parser->qualifying_scope = NULL_TREE;
4292 parser->object_scope = NULL_TREE;
4295 = finish_class_member_access_expr (postfix_expression, name);
4297 /* Otherwise, try the pseudo-destructor-name production. */
4303 /* Parse the pseudo-destructor-name. */
4304 cp_parser_pseudo_destructor_name (parser, &s, &type);
4305 /* Form the call. */
4307 = finish_pseudo_destructor_expr (postfix_expression,
4308 s, TREE_TYPE (type));
4311 /* We no longer need to look up names in the scope of the
4312 object on the left-hand side of the `.' or `->'
4314 parser->context->object_type = NULL_TREE;
4315 idk = CP_PARSER_ID_KIND_NONE;
4320 /* postfix-expression ++ */
4321 /* Consume the `++' token. */
4322 cp_lexer_consume_token (parser->lexer);
4323 /* Generate a reprsentation for the complete expression. */
4325 = finish_increment_expr (postfix_expression,
4326 POSTINCREMENT_EXPR);
4327 idk = CP_PARSER_ID_KIND_NONE;
4330 case CPP_MINUS_MINUS:
4331 /* postfix-expression -- */
4332 /* Consume the `--' token. */
4333 cp_lexer_consume_token (parser->lexer);
4334 /* Generate a reprsentation for the complete expression. */
4336 = finish_increment_expr (postfix_expression,
4337 POSTDECREMENT_EXPR);
4338 idk = CP_PARSER_ID_KIND_NONE;
4342 return postfix_expression;
4346 /* We should never get here. */
4348 return error_mark_node;
4351 /* Parse an expression-list.
4354 assignment-expression
4355 expression-list, assignment-expression
4357 Returns a TREE_LIST. The TREE_VALUE of each node is a
4358 representation of an assignment-expression. Note that a TREE_LIST
4359 is returned even if there is only a single expression in the list. */
4362 cp_parser_expression_list (parser)
4365 tree expression_list = NULL_TREE;
4367 /* Consume expressions until there are no more. */
4372 /* Parse the next assignment-expression. */
4373 expr = cp_parser_assignment_expression (parser);
4374 /* Add it to the list. */
4375 expression_list = tree_cons (NULL_TREE, expr, expression_list);
4377 /* If the next token isn't a `,', then we are done. */
4378 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
4380 /* All uses of expression-list in the grammar are followed
4381 by a `)'. Therefore, if the next token is not a `)' an
4382 error will be issued, unless we are parsing tentatively.
4383 Skip ahead to see if there is another `,' before the `)';
4384 if so, we can go there and recover. */
4385 if (cp_parser_parsing_tentatively (parser)
4386 || cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
4387 || !cp_parser_skip_to_closing_parenthesis_or_comma (parser))
4391 /* Otherwise, consume the `,' and keep going. */
4392 cp_lexer_consume_token (parser->lexer);
4395 /* We built up the list in reverse order so we must reverse it now. */
4396 return nreverse (expression_list);
4399 /* Parse a pseudo-destructor-name.
4401 pseudo-destructor-name:
4402 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
4403 :: [opt] nested-name-specifier template template-id :: ~ type-name
4404 :: [opt] nested-name-specifier [opt] ~ type-name
4406 If either of the first two productions is used, sets *SCOPE to the
4407 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
4408 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
4409 or ERROR_MARK_NODE if no type-name is present. */
4412 cp_parser_pseudo_destructor_name (parser, scope, type)
4417 bool nested_name_specifier_p;
4419 /* Look for the optional `::' operator. */
4420 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
4421 /* Look for the optional nested-name-specifier. */
4422 nested_name_specifier_p
4423 = (cp_parser_nested_name_specifier_opt (parser,
4424 /*typename_keyword_p=*/false,
4425 /*check_dependency_p=*/true,
4428 /* Now, if we saw a nested-name-specifier, we might be doing the
4429 second production. */
4430 if (nested_name_specifier_p
4431 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
4433 /* Consume the `template' keyword. */
4434 cp_lexer_consume_token (parser->lexer);
4435 /* Parse the template-id. */
4436 cp_parser_template_id (parser,
4437 /*template_keyword_p=*/true,
4438 /*check_dependency_p=*/false);
4439 /* Look for the `::' token. */
4440 cp_parser_require (parser, CPP_SCOPE, "`::'");
4442 /* If the next token is not a `~', then there might be some
4443 additional qualification. */
4444 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
4446 /* Look for the type-name. */
4447 *scope = TREE_TYPE (cp_parser_type_name (parser));
4448 /* Look for the `::' token. */
4449 cp_parser_require (parser, CPP_SCOPE, "`::'");
4454 /* Look for the `~'. */
4455 cp_parser_require (parser, CPP_COMPL, "`~'");
4456 /* Look for the type-name again. We are not responsible for
4457 checking that it matches the first type-name. */
4458 *type = cp_parser_type_name (parser);
4461 /* Parse a unary-expression.
4467 unary-operator cast-expression
4468 sizeof unary-expression
4476 __extension__ cast-expression
4477 __alignof__ unary-expression
4478 __alignof__ ( type-id )
4479 __real__ cast-expression
4480 __imag__ cast-expression
4483 ADDRESS_P is true iff the unary-expression is appearing as the
4484 operand of the `&' operator.
4486 Returns a representation of the expresion. */
4489 cp_parser_unary_expression (cp_parser *parser, bool address_p)
4492 enum tree_code unary_operator;
4494 /* Peek at the next token. */
4495 token = cp_lexer_peek_token (parser->lexer);
4496 /* Some keywords give away the kind of expression. */
4497 if (token->type == CPP_KEYWORD)
4499 enum rid keyword = token->keyword;
4505 /* Consume the `alignof' token. */
4506 cp_lexer_consume_token (parser->lexer);
4507 /* Parse the operand. */
4508 return finish_alignof (cp_parser_sizeof_operand
4516 /* Consume the `sizeof' token. */
4517 cp_lexer_consume_token (parser->lexer);
4518 /* Parse the operand. */
4519 operand = cp_parser_sizeof_operand (parser, keyword);
4521 /* If the type of the operand cannot be determined build a
4523 if (TYPE_P (operand)
4524 ? cp_parser_dependent_type_p (operand)
4525 : cp_parser_type_dependent_expression_p (operand))
4526 return build_min (SIZEOF_EXPR, size_type_node, operand);
4527 /* Otherwise, compute the constant value. */
4529 return finish_sizeof (operand);
4533 return cp_parser_new_expression (parser);
4536 return cp_parser_delete_expression (parser);
4540 /* The saved value of the PEDANTIC flag. */
4544 /* Save away the PEDANTIC flag. */
4545 cp_parser_extension_opt (parser, &saved_pedantic);
4546 /* Parse the cast-expression. */
4547 expr = cp_parser_cast_expression (parser, /*address_p=*/false);
4548 /* Restore the PEDANTIC flag. */
4549 pedantic = saved_pedantic;
4559 /* Consume the `__real__' or `__imag__' token. */
4560 cp_lexer_consume_token (parser->lexer);
4561 /* Parse the cast-expression. */
4562 expression = cp_parser_cast_expression (parser,
4563 /*address_p=*/false);
4564 /* Create the complete representation. */
4565 return build_x_unary_op ((keyword == RID_REALPART
4566 ? REALPART_EXPR : IMAGPART_EXPR),
4576 /* Look for the `:: new' and `:: delete', which also signal the
4577 beginning of a new-expression, or delete-expression,
4578 respectively. If the next token is `::', then it might be one of
4580 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
4584 /* See if the token after the `::' is one of the keywords in
4585 which we're interested. */
4586 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
4587 /* If it's `new', we have a new-expression. */
4588 if (keyword == RID_NEW)
4589 return cp_parser_new_expression (parser);
4590 /* Similarly, for `delete'. */
4591 else if (keyword == RID_DELETE)
4592 return cp_parser_delete_expression (parser);
4595 /* Look for a unary operator. */
4596 unary_operator = cp_parser_unary_operator (token);
4597 /* The `++' and `--' operators can be handled similarly, even though
4598 they are not technically unary-operators in the grammar. */
4599 if (unary_operator == ERROR_MARK)
4601 if (token->type == CPP_PLUS_PLUS)
4602 unary_operator = PREINCREMENT_EXPR;
4603 else if (token->type == CPP_MINUS_MINUS)
4604 unary_operator = PREDECREMENT_EXPR;
4605 /* Handle the GNU address-of-label extension. */
4606 else if (cp_parser_allow_gnu_extensions_p (parser)
4607 && token->type == CPP_AND_AND)
4611 /* Consume the '&&' token. */
4612 cp_lexer_consume_token (parser->lexer);
4613 /* Look for the identifier. */
4614 identifier = cp_parser_identifier (parser);
4615 /* Create an expression representing the address. */
4616 return finish_label_address_expr (identifier);
4619 if (unary_operator != ERROR_MARK)
4621 tree cast_expression;
4623 /* Consume the operator token. */
4624 token = cp_lexer_consume_token (parser->lexer);
4625 /* Parse the cast-expression. */
4627 = cp_parser_cast_expression (parser, unary_operator == ADDR_EXPR);
4628 /* Now, build an appropriate representation. */
4629 switch (unary_operator)
4632 return build_x_indirect_ref (cast_expression, "unary *");
4635 return build_x_unary_op (ADDR_EXPR, cast_expression);
4639 case TRUTH_NOT_EXPR:
4640 case PREINCREMENT_EXPR:
4641 case PREDECREMENT_EXPR:
4642 return finish_unary_op_expr (unary_operator, cast_expression);
4645 return build_x_unary_op (BIT_NOT_EXPR, cast_expression);
4649 return error_mark_node;
4653 return cp_parser_postfix_expression (parser, address_p);
4656 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
4657 unary-operator, the corresponding tree code is returned. */
4659 static enum tree_code
4660 cp_parser_unary_operator (token)
4663 switch (token->type)
4666 return INDIRECT_REF;
4672 return CONVERT_EXPR;
4678 return TRUTH_NOT_EXPR;
4681 return BIT_NOT_EXPR;
4688 /* Parse a new-expression.
4690 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
4691 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
4693 Returns a representation of the expression. */
4696 cp_parser_new_expression (parser)
4699 bool global_scope_p;
4704 /* Look for the optional `::' operator. */
4706 = (cp_parser_global_scope_opt (parser,
4707 /*current_scope_valid_p=*/false)
4709 /* Look for the `new' operator. */
4710 cp_parser_require_keyword (parser, RID_NEW, "`new'");
4711 /* There's no easy way to tell a new-placement from the
4712 `( type-id )' construct. */
4713 cp_parser_parse_tentatively (parser);
4714 /* Look for a new-placement. */
4715 placement = cp_parser_new_placement (parser);
4716 /* If that didn't work out, there's no new-placement. */
4717 if (!cp_parser_parse_definitely (parser))
4718 placement = NULL_TREE;
4720 /* If the next token is a `(', then we have a parenthesized
4722 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4724 /* Consume the `('. */
4725 cp_lexer_consume_token (parser->lexer);
4726 /* Parse the type-id. */
4727 type = cp_parser_type_id (parser);
4728 /* Look for the closing `)'. */
4729 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4731 /* Otherwise, there must be a new-type-id. */
4733 type = cp_parser_new_type_id (parser);
4735 /* If the next token is a `(', then we have a new-initializer. */
4736 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4737 initializer = cp_parser_new_initializer (parser);
4739 initializer = NULL_TREE;
4741 /* Create a representation of the new-expression. */
4742 return build_new (placement, type, initializer, global_scope_p);
4745 /* Parse a new-placement.
4750 Returns the same representation as for an expression-list. */
4753 cp_parser_new_placement (parser)
4756 tree expression_list;
4758 /* Look for the opening `('. */
4759 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
4760 return error_mark_node;
4761 /* Parse the expression-list. */
4762 expression_list = cp_parser_expression_list (parser);
4763 /* Look for the closing `)'. */
4764 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4766 return expression_list;
4769 /* Parse a new-type-id.
4772 type-specifier-seq new-declarator [opt]
4774 Returns a TREE_LIST whose TREE_PURPOSE is the type-specifier-seq,
4775 and whose TREE_VALUE is the new-declarator. */
4778 cp_parser_new_type_id (parser)
4781 tree type_specifier_seq;
4783 const char *saved_message;
4785 /* The type-specifier sequence must not contain type definitions.
4786 (It cannot contain declarations of new types either, but if they
4787 are not definitions we will catch that because they are not
4789 saved_message = parser->type_definition_forbidden_message;
4790 parser->type_definition_forbidden_message
4791 = "types may not be defined in a new-type-id";
4792 /* Parse the type-specifier-seq. */
4793 type_specifier_seq = cp_parser_type_specifier_seq (parser);
4794 /* Restore the old message. */
4795 parser->type_definition_forbidden_message = saved_message;
4796 /* Parse the new-declarator. */
4797 declarator = cp_parser_new_declarator_opt (parser);
4799 return build_tree_list (type_specifier_seq, declarator);
4802 /* Parse an (optional) new-declarator.
4805 ptr-operator new-declarator [opt]
4806 direct-new-declarator
4808 Returns a representation of the declarator. See
4809 cp_parser_declarator for the representations used. */
4812 cp_parser_new_declarator_opt (parser)
4815 enum tree_code code;
4817 tree cv_qualifier_seq;
4819 /* We don't know if there's a ptr-operator next, or not. */
4820 cp_parser_parse_tentatively (parser);
4821 /* Look for a ptr-operator. */
4822 code = cp_parser_ptr_operator (parser, &type, &cv_qualifier_seq);
4823 /* If that worked, look for more new-declarators. */
4824 if (cp_parser_parse_definitely (parser))
4828 /* Parse another optional declarator. */
4829 declarator = cp_parser_new_declarator_opt (parser);
4831 /* Create the representation of the declarator. */
4832 if (code == INDIRECT_REF)
4833 declarator = make_pointer_declarator (cv_qualifier_seq,
4836 declarator = make_reference_declarator (cv_qualifier_seq,
4839 /* Handle the pointer-to-member case. */
4841 declarator = build_nt (SCOPE_REF, type, declarator);
4846 /* If the next token is a `[', there is a direct-new-declarator. */
4847 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
4848 return cp_parser_direct_new_declarator (parser);
4853 /* Parse a direct-new-declarator.
4855 direct-new-declarator:
4857 direct-new-declarator [constant-expression]
4859 Returns an ARRAY_REF, following the same conventions as are
4860 documented for cp_parser_direct_declarator. */
4863 cp_parser_direct_new_declarator (parser)
4866 tree declarator = NULL_TREE;
4872 /* Look for the opening `['. */
4873 cp_parser_require (parser, CPP_OPEN_SQUARE, "`['");
4874 /* The first expression is not required to be constant. */
4877 expression = cp_parser_expression (parser);
4878 /* The standard requires that the expression have integral
4879 type. DR 74 adds enumeration types. We believe that the
4880 real intent is that these expressions be handled like the
4881 expression in a `switch' condition, which also allows
4882 classes with a single conversion to integral or
4883 enumeration type. */
4884 if (!processing_template_decl)
4887 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
4892 error ("expression in new-declarator must have integral or enumeration type");
4893 expression = error_mark_node;
4897 /* But all the other expressions must be. */
4899 expression = cp_parser_constant_expression (parser);
4900 /* Look for the closing `]'. */
4901 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4903 /* Add this bound to the declarator. */
4904 declarator = build_nt (ARRAY_REF, declarator, expression);
4906 /* If the next token is not a `[', then there are no more
4908 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
4915 /* Parse a new-initializer.
4918 ( expression-list [opt] )
4920 Returns a reprsentation of the expression-list. If there is no
4921 expression-list, VOID_ZERO_NODE is returned. */
4924 cp_parser_new_initializer (parser)
4927 tree expression_list;
4929 /* Look for the opening parenthesis. */
4930 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
4931 /* If the next token is not a `)', then there is an
4933 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
4934 expression_list = cp_parser_expression_list (parser);
4936 expression_list = void_zero_node;
4937 /* Look for the closing parenthesis. */
4938 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4940 return expression_list;
4943 /* Parse a delete-expression.
4946 :: [opt] delete cast-expression
4947 :: [opt] delete [ ] cast-expression
4949 Returns a representation of the expression. */
4952 cp_parser_delete_expression (parser)
4955 bool global_scope_p;
4959 /* Look for the optional `::' operator. */
4961 = (cp_parser_global_scope_opt (parser,
4962 /*current_scope_valid_p=*/false)
4964 /* Look for the `delete' keyword. */
4965 cp_parser_require_keyword (parser, RID_DELETE, "`delete'");
4966 /* See if the array syntax is in use. */
4967 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
4969 /* Consume the `[' token. */
4970 cp_lexer_consume_token (parser->lexer);
4971 /* Look for the `]' token. */
4972 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4973 /* Remember that this is the `[]' construct. */
4979 /* Parse the cast-expression. */
4980 expression = cp_parser_cast_expression (parser, /*address_p=*/false);
4982 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
4985 /* Parse a cast-expression.
4989 ( type-id ) cast-expression
4991 Returns a representation of the expression. */
4994 cp_parser_cast_expression (cp_parser *parser, bool address_p)
4996 /* If it's a `(', then we might be looking at a cast. */
4997 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4999 tree type = NULL_TREE;
5000 tree expr = NULL_TREE;
5001 bool compound_literal_p;
5002 const char *saved_message;
5004 /* There's no way to know yet whether or not this is a cast.
5005 For example, `(int (3))' is a unary-expression, while `(int)
5006 3' is a cast. So, we resort to parsing tentatively. */
5007 cp_parser_parse_tentatively (parser);
5008 /* Types may not be defined in a cast. */
5009 saved_message = parser->type_definition_forbidden_message;
5010 parser->type_definition_forbidden_message
5011 = "types may not be defined in casts";
5012 /* Consume the `('. */
5013 cp_lexer_consume_token (parser->lexer);
5014 /* A very tricky bit is that `(struct S) { 3 }' is a
5015 compound-literal (which we permit in C++ as an extension).
5016 But, that construct is not a cast-expression -- it is a
5017 postfix-expression. (The reason is that `(struct S) { 3 }.i'
5018 is legal; if the compound-literal were a cast-expression,
5019 you'd need an extra set of parentheses.) But, if we parse
5020 the type-id, and it happens to be a class-specifier, then we
5021 will commit to the parse at that point, because we cannot
5022 undo the action that is done when creating a new class. So,
5023 then we cannot back up and do a postfix-expression.
5025 Therefore, we scan ahead to the closing `)', and check to see
5026 if the token after the `)' is a `{'. If so, we are not
5027 looking at a cast-expression.
5029 Save tokens so that we can put them back. */
5030 cp_lexer_save_tokens (parser->lexer);
5031 /* Skip tokens until the next token is a closing parenthesis.
5032 If we find the closing `)', and the next token is a `{', then
5033 we are looking at a compound-literal. */
5035 = (cp_parser_skip_to_closing_parenthesis (parser)
5036 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
5037 /* Roll back the tokens we skipped. */
5038 cp_lexer_rollback_tokens (parser->lexer);
5039 /* If we were looking at a compound-literal, simulate an error
5040 so that the call to cp_parser_parse_definitely below will
5042 if (compound_literal_p)
5043 cp_parser_simulate_error (parser);
5046 /* Look for the type-id. */
5047 type = cp_parser_type_id (parser);
5048 /* Look for the closing `)'. */
5049 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5052 /* Restore the saved message. */
5053 parser->type_definition_forbidden_message = saved_message;
5055 /* If all went well, this is a cast. */
5056 if (cp_parser_parse_definitely (parser))
5058 /* Parse the dependent expression. */
5059 expr = cp_parser_cast_expression (parser, /*address_p=*/false);
5060 /* Warn about old-style casts, if so requested. */
5061 if (warn_old_style_cast
5062 && !in_system_header
5063 && !VOID_TYPE_P (type)
5064 && current_lang_name != lang_name_c)
5065 warning ("use of old-style cast");
5066 /* Perform the cast. */
5067 expr = build_c_cast (type, expr);
5074 /* If we get here, then it's not a cast, so it must be a
5075 unary-expression. */
5076 return cp_parser_unary_expression (parser, address_p);
5079 /* Parse a pm-expression.
5083 pm-expression .* cast-expression
5084 pm-expression ->* cast-expression
5086 Returns a representation of the expression. */
5089 cp_parser_pm_expression (parser)
5095 /* Parse the cast-expresion. */
5096 cast_expr = cp_parser_cast_expression (parser, /*address_p=*/false);
5097 pm_expr = cast_expr;
5098 /* Now look for pointer-to-member operators. */
5102 enum cpp_ttype token_type;
5104 /* Peek at the next token. */
5105 token = cp_lexer_peek_token (parser->lexer);
5106 token_type = token->type;
5107 /* If it's not `.*' or `->*' there's no pointer-to-member
5109 if (token_type != CPP_DOT_STAR
5110 && token_type != CPP_DEREF_STAR)
5113 /* Consume the token. */
5114 cp_lexer_consume_token (parser->lexer);
5116 /* Parse another cast-expression. */
5117 cast_expr = cp_parser_cast_expression (parser, /*address_p=*/false);
5119 /* Build the representation of the pointer-to-member
5121 if (token_type == CPP_DEREF_STAR)
5122 pm_expr = build_x_binary_op (MEMBER_REF, pm_expr, cast_expr);
5124 pm_expr = build_m_component_ref (pm_expr, cast_expr);
5130 /* Parse a multiplicative-expression.
5132 mulitplicative-expression:
5134 multiplicative-expression * pm-expression
5135 multiplicative-expression / pm-expression
5136 multiplicative-expression % pm-expression
5138 Returns a representation of the expression. */
5141 cp_parser_multiplicative_expression (parser)
5144 static cp_parser_token_tree_map map = {
5145 { CPP_MULT, MULT_EXPR },
5146 { CPP_DIV, TRUNC_DIV_EXPR },
5147 { CPP_MOD, TRUNC_MOD_EXPR },
5148 { CPP_EOF, ERROR_MARK }
5151 return cp_parser_binary_expression (parser,
5153 cp_parser_pm_expression);
5156 /* Parse an additive-expression.
5158 additive-expression:
5159 multiplicative-expression
5160 additive-expression + multiplicative-expression
5161 additive-expression - multiplicative-expression
5163 Returns a representation of the expression. */
5166 cp_parser_additive_expression (parser)
5169 static cp_parser_token_tree_map map = {
5170 { CPP_PLUS, PLUS_EXPR },
5171 { CPP_MINUS, MINUS_EXPR },
5172 { CPP_EOF, ERROR_MARK }
5175 return cp_parser_binary_expression (parser,
5177 cp_parser_multiplicative_expression);
5180 /* Parse a shift-expression.
5184 shift-expression << additive-expression
5185 shift-expression >> additive-expression
5187 Returns a representation of the expression. */
5190 cp_parser_shift_expression (parser)
5193 static cp_parser_token_tree_map map = {
5194 { CPP_LSHIFT, LSHIFT_EXPR },
5195 { CPP_RSHIFT, RSHIFT_EXPR },
5196 { CPP_EOF, ERROR_MARK }
5199 return cp_parser_binary_expression (parser,
5201 cp_parser_additive_expression);
5204 /* Parse a relational-expression.
5206 relational-expression:
5208 relational-expression < shift-expression
5209 relational-expression > shift-expression
5210 relational-expression <= shift-expression
5211 relational-expression >= shift-expression
5215 relational-expression:
5216 relational-expression <? shift-expression
5217 relational-expression >? shift-expression
5219 Returns a representation of the expression. */
5222 cp_parser_relational_expression (parser)
5225 static cp_parser_token_tree_map map = {
5226 { CPP_LESS, LT_EXPR },
5227 { CPP_GREATER, GT_EXPR },
5228 { CPP_LESS_EQ, LE_EXPR },
5229 { CPP_GREATER_EQ, GE_EXPR },
5230 { CPP_MIN, MIN_EXPR },
5231 { CPP_MAX, MAX_EXPR },
5232 { CPP_EOF, ERROR_MARK }
5235 return cp_parser_binary_expression (parser,
5237 cp_parser_shift_expression);
5240 /* Parse an equality-expression.
5242 equality-expression:
5243 relational-expression
5244 equality-expression == relational-expression
5245 equality-expression != relational-expression
5247 Returns a representation of the expression. */
5250 cp_parser_equality_expression (parser)
5253 static cp_parser_token_tree_map map = {
5254 { CPP_EQ_EQ, EQ_EXPR },
5255 { CPP_NOT_EQ, NE_EXPR },
5256 { CPP_EOF, ERROR_MARK }
5259 return cp_parser_binary_expression (parser,
5261 cp_parser_relational_expression);
5264 /* Parse an and-expression.
5268 and-expression & equality-expression
5270 Returns a representation of the expression. */
5273 cp_parser_and_expression (parser)
5276 static cp_parser_token_tree_map map = {
5277 { CPP_AND, BIT_AND_EXPR },
5278 { CPP_EOF, ERROR_MARK }
5281 return cp_parser_binary_expression (parser,
5283 cp_parser_equality_expression);
5286 /* Parse an exclusive-or-expression.
5288 exclusive-or-expression:
5290 exclusive-or-expression ^ and-expression
5292 Returns a representation of the expression. */
5295 cp_parser_exclusive_or_expression (parser)
5298 static cp_parser_token_tree_map map = {
5299 { CPP_XOR, BIT_XOR_EXPR },
5300 { CPP_EOF, ERROR_MARK }
5303 return cp_parser_binary_expression (parser,
5305 cp_parser_and_expression);
5309 /* Parse an inclusive-or-expression.
5311 inclusive-or-expression:
5312 exclusive-or-expression
5313 inclusive-or-expression | exclusive-or-expression
5315 Returns a representation of the expression. */
5318 cp_parser_inclusive_or_expression (parser)
5321 static cp_parser_token_tree_map map = {
5322 { CPP_OR, BIT_IOR_EXPR },
5323 { CPP_EOF, ERROR_MARK }
5326 return cp_parser_binary_expression (parser,
5328 cp_parser_exclusive_or_expression);
5331 /* Parse a logical-and-expression.
5333 logical-and-expression:
5334 inclusive-or-expression
5335 logical-and-expression && inclusive-or-expression
5337 Returns a representation of the expression. */
5340 cp_parser_logical_and_expression (parser)
5343 static cp_parser_token_tree_map map = {
5344 { CPP_AND_AND, TRUTH_ANDIF_EXPR },
5345 { CPP_EOF, ERROR_MARK }
5348 return cp_parser_binary_expression (parser,
5350 cp_parser_inclusive_or_expression);
5353 /* Parse a logical-or-expression.
5355 logical-or-expression:
5356 logical-and-expresion
5357 logical-or-expression || logical-and-expression
5359 Returns a representation of the expression. */
5362 cp_parser_logical_or_expression (parser)
5365 static cp_parser_token_tree_map map = {
5366 { CPP_OR_OR, TRUTH_ORIF_EXPR },
5367 { CPP_EOF, ERROR_MARK }
5370 return cp_parser_binary_expression (parser,
5372 cp_parser_logical_and_expression);
5375 /* Parse a conditional-expression.
5377 conditional-expression:
5378 logical-or-expression
5379 logical-or-expression ? expression : assignment-expression
5383 conditional-expression:
5384 logical-or-expression ? : assignment-expression
5386 Returns a representation of the expression. */
5389 cp_parser_conditional_expression (parser)
5392 tree logical_or_expr;
5394 /* Parse the logical-or-expression. */
5395 logical_or_expr = cp_parser_logical_or_expression (parser);
5396 /* If the next token is a `?', then we have a real conditional
5398 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
5399 return cp_parser_question_colon_clause (parser, logical_or_expr);
5400 /* Otherwise, the value is simply the logical-or-expression. */
5402 return logical_or_expr;
5405 /* Parse the `? expression : assignment-expression' part of a
5406 conditional-expression. The LOGICAL_OR_EXPR is the
5407 logical-or-expression that started the conditional-expression.
5408 Returns a representation of the entire conditional-expression.
5410 This routine exists only so that it can be shared between
5411 cp_parser_conditional_expression and
5412 cp_parser_assignment_expression.
5414 ? expression : assignment-expression
5418 ? : assignment-expression */
5421 cp_parser_question_colon_clause (parser, logical_or_expr)
5423 tree logical_or_expr;
5426 tree assignment_expr;
5428 /* Consume the `?' token. */
5429 cp_lexer_consume_token (parser->lexer);
5430 if (cp_parser_allow_gnu_extensions_p (parser)
5431 && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
5432 /* Implicit true clause. */
5435 /* Parse the expression. */
5436 expr = cp_parser_expression (parser);
5438 /* The next token should be a `:'. */
5439 cp_parser_require (parser, CPP_COLON, "`:'");
5440 /* Parse the assignment-expression. */
5441 assignment_expr = cp_parser_assignment_expression (parser);
5443 /* Build the conditional-expression. */
5444 return build_x_conditional_expr (logical_or_expr,
5449 /* Parse an assignment-expression.
5451 assignment-expression:
5452 conditional-expression
5453 logical-or-expression assignment-operator assignment_expression
5456 Returns a representation for the expression. */
5459 cp_parser_assignment_expression (parser)
5464 /* If the next token is the `throw' keyword, then we're looking at
5465 a throw-expression. */
5466 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
5467 expr = cp_parser_throw_expression (parser);
5468 /* Otherwise, it must be that we are looking at a
5469 logical-or-expression. */
5472 /* Parse the logical-or-expression. */
5473 expr = cp_parser_logical_or_expression (parser);
5474 /* If the next token is a `?' then we're actually looking at a
5475 conditional-expression. */
5476 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
5477 return cp_parser_question_colon_clause (parser, expr);
5480 enum tree_code assignment_operator;
5482 /* If it's an assignment-operator, we're using the second
5485 = cp_parser_assignment_operator_opt (parser);
5486 if (assignment_operator != ERROR_MARK)
5490 /* Parse the right-hand side of the assignment. */
5491 rhs = cp_parser_assignment_expression (parser);
5492 /* Build the asignment expression. */
5493 expr = build_x_modify_expr (expr,
5494 assignment_operator,
5503 /* Parse an (optional) assignment-operator.
5505 assignment-operator: one of
5506 = *= /= %= += -= >>= <<= &= ^= |=
5510 assignment-operator: one of
5513 If the next token is an assignment operator, the corresponding tree
5514 code is returned, and the token is consumed. For example, for
5515 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
5516 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
5517 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
5518 operator, ERROR_MARK is returned. */
5520 static enum tree_code
5521 cp_parser_assignment_operator_opt (parser)
5527 /* Peek at the next toen. */
5528 token = cp_lexer_peek_token (parser->lexer);
5530 switch (token->type)
5541 op = TRUNC_DIV_EXPR;
5545 op = TRUNC_MOD_EXPR;
5585 /* Nothing else is an assignment operator. */
5589 /* If it was an assignment operator, consume it. */
5590 if (op != ERROR_MARK)
5591 cp_lexer_consume_token (parser->lexer);
5596 /* Parse an expression.
5599 assignment-expression
5600 expression , assignment-expression
5602 Returns a representation of the expression. */
5605 cp_parser_expression (parser)
5608 tree expression = NULL_TREE;
5609 bool saw_comma_p = false;
5613 tree assignment_expression;
5615 /* Parse the next assignment-expression. */
5616 assignment_expression
5617 = cp_parser_assignment_expression (parser);
5618 /* If this is the first assignment-expression, we can just
5621 expression = assignment_expression;
5622 /* Otherwise, chain the expressions together. It is unclear why
5623 we do not simply build COMPOUND_EXPRs as we go. */
5625 expression = tree_cons (NULL_TREE,
5626 assignment_expression,
5628 /* If the next token is not a comma, then we are done with the
5630 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5632 /* Consume the `,'. */
5633 cp_lexer_consume_token (parser->lexer);
5634 /* The first time we see a `,', we must take special action
5635 because the representation used for a single expression is
5636 different from that used for a list containing the single
5640 /* Remember that this expression has a `,' in it. */
5642 /* Turn the EXPRESSION into a TREE_LIST so that we can link
5643 additional expressions to it. */
5644 expression = build_tree_list (NULL_TREE, expression);
5648 /* Build a COMPOUND_EXPR to represent the entire expression, if
5649 necessary. We built up the list in reverse order, so we must
5650 straighten it out here. */
5652 expression = build_x_compound_expr (nreverse (expression));
5657 /* Parse a constant-expression.
5659 constant-expression:
5660 conditional-expression */
5663 cp_parser_constant_expression (parser)
5666 bool saved_constant_expression_p;
5669 /* It might seem that we could simply parse the
5670 conditional-expression, and then check to see if it were
5671 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
5672 one that the compiler can figure out is constant, possibly after
5673 doing some simplifications or optimizations. The standard has a
5674 precise definition of constant-expression, and we must honor
5675 that, even though it is somewhat more restrictive.
5681 is not a legal declaration, because `(2, 3)' is not a
5682 constant-expression. The `,' operator is forbidden in a
5683 constant-expression. However, GCC's constant-folding machinery
5684 will fold this operation to an INTEGER_CST for `3'. */
5686 /* Save the old setting of CONSTANT_EXPRESSION_P. */
5687 saved_constant_expression_p = parser->constant_expression_p;
5688 /* We are now parsing a constant-expression. */
5689 parser->constant_expression_p = true;
5690 /* Parse the conditional-expression. */
5691 expression = cp_parser_conditional_expression (parser);
5692 /* Restore the old setting of CONSTANT_EXPRESSION_P. */
5693 parser->constant_expression_p = saved_constant_expression_p;
5698 /* Statements [gram.stmt.stmt] */
5700 /* Parse a statement.
5704 expression-statement
5709 declaration-statement
5713 cp_parser_statement (parser)
5718 int statement_line_number;
5720 /* There is no statement yet. */
5721 statement = NULL_TREE;
5722 /* Peek at the next token. */
5723 token = cp_lexer_peek_token (parser->lexer);
5724 /* Remember the line number of the first token in the statement. */
5725 statement_line_number = token->line_number;
5726 /* If this is a keyword, then that will often determine what kind of
5727 statement we have. */
5728 if (token->type == CPP_KEYWORD)
5730 enum rid keyword = token->keyword;
5736 statement = cp_parser_labeled_statement (parser);
5741 statement = cp_parser_selection_statement (parser);
5747 statement = cp_parser_iteration_statement (parser);
5754 statement = cp_parser_jump_statement (parser);
5758 statement = cp_parser_try_block (parser);
5762 /* It might be a keyword like `int' that can start a
5763 declaration-statement. */
5767 else if (token->type == CPP_NAME)
5769 /* If the next token is a `:', then we are looking at a
5770 labeled-statement. */
5771 token = cp_lexer_peek_nth_token (parser->lexer, 2);
5772 if (token->type == CPP_COLON)
5773 statement = cp_parser_labeled_statement (parser);
5775 /* Anything that starts with a `{' must be a compound-statement. */
5776 else if (token->type == CPP_OPEN_BRACE)
5777 statement = cp_parser_compound_statement (parser);
5779 /* Everything else must be a declaration-statement or an
5780 expression-statement. Try for the declaration-statement
5781 first, unless we are looking at a `;', in which case we know that
5782 we have an expression-statement. */
5785 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
5787 cp_parser_parse_tentatively (parser);
5788 /* Try to parse the declaration-statement. */
5789 cp_parser_declaration_statement (parser);
5790 /* If that worked, we're done. */
5791 if (cp_parser_parse_definitely (parser))
5794 /* Look for an expression-statement instead. */
5795 statement = cp_parser_expression_statement (parser);
5798 /* Set the line number for the statement. */
5799 if (statement && statement_code_p (TREE_CODE (statement)))
5800 STMT_LINENO (statement) = statement_line_number;
5803 /* Parse a labeled-statement.
5806 identifier : statement
5807 case constant-expression : statement
5810 Returns the new CASE_LABEL, for a `case' or `default' label. For
5811 an ordinary label, returns a LABEL_STMT. */
5814 cp_parser_labeled_statement (parser)
5818 tree statement = NULL_TREE;
5820 /* The next token should be an identifier. */
5821 token = cp_lexer_peek_token (parser->lexer);
5822 if (token->type != CPP_NAME
5823 && token->type != CPP_KEYWORD)
5825 cp_parser_error (parser, "expected labeled-statement");
5826 return error_mark_node;
5829 switch (token->keyword)
5835 /* Consume the `case' token. */
5836 cp_lexer_consume_token (parser->lexer);
5837 /* Parse the constant-expression. */
5838 expr = cp_parser_constant_expression (parser);
5839 /* Create the label. */
5840 statement = finish_case_label (expr, NULL_TREE);
5845 /* Consume the `default' token. */
5846 cp_lexer_consume_token (parser->lexer);
5847 /* Create the label. */
5848 statement = finish_case_label (NULL_TREE, NULL_TREE);
5852 /* Anything else must be an ordinary label. */
5853 statement = finish_label_stmt (cp_parser_identifier (parser));
5857 /* Require the `:' token. */
5858 cp_parser_require (parser, CPP_COLON, "`:'");
5859 /* Parse the labeled statement. */
5860 cp_parser_statement (parser);
5862 /* Return the label, in the case of a `case' or `default' label. */
5866 /* Parse an expression-statement.
5868 expression-statement:
5871 Returns the new EXPR_STMT -- or NULL_TREE if the expression
5872 statement consists of nothing more than an `;'. */
5875 cp_parser_expression_statement (parser)
5880 /* If the next token is not a `;', then there is an expression to parse. */
5881 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
5882 statement = finish_expr_stmt (cp_parser_expression (parser));
5883 /* Otherwise, we do not even bother to build an EXPR_STMT. */
5887 statement = NULL_TREE;
5889 /* Consume the final `;'. */
5890 if (!cp_parser_require (parser, CPP_SEMICOLON, "`;'"))
5892 /* If there is additional (erroneous) input, skip to the end of
5894 cp_parser_skip_to_end_of_statement (parser);
5895 /* If the next token is now a `;', consume it. */
5896 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
5897 cp_lexer_consume_token (parser->lexer);
5903 /* Parse a compound-statement.
5906 { statement-seq [opt] }
5908 Returns a COMPOUND_STMT representing the statement. */
5911 cp_parser_compound_statement (cp_parser *parser)
5915 /* Consume the `{'. */
5916 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
5917 return error_mark_node;
5918 /* Begin the compound-statement. */
5919 compound_stmt = begin_compound_stmt (/*has_no_scope=*/0);
5920 /* Parse an (optional) statement-seq. */
5921 cp_parser_statement_seq_opt (parser);
5922 /* Finish the compound-statement. */
5923 finish_compound_stmt (/*has_no_scope=*/0, compound_stmt);
5924 /* Consume the `}'. */
5925 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
5927 return compound_stmt;
5930 /* Parse an (optional) statement-seq.
5934 statement-seq [opt] statement */
5937 cp_parser_statement_seq_opt (parser)
5940 /* Scan statements until there aren't any more. */
5943 /* If we're looking at a `}', then we've run out of statements. */
5944 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE)
5945 || cp_lexer_next_token_is (parser->lexer, CPP_EOF))
5948 /* Parse the statement. */
5949 cp_parser_statement (parser);
5953 /* Parse a selection-statement.
5955 selection-statement:
5956 if ( condition ) statement
5957 if ( condition ) statement else statement
5958 switch ( condition ) statement
5960 Returns the new IF_STMT or SWITCH_STMT. */
5963 cp_parser_selection_statement (parser)
5969 /* Peek at the next token. */
5970 token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
5972 /* See what kind of keyword it is. */
5973 keyword = token->keyword;
5982 /* Look for the `('. */
5983 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
5985 cp_parser_skip_to_end_of_statement (parser);
5986 return error_mark_node;
5989 /* Begin the selection-statement. */
5990 if (keyword == RID_IF)
5991 statement = begin_if_stmt ();
5993 statement = begin_switch_stmt ();
5995 /* Parse the condition. */
5996 condition = cp_parser_condition (parser);
5997 /* Look for the `)'. */
5998 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
5999 cp_parser_skip_to_closing_parenthesis (parser);
6001 if (keyword == RID_IF)
6005 /* Add the condition. */
6006 finish_if_stmt_cond (condition, statement);
6008 /* Parse the then-clause. */
6009 then_stmt = cp_parser_implicitly_scoped_statement (parser);
6010 finish_then_clause (statement);
6012 /* If the next token is `else', parse the else-clause. */
6013 if (cp_lexer_next_token_is_keyword (parser->lexer,
6018 /* Consume the `else' keyword. */
6019 cp_lexer_consume_token (parser->lexer);
6020 /* Parse the else-clause. */
6022 = cp_parser_implicitly_scoped_statement (parser);
6023 finish_else_clause (statement);
6026 /* Now we're all done with the if-statement. */
6033 /* Add the condition. */
6034 finish_switch_cond (condition, statement);
6036 /* Parse the body of the switch-statement. */
6037 body = cp_parser_implicitly_scoped_statement (parser);
6039 /* Now we're all done with the switch-statement. */
6040 finish_switch_stmt (statement);
6048 cp_parser_error (parser, "expected selection-statement");
6049 return error_mark_node;
6053 /* Parse a condition.
6057 type-specifier-seq declarator = assignment-expression
6062 type-specifier-seq declarator asm-specification [opt]
6063 attributes [opt] = assignment-expression
6065 Returns the expression that should be tested. */
6068 cp_parser_condition (parser)
6071 tree type_specifiers;
6072 const char *saved_message;
6074 /* Try the declaration first. */
6075 cp_parser_parse_tentatively (parser);
6076 /* New types are not allowed in the type-specifier-seq for a
6078 saved_message = parser->type_definition_forbidden_message;
6079 parser->type_definition_forbidden_message
6080 = "types may not be defined in conditions";
6081 /* Parse the type-specifier-seq. */
6082 type_specifiers = cp_parser_type_specifier_seq (parser);
6083 /* Restore the saved message. */
6084 parser->type_definition_forbidden_message = saved_message;
6085 /* If all is well, we might be looking at a declaration. */
6086 if (!cp_parser_error_occurred (parser))
6089 tree asm_specification;
6092 tree initializer = NULL_TREE;
6094 /* Parse the declarator. */
6095 declarator = cp_parser_declarator (parser,
6096 /*abstract_p=*/false,
6097 /*ctor_dtor_or_conv_p=*/NULL);
6098 /* Parse the attributes. */
6099 attributes = cp_parser_attributes_opt (parser);
6100 /* Parse the asm-specification. */
6101 asm_specification = cp_parser_asm_specification_opt (parser);
6102 /* If the next token is not an `=', then we might still be
6103 looking at an expression. For example:
6107 looks like a decl-specifier-seq and a declarator -- but then
6108 there is no `=', so this is an expression. */
6109 cp_parser_require (parser, CPP_EQ, "`='");
6110 /* If we did see an `=', then we are looking at a declaration
6112 if (cp_parser_parse_definitely (parser))
6114 /* Create the declaration. */
6115 decl = start_decl (declarator, type_specifiers,
6116 /*initialized_p=*/true,
6117 attributes, /*prefix_attributes=*/NULL_TREE);
6118 /* Parse the assignment-expression. */
6119 initializer = cp_parser_assignment_expression (parser);
6121 /* Process the initializer. */
6122 cp_finish_decl (decl,
6125 LOOKUP_ONLYCONVERTING);
6127 return convert_from_reference (decl);
6130 /* If we didn't even get past the declarator successfully, we are
6131 definitely not looking at a declaration. */
6133 cp_parser_abort_tentative_parse (parser);
6135 /* Otherwise, we are looking at an expression. */
6136 return cp_parser_expression (parser);
6139 /* Parse an iteration-statement.
6141 iteration-statement:
6142 while ( condition ) statement
6143 do statement while ( expression ) ;
6144 for ( for-init-statement condition [opt] ; expression [opt] )
6147 Returns the new WHILE_STMT, DO_STMT, or FOR_STMT. */
6150 cp_parser_iteration_statement (parser)
6157 /* Peek at the next token. */
6158 token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
6160 return error_mark_node;
6162 /* See what kind of keyword it is. */
6163 keyword = token->keyword;
6170 /* Begin the while-statement. */
6171 statement = begin_while_stmt ();
6172 /* Look for the `('. */
6173 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6174 /* Parse the condition. */
6175 condition = cp_parser_condition (parser);
6176 finish_while_stmt_cond (condition, statement);
6177 /* Look for the `)'. */
6178 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6179 /* Parse the dependent statement. */
6180 cp_parser_already_scoped_statement (parser);
6181 /* We're done with the while-statement. */
6182 finish_while_stmt (statement);
6190 /* Begin the do-statement. */
6191 statement = begin_do_stmt ();
6192 /* Parse the body of the do-statement. */
6193 cp_parser_implicitly_scoped_statement (parser);
6194 finish_do_body (statement);
6195 /* Look for the `while' keyword. */
6196 cp_parser_require_keyword (parser, RID_WHILE, "`while'");
6197 /* Look for the `('. */
6198 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6199 /* Parse the expression. */
6200 expression = cp_parser_expression (parser);
6201 /* We're done with the do-statement. */
6202 finish_do_stmt (expression, statement);
6203 /* Look for the `)'. */
6204 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6205 /* Look for the `;'. */
6206 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6212 tree condition = NULL_TREE;
6213 tree expression = NULL_TREE;
6215 /* Begin the for-statement. */
6216 statement = begin_for_stmt ();
6217 /* Look for the `('. */
6218 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6219 /* Parse the initialization. */
6220 cp_parser_for_init_statement (parser);
6221 finish_for_init_stmt (statement);
6223 /* If there's a condition, process it. */
6224 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6225 condition = cp_parser_condition (parser);
6226 finish_for_cond (condition, statement);
6227 /* Look for the `;'. */
6228 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6230 /* If there's an expression, process it. */
6231 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
6232 expression = cp_parser_expression (parser);
6233 finish_for_expr (expression, statement);
6234 /* Look for the `)'. */
6235 cp_parser_require (parser, CPP_CLOSE_PAREN, "`;'");
6237 /* Parse the body of the for-statement. */
6238 cp_parser_already_scoped_statement (parser);
6240 /* We're done with the for-statement. */
6241 finish_for_stmt (statement);
6246 cp_parser_error (parser, "expected iteration-statement");
6247 statement = error_mark_node;
6254 /* Parse a for-init-statement.
6257 expression-statement
6258 simple-declaration */
6261 cp_parser_for_init_statement (parser)
6264 /* If the next token is a `;', then we have an empty
6265 expression-statement. Gramatically, this is also a
6266 simple-declaration, but an invalid one, because it does not
6267 declare anything. Therefore, if we did not handle this case
6268 specially, we would issue an error message about an invalid
6270 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6272 /* We're going to speculatively look for a declaration, falling back
6273 to an expression, if necessary. */
6274 cp_parser_parse_tentatively (parser);
6275 /* Parse the declaration. */
6276 cp_parser_simple_declaration (parser,
6277 /*function_definition_allowed_p=*/false);
6278 /* If the tentative parse failed, then we shall need to look for an
6279 expression-statement. */
6280 if (cp_parser_parse_definitely (parser))
6284 cp_parser_expression_statement (parser);
6287 /* Parse a jump-statement.
6292 return expression [opt] ;
6300 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_STMT, or
6304 cp_parser_jump_statement (parser)
6307 tree statement = error_mark_node;
6311 /* Peek at the next token. */
6312 token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
6314 return error_mark_node;
6316 /* See what kind of keyword it is. */
6317 keyword = token->keyword;
6321 statement = finish_break_stmt ();
6322 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6326 statement = finish_continue_stmt ();
6327 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6334 /* If the next token is a `;', then there is no
6336 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6337 expr = cp_parser_expression (parser);
6340 /* Build the return-statement. */
6341 statement = finish_return_stmt (expr);
6342 /* Look for the final `;'. */
6343 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6348 /* Create the goto-statement. */
6349 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
6351 /* Issue a warning about this use of a GNU extension. */
6353 pedwarn ("ISO C++ forbids computed gotos");
6354 /* Consume the '*' token. */
6355 cp_lexer_consume_token (parser->lexer);
6356 /* Parse the dependent expression. */
6357 finish_goto_stmt (cp_parser_expression (parser));
6360 finish_goto_stmt (cp_parser_identifier (parser));
6361 /* Look for the final `;'. */
6362 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6366 cp_parser_error (parser, "expected jump-statement");
6373 /* Parse a declaration-statement.
6375 declaration-statement:
6376 block-declaration */
6379 cp_parser_declaration_statement (parser)
6382 /* Parse the block-declaration. */
6383 cp_parser_block_declaration (parser, /*statement_p=*/true);
6385 /* Finish off the statement. */
6389 /* Some dependent statements (like `if (cond) statement'), are
6390 implicitly in their own scope. In other words, if the statement is
6391 a single statement (as opposed to a compound-statement), it is
6392 none-the-less treated as if it were enclosed in braces. Any
6393 declarations appearing in the dependent statement are out of scope
6394 after control passes that point. This function parses a statement,
6395 but ensures that is in its own scope, even if it is not a
6398 Returns the new statement. */
6401 cp_parser_implicitly_scoped_statement (parser)
6406 /* If the token is not a `{', then we must take special action. */
6407 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
6409 /* Create a compound-statement. */
6410 statement = begin_compound_stmt (/*has_no_scope=*/0);
6411 /* Parse the dependent-statement. */
6412 cp_parser_statement (parser);
6413 /* Finish the dummy compound-statement. */
6414 finish_compound_stmt (/*has_no_scope=*/0, statement);
6416 /* Otherwise, we simply parse the statement directly. */
6418 statement = cp_parser_compound_statement (parser);
6420 /* Return the statement. */
6424 /* For some dependent statements (like `while (cond) statement'), we
6425 have already created a scope. Therefore, even if the dependent
6426 statement is a compound-statement, we do not want to create another
6430 cp_parser_already_scoped_statement (parser)
6433 /* If the token is not a `{', then we must take special action. */
6434 if (cp_lexer_next_token_is_not(parser->lexer, CPP_OPEN_BRACE))
6438 /* Create a compound-statement. */
6439 statement = begin_compound_stmt (/*has_no_scope=*/1);
6440 /* Parse the dependent-statement. */
6441 cp_parser_statement (parser);
6442 /* Finish the dummy compound-statement. */
6443 finish_compound_stmt (/*has_no_scope=*/1, statement);
6445 /* Otherwise, we simply parse the statement directly. */
6447 cp_parser_statement (parser);
6450 /* Declarations [gram.dcl.dcl] */
6452 /* Parse an optional declaration-sequence.
6456 declaration-seq declaration */
6459 cp_parser_declaration_seq_opt (parser)
6466 token = cp_lexer_peek_token (parser->lexer);
6468 if (token->type == CPP_CLOSE_BRACE
6469 || token->type == CPP_EOF)
6472 if (token->type == CPP_SEMICOLON)
6474 /* A declaration consisting of a single semicolon is
6475 invalid. Allow it unless we're being pedantic. */
6477 pedwarn ("extra `;'");
6478 cp_lexer_consume_token (parser->lexer);
6482 /* The C lexer modifies PENDING_LANG_CHANGE when it wants the
6483 parser to enter or exit implict `extern "C"' blocks. */
6484 while (pending_lang_change > 0)
6486 push_lang_context (lang_name_c);
6487 --pending_lang_change;
6489 while (pending_lang_change < 0)
6491 pop_lang_context ();
6492 ++pending_lang_change;
6495 /* Parse the declaration itself. */
6496 cp_parser_declaration (parser);
6500 /* Parse a declaration.
6505 template-declaration
6506 explicit-instantiation
6507 explicit-specialization
6508 linkage-specification
6509 namespace-definition */
6512 cp_parser_declaration (parser)
6518 /* Try to figure out what kind of declaration is present. */
6519 token1 = *cp_lexer_peek_token (parser->lexer);
6520 if (token1.type != CPP_EOF)
6521 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
6523 /* If the next token is `extern' and the following token is a string
6524 literal, then we have a linkage specification. */
6525 if (token1.keyword == RID_EXTERN
6526 && cp_parser_is_string_literal (&token2))
6527 cp_parser_linkage_specification (parser);
6528 /* If the next token is `template', then we have either a template
6529 declaration, an explicit instantiation, or an explicit
6531 else if (token1.keyword == RID_TEMPLATE)
6533 /* `template <>' indicates a template specialization. */
6534 if (token2.type == CPP_LESS
6535 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
6536 cp_parser_explicit_specialization (parser);
6537 /* `template <' indicates a template declaration. */
6538 else if (token2.type == CPP_LESS)
6539 cp_parser_template_declaration (parser, /*member_p=*/false);
6540 /* Anything else must be an explicit instantiation. */
6542 cp_parser_explicit_instantiation (parser);
6544 /* If the next token is `export', then we have a template
6546 else if (token1.keyword == RID_EXPORT)
6547 cp_parser_template_declaration (parser, /*member_p=*/false);
6548 /* If the next token is `extern', 'static' or 'inline' and the one
6549 after that is `template', we have a GNU extended explicit
6550 instantiation directive. */
6551 else if (cp_parser_allow_gnu_extensions_p (parser)
6552 && (token1.keyword == RID_EXTERN
6553 || token1.keyword == RID_STATIC
6554 || token1.keyword == RID_INLINE)
6555 && token2.keyword == RID_TEMPLATE)
6556 cp_parser_explicit_instantiation (parser);
6557 /* If the next token is `namespace', check for a named or unnamed
6558 namespace definition. */
6559 else if (token1.keyword == RID_NAMESPACE
6560 && (/* A named namespace definition. */
6561 (token2.type == CPP_NAME
6562 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
6564 /* An unnamed namespace definition. */
6565 || token2.type == CPP_OPEN_BRACE))
6566 cp_parser_namespace_definition (parser);
6567 /* We must have either a block declaration or a function
6570 /* Try to parse a block-declaration, or a function-definition. */
6571 cp_parser_block_declaration (parser, /*statement_p=*/false);
6574 /* Parse a block-declaration.
6579 namespace-alias-definition
6586 __extension__ block-declaration
6589 If STATEMENT_P is TRUE, then this block-declaration is ocurring as
6590 part of a declaration-statement. */
6593 cp_parser_block_declaration (cp_parser *parser,
6599 /* Check for the `__extension__' keyword. */
6600 if (cp_parser_extension_opt (parser, &saved_pedantic))
6602 /* Parse the qualified declaration. */
6603 cp_parser_block_declaration (parser, statement_p);
6604 /* Restore the PEDANTIC flag. */
6605 pedantic = saved_pedantic;
6610 /* Peek at the next token to figure out which kind of declaration is
6612 token1 = cp_lexer_peek_token (parser->lexer);
6614 /* If the next keyword is `asm', we have an asm-definition. */
6615 if (token1->keyword == RID_ASM)
6618 cp_parser_commit_to_tentative_parse (parser);
6619 cp_parser_asm_definition (parser);
6621 /* If the next keyword is `namespace', we have a
6622 namespace-alias-definition. */
6623 else if (token1->keyword == RID_NAMESPACE)
6624 cp_parser_namespace_alias_definition (parser);
6625 /* If the next keyword is `using', we have either a
6626 using-declaration or a using-directive. */
6627 else if (token1->keyword == RID_USING)
6632 cp_parser_commit_to_tentative_parse (parser);
6633 /* If the token after `using' is `namespace', then we have a
6635 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
6636 if (token2->keyword == RID_NAMESPACE)
6637 cp_parser_using_directive (parser);
6638 /* Otherwise, it's a using-declaration. */
6640 cp_parser_using_declaration (parser);
6642 /* If the next keyword is `__label__' we have a label declaration. */
6643 else if (token1->keyword == RID_LABEL)
6646 cp_parser_commit_to_tentative_parse (parser);
6647 cp_parser_label_declaration (parser);
6649 /* Anything else must be a simple-declaration. */
6651 cp_parser_simple_declaration (parser, !statement_p);
6654 /* Parse a simple-declaration.
6657 decl-specifier-seq [opt] init-declarator-list [opt] ;
6659 init-declarator-list:
6661 init-declarator-list , init-declarator
6663 If FUNCTION_DEFINTION_ALLOWED_P is TRUE, then we also recognize a
6664 function-definition as a simple-declaration. */
6667 cp_parser_simple_declaration (parser, function_definition_allowed_p)
6669 bool function_definition_allowed_p;
6671 tree decl_specifiers;
6674 bool declares_class_or_enum;
6675 bool saw_declarator;
6677 /* Defer access checks until we know what is being declared; the
6678 checks for names appearing in the decl-specifier-seq should be
6679 done as if we were in the scope of the thing being declared. */
6680 cp_parser_start_deferring_access_checks (parser);
6681 /* Parse the decl-specifier-seq. We have to keep track of whether
6682 or not the decl-specifier-seq declares a named class or
6683 enumeration type, since that is the only case in which the
6684 init-declarator-list is allowed to be empty.
6688 In a simple-declaration, the optional init-declarator-list can be
6689 omitted only when declaring a class or enumeration, that is when
6690 the decl-specifier-seq contains either a class-specifier, an
6691 elaborated-type-specifier, or an enum-specifier. */
6693 = cp_parser_decl_specifier_seq (parser,
6694 CP_PARSER_FLAGS_OPTIONAL,
6696 &declares_class_or_enum);
6697 /* We no longer need to defer access checks. */
6698 access_checks = cp_parser_stop_deferring_access_checks (parser);
6700 /* Keep going until we hit the `;' at the end of the simple
6702 saw_declarator = false;
6703 while (cp_lexer_next_token_is_not (parser->lexer,
6707 bool function_definition_p;
6709 saw_declarator = true;
6710 /* Parse the init-declarator. */
6711 cp_parser_init_declarator (parser, decl_specifiers, attributes,
6713 function_definition_allowed_p,
6715 &function_definition_p);
6716 /* Handle function definitions specially. */
6717 if (function_definition_p)
6719 /* If the next token is a `,', then we are probably
6720 processing something like:
6724 which is erroneous. */
6725 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
6726 error ("mixing declarations and function-definitions is forbidden");
6727 /* Otherwise, we're done with the list of declarators. */
6731 /* The next token should be either a `,' or a `;'. */
6732 token = cp_lexer_peek_token (parser->lexer);
6733 /* If it's a `,', there are more declarators to come. */
6734 if (token->type == CPP_COMMA)
6735 cp_lexer_consume_token (parser->lexer);
6736 /* If it's a `;', we are done. */
6737 else if (token->type == CPP_SEMICOLON)
6739 /* Anything else is an error. */
6742 cp_parser_error (parser, "expected `,' or `;'");
6743 /* Skip tokens until we reach the end of the statement. */
6744 cp_parser_skip_to_end_of_statement (parser);
6747 /* After the first time around, a function-definition is not
6748 allowed -- even if it was OK at first. For example:
6753 function_definition_allowed_p = false;
6756 /* Issue an error message if no declarators are present, and the
6757 decl-specifier-seq does not itself declare a class or
6759 if (!saw_declarator)
6761 if (cp_parser_declares_only_class_p (parser))
6762 shadow_tag (decl_specifiers);
6763 /* Perform any deferred access checks. */
6764 cp_parser_perform_deferred_access_checks (access_checks);
6767 /* Consume the `;'. */
6768 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6770 /* Mark all the classes that appeared in the decl-specifier-seq as
6771 having received a `;'. */
6772 note_list_got_semicolon (decl_specifiers);
6775 /* Parse a decl-specifier-seq.
6778 decl-specifier-seq [opt] decl-specifier
6781 storage-class-specifier
6790 decl-specifier-seq [opt] attributes
6792 Returns a TREE_LIST, giving the decl-specifiers in the order they
6793 appear in the source code. The TREE_VALUE of each node is the
6794 decl-specifier. For a keyword (such as `auto' or `friend'), the
6795 TREE_VALUE is simply the correspoding TREE_IDENTIFIER. For the
6796 representation of a type-specifier, see cp_parser_type_specifier.
6798 If there are attributes, they will be stored in *ATTRIBUTES,
6799 represented as described above cp_parser_attributes.
6801 If FRIEND_IS_NOT_CLASS_P is non-NULL, and the `friend' specifier
6802 appears, and the entity that will be a friend is not going to be a
6803 class, then *FRIEND_IS_NOT_CLASS_P will be set to TRUE. Note that
6804 even if *FRIEND_IS_NOT_CLASS_P is FALSE, the entity to which
6805 friendship is granted might not be a class. */
6808 cp_parser_decl_specifier_seq (parser, flags, attributes,
6809 declares_class_or_enum)
6811 cp_parser_flags flags;
6813 bool *declares_class_or_enum;
6815 tree decl_specs = NULL_TREE;
6816 bool friend_p = false;
6818 /* Assume no class or enumeration type is declared. */
6819 *declares_class_or_enum = false;
6821 /* Assume there are no attributes. */
6822 *attributes = NULL_TREE;
6824 /* Keep reading specifiers until there are no more to read. */
6827 tree decl_spec = NULL_TREE;
6831 /* Peek at the next token. */
6832 token = cp_lexer_peek_token (parser->lexer);
6833 /* Handle attributes. */
6834 if (token->keyword == RID_ATTRIBUTE)
6836 /* Parse the attributes. */
6837 decl_spec = cp_parser_attributes_opt (parser);
6838 /* Add them to the list. */
6839 *attributes = chainon (*attributes, decl_spec);
6842 /* If the next token is an appropriate keyword, we can simply
6843 add it to the list. */
6844 switch (token->keyword)
6850 /* The representation of the specifier is simply the
6851 appropriate TREE_IDENTIFIER node. */
6852 decl_spec = token->value;
6853 /* Consume the token. */
6854 cp_lexer_consume_token (parser->lexer);
6857 /* function-specifier:
6864 decl_spec = cp_parser_function_specifier_opt (parser);
6870 /* The representation of the specifier is simply the
6871 appropriate TREE_IDENTIFIER node. */
6872 decl_spec = token->value;
6873 /* Consume the token. */
6874 cp_lexer_consume_token (parser->lexer);
6877 /* storage-class-specifier:
6892 decl_spec = cp_parser_storage_class_specifier_opt (parser);
6899 /* Constructors are a special case. The `S' in `S()' is not a
6900 decl-specifier; it is the beginning of the declarator. */
6901 constructor_p = (!decl_spec
6902 && cp_parser_constructor_declarator_p (parser,
6905 /* If we don't have a DECL_SPEC yet, then we must be looking at
6906 a type-specifier. */
6907 if (!decl_spec && !constructor_p)
6909 bool decl_spec_declares_class_or_enum;
6910 bool is_cv_qualifier;
6913 = cp_parser_type_specifier (parser, flags,
6915 /*is_declaration=*/true,
6916 &decl_spec_declares_class_or_enum,
6919 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
6921 /* If this type-specifier referenced a user-defined type
6922 (a typedef, class-name, etc.), then we can't allow any
6923 more such type-specifiers henceforth.
6927 The longest sequence of decl-specifiers that could
6928 possibly be a type name is taken as the
6929 decl-specifier-seq of a declaration. The sequence shall
6930 be self-consistent as described below.
6934 As a general rule, at most one type-specifier is allowed
6935 in the complete decl-specifier-seq of a declaration. The
6936 only exceptions are the following:
6938 -- const or volatile can be combined with any other
6941 -- signed or unsigned can be combined with char, long,
6949 void g (const int Pc);
6951 Here, Pc is *not* part of the decl-specifier seq; it's
6952 the declarator. Therefore, once we see a type-specifier
6953 (other than a cv-qualifier), we forbid any additional
6954 user-defined types. We *do* still allow things like `int
6955 int' to be considered a decl-specifier-seq, and issue the
6956 error message later. */
6957 if (decl_spec && !is_cv_qualifier)
6958 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
6961 /* If we still do not have a DECL_SPEC, then there are no more
6965 /* Issue an error message, unless the entire construct was
6967 if (!(flags & CP_PARSER_FLAGS_OPTIONAL))
6969 cp_parser_error (parser, "expected decl specifier");
6970 return error_mark_node;
6976 /* Add the DECL_SPEC to the list of specifiers. */
6977 decl_specs = tree_cons (NULL_TREE, decl_spec, decl_specs);
6979 /* After we see one decl-specifier, further decl-specifiers are
6981 flags |= CP_PARSER_FLAGS_OPTIONAL;
6984 /* We have built up the DECL_SPECS in reverse order. Return them in
6985 the correct order. */
6986 return nreverse (decl_specs);
6989 /* Parse an (optional) storage-class-specifier.
6991 storage-class-specifier:
7000 storage-class-specifier:
7003 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
7006 cp_parser_storage_class_specifier_opt (parser)
7009 switch (cp_lexer_peek_token (parser->lexer)->keyword)
7017 /* Consume the token. */
7018 return cp_lexer_consume_token (parser->lexer)->value;
7025 /* Parse an (optional) function-specifier.
7032 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
7035 cp_parser_function_specifier_opt (parser)
7038 switch (cp_lexer_peek_token (parser->lexer)->keyword)
7043 /* Consume the token. */
7044 return cp_lexer_consume_token (parser->lexer)->value;
7051 /* Parse a linkage-specification.
7053 linkage-specification:
7054 extern string-literal { declaration-seq [opt] }
7055 extern string-literal declaration */
7058 cp_parser_linkage_specification (parser)
7064 /* Look for the `extern' keyword. */
7065 cp_parser_require_keyword (parser, RID_EXTERN, "`extern'");
7067 /* Peek at the next token. */
7068 token = cp_lexer_peek_token (parser->lexer);
7069 /* If it's not a string-literal, then there's a problem. */
7070 if (!cp_parser_is_string_literal (token))
7072 cp_parser_error (parser, "expected language-name");
7075 /* Consume the token. */
7076 cp_lexer_consume_token (parser->lexer);
7078 /* Transform the literal into an identifier. If the literal is a
7079 wide-character string, or contains embedded NULs, then we can't
7080 handle it as the user wants. */
7081 if (token->type == CPP_WSTRING
7082 || (strlen (TREE_STRING_POINTER (token->value))
7083 != (size_t) (TREE_STRING_LENGTH (token->value) - 1)))
7085 cp_parser_error (parser, "invalid linkage-specification");
7086 /* Assume C++ linkage. */
7087 linkage = get_identifier ("c++");
7089 /* If it's a simple string constant, things are easier. */
7091 linkage = get_identifier (TREE_STRING_POINTER (token->value));
7093 /* We're now using the new linkage. */
7094 push_lang_context (linkage);
7096 /* If the next token is a `{', then we're using the first
7098 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7100 /* Consume the `{' token. */
7101 cp_lexer_consume_token (parser->lexer);
7102 /* Parse the declarations. */
7103 cp_parser_declaration_seq_opt (parser);
7104 /* Look for the closing `}'. */
7105 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
7107 /* Otherwise, there's just one declaration. */
7110 bool saved_in_unbraced_linkage_specification_p;
7112 saved_in_unbraced_linkage_specification_p
7113 = parser->in_unbraced_linkage_specification_p;
7114 parser->in_unbraced_linkage_specification_p = true;
7115 have_extern_spec = true;
7116 cp_parser_declaration (parser);
7117 have_extern_spec = false;
7118 parser->in_unbraced_linkage_specification_p
7119 = saved_in_unbraced_linkage_specification_p;
7122 /* We're done with the linkage-specification. */
7123 pop_lang_context ();
7126 /* Special member functions [gram.special] */
7128 /* Parse a conversion-function-id.
7130 conversion-function-id:
7131 operator conversion-type-id
7133 Returns an IDENTIFIER_NODE representing the operator. */
7136 cp_parser_conversion_function_id (parser)
7141 tree saved_qualifying_scope;
7142 tree saved_object_scope;
7144 /* Look for the `operator' token. */
7145 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
7146 return error_mark_node;
7147 /* When we parse the conversion-type-id, the current scope will be
7148 reset. However, we need that information in able to look up the
7149 conversion function later, so we save it here. */
7150 saved_scope = parser->scope;
7151 saved_qualifying_scope = parser->qualifying_scope;
7152 saved_object_scope = parser->object_scope;
7153 /* We must enter the scope of the class so that the names of
7154 entities declared within the class are available in the
7155 conversion-type-id. For example, consider:
7162 S::operator I() { ... }
7164 In order to see that `I' is a type-name in the definition, we
7165 must be in the scope of `S'. */
7167 push_scope (saved_scope);
7168 /* Parse the conversion-type-id. */
7169 type = cp_parser_conversion_type_id (parser);
7170 /* Leave the scope of the class, if any. */
7172 pop_scope (saved_scope);
7173 /* Restore the saved scope. */
7174 parser->scope = saved_scope;
7175 parser->qualifying_scope = saved_qualifying_scope;
7176 parser->object_scope = saved_object_scope;
7177 /* If the TYPE is invalid, indicate failure. */
7178 if (type == error_mark_node)
7179 return error_mark_node;
7180 return mangle_conv_op_name_for_type (type);
7183 /* Parse a conversion-type-id:
7186 type-specifier-seq conversion-declarator [opt]
7188 Returns the TYPE specified. */
7191 cp_parser_conversion_type_id (parser)
7195 tree type_specifiers;
7198 /* Parse the attributes. */
7199 attributes = cp_parser_attributes_opt (parser);
7200 /* Parse the type-specifiers. */
7201 type_specifiers = cp_parser_type_specifier_seq (parser);
7202 /* If that didn't work, stop. */
7203 if (type_specifiers == error_mark_node)
7204 return error_mark_node;
7205 /* Parse the conversion-declarator. */
7206 declarator = cp_parser_conversion_declarator_opt (parser);
7208 return grokdeclarator (declarator, type_specifiers, TYPENAME,
7209 /*initialized=*/0, &attributes);
7212 /* Parse an (optional) conversion-declarator.
7214 conversion-declarator:
7215 ptr-operator conversion-declarator [opt]
7217 Returns a representation of the declarator. See
7218 cp_parser_declarator for details. */
7221 cp_parser_conversion_declarator_opt (parser)
7224 enum tree_code code;
7226 tree cv_qualifier_seq;
7228 /* We don't know if there's a ptr-operator next, or not. */
7229 cp_parser_parse_tentatively (parser);
7230 /* Try the ptr-operator. */
7231 code = cp_parser_ptr_operator (parser, &class_type,
7233 /* If it worked, look for more conversion-declarators. */
7234 if (cp_parser_parse_definitely (parser))
7238 /* Parse another optional declarator. */
7239 declarator = cp_parser_conversion_declarator_opt (parser);
7241 /* Create the representation of the declarator. */
7242 if (code == INDIRECT_REF)
7243 declarator = make_pointer_declarator (cv_qualifier_seq,
7246 declarator = make_reference_declarator (cv_qualifier_seq,
7249 /* Handle the pointer-to-member case. */
7251 declarator = build_nt (SCOPE_REF, class_type, declarator);
7259 /* Parse an (optional) ctor-initializer.
7262 : mem-initializer-list
7264 Returns TRUE iff the ctor-initializer was actually present. */
7267 cp_parser_ctor_initializer_opt (parser)
7270 /* If the next token is not a `:', then there is no
7271 ctor-initializer. */
7272 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
7274 /* Do default initialization of any bases and members. */
7275 if (DECL_CONSTRUCTOR_P (current_function_decl))
7276 finish_mem_initializers (NULL_TREE);
7281 /* Consume the `:' token. */
7282 cp_lexer_consume_token (parser->lexer);
7283 /* And the mem-initializer-list. */
7284 cp_parser_mem_initializer_list (parser);
7289 /* Parse a mem-initializer-list.
7291 mem-initializer-list:
7293 mem-initializer , mem-initializer-list */
7296 cp_parser_mem_initializer_list (parser)
7299 tree mem_initializer_list = NULL_TREE;
7301 /* Let the semantic analysis code know that we are starting the
7302 mem-initializer-list. */
7303 begin_mem_initializers ();
7305 /* Loop through the list. */
7308 tree mem_initializer;
7310 /* Parse the mem-initializer. */
7311 mem_initializer = cp_parser_mem_initializer (parser);
7312 /* Add it to the list, unless it was erroneous. */
7313 if (mem_initializer)
7315 TREE_CHAIN (mem_initializer) = mem_initializer_list;
7316 mem_initializer_list = mem_initializer;
7318 /* If the next token is not a `,', we're done. */
7319 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7321 /* Consume the `,' token. */
7322 cp_lexer_consume_token (parser->lexer);
7325 /* Perform semantic analysis. */
7326 finish_mem_initializers (mem_initializer_list);
7329 /* Parse a mem-initializer.
7332 mem-initializer-id ( expression-list [opt] )
7337 ( expresion-list [opt] )
7339 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
7340 class) or FIELD_DECL (for a non-static data member) to initialize;
7341 the TREE_VALUE is the expression-list. */
7344 cp_parser_mem_initializer (parser)
7347 tree mem_initializer_id;
7348 tree expression_list;
7350 /* Find out what is being initialized. */
7351 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7353 pedwarn ("anachronistic old-style base class initializer");
7354 mem_initializer_id = NULL_TREE;
7357 mem_initializer_id = cp_parser_mem_initializer_id (parser);
7358 /* Look for the opening `('. */
7359 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
7360 /* Parse the expression-list. */
7361 if (cp_lexer_next_token_is_not (parser->lexer,
7363 expression_list = cp_parser_expression_list (parser);
7365 expression_list = void_type_node;
7366 /* Look for the closing `)'. */
7367 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
7369 return expand_member_init (mem_initializer_id,
7373 /* Parse a mem-initializer-id.
7376 :: [opt] nested-name-specifier [opt] class-name
7379 Returns a TYPE indicating the class to be initializer for the first
7380 production. Returns an IDENTIFIER_NODE indicating the data member
7381 to be initialized for the second production. */
7384 cp_parser_mem_initializer_id (parser)
7387 bool global_scope_p;
7388 bool nested_name_specifier_p;
7391 /* Look for the optional `::' operator. */
7393 = (cp_parser_global_scope_opt (parser,
7394 /*current_scope_valid_p=*/false)
7396 /* Look for the optional nested-name-specifier. The simplest way to
7401 The keyword `typename' is not permitted in a base-specifier or
7402 mem-initializer; in these contexts a qualified name that
7403 depends on a template-parameter is implicitly assumed to be a
7406 is to assume that we have seen the `typename' keyword at this
7408 nested_name_specifier_p
7409 = (cp_parser_nested_name_specifier_opt (parser,
7410 /*typename_keyword_p=*/true,
7411 /*check_dependency_p=*/true,
7414 /* If there is a `::' operator or a nested-name-specifier, then we
7415 are definitely looking for a class-name. */
7416 if (global_scope_p || nested_name_specifier_p)
7417 return cp_parser_class_name (parser,
7418 /*typename_keyword_p=*/true,
7419 /*template_keyword_p=*/false,
7421 /*check_access_p=*/true,
7422 /*check_dependency_p=*/true,
7423 /*class_head_p=*/false);
7424 /* Otherwise, we could also be looking for an ordinary identifier. */
7425 cp_parser_parse_tentatively (parser);
7426 /* Try a class-name. */
7427 id = cp_parser_class_name (parser,
7428 /*typename_keyword_p=*/true,
7429 /*template_keyword_p=*/false,
7431 /*check_access_p=*/true,
7432 /*check_dependency_p=*/true,
7433 /*class_head_p=*/false);
7434 /* If we found one, we're done. */
7435 if (cp_parser_parse_definitely (parser))
7437 /* Otherwise, look for an ordinary identifier. */
7438 return cp_parser_identifier (parser);
7441 /* Overloading [gram.over] */
7443 /* Parse an operator-function-id.
7445 operator-function-id:
7448 Returns an IDENTIFIER_NODE for the operator which is a
7449 human-readable spelling of the identifier, e.g., `operator +'. */
7452 cp_parser_operator_function_id (parser)
7455 /* Look for the `operator' keyword. */
7456 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
7457 return error_mark_node;
7458 /* And then the name of the operator itself. */
7459 return cp_parser_operator (parser);
7462 /* Parse an operator.
7465 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
7466 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
7467 || ++ -- , ->* -> () []
7474 Returns an IDENTIFIER_NODE for the operator which is a
7475 human-readable spelling of the identifier, e.g., `operator +'. */
7478 cp_parser_operator (parser)
7481 tree id = NULL_TREE;
7484 /* Peek at the next token. */
7485 token = cp_lexer_peek_token (parser->lexer);
7486 /* Figure out which operator we have. */
7487 switch (token->type)
7493 /* The keyword should be either `new' or `delete'. */
7494 if (token->keyword == RID_NEW)
7496 else if (token->keyword == RID_DELETE)
7501 /* Consume the `new' or `delete' token. */
7502 cp_lexer_consume_token (parser->lexer);
7504 /* Peek at the next token. */
7505 token = cp_lexer_peek_token (parser->lexer);
7506 /* If it's a `[' token then this is the array variant of the
7508 if (token->type == CPP_OPEN_SQUARE)
7510 /* Consume the `[' token. */
7511 cp_lexer_consume_token (parser->lexer);
7512 /* Look for the `]' token. */
7513 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
7514 id = ansi_opname (op == NEW_EXPR
7515 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
7517 /* Otherwise, we have the non-array variant. */
7519 id = ansi_opname (op);
7525 id = ansi_opname (PLUS_EXPR);
7529 id = ansi_opname (MINUS_EXPR);
7533 id = ansi_opname (MULT_EXPR);
7537 id = ansi_opname (TRUNC_DIV_EXPR);
7541 id = ansi_opname (TRUNC_MOD_EXPR);
7545 id = ansi_opname (BIT_XOR_EXPR);
7549 id = ansi_opname (BIT_AND_EXPR);
7553 id = ansi_opname (BIT_IOR_EXPR);
7557 id = ansi_opname (BIT_NOT_EXPR);
7561 id = ansi_opname (TRUTH_NOT_EXPR);
7565 id = ansi_assopname (NOP_EXPR);
7569 id = ansi_opname (LT_EXPR);
7573 id = ansi_opname (GT_EXPR);
7577 id = ansi_assopname (PLUS_EXPR);
7581 id = ansi_assopname (MINUS_EXPR);
7585 id = ansi_assopname (MULT_EXPR);
7589 id = ansi_assopname (TRUNC_DIV_EXPR);
7593 id = ansi_assopname (TRUNC_MOD_EXPR);
7597 id = ansi_assopname (BIT_XOR_EXPR);
7601 id = ansi_assopname (BIT_AND_EXPR);
7605 id = ansi_assopname (BIT_IOR_EXPR);
7609 id = ansi_opname (LSHIFT_EXPR);
7613 id = ansi_opname (RSHIFT_EXPR);
7617 id = ansi_assopname (LSHIFT_EXPR);
7621 id = ansi_assopname (RSHIFT_EXPR);
7625 id = ansi_opname (EQ_EXPR);
7629 id = ansi_opname (NE_EXPR);
7633 id = ansi_opname (LE_EXPR);
7636 case CPP_GREATER_EQ:
7637 id = ansi_opname (GE_EXPR);
7641 id = ansi_opname (TRUTH_ANDIF_EXPR);
7645 id = ansi_opname (TRUTH_ORIF_EXPR);
7649 id = ansi_opname (POSTINCREMENT_EXPR);
7652 case CPP_MINUS_MINUS:
7653 id = ansi_opname (PREDECREMENT_EXPR);
7657 id = ansi_opname (COMPOUND_EXPR);
7660 case CPP_DEREF_STAR:
7661 id = ansi_opname (MEMBER_REF);
7665 id = ansi_opname (COMPONENT_REF);
7668 case CPP_OPEN_PAREN:
7669 /* Consume the `('. */
7670 cp_lexer_consume_token (parser->lexer);
7671 /* Look for the matching `)'. */
7672 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
7673 return ansi_opname (CALL_EXPR);
7675 case CPP_OPEN_SQUARE:
7676 /* Consume the `['. */
7677 cp_lexer_consume_token (parser->lexer);
7678 /* Look for the matching `]'. */
7679 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
7680 return ansi_opname (ARRAY_REF);
7684 id = ansi_opname (MIN_EXPR);
7688 id = ansi_opname (MAX_EXPR);
7692 id = ansi_assopname (MIN_EXPR);
7696 id = ansi_assopname (MAX_EXPR);
7700 /* Anything else is an error. */
7704 /* If we have selected an identifier, we need to consume the
7707 cp_lexer_consume_token (parser->lexer);
7708 /* Otherwise, no valid operator name was present. */
7711 cp_parser_error (parser, "expected operator");
7712 id = error_mark_node;
7718 /* Parse a template-declaration.
7720 template-declaration:
7721 export [opt] template < template-parameter-list > declaration
7723 If MEMBER_P is TRUE, this template-declaration occurs within a
7726 The grammar rule given by the standard isn't correct. What
7729 template-declaration:
7730 export [opt] template-parameter-list-seq
7731 decl-specifier-seq [opt] init-declarator [opt] ;
7732 export [opt] template-parameter-list-seq
7735 template-parameter-list-seq:
7736 template-parameter-list-seq [opt]
7737 template < template-parameter-list > */
7740 cp_parser_template_declaration (parser, member_p)
7744 /* Check for `export'. */
7745 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
7747 /* Consume the `export' token. */
7748 cp_lexer_consume_token (parser->lexer);
7749 /* Warn that we do not support `export'. */
7750 warning ("keyword `export' not implemented, and will be ignored");
7753 cp_parser_template_declaration_after_export (parser, member_p);
7756 /* Parse a template-parameter-list.
7758 template-parameter-list:
7760 template-parameter-list , template-parameter
7762 Returns a TREE_LIST. Each node represents a template parameter.
7763 The nodes are connected via their TREE_CHAINs. */
7766 cp_parser_template_parameter_list (parser)
7769 tree parameter_list = NULL_TREE;
7776 /* Parse the template-parameter. */
7777 parameter = cp_parser_template_parameter (parser);
7778 /* Add it to the list. */
7779 parameter_list = process_template_parm (parameter_list,
7782 /* Peek at the next token. */
7783 token = cp_lexer_peek_token (parser->lexer);
7784 /* If it's not a `,', we're done. */
7785 if (token->type != CPP_COMMA)
7787 /* Otherwise, consume the `,' token. */
7788 cp_lexer_consume_token (parser->lexer);
7791 return parameter_list;
7794 /* Parse a template-parameter.
7798 parameter-declaration
7800 Returns a TREE_LIST. The TREE_VALUE represents the parameter. The
7801 TREE_PURPOSE is the default value, if any. */
7804 cp_parser_template_parameter (parser)
7809 /* Peek at the next token. */
7810 token = cp_lexer_peek_token (parser->lexer);
7811 /* If it is `class' or `template', we have a type-parameter. */
7812 if (token->keyword == RID_TEMPLATE)
7813 return cp_parser_type_parameter (parser);
7814 /* If it is `class' or `typename' we do not know yet whether it is a
7815 type parameter or a non-type parameter. Consider:
7817 template <typename T, typename T::X X> ...
7821 template <class C, class D*> ...
7823 Here, the first parameter is a type parameter, and the second is
7824 a non-type parameter. We can tell by looking at the token after
7825 the identifier -- if it is a `,', `=', or `>' then we have a type
7827 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
7829 /* Peek at the token after `class' or `typename'. */
7830 token = cp_lexer_peek_nth_token (parser->lexer, 2);
7831 /* If it's an identifier, skip it. */
7832 if (token->type == CPP_NAME)
7833 token = cp_lexer_peek_nth_token (parser->lexer, 3);
7834 /* Now, see if the token looks like the end of a template
7836 if (token->type == CPP_COMMA
7837 || token->type == CPP_EQ
7838 || token->type == CPP_GREATER)
7839 return cp_parser_type_parameter (parser);
7842 /* Otherwise, it is a non-type parameter.
7846 When parsing a default template-argument for a non-type
7847 template-parameter, the first non-nested `>' is taken as the end
7848 of the template parameter-list rather than a greater-than
7851 cp_parser_parameter_declaration (parser,
7852 /*greater_than_is_operator_p=*/false);
7855 /* Parse a type-parameter.
7858 class identifier [opt]
7859 class identifier [opt] = type-id
7860 typename identifier [opt]
7861 typename identifier [opt] = type-id
7862 template < template-parameter-list > class identifier [opt]
7863 template < template-parameter-list > class identifier [opt]
7866 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
7867 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
7868 the declaration of the parameter. */
7871 cp_parser_type_parameter (parser)
7877 /* Look for a keyword to tell us what kind of parameter this is. */
7878 token = cp_parser_require (parser, CPP_KEYWORD,
7879 "expected `class', `typename', or `template'");
7881 return error_mark_node;
7883 switch (token->keyword)
7889 tree default_argument;
7891 /* If the next token is an identifier, then it names the
7893 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
7894 identifier = cp_parser_identifier (parser);
7896 identifier = NULL_TREE;
7898 /* Create the parameter. */
7899 parameter = finish_template_type_parm (class_type_node, identifier);
7901 /* If the next token is an `=', we have a default argument. */
7902 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7904 /* Consume the `=' token. */
7905 cp_lexer_consume_token (parser->lexer);
7906 /* Parse the default-argumen. */
7907 default_argument = cp_parser_type_id (parser);
7910 default_argument = NULL_TREE;
7912 /* Create the combined representation of the parameter and the
7913 default argument. */
7914 parameter = build_tree_list (default_argument,
7921 tree parameter_list;
7923 tree default_argument;
7925 /* Look for the `<'. */
7926 cp_parser_require (parser, CPP_LESS, "`<'");
7927 /* Parse the template-parameter-list. */
7928 begin_template_parm_list ();
7930 = cp_parser_template_parameter_list (parser);
7931 parameter_list = end_template_parm_list (parameter_list);
7932 /* Look for the `>'. */
7933 cp_parser_require (parser, CPP_GREATER, "`>'");
7934 /* Look for the `class' keyword. */
7935 cp_parser_require_keyword (parser, RID_CLASS, "`class'");
7936 /* If the next token is an `=', then there is a
7937 default-argument. If the next token is a `>', we are at
7938 the end of the parameter-list. If the next token is a `,',
7939 then we are at the end of this parameter. */
7940 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
7941 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
7942 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7943 identifier = cp_parser_identifier (parser);
7945 identifier = NULL_TREE;
7946 /* Create the template parameter. */
7947 parameter = finish_template_template_parm (class_type_node,
7950 /* If the next token is an `=', then there is a
7951 default-argument. */
7952 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7954 /* Consume the `='. */
7955 cp_lexer_consume_token (parser->lexer);
7956 /* Parse the id-expression. */
7958 = cp_parser_id_expression (parser,
7959 /*template_keyword_p=*/false,
7960 /*check_dependency_p=*/true,
7961 /*template_p=*/NULL);
7962 /* Look up the name. */
7964 = cp_parser_lookup_name_simple (parser, default_argument);
7965 /* See if the default argument is valid. */
7967 = check_template_template_default_arg (default_argument);
7970 default_argument = NULL_TREE;
7972 /* Create the combined representation of the parameter and the
7973 default argument. */
7974 parameter = build_tree_list (default_argument,
7980 /* Anything else is an error. */
7981 cp_parser_error (parser,
7982 "expected `class', `typename', or `template'");
7983 parameter = error_mark_node;
7989 /* Parse a template-id.
7992 template-name < template-argument-list [opt] >
7994 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
7995 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
7996 returned. Otherwise, if the template-name names a function, or set
7997 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
7998 names a class, returns a TYPE_DECL for the specialization.
8000 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
8001 uninstantiated templates. */
8004 cp_parser_template_id (cp_parser *parser,
8005 bool template_keyword_p,
8006 bool check_dependency_p)
8011 tree saved_qualifying_scope;
8012 tree saved_object_scope;
8014 bool saved_greater_than_is_operator_p;
8015 ptrdiff_t start_of_id;
8016 tree access_check = NULL_TREE;
8018 /* If the next token corresponds to a template-id, there is no need
8020 if (cp_lexer_next_token_is (parser->lexer, CPP_TEMPLATE_ID))
8025 /* Get the stored value. */
8026 value = cp_lexer_consume_token (parser->lexer)->value;
8027 /* Perform any access checks that were deferred. */
8028 for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
8029 cp_parser_defer_access_check (parser,
8030 TREE_PURPOSE (check),
8031 TREE_VALUE (check));
8032 /* Return the stored value. */
8033 return TREE_VALUE (value);
8036 /* Remember where the template-id starts. */
8037 if (cp_parser_parsing_tentatively (parser)
8038 && !cp_parser_committed_to_tentative_parse (parser))
8040 cp_token *next_token = cp_lexer_peek_token (parser->lexer);
8041 start_of_id = cp_lexer_token_difference (parser->lexer,
8042 parser->lexer->first_token,
8044 access_check = parser->context->deferred_access_checks;
8049 /* Parse the template-name. */
8050 template = cp_parser_template_name (parser, template_keyword_p,
8051 check_dependency_p);
8052 if (template == error_mark_node)
8053 return error_mark_node;
8055 /* Look for the `<' that starts the template-argument-list. */
8056 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
8057 return error_mark_node;
8061 When parsing a template-id, the first non-nested `>' is taken as
8062 the end of the template-argument-list rather than a greater-than
8064 saved_greater_than_is_operator_p
8065 = parser->greater_than_is_operator_p;
8066 parser->greater_than_is_operator_p = false;
8067 /* Parsing the argument list may modify SCOPE, so we save it
8069 saved_scope = parser->scope;
8070 saved_qualifying_scope = parser->qualifying_scope;
8071 saved_object_scope = parser->object_scope;
8072 /* Parse the template-argument-list itself. */
8073 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
8074 arguments = NULL_TREE;
8076 arguments = cp_parser_template_argument_list (parser);
8077 /* Look for the `>' that ends the template-argument-list. */
8078 cp_parser_require (parser, CPP_GREATER, "`>'");
8079 /* The `>' token might be a greater-than operator again now. */
8080 parser->greater_than_is_operator_p
8081 = saved_greater_than_is_operator_p;
8082 /* Restore the SAVED_SCOPE. */
8083 parser->scope = saved_scope;
8084 parser->qualifying_scope = saved_qualifying_scope;
8085 parser->object_scope = saved_object_scope;
8087 /* Build a representation of the specialization. */
8088 if (TREE_CODE (template) == IDENTIFIER_NODE)
8089 template_id = build_min_nt (TEMPLATE_ID_EXPR, template, arguments);
8090 else if (DECL_CLASS_TEMPLATE_P (template)
8091 || DECL_TEMPLATE_TEMPLATE_PARM_P (template))
8093 = finish_template_type (template, arguments,
8094 cp_lexer_next_token_is (parser->lexer,
8098 /* If it's not a class-template or a template-template, it should be
8099 a function-template. */
8100 my_friendly_assert ((DECL_FUNCTION_TEMPLATE_P (template)
8101 || TREE_CODE (template) == OVERLOAD
8102 || BASELINK_P (template)),
8105 template_id = lookup_template_function (template, arguments);
8108 /* If parsing tentatively, replace the sequence of tokens that makes
8109 up the template-id with a CPP_TEMPLATE_ID token. That way,
8110 should we re-parse the token stream, we will not have to repeat
8111 the effort required to do the parse, nor will we issue duplicate
8112 error messages about problems during instantiation of the
8114 if (start_of_id >= 0)
8119 /* Find the token that corresponds to the start of the
8121 token = cp_lexer_advance_token (parser->lexer,
8122 parser->lexer->first_token,
8125 /* Remember the access checks associated with this
8126 nested-name-specifier. */
8127 c = parser->context->deferred_access_checks;
8128 if (c == access_check)
8129 access_check = NULL_TREE;
8132 while (TREE_CHAIN (c) != access_check)
8134 access_check = parser->context->deferred_access_checks;
8135 parser->context->deferred_access_checks = TREE_CHAIN (c);
8136 TREE_CHAIN (c) = NULL_TREE;
8139 /* Reset the contents of the START_OF_ID token. */
8140 token->type = CPP_TEMPLATE_ID;
8141 token->value = build_tree_list (access_check, template_id);
8142 token->keyword = RID_MAX;
8143 /* Purge all subsequent tokens. */
8144 cp_lexer_purge_tokens_after (parser->lexer, token);
8150 /* Parse a template-name.
8155 The standard should actually say:
8159 operator-function-id
8160 conversion-function-id
8162 A defect report has been filed about this issue.
8164 If TEMPLATE_KEYWORD_P is true, then we have just seen the
8165 `template' keyword, in a construction like:
8169 In that case `f' is taken to be a template-name, even though there
8170 is no way of knowing for sure.
8172 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
8173 name refers to a set of overloaded functions, at least one of which
8174 is a template, or an IDENTIFIER_NODE with the name of the template,
8175 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
8176 names are looked up inside uninstantiated templates. */
8179 cp_parser_template_name (parser, template_keyword_p, check_dependency_p)
8181 bool template_keyword_p;
8182 bool check_dependency_p;
8188 /* If the next token is `operator', then we have either an
8189 operator-function-id or a conversion-function-id. */
8190 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
8192 /* We don't know whether we're looking at an
8193 operator-function-id or a conversion-function-id. */
8194 cp_parser_parse_tentatively (parser);
8195 /* Try an operator-function-id. */
8196 identifier = cp_parser_operator_function_id (parser);
8197 /* If that didn't work, try a conversion-function-id. */
8198 if (!cp_parser_parse_definitely (parser))
8199 identifier = cp_parser_conversion_function_id (parser);
8201 /* Look for the identifier. */
8203 identifier = cp_parser_identifier (parser);
8205 /* If we didn't find an identifier, we don't have a template-id. */
8206 if (identifier == error_mark_node)
8207 return error_mark_node;
8209 /* If the name immediately followed the `template' keyword, then it
8210 is a template-name. However, if the next token is not `<', then
8211 we do not treat it as a template-name, since it is not being used
8212 as part of a template-id. This enables us to handle constructs
8215 template <typename T> struct S { S(); };
8216 template <typename T> S<T>::S();
8218 correctly. We would treat `S' as a template -- if it were `S<T>'
8219 -- but we do not if there is no `<'. */
8220 if (template_keyword_p && processing_template_decl
8221 && cp_lexer_next_token_is (parser->lexer, CPP_LESS))
8224 /* Look up the name. */
8225 decl = cp_parser_lookup_name (parser, identifier,
8226 /*check_access=*/true,
8228 check_dependency_p);
8229 decl = maybe_get_template_decl_from_type_decl (decl);
8231 /* If DECL is a template, then the name was a template-name. */
8232 if (TREE_CODE (decl) == TEMPLATE_DECL)
8236 /* The standard does not explicitly indicate whether a name that
8237 names a set of overloaded declarations, some of which are
8238 templates, is a template-name. However, such a name should
8239 be a template-name; otherwise, there is no way to form a
8240 template-id for the overloaded templates. */
8241 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
8242 if (TREE_CODE (fns) == OVERLOAD)
8246 for (fn = fns; fn; fn = OVL_NEXT (fn))
8247 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
8252 /* Otherwise, the name does not name a template. */
8253 cp_parser_error (parser, "expected template-name");
8254 return error_mark_node;
8258 /* If DECL is dependent, and refers to a function, then just return
8259 its name; we will look it up again during template instantiation. */
8260 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
8262 tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
8263 if (TYPE_P (scope) && cp_parser_dependent_type_p (scope))
8270 /* Parse a template-argument-list.
8272 template-argument-list:
8274 template-argument-list , template-argument
8276 Returns a TREE_LIST representing the arguments, in the order they
8277 appeared. The TREE_VALUE of each node is a representation of the
8281 cp_parser_template_argument_list (parser)
8284 tree arguments = NULL_TREE;
8290 /* Parse the template-argument. */
8291 argument = cp_parser_template_argument (parser);
8292 /* Add it to the list. */
8293 arguments = tree_cons (NULL_TREE, argument, arguments);
8294 /* If it is not a `,', then there are no more arguments. */
8295 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8297 /* Otherwise, consume the ','. */
8298 cp_lexer_consume_token (parser->lexer);
8301 /* We built up the arguments in reverse order. */
8302 return nreverse (arguments);
8305 /* Parse a template-argument.
8308 assignment-expression
8312 The representation is that of an assignment-expression, type-id, or
8313 id-expression -- except that the qualified id-expression is
8314 evaluated, so that the value returned is either a DECL or an
8318 cp_parser_template_argument (parser)
8324 /* There's really no way to know what we're looking at, so we just
8325 try each alternative in order.
8329 In a template-argument, an ambiguity between a type-id and an
8330 expression is resolved to a type-id, regardless of the form of
8331 the corresponding template-parameter.
8333 Therefore, we try a type-id first. */
8334 cp_parser_parse_tentatively (parser);
8335 argument = cp_parser_type_id (parser);
8336 /* If the next token isn't a `,' or a `>', then this argument wasn't
8338 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
8339 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER))
8340 cp_parser_error (parser, "expected template-argument");
8341 /* If that worked, we're done. */
8342 if (cp_parser_parse_definitely (parser))
8344 /* We're still not sure what the argument will be. */
8345 cp_parser_parse_tentatively (parser);
8346 /* Try a template. */
8347 argument = cp_parser_id_expression (parser,
8348 /*template_keyword_p=*/false,
8349 /*check_dependency_p=*/true,
8351 /* If the next token isn't a `,' or a `>', then this argument wasn't
8353 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
8354 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER))
8355 cp_parser_error (parser, "expected template-argument");
8356 if (!cp_parser_error_occurred (parser))
8358 /* Figure out what is being referred to. */
8359 argument = cp_parser_lookup_name_simple (parser, argument);
8361 argument = make_unbound_class_template (TREE_OPERAND (argument, 0),
8362 TREE_OPERAND (argument, 1),
8363 tf_error | tf_parsing);
8364 else if (TREE_CODE (argument) != TEMPLATE_DECL)
8365 cp_parser_error (parser, "expected template-name");
8367 if (cp_parser_parse_definitely (parser))
8369 /* It must be an assignment-expression. */
8370 return cp_parser_assignment_expression (parser);
8373 /* Parse an explicit-instantiation.
8375 explicit-instantiation:
8376 template declaration
8378 Although the standard says `declaration', what it really means is:
8380 explicit-instantiation:
8381 template decl-specifier-seq [opt] declarator [opt] ;
8383 Things like `template int S<int>::i = 5, int S<double>::j;' are not
8384 supposed to be allowed. A defect report has been filed about this
8389 explicit-instantiation:
8390 storage-class-specifier template
8391 decl-specifier-seq [opt] declarator [opt] ;
8392 function-specifier template
8393 decl-specifier-seq [opt] declarator [opt] ; */
8396 cp_parser_explicit_instantiation (parser)
8399 bool declares_class_or_enum;
8400 tree decl_specifiers;
8402 tree extension_specifier = NULL_TREE;
8404 /* Look for an (optional) storage-class-specifier or
8405 function-specifier. */
8406 if (cp_parser_allow_gnu_extensions_p (parser))
8409 = cp_parser_storage_class_specifier_opt (parser);
8410 if (!extension_specifier)
8411 extension_specifier = cp_parser_function_specifier_opt (parser);
8414 /* Look for the `template' keyword. */
8415 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
8416 /* Let the front end know that we are processing an explicit
8418 begin_explicit_instantiation ();
8419 /* [temp.explicit] says that we are supposed to ignore access
8420 control while processing explicit instantiation directives. */
8421 scope_chain->check_access = 0;
8422 /* Parse a decl-specifier-seq. */
8424 = cp_parser_decl_specifier_seq (parser,
8425 CP_PARSER_FLAGS_OPTIONAL,
8427 &declares_class_or_enum);
8428 /* If there was exactly one decl-specifier, and it declared a class,
8429 and there's no declarator, then we have an explicit type
8431 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
8435 type = check_tag_decl (decl_specifiers);
8437 do_type_instantiation (type, extension_specifier, /*complain=*/1);
8444 /* Parse the declarator. */
8446 = cp_parser_declarator (parser,
8447 /*abstract_p=*/false,
8448 /*ctor_dtor_or_conv_p=*/NULL);
8449 decl = grokdeclarator (declarator, decl_specifiers,
8451 /* Do the explicit instantiation. */
8452 do_decl_instantiation (decl, extension_specifier);
8454 /* We're done with the instantiation. */
8455 end_explicit_instantiation ();
8456 /* Trun access control back on. */
8457 scope_chain->check_access = flag_access_control;
8459 /* Look for the trailing `;'. */
8460 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
8463 /* Parse an explicit-specialization.
8465 explicit-specialization:
8466 template < > declaration
8468 Although the standard says `declaration', what it really means is:
8470 explicit-specialization:
8471 template <> decl-specifier [opt] init-declarator [opt] ;
8472 template <> function-definition
8473 template <> explicit-specialization
8474 template <> template-declaration */
8477 cp_parser_explicit_specialization (parser)
8480 /* Look for the `template' keyword. */
8481 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
8482 /* Look for the `<'. */
8483 cp_parser_require (parser, CPP_LESS, "`<'");
8484 /* Look for the `>'. */
8485 cp_parser_require (parser, CPP_GREATER, "`>'");
8486 /* We have processed another parameter list. */
8487 ++parser->num_template_parameter_lists;
8488 /* Let the front end know that we are beginning a specialization. */
8489 begin_specialization ();
8491 /* If the next keyword is `template', we need to figure out whether
8492 or not we're looking a template-declaration. */
8493 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
8495 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
8496 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
8497 cp_parser_template_declaration_after_export (parser,
8498 /*member_p=*/false);
8500 cp_parser_explicit_specialization (parser);
8503 /* Parse the dependent declaration. */
8504 cp_parser_single_declaration (parser,
8508 /* We're done with the specialization. */
8509 end_specialization ();
8510 /* We're done with this parameter list. */
8511 --parser->num_template_parameter_lists;
8514 /* Parse a type-specifier.
8517 simple-type-specifier
8520 elaborated-type-specifier
8528 Returns a representation of the type-specifier. If the
8529 type-specifier is a keyword (like `int' or `const', or
8530 `__complex__') then the correspoding IDENTIFIER_NODE is returned.
8531 For a class-specifier, enum-specifier, or elaborated-type-specifier
8532 a TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
8534 If IS_FRIEND is TRUE then this type-specifier is being declared a
8535 `friend'. If IS_DECLARATION is TRUE, then this type-specifier is
8536 appearing in a decl-specifier-seq.
8538 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
8539 class-specifier, enum-specifier, or elaborated-type-specifier, then
8540 *DECLARES_CLASS_OR_ENUM is set to TRUE. Otherwise, it is set to
8543 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
8544 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
8548 cp_parser_type_specifier (parser,
8552 declares_class_or_enum,
8555 cp_parser_flags flags;
8557 bool is_declaration;
8558 bool *declares_class_or_enum;
8559 bool *is_cv_qualifier;
8561 tree type_spec = NULL_TREE;
8565 /* Assume this type-specifier does not declare a new type. */
8566 if (declares_class_or_enum)
8567 *declares_class_or_enum = false;
8568 /* And that it does not specify a cv-qualifier. */
8569 if (is_cv_qualifier)
8570 *is_cv_qualifier = false;
8571 /* Peek at the next token. */
8572 token = cp_lexer_peek_token (parser->lexer);
8574 /* If we're looking at a keyword, we can use that to guide the
8575 production we choose. */
8576 keyword = token->keyword;
8579 /* Any of these indicate either a class-specifier, or an
8580 elaborated-type-specifier. */
8585 /* Parse tentatively so that we can back up if we don't find a
8586 class-specifier or enum-specifier. */
8587 cp_parser_parse_tentatively (parser);
8588 /* Look for the class-specifier or enum-specifier. */
8589 if (keyword == RID_ENUM)
8590 type_spec = cp_parser_enum_specifier (parser);
8592 type_spec = cp_parser_class_specifier (parser);
8594 /* If that worked, we're done. */
8595 if (cp_parser_parse_definitely (parser))
8597 if (declares_class_or_enum)
8598 *declares_class_or_enum = true;
8605 /* Look for an elaborated-type-specifier. */
8606 type_spec = cp_parser_elaborated_type_specifier (parser,
8609 /* We're declaring a class or enum -- unless we're using
8611 if (declares_class_or_enum && keyword != RID_TYPENAME)
8612 *declares_class_or_enum = true;
8618 type_spec = cp_parser_cv_qualifier_opt (parser);
8619 /* Even though we call a routine that looks for an optional
8620 qualifier, we know that there should be one. */
8621 my_friendly_assert (type_spec != NULL, 20000328);
8622 /* This type-specifier was a cv-qualified. */
8623 if (is_cv_qualifier)
8624 *is_cv_qualifier = true;
8629 /* The `__complex__' keyword is a GNU extension. */
8630 return cp_lexer_consume_token (parser->lexer)->value;
8636 /* If we do not already have a type-specifier, assume we are looking
8637 at a simple-type-specifier. */
8638 type_spec = cp_parser_simple_type_specifier (parser, flags);
8640 /* If we didn't find a type-specifier, and a type-specifier was not
8641 optional in this context, issue an error message. */
8642 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
8644 cp_parser_error (parser, "expected type specifier");
8645 return error_mark_node;
8651 /* Parse a simple-type-specifier.
8653 simple-type-specifier:
8654 :: [opt] nested-name-specifier [opt] type-name
8655 :: [opt] nested-name-specifier template template-id
8670 simple-type-specifier:
8671 __typeof__ unary-expression
8672 __typeof__ ( type-id )
8674 For the various keywords, the value returned is simply the
8675 TREE_IDENTIFIER representing the keyword. For the first two
8676 productions, the value returned is the indicated TYPE_DECL. */
8679 cp_parser_simple_type_specifier (parser, flags)
8681 cp_parser_flags flags;
8683 tree type = NULL_TREE;
8686 /* Peek at the next token. */
8687 token = cp_lexer_peek_token (parser->lexer);
8689 /* If we're looking at a keyword, things are easy. */
8690 switch (token->keyword)
8703 /* Consume the token. */
8704 return cp_lexer_consume_token (parser->lexer)->value;
8710 /* Consume the `typeof' token. */
8711 cp_lexer_consume_token (parser->lexer);
8712 /* Parse the operand to `typeof' */
8713 operand = cp_parser_sizeof_operand (parser, RID_TYPEOF);
8714 /* If it is not already a TYPE, take its type. */
8715 if (!TYPE_P (operand))
8716 operand = finish_typeof (operand);
8725 /* The type-specifier must be a user-defined type. */
8726 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
8728 /* Don't gobble tokens or issue error messages if this is an
8729 optional type-specifier. */
8730 if (flags & CP_PARSER_FLAGS_OPTIONAL)
8731 cp_parser_parse_tentatively (parser);
8733 /* Look for the optional `::' operator. */
8734 cp_parser_global_scope_opt (parser,
8735 /*current_scope_valid_p=*/false);
8736 /* Look for the nested-name specifier. */
8737 cp_parser_nested_name_specifier_opt (parser,
8738 /*typename_keyword_p=*/false,
8739 /*check_dependency_p=*/true,
8741 /* If we have seen a nested-name-specifier, and the next token
8742 is `template', then we are using the template-id production. */
8744 && cp_parser_optional_template_keyword (parser))
8746 /* Look for the template-id. */
8747 type = cp_parser_template_id (parser,
8748 /*template_keyword_p=*/true,
8749 /*check_dependency_p=*/true);
8750 /* If the template-id did not name a type, we are out of
8752 if (TREE_CODE (type) != TYPE_DECL)
8754 cp_parser_error (parser, "expected template-id for type");
8758 /* Otherwise, look for a type-name. */
8761 type = cp_parser_type_name (parser);
8762 if (type == error_mark_node)
8766 /* If it didn't work out, we don't have a TYPE. */
8767 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
8768 && !cp_parser_parse_definitely (parser))
8772 /* If we didn't get a type-name, issue an error message. */
8773 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
8775 cp_parser_error (parser, "expected type-name");
8776 return error_mark_node;
8782 /* Parse a type-name.
8795 Returns a TYPE_DECL for the the type. */
8798 cp_parser_type_name (parser)
8804 /* We can't know yet whether it is a class-name or not. */
8805 cp_parser_parse_tentatively (parser);
8806 /* Try a class-name. */
8807 type_decl = cp_parser_class_name (parser,
8808 /*typename_keyword_p=*/false,
8809 /*template_keyword_p=*/false,
8811 /*check_access_p=*/true,
8812 /*check_dependency_p=*/true,
8813 /*class_head_p=*/false);
8814 /* If it's not a class-name, keep looking. */
8815 if (!cp_parser_parse_definitely (parser))
8817 /* It must be a typedef-name or an enum-name. */
8818 identifier = cp_parser_identifier (parser);
8819 if (identifier == error_mark_node)
8820 return error_mark_node;
8822 /* Look up the type-name. */
8823 type_decl = cp_parser_lookup_name_simple (parser, identifier);
8824 /* Issue an error if we did not find a type-name. */
8825 if (TREE_CODE (type_decl) != TYPE_DECL)
8827 cp_parser_error (parser, "expected type-name");
8828 type_decl = error_mark_node;
8830 /* Remember that the name was used in the definition of the
8831 current class so that we can check later to see if the
8832 meaning would have been different after the class was
8833 entirely defined. */
8834 else if (type_decl != error_mark_node
8836 maybe_note_name_used_in_class (identifier, type_decl);
8843 /* Parse an elaborated-type-specifier. Note that the grammar given
8844 here incorporates the resolution to DR68.
8846 elaborated-type-specifier:
8847 class-key :: [opt] nested-name-specifier [opt] identifier
8848 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
8849 enum :: [opt] nested-name-specifier [opt] identifier
8850 typename :: [opt] nested-name-specifier identifier
8851 typename :: [opt] nested-name-specifier template [opt]
8854 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
8855 declared `friend'. If IS_DECLARATION is TRUE, then this
8856 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
8857 something is being declared.
8859 Returns the TYPE specified. */
8862 cp_parser_elaborated_type_specifier (parser, is_friend, is_declaration)
8865 bool is_declaration;
8867 enum tag_types tag_type;
8869 tree type = NULL_TREE;
8871 /* See if we're looking at the `enum' keyword. */
8872 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
8874 /* Consume the `enum' token. */
8875 cp_lexer_consume_token (parser->lexer);
8876 /* Remember that it's an enumeration type. */
8877 tag_type = enum_type;
8879 /* Or, it might be `typename'. */
8880 else if (cp_lexer_next_token_is_keyword (parser->lexer,
8883 /* Consume the `typename' token. */
8884 cp_lexer_consume_token (parser->lexer);
8885 /* Remember that it's a `typename' type. */
8886 tag_type = typename_type;
8887 /* The `typename' keyword is only allowed in templates. */
8888 if (!processing_template_decl)
8889 pedwarn ("using `typename' outside of template");
8891 /* Otherwise it must be a class-key. */
8894 tag_type = cp_parser_class_key (parser);
8895 if (tag_type == none_type)
8896 return error_mark_node;
8899 /* Look for the `::' operator. */
8900 cp_parser_global_scope_opt (parser,
8901 /*current_scope_valid_p=*/false);
8902 /* Look for the nested-name-specifier. */
8903 if (tag_type == typename_type)
8904 cp_parser_nested_name_specifier (parser,
8905 /*typename_keyword_p=*/true,
8906 /*check_dependency_p=*/true,
8909 /* Even though `typename' is not present, the proposed resolution
8910 to Core Issue 180 says that in `class A<T>::B', `B' should be
8911 considered a type-name, even if `A<T>' is dependent. */
8912 cp_parser_nested_name_specifier_opt (parser,
8913 /*typename_keyword_p=*/true,
8914 /*check_dependency_p=*/true,
8916 /* For everything but enumeration types, consider a template-id. */
8917 if (tag_type != enum_type)
8919 bool template_p = false;
8922 /* Allow the `template' keyword. */
8923 template_p = cp_parser_optional_template_keyword (parser);
8924 /* If we didn't see `template', we don't know if there's a
8925 template-id or not. */
8927 cp_parser_parse_tentatively (parser);
8928 /* Parse the template-id. */
8929 decl = cp_parser_template_id (parser, template_p,
8930 /*check_dependency_p=*/true);
8931 /* If we didn't find a template-id, look for an ordinary
8933 if (!template_p && !cp_parser_parse_definitely (parser))
8935 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
8936 in effect, then we must assume that, upon instantiation, the
8937 template will correspond to a class. */
8938 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
8939 && tag_type == typename_type)
8940 type = make_typename_type (parser->scope, decl,
8943 type = TREE_TYPE (decl);
8946 /* For an enumeration type, consider only a plain identifier. */
8949 identifier = cp_parser_identifier (parser);
8951 if (identifier == error_mark_node)
8952 return error_mark_node;
8954 /* For a `typename', we needn't call xref_tag. */
8955 if (tag_type == typename_type)
8956 return make_typename_type (parser->scope, identifier,
8958 /* Look up a qualified name in the usual way. */
8963 /* In an elaborated-type-specifier, names are assumed to name
8964 types, so we set IS_TYPE to TRUE when calling
8965 cp_parser_lookup_name. */
8966 decl = cp_parser_lookup_name (parser, identifier,
8967 /*check_access=*/true,
8969 /*check_dependency=*/true);
8970 decl = (cp_parser_maybe_treat_template_as_class
8971 (decl, /*tag_name_p=*/is_friend));
8973 if (TREE_CODE (decl) != TYPE_DECL)
8975 error ("expected type-name");
8976 return error_mark_node;
8978 else if (TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE
8979 && tag_type != enum_type)
8980 error ("`%T' referred to as `%s'", TREE_TYPE (decl),
8981 tag_type == record_type ? "struct" : "class");
8982 else if (TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE
8983 && tag_type == enum_type)
8984 error ("`%T' referred to as enum", TREE_TYPE (decl));
8986 type = TREE_TYPE (decl);
8990 /* An elaborated-type-specifier sometimes introduces a new type and
8991 sometimes names an existing type. Normally, the rule is that it
8992 introduces a new type only if there is not an existing type of
8993 the same name already in scope. For example, given:
8996 void f() { struct S s; }
8998 the `struct S' in the body of `f' is the same `struct S' as in
8999 the global scope; the existing definition is used. However, if
9000 there were no global declaration, this would introduce a new
9001 local class named `S'.
9003 An exception to this rule applies to the following code:
9005 namespace N { struct S; }
9007 Here, the elaborated-type-specifier names a new type
9008 unconditionally; even if there is already an `S' in the
9009 containing scope this declaration names a new type.
9010 This exception only applies if the elaborated-type-specifier
9011 forms the complete declaration:
9015 A declaration consisting solely of `class-key identifier ;' is
9016 either a redeclaration of the name in the current scope or a
9017 forward declaration of the identifier as a class name. It
9018 introduces the name into the current scope.
9020 We are in this situation precisely when the next token is a `;'.
9022 An exception to the exception is that a `friend' declaration does
9023 *not* name a new type; i.e., given:
9025 struct S { friend struct T; };
9027 `T' is not a new type in the scope of `S'.
9029 Also, `new struct S' or `sizeof (struct S)' never results in the
9030 definition of a new type; a new type can only be declared in a
9031 declaration context. */
9033 type = xref_tag (tag_type, identifier,
9034 /*attributes=*/NULL_TREE,
9037 || cp_lexer_next_token_is_not (parser->lexer,
9041 if (tag_type != enum_type)
9042 cp_parser_check_class_key (tag_type, type);
9046 /* Parse an enum-specifier.
9049 enum identifier [opt] { enumerator-list [opt] }
9051 Returns an ENUM_TYPE representing the enumeration. */
9054 cp_parser_enum_specifier (parser)
9058 tree identifier = NULL_TREE;
9061 /* Look for the `enum' keyword. */
9062 if (!cp_parser_require_keyword (parser, RID_ENUM, "`enum'"))
9063 return error_mark_node;
9064 /* Peek at the next token. */
9065 token = cp_lexer_peek_token (parser->lexer);
9067 /* See if it is an identifier. */
9068 if (token->type == CPP_NAME)
9069 identifier = cp_parser_identifier (parser);
9071 /* Look for the `{'. */
9072 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
9073 return error_mark_node;
9075 /* At this point, we're going ahead with the enum-specifier, even
9076 if some other problem occurs. */
9077 cp_parser_commit_to_tentative_parse (parser);
9079 /* Issue an error message if type-definitions are forbidden here. */
9080 cp_parser_check_type_definition (parser);
9082 /* Create the new type. */
9083 type = start_enum (identifier ? identifier : make_anon_name ());
9085 /* Peek at the next token. */
9086 token = cp_lexer_peek_token (parser->lexer);
9087 /* If it's not a `}', then there are some enumerators. */
9088 if (token->type != CPP_CLOSE_BRACE)
9089 cp_parser_enumerator_list (parser, type);
9090 /* Look for the `}'. */
9091 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
9093 /* Finish up the enumeration. */
9099 /* Parse an enumerator-list. The enumerators all have the indicated
9103 enumerator-definition
9104 enumerator-list , enumerator-definition */
9107 cp_parser_enumerator_list (parser, type)
9115 /* Parse an enumerator-definition. */
9116 cp_parser_enumerator_definition (parser, type);
9117 /* Peek at the next token. */
9118 token = cp_lexer_peek_token (parser->lexer);
9119 /* If it's not a `,', then we've reached the end of the
9121 if (token->type != CPP_COMMA)
9123 /* Otherwise, consume the `,' and keep going. */
9124 cp_lexer_consume_token (parser->lexer);
9125 /* If the next token is a `}', there is a trailing comma. */
9126 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
9128 if (pedantic && !in_system_header)
9129 pedwarn ("comma at end of enumerator list");
9135 /* Parse an enumerator-definition. The enumerator has the indicated
9138 enumerator-definition:
9140 enumerator = constant-expression
9146 cp_parser_enumerator_definition (parser, type)
9154 /* Look for the identifier. */
9155 identifier = cp_parser_identifier (parser);
9156 if (identifier == error_mark_node)
9159 /* Peek at the next token. */
9160 token = cp_lexer_peek_token (parser->lexer);
9161 /* If it's an `=', then there's an explicit value. */
9162 if (token->type == CPP_EQ)
9164 /* Consume the `=' token. */
9165 cp_lexer_consume_token (parser->lexer);
9166 /* Parse the value. */
9167 value = cp_parser_constant_expression (parser);
9172 /* Create the enumerator. */
9173 build_enumerator (identifier, value, type);
9176 /* Parse a namespace-name.
9179 original-namespace-name
9182 Returns the NAMESPACE_DECL for the namespace. */
9185 cp_parser_namespace_name (parser)
9189 tree namespace_decl;
9191 /* Get the name of the namespace. */
9192 identifier = cp_parser_identifier (parser);
9193 if (identifier == error_mark_node)
9194 return error_mark_node;
9196 /* Look up the identifier in the currently active scope. */
9197 namespace_decl = cp_parser_lookup_name_simple (parser, identifier);
9198 /* If it's not a namespace, issue an error. */
9199 if (namespace_decl == error_mark_node
9200 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
9202 cp_parser_error (parser, "expected namespace-name");
9203 namespace_decl = error_mark_node;
9206 return namespace_decl;
9209 /* Parse a namespace-definition.
9211 namespace-definition:
9212 named-namespace-definition
9213 unnamed-namespace-definition
9215 named-namespace-definition:
9216 original-namespace-definition
9217 extension-namespace-definition
9219 original-namespace-definition:
9220 namespace identifier { namespace-body }
9222 extension-namespace-definition:
9223 namespace original-namespace-name { namespace-body }
9225 unnamed-namespace-definition:
9226 namespace { namespace-body } */
9229 cp_parser_namespace_definition (parser)
9234 /* Look for the `namespace' keyword. */
9235 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
9237 /* Get the name of the namespace. We do not attempt to distinguish
9238 between an original-namespace-definition and an
9239 extension-namespace-definition at this point. The semantic
9240 analysis routines are responsible for that. */
9241 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
9242 identifier = cp_parser_identifier (parser);
9244 identifier = NULL_TREE;
9246 /* Look for the `{' to start the namespace. */
9247 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
9248 /* Start the namespace. */
9249 push_namespace (identifier);
9250 /* Parse the body of the namespace. */
9251 cp_parser_namespace_body (parser);
9252 /* Finish the namespace. */
9254 /* Look for the final `}'. */
9255 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
9258 /* Parse a namespace-body.
9261 declaration-seq [opt] */
9264 cp_parser_namespace_body (parser)
9267 cp_parser_declaration_seq_opt (parser);
9270 /* Parse a namespace-alias-definition.
9272 namespace-alias-definition:
9273 namespace identifier = qualified-namespace-specifier ; */
9276 cp_parser_namespace_alias_definition (parser)
9280 tree namespace_specifier;
9282 /* Look for the `namespace' keyword. */
9283 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
9284 /* Look for the identifier. */
9285 identifier = cp_parser_identifier (parser);
9286 if (identifier == error_mark_node)
9288 /* Look for the `=' token. */
9289 cp_parser_require (parser, CPP_EQ, "`='");
9290 /* Look for the qualified-namespace-specifier. */
9292 = cp_parser_qualified_namespace_specifier (parser);
9293 /* Look for the `;' token. */
9294 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
9296 /* Register the alias in the symbol table. */
9297 do_namespace_alias (identifier, namespace_specifier);
9300 /* Parse a qualified-namespace-specifier.
9302 qualified-namespace-specifier:
9303 :: [opt] nested-name-specifier [opt] namespace-name
9305 Returns a NAMESPACE_DECL corresponding to the specified
9309 cp_parser_qualified_namespace_specifier (parser)
9312 /* Look for the optional `::'. */
9313 cp_parser_global_scope_opt (parser,
9314 /*current_scope_valid_p=*/false);
9316 /* Look for the optional nested-name-specifier. */
9317 cp_parser_nested_name_specifier_opt (parser,
9318 /*typename_keyword_p=*/false,
9319 /*check_dependency_p=*/true,
9322 return cp_parser_namespace_name (parser);
9325 /* Parse a using-declaration.
9328 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
9329 using :: unqualified-id ; */
9332 cp_parser_using_declaration (parser)
9336 bool typename_p = false;
9337 bool global_scope_p;
9342 /* Look for the `using' keyword. */
9343 cp_parser_require_keyword (parser, RID_USING, "`using'");
9345 /* Peek at the next token. */
9346 token = cp_lexer_peek_token (parser->lexer);
9347 /* See if it's `typename'. */
9348 if (token->keyword == RID_TYPENAME)
9350 /* Remember that we've seen it. */
9352 /* Consume the `typename' token. */
9353 cp_lexer_consume_token (parser->lexer);
9356 /* Look for the optional global scope qualification. */
9358 = (cp_parser_global_scope_opt (parser,
9359 /*current_scope_valid_p=*/false)
9362 /* If we saw `typename', or didn't see `::', then there must be a
9363 nested-name-specifier present. */
9364 if (typename_p || !global_scope_p)
9365 cp_parser_nested_name_specifier (parser, typename_p,
9366 /*check_dependency_p=*/true,
9368 /* Otherwise, we could be in either of the two productions. In that
9369 case, treat the nested-name-specifier as optional. */
9371 cp_parser_nested_name_specifier_opt (parser,
9372 /*typename_keyword_p=*/false,
9373 /*check_dependency_p=*/true,
9376 /* Parse the unqualified-id. */
9377 identifier = cp_parser_unqualified_id (parser,
9378 /*template_keyword_p=*/false,
9379 /*check_dependency_p=*/true);
9381 /* The function we call to handle a using-declaration is different
9382 depending on what scope we are in. */
9383 scope = current_scope ();
9384 if (scope && TYPE_P (scope))
9386 /* Create the USING_DECL. */
9387 decl = do_class_using_decl (build_nt (SCOPE_REF,
9390 /* Add it to the list of members in this class. */
9391 finish_member_declaration (decl);
9395 decl = cp_parser_lookup_name_simple (parser, identifier);
9397 do_local_using_decl (decl);
9399 do_toplevel_using_decl (decl);
9402 /* Look for the final `;'. */
9403 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
9406 /* Parse a using-directive.
9409 using namespace :: [opt] nested-name-specifier [opt]
9413 cp_parser_using_directive (parser)
9416 tree namespace_decl;
9418 /* Look for the `using' keyword. */
9419 cp_parser_require_keyword (parser, RID_USING, "`using'");
9420 /* And the `namespace' keyword. */
9421 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
9422 /* Look for the optional `::' operator. */
9423 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
9424 /* And the optional nested-name-sepcifier. */
9425 cp_parser_nested_name_specifier_opt (parser,
9426 /*typename_keyword_p=*/false,
9427 /*check_dependency_p=*/true,
9429 /* Get the namespace being used. */
9430 namespace_decl = cp_parser_namespace_name (parser);
9431 /* Update the symbol table. */
9432 do_using_directive (namespace_decl);
9433 /* Look for the final `;'. */
9434 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
9437 /* Parse an asm-definition.
9440 asm ( string-literal ) ;
9445 asm volatile [opt] ( string-literal ) ;
9446 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
9447 asm volatile [opt] ( string-literal : asm-operand-list [opt]
9448 : asm-operand-list [opt] ) ;
9449 asm volatile [opt] ( string-literal : asm-operand-list [opt]
9450 : asm-operand-list [opt]
9451 : asm-operand-list [opt] ) ; */
9454 cp_parser_asm_definition (parser)
9459 tree outputs = NULL_TREE;
9460 tree inputs = NULL_TREE;
9461 tree clobbers = NULL_TREE;
9463 bool volatile_p = false;
9464 bool extended_p = false;
9466 /* Look for the `asm' keyword. */
9467 cp_parser_require_keyword (parser, RID_ASM, "`asm'");
9468 /* See if the next token is `volatile'. */
9469 if (cp_parser_allow_gnu_extensions_p (parser)
9470 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
9472 /* Remember that we saw the `volatile' keyword. */
9474 /* Consume the token. */
9475 cp_lexer_consume_token (parser->lexer);
9477 /* Look for the opening `('. */
9478 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
9479 /* Look for the string. */
9480 token = cp_parser_require (parser, CPP_STRING, "asm body");
9483 string = token->value;
9484 /* If we're allowing GNU extensions, check for the extended assembly
9485 syntax. Unfortunately, the `:' tokens need not be separated by
9486 a space in C, and so, for compatibility, we tolerate that here
9487 too. Doing that means that we have to treat the `::' operator as
9489 if (cp_parser_allow_gnu_extensions_p (parser)
9490 && at_function_scope_p ()
9491 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
9492 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
9494 bool inputs_p = false;
9495 bool clobbers_p = false;
9497 /* The extended syntax was used. */
9500 /* Look for outputs. */
9501 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
9503 /* Consume the `:'. */
9504 cp_lexer_consume_token (parser->lexer);
9505 /* Parse the output-operands. */
9506 if (cp_lexer_next_token_is_not (parser->lexer,
9508 && cp_lexer_next_token_is_not (parser->lexer,
9510 outputs = cp_parser_asm_operand_list (parser);
9512 /* If the next token is `::', there are no outputs, and the
9513 next token is the beginning of the inputs. */
9514 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
9516 /* Consume the `::' token. */
9517 cp_lexer_consume_token (parser->lexer);
9518 /* The inputs are coming next. */
9522 /* Look for inputs. */
9524 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
9527 /* Consume the `:'. */
9528 cp_lexer_consume_token (parser->lexer);
9529 /* Parse the output-operands. */
9530 if (cp_lexer_next_token_is_not (parser->lexer,
9532 && cp_lexer_next_token_is_not (parser->lexer,
9534 inputs = cp_parser_asm_operand_list (parser);
9536 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
9537 /* The clobbers are coming next. */
9540 /* Look for clobbers. */
9542 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
9545 /* Consume the `:'. */
9546 cp_lexer_consume_token (parser->lexer);
9547 /* Parse the clobbers. */
9548 clobbers = cp_parser_asm_clobber_list (parser);
9551 /* Look for the closing `)'. */
9552 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
9553 cp_parser_skip_to_closing_parenthesis (parser);
9554 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
9556 /* Create the ASM_STMT. */
9557 if (at_function_scope_p ())
9560 finish_asm_stmt (volatile_p
9561 ? ridpointers[(int) RID_VOLATILE] : NULL_TREE,
9562 string, outputs, inputs, clobbers);
9563 /* If the extended syntax was not used, mark the ASM_STMT. */
9565 ASM_INPUT_P (asm_stmt) = 1;
9568 assemble_asm (string);
9571 /* Declarators [gram.dcl.decl] */
9573 /* Parse an init-declarator.
9576 declarator initializer [opt]
9581 declarator asm-specification [opt] attributes [opt] initializer [opt]
9583 The DECL_SPECIFIERS and PREFIX_ATTRIBUTES apply to this declarator.
9584 Returns a reprsentation of the entity declared. The ACCESS_CHECKS
9585 represent deferred access checks from the decl-specifier-seq. If
9586 MEMBER_P is TRUE, then this declarator appears in a class scope.
9587 The new DECL created by this declarator is returned.
9589 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
9590 for a function-definition here as well. If the declarator is a
9591 declarator for a function-definition, *FUNCTION_DEFINITION_P will
9592 be TRUE upon return. By that point, the function-definition will
9593 have been completely parsed.
9595 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
9599 cp_parser_init_declarator (parser,
9603 function_definition_allowed_p,
9605 function_definition_p)
9607 tree decl_specifiers;
9608 tree prefix_attributes;
9610 bool function_definition_allowed_p;
9612 bool *function_definition_p;
9617 tree asm_specification;
9619 tree decl = NULL_TREE;
9621 tree declarator_access_checks;
9622 bool is_initialized;
9623 bool is_parenthesized_init;
9624 bool ctor_dtor_or_conv_p;
9627 /* Assume that this is not the declarator for a function
9629 if (function_definition_p)
9630 *function_definition_p = false;
9632 /* Defer access checks while parsing the declarator; we cannot know
9633 what names are accessible until we know what is being
9635 cp_parser_start_deferring_access_checks (parser);
9636 /* Parse the declarator. */
9638 = cp_parser_declarator (parser,
9639 /*abstract_p=*/false,
9640 &ctor_dtor_or_conv_p);
9641 /* Gather up the deferred checks. */
9642 declarator_access_checks
9643 = cp_parser_stop_deferring_access_checks (parser);
9645 /* If the DECLARATOR was erroneous, there's no need to go
9647 if (declarator == error_mark_node)
9648 return error_mark_node;
9650 /* Figure out what scope the entity declared by the DECLARATOR is
9651 located in. `grokdeclarator' sometimes changes the scope, so
9652 we compute it now. */
9653 scope = get_scope_of_declarator (declarator);
9655 /* If we're allowing GNU extensions, look for an asm-specification
9657 if (cp_parser_allow_gnu_extensions_p (parser))
9659 /* Look for an asm-specification. */
9660 asm_specification = cp_parser_asm_specification_opt (parser);
9661 /* And attributes. */
9662 attributes = cp_parser_attributes_opt (parser);
9666 asm_specification = NULL_TREE;
9667 attributes = NULL_TREE;
9670 /* Peek at the next token. */
9671 token = cp_lexer_peek_token (parser->lexer);
9672 /* Check to see if the token indicates the start of a
9673 function-definition. */
9674 if (cp_parser_token_starts_function_definition_p (token))
9676 if (!function_definition_allowed_p)
9678 /* If a function-definition should not appear here, issue an
9680 cp_parser_error (parser,
9681 "a function-definition is not allowed here");
9682 return error_mark_node;
9688 /* Neither attributes nor an asm-specification are allowed
9689 on a function-definition. */
9690 if (asm_specification)
9691 error ("an asm-specification is not allowed on a function-definition");
9693 error ("attributes are not allowed on a function-definition");
9694 /* This is a function-definition. */
9695 *function_definition_p = true;
9697 /* Thread the access checks together. */
9698 ac = &access_checks;
9700 ac = &TREE_CHAIN (*ac);
9701 *ac = declarator_access_checks;
9703 /* Parse the function definition. */
9704 decl = (cp_parser_function_definition_from_specifiers_and_declarator
9705 (parser, decl_specifiers, prefix_attributes, declarator,
9708 /* Pull the access-checks apart again. */
9717 Only in function declarations for constructors, destructors, and
9718 type conversions can the decl-specifier-seq be omitted.
9720 We explicitly postpone this check past the point where we handle
9721 function-definitions because we tolerate function-definitions
9722 that are missing their return types in some modes. */
9723 if (!decl_specifiers && !ctor_dtor_or_conv_p)
9725 cp_parser_error (parser,
9726 "expected constructor, destructor, or type conversion");
9727 return error_mark_node;
9730 /* An `=' or an `(' indicates an initializer. */
9731 is_initialized = (token->type == CPP_EQ
9732 || token->type == CPP_OPEN_PAREN);
9733 /* If the init-declarator isn't initialized and isn't followed by a
9734 `,' or `;', it's not a valid init-declarator. */
9736 && token->type != CPP_COMMA
9737 && token->type != CPP_SEMICOLON)
9739 cp_parser_error (parser, "expected init-declarator");
9740 return error_mark_node;
9743 /* Because start_decl has side-effects, we should only call it if we
9744 know we're going ahead. By this point, we know that we cannot
9745 possibly be looking at any other construct. */
9746 cp_parser_commit_to_tentative_parse (parser);
9748 /* Check to see whether or not this declaration is a friend. */
9749 friend_p = cp_parser_friend_p (decl_specifiers);
9751 /* Check that the number of template-parameter-lists is OK. */
9752 if (!cp_parser_check_declarator_template_parameters (parser,
9754 return error_mark_node;
9756 /* Enter the newly declared entry in the symbol table. If we're
9757 processing a declaration in a class-specifier, we wait until
9758 after processing the initializer. */
9761 if (parser->in_unbraced_linkage_specification_p)
9763 decl_specifiers = tree_cons (error_mark_node,
9764 get_identifier ("extern"),
9766 have_extern_spec = false;
9768 decl = start_decl (declarator,
9775 /* Enter the SCOPE. That way unqualified names appearing in the
9776 initializer will be looked up in SCOPE. */
9780 /* Perform deferred access control checks, now that we know in which
9781 SCOPE the declared entity resides. */
9782 if (!member_p && decl)
9784 tree saved_current_function_decl = NULL_TREE;
9786 /* If the entity being declared is a function, pretend that we
9787 are in its scope. If it is a `friend', it may have access to
9788 things that would not otherwise be accessible. */
9789 if (TREE_CODE (decl) == FUNCTION_DECL)
9791 saved_current_function_decl = current_function_decl;
9792 current_function_decl = decl;
9795 /* Perform the access control checks for the decl-specifiers. */
9796 cp_parser_perform_deferred_access_checks (access_checks);
9797 /* And for the declarator. */
9798 cp_parser_perform_deferred_access_checks (declarator_access_checks);
9800 /* Restore the saved value. */
9801 if (TREE_CODE (decl) == FUNCTION_DECL)
9802 current_function_decl = saved_current_function_decl;
9805 /* Parse the initializer. */
9807 initializer = cp_parser_initializer (parser,
9808 &is_parenthesized_init);
9811 initializer = NULL_TREE;
9812 is_parenthesized_init = false;
9815 /* The old parser allows attributes to appear after a parenthesized
9816 initializer. Mark Mitchell proposed removing this functionality
9817 on the GCC mailing lists on 2002-08-13. This parser accepts the
9818 attributes -- but ignores them. */
9819 if (cp_parser_allow_gnu_extensions_p (parser) && is_parenthesized_init)
9820 if (cp_parser_attributes_opt (parser))
9821 warning ("attributes after parenthesized initializer ignored");
9823 /* Leave the SCOPE, now that we have processed the initializer. It
9824 is important to do this before calling cp_finish_decl because it
9825 makes decisions about whether to create DECL_STMTs or not based
9826 on the current scope. */
9830 /* For an in-class declaration, use `grokfield' to create the
9833 decl = grokfield (declarator, decl_specifiers,
9834 initializer, /*asmspec=*/NULL_TREE,
9835 /*attributes=*/NULL_TREE);
9837 /* Finish processing the declaration. But, skip friend
9839 if (!friend_p && decl)
9840 cp_finish_decl (decl,
9843 /* If the initializer is in parentheses, then this is
9844 a direct-initialization, which means that an
9845 `explicit' constructor is OK. Otherwise, an
9846 `explicit' constructor cannot be used. */
9847 ((is_parenthesized_init || !is_initialized)
9848 ? 0 : LOOKUP_ONLYCONVERTING));
9853 /* Parse a declarator.
9857 ptr-operator declarator
9859 abstract-declarator:
9860 ptr-operator abstract-declarator [opt]
9861 direct-abstract-declarator
9866 attributes [opt] direct-declarator
9867 attributes [opt] ptr-operator declarator
9869 abstract-declarator:
9870 attributes [opt] ptr-operator abstract-declarator [opt]
9871 attributes [opt] direct-abstract-declarator
9873 Returns a representation of the declarator. If the declarator has
9874 the form `* declarator', then an INDIRECT_REF is returned, whose
9875 only operand is the sub-declarator. Analagously, `& declarator' is
9876 represented as an ADDR_EXPR. For `X::* declarator', a SCOPE_REF is
9877 used. The first operand is the TYPE for `X'. The second operand
9878 is an INDIRECT_REF whose operand is the sub-declarator.
9880 Otherwise, the reprsentation is as for a direct-declarator.
9882 (It would be better to define a structure type to represent
9883 declarators, rather than abusing `tree' nodes to represent
9884 declarators. That would be much clearer and save some memory.
9885 There is no reason for declarators to be garbage-collected, for
9886 example; they are created during parser and no longer needed after
9887 `grokdeclarator' has been called.)
9889 For a ptr-operator that has the optional cv-qualifier-seq,
9890 cv-qualifiers will be stored in the TREE_TYPE of the INDIRECT_REF
9893 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is set to
9894 true if this declarator represents a constructor, destructor, or
9895 type conversion operator. Otherwise, it is set to false.
9897 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
9898 a decl-specifier-seq unless it declares a constructor, destructor,
9899 or conversion. It might seem that we could check this condition in
9900 semantic analysis, rather than parsing, but that makes it difficult
9901 to handle something like `f()'. We want to notice that there are
9902 no decl-specifiers, and therefore realize that this is an
9903 expression, not a declaration.) */
9906 cp_parser_declarator (parser, abstract_p, ctor_dtor_or_conv_p)
9909 bool *ctor_dtor_or_conv_p;
9913 enum tree_code code;
9914 tree cv_qualifier_seq;
9916 tree attributes = NULL_TREE;
9918 /* Assume this is not a constructor, destructor, or type-conversion
9920 if (ctor_dtor_or_conv_p)
9921 *ctor_dtor_or_conv_p = false;
9923 if (cp_parser_allow_gnu_extensions_p (parser))
9924 attributes = cp_parser_attributes_opt (parser);
9926 /* Peek at the next token. */
9927 token = cp_lexer_peek_token (parser->lexer);
9929 /* Check for the ptr-operator production. */
9930 cp_parser_parse_tentatively (parser);
9931 /* Parse the ptr-operator. */
9932 code = cp_parser_ptr_operator (parser,
9935 /* If that worked, then we have a ptr-operator. */
9936 if (cp_parser_parse_definitely (parser))
9938 /* The dependent declarator is optional if we are parsing an
9939 abstract-declarator. */
9941 cp_parser_parse_tentatively (parser);
9943 /* Parse the dependent declarator. */
9944 declarator = cp_parser_declarator (parser, abstract_p,
9945 /*ctor_dtor_or_conv_p=*/NULL);
9947 /* If we are parsing an abstract-declarator, we must handle the
9948 case where the dependent declarator is absent. */
9949 if (abstract_p && !cp_parser_parse_definitely (parser))
9950 declarator = NULL_TREE;
9952 /* Build the representation of the ptr-operator. */
9953 if (code == INDIRECT_REF)
9954 declarator = make_pointer_declarator (cv_qualifier_seq,
9957 declarator = make_reference_declarator (cv_qualifier_seq,
9959 /* Handle the pointer-to-member case. */
9961 declarator = build_nt (SCOPE_REF, class_type, declarator);
9963 /* Everything else is a direct-declarator. */
9965 declarator = cp_parser_direct_declarator (parser,
9967 ctor_dtor_or_conv_p);
9969 if (attributes && declarator != error_mark_node)
9970 declarator = tree_cons (attributes, declarator, NULL_TREE);
9975 /* Parse a direct-declarator or direct-abstract-declarator.
9979 direct-declarator ( parameter-declaration-clause )
9980 cv-qualifier-seq [opt]
9981 exception-specification [opt]
9982 direct-declarator [ constant-expression [opt] ]
9985 direct-abstract-declarator:
9986 direct-abstract-declarator [opt]
9987 ( parameter-declaration-clause )
9988 cv-qualifier-seq [opt]
9989 exception-specification [opt]
9990 direct-abstract-declarator [opt] [ constant-expression [opt] ]
9991 ( abstract-declarator )
9993 Returns a representation of the declarator. ABSTRACT_P is TRUE if
9994 we are parsing a direct-abstract-declarator; FALSE if we are
9995 parsing a direct-declarator. CTOR_DTOR_OR_CONV_P is as for
9996 cp_parser_declarator.
9998 For the declarator-id production, the representation is as for an
9999 id-expression, except that a qualified name is represented as a
10000 SCOPE_REF. A function-declarator is represented as a CALL_EXPR;
10001 see the documentation of the FUNCTION_DECLARATOR_* macros for
10002 information about how to find the various declarator components.
10003 An array-declarator is represented as an ARRAY_REF. The
10004 direct-declarator is the first operand; the constant-expression
10005 indicating the size of the array is the second operand. */
10008 cp_parser_direct_declarator (parser, abstract_p, ctor_dtor_or_conv_p)
10011 bool *ctor_dtor_or_conv_p;
10015 tree scope = NULL_TREE;
10016 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
10017 bool saved_in_declarator_p = parser->in_declarator_p;
10019 /* Peek at the next token. */
10020 token = cp_lexer_peek_token (parser->lexer);
10021 /* Find the initial direct-declarator. It might be a parenthesized
10023 if (token->type == CPP_OPEN_PAREN)
10027 /* For an abstract declarator we do not know whether we are
10028 looking at the beginning of a parameter-declaration-clause,
10029 or at a parenthesized abstract declarator. For example, if
10030 we see `(int)', we are looking at a
10031 parameter-declaration-clause, and the
10032 direct-abstract-declarator has been omitted. If, on the
10033 other hand we are looking at `((*))' then we are looking at a
10034 parenthesized abstract-declarator. There is no easy way to
10035 tell which situation we are in. */
10037 cp_parser_parse_tentatively (parser);
10039 /* Consume the `('. */
10040 cp_lexer_consume_token (parser->lexer);
10041 /* Parse the nested declarator. */
10043 = cp_parser_declarator (parser, abstract_p, ctor_dtor_or_conv_p);
10044 /* Expect a `)'. */
10045 error_p = !cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
10047 /* If parsing a parenthesized abstract declarator didn't work,
10048 try a parameter-declaration-clause. */
10049 if (abstract_p && !cp_parser_parse_definitely (parser))
10050 declarator = NULL_TREE;
10051 /* If we were not parsing an abstract declarator, but failed to
10052 find a satisfactory nested declarator, then an error has
10054 else if (!abstract_p
10055 && (declarator == error_mark_node || error_p))
10056 return error_mark_node;
10057 /* Default args cannot appear in an abstract decl. */
10058 parser->default_arg_ok_p = false;
10060 /* Otherwise, for a non-abstract declarator, there should be a
10062 else if (!abstract_p)
10064 declarator = cp_parser_declarator_id (parser);
10066 if (TREE_CODE (declarator) == SCOPE_REF)
10068 scope = TREE_OPERAND (declarator, 0);
10070 /* In the declaration of a member of a template class
10071 outside of the class itself, the SCOPE will sometimes be
10072 a TYPENAME_TYPE. For example, given:
10074 template <typename T>
10075 int S<T>::R::i = 3;
10077 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In this
10078 context, we must resolve S<T>::R to an ordinary type,
10079 rather than a typename type.
10081 The reason we normally avoid resolving TYPENAME_TYPEs is
10082 that a specialization of `S' might render `S<T>::R' not a
10083 type. However, if `S' is specialized, then this `i' will
10084 not be used, so there is no harm in resolving the types
10086 if (TREE_CODE (scope) == TYPENAME_TYPE)
10088 /* Resolve the TYPENAME_TYPE. */
10089 scope = cp_parser_resolve_typename_type (parser, scope);
10090 /* If that failed, the declarator is invalid. */
10091 if (scope == error_mark_node)
10092 return error_mark_node;
10093 /* Build a new DECLARATOR. */
10094 declarator = build_nt (SCOPE_REF,
10096 TREE_OPERAND (declarator, 1));
10099 else if (TREE_CODE (declarator) != IDENTIFIER_NODE)
10100 /* Default args can only appear for a function decl. */
10101 parser->default_arg_ok_p = false;
10103 /* Check to see whether the declarator-id names a constructor,
10104 destructor, or conversion. */
10105 if (ctor_dtor_or_conv_p
10106 && ((TREE_CODE (declarator) == SCOPE_REF
10107 && CLASS_TYPE_P (TREE_OPERAND (declarator, 0)))
10108 || (TREE_CODE (declarator) != SCOPE_REF
10109 && at_class_scope_p ())))
10111 tree unqualified_name;
10114 /* Get the unqualified part of the name. */
10115 if (TREE_CODE (declarator) == SCOPE_REF)
10117 class_type = TREE_OPERAND (declarator, 0);
10118 unqualified_name = TREE_OPERAND (declarator, 1);
10122 class_type = current_class_type;
10123 unqualified_name = declarator;
10126 /* See if it names ctor, dtor or conv. */
10127 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR
10128 || IDENTIFIER_TYPENAME_P (unqualified_name)
10129 || constructor_name_p (unqualified_name, class_type))
10131 *ctor_dtor_or_conv_p = true;
10132 /* We would have cleared the default arg flag above, but
10134 parser->default_arg_ok_p = saved_default_arg_ok_p;
10138 /* But for an abstract declarator, the initial direct-declarator can
10142 declarator = NULL_TREE;
10143 parser->default_arg_ok_p = false;
10146 scope = get_scope_of_declarator (declarator);
10148 /* Any names that appear after the declarator-id for a member
10149 are looked up in the containing scope. */
10150 push_scope (scope);
10153 parser->in_declarator_p = true;
10155 /* Now, parse function-declarators and array-declarators until there
10159 /* Peek at the next token. */
10160 token = cp_lexer_peek_token (parser->lexer);
10161 /* If it's a `[', we're looking at an array-declarator. */
10162 if (token->type == CPP_OPEN_SQUARE)
10166 /* Consume the `['. */
10167 cp_lexer_consume_token (parser->lexer);
10168 /* Peek at the next token. */
10169 token = cp_lexer_peek_token (parser->lexer);
10170 /* If the next token is `]', then there is no
10171 constant-expression. */
10172 if (token->type != CPP_CLOSE_SQUARE)
10173 bounds = cp_parser_constant_expression (parser);
10175 bounds = NULL_TREE;
10176 /* Look for the closing `]'. */
10177 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
10179 declarator = build_nt (ARRAY_REF, declarator, bounds);
10181 /* If it's a `(', we're looking at a function-declarator. */
10182 else if (token->type == CPP_OPEN_PAREN)
10184 /* A function-declarator. Or maybe not. Consider, for
10190 The first is the declaration of a function while the
10191 second is a the definition of a variable, including its
10194 Having seen only the parenthesis, we cannot know which of
10195 these two alternatives should be selected. Even more
10196 complex are examples like:
10201 The former is a function-declaration; the latter is a
10202 variable initialization.
10204 First, we attempt to parse a parameter-declaration
10205 clause. If this works, then we continue; otherwise, we
10206 replace the tokens consumed in the process and continue. */
10209 /* We are now parsing tentatively. */
10210 cp_parser_parse_tentatively (parser);
10212 /* Consume the `('. */
10213 cp_lexer_consume_token (parser->lexer);
10214 /* Parse the parameter-declaration-clause. */
10215 params = cp_parser_parameter_declaration_clause (parser);
10217 /* If all went well, parse the cv-qualifier-seq and the
10218 exception-specification. */
10219 if (cp_parser_parse_definitely (parser))
10221 tree cv_qualifiers;
10222 tree exception_specification;
10224 /* Consume the `)'. */
10225 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
10227 /* Parse the cv-qualifier-seq. */
10228 cv_qualifiers = cp_parser_cv_qualifier_seq_opt (parser);
10229 /* And the exception-specification. */
10230 exception_specification
10231 = cp_parser_exception_specification_opt (parser);
10233 /* Create the function-declarator. */
10234 declarator = make_call_declarator (declarator,
10237 exception_specification);
10239 /* Otherwise, we must be done with the declarator. */
10243 /* Otherwise, we're done with the declarator. */
10246 /* Any subsequent parameter lists are to do with return type, so
10247 are not those of the declared function. */
10248 parser->default_arg_ok_p = false;
10251 /* For an abstract declarator, we might wind up with nothing at this
10252 point. That's an error; the declarator is not optional. */
10254 cp_parser_error (parser, "expected declarator");
10256 /* If we entered a scope, we must exit it now. */
10260 parser->default_arg_ok_p = saved_default_arg_ok_p;
10261 parser->in_declarator_p = saved_in_declarator_p;
10266 /* Parse a ptr-operator.
10269 * cv-qualifier-seq [opt]
10271 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
10276 & cv-qualifier-seq [opt]
10278 Returns INDIRECT_REF if a pointer, or pointer-to-member, was
10279 used. Returns ADDR_EXPR if a reference was used. In the
10280 case of a pointer-to-member, *TYPE is filled in with the
10281 TYPE containing the member. *CV_QUALIFIER_SEQ is filled in
10282 with the cv-qualifier-seq, or NULL_TREE, if there are no
10283 cv-qualifiers. Returns ERROR_MARK if an error occurred. */
10285 static enum tree_code
10286 cp_parser_ptr_operator (parser, type, cv_qualifier_seq)
10289 tree *cv_qualifier_seq;
10291 enum tree_code code = ERROR_MARK;
10294 /* Assume that it's not a pointer-to-member. */
10296 /* And that there are no cv-qualifiers. */
10297 *cv_qualifier_seq = NULL_TREE;
10299 /* Peek at the next token. */
10300 token = cp_lexer_peek_token (parser->lexer);
10301 /* If it's a `*' or `&' we have a pointer or reference. */
10302 if (token->type == CPP_MULT || token->type == CPP_AND)
10304 /* Remember which ptr-operator we were processing. */
10305 code = (token->type == CPP_AND ? ADDR_EXPR : INDIRECT_REF);
10307 /* Consume the `*' or `&'. */
10308 cp_lexer_consume_token (parser->lexer);
10310 /* A `*' can be followed by a cv-qualifier-seq, and so can a
10311 `&', if we are allowing GNU extensions. (The only qualifier
10312 that can legally appear after `&' is `restrict', but that is
10313 enforced during semantic analysis. */
10314 if (code == INDIRECT_REF
10315 || cp_parser_allow_gnu_extensions_p (parser))
10316 *cv_qualifier_seq = cp_parser_cv_qualifier_seq_opt (parser);
10320 /* Try the pointer-to-member case. */
10321 cp_parser_parse_tentatively (parser);
10322 /* Look for the optional `::' operator. */
10323 cp_parser_global_scope_opt (parser,
10324 /*current_scope_valid_p=*/false);
10325 /* Look for the nested-name specifier. */
10326 cp_parser_nested_name_specifier (parser,
10327 /*typename_keyword_p=*/false,
10328 /*check_dependency_p=*/true,
10330 /* If we found it, and the next token is a `*', then we are
10331 indeed looking at a pointer-to-member operator. */
10332 if (!cp_parser_error_occurred (parser)
10333 && cp_parser_require (parser, CPP_MULT, "`*'"))
10335 /* The type of which the member is a member is given by the
10337 *type = parser->scope;
10338 /* The next name will not be qualified. */
10339 parser->scope = NULL_TREE;
10340 parser->qualifying_scope = NULL_TREE;
10341 parser->object_scope = NULL_TREE;
10342 /* Indicate that the `*' operator was used. */
10343 code = INDIRECT_REF;
10344 /* Look for the optional cv-qualifier-seq. */
10345 *cv_qualifier_seq = cp_parser_cv_qualifier_seq_opt (parser);
10347 /* If that didn't work we don't have a ptr-operator. */
10348 if (!cp_parser_parse_definitely (parser))
10349 cp_parser_error (parser, "expected ptr-operator");
10355 /* Parse an (optional) cv-qualifier-seq.
10358 cv-qualifier cv-qualifier-seq [opt]
10360 Returns a TREE_LIST. The TREE_VALUE of each node is the
10361 representation of a cv-qualifier. */
10364 cp_parser_cv_qualifier_seq_opt (parser)
10367 tree cv_qualifiers = NULL_TREE;
10373 /* Look for the next cv-qualifier. */
10374 cv_qualifier = cp_parser_cv_qualifier_opt (parser);
10375 /* If we didn't find one, we're done. */
10379 /* Add this cv-qualifier to the list. */
10381 = tree_cons (NULL_TREE, cv_qualifier, cv_qualifiers);
10384 /* We built up the list in reverse order. */
10385 return nreverse (cv_qualifiers);
10388 /* Parse an (optional) cv-qualifier.
10400 cp_parser_cv_qualifier_opt (parser)
10404 tree cv_qualifier = NULL_TREE;
10406 /* Peek at the next token. */
10407 token = cp_lexer_peek_token (parser->lexer);
10408 /* See if it's a cv-qualifier. */
10409 switch (token->keyword)
10414 /* Save the value of the token. */
10415 cv_qualifier = token->value;
10416 /* Consume the token. */
10417 cp_lexer_consume_token (parser->lexer);
10424 return cv_qualifier;
10427 /* Parse a declarator-id.
10431 :: [opt] nested-name-specifier [opt] type-name
10433 In the `id-expression' case, the value returned is as for
10434 cp_parser_id_expression if the id-expression was an unqualified-id.
10435 If the id-expression was a qualified-id, then a SCOPE_REF is
10436 returned. The first operand is the scope (either a NAMESPACE_DECL
10437 or TREE_TYPE), but the second is still just a representation of an
10441 cp_parser_declarator_id (parser)
10444 tree id_expression;
10446 /* The expression must be an id-expression. Assume that qualified
10447 names are the names of types so that:
10450 int S<T>::R::i = 3;
10452 will work; we must treat `S<T>::R' as the name of a type.
10453 Similarly, assume that qualified names are templates, where
10457 int S<T>::R<T>::i = 3;
10460 id_expression = cp_parser_id_expression (parser,
10461 /*template_keyword_p=*/false,
10462 /*check_dependency_p=*/false,
10463 /*template_p=*/NULL);
10464 /* If the name was qualified, create a SCOPE_REF to represent
10467 id_expression = build_nt (SCOPE_REF, parser->scope, id_expression);
10469 return id_expression;
10472 /* Parse a type-id.
10475 type-specifier-seq abstract-declarator [opt]
10477 Returns the TYPE specified. */
10480 cp_parser_type_id (parser)
10483 tree type_specifier_seq;
10484 tree abstract_declarator;
10486 /* Parse the type-specifier-seq. */
10488 = cp_parser_type_specifier_seq (parser);
10489 if (type_specifier_seq == error_mark_node)
10490 return error_mark_node;
10492 /* There might or might not be an abstract declarator. */
10493 cp_parser_parse_tentatively (parser);
10494 /* Look for the declarator. */
10495 abstract_declarator
10496 = cp_parser_declarator (parser, /*abstract_p=*/true, NULL);
10497 /* Check to see if there really was a declarator. */
10498 if (!cp_parser_parse_definitely (parser))
10499 abstract_declarator = NULL_TREE;
10501 return groktypename (build_tree_list (type_specifier_seq,
10502 abstract_declarator));
10505 /* Parse a type-specifier-seq.
10507 type-specifier-seq:
10508 type-specifier type-specifier-seq [opt]
10512 type-specifier-seq:
10513 attributes type-specifier-seq [opt]
10515 Returns a TREE_LIST. Either the TREE_VALUE of each node is a
10516 type-specifier, or the TREE_PURPOSE is a list of attributes. */
10519 cp_parser_type_specifier_seq (parser)
10522 bool seen_type_specifier = false;
10523 tree type_specifier_seq = NULL_TREE;
10525 /* Parse the type-specifiers and attributes. */
10528 tree type_specifier;
10530 /* Check for attributes first. */
10531 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
10533 type_specifier_seq = tree_cons (cp_parser_attributes_opt (parser),
10535 type_specifier_seq);
10539 /* After the first type-specifier, others are optional. */
10540 if (seen_type_specifier)
10541 cp_parser_parse_tentatively (parser);
10542 /* Look for the type-specifier. */
10543 type_specifier = cp_parser_type_specifier (parser,
10544 CP_PARSER_FLAGS_NONE,
10545 /*is_friend=*/false,
10546 /*is_declaration=*/false,
10549 /* If the first type-specifier could not be found, this is not a
10550 type-specifier-seq at all. */
10551 if (!seen_type_specifier && type_specifier == error_mark_node)
10552 return error_mark_node;
10553 /* If subsequent type-specifiers could not be found, the
10554 type-specifier-seq is complete. */
10555 else if (seen_type_specifier && !cp_parser_parse_definitely (parser))
10558 /* Add the new type-specifier to the list. */
10560 = tree_cons (NULL_TREE, type_specifier, type_specifier_seq);
10561 seen_type_specifier = true;
10564 /* We built up the list in reverse order. */
10565 return nreverse (type_specifier_seq);
10568 /* Parse a parameter-declaration-clause.
10570 parameter-declaration-clause:
10571 parameter-declaration-list [opt] ... [opt]
10572 parameter-declaration-list , ...
10574 Returns a representation for the parameter declarations. Each node
10575 is a TREE_LIST. (See cp_parser_parameter_declaration for the exact
10576 representation.) If the parameter-declaration-clause ends with an
10577 ellipsis, PARMLIST_ELLIPSIS_P will hold of the first node in the
10578 list. A return value of NULL_TREE indicates a
10579 parameter-declaration-clause consisting only of an ellipsis. */
10582 cp_parser_parameter_declaration_clause (parser)
10589 /* Peek at the next token. */
10590 token = cp_lexer_peek_token (parser->lexer);
10591 /* Check for trivial parameter-declaration-clauses. */
10592 if (token->type == CPP_ELLIPSIS)
10594 /* Consume the `...' token. */
10595 cp_lexer_consume_token (parser->lexer);
10598 else if (token->type == CPP_CLOSE_PAREN)
10599 /* There are no parameters. */
10601 #ifndef NO_IMPLICIT_EXTERN_C
10602 if (in_system_header && current_class_type == NULL
10603 && current_lang_name == lang_name_c)
10607 return void_list_node;
10609 /* Check for `(void)', too, which is a special case. */
10610 else if (token->keyword == RID_VOID
10611 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
10612 == CPP_CLOSE_PAREN))
10614 /* Consume the `void' token. */
10615 cp_lexer_consume_token (parser->lexer);
10616 /* There are no parameters. */
10617 return void_list_node;
10620 /* Parse the parameter-declaration-list. */
10621 parameters = cp_parser_parameter_declaration_list (parser);
10622 /* If a parse error occurred while parsing the
10623 parameter-declaration-list, then the entire
10624 parameter-declaration-clause is erroneous. */
10625 if (parameters == error_mark_node)
10626 return error_mark_node;
10628 /* Peek at the next token. */
10629 token = cp_lexer_peek_token (parser->lexer);
10630 /* If it's a `,', the clause should terminate with an ellipsis. */
10631 if (token->type == CPP_COMMA)
10633 /* Consume the `,'. */
10634 cp_lexer_consume_token (parser->lexer);
10635 /* Expect an ellipsis. */
10637 = (cp_parser_require (parser, CPP_ELLIPSIS, "`...'") != NULL);
10639 /* It might also be `...' if the optional trailing `,' was
10641 else if (token->type == CPP_ELLIPSIS)
10643 /* Consume the `...' token. */
10644 cp_lexer_consume_token (parser->lexer);
10645 /* And remember that we saw it. */
10649 ellipsis_p = false;
10651 /* Finish the parameter list. */
10652 return finish_parmlist (parameters, ellipsis_p);
10655 /* Parse a parameter-declaration-list.
10657 parameter-declaration-list:
10658 parameter-declaration
10659 parameter-declaration-list , parameter-declaration
10661 Returns a representation of the parameter-declaration-list, as for
10662 cp_parser_parameter_declaration_clause. However, the
10663 `void_list_node' is never appended to the list. */
10666 cp_parser_parameter_declaration_list (parser)
10669 tree parameters = NULL_TREE;
10671 /* Look for more parameters. */
10675 /* Parse the parameter. */
10677 = cp_parser_parameter_declaration (parser,
10678 /*greater_than_is_operator_p=*/true);
10679 /* If a parse error ocurred parsing the parameter declaration,
10680 then the entire parameter-declaration-list is erroneous. */
10681 if (parameter == error_mark_node)
10683 parameters = error_mark_node;
10686 /* Add the new parameter to the list. */
10687 TREE_CHAIN (parameter) = parameters;
10688 parameters = parameter;
10690 /* Peek at the next token. */
10691 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
10692 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10693 /* The parameter-declaration-list is complete. */
10695 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
10699 /* Peek at the next token. */
10700 token = cp_lexer_peek_nth_token (parser->lexer, 2);
10701 /* If it's an ellipsis, then the list is complete. */
10702 if (token->type == CPP_ELLIPSIS)
10704 /* Otherwise, there must be more parameters. Consume the
10706 cp_lexer_consume_token (parser->lexer);
10710 cp_parser_error (parser, "expected `,' or `...'");
10715 /* We built up the list in reverse order; straighten it out now. */
10716 return nreverse (parameters);
10719 /* Parse a parameter declaration.
10721 parameter-declaration:
10722 decl-specifier-seq declarator
10723 decl-specifier-seq declarator = assignment-expression
10724 decl-specifier-seq abstract-declarator [opt]
10725 decl-specifier-seq abstract-declarator [opt] = assignment-expression
10727 If GREATER_THAN_IS_OPERATOR_P is FALSE, then a non-nested `>' token
10728 encountered during the parsing of the assignment-expression is not
10729 interpreted as a greater-than operator.
10731 Returns a TREE_LIST representing the parameter-declaration. The
10732 TREE_VALUE is a representation of the decl-specifier-seq and
10733 declarator. In particular, the TREE_VALUE will be a TREE_LIST
10734 whose TREE_PURPOSE represents the decl-specifier-seq and whose
10735 TREE_VALUE represents the declarator. */
10738 cp_parser_parameter_declaration (parser, greater_than_is_operator_p)
10740 bool greater_than_is_operator_p;
10742 bool declares_class_or_enum;
10743 tree decl_specifiers;
10746 tree default_argument;
10749 const char *saved_message;
10751 /* Type definitions may not appear in parameter types. */
10752 saved_message = parser->type_definition_forbidden_message;
10753 parser->type_definition_forbidden_message
10754 = "types may not be defined in parameter types";
10756 /* Parse the declaration-specifiers. */
10758 = cp_parser_decl_specifier_seq (parser,
10759 CP_PARSER_FLAGS_NONE,
10761 &declares_class_or_enum);
10762 /* If an error occurred, there's no reason to attempt to parse the
10763 rest of the declaration. */
10764 if (cp_parser_error_occurred (parser))
10766 parser->type_definition_forbidden_message = saved_message;
10767 return error_mark_node;
10770 /* Peek at the next token. */
10771 token = cp_lexer_peek_token (parser->lexer);
10772 /* If the next token is a `)', `,', `=', `>', or `...', then there
10773 is no declarator. */
10774 if (token->type == CPP_CLOSE_PAREN
10775 || token->type == CPP_COMMA
10776 || token->type == CPP_EQ
10777 || token->type == CPP_ELLIPSIS
10778 || token->type == CPP_GREATER)
10779 declarator = NULL_TREE;
10780 /* Otherwise, there should be a declarator. */
10783 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
10784 parser->default_arg_ok_p = false;
10786 /* We don't know whether the declarator will be abstract or
10787 not. So, first we try an ordinary declarator. */
10788 cp_parser_parse_tentatively (parser);
10789 declarator = cp_parser_declarator (parser,
10790 /*abstract_p=*/false,
10791 /*ctor_dtor_or_conv_p=*/NULL);
10792 /* If that didn't work, look for an abstract declarator. */
10793 if (!cp_parser_parse_definitely (parser))
10794 declarator = cp_parser_declarator (parser,
10795 /*abstract_p=*/true,
10796 /*ctor_dtor_or_conv_p=*/NULL);
10797 parser->default_arg_ok_p = saved_default_arg_ok_p;
10800 /* The restriction on definining new types applies only to the type
10801 of the parameter, not to the default argument. */
10802 parser->type_definition_forbidden_message = saved_message;
10804 /* If the next token is `=', then process a default argument. */
10805 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10807 bool saved_greater_than_is_operator_p;
10808 /* Consume the `='. */
10809 cp_lexer_consume_token (parser->lexer);
10811 /* If we are defining a class, then the tokens that make up the
10812 default argument must be saved and processed later. */
10813 if (at_class_scope_p () && TYPE_BEING_DEFINED (current_class_type))
10815 unsigned depth = 0;
10817 /* Create a DEFAULT_ARG to represented the unparsed default
10819 default_argument = make_node (DEFAULT_ARG);
10820 DEFARG_TOKENS (default_argument) = cp_token_cache_new ();
10822 /* Add tokens until we have processed the entire default
10829 /* Peek at the next token. */
10830 token = cp_lexer_peek_token (parser->lexer);
10831 /* What we do depends on what token we have. */
10832 switch (token->type)
10834 /* In valid code, a default argument must be
10835 immediately followed by a `,' `)', or `...'. */
10837 case CPP_CLOSE_PAREN:
10839 /* If we run into a non-nested `;', `}', or `]',
10840 then the code is invalid -- but the default
10841 argument is certainly over. */
10842 case CPP_SEMICOLON:
10843 case CPP_CLOSE_BRACE:
10844 case CPP_CLOSE_SQUARE:
10847 /* Update DEPTH, if necessary. */
10848 else if (token->type == CPP_CLOSE_PAREN
10849 || token->type == CPP_CLOSE_BRACE
10850 || token->type == CPP_CLOSE_SQUARE)
10854 case CPP_OPEN_PAREN:
10855 case CPP_OPEN_SQUARE:
10856 case CPP_OPEN_BRACE:
10861 /* If we see a non-nested `>', and `>' is not an
10862 operator, then it marks the end of the default
10864 if (!depth && !greater_than_is_operator_p)
10868 /* If we run out of tokens, issue an error message. */
10870 error ("file ends in default argument");
10876 /* In these cases, we should look for template-ids.
10877 For example, if the default argument is
10878 `X<int, double>()', we need to do name lookup to
10879 figure out whether or not `X' is a template; if
10880 so, the `,' does not end the deault argument.
10882 That is not yet done. */
10889 /* If we've reached the end, stop. */
10893 /* Add the token to the token block. */
10894 token = cp_lexer_consume_token (parser->lexer);
10895 cp_token_cache_push_token (DEFARG_TOKENS (default_argument),
10899 /* Outside of a class definition, we can just parse the
10900 assignment-expression. */
10903 bool saved_local_variables_forbidden_p;
10905 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
10907 saved_greater_than_is_operator_p
10908 = parser->greater_than_is_operator_p;
10909 parser->greater_than_is_operator_p = greater_than_is_operator_p;
10910 /* Local variable names (and the `this' keyword) may not
10911 appear in a default argument. */
10912 saved_local_variables_forbidden_p
10913 = parser->local_variables_forbidden_p;
10914 parser->local_variables_forbidden_p = true;
10915 /* Parse the assignment-expression. */
10916 default_argument = cp_parser_assignment_expression (parser);
10917 /* Restore saved state. */
10918 parser->greater_than_is_operator_p
10919 = saved_greater_than_is_operator_p;
10920 parser->local_variables_forbidden_p
10921 = saved_local_variables_forbidden_p;
10923 if (!parser->default_arg_ok_p)
10925 pedwarn ("default arguments are only permitted on functions");
10926 if (flag_pedantic_errors)
10927 default_argument = NULL_TREE;
10931 default_argument = NULL_TREE;
10933 /* Create the representation of the parameter. */
10935 decl_specifiers = tree_cons (attributes, NULL_TREE, decl_specifiers);
10936 parameter = build_tree_list (default_argument,
10937 build_tree_list (decl_specifiers,
10943 /* Parse a function-definition.
10945 function-definition:
10946 decl-specifier-seq [opt] declarator ctor-initializer [opt]
10948 decl-specifier-seq [opt] declarator function-try-block
10952 function-definition:
10953 __extension__ function-definition
10955 Returns the FUNCTION_DECL for the function. If FRIEND_P is
10956 non-NULL, *FRIEND_P is set to TRUE iff the function was declared to
10960 cp_parser_function_definition (parser, friend_p)
10964 tree decl_specifiers;
10968 tree access_checks;
10970 bool declares_class_or_enum;
10972 /* The saved value of the PEDANTIC flag. */
10973 int saved_pedantic;
10975 /* Any pending qualification must be cleared by our caller. It is
10976 more robust to force the callers to clear PARSER->SCOPE than to
10977 do it here since if the qualification is in effect here, it might
10978 also end up in effect elsewhere that it is not intended. */
10979 my_friendly_assert (!parser->scope, 20010821);
10981 /* Handle `__extension__'. */
10982 if (cp_parser_extension_opt (parser, &saved_pedantic))
10984 /* Parse the function-definition. */
10985 fn = cp_parser_function_definition (parser, friend_p);
10986 /* Restore the PEDANTIC flag. */
10987 pedantic = saved_pedantic;
10992 /* Check to see if this definition appears in a class-specifier. */
10993 member_p = (at_class_scope_p ()
10994 && TYPE_BEING_DEFINED (current_class_type));
10995 /* Defer access checks in the decl-specifier-seq until we know what
10996 function is being defined. There is no need to do this for the
10997 definition of member functions; we cannot be defining a member
10998 from another class. */
11000 cp_parser_start_deferring_access_checks (parser);
11001 /* Parse the decl-specifier-seq. */
11003 = cp_parser_decl_specifier_seq (parser,
11004 CP_PARSER_FLAGS_OPTIONAL,
11006 &declares_class_or_enum);
11007 /* Figure out whether this declaration is a `friend'. */
11009 *friend_p = cp_parser_friend_p (decl_specifiers);
11011 /* Parse the declarator. */
11012 declarator = cp_parser_declarator (parser,
11013 /*abstract_p=*/false,
11014 /*ctor_dtor_or_conv_p=*/NULL);
11016 /* Gather up any access checks that occurred. */
11018 access_checks = cp_parser_stop_deferring_access_checks (parser);
11020 access_checks = NULL_TREE;
11022 /* If something has already gone wrong, we may as well stop now. */
11023 if (declarator == error_mark_node)
11025 /* Skip to the end of the function, or if this wasn't anything
11026 like a function-definition, to a `;' in the hopes of finding
11027 a sensible place from which to continue parsing. */
11028 cp_parser_skip_to_end_of_block_or_statement (parser);
11029 return error_mark_node;
11032 /* The next character should be a `{' (for a simple function
11033 definition), a `:' (for a ctor-initializer), or `try' (for a
11034 function-try block). */
11035 token = cp_lexer_peek_token (parser->lexer);
11036 if (!cp_parser_token_starts_function_definition_p (token))
11038 /* Issue the error-message. */
11039 cp_parser_error (parser, "expected function-definition");
11040 /* Skip to the next `;'. */
11041 cp_parser_skip_to_end_of_block_or_statement (parser);
11043 return error_mark_node;
11046 /* If we are in a class scope, then we must handle
11047 function-definitions specially. In particular, we save away the
11048 tokens that make up the function body, and parse them again
11049 later, in order to handle code like:
11052 int f () { return i; }
11056 Here, we cannot parse the body of `f' until after we have seen
11057 the declaration of `i'. */
11060 cp_token_cache *cache;
11062 /* Create the function-declaration. */
11063 fn = start_method (decl_specifiers, declarator, attributes);
11064 /* If something went badly wrong, bail out now. */
11065 if (fn == error_mark_node)
11067 /* If there's a function-body, skip it. */
11068 if (cp_parser_token_starts_function_definition_p
11069 (cp_lexer_peek_token (parser->lexer)))
11070 cp_parser_skip_to_end_of_block_or_statement (parser);
11071 return error_mark_node;
11074 /* Create a token cache. */
11075 cache = cp_token_cache_new ();
11076 /* Save away the tokens that make up the body of the
11078 cp_parser_cache_group (parser, cache, CPP_CLOSE_BRACE, /*depth=*/0);
11079 /* Handle function try blocks. */
11080 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
11081 cp_parser_cache_group (parser, cache, CPP_CLOSE_BRACE, /*depth=*/0);
11083 /* Save away the inline definition; we will process it when the
11084 class is complete. */
11085 DECL_PENDING_INLINE_INFO (fn) = cache;
11086 DECL_PENDING_INLINE_P (fn) = 1;
11088 /* We're done with the inline definition. */
11089 finish_method (fn);
11091 /* Add FN to the queue of functions to be parsed later. */
11092 TREE_VALUE (parser->unparsed_functions_queues)
11093 = tree_cons (current_class_type, fn,
11094 TREE_VALUE (parser->unparsed_functions_queues));
11099 /* Check that the number of template-parameter-lists is OK. */
11100 if (!cp_parser_check_declarator_template_parameters (parser,
11103 cp_parser_skip_to_end_of_block_or_statement (parser);
11104 return error_mark_node;
11107 return (cp_parser_function_definition_from_specifiers_and_declarator
11108 (parser, decl_specifiers, attributes, declarator, access_checks));
11111 /* Parse a function-body.
11114 compound_statement */
11117 cp_parser_function_body (cp_parser *parser)
11119 cp_parser_compound_statement (parser);
11122 /* Parse a ctor-initializer-opt followed by a function-body. Return
11123 true if a ctor-initializer was present. */
11126 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
11129 bool ctor_initializer_p;
11131 /* Begin the function body. */
11132 body = begin_function_body ();
11133 /* Parse the optional ctor-initializer. */
11134 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
11135 /* Parse the function-body. */
11136 cp_parser_function_body (parser);
11137 /* Finish the function body. */
11138 finish_function_body (body);
11140 return ctor_initializer_p;
11143 /* Parse an initializer.
11146 = initializer-clause
11147 ( expression-list )
11149 Returns a expression representing the initializer. If no
11150 initializer is present, NULL_TREE is returned.
11152 *IS_PARENTHESIZED_INIT is set to TRUE if the `( expression-list )'
11153 production is used, and zero otherwise. *IS_PARENTHESIZED_INIT is
11154 set to FALSE if there is no initializer present. */
11157 cp_parser_initializer (parser, is_parenthesized_init)
11159 bool *is_parenthesized_init;
11164 /* Peek at the next token. */
11165 token = cp_lexer_peek_token (parser->lexer);
11167 /* Let our caller know whether or not this initializer was
11169 *is_parenthesized_init = (token->type == CPP_OPEN_PAREN);
11171 if (token->type == CPP_EQ)
11173 /* Consume the `='. */
11174 cp_lexer_consume_token (parser->lexer);
11175 /* Parse the initializer-clause. */
11176 init = cp_parser_initializer_clause (parser);
11178 else if (token->type == CPP_OPEN_PAREN)
11180 /* Consume the `('. */
11181 cp_lexer_consume_token (parser->lexer);
11182 /* Parse the expression-list. */
11183 init = cp_parser_expression_list (parser);
11184 /* Consume the `)' token. */
11185 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
11186 cp_parser_skip_to_closing_parenthesis (parser);
11190 /* Anything else is an error. */
11191 cp_parser_error (parser, "expected initializer");
11192 init = error_mark_node;
11198 /* Parse an initializer-clause.
11200 initializer-clause:
11201 assignment-expression
11202 { initializer-list , [opt] }
11205 Returns an expression representing the initializer.
11207 If the `assignment-expression' production is used the value
11208 returned is simply a reprsentation for the expression.
11210 Otherwise, a CONSTRUCTOR is returned. The CONSTRUCTOR_ELTS will be
11211 the elements of the initializer-list (or NULL_TREE, if the last
11212 production is used). The TREE_TYPE for the CONSTRUCTOR will be
11213 NULL_TREE. There is no way to detect whether or not the optional
11214 trailing `,' was provided. */
11217 cp_parser_initializer_clause (parser)
11222 /* If it is not a `{', then we are looking at an
11223 assignment-expression. */
11224 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
11225 initializer = cp_parser_assignment_expression (parser);
11228 /* Consume the `{' token. */
11229 cp_lexer_consume_token (parser->lexer);
11230 /* Create a CONSTRUCTOR to represent the braced-initializer. */
11231 initializer = make_node (CONSTRUCTOR);
11232 /* Mark it with TREE_HAS_CONSTRUCTOR. This should not be
11233 necessary, but check_initializer depends upon it, for
11235 TREE_HAS_CONSTRUCTOR (initializer) = 1;
11236 /* If it's not a `}', then there is a non-trivial initializer. */
11237 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
11239 /* Parse the initializer list. */
11240 CONSTRUCTOR_ELTS (initializer)
11241 = cp_parser_initializer_list (parser);
11242 /* A trailing `,' token is allowed. */
11243 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
11244 cp_lexer_consume_token (parser->lexer);
11247 /* Now, there should be a trailing `}'. */
11248 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
11251 return initializer;
11254 /* Parse an initializer-list.
11258 initializer-list , initializer-clause
11263 identifier : initializer-clause
11264 initializer-list, identifier : initializer-clause
11266 Returns a TREE_LIST. The TREE_VALUE of each node is an expression
11267 for the initializer. If the TREE_PURPOSE is non-NULL, it is the
11268 IDENTIFIER_NODE naming the field to initialize. */
11271 cp_parser_initializer_list (parser)
11274 tree initializers = NULL_TREE;
11276 /* Parse the rest of the list. */
11283 /* If the next token is an identifier and the following one is a
11284 colon, we are looking at the GNU designated-initializer
11286 if (cp_parser_allow_gnu_extensions_p (parser)
11287 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
11288 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
11290 /* Consume the identifier. */
11291 identifier = cp_lexer_consume_token (parser->lexer)->value;
11292 /* Consume the `:'. */
11293 cp_lexer_consume_token (parser->lexer);
11296 identifier = NULL_TREE;
11298 /* Parse the initializer. */
11299 initializer = cp_parser_initializer_clause (parser);
11301 /* Add it to the list. */
11302 initializers = tree_cons (identifier, initializer, initializers);
11304 /* If the next token is not a comma, we have reached the end of
11306 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11309 /* Peek at the next token. */
11310 token = cp_lexer_peek_nth_token (parser->lexer, 2);
11311 /* If the next token is a `}', then we're still done. An
11312 initializer-clause can have a trailing `,' after the
11313 initializer-list and before the closing `}'. */
11314 if (token->type == CPP_CLOSE_BRACE)
11317 /* Consume the `,' token. */
11318 cp_lexer_consume_token (parser->lexer);
11321 /* The initializers were built up in reverse order, so we need to
11322 reverse them now. */
11323 return nreverse (initializers);
11326 /* Classes [gram.class] */
11328 /* Parse a class-name.
11334 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
11335 to indicate that names looked up in dependent types should be
11336 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
11337 keyword has been used to indicate that the name that appears next
11338 is a template. TYPE_P is true iff the next name should be treated
11339 as class-name, even if it is declared to be some other kind of name
11340 as well. The accessibility of the class-name is checked iff
11341 CHECK_ACCESS_P is true. If CHECK_DEPENDENCY_P is FALSE, names are
11342 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
11343 is the class being defined in a class-head.
11345 Returns the TYPE_DECL representing the class. */
11348 cp_parser_class_name (cp_parser *parser,
11349 bool typename_keyword_p,
11350 bool template_keyword_p,
11352 bool check_access_p,
11353 bool check_dependency_p,
11361 /* All class-names start with an identifier. */
11362 token = cp_lexer_peek_token (parser->lexer);
11363 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
11365 cp_parser_error (parser, "expected class-name");
11366 return error_mark_node;
11369 /* PARSER->SCOPE can be cleared when parsing the template-arguments
11370 to a template-id, so we save it here. */
11371 scope = parser->scope;
11372 /* Any name names a type if we're following the `typename' keyword
11373 in a qualified name where the enclosing scope is type-dependent. */
11374 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
11375 && cp_parser_dependent_type_p (scope));
11376 /* Handle the common case (an identifier, but not a template-id)
11378 if (token->type == CPP_NAME
11379 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_LESS)
11383 /* Look for the identifier. */
11384 identifier = cp_parser_identifier (parser);
11385 /* If the next token isn't an identifier, we are certainly not
11386 looking at a class-name. */
11387 if (identifier == error_mark_node)
11388 decl = error_mark_node;
11389 /* If we know this is a type-name, there's no need to look it
11391 else if (typename_p)
11395 /* If the next token is a `::', then the name must be a type
11398 [basic.lookup.qual]
11400 During the lookup for a name preceding the :: scope
11401 resolution operator, object, function, and enumerator
11402 names are ignored. */
11403 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
11405 /* Look up the name. */
11406 decl = cp_parser_lookup_name (parser, identifier,
11409 check_dependency_p);
11414 /* Try a template-id. */
11415 decl = cp_parser_template_id (parser, template_keyword_p,
11416 check_dependency_p);
11417 if (decl == error_mark_node)
11418 return error_mark_node;
11421 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
11423 /* If this is a typename, create a TYPENAME_TYPE. */
11424 if (typename_p && decl != error_mark_node)
11425 decl = TYPE_NAME (make_typename_type (scope, decl,
11428 /* Check to see that it is really the name of a class. */
11429 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
11430 && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
11431 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
11432 /* Situations like this:
11434 template <typename T> struct A {
11435 typename T::template X<int>::I i;
11438 are problematic. Is `T::template X<int>' a class-name? The
11439 standard does not seem to be definitive, but there is no other
11440 valid interpretation of the following `::'. Therefore, those
11441 names are considered class-names. */
11442 decl = TYPE_NAME (make_typename_type (scope, decl,
11443 tf_error | tf_parsing));
11444 else if (decl == error_mark_node
11445 || TREE_CODE (decl) != TYPE_DECL
11446 || !IS_AGGR_TYPE (TREE_TYPE (decl)))
11448 cp_parser_error (parser, "expected class-name");
11449 return error_mark_node;
11455 /* Parse a class-specifier.
11458 class-head { member-specification [opt] }
11460 Returns the TREE_TYPE representing the class. */
11463 cp_parser_class_specifier (parser)
11468 tree attributes = NULL_TREE;
11469 int has_trailing_semicolon;
11470 bool nested_name_specifier_p;
11471 bool deferring_access_checks_p;
11472 tree saved_access_checks;
11473 unsigned saved_num_template_parameter_lists;
11475 /* Parse the class-head. */
11476 type = cp_parser_class_head (parser,
11477 &nested_name_specifier_p,
11478 &deferring_access_checks_p,
11479 &saved_access_checks);
11480 /* If the class-head was a semantic disaster, skip the entire body
11484 cp_parser_skip_to_end_of_block_or_statement (parser);
11485 return error_mark_node;
11487 /* Look for the `{'. */
11488 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
11489 return error_mark_node;
11490 /* Issue an error message if type-definitions are forbidden here. */
11491 cp_parser_check_type_definition (parser);
11492 /* Remember that we are defining one more class. */
11493 ++parser->num_classes_being_defined;
11494 /* Inside the class, surrounding template-parameter-lists do not
11496 saved_num_template_parameter_lists
11497 = parser->num_template_parameter_lists;
11498 parser->num_template_parameter_lists = 0;
11499 /* Start the class. */
11500 type = begin_class_definition (type);
11501 if (type == error_mark_node)
11502 /* If the type is erroneous, skip the entire body of the class. */
11503 cp_parser_skip_to_closing_brace (parser);
11505 /* Parse the member-specification. */
11506 cp_parser_member_specification_opt (parser);
11507 /* Look for the trailing `}'. */
11508 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
11509 /* We get better error messages by noticing a common problem: a
11510 missing trailing `;'. */
11511 token = cp_lexer_peek_token (parser->lexer);
11512 has_trailing_semicolon = (token->type == CPP_SEMICOLON);
11513 /* Look for attributes to apply to this class. */
11514 if (cp_parser_allow_gnu_extensions_p (parser))
11515 attributes = cp_parser_attributes_opt (parser);
11516 /* Finish the class definition. */
11517 type = finish_class_definition (type,
11519 has_trailing_semicolon,
11520 nested_name_specifier_p);
11521 /* If this class is not itself within the scope of another class,
11522 then we need to parse the bodies of all of the queued function
11523 definitions. Note that the queued functions defined in a class
11524 are not always processed immediately following the
11525 class-specifier for that class. Consider:
11528 struct B { void f() { sizeof (A); } };
11531 If `f' were processed before the processing of `A' were
11532 completed, there would be no way to compute the size of `A'.
11533 Note that the nesting we are interested in here is lexical --
11534 not the semantic nesting given by TYPE_CONTEXT. In particular,
11537 struct A { struct B; };
11538 struct A::B { void f() { } };
11540 there is no need to delay the parsing of `A::B::f'. */
11541 if (--parser->num_classes_being_defined == 0)
11543 tree last_scope = NULL_TREE;
11545 /* Process non FUNCTION_DECL related DEFAULT_ARGs. */
11546 for (parser->default_arg_types = nreverse (parser->default_arg_types);
11547 parser->default_arg_types;
11548 parser->default_arg_types = TREE_CHAIN (parser->default_arg_types))
11549 cp_parser_late_parsing_default_args
11550 (parser, TREE_PURPOSE (parser->default_arg_types), NULL_TREE);
11552 /* Reverse the queue, so that we process it in the order the
11553 functions were declared. */
11554 TREE_VALUE (parser->unparsed_functions_queues)
11555 = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
11556 /* Loop through all of the functions. */
11557 while (TREE_VALUE (parser->unparsed_functions_queues))
11564 /* Figure out which function we need to process. */
11565 queue_entry = TREE_VALUE (parser->unparsed_functions_queues);
11566 fn_scope = TREE_PURPOSE (queue_entry);
11567 fn = TREE_VALUE (queue_entry);
11569 /* Parse the function. */
11570 cp_parser_late_parsing_for_member (parser, fn);
11572 TREE_VALUE (parser->unparsed_functions_queues)
11573 = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues));
11576 /* If LAST_SCOPE is non-NULL, then we have pushed scopes one
11577 more time than we have popped, so me must pop here. */
11579 pop_scope (last_scope);
11582 /* Put back any saved access checks. */
11583 if (deferring_access_checks_p)
11585 cp_parser_start_deferring_access_checks (parser);
11586 parser->context->deferred_access_checks = saved_access_checks;
11589 /* Restore the count of active template-parameter-lists. */
11590 parser->num_template_parameter_lists
11591 = saved_num_template_parameter_lists;
11596 /* Parse a class-head.
11599 class-key identifier [opt] base-clause [opt]
11600 class-key nested-name-specifier identifier base-clause [opt]
11601 class-key nested-name-specifier [opt] template-id
11605 class-key attributes identifier [opt] base-clause [opt]
11606 class-key attributes nested-name-specifier identifier base-clause [opt]
11607 class-key attributes nested-name-specifier [opt] template-id
11610 Returns the TYPE of the indicated class. Sets
11611 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
11612 involving a nested-name-specifier was used, and FALSE otherwise.
11613 Sets *DEFERRING_ACCESS_CHECKS_P to TRUE iff we were deferring
11614 access checks before this class-head. In that case,
11615 *SAVED_ACCESS_CHECKS is set to the current list of deferred access
11618 Returns NULL_TREE if the class-head is syntactically valid, but
11619 semantically invalid in a way that means we should skip the entire
11620 body of the class. */
11623 cp_parser_class_head (parser,
11624 nested_name_specifier_p,
11625 deferring_access_checks_p,
11626 saved_access_checks)
11628 bool *nested_name_specifier_p;
11629 bool *deferring_access_checks_p;
11630 tree *saved_access_checks;
11633 tree nested_name_specifier;
11634 enum tag_types class_key;
11635 tree id = NULL_TREE;
11636 tree type = NULL_TREE;
11638 bool template_id_p = false;
11639 bool qualified_p = false;
11640 bool invalid_nested_name_p = false;
11641 unsigned num_templates;
11643 /* Assume no nested-name-specifier will be present. */
11644 *nested_name_specifier_p = false;
11645 /* Assume no template parameter lists will be used in defining the
11649 /* Look for the class-key. */
11650 class_key = cp_parser_class_key (parser);
11651 if (class_key == none_type)
11652 return error_mark_node;
11654 /* Parse the attributes. */
11655 attributes = cp_parser_attributes_opt (parser);
11657 /* If the next token is `::', that is invalid -- but sometimes
11658 people do try to write:
11662 Handle this gracefully by accepting the extra qualifier, and then
11663 issuing an error about it later if this really is a
11664 class-header. If it turns out just to be an elaborated type
11665 specifier, remain silent. */
11666 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
11667 qualified_p = true;
11669 /* Determine the name of the class. Begin by looking for an
11670 optional nested-name-specifier. */
11671 nested_name_specifier
11672 = cp_parser_nested_name_specifier_opt (parser,
11673 /*typename_keyword_p=*/false,
11674 /*check_dependency_p=*/true,
11676 /* If there was a nested-name-specifier, then there *must* be an
11678 if (nested_name_specifier)
11680 /* Although the grammar says `identifier', it really means
11681 `class-name' or `template-name'. You are only allowed to
11682 define a class that has already been declared with this
11685 The proposed resolution for Core Issue 180 says that whever
11686 you see `class T::X' you should treat `X' as a type-name.
11688 It is OK to define an inaccessible class; for example:
11690 class A { class B; };
11693 So, we ask cp_parser_class_name not to check accessibility.
11695 We do not know if we will see a class-name, or a
11696 template-name. We look for a class-name first, in case the
11697 class-name is a template-id; if we looked for the
11698 template-name first we would stop after the template-name. */
11699 cp_parser_parse_tentatively (parser);
11700 type = cp_parser_class_name (parser,
11701 /*typename_keyword_p=*/false,
11702 /*template_keyword_p=*/false,
11704 /*check_access_p=*/false,
11705 /*check_dependency_p=*/false,
11706 /*class_head_p=*/true);
11707 /* If that didn't work, ignore the nested-name-specifier. */
11708 if (!cp_parser_parse_definitely (parser))
11710 invalid_nested_name_p = true;
11711 id = cp_parser_identifier (parser);
11712 if (id == error_mark_node)
11715 /* If we could not find a corresponding TYPE, treat this
11716 declaration like an unqualified declaration. */
11717 if (type == error_mark_node)
11718 nested_name_specifier = NULL_TREE;
11719 /* Otherwise, count the number of templates used in TYPE and its
11720 containing scopes. */
11725 for (scope = TREE_TYPE (type);
11726 scope && TREE_CODE (scope) != NAMESPACE_DECL;
11727 scope = (TYPE_P (scope)
11728 ? TYPE_CONTEXT (scope)
11729 : DECL_CONTEXT (scope)))
11731 && CLASS_TYPE_P (scope)
11732 && CLASSTYPE_TEMPLATE_INFO (scope)
11733 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
11737 /* Otherwise, the identifier is optional. */
11740 /* We don't know whether what comes next is a template-id,
11741 an identifier, or nothing at all. */
11742 cp_parser_parse_tentatively (parser);
11743 /* Check for a template-id. */
11744 id = cp_parser_template_id (parser,
11745 /*template_keyword_p=*/false,
11746 /*check_dependency_p=*/true);
11747 /* If that didn't work, it could still be an identifier. */
11748 if (!cp_parser_parse_definitely (parser))
11750 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
11751 id = cp_parser_identifier (parser);
11757 template_id_p = true;
11762 /* If it's not a `:' or a `{' then we can't really be looking at a
11763 class-head, since a class-head only appears as part of a
11764 class-specifier. We have to detect this situation before calling
11765 xref_tag, since that has irreversible side-effects. */
11766 if (!cp_parser_next_token_starts_class_definition_p (parser))
11768 cp_parser_error (parser, "expected `{' or `:'");
11769 return error_mark_node;
11772 /* At this point, we're going ahead with the class-specifier, even
11773 if some other problem occurs. */
11774 cp_parser_commit_to_tentative_parse (parser);
11775 /* Issue the error about the overly-qualified name now. */
11777 cp_parser_error (parser,
11778 "global qualification of class name is invalid");
11779 else if (invalid_nested_name_p)
11780 cp_parser_error (parser,
11781 "qualified name does not name a class");
11782 /* Make sure that the right number of template parameters were
11784 if (!cp_parser_check_template_parameters (parser, num_templates))
11785 /* If something went wrong, there is no point in even trying to
11786 process the class-definition. */
11789 /* We do not need to defer access checks for entities declared
11790 within the class. But, we do need to save any access checks that
11791 are currently deferred and restore them later, in case we are in
11792 the middle of something else. */
11793 *deferring_access_checks_p = parser->context->deferring_access_checks_p;
11794 if (*deferring_access_checks_p)
11795 *saved_access_checks = cp_parser_stop_deferring_access_checks (parser);
11797 /* Look up the type. */
11800 type = TREE_TYPE (id);
11801 maybe_process_partial_specialization (type);
11803 else if (!nested_name_specifier)
11805 /* If the class was unnamed, create a dummy name. */
11807 id = make_anon_name ();
11808 type = xref_tag (class_key, id, attributes, /*globalize=*/0);
11817 template <typename T> struct S { struct T };
11818 template <typename T> struct S::T { };
11820 we will get a TYPENAME_TYPE when processing the definition of
11821 `S::T'. We need to resolve it to the actual type before we
11822 try to define it. */
11823 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
11825 type = cp_parser_resolve_typename_type (parser, TREE_TYPE (type));
11826 if (type != error_mark_node)
11827 type = TYPE_NAME (type);
11830 maybe_process_partial_specialization (TREE_TYPE (type));
11831 class_type = current_class_type;
11832 type = TREE_TYPE (handle_class_head (class_key,
11833 nested_name_specifier,
11838 if (type != error_mark_node)
11840 if (!class_type && TYPE_CONTEXT (type))
11841 *nested_name_specifier_p = true;
11842 else if (class_type && !same_type_p (TYPE_CONTEXT (type),
11844 *nested_name_specifier_p = true;
11847 /* Indicate whether this class was declared as a `class' or as a
11849 if (TREE_CODE (type) == RECORD_TYPE)
11850 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
11851 cp_parser_check_class_key (class_key, type);
11853 /* Enter the scope containing the class; the names of base classes
11854 should be looked up in that context. For example, given:
11856 struct A { struct B {}; struct C; };
11857 struct A::C : B {};
11860 if (nested_name_specifier)
11861 push_scope (nested_name_specifier);
11862 /* Now, look for the base-clause. */
11863 token = cp_lexer_peek_token (parser->lexer);
11864 if (token->type == CPP_COLON)
11868 /* Get the list of base-classes. */
11869 bases = cp_parser_base_clause (parser);
11870 /* Process them. */
11871 xref_basetypes (type, bases);
11873 /* Leave the scope given by the nested-name-specifier. We will
11874 enter the class scope itself while processing the members. */
11875 if (nested_name_specifier)
11876 pop_scope (nested_name_specifier);
11881 /* Parse a class-key.
11888 Returns the kind of class-key specified, or none_type to indicate
11891 static enum tag_types
11892 cp_parser_class_key (parser)
11896 enum tag_types tag_type;
11898 /* Look for the class-key. */
11899 token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
11903 /* Check to see if the TOKEN is a class-key. */
11904 tag_type = cp_parser_token_is_class_key (token);
11906 cp_parser_error (parser, "expected class-key");
11910 /* Parse an (optional) member-specification.
11912 member-specification:
11913 member-declaration member-specification [opt]
11914 access-specifier : member-specification [opt] */
11917 cp_parser_member_specification_opt (parser)
11925 /* Peek at the next token. */
11926 token = cp_lexer_peek_token (parser->lexer);
11927 /* If it's a `}', or EOF then we've seen all the members. */
11928 if (token->type == CPP_CLOSE_BRACE || token->type == CPP_EOF)
11931 /* See if this token is a keyword. */
11932 keyword = token->keyword;
11936 case RID_PROTECTED:
11938 /* Consume the access-specifier. */
11939 cp_lexer_consume_token (parser->lexer);
11940 /* Remember which access-specifier is active. */
11941 current_access_specifier = token->value;
11942 /* Look for the `:'. */
11943 cp_parser_require (parser, CPP_COLON, "`:'");
11947 /* Otherwise, the next construction must be a
11948 member-declaration. */
11949 cp_parser_member_declaration (parser);
11950 reset_type_access_control ();
11955 /* Parse a member-declaration.
11957 member-declaration:
11958 decl-specifier-seq [opt] member-declarator-list [opt] ;
11959 function-definition ; [opt]
11960 :: [opt] nested-name-specifier template [opt] unqualified-id ;
11962 template-declaration
11964 member-declarator-list:
11966 member-declarator-list , member-declarator
11969 declarator pure-specifier [opt]
11970 declarator constant-initializer [opt]
11971 identifier [opt] : constant-expression
11975 member-declaration:
11976 __extension__ member-declaration
11979 declarator attributes [opt] pure-specifier [opt]
11980 declarator attributes [opt] constant-initializer [opt]
11981 identifier [opt] attributes [opt] : constant-expression */
11984 cp_parser_member_declaration (parser)
11987 tree decl_specifiers;
11988 tree prefix_attributes;
11990 bool declares_class_or_enum;
11993 int saved_pedantic;
11995 /* Check for the `__extension__' keyword. */
11996 if (cp_parser_extension_opt (parser, &saved_pedantic))
11999 cp_parser_member_declaration (parser);
12000 /* Restore the old value of the PEDANTIC flag. */
12001 pedantic = saved_pedantic;
12006 /* Check for a template-declaration. */
12007 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
12009 /* Parse the template-declaration. */
12010 cp_parser_template_declaration (parser, /*member_p=*/true);
12015 /* Check for a using-declaration. */
12016 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
12018 /* Parse the using-declaration. */
12019 cp_parser_using_declaration (parser);
12024 /* We can't tell whether we're looking at a declaration or a
12025 function-definition. */
12026 cp_parser_parse_tentatively (parser);
12028 /* Parse the decl-specifier-seq. */
12030 = cp_parser_decl_specifier_seq (parser,
12031 CP_PARSER_FLAGS_OPTIONAL,
12032 &prefix_attributes,
12033 &declares_class_or_enum);
12034 /* If there is no declarator, then the decl-specifier-seq should
12036 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12038 /* If there was no decl-specifier-seq, and the next token is a
12039 `;', then we have something like:
12045 Each member-declaration shall declare at least one member
12046 name of the class. */
12047 if (!decl_specifiers)
12050 pedwarn ("extra semicolon");
12056 /* See if this declaration is a friend. */
12057 friend_p = cp_parser_friend_p (decl_specifiers);
12058 /* If there were decl-specifiers, check to see if there was
12059 a class-declaration. */
12060 type = check_tag_decl (decl_specifiers);
12061 /* Nested classes have already been added to the class, but
12062 a `friend' needs to be explicitly registered. */
12065 /* If the `friend' keyword was present, the friend must
12066 be introduced with a class-key. */
12067 if (!declares_class_or_enum)
12068 error ("a class-key must be used when declaring a friend");
12071 template <typename T> struct A {
12072 friend struct A<T>::B;
12075 A<T>::B will be represented by a TYPENAME_TYPE, and
12076 therefore not recognized by check_tag_decl. */
12081 for (specifier = decl_specifiers;
12083 specifier = TREE_CHAIN (specifier))
12085 tree s = TREE_VALUE (specifier);
12087 if (TREE_CODE (s) == IDENTIFIER_NODE
12088 && IDENTIFIER_GLOBAL_VALUE (s))
12089 type = IDENTIFIER_GLOBAL_VALUE (s);
12090 if (TREE_CODE (s) == TYPE_DECL)
12100 error ("friend declaration does not name a class or "
12103 make_friend_class (current_class_type, type);
12105 /* If there is no TYPE, an error message will already have
12109 /* An anonymous aggregate has to be handled specially; such
12110 a declaration really declares a data member (with a
12111 particular type), as opposed to a nested class. */
12112 else if (ANON_AGGR_TYPE_P (type))
12114 /* Remove constructors and such from TYPE, now that we
12115 know it is an anoymous aggregate. */
12116 fixup_anonymous_aggr (type);
12117 /* And make the corresponding data member. */
12118 decl = build_decl (FIELD_DECL, NULL_TREE, type);
12119 /* Add it to the class. */
12120 finish_member_declaration (decl);
12126 /* See if these declarations will be friends. */
12127 friend_p = cp_parser_friend_p (decl_specifiers);
12129 /* Keep going until we hit the `;' at the end of the
12131 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12133 tree attributes = NULL_TREE;
12134 tree first_attribute;
12136 /* Peek at the next token. */
12137 token = cp_lexer_peek_token (parser->lexer);
12139 /* Check for a bitfield declaration. */
12140 if (token->type == CPP_COLON
12141 || (token->type == CPP_NAME
12142 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
12148 /* Get the name of the bitfield. Note that we cannot just
12149 check TOKEN here because it may have been invalidated by
12150 the call to cp_lexer_peek_nth_token above. */
12151 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
12152 identifier = cp_parser_identifier (parser);
12154 identifier = NULL_TREE;
12156 /* Consume the `:' token. */
12157 cp_lexer_consume_token (parser->lexer);
12158 /* Get the width of the bitfield. */
12159 width = cp_parser_constant_expression (parser);
12161 /* Look for attributes that apply to the bitfield. */
12162 attributes = cp_parser_attributes_opt (parser);
12163 /* Remember which attributes are prefix attributes and
12165 first_attribute = attributes;
12166 /* Combine the attributes. */
12167 attributes = chainon (prefix_attributes, attributes);
12169 /* Create the bitfield declaration. */
12170 decl = grokbitfield (identifier,
12173 /* Apply the attributes. */
12174 cplus_decl_attributes (&decl, attributes, /*flags=*/0);
12180 tree asm_specification;
12181 bool ctor_dtor_or_conv_p;
12183 /* Parse the declarator. */
12185 = cp_parser_declarator (parser,
12186 /*abstract_p=*/false,
12187 &ctor_dtor_or_conv_p);
12189 /* If something went wrong parsing the declarator, make sure
12190 that we at least consume some tokens. */
12191 if (declarator == error_mark_node)
12193 /* Skip to the end of the statement. */
12194 cp_parser_skip_to_end_of_statement (parser);
12198 /* Look for an asm-specification. */
12199 asm_specification = cp_parser_asm_specification_opt (parser);
12200 /* Look for attributes that apply to the declaration. */
12201 attributes = cp_parser_attributes_opt (parser);
12202 /* Remember which attributes are prefix attributes and
12204 first_attribute = attributes;
12205 /* Combine the attributes. */
12206 attributes = chainon (prefix_attributes, attributes);
12208 /* If it's an `=', then we have a constant-initializer or a
12209 pure-specifier. It is not correct to parse the
12210 initializer before registering the member declaration
12211 since the member declaration should be in scope while
12212 its initializer is processed. However, the rest of the
12213 front end does not yet provide an interface that allows
12214 us to handle this correctly. */
12215 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12219 A pure-specifier shall be used only in the declaration of
12220 a virtual function.
12222 A member-declarator can contain a constant-initializer
12223 only if it declares a static member of integral or
12226 Therefore, if the DECLARATOR is for a function, we look
12227 for a pure-specifier; otherwise, we look for a
12228 constant-initializer. When we call `grokfield', it will
12229 perform more stringent semantics checks. */
12230 if (TREE_CODE (declarator) == CALL_EXPR)
12231 initializer = cp_parser_pure_specifier (parser);
12234 /* This declaration cannot be a function
12236 cp_parser_commit_to_tentative_parse (parser);
12237 /* Parse the initializer. */
12238 initializer = cp_parser_constant_initializer (parser);
12241 /* Otherwise, there is no initializer. */
12243 initializer = NULL_TREE;
12245 /* See if we are probably looking at a function
12246 definition. We are certainly not looking at at a
12247 member-declarator. Calling `grokfield' has
12248 side-effects, so we must not do it unless we are sure
12249 that we are looking at a member-declarator. */
12250 if (cp_parser_token_starts_function_definition_p
12251 (cp_lexer_peek_token (parser->lexer)))
12252 decl = error_mark_node;
12254 /* Create the declaration. */
12255 decl = grokfield (declarator,
12262 /* Reset PREFIX_ATTRIBUTES. */
12263 while (attributes && TREE_CHAIN (attributes) != first_attribute)
12264 attributes = TREE_CHAIN (attributes);
12266 TREE_CHAIN (attributes) = NULL_TREE;
12268 /* If there is any qualification still in effect, clear it
12269 now; we will be starting fresh with the next declarator. */
12270 parser->scope = NULL_TREE;
12271 parser->qualifying_scope = NULL_TREE;
12272 parser->object_scope = NULL_TREE;
12273 /* If it's a `,', then there are more declarators. */
12274 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
12275 cp_lexer_consume_token (parser->lexer);
12276 /* If the next token isn't a `;', then we have a parse error. */
12277 else if (cp_lexer_next_token_is_not (parser->lexer,
12280 cp_parser_error (parser, "expected `;'");
12281 /* Skip tokens until we find a `;' */
12282 cp_parser_skip_to_end_of_statement (parser);
12289 /* Add DECL to the list of members. */
12291 finish_member_declaration (decl);
12293 /* If DECL is a function, we must return
12294 to parse it later. (Even though there is no definition,
12295 there might be default arguments that need handling.) */
12296 if (TREE_CODE (decl) == FUNCTION_DECL)
12297 TREE_VALUE (parser->unparsed_functions_queues)
12298 = tree_cons (current_class_type, decl,
12299 TREE_VALUE (parser->unparsed_functions_queues));
12304 /* If everything went well, look for the `;'. */
12305 if (cp_parser_parse_definitely (parser))
12307 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
12311 /* Parse the function-definition. */
12312 decl = cp_parser_function_definition (parser, &friend_p);
12313 /* If the member was not a friend, declare it here. */
12315 finish_member_declaration (decl);
12316 /* Peek at the next token. */
12317 token = cp_lexer_peek_token (parser->lexer);
12318 /* If the next token is a semicolon, consume it. */
12319 if (token->type == CPP_SEMICOLON)
12320 cp_lexer_consume_token (parser->lexer);
12323 /* Parse a pure-specifier.
12328 Returns INTEGER_ZERO_NODE if a pure specifier is found.
12329 Otherwiser, ERROR_MARK_NODE is returned. */
12332 cp_parser_pure_specifier (parser)
12337 /* Look for the `=' token. */
12338 if (!cp_parser_require (parser, CPP_EQ, "`='"))
12339 return error_mark_node;
12340 /* Look for the `0' token. */
12341 token = cp_parser_require (parser, CPP_NUMBER, "`0'");
12342 /* Unfortunately, this will accept `0L' and `0x00' as well. We need
12343 to get information from the lexer about how the number was
12344 spelled in order to fix this problem. */
12345 if (!token || !integer_zerop (token->value))
12346 return error_mark_node;
12348 return integer_zero_node;
12351 /* Parse a constant-initializer.
12353 constant-initializer:
12354 = constant-expression
12356 Returns a representation of the constant-expression. */
12359 cp_parser_constant_initializer (parser)
12362 /* Look for the `=' token. */
12363 if (!cp_parser_require (parser, CPP_EQ, "`='"))
12364 return error_mark_node;
12366 /* It is invalid to write:
12368 struct S { static const int i = { 7 }; };
12371 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12373 cp_parser_error (parser,
12374 "a brace-enclosed initializer is not allowed here");
12375 /* Consume the opening brace. */
12376 cp_lexer_consume_token (parser->lexer);
12377 /* Skip the initializer. */
12378 cp_parser_skip_to_closing_brace (parser);
12379 /* Look for the trailing `}'. */
12380 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
12382 return error_mark_node;
12385 return cp_parser_constant_expression (parser);
12388 /* Derived classes [gram.class.derived] */
12390 /* Parse a base-clause.
12393 : base-specifier-list
12395 base-specifier-list:
12397 base-specifier-list , base-specifier
12399 Returns a TREE_LIST representing the base-classes, in the order in
12400 which they were declared. The representation of each node is as
12401 described by cp_parser_base_specifier.
12403 In the case that no bases are specified, this function will return
12404 NULL_TREE, not ERROR_MARK_NODE. */
12407 cp_parser_base_clause (parser)
12410 tree bases = NULL_TREE;
12412 /* Look for the `:' that begins the list. */
12413 cp_parser_require (parser, CPP_COLON, "`:'");
12415 /* Scan the base-specifier-list. */
12421 /* Look for the base-specifier. */
12422 base = cp_parser_base_specifier (parser);
12423 /* Add BASE to the front of the list. */
12424 if (base != error_mark_node)
12426 TREE_CHAIN (base) = bases;
12429 /* Peek at the next token. */
12430 token = cp_lexer_peek_token (parser->lexer);
12431 /* If it's not a comma, then the list is complete. */
12432 if (token->type != CPP_COMMA)
12434 /* Consume the `,'. */
12435 cp_lexer_consume_token (parser->lexer);
12438 /* PARSER->SCOPE may still be non-NULL at this point, if the last
12439 base class had a qualified name. However, the next name that
12440 appears is certainly not qualified. */
12441 parser->scope = NULL_TREE;
12442 parser->qualifying_scope = NULL_TREE;
12443 parser->object_scope = NULL_TREE;
12445 return nreverse (bases);
12448 /* Parse a base-specifier.
12451 :: [opt] nested-name-specifier [opt] class-name
12452 virtual access-specifier [opt] :: [opt] nested-name-specifier
12454 access-specifier virtual [opt] :: [opt] nested-name-specifier
12457 Returns a TREE_LIST. The TREE_PURPOSE will be one of
12458 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
12459 indicate the specifiers provided. The TREE_VALUE will be a TYPE
12460 (or the ERROR_MARK_NODE) indicating the type that was specified. */
12463 cp_parser_base_specifier (parser)
12468 bool virtual_p = false;
12469 bool duplicate_virtual_error_issued_p = false;
12470 bool duplicate_access_error_issued_p = false;
12471 bool class_scope_p;
12472 access_kind access = ak_none;
12476 /* Process the optional `virtual' and `access-specifier'. */
12479 /* Peek at the next token. */
12480 token = cp_lexer_peek_token (parser->lexer);
12481 /* Process `virtual'. */
12482 switch (token->keyword)
12485 /* If `virtual' appears more than once, issue an error. */
12486 if (virtual_p && !duplicate_virtual_error_issued_p)
12488 cp_parser_error (parser,
12489 "`virtual' specified more than once in base-specified");
12490 duplicate_virtual_error_issued_p = true;
12495 /* Consume the `virtual' token. */
12496 cp_lexer_consume_token (parser->lexer);
12501 case RID_PROTECTED:
12503 /* If more than one access specifier appears, issue an
12505 if (access != ak_none && !duplicate_access_error_issued_p)
12507 cp_parser_error (parser,
12508 "more than one access specifier in base-specified");
12509 duplicate_access_error_issued_p = true;
12512 access = ((access_kind)
12513 tree_low_cst (ridpointers[(int) token->keyword],
12516 /* Consume the access-specifier. */
12517 cp_lexer_consume_token (parser->lexer);
12527 /* Map `virtual_p' and `access' onto one of the access
12533 access_node = access_default_node;
12536 access_node = access_public_node;
12539 access_node = access_protected_node;
12542 access_node = access_private_node;
12551 access_node = access_default_virtual_node;
12554 access_node = access_public_virtual_node;
12557 access_node = access_protected_virtual_node;
12560 access_node = access_private_virtual_node;
12566 /* Look for the optional `::' operator. */
12567 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
12568 /* Look for the nested-name-specifier. The simplest way to
12573 The keyword `typename' is not permitted in a base-specifier or
12574 mem-initializer; in these contexts a qualified name that
12575 depends on a template-parameter is implicitly assumed to be a
12578 is to pretend that we have seen the `typename' keyword at this
12580 cp_parser_nested_name_specifier_opt (parser,
12581 /*typename_keyword_p=*/true,
12582 /*check_dependency_p=*/true,
12584 /* If the base class is given by a qualified name, assume that names
12585 we see are type names or templates, as appropriate. */
12586 class_scope_p = (parser->scope && TYPE_P (parser->scope));
12587 /* Finally, look for the class-name. */
12588 type = cp_parser_class_name (parser,
12592 /*check_access=*/true,
12593 /*check_dependency_p=*/true,
12594 /*class_head_p=*/false);
12596 if (type == error_mark_node)
12597 return error_mark_node;
12599 return finish_base_specifier (access_node, TREE_TYPE (type));
12602 /* Exception handling [gram.exception] */
12604 /* Parse an (optional) exception-specification.
12606 exception-specification:
12607 throw ( type-id-list [opt] )
12609 Returns a TREE_LIST representing the exception-specification. The
12610 TREE_VALUE of each node is a type. */
12613 cp_parser_exception_specification_opt (parser)
12619 /* Peek at the next token. */
12620 token = cp_lexer_peek_token (parser->lexer);
12621 /* If it's not `throw', then there's no exception-specification. */
12622 if (!cp_parser_is_keyword (token, RID_THROW))
12625 /* Consume the `throw'. */
12626 cp_lexer_consume_token (parser->lexer);
12628 /* Look for the `('. */
12629 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
12631 /* Peek at the next token. */
12632 token = cp_lexer_peek_token (parser->lexer);
12633 /* If it's not a `)', then there is a type-id-list. */
12634 if (token->type != CPP_CLOSE_PAREN)
12636 const char *saved_message;
12638 /* Types may not be defined in an exception-specification. */
12639 saved_message = parser->type_definition_forbidden_message;
12640 parser->type_definition_forbidden_message
12641 = "types may not be defined in an exception-specification";
12642 /* Parse the type-id-list. */
12643 type_id_list = cp_parser_type_id_list (parser);
12644 /* Restore the saved message. */
12645 parser->type_definition_forbidden_message = saved_message;
12648 type_id_list = empty_except_spec;
12650 /* Look for the `)'. */
12651 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
12653 return type_id_list;
12656 /* Parse an (optional) type-id-list.
12660 type-id-list , type-id
12662 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
12663 in the order that the types were presented. */
12666 cp_parser_type_id_list (parser)
12669 tree types = NULL_TREE;
12676 /* Get the next type-id. */
12677 type = cp_parser_type_id (parser);
12678 /* Add it to the list. */
12679 types = add_exception_specifier (types, type, /*complain=*/1);
12680 /* Peek at the next token. */
12681 token = cp_lexer_peek_token (parser->lexer);
12682 /* If it is not a `,', we are done. */
12683 if (token->type != CPP_COMMA)
12685 /* Consume the `,'. */
12686 cp_lexer_consume_token (parser->lexer);
12689 return nreverse (types);
12692 /* Parse a try-block.
12695 try compound-statement handler-seq */
12698 cp_parser_try_block (parser)
12703 cp_parser_require_keyword (parser, RID_TRY, "`try'");
12704 try_block = begin_try_block ();
12705 cp_parser_compound_statement (parser);
12706 finish_try_block (try_block);
12707 cp_parser_handler_seq (parser);
12708 finish_handler_sequence (try_block);
12713 /* Parse a function-try-block.
12715 function-try-block:
12716 try ctor-initializer [opt] function-body handler-seq */
12719 cp_parser_function_try_block (parser)
12723 bool ctor_initializer_p;
12725 /* Look for the `try' keyword. */
12726 if (!cp_parser_require_keyword (parser, RID_TRY, "`try'"))
12728 /* Let the rest of the front-end know where we are. */
12729 try_block = begin_function_try_block ();
12730 /* Parse the function-body. */
12732 = cp_parser_ctor_initializer_opt_and_function_body (parser);
12733 /* We're done with the `try' part. */
12734 finish_function_try_block (try_block);
12735 /* Parse the handlers. */
12736 cp_parser_handler_seq (parser);
12737 /* We're done with the handlers. */
12738 finish_function_handler_sequence (try_block);
12740 return ctor_initializer_p;
12743 /* Parse a handler-seq.
12746 handler handler-seq [opt] */
12749 cp_parser_handler_seq (parser)
12756 /* Parse the handler. */
12757 cp_parser_handler (parser);
12758 /* Peek at the next token. */
12759 token = cp_lexer_peek_token (parser->lexer);
12760 /* If it's not `catch' then there are no more handlers. */
12761 if (!cp_parser_is_keyword (token, RID_CATCH))
12766 /* Parse a handler.
12769 catch ( exception-declaration ) compound-statement */
12772 cp_parser_handler (parser)
12778 cp_parser_require_keyword (parser, RID_CATCH, "`catch'");
12779 handler = begin_handler ();
12780 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
12781 declaration = cp_parser_exception_declaration (parser);
12782 finish_handler_parms (declaration, handler);
12783 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
12784 cp_parser_compound_statement (parser);
12785 finish_handler (handler);
12788 /* Parse an exception-declaration.
12790 exception-declaration:
12791 type-specifier-seq declarator
12792 type-specifier-seq abstract-declarator
12796 Returns a VAR_DECL for the declaration, or NULL_TREE if the
12797 ellipsis variant is used. */
12800 cp_parser_exception_declaration (parser)
12803 tree type_specifiers;
12805 const char *saved_message;
12807 /* If it's an ellipsis, it's easy to handle. */
12808 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
12810 /* Consume the `...' token. */
12811 cp_lexer_consume_token (parser->lexer);
12815 /* Types may not be defined in exception-declarations. */
12816 saved_message = parser->type_definition_forbidden_message;
12817 parser->type_definition_forbidden_message
12818 = "types may not be defined in exception-declarations";
12820 /* Parse the type-specifier-seq. */
12821 type_specifiers = cp_parser_type_specifier_seq (parser);
12822 /* If it's a `)', then there is no declarator. */
12823 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
12824 declarator = NULL_TREE;
12827 /* Otherwise, we can't be sure whether we are looking at a
12828 direct, or an abstract, declarator. */
12829 cp_parser_parse_tentatively (parser);
12830 /* Try an ordinary declarator. */
12831 declarator = cp_parser_declarator (parser,
12832 /*abstract_p=*/false,
12833 /*ctor_dtor_or_conv_p=*/NULL);
12834 /* If that didn't work, try an abstract declarator. */
12835 if (!cp_parser_parse_definitely (parser))
12836 declarator = cp_parser_declarator (parser,
12837 /*abstract_p=*/true,
12838 /*ctor_dtor_or_conv_p=*/NULL);
12841 /* Restore the saved message. */
12842 parser->type_definition_forbidden_message = saved_message;
12844 return start_handler_parms (type_specifiers, declarator);
12847 /* Parse a throw-expression.
12850 throw assignment-expresion [opt]
12852 Returns a THROW_EXPR representing the throw-expression. */
12855 cp_parser_throw_expression (parser)
12860 cp_parser_require_keyword (parser, RID_THROW, "`throw'");
12861 /* We can't be sure if there is an assignment-expression or not. */
12862 cp_parser_parse_tentatively (parser);
12864 expression = cp_parser_assignment_expression (parser);
12865 /* If it didn't work, this is just a rethrow. */
12866 if (!cp_parser_parse_definitely (parser))
12867 expression = NULL_TREE;
12869 return build_throw (expression);
12872 /* GNU Extensions */
12874 /* Parse an (optional) asm-specification.
12877 asm ( string-literal )
12879 If the asm-specification is present, returns a STRING_CST
12880 corresponding to the string-literal. Otherwise, returns
12884 cp_parser_asm_specification_opt (parser)
12888 tree asm_specification;
12890 /* Peek at the next token. */
12891 token = cp_lexer_peek_token (parser->lexer);
12892 /* If the next token isn't the `asm' keyword, then there's no
12893 asm-specification. */
12894 if (!cp_parser_is_keyword (token, RID_ASM))
12897 /* Consume the `asm' token. */
12898 cp_lexer_consume_token (parser->lexer);
12899 /* Look for the `('. */
12900 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
12902 /* Look for the string-literal. */
12903 token = cp_parser_require (parser, CPP_STRING, "string-literal");
12905 asm_specification = token->value;
12907 asm_specification = NULL_TREE;
12909 /* Look for the `)'. */
12910 cp_parser_require (parser, CPP_CLOSE_PAREN, "`('");
12912 return asm_specification;
12915 /* Parse an asm-operand-list.
12919 asm-operand-list , asm-operand
12922 string-literal ( expression )
12923 [ string-literal ] string-literal ( expression )
12925 Returns a TREE_LIST representing the operands. The TREE_VALUE of
12926 each node is the expression. The TREE_PURPOSE is itself a
12927 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
12928 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
12929 is a STRING_CST for the string literal before the parenthesis. */
12932 cp_parser_asm_operand_list (parser)
12935 tree asm_operands = NULL_TREE;
12939 tree string_literal;
12944 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
12946 /* Consume the `[' token. */
12947 cp_lexer_consume_token (parser->lexer);
12948 /* Read the operand name. */
12949 name = cp_parser_identifier (parser);
12950 if (name != error_mark_node)
12951 name = build_string (IDENTIFIER_LENGTH (name),
12952 IDENTIFIER_POINTER (name));
12953 /* Look for the closing `]'. */
12954 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
12958 /* Look for the string-literal. */
12959 token = cp_parser_require (parser, CPP_STRING, "string-literal");
12960 string_literal = token ? token->value : error_mark_node;
12961 /* Look for the `('. */
12962 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
12963 /* Parse the expression. */
12964 expression = cp_parser_expression (parser);
12965 /* Look for the `)'. */
12966 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
12967 /* Add this operand to the list. */
12968 asm_operands = tree_cons (build_tree_list (name, string_literal),
12971 /* If the next token is not a `,', there are no more
12973 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12975 /* Consume the `,'. */
12976 cp_lexer_consume_token (parser->lexer);
12979 return nreverse (asm_operands);
12982 /* Parse an asm-clobber-list.
12986 asm-clobber-list , string-literal
12988 Returns a TREE_LIST, indicating the clobbers in the order that they
12989 appeared. The TREE_VALUE of each node is a STRING_CST. */
12992 cp_parser_asm_clobber_list (parser)
12995 tree clobbers = NULL_TREE;
13000 tree string_literal;
13002 /* Look for the string literal. */
13003 token = cp_parser_require (parser, CPP_STRING, "string-literal");
13004 string_literal = token ? token->value : error_mark_node;
13005 /* Add it to the list. */
13006 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
13007 /* If the next token is not a `,', then the list is
13009 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13011 /* Consume the `,' token. */
13012 cp_lexer_consume_token (parser->lexer);
13018 /* Parse an (optional) series of attributes.
13021 attributes attribute
13024 __attribute__ (( attribute-list [opt] ))
13026 The return value is as for cp_parser_attribute_list. */
13029 cp_parser_attributes_opt (parser)
13032 tree attributes = NULL_TREE;
13037 tree attribute_list;
13039 /* Peek at the next token. */
13040 token = cp_lexer_peek_token (parser->lexer);
13041 /* If it's not `__attribute__', then we're done. */
13042 if (token->keyword != RID_ATTRIBUTE)
13045 /* Consume the `__attribute__' keyword. */
13046 cp_lexer_consume_token (parser->lexer);
13047 /* Look for the two `(' tokens. */
13048 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13049 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13051 /* Peek at the next token. */
13052 token = cp_lexer_peek_token (parser->lexer);
13053 if (token->type != CPP_CLOSE_PAREN)
13054 /* Parse the attribute-list. */
13055 attribute_list = cp_parser_attribute_list (parser);
13057 /* If the next token is a `)', then there is no attribute
13059 attribute_list = NULL;
13061 /* Look for the two `)' tokens. */
13062 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13063 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13065 /* Add these new attributes to the list. */
13066 attributes = chainon (attributes, attribute_list);
13072 /* Parse an attribute-list.
13076 attribute-list , attribute
13080 identifier ( identifier )
13081 identifier ( identifier , expression-list )
13082 identifier ( expression-list )
13084 Returns a TREE_LIST. Each node corresponds to an attribute. THe
13085 TREE_PURPOSE of each node is the identifier indicating which
13086 attribute is in use. The TREE_VALUE represents the arguments, if
13090 cp_parser_attribute_list (parser)
13093 tree attribute_list = NULL_TREE;
13101 /* Look for the identifier. We also allow keywords here; for
13102 example `__attribute__ ((const))' is legal. */
13103 token = cp_lexer_peek_token (parser->lexer);
13104 if (token->type != CPP_NAME
13105 && token->type != CPP_KEYWORD)
13106 return error_mark_node;
13107 /* Consume the token. */
13108 token = cp_lexer_consume_token (parser->lexer);
13110 /* Save away the identifier that indicates which attribute this is. */
13111 identifier = token->value;
13112 attribute = build_tree_list (identifier, NULL_TREE);
13114 /* Peek at the next token. */
13115 token = cp_lexer_peek_token (parser->lexer);
13116 /* If it's an `(', then parse the attribute arguments. */
13117 if (token->type == CPP_OPEN_PAREN)
13120 int arguments_allowed_p = 1;
13122 /* Consume the `('. */
13123 cp_lexer_consume_token (parser->lexer);
13124 /* Peek at the next token. */
13125 token = cp_lexer_peek_token (parser->lexer);
13126 /* Check to see if the next token is an identifier. */
13127 if (token->type == CPP_NAME)
13129 /* Save the identifier. */
13130 identifier = token->value;
13131 /* Consume the identifier. */
13132 cp_lexer_consume_token (parser->lexer);
13133 /* Peek at the next token. */
13134 token = cp_lexer_peek_token (parser->lexer);
13135 /* If the next token is a `,', then there are some other
13136 expressions as well. */
13137 if (token->type == CPP_COMMA)
13138 /* Consume the comma. */
13139 cp_lexer_consume_token (parser->lexer);
13141 arguments_allowed_p = 0;
13144 identifier = NULL_TREE;
13146 /* If there are arguments, parse them too. */
13147 if (arguments_allowed_p)
13148 arguments = cp_parser_expression_list (parser);
13150 arguments = NULL_TREE;
13152 /* Combine the identifier and the arguments. */
13154 arguments = tree_cons (NULL_TREE, identifier, arguments);
13156 /* Save the identifier and arguments away. */
13157 TREE_VALUE (attribute) = arguments;
13159 /* Look for the closing `)'. */
13160 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13163 /* Add this attribute to the list. */
13164 TREE_CHAIN (attribute) = attribute_list;
13165 attribute_list = attribute;
13167 /* Now, look for more attributes. */
13168 token = cp_lexer_peek_token (parser->lexer);
13169 /* If the next token isn't a `,', we're done. */
13170 if (token->type != CPP_COMMA)
13173 /* Consume the commma and keep going. */
13174 cp_lexer_consume_token (parser->lexer);
13177 /* We built up the list in reverse order. */
13178 return nreverse (attribute_list);
13181 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
13182 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
13183 current value of the PEDANTIC flag, regardless of whether or not
13184 the `__extension__' keyword is present. The caller is responsible
13185 for restoring the value of the PEDANTIC flag. */
13188 cp_parser_extension_opt (parser, saved_pedantic)
13190 int *saved_pedantic;
13192 /* Save the old value of the PEDANTIC flag. */
13193 *saved_pedantic = pedantic;
13195 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
13197 /* Consume the `__extension__' token. */
13198 cp_lexer_consume_token (parser->lexer);
13199 /* We're not being pedantic while the `__extension__' keyword is
13209 /* Parse a label declaration.
13212 __label__ label-declarator-seq ;
13214 label-declarator-seq:
13215 identifier , label-declarator-seq
13219 cp_parser_label_declaration (parser)
13222 /* Look for the `__label__' keyword. */
13223 cp_parser_require_keyword (parser, RID_LABEL, "`__label__'");
13229 /* Look for an identifier. */
13230 identifier = cp_parser_identifier (parser);
13231 /* Declare it as a lobel. */
13232 finish_label_decl (identifier);
13233 /* If the next token is a `;', stop. */
13234 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13236 /* Look for the `,' separating the label declarations. */
13237 cp_parser_require (parser, CPP_COMMA, "`,'");
13240 /* Look for the final `;'. */
13241 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
13244 /* Support Functions */
13246 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
13247 NAME should have one of the representations used for an
13248 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
13249 is returned. If PARSER->SCOPE is a dependent type, then a
13250 SCOPE_REF is returned.
13252 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
13253 returned; the name was already resolved when the TEMPLATE_ID_EXPR
13254 was formed. Abstractly, such entities should not be passed to this
13255 function, because they do not need to be looked up, but it is
13256 simpler to check for this special case here, rather than at the
13259 In cases not explicitly covered above, this function returns a
13260 DECL, OVERLOAD, or baselink representing the result of the lookup.
13261 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
13264 If CHECK_ACCESS is TRUE, then access control is performed on the
13265 declaration to which the name resolves, and an error message is
13266 issued if the declaration is inaccessible.
13268 If IS_TYPE is TRUE, bindings that do not refer to types are
13271 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
13275 cp_parser_lookup_name (parser, name, check_access, is_type,
13281 bool check_dependency;
13284 tree object_type = parser->context->object_type;
13286 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
13287 no longer valid. Note that if we are parsing tentatively, and
13288 the parse fails, OBJECT_TYPE will be automatically restored. */
13289 parser->context->object_type = NULL_TREE;
13291 if (name == error_mark_node)
13292 return error_mark_node;
13294 /* A template-id has already been resolved; there is no lookup to
13296 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
13298 if (BASELINK_P (name))
13300 my_friendly_assert ((TREE_CODE (BASELINK_FUNCTIONS (name))
13301 == TEMPLATE_ID_EXPR),
13306 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
13307 it should already have been checked to make sure that the name
13308 used matches the type being destroyed. */
13309 if (TREE_CODE (name) == BIT_NOT_EXPR)
13313 /* Figure out to which type this destructor applies. */
13315 type = parser->scope;
13316 else if (object_type)
13317 type = object_type;
13319 type = current_class_type;
13320 /* If that's not a class type, there is no destructor. */
13321 if (!type || !CLASS_TYPE_P (type))
13322 return error_mark_node;
13323 /* If it was a class type, return the destructor. */
13324 return CLASSTYPE_DESTRUCTORS (type);
13327 /* By this point, the NAME should be an ordinary identifier. If
13328 the id-expression was a qualified name, the qualifying scope is
13329 stored in PARSER->SCOPE at this point. */
13330 my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE,
13333 /* Perform the lookup. */
13336 bool dependent_type_p;
13338 if (parser->scope == error_mark_node)
13339 return error_mark_node;
13341 /* If the SCOPE is dependent, the lookup must be deferred until
13342 the template is instantiated -- unless we are explicitly
13343 looking up names in uninstantiated templates. Even then, we
13344 cannot look up the name if the scope is not a class type; it
13345 might, for example, be a template type parameter. */
13346 dependent_type_p = (TYPE_P (parser->scope)
13347 && !(parser->in_declarator_p
13348 && currently_open_class (parser->scope))
13349 && cp_parser_dependent_type_p (parser->scope));
13350 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
13351 && dependent_type_p)
13354 decl = build_nt (SCOPE_REF, parser->scope, name);
13356 /* The resolution to Core Issue 180 says that `struct A::B'
13357 should be considered a type-name, even if `A' is
13359 decl = TYPE_NAME (make_typename_type (parser->scope,
13365 /* If PARSER->SCOPE is a dependent type, then it must be a
13366 class type, and we must not be checking dependencies;
13367 otherwise, we would have processed this lookup above. So
13368 that PARSER->SCOPE is not considered a dependent base by
13369 lookup_member, we must enter the scope here. */
13370 if (dependent_type_p)
13371 push_scope (parser->scope);
13372 /* If the PARSER->SCOPE is a a template specialization, it
13373 may be instantiated during name lookup. In that case,
13374 errors may be issued. Even if we rollback the current
13375 tentative parse, those errors are valid. */
13376 decl = lookup_qualified_name (parser->scope, name, is_type,
13378 if (dependent_type_p)
13379 pop_scope (parser->scope);
13381 parser->qualifying_scope = parser->scope;
13382 parser->object_scope = NULL_TREE;
13384 else if (object_type)
13386 tree object_decl = NULL_TREE;
13387 /* Look up the name in the scope of the OBJECT_TYPE, unless the
13388 OBJECT_TYPE is not a class. */
13389 if (CLASS_TYPE_P (object_type))
13390 /* If the OBJECT_TYPE is a template specialization, it may
13391 be instantiated during name lookup. In that case, errors
13392 may be issued. Even if we rollback the current tentative
13393 parse, those errors are valid. */
13394 object_decl = lookup_member (object_type,
13396 /*protect=*/0, is_type);
13397 /* Look it up in the enclosing context, too. */
13398 decl = lookup_name_real (name, is_type, /*nonclass=*/0,
13399 /*namespaces_only=*/0,
13401 parser->object_scope = object_type;
13402 parser->qualifying_scope = NULL_TREE;
13404 decl = object_decl;
13408 decl = lookup_name_real (name, is_type, /*nonclass=*/0,
13409 /*namespaces_only=*/0,
13411 parser->qualifying_scope = NULL_TREE;
13412 parser->object_scope = NULL_TREE;
13415 /* If the lookup failed, let our caller know. */
13417 || decl == error_mark_node
13418 || (TREE_CODE (decl) == FUNCTION_DECL
13419 && DECL_ANTICIPATED (decl)))
13420 return error_mark_node;
13422 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
13423 if (TREE_CODE (decl) == TREE_LIST)
13425 /* The error message we have to print is too complicated for
13426 cp_parser_error, so we incorporate its actions directly. */
13427 if (!cp_parser_simulate_error (parser))
13429 error ("reference to `%D' is ambiguous", name);
13430 print_candidates (decl);
13432 return error_mark_node;
13435 my_friendly_assert (DECL_P (decl)
13436 || TREE_CODE (decl) == OVERLOAD
13437 || TREE_CODE (decl) == SCOPE_REF
13438 || BASELINK_P (decl),
13441 /* If we have resolved the name of a member declaration, check to
13442 see if the declaration is accessible. When the name resolves to
13443 set of overloaded functions, accesibility is checked when
13444 overload resolution is done.
13446 During an explicit instantiation, access is not checked at all,
13447 as per [temp.explicit]. */
13448 if (check_access && scope_chain->check_access && DECL_P (decl))
13450 tree qualifying_type;
13452 /* Figure out the type through which DECL is being
13455 = cp_parser_scope_through_which_access_occurs (decl,
13458 if (qualifying_type)
13460 /* If we are supposed to defer access checks, just record
13461 the information for later. */
13462 if (parser->context->deferring_access_checks_p)
13463 cp_parser_defer_access_check (parser, qualifying_type, decl);
13464 /* Otherwise, check accessibility now. */
13466 enforce_access (qualifying_type, decl);
13473 /* Like cp_parser_lookup_name, but for use in the typical case where
13474 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, and CHECK_DEPENDENCY is
13478 cp_parser_lookup_name_simple (parser, name)
13482 return cp_parser_lookup_name (parser, name,
13483 /*check_access=*/true,
13485 /*check_dependency=*/true);
13488 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
13489 TYPENAME_TYPE corresponds. Note that this function peers inside
13490 uninstantiated templates and therefore should be used only in
13491 extremely limited situations. */
13494 cp_parser_resolve_typename_type (parser, type)
13502 my_friendly_assert (TREE_CODE (type) == TYPENAME_TYPE,
13505 scope = TYPE_CONTEXT (type);
13506 name = DECL_NAME (TYPE_NAME (type));
13508 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
13509 it first before we can figure out what NAME refers to. */
13510 if (TREE_CODE (scope) == TYPENAME_TYPE)
13511 scope = cp_parser_resolve_typename_type (parser, scope);
13512 /* If we don't know what SCOPE refers to, then we cannot resolve the
13514 if (scope == error_mark_node)
13515 return error_mark_node;
13516 /* If the SCOPE is a template type parameter, we have no way of
13517 resolving the name. */
13518 if (TREE_CODE (scope) == TEMPLATE_TYPE_PARM)
13520 /* Enter the SCOPE so that name lookup will be resolved as if we
13521 were in the class definition. In particular, SCOPE will no
13522 longer be considered a dependent type. */
13523 push_scope (scope);
13524 /* Look up the declaration. */
13525 decl = lookup_member (scope, name, /*protect=*/0, /*want_type=*/1);
13526 /* If all went well, we got a TYPE_DECL for a non-typename. */
13528 || TREE_CODE (decl) != TYPE_DECL
13529 || TREE_CODE (TREE_TYPE (decl)) == TYPENAME_TYPE)
13531 cp_parser_error (parser, "could not resolve typename type");
13532 type = error_mark_node;
13535 type = TREE_TYPE (decl);
13536 /* Leave the SCOPE. */
13542 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
13543 the current context, return the TYPE_DECL. If TAG_NAME_P is
13544 true, the DECL indicates the class being defined in a class-head,
13545 or declared in an elaborated-type-specifier.
13547 Otherwise, return DECL. */
13550 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
13552 /* If the DECL is a TEMPLATE_DECL for a class type, and we are in
13553 the scope of the class, then treat the TEMPLATE_DECL as a
13554 class-name. For example, in:
13556 template <class T> struct S {
13562 If the TEMPLATE_DECL is being declared as part of a class-head,
13563 the same translation occurs:
13566 template <typename T> struct B;
13569 template <typename T> struct A::B {};
13571 Similarly, in a elaborated-type-specifier:
13573 namespace N { struct X{}; }
13576 template <typename T> friend struct N::X;
13580 if (DECL_CLASS_TEMPLATE_P (decl)
13582 || (current_class_type
13583 && same_type_p (TREE_TYPE (DECL_TEMPLATE_RESULT (decl)),
13584 current_class_type))))
13585 return DECL_TEMPLATE_RESULT (decl);
13590 /* If too many, or too few, template-parameter lists apply to the
13591 declarator, issue an error message. Returns TRUE if all went well,
13592 and FALSE otherwise. */
13595 cp_parser_check_declarator_template_parameters (parser, declarator)
13599 unsigned num_templates;
13601 /* We haven't seen any classes that involve template parameters yet. */
13604 switch (TREE_CODE (declarator))
13611 tree main_declarator = TREE_OPERAND (declarator, 0);
13613 cp_parser_check_declarator_template_parameters (parser,
13622 scope = TREE_OPERAND (declarator, 0);
13623 member = TREE_OPERAND (declarator, 1);
13625 /* If this is a pointer-to-member, then we are not interested
13626 in the SCOPE, because it does not qualify the thing that is
13628 if (TREE_CODE (member) == INDIRECT_REF)
13629 return (cp_parser_check_declarator_template_parameters
13632 while (scope && CLASS_TYPE_P (scope))
13634 /* You're supposed to have one `template <...>'
13635 for every template class, but you don't need one
13636 for a full specialization. For example:
13638 template <class T> struct S{};
13639 template <> struct S<int> { void f(); };
13640 void S<int>::f () {}
13642 is correct; there shouldn't be a `template <>' for
13643 the definition of `S<int>::f'. */
13644 if (CLASSTYPE_TEMPLATE_INFO (scope)
13645 && (CLASSTYPE_TEMPLATE_INSTANTIATION (scope)
13646 || uses_template_parms (CLASSTYPE_TI_ARGS (scope)))
13647 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
13650 scope = TYPE_CONTEXT (scope);
13654 /* Fall through. */
13657 /* If the DECLARATOR has the form `X<y>' then it uses one
13658 additional level of template parameters. */
13659 if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
13662 return cp_parser_check_template_parameters (parser,
13667 /* NUM_TEMPLATES were used in the current declaration. If that is
13668 invalid, return FALSE and issue an error messages. Otherwise,
13672 cp_parser_check_template_parameters (parser, num_templates)
13674 unsigned num_templates;
13676 /* If there are more template classes than parameter lists, we have
13679 template <class T> void S<T>::R<T>::f (); */
13680 if (parser->num_template_parameter_lists < num_templates)
13682 error ("too few template-parameter-lists");
13685 /* If there are the same number of template classes and parameter
13686 lists, that's OK. */
13687 if (parser->num_template_parameter_lists == num_templates)
13689 /* If there are more, but only one more, then we are referring to a
13690 member template. That's OK too. */
13691 if (parser->num_template_parameter_lists == num_templates + 1)
13693 /* Otherwise, there are too many template parameter lists. We have
13696 template <class T> template <class U> void S::f(); */
13697 error ("too many template-parameter-lists");
13701 /* Parse a binary-expression of the general form:
13705 binary-expression <token> <expr>
13707 The TOKEN_TREE_MAP maps <token> types to <expr> codes. FN is used
13708 to parser the <expr>s. If the first production is used, then the
13709 value returned by FN is returned directly. Otherwise, a node with
13710 the indicated EXPR_TYPE is returned, with operands corresponding to
13711 the two sub-expressions. */
13714 cp_parser_binary_expression (parser, token_tree_map, fn)
13716 cp_parser_token_tree_map token_tree_map;
13717 cp_parser_expression_fn fn;
13721 /* Parse the first expression. */
13722 lhs = (*fn) (parser);
13723 /* Now, look for more expressions. */
13727 cp_parser_token_tree_map_node *map_node;
13730 /* Peek at the next token. */
13731 token = cp_lexer_peek_token (parser->lexer);
13732 /* If the token is `>', and that's not an operator at the
13733 moment, then we're done. */
13734 if (token->type == CPP_GREATER
13735 && !parser->greater_than_is_operator_p)
13737 /* If we find one of the tokens we want, build the correspoding
13738 tree representation. */
13739 for (map_node = token_tree_map;
13740 map_node->token_type != CPP_EOF;
13742 if (map_node->token_type == token->type)
13744 /* Consume the operator token. */
13745 cp_lexer_consume_token (parser->lexer);
13746 /* Parse the right-hand side of the expression. */
13747 rhs = (*fn) (parser);
13748 /* Build the binary tree node. */
13749 lhs = build_x_binary_op (map_node->tree_type, lhs, rhs);
13753 /* If the token wasn't one of the ones we want, we're done. */
13754 if (map_node->token_type == CPP_EOF)
13761 /* Parse an optional `::' token indicating that the following name is
13762 from the global namespace. If so, PARSER->SCOPE is set to the
13763 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
13764 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
13765 Returns the new value of PARSER->SCOPE, if the `::' token is
13766 present, and NULL_TREE otherwise. */
13769 cp_parser_global_scope_opt (parser, current_scope_valid_p)
13771 bool current_scope_valid_p;
13775 /* Peek at the next token. */
13776 token = cp_lexer_peek_token (parser->lexer);
13777 /* If we're looking at a `::' token then we're starting from the
13778 global namespace, not our current location. */
13779 if (token->type == CPP_SCOPE)
13781 /* Consume the `::' token. */
13782 cp_lexer_consume_token (parser->lexer);
13783 /* Set the SCOPE so that we know where to start the lookup. */
13784 parser->scope = global_namespace;
13785 parser->qualifying_scope = global_namespace;
13786 parser->object_scope = NULL_TREE;
13788 return parser->scope;
13790 else if (!current_scope_valid_p)
13792 parser->scope = NULL_TREE;
13793 parser->qualifying_scope = NULL_TREE;
13794 parser->object_scope = NULL_TREE;
13800 /* Returns TRUE if the upcoming token sequence is the start of a
13801 constructor declarator. If FRIEND_P is true, the declarator is
13802 preceded by the `friend' specifier. */
13805 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
13807 bool constructor_p;
13808 tree type_decl = NULL_TREE;
13809 bool nested_name_p;
13811 /* Parse tentatively; we are going to roll back all of the tokens
13813 cp_parser_parse_tentatively (parser);
13814 /* Assume that we are looking at a constructor declarator. */
13815 constructor_p = true;
13816 /* Look for the optional `::' operator. */
13817 cp_parser_global_scope_opt (parser,
13818 /*current_scope_valid_p=*/false);
13819 /* Look for the nested-name-specifier. */
13821 = (cp_parser_nested_name_specifier_opt (parser,
13822 /*typename_keyword_p=*/false,
13823 /*check_dependency_p=*/false,
13826 /* Outside of a class-specifier, there must be a
13827 nested-name-specifier. */
13828 if (!nested_name_p &&
13829 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
13831 constructor_p = false;
13832 /* If we still think that this might be a constructor-declarator,
13833 look for a class-name. */
13838 template <typename T> struct S { S(); }
13839 template <typename T> S<T>::S ();
13841 we must recognize that the nested `S' names a class.
13844 template <typename T> S<T>::S<T> ();
13846 we must recognize that the nested `S' names a template. */
13847 type_decl = cp_parser_class_name (parser,
13848 /*typename_keyword_p=*/false,
13849 /*template_keyword_p=*/false,
13851 /*check_access_p=*/false,
13852 /*check_dependency_p=*/false,
13853 /*class_head_p=*/false);
13854 /* If there was no class-name, then this is not a constructor. */
13855 constructor_p = !cp_parser_error_occurred (parser);
13857 /* If we're still considering a constructor, we have to see a `(',
13858 to begin the parameter-declaration-clause, followed by either a
13859 `)', an `...', or a decl-specifier. We need to check for a
13860 type-specifier to avoid being fooled into thinking that:
13864 is a constructor. (It is actually a function named `f' that
13865 takes one parameter (of type `int') and returns a value of type
13868 && cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
13870 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
13871 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
13872 && !cp_parser_storage_class_specifier_opt (parser))
13874 if (current_class_type
13875 && !same_type_p (current_class_type, TREE_TYPE (type_decl)))
13876 /* The constructor for one class cannot be declared inside
13878 constructor_p = false;
13883 /* Names appearing in the type-specifier should be looked up
13884 in the scope of the class. */
13885 if (current_class_type)
13889 type = TREE_TYPE (type_decl);
13890 if (TREE_CODE (type) == TYPENAME_TYPE)
13891 type = cp_parser_resolve_typename_type (parser, type);
13894 /* Look for the type-specifier. */
13895 cp_parser_type_specifier (parser,
13896 CP_PARSER_FLAGS_NONE,
13897 /*is_friend=*/false,
13898 /*is_declarator=*/true,
13899 /*declares_class_or_enum=*/NULL,
13900 /*is_cv_qualifier=*/NULL);
13901 /* Leave the scope of the class. */
13905 constructor_p = !cp_parser_error_occurred (parser);
13910 constructor_p = false;
13911 /* We did not really want to consume any tokens. */
13912 cp_parser_abort_tentative_parse (parser);
13914 return constructor_p;
13917 /* Parse the definition of the function given by the DECL_SPECIFIERS,
13918 ATTRIBUTES, and DECLARATOR. The ACCESS_CHECKS have been deferred;
13919 they must be performed once we are in the scope of the function.
13921 Returns the function defined. */
13924 cp_parser_function_definition_from_specifiers_and_declarator
13925 (parser, decl_specifiers, attributes, declarator, access_checks)
13927 tree decl_specifiers;
13930 tree access_checks;
13935 /* Begin the function-definition. */
13936 success_p = begin_function_definition (decl_specifiers,
13940 /* If there were names looked up in the decl-specifier-seq that we
13941 did not check, check them now. We must wait until we are in the
13942 scope of the function to perform the checks, since the function
13943 might be a friend. */
13944 cp_parser_perform_deferred_access_checks (access_checks);
13948 /* If begin_function_definition didn't like the definition, skip
13949 the entire function. */
13950 error ("invalid function declaration");
13951 cp_parser_skip_to_end_of_block_or_statement (parser);
13952 fn = error_mark_node;
13955 fn = cp_parser_function_definition_after_declarator (parser,
13956 /*inline_p=*/false);
13961 /* Parse the part of a function-definition that follows the
13962 declarator. INLINE_P is TRUE iff this function is an inline
13963 function defined with a class-specifier.
13965 Returns the function defined. */
13968 cp_parser_function_definition_after_declarator (parser,
13974 bool ctor_initializer_p = false;
13975 bool saved_in_unbraced_linkage_specification_p;
13976 unsigned saved_num_template_parameter_lists;
13978 /* If the next token is `return', then the code may be trying to
13979 make use of the "named return value" extension that G++ used to
13981 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
13983 /* Consume the `return' keyword. */
13984 cp_lexer_consume_token (parser->lexer);
13985 /* Look for the identifier that indicates what value is to be
13987 cp_parser_identifier (parser);
13988 /* Issue an error message. */
13989 error ("named return values are no longer supported");
13990 /* Skip tokens until we reach the start of the function body. */
13991 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
13992 cp_lexer_consume_token (parser->lexer);
13994 /* The `extern' in `extern "C" void f () { ... }' does not apply to
13995 anything declared inside `f'. */
13996 saved_in_unbraced_linkage_specification_p
13997 = parser->in_unbraced_linkage_specification_p;
13998 parser->in_unbraced_linkage_specification_p = false;
13999 /* Inside the function, surrounding template-parameter-lists do not
14001 saved_num_template_parameter_lists
14002 = parser->num_template_parameter_lists;
14003 parser->num_template_parameter_lists = 0;
14004 /* If the next token is `try', then we are looking at a
14005 function-try-block. */
14006 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
14007 ctor_initializer_p = cp_parser_function_try_block (parser);
14008 /* A function-try-block includes the function-body, so we only do
14009 this next part if we're not processing a function-try-block. */
14012 = cp_parser_ctor_initializer_opt_and_function_body (parser);
14014 /* Finish the function. */
14015 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
14016 (inline_p ? 2 : 0));
14017 /* Generate code for it, if necessary. */
14019 /* Restore the saved values. */
14020 parser->in_unbraced_linkage_specification_p
14021 = saved_in_unbraced_linkage_specification_p;
14022 parser->num_template_parameter_lists
14023 = saved_num_template_parameter_lists;
14028 /* Parse a template-declaration, assuming that the `export' (and
14029 `extern') keywords, if present, has already been scanned. MEMBER_P
14030 is as for cp_parser_template_declaration. */
14033 cp_parser_template_declaration_after_export (parser, member_p)
14037 tree decl = NULL_TREE;
14038 tree parameter_list;
14039 bool friend_p = false;
14041 /* Look for the `template' keyword. */
14042 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'"))
14046 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
14049 /* Parse the template parameters. */
14050 begin_template_parm_list ();
14051 /* If the next token is `>', then we have an invalid
14052 specialization. Rather than complain about an invalid template
14053 parameter, issue an error message here. */
14054 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
14056 cp_parser_error (parser, "invalid explicit specialization");
14057 parameter_list = NULL_TREE;
14060 parameter_list = cp_parser_template_parameter_list (parser);
14061 parameter_list = end_template_parm_list (parameter_list);
14062 /* Look for the `>'. */
14063 cp_parser_skip_until_found (parser, CPP_GREATER, "`>'");
14064 /* We just processed one more parameter list. */
14065 ++parser->num_template_parameter_lists;
14066 /* If the next token is `template', there are more template
14068 if (cp_lexer_next_token_is_keyword (parser->lexer,
14070 cp_parser_template_declaration_after_export (parser, member_p);
14073 decl = cp_parser_single_declaration (parser,
14077 /* If this is a member template declaration, let the front
14079 if (member_p && !friend_p && decl)
14080 decl = finish_member_template_decl (decl);
14081 else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
14082 make_friend_class (current_class_type, TREE_TYPE (decl));
14084 /* We are done with the current parameter list. */
14085 --parser->num_template_parameter_lists;
14088 finish_template_decl (parameter_list);
14090 /* Register member declarations. */
14091 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
14092 finish_member_declaration (decl);
14094 /* If DECL is a function template, we must return to parse it later.
14095 (Even though there is no definition, there might be default
14096 arguments that need handling.) */
14097 if (member_p && decl
14098 && (TREE_CODE (decl) == FUNCTION_DECL
14099 || DECL_FUNCTION_TEMPLATE_P (decl)))
14100 TREE_VALUE (parser->unparsed_functions_queues)
14101 = tree_cons (current_class_type, decl,
14102 TREE_VALUE (parser->unparsed_functions_queues));
14105 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
14106 `function-definition' sequence. MEMBER_P is true, this declaration
14107 appears in a class scope.
14109 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
14110 *FRIEND_P is set to TRUE iff the declaration is a friend. */
14113 cp_parser_single_declaration (parser,
14120 bool declares_class_or_enum;
14121 tree decl = NULL_TREE;
14122 tree decl_specifiers;
14124 tree access_checks;
14126 /* Parse the dependent declaration. We don't know yet
14127 whether it will be a function-definition. */
14128 cp_parser_parse_tentatively (parser);
14129 /* Defer access checks until we know what is being declared. */
14130 cp_parser_start_deferring_access_checks (parser);
14131 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
14134 = cp_parser_decl_specifier_seq (parser,
14135 CP_PARSER_FLAGS_OPTIONAL,
14137 &declares_class_or_enum);
14138 /* Gather up the access checks that occurred the
14139 decl-specifier-seq. */
14140 access_checks = cp_parser_stop_deferring_access_checks (parser);
14141 /* Check for the declaration of a template class. */
14142 if (declares_class_or_enum)
14144 if (cp_parser_declares_only_class_p (parser))
14146 decl = shadow_tag (decl_specifiers);
14148 decl = TYPE_NAME (decl);
14150 decl = error_mark_node;
14155 /* If it's not a template class, try for a template function. If
14156 the next token is a `;', then this declaration does not declare
14157 anything. But, if there were errors in the decl-specifiers, then
14158 the error might well have come from an attempted class-specifier.
14159 In that case, there's no need to warn about a missing declarator. */
14161 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
14162 || !value_member (error_mark_node, decl_specifiers)))
14163 decl = cp_parser_init_declarator (parser,
14167 /*function_definition_allowed_p=*/false,
14169 /*function_definition_p=*/NULL);
14170 /* Clear any current qualification; whatever comes next is the start
14171 of something new. */
14172 parser->scope = NULL_TREE;
14173 parser->qualifying_scope = NULL_TREE;
14174 parser->object_scope = NULL_TREE;
14175 /* Look for a trailing `;' after the declaration. */
14176 if (!cp_parser_require (parser, CPP_SEMICOLON, "expected `;'")
14177 && cp_parser_committed_to_tentative_parse (parser))
14178 cp_parser_skip_to_end_of_block_or_statement (parser);
14179 /* If it worked, set *FRIEND_P based on the DECL_SPECIFIERS. */
14180 if (cp_parser_parse_definitely (parser))
14183 *friend_p = cp_parser_friend_p (decl_specifiers);
14185 /* Otherwise, try a function-definition. */
14187 decl = cp_parser_function_definition (parser, friend_p);
14192 /* Parse a functional cast to TYPE. Returns an expression
14193 representing the cast. */
14196 cp_parser_functional_cast (parser, type)
14200 tree expression_list;
14202 /* Look for the opening `('. */
14203 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
14204 return error_mark_node;
14205 /* If the next token is not an `)', there are arguments to the
14207 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
14208 expression_list = cp_parser_expression_list (parser);
14210 expression_list = NULL_TREE;
14211 /* Look for the closing `)'. */
14212 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14214 return build_functional_cast (type, expression_list);
14217 /* MEMBER_FUNCTION is a member function, or a friend. If default
14218 arguments, or the body of the function have not yet been parsed,
14222 cp_parser_late_parsing_for_member (parser, member_function)
14224 tree member_function;
14226 cp_lexer *saved_lexer;
14228 /* If this member is a template, get the underlying
14230 if (DECL_FUNCTION_TEMPLATE_P (member_function))
14231 member_function = DECL_TEMPLATE_RESULT (member_function);
14233 /* There should not be any class definitions in progress at this
14234 point; the bodies of members are only parsed outside of all class
14236 my_friendly_assert (parser->num_classes_being_defined == 0, 20010816);
14237 /* While we're parsing the member functions we might encounter more
14238 classes. We want to handle them right away, but we don't want
14239 them getting mixed up with functions that are currently in the
14241 parser->unparsed_functions_queues
14242 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
14244 /* Make sure that any template parameters are in scope. */
14245 maybe_begin_member_template_processing (member_function);
14247 /* If there are default arguments that have not yet been processed,
14248 take care of them now. */
14249 cp_parser_late_parsing_default_args (parser, TREE_TYPE (member_function),
14250 DECL_FUNCTION_MEMBER_P (member_function)
14251 ? DECL_CONTEXT (member_function)
14254 /* If the body of the function has not yet been parsed, parse it
14256 if (DECL_PENDING_INLINE_P (member_function))
14258 tree function_scope;
14259 cp_token_cache *tokens;
14261 /* The function is no longer pending; we are processing it. */
14262 tokens = DECL_PENDING_INLINE_INFO (member_function);
14263 DECL_PENDING_INLINE_INFO (member_function) = NULL;
14264 DECL_PENDING_INLINE_P (member_function) = 0;
14265 /* If this was an inline function in a local class, enter the scope
14266 of the containing function. */
14267 function_scope = decl_function_context (member_function);
14268 if (function_scope)
14269 push_function_context_to (function_scope);
14271 /* Save away the current lexer. */
14272 saved_lexer = parser->lexer;
14273 /* Make a new lexer to feed us the tokens saved for this function. */
14274 parser->lexer = cp_lexer_new_from_tokens (tokens);
14275 parser->lexer->next = saved_lexer;
14277 /* Set the current source position to be the location of the first
14278 token in the saved inline body. */
14279 cp_lexer_set_source_position_from_token
14281 cp_lexer_peek_token (parser->lexer));
14283 /* Let the front end know that we going to be defining this
14285 start_function (NULL_TREE, member_function, NULL_TREE,
14286 SF_PRE_PARSED | SF_INCLASS_INLINE);
14288 /* Now, parse the body of the function. */
14289 cp_parser_function_definition_after_declarator (parser,
14290 /*inline_p=*/true);
14292 /* Leave the scope of the containing function. */
14293 if (function_scope)
14294 pop_function_context_from (function_scope);
14295 /* Restore the lexer. */
14296 parser->lexer = saved_lexer;
14299 /* Remove any template parameters from the symbol table. */
14300 maybe_end_member_template_processing ();
14302 /* Restore the queue. */
14303 parser->unparsed_functions_queues
14304 = TREE_CHAIN (parser->unparsed_functions_queues);
14307 /* TYPE is a FUNCTION_TYPE or METHOD_TYPE which contains a parameter
14308 with an unparsed DEFAULT_ARG. If non-NULL, SCOPE is the class in
14309 whose context name lookups in the default argument should occur.
14310 Parse the default args now. */
14313 cp_parser_late_parsing_default_args (cp_parser *parser, tree type, tree scope)
14315 cp_lexer *saved_lexer;
14316 cp_token_cache *tokens;
14317 bool saved_local_variables_forbidden_p;
14320 for (parameters = TYPE_ARG_TYPES (type);
14322 parameters = TREE_CHAIN (parameters))
14324 if (!TREE_PURPOSE (parameters)
14325 || TREE_CODE (TREE_PURPOSE (parameters)) != DEFAULT_ARG)
14328 /* Save away the current lexer. */
14329 saved_lexer = parser->lexer;
14330 /* Create a new one, using the tokens we have saved. */
14331 tokens = DEFARG_TOKENS (TREE_PURPOSE (parameters));
14332 parser->lexer = cp_lexer_new_from_tokens (tokens);
14334 /* Set the current source position to be the location of the
14335 first token in the default argument. */
14336 cp_lexer_set_source_position_from_token
14337 (parser->lexer, cp_lexer_peek_token (parser->lexer));
14339 /* Local variable names (and the `this' keyword) may not appear
14340 in a default argument. */
14341 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
14342 parser->local_variables_forbidden_p = true;
14343 /* Parse the assignment-expression. */
14345 push_nested_class (scope, 1);
14346 TREE_PURPOSE (parameters) = cp_parser_assignment_expression (parser);
14348 pop_nested_class ();
14350 /* Restore saved state. */
14351 parser->lexer = saved_lexer;
14352 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
14356 /* Parse the operand of `sizeof' (or a similar operator). Returns
14357 either a TYPE or an expression, depending on the form of the
14358 input. The KEYWORD indicates which kind of expression we have
14362 cp_parser_sizeof_operand (parser, keyword)
14366 static const char *format;
14367 tree expr = NULL_TREE;
14368 const char *saved_message;
14369 bool saved_constant_expression_p;
14371 /* Initialize FORMAT the first time we get here. */
14373 format = "types may not be defined in `%s' expressions";
14375 /* Types cannot be defined in a `sizeof' expression. Save away the
14377 saved_message = parser->type_definition_forbidden_message;
14378 /* And create the new one. */
14379 parser->type_definition_forbidden_message
14381 xmalloc (strlen (format)
14382 + strlen (IDENTIFIER_POINTER (ridpointers[keyword]))
14384 sprintf ((char *) parser->type_definition_forbidden_message,
14385 format, IDENTIFIER_POINTER (ridpointers[keyword]));
14387 /* The restrictions on constant-expressions do not apply inside
14388 sizeof expressions. */
14389 saved_constant_expression_p = parser->constant_expression_p;
14390 parser->constant_expression_p = false;
14392 /* If it's a `(', then we might be looking at the type-id
14394 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
14398 /* We can't be sure yet whether we're looking at a type-id or an
14400 cp_parser_parse_tentatively (parser);
14401 /* Consume the `('. */
14402 cp_lexer_consume_token (parser->lexer);
14403 /* Parse the type-id. */
14404 type = cp_parser_type_id (parser);
14405 /* Now, look for the trailing `)'. */
14406 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14407 /* If all went well, then we're done. */
14408 if (cp_parser_parse_definitely (parser))
14410 /* Build a list of decl-specifiers; right now, we have only
14411 a single type-specifier. */
14412 type = build_tree_list (NULL_TREE,
14415 /* Call grokdeclarator to figure out what type this is. */
14416 expr = grokdeclarator (NULL_TREE,
14420 /*attrlist=*/NULL);
14424 /* If the type-id production did not work out, then we must be
14425 looking at the unary-expression production. */
14427 expr = cp_parser_unary_expression (parser, /*address_p=*/false);
14429 /* Free the message we created. */
14430 free ((char *) parser->type_definition_forbidden_message);
14431 /* And restore the old one. */
14432 parser->type_definition_forbidden_message = saved_message;
14433 parser->constant_expression_p = saved_constant_expression_p;
14438 /* If the current declaration has no declarator, return true. */
14441 cp_parser_declares_only_class_p (cp_parser *parser)
14443 /* If the next token is a `;' or a `,' then there is no
14445 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
14446 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
14449 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
14450 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
14453 cp_parser_friend_p (decl_specifiers)
14454 tree decl_specifiers;
14456 while (decl_specifiers)
14458 /* See if this decl-specifier is `friend'. */
14459 if (TREE_CODE (TREE_VALUE (decl_specifiers)) == IDENTIFIER_NODE
14460 && C_RID_CODE (TREE_VALUE (decl_specifiers)) == RID_FRIEND)
14463 /* Go on to the next decl-specifier. */
14464 decl_specifiers = TREE_CHAIN (decl_specifiers);
14470 /* If the next token is of the indicated TYPE, consume it. Otherwise,
14471 issue an error message indicating that TOKEN_DESC was expected.
14473 Returns the token consumed, if the token had the appropriate type.
14474 Otherwise, returns NULL. */
14477 cp_parser_require (parser, type, token_desc)
14479 enum cpp_ttype type;
14480 const char *token_desc;
14482 if (cp_lexer_next_token_is (parser->lexer, type))
14483 return cp_lexer_consume_token (parser->lexer);
14486 /* Output the MESSAGE -- unless we're parsing tentatively. */
14487 if (!cp_parser_simulate_error (parser))
14488 error ("expected %s", token_desc);
14493 /* Like cp_parser_require, except that tokens will be skipped until
14494 the desired token is found. An error message is still produced if
14495 the next token is not as expected. */
14498 cp_parser_skip_until_found (parser, type, token_desc)
14500 enum cpp_ttype type;
14501 const char *token_desc;
14504 unsigned nesting_depth = 0;
14506 if (cp_parser_require (parser, type, token_desc))
14509 /* Skip tokens until the desired token is found. */
14512 /* Peek at the next token. */
14513 token = cp_lexer_peek_token (parser->lexer);
14514 /* If we've reached the token we want, consume it and
14516 if (token->type == type && !nesting_depth)
14518 cp_lexer_consume_token (parser->lexer);
14521 /* If we've run out of tokens, stop. */
14522 if (token->type == CPP_EOF)
14524 if (token->type == CPP_OPEN_BRACE
14525 || token->type == CPP_OPEN_PAREN
14526 || token->type == CPP_OPEN_SQUARE)
14528 else if (token->type == CPP_CLOSE_BRACE
14529 || token->type == CPP_CLOSE_PAREN
14530 || token->type == CPP_CLOSE_SQUARE)
14532 if (nesting_depth-- == 0)
14535 /* Consume this token. */
14536 cp_lexer_consume_token (parser->lexer);
14540 /* If the next token is the indicated keyword, consume it. Otherwise,
14541 issue an error message indicating that TOKEN_DESC was expected.
14543 Returns the token consumed, if the token had the appropriate type.
14544 Otherwise, returns NULL. */
14547 cp_parser_require_keyword (parser, keyword, token_desc)
14550 const char *token_desc;
14552 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
14554 if (token && token->keyword != keyword)
14556 dyn_string_t error_msg;
14558 /* Format the error message. */
14559 error_msg = dyn_string_new (0);
14560 dyn_string_append_cstr (error_msg, "expected ");
14561 dyn_string_append_cstr (error_msg, token_desc);
14562 cp_parser_error (parser, error_msg->s);
14563 dyn_string_delete (error_msg);
14570 /* Returns TRUE iff TOKEN is a token that can begin the body of a
14571 function-definition. */
14574 cp_parser_token_starts_function_definition_p (token)
14577 return (/* An ordinary function-body begins with an `{'. */
14578 token->type == CPP_OPEN_BRACE
14579 /* A ctor-initializer begins with a `:'. */
14580 || token->type == CPP_COLON
14581 /* A function-try-block begins with `try'. */
14582 || token->keyword == RID_TRY
14583 /* The named return value extension begins with `return'. */
14584 || token->keyword == RID_RETURN);
14587 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
14591 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
14595 token = cp_lexer_peek_token (parser->lexer);
14596 return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
14599 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
14600 or none_type otherwise. */
14602 static enum tag_types
14603 cp_parser_token_is_class_key (token)
14606 switch (token->keyword)
14611 return record_type;
14620 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
14623 cp_parser_check_class_key (enum tag_types class_key, tree type)
14625 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
14626 pedwarn ("`%s' tag used in naming `%#T'",
14627 class_key == union_type ? "union"
14628 : class_key == record_type ? "struct" : "class",
14632 /* Look for the `template' keyword, as a syntactic disambiguator.
14633 Return TRUE iff it is present, in which case it will be
14637 cp_parser_optional_template_keyword (cp_parser *parser)
14639 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
14641 /* The `template' keyword can only be used within templates;
14642 outside templates the parser can always figure out what is a
14643 template and what is not. */
14644 if (!processing_template_decl)
14646 error ("`template' (as a disambiguator) is only allowed "
14647 "within templates");
14648 /* If this part of the token stream is rescanned, the same
14649 error message would be generated. So, we purge the token
14650 from the stream. */
14651 cp_lexer_purge_token (parser->lexer);
14656 /* Consume the `template' keyword. */
14657 cp_lexer_consume_token (parser->lexer);
14665 /* Add tokens to CACHE until an non-nested END token appears. */
14668 cp_parser_cache_group (cp_parser *parser,
14669 cp_token_cache *cache,
14670 enum cpp_ttype end,
14677 /* Abort a parenthesized expression if we encounter a brace. */
14678 if ((end == CPP_CLOSE_PAREN || depth == 0)
14679 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
14681 /* Consume the next token. */
14682 token = cp_lexer_consume_token (parser->lexer);
14683 /* If we've reached the end of the file, stop. */
14684 if (token->type == CPP_EOF)
14686 /* Add this token to the tokens we are saving. */
14687 cp_token_cache_push_token (cache, token);
14688 /* See if it starts a new group. */
14689 if (token->type == CPP_OPEN_BRACE)
14691 cp_parser_cache_group (parser, cache, CPP_CLOSE_BRACE, depth + 1);
14695 else if (token->type == CPP_OPEN_PAREN)
14696 cp_parser_cache_group (parser, cache, CPP_CLOSE_PAREN, depth + 1);
14697 else if (token->type == end)
14702 /* Begin parsing tentatively. We always save tokens while parsing
14703 tentatively so that if the tentative parsing fails we can restore the
14707 cp_parser_parse_tentatively (parser)
14710 /* Enter a new parsing context. */
14711 parser->context = cp_parser_context_new (parser->context);
14712 /* Begin saving tokens. */
14713 cp_lexer_save_tokens (parser->lexer);
14714 /* In order to avoid repetitive access control error messages,
14715 access checks are queued up until we are no longer parsing
14717 cp_parser_start_deferring_access_checks (parser);
14720 /* Commit to the currently active tentative parse. */
14723 cp_parser_commit_to_tentative_parse (parser)
14726 cp_parser_context *context;
14729 /* Mark all of the levels as committed. */
14730 lexer = parser->lexer;
14731 for (context = parser->context; context->next; context = context->next)
14733 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
14735 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
14736 while (!cp_lexer_saving_tokens (lexer))
14737 lexer = lexer->next;
14738 cp_lexer_commit_tokens (lexer);
14742 /* Abort the currently active tentative parse. All consumed tokens
14743 will be rolled back, and no diagnostics will be issued. */
14746 cp_parser_abort_tentative_parse (parser)
14749 cp_parser_simulate_error (parser);
14750 /* Now, pretend that we want to see if the construct was
14751 successfully parsed. */
14752 cp_parser_parse_definitely (parser);
14755 /* Stop parsing tentatively. If a parse error has ocurred, restore the
14756 token stream. Otherwise, commit to the tokens we have consumed.
14757 Returns true if no error occurred; false otherwise. */
14760 cp_parser_parse_definitely (parser)
14763 bool error_occurred;
14764 cp_parser_context *context;
14766 /* Remember whether or not an error ocurred, since we are about to
14767 destroy that information. */
14768 error_occurred = cp_parser_error_occurred (parser);
14769 /* Remove the topmost context from the stack. */
14770 context = parser->context;
14771 parser->context = context->next;
14772 /* If no parse errors occurred, commit to the tentative parse. */
14773 if (!error_occurred)
14775 /* Commit to the tokens read tentatively, unless that was
14777 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
14778 cp_lexer_commit_tokens (parser->lexer);
14779 if (!parser->context->deferring_access_checks_p)
14780 /* If in the parent context we are not deferring checks, then
14781 these perform these checks now. */
14782 (cp_parser_perform_deferred_access_checks
14783 (context->deferred_access_checks));
14785 /* Any lookups that were deferred during the tentative parse are
14787 parser->context->deferred_access_checks
14788 = chainon (parser->context->deferred_access_checks,
14789 context->deferred_access_checks);
14791 /* Otherwise, if errors occurred, roll back our state so that things
14792 are just as they were before we began the tentative parse. */
14794 cp_lexer_rollback_tokens (parser->lexer);
14795 /* Add the context to the front of the free list. */
14796 context->next = cp_parser_context_free_list;
14797 cp_parser_context_free_list = context;
14799 return !error_occurred;
14802 /* Returns non-zero if we are parsing tentatively. */
14805 cp_parser_parsing_tentatively (parser)
14808 return parser->context->next != NULL;
14811 /* Returns true if we are parsing tentatively -- but have decided that
14812 we will stick with this tentative parse, even if errors occur. */
14815 cp_parser_committed_to_tentative_parse (parser)
14818 return (cp_parser_parsing_tentatively (parser)
14819 && parser->context->status == CP_PARSER_STATUS_KIND_COMMITTED);
14822 /* Returns non-zero iff an error has occurred during the most recent
14823 tentative parse. */
14826 cp_parser_error_occurred (parser)
14829 return (cp_parser_parsing_tentatively (parser)
14830 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
14833 /* Returns non-zero if GNU extensions are allowed. */
14836 cp_parser_allow_gnu_extensions_p (parser)
14839 return parser->allow_gnu_extensions_p;
14846 static GTY (()) cp_parser *the_parser;
14848 /* External interface. */
14850 /* Parse the entire translation unit. */
14855 bool error_occurred;
14857 the_parser = cp_parser_new ();
14858 error_occurred = cp_parser_translation_unit (the_parser);
14861 return error_occurred;
14864 /* Clean up after parsing the entire translation unit. */
14867 free_parser_stacks ()
14869 /* Nothing to do. */
14872 /* This variable must be provided by every front end. */
14876 #include "gt-cp-parser.h"