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 /* Constructors and destructors. */
1201 /* Construct a new context. The context below this one on the stack
1202 is given by NEXT. */
1204 static cp_parser_context *
1205 cp_parser_context_new (next)
1206 cp_parser_context *next;
1208 cp_parser_context *context;
1210 /* Allocate the storage. */
1211 context = ((cp_parser_context *)
1212 ggc_alloc_cleared (sizeof (cp_parser_context)));
1213 /* No errors have occurred yet in this context. */
1214 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1215 /* If this is not the bottomost context, copy information that we
1216 need from the previous context. */
1219 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1220 expression, then we are parsing one in this context, too. */
1221 context->object_type = next->object_type;
1222 /* We are deferring access checks here if we were in the NEXT
1224 context->deferring_access_checks_p
1225 = next->deferring_access_checks_p;
1226 /* Thread the stack. */
1227 context->next = next;
1233 /* The cp_parser structure represents the C++ parser. */
1235 typedef struct cp_parser GTY(())
1237 /* The lexer from which we are obtaining tokens. */
1240 /* The scope in which names should be looked up. If NULL_TREE, then
1241 we look up names in the scope that is currently open in the
1242 source program. If non-NULL, this is either a TYPE or
1243 NAMESPACE_DECL for the scope in which we should look.
1245 This value is not cleared automatically after a name is looked
1246 up, so we must be careful to clear it before starting a new look
1247 up sequence. (If it is not cleared, then `X::Y' followed by `Z'
1248 will look up `Z' in the scope of `X', rather than the current
1249 scope.) Unfortunately, it is difficult to tell when name lookup
1250 is complete, because we sometimes peek at a token, look it up,
1251 and then decide not to consume it. */
1254 /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1255 last lookup took place. OBJECT_SCOPE is used if an expression
1256 like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1257 respectively. QUALIFYING_SCOPE is used for an expression of the
1258 form "X::Y"; it refers to X. */
1260 tree qualifying_scope;
1262 /* A stack of parsing contexts. All but the bottom entry on the
1263 stack will be tentative contexts.
1265 We parse tentatively in order to determine which construct is in
1266 use in some situations. For example, in order to determine
1267 whether a statement is an expression-statement or a
1268 declaration-statement we parse it tentatively as a
1269 declaration-statement. If that fails, we then reparse the same
1270 token stream as an expression-statement. */
1271 cp_parser_context *context;
1273 /* True if we are parsing GNU C++. If this flag is not set, then
1274 GNU extensions are not recognized. */
1275 bool allow_gnu_extensions_p;
1277 /* TRUE if the `>' token should be interpreted as the greater-than
1278 operator. FALSE if it is the end of a template-id or
1279 template-parameter-list. */
1280 bool greater_than_is_operator_p;
1282 /* TRUE if default arguments are allowed within a parameter list
1283 that starts at this point. FALSE if only a gnu extension makes
1284 them permissable. */
1285 bool default_arg_ok_p;
1287 /* TRUE if we are parsing an integral constant-expression. See
1288 [expr.const] for a precise definition. */
1289 /* FIXME: Need to implement code that checks this flag. */
1290 bool constant_expression_p;
1292 /* TRUE if local variable names and `this' are forbidden in the
1294 bool local_variables_forbidden_p;
1296 /* TRUE if the declaration we are parsing is part of a
1297 linkage-specification of the form `extern string-literal
1299 bool in_unbraced_linkage_specification_p;
1301 /* TRUE if we are presently parsing a declarator, after the
1302 direct-declarator. */
1303 bool in_declarator_p;
1305 /* If non-NULL, then we are parsing a construct where new type
1306 definitions are not permitted. The string stored here will be
1307 issued as an error message if a type is defined. */
1308 const char *type_definition_forbidden_message;
1310 /* List of FUNCTION_TYPEs which contain unprocessed DEFAULT_ARGs
1311 during class parsing, and are not FUNCTION_DECLs. G++ has an
1312 awkward extension allowing default args on pointers to functions
1314 tree default_arg_types;
1316 /* A TREE_LIST of queues of functions whose bodies have been lexed,
1317 but may not have been parsed. These functions are friends of
1318 members defined within a class-specification; they are not
1319 procssed until the class is complete. The active queue is at the
1322 Within each queue, functions appear in the reverse order that
1323 they appeared in the source. The TREE_PURPOSE of each node is
1324 the class in which the function was defined or declared; the
1325 TREE_VALUE is the FUNCTION_DECL itself. */
1326 tree unparsed_functions_queues;
1328 /* The number of classes whose definitions are currently in
1330 unsigned num_classes_being_defined;
1332 /* The number of template parameter lists that apply directly to the
1333 current declaration. */
1334 unsigned num_template_parameter_lists;
1337 /* The type of a function that parses some kind of expression */
1338 typedef tree (*cp_parser_expression_fn) PARAMS ((cp_parser *));
1342 /* Constructors and destructors. */
1344 static cp_parser *cp_parser_new
1347 /* Routines to parse various constructs.
1349 Those that return `tree' will return the error_mark_node (rather
1350 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1351 Sometimes, they will return an ordinary node if error-recovery was
1352 attempted, even though a parse error occurrred. So, to check
1353 whether or not a parse error occurred, you should always use
1354 cp_parser_error_occurred. If the construct is optional (indicated
1355 either by an `_opt' in the name of the function that does the
1356 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1357 the construct is not present. */
1359 /* Lexical conventions [gram.lex] */
1361 static tree cp_parser_identifier
1362 PARAMS ((cp_parser *));
1364 /* Basic concepts [gram.basic] */
1366 static bool cp_parser_translation_unit
1367 PARAMS ((cp_parser *));
1369 /* Expressions [gram.expr] */
1371 static tree cp_parser_primary_expression
1372 (cp_parser *, cp_parser_id_kind *, tree *);
1373 static tree cp_parser_id_expression
1374 PARAMS ((cp_parser *, bool, bool, bool *));
1375 static tree cp_parser_unqualified_id
1376 PARAMS ((cp_parser *, bool, bool));
1377 static tree cp_parser_nested_name_specifier_opt
1378 (cp_parser *, bool, bool, bool);
1379 static tree cp_parser_nested_name_specifier
1380 (cp_parser *, bool, bool, bool);
1381 static tree cp_parser_class_or_namespace_name
1382 (cp_parser *, bool, bool, bool, bool);
1383 static tree cp_parser_postfix_expression
1384 (cp_parser *, bool);
1385 static tree cp_parser_expression_list
1386 PARAMS ((cp_parser *));
1387 static void cp_parser_pseudo_destructor_name
1388 PARAMS ((cp_parser *, tree *, tree *));
1389 static tree cp_parser_unary_expression
1390 (cp_parser *, bool);
1391 static enum tree_code cp_parser_unary_operator
1392 PARAMS ((cp_token *));
1393 static tree cp_parser_new_expression
1394 PARAMS ((cp_parser *));
1395 static tree cp_parser_new_placement
1396 PARAMS ((cp_parser *));
1397 static tree cp_parser_new_type_id
1398 PARAMS ((cp_parser *));
1399 static tree cp_parser_new_declarator_opt
1400 PARAMS ((cp_parser *));
1401 static tree cp_parser_direct_new_declarator
1402 PARAMS ((cp_parser *));
1403 static tree cp_parser_new_initializer
1404 PARAMS ((cp_parser *));
1405 static tree cp_parser_delete_expression
1406 PARAMS ((cp_parser *));
1407 static tree cp_parser_cast_expression
1408 (cp_parser *, bool);
1409 static tree cp_parser_pm_expression
1410 PARAMS ((cp_parser *));
1411 static tree cp_parser_multiplicative_expression
1412 PARAMS ((cp_parser *));
1413 static tree cp_parser_additive_expression
1414 PARAMS ((cp_parser *));
1415 static tree cp_parser_shift_expression
1416 PARAMS ((cp_parser *));
1417 static tree cp_parser_relational_expression
1418 PARAMS ((cp_parser *));
1419 static tree cp_parser_equality_expression
1420 PARAMS ((cp_parser *));
1421 static tree cp_parser_and_expression
1422 PARAMS ((cp_parser *));
1423 static tree cp_parser_exclusive_or_expression
1424 PARAMS ((cp_parser *));
1425 static tree cp_parser_inclusive_or_expression
1426 PARAMS ((cp_parser *));
1427 static tree cp_parser_logical_and_expression
1428 PARAMS ((cp_parser *));
1429 static tree cp_parser_logical_or_expression
1430 PARAMS ((cp_parser *));
1431 static tree cp_parser_conditional_expression
1432 PARAMS ((cp_parser *));
1433 static tree cp_parser_question_colon_clause
1434 PARAMS ((cp_parser *, tree));
1435 static tree cp_parser_assignment_expression
1436 PARAMS ((cp_parser *));
1437 static enum tree_code cp_parser_assignment_operator_opt
1438 PARAMS ((cp_parser *));
1439 static tree cp_parser_expression
1440 PARAMS ((cp_parser *));
1441 static tree cp_parser_constant_expression
1442 PARAMS ((cp_parser *));
1444 /* Statements [gram.stmt.stmt] */
1446 static void cp_parser_statement
1447 PARAMS ((cp_parser *));
1448 static tree cp_parser_labeled_statement
1449 PARAMS ((cp_parser *));
1450 static tree cp_parser_expression_statement
1451 PARAMS ((cp_parser *));
1452 static tree cp_parser_compound_statement
1454 static void cp_parser_statement_seq_opt
1455 PARAMS ((cp_parser *));
1456 static tree cp_parser_selection_statement
1457 PARAMS ((cp_parser *));
1458 static tree cp_parser_condition
1459 PARAMS ((cp_parser *));
1460 static tree cp_parser_iteration_statement
1461 PARAMS ((cp_parser *));
1462 static void cp_parser_for_init_statement
1463 PARAMS ((cp_parser *));
1464 static tree cp_parser_jump_statement
1465 PARAMS ((cp_parser *));
1466 static void cp_parser_declaration_statement
1467 PARAMS ((cp_parser *));
1469 static tree cp_parser_implicitly_scoped_statement
1470 PARAMS ((cp_parser *));
1471 static void cp_parser_already_scoped_statement
1472 PARAMS ((cp_parser *));
1474 /* Declarations [gram.dcl.dcl] */
1476 static void cp_parser_declaration_seq_opt
1477 PARAMS ((cp_parser *));
1478 static void cp_parser_declaration
1479 PARAMS ((cp_parser *));
1480 static void cp_parser_block_declaration
1481 PARAMS ((cp_parser *, bool));
1482 static void cp_parser_simple_declaration
1483 PARAMS ((cp_parser *, bool));
1484 static tree cp_parser_decl_specifier_seq
1485 PARAMS ((cp_parser *, cp_parser_flags, tree *, bool *));
1486 static tree cp_parser_storage_class_specifier_opt
1487 PARAMS ((cp_parser *));
1488 static tree cp_parser_function_specifier_opt
1489 PARAMS ((cp_parser *));
1490 static tree cp_parser_type_specifier
1491 (cp_parser *, cp_parser_flags, bool, bool, bool *, bool *);
1492 static tree cp_parser_simple_type_specifier
1493 PARAMS ((cp_parser *, cp_parser_flags));
1494 static tree cp_parser_type_name
1495 PARAMS ((cp_parser *));
1496 static tree cp_parser_elaborated_type_specifier
1497 PARAMS ((cp_parser *, bool, bool));
1498 static tree cp_parser_enum_specifier
1499 PARAMS ((cp_parser *));
1500 static void cp_parser_enumerator_list
1501 PARAMS ((cp_parser *, tree));
1502 static void cp_parser_enumerator_definition
1503 PARAMS ((cp_parser *, tree));
1504 static tree cp_parser_namespace_name
1505 PARAMS ((cp_parser *));
1506 static void cp_parser_namespace_definition
1507 PARAMS ((cp_parser *));
1508 static void cp_parser_namespace_body
1509 PARAMS ((cp_parser *));
1510 static tree cp_parser_qualified_namespace_specifier
1511 PARAMS ((cp_parser *));
1512 static void cp_parser_namespace_alias_definition
1513 PARAMS ((cp_parser *));
1514 static void cp_parser_using_declaration
1515 PARAMS ((cp_parser *));
1516 static void cp_parser_using_directive
1517 PARAMS ((cp_parser *));
1518 static void cp_parser_asm_definition
1519 PARAMS ((cp_parser *));
1520 static void cp_parser_linkage_specification
1521 PARAMS ((cp_parser *));
1523 /* Declarators [gram.dcl.decl] */
1525 static tree cp_parser_init_declarator
1526 PARAMS ((cp_parser *, tree, tree, tree, bool, bool, bool *));
1527 static tree cp_parser_declarator
1528 PARAMS ((cp_parser *, bool, bool *));
1529 static tree cp_parser_direct_declarator
1530 PARAMS ((cp_parser *, bool, bool *));
1531 static enum tree_code cp_parser_ptr_operator
1532 PARAMS ((cp_parser *, tree *, tree *));
1533 static tree cp_parser_cv_qualifier_seq_opt
1534 PARAMS ((cp_parser *));
1535 static tree cp_parser_cv_qualifier_opt
1536 PARAMS ((cp_parser *));
1537 static tree cp_parser_declarator_id
1538 PARAMS ((cp_parser *));
1539 static tree cp_parser_type_id
1540 PARAMS ((cp_parser *));
1541 static tree cp_parser_type_specifier_seq
1542 PARAMS ((cp_parser *));
1543 static tree cp_parser_parameter_declaration_clause
1544 PARAMS ((cp_parser *));
1545 static tree cp_parser_parameter_declaration_list
1546 PARAMS ((cp_parser *));
1547 static tree cp_parser_parameter_declaration
1548 PARAMS ((cp_parser *, bool));
1549 static tree cp_parser_function_definition
1550 PARAMS ((cp_parser *, bool *));
1551 static void cp_parser_function_body
1553 static tree cp_parser_initializer
1554 PARAMS ((cp_parser *, bool *));
1555 static tree cp_parser_initializer_clause
1556 PARAMS ((cp_parser *));
1557 static tree cp_parser_initializer_list
1558 PARAMS ((cp_parser *));
1560 static bool cp_parser_ctor_initializer_opt_and_function_body
1563 /* Classes [gram.class] */
1565 static tree cp_parser_class_name
1566 (cp_parser *, bool, bool, bool, bool, bool, bool);
1567 static tree cp_parser_class_specifier
1568 PARAMS ((cp_parser *));
1569 static tree cp_parser_class_head
1570 PARAMS ((cp_parser *, bool *, bool *, tree *));
1571 static enum tag_types cp_parser_class_key
1572 PARAMS ((cp_parser *));
1573 static void cp_parser_member_specification_opt
1574 PARAMS ((cp_parser *));
1575 static void cp_parser_member_declaration
1576 PARAMS ((cp_parser *));
1577 static tree cp_parser_pure_specifier
1578 PARAMS ((cp_parser *));
1579 static tree cp_parser_constant_initializer
1580 PARAMS ((cp_parser *));
1582 /* Derived classes [gram.class.derived] */
1584 static tree cp_parser_base_clause
1585 PARAMS ((cp_parser *));
1586 static tree cp_parser_base_specifier
1587 PARAMS ((cp_parser *));
1589 /* Special member functions [gram.special] */
1591 static tree cp_parser_conversion_function_id
1592 PARAMS ((cp_parser *));
1593 static tree cp_parser_conversion_type_id
1594 PARAMS ((cp_parser *));
1595 static tree cp_parser_conversion_declarator_opt
1596 PARAMS ((cp_parser *));
1597 static bool cp_parser_ctor_initializer_opt
1598 PARAMS ((cp_parser *));
1599 static void cp_parser_mem_initializer_list
1600 PARAMS ((cp_parser *));
1601 static tree cp_parser_mem_initializer
1602 PARAMS ((cp_parser *));
1603 static tree cp_parser_mem_initializer_id
1604 PARAMS ((cp_parser *));
1606 /* Overloading [gram.over] */
1608 static tree cp_parser_operator_function_id
1609 PARAMS ((cp_parser *));
1610 static tree cp_parser_operator
1611 PARAMS ((cp_parser *));
1613 /* Templates [gram.temp] */
1615 static void cp_parser_template_declaration
1616 PARAMS ((cp_parser *, bool));
1617 static tree cp_parser_template_parameter_list
1618 PARAMS ((cp_parser *));
1619 static tree cp_parser_template_parameter
1620 PARAMS ((cp_parser *));
1621 static tree cp_parser_type_parameter
1622 PARAMS ((cp_parser *));
1623 static tree cp_parser_template_id
1624 PARAMS ((cp_parser *, bool, bool));
1625 static tree cp_parser_template_name
1626 PARAMS ((cp_parser *, bool, bool));
1627 static tree cp_parser_template_argument_list
1628 PARAMS ((cp_parser *));
1629 static tree cp_parser_template_argument
1630 PARAMS ((cp_parser *));
1631 static void cp_parser_explicit_instantiation
1632 PARAMS ((cp_parser *));
1633 static void cp_parser_explicit_specialization
1634 PARAMS ((cp_parser *));
1636 /* Exception handling [gram.exception] */
1638 static tree cp_parser_try_block
1639 PARAMS ((cp_parser *));
1640 static bool cp_parser_function_try_block
1641 PARAMS ((cp_parser *));
1642 static void cp_parser_handler_seq
1643 PARAMS ((cp_parser *));
1644 static void cp_parser_handler
1645 PARAMS ((cp_parser *));
1646 static tree cp_parser_exception_declaration
1647 PARAMS ((cp_parser *));
1648 static tree cp_parser_throw_expression
1649 PARAMS ((cp_parser *));
1650 static tree cp_parser_exception_specification_opt
1651 PARAMS ((cp_parser *));
1652 static tree cp_parser_type_id_list
1653 PARAMS ((cp_parser *));
1655 /* GNU Extensions */
1657 static tree cp_parser_asm_specification_opt
1658 PARAMS ((cp_parser *));
1659 static tree cp_parser_asm_operand_list
1660 PARAMS ((cp_parser *));
1661 static tree cp_parser_asm_clobber_list
1662 PARAMS ((cp_parser *));
1663 static tree cp_parser_attributes_opt
1664 PARAMS ((cp_parser *));
1665 static tree cp_parser_attribute_list
1666 PARAMS ((cp_parser *));
1667 static bool cp_parser_extension_opt
1668 PARAMS ((cp_parser *, int *));
1669 static void cp_parser_label_declaration
1670 PARAMS ((cp_parser *));
1672 /* Utility Routines */
1674 static tree cp_parser_lookup_name
1675 PARAMS ((cp_parser *, tree, bool, bool, bool));
1676 static tree cp_parser_lookup_name_simple
1677 PARAMS ((cp_parser *, tree));
1678 static tree cp_parser_resolve_typename_type
1679 PARAMS ((cp_parser *, tree));
1680 static tree cp_parser_maybe_treat_template_as_class
1682 static bool cp_parser_check_declarator_template_parameters
1683 PARAMS ((cp_parser *, tree));
1684 static bool cp_parser_check_template_parameters
1685 PARAMS ((cp_parser *, unsigned));
1686 static tree cp_parser_binary_expression
1687 PARAMS ((cp_parser *,
1688 cp_parser_token_tree_map,
1689 cp_parser_expression_fn));
1690 static tree cp_parser_global_scope_opt
1691 PARAMS ((cp_parser *, bool));
1692 static bool cp_parser_constructor_declarator_p
1693 (cp_parser *, bool);
1694 static tree cp_parser_function_definition_from_specifiers_and_declarator
1695 PARAMS ((cp_parser *, tree, tree, tree, tree));
1696 static tree cp_parser_function_definition_after_declarator
1697 PARAMS ((cp_parser *, bool));
1698 static void cp_parser_template_declaration_after_export
1699 PARAMS ((cp_parser *, bool));
1700 static tree cp_parser_single_declaration
1701 PARAMS ((cp_parser *, bool, bool *));
1702 static tree cp_parser_functional_cast
1703 PARAMS ((cp_parser *, tree));
1704 static void cp_parser_late_parsing_for_member
1705 PARAMS ((cp_parser *, tree));
1706 static void cp_parser_late_parsing_default_args
1707 PARAMS ((cp_parser *, tree));
1708 static tree cp_parser_sizeof_operand
1709 PARAMS ((cp_parser *, enum rid));
1710 static bool cp_parser_declares_only_class_p
1711 PARAMS ((cp_parser *));
1712 static bool cp_parser_friend_p
1714 static cp_token *cp_parser_require
1715 PARAMS ((cp_parser *, enum cpp_ttype, const char *));
1716 static cp_token *cp_parser_require_keyword
1717 PARAMS ((cp_parser *, enum rid, const char *));
1718 static bool cp_parser_token_starts_function_definition_p
1719 PARAMS ((cp_token *));
1720 static bool cp_parser_next_token_starts_class_definition_p
1722 static enum tag_types cp_parser_token_is_class_key
1723 PARAMS ((cp_token *));
1724 static void cp_parser_check_class_key
1725 (enum tag_types, tree type);
1726 static bool cp_parser_optional_template_keyword
1728 static void cp_parser_cache_group
1729 (cp_parser *, cp_token_cache *, enum cpp_ttype, unsigned);
1730 static void cp_parser_parse_tentatively
1731 PARAMS ((cp_parser *));
1732 static void cp_parser_commit_to_tentative_parse
1733 PARAMS ((cp_parser *));
1734 static void cp_parser_abort_tentative_parse
1735 PARAMS ((cp_parser *));
1736 static bool cp_parser_parse_definitely
1737 PARAMS ((cp_parser *));
1738 static bool cp_parser_parsing_tentatively
1739 PARAMS ((cp_parser *));
1740 static bool cp_parser_committed_to_tentative_parse
1741 PARAMS ((cp_parser *));
1742 static void cp_parser_error
1743 PARAMS ((cp_parser *, const char *));
1744 static void cp_parser_simulate_error
1745 PARAMS ((cp_parser *));
1746 static void cp_parser_check_type_definition
1747 PARAMS ((cp_parser *));
1748 static bool cp_parser_skip_to_closing_parenthesis
1749 PARAMS ((cp_parser *));
1750 static bool cp_parser_skip_to_closing_parenthesis_or_comma
1752 static void cp_parser_skip_to_end_of_statement
1753 PARAMS ((cp_parser *));
1754 static void cp_parser_skip_to_end_of_block_or_statement
1755 PARAMS ((cp_parser *));
1756 static void cp_parser_skip_to_closing_brace
1758 static void cp_parser_skip_until_found
1759 PARAMS ((cp_parser *, enum cpp_ttype, const char *));
1760 static bool cp_parser_error_occurred
1761 PARAMS ((cp_parser *));
1762 static bool cp_parser_allow_gnu_extensions_p
1763 PARAMS ((cp_parser *));
1764 static bool cp_parser_is_string_literal
1765 PARAMS ((cp_token *));
1766 static bool cp_parser_is_keyword
1767 PARAMS ((cp_token *, enum rid));
1768 static bool cp_parser_dependent_type_p
1770 static bool cp_parser_value_dependent_expression_p
1772 static bool cp_parser_type_dependent_expression_p
1774 static bool cp_parser_dependent_template_arg_p
1776 static bool cp_parser_dependent_template_id_p
1778 static bool cp_parser_dependent_template_p
1780 static void cp_parser_defer_access_check
1781 (cp_parser *, tree, tree);
1782 static void cp_parser_start_deferring_access_checks
1784 static tree cp_parser_stop_deferring_access_checks
1785 PARAMS ((cp_parser *));
1786 static void cp_parser_perform_deferred_access_checks
1788 static tree cp_parser_scope_through_which_access_occurs
1791 /* Returns non-zero if TOKEN is a string literal. */
1794 cp_parser_is_string_literal (token)
1797 return (token->type == CPP_STRING || token->type == CPP_WSTRING);
1800 /* Returns non-zero if TOKEN is the indicated KEYWORD. */
1803 cp_parser_is_keyword (token, keyword)
1807 return token->keyword == keyword;
1810 /* Returns TRUE if TYPE is dependent, in the sense of
1814 cp_parser_dependent_type_p (type)
1819 if (!processing_template_decl)
1822 /* If the type is NULL, we have not computed a type for the entity
1823 in question; in that case, the type is dependent. */
1827 /* Erroneous types can be considered non-dependent. */
1828 if (type == error_mark_node)
1833 A type is dependent if it is:
1835 -- a template parameter. */
1836 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
1838 /* -- a qualified-id with a nested-name-specifier which contains a
1839 class-name that names a dependent type or whose unqualified-id
1840 names a dependent type. */
1841 if (TREE_CODE (type) == TYPENAME_TYPE)
1843 /* -- a cv-qualified type where the cv-unqualified type is
1845 type = TYPE_MAIN_VARIANT (type);
1846 /* -- a compound type constructed from any dependent type. */
1847 if (TYPE_PTRMEM_P (type) || TYPE_PTRMEMFUNC_P (type))
1848 return (cp_parser_dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type))
1849 || cp_parser_dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
1851 else if (TREE_CODE (type) == POINTER_TYPE
1852 || TREE_CODE (type) == REFERENCE_TYPE)
1853 return cp_parser_dependent_type_p (TREE_TYPE (type));
1854 else if (TREE_CODE (type) == FUNCTION_TYPE
1855 || TREE_CODE (type) == METHOD_TYPE)
1859 if (cp_parser_dependent_type_p (TREE_TYPE (type)))
1861 for (arg_type = TYPE_ARG_TYPES (type);
1863 arg_type = TREE_CHAIN (arg_type))
1864 if (cp_parser_dependent_type_p (TREE_VALUE (arg_type)))
1868 /* -- an array type constructed from any dependent type or whose
1869 size is specified by a constant expression that is
1871 if (TREE_CODE (type) == ARRAY_TYPE)
1873 if (TYPE_DOMAIN (TREE_TYPE (type))
1874 && ((cp_parser_value_dependent_expression_p
1875 (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
1876 || (cp_parser_type_dependent_expression_p
1877 (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))))
1879 return cp_parser_dependent_type_p (TREE_TYPE (type));
1881 /* -- a template-id in which either the template name is a template
1882 parameter or any of the template arguments is a dependent type or
1883 an expression that is type-dependent or value-dependent.
1885 This language seems somewhat confused; for example, it does not
1886 discuss template template arguments. Therefore, we use the
1887 definition for dependent template arguments in [temp.dep.temp]. */
1888 if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INFO (type)
1889 && (cp_parser_dependent_template_id_p
1890 (CLASSTYPE_TI_TEMPLATE (type),
1891 CLASSTYPE_TI_ARGS (type))))
1893 else if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
1895 /* All TYPEOF_TYPEs are dependent; if the argument of the `typeof'
1896 expression is not type-dependent, then it should already been
1898 if (TREE_CODE (type) == TYPEOF_TYPE)
1900 /* The standard does not specifically mention types that are local
1901 to template functions or local classes, but they should be
1902 considered dependent too. For example:
1904 template <int I> void f() {
1909 The size of `E' cannot be known until the value of `I' has been
1910 determined. Therefore, `E' must be considered dependent. */
1911 scope = TYPE_CONTEXT (type);
1912 if (scope && TYPE_P (scope))
1913 return cp_parser_dependent_type_p (scope);
1914 else if (scope && TREE_CODE (scope) == FUNCTION_DECL)
1915 return cp_parser_type_dependent_expression_p (scope);
1917 /* Other types are non-dependent. */
1921 /* Returns TRUE if the EXPRESSION is value-dependent. */
1924 cp_parser_value_dependent_expression_p (tree expression)
1926 if (!processing_template_decl)
1929 /* A name declared with a dependent type. */
1930 if (DECL_P (expression)
1931 && cp_parser_dependent_type_p (TREE_TYPE (expression)))
1933 /* A non-type template parameter. */
1934 if ((TREE_CODE (expression) == CONST_DECL
1935 && DECL_TEMPLATE_PARM_P (expression))
1936 || TREE_CODE (expression) == TEMPLATE_PARM_INDEX)
1938 /* A constant with integral or enumeration type and is initialized
1939 with an expression that is value-dependent. */
1940 if (TREE_CODE (expression) == VAR_DECL
1941 && DECL_INITIAL (expression)
1942 && (CP_INTEGRAL_TYPE_P (TREE_TYPE (expression))
1943 || TREE_CODE (TREE_TYPE (expression)) == ENUMERAL_TYPE)
1944 && cp_parser_value_dependent_expression_p (DECL_INITIAL (expression)))
1946 /* These expressions are value-dependent if the type to which the
1947 cast occurs is dependent. */
1948 if ((TREE_CODE (expression) == DYNAMIC_CAST_EXPR
1949 || TREE_CODE (expression) == STATIC_CAST_EXPR
1950 || TREE_CODE (expression) == CONST_CAST_EXPR
1951 || TREE_CODE (expression) == REINTERPRET_CAST_EXPR
1952 || TREE_CODE (expression) == CAST_EXPR)
1953 && cp_parser_dependent_type_p (TREE_TYPE (expression)))
1955 /* A `sizeof' expression where the sizeof operand is a type is
1956 value-dependent if the type is dependent. If the type was not
1957 dependent, we would no longer have a SIZEOF_EXPR, so any
1958 SIZEOF_EXPR is dependent. */
1959 if (TREE_CODE (expression) == SIZEOF_EXPR)
1961 /* A constant expression is value-dependent if any subexpression is
1963 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (expression))))
1965 switch (TREE_CODE_CLASS (TREE_CODE (expression)))
1968 return (cp_parser_value_dependent_expression_p
1969 (TREE_OPERAND (expression, 0)));
1972 return ((cp_parser_value_dependent_expression_p
1973 (TREE_OPERAND (expression, 0)))
1974 || (cp_parser_value_dependent_expression_p
1975 (TREE_OPERAND (expression, 1))));
1980 i < TREE_CODE_LENGTH (TREE_CODE (expression));
1982 if (cp_parser_value_dependent_expression_p
1983 (TREE_OPERAND (expression, i)))
1990 /* The expression is not value-dependent. */
1994 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
1998 cp_parser_type_dependent_expression_p (expression)
2001 if (!processing_template_decl)
2004 /* Some expression forms are never type-dependent. */
2005 if (TREE_CODE (expression) == PSEUDO_DTOR_EXPR
2006 || TREE_CODE (expression) == SIZEOF_EXPR
2007 || TREE_CODE (expression) == ALIGNOF_EXPR
2008 || TREE_CODE (expression) == TYPEID_EXPR
2009 || TREE_CODE (expression) == DELETE_EXPR
2010 || TREE_CODE (expression) == VEC_DELETE_EXPR
2011 || TREE_CODE (expression) == THROW_EXPR)
2014 /* The types of these expressions depends only on the type to which
2016 if (TREE_CODE (expression) == DYNAMIC_CAST_EXPR
2017 || TREE_CODE (expression) == STATIC_CAST_EXPR
2018 || TREE_CODE (expression) == CONST_CAST_EXPR
2019 || TREE_CODE (expression) == REINTERPRET_CAST_EXPR
2020 || TREE_CODE (expression) == CAST_EXPR)
2021 return cp_parser_dependent_type_p (TREE_TYPE (expression));
2022 /* The types of these expressions depends only on the type created
2023 by the expression. */
2024 else if (TREE_CODE (expression) == NEW_EXPR
2025 || TREE_CODE (expression) == VEC_NEW_EXPR)
2026 return cp_parser_dependent_type_p (TREE_OPERAND (expression, 1));
2028 if (TREE_CODE (expression) == FUNCTION_DECL
2029 && DECL_LANG_SPECIFIC (expression)
2030 && DECL_TEMPLATE_INFO (expression)
2031 && (cp_parser_dependent_template_id_p
2032 (DECL_TI_TEMPLATE (expression),
2033 INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression)))))
2036 return (cp_parser_dependent_type_p (TREE_TYPE (expression)));
2039 /* Returns TRUE if the ARG (a template argument) is dependent. */
2042 cp_parser_dependent_template_arg_p (tree arg)
2044 if (!processing_template_decl)
2047 if (TREE_CODE (arg) == TEMPLATE_DECL
2048 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
2049 return cp_parser_dependent_template_p (arg);
2050 else if (TYPE_P (arg))
2051 return cp_parser_dependent_type_p (arg);
2053 return (cp_parser_type_dependent_expression_p (arg)
2054 || cp_parser_value_dependent_expression_p (arg));
2057 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
2060 cp_parser_dependent_template_id_p (tree tmpl, tree args)
2064 if (cp_parser_dependent_template_p (tmpl))
2066 for (i = 0; i < TREE_VEC_LENGTH (args); ++i)
2067 if (cp_parser_dependent_template_arg_p (TREE_VEC_ELT (args, i)))
2072 /* Returns TRUE if the template TMPL is dependent. */
2075 cp_parser_dependent_template_p (tree tmpl)
2077 /* Template template parameters are dependent. */
2078 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)
2079 || TREE_CODE (tmpl) == TEMPLATE_TEMPLATE_PARM)
2081 /* So are member templates of dependent classes. */
2082 if (TYPE_P (CP_DECL_CONTEXT (tmpl)))
2083 return cp_parser_dependent_type_p (DECL_CONTEXT (tmpl));
2087 /* Defer checking the accessibility of DECL, when looked up in
2091 cp_parser_defer_access_check (cp_parser *parser,
2097 /* If we are not supposed to defer access checks, just check now. */
2098 if (!parser->context->deferring_access_checks_p)
2100 enforce_access (class_type, decl);
2104 /* See if we are already going to perform this check. */
2105 for (check = parser->context->deferred_access_checks;
2107 check = TREE_CHAIN (check))
2108 if (TREE_VALUE (check) == decl
2109 && same_type_p (TREE_PURPOSE (check), class_type))
2111 /* If not, record the check. */
2112 parser->context->deferred_access_checks
2113 = tree_cons (class_type, decl, parser->context->deferred_access_checks);
2116 /* Start deferring access control checks. */
2119 cp_parser_start_deferring_access_checks (cp_parser *parser)
2121 parser->context->deferring_access_checks_p = true;
2124 /* Stop deferring access control checks. Returns a TREE_LIST
2125 representing the deferred checks. The TREE_PURPOSE of each node is
2126 the type through which the access occurred; the TREE_VALUE is the
2127 declaration named. */
2130 cp_parser_stop_deferring_access_checks (parser)
2135 parser->context->deferring_access_checks_p = false;
2136 access_checks = parser->context->deferred_access_checks;
2137 parser->context->deferred_access_checks = NULL_TREE;
2139 return access_checks;
2142 /* Perform the deferred ACCESS_CHECKS, whose representation is as
2143 documented with cp_parser_stop_deferrring_access_checks. */
2146 cp_parser_perform_deferred_access_checks (access_checks)
2149 tree deferred_check;
2151 /* Look through all the deferred checks. */
2152 for (deferred_check = access_checks;
2154 deferred_check = TREE_CHAIN (deferred_check))
2156 enforce_access (TREE_PURPOSE (deferred_check),
2157 TREE_VALUE (deferred_check));
2160 /* Returns the scope through which DECL is being accessed, or
2161 NULL_TREE if DECL is not a member. If OBJECT_TYPE is non-NULL, we
2162 have just seen `x->' or `x.' and OBJECT_TYPE is the type of `*x',
2163 or `x', respectively. If the DECL was named as `A::B' then
2164 NESTED_NAME_SPECIFIER is `A'. */
2167 cp_parser_scope_through_which_access_occurs (decl,
2169 nested_name_specifier)
2172 tree nested_name_specifier;
2175 tree qualifying_type = NULL_TREE;
2177 /* Determine the SCOPE of DECL. */
2178 scope = context_for_name_lookup (decl);
2179 /* If the SCOPE is not a type, then DECL is not a member. */
2180 if (!TYPE_P (scope))
2182 /* Figure out the type through which DECL is being accessed. */
2183 if (object_type && DERIVED_FROM_P (scope, object_type))
2184 /* If we are processing a `->' or `.' expression, use the type of the
2186 qualifying_type = object_type;
2187 else if (nested_name_specifier)
2189 /* If the reference is to a non-static member of the
2190 current class, treat it as if it were referenced through
2192 if (DECL_NONSTATIC_MEMBER_P (decl)
2193 && current_class_ptr
2194 && DERIVED_FROM_P (scope, current_class_type))
2195 qualifying_type = current_class_type;
2196 /* Otherwise, use the type indicated by the
2197 nested-name-specifier. */
2199 qualifying_type = nested_name_specifier;
2202 /* Otherwise, the name must be from the current class or one of
2204 qualifying_type = currently_open_derived_class (scope);
2206 return qualifying_type;
2209 /* Issue the indicated error MESSAGE. */
2212 cp_parser_error (parser, message)
2214 const char *message;
2216 /* Remember that we have issued an error. */
2217 cp_parser_simulate_error (parser);
2218 /* Output the MESSAGE -- unless we're parsing tentatively. */
2219 if (!cp_parser_parsing_tentatively (parser)
2220 || cp_parser_committed_to_tentative_parse (parser))
2224 /* If we are parsing tentatively, remember that an error has occurred
2225 during this tentative parse. */
2228 cp_parser_simulate_error (parser)
2231 if (cp_parser_parsing_tentatively (parser)
2232 && !cp_parser_committed_to_tentative_parse (parser))
2233 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2236 /* This function is called when a type is defined. If type
2237 definitions are forbidden at this point, an error message is
2241 cp_parser_check_type_definition (parser)
2244 /* If types are forbidden here, issue a message. */
2245 if (parser->type_definition_forbidden_message)
2246 /* Use `%s' to print the string in case there are any escape
2247 characters in the message. */
2248 error ("%s", parser->type_definition_forbidden_message);
2251 /* Consume tokens up to, and including, the next non-nested closing `)'.
2252 Returns TRUE iff we found a closing `)'. */
2255 cp_parser_skip_to_closing_parenthesis (cp_parser *parser)
2257 unsigned nesting_depth = 0;
2263 /* If we've run out of tokens, then there is no closing `)'. */
2264 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2266 /* Consume the token. */
2267 token = cp_lexer_consume_token (parser->lexer);
2268 /* If it is an `(', we have entered another level of nesting. */
2269 if (token->type == CPP_OPEN_PAREN)
2271 /* If it is a `)', then we might be done. */
2272 else if (token->type == CPP_CLOSE_PAREN && nesting_depth-- == 0)
2277 /* Consume tokens until the next token is a `)', or a `,'. Returns
2278 TRUE if the next token is a `,'. */
2281 cp_parser_skip_to_closing_parenthesis_or_comma (cp_parser *parser)
2283 unsigned nesting_depth = 0;
2287 cp_token *token = cp_lexer_peek_token (parser->lexer);
2289 /* If we've run out of tokens, then there is no closing `)'. */
2290 if (token->type == CPP_EOF)
2292 /* If it is a `,' stop. */
2293 else if (token->type == CPP_COMMA && nesting_depth-- == 0)
2295 /* If it is a `)', stop. */
2296 else if (token->type == CPP_CLOSE_PAREN && nesting_depth-- == 0)
2298 /* If it is an `(', we have entered another level of nesting. */
2299 else if (token->type == CPP_OPEN_PAREN)
2301 /* Consume the token. */
2302 token = cp_lexer_consume_token (parser->lexer);
2306 /* Consume tokens until we reach the end of the current statement.
2307 Normally, that will be just before consuming a `;'. However, if a
2308 non-nested `}' comes first, then we stop before consuming that. */
2311 cp_parser_skip_to_end_of_statement (parser)
2314 unsigned nesting_depth = 0;
2320 /* Peek at the next token. */
2321 token = cp_lexer_peek_token (parser->lexer);
2322 /* If we've run out of tokens, stop. */
2323 if (token->type == CPP_EOF)
2325 /* If the next token is a `;', we have reached the end of the
2327 if (token->type == CPP_SEMICOLON && !nesting_depth)
2329 /* If the next token is a non-nested `}', then we have reached
2330 the end of the current block. */
2331 if (token->type == CPP_CLOSE_BRACE)
2333 /* If this is a non-nested `}', stop before consuming it.
2334 That way, when confronted with something like:
2338 we stop before consuming the closing `}', even though we
2339 have not yet reached a `;'. */
2340 if (nesting_depth == 0)
2342 /* If it is the closing `}' for a block that we have
2343 scanned, stop -- but only after consuming the token.
2349 we will stop after the body of the erroneously declared
2350 function, but before consuming the following `typedef'
2352 if (--nesting_depth == 0)
2354 cp_lexer_consume_token (parser->lexer);
2358 /* If it the next token is a `{', then we are entering a new
2359 block. Consume the entire block. */
2360 else if (token->type == CPP_OPEN_BRACE)
2362 /* Consume the token. */
2363 cp_lexer_consume_token (parser->lexer);
2367 /* Skip tokens until we have consumed an entire block, or until we
2368 have consumed a non-nested `;'. */
2371 cp_parser_skip_to_end_of_block_or_statement (parser)
2374 unsigned nesting_depth = 0;
2380 /* Peek at the next token. */
2381 token = cp_lexer_peek_token (parser->lexer);
2382 /* If we've run out of tokens, stop. */
2383 if (token->type == CPP_EOF)
2385 /* If the next token is a `;', we have reached the end of the
2387 if (token->type == CPP_SEMICOLON && !nesting_depth)
2389 /* Consume the `;'. */
2390 cp_lexer_consume_token (parser->lexer);
2393 /* Consume the token. */
2394 token = cp_lexer_consume_token (parser->lexer);
2395 /* If the next token is a non-nested `}', then we have reached
2396 the end of the current block. */
2397 if (token->type == CPP_CLOSE_BRACE
2398 && (nesting_depth == 0 || --nesting_depth == 0))
2400 /* If it the next token is a `{', then we are entering a new
2401 block. Consume the entire block. */
2402 if (token->type == CPP_OPEN_BRACE)
2407 /* Skip tokens until a non-nested closing curly brace is the next
2411 cp_parser_skip_to_closing_brace (cp_parser *parser)
2413 unsigned nesting_depth = 0;
2419 /* Peek at the next token. */
2420 token = cp_lexer_peek_token (parser->lexer);
2421 /* If we've run out of tokens, stop. */
2422 if (token->type == CPP_EOF)
2424 /* If the next token is a non-nested `}', then we have reached
2425 the end of the current block. */
2426 if (token->type == CPP_CLOSE_BRACE && nesting_depth-- == 0)
2428 /* If it the next token is a `{', then we are entering a new
2429 block. Consume the entire block. */
2430 else if (token->type == CPP_OPEN_BRACE)
2432 /* Consume the token. */
2433 cp_lexer_consume_token (parser->lexer);
2437 /* Create a new C++ parser. */
2444 parser = (cp_parser *) ggc_alloc_cleared (sizeof (cp_parser));
2445 parser->lexer = cp_lexer_new (/*main_lexer_p=*/true);
2446 parser->context = cp_parser_context_new (NULL);
2448 /* For now, we always accept GNU extensions. */
2449 parser->allow_gnu_extensions_p = 1;
2451 /* The `>' token is a greater-than operator, not the end of a
2453 parser->greater_than_is_operator_p = true;
2455 parser->default_arg_ok_p = true;
2457 /* We are not parsing a constant-expression. */
2458 parser->constant_expression_p = false;
2460 /* Local variable names are not forbidden. */
2461 parser->local_variables_forbidden_p = false;
2463 /* We are not procesing an `extern "C"' declaration. */
2464 parser->in_unbraced_linkage_specification_p = false;
2466 /* We are not processing a declarator. */
2467 parser->in_declarator_p = false;
2469 /* There are no default args to process. */
2470 parser->default_arg_types = NULL;
2472 /* The unparsed function queue is empty. */
2473 parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2475 /* There are no classes being defined. */
2476 parser->num_classes_being_defined = 0;
2478 /* No template parameters apply. */
2479 parser->num_template_parameter_lists = 0;
2484 /* Lexical conventions [gram.lex] */
2486 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
2490 cp_parser_identifier (parser)
2495 /* Look for the identifier. */
2496 token = cp_parser_require (parser, CPP_NAME, "identifier");
2497 /* Return the value. */
2498 return token ? token->value : error_mark_node;
2501 /* Basic concepts [gram.basic] */
2503 /* Parse a translation-unit.
2506 declaration-seq [opt]
2508 Returns TRUE if all went well. */
2511 cp_parser_translation_unit (parser)
2516 cp_parser_declaration_seq_opt (parser);
2518 /* If there are no tokens left then all went well. */
2519 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2522 /* Otherwise, issue an error message. */
2523 cp_parser_error (parser, "expected declaration");
2527 /* Consume the EOF token. */
2528 cp_parser_require (parser, CPP_EOF, "end-of-file");
2531 finish_translation_unit ();
2533 /* All went well. */
2537 /* Expressions [gram.expr] */
2539 /* Parse a primary-expression.
2550 ( compound-statement )
2551 __builtin_va_arg ( assignment-expression , type-id )
2556 Returns a representation of the expression.
2558 *IDK indicates what kind of id-expression (if any) was present.
2560 *QUALIFYING_CLASS is set to a non-NULL value if the id-expression can be
2561 used as the operand of a pointer-to-member. In that case,
2562 *QUALIFYING_CLASS gives the class that is used as the qualifying
2563 class in the pointer-to-member. */
2566 cp_parser_primary_expression (cp_parser *parser,
2567 cp_parser_id_kind *idk,
2568 tree *qualifying_class)
2572 /* Assume the primary expression is not an id-expression. */
2573 *idk = CP_PARSER_ID_KIND_NONE;
2574 /* And that it cannot be used as pointer-to-member. */
2575 *qualifying_class = NULL_TREE;
2577 /* Peek at the next token. */
2578 token = cp_lexer_peek_token (parser->lexer);
2579 switch (token->type)
2592 token = cp_lexer_consume_token (parser->lexer);
2593 return token->value;
2595 case CPP_OPEN_PAREN:
2598 bool saved_greater_than_is_operator_p;
2600 /* Consume the `('. */
2601 cp_lexer_consume_token (parser->lexer);
2602 /* Within a parenthesized expression, a `>' token is always
2603 the greater-than operator. */
2604 saved_greater_than_is_operator_p
2605 = parser->greater_than_is_operator_p;
2606 parser->greater_than_is_operator_p = true;
2607 /* If we see `( { ' then we are looking at the beginning of
2608 a GNU statement-expression. */
2609 if (cp_parser_allow_gnu_extensions_p (parser)
2610 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
2612 /* Statement-expressions are not allowed by the standard. */
2614 pedwarn ("ISO C++ forbids braced-groups within expressions");
2616 /* And they're not allowed outside of a function-body; you
2617 cannot, for example, write:
2619 int i = ({ int j = 3; j + 1; });
2621 at class or namespace scope. */
2622 if (!at_function_scope_p ())
2623 error ("statement-expressions are allowed only inside functions");
2624 /* Start the statement-expression. */
2625 expr = begin_stmt_expr ();
2626 /* Parse the compound-statement. */
2627 cp_parser_compound_statement (parser);
2629 expr = finish_stmt_expr (expr);
2633 /* Parse the parenthesized expression. */
2634 expr = cp_parser_expression (parser);
2635 /* Let the front end know that this expression was
2636 enclosed in parentheses. This matters in case, for
2637 example, the expression is of the form `A::B', since
2638 `&A::B' might be a pointer-to-member, but `&(A::B)' is
2640 finish_parenthesized_expr (expr);
2642 /* The `>' token might be the end of a template-id or
2643 template-parameter-list now. */
2644 parser->greater_than_is_operator_p
2645 = saved_greater_than_is_operator_p;
2646 /* Consume the `)'. */
2647 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
2648 cp_parser_skip_to_end_of_statement (parser);
2654 switch (token->keyword)
2656 /* These two are the boolean literals. */
2658 cp_lexer_consume_token (parser->lexer);
2659 return boolean_true_node;
2661 cp_lexer_consume_token (parser->lexer);
2662 return boolean_false_node;
2664 /* The `__null' literal. */
2666 cp_lexer_consume_token (parser->lexer);
2669 /* Recognize the `this' keyword. */
2671 cp_lexer_consume_token (parser->lexer);
2672 if (parser->local_variables_forbidden_p)
2674 error ("`this' may not be used in this context");
2675 return error_mark_node;
2677 return finish_this_expr ();
2679 /* The `operator' keyword can be the beginning of an
2684 case RID_FUNCTION_NAME:
2685 case RID_PRETTY_FUNCTION_NAME:
2686 case RID_C99_FUNCTION_NAME:
2687 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
2688 __func__ are the names of variables -- but they are
2689 treated specially. Therefore, they are handled here,
2690 rather than relying on the generic id-expression logic
2691 below. Gramatically, these names are id-expressions.
2693 Consume the token. */
2694 token = cp_lexer_consume_token (parser->lexer);
2695 /* Look up the name. */
2696 return finish_fname (token->value);
2703 /* The `__builtin_va_arg' construct is used to handle
2704 `va_arg'. Consume the `__builtin_va_arg' token. */
2705 cp_lexer_consume_token (parser->lexer);
2706 /* Look for the opening `('. */
2707 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
2708 /* Now, parse the assignment-expression. */
2709 expression = cp_parser_assignment_expression (parser);
2710 /* Look for the `,'. */
2711 cp_parser_require (parser, CPP_COMMA, "`,'");
2712 /* Parse the type-id. */
2713 type = cp_parser_type_id (parser);
2714 /* Look for the closing `)'. */
2715 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
2717 return build_x_va_arg (expression, type);
2721 cp_parser_error (parser, "expected primary-expression");
2722 return error_mark_node;
2726 /* An id-expression can start with either an identifier, a
2727 `::' as the beginning of a qualified-id, or the "operator"
2731 case CPP_TEMPLATE_ID:
2732 case CPP_NESTED_NAME_SPECIFIER:
2738 /* Parse the id-expression. */
2740 = cp_parser_id_expression (parser,
2741 /*template_keyword_p=*/false,
2742 /*check_dependency_p=*/true,
2743 /*template_p=*/NULL);
2744 if (id_expression == error_mark_node)
2745 return error_mark_node;
2746 /* If we have a template-id, then no further lookup is
2747 required. If the template-id was for a template-class, we
2748 will sometimes have a TYPE_DECL at this point. */
2749 else if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
2750 || TREE_CODE (id_expression) == TYPE_DECL)
2751 decl = id_expression;
2752 /* Look up the name. */
2755 decl = cp_parser_lookup_name_simple (parser, id_expression);
2756 /* If name lookup gives us a SCOPE_REF, then the
2757 qualifying scope was dependent. Just propagate the
2759 if (TREE_CODE (decl) == SCOPE_REF)
2761 if (TYPE_P (TREE_OPERAND (decl, 0)))
2762 *qualifying_class = TREE_OPERAND (decl, 0);
2765 /* Check to see if DECL is a local variable in a context
2766 where that is forbidden. */
2767 if (parser->local_variables_forbidden_p
2768 && local_variable_p (decl))
2770 /* It might be that we only found DECL because we are
2771 trying to be generous with pre-ISO scoping rules.
2772 For example, consider:
2776 for (int i = 0; i < 10; ++i) {}
2777 extern void f(int j = i);
2780 Here, name look up will originally find the out
2781 of scope `i'. We need to issue a warning message,
2782 but then use the global `i'. */
2783 decl = check_for_out_of_scope_variable (decl);
2784 if (local_variable_p (decl))
2786 error ("local variable `%D' may not appear in this context",
2788 return error_mark_node;
2792 /* If unqualified name lookup fails while processing a
2793 template, that just means that we need to do name
2794 lookup again when the template is instantiated. */
2796 && decl == error_mark_node
2797 && processing_template_decl)
2799 *idk = CP_PARSER_ID_KIND_UNQUALIFIED;
2800 return build_min_nt (LOOKUP_EXPR, id_expression);
2802 else if (decl == error_mark_node
2803 && !processing_template_decl)
2807 /* It may be resolvable as a koenig lookup function
2809 *idk = CP_PARSER_ID_KIND_UNQUALIFIED;
2810 return id_expression;
2812 else if (TYPE_P (parser->scope)
2813 && !COMPLETE_TYPE_P (parser->scope))
2814 error ("incomplete type `%T' used in nested name specifier",
2816 else if (parser->scope != global_namespace)
2817 error ("`%D' is not a member of `%D'",
2818 id_expression, parser->scope);
2820 error ("`::%D' has not been declared", id_expression);
2822 /* If DECL is a variable would be out of scope under
2823 ANSI/ISO rules, but in scope in the ARM, name lookup
2824 will succeed. Issue a diagnostic here. */
2826 decl = check_for_out_of_scope_variable (decl);
2828 /* Remember that the name was used in the definition of
2829 the current class so that we can check later to see if
2830 the meaning would have been different after the class
2831 was entirely defined. */
2832 if (!parser->scope && decl != error_mark_node)
2833 maybe_note_name_used_in_class (id_expression, decl);
2836 /* If we didn't find anything, or what we found was a type,
2837 then this wasn't really an id-expression. */
2838 if (TREE_CODE (decl) == TYPE_DECL
2839 || TREE_CODE (decl) == NAMESPACE_DECL
2840 || (TREE_CODE (decl) == TEMPLATE_DECL
2841 && !DECL_FUNCTION_TEMPLATE_P (decl)))
2843 cp_parser_error (parser,
2844 "expected primary-expression");
2845 return error_mark_node;
2848 /* If the name resolved to a template parameter, there is no
2849 need to look it up again later. Similarly, we resolve
2850 enumeration constants to their underlying values. */
2851 if (TREE_CODE (decl) == CONST_DECL)
2853 *idk = CP_PARSER_ID_KIND_NONE;
2854 if (DECL_TEMPLATE_PARM_P (decl) || !processing_template_decl)
2855 return DECL_INITIAL (decl);
2862 /* If the declaration was explicitly qualified indicate
2863 that. The semantics of `A::f(3)' are different than
2864 `f(3)' if `f' is virtual. */
2865 *idk = (parser->scope
2866 ? CP_PARSER_ID_KIND_QUALIFIED
2867 : (TREE_CODE (decl) == TEMPLATE_ID_EXPR
2868 ? CP_PARSER_ID_KIND_TEMPLATE_ID
2869 : CP_PARSER_ID_KIND_UNQUALIFIED));
2874 An id-expression is type-dependent if it contains an
2875 identifier that was declared with a dependent type.
2877 As an optimization, we could choose not to create a
2878 LOOKUP_EXPR for a name that resolved to a local
2879 variable in the template function that we are currently
2880 declaring; such a name cannot ever resolve to anything
2881 else. If we did that we would not have to look up
2882 these names at instantiation time.
2884 The standard is not very specific about an
2885 id-expression that names a set of overloaded functions.
2886 What if some of them have dependent types and some of
2887 them do not? Presumably, such a name should be treated
2888 as a dependent name. */
2889 /* Assume the name is not dependent. */
2890 dependent_p = false;
2891 if (!processing_template_decl)
2892 /* No names are dependent outside a template. */
2894 /* A template-id where the name of the template was not
2895 resolved is definitely dependent. */
2896 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
2897 && (TREE_CODE (TREE_OPERAND (decl, 0))
2898 == IDENTIFIER_NODE))
2900 /* For anything except an overloaded function, just check
2902 else if (!is_overloaded_fn (decl))
2904 = cp_parser_dependent_type_p (TREE_TYPE (decl));
2905 /* For a set of overloaded functions, check each of the
2911 if (BASELINK_P (fns))
2912 fns = BASELINK_FUNCTIONS (fns);
2914 /* For a template-id, check to see if the template
2915 arguments are dependent. */
2916 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
2918 tree args = TREE_OPERAND (fns, 1);
2920 if (args && TREE_CODE (args) == TREE_LIST)
2924 if (cp_parser_dependent_template_arg_p
2925 (TREE_VALUE (args)))
2930 args = TREE_CHAIN (args);
2933 else if (args && TREE_CODE (args) == TREE_VEC)
2936 for (i = 0; i < TREE_VEC_LENGTH (args); ++i)
2937 if (cp_parser_dependent_template_arg_p
2938 (TREE_VEC_ELT (args, i)))
2945 /* The functions are those referred to by the
2947 fns = TREE_OPERAND (fns, 0);
2950 /* If there are no dependent template arguments, go
2951 through the overlaoded functions. */
2952 while (fns && !dependent_p)
2954 tree fn = OVL_CURRENT (fns);
2956 /* Member functions of dependent classes are
2958 if (TREE_CODE (fn) == FUNCTION_DECL
2959 && cp_parser_type_dependent_expression_p (fn))
2961 else if (TREE_CODE (fn) == TEMPLATE_DECL
2962 && cp_parser_dependent_template_p (fn))
2965 fns = OVL_NEXT (fns);
2969 /* If the name was dependent on a template parameter,
2970 we will resolve the name at instantiation time. */
2973 /* Create a SCOPE_REF for qualified names. */
2976 if (TYPE_P (parser->scope))
2977 *qualifying_class = parser->scope;
2978 return build_nt (SCOPE_REF,
2982 /* A TEMPLATE_ID already contains all the information
2984 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR)
2985 return id_expression;
2986 /* Create a LOOKUP_EXPR for other unqualified names. */
2987 return build_min_nt (LOOKUP_EXPR, id_expression);
2992 decl = (adjust_result_of_qualified_name_lookup
2993 (decl, parser->scope, current_class_type));
2994 if (TREE_CODE (decl) == FIELD_DECL || BASELINK_P (decl))
2995 *qualifying_class = parser->scope;
2997 /* Resolve references to variables of anonymous unions
2998 into COMPONENT_REFs. */
2999 else if (TREE_CODE (decl) == ALIAS_DECL)
3000 decl = DECL_INITIAL (decl);
3002 /* Transform references to non-static data members into
3004 decl = hack_identifier (decl, id_expression);
3007 if (TREE_DEPRECATED (decl))
3008 warn_deprecated_use (decl);
3013 /* Anything else is an error. */
3015 cp_parser_error (parser, "expected primary-expression");
3016 return error_mark_node;
3020 /* Parse an id-expression.
3027 :: [opt] nested-name-specifier template [opt] unqualified-id
3029 :: operator-function-id
3032 Return a representation of the unqualified portion of the
3033 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
3034 a `::' or nested-name-specifier.
3036 Often, if the id-expression was a qualified-id, the caller will
3037 want to make a SCOPE_REF to represent the qualified-id. This
3038 function does not do this in order to avoid wastefully creating
3039 SCOPE_REFs when they are not required.
3041 If ASSUME_TYPENAME_P is true then we assume that qualified names
3042 are typenames. This flag is set when parsing a declarator-id;
3048 we are supposed to assume that `S<T>::R' is a class.
3050 If TEMPLATE_KEYWORD_P is true, then we have just seen the
3053 If CHECK_DEPENDENCY_P is false, then names are looked up inside
3054 uninstantiated templates.
3056 If *TEMPLATE_KEYWORD_P is non-NULL, it is set to true iff the
3057 `template' keyword is used to explicitly indicate that the entity
3058 named is a template. */
3061 cp_parser_id_expression (cp_parser *parser,
3062 bool template_keyword_p,
3063 bool check_dependency_p,
3066 bool global_scope_p;
3067 bool nested_name_specifier_p;
3069 /* Assume the `template' keyword was not used. */
3071 *template_p = false;
3073 /* Look for the optional `::' operator. */
3075 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
3077 /* Look for the optional nested-name-specifier. */
3078 nested_name_specifier_p
3079 = (cp_parser_nested_name_specifier_opt (parser,
3080 /*typename_keyword_p=*/false,
3084 /* If there is a nested-name-specifier, then we are looking at
3085 the first qualified-id production. */
3086 if (nested_name_specifier_p)
3089 tree saved_object_scope;
3090 tree saved_qualifying_scope;
3091 tree unqualified_id;
3094 /* See if the next token is the `template' keyword. */
3096 template_p = &is_template;
3097 *template_p = cp_parser_optional_template_keyword (parser);
3098 /* Name lookup we do during the processing of the
3099 unqualified-id might obliterate SCOPE. */
3100 saved_scope = parser->scope;
3101 saved_object_scope = parser->object_scope;
3102 saved_qualifying_scope = parser->qualifying_scope;
3103 /* Process the final unqualified-id. */
3104 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
3105 check_dependency_p);
3106 /* Restore the SAVED_SCOPE for our caller. */
3107 parser->scope = saved_scope;
3108 parser->object_scope = saved_object_scope;
3109 parser->qualifying_scope = saved_qualifying_scope;
3111 return unqualified_id;
3113 /* Otherwise, if we are in global scope, then we are looking at one
3114 of the other qualified-id productions. */
3115 else if (global_scope_p)
3120 /* We don't know yet whether or not this will be a
3122 cp_parser_parse_tentatively (parser);
3123 /* Try a template-id. */
3124 id = cp_parser_template_id (parser,
3125 /*template_keyword_p=*/false,
3126 /*check_dependency_p=*/true);
3127 /* If that worked, we're done. */
3128 if (cp_parser_parse_definitely (parser))
3131 /* Peek at the next token. */
3132 token = cp_lexer_peek_token (parser->lexer);
3134 switch (token->type)
3137 return cp_parser_identifier (parser);
3140 if (token->keyword == RID_OPERATOR)
3141 return cp_parser_operator_function_id (parser);
3145 cp_parser_error (parser, "expected id-expression");
3146 return error_mark_node;
3150 return cp_parser_unqualified_id (parser, template_keyword_p,
3151 /*check_dependency_p=*/true);
3154 /* Parse an unqualified-id.
3158 operator-function-id
3159 conversion-function-id
3163 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3164 keyword, in a construct like `A::template ...'.
3166 Returns a representation of unqualified-id. For the `identifier'
3167 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
3168 production a BIT_NOT_EXPR is returned; the operand of the
3169 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
3170 other productions, see the documentation accompanying the
3171 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
3172 names are looked up in uninstantiated templates. */
3175 cp_parser_unqualified_id (parser, template_keyword_p,
3178 bool template_keyword_p;
3179 bool check_dependency_p;
3183 /* Peek at the next token. */
3184 token = cp_lexer_peek_token (parser->lexer);
3186 switch (token->type)
3192 /* We don't know yet whether or not this will be a
3194 cp_parser_parse_tentatively (parser);
3195 /* Try a template-id. */
3196 id = cp_parser_template_id (parser, template_keyword_p,
3197 check_dependency_p);
3198 /* If it worked, we're done. */
3199 if (cp_parser_parse_definitely (parser))
3201 /* Otherwise, it's an ordinary identifier. */
3202 return cp_parser_identifier (parser);
3205 case CPP_TEMPLATE_ID:
3206 return cp_parser_template_id (parser, template_keyword_p,
3207 check_dependency_p);
3212 tree qualifying_scope;
3216 /* Consume the `~' token. */
3217 cp_lexer_consume_token (parser->lexer);
3218 /* Parse the class-name. The standard, as written, seems to
3221 template <typename T> struct S { ~S (); };
3222 template <typename T> S<T>::~S() {}
3224 is invalid, since `~' must be followed by a class-name, but
3225 `S<T>' is dependent, and so not known to be a class.
3226 That's not right; we need to look in uninstantiated
3227 templates. A further complication arises from:
3229 template <typename T> void f(T t) {
3233 Here, it is not possible to look up `T' in the scope of `T'
3234 itself. We must look in both the current scope, and the
3235 scope of the containing complete expression.
3237 Yet another issue is:
3246 The standard does not seem to say that the `S' in `~S'
3247 should refer to the type `S' and not the data member
3250 /* DR 244 says that we look up the name after the "~" in the
3251 same scope as we looked up the qualifying name. That idea
3252 isn't fully worked out; it's more complicated than that. */
3253 scope = parser->scope;
3254 object_scope = parser->object_scope;
3255 qualifying_scope = parser->qualifying_scope;
3257 /* If the name is of the form "X::~X" it's OK. */
3258 if (scope && TYPE_P (scope)
3259 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
3260 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3262 && (cp_lexer_peek_token (parser->lexer)->value
3263 == TYPE_IDENTIFIER (scope)))
3265 cp_lexer_consume_token (parser->lexer);
3266 return build_nt (BIT_NOT_EXPR, scope);
3269 /* If there was an explicit qualification (S::~T), first look
3270 in the scope given by the qualification (i.e., S). */
3273 cp_parser_parse_tentatively (parser);
3274 type_decl = cp_parser_class_name (parser,
3275 /*typename_keyword_p=*/false,
3276 /*template_keyword_p=*/false,
3278 /*check_access_p=*/true,
3279 /*check_dependency=*/false,
3280 /*class_head_p=*/false);
3281 if (cp_parser_parse_definitely (parser))
3282 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3284 /* In "N::S::~S", look in "N" as well. */
3285 if (scope && qualifying_scope)
3287 cp_parser_parse_tentatively (parser);
3288 parser->scope = qualifying_scope;
3289 parser->object_scope = NULL_TREE;
3290 parser->qualifying_scope = NULL_TREE;
3292 = cp_parser_class_name (parser,
3293 /*typename_keyword_p=*/false,
3294 /*template_keyword_p=*/false,
3296 /*check_access_p=*/true,
3297 /*check_dependency=*/false,
3298 /*class_head_p=*/false);
3299 if (cp_parser_parse_definitely (parser))
3300 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3302 /* In "p->S::~T", look in the scope given by "*p" as well. */
3303 else if (object_scope)
3305 cp_parser_parse_tentatively (parser);
3306 parser->scope = object_scope;
3307 parser->object_scope = NULL_TREE;
3308 parser->qualifying_scope = NULL_TREE;
3310 = cp_parser_class_name (parser,
3311 /*typename_keyword_p=*/false,
3312 /*template_keyword_p=*/false,
3314 /*check_access_p=*/true,
3315 /*check_dependency=*/false,
3316 /*class_head_p=*/false);
3317 if (cp_parser_parse_definitely (parser))
3318 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3320 /* Look in the surrounding context. */
3321 parser->scope = NULL_TREE;
3322 parser->object_scope = NULL_TREE;
3323 parser->qualifying_scope = NULL_TREE;
3325 = cp_parser_class_name (parser,
3326 /*typename_keyword_p=*/false,
3327 /*template_keyword_p=*/false,
3329 /*check_access_p=*/true,
3330 /*check_dependency=*/false,
3331 /*class_head_p=*/false);
3332 /* If an error occurred, assume that the name of the
3333 destructor is the same as the name of the qualifying
3334 class. That allows us to keep parsing after running
3335 into ill-formed destructor names. */
3336 if (type_decl == error_mark_node && scope && TYPE_P (scope))
3337 return build_nt (BIT_NOT_EXPR, scope);
3338 else if (type_decl == error_mark_node)
3339 return error_mark_node;
3341 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3345 if (token->keyword == RID_OPERATOR)
3349 /* This could be a template-id, so we try that first. */
3350 cp_parser_parse_tentatively (parser);
3351 /* Try a template-id. */
3352 id = cp_parser_template_id (parser, template_keyword_p,
3353 /*check_dependency_p=*/true);
3354 /* If that worked, we're done. */
3355 if (cp_parser_parse_definitely (parser))
3357 /* We still don't know whether we're looking at an
3358 operator-function-id or a conversion-function-id. */
3359 cp_parser_parse_tentatively (parser);
3360 /* Try an operator-function-id. */
3361 id = cp_parser_operator_function_id (parser);
3362 /* If that didn't work, try a conversion-function-id. */
3363 if (!cp_parser_parse_definitely (parser))
3364 id = cp_parser_conversion_function_id (parser);
3371 cp_parser_error (parser, "expected unqualified-id");
3372 return error_mark_node;
3376 /* Parse an (optional) nested-name-specifier.
3378 nested-name-specifier:
3379 class-or-namespace-name :: nested-name-specifier [opt]
3380 class-or-namespace-name :: template nested-name-specifier [opt]
3382 PARSER->SCOPE should be set appropriately before this function is
3383 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
3384 effect. TYPE_P is TRUE if we non-type bindings should be ignored
3387 Sets PARSER->SCOPE to the class (TYPE) or namespace
3388 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
3389 it unchanged if there is no nested-name-specifier. Returns the new
3390 scope iff there is a nested-name-specifier, or NULL_TREE otherwise. */
3393 cp_parser_nested_name_specifier_opt (cp_parser *parser,
3394 bool typename_keyword_p,
3395 bool check_dependency_p,
3398 bool success = false;
3399 tree access_check = NULL_TREE;
3402 /* If the next token corresponds to a nested name specifier, there
3403 is no need to reparse it. */
3404 if (cp_lexer_next_token_is (parser->lexer, CPP_NESTED_NAME_SPECIFIER))
3409 /* Get the stored value. */
3410 value = cp_lexer_consume_token (parser->lexer)->value;
3411 /* Perform any access checks that were deferred. */
3412 for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
3413 cp_parser_defer_access_check (parser,
3414 TREE_PURPOSE (check),
3415 TREE_VALUE (check));
3416 /* Set the scope from the stored value. */
3417 parser->scope = TREE_VALUE (value);
3418 parser->qualifying_scope = TREE_TYPE (value);
3419 parser->object_scope = NULL_TREE;
3420 return parser->scope;
3423 /* Remember where the nested-name-specifier starts. */
3424 if (cp_parser_parsing_tentatively (parser)
3425 && !cp_parser_committed_to_tentative_parse (parser))
3427 cp_token *next_token = cp_lexer_peek_token (parser->lexer);
3428 start = cp_lexer_token_difference (parser->lexer,
3429 parser->lexer->first_token,
3431 access_check = parser->context->deferred_access_checks;
3440 tree saved_qualifying_scope;
3442 bool template_keyword_p;
3444 /* Spot cases that cannot be the beginning of a
3445 nested-name-specifier. On the second and subsequent times
3446 through the loop, we look for the `template' keyword. */
3448 && cp_lexer_next_token_is_keyword (parser->lexer,
3451 /* A template-id can start a nested-name-specifier. */
3452 else if (cp_lexer_next_token_is (parser->lexer, CPP_TEMPLATE_ID))
3456 /* If the next token is not an identifier, then it is
3457 definitely not a class-or-namespace-name. */
3458 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
3460 /* If the following token is neither a `<' (to begin a
3461 template-id), nor a `::', then we are not looking at a
3462 nested-name-specifier. */
3463 token = cp_lexer_peek_nth_token (parser->lexer, 2);
3464 if (token->type != CPP_LESS && token->type != CPP_SCOPE)
3468 /* The nested-name-specifier is optional, so we parse
3470 cp_parser_parse_tentatively (parser);
3472 /* Look for the optional `template' keyword, if this isn't the
3473 first time through the loop. */
3475 template_keyword_p = cp_parser_optional_template_keyword (parser);
3477 template_keyword_p = false;
3479 /* Save the old scope since the name lookup we are about to do
3480 might destroy it. */
3481 old_scope = parser->scope;
3482 saved_qualifying_scope = parser->qualifying_scope;
3483 /* Parse the qualifying entity. */
3485 = cp_parser_class_or_namespace_name (parser,
3490 /* Look for the `::' token. */
3491 cp_parser_require (parser, CPP_SCOPE, "`::'");
3493 /* If we found what we wanted, we keep going; otherwise, we're
3495 if (!cp_parser_parse_definitely (parser))
3497 bool error_p = false;
3499 /* Restore the OLD_SCOPE since it was valid before the
3500 failed attempt at finding the last
3501 class-or-namespace-name. */
3502 parser->scope = old_scope;
3503 parser->qualifying_scope = saved_qualifying_scope;
3504 /* If the next token is an identifier, and the one after
3505 that is a `::', then any valid interpretation would have
3506 found a class-or-namespace-name. */
3507 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
3508 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3510 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
3513 token = cp_lexer_consume_token (parser->lexer);
3518 decl = cp_parser_lookup_name_simple (parser, token->value);
3519 if (TREE_CODE (decl) == TEMPLATE_DECL)
3520 error ("`%D' used without template parameters",
3522 else if (parser->scope)
3524 if (TYPE_P (parser->scope))
3525 error ("`%T::%D' is not a class-name or "
3527 parser->scope, token->value);
3529 error ("`%D::%D' is not a class-name or "
3531 parser->scope, token->value);
3534 error ("`%D' is not a class-name or namespace-name",
3536 parser->scope = NULL_TREE;
3539 cp_lexer_consume_token (parser->lexer);
3544 /* We've found one valid nested-name-specifier. */
3546 /* Make sure we look in the right scope the next time through
3548 parser->scope = (TREE_CODE (new_scope) == TYPE_DECL
3549 ? TREE_TYPE (new_scope)
3551 /* If it is a class scope, try to complete it; we are about to
3552 be looking up names inside the class. */
3553 if (TYPE_P (parser->scope))
3554 complete_type (parser->scope);
3557 /* If parsing tentatively, replace the sequence of tokens that makes
3558 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
3559 token. That way, should we re-parse the token stream, we will
3560 not have to repeat the effort required to do the parse, nor will
3561 we issue duplicate error messages. */
3562 if (success && start >= 0)
3567 /* Find the token that corresponds to the start of the
3569 token = cp_lexer_advance_token (parser->lexer,
3570 parser->lexer->first_token,
3573 /* Remember the access checks associated with this
3574 nested-name-specifier. */
3575 c = parser->context->deferred_access_checks;
3576 if (c == access_check)
3577 access_check = NULL_TREE;
3580 while (TREE_CHAIN (c) != access_check)
3582 access_check = parser->context->deferred_access_checks;
3583 parser->context->deferred_access_checks = TREE_CHAIN (c);
3584 TREE_CHAIN (c) = NULL_TREE;
3587 /* Reset the contents of the START token. */
3588 token->type = CPP_NESTED_NAME_SPECIFIER;
3589 token->value = build_tree_list (access_check, parser->scope);
3590 TREE_TYPE (token->value) = parser->qualifying_scope;
3591 token->keyword = RID_MAX;
3592 /* Purge all subsequent tokens. */
3593 cp_lexer_purge_tokens_after (parser->lexer, token);
3596 return success ? parser->scope : NULL_TREE;
3599 /* Parse a nested-name-specifier. See
3600 cp_parser_nested_name_specifier_opt for details. This function
3601 behaves identically, except that it will an issue an error if no
3602 nested-name-specifier is present, and it will return
3603 ERROR_MARK_NODE, rather than NULL_TREE, if no nested-name-specifier
3607 cp_parser_nested_name_specifier (cp_parser *parser,
3608 bool typename_keyword_p,
3609 bool check_dependency_p,
3614 /* Look for the nested-name-specifier. */
3615 scope = cp_parser_nested_name_specifier_opt (parser,
3619 /* If it was not present, issue an error message. */
3622 cp_parser_error (parser, "expected nested-name-specifier");
3623 return error_mark_node;
3629 /* Parse a class-or-namespace-name.
3631 class-or-namespace-name:
3635 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
3636 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
3637 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
3638 TYPE_P is TRUE iff the next name should be taken as a class-name,
3639 even the same name is declared to be another entity in the same
3642 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
3643 specified by the class-or-namespace-name. */
3646 cp_parser_class_or_namespace_name (cp_parser *parser,
3647 bool typename_keyword_p,
3648 bool template_keyword_p,
3649 bool check_dependency_p,
3653 tree saved_qualifying_scope;
3654 tree saved_object_scope;
3657 /* If the next token is the `template' keyword, we know that we are
3658 looking at a class-name. */
3659 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
3660 return cp_parser_class_name (parser,
3664 /*check_access_p=*/true,
3666 /*class_head_p=*/false);
3667 /* Before we try to parse the class-name, we must save away the
3668 current PARSER->SCOPE since cp_parser_class_name will destroy
3670 saved_scope = parser->scope;
3671 saved_qualifying_scope = parser->qualifying_scope;
3672 saved_object_scope = parser->object_scope;
3673 /* Try for a class-name first. */
3674 cp_parser_parse_tentatively (parser);
3675 scope = cp_parser_class_name (parser,
3679 /*check_access_p=*/true,
3681 /*class_head_p=*/false);
3682 /* If that didn't work, try for a namespace-name. */
3683 if (!cp_parser_parse_definitely (parser))
3685 /* Restore the saved scope. */
3686 parser->scope = saved_scope;
3687 parser->qualifying_scope = saved_qualifying_scope;
3688 parser->object_scope = saved_object_scope;
3689 /* Now look for a namespace-name. */
3690 scope = cp_parser_namespace_name (parser);
3696 /* Parse a postfix-expression.
3700 postfix-expression [ expression ]
3701 postfix-expression ( expression-list [opt] )
3702 simple-type-specifier ( expression-list [opt] )
3703 typename :: [opt] nested-name-specifier identifier
3704 ( expression-list [opt] )
3705 typename :: [opt] nested-name-specifier template [opt] template-id
3706 ( expression-list [opt] )
3707 postfix-expression . template [opt] id-expression
3708 postfix-expression -> template [opt] id-expression
3709 postfix-expression . pseudo-destructor-name
3710 postfix-expression -> pseudo-destructor-name
3711 postfix-expression ++
3712 postfix-expression --
3713 dynamic_cast < type-id > ( expression )
3714 static_cast < type-id > ( expression )
3715 reinterpret_cast < type-id > ( expression )
3716 const_cast < type-id > ( expression )
3717 typeid ( expression )
3723 ( type-id ) { initializer-list , [opt] }
3725 This extension is a GNU version of the C99 compound-literal
3726 construct. (The C99 grammar uses `type-name' instead of `type-id',
3727 but they are essentially the same concept.)
3729 If ADDRESS_P is true, the postfix expression is the operand of the
3732 Returns a representation of the expression. */
3735 cp_parser_postfix_expression (cp_parser *parser, bool address_p)
3739 cp_parser_id_kind idk = CP_PARSER_ID_KIND_NONE;
3740 tree postfix_expression = NULL_TREE;
3741 /* Non-NULL only if the current postfix-expression can be used to
3742 form a pointer-to-member. In that case, QUALIFYING_CLASS is the
3743 class used to qualify the member. */
3744 tree qualifying_class = NULL_TREE;
3747 /* Peek at the next token. */
3748 token = cp_lexer_peek_token (parser->lexer);
3749 /* Some of the productions are determined by keywords. */
3750 keyword = token->keyword;
3760 const char *saved_message;
3762 /* All of these can be handled in the same way from the point
3763 of view of parsing. Begin by consuming the token
3764 identifying the cast. */
3765 cp_lexer_consume_token (parser->lexer);
3767 /* New types cannot be defined in the cast. */
3768 saved_message = parser->type_definition_forbidden_message;
3769 parser->type_definition_forbidden_message
3770 = "types may not be defined in casts";
3772 /* Look for the opening `<'. */
3773 cp_parser_require (parser, CPP_LESS, "`<'");
3774 /* Parse the type to which we are casting. */
3775 type = cp_parser_type_id (parser);
3776 /* Look for the closing `>'. */
3777 cp_parser_require (parser, CPP_GREATER, "`>'");
3778 /* Restore the old message. */
3779 parser->type_definition_forbidden_message = saved_message;
3781 /* And the expression which is being cast. */
3782 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3783 expression = cp_parser_expression (parser);
3784 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3790 = build_dynamic_cast (type, expression);
3794 = build_static_cast (type, expression);
3798 = build_reinterpret_cast (type, expression);
3802 = build_const_cast (type, expression);
3813 const char *saved_message;
3815 /* Consume the `typeid' token. */
3816 cp_lexer_consume_token (parser->lexer);
3817 /* Look for the `(' token. */
3818 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3819 /* Types cannot be defined in a `typeid' expression. */
3820 saved_message = parser->type_definition_forbidden_message;
3821 parser->type_definition_forbidden_message
3822 = "types may not be defined in a `typeid\' expression";
3823 /* We can't be sure yet whether we're looking at a type-id or an
3825 cp_parser_parse_tentatively (parser);
3826 /* Try a type-id first. */
3827 type = cp_parser_type_id (parser);
3828 /* Look for the `)' token. Otherwise, we can't be sure that
3829 we're not looking at an expression: consider `typeid (int
3830 (3))', for example. */
3831 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3832 /* If all went well, simply lookup the type-id. */
3833 if (cp_parser_parse_definitely (parser))
3834 postfix_expression = get_typeid (type);
3835 /* Otherwise, fall back to the expression variant. */
3840 /* Look for an expression. */
3841 expression = cp_parser_expression (parser);
3842 /* Compute its typeid. */
3843 postfix_expression = build_typeid (expression);
3844 /* Look for the `)' token. */
3845 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3848 /* Restore the saved message. */
3849 parser->type_definition_forbidden_message = saved_message;
3855 bool template_p = false;
3859 /* Consume the `typename' token. */
3860 cp_lexer_consume_token (parser->lexer);
3861 /* Look for the optional `::' operator. */
3862 cp_parser_global_scope_opt (parser,
3863 /*current_scope_valid_p=*/false);
3864 /* Look for the nested-name-specifier. */
3865 cp_parser_nested_name_specifier (parser,
3866 /*typename_keyword_p=*/true,
3867 /*check_dependency_p=*/true,
3869 /* Look for the optional `template' keyword. */
3870 template_p = cp_parser_optional_template_keyword (parser);
3871 /* We don't know whether we're looking at a template-id or an
3873 cp_parser_parse_tentatively (parser);
3874 /* Try a template-id. */
3875 id = cp_parser_template_id (parser, template_p,
3876 /*check_dependency_p=*/true);
3877 /* If that didn't work, try an identifier. */
3878 if (!cp_parser_parse_definitely (parser))
3879 id = cp_parser_identifier (parser);
3880 /* Create a TYPENAME_TYPE to represent the type to which the
3881 functional cast is being performed. */
3882 type = make_typename_type (parser->scope, id,
3885 postfix_expression = cp_parser_functional_cast (parser, type);
3893 /* If the next thing is a simple-type-specifier, we may be
3894 looking at a functional cast. We could also be looking at
3895 an id-expression. So, we try the functional cast, and if
3896 that doesn't work we fall back to the primary-expression. */
3897 cp_parser_parse_tentatively (parser);
3898 /* Look for the simple-type-specifier. */
3899 type = cp_parser_simple_type_specifier (parser,
3900 CP_PARSER_FLAGS_NONE);
3901 /* Parse the cast itself. */
3902 if (!cp_parser_error_occurred (parser))
3904 = cp_parser_functional_cast (parser, type);
3905 /* If that worked, we're done. */
3906 if (cp_parser_parse_definitely (parser))
3909 /* If the functional-cast didn't work out, try a
3910 compound-literal. */
3911 if (cp_parser_allow_gnu_extensions_p (parser))
3913 tree initializer_list = NULL_TREE;
3915 cp_parser_parse_tentatively (parser);
3916 /* Look for the `('. */
3917 if (cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
3919 type = cp_parser_type_id (parser);
3920 /* Look for the `)'. */
3921 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3922 /* Look for the `{'. */
3923 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
3924 /* If things aren't going well, there's no need to
3926 if (!cp_parser_error_occurred (parser))
3928 /* Parse the initializer-list. */
3930 = cp_parser_initializer_list (parser);
3931 /* Allow a trailing `,'. */
3932 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
3933 cp_lexer_consume_token (parser->lexer);
3934 /* Look for the final `}'. */
3935 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
3938 /* If that worked, we're definitely looking at a
3939 compound-literal expression. */
3940 if (cp_parser_parse_definitely (parser))
3942 /* Warn the user that a compound literal is not
3943 allowed in standard C++. */
3945 pedwarn ("ISO C++ forbids compound-literals");
3946 /* Form the representation of the compound-literal. */
3948 = finish_compound_literal (type, initializer_list);
3953 /* It must be a primary-expression. */
3954 postfix_expression = cp_parser_primary_expression (parser,
3961 /* Peek at the next token. */
3962 token = cp_lexer_peek_token (parser->lexer);
3963 done = (token->type != CPP_OPEN_SQUARE
3964 && token->type != CPP_OPEN_PAREN
3965 && token->type != CPP_DOT
3966 && token->type != CPP_DEREF
3967 && token->type != CPP_PLUS_PLUS
3968 && token->type != CPP_MINUS_MINUS);
3970 /* If the postfix expression is complete, finish up. */
3971 if (address_p && qualifying_class && done)
3973 if (TREE_CODE (postfix_expression) == SCOPE_REF)
3974 postfix_expression = TREE_OPERAND (postfix_expression, 1);
3976 = build_offset_ref (qualifying_class, postfix_expression);
3977 return postfix_expression;
3980 /* Otherwise, if we were avoiding committing until we knew
3981 whether or not we had a pointer-to-member, we now know that
3982 the expression is an ordinary reference to a qualified name. */
3983 if (qualifying_class && !processing_template_decl)
3985 if (TREE_CODE (postfix_expression) == FIELD_DECL)
3987 = finish_non_static_data_member (postfix_expression,
3989 else if (BASELINK_P (postfix_expression))
3994 /* See if any of the functions are non-static members. */
3995 fns = BASELINK_FUNCTIONS (postfix_expression);
3996 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
3997 fns = TREE_OPERAND (fns, 0);
3998 for (fn = fns; fn; fn = OVL_NEXT (fn))
3999 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn))
4001 /* If so, the expression may be relative to the current
4003 if (fn && current_class_type
4004 && DERIVED_FROM_P (qualifying_class, current_class_type))
4006 = (build_class_member_access_expr
4007 (maybe_dummy_object (qualifying_class, NULL),
4009 BASELINK_ACCESS_BINFO (postfix_expression),
4010 /*preserve_reference=*/false));
4012 return build_offset_ref (qualifying_class,
4013 postfix_expression);
4017 /* Remember that there was a reference to this entity. */
4018 if (DECL_P (postfix_expression))
4019 mark_used (postfix_expression);
4021 /* Keep looping until the postfix-expression is complete. */
4024 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE
4025 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
4027 /* It is not a Koenig lookup function call. */
4028 unqualified_name_lookup_error (postfix_expression);
4029 postfix_expression = error_mark_node;
4032 /* Peek at the next token. */
4033 token = cp_lexer_peek_token (parser->lexer);
4035 switch (token->type)
4037 case CPP_OPEN_SQUARE:
4038 /* postfix-expression [ expression ] */
4042 /* Consume the `[' token. */
4043 cp_lexer_consume_token (parser->lexer);
4044 /* Parse the index expression. */
4045 index = cp_parser_expression (parser);
4046 /* Look for the closing `]'. */
4047 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4049 /* Build the ARRAY_REF. */
4051 = grok_array_decl (postfix_expression, index);
4052 idk = CP_PARSER_ID_KIND_NONE;
4056 case CPP_OPEN_PAREN:
4057 /* postfix-expression ( expression-list [opt] ) */
4061 /* Consume the `(' token. */
4062 cp_lexer_consume_token (parser->lexer);
4063 /* If the next token is not a `)', then there are some
4065 if (cp_lexer_next_token_is_not (parser->lexer,
4067 args = cp_parser_expression_list (parser);
4070 /* Look for the closing `)'. */
4071 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4073 if (idk == CP_PARSER_ID_KIND_UNQUALIFIED
4074 && (is_overloaded_fn (postfix_expression)
4075 || DECL_P (postfix_expression)
4076 || TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4080 tree identifier = NULL_TREE;
4081 tree functions = NULL_TREE;
4083 /* Find the name of the overloaded function. */
4084 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4085 identifier = postfix_expression;
4086 else if (is_overloaded_fn (postfix_expression))
4088 functions = postfix_expression;
4089 identifier = DECL_NAME (get_first_fn (functions));
4091 else if (DECL_P (postfix_expression))
4093 functions = postfix_expression;
4094 identifier = DECL_NAME (postfix_expression);
4097 /* A call to a namespace-scope function using an
4100 Do Koenig lookup -- unless any of the arguments are
4102 for (arg = args; arg; arg = TREE_CHAIN (arg))
4103 if (cp_parser_type_dependent_expression_p (TREE_VALUE (arg)))
4108 = lookup_arg_dependent(identifier, functions, args);
4109 if (!postfix_expression)
4111 /* The unqualified name could not be resolved. */
4112 unqualified_name_lookup_error (identifier);
4113 postfix_expression = error_mark_node;
4116 = build_call_from_tree (postfix_expression, args,
4117 /*diallow_virtual=*/false);
4120 postfix_expression = build_min_nt (LOOKUP_EXPR,
4123 else if (idk == CP_PARSER_ID_KIND_UNQUALIFIED
4124 && TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4126 /* The unqualified name could not be resolved. */
4127 unqualified_name_lookup_error (postfix_expression);
4128 postfix_expression = error_mark_node;
4132 /* In the body of a template, no further processing is
4134 if (processing_template_decl)
4136 postfix_expression = build_nt (CALL_EXPR,
4142 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
4144 = (build_new_method_call
4145 (TREE_OPERAND (postfix_expression, 0),
4146 TREE_OPERAND (postfix_expression, 1),
4148 (idk == CP_PARSER_ID_KIND_QUALIFIED
4149 ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL)));
4150 else if (TREE_CODE (postfix_expression) == OFFSET_REF)
4151 postfix_expression = (build_offset_ref_call_from_tree
4152 (postfix_expression, args));
4153 else if (idk == CP_PARSER_ID_KIND_QUALIFIED)
4155 /* A call to a static class member, or a
4156 namespace-scope function. */
4158 = finish_call_expr (postfix_expression, args,
4159 /*disallow_virtual=*/true);
4163 /* All other function calls. */
4165 = finish_call_expr (postfix_expression, args,
4166 /*disallow_virtual=*/false);
4169 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
4170 idk = CP_PARSER_ID_KIND_NONE;
4176 /* postfix-expression . template [opt] id-expression
4177 postfix-expression . pseudo-destructor-name
4178 postfix-expression -> template [opt] id-expression
4179 postfix-expression -> pseudo-destructor-name */
4184 tree scope = NULL_TREE;
4186 /* If this is a `->' operator, dereference the pointer. */
4187 if (token->type == CPP_DEREF)
4188 postfix_expression = build_x_arrow (postfix_expression);
4189 /* Check to see whether or not the expression is
4191 dependent_p = (cp_parser_type_dependent_expression_p
4192 (postfix_expression));
4193 /* The identifier following the `->' or `.' is not
4195 parser->scope = NULL_TREE;
4196 parser->qualifying_scope = NULL_TREE;
4197 parser->object_scope = NULL_TREE;
4198 /* Enter the scope corresponding to the type of the object
4199 given by the POSTFIX_EXPRESSION. */
4201 && TREE_TYPE (postfix_expression) != NULL_TREE)
4203 scope = TREE_TYPE (postfix_expression);
4204 /* According to the standard, no expression should
4205 ever have reference type. Unfortunately, we do not
4206 currently match the standard in this respect in
4207 that our internal representation of an expression
4208 may have reference type even when the standard says
4209 it does not. Therefore, we have to manually obtain
4210 the underlying type here. */
4211 if (TREE_CODE (scope) == REFERENCE_TYPE)
4212 scope = TREE_TYPE (scope);
4213 /* If the SCOPE is an OFFSET_TYPE, then we grab the
4214 type of the field. We get an OFFSET_TYPE for
4219 Probably, we should not get an OFFSET_TYPE here;
4220 that transformation should be made only if `&S::T'
4222 if (TREE_CODE (scope) == OFFSET_TYPE)
4223 scope = TREE_TYPE (scope);
4224 /* The type of the POSTFIX_EXPRESSION must be
4226 scope = complete_type_or_else (scope, NULL_TREE);
4227 /* Let the name lookup machinery know that we are
4228 processing a class member access expression. */
4229 parser->context->object_type = scope;
4230 /* If something went wrong, we want to be able to
4231 discern that case, as opposed to the case where
4232 there was no SCOPE due to the type of expression
4235 scope = error_mark_node;
4238 /* Consume the `.' or `->' operator. */
4239 cp_lexer_consume_token (parser->lexer);
4240 /* If the SCOPE is not a scalar type, we are looking at an
4241 ordinary class member access expression, rather than a
4242 pseudo-destructor-name. */
4243 if (!scope || !SCALAR_TYPE_P (scope))
4245 template_p = cp_parser_optional_template_keyword (parser);
4246 /* Parse the id-expression. */
4247 name = cp_parser_id_expression (parser,
4249 /*check_dependency_p=*/true,
4250 /*template_p=*/NULL);
4251 /* In general, build a SCOPE_REF if the member name is
4252 qualified. However, if the name was not dependent
4253 and has already been resolved; there is no need to
4254 build the SCOPE_REF. For example;
4256 struct X { void f(); };
4257 template <typename T> void f(T* t) { t->X::f(); }
4259 Even though "t" is dependent, "X::f" is not and has
4260 except that for a BASELINK there is no need to
4261 include scope information. */
4262 if (name != error_mark_node
4263 && !BASELINK_P (name)
4266 name = build_nt (SCOPE_REF, parser->scope, name);
4267 parser->scope = NULL_TREE;
4268 parser->qualifying_scope = NULL_TREE;
4269 parser->object_scope = NULL_TREE;
4272 = finish_class_member_access_expr (postfix_expression, name);
4274 /* Otherwise, try the pseudo-destructor-name production. */
4280 /* Parse the pseudo-destructor-name. */
4281 cp_parser_pseudo_destructor_name (parser, &s, &type);
4282 /* Form the call. */
4284 = finish_pseudo_destructor_expr (postfix_expression,
4285 s, TREE_TYPE (type));
4288 /* We no longer need to look up names in the scope of the
4289 object on the left-hand side of the `.' or `->'
4291 parser->context->object_type = NULL_TREE;
4292 idk = CP_PARSER_ID_KIND_NONE;
4297 /* postfix-expression ++ */
4298 /* Consume the `++' token. */
4299 cp_lexer_consume_token (parser->lexer);
4300 /* Generate a reprsentation for the complete expression. */
4302 = finish_increment_expr (postfix_expression,
4303 POSTINCREMENT_EXPR);
4304 idk = CP_PARSER_ID_KIND_NONE;
4307 case CPP_MINUS_MINUS:
4308 /* postfix-expression -- */
4309 /* Consume the `--' token. */
4310 cp_lexer_consume_token (parser->lexer);
4311 /* Generate a reprsentation for the complete expression. */
4313 = finish_increment_expr (postfix_expression,
4314 POSTDECREMENT_EXPR);
4315 idk = CP_PARSER_ID_KIND_NONE;
4319 return postfix_expression;
4323 /* We should never get here. */
4325 return error_mark_node;
4328 /* Parse an expression-list.
4331 assignment-expression
4332 expression-list, assignment-expression
4334 Returns a TREE_LIST. The TREE_VALUE of each node is a
4335 representation of an assignment-expression. Note that a TREE_LIST
4336 is returned even if there is only a single expression in the list. */
4339 cp_parser_expression_list (parser)
4342 tree expression_list = NULL_TREE;
4344 /* Consume expressions until there are no more. */
4349 /* Parse the next assignment-expression. */
4350 expr = cp_parser_assignment_expression (parser);
4351 /* Add it to the list. */
4352 expression_list = tree_cons (NULL_TREE, expr, expression_list);
4354 /* If the next token isn't a `,', then we are done. */
4355 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
4357 /* All uses of expression-list in the grammar are followed
4358 by a `)'. Therefore, if the next token is not a `)' an
4359 error will be issued, unless we are parsing tentatively.
4360 Skip ahead to see if there is another `,' before the `)';
4361 if so, we can go there and recover. */
4362 if (cp_parser_parsing_tentatively (parser)
4363 || cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
4364 || !cp_parser_skip_to_closing_parenthesis_or_comma (parser))
4368 /* Otherwise, consume the `,' and keep going. */
4369 cp_lexer_consume_token (parser->lexer);
4372 /* We built up the list in reverse order so we must reverse it now. */
4373 return nreverse (expression_list);
4376 /* Parse a pseudo-destructor-name.
4378 pseudo-destructor-name:
4379 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
4380 :: [opt] nested-name-specifier template template-id :: ~ type-name
4381 :: [opt] nested-name-specifier [opt] ~ type-name
4383 If either of the first two productions is used, sets *SCOPE to the
4384 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
4385 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
4386 or ERROR_MARK_NODE if no type-name is present. */
4389 cp_parser_pseudo_destructor_name (parser, scope, type)
4394 bool nested_name_specifier_p;
4396 /* Look for the optional `::' operator. */
4397 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
4398 /* Look for the optional nested-name-specifier. */
4399 nested_name_specifier_p
4400 = (cp_parser_nested_name_specifier_opt (parser,
4401 /*typename_keyword_p=*/false,
4402 /*check_dependency_p=*/true,
4405 /* Now, if we saw a nested-name-specifier, we might be doing the
4406 second production. */
4407 if (nested_name_specifier_p
4408 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
4410 /* Consume the `template' keyword. */
4411 cp_lexer_consume_token (parser->lexer);
4412 /* Parse the template-id. */
4413 cp_parser_template_id (parser,
4414 /*template_keyword_p=*/true,
4415 /*check_dependency_p=*/false);
4416 /* Look for the `::' token. */
4417 cp_parser_require (parser, CPP_SCOPE, "`::'");
4419 /* If the next token is not a `~', then there might be some
4420 additional qualification. */
4421 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
4423 /* Look for the type-name. */
4424 *scope = TREE_TYPE (cp_parser_type_name (parser));
4425 /* Look for the `::' token. */
4426 cp_parser_require (parser, CPP_SCOPE, "`::'");
4431 /* Look for the `~'. */
4432 cp_parser_require (parser, CPP_COMPL, "`~'");
4433 /* Look for the type-name again. We are not responsible for
4434 checking that it matches the first type-name. */
4435 *type = cp_parser_type_name (parser);
4438 /* Parse a unary-expression.
4444 unary-operator cast-expression
4445 sizeof unary-expression
4453 __extension__ cast-expression
4454 __alignof__ unary-expression
4455 __alignof__ ( type-id )
4456 __real__ cast-expression
4457 __imag__ cast-expression
4460 ADDRESS_P is true iff the unary-expression is appearing as the
4461 operand of the `&' operator.
4463 Returns a representation of the expresion. */
4466 cp_parser_unary_expression (cp_parser *parser, bool address_p)
4469 enum tree_code unary_operator;
4471 /* Peek at the next token. */
4472 token = cp_lexer_peek_token (parser->lexer);
4473 /* Some keywords give away the kind of expression. */
4474 if (token->type == CPP_KEYWORD)
4476 enum rid keyword = token->keyword;
4482 /* Consume the `alignof' token. */
4483 cp_lexer_consume_token (parser->lexer);
4484 /* Parse the operand. */
4485 return finish_alignof (cp_parser_sizeof_operand
4493 /* Consume the `sizeof' token. */
4494 cp_lexer_consume_token (parser->lexer);
4495 /* Parse the operand. */
4496 operand = cp_parser_sizeof_operand (parser, keyword);
4498 /* If the type of the operand cannot be determined build a
4500 if (TYPE_P (operand)
4501 ? cp_parser_dependent_type_p (operand)
4502 : cp_parser_type_dependent_expression_p (operand))
4503 return build_min (SIZEOF_EXPR, size_type_node, operand);
4504 /* Otherwise, compute the constant value. */
4506 return finish_sizeof (operand);
4510 return cp_parser_new_expression (parser);
4513 return cp_parser_delete_expression (parser);
4517 /* The saved value of the PEDANTIC flag. */
4521 /* Save away the PEDANTIC flag. */
4522 cp_parser_extension_opt (parser, &saved_pedantic);
4523 /* Parse the cast-expression. */
4524 expr = cp_parser_cast_expression (parser, /*address_p=*/false);
4525 /* Restore the PEDANTIC flag. */
4526 pedantic = saved_pedantic;
4536 /* Consume the `__real__' or `__imag__' token. */
4537 cp_lexer_consume_token (parser->lexer);
4538 /* Parse the cast-expression. */
4539 expression = cp_parser_cast_expression (parser,
4540 /*address_p=*/false);
4541 /* Create the complete representation. */
4542 return build_x_unary_op ((keyword == RID_REALPART
4543 ? REALPART_EXPR : IMAGPART_EXPR),
4553 /* Look for the `:: new' and `:: delete', which also signal the
4554 beginning of a new-expression, or delete-expression,
4555 respectively. If the next token is `::', then it might be one of
4557 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
4561 /* See if the token after the `::' is one of the keywords in
4562 which we're interested. */
4563 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
4564 /* If it's `new', we have a new-expression. */
4565 if (keyword == RID_NEW)
4566 return cp_parser_new_expression (parser);
4567 /* Similarly, for `delete'. */
4568 else if (keyword == RID_DELETE)
4569 return cp_parser_delete_expression (parser);
4572 /* Look for a unary operator. */
4573 unary_operator = cp_parser_unary_operator (token);
4574 /* The `++' and `--' operators can be handled similarly, even though
4575 they are not technically unary-operators in the grammar. */
4576 if (unary_operator == ERROR_MARK)
4578 if (token->type == CPP_PLUS_PLUS)
4579 unary_operator = PREINCREMENT_EXPR;
4580 else if (token->type == CPP_MINUS_MINUS)
4581 unary_operator = PREDECREMENT_EXPR;
4582 /* Handle the GNU address-of-label extension. */
4583 else if (cp_parser_allow_gnu_extensions_p (parser)
4584 && token->type == CPP_AND_AND)
4588 /* Consume the '&&' token. */
4589 cp_lexer_consume_token (parser->lexer);
4590 /* Look for the identifier. */
4591 identifier = cp_parser_identifier (parser);
4592 /* Create an expression representing the address. */
4593 return finish_label_address_expr (identifier);
4596 if (unary_operator != ERROR_MARK)
4598 tree cast_expression;
4600 /* Consume the operator token. */
4601 token = cp_lexer_consume_token (parser->lexer);
4602 /* Parse the cast-expression. */
4604 = cp_parser_cast_expression (parser, unary_operator == ADDR_EXPR);
4605 /* Now, build an appropriate representation. */
4606 switch (unary_operator)
4609 return build_x_indirect_ref (cast_expression, "unary *");
4612 return build_x_unary_op (ADDR_EXPR, cast_expression);
4616 case TRUTH_NOT_EXPR:
4617 case PREINCREMENT_EXPR:
4618 case PREDECREMENT_EXPR:
4619 return finish_unary_op_expr (unary_operator, cast_expression);
4622 return build_x_unary_op (BIT_NOT_EXPR, cast_expression);
4626 return error_mark_node;
4630 return cp_parser_postfix_expression (parser, address_p);
4633 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
4634 unary-operator, the corresponding tree code is returned. */
4636 static enum tree_code
4637 cp_parser_unary_operator (token)
4640 switch (token->type)
4643 return INDIRECT_REF;
4649 return CONVERT_EXPR;
4655 return TRUTH_NOT_EXPR;
4658 return BIT_NOT_EXPR;
4665 /* Parse a new-expression.
4667 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
4668 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
4670 Returns a representation of the expression. */
4673 cp_parser_new_expression (parser)
4676 bool global_scope_p;
4681 /* Look for the optional `::' operator. */
4683 = (cp_parser_global_scope_opt (parser,
4684 /*current_scope_valid_p=*/false)
4686 /* Look for the `new' operator. */
4687 cp_parser_require_keyword (parser, RID_NEW, "`new'");
4688 /* There's no easy way to tell a new-placement from the
4689 `( type-id )' construct. */
4690 cp_parser_parse_tentatively (parser);
4691 /* Look for a new-placement. */
4692 placement = cp_parser_new_placement (parser);
4693 /* If that didn't work out, there's no new-placement. */
4694 if (!cp_parser_parse_definitely (parser))
4695 placement = NULL_TREE;
4697 /* If the next token is a `(', then we have a parenthesized
4699 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4701 /* Consume the `('. */
4702 cp_lexer_consume_token (parser->lexer);
4703 /* Parse the type-id. */
4704 type = cp_parser_type_id (parser);
4705 /* Look for the closing `)'. */
4706 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4708 /* Otherwise, there must be a new-type-id. */
4710 type = cp_parser_new_type_id (parser);
4712 /* If the next token is a `(', then we have a new-initializer. */
4713 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4714 initializer = cp_parser_new_initializer (parser);
4716 initializer = NULL_TREE;
4718 /* Create a representation of the new-expression. */
4719 return build_new (placement, type, initializer, global_scope_p);
4722 /* Parse a new-placement.
4727 Returns the same representation as for an expression-list. */
4730 cp_parser_new_placement (parser)
4733 tree expression_list;
4735 /* Look for the opening `('. */
4736 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
4737 return error_mark_node;
4738 /* Parse the expression-list. */
4739 expression_list = cp_parser_expression_list (parser);
4740 /* Look for the closing `)'. */
4741 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4743 return expression_list;
4746 /* Parse a new-type-id.
4749 type-specifier-seq new-declarator [opt]
4751 Returns a TREE_LIST whose TREE_PURPOSE is the type-specifier-seq,
4752 and whose TREE_VALUE is the new-declarator. */
4755 cp_parser_new_type_id (parser)
4758 tree type_specifier_seq;
4760 const char *saved_message;
4762 /* The type-specifier sequence must not contain type definitions.
4763 (It cannot contain declarations of new types either, but if they
4764 are not definitions we will catch that because they are not
4766 saved_message = parser->type_definition_forbidden_message;
4767 parser->type_definition_forbidden_message
4768 = "types may not be defined in a new-type-id";
4769 /* Parse the type-specifier-seq. */
4770 type_specifier_seq = cp_parser_type_specifier_seq (parser);
4771 /* Restore the old message. */
4772 parser->type_definition_forbidden_message = saved_message;
4773 /* Parse the new-declarator. */
4774 declarator = cp_parser_new_declarator_opt (parser);
4776 return build_tree_list (type_specifier_seq, declarator);
4779 /* Parse an (optional) new-declarator.
4782 ptr-operator new-declarator [opt]
4783 direct-new-declarator
4785 Returns a representation of the declarator. See
4786 cp_parser_declarator for the representations used. */
4789 cp_parser_new_declarator_opt (parser)
4792 enum tree_code code;
4794 tree cv_qualifier_seq;
4796 /* We don't know if there's a ptr-operator next, or not. */
4797 cp_parser_parse_tentatively (parser);
4798 /* Look for a ptr-operator. */
4799 code = cp_parser_ptr_operator (parser, &type, &cv_qualifier_seq);
4800 /* If that worked, look for more new-declarators. */
4801 if (cp_parser_parse_definitely (parser))
4805 /* Parse another optional declarator. */
4806 declarator = cp_parser_new_declarator_opt (parser);
4808 /* Create the representation of the declarator. */
4809 if (code == INDIRECT_REF)
4810 declarator = make_pointer_declarator (cv_qualifier_seq,
4813 declarator = make_reference_declarator (cv_qualifier_seq,
4816 /* Handle the pointer-to-member case. */
4818 declarator = build_nt (SCOPE_REF, type, declarator);
4823 /* If the next token is a `[', there is a direct-new-declarator. */
4824 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
4825 return cp_parser_direct_new_declarator (parser);
4830 /* Parse a direct-new-declarator.
4832 direct-new-declarator:
4834 direct-new-declarator [constant-expression]
4836 Returns an ARRAY_REF, following the same conventions as are
4837 documented for cp_parser_direct_declarator. */
4840 cp_parser_direct_new_declarator (parser)
4843 tree declarator = NULL_TREE;
4849 /* Look for the opening `['. */
4850 cp_parser_require (parser, CPP_OPEN_SQUARE, "`['");
4851 /* The first expression is not required to be constant. */
4854 expression = cp_parser_expression (parser);
4855 /* The standard requires that the expression have integral
4856 type. DR 74 adds enumeration types. We believe that the
4857 real intent is that these expressions be handled like the
4858 expression in a `switch' condition, which also allows
4859 classes with a single conversion to integral or
4860 enumeration type. */
4861 if (!processing_template_decl)
4864 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
4869 error ("expression in new-declarator must have integral or enumeration type");
4870 expression = error_mark_node;
4874 /* But all the other expressions must be. */
4876 expression = cp_parser_constant_expression (parser);
4877 /* Look for the closing `]'. */
4878 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4880 /* Add this bound to the declarator. */
4881 declarator = build_nt (ARRAY_REF, declarator, expression);
4883 /* If the next token is not a `[', then there are no more
4885 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
4892 /* Parse a new-initializer.
4895 ( expression-list [opt] )
4897 Returns a reprsentation of the expression-list. If there is no
4898 expression-list, VOID_ZERO_NODE is returned. */
4901 cp_parser_new_initializer (parser)
4904 tree expression_list;
4906 /* Look for the opening parenthesis. */
4907 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
4908 /* If the next token is not a `)', then there is an
4910 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
4911 expression_list = cp_parser_expression_list (parser);
4913 expression_list = void_zero_node;
4914 /* Look for the closing parenthesis. */
4915 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4917 return expression_list;
4920 /* Parse a delete-expression.
4923 :: [opt] delete cast-expression
4924 :: [opt] delete [ ] cast-expression
4926 Returns a representation of the expression. */
4929 cp_parser_delete_expression (parser)
4932 bool global_scope_p;
4936 /* Look for the optional `::' operator. */
4938 = (cp_parser_global_scope_opt (parser,
4939 /*current_scope_valid_p=*/false)
4941 /* Look for the `delete' keyword. */
4942 cp_parser_require_keyword (parser, RID_DELETE, "`delete'");
4943 /* See if the array syntax is in use. */
4944 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
4946 /* Consume the `[' token. */
4947 cp_lexer_consume_token (parser->lexer);
4948 /* Look for the `]' token. */
4949 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4950 /* Remember that this is the `[]' construct. */
4956 /* Parse the cast-expression. */
4957 expression = cp_parser_cast_expression (parser, /*address_p=*/false);
4959 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
4962 /* Parse a cast-expression.
4966 ( type-id ) cast-expression
4968 Returns a representation of the expression. */
4971 cp_parser_cast_expression (cp_parser *parser, bool address_p)
4973 /* If it's a `(', then we might be looking at a cast. */
4974 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4976 tree type = NULL_TREE;
4977 tree expr = NULL_TREE;
4978 bool compound_literal_p;
4979 const char *saved_message;
4981 /* There's no way to know yet whether or not this is a cast.
4982 For example, `(int (3))' is a unary-expression, while `(int)
4983 3' is a cast. So, we resort to parsing tentatively. */
4984 cp_parser_parse_tentatively (parser);
4985 /* Types may not be defined in a cast. */
4986 saved_message = parser->type_definition_forbidden_message;
4987 parser->type_definition_forbidden_message
4988 = "types may not be defined in casts";
4989 /* Consume the `('. */
4990 cp_lexer_consume_token (parser->lexer);
4991 /* A very tricky bit is that `(struct S) { 3 }' is a
4992 compound-literal (which we permit in C++ as an extension).
4993 But, that construct is not a cast-expression -- it is a
4994 postfix-expression. (The reason is that `(struct S) { 3 }.i'
4995 is legal; if the compound-literal were a cast-expression,
4996 you'd need an extra set of parentheses.) But, if we parse
4997 the type-id, and it happens to be a class-specifier, then we
4998 will commit to the parse at that point, because we cannot
4999 undo the action that is done when creating a new class. So,
5000 then we cannot back up and do a postfix-expression.
5002 Therefore, we scan ahead to the closing `)', and check to see
5003 if the token after the `)' is a `{'. If so, we are not
5004 looking at a cast-expression.
5006 Save tokens so that we can put them back. */
5007 cp_lexer_save_tokens (parser->lexer);
5008 /* Skip tokens until the next token is a closing parenthesis.
5009 If we find the closing `)', and the next token is a `{', then
5010 we are looking at a compound-literal. */
5012 = (cp_parser_skip_to_closing_parenthesis (parser)
5013 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
5014 /* Roll back the tokens we skipped. */
5015 cp_lexer_rollback_tokens (parser->lexer);
5016 /* If we were looking at a compound-literal, simulate an error
5017 so that the call to cp_parser_parse_definitely below will
5019 if (compound_literal_p)
5020 cp_parser_simulate_error (parser);
5023 /* Look for the type-id. */
5024 type = cp_parser_type_id (parser);
5025 /* Look for the closing `)'. */
5026 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5029 /* Restore the saved message. */
5030 parser->type_definition_forbidden_message = saved_message;
5032 /* If all went well, this is a cast. */
5033 if (cp_parser_parse_definitely (parser))
5035 /* Parse the dependent expression. */
5036 expr = cp_parser_cast_expression (parser, /*address_p=*/false);
5037 /* Warn about old-style casts, if so requested. */
5038 if (warn_old_style_cast
5039 && !in_system_header
5040 && !VOID_TYPE_P (type)
5041 && current_lang_name != lang_name_c)
5042 warning ("use of old-style cast");
5043 /* Perform the cast. */
5044 expr = build_c_cast (type, expr);
5051 /* If we get here, then it's not a cast, so it must be a
5052 unary-expression. */
5053 return cp_parser_unary_expression (parser, address_p);
5056 /* Parse a pm-expression.
5060 pm-expression .* cast-expression
5061 pm-expression ->* cast-expression
5063 Returns a representation of the expression. */
5066 cp_parser_pm_expression (parser)
5072 /* Parse the cast-expresion. */
5073 cast_expr = cp_parser_cast_expression (parser, /*address_p=*/false);
5074 pm_expr = cast_expr;
5075 /* Now look for pointer-to-member operators. */
5079 enum cpp_ttype token_type;
5081 /* Peek at the next token. */
5082 token = cp_lexer_peek_token (parser->lexer);
5083 token_type = token->type;
5084 /* If it's not `.*' or `->*' there's no pointer-to-member
5086 if (token_type != CPP_DOT_STAR
5087 && token_type != CPP_DEREF_STAR)
5090 /* Consume the token. */
5091 cp_lexer_consume_token (parser->lexer);
5093 /* Parse another cast-expression. */
5094 cast_expr = cp_parser_cast_expression (parser, /*address_p=*/false);
5096 /* Build the representation of the pointer-to-member
5098 if (token_type == CPP_DEREF_STAR)
5099 pm_expr = build_x_binary_op (MEMBER_REF, pm_expr, cast_expr);
5101 pm_expr = build_m_component_ref (pm_expr, cast_expr);
5107 /* Parse a multiplicative-expression.
5109 mulitplicative-expression:
5111 multiplicative-expression * pm-expression
5112 multiplicative-expression / pm-expression
5113 multiplicative-expression % pm-expression
5115 Returns a representation of the expression. */
5118 cp_parser_multiplicative_expression (parser)
5121 static cp_parser_token_tree_map map = {
5122 { CPP_MULT, MULT_EXPR },
5123 { CPP_DIV, TRUNC_DIV_EXPR },
5124 { CPP_MOD, TRUNC_MOD_EXPR },
5125 { CPP_EOF, ERROR_MARK }
5128 return cp_parser_binary_expression (parser,
5130 cp_parser_pm_expression);
5133 /* Parse an additive-expression.
5135 additive-expression:
5136 multiplicative-expression
5137 additive-expression + multiplicative-expression
5138 additive-expression - multiplicative-expression
5140 Returns a representation of the expression. */
5143 cp_parser_additive_expression (parser)
5146 static cp_parser_token_tree_map map = {
5147 { CPP_PLUS, PLUS_EXPR },
5148 { CPP_MINUS, MINUS_EXPR },
5149 { CPP_EOF, ERROR_MARK }
5152 return cp_parser_binary_expression (parser,
5154 cp_parser_multiplicative_expression);
5157 /* Parse a shift-expression.
5161 shift-expression << additive-expression
5162 shift-expression >> additive-expression
5164 Returns a representation of the expression. */
5167 cp_parser_shift_expression (parser)
5170 static cp_parser_token_tree_map map = {
5171 { CPP_LSHIFT, LSHIFT_EXPR },
5172 { CPP_RSHIFT, RSHIFT_EXPR },
5173 { CPP_EOF, ERROR_MARK }
5176 return cp_parser_binary_expression (parser,
5178 cp_parser_additive_expression);
5181 /* Parse a relational-expression.
5183 relational-expression:
5185 relational-expression < shift-expression
5186 relational-expression > shift-expression
5187 relational-expression <= shift-expression
5188 relational-expression >= shift-expression
5192 relational-expression:
5193 relational-expression <? shift-expression
5194 relational-expression >? shift-expression
5196 Returns a representation of the expression. */
5199 cp_parser_relational_expression (parser)
5202 static cp_parser_token_tree_map map = {
5203 { CPP_LESS, LT_EXPR },
5204 { CPP_GREATER, GT_EXPR },
5205 { CPP_LESS_EQ, LE_EXPR },
5206 { CPP_GREATER_EQ, GE_EXPR },
5207 { CPP_MIN, MIN_EXPR },
5208 { CPP_MAX, MAX_EXPR },
5209 { CPP_EOF, ERROR_MARK }
5212 return cp_parser_binary_expression (parser,
5214 cp_parser_shift_expression);
5217 /* Parse an equality-expression.
5219 equality-expression:
5220 relational-expression
5221 equality-expression == relational-expression
5222 equality-expression != relational-expression
5224 Returns a representation of the expression. */
5227 cp_parser_equality_expression (parser)
5230 static cp_parser_token_tree_map map = {
5231 { CPP_EQ_EQ, EQ_EXPR },
5232 { CPP_NOT_EQ, NE_EXPR },
5233 { CPP_EOF, ERROR_MARK }
5236 return cp_parser_binary_expression (parser,
5238 cp_parser_relational_expression);
5241 /* Parse an and-expression.
5245 and-expression & equality-expression
5247 Returns a representation of the expression. */
5250 cp_parser_and_expression (parser)
5253 static cp_parser_token_tree_map map = {
5254 { CPP_AND, BIT_AND_EXPR },
5255 { CPP_EOF, ERROR_MARK }
5258 return cp_parser_binary_expression (parser,
5260 cp_parser_equality_expression);
5263 /* Parse an exclusive-or-expression.
5265 exclusive-or-expression:
5267 exclusive-or-expression ^ and-expression
5269 Returns a representation of the expression. */
5272 cp_parser_exclusive_or_expression (parser)
5275 static cp_parser_token_tree_map map = {
5276 { CPP_XOR, BIT_XOR_EXPR },
5277 { CPP_EOF, ERROR_MARK }
5280 return cp_parser_binary_expression (parser,
5282 cp_parser_and_expression);
5286 /* Parse an inclusive-or-expression.
5288 inclusive-or-expression:
5289 exclusive-or-expression
5290 inclusive-or-expression | exclusive-or-expression
5292 Returns a representation of the expression. */
5295 cp_parser_inclusive_or_expression (parser)
5298 static cp_parser_token_tree_map map = {
5299 { CPP_OR, BIT_IOR_EXPR },
5300 { CPP_EOF, ERROR_MARK }
5303 return cp_parser_binary_expression (parser,
5305 cp_parser_exclusive_or_expression);
5308 /* Parse a logical-and-expression.
5310 logical-and-expression:
5311 inclusive-or-expression
5312 logical-and-expression && inclusive-or-expression
5314 Returns a representation of the expression. */
5317 cp_parser_logical_and_expression (parser)
5320 static cp_parser_token_tree_map map = {
5321 { CPP_AND_AND, TRUTH_ANDIF_EXPR },
5322 { CPP_EOF, ERROR_MARK }
5325 return cp_parser_binary_expression (parser,
5327 cp_parser_inclusive_or_expression);
5330 /* Parse a logical-or-expression.
5332 logical-or-expression:
5333 logical-and-expresion
5334 logical-or-expression || logical-and-expression
5336 Returns a representation of the expression. */
5339 cp_parser_logical_or_expression (parser)
5342 static cp_parser_token_tree_map map = {
5343 { CPP_OR_OR, TRUTH_ORIF_EXPR },
5344 { CPP_EOF, ERROR_MARK }
5347 return cp_parser_binary_expression (parser,
5349 cp_parser_logical_and_expression);
5352 /* Parse a conditional-expression.
5354 conditional-expression:
5355 logical-or-expression
5356 logical-or-expression ? expression : assignment-expression
5360 conditional-expression:
5361 logical-or-expression ? : assignment-expression
5363 Returns a representation of the expression. */
5366 cp_parser_conditional_expression (parser)
5369 tree logical_or_expr;
5371 /* Parse the logical-or-expression. */
5372 logical_or_expr = cp_parser_logical_or_expression (parser);
5373 /* If the next token is a `?', then we have a real conditional
5375 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
5376 return cp_parser_question_colon_clause (parser, logical_or_expr);
5377 /* Otherwise, the value is simply the logical-or-expression. */
5379 return logical_or_expr;
5382 /* Parse the `? expression : assignment-expression' part of a
5383 conditional-expression. The LOGICAL_OR_EXPR is the
5384 logical-or-expression that started the conditional-expression.
5385 Returns a representation of the entire conditional-expression.
5387 This routine exists only so that it can be shared between
5388 cp_parser_conditional_expression and
5389 cp_parser_assignment_expression.
5391 ? expression : assignment-expression
5395 ? : assignment-expression */
5398 cp_parser_question_colon_clause (parser, logical_or_expr)
5400 tree logical_or_expr;
5403 tree assignment_expr;
5405 /* Consume the `?' token. */
5406 cp_lexer_consume_token (parser->lexer);
5407 if (cp_parser_allow_gnu_extensions_p (parser)
5408 && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
5409 /* Implicit true clause. */
5412 /* Parse the expression. */
5413 expr = cp_parser_expression (parser);
5415 /* The next token should be a `:'. */
5416 cp_parser_require (parser, CPP_COLON, "`:'");
5417 /* Parse the assignment-expression. */
5418 assignment_expr = cp_parser_assignment_expression (parser);
5420 /* Build the conditional-expression. */
5421 return build_x_conditional_expr (logical_or_expr,
5426 /* Parse an assignment-expression.
5428 assignment-expression:
5429 conditional-expression
5430 logical-or-expression assignment-operator assignment_expression
5433 Returns a representation for the expression. */
5436 cp_parser_assignment_expression (parser)
5441 /* If the next token is the `throw' keyword, then we're looking at
5442 a throw-expression. */
5443 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
5444 expr = cp_parser_throw_expression (parser);
5445 /* Otherwise, it must be that we are looking at a
5446 logical-or-expression. */
5449 /* Parse the logical-or-expression. */
5450 expr = cp_parser_logical_or_expression (parser);
5451 /* If the next token is a `?' then we're actually looking at a
5452 conditional-expression. */
5453 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
5454 return cp_parser_question_colon_clause (parser, expr);
5457 enum tree_code assignment_operator;
5459 /* If it's an assignment-operator, we're using the second
5462 = cp_parser_assignment_operator_opt (parser);
5463 if (assignment_operator != ERROR_MARK)
5467 /* Parse the right-hand side of the assignment. */
5468 rhs = cp_parser_assignment_expression (parser);
5469 /* Build the asignment expression. */
5470 expr = build_x_modify_expr (expr,
5471 assignment_operator,
5480 /* Parse an (optional) assignment-operator.
5482 assignment-operator: one of
5483 = *= /= %= += -= >>= <<= &= ^= |=
5487 assignment-operator: one of
5490 If the next token is an assignment operator, the corresponding tree
5491 code is returned, and the token is consumed. For example, for
5492 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
5493 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
5494 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
5495 operator, ERROR_MARK is returned. */
5497 static enum tree_code
5498 cp_parser_assignment_operator_opt (parser)
5504 /* Peek at the next toen. */
5505 token = cp_lexer_peek_token (parser->lexer);
5507 switch (token->type)
5518 op = TRUNC_DIV_EXPR;
5522 op = TRUNC_MOD_EXPR;
5562 /* Nothing else is an assignment operator. */
5566 /* If it was an assignment operator, consume it. */
5567 if (op != ERROR_MARK)
5568 cp_lexer_consume_token (parser->lexer);
5573 /* Parse an expression.
5576 assignment-expression
5577 expression , assignment-expression
5579 Returns a representation of the expression. */
5582 cp_parser_expression (parser)
5585 tree expression = NULL_TREE;
5586 bool saw_comma_p = false;
5590 tree assignment_expression;
5592 /* Parse the next assignment-expression. */
5593 assignment_expression
5594 = cp_parser_assignment_expression (parser);
5595 /* If this is the first assignment-expression, we can just
5598 expression = assignment_expression;
5599 /* Otherwise, chain the expressions together. It is unclear why
5600 we do not simply build COMPOUND_EXPRs as we go. */
5602 expression = tree_cons (NULL_TREE,
5603 assignment_expression,
5605 /* If the next token is not a comma, then we are done with the
5607 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5609 /* Consume the `,'. */
5610 cp_lexer_consume_token (parser->lexer);
5611 /* The first time we see a `,', we must take special action
5612 because the representation used for a single expression is
5613 different from that used for a list containing the single
5617 /* Remember that this expression has a `,' in it. */
5619 /* Turn the EXPRESSION into a TREE_LIST so that we can link
5620 additional expressions to it. */
5621 expression = build_tree_list (NULL_TREE, expression);
5625 /* Build a COMPOUND_EXPR to represent the entire expression, if
5626 necessary. We built up the list in reverse order, so we must
5627 straighten it out here. */
5629 expression = build_x_compound_expr (nreverse (expression));
5634 /* Parse a constant-expression.
5636 constant-expression:
5637 conditional-expression */
5640 cp_parser_constant_expression (parser)
5643 bool saved_constant_expression_p;
5646 /* It might seem that we could simply parse the
5647 conditional-expression, and then check to see if it were
5648 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
5649 one that the compiler can figure out is constant, possibly after
5650 doing some simplifications or optimizations. The standard has a
5651 precise definition of constant-expression, and we must honor
5652 that, even though it is somewhat more restrictive.
5658 is not a legal declaration, because `(2, 3)' is not a
5659 constant-expression. The `,' operator is forbidden in a
5660 constant-expression. However, GCC's constant-folding machinery
5661 will fold this operation to an INTEGER_CST for `3'. */
5663 /* Save the old setting of CONSTANT_EXPRESSION_P. */
5664 saved_constant_expression_p = parser->constant_expression_p;
5665 /* We are now parsing a constant-expression. */
5666 parser->constant_expression_p = true;
5667 /* Parse the conditional-expression. */
5668 expression = cp_parser_conditional_expression (parser);
5669 /* Restore the old setting of CONSTANT_EXPRESSION_P. */
5670 parser->constant_expression_p = saved_constant_expression_p;
5675 /* Statements [gram.stmt.stmt] */
5677 /* Parse a statement.
5681 expression-statement
5686 declaration-statement
5690 cp_parser_statement (parser)
5695 int statement_line_number;
5697 /* There is no statement yet. */
5698 statement = NULL_TREE;
5699 /* Peek at the next token. */
5700 token = cp_lexer_peek_token (parser->lexer);
5701 /* Remember the line number of the first token in the statement. */
5702 statement_line_number = token->line_number;
5703 /* If this is a keyword, then that will often determine what kind of
5704 statement we have. */
5705 if (token->type == CPP_KEYWORD)
5707 enum rid keyword = token->keyword;
5713 statement = cp_parser_labeled_statement (parser);
5718 statement = cp_parser_selection_statement (parser);
5724 statement = cp_parser_iteration_statement (parser);
5731 statement = cp_parser_jump_statement (parser);
5735 statement = cp_parser_try_block (parser);
5739 /* It might be a keyword like `int' that can start a
5740 declaration-statement. */
5744 else if (token->type == CPP_NAME)
5746 /* If the next token is a `:', then we are looking at a
5747 labeled-statement. */
5748 token = cp_lexer_peek_nth_token (parser->lexer, 2);
5749 if (token->type == CPP_COLON)
5750 statement = cp_parser_labeled_statement (parser);
5752 /* Anything that starts with a `{' must be a compound-statement. */
5753 else if (token->type == CPP_OPEN_BRACE)
5754 statement = cp_parser_compound_statement (parser);
5756 /* Everything else must be a declaration-statement or an
5757 expression-statement. Try for the declaration-statement
5758 first, unless we are looking at a `;', in which case we know that
5759 we have an expression-statement. */
5762 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
5764 cp_parser_parse_tentatively (parser);
5765 /* Try to parse the declaration-statement. */
5766 cp_parser_declaration_statement (parser);
5767 /* If that worked, we're done. */
5768 if (cp_parser_parse_definitely (parser))
5771 /* Look for an expression-statement instead. */
5772 statement = cp_parser_expression_statement (parser);
5775 /* Set the line number for the statement. */
5776 if (statement && statement_code_p (TREE_CODE (statement)))
5777 STMT_LINENO (statement) = statement_line_number;
5780 /* Parse a labeled-statement.
5783 identifier : statement
5784 case constant-expression : statement
5787 Returns the new CASE_LABEL, for a `case' or `default' label. For
5788 an ordinary label, returns a LABEL_STMT. */
5791 cp_parser_labeled_statement (parser)
5795 tree statement = NULL_TREE;
5797 /* The next token should be an identifier. */
5798 token = cp_lexer_peek_token (parser->lexer);
5799 if (token->type != CPP_NAME
5800 && token->type != CPP_KEYWORD)
5802 cp_parser_error (parser, "expected labeled-statement");
5803 return error_mark_node;
5806 switch (token->keyword)
5812 /* Consume the `case' token. */
5813 cp_lexer_consume_token (parser->lexer);
5814 /* Parse the constant-expression. */
5815 expr = cp_parser_constant_expression (parser);
5816 /* Create the label. */
5817 statement = finish_case_label (expr, NULL_TREE);
5822 /* Consume the `default' token. */
5823 cp_lexer_consume_token (parser->lexer);
5824 /* Create the label. */
5825 statement = finish_case_label (NULL_TREE, NULL_TREE);
5829 /* Anything else must be an ordinary label. */
5830 statement = finish_label_stmt (cp_parser_identifier (parser));
5834 /* Require the `:' token. */
5835 cp_parser_require (parser, CPP_COLON, "`:'");
5836 /* Parse the labeled statement. */
5837 cp_parser_statement (parser);
5839 /* Return the label, in the case of a `case' or `default' label. */
5843 /* Parse an expression-statement.
5845 expression-statement:
5848 Returns the new EXPR_STMT -- or NULL_TREE if the expression
5849 statement consists of nothing more than an `;'. */
5852 cp_parser_expression_statement (parser)
5857 /* If the next token is not a `;', then there is an expression to parse. */
5858 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
5859 statement = finish_expr_stmt (cp_parser_expression (parser));
5860 /* Otherwise, we do not even bother to build an EXPR_STMT. */
5864 statement = NULL_TREE;
5866 /* Consume the final `;'. */
5867 if (!cp_parser_require (parser, CPP_SEMICOLON, "`;'"))
5869 /* If there is additional (erroneous) input, skip to the end of
5871 cp_parser_skip_to_end_of_statement (parser);
5872 /* If the next token is now a `;', consume it. */
5873 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
5874 cp_lexer_consume_token (parser->lexer);
5880 /* Parse a compound-statement.
5883 { statement-seq [opt] }
5885 Returns a COMPOUND_STMT representing the statement. */
5888 cp_parser_compound_statement (cp_parser *parser)
5892 /* Consume the `{'. */
5893 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
5894 return error_mark_node;
5895 /* Begin the compound-statement. */
5896 compound_stmt = begin_compound_stmt (/*has_no_scope=*/0);
5897 /* Parse an (optional) statement-seq. */
5898 cp_parser_statement_seq_opt (parser);
5899 /* Finish the compound-statement. */
5900 finish_compound_stmt (/*has_no_scope=*/0, compound_stmt);
5901 /* Consume the `}'. */
5902 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
5904 return compound_stmt;
5907 /* Parse an (optional) statement-seq.
5911 statement-seq [opt] statement */
5914 cp_parser_statement_seq_opt (parser)
5917 /* Scan statements until there aren't any more. */
5920 /* If we're looking at a `}', then we've run out of statements. */
5921 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE)
5922 || cp_lexer_next_token_is (parser->lexer, CPP_EOF))
5925 /* Parse the statement. */
5926 cp_parser_statement (parser);
5930 /* Parse a selection-statement.
5932 selection-statement:
5933 if ( condition ) statement
5934 if ( condition ) statement else statement
5935 switch ( condition ) statement
5937 Returns the new IF_STMT or SWITCH_STMT. */
5940 cp_parser_selection_statement (parser)
5946 /* Peek at the next token. */
5947 token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
5949 /* See what kind of keyword it is. */
5950 keyword = token->keyword;
5959 /* Look for the `('. */
5960 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
5962 cp_parser_skip_to_end_of_statement (parser);
5963 return error_mark_node;
5966 /* Begin the selection-statement. */
5967 if (keyword == RID_IF)
5968 statement = begin_if_stmt ();
5970 statement = begin_switch_stmt ();
5972 /* Parse the condition. */
5973 condition = cp_parser_condition (parser);
5974 /* Look for the `)'. */
5975 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
5976 cp_parser_skip_to_closing_parenthesis (parser);
5978 if (keyword == RID_IF)
5982 /* Add the condition. */
5983 finish_if_stmt_cond (condition, statement);
5985 /* Parse the then-clause. */
5986 then_stmt = cp_parser_implicitly_scoped_statement (parser);
5987 finish_then_clause (statement);
5989 /* If the next token is `else', parse the else-clause. */
5990 if (cp_lexer_next_token_is_keyword (parser->lexer,
5995 /* Consume the `else' keyword. */
5996 cp_lexer_consume_token (parser->lexer);
5997 /* Parse the else-clause. */
5999 = cp_parser_implicitly_scoped_statement (parser);
6000 finish_else_clause (statement);
6003 /* Now we're all done with the if-statement. */
6010 /* Add the condition. */
6011 finish_switch_cond (condition, statement);
6013 /* Parse the body of the switch-statement. */
6014 body = cp_parser_implicitly_scoped_statement (parser);
6016 /* Now we're all done with the switch-statement. */
6017 finish_switch_stmt (statement);
6025 cp_parser_error (parser, "expected selection-statement");
6026 return error_mark_node;
6030 /* Parse a condition.
6034 type-specifier-seq declarator = assignment-expression
6039 type-specifier-seq declarator asm-specification [opt]
6040 attributes [opt] = assignment-expression
6042 Returns the expression that should be tested. */
6045 cp_parser_condition (parser)
6048 tree type_specifiers;
6049 const char *saved_message;
6051 /* Try the declaration first. */
6052 cp_parser_parse_tentatively (parser);
6053 /* New types are not allowed in the type-specifier-seq for a
6055 saved_message = parser->type_definition_forbidden_message;
6056 parser->type_definition_forbidden_message
6057 = "types may not be defined in conditions";
6058 /* Parse the type-specifier-seq. */
6059 type_specifiers = cp_parser_type_specifier_seq (parser);
6060 /* Restore the saved message. */
6061 parser->type_definition_forbidden_message = saved_message;
6062 /* If all is well, we might be looking at a declaration. */
6063 if (!cp_parser_error_occurred (parser))
6066 tree asm_specification;
6069 tree initializer = NULL_TREE;
6071 /* Parse the declarator. */
6072 declarator = cp_parser_declarator (parser,
6073 /*abstract_p=*/false,
6074 /*ctor_dtor_or_conv_p=*/NULL);
6075 /* Parse the attributes. */
6076 attributes = cp_parser_attributes_opt (parser);
6077 /* Parse the asm-specification. */
6078 asm_specification = cp_parser_asm_specification_opt (parser);
6079 /* If the next token is not an `=', then we might still be
6080 looking at an expression. For example:
6084 looks like a decl-specifier-seq and a declarator -- but then
6085 there is no `=', so this is an expression. */
6086 cp_parser_require (parser, CPP_EQ, "`='");
6087 /* If we did see an `=', then we are looking at a declaration
6089 if (cp_parser_parse_definitely (parser))
6091 /* Create the declaration. */
6092 decl = start_decl (declarator, type_specifiers,
6093 /*initialized_p=*/true,
6094 attributes, /*prefix_attributes=*/NULL_TREE);
6095 /* Parse the assignment-expression. */
6096 initializer = cp_parser_assignment_expression (parser);
6098 /* Process the initializer. */
6099 cp_finish_decl (decl,
6102 LOOKUP_ONLYCONVERTING);
6104 return convert_from_reference (decl);
6107 /* If we didn't even get past the declarator successfully, we are
6108 definitely not looking at a declaration. */
6110 cp_parser_abort_tentative_parse (parser);
6112 /* Otherwise, we are looking at an expression. */
6113 return cp_parser_expression (parser);
6116 /* Parse an iteration-statement.
6118 iteration-statement:
6119 while ( condition ) statement
6120 do statement while ( expression ) ;
6121 for ( for-init-statement condition [opt] ; expression [opt] )
6124 Returns the new WHILE_STMT, DO_STMT, or FOR_STMT. */
6127 cp_parser_iteration_statement (parser)
6134 /* Peek at the next token. */
6135 token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
6137 return error_mark_node;
6139 /* See what kind of keyword it is. */
6140 keyword = token->keyword;
6147 /* Begin the while-statement. */
6148 statement = begin_while_stmt ();
6149 /* Look for the `('. */
6150 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6151 /* Parse the condition. */
6152 condition = cp_parser_condition (parser);
6153 finish_while_stmt_cond (condition, statement);
6154 /* Look for the `)'. */
6155 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6156 /* Parse the dependent statement. */
6157 cp_parser_already_scoped_statement (parser);
6158 /* We're done with the while-statement. */
6159 finish_while_stmt (statement);
6167 /* Begin the do-statement. */
6168 statement = begin_do_stmt ();
6169 /* Parse the body of the do-statement. */
6170 cp_parser_implicitly_scoped_statement (parser);
6171 finish_do_body (statement);
6172 /* Look for the `while' keyword. */
6173 cp_parser_require_keyword (parser, RID_WHILE, "`while'");
6174 /* Look for the `('. */
6175 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6176 /* Parse the expression. */
6177 expression = cp_parser_expression (parser);
6178 /* We're done with the do-statement. */
6179 finish_do_stmt (expression, statement);
6180 /* Look for the `)'. */
6181 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6182 /* Look for the `;'. */
6183 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6189 tree condition = NULL_TREE;
6190 tree expression = NULL_TREE;
6192 /* Begin the for-statement. */
6193 statement = begin_for_stmt ();
6194 /* Look for the `('. */
6195 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6196 /* Parse the initialization. */
6197 cp_parser_for_init_statement (parser);
6198 finish_for_init_stmt (statement);
6200 /* If there's a condition, process it. */
6201 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6202 condition = cp_parser_condition (parser);
6203 finish_for_cond (condition, statement);
6204 /* Look for the `;'. */
6205 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6207 /* If there's an expression, process it. */
6208 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
6209 expression = cp_parser_expression (parser);
6210 finish_for_expr (expression, statement);
6211 /* Look for the `)'. */
6212 cp_parser_require (parser, CPP_CLOSE_PAREN, "`;'");
6214 /* Parse the body of the for-statement. */
6215 cp_parser_already_scoped_statement (parser);
6217 /* We're done with the for-statement. */
6218 finish_for_stmt (statement);
6223 cp_parser_error (parser, "expected iteration-statement");
6224 statement = error_mark_node;
6231 /* Parse a for-init-statement.
6234 expression-statement
6235 simple-declaration */
6238 cp_parser_for_init_statement (parser)
6241 /* If the next token is a `;', then we have an empty
6242 expression-statement. Gramatically, this is also a
6243 simple-declaration, but an invalid one, because it does not
6244 declare anything. Therefore, if we did not handle this case
6245 specially, we would issue an error message about an invalid
6247 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6249 /* We're going to speculatively look for a declaration, falling back
6250 to an expression, if necessary. */
6251 cp_parser_parse_tentatively (parser);
6252 /* Parse the declaration. */
6253 cp_parser_simple_declaration (parser,
6254 /*function_definition_allowed_p=*/false);
6255 /* If the tentative parse failed, then we shall need to look for an
6256 expression-statement. */
6257 if (cp_parser_parse_definitely (parser))
6261 cp_parser_expression_statement (parser);
6264 /* Parse a jump-statement.
6269 return expression [opt] ;
6277 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_STMT, or
6281 cp_parser_jump_statement (parser)
6284 tree statement = error_mark_node;
6288 /* Peek at the next token. */
6289 token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
6291 return error_mark_node;
6293 /* See what kind of keyword it is. */
6294 keyword = token->keyword;
6298 statement = finish_break_stmt ();
6299 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6303 statement = finish_continue_stmt ();
6304 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6311 /* If the next token is a `;', then there is no
6313 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6314 expr = cp_parser_expression (parser);
6317 /* Build the return-statement. */
6318 statement = finish_return_stmt (expr);
6319 /* Look for the final `;'. */
6320 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6325 /* Create the goto-statement. */
6326 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
6328 /* Issue a warning about this use of a GNU extension. */
6330 pedwarn ("ISO C++ forbids computed gotos");
6331 /* Consume the '*' token. */
6332 cp_lexer_consume_token (parser->lexer);
6333 /* Parse the dependent expression. */
6334 finish_goto_stmt (cp_parser_expression (parser));
6337 finish_goto_stmt (cp_parser_identifier (parser));
6338 /* Look for the final `;'. */
6339 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6343 cp_parser_error (parser, "expected jump-statement");
6350 /* Parse a declaration-statement.
6352 declaration-statement:
6353 block-declaration */
6356 cp_parser_declaration_statement (parser)
6359 /* Parse the block-declaration. */
6360 cp_parser_block_declaration (parser, /*statement_p=*/true);
6362 /* Finish off the statement. */
6366 /* Some dependent statements (like `if (cond) statement'), are
6367 implicitly in their own scope. In other words, if the statement is
6368 a single statement (as opposed to a compound-statement), it is
6369 none-the-less treated as if it were enclosed in braces. Any
6370 declarations appearing in the dependent statement are out of scope
6371 after control passes that point. This function parses a statement,
6372 but ensures that is in its own scope, even if it is not a
6375 Returns the new statement. */
6378 cp_parser_implicitly_scoped_statement (parser)
6383 /* If the token is not a `{', then we must take special action. */
6384 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
6386 /* Create a compound-statement. */
6387 statement = begin_compound_stmt (/*has_no_scope=*/0);
6388 /* Parse the dependent-statement. */
6389 cp_parser_statement (parser);
6390 /* Finish the dummy compound-statement. */
6391 finish_compound_stmt (/*has_no_scope=*/0, statement);
6393 /* Otherwise, we simply parse the statement directly. */
6395 statement = cp_parser_compound_statement (parser);
6397 /* Return the statement. */
6401 /* For some dependent statements (like `while (cond) statement'), we
6402 have already created a scope. Therefore, even if the dependent
6403 statement is a compound-statement, we do not want to create another
6407 cp_parser_already_scoped_statement (parser)
6410 /* If the token is not a `{', then we must take special action. */
6411 if (cp_lexer_next_token_is_not(parser->lexer, CPP_OPEN_BRACE))
6415 /* Create a compound-statement. */
6416 statement = begin_compound_stmt (/*has_no_scope=*/1);
6417 /* Parse the dependent-statement. */
6418 cp_parser_statement (parser);
6419 /* Finish the dummy compound-statement. */
6420 finish_compound_stmt (/*has_no_scope=*/1, statement);
6422 /* Otherwise, we simply parse the statement directly. */
6424 cp_parser_statement (parser);
6427 /* Declarations [gram.dcl.dcl] */
6429 /* Parse an optional declaration-sequence.
6433 declaration-seq declaration */
6436 cp_parser_declaration_seq_opt (parser)
6443 token = cp_lexer_peek_token (parser->lexer);
6445 if (token->type == CPP_CLOSE_BRACE
6446 || token->type == CPP_EOF)
6449 if (token->type == CPP_SEMICOLON)
6451 /* A declaration consisting of a single semicolon is
6452 invalid. Allow it unless we're being pedantic. */
6454 pedwarn ("extra `;'");
6455 cp_lexer_consume_token (parser->lexer);
6459 cp_parser_declaration (parser);
6463 /* Parse a declaration.
6468 template-declaration
6469 explicit-instantiation
6470 explicit-specialization
6471 linkage-specification
6472 namespace-definition */
6475 cp_parser_declaration (parser)
6481 /* Try to figure out what kind of declaration is present. */
6482 token1 = *cp_lexer_peek_token (parser->lexer);
6483 if (token1.type != CPP_EOF)
6484 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
6486 /* If the next token is `extern' and the following token is a string
6487 literal, then we have a linkage specification. */
6488 if (token1.keyword == RID_EXTERN
6489 && cp_parser_is_string_literal (&token2))
6490 cp_parser_linkage_specification (parser);
6491 /* If the next token is `template', then we have either a template
6492 declaration, an explicit instantiation, or an explicit
6494 else if (token1.keyword == RID_TEMPLATE)
6496 /* `template <>' indicates a template specialization. */
6497 if (token2.type == CPP_LESS
6498 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
6499 cp_parser_explicit_specialization (parser);
6500 /* `template <' indicates a template declaration. */
6501 else if (token2.type == CPP_LESS)
6502 cp_parser_template_declaration (parser, /*member_p=*/false);
6503 /* Anything else must be an explicit instantiation. */
6505 cp_parser_explicit_instantiation (parser);
6507 /* If the next token is `export', then we have a template
6509 else if (token1.keyword == RID_EXPORT)
6510 cp_parser_template_declaration (parser, /*member_p=*/false);
6511 /* If the next token is `extern', 'static' or 'inline' and the one
6512 after that is `template', we have a GNU extended explicit
6513 instantiation directive. */
6514 else if (cp_parser_allow_gnu_extensions_p (parser)
6515 && (token1.keyword == RID_EXTERN
6516 || token1.keyword == RID_STATIC
6517 || token1.keyword == RID_INLINE)
6518 && token2.keyword == RID_TEMPLATE)
6519 cp_parser_explicit_instantiation (parser);
6520 /* If the next token is `namespace', check for a named or unnamed
6521 namespace definition. */
6522 else if (token1.keyword == RID_NAMESPACE
6523 && (/* A named namespace definition. */
6524 (token2.type == CPP_NAME
6525 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
6527 /* An unnamed namespace definition. */
6528 || token2.type == CPP_OPEN_BRACE))
6529 cp_parser_namespace_definition (parser);
6530 /* We must have either a block declaration or a function
6533 /* Try to parse a block-declaration, or a function-definition. */
6534 cp_parser_block_declaration (parser, /*statement_p=*/false);
6537 /* Parse a block-declaration.
6542 namespace-alias-definition
6549 __extension__ block-declaration
6552 If STATEMENT_P is TRUE, then this block-declaration is ocurring as
6553 part of a declaration-statement. */
6556 cp_parser_block_declaration (cp_parser *parser,
6562 /* Check for the `__extension__' keyword. */
6563 if (cp_parser_extension_opt (parser, &saved_pedantic))
6565 /* Parse the qualified declaration. */
6566 cp_parser_block_declaration (parser, statement_p);
6567 /* Restore the PEDANTIC flag. */
6568 pedantic = saved_pedantic;
6573 /* Peek at the next token to figure out which kind of declaration is
6575 token1 = cp_lexer_peek_token (parser->lexer);
6577 /* If the next keyword is `asm', we have an asm-definition. */
6578 if (token1->keyword == RID_ASM)
6581 cp_parser_commit_to_tentative_parse (parser);
6582 cp_parser_asm_definition (parser);
6584 /* If the next keyword is `namespace', we have a
6585 namespace-alias-definition. */
6586 else if (token1->keyword == RID_NAMESPACE)
6587 cp_parser_namespace_alias_definition (parser);
6588 /* If the next keyword is `using', we have either a
6589 using-declaration or a using-directive. */
6590 else if (token1->keyword == RID_USING)
6595 cp_parser_commit_to_tentative_parse (parser);
6596 /* If the token after `using' is `namespace', then we have a
6598 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
6599 if (token2->keyword == RID_NAMESPACE)
6600 cp_parser_using_directive (parser);
6601 /* Otherwise, it's a using-declaration. */
6603 cp_parser_using_declaration (parser);
6605 /* If the next keyword is `__label__' we have a label declaration. */
6606 else if (token1->keyword == RID_LABEL)
6609 cp_parser_commit_to_tentative_parse (parser);
6610 cp_parser_label_declaration (parser);
6612 /* Anything else must be a simple-declaration. */
6614 cp_parser_simple_declaration (parser, !statement_p);
6617 /* Parse a simple-declaration.
6620 decl-specifier-seq [opt] init-declarator-list [opt] ;
6622 init-declarator-list:
6624 init-declarator-list , init-declarator
6626 If FUNCTION_DEFINTION_ALLOWED_P is TRUE, then we also recognize a
6627 function-definition as a simple-declaration. */
6630 cp_parser_simple_declaration (parser, function_definition_allowed_p)
6632 bool function_definition_allowed_p;
6634 tree decl_specifiers;
6637 bool declares_class_or_enum;
6638 bool saw_declarator;
6640 /* Defer access checks until we know what is being declared; the
6641 checks for names appearing in the decl-specifier-seq should be
6642 done as if we were in the scope of the thing being declared. */
6643 cp_parser_start_deferring_access_checks (parser);
6644 /* Parse the decl-specifier-seq. We have to keep track of whether
6645 or not the decl-specifier-seq declares a named class or
6646 enumeration type, since that is the only case in which the
6647 init-declarator-list is allowed to be empty.
6651 In a simple-declaration, the optional init-declarator-list can be
6652 omitted only when declaring a class or enumeration, that is when
6653 the decl-specifier-seq contains either a class-specifier, an
6654 elaborated-type-specifier, or an enum-specifier. */
6656 = cp_parser_decl_specifier_seq (parser,
6657 CP_PARSER_FLAGS_OPTIONAL,
6659 &declares_class_or_enum);
6660 /* We no longer need to defer access checks. */
6661 access_checks = cp_parser_stop_deferring_access_checks (parser);
6663 /* Keep going until we hit the `;' at the end of the simple
6665 saw_declarator = false;
6666 while (cp_lexer_next_token_is_not (parser->lexer,
6670 bool function_definition_p;
6672 saw_declarator = true;
6673 /* Parse the init-declarator. */
6674 cp_parser_init_declarator (parser, decl_specifiers, attributes,
6676 function_definition_allowed_p,
6678 &function_definition_p);
6679 /* Handle function definitions specially. */
6680 if (function_definition_p)
6682 /* If the next token is a `,', then we are probably
6683 processing something like:
6687 which is erroneous. */
6688 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
6689 error ("mixing declarations and function-definitions is forbidden");
6690 /* Otherwise, we're done with the list of declarators. */
6694 /* The next token should be either a `,' or a `;'. */
6695 token = cp_lexer_peek_token (parser->lexer);
6696 /* If it's a `,', there are more declarators to come. */
6697 if (token->type == CPP_COMMA)
6698 cp_lexer_consume_token (parser->lexer);
6699 /* If it's a `;', we are done. */
6700 else if (token->type == CPP_SEMICOLON)
6702 /* Anything else is an error. */
6705 cp_parser_error (parser, "expected `,' or `;'");
6706 /* Skip tokens until we reach the end of the statement. */
6707 cp_parser_skip_to_end_of_statement (parser);
6710 /* After the first time around, a function-definition is not
6711 allowed -- even if it was OK at first. For example:
6716 function_definition_allowed_p = false;
6719 /* Issue an error message if no declarators are present, and the
6720 decl-specifier-seq does not itself declare a class or
6722 if (!saw_declarator)
6724 if (cp_parser_declares_only_class_p (parser))
6725 shadow_tag (decl_specifiers);
6726 /* Perform any deferred access checks. */
6727 cp_parser_perform_deferred_access_checks (access_checks);
6730 /* Consume the `;'. */
6731 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6733 /* Mark all the classes that appeared in the decl-specifier-seq as
6734 having received a `;'. */
6735 note_list_got_semicolon (decl_specifiers);
6738 /* Parse a decl-specifier-seq.
6741 decl-specifier-seq [opt] decl-specifier
6744 storage-class-specifier
6753 decl-specifier-seq [opt] attributes
6755 Returns a TREE_LIST, giving the decl-specifiers in the order they
6756 appear in the source code. The TREE_VALUE of each node is the
6757 decl-specifier. For a keyword (such as `auto' or `friend'), the
6758 TREE_VALUE is simply the correspoding TREE_IDENTIFIER. For the
6759 representation of a type-specifier, see cp_parser_type_specifier.
6761 If there are attributes, they will be stored in *ATTRIBUTES,
6762 represented as described above cp_parser_attributes.
6764 If FRIEND_IS_NOT_CLASS_P is non-NULL, and the `friend' specifier
6765 appears, and the entity that will be a friend is not going to be a
6766 class, then *FRIEND_IS_NOT_CLASS_P will be set to TRUE. Note that
6767 even if *FRIEND_IS_NOT_CLASS_P is FALSE, the entity to which
6768 friendship is granted might not be a class. */
6771 cp_parser_decl_specifier_seq (parser, flags, attributes,
6772 declares_class_or_enum)
6774 cp_parser_flags flags;
6776 bool *declares_class_or_enum;
6778 tree decl_specs = NULL_TREE;
6779 bool friend_p = false;
6781 /* Assume no class or enumeration type is declared. */
6782 *declares_class_or_enum = false;
6784 /* Assume there are no attributes. */
6785 *attributes = NULL_TREE;
6787 /* Keep reading specifiers until there are no more to read. */
6790 tree decl_spec = NULL_TREE;
6794 /* Peek at the next token. */
6795 token = cp_lexer_peek_token (parser->lexer);
6796 /* Handle attributes. */
6797 if (token->keyword == RID_ATTRIBUTE)
6799 /* Parse the attributes. */
6800 decl_spec = cp_parser_attributes_opt (parser);
6801 /* Add them to the list. */
6802 *attributes = chainon (*attributes, decl_spec);
6805 /* If the next token is an appropriate keyword, we can simply
6806 add it to the list. */
6807 switch (token->keyword)
6813 /* The representation of the specifier is simply the
6814 appropriate TREE_IDENTIFIER node. */
6815 decl_spec = token->value;
6816 /* Consume the token. */
6817 cp_lexer_consume_token (parser->lexer);
6820 /* function-specifier:
6827 decl_spec = cp_parser_function_specifier_opt (parser);
6833 /* The representation of the specifier is simply the
6834 appropriate TREE_IDENTIFIER node. */
6835 decl_spec = token->value;
6836 /* Consume the token. */
6837 cp_lexer_consume_token (parser->lexer);
6840 /* storage-class-specifier:
6855 decl_spec = cp_parser_storage_class_specifier_opt (parser);
6862 /* Constructors are a special case. The `S' in `S()' is not a
6863 decl-specifier; it is the beginning of the declarator. */
6864 constructor_p = (!decl_spec
6865 && cp_parser_constructor_declarator_p (parser,
6868 /* If we don't have a DECL_SPEC yet, then we must be looking at
6869 a type-specifier. */
6870 if (!decl_spec && !constructor_p)
6872 bool decl_spec_declares_class_or_enum;
6873 bool is_cv_qualifier;
6876 = cp_parser_type_specifier (parser, flags,
6878 /*is_declaration=*/true,
6879 &decl_spec_declares_class_or_enum,
6882 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
6884 /* If this type-specifier referenced a user-defined type
6885 (a typedef, class-name, etc.), then we can't allow any
6886 more such type-specifiers henceforth.
6890 The longest sequence of decl-specifiers that could
6891 possibly be a type name is taken as the
6892 decl-specifier-seq of a declaration. The sequence shall
6893 be self-consistent as described below.
6897 As a general rule, at most one type-specifier is allowed
6898 in the complete decl-specifier-seq of a declaration. The
6899 only exceptions are the following:
6901 -- const or volatile can be combined with any other
6904 -- signed or unsigned can be combined with char, long,
6912 void g (const int Pc);
6914 Here, Pc is *not* part of the decl-specifier seq; it's
6915 the declarator. Therefore, once we see a type-specifier
6916 (other than a cv-qualifier), we forbid any additional
6917 user-defined types. We *do* still allow things like `int
6918 int' to be considered a decl-specifier-seq, and issue the
6919 error message later. */
6920 if (decl_spec && !is_cv_qualifier)
6921 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
6924 /* If we still do not have a DECL_SPEC, then there are no more
6928 /* Issue an error message, unless the entire construct was
6930 if (!(flags & CP_PARSER_FLAGS_OPTIONAL))
6932 cp_parser_error (parser, "expected decl specifier");
6933 return error_mark_node;
6939 /* Add the DECL_SPEC to the list of specifiers. */
6940 decl_specs = tree_cons (NULL_TREE, decl_spec, decl_specs);
6942 /* After we see one decl-specifier, further decl-specifiers are
6944 flags |= CP_PARSER_FLAGS_OPTIONAL;
6947 /* We have built up the DECL_SPECS in reverse order. Return them in
6948 the correct order. */
6949 return nreverse (decl_specs);
6952 /* Parse an (optional) storage-class-specifier.
6954 storage-class-specifier:
6963 storage-class-specifier:
6966 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
6969 cp_parser_storage_class_specifier_opt (parser)
6972 switch (cp_lexer_peek_token (parser->lexer)->keyword)
6980 /* Consume the token. */
6981 return cp_lexer_consume_token (parser->lexer)->value;
6988 /* Parse an (optional) function-specifier.
6995 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
6998 cp_parser_function_specifier_opt (parser)
7001 switch (cp_lexer_peek_token (parser->lexer)->keyword)
7006 /* Consume the token. */
7007 return cp_lexer_consume_token (parser->lexer)->value;
7014 /* Parse a linkage-specification.
7016 linkage-specification:
7017 extern string-literal { declaration-seq [opt] }
7018 extern string-literal declaration */
7021 cp_parser_linkage_specification (parser)
7027 /* Look for the `extern' keyword. */
7028 cp_parser_require_keyword (parser, RID_EXTERN, "`extern'");
7030 /* Peek at the next token. */
7031 token = cp_lexer_peek_token (parser->lexer);
7032 /* If it's not a string-literal, then there's a problem. */
7033 if (!cp_parser_is_string_literal (token))
7035 cp_parser_error (parser, "expected language-name");
7038 /* Consume the token. */
7039 cp_lexer_consume_token (parser->lexer);
7041 /* Transform the literal into an identifier. If the literal is a
7042 wide-character string, or contains embedded NULs, then we can't
7043 handle it as the user wants. */
7044 if (token->type == CPP_WSTRING
7045 || (strlen (TREE_STRING_POINTER (token->value))
7046 != (size_t) (TREE_STRING_LENGTH (token->value) - 1)))
7048 cp_parser_error (parser, "invalid linkage-specification");
7049 /* Assume C++ linkage. */
7050 linkage = get_identifier ("c++");
7052 /* If it's a simple string constant, things are easier. */
7054 linkage = get_identifier (TREE_STRING_POINTER (token->value));
7056 /* We're now using the new linkage. */
7057 push_lang_context (linkage);
7059 /* If the next token is a `{', then we're using the first
7061 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7063 /* Consume the `{' token. */
7064 cp_lexer_consume_token (parser->lexer);
7065 /* Parse the declarations. */
7066 cp_parser_declaration_seq_opt (parser);
7067 /* Look for the closing `}'. */
7068 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
7070 /* Otherwise, there's just one declaration. */
7073 bool saved_in_unbraced_linkage_specification_p;
7075 saved_in_unbraced_linkage_specification_p
7076 = parser->in_unbraced_linkage_specification_p;
7077 parser->in_unbraced_linkage_specification_p = true;
7078 have_extern_spec = true;
7079 cp_parser_declaration (parser);
7080 have_extern_spec = false;
7081 parser->in_unbraced_linkage_specification_p
7082 = saved_in_unbraced_linkage_specification_p;
7085 /* We're done with the linkage-specification. */
7086 pop_lang_context ();
7089 /* Special member functions [gram.special] */
7091 /* Parse a conversion-function-id.
7093 conversion-function-id:
7094 operator conversion-type-id
7096 Returns an IDENTIFIER_NODE representing the operator. */
7099 cp_parser_conversion_function_id (parser)
7104 tree saved_qualifying_scope;
7105 tree saved_object_scope;
7107 /* Look for the `operator' token. */
7108 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
7109 return error_mark_node;
7110 /* When we parse the conversion-type-id, the current scope will be
7111 reset. However, we need that information in able to look up the
7112 conversion function later, so we save it here. */
7113 saved_scope = parser->scope;
7114 saved_qualifying_scope = parser->qualifying_scope;
7115 saved_object_scope = parser->object_scope;
7116 /* We must enter the scope of the class so that the names of
7117 entities declared within the class are available in the
7118 conversion-type-id. For example, consider:
7125 S::operator I() { ... }
7127 In order to see that `I' is a type-name in the definition, we
7128 must be in the scope of `S'. */
7130 push_scope (saved_scope);
7131 /* Parse the conversion-type-id. */
7132 type = cp_parser_conversion_type_id (parser);
7133 /* Leave the scope of the class, if any. */
7135 pop_scope (saved_scope);
7136 /* Restore the saved scope. */
7137 parser->scope = saved_scope;
7138 parser->qualifying_scope = saved_qualifying_scope;
7139 parser->object_scope = saved_object_scope;
7140 /* If the TYPE is invalid, indicate failure. */
7141 if (type == error_mark_node)
7142 return error_mark_node;
7143 return mangle_conv_op_name_for_type (type);
7146 /* Parse a conversion-type-id:
7149 type-specifier-seq conversion-declarator [opt]
7151 Returns the TYPE specified. */
7154 cp_parser_conversion_type_id (parser)
7158 tree type_specifiers;
7161 /* Parse the attributes. */
7162 attributes = cp_parser_attributes_opt (parser);
7163 /* Parse the type-specifiers. */
7164 type_specifiers = cp_parser_type_specifier_seq (parser);
7165 /* If that didn't work, stop. */
7166 if (type_specifiers == error_mark_node)
7167 return error_mark_node;
7168 /* Parse the conversion-declarator. */
7169 declarator = cp_parser_conversion_declarator_opt (parser);
7171 return grokdeclarator (declarator, type_specifiers, TYPENAME,
7172 /*initialized=*/0, &attributes);
7175 /* Parse an (optional) conversion-declarator.
7177 conversion-declarator:
7178 ptr-operator conversion-declarator [opt]
7180 Returns a representation of the declarator. See
7181 cp_parser_declarator for details. */
7184 cp_parser_conversion_declarator_opt (parser)
7187 enum tree_code code;
7189 tree cv_qualifier_seq;
7191 /* We don't know if there's a ptr-operator next, or not. */
7192 cp_parser_parse_tentatively (parser);
7193 /* Try the ptr-operator. */
7194 code = cp_parser_ptr_operator (parser, &class_type,
7196 /* If it worked, look for more conversion-declarators. */
7197 if (cp_parser_parse_definitely (parser))
7201 /* Parse another optional declarator. */
7202 declarator = cp_parser_conversion_declarator_opt (parser);
7204 /* Create the representation of the declarator. */
7205 if (code == INDIRECT_REF)
7206 declarator = make_pointer_declarator (cv_qualifier_seq,
7209 declarator = make_reference_declarator (cv_qualifier_seq,
7212 /* Handle the pointer-to-member case. */
7214 declarator = build_nt (SCOPE_REF, class_type, declarator);
7222 /* Parse an (optional) ctor-initializer.
7225 : mem-initializer-list
7227 Returns TRUE iff the ctor-initializer was actually present. */
7230 cp_parser_ctor_initializer_opt (parser)
7233 /* If the next token is not a `:', then there is no
7234 ctor-initializer. */
7235 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
7237 /* Do default initialization of any bases and members. */
7238 if (DECL_CONSTRUCTOR_P (current_function_decl))
7239 finish_mem_initializers (NULL_TREE);
7244 /* Consume the `:' token. */
7245 cp_lexer_consume_token (parser->lexer);
7246 /* And the mem-initializer-list. */
7247 cp_parser_mem_initializer_list (parser);
7252 /* Parse a mem-initializer-list.
7254 mem-initializer-list:
7256 mem-initializer , mem-initializer-list */
7259 cp_parser_mem_initializer_list (parser)
7262 tree mem_initializer_list = NULL_TREE;
7264 /* Let the semantic analysis code know that we are starting the
7265 mem-initializer-list. */
7266 begin_mem_initializers ();
7268 /* Loop through the list. */
7271 tree mem_initializer;
7273 /* Parse the mem-initializer. */
7274 mem_initializer = cp_parser_mem_initializer (parser);
7275 /* Add it to the list, unless it was erroneous. */
7276 if (mem_initializer)
7278 TREE_CHAIN (mem_initializer) = mem_initializer_list;
7279 mem_initializer_list = mem_initializer;
7281 /* If the next token is not a `,', we're done. */
7282 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7284 /* Consume the `,' token. */
7285 cp_lexer_consume_token (parser->lexer);
7288 /* Perform semantic analysis. */
7289 finish_mem_initializers (mem_initializer_list);
7292 /* Parse a mem-initializer.
7295 mem-initializer-id ( expression-list [opt] )
7300 ( expresion-list [opt] )
7302 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
7303 class) or FIELD_DECL (for a non-static data member) to initialize;
7304 the TREE_VALUE is the expression-list. */
7307 cp_parser_mem_initializer (parser)
7310 tree mem_initializer_id;
7311 tree expression_list;
7313 /* Find out what is being initialized. */
7314 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7316 pedwarn ("anachronistic old-style base class initializer");
7317 mem_initializer_id = NULL_TREE;
7320 mem_initializer_id = cp_parser_mem_initializer_id (parser);
7321 /* Look for the opening `('. */
7322 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
7323 /* Parse the expression-list. */
7324 if (cp_lexer_next_token_is_not (parser->lexer,
7326 expression_list = cp_parser_expression_list (parser);
7328 expression_list = void_type_node;
7329 /* Look for the closing `)'. */
7330 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
7332 return expand_member_init (mem_initializer_id,
7336 /* Parse a mem-initializer-id.
7339 :: [opt] nested-name-specifier [opt] class-name
7342 Returns a TYPE indicating the class to be initializer for the first
7343 production. Returns an IDENTIFIER_NODE indicating the data member
7344 to be initialized for the second production. */
7347 cp_parser_mem_initializer_id (parser)
7350 bool global_scope_p;
7351 bool nested_name_specifier_p;
7354 /* Look for the optional `::' operator. */
7356 = (cp_parser_global_scope_opt (parser,
7357 /*current_scope_valid_p=*/false)
7359 /* Look for the optional nested-name-specifier. The simplest way to
7364 The keyword `typename' is not permitted in a base-specifier or
7365 mem-initializer; in these contexts a qualified name that
7366 depends on a template-parameter is implicitly assumed to be a
7369 is to assume that we have seen the `typename' keyword at this
7371 nested_name_specifier_p
7372 = (cp_parser_nested_name_specifier_opt (parser,
7373 /*typename_keyword_p=*/true,
7374 /*check_dependency_p=*/true,
7377 /* If there is a `::' operator or a nested-name-specifier, then we
7378 are definitely looking for a class-name. */
7379 if (global_scope_p || nested_name_specifier_p)
7380 return cp_parser_class_name (parser,
7381 /*typename_keyword_p=*/true,
7382 /*template_keyword_p=*/false,
7384 /*check_access_p=*/true,
7385 /*check_dependency_p=*/true,
7386 /*class_head_p=*/false);
7387 /* Otherwise, we could also be looking for an ordinary identifier. */
7388 cp_parser_parse_tentatively (parser);
7389 /* Try a class-name. */
7390 id = cp_parser_class_name (parser,
7391 /*typename_keyword_p=*/true,
7392 /*template_keyword_p=*/false,
7394 /*check_access_p=*/true,
7395 /*check_dependency_p=*/true,
7396 /*class_head_p=*/false);
7397 /* If we found one, we're done. */
7398 if (cp_parser_parse_definitely (parser))
7400 /* Otherwise, look for an ordinary identifier. */
7401 return cp_parser_identifier (parser);
7404 /* Overloading [gram.over] */
7406 /* Parse an operator-function-id.
7408 operator-function-id:
7411 Returns an IDENTIFIER_NODE for the operator which is a
7412 human-readable spelling of the identifier, e.g., `operator +'. */
7415 cp_parser_operator_function_id (parser)
7418 /* Look for the `operator' keyword. */
7419 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
7420 return error_mark_node;
7421 /* And then the name of the operator itself. */
7422 return cp_parser_operator (parser);
7425 /* Parse an operator.
7428 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
7429 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
7430 || ++ -- , ->* -> () []
7437 Returns an IDENTIFIER_NODE for the operator which is a
7438 human-readable spelling of the identifier, e.g., `operator +'. */
7441 cp_parser_operator (parser)
7444 tree id = NULL_TREE;
7447 /* Peek at the next token. */
7448 token = cp_lexer_peek_token (parser->lexer);
7449 /* Figure out which operator we have. */
7450 switch (token->type)
7456 /* The keyword should be either `new' or `delete'. */
7457 if (token->keyword == RID_NEW)
7459 else if (token->keyword == RID_DELETE)
7464 /* Consume the `new' or `delete' token. */
7465 cp_lexer_consume_token (parser->lexer);
7467 /* Peek at the next token. */
7468 token = cp_lexer_peek_token (parser->lexer);
7469 /* If it's a `[' token then this is the array variant of the
7471 if (token->type == CPP_OPEN_SQUARE)
7473 /* Consume the `[' token. */
7474 cp_lexer_consume_token (parser->lexer);
7475 /* Look for the `]' token. */
7476 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
7477 id = ansi_opname (op == NEW_EXPR
7478 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
7480 /* Otherwise, we have the non-array variant. */
7482 id = ansi_opname (op);
7488 id = ansi_opname (PLUS_EXPR);
7492 id = ansi_opname (MINUS_EXPR);
7496 id = ansi_opname (MULT_EXPR);
7500 id = ansi_opname (TRUNC_DIV_EXPR);
7504 id = ansi_opname (TRUNC_MOD_EXPR);
7508 id = ansi_opname (BIT_XOR_EXPR);
7512 id = ansi_opname (BIT_AND_EXPR);
7516 id = ansi_opname (BIT_IOR_EXPR);
7520 id = ansi_opname (BIT_NOT_EXPR);
7524 id = ansi_opname (TRUTH_NOT_EXPR);
7528 id = ansi_assopname (NOP_EXPR);
7532 id = ansi_opname (LT_EXPR);
7536 id = ansi_opname (GT_EXPR);
7540 id = ansi_assopname (PLUS_EXPR);
7544 id = ansi_assopname (MINUS_EXPR);
7548 id = ansi_assopname (MULT_EXPR);
7552 id = ansi_assopname (TRUNC_DIV_EXPR);
7556 id = ansi_assopname (TRUNC_MOD_EXPR);
7560 id = ansi_assopname (BIT_XOR_EXPR);
7564 id = ansi_assopname (BIT_AND_EXPR);
7568 id = ansi_assopname (BIT_IOR_EXPR);
7572 id = ansi_opname (LSHIFT_EXPR);
7576 id = ansi_opname (RSHIFT_EXPR);
7580 id = ansi_assopname (LSHIFT_EXPR);
7584 id = ansi_assopname (RSHIFT_EXPR);
7588 id = ansi_opname (EQ_EXPR);
7592 id = ansi_opname (NE_EXPR);
7596 id = ansi_opname (LE_EXPR);
7599 case CPP_GREATER_EQ:
7600 id = ansi_opname (GE_EXPR);
7604 id = ansi_opname (TRUTH_ANDIF_EXPR);
7608 id = ansi_opname (TRUTH_ORIF_EXPR);
7612 id = ansi_opname (POSTINCREMENT_EXPR);
7615 case CPP_MINUS_MINUS:
7616 id = ansi_opname (PREDECREMENT_EXPR);
7620 id = ansi_opname (COMPOUND_EXPR);
7623 case CPP_DEREF_STAR:
7624 id = ansi_opname (MEMBER_REF);
7628 id = ansi_opname (COMPONENT_REF);
7631 case CPP_OPEN_PAREN:
7632 /* Consume the `('. */
7633 cp_lexer_consume_token (parser->lexer);
7634 /* Look for the matching `)'. */
7635 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
7636 return ansi_opname (CALL_EXPR);
7638 case CPP_OPEN_SQUARE:
7639 /* Consume the `['. */
7640 cp_lexer_consume_token (parser->lexer);
7641 /* Look for the matching `]'. */
7642 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
7643 return ansi_opname (ARRAY_REF);
7647 id = ansi_opname (MIN_EXPR);
7651 id = ansi_opname (MAX_EXPR);
7655 id = ansi_assopname (MIN_EXPR);
7659 id = ansi_assopname (MAX_EXPR);
7663 /* Anything else is an error. */
7667 /* If we have selected an identifier, we need to consume the
7670 cp_lexer_consume_token (parser->lexer);
7671 /* Otherwise, no valid operator name was present. */
7674 cp_parser_error (parser, "expected operator");
7675 id = error_mark_node;
7681 /* Parse a template-declaration.
7683 template-declaration:
7684 export [opt] template < template-parameter-list > declaration
7686 If MEMBER_P is TRUE, this template-declaration occurs within a
7689 The grammar rule given by the standard isn't correct. What
7692 template-declaration:
7693 export [opt] template-parameter-list-seq
7694 decl-specifier-seq [opt] init-declarator [opt] ;
7695 export [opt] template-parameter-list-seq
7698 template-parameter-list-seq:
7699 template-parameter-list-seq [opt]
7700 template < template-parameter-list > */
7703 cp_parser_template_declaration (parser, member_p)
7707 /* Check for `export'. */
7708 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
7710 /* Consume the `export' token. */
7711 cp_lexer_consume_token (parser->lexer);
7712 /* Warn that we do not support `export'. */
7713 warning ("keyword `export' not implemented, and will be ignored");
7716 cp_parser_template_declaration_after_export (parser, member_p);
7719 /* Parse a template-parameter-list.
7721 template-parameter-list:
7723 template-parameter-list , template-parameter
7725 Returns a TREE_LIST. Each node represents a template parameter.
7726 The nodes are connected via their TREE_CHAINs. */
7729 cp_parser_template_parameter_list (parser)
7732 tree parameter_list = NULL_TREE;
7739 /* Parse the template-parameter. */
7740 parameter = cp_parser_template_parameter (parser);
7741 /* Add it to the list. */
7742 parameter_list = process_template_parm (parameter_list,
7745 /* Peek at the next token. */
7746 token = cp_lexer_peek_token (parser->lexer);
7747 /* If it's not a `,', we're done. */
7748 if (token->type != CPP_COMMA)
7750 /* Otherwise, consume the `,' token. */
7751 cp_lexer_consume_token (parser->lexer);
7754 return parameter_list;
7757 /* Parse a template-parameter.
7761 parameter-declaration
7763 Returns a TREE_LIST. The TREE_VALUE represents the parameter. The
7764 TREE_PURPOSE is the default value, if any. */
7767 cp_parser_template_parameter (parser)
7772 /* Peek at the next token. */
7773 token = cp_lexer_peek_token (parser->lexer);
7774 /* If it is `class' or `template', we have a type-parameter. */
7775 if (token->keyword == RID_TEMPLATE)
7776 return cp_parser_type_parameter (parser);
7777 /* If it is `class' or `typename' we do not know yet whether it is a
7778 type parameter or a non-type parameter. Consider:
7780 template <typename T, typename T::X X> ...
7784 template <class C, class D*> ...
7786 Here, the first parameter is a type parameter, and the second is
7787 a non-type parameter. We can tell by looking at the token after
7788 the identifier -- if it is a `,', `=', or `>' then we have a type
7790 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
7792 /* Peek at the token after `class' or `typename'. */
7793 token = cp_lexer_peek_nth_token (parser->lexer, 2);
7794 /* If it's an identifier, skip it. */
7795 if (token->type == CPP_NAME)
7796 token = cp_lexer_peek_nth_token (parser->lexer, 3);
7797 /* Now, see if the token looks like the end of a template
7799 if (token->type == CPP_COMMA
7800 || token->type == CPP_EQ
7801 || token->type == CPP_GREATER)
7802 return cp_parser_type_parameter (parser);
7805 /* Otherwise, it is a non-type parameter.
7809 When parsing a default template-argument for a non-type
7810 template-parameter, the first non-nested `>' is taken as the end
7811 of the template parameter-list rather than a greater-than
7814 cp_parser_parameter_declaration (parser,
7815 /*greater_than_is_operator_p=*/false);
7818 /* Parse a type-parameter.
7821 class identifier [opt]
7822 class identifier [opt] = type-id
7823 typename identifier [opt]
7824 typename identifier [opt] = type-id
7825 template < template-parameter-list > class identifier [opt]
7826 template < template-parameter-list > class identifier [opt]
7829 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
7830 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
7831 the declaration of the parameter. */
7834 cp_parser_type_parameter (parser)
7840 /* Look for a keyword to tell us what kind of parameter this is. */
7841 token = cp_parser_require (parser, CPP_KEYWORD,
7842 "expected `class', `typename', or `template'");
7844 return error_mark_node;
7846 switch (token->keyword)
7852 tree default_argument;
7854 /* If the next token is an identifier, then it names the
7856 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
7857 identifier = cp_parser_identifier (parser);
7859 identifier = NULL_TREE;
7861 /* Create the parameter. */
7862 parameter = finish_template_type_parm (class_type_node, identifier);
7864 /* If the next token is an `=', we have a default argument. */
7865 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7867 /* Consume the `=' token. */
7868 cp_lexer_consume_token (parser->lexer);
7869 /* Parse the default-argumen. */
7870 default_argument = cp_parser_type_id (parser);
7873 default_argument = NULL_TREE;
7875 /* Create the combined representation of the parameter and the
7876 default argument. */
7877 parameter = build_tree_list (default_argument,
7884 tree parameter_list;
7886 tree default_argument;
7888 /* Look for the `<'. */
7889 cp_parser_require (parser, CPP_LESS, "`<'");
7890 /* Parse the template-parameter-list. */
7891 begin_template_parm_list ();
7893 = cp_parser_template_parameter_list (parser);
7894 parameter_list = end_template_parm_list (parameter_list);
7895 /* Look for the `>'. */
7896 cp_parser_require (parser, CPP_GREATER, "`>'");
7897 /* Look for the `class' keyword. */
7898 cp_parser_require_keyword (parser, RID_CLASS, "`class'");
7899 /* If the next token is an `=', then there is a
7900 default-argument. If the next token is a `>', we are at
7901 the end of the parameter-list. If the next token is a `,',
7902 then we are at the end of this parameter. */
7903 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
7904 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
7905 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7906 identifier = cp_parser_identifier (parser);
7908 identifier = NULL_TREE;
7909 /* Create the template parameter. */
7910 parameter = finish_template_template_parm (class_type_node,
7913 /* If the next token is an `=', then there is a
7914 default-argument. */
7915 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7917 /* Consume the `='. */
7918 cp_lexer_consume_token (parser->lexer);
7919 /* Parse the id-expression. */
7921 = cp_parser_id_expression (parser,
7922 /*template_keyword_p=*/false,
7923 /*check_dependency_p=*/true,
7924 /*template_p=*/NULL);
7925 /* Look up the name. */
7927 = cp_parser_lookup_name_simple (parser, default_argument);
7928 /* See if the default argument is valid. */
7930 = check_template_template_default_arg (default_argument);
7933 default_argument = NULL_TREE;
7935 /* Create the combined representation of the parameter and the
7936 default argument. */
7937 parameter = build_tree_list (default_argument,
7943 /* Anything else is an error. */
7944 cp_parser_error (parser,
7945 "expected `class', `typename', or `template'");
7946 parameter = error_mark_node;
7952 /* Parse a template-id.
7955 template-name < template-argument-list [opt] >
7957 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
7958 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
7959 returned. Otherwise, if the template-name names a function, or set
7960 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
7961 names a class, returns a TYPE_DECL for the specialization.
7963 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
7964 uninstantiated templates. */
7967 cp_parser_template_id (cp_parser *parser,
7968 bool template_keyword_p,
7969 bool check_dependency_p)
7974 tree saved_qualifying_scope;
7975 tree saved_object_scope;
7977 bool saved_greater_than_is_operator_p;
7978 ptrdiff_t start_of_id;
7979 tree access_check = NULL_TREE;
7981 /* If the next token corresponds to a template-id, there is no need
7983 if (cp_lexer_next_token_is (parser->lexer, CPP_TEMPLATE_ID))
7988 /* Get the stored value. */
7989 value = cp_lexer_consume_token (parser->lexer)->value;
7990 /* Perform any access checks that were deferred. */
7991 for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
7992 cp_parser_defer_access_check (parser,
7993 TREE_PURPOSE (check),
7994 TREE_VALUE (check));
7995 /* Return the stored value. */
7996 return TREE_VALUE (value);
7999 /* Remember where the template-id starts. */
8000 if (cp_parser_parsing_tentatively (parser)
8001 && !cp_parser_committed_to_tentative_parse (parser))
8003 cp_token *next_token = cp_lexer_peek_token (parser->lexer);
8004 start_of_id = cp_lexer_token_difference (parser->lexer,
8005 parser->lexer->first_token,
8007 access_check = parser->context->deferred_access_checks;
8012 /* Parse the template-name. */
8013 template = cp_parser_template_name (parser, template_keyword_p,
8014 check_dependency_p);
8015 if (template == error_mark_node)
8016 return error_mark_node;
8018 /* Look for the `<' that starts the template-argument-list. */
8019 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
8020 return error_mark_node;
8024 When parsing a template-id, the first non-nested `>' is taken as
8025 the end of the template-argument-list rather than a greater-than
8027 saved_greater_than_is_operator_p
8028 = parser->greater_than_is_operator_p;
8029 parser->greater_than_is_operator_p = false;
8030 /* Parsing the argument list may modify SCOPE, so we save it
8032 saved_scope = parser->scope;
8033 saved_qualifying_scope = parser->qualifying_scope;
8034 saved_object_scope = parser->object_scope;
8035 /* Parse the template-argument-list itself. */
8036 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
8037 arguments = NULL_TREE;
8039 arguments = cp_parser_template_argument_list (parser);
8040 /* Look for the `>' that ends the template-argument-list. */
8041 cp_parser_require (parser, CPP_GREATER, "`>'");
8042 /* The `>' token might be a greater-than operator again now. */
8043 parser->greater_than_is_operator_p
8044 = saved_greater_than_is_operator_p;
8045 /* Restore the SAVED_SCOPE. */
8046 parser->scope = saved_scope;
8047 parser->qualifying_scope = saved_qualifying_scope;
8048 parser->object_scope = saved_object_scope;
8050 /* Build a representation of the specialization. */
8051 if (TREE_CODE (template) == IDENTIFIER_NODE)
8052 template_id = build_min_nt (TEMPLATE_ID_EXPR, template, arguments);
8053 else if (DECL_CLASS_TEMPLATE_P (template)
8054 || DECL_TEMPLATE_TEMPLATE_PARM_P (template))
8056 = finish_template_type (template, arguments,
8057 cp_lexer_next_token_is (parser->lexer,
8061 /* If it's not a class-template or a template-template, it should be
8062 a function-template. */
8063 my_friendly_assert ((DECL_FUNCTION_TEMPLATE_P (template)
8064 || TREE_CODE (template) == OVERLOAD
8065 || BASELINK_P (template)),
8068 template_id = lookup_template_function (template, arguments);
8071 /* If parsing tentatively, replace the sequence of tokens that makes
8072 up the template-id with a CPP_TEMPLATE_ID token. That way,
8073 should we re-parse the token stream, we will not have to repeat
8074 the effort required to do the parse, nor will we issue duplicate
8075 error messages about problems during instantiation of the
8077 if (start_of_id >= 0)
8082 /* Find the token that corresponds to the start of the
8084 token = cp_lexer_advance_token (parser->lexer,
8085 parser->lexer->first_token,
8088 /* Remember the access checks associated with this
8089 nested-name-specifier. */
8090 c = parser->context->deferred_access_checks;
8091 if (c == access_check)
8092 access_check = NULL_TREE;
8095 while (TREE_CHAIN (c) != access_check)
8097 access_check = parser->context->deferred_access_checks;
8098 parser->context->deferred_access_checks = TREE_CHAIN (c);
8099 TREE_CHAIN (c) = NULL_TREE;
8102 /* Reset the contents of the START_OF_ID token. */
8103 token->type = CPP_TEMPLATE_ID;
8104 token->value = build_tree_list (access_check, template_id);
8105 token->keyword = RID_MAX;
8106 /* Purge all subsequent tokens. */
8107 cp_lexer_purge_tokens_after (parser->lexer, token);
8113 /* Parse a template-name.
8118 The standard should actually say:
8122 operator-function-id
8123 conversion-function-id
8125 A defect report has been filed about this issue.
8127 If TEMPLATE_KEYWORD_P is true, then we have just seen the
8128 `template' keyword, in a construction like:
8132 In that case `f' is taken to be a template-name, even though there
8133 is no way of knowing for sure.
8135 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
8136 name refers to a set of overloaded functions, at least one of which
8137 is a template, or an IDENTIFIER_NODE with the name of the template,
8138 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
8139 names are looked up inside uninstantiated templates. */
8142 cp_parser_template_name (parser, template_keyword_p, check_dependency_p)
8144 bool template_keyword_p;
8145 bool check_dependency_p;
8151 /* If the next token is `operator', then we have either an
8152 operator-function-id or a conversion-function-id. */
8153 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
8155 /* We don't know whether we're looking at an
8156 operator-function-id or a conversion-function-id. */
8157 cp_parser_parse_tentatively (parser);
8158 /* Try an operator-function-id. */
8159 identifier = cp_parser_operator_function_id (parser);
8160 /* If that didn't work, try a conversion-function-id. */
8161 if (!cp_parser_parse_definitely (parser))
8162 identifier = cp_parser_conversion_function_id (parser);
8164 /* Look for the identifier. */
8166 identifier = cp_parser_identifier (parser);
8168 /* If we didn't find an identifier, we don't have a template-id. */
8169 if (identifier == error_mark_node)
8170 return error_mark_node;
8172 /* If the name immediately followed the `template' keyword, then it
8173 is a template-name. However, if the next token is not `<', then
8174 we do not treat it as a template-name, since it is not being used
8175 as part of a template-id. This enables us to handle constructs
8178 template <typename T> struct S { S(); };
8179 template <typename T> S<T>::S();
8181 correctly. We would treat `S' as a template -- if it were `S<T>'
8182 -- but we do not if there is no `<'. */
8183 if (template_keyword_p && processing_template_decl
8184 && cp_lexer_next_token_is (parser->lexer, CPP_LESS))
8187 /* Look up the name. */
8188 decl = cp_parser_lookup_name (parser, identifier,
8189 /*check_access=*/true,
8191 check_dependency_p);
8192 decl = maybe_get_template_decl_from_type_decl (decl);
8194 /* If DECL is a template, then the name was a template-name. */
8195 if (TREE_CODE (decl) == TEMPLATE_DECL)
8199 /* The standard does not explicitly indicate whether a name that
8200 names a set of overloaded declarations, some of which are
8201 templates, is a template-name. However, such a name should
8202 be a template-name; otherwise, there is no way to form a
8203 template-id for the overloaded templates. */
8204 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
8205 if (TREE_CODE (fns) == OVERLOAD)
8209 for (fn = fns; fn; fn = OVL_NEXT (fn))
8210 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
8215 /* Otherwise, the name does not name a template. */
8216 cp_parser_error (parser, "expected template-name");
8217 return error_mark_node;
8221 /* If DECL is dependent, and refers to a function, then just return
8222 its name; we will look it up again during template instantiation. */
8223 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
8225 tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
8226 if (TYPE_P (scope) && cp_parser_dependent_type_p (scope))
8233 /* Parse a template-argument-list.
8235 template-argument-list:
8237 template-argument-list , template-argument
8239 Returns a TREE_LIST representing the arguments, in the order they
8240 appeared. The TREE_VALUE of each node is a representation of the
8244 cp_parser_template_argument_list (parser)
8247 tree arguments = NULL_TREE;
8253 /* Parse the template-argument. */
8254 argument = cp_parser_template_argument (parser);
8255 /* Add it to the list. */
8256 arguments = tree_cons (NULL_TREE, argument, arguments);
8257 /* If it is not a `,', then there are no more arguments. */
8258 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8260 /* Otherwise, consume the ','. */
8261 cp_lexer_consume_token (parser->lexer);
8264 /* We built up the arguments in reverse order. */
8265 return nreverse (arguments);
8268 /* Parse a template-argument.
8271 assignment-expression
8275 The representation is that of an assignment-expression, type-id, or
8276 id-expression -- except that the qualified id-expression is
8277 evaluated, so that the value returned is either a DECL or an
8281 cp_parser_template_argument (parser)
8287 /* There's really no way to know what we're looking at, so we just
8288 try each alternative in order.
8292 In a template-argument, an ambiguity between a type-id and an
8293 expression is resolved to a type-id, regardless of the form of
8294 the corresponding template-parameter.
8296 Therefore, we try a type-id first. */
8297 cp_parser_parse_tentatively (parser);
8298 /* Otherwise, try a type-id. */
8299 argument = cp_parser_type_id (parser);
8300 /* If the next token isn't a `,' or a `>', then this argument wasn't
8302 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
8303 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER))
8304 cp_parser_error (parser, "expected template-argument");
8305 /* If that worked, we're done. */
8306 if (cp_parser_parse_definitely (parser))
8308 /* We're still not sure what the argument will be. */
8309 cp_parser_parse_tentatively (parser);
8310 /* Try a template. */
8311 argument = cp_parser_id_expression (parser,
8312 /*template_keyword_p=*/false,
8313 /*check_dependency_p=*/true,
8315 /* If the next token isn't a `,' or a `>', then this argument wasn't
8317 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
8318 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER))
8319 cp_parser_error (parser, "expected template-argument");
8320 if (!cp_parser_error_occurred (parser))
8322 /* Figure out what is being referred to. */
8323 argument = cp_parser_lookup_name_simple (parser, argument);
8325 argument = make_unbound_class_template (TREE_OPERAND (argument, 0),
8326 TREE_OPERAND (argument, 1),
8327 tf_error | tf_parsing);
8328 else if (TREE_CODE (argument) != TEMPLATE_DECL)
8329 cp_parser_error (parser, "expected template-name");
8331 if (cp_parser_parse_definitely (parser))
8333 /* It must be an assignment-expression. */
8334 return cp_parser_assignment_expression (parser);
8337 /* Parse an explicit-instantiation.
8339 explicit-instantiation:
8340 template declaration
8342 Although the standard says `declaration', what it really means is:
8344 explicit-instantiation:
8345 template decl-specifier-seq [opt] declarator [opt] ;
8347 Things like `template int S<int>::i = 5, int S<double>::j;' are not
8348 supposed to be allowed. A defect report has been filed about this
8353 explicit-instantiation:
8354 storage-class-specifier template
8355 decl-specifier-seq [opt] declarator [opt] ;
8356 function-specifier template
8357 decl-specifier-seq [opt] declarator [opt] ; */
8360 cp_parser_explicit_instantiation (parser)
8363 bool declares_class_or_enum;
8364 tree decl_specifiers;
8366 tree extension_specifier = NULL_TREE;
8368 /* Look for an (optional) storage-class-specifier or
8369 function-specifier. */
8370 if (cp_parser_allow_gnu_extensions_p (parser))
8373 = cp_parser_storage_class_specifier_opt (parser);
8374 if (!extension_specifier)
8375 extension_specifier = cp_parser_function_specifier_opt (parser);
8378 /* Look for the `template' keyword. */
8379 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
8380 /* Let the front end know that we are processing an explicit
8382 begin_explicit_instantiation ();
8383 /* [temp.explicit] says that we are supposed to ignore access
8384 control while processing explicit instantiation directives. */
8385 scope_chain->check_access = 0;
8386 /* Parse a decl-specifier-seq. */
8388 = cp_parser_decl_specifier_seq (parser,
8389 CP_PARSER_FLAGS_OPTIONAL,
8391 &declares_class_or_enum);
8392 /* If there was exactly one decl-specifier, and it declared a class,
8393 and there's no declarator, then we have an explicit type
8395 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
8399 type = check_tag_decl (decl_specifiers);
8401 do_type_instantiation (type, extension_specifier, /*complain=*/1);
8408 /* Parse the declarator. */
8410 = cp_parser_declarator (parser,
8411 /*abstract_p=*/false,
8412 /*ctor_dtor_or_conv_p=*/NULL);
8413 decl = grokdeclarator (declarator, decl_specifiers,
8415 /* Do the explicit instantiation. */
8416 do_decl_instantiation (decl, extension_specifier);
8418 /* We're done with the instantiation. */
8419 end_explicit_instantiation ();
8420 /* Trun access control back on. */
8421 scope_chain->check_access = flag_access_control;
8423 /* Look for the trailing `;'. */
8424 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
8427 /* Parse an explicit-specialization.
8429 explicit-specialization:
8430 template < > declaration
8432 Although the standard says `declaration', what it really means is:
8434 explicit-specialization:
8435 template <> decl-specifier [opt] init-declarator [opt] ;
8436 template <> function-definition
8437 template <> explicit-specialization
8438 template <> template-declaration */
8441 cp_parser_explicit_specialization (parser)
8444 /* Look for the `template' keyword. */
8445 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
8446 /* Look for the `<'. */
8447 cp_parser_require (parser, CPP_LESS, "`<'");
8448 /* Look for the `>'. */
8449 cp_parser_require (parser, CPP_GREATER, "`>'");
8450 /* We have processed another parameter list. */
8451 ++parser->num_template_parameter_lists;
8452 /* Let the front end know that we are beginning a specialization. */
8453 begin_specialization ();
8455 /* If the next keyword is `template', we need to figure out whether
8456 or not we're looking a template-declaration. */
8457 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
8459 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
8460 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
8461 cp_parser_template_declaration_after_export (parser,
8462 /*member_p=*/false);
8464 cp_parser_explicit_specialization (parser);
8467 /* Parse the dependent declaration. */
8468 cp_parser_single_declaration (parser,
8472 /* We're done with the specialization. */
8473 end_specialization ();
8474 /* We're done with this parameter list. */
8475 --parser->num_template_parameter_lists;
8478 /* Parse a type-specifier.
8481 simple-type-specifier
8484 elaborated-type-specifier
8492 Returns a representation of the type-specifier. If the
8493 type-specifier is a keyword (like `int' or `const', or
8494 `__complex__') then the correspoding IDENTIFIER_NODE is returned.
8495 For a class-specifier, enum-specifier, or elaborated-type-specifier
8496 a TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
8498 If IS_FRIEND is TRUE then this type-specifier is being declared a
8499 `friend'. If IS_DECLARATION is TRUE, then this type-specifier is
8500 appearing in a decl-specifier-seq.
8502 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
8503 class-specifier, enum-specifier, or elaborated-type-specifier, then
8504 *DECLARES_CLASS_OR_ENUM is set to TRUE. Otherwise, it is set to
8507 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
8508 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
8512 cp_parser_type_specifier (parser,
8516 declares_class_or_enum,
8519 cp_parser_flags flags;
8521 bool is_declaration;
8522 bool *declares_class_or_enum;
8523 bool *is_cv_qualifier;
8525 tree type_spec = NULL_TREE;
8529 /* Assume this type-specifier does not declare a new type. */
8530 if (declares_class_or_enum)
8531 *declares_class_or_enum = false;
8532 /* And that it does not specify a cv-qualifier. */
8533 if (is_cv_qualifier)
8534 *is_cv_qualifier = false;
8535 /* Peek at the next token. */
8536 token = cp_lexer_peek_token (parser->lexer);
8538 /* If we're looking at a keyword, we can use that to guide the
8539 production we choose. */
8540 keyword = token->keyword;
8543 /* Any of these indicate either a class-specifier, or an
8544 elaborated-type-specifier. */
8549 /* Parse tentatively so that we can back up if we don't find a
8550 class-specifier or enum-specifier. */
8551 cp_parser_parse_tentatively (parser);
8552 /* Look for the class-specifier or enum-specifier. */
8553 if (keyword == RID_ENUM)
8554 type_spec = cp_parser_enum_specifier (parser);
8556 type_spec = cp_parser_class_specifier (parser);
8558 /* If that worked, we're done. */
8559 if (cp_parser_parse_definitely (parser))
8561 if (declares_class_or_enum)
8562 *declares_class_or_enum = true;
8569 /* Look for an elaborated-type-specifier. */
8570 type_spec = cp_parser_elaborated_type_specifier (parser,
8573 /* We're declaring a class or enum -- unless we're using
8575 if (declares_class_or_enum && keyword != RID_TYPENAME)
8576 *declares_class_or_enum = true;
8582 type_spec = cp_parser_cv_qualifier_opt (parser);
8583 /* Even though we call a routine that looks for an optional
8584 qualifier, we know that there should be one. */
8585 my_friendly_assert (type_spec != NULL, 20000328);
8586 /* This type-specifier was a cv-qualified. */
8587 if (is_cv_qualifier)
8588 *is_cv_qualifier = true;
8593 /* The `__complex__' keyword is a GNU extension. */
8594 return cp_lexer_consume_token (parser->lexer)->value;
8600 /* If we do not already have a type-specifier, assume we are looking
8601 at a simple-type-specifier. */
8602 type_spec = cp_parser_simple_type_specifier (parser, flags);
8604 /* If we didn't find a type-specifier, and a type-specifier was not
8605 optional in this context, issue an error message. */
8606 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
8608 cp_parser_error (parser, "expected type specifier");
8609 return error_mark_node;
8615 /* Parse a simple-type-specifier.
8617 simple-type-specifier:
8618 :: [opt] nested-name-specifier [opt] type-name
8619 :: [opt] nested-name-specifier template template-id
8634 simple-type-specifier:
8635 __typeof__ unary-expression
8636 __typeof__ ( type-id )
8638 For the various keywords, the value returned is simply the
8639 TREE_IDENTIFIER representing the keyword. For the first two
8640 productions, the value returned is the indicated TYPE_DECL. */
8643 cp_parser_simple_type_specifier (parser, flags)
8645 cp_parser_flags flags;
8647 tree type = NULL_TREE;
8650 /* Peek at the next token. */
8651 token = cp_lexer_peek_token (parser->lexer);
8653 /* If we're looking at a keyword, things are easy. */
8654 switch (token->keyword)
8667 /* Consume the token. */
8668 return cp_lexer_consume_token (parser->lexer)->value;
8674 /* Consume the `typeof' token. */
8675 cp_lexer_consume_token (parser->lexer);
8676 /* Parse the operand to `typeof' */
8677 operand = cp_parser_sizeof_operand (parser, RID_TYPEOF);
8678 /* If it is not already a TYPE, take its type. */
8679 if (!TYPE_P (operand))
8680 operand = finish_typeof (operand);
8689 /* The type-specifier must be a user-defined type. */
8690 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
8692 /* Don't gobble tokens or issue error messages if this is an
8693 optional type-specifier. */
8694 if (flags & CP_PARSER_FLAGS_OPTIONAL)
8695 cp_parser_parse_tentatively (parser);
8697 /* Look for the optional `::' operator. */
8698 cp_parser_global_scope_opt (parser,
8699 /*current_scope_valid_p=*/false);
8700 /* Look for the nested-name specifier. */
8701 cp_parser_nested_name_specifier_opt (parser,
8702 /*typename_keyword_p=*/false,
8703 /*check_dependency_p=*/true,
8705 /* If we have seen a nested-name-specifier, and the next token
8706 is `template', then we are using the template-id production. */
8708 && cp_parser_optional_template_keyword (parser))
8710 /* Look for the template-id. */
8711 type = cp_parser_template_id (parser,
8712 /*template_keyword_p=*/true,
8713 /*check_dependency_p=*/true);
8714 /* If the template-id did not name a type, we are out of
8716 if (TREE_CODE (type) != TYPE_DECL)
8718 cp_parser_error (parser, "expected template-id for type");
8722 /* Otherwise, look for a type-name. */
8725 type = cp_parser_type_name (parser);
8726 if (type == error_mark_node)
8730 /* If it didn't work out, we don't have a TYPE. */
8731 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
8732 && !cp_parser_parse_definitely (parser))
8736 /* If we didn't get a type-name, issue an error message. */
8737 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
8739 cp_parser_error (parser, "expected type-name");
8740 return error_mark_node;
8746 /* Parse a type-name.
8759 Returns a TYPE_DECL for the the type. */
8762 cp_parser_type_name (parser)
8768 /* We can't know yet whether it is a class-name or not. */
8769 cp_parser_parse_tentatively (parser);
8770 /* Try a class-name. */
8771 type_decl = cp_parser_class_name (parser,
8772 /*typename_keyword_p=*/false,
8773 /*template_keyword_p=*/false,
8775 /*check_access_p=*/true,
8776 /*check_dependency_p=*/true,
8777 /*class_head_p=*/false);
8778 /* If it's not a class-name, keep looking. */
8779 if (!cp_parser_parse_definitely (parser))
8781 /* It must be a typedef-name or an enum-name. */
8782 identifier = cp_parser_identifier (parser);
8783 if (identifier == error_mark_node)
8784 return error_mark_node;
8786 /* Look up the type-name. */
8787 type_decl = cp_parser_lookup_name_simple (parser, identifier);
8788 /* Issue an error if we did not find a type-name. */
8789 if (TREE_CODE (type_decl) != TYPE_DECL)
8791 cp_parser_error (parser, "expected type-name");
8792 type_decl = error_mark_node;
8794 /* Remember that the name was used in the definition of the
8795 current class so that we can check later to see if the
8796 meaning would have been different after the class was
8797 entirely defined. */
8798 else if (type_decl != error_mark_node
8800 maybe_note_name_used_in_class (identifier, type_decl);
8807 /* Parse an elaborated-type-specifier. Note that the grammar given
8808 here incorporates the resolution to DR68.
8810 elaborated-type-specifier:
8811 class-key :: [opt] nested-name-specifier [opt] identifier
8812 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
8813 enum :: [opt] nested-name-specifier [opt] identifier
8814 typename :: [opt] nested-name-specifier identifier
8815 typename :: [opt] nested-name-specifier template [opt]
8818 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
8819 declared `friend'. If IS_DECLARATION is TRUE, then this
8820 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
8821 something is being declared.
8823 Returns the TYPE specified. */
8826 cp_parser_elaborated_type_specifier (parser, is_friend, is_declaration)
8829 bool is_declaration;
8831 enum tag_types tag_type;
8833 tree type = NULL_TREE;
8835 /* See if we're looking at the `enum' keyword. */
8836 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
8838 /* Consume the `enum' token. */
8839 cp_lexer_consume_token (parser->lexer);
8840 /* Remember that it's an enumeration type. */
8841 tag_type = enum_type;
8843 /* Or, it might be `typename'. */
8844 else if (cp_lexer_next_token_is_keyword (parser->lexer,
8847 /* Consume the `typename' token. */
8848 cp_lexer_consume_token (parser->lexer);
8849 /* Remember that it's a `typename' type. */
8850 tag_type = typename_type;
8851 /* The `typename' keyword is only allowed in templates. */
8852 if (!processing_template_decl)
8853 pedwarn ("using `typename' outside of template");
8855 /* Otherwise it must be a class-key. */
8858 tag_type = cp_parser_class_key (parser);
8859 if (tag_type == none_type)
8860 return error_mark_node;
8863 /* Look for the `::' operator. */
8864 cp_parser_global_scope_opt (parser,
8865 /*current_scope_valid_p=*/false);
8866 /* Look for the nested-name-specifier. */
8867 if (tag_type == typename_type)
8868 cp_parser_nested_name_specifier (parser,
8869 /*typename_keyword_p=*/true,
8870 /*check_dependency_p=*/true,
8873 /* Even though `typename' is not present, the proposed resolution
8874 to Core Issue 180 says that in `class A<T>::B', `B' should be
8875 considered a type-name, even if `A<T>' is dependent. */
8876 cp_parser_nested_name_specifier_opt (parser,
8877 /*typename_keyword_p=*/true,
8878 /*check_dependency_p=*/true,
8880 /* For everything but enumeration types, consider a template-id. */
8881 if (tag_type != enum_type)
8883 bool template_p = false;
8886 /* Allow the `template' keyword. */
8887 template_p = cp_parser_optional_template_keyword (parser);
8888 /* If we didn't see `template', we don't know if there's a
8889 template-id or not. */
8891 cp_parser_parse_tentatively (parser);
8892 /* Parse the template-id. */
8893 decl = cp_parser_template_id (parser, template_p,
8894 /*check_dependency_p=*/true);
8895 /* If we didn't find a template-id, look for an ordinary
8897 if (!template_p && !cp_parser_parse_definitely (parser))
8899 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
8900 in effect, then we must assume that, upon instantiation, the
8901 template will correspond to a class. */
8902 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
8903 && tag_type == typename_type)
8904 type = make_typename_type (parser->scope, decl,
8907 type = TREE_TYPE (decl);
8910 /* For an enumeration type, consider only a plain identifier. */
8913 identifier = cp_parser_identifier (parser);
8915 if (identifier == error_mark_node)
8916 return error_mark_node;
8918 /* For a `typename', we needn't call xref_tag. */
8919 if (tag_type == typename_type)
8920 return make_typename_type (parser->scope, identifier,
8922 /* Look up a qualified name in the usual way. */
8927 /* In an elaborated-type-specifier, names are assumed to name
8928 types, so we set IS_TYPE to TRUE when calling
8929 cp_parser_lookup_name. */
8930 decl = cp_parser_lookup_name (parser, identifier,
8931 /*check_access=*/true,
8933 /*check_dependency=*/true);
8934 decl = (cp_parser_maybe_treat_template_as_class
8935 (decl, /*tag_name_p=*/is_friend));
8937 if (TREE_CODE (decl) != TYPE_DECL)
8939 error ("expected type-name");
8940 return error_mark_node;
8942 else if (TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE
8943 && tag_type != enum_type)
8944 error ("`%T' referred to as `%s'", TREE_TYPE (decl),
8945 tag_type == record_type ? "struct" : "class");
8946 else if (TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE
8947 && tag_type == enum_type)
8948 error ("`%T' referred to as enum", TREE_TYPE (decl));
8950 type = TREE_TYPE (decl);
8954 /* An elaborated-type-specifier sometimes introduces a new type and
8955 sometimes names an existing type. Normally, the rule is that it
8956 introduces a new type only if there is not an existing type of
8957 the same name already in scope. For example, given:
8960 void f() { struct S s; }
8962 the `struct S' in the body of `f' is the same `struct S' as in
8963 the global scope; the existing definition is used. However, if
8964 there were no global declaration, this would introduce a new
8965 local class named `S'.
8967 An exception to this rule applies to the following code:
8969 namespace N { struct S; }
8971 Here, the elaborated-type-specifier names a new type
8972 unconditionally; even if there is already an `S' in the
8973 containing scope this declaration names a new type.
8974 This exception only applies if the elaborated-type-specifier
8975 forms the complete declaration:
8979 A declaration consisting solely of `class-key identifier ;' is
8980 either a redeclaration of the name in the current scope or a
8981 forward declaration of the identifier as a class name. It
8982 introduces the name into the current scope.
8984 We are in this situation precisely when the next token is a `;'.
8986 An exception to the exception is that a `friend' declaration does
8987 *not* name a new type; i.e., given:
8989 struct S { friend struct T; };
8991 `T' is not a new type in the scope of `S'.
8993 Also, `new struct S' or `sizeof (struct S)' never results in the
8994 definition of a new type; a new type can only be declared in a
8995 declaration context. */
8997 type = xref_tag (tag_type, identifier,
8998 /*attributes=*/NULL_TREE,
9001 || cp_lexer_next_token_is_not (parser->lexer,
9005 if (tag_type != enum_type)
9006 cp_parser_check_class_key (tag_type, type);
9010 /* Parse an enum-specifier.
9013 enum identifier [opt] { enumerator-list [opt] }
9015 Returns an ENUM_TYPE representing the enumeration. */
9018 cp_parser_enum_specifier (parser)
9022 tree identifier = NULL_TREE;
9025 /* Look for the `enum' keyword. */
9026 if (!cp_parser_require_keyword (parser, RID_ENUM, "`enum'"))
9027 return error_mark_node;
9028 /* Peek at the next token. */
9029 token = cp_lexer_peek_token (parser->lexer);
9031 /* See if it is an identifier. */
9032 if (token->type == CPP_NAME)
9033 identifier = cp_parser_identifier (parser);
9035 /* Look for the `{'. */
9036 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
9037 return error_mark_node;
9039 /* At this point, we're going ahead with the enum-specifier, even
9040 if some other problem occurs. */
9041 cp_parser_commit_to_tentative_parse (parser);
9043 /* Issue an error message if type-definitions are forbidden here. */
9044 cp_parser_check_type_definition (parser);
9046 /* Create the new type. */
9047 type = start_enum (identifier ? identifier : make_anon_name ());
9049 /* Peek at the next token. */
9050 token = cp_lexer_peek_token (parser->lexer);
9051 /* If it's not a `}', then there are some enumerators. */
9052 if (token->type != CPP_CLOSE_BRACE)
9053 cp_parser_enumerator_list (parser, type);
9054 /* Look for the `}'. */
9055 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
9057 /* Finish up the enumeration. */
9063 /* Parse an enumerator-list. The enumerators all have the indicated
9067 enumerator-definition
9068 enumerator-list , enumerator-definition */
9071 cp_parser_enumerator_list (parser, type)
9079 /* Parse an enumerator-definition. */
9080 cp_parser_enumerator_definition (parser, type);
9081 /* Peek at the next token. */
9082 token = cp_lexer_peek_token (parser->lexer);
9083 /* If it's not a `,', then we've reached the end of the
9085 if (token->type != CPP_COMMA)
9087 /* Otherwise, consume the `,' and keep going. */
9088 cp_lexer_consume_token (parser->lexer);
9089 /* If the next token is a `}', there is a trailing comma. */
9090 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
9092 if (pedantic && !in_system_header)
9093 pedwarn ("comma at end of enumerator list");
9099 /* Parse an enumerator-definition. The enumerator has the indicated
9102 enumerator-definition:
9104 enumerator = constant-expression
9110 cp_parser_enumerator_definition (parser, type)
9118 /* Look for the identifier. */
9119 identifier = cp_parser_identifier (parser);
9120 if (identifier == error_mark_node)
9123 /* Peek at the next token. */
9124 token = cp_lexer_peek_token (parser->lexer);
9125 /* If it's an `=', then there's an explicit value. */
9126 if (token->type == CPP_EQ)
9128 /* Consume the `=' token. */
9129 cp_lexer_consume_token (parser->lexer);
9130 /* Parse the value. */
9131 value = cp_parser_constant_expression (parser);
9136 /* Create the enumerator. */
9137 build_enumerator (identifier, value, type);
9140 /* Parse a namespace-name.
9143 original-namespace-name
9146 Returns the NAMESPACE_DECL for the namespace. */
9149 cp_parser_namespace_name (parser)
9153 tree namespace_decl;
9155 /* Get the name of the namespace. */
9156 identifier = cp_parser_identifier (parser);
9157 if (identifier == error_mark_node)
9158 return error_mark_node;
9160 /* Look up the identifier in the currently active scope. */
9161 namespace_decl = cp_parser_lookup_name_simple (parser, identifier);
9162 /* If it's not a namespace, issue an error. */
9163 if (namespace_decl == error_mark_node
9164 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
9166 cp_parser_error (parser, "expected namespace-name");
9167 namespace_decl = error_mark_node;
9170 return namespace_decl;
9173 /* Parse a namespace-definition.
9175 namespace-definition:
9176 named-namespace-definition
9177 unnamed-namespace-definition
9179 named-namespace-definition:
9180 original-namespace-definition
9181 extension-namespace-definition
9183 original-namespace-definition:
9184 namespace identifier { namespace-body }
9186 extension-namespace-definition:
9187 namespace original-namespace-name { namespace-body }
9189 unnamed-namespace-definition:
9190 namespace { namespace-body } */
9193 cp_parser_namespace_definition (parser)
9198 /* Look for the `namespace' keyword. */
9199 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
9201 /* Get the name of the namespace. We do not attempt to distinguish
9202 between an original-namespace-definition and an
9203 extension-namespace-definition at this point. The semantic
9204 analysis routines are responsible for that. */
9205 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
9206 identifier = cp_parser_identifier (parser);
9208 identifier = NULL_TREE;
9210 /* Look for the `{' to start the namespace. */
9211 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
9212 /* Start the namespace. */
9213 push_namespace (identifier);
9214 /* Parse the body of the namespace. */
9215 cp_parser_namespace_body (parser);
9216 /* Finish the namespace. */
9218 /* Look for the final `}'. */
9219 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
9222 /* Parse a namespace-body.
9225 declaration-seq [opt] */
9228 cp_parser_namespace_body (parser)
9231 cp_parser_declaration_seq_opt (parser);
9234 /* Parse a namespace-alias-definition.
9236 namespace-alias-definition:
9237 namespace identifier = qualified-namespace-specifier ; */
9240 cp_parser_namespace_alias_definition (parser)
9244 tree namespace_specifier;
9246 /* Look for the `namespace' keyword. */
9247 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
9248 /* Look for the identifier. */
9249 identifier = cp_parser_identifier (parser);
9250 if (identifier == error_mark_node)
9252 /* Look for the `=' token. */
9253 cp_parser_require (parser, CPP_EQ, "`='");
9254 /* Look for the qualified-namespace-specifier. */
9256 = cp_parser_qualified_namespace_specifier (parser);
9257 /* Look for the `;' token. */
9258 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
9260 /* Register the alias in the symbol table. */
9261 do_namespace_alias (identifier, namespace_specifier);
9264 /* Parse a qualified-namespace-specifier.
9266 qualified-namespace-specifier:
9267 :: [opt] nested-name-specifier [opt] namespace-name
9269 Returns a NAMESPACE_DECL corresponding to the specified
9273 cp_parser_qualified_namespace_specifier (parser)
9276 /* Look for the optional `::'. */
9277 cp_parser_global_scope_opt (parser,
9278 /*current_scope_valid_p=*/false);
9280 /* Look for the optional nested-name-specifier. */
9281 cp_parser_nested_name_specifier_opt (parser,
9282 /*typename_keyword_p=*/false,
9283 /*check_dependency_p=*/true,
9286 return cp_parser_namespace_name (parser);
9289 /* Parse a using-declaration.
9292 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
9293 using :: unqualified-id ; */
9296 cp_parser_using_declaration (parser)
9300 bool typename_p = false;
9301 bool global_scope_p;
9306 /* Look for the `using' keyword. */
9307 cp_parser_require_keyword (parser, RID_USING, "`using'");
9309 /* Peek at the next token. */
9310 token = cp_lexer_peek_token (parser->lexer);
9311 /* See if it's `typename'. */
9312 if (token->keyword == RID_TYPENAME)
9314 /* Remember that we've seen it. */
9316 /* Consume the `typename' token. */
9317 cp_lexer_consume_token (parser->lexer);
9320 /* Look for the optional global scope qualification. */
9322 = (cp_parser_global_scope_opt (parser,
9323 /*current_scope_valid_p=*/false)
9326 /* If we saw `typename', or didn't see `::', then there must be a
9327 nested-name-specifier present. */
9328 if (typename_p || !global_scope_p)
9329 cp_parser_nested_name_specifier (parser, typename_p,
9330 /*check_dependency_p=*/true,
9332 /* Otherwise, we could be in either of the two productions. In that
9333 case, treat the nested-name-specifier as optional. */
9335 cp_parser_nested_name_specifier_opt (parser,
9336 /*typename_keyword_p=*/false,
9337 /*check_dependency_p=*/true,
9340 /* Parse the unqualified-id. */
9341 identifier = cp_parser_unqualified_id (parser,
9342 /*template_keyword_p=*/false,
9343 /*check_dependency_p=*/true);
9345 /* The function we call to handle a using-declaration is different
9346 depending on what scope we are in. */
9347 scope = current_scope ();
9348 if (scope && TYPE_P (scope))
9350 /* Create the USING_DECL. */
9351 decl = do_class_using_decl (build_nt (SCOPE_REF,
9354 /* Add it to the list of members in this class. */
9355 finish_member_declaration (decl);
9359 decl = cp_parser_lookup_name_simple (parser, identifier);
9361 do_local_using_decl (decl);
9363 do_toplevel_using_decl (decl);
9366 /* Look for the final `;'. */
9367 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
9370 /* Parse a using-directive.
9373 using namespace :: [opt] nested-name-specifier [opt]
9377 cp_parser_using_directive (parser)
9380 tree namespace_decl;
9382 /* Look for the `using' keyword. */
9383 cp_parser_require_keyword (parser, RID_USING, "`using'");
9384 /* And the `namespace' keyword. */
9385 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
9386 /* Look for the optional `::' operator. */
9387 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
9388 /* And the optional nested-name-sepcifier. */
9389 cp_parser_nested_name_specifier_opt (parser,
9390 /*typename_keyword_p=*/false,
9391 /*check_dependency_p=*/true,
9393 /* Get the namespace being used. */
9394 namespace_decl = cp_parser_namespace_name (parser);
9395 /* Update the symbol table. */
9396 do_using_directive (namespace_decl);
9397 /* Look for the final `;'. */
9398 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
9401 /* Parse an asm-definition.
9404 asm ( string-literal ) ;
9409 asm volatile [opt] ( string-literal ) ;
9410 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
9411 asm volatile [opt] ( string-literal : asm-operand-list [opt]
9412 : asm-operand-list [opt] ) ;
9413 asm volatile [opt] ( string-literal : asm-operand-list [opt]
9414 : asm-operand-list [opt]
9415 : asm-operand-list [opt] ) ; */
9418 cp_parser_asm_definition (parser)
9423 tree outputs = NULL_TREE;
9424 tree inputs = NULL_TREE;
9425 tree clobbers = NULL_TREE;
9427 bool volatile_p = false;
9428 bool extended_p = false;
9430 /* Look for the `asm' keyword. */
9431 cp_parser_require_keyword (parser, RID_ASM, "`asm'");
9432 /* See if the next token is `volatile'. */
9433 if (cp_parser_allow_gnu_extensions_p (parser)
9434 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
9436 /* Remember that we saw the `volatile' keyword. */
9438 /* Consume the token. */
9439 cp_lexer_consume_token (parser->lexer);
9441 /* Look for the opening `('. */
9442 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
9443 /* Look for the string. */
9444 token = cp_parser_require (parser, CPP_STRING, "asm body");
9447 string = token->value;
9448 /* If we're allowing GNU extensions, check for the extended assembly
9449 syntax. Unfortunately, the `:' tokens need not be separated by
9450 a space in C, and so, for compatibility, we tolerate that here
9451 too. Doing that means that we have to treat the `::' operator as
9453 if (cp_parser_allow_gnu_extensions_p (parser)
9454 && at_function_scope_p ()
9455 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
9456 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
9458 bool inputs_p = false;
9459 bool clobbers_p = false;
9461 /* The extended syntax was used. */
9464 /* Look for outputs. */
9465 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
9467 /* Consume the `:'. */
9468 cp_lexer_consume_token (parser->lexer);
9469 /* Parse the output-operands. */
9470 if (cp_lexer_next_token_is_not (parser->lexer,
9472 && cp_lexer_next_token_is_not (parser->lexer,
9474 outputs = cp_parser_asm_operand_list (parser);
9476 /* If the next token is `::', there are no outputs, and the
9477 next token is the beginning of the inputs. */
9478 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
9480 /* Consume the `::' token. */
9481 cp_lexer_consume_token (parser->lexer);
9482 /* The inputs are coming next. */
9486 /* Look for inputs. */
9488 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
9491 /* Consume the `:'. */
9492 cp_lexer_consume_token (parser->lexer);
9493 /* Parse the output-operands. */
9494 if (cp_lexer_next_token_is_not (parser->lexer,
9496 && cp_lexer_next_token_is_not (parser->lexer,
9498 inputs = cp_parser_asm_operand_list (parser);
9500 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
9501 /* The clobbers are coming next. */
9504 /* Look for clobbers. */
9506 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
9509 /* Consume the `:'. */
9510 cp_lexer_consume_token (parser->lexer);
9511 /* Parse the clobbers. */
9512 clobbers = cp_parser_asm_clobber_list (parser);
9515 /* Look for the closing `)'. */
9516 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
9517 cp_parser_skip_to_closing_parenthesis (parser);
9518 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
9520 /* Create the ASM_STMT. */
9521 if (at_function_scope_p ())
9524 finish_asm_stmt (volatile_p
9525 ? ridpointers[(int) RID_VOLATILE] : NULL_TREE,
9526 string, outputs, inputs, clobbers);
9527 /* If the extended syntax was not used, mark the ASM_STMT. */
9529 ASM_INPUT_P (asm_stmt) = 1;
9532 assemble_asm (string);
9535 /* Declarators [gram.dcl.decl] */
9537 /* Parse an init-declarator.
9540 declarator initializer [opt]
9545 declarator asm-specification [opt] attributes [opt] initializer [opt]
9547 The DECL_SPECIFIERS and PREFIX_ATTRIBUTES apply to this declarator.
9548 Returns a reprsentation of the entity declared. The ACCESS_CHECKS
9549 represent deferred access checks from the decl-specifier-seq. If
9550 MEMBER_P is TRUE, then this declarator appears in a class scope.
9551 The new DECL created by this declarator is returned.
9553 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
9554 for a function-definition here as well. If the declarator is a
9555 declarator for a function-definition, *FUNCTION_DEFINITION_P will
9556 be TRUE upon return. By that point, the function-definition will
9557 have been completely parsed.
9559 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
9563 cp_parser_init_declarator (parser,
9567 function_definition_allowed_p,
9569 function_definition_p)
9571 tree decl_specifiers;
9572 tree prefix_attributes;
9574 bool function_definition_allowed_p;
9576 bool *function_definition_p;
9581 tree asm_specification;
9583 tree decl = NULL_TREE;
9585 tree declarator_access_checks;
9586 bool is_initialized;
9587 bool is_parenthesized_init;
9588 bool ctor_dtor_or_conv_p;
9591 /* Assume that this is not the declarator for a function
9593 if (function_definition_p)
9594 *function_definition_p = false;
9596 /* Defer access checks while parsing the declarator; we cannot know
9597 what names are accessible until we know what is being
9599 cp_parser_start_deferring_access_checks (parser);
9600 /* Parse the declarator. */
9602 = cp_parser_declarator (parser,
9603 /*abstract_p=*/false,
9604 &ctor_dtor_or_conv_p);
9605 /* Gather up the deferred checks. */
9606 declarator_access_checks
9607 = cp_parser_stop_deferring_access_checks (parser);
9609 /* If the DECLARATOR was erroneous, there's no need to go
9611 if (declarator == error_mark_node)
9612 return error_mark_node;
9614 /* Figure out what scope the entity declared by the DECLARATOR is
9615 located in. `grokdeclarator' sometimes changes the scope, so
9616 we compute it now. */
9617 scope = get_scope_of_declarator (declarator);
9619 /* If we're allowing GNU extensions, look for an asm-specification
9621 if (cp_parser_allow_gnu_extensions_p (parser))
9623 /* Look for an asm-specification. */
9624 asm_specification = cp_parser_asm_specification_opt (parser);
9625 /* And attributes. */
9626 attributes = cp_parser_attributes_opt (parser);
9630 asm_specification = NULL_TREE;
9631 attributes = NULL_TREE;
9634 /* Peek at the next token. */
9635 token = cp_lexer_peek_token (parser->lexer);
9636 /* Check to see if the token indicates the start of a
9637 function-definition. */
9638 if (cp_parser_token_starts_function_definition_p (token))
9640 if (!function_definition_allowed_p)
9642 /* If a function-definition should not appear here, issue an
9644 cp_parser_error (parser,
9645 "a function-definition is not allowed here");
9646 return error_mark_node;
9652 /* Neither attributes nor an asm-specification are allowed
9653 on a function-definition. */
9654 if (asm_specification)
9655 error ("an asm-specification is not allowed on a function-definition");
9657 error ("attributes are not allowed on a function-definition");
9658 /* This is a function-definition. */
9659 *function_definition_p = true;
9661 /* Thread the access checks together. */
9662 ac = &access_checks;
9664 ac = &TREE_CHAIN (*ac);
9665 *ac = declarator_access_checks;
9667 /* Parse the function definition. */
9668 decl = (cp_parser_function_definition_from_specifiers_and_declarator
9669 (parser, decl_specifiers, prefix_attributes, declarator,
9672 /* Pull the access-checks apart again. */
9681 Only in function declarations for constructors, destructors, and
9682 type conversions can the decl-specifier-seq be omitted.
9684 We explicitly postpone this check past the point where we handle
9685 function-definitions because we tolerate function-definitions
9686 that are missing their return types in some modes. */
9687 if (!decl_specifiers && !ctor_dtor_or_conv_p)
9689 cp_parser_error (parser,
9690 "expected constructor, destructor, or type conversion");
9691 return error_mark_node;
9694 /* An `=' or an `(' indicates an initializer. */
9695 is_initialized = (token->type == CPP_EQ
9696 || token->type == CPP_OPEN_PAREN);
9697 /* If the init-declarator isn't initialized and isn't followed by a
9698 `,' or `;', it's not a valid init-declarator. */
9700 && token->type != CPP_COMMA
9701 && token->type != CPP_SEMICOLON)
9703 cp_parser_error (parser, "expected init-declarator");
9704 return error_mark_node;
9707 /* Because start_decl has side-effects, we should only call it if we
9708 know we're going ahead. By this point, we know that we cannot
9709 possibly be looking at any other construct. */
9710 cp_parser_commit_to_tentative_parse (parser);
9712 /* Check to see whether or not this declaration is a friend. */
9713 friend_p = cp_parser_friend_p (decl_specifiers);
9715 /* Check that the number of template-parameter-lists is OK. */
9716 if (!cp_parser_check_declarator_template_parameters (parser,
9718 return error_mark_node;
9720 /* Enter the newly declared entry in the symbol table. If we're
9721 processing a declaration in a class-specifier, we wait until
9722 after processing the initializer. */
9725 if (parser->in_unbraced_linkage_specification_p)
9727 decl_specifiers = tree_cons (error_mark_node,
9728 get_identifier ("extern"),
9730 have_extern_spec = false;
9732 decl = start_decl (declarator,
9739 /* Enter the SCOPE. That way unqualified names appearing in the
9740 initializer will be looked up in SCOPE. */
9744 /* Perform deferred access control checks, now that we know in which
9745 SCOPE the declared entity resides. */
9746 if (!member_p && decl)
9748 tree saved_current_function_decl = NULL_TREE;
9750 /* If the entity being declared is a function, pretend that we
9751 are in its scope. If it is a `friend', it may have access to
9752 things that would not otherwise be accessible. */
9753 if (TREE_CODE (decl) == FUNCTION_DECL)
9755 saved_current_function_decl = current_function_decl;
9756 current_function_decl = decl;
9759 /* Perform the access control checks for the decl-specifiers. */
9760 cp_parser_perform_deferred_access_checks (access_checks);
9761 /* And for the declarator. */
9762 cp_parser_perform_deferred_access_checks (declarator_access_checks);
9764 /* Restore the saved value. */
9765 if (TREE_CODE (decl) == FUNCTION_DECL)
9766 current_function_decl = saved_current_function_decl;
9769 /* Parse the initializer. */
9771 initializer = cp_parser_initializer (parser,
9772 &is_parenthesized_init);
9775 initializer = NULL_TREE;
9776 is_parenthesized_init = false;
9779 /* The old parser allows attributes to appear after a parenthesized
9780 initializer. Mark Mitchell proposed removing this functionality
9781 on the GCC mailing lists on 2002-08-13. This parser accepts the
9782 attributes -- but ignores them. */
9783 if (cp_parser_allow_gnu_extensions_p (parser) && is_parenthesized_init)
9784 if (cp_parser_attributes_opt (parser))
9785 warning ("attributes after parenthesized initializer ignored");
9787 /* Leave the SCOPE, now that we have processed the initializer. It
9788 is important to do this before calling cp_finish_decl because it
9789 makes decisions about whether to create DECL_STMTs or not based
9790 on the current scope. */
9794 /* For an in-class declaration, use `grokfield' to create the
9797 decl = grokfield (declarator, decl_specifiers,
9798 initializer, /*asmspec=*/NULL_TREE,
9799 /*attributes=*/NULL_TREE);
9801 /* Finish processing the declaration. But, skip friend
9803 if (!friend_p && decl)
9804 cp_finish_decl (decl,
9807 /* If the initializer is in parentheses, then this is
9808 a direct-initialization, which means that an
9809 `explicit' constructor is OK. Otherwise, an
9810 `explicit' constructor cannot be used. */
9811 ((is_parenthesized_init || !is_initialized)
9812 ? 0 : LOOKUP_ONLYCONVERTING));
9817 /* Parse a declarator.
9821 ptr-operator declarator
9823 abstract-declarator:
9824 ptr-operator abstract-declarator [opt]
9825 direct-abstract-declarator
9830 attributes [opt] direct-declarator
9831 attributes [opt] ptr-operator declarator
9833 abstract-declarator:
9834 attributes [opt] ptr-operator abstract-declarator [opt]
9835 attributes [opt] direct-abstract-declarator
9837 Returns a representation of the declarator. If the declarator has
9838 the form `* declarator', then an INDIRECT_REF is returned, whose
9839 only operand is the sub-declarator. Analagously, `& declarator' is
9840 represented as an ADDR_EXPR. For `X::* declarator', a SCOPE_REF is
9841 used. The first operand is the TYPE for `X'. The second operand
9842 is an INDIRECT_REF whose operand is the sub-declarator.
9844 Otherwise, the reprsentation is as for a direct-declarator.
9846 (It would be better to define a structure type to represent
9847 declarators, rather than abusing `tree' nodes to represent
9848 declarators. That would be much clearer and save some memory.
9849 There is no reason for declarators to be garbage-collected, for
9850 example; they are created during parser and no longer needed after
9851 `grokdeclarator' has been called.)
9853 For a ptr-operator that has the optional cv-qualifier-seq,
9854 cv-qualifiers will be stored in the TREE_TYPE of the INDIRECT_REF
9857 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is set to
9858 true if this declarator represents a constructor, destructor, or
9859 type conversion operator. Otherwise, it is set to false.
9861 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
9862 a decl-specifier-seq unless it declares a constructor, destructor,
9863 or conversion. It might seem that we could check this condition in
9864 semantic analysis, rather than parsing, but that makes it difficult
9865 to handle something like `f()'. We want to notice that there are
9866 no decl-specifiers, and therefore realize that this is an
9867 expression, not a declaration.) */
9870 cp_parser_declarator (parser, abstract_p, ctor_dtor_or_conv_p)
9873 bool *ctor_dtor_or_conv_p;
9877 enum tree_code code;
9878 tree cv_qualifier_seq;
9880 tree attributes = NULL_TREE;
9882 /* Assume this is not a constructor, destructor, or type-conversion
9884 if (ctor_dtor_or_conv_p)
9885 *ctor_dtor_or_conv_p = false;
9887 if (cp_parser_allow_gnu_extensions_p (parser))
9888 attributes = cp_parser_attributes_opt (parser);
9890 /* Peek at the next token. */
9891 token = cp_lexer_peek_token (parser->lexer);
9893 /* Check for the ptr-operator production. */
9894 cp_parser_parse_tentatively (parser);
9895 /* Parse the ptr-operator. */
9896 code = cp_parser_ptr_operator (parser,
9899 /* If that worked, then we have a ptr-operator. */
9900 if (cp_parser_parse_definitely (parser))
9902 /* The dependent declarator is optional if we are parsing an
9903 abstract-declarator. */
9905 cp_parser_parse_tentatively (parser);
9907 /* Parse the dependent declarator. */
9908 declarator = cp_parser_declarator (parser, abstract_p,
9909 /*ctor_dtor_or_conv_p=*/NULL);
9911 /* If we are parsing an abstract-declarator, we must handle the
9912 case where the dependent declarator is absent. */
9913 if (abstract_p && !cp_parser_parse_definitely (parser))
9914 declarator = NULL_TREE;
9916 /* Build the representation of the ptr-operator. */
9917 if (code == INDIRECT_REF)
9918 declarator = make_pointer_declarator (cv_qualifier_seq,
9921 declarator = make_reference_declarator (cv_qualifier_seq,
9923 /* Handle the pointer-to-member case. */
9925 declarator = build_nt (SCOPE_REF, class_type, declarator);
9927 /* Everything else is a direct-declarator. */
9929 declarator = cp_parser_direct_declarator (parser,
9931 ctor_dtor_or_conv_p);
9933 if (attributes && declarator != error_mark_node)
9934 declarator = tree_cons (attributes, declarator, NULL_TREE);
9939 /* Parse a direct-declarator or direct-abstract-declarator.
9943 direct-declarator ( parameter-declaration-clause )
9944 cv-qualifier-seq [opt]
9945 exception-specification [opt]
9946 direct-declarator [ constant-expression [opt] ]
9949 direct-abstract-declarator:
9950 direct-abstract-declarator [opt]
9951 ( parameter-declaration-clause )
9952 cv-qualifier-seq [opt]
9953 exception-specification [opt]
9954 direct-abstract-declarator [opt] [ constant-expression [opt] ]
9955 ( abstract-declarator )
9957 Returns a representation of the declarator. ABSTRACT_P is TRUE if
9958 we are parsing a direct-abstract-declarator; FALSE if we are
9959 parsing a direct-declarator. CTOR_DTOR_OR_CONV_P is as for
9960 cp_parser_declarator.
9962 For the declarator-id production, the representation is as for an
9963 id-expression, except that a qualified name is represented as a
9964 SCOPE_REF. A function-declarator is represented as a CALL_EXPR;
9965 see the documentation of the FUNCTION_DECLARATOR_* macros for
9966 information about how to find the various declarator components.
9967 An array-declarator is represented as an ARRAY_REF. The
9968 direct-declarator is the first operand; the constant-expression
9969 indicating the size of the array is the second operand. */
9972 cp_parser_direct_declarator (parser, abstract_p, ctor_dtor_or_conv_p)
9975 bool *ctor_dtor_or_conv_p;
9979 tree scope = NULL_TREE;
9980 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
9981 bool saved_in_declarator_p = parser->in_declarator_p;
9983 /* Peek at the next token. */
9984 token = cp_lexer_peek_token (parser->lexer);
9985 /* Find the initial direct-declarator. It might be a parenthesized
9987 if (token->type == CPP_OPEN_PAREN)
9989 /* For an abstract declarator we do not know whether we are
9990 looking at the beginning of a parameter-declaration-clause,
9991 or at a parenthesized abstract declarator. For example, if
9992 we see `(int)', we are looking at a
9993 parameter-declaration-clause, and the
9994 direct-abstract-declarator has been omitted. If, on the
9995 other hand we are looking at `((*))' then we are looking at a
9996 parenthesized abstract-declarator. There is no easy way to
9997 tell which situation we are in. */
9999 cp_parser_parse_tentatively (parser);
10001 /* Consume the `('. */
10002 cp_lexer_consume_token (parser->lexer);
10003 /* Parse the nested declarator. */
10005 = cp_parser_declarator (parser, abstract_p, ctor_dtor_or_conv_p);
10006 /* Expect a `)'. */
10007 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
10009 /* If parsing a parenthesized abstract declarator didn't work,
10010 try a parameter-declaration-clause. */
10011 if (abstract_p && !cp_parser_parse_definitely (parser))
10012 declarator = NULL_TREE;
10013 /* If we were not parsing an abstract declarator, but failed to
10014 find a satisfactory nested declarator, then an error has
10016 else if (!abstract_p && declarator == error_mark_node)
10017 return error_mark_node;
10018 /* Default args cannot appear in an abstract decl. */
10019 parser->default_arg_ok_p = false;
10021 /* Otherwise, for a non-abstract declarator, there should be a
10023 else if (!abstract_p)
10025 declarator = cp_parser_declarator_id (parser);
10027 if (TREE_CODE (declarator) == SCOPE_REF)
10029 scope = TREE_OPERAND (declarator, 0);
10031 /* In the declaration of a member of a template class
10032 outside of the class itself, the SCOPE will sometimes be
10033 a TYPENAME_TYPE. For example, given:
10035 template <typename T>
10036 int S<T>::R::i = 3;
10038 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In this
10039 context, we must resolve S<T>::R to an ordinary type,
10040 rather than a typename type.
10042 The reason we normally avoid resolving TYPENAME_TYPEs is
10043 that a specialization of `S' might render `S<T>::R' not a
10044 type. However, if `S' is specialized, then this `i' will
10045 not be used, so there is no harm in resolving the types
10047 if (TREE_CODE (scope) == TYPENAME_TYPE)
10049 /* Resolve the TYPENAME_TYPE. */
10050 scope = cp_parser_resolve_typename_type (parser, scope);
10051 /* If that failed, the declarator is invalid. */
10052 if (scope == error_mark_node)
10053 return error_mark_node;
10054 /* Build a new DECLARATOR. */
10055 declarator = build_nt (SCOPE_REF,
10057 TREE_OPERAND (declarator, 1));
10060 else if (TREE_CODE (declarator) != IDENTIFIER_NODE)
10061 /* Default args can only appear for a function decl. */
10062 parser->default_arg_ok_p = false;
10064 /* Check to see whether the declarator-id names a constructor,
10065 destructor, or conversion. */
10066 if (ctor_dtor_or_conv_p
10067 && ((TREE_CODE (declarator) == SCOPE_REF
10068 && CLASS_TYPE_P (TREE_OPERAND (declarator, 0)))
10069 || (TREE_CODE (declarator) != SCOPE_REF
10070 && at_class_scope_p ())))
10072 tree unqualified_name;
10075 /* Get the unqualified part of the name. */
10076 if (TREE_CODE (declarator) == SCOPE_REF)
10078 class_type = TREE_OPERAND (declarator, 0);
10079 unqualified_name = TREE_OPERAND (declarator, 1);
10083 class_type = current_class_type;
10084 unqualified_name = declarator;
10087 /* See if it names ctor, dtor or conv. */
10088 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR
10089 || IDENTIFIER_TYPENAME_P (unqualified_name)
10090 || constructor_name_p (unqualified_name, class_type))
10092 *ctor_dtor_or_conv_p = true;
10093 /* We would have cleared the default arg flag above, but
10095 parser->default_arg_ok_p = saved_default_arg_ok_p;
10099 /* But for an abstract declarator, the initial direct-declarator can
10103 declarator = NULL_TREE;
10104 parser->default_arg_ok_p = false;
10107 scope = get_scope_of_declarator (declarator);
10109 /* Any names that appear after the declarator-id for a member
10110 are looked up in the containing scope. */
10111 push_scope (scope);
10114 parser->in_declarator_p = true;
10116 /* Now, parse function-declarators and array-declarators until there
10120 /* Peek at the next token. */
10121 token = cp_lexer_peek_token (parser->lexer);
10122 /* If it's a `[', we're looking at an array-declarator. */
10123 if (token->type == CPP_OPEN_SQUARE)
10127 /* Consume the `['. */
10128 cp_lexer_consume_token (parser->lexer);
10129 /* Peek at the next token. */
10130 token = cp_lexer_peek_token (parser->lexer);
10131 /* If the next token is `]', then there is no
10132 constant-expression. */
10133 if (token->type != CPP_CLOSE_SQUARE)
10134 bounds = cp_parser_constant_expression (parser);
10136 bounds = NULL_TREE;
10137 /* Look for the closing `]'. */
10138 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
10140 declarator = build_nt (ARRAY_REF, declarator, bounds);
10142 /* If it's a `(', we're looking at a function-declarator. */
10143 else if (token->type == CPP_OPEN_PAREN)
10145 /* A function-declarator. Or maybe not. Consider, for
10151 The first is the declaration of a function while the
10152 second is a the definition of a variable, including its
10155 Having seen only the parenthesis, we cannot know which of
10156 these two alternatives should be selected. Even more
10157 complex are examples like:
10162 The former is a function-declaration; the latter is a
10163 variable initialization.
10165 First, we attempt to parse a parameter-declaration
10166 clause. If this works, then we continue; otherwise, we
10167 replace the tokens consumed in the process and continue. */
10170 /* We are now parsing tentatively. */
10171 cp_parser_parse_tentatively (parser);
10173 /* Consume the `('. */
10174 cp_lexer_consume_token (parser->lexer);
10175 /* Parse the parameter-declaration-clause. */
10176 params = cp_parser_parameter_declaration_clause (parser);
10178 /* If all went well, parse the cv-qualifier-seq and the
10179 exception-specification. */
10180 if (cp_parser_parse_definitely (parser))
10182 tree cv_qualifiers;
10183 tree exception_specification;
10185 /* Consume the `)'. */
10186 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
10188 /* Parse the cv-qualifier-seq. */
10189 cv_qualifiers = cp_parser_cv_qualifier_seq_opt (parser);
10190 /* And the exception-specification. */
10191 exception_specification
10192 = cp_parser_exception_specification_opt (parser);
10194 /* Create the function-declarator. */
10195 declarator = make_call_declarator (declarator,
10198 exception_specification);
10200 /* Otherwise, we must be done with the declarator. */
10204 /* Otherwise, we're done with the declarator. */
10207 /* Any subsequent parameter lists are to do with return type, so
10208 are not those of the declared function. */
10209 parser->default_arg_ok_p = false;
10212 /* For an abstract declarator, we might wind up with nothing at this
10213 point. That's an error; the declarator is not optional. */
10215 cp_parser_error (parser, "expected declarator");
10217 /* If we entered a scope, we must exit it now. */
10221 parser->default_arg_ok_p = saved_default_arg_ok_p;
10222 parser->in_declarator_p = saved_in_declarator_p;
10227 /* Parse a ptr-operator.
10230 * cv-qualifier-seq [opt]
10232 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
10237 & cv-qualifier-seq [opt]
10239 Returns INDIRECT_REF if a pointer, or pointer-to-member, was
10240 used. Returns ADDR_EXPR if a reference was used. In the
10241 case of a pointer-to-member, *TYPE is filled in with the
10242 TYPE containing the member. *CV_QUALIFIER_SEQ is filled in
10243 with the cv-qualifier-seq, or NULL_TREE, if there are no
10244 cv-qualifiers. Returns ERROR_MARK if an error occurred. */
10246 static enum tree_code
10247 cp_parser_ptr_operator (parser, type, cv_qualifier_seq)
10250 tree *cv_qualifier_seq;
10252 enum tree_code code = ERROR_MARK;
10255 /* Assume that it's not a pointer-to-member. */
10257 /* And that there are no cv-qualifiers. */
10258 *cv_qualifier_seq = NULL_TREE;
10260 /* Peek at the next token. */
10261 token = cp_lexer_peek_token (parser->lexer);
10262 /* If it's a `*' or `&' we have a pointer or reference. */
10263 if (token->type == CPP_MULT || token->type == CPP_AND)
10265 /* Remember which ptr-operator we were processing. */
10266 code = (token->type == CPP_AND ? ADDR_EXPR : INDIRECT_REF);
10268 /* Consume the `*' or `&'. */
10269 cp_lexer_consume_token (parser->lexer);
10271 /* A `*' can be followed by a cv-qualifier-seq, and so can a
10272 `&', if we are allowing GNU extensions. (The only qualifier
10273 that can legally appear after `&' is `restrict', but that is
10274 enforced during semantic analysis. */
10275 if (code == INDIRECT_REF
10276 || cp_parser_allow_gnu_extensions_p (parser))
10277 *cv_qualifier_seq = cp_parser_cv_qualifier_seq_opt (parser);
10281 /* Try the pointer-to-member case. */
10282 cp_parser_parse_tentatively (parser);
10283 /* Look for the optional `::' operator. */
10284 cp_parser_global_scope_opt (parser,
10285 /*current_scope_valid_p=*/false);
10286 /* Look for the nested-name specifier. */
10287 cp_parser_nested_name_specifier (parser,
10288 /*typename_keyword_p=*/false,
10289 /*check_dependency_p=*/true,
10291 /* If we found it, and the next token is a `*', then we are
10292 indeed looking at a pointer-to-member operator. */
10293 if (!cp_parser_error_occurred (parser)
10294 && cp_parser_require (parser, CPP_MULT, "`*'"))
10296 /* The type of which the member is a member is given by the
10298 *type = parser->scope;
10299 /* The next name will not be qualified. */
10300 parser->scope = NULL_TREE;
10301 parser->qualifying_scope = NULL_TREE;
10302 parser->object_scope = NULL_TREE;
10303 /* Indicate that the `*' operator was used. */
10304 code = INDIRECT_REF;
10305 /* Look for the optional cv-qualifier-seq. */
10306 *cv_qualifier_seq = cp_parser_cv_qualifier_seq_opt (parser);
10308 /* If that didn't work we don't have a ptr-operator. */
10309 if (!cp_parser_parse_definitely (parser))
10310 cp_parser_error (parser, "expected ptr-operator");
10316 /* Parse an (optional) cv-qualifier-seq.
10319 cv-qualifier cv-qualifier-seq [opt]
10321 Returns a TREE_LIST. The TREE_VALUE of each node is the
10322 representation of a cv-qualifier. */
10325 cp_parser_cv_qualifier_seq_opt (parser)
10328 tree cv_qualifiers = NULL_TREE;
10334 /* Look for the next cv-qualifier. */
10335 cv_qualifier = cp_parser_cv_qualifier_opt (parser);
10336 /* If we didn't find one, we're done. */
10340 /* Add this cv-qualifier to the list. */
10342 = tree_cons (NULL_TREE, cv_qualifier, cv_qualifiers);
10345 /* We built up the list in reverse order. */
10346 return nreverse (cv_qualifiers);
10349 /* Parse an (optional) cv-qualifier.
10361 cp_parser_cv_qualifier_opt (parser)
10365 tree cv_qualifier = NULL_TREE;
10367 /* Peek at the next token. */
10368 token = cp_lexer_peek_token (parser->lexer);
10369 /* See if it's a cv-qualifier. */
10370 switch (token->keyword)
10375 /* Save the value of the token. */
10376 cv_qualifier = token->value;
10377 /* Consume the token. */
10378 cp_lexer_consume_token (parser->lexer);
10385 return cv_qualifier;
10388 /* Parse a declarator-id.
10392 :: [opt] nested-name-specifier [opt] type-name
10394 In the `id-expression' case, the value returned is as for
10395 cp_parser_id_expression if the id-expression was an unqualified-id.
10396 If the id-expression was a qualified-id, then a SCOPE_REF is
10397 returned. The first operand is the scope (either a NAMESPACE_DECL
10398 or TREE_TYPE), but the second is still just a representation of an
10402 cp_parser_declarator_id (parser)
10405 tree id_expression;
10407 /* The expression must be an id-expression. Assume that qualified
10408 names are the names of types so that:
10411 int S<T>::R::i = 3;
10413 will work; we must treat `S<T>::R' as the name of a type.
10414 Similarly, assume that qualified names are templates, where
10418 int S<T>::R<T>::i = 3;
10421 id_expression = cp_parser_id_expression (parser,
10422 /*template_keyword_p=*/false,
10423 /*check_dependency_p=*/false,
10424 /*template_p=*/NULL);
10425 /* If the name was qualified, create a SCOPE_REF to represent
10428 id_expression = build_nt (SCOPE_REF, parser->scope, id_expression);
10430 return id_expression;
10433 /* Parse a type-id.
10436 type-specifier-seq abstract-declarator [opt]
10438 Returns the TYPE specified. */
10441 cp_parser_type_id (parser)
10444 tree type_specifier_seq;
10445 tree abstract_declarator;
10447 /* Parse the type-specifier-seq. */
10449 = cp_parser_type_specifier_seq (parser);
10450 if (type_specifier_seq == error_mark_node)
10451 return error_mark_node;
10453 /* There might or might not be an abstract declarator. */
10454 cp_parser_parse_tentatively (parser);
10455 /* Look for the declarator. */
10456 abstract_declarator
10457 = cp_parser_declarator (parser, /*abstract_p=*/true, NULL);
10458 /* Check to see if there really was a declarator. */
10459 if (!cp_parser_parse_definitely (parser))
10460 abstract_declarator = NULL_TREE;
10462 return groktypename (build_tree_list (type_specifier_seq,
10463 abstract_declarator));
10466 /* Parse a type-specifier-seq.
10468 type-specifier-seq:
10469 type-specifier type-specifier-seq [opt]
10473 type-specifier-seq:
10474 attributes type-specifier-seq [opt]
10476 Returns a TREE_LIST. Either the TREE_VALUE of each node is a
10477 type-specifier, or the TREE_PURPOSE is a list of attributes. */
10480 cp_parser_type_specifier_seq (parser)
10483 bool seen_type_specifier = false;
10484 tree type_specifier_seq = NULL_TREE;
10486 /* Parse the type-specifiers and attributes. */
10489 tree type_specifier;
10491 /* Check for attributes first. */
10492 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
10494 type_specifier_seq = tree_cons (cp_parser_attributes_opt (parser),
10496 type_specifier_seq);
10500 /* After the first type-specifier, others are optional. */
10501 if (seen_type_specifier)
10502 cp_parser_parse_tentatively (parser);
10503 /* Look for the type-specifier. */
10504 type_specifier = cp_parser_type_specifier (parser,
10505 CP_PARSER_FLAGS_NONE,
10506 /*is_friend=*/false,
10507 /*is_declaration=*/false,
10510 /* If the first type-specifier could not be found, this is not a
10511 type-specifier-seq at all. */
10512 if (!seen_type_specifier && type_specifier == error_mark_node)
10513 return error_mark_node;
10514 /* If subsequent type-specifiers could not be found, the
10515 type-specifier-seq is complete. */
10516 else if (seen_type_specifier && !cp_parser_parse_definitely (parser))
10519 /* Add the new type-specifier to the list. */
10521 = tree_cons (NULL_TREE, type_specifier, type_specifier_seq);
10522 seen_type_specifier = true;
10525 /* We built up the list in reverse order. */
10526 return nreverse (type_specifier_seq);
10529 /* Parse a parameter-declaration-clause.
10531 parameter-declaration-clause:
10532 parameter-declaration-list [opt] ... [opt]
10533 parameter-declaration-list , ...
10535 Returns a representation for the parameter declarations. Each node
10536 is a TREE_LIST. (See cp_parser_parameter_declaration for the exact
10537 representation.) If the parameter-declaration-clause ends with an
10538 ellipsis, PARMLIST_ELLIPSIS_P will hold of the first node in the
10539 list. A return value of NULL_TREE indicates a
10540 parameter-declaration-clause consisting only of an ellipsis. */
10543 cp_parser_parameter_declaration_clause (parser)
10550 /* Peek at the next token. */
10551 token = cp_lexer_peek_token (parser->lexer);
10552 /* Check for trivial parameter-declaration-clauses. */
10553 if (token->type == CPP_ELLIPSIS)
10555 /* Consume the `...' token. */
10556 cp_lexer_consume_token (parser->lexer);
10559 else if (token->type == CPP_CLOSE_PAREN)
10560 /* There are no parameters. */
10561 return void_list_node;
10562 /* Check for `(void)', too, which is a special case. */
10563 else if (token->keyword == RID_VOID
10564 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
10565 == CPP_CLOSE_PAREN))
10567 /* Consume the `void' token. */
10568 cp_lexer_consume_token (parser->lexer);
10569 /* There are no parameters. */
10570 return void_list_node;
10573 /* Parse the parameter-declaration-list. */
10574 parameters = cp_parser_parameter_declaration_list (parser);
10575 /* If a parse error occurred while parsing the
10576 parameter-declaration-list, then the entire
10577 parameter-declaration-clause is erroneous. */
10578 if (parameters == error_mark_node)
10579 return error_mark_node;
10581 /* Peek at the next token. */
10582 token = cp_lexer_peek_token (parser->lexer);
10583 /* If it's a `,', the clause should terminate with an ellipsis. */
10584 if (token->type == CPP_COMMA)
10586 /* Consume the `,'. */
10587 cp_lexer_consume_token (parser->lexer);
10588 /* Expect an ellipsis. */
10590 = (cp_parser_require (parser, CPP_ELLIPSIS, "`...'") != NULL);
10592 /* It might also be `...' if the optional trailing `,' was
10594 else if (token->type == CPP_ELLIPSIS)
10596 /* Consume the `...' token. */
10597 cp_lexer_consume_token (parser->lexer);
10598 /* And remember that we saw it. */
10602 ellipsis_p = false;
10604 /* Finish the parameter list. */
10605 return finish_parmlist (parameters, ellipsis_p);
10608 /* Parse a parameter-declaration-list.
10610 parameter-declaration-list:
10611 parameter-declaration
10612 parameter-declaration-list , parameter-declaration
10614 Returns a representation of the parameter-declaration-list, as for
10615 cp_parser_parameter_declaration_clause. However, the
10616 `void_list_node' is never appended to the list. */
10619 cp_parser_parameter_declaration_list (parser)
10622 tree parameters = NULL_TREE;
10624 /* Look for more parameters. */
10628 /* Parse the parameter. */
10630 = cp_parser_parameter_declaration (parser,
10631 /*greater_than_is_operator_p=*/true);
10632 /* If a parse error ocurred parsing the parameter declaration,
10633 then the entire parameter-declaration-list is erroneous. */
10634 if (parameter == error_mark_node)
10636 parameters = error_mark_node;
10639 /* Add the new parameter to the list. */
10640 TREE_CHAIN (parameter) = parameters;
10641 parameters = parameter;
10643 /* Peek at the next token. */
10644 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
10645 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10646 /* The parameter-declaration-list is complete. */
10648 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
10652 /* Peek at the next token. */
10653 token = cp_lexer_peek_nth_token (parser->lexer, 2);
10654 /* If it's an ellipsis, then the list is complete. */
10655 if (token->type == CPP_ELLIPSIS)
10657 /* Otherwise, there must be more parameters. Consume the
10659 cp_lexer_consume_token (parser->lexer);
10663 cp_parser_error (parser, "expected `,' or `...'");
10668 /* We built up the list in reverse order; straighten it out now. */
10669 return nreverse (parameters);
10672 /* Parse a parameter declaration.
10674 parameter-declaration:
10675 decl-specifier-seq declarator
10676 decl-specifier-seq declarator = assignment-expression
10677 decl-specifier-seq abstract-declarator [opt]
10678 decl-specifier-seq abstract-declarator [opt] = assignment-expression
10680 If GREATER_THAN_IS_OPERATOR_P is FALSE, then a non-nested `>' token
10681 encountered during the parsing of the assignment-expression is not
10682 interpreted as a greater-than operator.
10684 Returns a TREE_LIST representing the parameter-declaration. The
10685 TREE_VALUE is a representation of the decl-specifier-seq and
10686 declarator. In particular, the TREE_VALUE will be a TREE_LIST
10687 whose TREE_PURPOSE represents the decl-specifier-seq and whose
10688 TREE_VALUE represents the declarator. */
10691 cp_parser_parameter_declaration (parser, greater_than_is_operator_p)
10693 bool greater_than_is_operator_p;
10695 bool declares_class_or_enum;
10696 tree decl_specifiers;
10699 tree default_argument;
10702 const char *saved_message;
10704 /* Type definitions may not appear in parameter types. */
10705 saved_message = parser->type_definition_forbidden_message;
10706 parser->type_definition_forbidden_message
10707 = "types may not be defined in parameter types";
10709 /* Parse the declaration-specifiers. */
10711 = cp_parser_decl_specifier_seq (parser,
10712 CP_PARSER_FLAGS_NONE,
10714 &declares_class_or_enum);
10715 /* If an error occurred, there's no reason to attempt to parse the
10716 rest of the declaration. */
10717 if (cp_parser_error_occurred (parser))
10719 parser->type_definition_forbidden_message = saved_message;
10720 return error_mark_node;
10723 /* Peek at the next token. */
10724 token = cp_lexer_peek_token (parser->lexer);
10725 /* If the next token is a `)', `,', `=', `>', or `...', then there
10726 is no declarator. */
10727 if (token->type == CPP_CLOSE_PAREN
10728 || token->type == CPP_COMMA
10729 || token->type == CPP_EQ
10730 || token->type == CPP_ELLIPSIS
10731 || token->type == CPP_GREATER)
10732 declarator = NULL_TREE;
10733 /* Otherwise, there should be a declarator. */
10736 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
10737 parser->default_arg_ok_p = false;
10739 /* We don't know whether the declarator will be abstract or
10740 not. So, first we try an ordinary declarator. */
10741 cp_parser_parse_tentatively (parser);
10742 declarator = cp_parser_declarator (parser,
10743 /*abstract_p=*/false,
10744 /*ctor_dtor_or_conv_p=*/NULL);
10745 /* If that didn't work, look for an abstract declarator. */
10746 if (!cp_parser_parse_definitely (parser))
10747 declarator = cp_parser_declarator (parser,
10748 /*abstract_p=*/true,
10749 /*ctor_dtor_or_conv_p=*/NULL);
10750 parser->default_arg_ok_p = saved_default_arg_ok_p;
10753 /* The restriction on definining new types applies only to the type
10754 of the parameter, not to the default argument. */
10755 parser->type_definition_forbidden_message = saved_message;
10757 /* If the next token is `=', then process a default argument. */
10758 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10760 bool saved_greater_than_is_operator_p;
10761 /* Consume the `='. */
10762 cp_lexer_consume_token (parser->lexer);
10764 /* If we are defining a class, then the tokens that make up the
10765 default argument must be saved and processed later. */
10766 if (at_class_scope_p () && TYPE_BEING_DEFINED (current_class_type))
10768 unsigned depth = 0;
10770 /* Create a DEFAULT_ARG to represented the unparsed default
10772 default_argument = make_node (DEFAULT_ARG);
10773 DEFARG_TOKENS (default_argument) = cp_token_cache_new ();
10775 /* Add tokens until we have processed the entire default
10782 /* Peek at the next token. */
10783 token = cp_lexer_peek_token (parser->lexer);
10784 /* What we do depends on what token we have. */
10785 switch (token->type)
10787 /* In valid code, a default argument must be
10788 immediately followed by a `,' `)', or `...'. */
10790 case CPP_CLOSE_PAREN:
10792 /* If we run into a non-nested `;', `}', or `]',
10793 then the code is invalid -- but the default
10794 argument is certainly over. */
10795 case CPP_SEMICOLON:
10796 case CPP_CLOSE_BRACE:
10797 case CPP_CLOSE_SQUARE:
10800 /* Update DEPTH, if necessary. */
10801 else if (token->type == CPP_CLOSE_PAREN
10802 || token->type == CPP_CLOSE_BRACE
10803 || token->type == CPP_CLOSE_SQUARE)
10807 case CPP_OPEN_PAREN:
10808 case CPP_OPEN_SQUARE:
10809 case CPP_OPEN_BRACE:
10814 /* If we see a non-nested `>', and `>' is not an
10815 operator, then it marks the end of the default
10817 if (!depth && !greater_than_is_operator_p)
10821 /* If we run out of tokens, issue an error message. */
10823 error ("file ends in default argument");
10829 /* In these cases, we should look for template-ids.
10830 For example, if the default argument is
10831 `X<int, double>()', we need to do name lookup to
10832 figure out whether or not `X' is a template; if
10833 so, the `,' does not end the deault argument.
10835 That is not yet done. */
10842 /* If we've reached the end, stop. */
10846 /* Add the token to the token block. */
10847 token = cp_lexer_consume_token (parser->lexer);
10848 cp_token_cache_push_token (DEFARG_TOKENS (default_argument),
10852 /* Outside of a class definition, we can just parse the
10853 assignment-expression. */
10856 bool saved_local_variables_forbidden_p;
10858 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
10860 saved_greater_than_is_operator_p
10861 = parser->greater_than_is_operator_p;
10862 parser->greater_than_is_operator_p = greater_than_is_operator_p;
10863 /* Local variable names (and the `this' keyword) may not
10864 appear in a default argument. */
10865 saved_local_variables_forbidden_p
10866 = parser->local_variables_forbidden_p;
10867 parser->local_variables_forbidden_p = true;
10868 /* Parse the assignment-expression. */
10869 default_argument = cp_parser_assignment_expression (parser);
10870 /* Restore saved state. */
10871 parser->greater_than_is_operator_p
10872 = saved_greater_than_is_operator_p;
10873 parser->local_variables_forbidden_p
10874 = saved_local_variables_forbidden_p;
10876 if (!parser->default_arg_ok_p)
10878 pedwarn ("default arguments are only permitted on functions");
10879 if (flag_pedantic_errors)
10880 default_argument = NULL_TREE;
10884 default_argument = NULL_TREE;
10886 /* Create the representation of the parameter. */
10888 decl_specifiers = tree_cons (attributes, NULL_TREE, decl_specifiers);
10889 parameter = build_tree_list (default_argument,
10890 build_tree_list (decl_specifiers,
10896 /* Parse a function-definition.
10898 function-definition:
10899 decl-specifier-seq [opt] declarator ctor-initializer [opt]
10901 decl-specifier-seq [opt] declarator function-try-block
10905 function-definition:
10906 __extension__ function-definition
10908 Returns the FUNCTION_DECL for the function. If FRIEND_P is
10909 non-NULL, *FRIEND_P is set to TRUE iff the function was declared to
10913 cp_parser_function_definition (parser, friend_p)
10917 tree decl_specifiers;
10921 tree access_checks;
10923 bool declares_class_or_enum;
10925 /* The saved value of the PEDANTIC flag. */
10926 int saved_pedantic;
10928 /* Any pending qualification must be cleared by our caller. It is
10929 more robust to force the callers to clear PARSER->SCOPE than to
10930 do it here since if the qualification is in effect here, it might
10931 also end up in effect elsewhere that it is not intended. */
10932 my_friendly_assert (!parser->scope, 20010821);
10934 /* Handle `__extension__'. */
10935 if (cp_parser_extension_opt (parser, &saved_pedantic))
10937 /* Parse the function-definition. */
10938 fn = cp_parser_function_definition (parser, friend_p);
10939 /* Restore the PEDANTIC flag. */
10940 pedantic = saved_pedantic;
10945 /* Check to see if this definition appears in a class-specifier. */
10946 member_p = (at_class_scope_p ()
10947 && TYPE_BEING_DEFINED (current_class_type));
10948 /* Defer access checks in the decl-specifier-seq until we know what
10949 function is being defined. There is no need to do this for the
10950 definition of member functions; we cannot be defining a member
10951 from another class. */
10953 cp_parser_start_deferring_access_checks (parser);
10954 /* Parse the decl-specifier-seq. */
10956 = cp_parser_decl_specifier_seq (parser,
10957 CP_PARSER_FLAGS_OPTIONAL,
10959 &declares_class_or_enum);
10960 /* Figure out whether this declaration is a `friend'. */
10962 *friend_p = cp_parser_friend_p (decl_specifiers);
10964 /* Parse the declarator. */
10965 declarator = cp_parser_declarator (parser,
10966 /*abstract_p=*/false,
10967 /*ctor_dtor_or_conv_p=*/NULL);
10969 /* Gather up any access checks that occurred. */
10971 access_checks = cp_parser_stop_deferring_access_checks (parser);
10973 access_checks = NULL_TREE;
10975 /* If something has already gone wrong, we may as well stop now. */
10976 if (declarator == error_mark_node)
10978 /* Skip to the end of the function, or if this wasn't anything
10979 like a function-definition, to a `;' in the hopes of finding
10980 a sensible place from which to continue parsing. */
10981 cp_parser_skip_to_end_of_block_or_statement (parser);
10982 return error_mark_node;
10985 /* The next character should be a `{' (for a simple function
10986 definition), a `:' (for a ctor-initializer), or `try' (for a
10987 function-try block). */
10988 token = cp_lexer_peek_token (parser->lexer);
10989 if (!cp_parser_token_starts_function_definition_p (token))
10991 /* Issue the error-message. */
10992 cp_parser_error (parser, "expected function-definition");
10993 /* Skip to the next `;'. */
10994 cp_parser_skip_to_end_of_block_or_statement (parser);
10996 return error_mark_node;
10999 /* If we are in a class scope, then we must handle
11000 function-definitions specially. In particular, we save away the
11001 tokens that make up the function body, and parse them again
11002 later, in order to handle code like:
11005 int f () { return i; }
11009 Here, we cannot parse the body of `f' until after we have seen
11010 the declaration of `i'. */
11013 cp_token_cache *cache;
11015 /* Create the function-declaration. */
11016 fn = start_method (decl_specifiers, declarator, attributes);
11017 /* If something went badly wrong, bail out now. */
11018 if (fn == error_mark_node)
11020 /* If there's a function-body, skip it. */
11021 if (cp_parser_token_starts_function_definition_p
11022 (cp_lexer_peek_token (parser->lexer)))
11023 cp_parser_skip_to_end_of_block_or_statement (parser);
11024 return error_mark_node;
11027 /* Create a token cache. */
11028 cache = cp_token_cache_new ();
11029 /* Save away the tokens that make up the body of the
11031 cp_parser_cache_group (parser, cache, CPP_CLOSE_BRACE, /*depth=*/0);
11032 /* Handle function try blocks. */
11033 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
11034 cp_parser_cache_group (parser, cache, CPP_CLOSE_BRACE, /*depth=*/0);
11036 /* Save away the inline definition; we will process it when the
11037 class is complete. */
11038 DECL_PENDING_INLINE_INFO (fn) = cache;
11039 DECL_PENDING_INLINE_P (fn) = 1;
11041 /* We're done with the inline definition. */
11042 finish_method (fn);
11044 /* Add FN to the queue of functions to be parsed later. */
11045 TREE_VALUE (parser->unparsed_functions_queues)
11046 = tree_cons (current_class_type, fn,
11047 TREE_VALUE (parser->unparsed_functions_queues));
11052 /* Check that the number of template-parameter-lists is OK. */
11053 if (!cp_parser_check_declarator_template_parameters (parser,
11056 cp_parser_skip_to_end_of_block_or_statement (parser);
11057 return error_mark_node;
11060 return (cp_parser_function_definition_from_specifiers_and_declarator
11061 (parser, decl_specifiers, attributes, declarator, access_checks));
11064 /* Parse a function-body.
11067 compound_statement */
11070 cp_parser_function_body (cp_parser *parser)
11072 cp_parser_compound_statement (parser);
11075 /* Parse a ctor-initializer-opt followed by a function-body. Return
11076 true if a ctor-initializer was present. */
11079 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
11082 bool ctor_initializer_p;
11084 /* Begin the function body. */
11085 body = begin_function_body ();
11086 /* Parse the optional ctor-initializer. */
11087 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
11088 /* Parse the function-body. */
11089 cp_parser_function_body (parser);
11090 /* Finish the function body. */
11091 finish_function_body (body);
11093 return ctor_initializer_p;
11096 /* Parse an initializer.
11099 = initializer-clause
11100 ( expression-list )
11102 Returns a expression representing the initializer. If no
11103 initializer is present, NULL_TREE is returned.
11105 *IS_PARENTHESIZED_INIT is set to TRUE if the `( expression-list )'
11106 production is used, and zero otherwise. *IS_PARENTHESIZED_INIT is
11107 set to FALSE if there is no initializer present. */
11110 cp_parser_initializer (parser, is_parenthesized_init)
11112 bool *is_parenthesized_init;
11117 /* Peek at the next token. */
11118 token = cp_lexer_peek_token (parser->lexer);
11120 /* Let our caller know whether or not this initializer was
11122 *is_parenthesized_init = (token->type == CPP_OPEN_PAREN);
11124 if (token->type == CPP_EQ)
11126 /* Consume the `='. */
11127 cp_lexer_consume_token (parser->lexer);
11128 /* Parse the initializer-clause. */
11129 init = cp_parser_initializer_clause (parser);
11131 else if (token->type == CPP_OPEN_PAREN)
11133 /* Consume the `('. */
11134 cp_lexer_consume_token (parser->lexer);
11135 /* Parse the expression-list. */
11136 init = cp_parser_expression_list (parser);
11137 /* Consume the `)' token. */
11138 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
11139 cp_parser_skip_to_closing_parenthesis (parser);
11143 /* Anything else is an error. */
11144 cp_parser_error (parser, "expected initializer");
11145 init = error_mark_node;
11151 /* Parse an initializer-clause.
11153 initializer-clause:
11154 assignment-expression
11155 { initializer-list , [opt] }
11158 Returns an expression representing the initializer.
11160 If the `assignment-expression' production is used the value
11161 returned is simply a reprsentation for the expression.
11163 Otherwise, a CONSTRUCTOR is returned. The CONSTRUCTOR_ELTS will be
11164 the elements of the initializer-list (or NULL_TREE, if the last
11165 production is used). The TREE_TYPE for the CONSTRUCTOR will be
11166 NULL_TREE. There is no way to detect whether or not the optional
11167 trailing `,' was provided. */
11170 cp_parser_initializer_clause (parser)
11175 /* If it is not a `{', then we are looking at an
11176 assignment-expression. */
11177 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
11178 initializer = cp_parser_assignment_expression (parser);
11181 /* Consume the `{' token. */
11182 cp_lexer_consume_token (parser->lexer);
11183 /* Create a CONSTRUCTOR to represent the braced-initializer. */
11184 initializer = make_node (CONSTRUCTOR);
11185 /* Mark it with TREE_HAS_CONSTRUCTOR. This should not be
11186 necessary, but check_initializer depends upon it, for
11188 TREE_HAS_CONSTRUCTOR (initializer) = 1;
11189 /* If it's not a `}', then there is a non-trivial initializer. */
11190 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
11192 /* Parse the initializer list. */
11193 CONSTRUCTOR_ELTS (initializer)
11194 = cp_parser_initializer_list (parser);
11195 /* A trailing `,' token is allowed. */
11196 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
11197 cp_lexer_consume_token (parser->lexer);
11200 /* Now, there should be a trailing `}'. */
11201 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
11204 return initializer;
11207 /* Parse an initializer-list.
11211 initializer-list , initializer-clause
11216 identifier : initializer-clause
11217 initializer-list, identifier : initializer-clause
11219 Returns a TREE_LIST. The TREE_VALUE of each node is an expression
11220 for the initializer. If the TREE_PURPOSE is non-NULL, it is the
11221 IDENTIFIER_NODE naming the field to initialize. */
11224 cp_parser_initializer_list (parser)
11227 tree initializers = NULL_TREE;
11229 /* Parse the rest of the list. */
11236 /* If the next token is an identifier and the following one is a
11237 colon, we are looking at the GNU designated-initializer
11239 if (cp_parser_allow_gnu_extensions_p (parser)
11240 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
11241 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
11243 /* Consume the identifier. */
11244 identifier = cp_lexer_consume_token (parser->lexer)->value;
11245 /* Consume the `:'. */
11246 cp_lexer_consume_token (parser->lexer);
11249 identifier = NULL_TREE;
11251 /* Parse the initializer. */
11252 initializer = cp_parser_initializer_clause (parser);
11254 /* Add it to the list. */
11255 initializers = tree_cons (identifier, initializer, initializers);
11257 /* If the next token is not a comma, we have reached the end of
11259 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11262 /* Peek at the next token. */
11263 token = cp_lexer_peek_nth_token (parser->lexer, 2);
11264 /* If the next token is a `}', then we're still done. An
11265 initializer-clause can have a trailing `,' after the
11266 initializer-list and before the closing `}'. */
11267 if (token->type == CPP_CLOSE_BRACE)
11270 /* Consume the `,' token. */
11271 cp_lexer_consume_token (parser->lexer);
11274 /* The initializers were built up in reverse order, so we need to
11275 reverse them now. */
11276 return nreverse (initializers);
11279 /* Classes [gram.class] */
11281 /* Parse a class-name.
11287 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
11288 to indicate that names looked up in dependent types should be
11289 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
11290 keyword has been used to indicate that the name that appears next
11291 is a template. TYPE_P is true iff the next name should be treated
11292 as class-name, even if it is declared to be some other kind of name
11293 as well. The accessibility of the class-name is checked iff
11294 CHECK_ACCESS_P is true. If CHECK_DEPENDENCY_P is FALSE, names are
11295 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
11296 is the class being defined in a class-head.
11298 Returns the TYPE_DECL representing the class. */
11301 cp_parser_class_name (cp_parser *parser,
11302 bool typename_keyword_p,
11303 bool template_keyword_p,
11305 bool check_access_p,
11306 bool check_dependency_p,
11313 /* PARSER->SCOPE can be cleared when parsing the template-arguments
11314 to a template-id, so we save it here. */
11315 scope = parser->scope;
11316 /* Any name names a type if we're following the `typename' keyword
11317 in a qualified name where the enclosing scope is type-dependent. */
11318 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
11319 && cp_parser_dependent_type_p (scope));
11321 /* We don't know whether what comes next is a template-id or
11323 cp_parser_parse_tentatively (parser);
11324 /* Try a template-id. */
11325 decl = cp_parser_template_id (parser, template_keyword_p,
11326 check_dependency_p);
11327 if (cp_parser_parse_definitely (parser))
11329 if (decl == error_mark_node)
11330 return error_mark_node;
11334 /* If it wasn't a template-id, try a simple identifier. */
11337 /* Look for the identifier. */
11338 identifier = cp_parser_identifier (parser);
11339 /* If the next token isn't an identifier, we are certainly not
11340 looking at a class-name. */
11341 if (identifier == error_mark_node)
11342 decl = error_mark_node;
11343 /* If we know this is a type-name, there's no need to look it
11345 else if (typename_p)
11349 /* If the next token is a `::', then the name must be a type
11352 [basic.lookup.qual]
11354 During the lookup for a name preceding the :: scope
11355 resolution operator, object, function, and enumerator
11356 names are ignored. */
11357 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
11359 /* Look up the name. */
11360 decl = cp_parser_lookup_name (parser, identifier,
11363 check_dependency_p);
11367 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
11369 /* If this is a typename, create a TYPENAME_TYPE. */
11370 if (typename_p && decl != error_mark_node)
11371 decl = TYPE_NAME (make_typename_type (scope, decl,
11374 /* Check to see that it is really the name of a class. */
11375 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
11376 && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
11377 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
11378 /* Situations like this:
11380 template <typename T> struct A {
11381 typename T::template X<int>::I i;
11384 are problematic. Is `T::template X<int>' a class-name? The
11385 standard does not seem to be definitive, but there is no other
11386 valid interpretation of the following `::'. Therefore, those
11387 names are considered class-names. */
11388 decl = TYPE_NAME (make_typename_type (scope, decl,
11389 tf_error | tf_parsing));
11390 else if (decl == error_mark_node
11391 || TREE_CODE (decl) != TYPE_DECL
11392 || !IS_AGGR_TYPE (TREE_TYPE (decl)))
11394 cp_parser_error (parser, "expected class-name");
11395 return error_mark_node;
11401 /* Parse a class-specifier.
11404 class-head { member-specification [opt] }
11406 Returns the TREE_TYPE representing the class. */
11409 cp_parser_class_specifier (parser)
11414 tree attributes = NULL_TREE;
11415 int has_trailing_semicolon;
11416 bool nested_name_specifier_p;
11417 bool deferring_access_checks_p;
11418 tree saved_access_checks;
11419 unsigned saved_num_template_parameter_lists;
11421 /* Parse the class-head. */
11422 type = cp_parser_class_head (parser,
11423 &nested_name_specifier_p,
11424 &deferring_access_checks_p,
11425 &saved_access_checks);
11426 /* If the class-head was a semantic disaster, skip the entire body
11430 cp_parser_skip_to_end_of_block_or_statement (parser);
11431 return error_mark_node;
11433 /* Look for the `{'. */
11434 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
11435 return error_mark_node;
11436 /* Issue an error message if type-definitions are forbidden here. */
11437 cp_parser_check_type_definition (parser);
11438 /* Remember that we are defining one more class. */
11439 ++parser->num_classes_being_defined;
11440 /* Inside the class, surrounding template-parameter-lists do not
11442 saved_num_template_parameter_lists
11443 = parser->num_template_parameter_lists;
11444 parser->num_template_parameter_lists = 0;
11445 /* Start the class. */
11446 type = begin_class_definition (type);
11447 if (type == error_mark_node)
11448 /* If the type is erroneous, skip the entire body of the class. */
11449 cp_parser_skip_to_closing_brace (parser);
11451 /* Parse the member-specification. */
11452 cp_parser_member_specification_opt (parser);
11453 /* Look for the trailing `}'. */
11454 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
11455 /* We get better error messages by noticing a common problem: a
11456 missing trailing `;'. */
11457 token = cp_lexer_peek_token (parser->lexer);
11458 has_trailing_semicolon = (token->type == CPP_SEMICOLON);
11459 /* Look for attributes to apply to this class. */
11460 if (cp_parser_allow_gnu_extensions_p (parser))
11461 attributes = cp_parser_attributes_opt (parser);
11462 /* Finish the class definition. */
11463 type = finish_class_definition (type,
11465 has_trailing_semicolon,
11466 nested_name_specifier_p);
11467 /* If this class is not itself within the scope of another class,
11468 then we need to parse the bodies of all of the queued function
11469 definitions. Note that the queued functions defined in a class
11470 are not always processed immediately following the
11471 class-specifier for that class. Consider:
11474 struct B { void f() { sizeof (A); } };
11477 If `f' were processed before the processing of `A' were
11478 completed, there would be no way to compute the size of `A'.
11479 Note that the nesting we are interested in here is lexical --
11480 not the semantic nesting given by TYPE_CONTEXT. In particular,
11483 struct A { struct B; };
11484 struct A::B { void f() { } };
11486 there is no need to delay the parsing of `A::B::f'. */
11487 if (--parser->num_classes_being_defined == 0)
11489 tree last_scope = NULL_TREE;
11491 /* Process non FUNCTION_DECL related DEFAULT_ARGs. */
11492 for (parser->default_arg_types = nreverse (parser->default_arg_types);
11493 parser->default_arg_types;
11494 parser->default_arg_types = TREE_CHAIN (parser->default_arg_types))
11495 cp_parser_late_parsing_default_args
11496 (parser, TREE_PURPOSE (parser->default_arg_types));
11498 /* Reverse the queue, so that we process it in the order the
11499 functions were declared. */
11500 TREE_VALUE (parser->unparsed_functions_queues)
11501 = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
11502 /* Loop through all of the functions. */
11503 while (TREE_VALUE (parser->unparsed_functions_queues))
11510 /* Figure out which function we need to process. */
11511 queue_entry = TREE_VALUE (parser->unparsed_functions_queues);
11512 fn_scope = TREE_PURPOSE (queue_entry);
11513 fn = TREE_VALUE (queue_entry);
11515 /* Parse the function. */
11516 cp_parser_late_parsing_for_member (parser, fn);
11518 TREE_VALUE (parser->unparsed_functions_queues)
11519 = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues));
11522 /* If LAST_SCOPE is non-NULL, then we have pushed scopes one
11523 more time than we have popped, so me must pop here. */
11525 pop_scope (last_scope);
11528 /* Put back any saved access checks. */
11529 if (deferring_access_checks_p)
11531 cp_parser_start_deferring_access_checks (parser);
11532 parser->context->deferred_access_checks = saved_access_checks;
11535 /* Restore the count of active template-parameter-lists. */
11536 parser->num_template_parameter_lists
11537 = saved_num_template_parameter_lists;
11542 /* Parse a class-head.
11545 class-key identifier [opt] base-clause [opt]
11546 class-key nested-name-specifier identifier base-clause [opt]
11547 class-key nested-name-specifier [opt] template-id
11551 class-key attributes identifier [opt] base-clause [opt]
11552 class-key attributes nested-name-specifier identifier base-clause [opt]
11553 class-key attributes nested-name-specifier [opt] template-id
11556 Returns the TYPE of the indicated class. Sets
11557 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
11558 involving a nested-name-specifier was used, and FALSE otherwise.
11559 Sets *DEFERRING_ACCESS_CHECKS_P to TRUE iff we were deferring
11560 access checks before this class-head. In that case,
11561 *SAVED_ACCESS_CHECKS is set to the current list of deferred access
11564 Returns NULL_TREE if the class-head is syntactically valid, but
11565 semantically invalid in a way that means we should skip the entire
11566 body of the class. */
11569 cp_parser_class_head (parser,
11570 nested_name_specifier_p,
11571 deferring_access_checks_p,
11572 saved_access_checks)
11574 bool *nested_name_specifier_p;
11575 bool *deferring_access_checks_p;
11576 tree *saved_access_checks;
11579 tree nested_name_specifier;
11580 enum tag_types class_key;
11581 tree id = NULL_TREE;
11582 tree type = NULL_TREE;
11584 bool template_id_p = false;
11585 bool qualified_p = false;
11586 bool invalid_nested_name_p = false;
11587 unsigned num_templates;
11589 /* Assume no nested-name-specifier will be present. */
11590 *nested_name_specifier_p = false;
11591 /* Assume no template parameter lists will be used in defining the
11595 /* Look for the class-key. */
11596 class_key = cp_parser_class_key (parser);
11597 if (class_key == none_type)
11598 return error_mark_node;
11600 /* Parse the attributes. */
11601 attributes = cp_parser_attributes_opt (parser);
11603 /* If the next token is `::', that is invalid -- but sometimes
11604 people do try to write:
11608 Handle this gracefully by accepting the extra qualifier, and then
11609 issuing an error about it later if this really is a
11610 class-header. If it turns out just to be an elaborated type
11611 specifier, remain silent. */
11612 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
11613 qualified_p = true;
11615 /* Determine the name of the class. Begin by looking for an
11616 optional nested-name-specifier. */
11617 nested_name_specifier
11618 = cp_parser_nested_name_specifier_opt (parser,
11619 /*typename_keyword_p=*/false,
11620 /*check_dependency_p=*/true,
11622 /* If there was a nested-name-specifier, then there *must* be an
11624 if (nested_name_specifier)
11626 /* Although the grammar says `identifier', it really means
11627 `class-name' or `template-name'. You are only allowed to
11628 define a class that has already been declared with this
11631 The proposed resolution for Core Issue 180 says that whever
11632 you see `class T::X' you should treat `X' as a type-name.
11634 It is OK to define an inaccessible class; for example:
11636 class A { class B; };
11639 So, we ask cp_parser_class_name not to check accessibility.
11641 We do not know if we will see a class-name, or a
11642 template-name. We look for a class-name first, in case the
11643 class-name is a template-id; if we looked for the
11644 template-name first we would stop after the template-name. */
11645 cp_parser_parse_tentatively (parser);
11646 type = cp_parser_class_name (parser,
11647 /*typename_keyword_p=*/false,
11648 /*template_keyword_p=*/false,
11650 /*check_access_p=*/false,
11651 /*check_dependency_p=*/false,
11652 /*class_head_p=*/true);
11653 /* If that didn't work, ignore the nested-name-specifier. */
11654 if (!cp_parser_parse_definitely (parser))
11656 invalid_nested_name_p = true;
11657 id = cp_parser_identifier (parser);
11658 if (id == error_mark_node)
11661 /* If we could not find a corresponding TYPE, treat this
11662 declaration like an unqualified declaration. */
11663 if (type == error_mark_node)
11664 nested_name_specifier = NULL_TREE;
11665 /* Otherwise, count the number of templates used in TYPE and its
11666 containing scopes. */
11671 for (scope = TREE_TYPE (type);
11672 scope && TREE_CODE (scope) != NAMESPACE_DECL;
11673 scope = (TYPE_P (scope)
11674 ? TYPE_CONTEXT (scope)
11675 : DECL_CONTEXT (scope)))
11677 && CLASS_TYPE_P (scope)
11678 && CLASSTYPE_TEMPLATE_INFO (scope)
11679 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
11683 /* Otherwise, the identifier is optional. */
11686 /* We don't know whether what comes next is a template-id,
11687 an identifier, or nothing at all. */
11688 cp_parser_parse_tentatively (parser);
11689 /* Check for a template-id. */
11690 id = cp_parser_template_id (parser,
11691 /*template_keyword_p=*/false,
11692 /*check_dependency_p=*/true);
11693 /* If that didn't work, it could still be an identifier. */
11694 if (!cp_parser_parse_definitely (parser))
11696 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
11697 id = cp_parser_identifier (parser);
11703 template_id_p = true;
11708 /* If it's not a `:' or a `{' then we can't really be looking at a
11709 class-head, since a class-head only appears as part of a
11710 class-specifier. We have to detect this situation before calling
11711 xref_tag, since that has irreversible side-effects. */
11712 if (!cp_parser_next_token_starts_class_definition_p (parser))
11714 cp_parser_error (parser, "expected `{' or `:'");
11715 return error_mark_node;
11718 /* At this point, we're going ahead with the class-specifier, even
11719 if some other problem occurs. */
11720 cp_parser_commit_to_tentative_parse (parser);
11721 /* Issue the error about the overly-qualified name now. */
11723 cp_parser_error (parser,
11724 "global qualification of class name is invalid");
11725 else if (invalid_nested_name_p)
11726 cp_parser_error (parser,
11727 "qualified name does not name a class");
11728 /* Make sure that the right number of template parameters were
11730 if (!cp_parser_check_template_parameters (parser, num_templates))
11731 /* If something went wrong, there is no point in even trying to
11732 process the class-definition. */
11735 /* We do not need to defer access checks for entities declared
11736 within the class. But, we do need to save any access checks that
11737 are currently deferred and restore them later, in case we are in
11738 the middle of something else. */
11739 *deferring_access_checks_p = parser->context->deferring_access_checks_p;
11740 if (*deferring_access_checks_p)
11741 *saved_access_checks = cp_parser_stop_deferring_access_checks (parser);
11743 /* Look up the type. */
11746 type = TREE_TYPE (id);
11747 maybe_process_partial_specialization (type);
11749 else if (!nested_name_specifier)
11751 /* If the class was unnamed, create a dummy name. */
11753 id = make_anon_name ();
11754 type = xref_tag (class_key, id, attributes, /*globalize=*/0);
11763 template <typename T> struct S { struct T };
11764 template <typename T> struct S::T { };
11766 we will get a TYPENAME_TYPE when processing the definition of
11767 `S::T'. We need to resolve it to the actual type before we
11768 try to define it. */
11769 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
11771 type = cp_parser_resolve_typename_type (parser, TREE_TYPE (type));
11772 if (type != error_mark_node)
11773 type = TYPE_NAME (type);
11776 maybe_process_partial_specialization (TREE_TYPE (type));
11777 class_type = current_class_type;
11778 type = TREE_TYPE (handle_class_head (class_key,
11779 nested_name_specifier,
11784 if (type != error_mark_node)
11786 if (!class_type && TYPE_CONTEXT (type))
11787 *nested_name_specifier_p = true;
11788 else if (class_type && !same_type_p (TYPE_CONTEXT (type),
11790 *nested_name_specifier_p = true;
11793 /* Indicate whether this class was declared as a `class' or as a
11795 if (TREE_CODE (type) == RECORD_TYPE)
11796 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
11797 cp_parser_check_class_key (class_key, type);
11799 /* Enter the scope containing the class; the names of base classes
11800 should be looked up in that context. For example, given:
11802 struct A { struct B {}; struct C; };
11803 struct A::C : B {};
11806 if (nested_name_specifier)
11807 push_scope (nested_name_specifier);
11808 /* Now, look for the base-clause. */
11809 token = cp_lexer_peek_token (parser->lexer);
11810 if (token->type == CPP_COLON)
11814 /* Get the list of base-classes. */
11815 bases = cp_parser_base_clause (parser);
11816 /* Process them. */
11817 xref_basetypes (type, bases);
11819 /* Leave the scope given by the nested-name-specifier. We will
11820 enter the class scope itself while processing the members. */
11821 if (nested_name_specifier)
11822 pop_scope (nested_name_specifier);
11827 /* Parse a class-key.
11834 Returns the kind of class-key specified, or none_type to indicate
11837 static enum tag_types
11838 cp_parser_class_key (parser)
11842 enum tag_types tag_type;
11844 /* Look for the class-key. */
11845 token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
11849 /* Check to see if the TOKEN is a class-key. */
11850 tag_type = cp_parser_token_is_class_key (token);
11852 cp_parser_error (parser, "expected class-key");
11856 /* Parse an (optional) member-specification.
11858 member-specification:
11859 member-declaration member-specification [opt]
11860 access-specifier : member-specification [opt] */
11863 cp_parser_member_specification_opt (parser)
11871 /* Peek at the next token. */
11872 token = cp_lexer_peek_token (parser->lexer);
11873 /* If it's a `}', or EOF then we've seen all the members. */
11874 if (token->type == CPP_CLOSE_BRACE || token->type == CPP_EOF)
11877 /* See if this token is a keyword. */
11878 keyword = token->keyword;
11882 case RID_PROTECTED:
11884 /* Consume the access-specifier. */
11885 cp_lexer_consume_token (parser->lexer);
11886 /* Remember which access-specifier is active. */
11887 current_access_specifier = token->value;
11888 /* Look for the `:'. */
11889 cp_parser_require (parser, CPP_COLON, "`:'");
11893 /* Otherwise, the next construction must be a
11894 member-declaration. */
11895 cp_parser_member_declaration (parser);
11896 reset_type_access_control ();
11901 /* Parse a member-declaration.
11903 member-declaration:
11904 decl-specifier-seq [opt] member-declarator-list [opt] ;
11905 function-definition ; [opt]
11906 :: [opt] nested-name-specifier template [opt] unqualified-id ;
11908 template-declaration
11910 member-declarator-list:
11912 member-declarator-list , member-declarator
11915 declarator pure-specifier [opt]
11916 declarator constant-initializer [opt]
11917 identifier [opt] : constant-expression
11921 member-declaration:
11922 __extension__ member-declaration
11925 declarator attributes [opt] pure-specifier [opt]
11926 declarator attributes [opt] constant-initializer [opt]
11927 identifier [opt] attributes [opt] : constant-expression */
11930 cp_parser_member_declaration (parser)
11933 tree decl_specifiers;
11934 tree prefix_attributes;
11936 bool declares_class_or_enum;
11939 int saved_pedantic;
11941 /* Check for the `__extension__' keyword. */
11942 if (cp_parser_extension_opt (parser, &saved_pedantic))
11945 cp_parser_member_declaration (parser);
11946 /* Restore the old value of the PEDANTIC flag. */
11947 pedantic = saved_pedantic;
11952 /* Check for a template-declaration. */
11953 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
11955 /* Parse the template-declaration. */
11956 cp_parser_template_declaration (parser, /*member_p=*/true);
11961 /* Check for a using-declaration. */
11962 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
11964 /* Parse the using-declaration. */
11965 cp_parser_using_declaration (parser);
11970 /* We can't tell whether we're looking at a declaration or a
11971 function-definition. */
11972 cp_parser_parse_tentatively (parser);
11974 /* Parse the decl-specifier-seq. */
11976 = cp_parser_decl_specifier_seq (parser,
11977 CP_PARSER_FLAGS_OPTIONAL,
11978 &prefix_attributes,
11979 &declares_class_or_enum);
11980 /* If there is no declarator, then the decl-specifier-seq should
11982 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
11984 /* If there was no decl-specifier-seq, and the next token is a
11985 `;', then we have something like:
11991 Each member-declaration shall declare at least one member
11992 name of the class. */
11993 if (!decl_specifiers)
11996 pedwarn ("extra semicolon");
12002 /* See if this declaration is a friend. */
12003 friend_p = cp_parser_friend_p (decl_specifiers);
12004 /* If there were decl-specifiers, check to see if there was
12005 a class-declaration. */
12006 type = check_tag_decl (decl_specifiers);
12007 /* Nested classes have already been added to the class, but
12008 a `friend' needs to be explicitly registered. */
12011 /* If the `friend' keyword was present, the friend must
12012 be introduced with a class-key. */
12013 if (!declares_class_or_enum)
12014 error ("a class-key must be used when declaring a friend");
12017 template <typename T> struct A {
12018 friend struct A<T>::B;
12021 A<T>::B will be represented by a TYPENAME_TYPE, and
12022 therefore not recognized by check_tag_decl. */
12027 for (specifier = decl_specifiers;
12029 specifier = TREE_CHAIN (specifier))
12031 tree s = TREE_VALUE (specifier);
12033 if (TREE_CODE (s) == IDENTIFIER_NODE
12034 && IDENTIFIER_GLOBAL_VALUE (s))
12035 type = IDENTIFIER_GLOBAL_VALUE (s);
12036 if (TREE_CODE (s) == TYPE_DECL)
12046 error ("friend declaration does not name a class or "
12049 make_friend_class (current_class_type, type);
12051 /* If there is no TYPE, an error message will already have
12055 /* An anonymous aggregate has to be handled specially; such
12056 a declaration really declares a data member (with a
12057 particular type), as opposed to a nested class. */
12058 else if (ANON_AGGR_TYPE_P (type))
12060 /* Remove constructors and such from TYPE, now that we
12061 know it is an anoymous aggregate. */
12062 fixup_anonymous_aggr (type);
12063 /* And make the corresponding data member. */
12064 decl = build_decl (FIELD_DECL, NULL_TREE, type);
12065 /* Add it to the class. */
12066 finish_member_declaration (decl);
12072 /* See if these declarations will be friends. */
12073 friend_p = cp_parser_friend_p (decl_specifiers);
12075 /* Keep going until we hit the `;' at the end of the
12077 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12079 tree attributes = NULL_TREE;
12080 tree first_attribute;
12082 /* Peek at the next token. */
12083 token = cp_lexer_peek_token (parser->lexer);
12085 /* Check for a bitfield declaration. */
12086 if (token->type == CPP_COLON
12087 || (token->type == CPP_NAME
12088 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
12094 /* Get the name of the bitfield. Note that we cannot just
12095 check TOKEN here because it may have been invalidated by
12096 the call to cp_lexer_peek_nth_token above. */
12097 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
12098 identifier = cp_parser_identifier (parser);
12100 identifier = NULL_TREE;
12102 /* Consume the `:' token. */
12103 cp_lexer_consume_token (parser->lexer);
12104 /* Get the width of the bitfield. */
12105 width = cp_parser_constant_expression (parser);
12107 /* Look for attributes that apply to the bitfield. */
12108 attributes = cp_parser_attributes_opt (parser);
12109 /* Remember which attributes are prefix attributes and
12111 first_attribute = attributes;
12112 /* Combine the attributes. */
12113 attributes = chainon (prefix_attributes, attributes);
12115 /* Create the bitfield declaration. */
12116 decl = grokbitfield (identifier,
12119 /* Apply the attributes. */
12120 cplus_decl_attributes (&decl, attributes, /*flags=*/0);
12126 tree asm_specification;
12127 bool ctor_dtor_or_conv_p;
12129 /* Parse the declarator. */
12131 = cp_parser_declarator (parser,
12132 /*abstract_p=*/false,
12133 &ctor_dtor_or_conv_p);
12135 /* If something went wrong parsing the declarator, make sure
12136 that we at least consume some tokens. */
12137 if (declarator == error_mark_node)
12139 /* Skip to the end of the statement. */
12140 cp_parser_skip_to_end_of_statement (parser);
12144 /* Look for an asm-specification. */
12145 asm_specification = cp_parser_asm_specification_opt (parser);
12146 /* Look for attributes that apply to the declaration. */
12147 attributes = cp_parser_attributes_opt (parser);
12148 /* Remember which attributes are prefix attributes and
12150 first_attribute = attributes;
12151 /* Combine the attributes. */
12152 attributes = chainon (prefix_attributes, attributes);
12154 /* If it's an `=', then we have a constant-initializer or a
12155 pure-specifier. It is not correct to parse the
12156 initializer before registering the member declaration
12157 since the member declaration should be in scope while
12158 its initializer is processed. However, the rest of the
12159 front end does not yet provide an interface that allows
12160 us to handle this correctly. */
12161 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12165 A pure-specifier shall be used only in the declaration of
12166 a virtual function.
12168 A member-declarator can contain a constant-initializer
12169 only if it declares a static member of integral or
12172 Therefore, if the DECLARATOR is for a function, we look
12173 for a pure-specifier; otherwise, we look for a
12174 constant-initializer. When we call `grokfield', it will
12175 perform more stringent semantics checks. */
12176 if (TREE_CODE (declarator) == CALL_EXPR)
12177 initializer = cp_parser_pure_specifier (parser);
12180 /* This declaration cannot be a function
12182 cp_parser_commit_to_tentative_parse (parser);
12183 /* Parse the initializer. */
12184 initializer = cp_parser_constant_initializer (parser);
12187 /* Otherwise, there is no initializer. */
12189 initializer = NULL_TREE;
12191 /* See if we are probably looking at a function
12192 definition. We are certainly not looking at at a
12193 member-declarator. Calling `grokfield' has
12194 side-effects, so we must not do it unless we are sure
12195 that we are looking at a member-declarator. */
12196 if (cp_parser_token_starts_function_definition_p
12197 (cp_lexer_peek_token (parser->lexer)))
12198 decl = error_mark_node;
12200 /* Create the declaration. */
12201 decl = grokfield (declarator,
12208 /* Reset PREFIX_ATTRIBUTES. */
12209 while (attributes && TREE_CHAIN (attributes) != first_attribute)
12210 attributes = TREE_CHAIN (attributes);
12212 TREE_CHAIN (attributes) = NULL_TREE;
12214 /* If there is any qualification still in effect, clear it
12215 now; we will be starting fresh with the next declarator. */
12216 parser->scope = NULL_TREE;
12217 parser->qualifying_scope = NULL_TREE;
12218 parser->object_scope = NULL_TREE;
12219 /* If it's a `,', then there are more declarators. */
12220 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
12221 cp_lexer_consume_token (parser->lexer);
12222 /* If the next token isn't a `;', then we have a parse error. */
12223 else if (cp_lexer_next_token_is_not (parser->lexer,
12226 cp_parser_error (parser, "expected `;'");
12227 /* Skip tokens until we find a `;' */
12228 cp_parser_skip_to_end_of_statement (parser);
12235 /* Add DECL to the list of members. */
12237 finish_member_declaration (decl);
12239 /* If DECL is a function, we must return
12240 to parse it later. (Even though there is no definition,
12241 there might be default arguments that need handling.) */
12242 if (TREE_CODE (decl) == FUNCTION_DECL)
12243 TREE_VALUE (parser->unparsed_functions_queues)
12244 = tree_cons (current_class_type, decl,
12245 TREE_VALUE (parser->unparsed_functions_queues));
12250 /* If everything went well, look for the `;'. */
12251 if (cp_parser_parse_definitely (parser))
12253 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
12257 /* Parse the function-definition. */
12258 decl = cp_parser_function_definition (parser, &friend_p);
12259 /* If the member was not a friend, declare it here. */
12261 finish_member_declaration (decl);
12262 /* Peek at the next token. */
12263 token = cp_lexer_peek_token (parser->lexer);
12264 /* If the next token is a semicolon, consume it. */
12265 if (token->type == CPP_SEMICOLON)
12266 cp_lexer_consume_token (parser->lexer);
12269 /* Parse a pure-specifier.
12274 Returns INTEGER_ZERO_NODE if a pure specifier is found.
12275 Otherwiser, ERROR_MARK_NODE is returned. */
12278 cp_parser_pure_specifier (parser)
12283 /* Look for the `=' token. */
12284 if (!cp_parser_require (parser, CPP_EQ, "`='"))
12285 return error_mark_node;
12286 /* Look for the `0' token. */
12287 token = cp_parser_require (parser, CPP_NUMBER, "`0'");
12288 /* Unfortunately, this will accept `0L' and `0x00' as well. We need
12289 to get information from the lexer about how the number was
12290 spelled in order to fix this problem. */
12291 if (!token || !integer_zerop (token->value))
12292 return error_mark_node;
12294 return integer_zero_node;
12297 /* Parse a constant-initializer.
12299 constant-initializer:
12300 = constant-expression
12302 Returns a representation of the constant-expression. */
12305 cp_parser_constant_initializer (parser)
12308 /* Look for the `=' token. */
12309 if (!cp_parser_require (parser, CPP_EQ, "`='"))
12310 return error_mark_node;
12312 /* It is invalid to write:
12314 struct S { static const int i = { 7 }; };
12317 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12319 cp_parser_error (parser,
12320 "a brace-enclosed initializer is not allowed here");
12321 /* Consume the opening brace. */
12322 cp_lexer_consume_token (parser->lexer);
12323 /* Skip the initializer. */
12324 cp_parser_skip_to_closing_brace (parser);
12325 /* Look for the trailing `}'. */
12326 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
12328 return error_mark_node;
12331 return cp_parser_constant_expression (parser);
12334 /* Derived classes [gram.class.derived] */
12336 /* Parse a base-clause.
12339 : base-specifier-list
12341 base-specifier-list:
12343 base-specifier-list , base-specifier
12345 Returns a TREE_LIST representing the base-classes, in the order in
12346 which they were declared. The representation of each node is as
12347 described by cp_parser_base_specifier.
12349 In the case that no bases are specified, this function will return
12350 NULL_TREE, not ERROR_MARK_NODE. */
12353 cp_parser_base_clause (parser)
12356 tree bases = NULL_TREE;
12358 /* Look for the `:' that begins the list. */
12359 cp_parser_require (parser, CPP_COLON, "`:'");
12361 /* Scan the base-specifier-list. */
12367 /* Look for the base-specifier. */
12368 base = cp_parser_base_specifier (parser);
12369 /* Add BASE to the front of the list. */
12370 if (base != error_mark_node)
12372 TREE_CHAIN (base) = bases;
12375 /* Peek at the next token. */
12376 token = cp_lexer_peek_token (parser->lexer);
12377 /* If it's not a comma, then the list is complete. */
12378 if (token->type != CPP_COMMA)
12380 /* Consume the `,'. */
12381 cp_lexer_consume_token (parser->lexer);
12384 /* PARSER->SCOPE may still be non-NULL at this point, if the last
12385 base class had a qualified name. However, the next name that
12386 appears is certainly not qualified. */
12387 parser->scope = NULL_TREE;
12388 parser->qualifying_scope = NULL_TREE;
12389 parser->object_scope = NULL_TREE;
12391 return nreverse (bases);
12394 /* Parse a base-specifier.
12397 :: [opt] nested-name-specifier [opt] class-name
12398 virtual access-specifier [opt] :: [opt] nested-name-specifier
12400 access-specifier virtual [opt] :: [opt] nested-name-specifier
12403 Returns a TREE_LIST. The TREE_PURPOSE will be one of
12404 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
12405 indicate the specifiers provided. The TREE_VALUE will be a TYPE
12406 (or the ERROR_MARK_NODE) indicating the type that was specified. */
12409 cp_parser_base_specifier (parser)
12414 bool virtual_p = false;
12415 bool duplicate_virtual_error_issued_p = false;
12416 bool duplicate_access_error_issued_p = false;
12417 bool class_scope_p;
12418 access_kind access = ak_none;
12422 /* Process the optional `virtual' and `access-specifier'. */
12425 /* Peek at the next token. */
12426 token = cp_lexer_peek_token (parser->lexer);
12427 /* Process `virtual'. */
12428 switch (token->keyword)
12431 /* If `virtual' appears more than once, issue an error. */
12432 if (virtual_p && !duplicate_virtual_error_issued_p)
12434 cp_parser_error (parser,
12435 "`virtual' specified more than once in base-specified");
12436 duplicate_virtual_error_issued_p = true;
12441 /* Consume the `virtual' token. */
12442 cp_lexer_consume_token (parser->lexer);
12447 case RID_PROTECTED:
12449 /* If more than one access specifier appears, issue an
12451 if (access != ak_none && !duplicate_access_error_issued_p)
12453 cp_parser_error (parser,
12454 "more than one access specifier in base-specified");
12455 duplicate_access_error_issued_p = true;
12458 access = ((access_kind)
12459 tree_low_cst (ridpointers[(int) token->keyword],
12462 /* Consume the access-specifier. */
12463 cp_lexer_consume_token (parser->lexer);
12473 /* Map `virtual_p' and `access' onto one of the access
12479 access_node = access_default_node;
12482 access_node = access_public_node;
12485 access_node = access_protected_node;
12488 access_node = access_private_node;
12497 access_node = access_default_virtual_node;
12500 access_node = access_public_virtual_node;
12503 access_node = access_protected_virtual_node;
12506 access_node = access_private_virtual_node;
12512 /* Look for the optional `::' operator. */
12513 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
12514 /* Look for the nested-name-specifier. The simplest way to
12519 The keyword `typename' is not permitted in a base-specifier or
12520 mem-initializer; in these contexts a qualified name that
12521 depends on a template-parameter is implicitly assumed to be a
12524 is to pretend that we have seen the `typename' keyword at this
12526 cp_parser_nested_name_specifier_opt (parser,
12527 /*typename_keyword_p=*/true,
12528 /*check_dependency_p=*/true,
12530 /* If the base class is given by a qualified name, assume that names
12531 we see are type names or templates, as appropriate. */
12532 class_scope_p = (parser->scope && TYPE_P (parser->scope));
12533 /* Finally, look for the class-name. */
12534 type = cp_parser_class_name (parser,
12538 /*check_access=*/true,
12539 /*check_dependency_p=*/true,
12540 /*class_head_p=*/false);
12542 if (type == error_mark_node)
12543 return error_mark_node;
12545 return finish_base_specifier (access_node, TREE_TYPE (type));
12548 /* Exception handling [gram.exception] */
12550 /* Parse an (optional) exception-specification.
12552 exception-specification:
12553 throw ( type-id-list [opt] )
12555 Returns a TREE_LIST representing the exception-specification. The
12556 TREE_VALUE of each node is a type. */
12559 cp_parser_exception_specification_opt (parser)
12565 /* Peek at the next token. */
12566 token = cp_lexer_peek_token (parser->lexer);
12567 /* If it's not `throw', then there's no exception-specification. */
12568 if (!cp_parser_is_keyword (token, RID_THROW))
12571 /* Consume the `throw'. */
12572 cp_lexer_consume_token (parser->lexer);
12574 /* Look for the `('. */
12575 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
12577 /* Peek at the next token. */
12578 token = cp_lexer_peek_token (parser->lexer);
12579 /* If it's not a `)', then there is a type-id-list. */
12580 if (token->type != CPP_CLOSE_PAREN)
12582 const char *saved_message;
12584 /* Types may not be defined in an exception-specification. */
12585 saved_message = parser->type_definition_forbidden_message;
12586 parser->type_definition_forbidden_message
12587 = "types may not be defined in an exception-specification";
12588 /* Parse the type-id-list. */
12589 type_id_list = cp_parser_type_id_list (parser);
12590 /* Restore the saved message. */
12591 parser->type_definition_forbidden_message = saved_message;
12594 type_id_list = empty_except_spec;
12596 /* Look for the `)'. */
12597 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
12599 return type_id_list;
12602 /* Parse an (optional) type-id-list.
12606 type-id-list , type-id
12608 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
12609 in the order that the types were presented. */
12612 cp_parser_type_id_list (parser)
12615 tree types = NULL_TREE;
12622 /* Get the next type-id. */
12623 type = cp_parser_type_id (parser);
12624 /* Add it to the list. */
12625 types = add_exception_specifier (types, type, /*complain=*/1);
12626 /* Peek at the next token. */
12627 token = cp_lexer_peek_token (parser->lexer);
12628 /* If it is not a `,', we are done. */
12629 if (token->type != CPP_COMMA)
12631 /* Consume the `,'. */
12632 cp_lexer_consume_token (parser->lexer);
12635 return nreverse (types);
12638 /* Parse a try-block.
12641 try compound-statement handler-seq */
12644 cp_parser_try_block (parser)
12649 cp_parser_require_keyword (parser, RID_TRY, "`try'");
12650 try_block = begin_try_block ();
12651 cp_parser_compound_statement (parser);
12652 finish_try_block (try_block);
12653 cp_parser_handler_seq (parser);
12654 finish_handler_sequence (try_block);
12659 /* Parse a function-try-block.
12661 function-try-block:
12662 try ctor-initializer [opt] function-body handler-seq */
12665 cp_parser_function_try_block (parser)
12669 bool ctor_initializer_p;
12671 /* Look for the `try' keyword. */
12672 if (!cp_parser_require_keyword (parser, RID_TRY, "`try'"))
12674 /* Let the rest of the front-end know where we are. */
12675 try_block = begin_function_try_block ();
12676 /* Parse the function-body. */
12678 = cp_parser_ctor_initializer_opt_and_function_body (parser);
12679 /* We're done with the `try' part. */
12680 finish_function_try_block (try_block);
12681 /* Parse the handlers. */
12682 cp_parser_handler_seq (parser);
12683 /* We're done with the handlers. */
12684 finish_function_handler_sequence (try_block);
12686 return ctor_initializer_p;
12689 /* Parse a handler-seq.
12692 handler handler-seq [opt] */
12695 cp_parser_handler_seq (parser)
12702 /* Parse the handler. */
12703 cp_parser_handler (parser);
12704 /* Peek at the next token. */
12705 token = cp_lexer_peek_token (parser->lexer);
12706 /* If it's not `catch' then there are no more handlers. */
12707 if (!cp_parser_is_keyword (token, RID_CATCH))
12712 /* Parse a handler.
12715 catch ( exception-declaration ) compound-statement */
12718 cp_parser_handler (parser)
12724 cp_parser_require_keyword (parser, RID_CATCH, "`catch'");
12725 handler = begin_handler ();
12726 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
12727 declaration = cp_parser_exception_declaration (parser);
12728 finish_handler_parms (declaration, handler);
12729 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
12730 cp_parser_compound_statement (parser);
12731 finish_handler (handler);
12734 /* Parse an exception-declaration.
12736 exception-declaration:
12737 type-specifier-seq declarator
12738 type-specifier-seq abstract-declarator
12742 Returns a VAR_DECL for the declaration, or NULL_TREE if the
12743 ellipsis variant is used. */
12746 cp_parser_exception_declaration (parser)
12749 tree type_specifiers;
12751 const char *saved_message;
12753 /* If it's an ellipsis, it's easy to handle. */
12754 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
12756 /* Consume the `...' token. */
12757 cp_lexer_consume_token (parser->lexer);
12761 /* Types may not be defined in exception-declarations. */
12762 saved_message = parser->type_definition_forbidden_message;
12763 parser->type_definition_forbidden_message
12764 = "types may not be defined in exception-declarations";
12766 /* Parse the type-specifier-seq. */
12767 type_specifiers = cp_parser_type_specifier_seq (parser);
12768 /* If it's a `)', then there is no declarator. */
12769 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
12770 declarator = NULL_TREE;
12773 /* Otherwise, we can't be sure whether we are looking at a
12774 direct, or an abstract, declarator. */
12775 cp_parser_parse_tentatively (parser);
12776 /* Try an ordinary declarator. */
12777 declarator = cp_parser_declarator (parser,
12778 /*abstract_p=*/false,
12779 /*ctor_dtor_or_conv_p=*/NULL);
12780 /* If that didn't work, try an abstract declarator. */
12781 if (!cp_parser_parse_definitely (parser))
12782 declarator = cp_parser_declarator (parser,
12783 /*abstract_p=*/true,
12784 /*ctor_dtor_or_conv_p=*/NULL);
12787 /* Restore the saved message. */
12788 parser->type_definition_forbidden_message = saved_message;
12790 return start_handler_parms (type_specifiers, declarator);
12793 /* Parse a throw-expression.
12796 throw assignment-expresion [opt]
12798 Returns a THROW_EXPR representing the throw-expression. */
12801 cp_parser_throw_expression (parser)
12806 cp_parser_require_keyword (parser, RID_THROW, "`throw'");
12807 /* We can't be sure if there is an assignment-expression or not. */
12808 cp_parser_parse_tentatively (parser);
12810 expression = cp_parser_assignment_expression (parser);
12811 /* If it didn't work, this is just a rethrow. */
12812 if (!cp_parser_parse_definitely (parser))
12813 expression = NULL_TREE;
12815 return build_throw (expression);
12818 /* GNU Extensions */
12820 /* Parse an (optional) asm-specification.
12823 asm ( string-literal )
12825 If the asm-specification is present, returns a STRING_CST
12826 corresponding to the string-literal. Otherwise, returns
12830 cp_parser_asm_specification_opt (parser)
12834 tree asm_specification;
12836 /* Peek at the next token. */
12837 token = cp_lexer_peek_token (parser->lexer);
12838 /* If the next token isn't the `asm' keyword, then there's no
12839 asm-specification. */
12840 if (!cp_parser_is_keyword (token, RID_ASM))
12843 /* Consume the `asm' token. */
12844 cp_lexer_consume_token (parser->lexer);
12845 /* Look for the `('. */
12846 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
12848 /* Look for the string-literal. */
12849 token = cp_parser_require (parser, CPP_STRING, "string-literal");
12851 asm_specification = token->value;
12853 asm_specification = NULL_TREE;
12855 /* Look for the `)'. */
12856 cp_parser_require (parser, CPP_CLOSE_PAREN, "`('");
12858 return asm_specification;
12861 /* Parse an asm-operand-list.
12865 asm-operand-list , asm-operand
12868 string-literal ( expression )
12869 [ string-literal ] string-literal ( expression )
12871 Returns a TREE_LIST representing the operands. The TREE_VALUE of
12872 each node is the expression. The TREE_PURPOSE is itself a
12873 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
12874 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
12875 is a STRING_CST for the string literal before the parenthesis. */
12878 cp_parser_asm_operand_list (parser)
12881 tree asm_operands = NULL_TREE;
12885 tree string_literal;
12890 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
12892 /* Consume the `[' token. */
12893 cp_lexer_consume_token (parser->lexer);
12894 /* Read the operand name. */
12895 name = cp_parser_identifier (parser);
12896 if (name != error_mark_node)
12897 name = build_string (IDENTIFIER_LENGTH (name),
12898 IDENTIFIER_POINTER (name));
12899 /* Look for the closing `]'. */
12900 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
12904 /* Look for the string-literal. */
12905 token = cp_parser_require (parser, CPP_STRING, "string-literal");
12906 string_literal = token ? token->value : error_mark_node;
12907 /* Look for the `('. */
12908 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
12909 /* Parse the expression. */
12910 expression = cp_parser_expression (parser);
12911 /* Look for the `)'. */
12912 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
12913 /* Add this operand to the list. */
12914 asm_operands = tree_cons (build_tree_list (name, string_literal),
12917 /* If the next token is not a `,', there are no more
12919 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12921 /* Consume the `,'. */
12922 cp_lexer_consume_token (parser->lexer);
12925 return nreverse (asm_operands);
12928 /* Parse an asm-clobber-list.
12932 asm-clobber-list , string-literal
12934 Returns a TREE_LIST, indicating the clobbers in the order that they
12935 appeared. The TREE_VALUE of each node is a STRING_CST. */
12938 cp_parser_asm_clobber_list (parser)
12941 tree clobbers = NULL_TREE;
12946 tree string_literal;
12948 /* Look for the string literal. */
12949 token = cp_parser_require (parser, CPP_STRING, "string-literal");
12950 string_literal = token ? token->value : error_mark_node;
12951 /* Add it to the list. */
12952 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
12953 /* If the next token is not a `,', then the list is
12955 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12957 /* Consume the `,' token. */
12958 cp_lexer_consume_token (parser->lexer);
12964 /* Parse an (optional) series of attributes.
12967 attributes attribute
12970 __attribute__ (( attribute-list [opt] ))
12972 The return value is as for cp_parser_attribute_list. */
12975 cp_parser_attributes_opt (parser)
12978 tree attributes = NULL_TREE;
12983 tree attribute_list;
12985 /* Peek at the next token. */
12986 token = cp_lexer_peek_token (parser->lexer);
12987 /* If it's not `__attribute__', then we're done. */
12988 if (token->keyword != RID_ATTRIBUTE)
12991 /* Consume the `__attribute__' keyword. */
12992 cp_lexer_consume_token (parser->lexer);
12993 /* Look for the two `(' tokens. */
12994 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
12995 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
12997 /* Peek at the next token. */
12998 token = cp_lexer_peek_token (parser->lexer);
12999 if (token->type != CPP_CLOSE_PAREN)
13000 /* Parse the attribute-list. */
13001 attribute_list = cp_parser_attribute_list (parser);
13003 /* If the next token is a `)', then there is no attribute
13005 attribute_list = NULL;
13007 /* Look for the two `)' tokens. */
13008 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13009 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13011 /* Add these new attributes to the list. */
13012 attributes = chainon (attributes, attribute_list);
13018 /* Parse an attribute-list.
13022 attribute-list , attribute
13026 identifier ( identifier )
13027 identifier ( identifier , expression-list )
13028 identifier ( expression-list )
13030 Returns a TREE_LIST. Each node corresponds to an attribute. THe
13031 TREE_PURPOSE of each node is the identifier indicating which
13032 attribute is in use. The TREE_VALUE represents the arguments, if
13036 cp_parser_attribute_list (parser)
13039 tree attribute_list = NULL_TREE;
13047 /* Look for the identifier. We also allow keywords here; for
13048 example `__attribute__ ((const))' is legal. */
13049 token = cp_lexer_peek_token (parser->lexer);
13050 if (token->type != CPP_NAME
13051 && token->type != CPP_KEYWORD)
13052 return error_mark_node;
13053 /* Consume the token. */
13054 token = cp_lexer_consume_token (parser->lexer);
13056 /* Save away the identifier that indicates which attribute this is. */
13057 identifier = token->value;
13058 attribute = build_tree_list (identifier, NULL_TREE);
13060 /* Peek at the next token. */
13061 token = cp_lexer_peek_token (parser->lexer);
13062 /* If it's an `(', then parse the attribute arguments. */
13063 if (token->type == CPP_OPEN_PAREN)
13066 int arguments_allowed_p = 1;
13068 /* Consume the `('. */
13069 cp_lexer_consume_token (parser->lexer);
13070 /* Peek at the next token. */
13071 token = cp_lexer_peek_token (parser->lexer);
13072 /* Check to see if the next token is an identifier. */
13073 if (token->type == CPP_NAME)
13075 /* Save the identifier. */
13076 identifier = token->value;
13077 /* Consume the identifier. */
13078 cp_lexer_consume_token (parser->lexer);
13079 /* Peek at the next token. */
13080 token = cp_lexer_peek_token (parser->lexer);
13081 /* If the next token is a `,', then there are some other
13082 expressions as well. */
13083 if (token->type == CPP_COMMA)
13084 /* Consume the comma. */
13085 cp_lexer_consume_token (parser->lexer);
13087 arguments_allowed_p = 0;
13090 identifier = NULL_TREE;
13092 /* If there are arguments, parse them too. */
13093 if (arguments_allowed_p)
13094 arguments = cp_parser_expression_list (parser);
13096 arguments = NULL_TREE;
13098 /* Combine the identifier and the arguments. */
13100 arguments = tree_cons (NULL_TREE, identifier, arguments);
13102 /* Save the identifier and arguments away. */
13103 TREE_VALUE (attribute) = arguments;
13105 /* Look for the closing `)'. */
13106 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13109 /* Add this attribute to the list. */
13110 TREE_CHAIN (attribute) = attribute_list;
13111 attribute_list = attribute;
13113 /* Now, look for more attributes. */
13114 token = cp_lexer_peek_token (parser->lexer);
13115 /* If the next token isn't a `,', we're done. */
13116 if (token->type != CPP_COMMA)
13119 /* Consume the commma and keep going. */
13120 cp_lexer_consume_token (parser->lexer);
13123 /* We built up the list in reverse order. */
13124 return nreverse (attribute_list);
13127 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
13128 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
13129 current value of the PEDANTIC flag, regardless of whether or not
13130 the `__extension__' keyword is present. The caller is responsible
13131 for restoring the value of the PEDANTIC flag. */
13134 cp_parser_extension_opt (parser, saved_pedantic)
13136 int *saved_pedantic;
13138 /* Save the old value of the PEDANTIC flag. */
13139 *saved_pedantic = pedantic;
13141 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
13143 /* Consume the `__extension__' token. */
13144 cp_lexer_consume_token (parser->lexer);
13145 /* We're not being pedantic while the `__extension__' keyword is
13155 /* Parse a label declaration.
13158 __label__ label-declarator-seq ;
13160 label-declarator-seq:
13161 identifier , label-declarator-seq
13165 cp_parser_label_declaration (parser)
13168 /* Look for the `__label__' keyword. */
13169 cp_parser_require_keyword (parser, RID_LABEL, "`__label__'");
13175 /* Look for an identifier. */
13176 identifier = cp_parser_identifier (parser);
13177 /* Declare it as a lobel. */
13178 finish_label_decl (identifier);
13179 /* If the next token is a `;', stop. */
13180 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13182 /* Look for the `,' separating the label declarations. */
13183 cp_parser_require (parser, CPP_COMMA, "`,'");
13186 /* Look for the final `;'. */
13187 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
13190 /* Support Functions */
13192 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
13193 NAME should have one of the representations used for an
13194 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
13195 is returned. If PARSER->SCOPE is a dependent type, then a
13196 SCOPE_REF is returned.
13198 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
13199 returned; the name was already resolved when the TEMPLATE_ID_EXPR
13200 was formed. Abstractly, such entities should not be passed to this
13201 function, because they do not need to be looked up, but it is
13202 simpler to check for this special case here, rather than at the
13205 In cases not explicitly covered above, this function returns a
13206 DECL, OVERLOAD, or baselink representing the result of the lookup.
13207 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
13210 If CHECK_ACCESS is TRUE, then access control is performed on the
13211 declaration to which the name resolves, and an error message is
13212 issued if the declaration is inaccessible.
13214 If IS_TYPE is TRUE, bindings that do not refer to types are
13217 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
13221 cp_parser_lookup_name (parser, name, check_access, is_type,
13227 bool check_dependency;
13230 tree object_type = parser->context->object_type;
13232 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
13233 no longer valid. Note that if we are parsing tentatively, and
13234 the parse fails, OBJECT_TYPE will be automatically restored. */
13235 parser->context->object_type = NULL_TREE;
13237 if (name == error_mark_node)
13238 return error_mark_node;
13240 /* A template-id has already been resolved; there is no lookup to
13242 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
13244 if (BASELINK_P (name))
13246 my_friendly_assert ((TREE_CODE (BASELINK_FUNCTIONS (name))
13247 == TEMPLATE_ID_EXPR),
13252 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
13253 it should already have been checked to make sure that the name
13254 used matches the type being destroyed. */
13255 if (TREE_CODE (name) == BIT_NOT_EXPR)
13259 /* Figure out to which type this destructor applies. */
13261 type = parser->scope;
13262 else if (object_type)
13263 type = object_type;
13265 type = current_class_type;
13266 /* If that's not a class type, there is no destructor. */
13267 if (!type || !CLASS_TYPE_P (type))
13268 return error_mark_node;
13269 /* If it was a class type, return the destructor. */
13270 return CLASSTYPE_DESTRUCTORS (type);
13273 /* By this point, the NAME should be an ordinary identifier. If
13274 the id-expression was a qualified name, the qualifying scope is
13275 stored in PARSER->SCOPE at this point. */
13276 my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE,
13279 /* Perform the lookup. */
13282 bool dependent_type_p;
13284 if (parser->scope == error_mark_node)
13285 return error_mark_node;
13287 /* If the SCOPE is dependent, the lookup must be deferred until
13288 the template is instantiated -- unless we are explicitly
13289 looking up names in uninstantiated templates. Even then, we
13290 cannot look up the name if the scope is not a class type; it
13291 might, for example, be a template type parameter. */
13292 dependent_type_p = (TYPE_P (parser->scope)
13293 && !(parser->in_declarator_p
13294 && currently_open_class (parser->scope))
13295 && cp_parser_dependent_type_p (parser->scope));
13296 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
13297 && dependent_type_p)
13300 decl = build_nt (SCOPE_REF, parser->scope, name);
13302 /* The resolution to Core Issue 180 says that `struct A::B'
13303 should be considered a type-name, even if `A' is
13305 decl = TYPE_NAME (make_typename_type (parser->scope,
13311 /* If PARSER->SCOPE is a dependent type, then it must be a
13312 class type, and we must not be checking dependencies;
13313 otherwise, we would have processed this lookup above. So
13314 that PARSER->SCOPE is not considered a dependent base by
13315 lookup_member, we must enter the scope here. */
13316 if (dependent_type_p)
13317 push_scope (parser->scope);
13318 /* If the PARSER->SCOPE is a a template specialization, it
13319 may be instantiated during name lookup. In that case,
13320 errors may be issued. Even if we rollback the current
13321 tentative parse, those errors are valid. */
13322 decl = lookup_qualified_name (parser->scope, name, is_type,
13324 if (dependent_type_p)
13325 pop_scope (parser->scope);
13327 parser->qualifying_scope = parser->scope;
13328 parser->object_scope = NULL_TREE;
13330 else if (object_type)
13332 tree object_decl = NULL_TREE;
13333 /* Look up the name in the scope of the OBJECT_TYPE, unless the
13334 OBJECT_TYPE is not a class. */
13335 if (CLASS_TYPE_P (object_type))
13336 /* If the OBJECT_TYPE is a template specialization, it may
13337 be instantiated during name lookup. In that case, errors
13338 may be issued. Even if we rollback the current tentative
13339 parse, those errors are valid. */
13340 object_decl = lookup_member (object_type,
13342 /*protect=*/0, is_type);
13343 /* Look it up in the enclosing context, too. */
13344 decl = lookup_name_real (name, is_type, /*nonclass=*/0,
13345 /*namespaces_only=*/0,
13347 parser->object_scope = object_type;
13348 parser->qualifying_scope = NULL_TREE;
13350 decl = object_decl;
13354 decl = lookup_name_real (name, is_type, /*nonclass=*/0,
13355 /*namespaces_only=*/0,
13357 parser->qualifying_scope = NULL_TREE;
13358 parser->object_scope = NULL_TREE;
13361 /* If the lookup failed, let our caller know. */
13363 || decl == error_mark_node
13364 || (TREE_CODE (decl) == FUNCTION_DECL
13365 && DECL_ANTICIPATED (decl)))
13366 return error_mark_node;
13368 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
13369 if (TREE_CODE (decl) == TREE_LIST)
13371 /* The error message we have to print is too complicated for
13372 cp_parser_error, so we incorporate its actions directly. */
13373 cp_parser_simulate_error (parser);
13374 if (!cp_parser_parsing_tentatively (parser)
13375 || cp_parser_committed_to_tentative_parse (parser))
13377 error ("reference to `%D' is ambiguous", name);
13378 print_candidates (decl);
13380 return error_mark_node;
13383 my_friendly_assert (DECL_P (decl)
13384 || TREE_CODE (decl) == OVERLOAD
13385 || TREE_CODE (decl) == SCOPE_REF
13386 || BASELINK_P (decl),
13389 /* If we have resolved the name of a member declaration, check to
13390 see if the declaration is accessible. When the name resolves to
13391 set of overloaded functions, accesibility is checked when
13392 overload resolution is done.
13394 During an explicit instantiation, access is not checked at all,
13395 as per [temp.explicit]. */
13396 if (check_access && scope_chain->check_access && DECL_P (decl))
13398 tree qualifying_type;
13400 /* Figure out the type through which DECL is being
13403 = cp_parser_scope_through_which_access_occurs (decl,
13406 if (qualifying_type)
13408 /* If we are supposed to defer access checks, just record
13409 the information for later. */
13410 if (parser->context->deferring_access_checks_p)
13411 cp_parser_defer_access_check (parser, qualifying_type, decl);
13412 /* Otherwise, check accessibility now. */
13414 enforce_access (qualifying_type, decl);
13421 /* Like cp_parser_lookup_name, but for use in the typical case where
13422 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, and CHECK_DEPENDENCY is
13426 cp_parser_lookup_name_simple (parser, name)
13430 return cp_parser_lookup_name (parser, name,
13431 /*check_access=*/true,
13433 /*check_dependency=*/true);
13436 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
13437 TYPENAME_TYPE corresponds. Note that this function peers inside
13438 uninstantiated templates and therefore should be used only in
13439 extremely limited situations. */
13442 cp_parser_resolve_typename_type (parser, type)
13450 my_friendly_assert (TREE_CODE (type) == TYPENAME_TYPE,
13453 scope = TYPE_CONTEXT (type);
13454 name = DECL_NAME (TYPE_NAME (type));
13456 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
13457 it first before we can figure out what NAME refers to. */
13458 if (TREE_CODE (scope) == TYPENAME_TYPE)
13459 scope = cp_parser_resolve_typename_type (parser, scope);
13460 /* If we don't know what SCOPE refers to, then we cannot resolve the
13462 if (scope == error_mark_node)
13463 return error_mark_node;
13464 /* If the SCOPE is a template type parameter, we have no way of
13465 resolving the name. */
13466 if (TREE_CODE (scope) == TEMPLATE_TYPE_PARM)
13468 /* Enter the SCOPE so that name lookup will be resolved as if we
13469 were in the class definition. In particular, SCOPE will no
13470 longer be considered a dependent type. */
13471 push_scope (scope);
13472 /* Look up the declaration. */
13473 decl = lookup_member (scope, name, /*protect=*/0, /*want_type=*/1);
13474 /* If all went well, we got a TYPE_DECL for a non-typename. */
13476 || TREE_CODE (decl) != TYPE_DECL
13477 || TREE_CODE (TREE_TYPE (decl)) == TYPENAME_TYPE)
13479 cp_parser_error (parser, "could not resolve typename type");
13480 type = error_mark_node;
13483 type = TREE_TYPE (decl);
13484 /* Leave the SCOPE. */
13490 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
13491 the current context, return the TYPE_DECL. If TAG_NAME_P is
13492 true, the DECL indicates the class being defined in a class-head,
13493 or declared in an elaborated-type-specifier.
13495 Otherwise, return DECL. */
13498 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
13500 /* If the DECL is a TEMPLATE_DECL for a class type, and we are in
13501 the scope of the class, then treat the TEMPLATE_DECL as a
13502 class-name. For example, in:
13504 template <class T> struct S {
13510 If the TEMPLATE_DECL is being declared as part of a class-head,
13511 the same translation occurs:
13514 template <typename T> struct B;
13517 template <typename T> struct A::B {};
13519 Similarly, in a elaborated-type-specifier:
13521 namespace N { struct X{}; }
13524 template <typename T> friend struct N::X;
13528 if (DECL_CLASS_TEMPLATE_P (decl)
13530 || (current_class_type
13531 && same_type_p (TREE_TYPE (DECL_TEMPLATE_RESULT (decl)),
13532 current_class_type))))
13533 return DECL_TEMPLATE_RESULT (decl);
13538 /* If too many, or too few, template-parameter lists apply to the
13539 declarator, issue an error message. Returns TRUE if all went well,
13540 and FALSE otherwise. */
13543 cp_parser_check_declarator_template_parameters (parser, declarator)
13547 unsigned num_templates;
13549 /* We haven't seen any classes that involve template parameters yet. */
13552 switch (TREE_CODE (declarator))
13559 tree main_declarator = TREE_OPERAND (declarator, 0);
13561 cp_parser_check_declarator_template_parameters (parser,
13570 scope = TREE_OPERAND (declarator, 0);
13571 member = TREE_OPERAND (declarator, 1);
13573 /* If this is a pointer-to-member, then we are not interested
13574 in the SCOPE, because it does not qualify the thing that is
13576 if (TREE_CODE (member) == INDIRECT_REF)
13577 return (cp_parser_check_declarator_template_parameters
13580 while (scope && CLASS_TYPE_P (scope))
13582 /* You're supposed to have one `template <...>'
13583 for every template class, but you don't need one
13584 for a full specialization. For example:
13586 template <class T> struct S{};
13587 template <> struct S<int> { void f(); };
13588 void S<int>::f () {}
13590 is correct; there shouldn't be a `template <>' for
13591 the definition of `S<int>::f'. */
13592 if (CLASSTYPE_TEMPLATE_INFO (scope)
13593 && (CLASSTYPE_TEMPLATE_INSTANTIATION (scope)
13594 || uses_template_parms (CLASSTYPE_TI_ARGS (scope)))
13595 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
13598 scope = TYPE_CONTEXT (scope);
13602 /* Fall through. */
13605 /* If the DECLARATOR has the form `X<y>' then it uses one
13606 additional level of template parameters. */
13607 if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
13610 return cp_parser_check_template_parameters (parser,
13615 /* NUM_TEMPLATES were used in the current declaration. If that is
13616 invalid, return FALSE and issue an error messages. Otherwise,
13620 cp_parser_check_template_parameters (parser, num_templates)
13622 unsigned num_templates;
13624 /* If there are more template classes than parameter lists, we have
13627 template <class T> void S<T>::R<T>::f (); */
13628 if (parser->num_template_parameter_lists < num_templates)
13630 error ("too few template-parameter-lists");
13633 /* If there are the same number of template classes and parameter
13634 lists, that's OK. */
13635 if (parser->num_template_parameter_lists == num_templates)
13637 /* If there are more, but only one more, then we are referring to a
13638 member template. That's OK too. */
13639 if (parser->num_template_parameter_lists == num_templates + 1)
13641 /* Otherwise, there are too many template parameter lists. We have
13644 template <class T> template <class U> void S::f(); */
13645 error ("too many template-parameter-lists");
13649 /* Parse a binary-expression of the general form:
13653 binary-expression <token> <expr>
13655 The TOKEN_TREE_MAP maps <token> types to <expr> codes. FN is used
13656 to parser the <expr>s. If the first production is used, then the
13657 value returned by FN is returned directly. Otherwise, a node with
13658 the indicated EXPR_TYPE is returned, with operands corresponding to
13659 the two sub-expressions. */
13662 cp_parser_binary_expression (parser, token_tree_map, fn)
13664 cp_parser_token_tree_map token_tree_map;
13665 cp_parser_expression_fn fn;
13669 /* Parse the first expression. */
13670 lhs = (*fn) (parser);
13671 /* Now, look for more expressions. */
13675 cp_parser_token_tree_map_node *map_node;
13678 /* Peek at the next token. */
13679 token = cp_lexer_peek_token (parser->lexer);
13680 /* If the token is `>', and that's not an operator at the
13681 moment, then we're done. */
13682 if (token->type == CPP_GREATER
13683 && !parser->greater_than_is_operator_p)
13685 /* If we find one of the tokens we want, build the correspoding
13686 tree representation. */
13687 for (map_node = token_tree_map;
13688 map_node->token_type != CPP_EOF;
13690 if (map_node->token_type == token->type)
13692 /* Consume the operator token. */
13693 cp_lexer_consume_token (parser->lexer);
13694 /* Parse the right-hand side of the expression. */
13695 rhs = (*fn) (parser);
13696 /* Build the binary tree node. */
13697 lhs = build_x_binary_op (map_node->tree_type, lhs, rhs);
13701 /* If the token wasn't one of the ones we want, we're done. */
13702 if (map_node->token_type == CPP_EOF)
13709 /* Parse an optional `::' token indicating that the following name is
13710 from the global namespace. If so, PARSER->SCOPE is set to the
13711 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
13712 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
13713 Returns the new value of PARSER->SCOPE, if the `::' token is
13714 present, and NULL_TREE otherwise. */
13717 cp_parser_global_scope_opt (parser, current_scope_valid_p)
13719 bool current_scope_valid_p;
13723 /* Peek at the next token. */
13724 token = cp_lexer_peek_token (parser->lexer);
13725 /* If we're looking at a `::' token then we're starting from the
13726 global namespace, not our current location. */
13727 if (token->type == CPP_SCOPE)
13729 /* Consume the `::' token. */
13730 cp_lexer_consume_token (parser->lexer);
13731 /* Set the SCOPE so that we know where to start the lookup. */
13732 parser->scope = global_namespace;
13733 parser->qualifying_scope = global_namespace;
13734 parser->object_scope = NULL_TREE;
13736 return parser->scope;
13738 else if (!current_scope_valid_p)
13740 parser->scope = NULL_TREE;
13741 parser->qualifying_scope = NULL_TREE;
13742 parser->object_scope = NULL_TREE;
13748 /* Returns TRUE if the upcoming token sequence is the start of a
13749 constructor declarator. If FRIEND_P is true, the declarator is
13750 preceded by the `friend' specifier. */
13753 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
13755 bool constructor_p;
13756 tree type_decl = NULL_TREE;
13757 bool nested_name_p;
13759 /* Parse tentatively; we are going to roll back all of the tokens
13761 cp_parser_parse_tentatively (parser);
13762 /* Assume that we are looking at a constructor declarator. */
13763 constructor_p = true;
13764 /* Look for the optional `::' operator. */
13765 cp_parser_global_scope_opt (parser,
13766 /*current_scope_valid_p=*/false);
13767 /* Look for the nested-name-specifier. */
13769 = (cp_parser_nested_name_specifier_opt (parser,
13770 /*typename_keyword_p=*/false,
13771 /*check_dependency_p=*/false,
13774 /* Outside of a class-specifier, there must be a
13775 nested-name-specifier. */
13776 if (!nested_name_p &&
13777 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
13779 constructor_p = false;
13780 /* If we still think that this might be a constructor-declarator,
13781 look for a class-name. */
13786 template <typename T> struct S { S(); }
13787 template <typename T> S<T>::S ();
13789 we must recognize that the nested `S' names a class.
13792 template <typename T> S<T>::S<T> ();
13794 we must recognize that the nested `S' names a template. */
13795 type_decl = cp_parser_class_name (parser,
13796 /*typename_keyword_p=*/false,
13797 /*template_keyword_p=*/false,
13799 /*check_access_p=*/false,
13800 /*check_dependency_p=*/false,
13801 /*class_head_p=*/false);
13802 /* If there was no class-name, then this is not a constructor. */
13803 constructor_p = !cp_parser_error_occurred (parser);
13805 /* If we're still considering a constructor, we have to see a `(',
13806 to begin the parameter-declaration-clause, followed by either a
13807 `)', an `...', or a decl-specifier. We need to check for a
13808 type-specifier to avoid being fooled into thinking that:
13812 is a constructor. (It is actually a function named `f' that
13813 takes one parameter (of type `int') and returns a value of type
13816 && cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
13818 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
13819 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
13820 && !cp_parser_storage_class_specifier_opt (parser))
13822 if (current_class_type
13823 && !same_type_p (current_class_type, TREE_TYPE (type_decl)))
13824 /* The constructor for one class cannot be declared inside
13826 constructor_p = false;
13831 /* Names appearing in the type-specifier should be looked up
13832 in the scope of the class. */
13833 if (current_class_type)
13837 type = TREE_TYPE (type_decl);
13838 if (TREE_CODE (type) == TYPENAME_TYPE)
13839 type = cp_parser_resolve_typename_type (parser, type);
13842 /* Look for the type-specifier. */
13843 cp_parser_type_specifier (parser,
13844 CP_PARSER_FLAGS_NONE,
13845 /*is_friend=*/false,
13846 /*is_declarator=*/true,
13847 /*declares_class_or_enum=*/NULL,
13848 /*is_cv_qualifier=*/NULL);
13849 /* Leave the scope of the class. */
13853 constructor_p = !cp_parser_error_occurred (parser);
13858 constructor_p = false;
13859 /* We did not really want to consume any tokens. */
13860 cp_parser_abort_tentative_parse (parser);
13862 return constructor_p;
13865 /* Parse the definition of the function given by the DECL_SPECIFIERS,
13866 ATTRIBUTES, and DECLARATOR. The ACCESS_CHECKS have been deferred;
13867 they must be performed once we are in the scope of the function.
13869 Returns the function defined. */
13872 cp_parser_function_definition_from_specifiers_and_declarator
13873 (parser, decl_specifiers, attributes, declarator, access_checks)
13875 tree decl_specifiers;
13878 tree access_checks;
13883 /* Begin the function-definition. */
13884 success_p = begin_function_definition (decl_specifiers,
13888 /* If there were names looked up in the decl-specifier-seq that we
13889 did not check, check them now. We must wait until we are in the
13890 scope of the function to perform the checks, since the function
13891 might be a friend. */
13892 cp_parser_perform_deferred_access_checks (access_checks);
13896 /* If begin_function_definition didn't like the definition, skip
13897 the entire function. */
13898 error ("invalid function declaration");
13899 cp_parser_skip_to_end_of_block_or_statement (parser);
13900 fn = error_mark_node;
13903 fn = cp_parser_function_definition_after_declarator (parser,
13904 /*inline_p=*/false);
13909 /* Parse the part of a function-definition that follows the
13910 declarator. INLINE_P is TRUE iff this function is an inline
13911 function defined with a class-specifier.
13913 Returns the function defined. */
13916 cp_parser_function_definition_after_declarator (parser,
13922 bool ctor_initializer_p = false;
13923 bool saved_in_unbraced_linkage_specification_p;
13924 unsigned saved_num_template_parameter_lists;
13926 /* If the next token is `return', then the code may be trying to
13927 make use of the "named return value" extension that G++ used to
13929 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
13931 /* Consume the `return' keyword. */
13932 cp_lexer_consume_token (parser->lexer);
13933 /* Look for the identifier that indicates what value is to be
13935 cp_parser_identifier (parser);
13936 /* Issue an error message. */
13937 error ("named return values are no longer supported");
13938 /* Skip tokens until we reach the start of the function body. */
13939 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
13940 cp_lexer_consume_token (parser->lexer);
13942 /* The `extern' in `extern "C" void f () { ... }' does not apply to
13943 anything declared inside `f'. */
13944 saved_in_unbraced_linkage_specification_p
13945 = parser->in_unbraced_linkage_specification_p;
13946 parser->in_unbraced_linkage_specification_p = false;
13947 /* Inside the function, surrounding template-parameter-lists do not
13949 saved_num_template_parameter_lists
13950 = parser->num_template_parameter_lists;
13951 parser->num_template_parameter_lists = 0;
13952 /* If the next token is `try', then we are looking at a
13953 function-try-block. */
13954 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
13955 ctor_initializer_p = cp_parser_function_try_block (parser);
13956 /* A function-try-block includes the function-body, so we only do
13957 this next part if we're not processing a function-try-block. */
13960 = cp_parser_ctor_initializer_opt_and_function_body (parser);
13962 /* Finish the function. */
13963 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
13964 (inline_p ? 2 : 0));
13965 /* Generate code for it, if necessary. */
13967 /* Restore the saved values. */
13968 parser->in_unbraced_linkage_specification_p
13969 = saved_in_unbraced_linkage_specification_p;
13970 parser->num_template_parameter_lists
13971 = saved_num_template_parameter_lists;
13976 /* Parse a template-declaration, assuming that the `export' (and
13977 `extern') keywords, if present, has already been scanned. MEMBER_P
13978 is as for cp_parser_template_declaration. */
13981 cp_parser_template_declaration_after_export (parser, member_p)
13985 tree decl = NULL_TREE;
13986 tree parameter_list;
13987 bool friend_p = false;
13989 /* Look for the `template' keyword. */
13990 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'"))
13994 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
13997 /* Parse the template parameters. */
13998 begin_template_parm_list ();
13999 /* If the next token is `>', then we have an invalid
14000 specialization. Rather than complain about an invalid template
14001 parameter, issue an error message here. */
14002 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
14004 cp_parser_error (parser, "invalid explicit specialization");
14005 parameter_list = NULL_TREE;
14008 parameter_list = cp_parser_template_parameter_list (parser);
14009 parameter_list = end_template_parm_list (parameter_list);
14010 /* Look for the `>'. */
14011 cp_parser_skip_until_found (parser, CPP_GREATER, "`>'");
14012 /* We just processed one more parameter list. */
14013 ++parser->num_template_parameter_lists;
14014 /* If the next token is `template', there are more template
14016 if (cp_lexer_next_token_is_keyword (parser->lexer,
14018 cp_parser_template_declaration_after_export (parser, member_p);
14021 decl = cp_parser_single_declaration (parser,
14025 /* If this is a member template declaration, let the front
14027 if (member_p && !friend_p && decl)
14028 decl = finish_member_template_decl (decl);
14029 else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
14030 make_friend_class (current_class_type, TREE_TYPE (decl));
14032 /* We are done with the current parameter list. */
14033 --parser->num_template_parameter_lists;
14036 finish_template_decl (parameter_list);
14038 /* Register member declarations. */
14039 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
14040 finish_member_declaration (decl);
14042 /* If DECL is a function template, we must return to parse it later.
14043 (Even though there is no definition, there might be default
14044 arguments that need handling.) */
14045 if (member_p && decl
14046 && (TREE_CODE (decl) == FUNCTION_DECL
14047 || DECL_FUNCTION_TEMPLATE_P (decl)))
14048 TREE_VALUE (parser->unparsed_functions_queues)
14049 = tree_cons (current_class_type, decl,
14050 TREE_VALUE (parser->unparsed_functions_queues));
14053 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
14054 `function-definition' sequence. MEMBER_P is true, this declaration
14055 appears in a class scope.
14057 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
14058 *FRIEND_P is set to TRUE iff the declaration is a friend. */
14061 cp_parser_single_declaration (parser,
14068 bool declares_class_or_enum;
14069 tree decl = NULL_TREE;
14070 tree decl_specifiers;
14072 tree access_checks;
14074 /* Parse the dependent declaration. We don't know yet
14075 whether it will be a function-definition. */
14076 cp_parser_parse_tentatively (parser);
14077 /* Defer access checks until we know what is being declared. */
14078 cp_parser_start_deferring_access_checks (parser);
14079 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
14082 = cp_parser_decl_specifier_seq (parser,
14083 CP_PARSER_FLAGS_OPTIONAL,
14085 &declares_class_or_enum);
14086 /* Gather up the access checks that occurred the
14087 decl-specifier-seq. */
14088 access_checks = cp_parser_stop_deferring_access_checks (parser);
14089 /* Check for the declaration of a template class. */
14090 if (declares_class_or_enum)
14092 if (cp_parser_declares_only_class_p (parser))
14094 decl = shadow_tag (decl_specifiers);
14096 decl = TYPE_NAME (decl);
14098 decl = error_mark_node;
14103 /* If it's not a template class, try for a template function. If
14104 the next token is a `;', then this declaration does not declare
14105 anything. But, if there were errors in the decl-specifiers, then
14106 the error might well have come from an attempted class-specifier.
14107 In that case, there's no need to warn about a missing declarator. */
14109 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
14110 || !value_member (error_mark_node, decl_specifiers)))
14111 decl = cp_parser_init_declarator (parser,
14115 /*function_definition_allowed_p=*/false,
14117 /*function_definition_p=*/NULL);
14118 /* Clear any current qualification; whatever comes next is the start
14119 of something new. */
14120 parser->scope = NULL_TREE;
14121 parser->qualifying_scope = NULL_TREE;
14122 parser->object_scope = NULL_TREE;
14123 /* Look for a trailing `;' after the declaration. */
14124 if (!cp_parser_require (parser, CPP_SEMICOLON, "expected `;'")
14125 && cp_parser_committed_to_tentative_parse (parser))
14126 cp_parser_skip_to_end_of_block_or_statement (parser);
14127 /* If it worked, set *FRIEND_P based on the DECL_SPECIFIERS. */
14128 if (cp_parser_parse_definitely (parser))
14131 *friend_p = cp_parser_friend_p (decl_specifiers);
14133 /* Otherwise, try a function-definition. */
14135 decl = cp_parser_function_definition (parser, friend_p);
14140 /* Parse a functional cast to TYPE. Returns an expression
14141 representing the cast. */
14144 cp_parser_functional_cast (parser, type)
14148 tree expression_list;
14150 /* Look for the opening `('. */
14151 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
14152 return error_mark_node;
14153 /* If the next token is not an `)', there are arguments to the
14155 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
14156 expression_list = cp_parser_expression_list (parser);
14158 expression_list = NULL_TREE;
14159 /* Look for the closing `)'. */
14160 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14162 return build_functional_cast (type, expression_list);
14165 /* MEMBER_FUNCTION is a member function, or a friend. If default
14166 arguments, or the body of the function have not yet been parsed,
14170 cp_parser_late_parsing_for_member (parser, member_function)
14172 tree member_function;
14174 cp_lexer *saved_lexer;
14176 /* If this member is a template, get the underlying
14178 if (DECL_FUNCTION_TEMPLATE_P (member_function))
14179 member_function = DECL_TEMPLATE_RESULT (member_function);
14181 /* There should not be any class definitions in progress at this
14182 point; the bodies of members are only parsed outside of all class
14184 my_friendly_assert (parser->num_classes_being_defined == 0, 20010816);
14185 /* While we're parsing the member functions we might encounter more
14186 classes. We want to handle them right away, but we don't want
14187 them getting mixed up with functions that are currently in the
14189 parser->unparsed_functions_queues
14190 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
14192 /* Make sure that any template parameters are in scope. */
14193 maybe_begin_member_template_processing (member_function);
14195 /* If there are default arguments that have not yet been processed,
14196 take care of them now. */
14197 if (DECL_FUNCTION_MEMBER_P (member_function))
14198 push_nested_class (DECL_CONTEXT (member_function), 1);
14199 cp_parser_late_parsing_default_args (parser, TREE_TYPE (member_function));
14200 if (DECL_FUNCTION_MEMBER_P (member_function))
14201 pop_nested_class ();
14203 /* If the body of the function has not yet been parsed, parse it
14205 if (DECL_PENDING_INLINE_P (member_function))
14207 tree function_scope;
14208 cp_token_cache *tokens;
14210 /* The function is no longer pending; we are processing it. */
14211 tokens = DECL_PENDING_INLINE_INFO (member_function);
14212 DECL_PENDING_INLINE_INFO (member_function) = NULL;
14213 DECL_PENDING_INLINE_P (member_function) = 0;
14214 /* If this was an inline function in a local class, enter the scope
14215 of the containing function. */
14216 function_scope = decl_function_context (member_function);
14217 if (function_scope)
14218 push_function_context_to (function_scope);
14220 /* Save away the current lexer. */
14221 saved_lexer = parser->lexer;
14222 /* Make a new lexer to feed us the tokens saved for this function. */
14223 parser->lexer = cp_lexer_new_from_tokens (tokens);
14224 parser->lexer->next = saved_lexer;
14226 /* Set the current source position to be the location of the first
14227 token in the saved inline body. */
14228 cp_lexer_set_source_position_from_token
14230 cp_lexer_peek_token (parser->lexer));
14232 /* Let the front end know that we going to be defining this
14234 start_function (NULL_TREE, member_function, NULL_TREE,
14235 SF_PRE_PARSED | SF_INCLASS_INLINE);
14237 /* Now, parse the body of the function. */
14238 cp_parser_function_definition_after_declarator (parser,
14239 /*inline_p=*/true);
14241 /* Leave the scope of the containing function. */
14242 if (function_scope)
14243 pop_function_context_from (function_scope);
14244 /* Restore the lexer. */
14245 parser->lexer = saved_lexer;
14248 /* Remove any template parameters from the symbol table. */
14249 maybe_end_member_template_processing ();
14251 /* Restore the queue. */
14252 parser->unparsed_functions_queues
14253 = TREE_CHAIN (parser->unparsed_functions_queues);
14256 /* TYPE is a FUNCTION_TYPE or METHOD_TYPE which contains a parameter
14257 with an unparsed DEFAULT_ARG. Parse those default args now. */
14260 cp_parser_late_parsing_default_args (parser, type)
14264 cp_lexer *saved_lexer;
14265 cp_token_cache *tokens;
14266 bool saved_local_variables_forbidden_p;
14269 for (parameters = TYPE_ARG_TYPES (type);
14271 parameters = TREE_CHAIN (parameters))
14273 if (!TREE_PURPOSE (parameters)
14274 || TREE_CODE (TREE_PURPOSE (parameters)) != DEFAULT_ARG)
14277 /* Save away the current lexer. */
14278 saved_lexer = parser->lexer;
14279 /* Create a new one, using the tokens we have saved. */
14280 tokens = DEFARG_TOKENS (TREE_PURPOSE (parameters));
14281 parser->lexer = cp_lexer_new_from_tokens (tokens);
14283 /* Set the current source position to be the location of the
14284 first token in the default argument. */
14285 cp_lexer_set_source_position_from_token
14286 (parser->lexer, cp_lexer_peek_token (parser->lexer));
14288 /* Local variable names (and the `this' keyword) may not appear
14289 in a default argument. */
14290 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
14291 parser->local_variables_forbidden_p = true;
14292 /* Parse the assignment-expression. */
14293 TREE_PURPOSE (parameters) = cp_parser_assignment_expression (parser);
14295 /* Restore saved state. */
14296 parser->lexer = saved_lexer;
14297 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
14301 /* Parse the operand of `sizeof' (or a similar operator). Returns
14302 either a TYPE or an expression, depending on the form of the
14303 input. The KEYWORD indicates which kind of expression we have
14307 cp_parser_sizeof_operand (parser, keyword)
14311 static const char *format;
14312 tree expr = NULL_TREE;
14313 const char *saved_message;
14314 bool saved_constant_expression_p;
14316 /* Initialize FORMAT the first time we get here. */
14318 format = "types may not be defined in `%s' expressions";
14320 /* Types cannot be defined in a `sizeof' expression. Save away the
14322 saved_message = parser->type_definition_forbidden_message;
14323 /* And create the new one. */
14324 parser->type_definition_forbidden_message
14326 xmalloc (strlen (format)
14327 + strlen (IDENTIFIER_POINTER (ridpointers[keyword]))
14329 sprintf ((char *) parser->type_definition_forbidden_message,
14330 format, IDENTIFIER_POINTER (ridpointers[keyword]));
14332 /* The restrictions on constant-expressions do not apply inside
14333 sizeof expressions. */
14334 saved_constant_expression_p = parser->constant_expression_p;
14335 parser->constant_expression_p = false;
14337 /* If it's a `(', then we might be looking at the type-id
14339 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
14343 /* We can't be sure yet whether we're looking at a type-id or an
14345 cp_parser_parse_tentatively (parser);
14346 /* Consume the `('. */
14347 cp_lexer_consume_token (parser->lexer);
14348 /* Parse the type-id. */
14349 type = cp_parser_type_id (parser);
14350 /* Now, look for the trailing `)'. */
14351 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14352 /* If all went well, then we're done. */
14353 if (cp_parser_parse_definitely (parser))
14355 /* Build a list of decl-specifiers; right now, we have only
14356 a single type-specifier. */
14357 type = build_tree_list (NULL_TREE,
14360 /* Call grokdeclarator to figure out what type this is. */
14361 expr = grokdeclarator (NULL_TREE,
14365 /*attrlist=*/NULL);
14369 /* If the type-id production did not work out, then we must be
14370 looking at the unary-expression production. */
14372 expr = cp_parser_unary_expression (parser, /*address_p=*/false);
14374 /* Free the message we created. */
14375 free ((char *) parser->type_definition_forbidden_message);
14376 /* And restore the old one. */
14377 parser->type_definition_forbidden_message = saved_message;
14378 parser->constant_expression_p = saved_constant_expression_p;
14383 /* If the current declaration has no declarator, return true. */
14386 cp_parser_declares_only_class_p (cp_parser *parser)
14388 /* If the next token is a `;' or a `,' then there is no
14390 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
14391 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
14394 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
14395 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
14398 cp_parser_friend_p (decl_specifiers)
14399 tree decl_specifiers;
14401 while (decl_specifiers)
14403 /* See if this decl-specifier is `friend'. */
14404 if (TREE_CODE (TREE_VALUE (decl_specifiers)) == IDENTIFIER_NODE
14405 && C_RID_CODE (TREE_VALUE (decl_specifiers)) == RID_FRIEND)
14408 /* Go on to the next decl-specifier. */
14409 decl_specifiers = TREE_CHAIN (decl_specifiers);
14415 /* If the next token is of the indicated TYPE, consume it. Otherwise,
14416 issue an error message indicating that TOKEN_DESC was expected.
14418 Returns the token consumed, if the token had the appropriate type.
14419 Otherwise, returns NULL. */
14422 cp_parser_require (parser, type, token_desc)
14424 enum cpp_ttype type;
14425 const char *token_desc;
14427 if (cp_lexer_next_token_is (parser->lexer, type))
14428 return cp_lexer_consume_token (parser->lexer);
14431 dyn_string_t error_msg;
14433 /* Format the error message. */
14434 error_msg = dyn_string_new (0);
14435 dyn_string_append_cstr (error_msg, "expected ");
14436 dyn_string_append_cstr (error_msg, token_desc);
14437 cp_parser_error (parser, error_msg->s);
14438 dyn_string_delete (error_msg);
14443 /* Like cp_parser_require, except that tokens will be skipped until
14444 the desired token is found. An error message is still produced if
14445 the next token is not as expected. */
14448 cp_parser_skip_until_found (parser, type, token_desc)
14450 enum cpp_ttype type;
14451 const char *token_desc;
14454 unsigned nesting_depth = 0;
14456 if (cp_parser_require (parser, type, token_desc))
14459 /* Skip tokens until the desired token is found. */
14462 /* Peek at the next token. */
14463 token = cp_lexer_peek_token (parser->lexer);
14464 /* If we've reached the token we want, consume it and
14466 if (token->type == type && !nesting_depth)
14468 cp_lexer_consume_token (parser->lexer);
14471 /* If we've run out of tokens, stop. */
14472 if (token->type == CPP_EOF)
14474 if (token->type == CPP_OPEN_BRACE
14475 || token->type == CPP_OPEN_PAREN
14476 || token->type == CPP_OPEN_SQUARE)
14478 else if (token->type == CPP_CLOSE_BRACE
14479 || token->type == CPP_CLOSE_PAREN
14480 || token->type == CPP_CLOSE_SQUARE)
14482 if (nesting_depth-- == 0)
14485 /* Consume this token. */
14486 cp_lexer_consume_token (parser->lexer);
14490 /* If the next token is the indicated keyword, consume it. Otherwise,
14491 issue an error message indicating that TOKEN_DESC was expected.
14493 Returns the token consumed, if the token had the appropriate type.
14494 Otherwise, returns NULL. */
14497 cp_parser_require_keyword (parser, keyword, token_desc)
14500 const char *token_desc;
14502 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
14504 if (token && token->keyword != keyword)
14506 dyn_string_t error_msg;
14508 /* Format the error message. */
14509 error_msg = dyn_string_new (0);
14510 dyn_string_append_cstr (error_msg, "expected ");
14511 dyn_string_append_cstr (error_msg, token_desc);
14512 cp_parser_error (parser, error_msg->s);
14513 dyn_string_delete (error_msg);
14520 /* Returns TRUE iff TOKEN is a token that can begin the body of a
14521 function-definition. */
14524 cp_parser_token_starts_function_definition_p (token)
14527 return (/* An ordinary function-body begins with an `{'. */
14528 token->type == CPP_OPEN_BRACE
14529 /* A ctor-initializer begins with a `:'. */
14530 || token->type == CPP_COLON
14531 /* A function-try-block begins with `try'. */
14532 || token->keyword == RID_TRY
14533 /* The named return value extension begins with `return'. */
14534 || token->keyword == RID_RETURN);
14537 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
14541 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
14545 token = cp_lexer_peek_token (parser->lexer);
14546 return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
14549 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
14550 or none_type otherwise. */
14552 static enum tag_types
14553 cp_parser_token_is_class_key (token)
14556 switch (token->keyword)
14561 return record_type;
14570 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
14573 cp_parser_check_class_key (enum tag_types class_key, tree type)
14575 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
14576 pedwarn ("`%s' tag used in naming `%#T'",
14577 class_key == union_type ? "union"
14578 : class_key == record_type ? "struct" : "class",
14582 /* Look for the `template' keyword, as a syntactic disambiguator.
14583 Return TRUE iff it is present, in which case it will be
14587 cp_parser_optional_template_keyword (cp_parser *parser)
14589 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
14591 /* The `template' keyword can only be used within templates;
14592 outside templates the parser can always figure out what is a
14593 template and what is not. */
14594 if (!processing_template_decl)
14596 error ("`template' (as a disambiguator) is only allowed "
14597 "within templates");
14598 /* If this part of the token stream is rescanned, the same
14599 error message would be generated. So, we purge the token
14600 from the stream. */
14601 cp_lexer_purge_token (parser->lexer);
14606 /* Consume the `template' keyword. */
14607 cp_lexer_consume_token (parser->lexer);
14615 /* Add tokens to CACHE until an non-nested END token appears. */
14618 cp_parser_cache_group (cp_parser *parser,
14619 cp_token_cache *cache,
14620 enum cpp_ttype end,
14627 /* Abort a parenthesized expression if we encounter a brace. */
14628 if ((end == CPP_CLOSE_PAREN || depth == 0)
14629 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
14631 /* Consume the next token. */
14632 token = cp_lexer_consume_token (parser->lexer);
14633 /* If we've reached the end of the file, stop. */
14634 if (token->type == CPP_EOF)
14636 /* Add this token to the tokens we are saving. */
14637 cp_token_cache_push_token (cache, token);
14638 /* See if it starts a new group. */
14639 if (token->type == CPP_OPEN_BRACE)
14641 cp_parser_cache_group (parser, cache, CPP_CLOSE_BRACE, depth + 1);
14645 else if (token->type == CPP_OPEN_PAREN)
14646 cp_parser_cache_group (parser, cache, CPP_CLOSE_PAREN, depth + 1);
14647 else if (token->type == end)
14652 /* Begin parsing tentatively. We always save tokens while parsing
14653 tentatively so that if the tentative parsing fails we can restore the
14657 cp_parser_parse_tentatively (parser)
14660 /* Enter a new parsing context. */
14661 parser->context = cp_parser_context_new (parser->context);
14662 /* Begin saving tokens. */
14663 cp_lexer_save_tokens (parser->lexer);
14664 /* In order to avoid repetitive access control error messages,
14665 access checks are queued up until we are no longer parsing
14667 cp_parser_start_deferring_access_checks (parser);
14670 /* Commit to the currently active tentative parse. */
14673 cp_parser_commit_to_tentative_parse (parser)
14676 cp_parser_context *context;
14679 /* Mark all of the levels as committed. */
14680 lexer = parser->lexer;
14681 for (context = parser->context; context->next; context = context->next)
14683 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
14685 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
14686 while (!cp_lexer_saving_tokens (lexer))
14687 lexer = lexer->next;
14688 cp_lexer_commit_tokens (lexer);
14692 /* Abort the currently active tentative parse. All consumed tokens
14693 will be rolled back, and no diagnostics will be issued. */
14696 cp_parser_abort_tentative_parse (parser)
14699 cp_parser_simulate_error (parser);
14700 /* Now, pretend that we want to see if the construct was
14701 successfully parsed. */
14702 cp_parser_parse_definitely (parser);
14705 /* Stop parsing tentatively. If a parse error has ocurred, restore the
14706 token stream. Otherwise, commit to the tokens we have consumed.
14707 Returns true if no error occurred; false otherwise. */
14710 cp_parser_parse_definitely (parser)
14713 bool error_occurred;
14714 cp_parser_context *context;
14716 /* Remember whether or not an error ocurred, since we are about to
14717 destroy that information. */
14718 error_occurred = cp_parser_error_occurred (parser);
14719 /* Remove the topmost context from the stack. */
14720 context = parser->context;
14721 parser->context = context->next;
14722 /* If no parse errors occurred, commit to the tentative parse. */
14723 if (!error_occurred)
14725 /* Commit to the tokens read tentatively, unless that was
14727 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
14728 cp_lexer_commit_tokens (parser->lexer);
14729 if (!parser->context->deferring_access_checks_p)
14730 /* If in the parent context we are not deferring checks, then
14731 these perform these checks now. */
14732 (cp_parser_perform_deferred_access_checks
14733 (context->deferred_access_checks));
14735 /* Any lookups that were deferred during the tentative parse are
14737 parser->context->deferred_access_checks
14738 = chainon (parser->context->deferred_access_checks,
14739 context->deferred_access_checks);
14742 /* Otherwise, if errors occurred, roll back our state so that things
14743 are just as they were before we began the tentative parse. */
14746 cp_lexer_rollback_tokens (parser->lexer);
14751 /* Returns non-zero if we are parsing tentatively. */
14754 cp_parser_parsing_tentatively (parser)
14757 return parser->context->next != NULL;
14760 /* Returns true if we are parsing tentatively -- but have decided that
14761 we will stick with this tentative parse, even if errors occur. */
14764 cp_parser_committed_to_tentative_parse (parser)
14767 return (cp_parser_parsing_tentatively (parser)
14768 && parser->context->status == CP_PARSER_STATUS_KIND_COMMITTED);
14771 /* Returns non-zero iff an error has occurred during the most recent
14772 tentative parse. */
14775 cp_parser_error_occurred (parser)
14778 return (cp_parser_parsing_tentatively (parser)
14779 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
14782 /* Returns non-zero if GNU extensions are allowed. */
14785 cp_parser_allow_gnu_extensions_p (parser)
14788 return parser->allow_gnu_extensions_p;
14795 static GTY (()) cp_parser *the_parser;
14797 /* External interface. */
14799 /* Parse the entire translation unit. */
14804 bool error_occurred;
14806 the_parser = cp_parser_new ();
14807 error_occurred = cp_parser_translation_unit (the_parser);
14810 return error_occurred;
14813 /* Clean up after parsing the entire translation unit. */
14816 free_parser_stacks ()
14818 /* Nothing to do. */
14821 /* This variable must be provided by every front end. */
14825 #include "gt-cp-parser.h"