2 Copyright (C) 2000, 2001, 2002, 2003 Free Software Foundation, Inc.
3 Written by Mark Mitchell <mark@codesourcery.com>.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 GCC is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
24 #include "coretypes.h"
26 #include "dyn-string.h"
34 #include "diagnostic.h"
45 A cp_lexer represents a stream of cp_tokens. It allows arbitrary
51 We use a circular buffer to store incoming tokens.
53 Some artifacts of the C++ language (such as the
54 expression/declaration ambiguity) require arbitrary look-ahead.
55 The strategy we adopt for dealing with these problems is to attempt
56 to parse one construct (e.g., the declaration) and fall back to the
57 other (e.g., the expression) if that attempt does not succeed.
58 Therefore, we must sometimes store an arbitrary number of tokens.
60 The parser routinely peeks at the next token, and then consumes it
61 later. That also requires a buffer in which to store the tokens.
63 In order to easily permit adding tokens to the end of the buffer,
64 while removing them from the beginning of the buffer, we use a
69 typedef struct cp_token GTY (())
71 /* The kind of token. */
73 /* The value associated with this token, if any. */
75 /* If this token is a keyword, this value indicates which keyword.
76 Otherwise, this value is RID_MAX. */
78 /* The file in which this token was found. */
79 const char *file_name;
80 /* The line at which this token was found. */
84 /* The number of tokens in a single token block. */
86 #define CP_TOKEN_BLOCK_NUM_TOKENS 32
88 /* A group of tokens. These groups are chained together to store
89 large numbers of tokens. (For example, a token block is created
90 when the body of an inline member function is first encountered;
91 the tokens are processed later after the class definition is
94 This somewhat ungainly data structure (as opposed to, say, a
95 variable-length array), is used due to contraints imposed by the
96 current garbage-collection methodology. If it is made more
97 flexible, we could perhaps simplify the data structures involved. */
99 typedef struct cp_token_block GTY (())
102 cp_token tokens[CP_TOKEN_BLOCK_NUM_TOKENS];
103 /* The number of tokens in this block. */
105 /* The next token block in the chain. */
106 struct cp_token_block *next;
107 /* The previous block in the chain. */
108 struct cp_token_block *prev;
111 typedef struct cp_token_cache GTY (())
113 /* The first block in the cache. NULL if there are no tokens in the
115 cp_token_block *first;
116 /* The last block in the cache. NULL If there are no tokens in the
118 cp_token_block *last;
123 static cp_token_cache *cp_token_cache_new
125 static void cp_token_cache_push_token
126 (cp_token_cache *, cp_token *);
128 /* Create a new cp_token_cache. */
130 static cp_token_cache *
131 cp_token_cache_new ()
133 return (cp_token_cache *) ggc_alloc_cleared (sizeof (cp_token_cache));
136 /* Add *TOKEN to *CACHE. */
139 cp_token_cache_push_token (cp_token_cache *cache,
142 cp_token_block *b = cache->last;
144 /* See if we need to allocate a new token block. */
145 if (!b || b->num_tokens == CP_TOKEN_BLOCK_NUM_TOKENS)
147 b = ((cp_token_block *) ggc_alloc_cleared (sizeof (cp_token_block)));
148 b->prev = cache->last;
151 cache->last->next = b;
155 cache->first = cache->last = b;
157 /* Add this token to the current token block. */
158 b->tokens[b->num_tokens++] = *token;
161 /* The cp_lexer structure represents the C++ lexer. It is responsible
162 for managing the token stream from the preprocessor and supplying
165 typedef struct cp_lexer GTY (())
167 /* The memory allocated for the buffer. Never NULL. */
168 cp_token * GTY ((length ("(%h.buffer_end - %h.buffer)"))) buffer;
169 /* A pointer just past the end of the memory allocated for the buffer. */
170 cp_token * GTY ((skip (""))) buffer_end;
171 /* The first valid token in the buffer, or NULL if none. */
172 cp_token * GTY ((skip (""))) first_token;
173 /* The next available token. If NEXT_TOKEN is NULL, then there are
174 no more available tokens. */
175 cp_token * GTY ((skip (""))) next_token;
176 /* A pointer just past the last available token. If FIRST_TOKEN is
177 NULL, however, there are no available tokens, and then this
178 location is simply the place in which the next token read will be
179 placed. If LAST_TOKEN == FIRST_TOKEN, then the buffer is full.
180 When the LAST_TOKEN == BUFFER, then the last token is at the
181 highest memory address in the BUFFER. */
182 cp_token * GTY ((skip (""))) last_token;
184 /* A stack indicating positions at which cp_lexer_save_tokens was
185 called. The top entry is the most recent position at which we
186 began saving tokens. The entries are differences in token
187 position between FIRST_TOKEN and the first saved token.
189 If the stack is non-empty, we are saving tokens. When a token is
190 consumed, the NEXT_TOKEN pointer will move, but the FIRST_TOKEN
191 pointer will not. The token stream will be preserved so that it
192 can be reexamined later.
194 If the stack is empty, then we are not saving tokens. Whenever a
195 token is consumed, the FIRST_TOKEN pointer will be moved, and the
196 consumed token will be gone forever. */
197 varray_type saved_tokens;
199 /* The STRING_CST tokens encountered while processing the current
201 varray_type string_tokens;
203 /* True if we should obtain more tokens from the preprocessor; false
204 if we are processing a saved token cache. */
207 /* True if we should output debugging information. */
210 /* The next lexer in a linked list of lexers. */
211 struct cp_lexer *next;
216 static cp_lexer *cp_lexer_new
218 static cp_lexer *cp_lexer_new_from_tokens
219 PARAMS ((struct cp_token_cache *));
220 static int cp_lexer_saving_tokens
221 PARAMS ((const cp_lexer *));
222 static cp_token *cp_lexer_next_token
223 PARAMS ((cp_lexer *, cp_token *));
224 static ptrdiff_t cp_lexer_token_difference
225 PARAMS ((cp_lexer *, cp_token *, cp_token *));
226 static cp_token *cp_lexer_read_token
227 PARAMS ((cp_lexer *));
228 static void cp_lexer_maybe_grow_buffer
229 PARAMS ((cp_lexer *));
230 static void cp_lexer_get_preprocessor_token
231 PARAMS ((cp_lexer *, cp_token *));
232 static cp_token *cp_lexer_peek_token
233 PARAMS ((cp_lexer *));
234 static cp_token *cp_lexer_peek_nth_token
235 PARAMS ((cp_lexer *, size_t));
236 static bool cp_lexer_next_token_is
237 PARAMS ((cp_lexer *, enum cpp_ttype));
238 static bool cp_lexer_next_token_is_not
239 PARAMS ((cp_lexer *, enum cpp_ttype));
240 static bool cp_lexer_next_token_is_keyword
241 PARAMS ((cp_lexer *, enum rid));
242 static cp_token *cp_lexer_consume_token
243 PARAMS ((cp_lexer *));
244 static void cp_lexer_purge_token
246 static void cp_lexer_purge_tokens_after
247 (cp_lexer *, cp_token *);
248 static void cp_lexer_save_tokens
249 PARAMS ((cp_lexer *));
250 static void cp_lexer_commit_tokens
251 PARAMS ((cp_lexer *));
252 static void cp_lexer_rollback_tokens
253 PARAMS ((cp_lexer *));
254 static void cp_lexer_set_source_position_from_token
255 PARAMS ((cp_lexer *, const cp_token *));
256 static void cp_lexer_print_token
257 PARAMS ((FILE *, cp_token *));
258 static bool cp_lexer_debugging_p
259 PARAMS ((cp_lexer *));
260 static void cp_lexer_start_debugging
261 PARAMS ((cp_lexer *)) ATTRIBUTE_UNUSED;
262 static void cp_lexer_stop_debugging
263 PARAMS ((cp_lexer *)) ATTRIBUTE_UNUSED;
265 /* Manifest constants. */
267 #define CP_TOKEN_BUFFER_SIZE 5
268 #define CP_SAVED_TOKENS_SIZE 5
270 /* A token type for keywords, as opposed to ordinary identifiers. */
271 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
273 /* A token type for template-ids. If a template-id is processed while
274 parsing tentatively, it is replaced with a CPP_TEMPLATE_ID token;
275 the value of the CPP_TEMPLATE_ID is whatever was returned by
276 cp_parser_template_id. */
277 #define CPP_TEMPLATE_ID ((enum cpp_ttype) (CPP_KEYWORD + 1))
279 /* A token type for nested-name-specifiers. If a
280 nested-name-specifier is processed while parsing tentatively, it is
281 replaced with a CPP_NESTED_NAME_SPECIFIER token; the value of the
282 CPP_NESTED_NAME_SPECIFIER is whatever was returned by
283 cp_parser_nested_name_specifier_opt. */
284 #define CPP_NESTED_NAME_SPECIFIER ((enum cpp_ttype) (CPP_TEMPLATE_ID + 1))
286 /* A token type for tokens that are not tokens at all; these are used
287 to mark the end of a token block. */
288 #define CPP_NONE (CPP_NESTED_NAME_SPECIFIER + 1)
292 /* The stream to which debugging output should be written. */
293 static FILE *cp_lexer_debug_stream;
295 /* Create a new C++ lexer. If MAIN_LEXER_P is true the new lexer is
296 the main lexer -- i.e, the lexer that gets tokens from the
297 preprocessor. Otherwise, it is a lexer that uses a cache of stored
301 cp_lexer_new (bool main_lexer_p)
305 /* Allocate the memory. */
306 lexer = (cp_lexer *) ggc_alloc_cleared (sizeof (cp_lexer));
308 /* Create the circular buffer. */
309 lexer->buffer = ((cp_token *)
310 ggc_alloc (CP_TOKEN_BUFFER_SIZE * sizeof (cp_token)));
311 lexer->buffer_end = lexer->buffer + CP_TOKEN_BUFFER_SIZE;
313 /* There are no tokens in the buffer. */
314 lexer->last_token = lexer->buffer;
316 /* This lexer obtains more tokens by calling c_lex. */
317 lexer->main_lexer_p = main_lexer_p;
319 /* Create the SAVED_TOKENS stack. */
320 VARRAY_INT_INIT (lexer->saved_tokens, CP_SAVED_TOKENS_SIZE, "saved_tokens");
322 /* Create the STRINGS array. */
323 VARRAY_TREE_INIT (lexer->string_tokens, 32, "strings");
325 /* Assume we are not debugging. */
326 lexer->debugging_p = false;
331 /* Create a new lexer whose token stream is primed with the TOKENS.
332 When these tokens are exhausted, no new tokens will be read. */
335 cp_lexer_new_from_tokens (cp_token_cache *tokens)
339 cp_token_block *block;
340 ptrdiff_t num_tokens;
342 /* Create the lexer. */
343 lexer = cp_lexer_new (/*main_lexer_p=*/false);
345 /* Create a new buffer, appropriately sized. */
347 for (block = tokens->first; block != NULL; block = block->next)
348 num_tokens += block->num_tokens;
349 lexer->buffer = ((cp_token *)
350 ggc_alloc (num_tokens * sizeof (cp_token)));
351 lexer->buffer_end = lexer->buffer + num_tokens;
353 /* Install the tokens. */
354 token = lexer->buffer;
355 for (block = tokens->first; block != NULL; block = block->next)
357 memcpy (token, block->tokens, block->num_tokens * sizeof (cp_token));
358 token += block->num_tokens;
361 /* The FIRST_TOKEN is the beginning of the buffer. */
362 lexer->first_token = lexer->buffer;
363 /* The next available token is also at the beginning of the buffer. */
364 lexer->next_token = lexer->buffer;
365 /* The buffer is full. */
366 lexer->last_token = lexer->first_token;
371 /* Non-zero if we are presently saving tokens. */
374 cp_lexer_saving_tokens (lexer)
375 const cp_lexer *lexer;
377 return VARRAY_ACTIVE_SIZE (lexer->saved_tokens) != 0;
380 /* TOKEN points into the circular token buffer. Return a pointer to
381 the next token in the buffer. */
384 cp_lexer_next_token (lexer, token)
389 if (token == lexer->buffer_end)
390 token = lexer->buffer;
394 /* Return a pointer to the token that is N tokens beyond TOKEN in the
398 cp_lexer_advance_token (cp_lexer *lexer, cp_token *token, ptrdiff_t n)
401 if (token >= lexer->buffer_end)
402 token = lexer->buffer + (token - lexer->buffer_end);
406 /* Returns the number of times that START would have to be incremented
407 to reach FINISH. If START and FINISH are the same, returns zero. */
410 cp_lexer_token_difference (lexer, start, finish)
416 return finish - start;
418 return ((lexer->buffer_end - lexer->buffer)
422 /* Obtain another token from the C preprocessor and add it to the
423 token buffer. Returns the newly read token. */
426 cp_lexer_read_token (lexer)
431 /* Make sure there is room in the buffer. */
432 cp_lexer_maybe_grow_buffer (lexer);
434 /* If there weren't any tokens, then this one will be the first. */
435 if (!lexer->first_token)
436 lexer->first_token = lexer->last_token;
437 /* Similarly, if there were no available tokens, there is one now. */
438 if (!lexer->next_token)
439 lexer->next_token = lexer->last_token;
441 /* Figure out where we're going to store the new token. */
442 token = lexer->last_token;
444 /* Get a new token from the preprocessor. */
445 cp_lexer_get_preprocessor_token (lexer, token);
447 /* Increment LAST_TOKEN. */
448 lexer->last_token = cp_lexer_next_token (lexer, token);
450 /* The preprocessor does not yet do translation phase six, i.e., the
451 combination of adjacent string literals. Therefore, we do it
453 if (token->type == CPP_STRING || token->type == CPP_WSTRING)
458 /* When we grow the buffer, we may invalidate TOKEN. So, save
459 the distance from the beginning of the BUFFER so that we can
461 delta = cp_lexer_token_difference (lexer, lexer->buffer, token);
462 /* Make sure there is room in the buffer for another token. */
463 cp_lexer_maybe_grow_buffer (lexer);
465 token = lexer->buffer;
466 for (i = 0; i < delta; ++i)
467 token = cp_lexer_next_token (lexer, token);
469 VARRAY_PUSH_TREE (lexer->string_tokens, token->value);
472 /* Read the token after TOKEN. */
473 cp_lexer_get_preprocessor_token (lexer, lexer->last_token);
474 /* See whether it's another string constant. */
475 if (lexer->last_token->type != token->type)
477 /* If not, then it will be the next real token. */
478 lexer->last_token = cp_lexer_next_token (lexer,
483 /* Chain the strings together. */
484 VARRAY_PUSH_TREE (lexer->string_tokens,
485 lexer->last_token->value);
488 /* Create a single STRING_CST. Curiously we have to call
489 combine_strings even if there is only a single string in
490 order to get the type set correctly. */
491 token->value = combine_strings (lexer->string_tokens);
492 VARRAY_CLEAR (lexer->string_tokens);
493 token->value = fix_string_type (token->value);
494 /* Strings should have type `const char []'. Right now, we will
495 have an ARRAY_TYPE that is constant rather than an array of
496 constant elements. */
497 if (flag_const_strings)
501 /* Get the current type. It will be an ARRAY_TYPE. */
502 type = TREE_TYPE (token->value);
503 /* Use build_cplus_array_type to rebuild the array, thereby
504 getting the right type. */
505 type = build_cplus_array_type (TREE_TYPE (type),
507 /* Reset the type of the token. */
508 TREE_TYPE (token->value) = type;
515 /* If the circular buffer is full, make it bigger. */
518 cp_lexer_maybe_grow_buffer (lexer)
521 /* If the buffer is full, enlarge it. */
522 if (lexer->last_token == lexer->first_token)
524 cp_token *new_buffer;
525 cp_token *old_buffer;
526 cp_token *new_first_token;
527 ptrdiff_t buffer_length;
528 size_t num_tokens_to_copy;
530 /* Remember the current buffer pointer. It will become invalid,
531 but we will need to do pointer arithmetic involving this
533 old_buffer = lexer->buffer;
534 /* Compute the current buffer size. */
535 buffer_length = lexer->buffer_end - lexer->buffer;
536 /* Allocate a buffer twice as big. */
537 new_buffer = ((cp_token *)
538 ggc_realloc (lexer->buffer,
539 2 * buffer_length * sizeof (cp_token)));
541 /* Because the buffer is circular, logically consecutive tokens
542 are not necessarily placed consecutively in memory.
543 Therefore, we must keep move the tokens that were before
544 FIRST_TOKEN to the second half of the newly allocated
546 num_tokens_to_copy = (lexer->first_token - old_buffer);
547 memcpy (new_buffer + buffer_length,
549 num_tokens_to_copy * sizeof (cp_token));
550 /* Clear the rest of the buffer. We never look at this storage,
551 but the garbage collector may. */
552 memset (new_buffer + buffer_length + num_tokens_to_copy, 0,
553 (buffer_length - num_tokens_to_copy) * sizeof (cp_token));
555 /* Now recompute all of the buffer pointers. */
557 = new_buffer + (lexer->first_token - old_buffer);
558 if (lexer->next_token != NULL)
560 ptrdiff_t next_token_delta;
562 if (lexer->next_token > lexer->first_token)
563 next_token_delta = lexer->next_token - lexer->first_token;
566 buffer_length - (lexer->first_token - lexer->next_token);
567 lexer->next_token = new_first_token + next_token_delta;
569 lexer->last_token = new_first_token + buffer_length;
570 lexer->buffer = new_buffer;
571 lexer->buffer_end = new_buffer + buffer_length * 2;
572 lexer->first_token = new_first_token;
576 /* Store the next token from the preprocessor in *TOKEN. */
579 cp_lexer_get_preprocessor_token (lexer, token)
580 cp_lexer *lexer ATTRIBUTE_UNUSED;
585 /* If this not the main lexer, return a terminating CPP_EOF token. */
586 if (!lexer->main_lexer_p)
588 token->type = CPP_EOF;
589 token->line_number = 0;
590 token->file_name = NULL;
591 token->value = NULL_TREE;
592 token->keyword = RID_MAX;
598 /* Keep going until we get a token we like. */
601 /* Get a new token from the preprocessor. */
602 token->type = c_lex (&token->value);
603 /* Issue messages about tokens we cannot process. */
609 error ("invalid token");
613 /* These tokens are already warned about by c_lex. */
617 /* This is a good token, so we exit the loop. */
622 /* Now we've got our token. */
623 token->line_number = lineno;
624 token->file_name = input_filename;
626 /* Check to see if this token is a keyword. */
627 if (token->type == CPP_NAME
628 && C_IS_RESERVED_WORD (token->value))
630 /* Mark this token as a keyword. */
631 token->type = CPP_KEYWORD;
632 /* Record which keyword. */
633 token->keyword = C_RID_CODE (token->value);
634 /* Update the value. Some keywords are mapped to particular
635 entities, rather than simply having the value of the
636 corresponding IDENTIFIER_NODE. For example, `__const' is
637 mapped to `const'. */
638 token->value = ridpointers[token->keyword];
641 token->keyword = RID_MAX;
644 /* Return a pointer to the next token in the token stream, but do not
648 cp_lexer_peek_token (lexer)
653 /* If there are no tokens, read one now. */
654 if (!lexer->next_token)
655 cp_lexer_read_token (lexer);
657 /* Provide debugging output. */
658 if (cp_lexer_debugging_p (lexer))
660 fprintf (cp_lexer_debug_stream, "cp_lexer: peeking at token: ");
661 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
662 fprintf (cp_lexer_debug_stream, "\n");
665 token = lexer->next_token;
666 cp_lexer_set_source_position_from_token (lexer, token);
670 /* Return true if the next token has the indicated TYPE. */
673 cp_lexer_next_token_is (lexer, type)
679 /* Peek at the next token. */
680 token = cp_lexer_peek_token (lexer);
681 /* Check to see if it has the indicated TYPE. */
682 return token->type == type;
685 /* Return true if the next token does not have the indicated TYPE. */
688 cp_lexer_next_token_is_not (lexer, type)
692 return !cp_lexer_next_token_is (lexer, type);
695 /* Return true if the next token is the indicated KEYWORD. */
698 cp_lexer_next_token_is_keyword (lexer, keyword)
704 /* Peek at the next token. */
705 token = cp_lexer_peek_token (lexer);
706 /* Check to see if it is the indicated keyword. */
707 return token->keyword == keyword;
710 /* Return a pointer to the Nth token in the token stream. If N is 1,
711 then this is precisely equivalent to cp_lexer_peek_token. */
714 cp_lexer_peek_nth_token (lexer, n)
720 /* N is 1-based, not zero-based. */
721 my_friendly_assert (n > 0, 20000224);
723 /* Skip ahead from NEXT_TOKEN, reading more tokens as necessary. */
724 token = lexer->next_token;
725 /* If there are no tokens in the buffer, get one now. */
728 cp_lexer_read_token (lexer);
729 token = lexer->next_token;
732 /* Now, read tokens until we have enough. */
735 /* Advance to the next token. */
736 token = cp_lexer_next_token (lexer, token);
737 /* If that's all the tokens we have, read a new one. */
738 if (token == lexer->last_token)
739 token = cp_lexer_read_token (lexer);
745 /* Consume the next token. The pointer returned is valid only until
746 another token is read. Callers should preserve copy the token
747 explicitly if they will need its value for a longer period of
751 cp_lexer_consume_token (lexer)
756 /* If there are no tokens, read one now. */
757 if (!lexer->next_token)
758 cp_lexer_read_token (lexer);
760 /* Remember the token we'll be returning. */
761 token = lexer->next_token;
763 /* Increment NEXT_TOKEN. */
764 lexer->next_token = cp_lexer_next_token (lexer,
766 /* Check to see if we're all out of tokens. */
767 if (lexer->next_token == lexer->last_token)
768 lexer->next_token = NULL;
770 /* If we're not saving tokens, then move FIRST_TOKEN too. */
771 if (!cp_lexer_saving_tokens (lexer))
773 /* If there are no tokens available, set FIRST_TOKEN to NULL. */
774 if (!lexer->next_token)
775 lexer->first_token = NULL;
777 lexer->first_token = lexer->next_token;
780 /* Provide debugging output. */
781 if (cp_lexer_debugging_p (lexer))
783 fprintf (cp_lexer_debug_stream, "cp_lexer: consuming token: ");
784 cp_lexer_print_token (cp_lexer_debug_stream, token);
785 fprintf (cp_lexer_debug_stream, "\n");
791 /* Permanently remove the next token from the token stream. There
792 must be a valid next token already; this token never reads
793 additional tokens from the preprocessor. */
796 cp_lexer_purge_token (cp_lexer *lexer)
799 cp_token *next_token;
801 token = lexer->next_token;
804 next_token = cp_lexer_next_token (lexer, token);
805 if (next_token == lexer->last_token)
807 *token = *next_token;
811 lexer->last_token = token;
812 /* The token purged may have been the only token remaining; if so,
814 if (lexer->next_token == token)
815 lexer->next_token = NULL;
818 /* Permanently remove all tokens after TOKEN, up to, but not
819 including, the token that will be returned next by
820 cp_lexer_peek_token. */
823 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *token)
829 if (lexer->next_token)
831 /* Copy the tokens that have not yet been read to the location
832 immediately following TOKEN. */
833 t1 = cp_lexer_next_token (lexer, token);
834 t2 = peek = cp_lexer_peek_token (lexer);
835 /* Move tokens into the vacant area between TOKEN and PEEK. */
836 while (t2 != lexer->last_token)
839 t1 = cp_lexer_next_token (lexer, t1);
840 t2 = cp_lexer_next_token (lexer, t2);
842 /* Now, the next available token is right after TOKEN. */
843 lexer->next_token = cp_lexer_next_token (lexer, token);
844 /* And the last token is wherever we ended up. */
845 lexer->last_token = t1;
849 /* There are no tokens in the buffer, so there is nothing to
850 copy. The last token in the buffer is TOKEN itself. */
851 lexer->last_token = cp_lexer_next_token (lexer, token);
855 /* Begin saving tokens. All tokens consumed after this point will be
859 cp_lexer_save_tokens (lexer)
862 /* Provide debugging output. */
863 if (cp_lexer_debugging_p (lexer))
864 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
866 /* Make sure that LEXER->NEXT_TOKEN is non-NULL so that we can
867 restore the tokens if required. */
868 if (!lexer->next_token)
869 cp_lexer_read_token (lexer);
871 VARRAY_PUSH_INT (lexer->saved_tokens,
872 cp_lexer_token_difference (lexer,
877 /* Commit to the portion of the token stream most recently saved. */
880 cp_lexer_commit_tokens (lexer)
883 /* Provide debugging output. */
884 if (cp_lexer_debugging_p (lexer))
885 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
887 VARRAY_POP (lexer->saved_tokens);
890 /* Return all tokens saved since the last call to cp_lexer_save_tokens
891 to the token stream. Stop saving tokens. */
894 cp_lexer_rollback_tokens (lexer)
899 /* Provide debugging output. */
900 if (cp_lexer_debugging_p (lexer))
901 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
903 /* Find the token that was the NEXT_TOKEN when we started saving
905 delta = VARRAY_TOP_INT(lexer->saved_tokens);
906 /* Make it the next token again now. */
907 lexer->next_token = cp_lexer_advance_token (lexer,
910 /* It might be the case that there wer no tokens when we started
911 saving tokens, but that there are some tokens now. */
912 if (!lexer->next_token && lexer->first_token)
913 lexer->next_token = lexer->first_token;
915 /* Stop saving tokens. */
916 VARRAY_POP (lexer->saved_tokens);
919 /* Set the current source position from the information stored in
923 cp_lexer_set_source_position_from_token (lexer, token)
924 cp_lexer *lexer ATTRIBUTE_UNUSED;
925 const cp_token *token;
927 /* Ideally, the source position information would not be a global
928 variable, but it is. */
930 /* Update the line number. */
931 if (token->type != CPP_EOF)
933 lineno = token->line_number;
934 input_filename = token->file_name;
938 /* Print a representation of the TOKEN on the STREAM. */
941 cp_lexer_print_token (stream, token)
945 const char *token_type = NULL;
947 /* Figure out what kind of token this is. */
955 token_type = "COMMA";
959 token_type = "OPEN_PAREN";
962 case CPP_CLOSE_PAREN:
963 token_type = "CLOSE_PAREN";
967 token_type = "OPEN_BRACE";
970 case CPP_CLOSE_BRACE:
971 token_type = "CLOSE_BRACE";
975 token_type = "SEMICOLON";
987 token_type = "keyword";
990 /* This is not a token that we know how to handle yet. */
995 /* If we have a name for the token, print it out. Otherwise, we
996 simply give the numeric code. */
998 fprintf (stream, "%s", token_type);
1000 fprintf (stream, "%d", token->type);
1001 /* And, for an identifier, print the identifier name. */
1002 if (token->type == CPP_NAME
1003 /* Some keywords have a value that is not an IDENTIFIER_NODE.
1004 For example, `struct' is mapped to an INTEGER_CST. */
1005 || (token->type == CPP_KEYWORD
1006 && TREE_CODE (token->value) == IDENTIFIER_NODE))
1007 fprintf (stream, " %s", IDENTIFIER_POINTER (token->value));
1010 /* Returns non-zero if debugging information should be output. */
1013 cp_lexer_debugging_p (lexer)
1016 return lexer->debugging_p;
1019 /* Start emitting debugging information. */
1022 cp_lexer_start_debugging (lexer)
1025 ++lexer->debugging_p;
1028 /* Stop emitting debugging information. */
1031 cp_lexer_stop_debugging (lexer)
1034 --lexer->debugging_p;
1043 A cp_parser parses the token stream as specified by the C++
1044 grammar. Its job is purely parsing, not semantic analysis. For
1045 example, the parser breaks the token stream into declarators,
1046 expressions, statements, and other similar syntactic constructs.
1047 It does not check that the types of the expressions on either side
1048 of an assignment-statement are compatible, or that a function is
1049 not declared with a parameter of type `void'.
1051 The parser invokes routines elsewhere in the compiler to perform
1052 semantic analysis and to build up the abstract syntax tree for the
1055 The parser (and the template instantiation code, which is, in a
1056 way, a close relative of parsing) are the only parts of the
1057 compiler that should be calling push_scope and pop_scope, or
1058 related functions. The parser (and template instantiation code)
1059 keeps track of what scope is presently active; everything else
1060 should simply honor that. (The code that generates static
1061 initializers may also need to set the scope, in order to check
1062 access control correctly when emitting the initializers.)
1067 The parser is of the standard recursive-descent variety. Upcoming
1068 tokens in the token stream are examined in order to determine which
1069 production to use when parsing a non-terminal. Some C++ constructs
1070 require arbitrary look ahead to disambiguate. For example, it is
1071 impossible, in the general case, to tell whether a statement is an
1072 expression or declaration without scanning the entire statement.
1073 Therefore, the parser is capable of "parsing tentatively." When the
1074 parser is not sure what construct comes next, it enters this mode.
1075 Then, while we attempt to parse the construct, the parser queues up
1076 error messages, rather than issuing them immediately, and saves the
1077 tokens it consumes. If the construct is parsed successfully, the
1078 parser "commits", i.e., it issues any queued error messages and
1079 the tokens that were being preserved are permanently discarded.
1080 If, however, the construct is not parsed successfully, the parser
1081 rolls back its state completely so that it can resume parsing using
1082 a different alternative.
1087 The performance of the parser could probably be improved
1088 substantially. Some possible improvements include:
1090 - The expression parser recurses through the various levels of
1091 precedence as specified in the grammar, rather than using an
1092 operator-precedence technique. Therefore, parsing a simple
1093 identifier requires multiple recursive calls.
1095 - We could often eliminate the need to parse tentatively by
1096 looking ahead a little bit. In some places, this approach
1097 might not entirely eliminate the need to parse tentatively, but
1098 it might still speed up the average case. */
1100 /* Flags that are passed to some parsing functions. These values can
1101 be bitwise-ored together. */
1103 typedef enum cp_parser_flags
1106 CP_PARSER_FLAGS_NONE = 0x0,
1107 /* The construct is optional. If it is not present, then no error
1108 should be issued. */
1109 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1110 /* When parsing a type-specifier, do not allow user-defined types. */
1111 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2
1114 /* The different kinds of ids that we ecounter. */
1116 typedef enum cp_parser_id_kind
1118 /* Not an id at all. */
1119 CP_PARSER_ID_KIND_NONE,
1120 /* An unqualified-id that is not a template-id. */
1121 CP_PARSER_ID_KIND_UNQUALIFIED,
1122 /* An unqualified template-id. */
1123 CP_PARSER_ID_KIND_TEMPLATE_ID,
1124 /* A qualified-id. */
1125 CP_PARSER_ID_KIND_QUALIFIED
1126 } cp_parser_id_kind;
1128 /* A mapping from a token type to a corresponding tree node type. */
1130 typedef struct cp_parser_token_tree_map_node
1132 /* The token type. */
1133 enum cpp_ttype token_type;
1134 /* The corresponding tree code. */
1135 enum tree_code tree_type;
1136 } cp_parser_token_tree_map_node;
1138 /* A complete map consists of several ordinary entries, followed by a
1139 terminator. The terminating entry has a token_type of CPP_EOF. */
1141 typedef cp_parser_token_tree_map_node cp_parser_token_tree_map[];
1143 /* The status of a tentative parse. */
1145 typedef enum cp_parser_status_kind
1147 /* No errors have occurred. */
1148 CP_PARSER_STATUS_KIND_NO_ERROR,
1149 /* An error has occurred. */
1150 CP_PARSER_STATUS_KIND_ERROR,
1151 /* We are committed to this tentative parse, whether or not an error
1153 CP_PARSER_STATUS_KIND_COMMITTED
1154 } cp_parser_status_kind;
1156 /* Context that is saved and restored when parsing tentatively. */
1158 typedef struct cp_parser_context GTY (())
1160 /* If this is a tentative parsing context, the status of the
1162 enum cp_parser_status_kind status;
1163 /* If non-NULL, we have just seen a `x->' or `x.' expression. Names
1164 that are looked up in this context must be looked up both in the
1165 scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1166 the context of the containing expression. */
1168 /* A TREE_LIST representing name-lookups for which we have deferred
1169 checking access controls. We cannot check the accessibility of
1170 names used in a decl-specifier-seq until we know what is being
1171 declared because code like:
1178 A::B* A::f() { return 0; }
1180 is valid, even though `A::B' is not generally accessible.
1182 The TREE_PURPOSE of each node is the scope used to qualify the
1183 name being looked up; the TREE_VALUE is the DECL to which the
1184 name was resolved. */
1185 tree deferred_access_checks;
1186 /* TRUE iff we are deferring access checks. */
1187 bool deferring_access_checks_p;
1188 /* The next parsing context in the stack. */
1189 struct cp_parser_context *next;
1190 } cp_parser_context;
1194 /* Constructors and destructors. */
1196 static cp_parser_context *cp_parser_context_new
1197 PARAMS ((cp_parser_context *));
1199 /* Class variables. */
1201 static GTY((deletable (""))) cp_parser_context* cp_parser_context_free_list;
1203 /* Constructors and destructors. */
1205 /* Construct a new context. The context below this one on the stack
1206 is given by NEXT. */
1208 static cp_parser_context *
1209 cp_parser_context_new (next)
1210 cp_parser_context *next;
1212 cp_parser_context *context;
1214 /* Allocate the storage. */
1215 if (cp_parser_context_free_list != NULL)
1217 /* Pull the first entry from the free list. */
1218 context = cp_parser_context_free_list;
1219 cp_parser_context_free_list = context->next;
1220 memset ((char *)context, 0, sizeof (*context));
1223 context = ((cp_parser_context *)
1224 ggc_alloc_cleared (sizeof (cp_parser_context)));
1225 /* No errors have occurred yet in this context. */
1226 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1227 /* If this is not the bottomost context, copy information that we
1228 need from the previous context. */
1231 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1232 expression, then we are parsing one in this context, too. */
1233 context->object_type = next->object_type;
1234 /* We are deferring access checks here if we were in the NEXT
1236 context->deferring_access_checks_p
1237 = next->deferring_access_checks_p;
1238 /* Thread the stack. */
1239 context->next = next;
1245 /* The cp_parser structure represents the C++ parser. */
1247 typedef struct cp_parser GTY(())
1249 /* The lexer from which we are obtaining tokens. */
1252 /* The scope in which names should be looked up. If NULL_TREE, then
1253 we look up names in the scope that is currently open in the
1254 source program. If non-NULL, this is either a TYPE or
1255 NAMESPACE_DECL for the scope in which we should look.
1257 This value is not cleared automatically after a name is looked
1258 up, so we must be careful to clear it before starting a new look
1259 up sequence. (If it is not cleared, then `X::Y' followed by `Z'
1260 will look up `Z' in the scope of `X', rather than the current
1261 scope.) Unfortunately, it is difficult to tell when name lookup
1262 is complete, because we sometimes peek at a token, look it up,
1263 and then decide not to consume it. */
1266 /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1267 last lookup took place. OBJECT_SCOPE is used if an expression
1268 like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1269 respectively. QUALIFYING_SCOPE is used for an expression of the
1270 form "X::Y"; it refers to X. */
1272 tree qualifying_scope;
1274 /* A stack of parsing contexts. All but the bottom entry on the
1275 stack will be tentative contexts.
1277 We parse tentatively in order to determine which construct is in
1278 use in some situations. For example, in order to determine
1279 whether a statement is an expression-statement or a
1280 declaration-statement we parse it tentatively as a
1281 declaration-statement. If that fails, we then reparse the same
1282 token stream as an expression-statement. */
1283 cp_parser_context *context;
1285 /* True if we are parsing GNU C++. If this flag is not set, then
1286 GNU extensions are not recognized. */
1287 bool allow_gnu_extensions_p;
1289 /* TRUE if the `>' token should be interpreted as the greater-than
1290 operator. FALSE if it is the end of a template-id or
1291 template-parameter-list. */
1292 bool greater_than_is_operator_p;
1294 /* TRUE if default arguments are allowed within a parameter list
1295 that starts at this point. FALSE if only a gnu extension makes
1296 them permissable. */
1297 bool default_arg_ok_p;
1299 /* TRUE if we are parsing an integral constant-expression. See
1300 [expr.const] for a precise definition. */
1301 /* FIXME: Need to implement code that checks this flag. */
1302 bool constant_expression_p;
1304 /* TRUE if local variable names and `this' are forbidden in the
1306 bool local_variables_forbidden_p;
1308 /* TRUE if the declaration we are parsing is part of a
1309 linkage-specification of the form `extern string-literal
1311 bool in_unbraced_linkage_specification_p;
1313 /* TRUE if we are presently parsing a declarator, after the
1314 direct-declarator. */
1315 bool in_declarator_p;
1317 /* If non-NULL, then we are parsing a construct where new type
1318 definitions are not permitted. The string stored here will be
1319 issued as an error message if a type is defined. */
1320 const char *type_definition_forbidden_message;
1322 /* List of FUNCTION_TYPEs which contain unprocessed DEFAULT_ARGs
1323 during class parsing, and are not FUNCTION_DECLs. G++ has an
1324 awkward extension allowing default args on pointers to functions
1326 tree default_arg_types;
1328 /* A TREE_LIST of queues of functions whose bodies have been lexed,
1329 but may not have been parsed. These functions are friends of
1330 members defined within a class-specification; they are not
1331 procssed until the class is complete. The active queue is at the
1334 Within each queue, functions appear in the reverse order that
1335 they appeared in the source. The TREE_PURPOSE of each node is
1336 the class in which the function was defined or declared; the
1337 TREE_VALUE is the FUNCTION_DECL itself. */
1338 tree unparsed_functions_queues;
1340 /* The number of classes whose definitions are currently in
1342 unsigned num_classes_being_defined;
1344 /* The number of template parameter lists that apply directly to the
1345 current declaration. */
1346 unsigned num_template_parameter_lists;
1349 /* The type of a function that parses some kind of expression */
1350 typedef tree (*cp_parser_expression_fn) PARAMS ((cp_parser *));
1354 /* Constructors and destructors. */
1356 static cp_parser *cp_parser_new
1359 /* Routines to parse various constructs.
1361 Those that return `tree' will return the error_mark_node (rather
1362 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1363 Sometimes, they will return an ordinary node if error-recovery was
1364 attempted, even though a parse error occurrred. So, to check
1365 whether or not a parse error occurred, you should always use
1366 cp_parser_error_occurred. If the construct is optional (indicated
1367 either by an `_opt' in the name of the function that does the
1368 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1369 the construct is not present. */
1371 /* Lexical conventions [gram.lex] */
1373 static tree cp_parser_identifier
1374 PARAMS ((cp_parser *));
1376 /* Basic concepts [gram.basic] */
1378 static bool cp_parser_translation_unit
1379 PARAMS ((cp_parser *));
1381 /* Expressions [gram.expr] */
1383 static tree cp_parser_primary_expression
1384 (cp_parser *, cp_parser_id_kind *, tree *);
1385 static tree cp_parser_id_expression
1386 PARAMS ((cp_parser *, bool, bool, bool *));
1387 static tree cp_parser_unqualified_id
1388 PARAMS ((cp_parser *, bool, bool));
1389 static tree cp_parser_nested_name_specifier_opt
1390 (cp_parser *, bool, bool, bool);
1391 static tree cp_parser_nested_name_specifier
1392 (cp_parser *, bool, bool, bool);
1393 static tree cp_parser_class_or_namespace_name
1394 (cp_parser *, bool, bool, bool, bool);
1395 static tree cp_parser_postfix_expression
1396 (cp_parser *, bool);
1397 static tree cp_parser_expression_list
1398 PARAMS ((cp_parser *));
1399 static void cp_parser_pseudo_destructor_name
1400 PARAMS ((cp_parser *, tree *, tree *));
1401 static tree cp_parser_unary_expression
1402 (cp_parser *, bool);
1403 static enum tree_code cp_parser_unary_operator
1404 PARAMS ((cp_token *));
1405 static tree cp_parser_new_expression
1406 PARAMS ((cp_parser *));
1407 static tree cp_parser_new_placement
1408 PARAMS ((cp_parser *));
1409 static tree cp_parser_new_type_id
1410 PARAMS ((cp_parser *));
1411 static tree cp_parser_new_declarator_opt
1412 PARAMS ((cp_parser *));
1413 static tree cp_parser_direct_new_declarator
1414 PARAMS ((cp_parser *));
1415 static tree cp_parser_new_initializer
1416 PARAMS ((cp_parser *));
1417 static tree cp_parser_delete_expression
1418 PARAMS ((cp_parser *));
1419 static tree cp_parser_cast_expression
1420 (cp_parser *, bool);
1421 static tree cp_parser_pm_expression
1422 PARAMS ((cp_parser *));
1423 static tree cp_parser_multiplicative_expression
1424 PARAMS ((cp_parser *));
1425 static tree cp_parser_additive_expression
1426 PARAMS ((cp_parser *));
1427 static tree cp_parser_shift_expression
1428 PARAMS ((cp_parser *));
1429 static tree cp_parser_relational_expression
1430 PARAMS ((cp_parser *));
1431 static tree cp_parser_equality_expression
1432 PARAMS ((cp_parser *));
1433 static tree cp_parser_and_expression
1434 PARAMS ((cp_parser *));
1435 static tree cp_parser_exclusive_or_expression
1436 PARAMS ((cp_parser *));
1437 static tree cp_parser_inclusive_or_expression
1438 PARAMS ((cp_parser *));
1439 static tree cp_parser_logical_and_expression
1440 PARAMS ((cp_parser *));
1441 static tree cp_parser_logical_or_expression
1442 PARAMS ((cp_parser *));
1443 static tree cp_parser_conditional_expression
1444 PARAMS ((cp_parser *));
1445 static tree cp_parser_question_colon_clause
1446 PARAMS ((cp_parser *, tree));
1447 static tree cp_parser_assignment_expression
1448 PARAMS ((cp_parser *));
1449 static enum tree_code cp_parser_assignment_operator_opt
1450 PARAMS ((cp_parser *));
1451 static tree cp_parser_expression
1452 PARAMS ((cp_parser *));
1453 static tree cp_parser_constant_expression
1454 PARAMS ((cp_parser *));
1456 /* Statements [gram.stmt.stmt] */
1458 static void cp_parser_statement
1459 PARAMS ((cp_parser *));
1460 static tree cp_parser_labeled_statement
1461 PARAMS ((cp_parser *));
1462 static tree cp_parser_expression_statement
1463 PARAMS ((cp_parser *));
1464 static tree cp_parser_compound_statement
1466 static void cp_parser_statement_seq_opt
1467 PARAMS ((cp_parser *));
1468 static tree cp_parser_selection_statement
1469 PARAMS ((cp_parser *));
1470 static tree cp_parser_condition
1471 PARAMS ((cp_parser *));
1472 static tree cp_parser_iteration_statement
1473 PARAMS ((cp_parser *));
1474 static void cp_parser_for_init_statement
1475 PARAMS ((cp_parser *));
1476 static tree cp_parser_jump_statement
1477 PARAMS ((cp_parser *));
1478 static void cp_parser_declaration_statement
1479 PARAMS ((cp_parser *));
1481 static tree cp_parser_implicitly_scoped_statement
1482 PARAMS ((cp_parser *));
1483 static void cp_parser_already_scoped_statement
1484 PARAMS ((cp_parser *));
1486 /* Declarations [gram.dcl.dcl] */
1488 static void cp_parser_declaration_seq_opt
1489 PARAMS ((cp_parser *));
1490 static void cp_parser_declaration
1491 PARAMS ((cp_parser *));
1492 static void cp_parser_block_declaration
1493 PARAMS ((cp_parser *, bool));
1494 static void cp_parser_simple_declaration
1495 PARAMS ((cp_parser *, bool));
1496 static tree cp_parser_decl_specifier_seq
1497 PARAMS ((cp_parser *, cp_parser_flags, tree *, bool *));
1498 static tree cp_parser_storage_class_specifier_opt
1499 PARAMS ((cp_parser *));
1500 static tree cp_parser_function_specifier_opt
1501 PARAMS ((cp_parser *));
1502 static tree cp_parser_type_specifier
1503 (cp_parser *, cp_parser_flags, bool, bool, bool *, bool *);
1504 static tree cp_parser_simple_type_specifier
1505 PARAMS ((cp_parser *, cp_parser_flags));
1506 static tree cp_parser_type_name
1507 PARAMS ((cp_parser *));
1508 static tree cp_parser_elaborated_type_specifier
1509 PARAMS ((cp_parser *, bool, bool));
1510 static tree cp_parser_enum_specifier
1511 PARAMS ((cp_parser *));
1512 static void cp_parser_enumerator_list
1513 PARAMS ((cp_parser *, tree));
1514 static void cp_parser_enumerator_definition
1515 PARAMS ((cp_parser *, tree));
1516 static tree cp_parser_namespace_name
1517 PARAMS ((cp_parser *));
1518 static void cp_parser_namespace_definition
1519 PARAMS ((cp_parser *));
1520 static void cp_parser_namespace_body
1521 PARAMS ((cp_parser *));
1522 static tree cp_parser_qualified_namespace_specifier
1523 PARAMS ((cp_parser *));
1524 static void cp_parser_namespace_alias_definition
1525 PARAMS ((cp_parser *));
1526 static void cp_parser_using_declaration
1527 PARAMS ((cp_parser *));
1528 static void cp_parser_using_directive
1529 PARAMS ((cp_parser *));
1530 static void cp_parser_asm_definition
1531 PARAMS ((cp_parser *));
1532 static void cp_parser_linkage_specification
1533 PARAMS ((cp_parser *));
1535 /* Declarators [gram.dcl.decl] */
1537 static tree cp_parser_init_declarator
1538 PARAMS ((cp_parser *, tree, tree, tree, bool, bool, bool *));
1539 static tree cp_parser_declarator
1540 PARAMS ((cp_parser *, bool, bool *));
1541 static tree cp_parser_direct_declarator
1542 PARAMS ((cp_parser *, bool, bool *));
1543 static enum tree_code cp_parser_ptr_operator
1544 PARAMS ((cp_parser *, tree *, tree *));
1545 static tree cp_parser_cv_qualifier_seq_opt
1546 PARAMS ((cp_parser *));
1547 static tree cp_parser_cv_qualifier_opt
1548 PARAMS ((cp_parser *));
1549 static tree cp_parser_declarator_id
1550 PARAMS ((cp_parser *));
1551 static tree cp_parser_type_id
1552 PARAMS ((cp_parser *));
1553 static tree cp_parser_type_specifier_seq
1554 PARAMS ((cp_parser *));
1555 static tree cp_parser_parameter_declaration_clause
1556 PARAMS ((cp_parser *));
1557 static tree cp_parser_parameter_declaration_list
1558 PARAMS ((cp_parser *));
1559 static tree cp_parser_parameter_declaration
1560 PARAMS ((cp_parser *, bool));
1561 static tree cp_parser_function_definition
1562 PARAMS ((cp_parser *, bool *));
1563 static void cp_parser_function_body
1565 static tree cp_parser_initializer
1566 PARAMS ((cp_parser *, bool *));
1567 static tree cp_parser_initializer_clause
1568 PARAMS ((cp_parser *));
1569 static tree cp_parser_initializer_list
1570 PARAMS ((cp_parser *));
1572 static bool cp_parser_ctor_initializer_opt_and_function_body
1575 /* Classes [gram.class] */
1577 static tree cp_parser_class_name
1578 (cp_parser *, bool, bool, bool, bool, bool, bool);
1579 static tree cp_parser_class_specifier
1580 PARAMS ((cp_parser *));
1581 static tree cp_parser_class_head
1582 PARAMS ((cp_parser *, bool *, bool *, tree *));
1583 static enum tag_types cp_parser_class_key
1584 PARAMS ((cp_parser *));
1585 static void cp_parser_member_specification_opt
1586 PARAMS ((cp_parser *));
1587 static void cp_parser_member_declaration
1588 PARAMS ((cp_parser *));
1589 static tree cp_parser_pure_specifier
1590 PARAMS ((cp_parser *));
1591 static tree cp_parser_constant_initializer
1592 PARAMS ((cp_parser *));
1594 /* Derived classes [gram.class.derived] */
1596 static tree cp_parser_base_clause
1597 PARAMS ((cp_parser *));
1598 static tree cp_parser_base_specifier
1599 PARAMS ((cp_parser *));
1601 /* Special member functions [gram.special] */
1603 static tree cp_parser_conversion_function_id
1604 PARAMS ((cp_parser *));
1605 static tree cp_parser_conversion_type_id
1606 PARAMS ((cp_parser *));
1607 static tree cp_parser_conversion_declarator_opt
1608 PARAMS ((cp_parser *));
1609 static bool cp_parser_ctor_initializer_opt
1610 PARAMS ((cp_parser *));
1611 static void cp_parser_mem_initializer_list
1612 PARAMS ((cp_parser *));
1613 static tree cp_parser_mem_initializer
1614 PARAMS ((cp_parser *));
1615 static tree cp_parser_mem_initializer_id
1616 PARAMS ((cp_parser *));
1618 /* Overloading [gram.over] */
1620 static tree cp_parser_operator_function_id
1621 PARAMS ((cp_parser *));
1622 static tree cp_parser_operator
1623 PARAMS ((cp_parser *));
1625 /* Templates [gram.temp] */
1627 static void cp_parser_template_declaration
1628 PARAMS ((cp_parser *, bool));
1629 static tree cp_parser_template_parameter_list
1630 PARAMS ((cp_parser *));
1631 static tree cp_parser_template_parameter
1632 PARAMS ((cp_parser *));
1633 static tree cp_parser_type_parameter
1634 PARAMS ((cp_parser *));
1635 static tree cp_parser_template_id
1636 PARAMS ((cp_parser *, bool, bool));
1637 static tree cp_parser_template_name
1638 PARAMS ((cp_parser *, bool, bool));
1639 static tree cp_parser_template_argument_list
1640 PARAMS ((cp_parser *));
1641 static tree cp_parser_template_argument
1642 PARAMS ((cp_parser *));
1643 static void cp_parser_explicit_instantiation
1644 PARAMS ((cp_parser *));
1645 static void cp_parser_explicit_specialization
1646 PARAMS ((cp_parser *));
1648 /* Exception handling [gram.exception] */
1650 static tree cp_parser_try_block
1651 PARAMS ((cp_parser *));
1652 static bool cp_parser_function_try_block
1653 PARAMS ((cp_parser *));
1654 static void cp_parser_handler_seq
1655 PARAMS ((cp_parser *));
1656 static void cp_parser_handler
1657 PARAMS ((cp_parser *));
1658 static tree cp_parser_exception_declaration
1659 PARAMS ((cp_parser *));
1660 static tree cp_parser_throw_expression
1661 PARAMS ((cp_parser *));
1662 static tree cp_parser_exception_specification_opt
1663 PARAMS ((cp_parser *));
1664 static tree cp_parser_type_id_list
1665 PARAMS ((cp_parser *));
1667 /* GNU Extensions */
1669 static tree cp_parser_asm_specification_opt
1670 PARAMS ((cp_parser *));
1671 static tree cp_parser_asm_operand_list
1672 PARAMS ((cp_parser *));
1673 static tree cp_parser_asm_clobber_list
1674 PARAMS ((cp_parser *));
1675 static tree cp_parser_attributes_opt
1676 PARAMS ((cp_parser *));
1677 static tree cp_parser_attribute_list
1678 PARAMS ((cp_parser *));
1679 static bool cp_parser_extension_opt
1680 PARAMS ((cp_parser *, int *));
1681 static void cp_parser_label_declaration
1682 PARAMS ((cp_parser *));
1684 /* Utility Routines */
1686 static tree cp_parser_lookup_name
1687 PARAMS ((cp_parser *, tree, bool, bool, bool, bool));
1688 static tree cp_parser_lookup_name_simple
1689 PARAMS ((cp_parser *, tree));
1690 static tree cp_parser_resolve_typename_type
1691 PARAMS ((cp_parser *, tree));
1692 static tree cp_parser_maybe_treat_template_as_class
1694 static bool cp_parser_check_declarator_template_parameters
1695 PARAMS ((cp_parser *, tree));
1696 static bool cp_parser_check_template_parameters
1697 PARAMS ((cp_parser *, unsigned));
1698 static tree cp_parser_binary_expression
1699 PARAMS ((cp_parser *,
1700 cp_parser_token_tree_map,
1701 cp_parser_expression_fn));
1702 static tree cp_parser_global_scope_opt
1703 PARAMS ((cp_parser *, bool));
1704 static bool cp_parser_constructor_declarator_p
1705 (cp_parser *, bool);
1706 static tree cp_parser_function_definition_from_specifiers_and_declarator
1707 PARAMS ((cp_parser *, tree, tree, tree, tree));
1708 static tree cp_parser_function_definition_after_declarator
1709 PARAMS ((cp_parser *, bool));
1710 static void cp_parser_template_declaration_after_export
1711 PARAMS ((cp_parser *, bool));
1712 static tree cp_parser_single_declaration
1713 PARAMS ((cp_parser *, bool, bool *));
1714 static tree cp_parser_functional_cast
1715 PARAMS ((cp_parser *, tree));
1716 static void cp_parser_late_parsing_for_member
1717 PARAMS ((cp_parser *, tree));
1718 static void cp_parser_late_parsing_default_args
1719 (cp_parser *, tree, tree);
1720 static tree cp_parser_sizeof_operand
1721 PARAMS ((cp_parser *, enum rid));
1722 static bool cp_parser_declares_only_class_p
1723 PARAMS ((cp_parser *));
1724 static bool cp_parser_friend_p
1726 static cp_token *cp_parser_require
1727 PARAMS ((cp_parser *, enum cpp_ttype, const char *));
1728 static cp_token *cp_parser_require_keyword
1729 PARAMS ((cp_parser *, enum rid, const char *));
1730 static bool cp_parser_token_starts_function_definition_p
1731 PARAMS ((cp_token *));
1732 static bool cp_parser_next_token_starts_class_definition_p
1734 static enum tag_types cp_parser_token_is_class_key
1735 PARAMS ((cp_token *));
1736 static void cp_parser_check_class_key
1737 (enum tag_types, tree type);
1738 static bool cp_parser_optional_template_keyword
1740 static void cp_parser_cache_group
1741 (cp_parser *, cp_token_cache *, enum cpp_ttype, unsigned);
1742 static void cp_parser_parse_tentatively
1743 PARAMS ((cp_parser *));
1744 static void cp_parser_commit_to_tentative_parse
1745 PARAMS ((cp_parser *));
1746 static void cp_parser_abort_tentative_parse
1747 PARAMS ((cp_parser *));
1748 static bool cp_parser_parse_definitely
1749 PARAMS ((cp_parser *));
1750 static bool cp_parser_parsing_tentatively
1751 PARAMS ((cp_parser *));
1752 static bool cp_parser_committed_to_tentative_parse
1753 PARAMS ((cp_parser *));
1754 static void cp_parser_error
1755 PARAMS ((cp_parser *, const char *));
1756 static bool cp_parser_simulate_error
1757 PARAMS ((cp_parser *));
1758 static void cp_parser_check_type_definition
1759 PARAMS ((cp_parser *));
1760 static bool cp_parser_skip_to_closing_parenthesis
1761 PARAMS ((cp_parser *));
1762 static bool cp_parser_skip_to_closing_parenthesis_or_comma
1764 static void cp_parser_skip_to_end_of_statement
1765 PARAMS ((cp_parser *));
1766 static void cp_parser_skip_to_end_of_block_or_statement
1767 PARAMS ((cp_parser *));
1768 static void cp_parser_skip_to_closing_brace
1770 static void cp_parser_skip_until_found
1771 PARAMS ((cp_parser *, enum cpp_ttype, const char *));
1772 static bool cp_parser_error_occurred
1773 PARAMS ((cp_parser *));
1774 static bool cp_parser_allow_gnu_extensions_p
1775 PARAMS ((cp_parser *));
1776 static bool cp_parser_is_string_literal
1777 PARAMS ((cp_token *));
1778 static bool cp_parser_is_keyword
1779 PARAMS ((cp_token *, enum rid));
1780 static bool cp_parser_dependent_type_p
1782 static bool cp_parser_value_dependent_expression_p
1784 static bool cp_parser_type_dependent_expression_p
1786 static bool cp_parser_dependent_template_arg_p
1788 static bool cp_parser_dependent_template_id_p
1790 static bool cp_parser_dependent_template_p
1792 static void cp_parser_defer_access_check
1793 (cp_parser *, tree, tree);
1794 static void cp_parser_start_deferring_access_checks
1796 static tree cp_parser_stop_deferring_access_checks
1797 PARAMS ((cp_parser *));
1798 static void cp_parser_perform_deferred_access_checks
1800 static tree cp_parser_scope_through_which_access_occurs
1803 /* Returns non-zero if TOKEN is a string literal. */
1806 cp_parser_is_string_literal (token)
1809 return (token->type == CPP_STRING || token->type == CPP_WSTRING);
1812 /* Returns non-zero if TOKEN is the indicated KEYWORD. */
1815 cp_parser_is_keyword (token, keyword)
1819 return token->keyword == keyword;
1822 /* Returns TRUE if TYPE is dependent, in the sense of
1826 cp_parser_dependent_type_p (type)
1831 if (!processing_template_decl)
1834 /* If the type is NULL, we have not computed a type for the entity
1835 in question; in that case, the type is dependent. */
1839 /* Erroneous types can be considered non-dependent. */
1840 if (type == error_mark_node)
1845 A type is dependent if it is:
1847 -- a template parameter. */
1848 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
1850 /* -- a qualified-id with a nested-name-specifier which contains a
1851 class-name that names a dependent type or whose unqualified-id
1852 names a dependent type. */
1853 if (TREE_CODE (type) == TYPENAME_TYPE)
1855 /* -- a cv-qualified type where the cv-unqualified type is
1857 type = TYPE_MAIN_VARIANT (type);
1858 /* -- a compound type constructed from any dependent type. */
1859 if (TYPE_PTRMEM_P (type) || TYPE_PTRMEMFUNC_P (type))
1860 return (cp_parser_dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type))
1861 || cp_parser_dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
1863 else if (TREE_CODE (type) == POINTER_TYPE
1864 || TREE_CODE (type) == REFERENCE_TYPE)
1865 return cp_parser_dependent_type_p (TREE_TYPE (type));
1866 else if (TREE_CODE (type) == FUNCTION_TYPE
1867 || TREE_CODE (type) == METHOD_TYPE)
1871 if (cp_parser_dependent_type_p (TREE_TYPE (type)))
1873 for (arg_type = TYPE_ARG_TYPES (type);
1875 arg_type = TREE_CHAIN (arg_type))
1876 if (cp_parser_dependent_type_p (TREE_VALUE (arg_type)))
1880 /* -- an array type constructed from any dependent type or whose
1881 size is specified by a constant expression that is
1883 if (TREE_CODE (type) == ARRAY_TYPE)
1885 if (TYPE_DOMAIN (type)
1886 && ((cp_parser_value_dependent_expression_p
1887 (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
1888 || (cp_parser_type_dependent_expression_p
1889 (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))))
1891 return cp_parser_dependent_type_p (TREE_TYPE (type));
1893 /* -- a template-id in which either the template name is a template
1894 parameter or any of the template arguments is a dependent type or
1895 an expression that is type-dependent or value-dependent.
1897 This language seems somewhat confused; for example, it does not
1898 discuss template template arguments. Therefore, we use the
1899 definition for dependent template arguments in [temp.dep.temp]. */
1900 if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INFO (type)
1901 && (cp_parser_dependent_template_id_p
1902 (CLASSTYPE_TI_TEMPLATE (type),
1903 CLASSTYPE_TI_ARGS (type))))
1905 else if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
1907 /* All TYPEOF_TYPEs are dependent; if the argument of the `typeof'
1908 expression is not type-dependent, then it should already been
1910 if (TREE_CODE (type) == TYPEOF_TYPE)
1912 /* The standard does not specifically mention types that are local
1913 to template functions or local classes, but they should be
1914 considered dependent too. For example:
1916 template <int I> void f() {
1921 The size of `E' cannot be known until the value of `I' has been
1922 determined. Therefore, `E' must be considered dependent. */
1923 scope = TYPE_CONTEXT (type);
1924 if (scope && TYPE_P (scope))
1925 return cp_parser_dependent_type_p (scope);
1926 else if (scope && TREE_CODE (scope) == FUNCTION_DECL)
1927 return cp_parser_type_dependent_expression_p (scope);
1929 /* Other types are non-dependent. */
1933 /* Returns TRUE if the EXPRESSION is value-dependent. */
1936 cp_parser_value_dependent_expression_p (tree expression)
1938 if (!processing_template_decl)
1941 /* A name declared with a dependent type. */
1942 if (DECL_P (expression)
1943 && cp_parser_dependent_type_p (TREE_TYPE (expression)))
1945 /* A non-type template parameter. */
1946 if ((TREE_CODE (expression) == CONST_DECL
1947 && DECL_TEMPLATE_PARM_P (expression))
1948 || TREE_CODE (expression) == TEMPLATE_PARM_INDEX)
1950 /* A constant with integral or enumeration type and is initialized
1951 with an expression that is value-dependent. */
1952 if (TREE_CODE (expression) == VAR_DECL
1953 && DECL_INITIAL (expression)
1954 && (CP_INTEGRAL_TYPE_P (TREE_TYPE (expression))
1955 || TREE_CODE (TREE_TYPE (expression)) == ENUMERAL_TYPE)
1956 && cp_parser_value_dependent_expression_p (DECL_INITIAL (expression)))
1958 /* These expressions are value-dependent if the type to which the
1959 cast occurs is dependent. */
1960 if ((TREE_CODE (expression) == DYNAMIC_CAST_EXPR
1961 || TREE_CODE (expression) == STATIC_CAST_EXPR
1962 || TREE_CODE (expression) == CONST_CAST_EXPR
1963 || TREE_CODE (expression) == REINTERPRET_CAST_EXPR
1964 || TREE_CODE (expression) == CAST_EXPR)
1965 && cp_parser_dependent_type_p (TREE_TYPE (expression)))
1967 /* A `sizeof' expression where the sizeof operand is a type is
1968 value-dependent if the type is dependent. If the type was not
1969 dependent, we would no longer have a SIZEOF_EXPR, so any
1970 SIZEOF_EXPR is dependent. */
1971 if (TREE_CODE (expression) == SIZEOF_EXPR)
1973 /* A constant expression is value-dependent if any subexpression is
1975 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (expression))))
1977 switch (TREE_CODE_CLASS (TREE_CODE (expression)))
1980 return (cp_parser_value_dependent_expression_p
1981 (TREE_OPERAND (expression, 0)));
1984 return ((cp_parser_value_dependent_expression_p
1985 (TREE_OPERAND (expression, 0)))
1986 || (cp_parser_value_dependent_expression_p
1987 (TREE_OPERAND (expression, 1))));
1992 i < TREE_CODE_LENGTH (TREE_CODE (expression));
1994 if (cp_parser_value_dependent_expression_p
1995 (TREE_OPERAND (expression, i)))
2002 /* The expression is not value-dependent. */
2006 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
2010 cp_parser_type_dependent_expression_p (expression)
2013 if (!processing_template_decl)
2016 /* Some expression forms are never type-dependent. */
2017 if (TREE_CODE (expression) == PSEUDO_DTOR_EXPR
2018 || TREE_CODE (expression) == SIZEOF_EXPR
2019 || TREE_CODE (expression) == ALIGNOF_EXPR
2020 || TREE_CODE (expression) == TYPEID_EXPR
2021 || TREE_CODE (expression) == DELETE_EXPR
2022 || TREE_CODE (expression) == VEC_DELETE_EXPR
2023 || TREE_CODE (expression) == THROW_EXPR)
2026 /* The types of these expressions depends only on the type to which
2028 if (TREE_CODE (expression) == DYNAMIC_CAST_EXPR
2029 || TREE_CODE (expression) == STATIC_CAST_EXPR
2030 || TREE_CODE (expression) == CONST_CAST_EXPR
2031 || TREE_CODE (expression) == REINTERPRET_CAST_EXPR
2032 || TREE_CODE (expression) == CAST_EXPR)
2033 return cp_parser_dependent_type_p (TREE_TYPE (expression));
2034 /* The types of these expressions depends only on the type created
2035 by the expression. */
2036 else if (TREE_CODE (expression) == NEW_EXPR
2037 || TREE_CODE (expression) == VEC_NEW_EXPR)
2038 return cp_parser_dependent_type_p (TREE_OPERAND (expression, 1));
2040 if (TREE_CODE (expression) == FUNCTION_DECL
2041 && DECL_LANG_SPECIFIC (expression)
2042 && DECL_TEMPLATE_INFO (expression)
2043 && (cp_parser_dependent_template_id_p
2044 (DECL_TI_TEMPLATE (expression),
2045 INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression)))))
2048 return (cp_parser_dependent_type_p (TREE_TYPE (expression)));
2051 /* Returns TRUE if the ARG (a template argument) is dependent. */
2054 cp_parser_dependent_template_arg_p (tree arg)
2056 if (!processing_template_decl)
2059 if (TREE_CODE (arg) == TEMPLATE_DECL
2060 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
2061 return cp_parser_dependent_template_p (arg);
2062 else if (TYPE_P (arg))
2063 return cp_parser_dependent_type_p (arg);
2065 return (cp_parser_type_dependent_expression_p (arg)
2066 || cp_parser_value_dependent_expression_p (arg));
2069 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
2072 cp_parser_dependent_template_id_p (tree tmpl, tree args)
2076 if (cp_parser_dependent_template_p (tmpl))
2078 for (i = 0; i < TREE_VEC_LENGTH (args); ++i)
2079 if (cp_parser_dependent_template_arg_p (TREE_VEC_ELT (args, i)))
2084 /* Returns TRUE if the template TMPL is dependent. */
2087 cp_parser_dependent_template_p (tree tmpl)
2089 /* Template template parameters are dependent. */
2090 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)
2091 || TREE_CODE (tmpl) == TEMPLATE_TEMPLATE_PARM)
2093 /* So are member templates of dependent classes. */
2094 if (TYPE_P (CP_DECL_CONTEXT (tmpl)))
2095 return cp_parser_dependent_type_p (DECL_CONTEXT (tmpl));
2099 /* Defer checking the accessibility of DECL, when looked up in
2103 cp_parser_defer_access_check (cp_parser *parser,
2109 /* If we are not supposed to defer access checks, just check now. */
2110 if (!parser->context->deferring_access_checks_p)
2112 enforce_access (class_type, decl);
2116 /* See if we are already going to perform this check. */
2117 for (check = parser->context->deferred_access_checks;
2119 check = TREE_CHAIN (check))
2120 if (TREE_VALUE (check) == decl
2121 && same_type_p (TREE_PURPOSE (check), class_type))
2123 /* If not, record the check. */
2124 parser->context->deferred_access_checks
2125 = tree_cons (class_type, decl, parser->context->deferred_access_checks);
2128 /* Start deferring access control checks. */
2131 cp_parser_start_deferring_access_checks (cp_parser *parser)
2133 parser->context->deferring_access_checks_p = true;
2136 /* Stop deferring access control checks. Returns a TREE_LIST
2137 representing the deferred checks. The TREE_PURPOSE of each node is
2138 the type through which the access occurred; the TREE_VALUE is the
2139 declaration named. */
2142 cp_parser_stop_deferring_access_checks (parser)
2147 parser->context->deferring_access_checks_p = false;
2148 access_checks = parser->context->deferred_access_checks;
2149 parser->context->deferred_access_checks = NULL_TREE;
2151 return access_checks;
2154 /* Perform the deferred ACCESS_CHECKS, whose representation is as
2155 documented with cp_parser_stop_deferrring_access_checks. */
2158 cp_parser_perform_deferred_access_checks (access_checks)
2161 tree deferred_check;
2163 /* Look through all the deferred checks. */
2164 for (deferred_check = access_checks;
2166 deferred_check = TREE_CHAIN (deferred_check))
2168 enforce_access (TREE_PURPOSE (deferred_check),
2169 TREE_VALUE (deferred_check));
2172 /* Returns the scope through which DECL is being accessed, or
2173 NULL_TREE if DECL is not a member. If OBJECT_TYPE is non-NULL, we
2174 have just seen `x->' or `x.' and OBJECT_TYPE is the type of `*x',
2175 or `x', respectively. If the DECL was named as `A::B' then
2176 NESTED_NAME_SPECIFIER is `A'. */
2179 cp_parser_scope_through_which_access_occurs (decl,
2181 nested_name_specifier)
2184 tree nested_name_specifier;
2187 tree qualifying_type = NULL_TREE;
2189 /* Determine the SCOPE of DECL. */
2190 scope = context_for_name_lookup (decl);
2191 /* If the SCOPE is not a type, then DECL is not a member. */
2192 if (!TYPE_P (scope))
2194 /* Figure out the type through which DECL is being accessed. */
2195 if (object_type && DERIVED_FROM_P (scope, object_type))
2196 /* If we are processing a `->' or `.' expression, use the type of the
2198 qualifying_type = object_type;
2199 else if (nested_name_specifier)
2201 /* If the reference is to a non-static member of the
2202 current class, treat it as if it were referenced through
2204 if (DECL_NONSTATIC_MEMBER_P (decl)
2205 && current_class_ptr
2206 && DERIVED_FROM_P (scope, current_class_type))
2207 qualifying_type = current_class_type;
2208 /* Otherwise, use the type indicated by the
2209 nested-name-specifier. */
2211 qualifying_type = nested_name_specifier;
2214 /* Otherwise, the name must be from the current class or one of
2216 qualifying_type = currently_open_derived_class (scope);
2218 return qualifying_type;
2221 /* Issue the indicated error MESSAGE. */
2224 cp_parser_error (parser, message)
2226 const char *message;
2228 /* Output the MESSAGE -- unless we're parsing tentatively. */
2229 if (!cp_parser_simulate_error (parser))
2233 /* If we are parsing tentatively, remember that an error has occurred
2234 during this tentative parse. Returns true if the error was
2235 simulated; false if a messgae should be issued by the caller. */
2238 cp_parser_simulate_error (parser)
2241 if (cp_parser_parsing_tentatively (parser)
2242 && !cp_parser_committed_to_tentative_parse (parser))
2244 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2250 /* This function is called when a type is defined. If type
2251 definitions are forbidden at this point, an error message is
2255 cp_parser_check_type_definition (parser)
2258 /* If types are forbidden here, issue a message. */
2259 if (parser->type_definition_forbidden_message)
2260 /* Use `%s' to print the string in case there are any escape
2261 characters in the message. */
2262 error ("%s", parser->type_definition_forbidden_message);
2265 /* Consume tokens up to, and including, the next non-nested closing `)'.
2266 Returns TRUE iff we found a closing `)'. */
2269 cp_parser_skip_to_closing_parenthesis (cp_parser *parser)
2271 unsigned nesting_depth = 0;
2277 /* If we've run out of tokens, then there is no closing `)'. */
2278 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2280 /* Consume the token. */
2281 token = cp_lexer_consume_token (parser->lexer);
2282 /* If it is an `(', we have entered another level of nesting. */
2283 if (token->type == CPP_OPEN_PAREN)
2285 /* If it is a `)', then we might be done. */
2286 else if (token->type == CPP_CLOSE_PAREN && nesting_depth-- == 0)
2291 /* Consume tokens until the next token is a `)', or a `,'. Returns
2292 TRUE if the next token is a `,'. */
2295 cp_parser_skip_to_closing_parenthesis_or_comma (cp_parser *parser)
2297 unsigned nesting_depth = 0;
2301 cp_token *token = cp_lexer_peek_token (parser->lexer);
2303 /* If we've run out of tokens, then there is no closing `)'. */
2304 if (token->type == CPP_EOF)
2306 /* If it is a `,' stop. */
2307 else if (token->type == CPP_COMMA && nesting_depth-- == 0)
2309 /* If it is a `)', stop. */
2310 else if (token->type == CPP_CLOSE_PAREN && nesting_depth-- == 0)
2312 /* If it is an `(', we have entered another level of nesting. */
2313 else if (token->type == CPP_OPEN_PAREN)
2315 /* Consume the token. */
2316 token = cp_lexer_consume_token (parser->lexer);
2320 /* Consume tokens until we reach the end of the current statement.
2321 Normally, that will be just before consuming a `;'. However, if a
2322 non-nested `}' comes first, then we stop before consuming that. */
2325 cp_parser_skip_to_end_of_statement (parser)
2328 unsigned nesting_depth = 0;
2334 /* Peek at the next token. */
2335 token = cp_lexer_peek_token (parser->lexer);
2336 /* If we've run out of tokens, stop. */
2337 if (token->type == CPP_EOF)
2339 /* If the next token is a `;', we have reached the end of the
2341 if (token->type == CPP_SEMICOLON && !nesting_depth)
2343 /* If the next token is a non-nested `}', then we have reached
2344 the end of the current block. */
2345 if (token->type == CPP_CLOSE_BRACE)
2347 /* If this is a non-nested `}', stop before consuming it.
2348 That way, when confronted with something like:
2352 we stop before consuming the closing `}', even though we
2353 have not yet reached a `;'. */
2354 if (nesting_depth == 0)
2356 /* If it is the closing `}' for a block that we have
2357 scanned, stop -- but only after consuming the token.
2363 we will stop after the body of the erroneously declared
2364 function, but before consuming the following `typedef'
2366 if (--nesting_depth == 0)
2368 cp_lexer_consume_token (parser->lexer);
2372 /* If it the next token is a `{', then we are entering a new
2373 block. Consume the entire block. */
2374 else if (token->type == CPP_OPEN_BRACE)
2376 /* Consume the token. */
2377 cp_lexer_consume_token (parser->lexer);
2381 /* Skip tokens until we have consumed an entire block, or until we
2382 have consumed a non-nested `;'. */
2385 cp_parser_skip_to_end_of_block_or_statement (parser)
2388 unsigned nesting_depth = 0;
2394 /* Peek at the next token. */
2395 token = cp_lexer_peek_token (parser->lexer);
2396 /* If we've run out of tokens, stop. */
2397 if (token->type == CPP_EOF)
2399 /* If the next token is a `;', we have reached the end of the
2401 if (token->type == CPP_SEMICOLON && !nesting_depth)
2403 /* Consume the `;'. */
2404 cp_lexer_consume_token (parser->lexer);
2407 /* Consume the token. */
2408 token = cp_lexer_consume_token (parser->lexer);
2409 /* If the next token is a non-nested `}', then we have reached
2410 the end of the current block. */
2411 if (token->type == CPP_CLOSE_BRACE
2412 && (nesting_depth == 0 || --nesting_depth == 0))
2414 /* If it the next token is a `{', then we are entering a new
2415 block. Consume the entire block. */
2416 if (token->type == CPP_OPEN_BRACE)
2421 /* Skip tokens until a non-nested closing curly brace is the next
2425 cp_parser_skip_to_closing_brace (cp_parser *parser)
2427 unsigned nesting_depth = 0;
2433 /* Peek at the next token. */
2434 token = cp_lexer_peek_token (parser->lexer);
2435 /* If we've run out of tokens, stop. */
2436 if (token->type == CPP_EOF)
2438 /* If the next token is a non-nested `}', then we have reached
2439 the end of the current block. */
2440 if (token->type == CPP_CLOSE_BRACE && nesting_depth-- == 0)
2442 /* If it the next token is a `{', then we are entering a new
2443 block. Consume the entire block. */
2444 else if (token->type == CPP_OPEN_BRACE)
2446 /* Consume the token. */
2447 cp_lexer_consume_token (parser->lexer);
2451 /* Create a new C++ parser. */
2458 parser = (cp_parser *) ggc_alloc_cleared (sizeof (cp_parser));
2459 parser->lexer = cp_lexer_new (/*main_lexer_p=*/true);
2460 parser->context = cp_parser_context_new (NULL);
2462 /* For now, we always accept GNU extensions. */
2463 parser->allow_gnu_extensions_p = 1;
2465 /* The `>' token is a greater-than operator, not the end of a
2467 parser->greater_than_is_operator_p = true;
2469 parser->default_arg_ok_p = true;
2471 /* We are not parsing a constant-expression. */
2472 parser->constant_expression_p = false;
2474 /* Local variable names are not forbidden. */
2475 parser->local_variables_forbidden_p = false;
2477 /* We are not procesing an `extern "C"' declaration. */
2478 parser->in_unbraced_linkage_specification_p = false;
2480 /* We are not processing a declarator. */
2481 parser->in_declarator_p = false;
2483 /* There are no default args to process. */
2484 parser->default_arg_types = NULL;
2486 /* The unparsed function queue is empty. */
2487 parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2489 /* There are no classes being defined. */
2490 parser->num_classes_being_defined = 0;
2492 /* No template parameters apply. */
2493 parser->num_template_parameter_lists = 0;
2498 /* Lexical conventions [gram.lex] */
2500 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
2504 cp_parser_identifier (parser)
2509 /* Look for the identifier. */
2510 token = cp_parser_require (parser, CPP_NAME, "identifier");
2511 /* Return the value. */
2512 return token ? token->value : error_mark_node;
2515 /* Basic concepts [gram.basic] */
2517 /* Parse a translation-unit.
2520 declaration-seq [opt]
2522 Returns TRUE if all went well. */
2525 cp_parser_translation_unit (parser)
2530 cp_parser_declaration_seq_opt (parser);
2532 /* If there are no tokens left then all went well. */
2533 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2536 /* Otherwise, issue an error message. */
2537 cp_parser_error (parser, "expected declaration");
2541 /* Consume the EOF token. */
2542 cp_parser_require (parser, CPP_EOF, "end-of-file");
2545 finish_translation_unit ();
2547 /* All went well. */
2551 /* Expressions [gram.expr] */
2553 /* Parse a primary-expression.
2564 ( compound-statement )
2565 __builtin_va_arg ( assignment-expression , type-id )
2570 Returns a representation of the expression.
2572 *IDK indicates what kind of id-expression (if any) was present.
2574 *QUALIFYING_CLASS is set to a non-NULL value if the id-expression can be
2575 used as the operand of a pointer-to-member. In that case,
2576 *QUALIFYING_CLASS gives the class that is used as the qualifying
2577 class in the pointer-to-member. */
2580 cp_parser_primary_expression (cp_parser *parser,
2581 cp_parser_id_kind *idk,
2582 tree *qualifying_class)
2586 /* Assume the primary expression is not an id-expression. */
2587 *idk = CP_PARSER_ID_KIND_NONE;
2588 /* And that it cannot be used as pointer-to-member. */
2589 *qualifying_class = NULL_TREE;
2591 /* Peek at the next token. */
2592 token = cp_lexer_peek_token (parser->lexer);
2593 switch (token->type)
2606 token = cp_lexer_consume_token (parser->lexer);
2607 return token->value;
2609 case CPP_OPEN_PAREN:
2612 bool saved_greater_than_is_operator_p;
2614 /* Consume the `('. */
2615 cp_lexer_consume_token (parser->lexer);
2616 /* Within a parenthesized expression, a `>' token is always
2617 the greater-than operator. */
2618 saved_greater_than_is_operator_p
2619 = parser->greater_than_is_operator_p;
2620 parser->greater_than_is_operator_p = true;
2621 /* If we see `( { ' then we are looking at the beginning of
2622 a GNU statement-expression. */
2623 if (cp_parser_allow_gnu_extensions_p (parser)
2624 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
2626 /* Statement-expressions are not allowed by the standard. */
2628 pedwarn ("ISO C++ forbids braced-groups within expressions");
2630 /* And they're not allowed outside of a function-body; you
2631 cannot, for example, write:
2633 int i = ({ int j = 3; j + 1; });
2635 at class or namespace scope. */
2636 if (!at_function_scope_p ())
2637 error ("statement-expressions are allowed only inside functions");
2638 /* Start the statement-expression. */
2639 expr = begin_stmt_expr ();
2640 /* Parse the compound-statement. */
2641 cp_parser_compound_statement (parser);
2643 expr = finish_stmt_expr (expr);
2647 /* Parse the parenthesized expression. */
2648 expr = cp_parser_expression (parser);
2649 /* Let the front end know that this expression was
2650 enclosed in parentheses. This matters in case, for
2651 example, the expression is of the form `A::B', since
2652 `&A::B' might be a pointer-to-member, but `&(A::B)' is
2654 finish_parenthesized_expr (expr);
2656 /* The `>' token might be the end of a template-id or
2657 template-parameter-list now. */
2658 parser->greater_than_is_operator_p
2659 = saved_greater_than_is_operator_p;
2660 /* Consume the `)'. */
2661 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
2662 cp_parser_skip_to_end_of_statement (parser);
2668 switch (token->keyword)
2670 /* These two are the boolean literals. */
2672 cp_lexer_consume_token (parser->lexer);
2673 return boolean_true_node;
2675 cp_lexer_consume_token (parser->lexer);
2676 return boolean_false_node;
2678 /* The `__null' literal. */
2680 cp_lexer_consume_token (parser->lexer);
2683 /* Recognize the `this' keyword. */
2685 cp_lexer_consume_token (parser->lexer);
2686 if (parser->local_variables_forbidden_p)
2688 error ("`this' may not be used in this context");
2689 return error_mark_node;
2691 return finish_this_expr ();
2693 /* The `operator' keyword can be the beginning of an
2698 case RID_FUNCTION_NAME:
2699 case RID_PRETTY_FUNCTION_NAME:
2700 case RID_C99_FUNCTION_NAME:
2701 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
2702 __func__ are the names of variables -- but they are
2703 treated specially. Therefore, they are handled here,
2704 rather than relying on the generic id-expression logic
2705 below. Gramatically, these names are id-expressions.
2707 Consume the token. */
2708 token = cp_lexer_consume_token (parser->lexer);
2709 /* Look up the name. */
2710 return finish_fname (token->value);
2717 /* The `__builtin_va_arg' construct is used to handle
2718 `va_arg'. Consume the `__builtin_va_arg' token. */
2719 cp_lexer_consume_token (parser->lexer);
2720 /* Look for the opening `('. */
2721 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
2722 /* Now, parse the assignment-expression. */
2723 expression = cp_parser_assignment_expression (parser);
2724 /* Look for the `,'. */
2725 cp_parser_require (parser, CPP_COMMA, "`,'");
2726 /* Parse the type-id. */
2727 type = cp_parser_type_id (parser);
2728 /* Look for the closing `)'. */
2729 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
2731 return build_x_va_arg (expression, type);
2735 cp_parser_error (parser, "expected primary-expression");
2736 return error_mark_node;
2740 /* An id-expression can start with either an identifier, a
2741 `::' as the beginning of a qualified-id, or the "operator"
2745 case CPP_TEMPLATE_ID:
2746 case CPP_NESTED_NAME_SPECIFIER:
2752 /* Parse the id-expression. */
2754 = cp_parser_id_expression (parser,
2755 /*template_keyword_p=*/false,
2756 /*check_dependency_p=*/true,
2757 /*template_p=*/NULL);
2758 if (id_expression == error_mark_node)
2759 return error_mark_node;
2760 /* If we have a template-id, then no further lookup is
2761 required. If the template-id was for a template-class, we
2762 will sometimes have a TYPE_DECL at this point. */
2763 else if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
2764 || TREE_CODE (id_expression) == TYPE_DECL)
2765 decl = id_expression;
2766 /* Look up the name. */
2769 decl = cp_parser_lookup_name_simple (parser, id_expression);
2770 /* If name lookup gives us a SCOPE_REF, then the
2771 qualifying scope was dependent. Just propagate the
2773 if (TREE_CODE (decl) == SCOPE_REF)
2775 if (TYPE_P (TREE_OPERAND (decl, 0)))
2776 *qualifying_class = TREE_OPERAND (decl, 0);
2779 /* Check to see if DECL is a local variable in a context
2780 where that is forbidden. */
2781 if (parser->local_variables_forbidden_p
2782 && local_variable_p (decl))
2784 /* It might be that we only found DECL because we are
2785 trying to be generous with pre-ISO scoping rules.
2786 For example, consider:
2790 for (int i = 0; i < 10; ++i) {}
2791 extern void f(int j = i);
2794 Here, name look up will originally find the out
2795 of scope `i'. We need to issue a warning message,
2796 but then use the global `i'. */
2797 decl = check_for_out_of_scope_variable (decl);
2798 if (local_variable_p (decl))
2800 error ("local variable `%D' may not appear in this context",
2802 return error_mark_node;
2806 /* If unqualified name lookup fails while processing a
2807 template, that just means that we need to do name
2808 lookup again when the template is instantiated. */
2810 && decl == error_mark_node
2811 && processing_template_decl)
2813 *idk = CP_PARSER_ID_KIND_UNQUALIFIED;
2814 return build_min_nt (LOOKUP_EXPR, id_expression);
2816 else if (decl == error_mark_node
2817 && !processing_template_decl)
2821 /* It may be resolvable as a koenig lookup function
2823 *idk = CP_PARSER_ID_KIND_UNQUALIFIED;
2824 return id_expression;
2826 else if (TYPE_P (parser->scope)
2827 && !COMPLETE_TYPE_P (parser->scope))
2828 error ("incomplete type `%T' used in nested name specifier",
2830 else if (parser->scope != global_namespace)
2831 error ("`%D' is not a member of `%D'",
2832 id_expression, parser->scope);
2834 error ("`::%D' has not been declared", id_expression);
2836 /* If DECL is a variable would be out of scope under
2837 ANSI/ISO rules, but in scope in the ARM, name lookup
2838 will succeed. Issue a diagnostic here. */
2840 decl = check_for_out_of_scope_variable (decl);
2842 /* Remember that the name was used in the definition of
2843 the current class so that we can check later to see if
2844 the meaning would have been different after the class
2845 was entirely defined. */
2846 if (!parser->scope && decl != error_mark_node)
2847 maybe_note_name_used_in_class (id_expression, decl);
2850 /* If we didn't find anything, or what we found was a type,
2851 then this wasn't really an id-expression. */
2852 if (TREE_CODE (decl) == TYPE_DECL
2853 || TREE_CODE (decl) == NAMESPACE_DECL
2854 || (TREE_CODE (decl) == TEMPLATE_DECL
2855 && !DECL_FUNCTION_TEMPLATE_P (decl)))
2857 cp_parser_error (parser,
2858 "expected primary-expression");
2859 return error_mark_node;
2862 /* If the name resolved to a template parameter, there is no
2863 need to look it up again later. Similarly, we resolve
2864 enumeration constants to their underlying values. */
2865 if (TREE_CODE (decl) == CONST_DECL)
2867 *idk = CP_PARSER_ID_KIND_NONE;
2868 if (DECL_TEMPLATE_PARM_P (decl) || !processing_template_decl)
2869 return DECL_INITIAL (decl);
2876 /* If the declaration was explicitly qualified indicate
2877 that. The semantics of `A::f(3)' are different than
2878 `f(3)' if `f' is virtual. */
2879 *idk = (parser->scope
2880 ? CP_PARSER_ID_KIND_QUALIFIED
2881 : (TREE_CODE (decl) == TEMPLATE_ID_EXPR
2882 ? CP_PARSER_ID_KIND_TEMPLATE_ID
2883 : CP_PARSER_ID_KIND_UNQUALIFIED));
2888 An id-expression is type-dependent if it contains an
2889 identifier that was declared with a dependent type.
2891 As an optimization, we could choose not to create a
2892 LOOKUP_EXPR for a name that resolved to a local
2893 variable in the template function that we are currently
2894 declaring; such a name cannot ever resolve to anything
2895 else. If we did that we would not have to look up
2896 these names at instantiation time.
2898 The standard is not very specific about an
2899 id-expression that names a set of overloaded functions.
2900 What if some of them have dependent types and some of
2901 them do not? Presumably, such a name should be treated
2902 as a dependent name. */
2903 /* Assume the name is not dependent. */
2904 dependent_p = false;
2905 if (!processing_template_decl)
2906 /* No names are dependent outside a template. */
2908 /* A template-id where the name of the template was not
2909 resolved is definitely dependent. */
2910 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
2911 && (TREE_CODE (TREE_OPERAND (decl, 0))
2912 == IDENTIFIER_NODE))
2914 /* For anything except an overloaded function, just check
2916 else if (!is_overloaded_fn (decl))
2918 = cp_parser_dependent_type_p (TREE_TYPE (decl));
2919 /* For a set of overloaded functions, check each of the
2925 if (BASELINK_P (fns))
2926 fns = BASELINK_FUNCTIONS (fns);
2928 /* For a template-id, check to see if the template
2929 arguments are dependent. */
2930 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
2932 tree args = TREE_OPERAND (fns, 1);
2934 if (args && TREE_CODE (args) == TREE_LIST)
2938 if (cp_parser_dependent_template_arg_p
2939 (TREE_VALUE (args)))
2944 args = TREE_CHAIN (args);
2947 else if (args && TREE_CODE (args) == TREE_VEC)
2950 for (i = 0; i < TREE_VEC_LENGTH (args); ++i)
2951 if (cp_parser_dependent_template_arg_p
2952 (TREE_VEC_ELT (args, i)))
2959 /* The functions are those referred to by the
2961 fns = TREE_OPERAND (fns, 0);
2964 /* If there are no dependent template arguments, go
2965 through the overlaoded functions. */
2966 while (fns && !dependent_p)
2968 tree fn = OVL_CURRENT (fns);
2970 /* Member functions of dependent classes are
2972 if (TREE_CODE (fn) == FUNCTION_DECL
2973 && cp_parser_type_dependent_expression_p (fn))
2975 else if (TREE_CODE (fn) == TEMPLATE_DECL
2976 && cp_parser_dependent_template_p (fn))
2979 fns = OVL_NEXT (fns);
2983 /* If the name was dependent on a template parameter,
2984 we will resolve the name at instantiation time. */
2987 /* Create a SCOPE_REF for qualified names. */
2990 if (TYPE_P (parser->scope))
2991 *qualifying_class = parser->scope;
2992 return build_nt (SCOPE_REF,
2996 /* A TEMPLATE_ID already contains all the information
2998 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR)
2999 return id_expression;
3000 /* Create a LOOKUP_EXPR for other unqualified names. */
3001 return build_min_nt (LOOKUP_EXPR, id_expression);
3006 decl = (adjust_result_of_qualified_name_lookup
3007 (decl, parser->scope, current_class_type));
3008 if (TREE_CODE (decl) == FIELD_DECL || BASELINK_P (decl))
3009 *qualifying_class = parser->scope;
3011 /* Resolve references to variables of anonymous unions
3012 into COMPONENT_REFs. */
3013 else if (TREE_CODE (decl) == ALIAS_DECL)
3014 decl = DECL_INITIAL (decl);
3016 /* Transform references to non-static data members into
3018 decl = hack_identifier (decl, id_expression);
3021 if (TREE_DEPRECATED (decl))
3022 warn_deprecated_use (decl);
3027 /* Anything else is an error. */
3029 cp_parser_error (parser, "expected primary-expression");
3030 return error_mark_node;
3034 /* Parse an id-expression.
3041 :: [opt] nested-name-specifier template [opt] unqualified-id
3043 :: operator-function-id
3046 Return a representation of the unqualified portion of the
3047 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
3048 a `::' or nested-name-specifier.
3050 Often, if the id-expression was a qualified-id, the caller will
3051 want to make a SCOPE_REF to represent the qualified-id. This
3052 function does not do this in order to avoid wastefully creating
3053 SCOPE_REFs when they are not required.
3055 If ASSUME_TYPENAME_P is true then we assume that qualified names
3056 are typenames. This flag is set when parsing a declarator-id;
3062 we are supposed to assume that `S<T>::R' is a class.
3064 If TEMPLATE_KEYWORD_P is true, then we have just seen the
3067 If CHECK_DEPENDENCY_P is false, then names are looked up inside
3068 uninstantiated templates.
3070 If *TEMPLATE_KEYWORD_P is non-NULL, it is set to true iff the
3071 `template' keyword is used to explicitly indicate that the entity
3072 named is a template. */
3075 cp_parser_id_expression (cp_parser *parser,
3076 bool template_keyword_p,
3077 bool check_dependency_p,
3080 bool global_scope_p;
3081 bool nested_name_specifier_p;
3083 /* Assume the `template' keyword was not used. */
3085 *template_p = false;
3087 /* Look for the optional `::' operator. */
3089 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
3091 /* Look for the optional nested-name-specifier. */
3092 nested_name_specifier_p
3093 = (cp_parser_nested_name_specifier_opt (parser,
3094 /*typename_keyword_p=*/false,
3098 /* If there is a nested-name-specifier, then we are looking at
3099 the first qualified-id production. */
3100 if (nested_name_specifier_p)
3103 tree saved_object_scope;
3104 tree saved_qualifying_scope;
3105 tree unqualified_id;
3108 /* See if the next token is the `template' keyword. */
3110 template_p = &is_template;
3111 *template_p = cp_parser_optional_template_keyword (parser);
3112 /* Name lookup we do during the processing of the
3113 unqualified-id might obliterate SCOPE. */
3114 saved_scope = parser->scope;
3115 saved_object_scope = parser->object_scope;
3116 saved_qualifying_scope = parser->qualifying_scope;
3117 /* Process the final unqualified-id. */
3118 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
3119 check_dependency_p);
3120 /* Restore the SAVED_SCOPE for our caller. */
3121 parser->scope = saved_scope;
3122 parser->object_scope = saved_object_scope;
3123 parser->qualifying_scope = saved_qualifying_scope;
3125 return unqualified_id;
3127 /* Otherwise, if we are in global scope, then we are looking at one
3128 of the other qualified-id productions. */
3129 else if (global_scope_p)
3134 /* Peek at the next token. */
3135 token = cp_lexer_peek_token (parser->lexer);
3137 /* If it's an identifier, and the next token is not a "<", then
3138 we can avoid the template-id case. This is an optimization
3139 for this common case. */
3140 if (token->type == CPP_NAME
3141 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_LESS)
3142 return cp_parser_identifier (parser);
3144 cp_parser_parse_tentatively (parser);
3145 /* Try a template-id. */
3146 id = cp_parser_template_id (parser,
3147 /*template_keyword_p=*/false,
3148 /*check_dependency_p=*/true);
3149 /* If that worked, we're done. */
3150 if (cp_parser_parse_definitely (parser))
3153 /* Peek at the next token. (Changes in the token buffer may
3154 have invalidated the pointer obtained above.) */
3155 token = cp_lexer_peek_token (parser->lexer);
3157 switch (token->type)
3160 return cp_parser_identifier (parser);
3163 if (token->keyword == RID_OPERATOR)
3164 return cp_parser_operator_function_id (parser);
3168 cp_parser_error (parser, "expected id-expression");
3169 return error_mark_node;
3173 return cp_parser_unqualified_id (parser, template_keyword_p,
3174 /*check_dependency_p=*/true);
3177 /* Parse an unqualified-id.
3181 operator-function-id
3182 conversion-function-id
3186 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3187 keyword, in a construct like `A::template ...'.
3189 Returns a representation of unqualified-id. For the `identifier'
3190 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
3191 production a BIT_NOT_EXPR is returned; the operand of the
3192 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
3193 other productions, see the documentation accompanying the
3194 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
3195 names are looked up in uninstantiated templates. */
3198 cp_parser_unqualified_id (parser, template_keyword_p,
3201 bool template_keyword_p;
3202 bool check_dependency_p;
3206 /* Peek at the next token. */
3207 token = cp_lexer_peek_token (parser->lexer);
3209 switch (token->type)
3215 /* We don't know yet whether or not this will be a
3217 cp_parser_parse_tentatively (parser);
3218 /* Try a template-id. */
3219 id = cp_parser_template_id (parser, template_keyword_p,
3220 check_dependency_p);
3221 /* If it worked, we're done. */
3222 if (cp_parser_parse_definitely (parser))
3224 /* Otherwise, it's an ordinary identifier. */
3225 return cp_parser_identifier (parser);
3228 case CPP_TEMPLATE_ID:
3229 return cp_parser_template_id (parser, template_keyword_p,
3230 check_dependency_p);
3235 tree qualifying_scope;
3239 /* Consume the `~' token. */
3240 cp_lexer_consume_token (parser->lexer);
3241 /* Parse the class-name. The standard, as written, seems to
3244 template <typename T> struct S { ~S (); };
3245 template <typename T> S<T>::~S() {}
3247 is invalid, since `~' must be followed by a class-name, but
3248 `S<T>' is dependent, and so not known to be a class.
3249 That's not right; we need to look in uninstantiated
3250 templates. A further complication arises from:
3252 template <typename T> void f(T t) {
3256 Here, it is not possible to look up `T' in the scope of `T'
3257 itself. We must look in both the current scope, and the
3258 scope of the containing complete expression.
3260 Yet another issue is:
3269 The standard does not seem to say that the `S' in `~S'
3270 should refer to the type `S' and not the data member
3273 /* DR 244 says that we look up the name after the "~" in the
3274 same scope as we looked up the qualifying name. That idea
3275 isn't fully worked out; it's more complicated than that. */
3276 scope = parser->scope;
3277 object_scope = parser->object_scope;
3278 qualifying_scope = parser->qualifying_scope;
3280 /* If the name is of the form "X::~X" it's OK. */
3281 if (scope && TYPE_P (scope)
3282 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
3283 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3285 && (cp_lexer_peek_token (parser->lexer)->value
3286 == TYPE_IDENTIFIER (scope)))
3288 cp_lexer_consume_token (parser->lexer);
3289 return build_nt (BIT_NOT_EXPR, scope);
3292 /* If there was an explicit qualification (S::~T), first look
3293 in the scope given by the qualification (i.e., S). */
3296 cp_parser_parse_tentatively (parser);
3297 type_decl = cp_parser_class_name (parser,
3298 /*typename_keyword_p=*/false,
3299 /*template_keyword_p=*/false,
3301 /*check_access_p=*/true,
3302 /*check_dependency=*/false,
3303 /*class_head_p=*/false);
3304 if (cp_parser_parse_definitely (parser))
3305 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3307 /* In "N::S::~S", look in "N" as well. */
3308 if (scope && qualifying_scope)
3310 cp_parser_parse_tentatively (parser);
3311 parser->scope = qualifying_scope;
3312 parser->object_scope = NULL_TREE;
3313 parser->qualifying_scope = NULL_TREE;
3315 = cp_parser_class_name (parser,
3316 /*typename_keyword_p=*/false,
3317 /*template_keyword_p=*/false,
3319 /*check_access_p=*/true,
3320 /*check_dependency=*/false,
3321 /*class_head_p=*/false);
3322 if (cp_parser_parse_definitely (parser))
3323 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3325 /* In "p->S::~T", look in the scope given by "*p" as well. */
3326 else if (object_scope)
3328 cp_parser_parse_tentatively (parser);
3329 parser->scope = object_scope;
3330 parser->object_scope = NULL_TREE;
3331 parser->qualifying_scope = NULL_TREE;
3333 = cp_parser_class_name (parser,
3334 /*typename_keyword_p=*/false,
3335 /*template_keyword_p=*/false,
3337 /*check_access_p=*/true,
3338 /*check_dependency=*/false,
3339 /*class_head_p=*/false);
3340 if (cp_parser_parse_definitely (parser))
3341 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3343 /* Look in the surrounding context. */
3344 parser->scope = NULL_TREE;
3345 parser->object_scope = NULL_TREE;
3346 parser->qualifying_scope = NULL_TREE;
3348 = cp_parser_class_name (parser,
3349 /*typename_keyword_p=*/false,
3350 /*template_keyword_p=*/false,
3352 /*check_access_p=*/true,
3353 /*check_dependency=*/false,
3354 /*class_head_p=*/false);
3355 /* If an error occurred, assume that the name of the
3356 destructor is the same as the name of the qualifying
3357 class. That allows us to keep parsing after running
3358 into ill-formed destructor names. */
3359 if (type_decl == error_mark_node && scope && TYPE_P (scope))
3360 return build_nt (BIT_NOT_EXPR, scope);
3361 else if (type_decl == error_mark_node)
3362 return error_mark_node;
3364 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3368 if (token->keyword == RID_OPERATOR)
3372 /* This could be a template-id, so we try that first. */
3373 cp_parser_parse_tentatively (parser);
3374 /* Try a template-id. */
3375 id = cp_parser_template_id (parser, template_keyword_p,
3376 /*check_dependency_p=*/true);
3377 /* If that worked, we're done. */
3378 if (cp_parser_parse_definitely (parser))
3380 /* We still don't know whether we're looking at an
3381 operator-function-id or a conversion-function-id. */
3382 cp_parser_parse_tentatively (parser);
3383 /* Try an operator-function-id. */
3384 id = cp_parser_operator_function_id (parser);
3385 /* If that didn't work, try a conversion-function-id. */
3386 if (!cp_parser_parse_definitely (parser))
3387 id = cp_parser_conversion_function_id (parser);
3394 cp_parser_error (parser, "expected unqualified-id");
3395 return error_mark_node;
3399 /* Parse an (optional) nested-name-specifier.
3401 nested-name-specifier:
3402 class-or-namespace-name :: nested-name-specifier [opt]
3403 class-or-namespace-name :: template nested-name-specifier [opt]
3405 PARSER->SCOPE should be set appropriately before this function is
3406 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
3407 effect. TYPE_P is TRUE if we non-type bindings should be ignored
3410 Sets PARSER->SCOPE to the class (TYPE) or namespace
3411 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
3412 it unchanged if there is no nested-name-specifier. Returns the new
3413 scope iff there is a nested-name-specifier, or NULL_TREE otherwise. */
3416 cp_parser_nested_name_specifier_opt (cp_parser *parser,
3417 bool typename_keyword_p,
3418 bool check_dependency_p,
3421 bool success = false;
3422 tree access_check = NULL_TREE;
3425 /* If the next token corresponds to a nested name specifier, there
3426 is no need to reparse it. */
3427 if (cp_lexer_next_token_is (parser->lexer, CPP_NESTED_NAME_SPECIFIER))
3432 /* Get the stored value. */
3433 value = cp_lexer_consume_token (parser->lexer)->value;
3434 /* Perform any access checks that were deferred. */
3435 for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
3436 cp_parser_defer_access_check (parser,
3437 TREE_PURPOSE (check),
3438 TREE_VALUE (check));
3439 /* Set the scope from the stored value. */
3440 parser->scope = TREE_VALUE (value);
3441 parser->qualifying_scope = TREE_TYPE (value);
3442 parser->object_scope = NULL_TREE;
3443 return parser->scope;
3446 /* Remember where the nested-name-specifier starts. */
3447 if (cp_parser_parsing_tentatively (parser)
3448 && !cp_parser_committed_to_tentative_parse (parser))
3450 cp_token *next_token = cp_lexer_peek_token (parser->lexer);
3451 start = cp_lexer_token_difference (parser->lexer,
3452 parser->lexer->first_token,
3454 access_check = parser->context->deferred_access_checks;
3463 tree saved_qualifying_scope;
3465 bool template_keyword_p;
3467 /* Spot cases that cannot be the beginning of a
3468 nested-name-specifier. On the second and subsequent times
3469 through the loop, we look for the `template' keyword. */
3471 && cp_lexer_next_token_is_keyword (parser->lexer,
3474 /* A template-id can start a nested-name-specifier. */
3475 else if (cp_lexer_next_token_is (parser->lexer, CPP_TEMPLATE_ID))
3479 /* If the next token is not an identifier, then it is
3480 definitely not a class-or-namespace-name. */
3481 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
3483 /* If the following token is neither a `<' (to begin a
3484 template-id), nor a `::', then we are not looking at a
3485 nested-name-specifier. */
3486 token = cp_lexer_peek_nth_token (parser->lexer, 2);
3487 if (token->type != CPP_LESS && token->type != CPP_SCOPE)
3491 /* The nested-name-specifier is optional, so we parse
3493 cp_parser_parse_tentatively (parser);
3495 /* Look for the optional `template' keyword, if this isn't the
3496 first time through the loop. */
3498 template_keyword_p = cp_parser_optional_template_keyword (parser);
3500 template_keyword_p = false;
3502 /* Save the old scope since the name lookup we are about to do
3503 might destroy it. */
3504 old_scope = parser->scope;
3505 saved_qualifying_scope = parser->qualifying_scope;
3506 /* Parse the qualifying entity. */
3508 = cp_parser_class_or_namespace_name (parser,
3513 /* Look for the `::' token. */
3514 cp_parser_require (parser, CPP_SCOPE, "`::'");
3516 /* If we found what we wanted, we keep going; otherwise, we're
3518 if (!cp_parser_parse_definitely (parser))
3520 bool error_p = false;
3522 /* Restore the OLD_SCOPE since it was valid before the
3523 failed attempt at finding the last
3524 class-or-namespace-name. */
3525 parser->scope = old_scope;
3526 parser->qualifying_scope = saved_qualifying_scope;
3527 /* If the next token is an identifier, and the one after
3528 that is a `::', then any valid interpretation would have
3529 found a class-or-namespace-name. */
3530 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
3531 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3533 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
3536 token = cp_lexer_consume_token (parser->lexer);
3541 decl = cp_parser_lookup_name_simple (parser, token->value);
3542 if (TREE_CODE (decl) == TEMPLATE_DECL)
3543 error ("`%D' used without template parameters",
3545 else if (parser->scope)
3547 if (TYPE_P (parser->scope))
3548 error ("`%T::%D' is not a class-name or "
3550 parser->scope, token->value);
3552 error ("`%D::%D' is not a class-name or "
3554 parser->scope, token->value);
3557 error ("`%D' is not a class-name or namespace-name",
3559 parser->scope = NULL_TREE;
3561 /* Treat this as a successful nested-name-specifier
3566 If the name found is not a class-name (clause
3567 _class_) or namespace-name (_namespace.def_), the
3568 program is ill-formed. */
3571 cp_lexer_consume_token (parser->lexer);
3576 /* We've found one valid nested-name-specifier. */
3578 /* Make sure we look in the right scope the next time through
3580 parser->scope = (TREE_CODE (new_scope) == TYPE_DECL
3581 ? TREE_TYPE (new_scope)
3583 /* If it is a class scope, try to complete it; we are about to
3584 be looking up names inside the class. */
3585 if (TYPE_P (parser->scope))
3586 complete_type (parser->scope);
3589 /* If parsing tentatively, replace the sequence of tokens that makes
3590 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
3591 token. That way, should we re-parse the token stream, we will
3592 not have to repeat the effort required to do the parse, nor will
3593 we issue duplicate error messages. */
3594 if (success && start >= 0)
3599 /* Find the token that corresponds to the start of the
3601 token = cp_lexer_advance_token (parser->lexer,
3602 parser->lexer->first_token,
3605 /* Remember the access checks associated with this
3606 nested-name-specifier. */
3607 c = parser->context->deferred_access_checks;
3608 if (c == access_check)
3609 access_check = NULL_TREE;
3612 while (TREE_CHAIN (c) != access_check)
3614 access_check = parser->context->deferred_access_checks;
3615 parser->context->deferred_access_checks = TREE_CHAIN (c);
3616 TREE_CHAIN (c) = NULL_TREE;
3619 /* Reset the contents of the START token. */
3620 token->type = CPP_NESTED_NAME_SPECIFIER;
3621 token->value = build_tree_list (access_check, parser->scope);
3622 TREE_TYPE (token->value) = parser->qualifying_scope;
3623 token->keyword = RID_MAX;
3624 /* Purge all subsequent tokens. */
3625 cp_lexer_purge_tokens_after (parser->lexer, token);
3628 return success ? parser->scope : NULL_TREE;
3631 /* Parse a nested-name-specifier. See
3632 cp_parser_nested_name_specifier_opt for details. This function
3633 behaves identically, except that it will an issue an error if no
3634 nested-name-specifier is present, and it will return
3635 ERROR_MARK_NODE, rather than NULL_TREE, if no nested-name-specifier
3639 cp_parser_nested_name_specifier (cp_parser *parser,
3640 bool typename_keyword_p,
3641 bool check_dependency_p,
3646 /* Look for the nested-name-specifier. */
3647 scope = cp_parser_nested_name_specifier_opt (parser,
3651 /* If it was not present, issue an error message. */
3654 cp_parser_error (parser, "expected nested-name-specifier");
3655 return error_mark_node;
3661 /* Parse a class-or-namespace-name.
3663 class-or-namespace-name:
3667 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
3668 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
3669 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
3670 TYPE_P is TRUE iff the next name should be taken as a class-name,
3671 even the same name is declared to be another entity in the same
3674 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
3675 specified by the class-or-namespace-name. If neither is found the
3676 ERROR_MARK_NODE is returned. */
3679 cp_parser_class_or_namespace_name (cp_parser *parser,
3680 bool typename_keyword_p,
3681 bool template_keyword_p,
3682 bool check_dependency_p,
3686 tree saved_qualifying_scope;
3687 tree saved_object_scope;
3691 /* If the next token is the `template' keyword, we know that we are
3692 looking at a class-name. */
3693 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
3694 return cp_parser_class_name (parser,
3698 /*check_access_p=*/true,
3700 /*class_head_p=*/false);
3701 /* Before we try to parse the class-name, we must save away the
3702 current PARSER->SCOPE since cp_parser_class_name will destroy
3704 saved_scope = parser->scope;
3705 saved_qualifying_scope = parser->qualifying_scope;
3706 saved_object_scope = parser->object_scope;
3707 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
3708 there is no need to look for a namespace-name. */
3709 only_class_p = saved_scope && TYPE_P (saved_scope);
3711 cp_parser_parse_tentatively (parser);
3712 scope = cp_parser_class_name (parser,
3716 /*check_access_p=*/true,
3718 /*class_head_p=*/false);
3719 /* If that didn't work, try for a namespace-name. */
3720 if (!only_class_p && !cp_parser_parse_definitely (parser))
3722 /* Restore the saved scope. */
3723 parser->scope = saved_scope;
3724 parser->qualifying_scope = saved_qualifying_scope;
3725 parser->object_scope = saved_object_scope;
3726 /* If we are not looking at an identifier followed by the scope
3727 resolution operator, then this is not part of a
3728 nested-name-specifier. (Note that this function is only used
3729 to parse the components of a nested-name-specifier.) */
3730 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
3731 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
3732 return error_mark_node;
3733 scope = cp_parser_namespace_name (parser);
3739 /* Parse a postfix-expression.
3743 postfix-expression [ expression ]
3744 postfix-expression ( expression-list [opt] )
3745 simple-type-specifier ( expression-list [opt] )
3746 typename :: [opt] nested-name-specifier identifier
3747 ( expression-list [opt] )
3748 typename :: [opt] nested-name-specifier template [opt] template-id
3749 ( expression-list [opt] )
3750 postfix-expression . template [opt] id-expression
3751 postfix-expression -> template [opt] id-expression
3752 postfix-expression . pseudo-destructor-name
3753 postfix-expression -> pseudo-destructor-name
3754 postfix-expression ++
3755 postfix-expression --
3756 dynamic_cast < type-id > ( expression )
3757 static_cast < type-id > ( expression )
3758 reinterpret_cast < type-id > ( expression )
3759 const_cast < type-id > ( expression )
3760 typeid ( expression )
3766 ( type-id ) { initializer-list , [opt] }
3768 This extension is a GNU version of the C99 compound-literal
3769 construct. (The C99 grammar uses `type-name' instead of `type-id',
3770 but they are essentially the same concept.)
3772 If ADDRESS_P is true, the postfix expression is the operand of the
3775 Returns a representation of the expression. */
3778 cp_parser_postfix_expression (cp_parser *parser, bool address_p)
3782 cp_parser_id_kind idk = CP_PARSER_ID_KIND_NONE;
3783 tree postfix_expression = NULL_TREE;
3784 /* Non-NULL only if the current postfix-expression can be used to
3785 form a pointer-to-member. In that case, QUALIFYING_CLASS is the
3786 class used to qualify the member. */
3787 tree qualifying_class = NULL_TREE;
3790 /* Peek at the next token. */
3791 token = cp_lexer_peek_token (parser->lexer);
3792 /* Some of the productions are determined by keywords. */
3793 keyword = token->keyword;
3803 const char *saved_message;
3805 /* All of these can be handled in the same way from the point
3806 of view of parsing. Begin by consuming the token
3807 identifying the cast. */
3808 cp_lexer_consume_token (parser->lexer);
3810 /* New types cannot be defined in the cast. */
3811 saved_message = parser->type_definition_forbidden_message;
3812 parser->type_definition_forbidden_message
3813 = "types may not be defined in casts";
3815 /* Look for the opening `<'. */
3816 cp_parser_require (parser, CPP_LESS, "`<'");
3817 /* Parse the type to which we are casting. */
3818 type = cp_parser_type_id (parser);
3819 /* Look for the closing `>'. */
3820 cp_parser_require (parser, CPP_GREATER, "`>'");
3821 /* Restore the old message. */
3822 parser->type_definition_forbidden_message = saved_message;
3824 /* And the expression which is being cast. */
3825 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3826 expression = cp_parser_expression (parser);
3827 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3833 = build_dynamic_cast (type, expression);
3837 = build_static_cast (type, expression);
3841 = build_reinterpret_cast (type, expression);
3845 = build_const_cast (type, expression);
3856 const char *saved_message;
3858 /* Consume the `typeid' token. */
3859 cp_lexer_consume_token (parser->lexer);
3860 /* Look for the `(' token. */
3861 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3862 /* Types cannot be defined in a `typeid' expression. */
3863 saved_message = parser->type_definition_forbidden_message;
3864 parser->type_definition_forbidden_message
3865 = "types may not be defined in a `typeid\' expression";
3866 /* We can't be sure yet whether we're looking at a type-id or an
3868 cp_parser_parse_tentatively (parser);
3869 /* Try a type-id first. */
3870 type = cp_parser_type_id (parser);
3871 /* Look for the `)' token. Otherwise, we can't be sure that
3872 we're not looking at an expression: consider `typeid (int
3873 (3))', for example. */
3874 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3875 /* If all went well, simply lookup the type-id. */
3876 if (cp_parser_parse_definitely (parser))
3877 postfix_expression = get_typeid (type);
3878 /* Otherwise, fall back to the expression variant. */
3883 /* Look for an expression. */
3884 expression = cp_parser_expression (parser);
3885 /* Compute its typeid. */
3886 postfix_expression = build_typeid (expression);
3887 /* Look for the `)' token. */
3888 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3891 /* Restore the saved message. */
3892 parser->type_definition_forbidden_message = saved_message;
3898 bool template_p = false;
3902 /* Consume the `typename' token. */
3903 cp_lexer_consume_token (parser->lexer);
3904 /* Look for the optional `::' operator. */
3905 cp_parser_global_scope_opt (parser,
3906 /*current_scope_valid_p=*/false);
3907 /* Look for the nested-name-specifier. */
3908 cp_parser_nested_name_specifier (parser,
3909 /*typename_keyword_p=*/true,
3910 /*check_dependency_p=*/true,
3912 /* Look for the optional `template' keyword. */
3913 template_p = cp_parser_optional_template_keyword (parser);
3914 /* We don't know whether we're looking at a template-id or an
3916 cp_parser_parse_tentatively (parser);
3917 /* Try a template-id. */
3918 id = cp_parser_template_id (parser, template_p,
3919 /*check_dependency_p=*/true);
3920 /* If that didn't work, try an identifier. */
3921 if (!cp_parser_parse_definitely (parser))
3922 id = cp_parser_identifier (parser);
3923 /* Create a TYPENAME_TYPE to represent the type to which the
3924 functional cast is being performed. */
3925 type = make_typename_type (parser->scope, id,
3928 postfix_expression = cp_parser_functional_cast (parser, type);
3936 /* If the next thing is a simple-type-specifier, we may be
3937 looking at a functional cast. We could also be looking at
3938 an id-expression. So, we try the functional cast, and if
3939 that doesn't work we fall back to the primary-expression. */
3940 cp_parser_parse_tentatively (parser);
3941 /* Look for the simple-type-specifier. */
3942 type = cp_parser_simple_type_specifier (parser,
3943 CP_PARSER_FLAGS_NONE);
3944 /* Parse the cast itself. */
3945 if (!cp_parser_error_occurred (parser))
3947 = cp_parser_functional_cast (parser, type);
3948 /* If that worked, we're done. */
3949 if (cp_parser_parse_definitely (parser))
3952 /* If the functional-cast didn't work out, try a
3953 compound-literal. */
3954 if (cp_parser_allow_gnu_extensions_p (parser))
3956 tree initializer_list = NULL_TREE;
3958 cp_parser_parse_tentatively (parser);
3959 /* Look for the `('. */
3960 if (cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
3962 type = cp_parser_type_id (parser);
3963 /* Look for the `)'. */
3964 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3965 /* Look for the `{'. */
3966 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
3967 /* If things aren't going well, there's no need to
3969 if (!cp_parser_error_occurred (parser))
3971 /* Parse the initializer-list. */
3973 = cp_parser_initializer_list (parser);
3974 /* Allow a trailing `,'. */
3975 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
3976 cp_lexer_consume_token (parser->lexer);
3977 /* Look for the final `}'. */
3978 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
3981 /* If that worked, we're definitely looking at a
3982 compound-literal expression. */
3983 if (cp_parser_parse_definitely (parser))
3985 /* Warn the user that a compound literal is not
3986 allowed in standard C++. */
3988 pedwarn ("ISO C++ forbids compound-literals");
3989 /* Form the representation of the compound-literal. */
3991 = finish_compound_literal (type, initializer_list);
3996 /* It must be a primary-expression. */
3997 postfix_expression = cp_parser_primary_expression (parser,
4004 /* Peek at the next token. */
4005 token = cp_lexer_peek_token (parser->lexer);
4006 done = (token->type != CPP_OPEN_SQUARE
4007 && token->type != CPP_OPEN_PAREN
4008 && token->type != CPP_DOT
4009 && token->type != CPP_DEREF
4010 && token->type != CPP_PLUS_PLUS
4011 && token->type != CPP_MINUS_MINUS);
4013 /* If the postfix expression is complete, finish up. */
4014 if (address_p && qualifying_class && done)
4016 if (TREE_CODE (postfix_expression) == SCOPE_REF)
4017 postfix_expression = TREE_OPERAND (postfix_expression, 1);
4019 = build_offset_ref (qualifying_class, postfix_expression);
4020 return postfix_expression;
4023 /* Otherwise, if we were avoiding committing until we knew
4024 whether or not we had a pointer-to-member, we now know that
4025 the expression is an ordinary reference to a qualified name. */
4026 if (qualifying_class && !processing_template_decl)
4028 if (TREE_CODE (postfix_expression) == FIELD_DECL)
4030 = finish_non_static_data_member (postfix_expression,
4032 else if (BASELINK_P (postfix_expression))
4037 /* See if any of the functions are non-static members. */
4038 fns = BASELINK_FUNCTIONS (postfix_expression);
4039 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
4040 fns = TREE_OPERAND (fns, 0);
4041 for (fn = fns; fn; fn = OVL_NEXT (fn))
4042 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn))
4044 /* If so, the expression may be relative to the current
4046 if (fn && current_class_type
4047 && DERIVED_FROM_P (qualifying_class, current_class_type))
4049 = (build_class_member_access_expr
4050 (maybe_dummy_object (qualifying_class, NULL),
4052 BASELINK_ACCESS_BINFO (postfix_expression),
4053 /*preserve_reference=*/false));
4055 return build_offset_ref (qualifying_class,
4056 postfix_expression);
4060 /* Remember that there was a reference to this entity. */
4061 if (DECL_P (postfix_expression))
4062 mark_used (postfix_expression);
4064 /* Keep looping until the postfix-expression is complete. */
4067 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE
4068 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
4070 /* It is not a Koenig lookup function call. */
4071 unqualified_name_lookup_error (postfix_expression);
4072 postfix_expression = error_mark_node;
4075 /* Peek at the next token. */
4076 token = cp_lexer_peek_token (parser->lexer);
4078 switch (token->type)
4080 case CPP_OPEN_SQUARE:
4081 /* postfix-expression [ expression ] */
4085 /* Consume the `[' token. */
4086 cp_lexer_consume_token (parser->lexer);
4087 /* Parse the index expression. */
4088 index = cp_parser_expression (parser);
4089 /* Look for the closing `]'. */
4090 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4092 /* Build the ARRAY_REF. */
4094 = grok_array_decl (postfix_expression, index);
4095 idk = CP_PARSER_ID_KIND_NONE;
4099 case CPP_OPEN_PAREN:
4100 /* postfix-expression ( expression-list [opt] ) */
4104 /* Consume the `(' token. */
4105 cp_lexer_consume_token (parser->lexer);
4106 /* If the next token is not a `)', then there are some
4108 if (cp_lexer_next_token_is_not (parser->lexer,
4110 args = cp_parser_expression_list (parser);
4113 /* Look for the closing `)'. */
4114 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4116 if (idk == CP_PARSER_ID_KIND_UNQUALIFIED
4117 && (is_overloaded_fn (postfix_expression)
4118 || DECL_P (postfix_expression)
4119 || TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4123 tree identifier = NULL_TREE;
4124 tree functions = NULL_TREE;
4126 /* Find the name of the overloaded function. */
4127 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4128 identifier = postfix_expression;
4129 else if (is_overloaded_fn (postfix_expression))
4131 functions = postfix_expression;
4132 identifier = DECL_NAME (get_first_fn (functions));
4134 else if (DECL_P (postfix_expression))
4136 functions = postfix_expression;
4137 identifier = DECL_NAME (postfix_expression);
4140 /* A call to a namespace-scope function using an
4143 Do Koenig lookup -- unless any of the arguments are
4145 for (arg = args; arg; arg = TREE_CHAIN (arg))
4146 if (cp_parser_type_dependent_expression_p (TREE_VALUE (arg)))
4151 = lookup_arg_dependent(identifier, functions, args);
4152 if (!postfix_expression)
4154 /* The unqualified name could not be resolved. */
4155 unqualified_name_lookup_error (identifier);
4156 postfix_expression = error_mark_node;
4159 = build_call_from_tree (postfix_expression, args,
4160 /*diallow_virtual=*/false);
4163 postfix_expression = build_min_nt (LOOKUP_EXPR,
4166 else if (idk == CP_PARSER_ID_KIND_UNQUALIFIED
4167 && TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4169 /* The unqualified name could not be resolved. */
4170 unqualified_name_lookup_error (postfix_expression);
4171 postfix_expression = error_mark_node;
4175 /* In the body of a template, no further processing is
4177 if (processing_template_decl)
4179 postfix_expression = build_nt (CALL_EXPR,
4185 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
4187 = (build_new_method_call
4188 (TREE_OPERAND (postfix_expression, 0),
4189 TREE_OPERAND (postfix_expression, 1),
4191 (idk == CP_PARSER_ID_KIND_QUALIFIED
4192 ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL)));
4193 else if (TREE_CODE (postfix_expression) == OFFSET_REF)
4194 postfix_expression = (build_offset_ref_call_from_tree
4195 (postfix_expression, args));
4196 else if (idk == CP_PARSER_ID_KIND_QUALIFIED)
4198 /* A call to a static class member, or a
4199 namespace-scope function. */
4201 = finish_call_expr (postfix_expression, args,
4202 /*disallow_virtual=*/true);
4206 /* All other function calls. */
4208 = finish_call_expr (postfix_expression, args,
4209 /*disallow_virtual=*/false);
4212 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
4213 idk = CP_PARSER_ID_KIND_NONE;
4219 /* postfix-expression . template [opt] id-expression
4220 postfix-expression . pseudo-destructor-name
4221 postfix-expression -> template [opt] id-expression
4222 postfix-expression -> pseudo-destructor-name */
4227 tree scope = NULL_TREE;
4229 /* If this is a `->' operator, dereference the pointer. */
4230 if (token->type == CPP_DEREF)
4231 postfix_expression = build_x_arrow (postfix_expression);
4232 /* Check to see whether or not the expression is
4234 dependent_p = (cp_parser_type_dependent_expression_p
4235 (postfix_expression));
4236 /* The identifier following the `->' or `.' is not
4238 parser->scope = NULL_TREE;
4239 parser->qualifying_scope = NULL_TREE;
4240 parser->object_scope = NULL_TREE;
4241 /* Enter the scope corresponding to the type of the object
4242 given by the POSTFIX_EXPRESSION. */
4244 && TREE_TYPE (postfix_expression) != NULL_TREE)
4246 scope = TREE_TYPE (postfix_expression);
4247 /* According to the standard, no expression should
4248 ever have reference type. Unfortunately, we do not
4249 currently match the standard in this respect in
4250 that our internal representation of an expression
4251 may have reference type even when the standard says
4252 it does not. Therefore, we have to manually obtain
4253 the underlying type here. */
4254 if (TREE_CODE (scope) == REFERENCE_TYPE)
4255 scope = TREE_TYPE (scope);
4256 /* If the SCOPE is an OFFSET_TYPE, then we grab the
4257 type of the field. We get an OFFSET_TYPE for
4262 Probably, we should not get an OFFSET_TYPE here;
4263 that transformation should be made only if `&S::T'
4265 if (TREE_CODE (scope) == OFFSET_TYPE)
4266 scope = TREE_TYPE (scope);
4267 /* The type of the POSTFIX_EXPRESSION must be
4269 scope = complete_type_or_else (scope, NULL_TREE);
4270 /* Let the name lookup machinery know that we are
4271 processing a class member access expression. */
4272 parser->context->object_type = scope;
4273 /* If something went wrong, we want to be able to
4274 discern that case, as opposed to the case where
4275 there was no SCOPE due to the type of expression
4278 scope = error_mark_node;
4281 /* Consume the `.' or `->' operator. */
4282 cp_lexer_consume_token (parser->lexer);
4283 /* If the SCOPE is not a scalar type, we are looking at an
4284 ordinary class member access expression, rather than a
4285 pseudo-destructor-name. */
4286 if (!scope || !SCALAR_TYPE_P (scope))
4288 template_p = cp_parser_optional_template_keyword (parser);
4289 /* Parse the id-expression. */
4290 name = cp_parser_id_expression (parser,
4292 /*check_dependency_p=*/true,
4293 /*template_p=*/NULL);
4294 /* In general, build a SCOPE_REF if the member name is
4295 qualified. However, if the name was not dependent
4296 and has already been resolved; there is no need to
4297 build the SCOPE_REF. For example;
4299 struct X { void f(); };
4300 template <typename T> void f(T* t) { t->X::f(); }
4302 Even though "t" is dependent, "X::f" is not and has
4303 except that for a BASELINK there is no need to
4304 include scope information. */
4305 if (name != error_mark_node
4306 && !BASELINK_P (name)
4309 name = build_nt (SCOPE_REF, parser->scope, name);
4310 parser->scope = NULL_TREE;
4311 parser->qualifying_scope = NULL_TREE;
4312 parser->object_scope = NULL_TREE;
4315 = finish_class_member_access_expr (postfix_expression, name);
4317 /* Otherwise, try the pseudo-destructor-name production. */
4323 /* Parse the pseudo-destructor-name. */
4324 cp_parser_pseudo_destructor_name (parser, &s, &type);
4325 /* Form the call. */
4327 = finish_pseudo_destructor_expr (postfix_expression,
4328 s, TREE_TYPE (type));
4331 /* We no longer need to look up names in the scope of the
4332 object on the left-hand side of the `.' or `->'
4334 parser->context->object_type = NULL_TREE;
4335 idk = CP_PARSER_ID_KIND_NONE;
4340 /* postfix-expression ++ */
4341 /* Consume the `++' token. */
4342 cp_lexer_consume_token (parser->lexer);
4343 /* Generate a reprsentation for the complete expression. */
4345 = finish_increment_expr (postfix_expression,
4346 POSTINCREMENT_EXPR);
4347 idk = CP_PARSER_ID_KIND_NONE;
4350 case CPP_MINUS_MINUS:
4351 /* postfix-expression -- */
4352 /* Consume the `--' token. */
4353 cp_lexer_consume_token (parser->lexer);
4354 /* Generate a reprsentation for the complete expression. */
4356 = finish_increment_expr (postfix_expression,
4357 POSTDECREMENT_EXPR);
4358 idk = CP_PARSER_ID_KIND_NONE;
4362 return postfix_expression;
4366 /* We should never get here. */
4368 return error_mark_node;
4371 /* Parse an expression-list.
4374 assignment-expression
4375 expression-list, assignment-expression
4377 Returns a TREE_LIST. The TREE_VALUE of each node is a
4378 representation of an assignment-expression. Note that a TREE_LIST
4379 is returned even if there is only a single expression in the list. */
4382 cp_parser_expression_list (parser)
4385 tree expression_list = NULL_TREE;
4387 /* Consume expressions until there are no more. */
4392 /* Parse the next assignment-expression. */
4393 expr = cp_parser_assignment_expression (parser);
4394 /* Add it to the list. */
4395 expression_list = tree_cons (NULL_TREE, expr, expression_list);
4397 /* If the next token isn't a `,', then we are done. */
4398 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
4400 /* All uses of expression-list in the grammar are followed
4401 by a `)'. Therefore, if the next token is not a `)' an
4402 error will be issued, unless we are parsing tentatively.
4403 Skip ahead to see if there is another `,' before the `)';
4404 if so, we can go there and recover. */
4405 if (cp_parser_parsing_tentatively (parser)
4406 || cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
4407 || !cp_parser_skip_to_closing_parenthesis_or_comma (parser))
4411 /* Otherwise, consume the `,' and keep going. */
4412 cp_lexer_consume_token (parser->lexer);
4415 /* We built up the list in reverse order so we must reverse it now. */
4416 return nreverse (expression_list);
4419 /* Parse a pseudo-destructor-name.
4421 pseudo-destructor-name:
4422 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
4423 :: [opt] nested-name-specifier template template-id :: ~ type-name
4424 :: [opt] nested-name-specifier [opt] ~ type-name
4426 If either of the first two productions is used, sets *SCOPE to the
4427 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
4428 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
4429 or ERROR_MARK_NODE if no type-name is present. */
4432 cp_parser_pseudo_destructor_name (parser, scope, type)
4437 bool nested_name_specifier_p;
4439 /* Look for the optional `::' operator. */
4440 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
4441 /* Look for the optional nested-name-specifier. */
4442 nested_name_specifier_p
4443 = (cp_parser_nested_name_specifier_opt (parser,
4444 /*typename_keyword_p=*/false,
4445 /*check_dependency_p=*/true,
4448 /* Now, if we saw a nested-name-specifier, we might be doing the
4449 second production. */
4450 if (nested_name_specifier_p
4451 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
4453 /* Consume the `template' keyword. */
4454 cp_lexer_consume_token (parser->lexer);
4455 /* Parse the template-id. */
4456 cp_parser_template_id (parser,
4457 /*template_keyword_p=*/true,
4458 /*check_dependency_p=*/false);
4459 /* Look for the `::' token. */
4460 cp_parser_require (parser, CPP_SCOPE, "`::'");
4462 /* If the next token is not a `~', then there might be some
4463 additional qualification. */
4464 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
4466 /* Look for the type-name. */
4467 *scope = TREE_TYPE (cp_parser_type_name (parser));
4468 /* Look for the `::' token. */
4469 cp_parser_require (parser, CPP_SCOPE, "`::'");
4474 /* Look for the `~'. */
4475 cp_parser_require (parser, CPP_COMPL, "`~'");
4476 /* Look for the type-name again. We are not responsible for
4477 checking that it matches the first type-name. */
4478 *type = cp_parser_type_name (parser);
4481 /* Parse a unary-expression.
4487 unary-operator cast-expression
4488 sizeof unary-expression
4496 __extension__ cast-expression
4497 __alignof__ unary-expression
4498 __alignof__ ( type-id )
4499 __real__ cast-expression
4500 __imag__ cast-expression
4503 ADDRESS_P is true iff the unary-expression is appearing as the
4504 operand of the `&' operator.
4506 Returns a representation of the expresion. */
4509 cp_parser_unary_expression (cp_parser *parser, bool address_p)
4512 enum tree_code unary_operator;
4514 /* Peek at the next token. */
4515 token = cp_lexer_peek_token (parser->lexer);
4516 /* Some keywords give away the kind of expression. */
4517 if (token->type == CPP_KEYWORD)
4519 enum rid keyword = token->keyword;
4525 /* Consume the `alignof' token. */
4526 cp_lexer_consume_token (parser->lexer);
4527 /* Parse the operand. */
4528 return finish_alignof (cp_parser_sizeof_operand
4536 /* Consume the `sizeof' token. */
4537 cp_lexer_consume_token (parser->lexer);
4538 /* Parse the operand. */
4539 operand = cp_parser_sizeof_operand (parser, keyword);
4541 /* If the type of the operand cannot be determined build a
4543 if (TYPE_P (operand)
4544 ? cp_parser_dependent_type_p (operand)
4545 : cp_parser_type_dependent_expression_p (operand))
4546 return build_min (SIZEOF_EXPR, size_type_node, operand);
4547 /* Otherwise, compute the constant value. */
4549 return finish_sizeof (operand);
4553 return cp_parser_new_expression (parser);
4556 return cp_parser_delete_expression (parser);
4560 /* The saved value of the PEDANTIC flag. */
4564 /* Save away the PEDANTIC flag. */
4565 cp_parser_extension_opt (parser, &saved_pedantic);
4566 /* Parse the cast-expression. */
4567 expr = cp_parser_cast_expression (parser, /*address_p=*/false);
4568 /* Restore the PEDANTIC flag. */
4569 pedantic = saved_pedantic;
4579 /* Consume the `__real__' or `__imag__' token. */
4580 cp_lexer_consume_token (parser->lexer);
4581 /* Parse the cast-expression. */
4582 expression = cp_parser_cast_expression (parser,
4583 /*address_p=*/false);
4584 /* Create the complete representation. */
4585 return build_x_unary_op ((keyword == RID_REALPART
4586 ? REALPART_EXPR : IMAGPART_EXPR),
4596 /* Look for the `:: new' and `:: delete', which also signal the
4597 beginning of a new-expression, or delete-expression,
4598 respectively. If the next token is `::', then it might be one of
4600 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
4604 /* See if the token after the `::' is one of the keywords in
4605 which we're interested. */
4606 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
4607 /* If it's `new', we have a new-expression. */
4608 if (keyword == RID_NEW)
4609 return cp_parser_new_expression (parser);
4610 /* Similarly, for `delete'. */
4611 else if (keyword == RID_DELETE)
4612 return cp_parser_delete_expression (parser);
4615 /* Look for a unary operator. */
4616 unary_operator = cp_parser_unary_operator (token);
4617 /* The `++' and `--' operators can be handled similarly, even though
4618 they are not technically unary-operators in the grammar. */
4619 if (unary_operator == ERROR_MARK)
4621 if (token->type == CPP_PLUS_PLUS)
4622 unary_operator = PREINCREMENT_EXPR;
4623 else if (token->type == CPP_MINUS_MINUS)
4624 unary_operator = PREDECREMENT_EXPR;
4625 /* Handle the GNU address-of-label extension. */
4626 else if (cp_parser_allow_gnu_extensions_p (parser)
4627 && token->type == CPP_AND_AND)
4631 /* Consume the '&&' token. */
4632 cp_lexer_consume_token (parser->lexer);
4633 /* Look for the identifier. */
4634 identifier = cp_parser_identifier (parser);
4635 /* Create an expression representing the address. */
4636 return finish_label_address_expr (identifier);
4639 if (unary_operator != ERROR_MARK)
4641 tree cast_expression;
4643 /* Consume the operator token. */
4644 token = cp_lexer_consume_token (parser->lexer);
4645 /* Parse the cast-expression. */
4647 = cp_parser_cast_expression (parser, unary_operator == ADDR_EXPR);
4648 /* Now, build an appropriate representation. */
4649 switch (unary_operator)
4652 return build_x_indirect_ref (cast_expression, "unary *");
4655 return build_x_unary_op (ADDR_EXPR, cast_expression);
4659 case TRUTH_NOT_EXPR:
4660 case PREINCREMENT_EXPR:
4661 case PREDECREMENT_EXPR:
4662 return finish_unary_op_expr (unary_operator, cast_expression);
4665 return build_x_unary_op (BIT_NOT_EXPR, cast_expression);
4669 return error_mark_node;
4673 return cp_parser_postfix_expression (parser, address_p);
4676 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
4677 unary-operator, the corresponding tree code is returned. */
4679 static enum tree_code
4680 cp_parser_unary_operator (token)
4683 switch (token->type)
4686 return INDIRECT_REF;
4692 return CONVERT_EXPR;
4698 return TRUTH_NOT_EXPR;
4701 return BIT_NOT_EXPR;
4708 /* Parse a new-expression.
4710 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
4711 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
4713 Returns a representation of the expression. */
4716 cp_parser_new_expression (parser)
4719 bool global_scope_p;
4724 /* Look for the optional `::' operator. */
4726 = (cp_parser_global_scope_opt (parser,
4727 /*current_scope_valid_p=*/false)
4729 /* Look for the `new' operator. */
4730 cp_parser_require_keyword (parser, RID_NEW, "`new'");
4731 /* There's no easy way to tell a new-placement from the
4732 `( type-id )' construct. */
4733 cp_parser_parse_tentatively (parser);
4734 /* Look for a new-placement. */
4735 placement = cp_parser_new_placement (parser);
4736 /* If that didn't work out, there's no new-placement. */
4737 if (!cp_parser_parse_definitely (parser))
4738 placement = NULL_TREE;
4740 /* If the next token is a `(', then we have a parenthesized
4742 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4744 /* Consume the `('. */
4745 cp_lexer_consume_token (parser->lexer);
4746 /* Parse the type-id. */
4747 type = cp_parser_type_id (parser);
4748 /* Look for the closing `)'. */
4749 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4751 /* Otherwise, there must be a new-type-id. */
4753 type = cp_parser_new_type_id (parser);
4755 /* If the next token is a `(', then we have a new-initializer. */
4756 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4757 initializer = cp_parser_new_initializer (parser);
4759 initializer = NULL_TREE;
4761 /* Create a representation of the new-expression. */
4762 return build_new (placement, type, initializer, global_scope_p);
4765 /* Parse a new-placement.
4770 Returns the same representation as for an expression-list. */
4773 cp_parser_new_placement (parser)
4776 tree expression_list;
4778 /* Look for the opening `('. */
4779 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
4780 return error_mark_node;
4781 /* Parse the expression-list. */
4782 expression_list = cp_parser_expression_list (parser);
4783 /* Look for the closing `)'. */
4784 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4786 return expression_list;
4789 /* Parse a new-type-id.
4792 type-specifier-seq new-declarator [opt]
4794 Returns a TREE_LIST whose TREE_PURPOSE is the type-specifier-seq,
4795 and whose TREE_VALUE is the new-declarator. */
4798 cp_parser_new_type_id (parser)
4801 tree type_specifier_seq;
4803 const char *saved_message;
4805 /* The type-specifier sequence must not contain type definitions.
4806 (It cannot contain declarations of new types either, but if they
4807 are not definitions we will catch that because they are not
4809 saved_message = parser->type_definition_forbidden_message;
4810 parser->type_definition_forbidden_message
4811 = "types may not be defined in a new-type-id";
4812 /* Parse the type-specifier-seq. */
4813 type_specifier_seq = cp_parser_type_specifier_seq (parser);
4814 /* Restore the old message. */
4815 parser->type_definition_forbidden_message = saved_message;
4816 /* Parse the new-declarator. */
4817 declarator = cp_parser_new_declarator_opt (parser);
4819 return build_tree_list (type_specifier_seq, declarator);
4822 /* Parse an (optional) new-declarator.
4825 ptr-operator new-declarator [opt]
4826 direct-new-declarator
4828 Returns a representation of the declarator. See
4829 cp_parser_declarator for the representations used. */
4832 cp_parser_new_declarator_opt (parser)
4835 enum tree_code code;
4837 tree cv_qualifier_seq;
4839 /* We don't know if there's a ptr-operator next, or not. */
4840 cp_parser_parse_tentatively (parser);
4841 /* Look for a ptr-operator. */
4842 code = cp_parser_ptr_operator (parser, &type, &cv_qualifier_seq);
4843 /* If that worked, look for more new-declarators. */
4844 if (cp_parser_parse_definitely (parser))
4848 /* Parse another optional declarator. */
4849 declarator = cp_parser_new_declarator_opt (parser);
4851 /* Create the representation of the declarator. */
4852 if (code == INDIRECT_REF)
4853 declarator = make_pointer_declarator (cv_qualifier_seq,
4856 declarator = make_reference_declarator (cv_qualifier_seq,
4859 /* Handle the pointer-to-member case. */
4861 declarator = build_nt (SCOPE_REF, type, declarator);
4866 /* If the next token is a `[', there is a direct-new-declarator. */
4867 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
4868 return cp_parser_direct_new_declarator (parser);
4873 /* Parse a direct-new-declarator.
4875 direct-new-declarator:
4877 direct-new-declarator [constant-expression]
4879 Returns an ARRAY_REF, following the same conventions as are
4880 documented for cp_parser_direct_declarator. */
4883 cp_parser_direct_new_declarator (parser)
4886 tree declarator = NULL_TREE;
4892 /* Look for the opening `['. */
4893 cp_parser_require (parser, CPP_OPEN_SQUARE, "`['");
4894 /* The first expression is not required to be constant. */
4897 expression = cp_parser_expression (parser);
4898 /* The standard requires that the expression have integral
4899 type. DR 74 adds enumeration types. We believe that the
4900 real intent is that these expressions be handled like the
4901 expression in a `switch' condition, which also allows
4902 classes with a single conversion to integral or
4903 enumeration type. */
4904 if (!processing_template_decl)
4907 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
4912 error ("expression in new-declarator must have integral or enumeration type");
4913 expression = error_mark_node;
4917 /* But all the other expressions must be. */
4919 expression = cp_parser_constant_expression (parser);
4920 /* Look for the closing `]'. */
4921 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4923 /* Add this bound to the declarator. */
4924 declarator = build_nt (ARRAY_REF, declarator, expression);
4926 /* If the next token is not a `[', then there are no more
4928 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
4935 /* Parse a new-initializer.
4938 ( expression-list [opt] )
4940 Returns a reprsentation of the expression-list. If there is no
4941 expression-list, VOID_ZERO_NODE is returned. */
4944 cp_parser_new_initializer (parser)
4947 tree expression_list;
4949 /* Look for the opening parenthesis. */
4950 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
4951 /* If the next token is not a `)', then there is an
4953 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
4954 expression_list = cp_parser_expression_list (parser);
4956 expression_list = void_zero_node;
4957 /* Look for the closing parenthesis. */
4958 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4960 return expression_list;
4963 /* Parse a delete-expression.
4966 :: [opt] delete cast-expression
4967 :: [opt] delete [ ] cast-expression
4969 Returns a representation of the expression. */
4972 cp_parser_delete_expression (parser)
4975 bool global_scope_p;
4979 /* Look for the optional `::' operator. */
4981 = (cp_parser_global_scope_opt (parser,
4982 /*current_scope_valid_p=*/false)
4984 /* Look for the `delete' keyword. */
4985 cp_parser_require_keyword (parser, RID_DELETE, "`delete'");
4986 /* See if the array syntax is in use. */
4987 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
4989 /* Consume the `[' token. */
4990 cp_lexer_consume_token (parser->lexer);
4991 /* Look for the `]' token. */
4992 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4993 /* Remember that this is the `[]' construct. */
4999 /* Parse the cast-expression. */
5000 expression = cp_parser_cast_expression (parser, /*address_p=*/false);
5002 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
5005 /* Parse a cast-expression.
5009 ( type-id ) cast-expression
5011 Returns a representation of the expression. */
5014 cp_parser_cast_expression (cp_parser *parser, bool address_p)
5016 /* If it's a `(', then we might be looking at a cast. */
5017 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5019 tree type = NULL_TREE;
5020 tree expr = NULL_TREE;
5021 bool compound_literal_p;
5022 const char *saved_message;
5024 /* There's no way to know yet whether or not this is a cast.
5025 For example, `(int (3))' is a unary-expression, while `(int)
5026 3' is a cast. So, we resort to parsing tentatively. */
5027 cp_parser_parse_tentatively (parser);
5028 /* Types may not be defined in a cast. */
5029 saved_message = parser->type_definition_forbidden_message;
5030 parser->type_definition_forbidden_message
5031 = "types may not be defined in casts";
5032 /* Consume the `('. */
5033 cp_lexer_consume_token (parser->lexer);
5034 /* A very tricky bit is that `(struct S) { 3 }' is a
5035 compound-literal (which we permit in C++ as an extension).
5036 But, that construct is not a cast-expression -- it is a
5037 postfix-expression. (The reason is that `(struct S) { 3 }.i'
5038 is legal; if the compound-literal were a cast-expression,
5039 you'd need an extra set of parentheses.) But, if we parse
5040 the type-id, and it happens to be a class-specifier, then we
5041 will commit to the parse at that point, because we cannot
5042 undo the action that is done when creating a new class. So,
5043 then we cannot back up and do a postfix-expression.
5045 Therefore, we scan ahead to the closing `)', and check to see
5046 if the token after the `)' is a `{'. If so, we are not
5047 looking at a cast-expression.
5049 Save tokens so that we can put them back. */
5050 cp_lexer_save_tokens (parser->lexer);
5051 /* Skip tokens until the next token is a closing parenthesis.
5052 If we find the closing `)', and the next token is a `{', then
5053 we are looking at a compound-literal. */
5055 = (cp_parser_skip_to_closing_parenthesis (parser)
5056 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
5057 /* Roll back the tokens we skipped. */
5058 cp_lexer_rollback_tokens (parser->lexer);
5059 /* If we were looking at a compound-literal, simulate an error
5060 so that the call to cp_parser_parse_definitely below will
5062 if (compound_literal_p)
5063 cp_parser_simulate_error (parser);
5066 /* Look for the type-id. */
5067 type = cp_parser_type_id (parser);
5068 /* Look for the closing `)'. */
5069 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5072 /* Restore the saved message. */
5073 parser->type_definition_forbidden_message = saved_message;
5075 /* If all went well, this is a cast. */
5076 if (cp_parser_parse_definitely (parser))
5078 /* Parse the dependent expression. */
5079 expr = cp_parser_cast_expression (parser, /*address_p=*/false);
5080 /* Warn about old-style casts, if so requested. */
5081 if (warn_old_style_cast
5082 && !in_system_header
5083 && !VOID_TYPE_P (type)
5084 && current_lang_name != lang_name_c)
5085 warning ("use of old-style cast");
5086 /* Perform the cast. */
5087 expr = build_c_cast (type, expr);
5094 /* If we get here, then it's not a cast, so it must be a
5095 unary-expression. */
5096 return cp_parser_unary_expression (parser, address_p);
5099 /* Parse a pm-expression.
5103 pm-expression .* cast-expression
5104 pm-expression ->* cast-expression
5106 Returns a representation of the expression. */
5109 cp_parser_pm_expression (parser)
5115 /* Parse the cast-expresion. */
5116 cast_expr = cp_parser_cast_expression (parser, /*address_p=*/false);
5117 pm_expr = cast_expr;
5118 /* Now look for pointer-to-member operators. */
5122 enum cpp_ttype token_type;
5124 /* Peek at the next token. */
5125 token = cp_lexer_peek_token (parser->lexer);
5126 token_type = token->type;
5127 /* If it's not `.*' or `->*' there's no pointer-to-member
5129 if (token_type != CPP_DOT_STAR
5130 && token_type != CPP_DEREF_STAR)
5133 /* Consume the token. */
5134 cp_lexer_consume_token (parser->lexer);
5136 /* Parse another cast-expression. */
5137 cast_expr = cp_parser_cast_expression (parser, /*address_p=*/false);
5139 /* Build the representation of the pointer-to-member
5141 if (token_type == CPP_DEREF_STAR)
5142 pm_expr = build_x_binary_op (MEMBER_REF, pm_expr, cast_expr);
5144 pm_expr = build_m_component_ref (pm_expr, cast_expr);
5150 /* Parse a multiplicative-expression.
5152 mulitplicative-expression:
5154 multiplicative-expression * pm-expression
5155 multiplicative-expression / pm-expression
5156 multiplicative-expression % pm-expression
5158 Returns a representation of the expression. */
5161 cp_parser_multiplicative_expression (parser)
5164 static cp_parser_token_tree_map map = {
5165 { CPP_MULT, MULT_EXPR },
5166 { CPP_DIV, TRUNC_DIV_EXPR },
5167 { CPP_MOD, TRUNC_MOD_EXPR },
5168 { CPP_EOF, ERROR_MARK }
5171 return cp_parser_binary_expression (parser,
5173 cp_parser_pm_expression);
5176 /* Parse an additive-expression.
5178 additive-expression:
5179 multiplicative-expression
5180 additive-expression + multiplicative-expression
5181 additive-expression - multiplicative-expression
5183 Returns a representation of the expression. */
5186 cp_parser_additive_expression (parser)
5189 static cp_parser_token_tree_map map = {
5190 { CPP_PLUS, PLUS_EXPR },
5191 { CPP_MINUS, MINUS_EXPR },
5192 { CPP_EOF, ERROR_MARK }
5195 return cp_parser_binary_expression (parser,
5197 cp_parser_multiplicative_expression);
5200 /* Parse a shift-expression.
5204 shift-expression << additive-expression
5205 shift-expression >> additive-expression
5207 Returns a representation of the expression. */
5210 cp_parser_shift_expression (parser)
5213 static cp_parser_token_tree_map map = {
5214 { CPP_LSHIFT, LSHIFT_EXPR },
5215 { CPP_RSHIFT, RSHIFT_EXPR },
5216 { CPP_EOF, ERROR_MARK }
5219 return cp_parser_binary_expression (parser,
5221 cp_parser_additive_expression);
5224 /* Parse a relational-expression.
5226 relational-expression:
5228 relational-expression < shift-expression
5229 relational-expression > shift-expression
5230 relational-expression <= shift-expression
5231 relational-expression >= shift-expression
5235 relational-expression:
5236 relational-expression <? shift-expression
5237 relational-expression >? shift-expression
5239 Returns a representation of the expression. */
5242 cp_parser_relational_expression (parser)
5245 static cp_parser_token_tree_map map = {
5246 { CPP_LESS, LT_EXPR },
5247 { CPP_GREATER, GT_EXPR },
5248 { CPP_LESS_EQ, LE_EXPR },
5249 { CPP_GREATER_EQ, GE_EXPR },
5250 { CPP_MIN, MIN_EXPR },
5251 { CPP_MAX, MAX_EXPR },
5252 { CPP_EOF, ERROR_MARK }
5255 return cp_parser_binary_expression (parser,
5257 cp_parser_shift_expression);
5260 /* Parse an equality-expression.
5262 equality-expression:
5263 relational-expression
5264 equality-expression == relational-expression
5265 equality-expression != relational-expression
5267 Returns a representation of the expression. */
5270 cp_parser_equality_expression (parser)
5273 static cp_parser_token_tree_map map = {
5274 { CPP_EQ_EQ, EQ_EXPR },
5275 { CPP_NOT_EQ, NE_EXPR },
5276 { CPP_EOF, ERROR_MARK }
5279 return cp_parser_binary_expression (parser,
5281 cp_parser_relational_expression);
5284 /* Parse an and-expression.
5288 and-expression & equality-expression
5290 Returns a representation of the expression. */
5293 cp_parser_and_expression (parser)
5296 static cp_parser_token_tree_map map = {
5297 { CPP_AND, BIT_AND_EXPR },
5298 { CPP_EOF, ERROR_MARK }
5301 return cp_parser_binary_expression (parser,
5303 cp_parser_equality_expression);
5306 /* Parse an exclusive-or-expression.
5308 exclusive-or-expression:
5310 exclusive-or-expression ^ and-expression
5312 Returns a representation of the expression. */
5315 cp_parser_exclusive_or_expression (parser)
5318 static cp_parser_token_tree_map map = {
5319 { CPP_XOR, BIT_XOR_EXPR },
5320 { CPP_EOF, ERROR_MARK }
5323 return cp_parser_binary_expression (parser,
5325 cp_parser_and_expression);
5329 /* Parse an inclusive-or-expression.
5331 inclusive-or-expression:
5332 exclusive-or-expression
5333 inclusive-or-expression | exclusive-or-expression
5335 Returns a representation of the expression. */
5338 cp_parser_inclusive_or_expression (parser)
5341 static cp_parser_token_tree_map map = {
5342 { CPP_OR, BIT_IOR_EXPR },
5343 { CPP_EOF, ERROR_MARK }
5346 return cp_parser_binary_expression (parser,
5348 cp_parser_exclusive_or_expression);
5351 /* Parse a logical-and-expression.
5353 logical-and-expression:
5354 inclusive-or-expression
5355 logical-and-expression && inclusive-or-expression
5357 Returns a representation of the expression. */
5360 cp_parser_logical_and_expression (parser)
5363 static cp_parser_token_tree_map map = {
5364 { CPP_AND_AND, TRUTH_ANDIF_EXPR },
5365 { CPP_EOF, ERROR_MARK }
5368 return cp_parser_binary_expression (parser,
5370 cp_parser_inclusive_or_expression);
5373 /* Parse a logical-or-expression.
5375 logical-or-expression:
5376 logical-and-expresion
5377 logical-or-expression || logical-and-expression
5379 Returns a representation of the expression. */
5382 cp_parser_logical_or_expression (parser)
5385 static cp_parser_token_tree_map map = {
5386 { CPP_OR_OR, TRUTH_ORIF_EXPR },
5387 { CPP_EOF, ERROR_MARK }
5390 return cp_parser_binary_expression (parser,
5392 cp_parser_logical_and_expression);
5395 /* Parse a conditional-expression.
5397 conditional-expression:
5398 logical-or-expression
5399 logical-or-expression ? expression : assignment-expression
5403 conditional-expression:
5404 logical-or-expression ? : assignment-expression
5406 Returns a representation of the expression. */
5409 cp_parser_conditional_expression (parser)
5412 tree logical_or_expr;
5414 /* Parse the logical-or-expression. */
5415 logical_or_expr = cp_parser_logical_or_expression (parser);
5416 /* If the next token is a `?', then we have a real conditional
5418 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
5419 return cp_parser_question_colon_clause (parser, logical_or_expr);
5420 /* Otherwise, the value is simply the logical-or-expression. */
5422 return logical_or_expr;
5425 /* Parse the `? expression : assignment-expression' part of a
5426 conditional-expression. The LOGICAL_OR_EXPR is the
5427 logical-or-expression that started the conditional-expression.
5428 Returns a representation of the entire conditional-expression.
5430 This routine exists only so that it can be shared between
5431 cp_parser_conditional_expression and
5432 cp_parser_assignment_expression.
5434 ? expression : assignment-expression
5438 ? : assignment-expression */
5441 cp_parser_question_colon_clause (parser, logical_or_expr)
5443 tree logical_or_expr;
5446 tree assignment_expr;
5448 /* Consume the `?' token. */
5449 cp_lexer_consume_token (parser->lexer);
5450 if (cp_parser_allow_gnu_extensions_p (parser)
5451 && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
5452 /* Implicit true clause. */
5455 /* Parse the expression. */
5456 expr = cp_parser_expression (parser);
5458 /* The next token should be a `:'. */
5459 cp_parser_require (parser, CPP_COLON, "`:'");
5460 /* Parse the assignment-expression. */
5461 assignment_expr = cp_parser_assignment_expression (parser);
5463 /* Build the conditional-expression. */
5464 return build_x_conditional_expr (logical_or_expr,
5469 /* Parse an assignment-expression.
5471 assignment-expression:
5472 conditional-expression
5473 logical-or-expression assignment-operator assignment_expression
5476 Returns a representation for the expression. */
5479 cp_parser_assignment_expression (parser)
5484 /* If the next token is the `throw' keyword, then we're looking at
5485 a throw-expression. */
5486 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
5487 expr = cp_parser_throw_expression (parser);
5488 /* Otherwise, it must be that we are looking at a
5489 logical-or-expression. */
5492 /* Parse the logical-or-expression. */
5493 expr = cp_parser_logical_or_expression (parser);
5494 /* If the next token is a `?' then we're actually looking at a
5495 conditional-expression. */
5496 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
5497 return cp_parser_question_colon_clause (parser, expr);
5500 enum tree_code assignment_operator;
5502 /* If it's an assignment-operator, we're using the second
5505 = cp_parser_assignment_operator_opt (parser);
5506 if (assignment_operator != ERROR_MARK)
5510 /* Parse the right-hand side of the assignment. */
5511 rhs = cp_parser_assignment_expression (parser);
5512 /* Build the asignment expression. */
5513 expr = build_x_modify_expr (expr,
5514 assignment_operator,
5523 /* Parse an (optional) assignment-operator.
5525 assignment-operator: one of
5526 = *= /= %= += -= >>= <<= &= ^= |=
5530 assignment-operator: one of
5533 If the next token is an assignment operator, the corresponding tree
5534 code is returned, and the token is consumed. For example, for
5535 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
5536 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
5537 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
5538 operator, ERROR_MARK is returned. */
5540 static enum tree_code
5541 cp_parser_assignment_operator_opt (parser)
5547 /* Peek at the next toen. */
5548 token = cp_lexer_peek_token (parser->lexer);
5550 switch (token->type)
5561 op = TRUNC_DIV_EXPR;
5565 op = TRUNC_MOD_EXPR;
5605 /* Nothing else is an assignment operator. */
5609 /* If it was an assignment operator, consume it. */
5610 if (op != ERROR_MARK)
5611 cp_lexer_consume_token (parser->lexer);
5616 /* Parse an expression.
5619 assignment-expression
5620 expression , assignment-expression
5622 Returns a representation of the expression. */
5625 cp_parser_expression (parser)
5628 tree expression = NULL_TREE;
5629 bool saw_comma_p = false;
5633 tree assignment_expression;
5635 /* Parse the next assignment-expression. */
5636 assignment_expression
5637 = cp_parser_assignment_expression (parser);
5638 /* If this is the first assignment-expression, we can just
5641 expression = assignment_expression;
5642 /* Otherwise, chain the expressions together. It is unclear why
5643 we do not simply build COMPOUND_EXPRs as we go. */
5645 expression = tree_cons (NULL_TREE,
5646 assignment_expression,
5648 /* If the next token is not a comma, then we are done with the
5650 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5652 /* Consume the `,'. */
5653 cp_lexer_consume_token (parser->lexer);
5654 /* The first time we see a `,', we must take special action
5655 because the representation used for a single expression is
5656 different from that used for a list containing the single
5660 /* Remember that this expression has a `,' in it. */
5662 /* Turn the EXPRESSION into a TREE_LIST so that we can link
5663 additional expressions to it. */
5664 expression = build_tree_list (NULL_TREE, expression);
5668 /* Build a COMPOUND_EXPR to represent the entire expression, if
5669 necessary. We built up the list in reverse order, so we must
5670 straighten it out here. */
5672 expression = build_x_compound_expr (nreverse (expression));
5677 /* Parse a constant-expression.
5679 constant-expression:
5680 conditional-expression */
5683 cp_parser_constant_expression (parser)
5686 bool saved_constant_expression_p;
5689 /* It might seem that we could simply parse the
5690 conditional-expression, and then check to see if it were
5691 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
5692 one that the compiler can figure out is constant, possibly after
5693 doing some simplifications or optimizations. The standard has a
5694 precise definition of constant-expression, and we must honor
5695 that, even though it is somewhat more restrictive.
5701 is not a legal declaration, because `(2, 3)' is not a
5702 constant-expression. The `,' operator is forbidden in a
5703 constant-expression. However, GCC's constant-folding machinery
5704 will fold this operation to an INTEGER_CST for `3'. */
5706 /* Save the old setting of CONSTANT_EXPRESSION_P. */
5707 saved_constant_expression_p = parser->constant_expression_p;
5708 /* We are now parsing a constant-expression. */
5709 parser->constant_expression_p = true;
5710 /* Parse the conditional-expression. */
5711 expression = cp_parser_conditional_expression (parser);
5712 /* Restore the old setting of CONSTANT_EXPRESSION_P. */
5713 parser->constant_expression_p = saved_constant_expression_p;
5718 /* Statements [gram.stmt.stmt] */
5720 /* Parse a statement.
5724 expression-statement
5729 declaration-statement
5733 cp_parser_statement (parser)
5738 int statement_line_number;
5740 /* There is no statement yet. */
5741 statement = NULL_TREE;
5742 /* Peek at the next token. */
5743 token = cp_lexer_peek_token (parser->lexer);
5744 /* Remember the line number of the first token in the statement. */
5745 statement_line_number = token->line_number;
5746 /* If this is a keyword, then that will often determine what kind of
5747 statement we have. */
5748 if (token->type == CPP_KEYWORD)
5750 enum rid keyword = token->keyword;
5756 statement = cp_parser_labeled_statement (parser);
5761 statement = cp_parser_selection_statement (parser);
5767 statement = cp_parser_iteration_statement (parser);
5774 statement = cp_parser_jump_statement (parser);
5778 statement = cp_parser_try_block (parser);
5782 /* It might be a keyword like `int' that can start a
5783 declaration-statement. */
5787 else if (token->type == CPP_NAME)
5789 /* If the next token is a `:', then we are looking at a
5790 labeled-statement. */
5791 token = cp_lexer_peek_nth_token (parser->lexer, 2);
5792 if (token->type == CPP_COLON)
5793 statement = cp_parser_labeled_statement (parser);
5795 /* Anything that starts with a `{' must be a compound-statement. */
5796 else if (token->type == CPP_OPEN_BRACE)
5797 statement = cp_parser_compound_statement (parser);
5799 /* Everything else must be a declaration-statement or an
5800 expression-statement. Try for the declaration-statement
5801 first, unless we are looking at a `;', in which case we know that
5802 we have an expression-statement. */
5805 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
5807 cp_parser_parse_tentatively (parser);
5808 /* Try to parse the declaration-statement. */
5809 cp_parser_declaration_statement (parser);
5810 /* If that worked, we're done. */
5811 if (cp_parser_parse_definitely (parser))
5814 /* Look for an expression-statement instead. */
5815 statement = cp_parser_expression_statement (parser);
5818 /* Set the line number for the statement. */
5819 if (statement && statement_code_p (TREE_CODE (statement)))
5820 STMT_LINENO (statement) = statement_line_number;
5823 /* Parse a labeled-statement.
5826 identifier : statement
5827 case constant-expression : statement
5830 Returns the new CASE_LABEL, for a `case' or `default' label. For
5831 an ordinary label, returns a LABEL_STMT. */
5834 cp_parser_labeled_statement (parser)
5838 tree statement = NULL_TREE;
5840 /* The next token should be an identifier. */
5841 token = cp_lexer_peek_token (parser->lexer);
5842 if (token->type != CPP_NAME
5843 && token->type != CPP_KEYWORD)
5845 cp_parser_error (parser, "expected labeled-statement");
5846 return error_mark_node;
5849 switch (token->keyword)
5855 /* Consume the `case' token. */
5856 cp_lexer_consume_token (parser->lexer);
5857 /* Parse the constant-expression. */
5858 expr = cp_parser_constant_expression (parser);
5859 /* Create the label. */
5860 statement = finish_case_label (expr, NULL_TREE);
5865 /* Consume the `default' token. */
5866 cp_lexer_consume_token (parser->lexer);
5867 /* Create the label. */
5868 statement = finish_case_label (NULL_TREE, NULL_TREE);
5872 /* Anything else must be an ordinary label. */
5873 statement = finish_label_stmt (cp_parser_identifier (parser));
5877 /* Require the `:' token. */
5878 cp_parser_require (parser, CPP_COLON, "`:'");
5879 /* Parse the labeled statement. */
5880 cp_parser_statement (parser);
5882 /* Return the label, in the case of a `case' or `default' label. */
5886 /* Parse an expression-statement.
5888 expression-statement:
5891 Returns the new EXPR_STMT -- or NULL_TREE if the expression
5892 statement consists of nothing more than an `;'. */
5895 cp_parser_expression_statement (parser)
5900 /* If the next token is not a `;', then there is an expression to parse. */
5901 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
5902 statement = finish_expr_stmt (cp_parser_expression (parser));
5903 /* Otherwise, we do not even bother to build an EXPR_STMT. */
5907 statement = NULL_TREE;
5909 /* Consume the final `;'. */
5910 if (!cp_parser_require (parser, CPP_SEMICOLON, "`;'"))
5912 /* If there is additional (erroneous) input, skip to the end of
5914 cp_parser_skip_to_end_of_statement (parser);
5915 /* If the next token is now a `;', consume it. */
5916 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
5917 cp_lexer_consume_token (parser->lexer);
5923 /* Parse a compound-statement.
5926 { statement-seq [opt] }
5928 Returns a COMPOUND_STMT representing the statement. */
5931 cp_parser_compound_statement (cp_parser *parser)
5935 /* Consume the `{'. */
5936 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
5937 return error_mark_node;
5938 /* Begin the compound-statement. */
5939 compound_stmt = begin_compound_stmt (/*has_no_scope=*/0);
5940 /* Parse an (optional) statement-seq. */
5941 cp_parser_statement_seq_opt (parser);
5942 /* Finish the compound-statement. */
5943 finish_compound_stmt (/*has_no_scope=*/0, compound_stmt);
5944 /* Consume the `}'. */
5945 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
5947 return compound_stmt;
5950 /* Parse an (optional) statement-seq.
5954 statement-seq [opt] statement */
5957 cp_parser_statement_seq_opt (parser)
5960 /* Scan statements until there aren't any more. */
5963 /* If we're looking at a `}', then we've run out of statements. */
5964 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE)
5965 || cp_lexer_next_token_is (parser->lexer, CPP_EOF))
5968 /* Parse the statement. */
5969 cp_parser_statement (parser);
5973 /* Parse a selection-statement.
5975 selection-statement:
5976 if ( condition ) statement
5977 if ( condition ) statement else statement
5978 switch ( condition ) statement
5980 Returns the new IF_STMT or SWITCH_STMT. */
5983 cp_parser_selection_statement (parser)
5989 /* Peek at the next token. */
5990 token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
5992 /* See what kind of keyword it is. */
5993 keyword = token->keyword;
6002 /* Look for the `('. */
6003 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
6005 cp_parser_skip_to_end_of_statement (parser);
6006 return error_mark_node;
6009 /* Begin the selection-statement. */
6010 if (keyword == RID_IF)
6011 statement = begin_if_stmt ();
6013 statement = begin_switch_stmt ();
6015 /* Parse the condition. */
6016 condition = cp_parser_condition (parser);
6017 /* Look for the `)'. */
6018 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
6019 cp_parser_skip_to_closing_parenthesis (parser);
6021 if (keyword == RID_IF)
6025 /* Add the condition. */
6026 finish_if_stmt_cond (condition, statement);
6028 /* Parse the then-clause. */
6029 then_stmt = cp_parser_implicitly_scoped_statement (parser);
6030 finish_then_clause (statement);
6032 /* If the next token is `else', parse the else-clause. */
6033 if (cp_lexer_next_token_is_keyword (parser->lexer,
6038 /* Consume the `else' keyword. */
6039 cp_lexer_consume_token (parser->lexer);
6040 /* Parse the else-clause. */
6042 = cp_parser_implicitly_scoped_statement (parser);
6043 finish_else_clause (statement);
6046 /* Now we're all done with the if-statement. */
6053 /* Add the condition. */
6054 finish_switch_cond (condition, statement);
6056 /* Parse the body of the switch-statement. */
6057 body = cp_parser_implicitly_scoped_statement (parser);
6059 /* Now we're all done with the switch-statement. */
6060 finish_switch_stmt (statement);
6068 cp_parser_error (parser, "expected selection-statement");
6069 return error_mark_node;
6073 /* Parse a condition.
6077 type-specifier-seq declarator = assignment-expression
6082 type-specifier-seq declarator asm-specification [opt]
6083 attributes [opt] = assignment-expression
6085 Returns the expression that should be tested. */
6088 cp_parser_condition (parser)
6091 tree type_specifiers;
6092 const char *saved_message;
6094 /* Try the declaration first. */
6095 cp_parser_parse_tentatively (parser);
6096 /* New types are not allowed in the type-specifier-seq for a
6098 saved_message = parser->type_definition_forbidden_message;
6099 parser->type_definition_forbidden_message
6100 = "types may not be defined in conditions";
6101 /* Parse the type-specifier-seq. */
6102 type_specifiers = cp_parser_type_specifier_seq (parser);
6103 /* Restore the saved message. */
6104 parser->type_definition_forbidden_message = saved_message;
6105 /* If all is well, we might be looking at a declaration. */
6106 if (!cp_parser_error_occurred (parser))
6109 tree asm_specification;
6112 tree initializer = NULL_TREE;
6114 /* Parse the declarator. */
6115 declarator = cp_parser_declarator (parser,
6116 /*abstract_p=*/false,
6117 /*ctor_dtor_or_conv_p=*/NULL);
6118 /* Parse the attributes. */
6119 attributes = cp_parser_attributes_opt (parser);
6120 /* Parse the asm-specification. */
6121 asm_specification = cp_parser_asm_specification_opt (parser);
6122 /* If the next token is not an `=', then we might still be
6123 looking at an expression. For example:
6127 looks like a decl-specifier-seq and a declarator -- but then
6128 there is no `=', so this is an expression. */
6129 cp_parser_require (parser, CPP_EQ, "`='");
6130 /* If we did see an `=', then we are looking at a declaration
6132 if (cp_parser_parse_definitely (parser))
6134 /* Create the declaration. */
6135 decl = start_decl (declarator, type_specifiers,
6136 /*initialized_p=*/true,
6137 attributes, /*prefix_attributes=*/NULL_TREE);
6138 /* Parse the assignment-expression. */
6139 initializer = cp_parser_assignment_expression (parser);
6141 /* Process the initializer. */
6142 cp_finish_decl (decl,
6145 LOOKUP_ONLYCONVERTING);
6147 return convert_from_reference (decl);
6150 /* If we didn't even get past the declarator successfully, we are
6151 definitely not looking at a declaration. */
6153 cp_parser_abort_tentative_parse (parser);
6155 /* Otherwise, we are looking at an expression. */
6156 return cp_parser_expression (parser);
6159 /* Parse an iteration-statement.
6161 iteration-statement:
6162 while ( condition ) statement
6163 do statement while ( expression ) ;
6164 for ( for-init-statement condition [opt] ; expression [opt] )
6167 Returns the new WHILE_STMT, DO_STMT, or FOR_STMT. */
6170 cp_parser_iteration_statement (parser)
6177 /* Peek at the next token. */
6178 token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
6180 return error_mark_node;
6182 /* See what kind of keyword it is. */
6183 keyword = token->keyword;
6190 /* Begin the while-statement. */
6191 statement = begin_while_stmt ();
6192 /* Look for the `('. */
6193 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6194 /* Parse the condition. */
6195 condition = cp_parser_condition (parser);
6196 finish_while_stmt_cond (condition, statement);
6197 /* Look for the `)'. */
6198 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6199 /* Parse the dependent statement. */
6200 cp_parser_already_scoped_statement (parser);
6201 /* We're done with the while-statement. */
6202 finish_while_stmt (statement);
6210 /* Begin the do-statement. */
6211 statement = begin_do_stmt ();
6212 /* Parse the body of the do-statement. */
6213 cp_parser_implicitly_scoped_statement (parser);
6214 finish_do_body (statement);
6215 /* Look for the `while' keyword. */
6216 cp_parser_require_keyword (parser, RID_WHILE, "`while'");
6217 /* Look for the `('. */
6218 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6219 /* Parse the expression. */
6220 expression = cp_parser_expression (parser);
6221 /* We're done with the do-statement. */
6222 finish_do_stmt (expression, statement);
6223 /* Look for the `)'. */
6224 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6225 /* Look for the `;'. */
6226 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6232 tree condition = NULL_TREE;
6233 tree expression = NULL_TREE;
6235 /* Begin the for-statement. */
6236 statement = begin_for_stmt ();
6237 /* Look for the `('. */
6238 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6239 /* Parse the initialization. */
6240 cp_parser_for_init_statement (parser);
6241 finish_for_init_stmt (statement);
6243 /* If there's a condition, process it. */
6244 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6245 condition = cp_parser_condition (parser);
6246 finish_for_cond (condition, statement);
6247 /* Look for the `;'. */
6248 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6250 /* If there's an expression, process it. */
6251 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
6252 expression = cp_parser_expression (parser);
6253 finish_for_expr (expression, statement);
6254 /* Look for the `)'. */
6255 cp_parser_require (parser, CPP_CLOSE_PAREN, "`;'");
6257 /* Parse the body of the for-statement. */
6258 cp_parser_already_scoped_statement (parser);
6260 /* We're done with the for-statement. */
6261 finish_for_stmt (statement);
6266 cp_parser_error (parser, "expected iteration-statement");
6267 statement = error_mark_node;
6274 /* Parse a for-init-statement.
6277 expression-statement
6278 simple-declaration */
6281 cp_parser_for_init_statement (parser)
6284 /* If the next token is a `;', then we have an empty
6285 expression-statement. Gramatically, this is also a
6286 simple-declaration, but an invalid one, because it does not
6287 declare anything. Therefore, if we did not handle this case
6288 specially, we would issue an error message about an invalid
6290 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6292 /* We're going to speculatively look for a declaration, falling back
6293 to an expression, if necessary. */
6294 cp_parser_parse_tentatively (parser);
6295 /* Parse the declaration. */
6296 cp_parser_simple_declaration (parser,
6297 /*function_definition_allowed_p=*/false);
6298 /* If the tentative parse failed, then we shall need to look for an
6299 expression-statement. */
6300 if (cp_parser_parse_definitely (parser))
6304 cp_parser_expression_statement (parser);
6307 /* Parse a jump-statement.
6312 return expression [opt] ;
6320 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_STMT, or
6324 cp_parser_jump_statement (parser)
6327 tree statement = error_mark_node;
6331 /* Peek at the next token. */
6332 token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
6334 return error_mark_node;
6336 /* See what kind of keyword it is. */
6337 keyword = token->keyword;
6341 statement = finish_break_stmt ();
6342 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6346 statement = finish_continue_stmt ();
6347 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6354 /* If the next token is a `;', then there is no
6356 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6357 expr = cp_parser_expression (parser);
6360 /* Build the return-statement. */
6361 statement = finish_return_stmt (expr);
6362 /* Look for the final `;'. */
6363 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6368 /* Create the goto-statement. */
6369 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
6371 /* Issue a warning about this use of a GNU extension. */
6373 pedwarn ("ISO C++ forbids computed gotos");
6374 /* Consume the '*' token. */
6375 cp_lexer_consume_token (parser->lexer);
6376 /* Parse the dependent expression. */
6377 finish_goto_stmt (cp_parser_expression (parser));
6380 finish_goto_stmt (cp_parser_identifier (parser));
6381 /* Look for the final `;'. */
6382 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6386 cp_parser_error (parser, "expected jump-statement");
6393 /* Parse a declaration-statement.
6395 declaration-statement:
6396 block-declaration */
6399 cp_parser_declaration_statement (parser)
6402 /* Parse the block-declaration. */
6403 cp_parser_block_declaration (parser, /*statement_p=*/true);
6405 /* Finish off the statement. */
6409 /* Some dependent statements (like `if (cond) statement'), are
6410 implicitly in their own scope. In other words, if the statement is
6411 a single statement (as opposed to a compound-statement), it is
6412 none-the-less treated as if it were enclosed in braces. Any
6413 declarations appearing in the dependent statement are out of scope
6414 after control passes that point. This function parses a statement,
6415 but ensures that is in its own scope, even if it is not a
6418 Returns the new statement. */
6421 cp_parser_implicitly_scoped_statement (parser)
6426 /* If the token is not a `{', then we must take special action. */
6427 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
6429 /* Create a compound-statement. */
6430 statement = begin_compound_stmt (/*has_no_scope=*/0);
6431 /* Parse the dependent-statement. */
6432 cp_parser_statement (parser);
6433 /* Finish the dummy compound-statement. */
6434 finish_compound_stmt (/*has_no_scope=*/0, statement);
6436 /* Otherwise, we simply parse the statement directly. */
6438 statement = cp_parser_compound_statement (parser);
6440 /* Return the statement. */
6444 /* For some dependent statements (like `while (cond) statement'), we
6445 have already created a scope. Therefore, even if the dependent
6446 statement is a compound-statement, we do not want to create another
6450 cp_parser_already_scoped_statement (parser)
6453 /* If the token is not a `{', then we must take special action. */
6454 if (cp_lexer_next_token_is_not(parser->lexer, CPP_OPEN_BRACE))
6458 /* Create a compound-statement. */
6459 statement = begin_compound_stmt (/*has_no_scope=*/1);
6460 /* Parse the dependent-statement. */
6461 cp_parser_statement (parser);
6462 /* Finish the dummy compound-statement. */
6463 finish_compound_stmt (/*has_no_scope=*/1, statement);
6465 /* Otherwise, we simply parse the statement directly. */
6467 cp_parser_statement (parser);
6470 /* Declarations [gram.dcl.dcl] */
6472 /* Parse an optional declaration-sequence.
6476 declaration-seq declaration */
6479 cp_parser_declaration_seq_opt (parser)
6486 token = cp_lexer_peek_token (parser->lexer);
6488 if (token->type == CPP_CLOSE_BRACE
6489 || token->type == CPP_EOF)
6492 if (token->type == CPP_SEMICOLON)
6494 /* A declaration consisting of a single semicolon is
6495 invalid. Allow it unless we're being pedantic. */
6497 pedwarn ("extra `;'");
6498 cp_lexer_consume_token (parser->lexer);
6502 /* The C lexer modifies PENDING_LANG_CHANGE when it wants the
6503 parser to enter or exit implict `extern "C"' blocks. */
6504 while (pending_lang_change > 0)
6506 push_lang_context (lang_name_c);
6507 --pending_lang_change;
6509 while (pending_lang_change < 0)
6511 pop_lang_context ();
6512 ++pending_lang_change;
6515 /* Parse the declaration itself. */
6516 cp_parser_declaration (parser);
6520 /* Parse a declaration.
6525 template-declaration
6526 explicit-instantiation
6527 explicit-specialization
6528 linkage-specification
6529 namespace-definition */
6532 cp_parser_declaration (parser)
6538 /* Try to figure out what kind of declaration is present. */
6539 token1 = *cp_lexer_peek_token (parser->lexer);
6540 if (token1.type != CPP_EOF)
6541 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
6543 /* If the next token is `extern' and the following token is a string
6544 literal, then we have a linkage specification. */
6545 if (token1.keyword == RID_EXTERN
6546 && cp_parser_is_string_literal (&token2))
6547 cp_parser_linkage_specification (parser);
6548 /* If the next token is `template', then we have either a template
6549 declaration, an explicit instantiation, or an explicit
6551 else if (token1.keyword == RID_TEMPLATE)
6553 /* `template <>' indicates a template specialization. */
6554 if (token2.type == CPP_LESS
6555 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
6556 cp_parser_explicit_specialization (parser);
6557 /* `template <' indicates a template declaration. */
6558 else if (token2.type == CPP_LESS)
6559 cp_parser_template_declaration (parser, /*member_p=*/false);
6560 /* Anything else must be an explicit instantiation. */
6562 cp_parser_explicit_instantiation (parser);
6564 /* If the next token is `export', then we have a template
6566 else if (token1.keyword == RID_EXPORT)
6567 cp_parser_template_declaration (parser, /*member_p=*/false);
6568 /* If the next token is `extern', 'static' or 'inline' and the one
6569 after that is `template', we have a GNU extended explicit
6570 instantiation directive. */
6571 else if (cp_parser_allow_gnu_extensions_p (parser)
6572 && (token1.keyword == RID_EXTERN
6573 || token1.keyword == RID_STATIC
6574 || token1.keyword == RID_INLINE)
6575 && token2.keyword == RID_TEMPLATE)
6576 cp_parser_explicit_instantiation (parser);
6577 /* If the next token is `namespace', check for a named or unnamed
6578 namespace definition. */
6579 else if (token1.keyword == RID_NAMESPACE
6580 && (/* A named namespace definition. */
6581 (token2.type == CPP_NAME
6582 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
6584 /* An unnamed namespace definition. */
6585 || token2.type == CPP_OPEN_BRACE))
6586 cp_parser_namespace_definition (parser);
6587 /* We must have either a block declaration or a function
6590 /* Try to parse a block-declaration, or a function-definition. */
6591 cp_parser_block_declaration (parser, /*statement_p=*/false);
6594 /* Parse a block-declaration.
6599 namespace-alias-definition
6606 __extension__ block-declaration
6609 If STATEMENT_P is TRUE, then this block-declaration is ocurring as
6610 part of a declaration-statement. */
6613 cp_parser_block_declaration (cp_parser *parser,
6619 /* Check for the `__extension__' keyword. */
6620 if (cp_parser_extension_opt (parser, &saved_pedantic))
6622 /* Parse the qualified declaration. */
6623 cp_parser_block_declaration (parser, statement_p);
6624 /* Restore the PEDANTIC flag. */
6625 pedantic = saved_pedantic;
6630 /* Peek at the next token to figure out which kind of declaration is
6632 token1 = cp_lexer_peek_token (parser->lexer);
6634 /* If the next keyword is `asm', we have an asm-definition. */
6635 if (token1->keyword == RID_ASM)
6638 cp_parser_commit_to_tentative_parse (parser);
6639 cp_parser_asm_definition (parser);
6641 /* If the next keyword is `namespace', we have a
6642 namespace-alias-definition. */
6643 else if (token1->keyword == RID_NAMESPACE)
6644 cp_parser_namespace_alias_definition (parser);
6645 /* If the next keyword is `using', we have either a
6646 using-declaration or a using-directive. */
6647 else if (token1->keyword == RID_USING)
6652 cp_parser_commit_to_tentative_parse (parser);
6653 /* If the token after `using' is `namespace', then we have a
6655 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
6656 if (token2->keyword == RID_NAMESPACE)
6657 cp_parser_using_directive (parser);
6658 /* Otherwise, it's a using-declaration. */
6660 cp_parser_using_declaration (parser);
6662 /* If the next keyword is `__label__' we have a label declaration. */
6663 else if (token1->keyword == RID_LABEL)
6666 cp_parser_commit_to_tentative_parse (parser);
6667 cp_parser_label_declaration (parser);
6669 /* Anything else must be a simple-declaration. */
6671 cp_parser_simple_declaration (parser, !statement_p);
6674 /* Parse a simple-declaration.
6677 decl-specifier-seq [opt] init-declarator-list [opt] ;
6679 init-declarator-list:
6681 init-declarator-list , init-declarator
6683 If FUNCTION_DEFINTION_ALLOWED_P is TRUE, then we also recognize a
6684 function-definition as a simple-declaration. */
6687 cp_parser_simple_declaration (parser, function_definition_allowed_p)
6689 bool function_definition_allowed_p;
6691 tree decl_specifiers;
6694 bool declares_class_or_enum;
6695 bool saw_declarator;
6697 /* Defer access checks until we know what is being declared; the
6698 checks for names appearing in the decl-specifier-seq should be
6699 done as if we were in the scope of the thing being declared. */
6700 cp_parser_start_deferring_access_checks (parser);
6701 /* Parse the decl-specifier-seq. We have to keep track of whether
6702 or not the decl-specifier-seq declares a named class or
6703 enumeration type, since that is the only case in which the
6704 init-declarator-list is allowed to be empty.
6708 In a simple-declaration, the optional init-declarator-list can be
6709 omitted only when declaring a class or enumeration, that is when
6710 the decl-specifier-seq contains either a class-specifier, an
6711 elaborated-type-specifier, or an enum-specifier. */
6713 = cp_parser_decl_specifier_seq (parser,
6714 CP_PARSER_FLAGS_OPTIONAL,
6716 &declares_class_or_enum);
6717 /* We no longer need to defer access checks. */
6718 access_checks = cp_parser_stop_deferring_access_checks (parser);
6720 /* Keep going until we hit the `;' at the end of the simple
6722 saw_declarator = false;
6723 while (cp_lexer_next_token_is_not (parser->lexer,
6727 bool function_definition_p;
6729 saw_declarator = true;
6730 /* Parse the init-declarator. */
6731 cp_parser_init_declarator (parser, decl_specifiers, attributes,
6733 function_definition_allowed_p,
6735 &function_definition_p);
6736 /* Handle function definitions specially. */
6737 if (function_definition_p)
6739 /* If the next token is a `,', then we are probably
6740 processing something like:
6744 which is erroneous. */
6745 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
6746 error ("mixing declarations and function-definitions is forbidden");
6747 /* Otherwise, we're done with the list of declarators. */
6751 /* The next token should be either a `,' or a `;'. */
6752 token = cp_lexer_peek_token (parser->lexer);
6753 /* If it's a `,', there are more declarators to come. */
6754 if (token->type == CPP_COMMA)
6755 cp_lexer_consume_token (parser->lexer);
6756 /* If it's a `;', we are done. */
6757 else if (token->type == CPP_SEMICOLON)
6759 /* Anything else is an error. */
6762 cp_parser_error (parser, "expected `,' or `;'");
6763 /* Skip tokens until we reach the end of the statement. */
6764 cp_parser_skip_to_end_of_statement (parser);
6767 /* After the first time around, a function-definition is not
6768 allowed -- even if it was OK at first. For example:
6773 function_definition_allowed_p = false;
6776 /* Issue an error message if no declarators are present, and the
6777 decl-specifier-seq does not itself declare a class or
6779 if (!saw_declarator)
6781 if (cp_parser_declares_only_class_p (parser))
6782 shadow_tag (decl_specifiers);
6783 /* Perform any deferred access checks. */
6784 cp_parser_perform_deferred_access_checks (access_checks);
6787 /* Consume the `;'. */
6788 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6790 /* Mark all the classes that appeared in the decl-specifier-seq as
6791 having received a `;'. */
6792 note_list_got_semicolon (decl_specifiers);
6795 /* Parse a decl-specifier-seq.
6798 decl-specifier-seq [opt] decl-specifier
6801 storage-class-specifier
6810 decl-specifier-seq [opt] attributes
6812 Returns a TREE_LIST, giving the decl-specifiers in the order they
6813 appear in the source code. The TREE_VALUE of each node is the
6814 decl-specifier. For a keyword (such as `auto' or `friend'), the
6815 TREE_VALUE is simply the correspoding TREE_IDENTIFIER. For the
6816 representation of a type-specifier, see cp_parser_type_specifier.
6818 If there are attributes, they will be stored in *ATTRIBUTES,
6819 represented as described above cp_parser_attributes.
6821 If FRIEND_IS_NOT_CLASS_P is non-NULL, and the `friend' specifier
6822 appears, and the entity that will be a friend is not going to be a
6823 class, then *FRIEND_IS_NOT_CLASS_P will be set to TRUE. Note that
6824 even if *FRIEND_IS_NOT_CLASS_P is FALSE, the entity to which
6825 friendship is granted might not be a class. */
6828 cp_parser_decl_specifier_seq (parser, flags, attributes,
6829 declares_class_or_enum)
6831 cp_parser_flags flags;
6833 bool *declares_class_or_enum;
6835 tree decl_specs = NULL_TREE;
6836 bool friend_p = false;
6838 /* Assume no class or enumeration type is declared. */
6839 *declares_class_or_enum = false;
6841 /* Assume there are no attributes. */
6842 *attributes = NULL_TREE;
6844 /* Keep reading specifiers until there are no more to read. */
6847 tree decl_spec = NULL_TREE;
6851 /* Peek at the next token. */
6852 token = cp_lexer_peek_token (parser->lexer);
6853 /* Handle attributes. */
6854 if (token->keyword == RID_ATTRIBUTE)
6856 /* Parse the attributes. */
6857 decl_spec = cp_parser_attributes_opt (parser);
6858 /* Add them to the list. */
6859 *attributes = chainon (*attributes, decl_spec);
6862 /* If the next token is an appropriate keyword, we can simply
6863 add it to the list. */
6864 switch (token->keyword)
6870 /* The representation of the specifier is simply the
6871 appropriate TREE_IDENTIFIER node. */
6872 decl_spec = token->value;
6873 /* Consume the token. */
6874 cp_lexer_consume_token (parser->lexer);
6877 /* function-specifier:
6884 decl_spec = cp_parser_function_specifier_opt (parser);
6890 /* The representation of the specifier is simply the
6891 appropriate TREE_IDENTIFIER node. */
6892 decl_spec = token->value;
6893 /* Consume the token. */
6894 cp_lexer_consume_token (parser->lexer);
6897 /* storage-class-specifier:
6912 decl_spec = cp_parser_storage_class_specifier_opt (parser);
6919 /* Constructors are a special case. The `S' in `S()' is not a
6920 decl-specifier; it is the beginning of the declarator. */
6921 constructor_p = (!decl_spec
6922 && cp_parser_constructor_declarator_p (parser,
6925 /* If we don't have a DECL_SPEC yet, then we must be looking at
6926 a type-specifier. */
6927 if (!decl_spec && !constructor_p)
6929 bool decl_spec_declares_class_or_enum;
6930 bool is_cv_qualifier;
6933 = cp_parser_type_specifier (parser, flags,
6935 /*is_declaration=*/true,
6936 &decl_spec_declares_class_or_enum,
6939 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
6941 /* If this type-specifier referenced a user-defined type
6942 (a typedef, class-name, etc.), then we can't allow any
6943 more such type-specifiers henceforth.
6947 The longest sequence of decl-specifiers that could
6948 possibly be a type name is taken as the
6949 decl-specifier-seq of a declaration. The sequence shall
6950 be self-consistent as described below.
6954 As a general rule, at most one type-specifier is allowed
6955 in the complete decl-specifier-seq of a declaration. The
6956 only exceptions are the following:
6958 -- const or volatile can be combined with any other
6961 -- signed or unsigned can be combined with char, long,
6969 void g (const int Pc);
6971 Here, Pc is *not* part of the decl-specifier seq; it's
6972 the declarator. Therefore, once we see a type-specifier
6973 (other than a cv-qualifier), we forbid any additional
6974 user-defined types. We *do* still allow things like `int
6975 int' to be considered a decl-specifier-seq, and issue the
6976 error message later. */
6977 if (decl_spec && !is_cv_qualifier)
6978 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
6981 /* If we still do not have a DECL_SPEC, then there are no more
6985 /* Issue an error message, unless the entire construct was
6987 if (!(flags & CP_PARSER_FLAGS_OPTIONAL))
6989 cp_parser_error (parser, "expected decl specifier");
6990 return error_mark_node;
6996 /* Add the DECL_SPEC to the list of specifiers. */
6997 decl_specs = tree_cons (NULL_TREE, decl_spec, decl_specs);
6999 /* After we see one decl-specifier, further decl-specifiers are
7001 flags |= CP_PARSER_FLAGS_OPTIONAL;
7004 /* We have built up the DECL_SPECS in reverse order. Return them in
7005 the correct order. */
7006 return nreverse (decl_specs);
7009 /* Parse an (optional) storage-class-specifier.
7011 storage-class-specifier:
7020 storage-class-specifier:
7023 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
7026 cp_parser_storage_class_specifier_opt (parser)
7029 switch (cp_lexer_peek_token (parser->lexer)->keyword)
7037 /* Consume the token. */
7038 return cp_lexer_consume_token (parser->lexer)->value;
7045 /* Parse an (optional) function-specifier.
7052 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
7055 cp_parser_function_specifier_opt (parser)
7058 switch (cp_lexer_peek_token (parser->lexer)->keyword)
7063 /* Consume the token. */
7064 return cp_lexer_consume_token (parser->lexer)->value;
7071 /* Parse a linkage-specification.
7073 linkage-specification:
7074 extern string-literal { declaration-seq [opt] }
7075 extern string-literal declaration */
7078 cp_parser_linkage_specification (parser)
7084 /* Look for the `extern' keyword. */
7085 cp_parser_require_keyword (parser, RID_EXTERN, "`extern'");
7087 /* Peek at the next token. */
7088 token = cp_lexer_peek_token (parser->lexer);
7089 /* If it's not a string-literal, then there's a problem. */
7090 if (!cp_parser_is_string_literal (token))
7092 cp_parser_error (parser, "expected language-name");
7095 /* Consume the token. */
7096 cp_lexer_consume_token (parser->lexer);
7098 /* Transform the literal into an identifier. If the literal is a
7099 wide-character string, or contains embedded NULs, then we can't
7100 handle it as the user wants. */
7101 if (token->type == CPP_WSTRING
7102 || (strlen (TREE_STRING_POINTER (token->value))
7103 != (size_t) (TREE_STRING_LENGTH (token->value) - 1)))
7105 cp_parser_error (parser, "invalid linkage-specification");
7106 /* Assume C++ linkage. */
7107 linkage = get_identifier ("c++");
7109 /* If it's a simple string constant, things are easier. */
7111 linkage = get_identifier (TREE_STRING_POINTER (token->value));
7113 /* We're now using the new linkage. */
7114 push_lang_context (linkage);
7116 /* If the next token is a `{', then we're using the first
7118 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7120 /* Consume the `{' token. */
7121 cp_lexer_consume_token (parser->lexer);
7122 /* Parse the declarations. */
7123 cp_parser_declaration_seq_opt (parser);
7124 /* Look for the closing `}'. */
7125 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
7127 /* Otherwise, there's just one declaration. */
7130 bool saved_in_unbraced_linkage_specification_p;
7132 saved_in_unbraced_linkage_specification_p
7133 = parser->in_unbraced_linkage_specification_p;
7134 parser->in_unbraced_linkage_specification_p = true;
7135 have_extern_spec = true;
7136 cp_parser_declaration (parser);
7137 have_extern_spec = false;
7138 parser->in_unbraced_linkage_specification_p
7139 = saved_in_unbraced_linkage_specification_p;
7142 /* We're done with the linkage-specification. */
7143 pop_lang_context ();
7146 /* Special member functions [gram.special] */
7148 /* Parse a conversion-function-id.
7150 conversion-function-id:
7151 operator conversion-type-id
7153 Returns an IDENTIFIER_NODE representing the operator. */
7156 cp_parser_conversion_function_id (parser)
7161 tree saved_qualifying_scope;
7162 tree saved_object_scope;
7164 /* Look for the `operator' token. */
7165 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
7166 return error_mark_node;
7167 /* When we parse the conversion-type-id, the current scope will be
7168 reset. However, we need that information in able to look up the
7169 conversion function later, so we save it here. */
7170 saved_scope = parser->scope;
7171 saved_qualifying_scope = parser->qualifying_scope;
7172 saved_object_scope = parser->object_scope;
7173 /* We must enter the scope of the class so that the names of
7174 entities declared within the class are available in the
7175 conversion-type-id. For example, consider:
7182 S::operator I() { ... }
7184 In order to see that `I' is a type-name in the definition, we
7185 must be in the scope of `S'. */
7187 push_scope (saved_scope);
7188 /* Parse the conversion-type-id. */
7189 type = cp_parser_conversion_type_id (parser);
7190 /* Leave the scope of the class, if any. */
7192 pop_scope (saved_scope);
7193 /* Restore the saved scope. */
7194 parser->scope = saved_scope;
7195 parser->qualifying_scope = saved_qualifying_scope;
7196 parser->object_scope = saved_object_scope;
7197 /* If the TYPE is invalid, indicate failure. */
7198 if (type == error_mark_node)
7199 return error_mark_node;
7200 return mangle_conv_op_name_for_type (type);
7203 /* Parse a conversion-type-id:
7206 type-specifier-seq conversion-declarator [opt]
7208 Returns the TYPE specified. */
7211 cp_parser_conversion_type_id (parser)
7215 tree type_specifiers;
7218 /* Parse the attributes. */
7219 attributes = cp_parser_attributes_opt (parser);
7220 /* Parse the type-specifiers. */
7221 type_specifiers = cp_parser_type_specifier_seq (parser);
7222 /* If that didn't work, stop. */
7223 if (type_specifiers == error_mark_node)
7224 return error_mark_node;
7225 /* Parse the conversion-declarator. */
7226 declarator = cp_parser_conversion_declarator_opt (parser);
7228 return grokdeclarator (declarator, type_specifiers, TYPENAME,
7229 /*initialized=*/0, &attributes);
7232 /* Parse an (optional) conversion-declarator.
7234 conversion-declarator:
7235 ptr-operator conversion-declarator [opt]
7237 Returns a representation of the declarator. See
7238 cp_parser_declarator for details. */
7241 cp_parser_conversion_declarator_opt (parser)
7244 enum tree_code code;
7246 tree cv_qualifier_seq;
7248 /* We don't know if there's a ptr-operator next, or not. */
7249 cp_parser_parse_tentatively (parser);
7250 /* Try the ptr-operator. */
7251 code = cp_parser_ptr_operator (parser, &class_type,
7253 /* If it worked, look for more conversion-declarators. */
7254 if (cp_parser_parse_definitely (parser))
7258 /* Parse another optional declarator. */
7259 declarator = cp_parser_conversion_declarator_opt (parser);
7261 /* Create the representation of the declarator. */
7262 if (code == INDIRECT_REF)
7263 declarator = make_pointer_declarator (cv_qualifier_seq,
7266 declarator = make_reference_declarator (cv_qualifier_seq,
7269 /* Handle the pointer-to-member case. */
7271 declarator = build_nt (SCOPE_REF, class_type, declarator);
7279 /* Parse an (optional) ctor-initializer.
7282 : mem-initializer-list
7284 Returns TRUE iff the ctor-initializer was actually present. */
7287 cp_parser_ctor_initializer_opt (parser)
7290 /* If the next token is not a `:', then there is no
7291 ctor-initializer. */
7292 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
7294 /* Do default initialization of any bases and members. */
7295 if (DECL_CONSTRUCTOR_P (current_function_decl))
7296 finish_mem_initializers (NULL_TREE);
7301 /* Consume the `:' token. */
7302 cp_lexer_consume_token (parser->lexer);
7303 /* And the mem-initializer-list. */
7304 cp_parser_mem_initializer_list (parser);
7309 /* Parse a mem-initializer-list.
7311 mem-initializer-list:
7313 mem-initializer , mem-initializer-list */
7316 cp_parser_mem_initializer_list (parser)
7319 tree mem_initializer_list = NULL_TREE;
7321 /* Let the semantic analysis code know that we are starting the
7322 mem-initializer-list. */
7323 begin_mem_initializers ();
7325 /* Loop through the list. */
7328 tree mem_initializer;
7330 /* Parse the mem-initializer. */
7331 mem_initializer = cp_parser_mem_initializer (parser);
7332 /* Add it to the list, unless it was erroneous. */
7333 if (mem_initializer)
7335 TREE_CHAIN (mem_initializer) = mem_initializer_list;
7336 mem_initializer_list = mem_initializer;
7338 /* If the next token is not a `,', we're done. */
7339 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7341 /* Consume the `,' token. */
7342 cp_lexer_consume_token (parser->lexer);
7345 /* Perform semantic analysis. */
7346 finish_mem_initializers (mem_initializer_list);
7349 /* Parse a mem-initializer.
7352 mem-initializer-id ( expression-list [opt] )
7357 ( expresion-list [opt] )
7359 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
7360 class) or FIELD_DECL (for a non-static data member) to initialize;
7361 the TREE_VALUE is the expression-list. */
7364 cp_parser_mem_initializer (parser)
7367 tree mem_initializer_id;
7368 tree expression_list;
7370 /* Find out what is being initialized. */
7371 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7373 pedwarn ("anachronistic old-style base class initializer");
7374 mem_initializer_id = NULL_TREE;
7377 mem_initializer_id = cp_parser_mem_initializer_id (parser);
7378 /* Look for the opening `('. */
7379 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
7380 /* Parse the expression-list. */
7381 if (cp_lexer_next_token_is_not (parser->lexer,
7383 expression_list = cp_parser_expression_list (parser);
7385 expression_list = void_type_node;
7386 /* Look for the closing `)'. */
7387 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
7389 return expand_member_init (mem_initializer_id,
7393 /* Parse a mem-initializer-id.
7396 :: [opt] nested-name-specifier [opt] class-name
7399 Returns a TYPE indicating the class to be initializer for the first
7400 production. Returns an IDENTIFIER_NODE indicating the data member
7401 to be initialized for the second production. */
7404 cp_parser_mem_initializer_id (parser)
7407 bool global_scope_p;
7408 bool nested_name_specifier_p;
7411 /* Look for the optional `::' operator. */
7413 = (cp_parser_global_scope_opt (parser,
7414 /*current_scope_valid_p=*/false)
7416 /* Look for the optional nested-name-specifier. The simplest way to
7421 The keyword `typename' is not permitted in a base-specifier or
7422 mem-initializer; in these contexts a qualified name that
7423 depends on a template-parameter is implicitly assumed to be a
7426 is to assume that we have seen the `typename' keyword at this
7428 nested_name_specifier_p
7429 = (cp_parser_nested_name_specifier_opt (parser,
7430 /*typename_keyword_p=*/true,
7431 /*check_dependency_p=*/true,
7434 /* If there is a `::' operator or a nested-name-specifier, then we
7435 are definitely looking for a class-name. */
7436 if (global_scope_p || nested_name_specifier_p)
7437 return cp_parser_class_name (parser,
7438 /*typename_keyword_p=*/true,
7439 /*template_keyword_p=*/false,
7441 /*check_access_p=*/true,
7442 /*check_dependency_p=*/true,
7443 /*class_head_p=*/false);
7444 /* Otherwise, we could also be looking for an ordinary identifier. */
7445 cp_parser_parse_tentatively (parser);
7446 /* Try a class-name. */
7447 id = cp_parser_class_name (parser,
7448 /*typename_keyword_p=*/true,
7449 /*template_keyword_p=*/false,
7451 /*check_access_p=*/true,
7452 /*check_dependency_p=*/true,
7453 /*class_head_p=*/false);
7454 /* If we found one, we're done. */
7455 if (cp_parser_parse_definitely (parser))
7457 /* Otherwise, look for an ordinary identifier. */
7458 return cp_parser_identifier (parser);
7461 /* Overloading [gram.over] */
7463 /* Parse an operator-function-id.
7465 operator-function-id:
7468 Returns an IDENTIFIER_NODE for the operator which is a
7469 human-readable spelling of the identifier, e.g., `operator +'. */
7472 cp_parser_operator_function_id (parser)
7475 /* Look for the `operator' keyword. */
7476 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
7477 return error_mark_node;
7478 /* And then the name of the operator itself. */
7479 return cp_parser_operator (parser);
7482 /* Parse an operator.
7485 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
7486 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
7487 || ++ -- , ->* -> () []
7494 Returns an IDENTIFIER_NODE for the operator which is a
7495 human-readable spelling of the identifier, e.g., `operator +'. */
7498 cp_parser_operator (parser)
7501 tree id = NULL_TREE;
7504 /* Peek at the next token. */
7505 token = cp_lexer_peek_token (parser->lexer);
7506 /* Figure out which operator we have. */
7507 switch (token->type)
7513 /* The keyword should be either `new' or `delete'. */
7514 if (token->keyword == RID_NEW)
7516 else if (token->keyword == RID_DELETE)
7521 /* Consume the `new' or `delete' token. */
7522 cp_lexer_consume_token (parser->lexer);
7524 /* Peek at the next token. */
7525 token = cp_lexer_peek_token (parser->lexer);
7526 /* If it's a `[' token then this is the array variant of the
7528 if (token->type == CPP_OPEN_SQUARE)
7530 /* Consume the `[' token. */
7531 cp_lexer_consume_token (parser->lexer);
7532 /* Look for the `]' token. */
7533 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
7534 id = ansi_opname (op == NEW_EXPR
7535 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
7537 /* Otherwise, we have the non-array variant. */
7539 id = ansi_opname (op);
7545 id = ansi_opname (PLUS_EXPR);
7549 id = ansi_opname (MINUS_EXPR);
7553 id = ansi_opname (MULT_EXPR);
7557 id = ansi_opname (TRUNC_DIV_EXPR);
7561 id = ansi_opname (TRUNC_MOD_EXPR);
7565 id = ansi_opname (BIT_XOR_EXPR);
7569 id = ansi_opname (BIT_AND_EXPR);
7573 id = ansi_opname (BIT_IOR_EXPR);
7577 id = ansi_opname (BIT_NOT_EXPR);
7581 id = ansi_opname (TRUTH_NOT_EXPR);
7585 id = ansi_assopname (NOP_EXPR);
7589 id = ansi_opname (LT_EXPR);
7593 id = ansi_opname (GT_EXPR);
7597 id = ansi_assopname (PLUS_EXPR);
7601 id = ansi_assopname (MINUS_EXPR);
7605 id = ansi_assopname (MULT_EXPR);
7609 id = ansi_assopname (TRUNC_DIV_EXPR);
7613 id = ansi_assopname (TRUNC_MOD_EXPR);
7617 id = ansi_assopname (BIT_XOR_EXPR);
7621 id = ansi_assopname (BIT_AND_EXPR);
7625 id = ansi_assopname (BIT_IOR_EXPR);
7629 id = ansi_opname (LSHIFT_EXPR);
7633 id = ansi_opname (RSHIFT_EXPR);
7637 id = ansi_assopname (LSHIFT_EXPR);
7641 id = ansi_assopname (RSHIFT_EXPR);
7645 id = ansi_opname (EQ_EXPR);
7649 id = ansi_opname (NE_EXPR);
7653 id = ansi_opname (LE_EXPR);
7656 case CPP_GREATER_EQ:
7657 id = ansi_opname (GE_EXPR);
7661 id = ansi_opname (TRUTH_ANDIF_EXPR);
7665 id = ansi_opname (TRUTH_ORIF_EXPR);
7669 id = ansi_opname (POSTINCREMENT_EXPR);
7672 case CPP_MINUS_MINUS:
7673 id = ansi_opname (PREDECREMENT_EXPR);
7677 id = ansi_opname (COMPOUND_EXPR);
7680 case CPP_DEREF_STAR:
7681 id = ansi_opname (MEMBER_REF);
7685 id = ansi_opname (COMPONENT_REF);
7688 case CPP_OPEN_PAREN:
7689 /* Consume the `('. */
7690 cp_lexer_consume_token (parser->lexer);
7691 /* Look for the matching `)'. */
7692 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
7693 return ansi_opname (CALL_EXPR);
7695 case CPP_OPEN_SQUARE:
7696 /* Consume the `['. */
7697 cp_lexer_consume_token (parser->lexer);
7698 /* Look for the matching `]'. */
7699 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
7700 return ansi_opname (ARRAY_REF);
7704 id = ansi_opname (MIN_EXPR);
7708 id = ansi_opname (MAX_EXPR);
7712 id = ansi_assopname (MIN_EXPR);
7716 id = ansi_assopname (MAX_EXPR);
7720 /* Anything else is an error. */
7724 /* If we have selected an identifier, we need to consume the
7727 cp_lexer_consume_token (parser->lexer);
7728 /* Otherwise, no valid operator name was present. */
7731 cp_parser_error (parser, "expected operator");
7732 id = error_mark_node;
7738 /* Parse a template-declaration.
7740 template-declaration:
7741 export [opt] template < template-parameter-list > declaration
7743 If MEMBER_P is TRUE, this template-declaration occurs within a
7746 The grammar rule given by the standard isn't correct. What
7749 template-declaration:
7750 export [opt] template-parameter-list-seq
7751 decl-specifier-seq [opt] init-declarator [opt] ;
7752 export [opt] template-parameter-list-seq
7755 template-parameter-list-seq:
7756 template-parameter-list-seq [opt]
7757 template < template-parameter-list > */
7760 cp_parser_template_declaration (parser, member_p)
7764 /* Check for `export'. */
7765 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
7767 /* Consume the `export' token. */
7768 cp_lexer_consume_token (parser->lexer);
7769 /* Warn that we do not support `export'. */
7770 warning ("keyword `export' not implemented, and will be ignored");
7773 cp_parser_template_declaration_after_export (parser, member_p);
7776 /* Parse a template-parameter-list.
7778 template-parameter-list:
7780 template-parameter-list , template-parameter
7782 Returns a TREE_LIST. Each node represents a template parameter.
7783 The nodes are connected via their TREE_CHAINs. */
7786 cp_parser_template_parameter_list (parser)
7789 tree parameter_list = NULL_TREE;
7796 /* Parse the template-parameter. */
7797 parameter = cp_parser_template_parameter (parser);
7798 /* Add it to the list. */
7799 parameter_list = process_template_parm (parameter_list,
7802 /* Peek at the next token. */
7803 token = cp_lexer_peek_token (parser->lexer);
7804 /* If it's not a `,', we're done. */
7805 if (token->type != CPP_COMMA)
7807 /* Otherwise, consume the `,' token. */
7808 cp_lexer_consume_token (parser->lexer);
7811 return parameter_list;
7814 /* Parse a template-parameter.
7818 parameter-declaration
7820 Returns a TREE_LIST. The TREE_VALUE represents the parameter. The
7821 TREE_PURPOSE is the default value, if any. */
7824 cp_parser_template_parameter (parser)
7829 /* Peek at the next token. */
7830 token = cp_lexer_peek_token (parser->lexer);
7831 /* If it is `class' or `template', we have a type-parameter. */
7832 if (token->keyword == RID_TEMPLATE)
7833 return cp_parser_type_parameter (parser);
7834 /* If it is `class' or `typename' we do not know yet whether it is a
7835 type parameter or a non-type parameter. Consider:
7837 template <typename T, typename T::X X> ...
7841 template <class C, class D*> ...
7843 Here, the first parameter is a type parameter, and the second is
7844 a non-type parameter. We can tell by looking at the token after
7845 the identifier -- if it is a `,', `=', or `>' then we have a type
7847 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
7849 /* Peek at the token after `class' or `typename'. */
7850 token = cp_lexer_peek_nth_token (parser->lexer, 2);
7851 /* If it's an identifier, skip it. */
7852 if (token->type == CPP_NAME)
7853 token = cp_lexer_peek_nth_token (parser->lexer, 3);
7854 /* Now, see if the token looks like the end of a template
7856 if (token->type == CPP_COMMA
7857 || token->type == CPP_EQ
7858 || token->type == CPP_GREATER)
7859 return cp_parser_type_parameter (parser);
7862 /* Otherwise, it is a non-type parameter.
7866 When parsing a default template-argument for a non-type
7867 template-parameter, the first non-nested `>' is taken as the end
7868 of the template parameter-list rather than a greater-than
7871 cp_parser_parameter_declaration (parser,
7872 /*greater_than_is_operator_p=*/false);
7875 /* Parse a type-parameter.
7878 class identifier [opt]
7879 class identifier [opt] = type-id
7880 typename identifier [opt]
7881 typename identifier [opt] = type-id
7882 template < template-parameter-list > class identifier [opt]
7883 template < template-parameter-list > class identifier [opt]
7886 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
7887 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
7888 the declaration of the parameter. */
7891 cp_parser_type_parameter (parser)
7897 /* Look for a keyword to tell us what kind of parameter this is. */
7898 token = cp_parser_require (parser, CPP_KEYWORD,
7899 "expected `class', `typename', or `template'");
7901 return error_mark_node;
7903 switch (token->keyword)
7909 tree default_argument;
7911 /* If the next token is an identifier, then it names the
7913 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
7914 identifier = cp_parser_identifier (parser);
7916 identifier = NULL_TREE;
7918 /* Create the parameter. */
7919 parameter = finish_template_type_parm (class_type_node, identifier);
7921 /* If the next token is an `=', we have a default argument. */
7922 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7924 /* Consume the `=' token. */
7925 cp_lexer_consume_token (parser->lexer);
7926 /* Parse the default-argumen. */
7927 default_argument = cp_parser_type_id (parser);
7930 default_argument = NULL_TREE;
7932 /* Create the combined representation of the parameter and the
7933 default argument. */
7934 parameter = build_tree_list (default_argument,
7941 tree parameter_list;
7943 tree default_argument;
7945 /* Look for the `<'. */
7946 cp_parser_require (parser, CPP_LESS, "`<'");
7947 /* Parse the template-parameter-list. */
7948 begin_template_parm_list ();
7950 = cp_parser_template_parameter_list (parser);
7951 parameter_list = end_template_parm_list (parameter_list);
7952 /* Look for the `>'. */
7953 cp_parser_require (parser, CPP_GREATER, "`>'");
7954 /* Look for the `class' keyword. */
7955 cp_parser_require_keyword (parser, RID_CLASS, "`class'");
7956 /* If the next token is an `=', then there is a
7957 default-argument. If the next token is a `>', we are at
7958 the end of the parameter-list. If the next token is a `,',
7959 then we are at the end of this parameter. */
7960 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
7961 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
7962 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7963 identifier = cp_parser_identifier (parser);
7965 identifier = NULL_TREE;
7966 /* Create the template parameter. */
7967 parameter = finish_template_template_parm (class_type_node,
7970 /* If the next token is an `=', then there is a
7971 default-argument. */
7972 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7974 /* Consume the `='. */
7975 cp_lexer_consume_token (parser->lexer);
7976 /* Parse the id-expression. */
7978 = cp_parser_id_expression (parser,
7979 /*template_keyword_p=*/false,
7980 /*check_dependency_p=*/true,
7981 /*template_p=*/NULL);
7982 /* Look up the name. */
7984 = cp_parser_lookup_name_simple (parser, default_argument);
7985 /* See if the default argument is valid. */
7987 = check_template_template_default_arg (default_argument);
7990 default_argument = NULL_TREE;
7992 /* Create the combined representation of the parameter and the
7993 default argument. */
7994 parameter = build_tree_list (default_argument,
8000 /* Anything else is an error. */
8001 cp_parser_error (parser,
8002 "expected `class', `typename', or `template'");
8003 parameter = error_mark_node;
8009 /* Parse a template-id.
8012 template-name < template-argument-list [opt] >
8014 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
8015 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
8016 returned. Otherwise, if the template-name names a function, or set
8017 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
8018 names a class, returns a TYPE_DECL for the specialization.
8020 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
8021 uninstantiated templates. */
8024 cp_parser_template_id (cp_parser *parser,
8025 bool template_keyword_p,
8026 bool check_dependency_p)
8031 tree saved_qualifying_scope;
8032 tree saved_object_scope;
8034 bool saved_greater_than_is_operator_p;
8035 ptrdiff_t start_of_id;
8036 tree access_check = NULL_TREE;
8038 /* If the next token corresponds to a template-id, there is no need
8040 if (cp_lexer_next_token_is (parser->lexer, CPP_TEMPLATE_ID))
8045 /* Get the stored value. */
8046 value = cp_lexer_consume_token (parser->lexer)->value;
8047 /* Perform any access checks that were deferred. */
8048 for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
8049 cp_parser_defer_access_check (parser,
8050 TREE_PURPOSE (check),
8051 TREE_VALUE (check));
8052 /* Return the stored value. */
8053 return TREE_VALUE (value);
8056 /* Remember where the template-id starts. */
8057 if (cp_parser_parsing_tentatively (parser)
8058 && !cp_parser_committed_to_tentative_parse (parser))
8060 cp_token *next_token = cp_lexer_peek_token (parser->lexer);
8061 start_of_id = cp_lexer_token_difference (parser->lexer,
8062 parser->lexer->first_token,
8064 access_check = parser->context->deferred_access_checks;
8069 /* Parse the template-name. */
8070 template = cp_parser_template_name (parser, template_keyword_p,
8071 check_dependency_p);
8072 if (template == error_mark_node)
8073 return error_mark_node;
8075 /* Look for the `<' that starts the template-argument-list. */
8076 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
8077 return error_mark_node;
8081 When parsing a template-id, the first non-nested `>' is taken as
8082 the end of the template-argument-list rather than a greater-than
8084 saved_greater_than_is_operator_p
8085 = parser->greater_than_is_operator_p;
8086 parser->greater_than_is_operator_p = false;
8087 /* Parsing the argument list may modify SCOPE, so we save it
8089 saved_scope = parser->scope;
8090 saved_qualifying_scope = parser->qualifying_scope;
8091 saved_object_scope = parser->object_scope;
8092 /* Parse the template-argument-list itself. */
8093 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
8094 arguments = NULL_TREE;
8096 arguments = cp_parser_template_argument_list (parser);
8097 /* Look for the `>' that ends the template-argument-list. */
8098 cp_parser_require (parser, CPP_GREATER, "`>'");
8099 /* The `>' token might be a greater-than operator again now. */
8100 parser->greater_than_is_operator_p
8101 = saved_greater_than_is_operator_p;
8102 /* Restore the SAVED_SCOPE. */
8103 parser->scope = saved_scope;
8104 parser->qualifying_scope = saved_qualifying_scope;
8105 parser->object_scope = saved_object_scope;
8107 /* Build a representation of the specialization. */
8108 if (TREE_CODE (template) == IDENTIFIER_NODE)
8109 template_id = build_min_nt (TEMPLATE_ID_EXPR, template, arguments);
8110 else if (DECL_CLASS_TEMPLATE_P (template)
8111 || DECL_TEMPLATE_TEMPLATE_PARM_P (template))
8113 = finish_template_type (template, arguments,
8114 cp_lexer_next_token_is (parser->lexer,
8118 /* If it's not a class-template or a template-template, it should be
8119 a function-template. */
8120 my_friendly_assert ((DECL_FUNCTION_TEMPLATE_P (template)
8121 || TREE_CODE (template) == OVERLOAD
8122 || BASELINK_P (template)),
8125 template_id = lookup_template_function (template, arguments);
8128 /* If parsing tentatively, replace the sequence of tokens that makes
8129 up the template-id with a CPP_TEMPLATE_ID token. That way,
8130 should we re-parse the token stream, we will not have to repeat
8131 the effort required to do the parse, nor will we issue duplicate
8132 error messages about problems during instantiation of the
8134 if (start_of_id >= 0)
8139 /* Find the token that corresponds to the start of the
8141 token = cp_lexer_advance_token (parser->lexer,
8142 parser->lexer->first_token,
8145 /* Remember the access checks associated with this
8146 nested-name-specifier. */
8147 c = parser->context->deferred_access_checks;
8148 if (c == access_check)
8149 access_check = NULL_TREE;
8152 while (TREE_CHAIN (c) != access_check)
8154 access_check = parser->context->deferred_access_checks;
8155 parser->context->deferred_access_checks = TREE_CHAIN (c);
8156 TREE_CHAIN (c) = NULL_TREE;
8159 /* Reset the contents of the START_OF_ID token. */
8160 token->type = CPP_TEMPLATE_ID;
8161 token->value = build_tree_list (access_check, template_id);
8162 token->keyword = RID_MAX;
8163 /* Purge all subsequent tokens. */
8164 cp_lexer_purge_tokens_after (parser->lexer, token);
8170 /* Parse a template-name.
8175 The standard should actually say:
8179 operator-function-id
8180 conversion-function-id
8182 A defect report has been filed about this issue.
8184 If TEMPLATE_KEYWORD_P is true, then we have just seen the
8185 `template' keyword, in a construction like:
8189 In that case `f' is taken to be a template-name, even though there
8190 is no way of knowing for sure.
8192 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
8193 name refers to a set of overloaded functions, at least one of which
8194 is a template, or an IDENTIFIER_NODE with the name of the template,
8195 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
8196 names are looked up inside uninstantiated templates. */
8199 cp_parser_template_name (parser, template_keyword_p, check_dependency_p)
8201 bool template_keyword_p;
8202 bool check_dependency_p;
8208 /* If the next token is `operator', then we have either an
8209 operator-function-id or a conversion-function-id. */
8210 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
8212 /* We don't know whether we're looking at an
8213 operator-function-id or a conversion-function-id. */
8214 cp_parser_parse_tentatively (parser);
8215 /* Try an operator-function-id. */
8216 identifier = cp_parser_operator_function_id (parser);
8217 /* If that didn't work, try a conversion-function-id. */
8218 if (!cp_parser_parse_definitely (parser))
8219 identifier = cp_parser_conversion_function_id (parser);
8221 /* Look for the identifier. */
8223 identifier = cp_parser_identifier (parser);
8225 /* If we didn't find an identifier, we don't have a template-id. */
8226 if (identifier == error_mark_node)
8227 return error_mark_node;
8229 /* If the name immediately followed the `template' keyword, then it
8230 is a template-name. However, if the next token is not `<', then
8231 we do not treat it as a template-name, since it is not being used
8232 as part of a template-id. This enables us to handle constructs
8235 template <typename T> struct S { S(); };
8236 template <typename T> S<T>::S();
8238 correctly. We would treat `S' as a template -- if it were `S<T>'
8239 -- but we do not if there is no `<'. */
8240 if (template_keyword_p && processing_template_decl
8241 && cp_lexer_next_token_is (parser->lexer, CPP_LESS))
8244 /* Look up the name. */
8245 decl = cp_parser_lookup_name (parser, identifier,
8246 /*check_access=*/true,
8248 /*is_namespace=*/false,
8249 check_dependency_p);
8250 decl = maybe_get_template_decl_from_type_decl (decl);
8252 /* If DECL is a template, then the name was a template-name. */
8253 if (TREE_CODE (decl) == TEMPLATE_DECL)
8257 /* The standard does not explicitly indicate whether a name that
8258 names a set of overloaded declarations, some of which are
8259 templates, is a template-name. However, such a name should
8260 be a template-name; otherwise, there is no way to form a
8261 template-id for the overloaded templates. */
8262 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
8263 if (TREE_CODE (fns) == OVERLOAD)
8267 for (fn = fns; fn; fn = OVL_NEXT (fn))
8268 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
8273 /* Otherwise, the name does not name a template. */
8274 cp_parser_error (parser, "expected template-name");
8275 return error_mark_node;
8279 /* If DECL is dependent, and refers to a function, then just return
8280 its name; we will look it up again during template instantiation. */
8281 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
8283 tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
8284 if (TYPE_P (scope) && cp_parser_dependent_type_p (scope))
8291 /* Parse a template-argument-list.
8293 template-argument-list:
8295 template-argument-list , template-argument
8297 Returns a TREE_LIST representing the arguments, in the order they
8298 appeared. The TREE_VALUE of each node is a representation of the
8302 cp_parser_template_argument_list (parser)
8305 tree arguments = NULL_TREE;
8311 /* Parse the template-argument. */
8312 argument = cp_parser_template_argument (parser);
8313 /* Add it to the list. */
8314 arguments = tree_cons (NULL_TREE, argument, arguments);
8315 /* If it is not a `,', then there are no more arguments. */
8316 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8318 /* Otherwise, consume the ','. */
8319 cp_lexer_consume_token (parser->lexer);
8322 /* We built up the arguments in reverse order. */
8323 return nreverse (arguments);
8326 /* Parse a template-argument.
8329 assignment-expression
8333 The representation is that of an assignment-expression, type-id, or
8334 id-expression -- except that the qualified id-expression is
8335 evaluated, so that the value returned is either a DECL or an
8339 cp_parser_template_argument (parser)
8345 /* There's really no way to know what we're looking at, so we just
8346 try each alternative in order.
8350 In a template-argument, an ambiguity between a type-id and an
8351 expression is resolved to a type-id, regardless of the form of
8352 the corresponding template-parameter.
8354 Therefore, we try a type-id first. */
8355 cp_parser_parse_tentatively (parser);
8356 argument = cp_parser_type_id (parser);
8357 /* If the next token isn't a `,' or a `>', then this argument wasn't
8359 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
8360 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER))
8361 cp_parser_error (parser, "expected template-argument");
8362 /* If that worked, we're done. */
8363 if (cp_parser_parse_definitely (parser))
8365 /* We're still not sure what the argument will be. */
8366 cp_parser_parse_tentatively (parser);
8367 /* Try a template. */
8368 argument = cp_parser_id_expression (parser,
8369 /*template_keyword_p=*/false,
8370 /*check_dependency_p=*/true,
8372 /* If the next token isn't a `,' or a `>', then this argument wasn't
8374 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
8375 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER))
8376 cp_parser_error (parser, "expected template-argument");
8377 if (!cp_parser_error_occurred (parser))
8379 /* Figure out what is being referred to. */
8380 argument = cp_parser_lookup_name_simple (parser, argument);
8382 argument = make_unbound_class_template (TREE_OPERAND (argument, 0),
8383 TREE_OPERAND (argument, 1),
8384 tf_error | tf_parsing);
8385 else if (TREE_CODE (argument) != TEMPLATE_DECL)
8386 cp_parser_error (parser, "expected template-name");
8388 if (cp_parser_parse_definitely (parser))
8390 /* It must be an assignment-expression. */
8391 return cp_parser_assignment_expression (parser);
8394 /* Parse an explicit-instantiation.
8396 explicit-instantiation:
8397 template declaration
8399 Although the standard says `declaration', what it really means is:
8401 explicit-instantiation:
8402 template decl-specifier-seq [opt] declarator [opt] ;
8404 Things like `template int S<int>::i = 5, int S<double>::j;' are not
8405 supposed to be allowed. A defect report has been filed about this
8410 explicit-instantiation:
8411 storage-class-specifier template
8412 decl-specifier-seq [opt] declarator [opt] ;
8413 function-specifier template
8414 decl-specifier-seq [opt] declarator [opt] ; */
8417 cp_parser_explicit_instantiation (parser)
8420 bool declares_class_or_enum;
8421 tree decl_specifiers;
8423 tree extension_specifier = NULL_TREE;
8425 /* Look for an (optional) storage-class-specifier or
8426 function-specifier. */
8427 if (cp_parser_allow_gnu_extensions_p (parser))
8430 = cp_parser_storage_class_specifier_opt (parser);
8431 if (!extension_specifier)
8432 extension_specifier = cp_parser_function_specifier_opt (parser);
8435 /* Look for the `template' keyword. */
8436 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
8437 /* Let the front end know that we are processing an explicit
8439 begin_explicit_instantiation ();
8440 /* [temp.explicit] says that we are supposed to ignore access
8441 control while processing explicit instantiation directives. */
8442 scope_chain->check_access = 0;
8443 /* Parse a decl-specifier-seq. */
8445 = cp_parser_decl_specifier_seq (parser,
8446 CP_PARSER_FLAGS_OPTIONAL,
8448 &declares_class_or_enum);
8449 /* If there was exactly one decl-specifier, and it declared a class,
8450 and there's no declarator, then we have an explicit type
8452 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
8456 type = check_tag_decl (decl_specifiers);
8458 do_type_instantiation (type, extension_specifier, /*complain=*/1);
8465 /* Parse the declarator. */
8467 = cp_parser_declarator (parser,
8468 /*abstract_p=*/false,
8469 /*ctor_dtor_or_conv_p=*/NULL);
8470 decl = grokdeclarator (declarator, decl_specifiers,
8472 /* Do the explicit instantiation. */
8473 do_decl_instantiation (decl, extension_specifier);
8475 /* We're done with the instantiation. */
8476 end_explicit_instantiation ();
8477 /* Trun access control back on. */
8478 scope_chain->check_access = flag_access_control;
8480 /* Look for the trailing `;'. */
8481 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
8484 /* Parse an explicit-specialization.
8486 explicit-specialization:
8487 template < > declaration
8489 Although the standard says `declaration', what it really means is:
8491 explicit-specialization:
8492 template <> decl-specifier [opt] init-declarator [opt] ;
8493 template <> function-definition
8494 template <> explicit-specialization
8495 template <> template-declaration */
8498 cp_parser_explicit_specialization (parser)
8501 /* Look for the `template' keyword. */
8502 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
8503 /* Look for the `<'. */
8504 cp_parser_require (parser, CPP_LESS, "`<'");
8505 /* Look for the `>'. */
8506 cp_parser_require (parser, CPP_GREATER, "`>'");
8507 /* We have processed another parameter list. */
8508 ++parser->num_template_parameter_lists;
8509 /* Let the front end know that we are beginning a specialization. */
8510 begin_specialization ();
8512 /* If the next keyword is `template', we need to figure out whether
8513 or not we're looking a template-declaration. */
8514 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
8516 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
8517 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
8518 cp_parser_template_declaration_after_export (parser,
8519 /*member_p=*/false);
8521 cp_parser_explicit_specialization (parser);
8524 /* Parse the dependent declaration. */
8525 cp_parser_single_declaration (parser,
8529 /* We're done with the specialization. */
8530 end_specialization ();
8531 /* We're done with this parameter list. */
8532 --parser->num_template_parameter_lists;
8535 /* Parse a type-specifier.
8538 simple-type-specifier
8541 elaborated-type-specifier
8549 Returns a representation of the type-specifier. If the
8550 type-specifier is a keyword (like `int' or `const', or
8551 `__complex__') then the correspoding IDENTIFIER_NODE is returned.
8552 For a class-specifier, enum-specifier, or elaborated-type-specifier
8553 a TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
8555 If IS_FRIEND is TRUE then this type-specifier is being declared a
8556 `friend'. If IS_DECLARATION is TRUE, then this type-specifier is
8557 appearing in a decl-specifier-seq.
8559 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
8560 class-specifier, enum-specifier, or elaborated-type-specifier, then
8561 *DECLARES_CLASS_OR_ENUM is set to TRUE. Otherwise, it is set to
8564 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
8565 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
8569 cp_parser_type_specifier (parser,
8573 declares_class_or_enum,
8576 cp_parser_flags flags;
8578 bool is_declaration;
8579 bool *declares_class_or_enum;
8580 bool *is_cv_qualifier;
8582 tree type_spec = NULL_TREE;
8586 /* Assume this type-specifier does not declare a new type. */
8587 if (declares_class_or_enum)
8588 *declares_class_or_enum = false;
8589 /* And that it does not specify a cv-qualifier. */
8590 if (is_cv_qualifier)
8591 *is_cv_qualifier = false;
8592 /* Peek at the next token. */
8593 token = cp_lexer_peek_token (parser->lexer);
8595 /* If we're looking at a keyword, we can use that to guide the
8596 production we choose. */
8597 keyword = token->keyword;
8600 /* Any of these indicate either a class-specifier, or an
8601 elaborated-type-specifier. */
8606 /* Parse tentatively so that we can back up if we don't find a
8607 class-specifier or enum-specifier. */
8608 cp_parser_parse_tentatively (parser);
8609 /* Look for the class-specifier or enum-specifier. */
8610 if (keyword == RID_ENUM)
8611 type_spec = cp_parser_enum_specifier (parser);
8613 type_spec = cp_parser_class_specifier (parser);
8615 /* If that worked, we're done. */
8616 if (cp_parser_parse_definitely (parser))
8618 if (declares_class_or_enum)
8619 *declares_class_or_enum = true;
8626 /* Look for an elaborated-type-specifier. */
8627 type_spec = cp_parser_elaborated_type_specifier (parser,
8630 /* We're declaring a class or enum -- unless we're using
8632 if (declares_class_or_enum && keyword != RID_TYPENAME)
8633 *declares_class_or_enum = true;
8639 type_spec = cp_parser_cv_qualifier_opt (parser);
8640 /* Even though we call a routine that looks for an optional
8641 qualifier, we know that there should be one. */
8642 my_friendly_assert (type_spec != NULL, 20000328);
8643 /* This type-specifier was a cv-qualified. */
8644 if (is_cv_qualifier)
8645 *is_cv_qualifier = true;
8650 /* The `__complex__' keyword is a GNU extension. */
8651 return cp_lexer_consume_token (parser->lexer)->value;
8657 /* If we do not already have a type-specifier, assume we are looking
8658 at a simple-type-specifier. */
8659 type_spec = cp_parser_simple_type_specifier (parser, flags);
8661 /* If we didn't find a type-specifier, and a type-specifier was not
8662 optional in this context, issue an error message. */
8663 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
8665 cp_parser_error (parser, "expected type specifier");
8666 return error_mark_node;
8672 /* Parse a simple-type-specifier.
8674 simple-type-specifier:
8675 :: [opt] nested-name-specifier [opt] type-name
8676 :: [opt] nested-name-specifier template template-id
8691 simple-type-specifier:
8692 __typeof__ unary-expression
8693 __typeof__ ( type-id )
8695 For the various keywords, the value returned is simply the
8696 TREE_IDENTIFIER representing the keyword. For the first two
8697 productions, the value returned is the indicated TYPE_DECL. */
8700 cp_parser_simple_type_specifier (parser, flags)
8702 cp_parser_flags flags;
8704 tree type = NULL_TREE;
8707 /* Peek at the next token. */
8708 token = cp_lexer_peek_token (parser->lexer);
8710 /* If we're looking at a keyword, things are easy. */
8711 switch (token->keyword)
8724 /* Consume the token. */
8725 return cp_lexer_consume_token (parser->lexer)->value;
8731 /* Consume the `typeof' token. */
8732 cp_lexer_consume_token (parser->lexer);
8733 /* Parse the operand to `typeof' */
8734 operand = cp_parser_sizeof_operand (parser, RID_TYPEOF);
8735 /* If it is not already a TYPE, take its type. */
8736 if (!TYPE_P (operand))
8737 operand = finish_typeof (operand);
8746 /* The type-specifier must be a user-defined type. */
8747 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
8749 /* Don't gobble tokens or issue error messages if this is an
8750 optional type-specifier. */
8751 if (flags & CP_PARSER_FLAGS_OPTIONAL)
8752 cp_parser_parse_tentatively (parser);
8754 /* Look for the optional `::' operator. */
8755 cp_parser_global_scope_opt (parser,
8756 /*current_scope_valid_p=*/false);
8757 /* Look for the nested-name specifier. */
8758 cp_parser_nested_name_specifier_opt (parser,
8759 /*typename_keyword_p=*/false,
8760 /*check_dependency_p=*/true,
8762 /* If we have seen a nested-name-specifier, and the next token
8763 is `template', then we are using the template-id production. */
8765 && cp_parser_optional_template_keyword (parser))
8767 /* Look for the template-id. */
8768 type = cp_parser_template_id (parser,
8769 /*template_keyword_p=*/true,
8770 /*check_dependency_p=*/true);
8771 /* If the template-id did not name a type, we are out of
8773 if (TREE_CODE (type) != TYPE_DECL)
8775 cp_parser_error (parser, "expected template-id for type");
8779 /* Otherwise, look for a type-name. */
8782 type = cp_parser_type_name (parser);
8783 if (type == error_mark_node)
8787 /* If it didn't work out, we don't have a TYPE. */
8788 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
8789 && !cp_parser_parse_definitely (parser))
8793 /* If we didn't get a type-name, issue an error message. */
8794 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
8796 cp_parser_error (parser, "expected type-name");
8797 return error_mark_node;
8803 /* Parse a type-name.
8816 Returns a TYPE_DECL for the the type. */
8819 cp_parser_type_name (parser)
8825 /* We can't know yet whether it is a class-name or not. */
8826 cp_parser_parse_tentatively (parser);
8827 /* Try a class-name. */
8828 type_decl = cp_parser_class_name (parser,
8829 /*typename_keyword_p=*/false,
8830 /*template_keyword_p=*/false,
8832 /*check_access_p=*/true,
8833 /*check_dependency_p=*/true,
8834 /*class_head_p=*/false);
8835 /* If it's not a class-name, keep looking. */
8836 if (!cp_parser_parse_definitely (parser))
8838 /* It must be a typedef-name or an enum-name. */
8839 identifier = cp_parser_identifier (parser);
8840 if (identifier == error_mark_node)
8841 return error_mark_node;
8843 /* Look up the type-name. */
8844 type_decl = cp_parser_lookup_name_simple (parser, identifier);
8845 /* Issue an error if we did not find a type-name. */
8846 if (TREE_CODE (type_decl) != TYPE_DECL)
8848 cp_parser_error (parser, "expected type-name");
8849 type_decl = error_mark_node;
8851 /* Remember that the name was used in the definition of the
8852 current class so that we can check later to see if the
8853 meaning would have been different after the class was
8854 entirely defined. */
8855 else if (type_decl != error_mark_node
8857 maybe_note_name_used_in_class (identifier, type_decl);
8864 /* Parse an elaborated-type-specifier. Note that the grammar given
8865 here incorporates the resolution to DR68.
8867 elaborated-type-specifier:
8868 class-key :: [opt] nested-name-specifier [opt] identifier
8869 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
8870 enum :: [opt] nested-name-specifier [opt] identifier
8871 typename :: [opt] nested-name-specifier identifier
8872 typename :: [opt] nested-name-specifier template [opt]
8875 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
8876 declared `friend'. If IS_DECLARATION is TRUE, then this
8877 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
8878 something is being declared.
8880 Returns the TYPE specified. */
8883 cp_parser_elaborated_type_specifier (parser, is_friend, is_declaration)
8886 bool is_declaration;
8888 enum tag_types tag_type;
8890 tree type = NULL_TREE;
8892 /* See if we're looking at the `enum' keyword. */
8893 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
8895 /* Consume the `enum' token. */
8896 cp_lexer_consume_token (parser->lexer);
8897 /* Remember that it's an enumeration type. */
8898 tag_type = enum_type;
8900 /* Or, it might be `typename'. */
8901 else if (cp_lexer_next_token_is_keyword (parser->lexer,
8904 /* Consume the `typename' token. */
8905 cp_lexer_consume_token (parser->lexer);
8906 /* Remember that it's a `typename' type. */
8907 tag_type = typename_type;
8908 /* The `typename' keyword is only allowed in templates. */
8909 if (!processing_template_decl)
8910 pedwarn ("using `typename' outside of template");
8912 /* Otherwise it must be a class-key. */
8915 tag_type = cp_parser_class_key (parser);
8916 if (tag_type == none_type)
8917 return error_mark_node;
8920 /* Look for the `::' operator. */
8921 cp_parser_global_scope_opt (parser,
8922 /*current_scope_valid_p=*/false);
8923 /* Look for the nested-name-specifier. */
8924 if (tag_type == typename_type)
8925 cp_parser_nested_name_specifier (parser,
8926 /*typename_keyword_p=*/true,
8927 /*check_dependency_p=*/true,
8930 /* Even though `typename' is not present, the proposed resolution
8931 to Core Issue 180 says that in `class A<T>::B', `B' should be
8932 considered a type-name, even if `A<T>' is dependent. */
8933 cp_parser_nested_name_specifier_opt (parser,
8934 /*typename_keyword_p=*/true,
8935 /*check_dependency_p=*/true,
8937 /* For everything but enumeration types, consider a template-id. */
8938 if (tag_type != enum_type)
8940 bool template_p = false;
8943 /* Allow the `template' keyword. */
8944 template_p = cp_parser_optional_template_keyword (parser);
8945 /* If we didn't see `template', we don't know if there's a
8946 template-id or not. */
8948 cp_parser_parse_tentatively (parser);
8949 /* Parse the template-id. */
8950 decl = cp_parser_template_id (parser, template_p,
8951 /*check_dependency_p=*/true);
8952 /* If we didn't find a template-id, look for an ordinary
8954 if (!template_p && !cp_parser_parse_definitely (parser))
8956 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
8957 in effect, then we must assume that, upon instantiation, the
8958 template will correspond to a class. */
8959 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
8960 && tag_type == typename_type)
8961 type = make_typename_type (parser->scope, decl,
8964 type = TREE_TYPE (decl);
8967 /* For an enumeration type, consider only a plain identifier. */
8970 identifier = cp_parser_identifier (parser);
8972 if (identifier == error_mark_node)
8973 return error_mark_node;
8975 /* For a `typename', we needn't call xref_tag. */
8976 if (tag_type == typename_type)
8977 return make_typename_type (parser->scope, identifier,
8979 /* Look up a qualified name in the usual way. */
8984 /* In an elaborated-type-specifier, names are assumed to name
8985 types, so we set IS_TYPE to TRUE when calling
8986 cp_parser_lookup_name. */
8987 decl = cp_parser_lookup_name (parser, identifier,
8988 /*check_access=*/true,
8990 /*is_namespace=*/false,
8991 /*check_dependency=*/true);
8992 decl = (cp_parser_maybe_treat_template_as_class
8993 (decl, /*tag_name_p=*/is_friend));
8995 if (TREE_CODE (decl) != TYPE_DECL)
8997 error ("expected type-name");
8998 return error_mark_node;
9000 else if (TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE
9001 && tag_type != enum_type)
9002 error ("`%T' referred to as `%s'", TREE_TYPE (decl),
9003 tag_type == record_type ? "struct" : "class");
9004 else if (TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE
9005 && tag_type == enum_type)
9006 error ("`%T' referred to as enum", TREE_TYPE (decl));
9008 type = TREE_TYPE (decl);
9012 /* An elaborated-type-specifier sometimes introduces a new type and
9013 sometimes names an existing type. Normally, the rule is that it
9014 introduces a new type only if there is not an existing type of
9015 the same name already in scope. For example, given:
9018 void f() { struct S s; }
9020 the `struct S' in the body of `f' is the same `struct S' as in
9021 the global scope; the existing definition is used. However, if
9022 there were no global declaration, this would introduce a new
9023 local class named `S'.
9025 An exception to this rule applies to the following code:
9027 namespace N { struct S; }
9029 Here, the elaborated-type-specifier names a new type
9030 unconditionally; even if there is already an `S' in the
9031 containing scope this declaration names a new type.
9032 This exception only applies if the elaborated-type-specifier
9033 forms the complete declaration:
9037 A declaration consisting solely of `class-key identifier ;' is
9038 either a redeclaration of the name in the current scope or a
9039 forward declaration of the identifier as a class name. It
9040 introduces the name into the current scope.
9042 We are in this situation precisely when the next token is a `;'.
9044 An exception to the exception is that a `friend' declaration does
9045 *not* name a new type; i.e., given:
9047 struct S { friend struct T; };
9049 `T' is not a new type in the scope of `S'.
9051 Also, `new struct S' or `sizeof (struct S)' never results in the
9052 definition of a new type; a new type can only be declared in a
9053 declaration context. */
9055 type = xref_tag (tag_type, identifier,
9056 /*attributes=*/NULL_TREE,
9059 || cp_lexer_next_token_is_not (parser->lexer,
9063 if (tag_type != enum_type)
9064 cp_parser_check_class_key (tag_type, type);
9068 /* Parse an enum-specifier.
9071 enum identifier [opt] { enumerator-list [opt] }
9073 Returns an ENUM_TYPE representing the enumeration. */
9076 cp_parser_enum_specifier (parser)
9080 tree identifier = NULL_TREE;
9083 /* Look for the `enum' keyword. */
9084 if (!cp_parser_require_keyword (parser, RID_ENUM, "`enum'"))
9085 return error_mark_node;
9086 /* Peek at the next token. */
9087 token = cp_lexer_peek_token (parser->lexer);
9089 /* See if it is an identifier. */
9090 if (token->type == CPP_NAME)
9091 identifier = cp_parser_identifier (parser);
9093 /* Look for the `{'. */
9094 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
9095 return error_mark_node;
9097 /* At this point, we're going ahead with the enum-specifier, even
9098 if some other problem occurs. */
9099 cp_parser_commit_to_tentative_parse (parser);
9101 /* Issue an error message if type-definitions are forbidden here. */
9102 cp_parser_check_type_definition (parser);
9104 /* Create the new type. */
9105 type = start_enum (identifier ? identifier : make_anon_name ());
9107 /* Peek at the next token. */
9108 token = cp_lexer_peek_token (parser->lexer);
9109 /* If it's not a `}', then there are some enumerators. */
9110 if (token->type != CPP_CLOSE_BRACE)
9111 cp_parser_enumerator_list (parser, type);
9112 /* Look for the `}'. */
9113 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
9115 /* Finish up the enumeration. */
9121 /* Parse an enumerator-list. The enumerators all have the indicated
9125 enumerator-definition
9126 enumerator-list , enumerator-definition */
9129 cp_parser_enumerator_list (parser, type)
9137 /* Parse an enumerator-definition. */
9138 cp_parser_enumerator_definition (parser, type);
9139 /* Peek at the next token. */
9140 token = cp_lexer_peek_token (parser->lexer);
9141 /* If it's not a `,', then we've reached the end of the
9143 if (token->type != CPP_COMMA)
9145 /* Otherwise, consume the `,' and keep going. */
9146 cp_lexer_consume_token (parser->lexer);
9147 /* If the next token is a `}', there is a trailing comma. */
9148 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
9150 if (pedantic && !in_system_header)
9151 pedwarn ("comma at end of enumerator list");
9157 /* Parse an enumerator-definition. The enumerator has the indicated
9160 enumerator-definition:
9162 enumerator = constant-expression
9168 cp_parser_enumerator_definition (parser, type)
9176 /* Look for the identifier. */
9177 identifier = cp_parser_identifier (parser);
9178 if (identifier == error_mark_node)
9181 /* Peek at the next token. */
9182 token = cp_lexer_peek_token (parser->lexer);
9183 /* If it's an `=', then there's an explicit value. */
9184 if (token->type == CPP_EQ)
9186 /* Consume the `=' token. */
9187 cp_lexer_consume_token (parser->lexer);
9188 /* Parse the value. */
9189 value = cp_parser_constant_expression (parser);
9194 /* Create the enumerator. */
9195 build_enumerator (identifier, value, type);
9198 /* Parse a namespace-name.
9201 original-namespace-name
9204 Returns the NAMESPACE_DECL for the namespace. */
9207 cp_parser_namespace_name (parser)
9211 tree namespace_decl;
9213 /* Get the name of the namespace. */
9214 identifier = cp_parser_identifier (parser);
9215 if (identifier == error_mark_node)
9216 return error_mark_node;
9218 /* Look up the identifier in the currently active scope. Look only
9219 for namespaces, due to:
9223 When looking up a namespace-name in a using-directive or alias
9224 definition, only namespace names are considered.
9230 During the lookup of a name preceding the :: scope resolution
9231 operator, object, function, and enumerator names are ignored.
9233 (Note that cp_parser_class_or_namespace_name only calls this
9234 function if the token after the name is the scope resolution
9236 namespace_decl = cp_parser_lookup_name (parser, identifier,
9237 /*check_access=*/true,
9239 /*is_namespace=*/true,
9240 /*check_dependency=*/true);
9241 /* If it's not a namespace, issue an error. */
9242 if (namespace_decl == error_mark_node
9243 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
9245 cp_parser_error (parser, "expected namespace-name");
9246 namespace_decl = error_mark_node;
9249 return namespace_decl;
9252 /* Parse a namespace-definition.
9254 namespace-definition:
9255 named-namespace-definition
9256 unnamed-namespace-definition
9258 named-namespace-definition:
9259 original-namespace-definition
9260 extension-namespace-definition
9262 original-namespace-definition:
9263 namespace identifier { namespace-body }
9265 extension-namespace-definition:
9266 namespace original-namespace-name { namespace-body }
9268 unnamed-namespace-definition:
9269 namespace { namespace-body } */
9272 cp_parser_namespace_definition (parser)
9277 /* Look for the `namespace' keyword. */
9278 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
9280 /* Get the name of the namespace. We do not attempt to distinguish
9281 between an original-namespace-definition and an
9282 extension-namespace-definition at this point. The semantic
9283 analysis routines are responsible for that. */
9284 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
9285 identifier = cp_parser_identifier (parser);
9287 identifier = NULL_TREE;
9289 /* Look for the `{' to start the namespace. */
9290 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
9291 /* Start the namespace. */
9292 push_namespace (identifier);
9293 /* Parse the body of the namespace. */
9294 cp_parser_namespace_body (parser);
9295 /* Finish the namespace. */
9297 /* Look for the final `}'. */
9298 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
9301 /* Parse a namespace-body.
9304 declaration-seq [opt] */
9307 cp_parser_namespace_body (parser)
9310 cp_parser_declaration_seq_opt (parser);
9313 /* Parse a namespace-alias-definition.
9315 namespace-alias-definition:
9316 namespace identifier = qualified-namespace-specifier ; */
9319 cp_parser_namespace_alias_definition (parser)
9323 tree namespace_specifier;
9325 /* Look for the `namespace' keyword. */
9326 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
9327 /* Look for the identifier. */
9328 identifier = cp_parser_identifier (parser);
9329 if (identifier == error_mark_node)
9331 /* Look for the `=' token. */
9332 cp_parser_require (parser, CPP_EQ, "`='");
9333 /* Look for the qualified-namespace-specifier. */
9335 = cp_parser_qualified_namespace_specifier (parser);
9336 /* Look for the `;' token. */
9337 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
9339 /* Register the alias in the symbol table. */
9340 do_namespace_alias (identifier, namespace_specifier);
9343 /* Parse a qualified-namespace-specifier.
9345 qualified-namespace-specifier:
9346 :: [opt] nested-name-specifier [opt] namespace-name
9348 Returns a NAMESPACE_DECL corresponding to the specified
9352 cp_parser_qualified_namespace_specifier (parser)
9355 /* Look for the optional `::'. */
9356 cp_parser_global_scope_opt (parser,
9357 /*current_scope_valid_p=*/false);
9359 /* Look for the optional nested-name-specifier. */
9360 cp_parser_nested_name_specifier_opt (parser,
9361 /*typename_keyword_p=*/false,
9362 /*check_dependency_p=*/true,
9365 return cp_parser_namespace_name (parser);
9368 /* Parse a using-declaration.
9371 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
9372 using :: unqualified-id ; */
9375 cp_parser_using_declaration (parser)
9379 bool typename_p = false;
9380 bool global_scope_p;
9385 /* Look for the `using' keyword. */
9386 cp_parser_require_keyword (parser, RID_USING, "`using'");
9388 /* Peek at the next token. */
9389 token = cp_lexer_peek_token (parser->lexer);
9390 /* See if it's `typename'. */
9391 if (token->keyword == RID_TYPENAME)
9393 /* Remember that we've seen it. */
9395 /* Consume the `typename' token. */
9396 cp_lexer_consume_token (parser->lexer);
9399 /* Look for the optional global scope qualification. */
9401 = (cp_parser_global_scope_opt (parser,
9402 /*current_scope_valid_p=*/false)
9405 /* If we saw `typename', or didn't see `::', then there must be a
9406 nested-name-specifier present. */
9407 if (typename_p || !global_scope_p)
9408 cp_parser_nested_name_specifier (parser, typename_p,
9409 /*check_dependency_p=*/true,
9411 /* Otherwise, we could be in either of the two productions. In that
9412 case, treat the nested-name-specifier as optional. */
9414 cp_parser_nested_name_specifier_opt (parser,
9415 /*typename_keyword_p=*/false,
9416 /*check_dependency_p=*/true,
9419 /* Parse the unqualified-id. */
9420 identifier = cp_parser_unqualified_id (parser,
9421 /*template_keyword_p=*/false,
9422 /*check_dependency_p=*/true);
9424 /* The function we call to handle a using-declaration is different
9425 depending on what scope we are in. */
9426 scope = current_scope ();
9427 if (scope && TYPE_P (scope))
9429 /* Create the USING_DECL. */
9430 decl = do_class_using_decl (build_nt (SCOPE_REF,
9433 /* Add it to the list of members in this class. */
9434 finish_member_declaration (decl);
9438 decl = cp_parser_lookup_name_simple (parser, identifier);
9440 do_local_using_decl (decl);
9442 do_toplevel_using_decl (decl);
9445 /* Look for the final `;'. */
9446 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
9449 /* Parse a using-directive.
9452 using namespace :: [opt] nested-name-specifier [opt]
9456 cp_parser_using_directive (parser)
9459 tree namespace_decl;
9461 /* Look for the `using' keyword. */
9462 cp_parser_require_keyword (parser, RID_USING, "`using'");
9463 /* And the `namespace' keyword. */
9464 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
9465 /* Look for the optional `::' operator. */
9466 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
9467 /* And the optional nested-name-sepcifier. */
9468 cp_parser_nested_name_specifier_opt (parser,
9469 /*typename_keyword_p=*/false,
9470 /*check_dependency_p=*/true,
9472 /* Get the namespace being used. */
9473 namespace_decl = cp_parser_namespace_name (parser);
9474 /* Update the symbol table. */
9475 do_using_directive (namespace_decl);
9476 /* Look for the final `;'. */
9477 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
9480 /* Parse an asm-definition.
9483 asm ( string-literal ) ;
9488 asm volatile [opt] ( string-literal ) ;
9489 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
9490 asm volatile [opt] ( string-literal : asm-operand-list [opt]
9491 : asm-operand-list [opt] ) ;
9492 asm volatile [opt] ( string-literal : asm-operand-list [opt]
9493 : asm-operand-list [opt]
9494 : asm-operand-list [opt] ) ; */
9497 cp_parser_asm_definition (parser)
9502 tree outputs = NULL_TREE;
9503 tree inputs = NULL_TREE;
9504 tree clobbers = NULL_TREE;
9506 bool volatile_p = false;
9507 bool extended_p = false;
9509 /* Look for the `asm' keyword. */
9510 cp_parser_require_keyword (parser, RID_ASM, "`asm'");
9511 /* See if the next token is `volatile'. */
9512 if (cp_parser_allow_gnu_extensions_p (parser)
9513 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
9515 /* Remember that we saw the `volatile' keyword. */
9517 /* Consume the token. */
9518 cp_lexer_consume_token (parser->lexer);
9520 /* Look for the opening `('. */
9521 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
9522 /* Look for the string. */
9523 token = cp_parser_require (parser, CPP_STRING, "asm body");
9526 string = token->value;
9527 /* If we're allowing GNU extensions, check for the extended assembly
9528 syntax. Unfortunately, the `:' tokens need not be separated by
9529 a space in C, and so, for compatibility, we tolerate that here
9530 too. Doing that means that we have to treat the `::' operator as
9532 if (cp_parser_allow_gnu_extensions_p (parser)
9533 && at_function_scope_p ()
9534 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
9535 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
9537 bool inputs_p = false;
9538 bool clobbers_p = false;
9540 /* The extended syntax was used. */
9543 /* Look for outputs. */
9544 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
9546 /* Consume the `:'. */
9547 cp_lexer_consume_token (parser->lexer);
9548 /* Parse the output-operands. */
9549 if (cp_lexer_next_token_is_not (parser->lexer,
9551 && cp_lexer_next_token_is_not (parser->lexer,
9553 outputs = cp_parser_asm_operand_list (parser);
9555 /* If the next token is `::', there are no outputs, and the
9556 next token is the beginning of the inputs. */
9557 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
9559 /* Consume the `::' token. */
9560 cp_lexer_consume_token (parser->lexer);
9561 /* The inputs are coming next. */
9565 /* Look for inputs. */
9567 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
9570 /* Consume the `:'. */
9571 cp_lexer_consume_token (parser->lexer);
9572 /* Parse the output-operands. */
9573 if (cp_lexer_next_token_is_not (parser->lexer,
9575 && cp_lexer_next_token_is_not (parser->lexer,
9577 inputs = cp_parser_asm_operand_list (parser);
9579 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
9580 /* The clobbers are coming next. */
9583 /* Look for clobbers. */
9585 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
9588 /* Consume the `:'. */
9589 cp_lexer_consume_token (parser->lexer);
9590 /* Parse the clobbers. */
9591 clobbers = cp_parser_asm_clobber_list (parser);
9594 /* Look for the closing `)'. */
9595 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
9596 cp_parser_skip_to_closing_parenthesis (parser);
9597 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
9599 /* Create the ASM_STMT. */
9600 if (at_function_scope_p ())
9603 finish_asm_stmt (volatile_p
9604 ? ridpointers[(int) RID_VOLATILE] : NULL_TREE,
9605 string, outputs, inputs, clobbers);
9606 /* If the extended syntax was not used, mark the ASM_STMT. */
9608 ASM_INPUT_P (asm_stmt) = 1;
9611 assemble_asm (string);
9614 /* Declarators [gram.dcl.decl] */
9616 /* Parse an init-declarator.
9619 declarator initializer [opt]
9624 declarator asm-specification [opt] attributes [opt] initializer [opt]
9626 The DECL_SPECIFIERS and PREFIX_ATTRIBUTES apply to this declarator.
9627 Returns a reprsentation of the entity declared. The ACCESS_CHECKS
9628 represent deferred access checks from the decl-specifier-seq. If
9629 MEMBER_P is TRUE, then this declarator appears in a class scope.
9630 The new DECL created by this declarator is returned.
9632 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
9633 for a function-definition here as well. If the declarator is a
9634 declarator for a function-definition, *FUNCTION_DEFINITION_P will
9635 be TRUE upon return. By that point, the function-definition will
9636 have been completely parsed.
9638 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
9642 cp_parser_init_declarator (parser,
9646 function_definition_allowed_p,
9648 function_definition_p)
9650 tree decl_specifiers;
9651 tree prefix_attributes;
9653 bool function_definition_allowed_p;
9655 bool *function_definition_p;
9660 tree asm_specification;
9662 tree decl = NULL_TREE;
9664 tree declarator_access_checks;
9665 bool is_initialized;
9666 bool is_parenthesized_init;
9667 bool ctor_dtor_or_conv_p;
9670 /* Assume that this is not the declarator for a function
9672 if (function_definition_p)
9673 *function_definition_p = false;
9675 /* Defer access checks while parsing the declarator; we cannot know
9676 what names are accessible until we know what is being
9678 cp_parser_start_deferring_access_checks (parser);
9679 /* Parse the declarator. */
9681 = cp_parser_declarator (parser,
9682 /*abstract_p=*/false,
9683 &ctor_dtor_or_conv_p);
9684 /* Gather up the deferred checks. */
9685 declarator_access_checks
9686 = cp_parser_stop_deferring_access_checks (parser);
9688 /* If the DECLARATOR was erroneous, there's no need to go
9690 if (declarator == error_mark_node)
9691 return error_mark_node;
9693 /* Figure out what scope the entity declared by the DECLARATOR is
9694 located in. `grokdeclarator' sometimes changes the scope, so
9695 we compute it now. */
9696 scope = get_scope_of_declarator (declarator);
9698 /* If we're allowing GNU extensions, look for an asm-specification
9700 if (cp_parser_allow_gnu_extensions_p (parser))
9702 /* Look for an asm-specification. */
9703 asm_specification = cp_parser_asm_specification_opt (parser);
9704 /* And attributes. */
9705 attributes = cp_parser_attributes_opt (parser);
9709 asm_specification = NULL_TREE;
9710 attributes = NULL_TREE;
9713 /* Peek at the next token. */
9714 token = cp_lexer_peek_token (parser->lexer);
9715 /* Check to see if the token indicates the start of a
9716 function-definition. */
9717 if (cp_parser_token_starts_function_definition_p (token))
9719 if (!function_definition_allowed_p)
9721 /* If a function-definition should not appear here, issue an
9723 cp_parser_error (parser,
9724 "a function-definition is not allowed here");
9725 return error_mark_node;
9731 /* Neither attributes nor an asm-specification are allowed
9732 on a function-definition. */
9733 if (asm_specification)
9734 error ("an asm-specification is not allowed on a function-definition");
9736 error ("attributes are not allowed on a function-definition");
9737 /* This is a function-definition. */
9738 *function_definition_p = true;
9740 /* Thread the access checks together. */
9741 ac = &access_checks;
9743 ac = &TREE_CHAIN (*ac);
9744 *ac = declarator_access_checks;
9746 /* Parse the function definition. */
9747 decl = (cp_parser_function_definition_from_specifiers_and_declarator
9748 (parser, decl_specifiers, prefix_attributes, declarator,
9751 /* Pull the access-checks apart again. */
9760 Only in function declarations for constructors, destructors, and
9761 type conversions can the decl-specifier-seq be omitted.
9763 We explicitly postpone this check past the point where we handle
9764 function-definitions because we tolerate function-definitions
9765 that are missing their return types in some modes. */
9766 if (!decl_specifiers && !ctor_dtor_or_conv_p)
9768 cp_parser_error (parser,
9769 "expected constructor, destructor, or type conversion");
9770 return error_mark_node;
9773 /* An `=' or an `(' indicates an initializer. */
9774 is_initialized = (token->type == CPP_EQ
9775 || token->type == CPP_OPEN_PAREN);
9776 /* If the init-declarator isn't initialized and isn't followed by a
9777 `,' or `;', it's not a valid init-declarator. */
9779 && token->type != CPP_COMMA
9780 && token->type != CPP_SEMICOLON)
9782 cp_parser_error (parser, "expected init-declarator");
9783 return error_mark_node;
9786 /* Because start_decl has side-effects, we should only call it if we
9787 know we're going ahead. By this point, we know that we cannot
9788 possibly be looking at any other construct. */
9789 cp_parser_commit_to_tentative_parse (parser);
9791 /* Check to see whether or not this declaration is a friend. */
9792 friend_p = cp_parser_friend_p (decl_specifiers);
9794 /* Check that the number of template-parameter-lists is OK. */
9795 if (!cp_parser_check_declarator_template_parameters (parser,
9797 return error_mark_node;
9799 /* Enter the newly declared entry in the symbol table. If we're
9800 processing a declaration in a class-specifier, we wait until
9801 after processing the initializer. */
9804 if (parser->in_unbraced_linkage_specification_p)
9806 decl_specifiers = tree_cons (error_mark_node,
9807 get_identifier ("extern"),
9809 have_extern_spec = false;
9811 decl = start_decl (declarator,
9818 /* Enter the SCOPE. That way unqualified names appearing in the
9819 initializer will be looked up in SCOPE. */
9823 /* Perform deferred access control checks, now that we know in which
9824 SCOPE the declared entity resides. */
9825 if (!member_p && decl)
9827 tree saved_current_function_decl = NULL_TREE;
9829 /* If the entity being declared is a function, pretend that we
9830 are in its scope. If it is a `friend', it may have access to
9831 things that would not otherwise be accessible. */
9832 if (TREE_CODE (decl) == FUNCTION_DECL)
9834 saved_current_function_decl = current_function_decl;
9835 current_function_decl = decl;
9838 /* Perform the access control checks for the decl-specifiers. */
9839 cp_parser_perform_deferred_access_checks (access_checks);
9840 /* And for the declarator. */
9841 cp_parser_perform_deferred_access_checks (declarator_access_checks);
9843 /* Restore the saved value. */
9844 if (TREE_CODE (decl) == FUNCTION_DECL)
9845 current_function_decl = saved_current_function_decl;
9848 /* Parse the initializer. */
9850 initializer = cp_parser_initializer (parser,
9851 &is_parenthesized_init);
9854 initializer = NULL_TREE;
9855 is_parenthesized_init = false;
9858 /* The old parser allows attributes to appear after a parenthesized
9859 initializer. Mark Mitchell proposed removing this functionality
9860 on the GCC mailing lists on 2002-08-13. This parser accepts the
9861 attributes -- but ignores them. */
9862 if (cp_parser_allow_gnu_extensions_p (parser) && is_parenthesized_init)
9863 if (cp_parser_attributes_opt (parser))
9864 warning ("attributes after parenthesized initializer ignored");
9866 /* Leave the SCOPE, now that we have processed the initializer. It
9867 is important to do this before calling cp_finish_decl because it
9868 makes decisions about whether to create DECL_STMTs or not based
9869 on the current scope. */
9873 /* For an in-class declaration, use `grokfield' to create the
9876 decl = grokfield (declarator, decl_specifiers,
9877 initializer, /*asmspec=*/NULL_TREE,
9878 /*attributes=*/NULL_TREE);
9880 /* Finish processing the declaration. But, skip friend
9882 if (!friend_p && decl)
9883 cp_finish_decl (decl,
9886 /* If the initializer is in parentheses, then this is
9887 a direct-initialization, which means that an
9888 `explicit' constructor is OK. Otherwise, an
9889 `explicit' constructor cannot be used. */
9890 ((is_parenthesized_init || !is_initialized)
9891 ? 0 : LOOKUP_ONLYCONVERTING));
9896 /* Parse a declarator.
9900 ptr-operator declarator
9902 abstract-declarator:
9903 ptr-operator abstract-declarator [opt]
9904 direct-abstract-declarator
9909 attributes [opt] direct-declarator
9910 attributes [opt] ptr-operator declarator
9912 abstract-declarator:
9913 attributes [opt] ptr-operator abstract-declarator [opt]
9914 attributes [opt] direct-abstract-declarator
9916 Returns a representation of the declarator. If the declarator has
9917 the form `* declarator', then an INDIRECT_REF is returned, whose
9918 only operand is the sub-declarator. Analagously, `& declarator' is
9919 represented as an ADDR_EXPR. For `X::* declarator', a SCOPE_REF is
9920 used. The first operand is the TYPE for `X'. The second operand
9921 is an INDIRECT_REF whose operand is the sub-declarator.
9923 Otherwise, the reprsentation is as for a direct-declarator.
9925 (It would be better to define a structure type to represent
9926 declarators, rather than abusing `tree' nodes to represent
9927 declarators. That would be much clearer and save some memory.
9928 There is no reason for declarators to be garbage-collected, for
9929 example; they are created during parser and no longer needed after
9930 `grokdeclarator' has been called.)
9932 For a ptr-operator that has the optional cv-qualifier-seq,
9933 cv-qualifiers will be stored in the TREE_TYPE of the INDIRECT_REF
9936 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is set to
9937 true if this declarator represents a constructor, destructor, or
9938 type conversion operator. Otherwise, it is set to false.
9940 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
9941 a decl-specifier-seq unless it declares a constructor, destructor,
9942 or conversion. It might seem that we could check this condition in
9943 semantic analysis, rather than parsing, but that makes it difficult
9944 to handle something like `f()'. We want to notice that there are
9945 no decl-specifiers, and therefore realize that this is an
9946 expression, not a declaration.) */
9949 cp_parser_declarator (parser, abstract_p, ctor_dtor_or_conv_p)
9952 bool *ctor_dtor_or_conv_p;
9956 enum tree_code code;
9957 tree cv_qualifier_seq;
9959 tree attributes = NULL_TREE;
9961 /* Assume this is not a constructor, destructor, or type-conversion
9963 if (ctor_dtor_or_conv_p)
9964 *ctor_dtor_or_conv_p = false;
9966 if (cp_parser_allow_gnu_extensions_p (parser))
9967 attributes = cp_parser_attributes_opt (parser);
9969 /* Peek at the next token. */
9970 token = cp_lexer_peek_token (parser->lexer);
9972 /* Check for the ptr-operator production. */
9973 cp_parser_parse_tentatively (parser);
9974 /* Parse the ptr-operator. */
9975 code = cp_parser_ptr_operator (parser,
9978 /* If that worked, then we have a ptr-operator. */
9979 if (cp_parser_parse_definitely (parser))
9981 /* The dependent declarator is optional if we are parsing an
9982 abstract-declarator. */
9984 cp_parser_parse_tentatively (parser);
9986 /* Parse the dependent declarator. */
9987 declarator = cp_parser_declarator (parser, abstract_p,
9988 /*ctor_dtor_or_conv_p=*/NULL);
9990 /* If we are parsing an abstract-declarator, we must handle the
9991 case where the dependent declarator is absent. */
9992 if (abstract_p && !cp_parser_parse_definitely (parser))
9993 declarator = NULL_TREE;
9995 /* Build the representation of the ptr-operator. */
9996 if (code == INDIRECT_REF)
9997 declarator = make_pointer_declarator (cv_qualifier_seq,
10000 declarator = make_reference_declarator (cv_qualifier_seq,
10002 /* Handle the pointer-to-member case. */
10004 declarator = build_nt (SCOPE_REF, class_type, declarator);
10006 /* Everything else is a direct-declarator. */
10008 declarator = cp_parser_direct_declarator (parser,
10010 ctor_dtor_or_conv_p);
10012 if (attributes && declarator != error_mark_node)
10013 declarator = tree_cons (attributes, declarator, NULL_TREE);
10018 /* Parse a direct-declarator or direct-abstract-declarator.
10022 direct-declarator ( parameter-declaration-clause )
10023 cv-qualifier-seq [opt]
10024 exception-specification [opt]
10025 direct-declarator [ constant-expression [opt] ]
10028 direct-abstract-declarator:
10029 direct-abstract-declarator [opt]
10030 ( parameter-declaration-clause )
10031 cv-qualifier-seq [opt]
10032 exception-specification [opt]
10033 direct-abstract-declarator [opt] [ constant-expression [opt] ]
10034 ( abstract-declarator )
10036 Returns a representation of the declarator. ABSTRACT_P is TRUE if
10037 we are parsing a direct-abstract-declarator; FALSE if we are
10038 parsing a direct-declarator. CTOR_DTOR_OR_CONV_P is as for
10039 cp_parser_declarator.
10041 For the declarator-id production, the representation is as for an
10042 id-expression, except that a qualified name is represented as a
10043 SCOPE_REF. A function-declarator is represented as a CALL_EXPR;
10044 see the documentation of the FUNCTION_DECLARATOR_* macros for
10045 information about how to find the various declarator components.
10046 An array-declarator is represented as an ARRAY_REF. The
10047 direct-declarator is the first operand; the constant-expression
10048 indicating the size of the array is the second operand. */
10051 cp_parser_direct_declarator (parser, abstract_p, ctor_dtor_or_conv_p)
10054 bool *ctor_dtor_or_conv_p;
10058 tree scope = NULL_TREE;
10059 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
10060 bool saved_in_declarator_p = parser->in_declarator_p;
10062 /* Peek at the next token. */
10063 token = cp_lexer_peek_token (parser->lexer);
10064 /* Find the initial direct-declarator. It might be a parenthesized
10066 if (token->type == CPP_OPEN_PAREN)
10070 /* For an abstract declarator we do not know whether we are
10071 looking at the beginning of a parameter-declaration-clause,
10072 or at a parenthesized abstract declarator. For example, if
10073 we see `(int)', we are looking at a
10074 parameter-declaration-clause, and the
10075 direct-abstract-declarator has been omitted. If, on the
10076 other hand we are looking at `((*))' then we are looking at a
10077 parenthesized abstract-declarator. There is no easy way to
10078 tell which situation we are in. */
10080 cp_parser_parse_tentatively (parser);
10082 /* Consume the `('. */
10083 cp_lexer_consume_token (parser->lexer);
10084 /* Parse the nested declarator. */
10086 = cp_parser_declarator (parser, abstract_p, ctor_dtor_or_conv_p);
10087 /* Expect a `)'. */
10088 error_p = !cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
10090 /* If parsing a parenthesized abstract declarator didn't work,
10091 try a parameter-declaration-clause. */
10092 if (abstract_p && !cp_parser_parse_definitely (parser))
10093 declarator = NULL_TREE;
10094 /* If we were not parsing an abstract declarator, but failed to
10095 find a satisfactory nested declarator, then an error has
10097 else if (!abstract_p
10098 && (declarator == error_mark_node || error_p))
10099 return error_mark_node;
10100 /* Default args cannot appear in an abstract decl. */
10101 parser->default_arg_ok_p = false;
10103 /* Otherwise, for a non-abstract declarator, there should be a
10105 else if (!abstract_p)
10107 declarator = cp_parser_declarator_id (parser);
10109 if (TREE_CODE (declarator) == SCOPE_REF)
10111 scope = TREE_OPERAND (declarator, 0);
10113 /* In the declaration of a member of a template class
10114 outside of the class itself, the SCOPE will sometimes be
10115 a TYPENAME_TYPE. For example, given:
10117 template <typename T>
10118 int S<T>::R::i = 3;
10120 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In this
10121 context, we must resolve S<T>::R to an ordinary type,
10122 rather than a typename type.
10124 The reason we normally avoid resolving TYPENAME_TYPEs is
10125 that a specialization of `S' might render `S<T>::R' not a
10126 type. However, if `S' is specialized, then this `i' will
10127 not be used, so there is no harm in resolving the types
10129 if (TREE_CODE (scope) == TYPENAME_TYPE)
10131 /* Resolve the TYPENAME_TYPE. */
10132 scope = cp_parser_resolve_typename_type (parser, scope);
10133 /* If that failed, the declarator is invalid. */
10134 if (scope == error_mark_node)
10135 return error_mark_node;
10136 /* Build a new DECLARATOR. */
10137 declarator = build_nt (SCOPE_REF,
10139 TREE_OPERAND (declarator, 1));
10142 else if (TREE_CODE (declarator) != IDENTIFIER_NODE)
10143 /* Default args can only appear for a function decl. */
10144 parser->default_arg_ok_p = false;
10146 /* Check to see whether the declarator-id names a constructor,
10147 destructor, or conversion. */
10148 if (ctor_dtor_or_conv_p
10149 && ((TREE_CODE (declarator) == SCOPE_REF
10150 && CLASS_TYPE_P (TREE_OPERAND (declarator, 0)))
10151 || (TREE_CODE (declarator) != SCOPE_REF
10152 && at_class_scope_p ())))
10154 tree unqualified_name;
10157 /* Get the unqualified part of the name. */
10158 if (TREE_CODE (declarator) == SCOPE_REF)
10160 class_type = TREE_OPERAND (declarator, 0);
10161 unqualified_name = TREE_OPERAND (declarator, 1);
10165 class_type = current_class_type;
10166 unqualified_name = declarator;
10169 /* See if it names ctor, dtor or conv. */
10170 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR
10171 || IDENTIFIER_TYPENAME_P (unqualified_name)
10172 || constructor_name_p (unqualified_name, class_type))
10174 *ctor_dtor_or_conv_p = true;
10175 /* We would have cleared the default arg flag above, but
10177 parser->default_arg_ok_p = saved_default_arg_ok_p;
10181 /* But for an abstract declarator, the initial direct-declarator can
10185 declarator = NULL_TREE;
10186 parser->default_arg_ok_p = false;
10189 scope = get_scope_of_declarator (declarator);
10191 /* Any names that appear after the declarator-id for a member
10192 are looked up in the containing scope. */
10193 push_scope (scope);
10196 parser->in_declarator_p = true;
10198 /* Now, parse function-declarators and array-declarators until there
10202 /* Peek at the next token. */
10203 token = cp_lexer_peek_token (parser->lexer);
10204 /* If it's a `[', we're looking at an array-declarator. */
10205 if (token->type == CPP_OPEN_SQUARE)
10209 /* Consume the `['. */
10210 cp_lexer_consume_token (parser->lexer);
10211 /* Peek at the next token. */
10212 token = cp_lexer_peek_token (parser->lexer);
10213 /* If the next token is `]', then there is no
10214 constant-expression. */
10215 if (token->type != CPP_CLOSE_SQUARE)
10216 bounds = cp_parser_constant_expression (parser);
10218 bounds = NULL_TREE;
10219 /* Look for the closing `]'. */
10220 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
10222 declarator = build_nt (ARRAY_REF, declarator, bounds);
10224 /* If it's a `(', we're looking at a function-declarator. */
10225 else if (token->type == CPP_OPEN_PAREN)
10227 /* A function-declarator. Or maybe not. Consider, for
10233 The first is the declaration of a function while the
10234 second is a the definition of a variable, including its
10237 Having seen only the parenthesis, we cannot know which of
10238 these two alternatives should be selected. Even more
10239 complex are examples like:
10244 The former is a function-declaration; the latter is a
10245 variable initialization.
10247 First, we attempt to parse a parameter-declaration
10248 clause. If this works, then we continue; otherwise, we
10249 replace the tokens consumed in the process and continue. */
10252 /* We are now parsing tentatively. */
10253 cp_parser_parse_tentatively (parser);
10255 /* Consume the `('. */
10256 cp_lexer_consume_token (parser->lexer);
10257 /* Parse the parameter-declaration-clause. */
10258 params = cp_parser_parameter_declaration_clause (parser);
10260 /* If all went well, parse the cv-qualifier-seq and the
10261 exception-specification. */
10262 if (cp_parser_parse_definitely (parser))
10264 tree cv_qualifiers;
10265 tree exception_specification;
10267 /* Consume the `)'. */
10268 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
10270 /* Parse the cv-qualifier-seq. */
10271 cv_qualifiers = cp_parser_cv_qualifier_seq_opt (parser);
10272 /* And the exception-specification. */
10273 exception_specification
10274 = cp_parser_exception_specification_opt (parser);
10276 /* Create the function-declarator. */
10277 declarator = make_call_declarator (declarator,
10280 exception_specification);
10282 /* Otherwise, we must be done with the declarator. */
10286 /* Otherwise, we're done with the declarator. */
10289 /* Any subsequent parameter lists are to do with return type, so
10290 are not those of the declared function. */
10291 parser->default_arg_ok_p = false;
10294 /* For an abstract declarator, we might wind up with nothing at this
10295 point. That's an error; the declarator is not optional. */
10297 cp_parser_error (parser, "expected declarator");
10299 /* If we entered a scope, we must exit it now. */
10303 parser->default_arg_ok_p = saved_default_arg_ok_p;
10304 parser->in_declarator_p = saved_in_declarator_p;
10309 /* Parse a ptr-operator.
10312 * cv-qualifier-seq [opt]
10314 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
10319 & cv-qualifier-seq [opt]
10321 Returns INDIRECT_REF if a pointer, or pointer-to-member, was
10322 used. Returns ADDR_EXPR if a reference was used. In the
10323 case of a pointer-to-member, *TYPE is filled in with the
10324 TYPE containing the member. *CV_QUALIFIER_SEQ is filled in
10325 with the cv-qualifier-seq, or NULL_TREE, if there are no
10326 cv-qualifiers. Returns ERROR_MARK if an error occurred. */
10328 static enum tree_code
10329 cp_parser_ptr_operator (parser, type, cv_qualifier_seq)
10332 tree *cv_qualifier_seq;
10334 enum tree_code code = ERROR_MARK;
10337 /* Assume that it's not a pointer-to-member. */
10339 /* And that there are no cv-qualifiers. */
10340 *cv_qualifier_seq = NULL_TREE;
10342 /* Peek at the next token. */
10343 token = cp_lexer_peek_token (parser->lexer);
10344 /* If it's a `*' or `&' we have a pointer or reference. */
10345 if (token->type == CPP_MULT || token->type == CPP_AND)
10347 /* Remember which ptr-operator we were processing. */
10348 code = (token->type == CPP_AND ? ADDR_EXPR : INDIRECT_REF);
10350 /* Consume the `*' or `&'. */
10351 cp_lexer_consume_token (parser->lexer);
10353 /* A `*' can be followed by a cv-qualifier-seq, and so can a
10354 `&', if we are allowing GNU extensions. (The only qualifier
10355 that can legally appear after `&' is `restrict', but that is
10356 enforced during semantic analysis. */
10357 if (code == INDIRECT_REF
10358 || cp_parser_allow_gnu_extensions_p (parser))
10359 *cv_qualifier_seq = cp_parser_cv_qualifier_seq_opt (parser);
10363 /* Try the pointer-to-member case. */
10364 cp_parser_parse_tentatively (parser);
10365 /* Look for the optional `::' operator. */
10366 cp_parser_global_scope_opt (parser,
10367 /*current_scope_valid_p=*/false);
10368 /* Look for the nested-name specifier. */
10369 cp_parser_nested_name_specifier (parser,
10370 /*typename_keyword_p=*/false,
10371 /*check_dependency_p=*/true,
10373 /* If we found it, and the next token is a `*', then we are
10374 indeed looking at a pointer-to-member operator. */
10375 if (!cp_parser_error_occurred (parser)
10376 && cp_parser_require (parser, CPP_MULT, "`*'"))
10378 /* The type of which the member is a member is given by the
10380 *type = parser->scope;
10381 /* The next name will not be qualified. */
10382 parser->scope = NULL_TREE;
10383 parser->qualifying_scope = NULL_TREE;
10384 parser->object_scope = NULL_TREE;
10385 /* Indicate that the `*' operator was used. */
10386 code = INDIRECT_REF;
10387 /* Look for the optional cv-qualifier-seq. */
10388 *cv_qualifier_seq = cp_parser_cv_qualifier_seq_opt (parser);
10390 /* If that didn't work we don't have a ptr-operator. */
10391 if (!cp_parser_parse_definitely (parser))
10392 cp_parser_error (parser, "expected ptr-operator");
10398 /* Parse an (optional) cv-qualifier-seq.
10401 cv-qualifier cv-qualifier-seq [opt]
10403 Returns a TREE_LIST. The TREE_VALUE of each node is the
10404 representation of a cv-qualifier. */
10407 cp_parser_cv_qualifier_seq_opt (parser)
10410 tree cv_qualifiers = NULL_TREE;
10416 /* Look for the next cv-qualifier. */
10417 cv_qualifier = cp_parser_cv_qualifier_opt (parser);
10418 /* If we didn't find one, we're done. */
10422 /* Add this cv-qualifier to the list. */
10424 = tree_cons (NULL_TREE, cv_qualifier, cv_qualifiers);
10427 /* We built up the list in reverse order. */
10428 return nreverse (cv_qualifiers);
10431 /* Parse an (optional) cv-qualifier.
10443 cp_parser_cv_qualifier_opt (parser)
10447 tree cv_qualifier = NULL_TREE;
10449 /* Peek at the next token. */
10450 token = cp_lexer_peek_token (parser->lexer);
10451 /* See if it's a cv-qualifier. */
10452 switch (token->keyword)
10457 /* Save the value of the token. */
10458 cv_qualifier = token->value;
10459 /* Consume the token. */
10460 cp_lexer_consume_token (parser->lexer);
10467 return cv_qualifier;
10470 /* Parse a declarator-id.
10474 :: [opt] nested-name-specifier [opt] type-name
10476 In the `id-expression' case, the value returned is as for
10477 cp_parser_id_expression if the id-expression was an unqualified-id.
10478 If the id-expression was a qualified-id, then a SCOPE_REF is
10479 returned. The first operand is the scope (either a NAMESPACE_DECL
10480 or TREE_TYPE), but the second is still just a representation of an
10484 cp_parser_declarator_id (parser)
10487 tree id_expression;
10489 /* The expression must be an id-expression. Assume that qualified
10490 names are the names of types so that:
10493 int S<T>::R::i = 3;
10495 will work; we must treat `S<T>::R' as the name of a type.
10496 Similarly, assume that qualified names are templates, where
10500 int S<T>::R<T>::i = 3;
10503 id_expression = cp_parser_id_expression (parser,
10504 /*template_keyword_p=*/false,
10505 /*check_dependency_p=*/false,
10506 /*template_p=*/NULL);
10507 /* If the name was qualified, create a SCOPE_REF to represent
10510 id_expression = build_nt (SCOPE_REF, parser->scope, id_expression);
10512 return id_expression;
10515 /* Parse a type-id.
10518 type-specifier-seq abstract-declarator [opt]
10520 Returns the TYPE specified. */
10523 cp_parser_type_id (parser)
10526 tree type_specifier_seq;
10527 tree abstract_declarator;
10529 /* Parse the type-specifier-seq. */
10531 = cp_parser_type_specifier_seq (parser);
10532 if (type_specifier_seq == error_mark_node)
10533 return error_mark_node;
10535 /* There might or might not be an abstract declarator. */
10536 cp_parser_parse_tentatively (parser);
10537 /* Look for the declarator. */
10538 abstract_declarator
10539 = cp_parser_declarator (parser, /*abstract_p=*/true, NULL);
10540 /* Check to see if there really was a declarator. */
10541 if (!cp_parser_parse_definitely (parser))
10542 abstract_declarator = NULL_TREE;
10544 return groktypename (build_tree_list (type_specifier_seq,
10545 abstract_declarator));
10548 /* Parse a type-specifier-seq.
10550 type-specifier-seq:
10551 type-specifier type-specifier-seq [opt]
10555 type-specifier-seq:
10556 attributes type-specifier-seq [opt]
10558 Returns a TREE_LIST. Either the TREE_VALUE of each node is a
10559 type-specifier, or the TREE_PURPOSE is a list of attributes. */
10562 cp_parser_type_specifier_seq (parser)
10565 bool seen_type_specifier = false;
10566 tree type_specifier_seq = NULL_TREE;
10568 /* Parse the type-specifiers and attributes. */
10571 tree type_specifier;
10573 /* Check for attributes first. */
10574 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
10576 type_specifier_seq = tree_cons (cp_parser_attributes_opt (parser),
10578 type_specifier_seq);
10582 /* After the first type-specifier, others are optional. */
10583 if (seen_type_specifier)
10584 cp_parser_parse_tentatively (parser);
10585 /* Look for the type-specifier. */
10586 type_specifier = cp_parser_type_specifier (parser,
10587 CP_PARSER_FLAGS_NONE,
10588 /*is_friend=*/false,
10589 /*is_declaration=*/false,
10592 /* If the first type-specifier could not be found, this is not a
10593 type-specifier-seq at all. */
10594 if (!seen_type_specifier && type_specifier == error_mark_node)
10595 return error_mark_node;
10596 /* If subsequent type-specifiers could not be found, the
10597 type-specifier-seq is complete. */
10598 else if (seen_type_specifier && !cp_parser_parse_definitely (parser))
10601 /* Add the new type-specifier to the list. */
10603 = tree_cons (NULL_TREE, type_specifier, type_specifier_seq);
10604 seen_type_specifier = true;
10607 /* We built up the list in reverse order. */
10608 return nreverse (type_specifier_seq);
10611 /* Parse a parameter-declaration-clause.
10613 parameter-declaration-clause:
10614 parameter-declaration-list [opt] ... [opt]
10615 parameter-declaration-list , ...
10617 Returns a representation for the parameter declarations. Each node
10618 is a TREE_LIST. (See cp_parser_parameter_declaration for the exact
10619 representation.) If the parameter-declaration-clause ends with an
10620 ellipsis, PARMLIST_ELLIPSIS_P will hold of the first node in the
10621 list. A return value of NULL_TREE indicates a
10622 parameter-declaration-clause consisting only of an ellipsis. */
10625 cp_parser_parameter_declaration_clause (parser)
10632 /* Peek at the next token. */
10633 token = cp_lexer_peek_token (parser->lexer);
10634 /* Check for trivial parameter-declaration-clauses. */
10635 if (token->type == CPP_ELLIPSIS)
10637 /* Consume the `...' token. */
10638 cp_lexer_consume_token (parser->lexer);
10641 else if (token->type == CPP_CLOSE_PAREN)
10642 /* There are no parameters. */
10644 #ifndef NO_IMPLICIT_EXTERN_C
10645 if (in_system_header && current_class_type == NULL
10646 && current_lang_name == lang_name_c)
10650 return void_list_node;
10652 /* Check for `(void)', too, which is a special case. */
10653 else if (token->keyword == RID_VOID
10654 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
10655 == CPP_CLOSE_PAREN))
10657 /* Consume the `void' token. */
10658 cp_lexer_consume_token (parser->lexer);
10659 /* There are no parameters. */
10660 return void_list_node;
10663 /* Parse the parameter-declaration-list. */
10664 parameters = cp_parser_parameter_declaration_list (parser);
10665 /* If a parse error occurred while parsing the
10666 parameter-declaration-list, then the entire
10667 parameter-declaration-clause is erroneous. */
10668 if (parameters == error_mark_node)
10669 return error_mark_node;
10671 /* Peek at the next token. */
10672 token = cp_lexer_peek_token (parser->lexer);
10673 /* If it's a `,', the clause should terminate with an ellipsis. */
10674 if (token->type == CPP_COMMA)
10676 /* Consume the `,'. */
10677 cp_lexer_consume_token (parser->lexer);
10678 /* Expect an ellipsis. */
10680 = (cp_parser_require (parser, CPP_ELLIPSIS, "`...'") != NULL);
10682 /* It might also be `...' if the optional trailing `,' was
10684 else if (token->type == CPP_ELLIPSIS)
10686 /* Consume the `...' token. */
10687 cp_lexer_consume_token (parser->lexer);
10688 /* And remember that we saw it. */
10692 ellipsis_p = false;
10694 /* Finish the parameter list. */
10695 return finish_parmlist (parameters, ellipsis_p);
10698 /* Parse a parameter-declaration-list.
10700 parameter-declaration-list:
10701 parameter-declaration
10702 parameter-declaration-list , parameter-declaration
10704 Returns a representation of the parameter-declaration-list, as for
10705 cp_parser_parameter_declaration_clause. However, the
10706 `void_list_node' is never appended to the list. */
10709 cp_parser_parameter_declaration_list (parser)
10712 tree parameters = NULL_TREE;
10714 /* Look for more parameters. */
10718 /* Parse the parameter. */
10720 = cp_parser_parameter_declaration (parser,
10721 /*greater_than_is_operator_p=*/true);
10722 /* If a parse error ocurred parsing the parameter declaration,
10723 then the entire parameter-declaration-list is erroneous. */
10724 if (parameter == error_mark_node)
10726 parameters = error_mark_node;
10729 /* Add the new parameter to the list. */
10730 TREE_CHAIN (parameter) = parameters;
10731 parameters = parameter;
10733 /* Peek at the next token. */
10734 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
10735 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10736 /* The parameter-declaration-list is complete. */
10738 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
10742 /* Peek at the next token. */
10743 token = cp_lexer_peek_nth_token (parser->lexer, 2);
10744 /* If it's an ellipsis, then the list is complete. */
10745 if (token->type == CPP_ELLIPSIS)
10747 /* Otherwise, there must be more parameters. Consume the
10749 cp_lexer_consume_token (parser->lexer);
10753 cp_parser_error (parser, "expected `,' or `...'");
10758 /* We built up the list in reverse order; straighten it out now. */
10759 return nreverse (parameters);
10762 /* Parse a parameter declaration.
10764 parameter-declaration:
10765 decl-specifier-seq declarator
10766 decl-specifier-seq declarator = assignment-expression
10767 decl-specifier-seq abstract-declarator [opt]
10768 decl-specifier-seq abstract-declarator [opt] = assignment-expression
10770 If GREATER_THAN_IS_OPERATOR_P is FALSE, then a non-nested `>' token
10771 encountered during the parsing of the assignment-expression is not
10772 interpreted as a greater-than operator.
10774 Returns a TREE_LIST representing the parameter-declaration. The
10775 TREE_VALUE is a representation of the decl-specifier-seq and
10776 declarator. In particular, the TREE_VALUE will be a TREE_LIST
10777 whose TREE_PURPOSE represents the decl-specifier-seq and whose
10778 TREE_VALUE represents the declarator. */
10781 cp_parser_parameter_declaration (parser, greater_than_is_operator_p)
10783 bool greater_than_is_operator_p;
10785 bool declares_class_or_enum;
10786 tree decl_specifiers;
10789 tree default_argument;
10792 const char *saved_message;
10794 /* Type definitions may not appear in parameter types. */
10795 saved_message = parser->type_definition_forbidden_message;
10796 parser->type_definition_forbidden_message
10797 = "types may not be defined in parameter types";
10799 /* Parse the declaration-specifiers. */
10801 = cp_parser_decl_specifier_seq (parser,
10802 CP_PARSER_FLAGS_NONE,
10804 &declares_class_or_enum);
10805 /* If an error occurred, there's no reason to attempt to parse the
10806 rest of the declaration. */
10807 if (cp_parser_error_occurred (parser))
10809 parser->type_definition_forbidden_message = saved_message;
10810 return error_mark_node;
10813 /* Peek at the next token. */
10814 token = cp_lexer_peek_token (parser->lexer);
10815 /* If the next token is a `)', `,', `=', `>', or `...', then there
10816 is no declarator. */
10817 if (token->type == CPP_CLOSE_PAREN
10818 || token->type == CPP_COMMA
10819 || token->type == CPP_EQ
10820 || token->type == CPP_ELLIPSIS
10821 || token->type == CPP_GREATER)
10822 declarator = NULL_TREE;
10823 /* Otherwise, there should be a declarator. */
10826 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
10827 parser->default_arg_ok_p = false;
10829 /* We don't know whether the declarator will be abstract or
10830 not. So, first we try an ordinary declarator. */
10831 cp_parser_parse_tentatively (parser);
10832 declarator = cp_parser_declarator (parser,
10833 /*abstract_p=*/false,
10834 /*ctor_dtor_or_conv_p=*/NULL);
10835 /* If that didn't work, look for an abstract declarator. */
10836 if (!cp_parser_parse_definitely (parser))
10837 declarator = cp_parser_declarator (parser,
10838 /*abstract_p=*/true,
10839 /*ctor_dtor_or_conv_p=*/NULL);
10840 parser->default_arg_ok_p = saved_default_arg_ok_p;
10843 /* The restriction on definining new types applies only to the type
10844 of the parameter, not to the default argument. */
10845 parser->type_definition_forbidden_message = saved_message;
10847 /* If the next token is `=', then process a default argument. */
10848 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10850 bool saved_greater_than_is_operator_p;
10851 /* Consume the `='. */
10852 cp_lexer_consume_token (parser->lexer);
10854 /* If we are defining a class, then the tokens that make up the
10855 default argument must be saved and processed later. */
10856 if (at_class_scope_p () && TYPE_BEING_DEFINED (current_class_type))
10858 unsigned depth = 0;
10860 /* Create a DEFAULT_ARG to represented the unparsed default
10862 default_argument = make_node (DEFAULT_ARG);
10863 DEFARG_TOKENS (default_argument) = cp_token_cache_new ();
10865 /* Add tokens until we have processed the entire default
10872 /* Peek at the next token. */
10873 token = cp_lexer_peek_token (parser->lexer);
10874 /* What we do depends on what token we have. */
10875 switch (token->type)
10877 /* In valid code, a default argument must be
10878 immediately followed by a `,' `)', or `...'. */
10880 case CPP_CLOSE_PAREN:
10882 /* If we run into a non-nested `;', `}', or `]',
10883 then the code is invalid -- but the default
10884 argument is certainly over. */
10885 case CPP_SEMICOLON:
10886 case CPP_CLOSE_BRACE:
10887 case CPP_CLOSE_SQUARE:
10890 /* Update DEPTH, if necessary. */
10891 else if (token->type == CPP_CLOSE_PAREN
10892 || token->type == CPP_CLOSE_BRACE
10893 || token->type == CPP_CLOSE_SQUARE)
10897 case CPP_OPEN_PAREN:
10898 case CPP_OPEN_SQUARE:
10899 case CPP_OPEN_BRACE:
10904 /* If we see a non-nested `>', and `>' is not an
10905 operator, then it marks the end of the default
10907 if (!depth && !greater_than_is_operator_p)
10911 /* If we run out of tokens, issue an error message. */
10913 error ("file ends in default argument");
10919 /* In these cases, we should look for template-ids.
10920 For example, if the default argument is
10921 `X<int, double>()', we need to do name lookup to
10922 figure out whether or not `X' is a template; if
10923 so, the `,' does not end the deault argument.
10925 That is not yet done. */
10932 /* If we've reached the end, stop. */
10936 /* Add the token to the token block. */
10937 token = cp_lexer_consume_token (parser->lexer);
10938 cp_token_cache_push_token (DEFARG_TOKENS (default_argument),
10942 /* Outside of a class definition, we can just parse the
10943 assignment-expression. */
10946 bool saved_local_variables_forbidden_p;
10948 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
10950 saved_greater_than_is_operator_p
10951 = parser->greater_than_is_operator_p;
10952 parser->greater_than_is_operator_p = greater_than_is_operator_p;
10953 /* Local variable names (and the `this' keyword) may not
10954 appear in a default argument. */
10955 saved_local_variables_forbidden_p
10956 = parser->local_variables_forbidden_p;
10957 parser->local_variables_forbidden_p = true;
10958 /* Parse the assignment-expression. */
10959 default_argument = cp_parser_assignment_expression (parser);
10960 /* Restore saved state. */
10961 parser->greater_than_is_operator_p
10962 = saved_greater_than_is_operator_p;
10963 parser->local_variables_forbidden_p
10964 = saved_local_variables_forbidden_p;
10966 if (!parser->default_arg_ok_p)
10968 pedwarn ("default arguments are only permitted on functions");
10969 if (flag_pedantic_errors)
10970 default_argument = NULL_TREE;
10974 default_argument = NULL_TREE;
10976 /* Create the representation of the parameter. */
10978 decl_specifiers = tree_cons (attributes, NULL_TREE, decl_specifiers);
10979 parameter = build_tree_list (default_argument,
10980 build_tree_list (decl_specifiers,
10986 /* Parse a function-definition.
10988 function-definition:
10989 decl-specifier-seq [opt] declarator ctor-initializer [opt]
10991 decl-specifier-seq [opt] declarator function-try-block
10995 function-definition:
10996 __extension__ function-definition
10998 Returns the FUNCTION_DECL for the function. If FRIEND_P is
10999 non-NULL, *FRIEND_P is set to TRUE iff the function was declared to
11003 cp_parser_function_definition (parser, friend_p)
11007 tree decl_specifiers;
11011 tree access_checks;
11013 bool declares_class_or_enum;
11015 /* The saved value of the PEDANTIC flag. */
11016 int saved_pedantic;
11018 /* Any pending qualification must be cleared by our caller. It is
11019 more robust to force the callers to clear PARSER->SCOPE than to
11020 do it here since if the qualification is in effect here, it might
11021 also end up in effect elsewhere that it is not intended. */
11022 my_friendly_assert (!parser->scope, 20010821);
11024 /* Handle `__extension__'. */
11025 if (cp_parser_extension_opt (parser, &saved_pedantic))
11027 /* Parse the function-definition. */
11028 fn = cp_parser_function_definition (parser, friend_p);
11029 /* Restore the PEDANTIC flag. */
11030 pedantic = saved_pedantic;
11035 /* Check to see if this definition appears in a class-specifier. */
11036 member_p = (at_class_scope_p ()
11037 && TYPE_BEING_DEFINED (current_class_type));
11038 /* Defer access checks in the decl-specifier-seq until we know what
11039 function is being defined. There is no need to do this for the
11040 definition of member functions; we cannot be defining a member
11041 from another class. */
11043 cp_parser_start_deferring_access_checks (parser);
11044 /* Parse the decl-specifier-seq. */
11046 = cp_parser_decl_specifier_seq (parser,
11047 CP_PARSER_FLAGS_OPTIONAL,
11049 &declares_class_or_enum);
11050 /* Figure out whether this declaration is a `friend'. */
11052 *friend_p = cp_parser_friend_p (decl_specifiers);
11054 /* Parse the declarator. */
11055 declarator = cp_parser_declarator (parser,
11056 /*abstract_p=*/false,
11057 /*ctor_dtor_or_conv_p=*/NULL);
11059 /* Gather up any access checks that occurred. */
11061 access_checks = cp_parser_stop_deferring_access_checks (parser);
11063 access_checks = NULL_TREE;
11065 /* If something has already gone wrong, we may as well stop now. */
11066 if (declarator == error_mark_node)
11068 /* Skip to the end of the function, or if this wasn't anything
11069 like a function-definition, to a `;' in the hopes of finding
11070 a sensible place from which to continue parsing. */
11071 cp_parser_skip_to_end_of_block_or_statement (parser);
11072 return error_mark_node;
11075 /* The next character should be a `{' (for a simple function
11076 definition), a `:' (for a ctor-initializer), or `try' (for a
11077 function-try block). */
11078 token = cp_lexer_peek_token (parser->lexer);
11079 if (!cp_parser_token_starts_function_definition_p (token))
11081 /* Issue the error-message. */
11082 cp_parser_error (parser, "expected function-definition");
11083 /* Skip to the next `;'. */
11084 cp_parser_skip_to_end_of_block_or_statement (parser);
11086 return error_mark_node;
11089 /* If we are in a class scope, then we must handle
11090 function-definitions specially. In particular, we save away the
11091 tokens that make up the function body, and parse them again
11092 later, in order to handle code like:
11095 int f () { return i; }
11099 Here, we cannot parse the body of `f' until after we have seen
11100 the declaration of `i'. */
11103 cp_token_cache *cache;
11105 /* Create the function-declaration. */
11106 fn = start_method (decl_specifiers, declarator, attributes);
11107 /* If something went badly wrong, bail out now. */
11108 if (fn == error_mark_node)
11110 /* If there's a function-body, skip it. */
11111 if (cp_parser_token_starts_function_definition_p
11112 (cp_lexer_peek_token (parser->lexer)))
11113 cp_parser_skip_to_end_of_block_or_statement (parser);
11114 return error_mark_node;
11117 /* Create a token cache. */
11118 cache = cp_token_cache_new ();
11119 /* Save away the tokens that make up the body of the
11121 cp_parser_cache_group (parser, cache, CPP_CLOSE_BRACE, /*depth=*/0);
11122 /* Handle function try blocks. */
11123 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
11124 cp_parser_cache_group (parser, cache, CPP_CLOSE_BRACE, /*depth=*/0);
11126 /* Save away the inline definition; we will process it when the
11127 class is complete. */
11128 DECL_PENDING_INLINE_INFO (fn) = cache;
11129 DECL_PENDING_INLINE_P (fn) = 1;
11131 /* We're done with the inline definition. */
11132 finish_method (fn);
11134 /* Add FN to the queue of functions to be parsed later. */
11135 TREE_VALUE (parser->unparsed_functions_queues)
11136 = tree_cons (current_class_type, fn,
11137 TREE_VALUE (parser->unparsed_functions_queues));
11142 /* Check that the number of template-parameter-lists is OK. */
11143 if (!cp_parser_check_declarator_template_parameters (parser,
11146 cp_parser_skip_to_end_of_block_or_statement (parser);
11147 return error_mark_node;
11150 return (cp_parser_function_definition_from_specifiers_and_declarator
11151 (parser, decl_specifiers, attributes, declarator, access_checks));
11154 /* Parse a function-body.
11157 compound_statement */
11160 cp_parser_function_body (cp_parser *parser)
11162 cp_parser_compound_statement (parser);
11165 /* Parse a ctor-initializer-opt followed by a function-body. Return
11166 true if a ctor-initializer was present. */
11169 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
11172 bool ctor_initializer_p;
11174 /* Begin the function body. */
11175 body = begin_function_body ();
11176 /* Parse the optional ctor-initializer. */
11177 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
11178 /* Parse the function-body. */
11179 cp_parser_function_body (parser);
11180 /* Finish the function body. */
11181 finish_function_body (body);
11183 return ctor_initializer_p;
11186 /* Parse an initializer.
11189 = initializer-clause
11190 ( expression-list )
11192 Returns a expression representing the initializer. If no
11193 initializer is present, NULL_TREE is returned.
11195 *IS_PARENTHESIZED_INIT is set to TRUE if the `( expression-list )'
11196 production is used, and zero otherwise. *IS_PARENTHESIZED_INIT is
11197 set to FALSE if there is no initializer present. */
11200 cp_parser_initializer (parser, is_parenthesized_init)
11202 bool *is_parenthesized_init;
11207 /* Peek at the next token. */
11208 token = cp_lexer_peek_token (parser->lexer);
11210 /* Let our caller know whether or not this initializer was
11212 *is_parenthesized_init = (token->type == CPP_OPEN_PAREN);
11214 if (token->type == CPP_EQ)
11216 /* Consume the `='. */
11217 cp_lexer_consume_token (parser->lexer);
11218 /* Parse the initializer-clause. */
11219 init = cp_parser_initializer_clause (parser);
11221 else if (token->type == CPP_OPEN_PAREN)
11223 /* Consume the `('. */
11224 cp_lexer_consume_token (parser->lexer);
11225 /* Parse the expression-list. */
11226 init = cp_parser_expression_list (parser);
11227 /* Consume the `)' token. */
11228 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
11229 cp_parser_skip_to_closing_parenthesis (parser);
11233 /* Anything else is an error. */
11234 cp_parser_error (parser, "expected initializer");
11235 init = error_mark_node;
11241 /* Parse an initializer-clause.
11243 initializer-clause:
11244 assignment-expression
11245 { initializer-list , [opt] }
11248 Returns an expression representing the initializer.
11250 If the `assignment-expression' production is used the value
11251 returned is simply a reprsentation for the expression.
11253 Otherwise, a CONSTRUCTOR is returned. The CONSTRUCTOR_ELTS will be
11254 the elements of the initializer-list (or NULL_TREE, if the last
11255 production is used). The TREE_TYPE for the CONSTRUCTOR will be
11256 NULL_TREE. There is no way to detect whether or not the optional
11257 trailing `,' was provided. */
11260 cp_parser_initializer_clause (parser)
11265 /* If it is not a `{', then we are looking at an
11266 assignment-expression. */
11267 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
11268 initializer = cp_parser_assignment_expression (parser);
11271 /* Consume the `{' token. */
11272 cp_lexer_consume_token (parser->lexer);
11273 /* Create a CONSTRUCTOR to represent the braced-initializer. */
11274 initializer = make_node (CONSTRUCTOR);
11275 /* Mark it with TREE_HAS_CONSTRUCTOR. This should not be
11276 necessary, but check_initializer depends upon it, for
11278 TREE_HAS_CONSTRUCTOR (initializer) = 1;
11279 /* If it's not a `}', then there is a non-trivial initializer. */
11280 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
11282 /* Parse the initializer list. */
11283 CONSTRUCTOR_ELTS (initializer)
11284 = cp_parser_initializer_list (parser);
11285 /* A trailing `,' token is allowed. */
11286 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
11287 cp_lexer_consume_token (parser->lexer);
11290 /* Now, there should be a trailing `}'. */
11291 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
11294 return initializer;
11297 /* Parse an initializer-list.
11301 initializer-list , initializer-clause
11306 identifier : initializer-clause
11307 initializer-list, identifier : initializer-clause
11309 Returns a TREE_LIST. The TREE_VALUE of each node is an expression
11310 for the initializer. If the TREE_PURPOSE is non-NULL, it is the
11311 IDENTIFIER_NODE naming the field to initialize. */
11314 cp_parser_initializer_list (parser)
11317 tree initializers = NULL_TREE;
11319 /* Parse the rest of the list. */
11326 /* If the next token is an identifier and the following one is a
11327 colon, we are looking at the GNU designated-initializer
11329 if (cp_parser_allow_gnu_extensions_p (parser)
11330 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
11331 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
11333 /* Consume the identifier. */
11334 identifier = cp_lexer_consume_token (parser->lexer)->value;
11335 /* Consume the `:'. */
11336 cp_lexer_consume_token (parser->lexer);
11339 identifier = NULL_TREE;
11341 /* Parse the initializer. */
11342 initializer = cp_parser_initializer_clause (parser);
11344 /* Add it to the list. */
11345 initializers = tree_cons (identifier, initializer, initializers);
11347 /* If the next token is not a comma, we have reached the end of
11349 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11352 /* Peek at the next token. */
11353 token = cp_lexer_peek_nth_token (parser->lexer, 2);
11354 /* If the next token is a `}', then we're still done. An
11355 initializer-clause can have a trailing `,' after the
11356 initializer-list and before the closing `}'. */
11357 if (token->type == CPP_CLOSE_BRACE)
11360 /* Consume the `,' token. */
11361 cp_lexer_consume_token (parser->lexer);
11364 /* The initializers were built up in reverse order, so we need to
11365 reverse them now. */
11366 return nreverse (initializers);
11369 /* Classes [gram.class] */
11371 /* Parse a class-name.
11377 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
11378 to indicate that names looked up in dependent types should be
11379 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
11380 keyword has been used to indicate that the name that appears next
11381 is a template. TYPE_P is true iff the next name should be treated
11382 as class-name, even if it is declared to be some other kind of name
11383 as well. The accessibility of the class-name is checked iff
11384 CHECK_ACCESS_P is true. If CHECK_DEPENDENCY_P is FALSE, names are
11385 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
11386 is the class being defined in a class-head.
11388 Returns the TYPE_DECL representing the class. */
11391 cp_parser_class_name (cp_parser *parser,
11392 bool typename_keyword_p,
11393 bool template_keyword_p,
11395 bool check_access_p,
11396 bool check_dependency_p,
11404 /* All class-names start with an identifier. */
11405 token = cp_lexer_peek_token (parser->lexer);
11406 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
11408 cp_parser_error (parser, "expected class-name");
11409 return error_mark_node;
11412 /* PARSER->SCOPE can be cleared when parsing the template-arguments
11413 to a template-id, so we save it here. */
11414 scope = parser->scope;
11415 /* Any name names a type if we're following the `typename' keyword
11416 in a qualified name where the enclosing scope is type-dependent. */
11417 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
11418 && cp_parser_dependent_type_p (scope));
11419 /* Handle the common case (an identifier, but not a template-id)
11421 if (token->type == CPP_NAME
11422 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_LESS)
11426 /* Look for the identifier. */
11427 identifier = cp_parser_identifier (parser);
11428 /* If the next token isn't an identifier, we are certainly not
11429 looking at a class-name. */
11430 if (identifier == error_mark_node)
11431 decl = error_mark_node;
11432 /* If we know this is a type-name, there's no need to look it
11434 else if (typename_p)
11438 /* If the next token is a `::', then the name must be a type
11441 [basic.lookup.qual]
11443 During the lookup for a name preceding the :: scope
11444 resolution operator, object, function, and enumerator
11445 names are ignored. */
11446 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
11448 /* Look up the name. */
11449 decl = cp_parser_lookup_name (parser, identifier,
11452 /*is_namespace=*/false,
11453 check_dependency_p);
11458 /* Try a template-id. */
11459 decl = cp_parser_template_id (parser, template_keyword_p,
11460 check_dependency_p);
11461 if (decl == error_mark_node)
11462 return error_mark_node;
11465 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
11467 /* If this is a typename, create a TYPENAME_TYPE. */
11468 if (typename_p && decl != error_mark_node)
11469 decl = TYPE_NAME (make_typename_type (scope, decl,
11472 /* Check to see that it is really the name of a class. */
11473 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
11474 && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
11475 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
11476 /* Situations like this:
11478 template <typename T> struct A {
11479 typename T::template X<int>::I i;
11482 are problematic. Is `T::template X<int>' a class-name? The
11483 standard does not seem to be definitive, but there is no other
11484 valid interpretation of the following `::'. Therefore, those
11485 names are considered class-names. */
11486 decl = TYPE_NAME (make_typename_type (scope, decl,
11487 tf_error | tf_parsing));
11488 else if (decl == error_mark_node
11489 || TREE_CODE (decl) != TYPE_DECL
11490 || !IS_AGGR_TYPE (TREE_TYPE (decl)))
11492 cp_parser_error (parser, "expected class-name");
11493 return error_mark_node;
11499 /* Parse a class-specifier.
11502 class-head { member-specification [opt] }
11504 Returns the TREE_TYPE representing the class. */
11507 cp_parser_class_specifier (parser)
11512 tree attributes = NULL_TREE;
11513 int has_trailing_semicolon;
11514 bool nested_name_specifier_p;
11515 bool deferring_access_checks_p;
11516 tree saved_access_checks;
11517 unsigned saved_num_template_parameter_lists;
11519 /* Parse the class-head. */
11520 type = cp_parser_class_head (parser,
11521 &nested_name_specifier_p,
11522 &deferring_access_checks_p,
11523 &saved_access_checks);
11524 /* If the class-head was a semantic disaster, skip the entire body
11528 cp_parser_skip_to_end_of_block_or_statement (parser);
11529 return error_mark_node;
11531 /* Look for the `{'. */
11532 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
11533 return error_mark_node;
11534 /* Issue an error message if type-definitions are forbidden here. */
11535 cp_parser_check_type_definition (parser);
11536 /* Remember that we are defining one more class. */
11537 ++parser->num_classes_being_defined;
11538 /* Inside the class, surrounding template-parameter-lists do not
11540 saved_num_template_parameter_lists
11541 = parser->num_template_parameter_lists;
11542 parser->num_template_parameter_lists = 0;
11543 /* Start the class. */
11544 type = begin_class_definition (type);
11545 if (type == error_mark_node)
11546 /* If the type is erroneous, skip the entire body of the class. */
11547 cp_parser_skip_to_closing_brace (parser);
11549 /* Parse the member-specification. */
11550 cp_parser_member_specification_opt (parser);
11551 /* Look for the trailing `}'. */
11552 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
11553 /* We get better error messages by noticing a common problem: a
11554 missing trailing `;'. */
11555 token = cp_lexer_peek_token (parser->lexer);
11556 has_trailing_semicolon = (token->type == CPP_SEMICOLON);
11557 /* Look for attributes to apply to this class. */
11558 if (cp_parser_allow_gnu_extensions_p (parser))
11559 attributes = cp_parser_attributes_opt (parser);
11560 /* Finish the class definition. */
11561 type = finish_class_definition (type,
11563 has_trailing_semicolon,
11564 nested_name_specifier_p);
11565 /* If this class is not itself within the scope of another class,
11566 then we need to parse the bodies of all of the queued function
11567 definitions. Note that the queued functions defined in a class
11568 are not always processed immediately following the
11569 class-specifier for that class. Consider:
11572 struct B { void f() { sizeof (A); } };
11575 If `f' were processed before the processing of `A' were
11576 completed, there would be no way to compute the size of `A'.
11577 Note that the nesting we are interested in here is lexical --
11578 not the semantic nesting given by TYPE_CONTEXT. In particular,
11581 struct A { struct B; };
11582 struct A::B { void f() { } };
11584 there is no need to delay the parsing of `A::B::f'. */
11585 if (--parser->num_classes_being_defined == 0)
11587 tree last_scope = NULL_TREE;
11589 /* Process non FUNCTION_DECL related DEFAULT_ARGs. */
11590 for (parser->default_arg_types = nreverse (parser->default_arg_types);
11591 parser->default_arg_types;
11592 parser->default_arg_types = TREE_CHAIN (parser->default_arg_types))
11593 cp_parser_late_parsing_default_args
11594 (parser, TREE_PURPOSE (parser->default_arg_types), NULL_TREE);
11596 /* Reverse the queue, so that we process it in the order the
11597 functions were declared. */
11598 TREE_VALUE (parser->unparsed_functions_queues)
11599 = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
11600 /* Loop through all of the functions. */
11601 while (TREE_VALUE (parser->unparsed_functions_queues))
11608 /* Figure out which function we need to process. */
11609 queue_entry = TREE_VALUE (parser->unparsed_functions_queues);
11610 fn_scope = TREE_PURPOSE (queue_entry);
11611 fn = TREE_VALUE (queue_entry);
11613 /* Parse the function. */
11614 cp_parser_late_parsing_for_member (parser, fn);
11616 TREE_VALUE (parser->unparsed_functions_queues)
11617 = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues));
11620 /* If LAST_SCOPE is non-NULL, then we have pushed scopes one
11621 more time than we have popped, so me must pop here. */
11623 pop_scope (last_scope);
11626 /* Put back any saved access checks. */
11627 if (deferring_access_checks_p)
11629 cp_parser_start_deferring_access_checks (parser);
11630 parser->context->deferred_access_checks = saved_access_checks;
11633 /* Restore the count of active template-parameter-lists. */
11634 parser->num_template_parameter_lists
11635 = saved_num_template_parameter_lists;
11640 /* Parse a class-head.
11643 class-key identifier [opt] base-clause [opt]
11644 class-key nested-name-specifier identifier base-clause [opt]
11645 class-key nested-name-specifier [opt] template-id
11649 class-key attributes identifier [opt] base-clause [opt]
11650 class-key attributes nested-name-specifier identifier base-clause [opt]
11651 class-key attributes nested-name-specifier [opt] template-id
11654 Returns the TYPE of the indicated class. Sets
11655 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
11656 involving a nested-name-specifier was used, and FALSE otherwise.
11657 Sets *DEFERRING_ACCESS_CHECKS_P to TRUE iff we were deferring
11658 access checks before this class-head. In that case,
11659 *SAVED_ACCESS_CHECKS is set to the current list of deferred access
11662 Returns NULL_TREE if the class-head is syntactically valid, but
11663 semantically invalid in a way that means we should skip the entire
11664 body of the class. */
11667 cp_parser_class_head (parser,
11668 nested_name_specifier_p,
11669 deferring_access_checks_p,
11670 saved_access_checks)
11672 bool *nested_name_specifier_p;
11673 bool *deferring_access_checks_p;
11674 tree *saved_access_checks;
11677 tree nested_name_specifier;
11678 enum tag_types class_key;
11679 tree id = NULL_TREE;
11680 tree type = NULL_TREE;
11682 bool template_id_p = false;
11683 bool qualified_p = false;
11684 bool invalid_nested_name_p = false;
11685 unsigned num_templates;
11687 /* Assume no nested-name-specifier will be present. */
11688 *nested_name_specifier_p = false;
11689 /* Assume no template parameter lists will be used in defining the
11693 /* Look for the class-key. */
11694 class_key = cp_parser_class_key (parser);
11695 if (class_key == none_type)
11696 return error_mark_node;
11698 /* Parse the attributes. */
11699 attributes = cp_parser_attributes_opt (parser);
11701 /* If the next token is `::', that is invalid -- but sometimes
11702 people do try to write:
11706 Handle this gracefully by accepting the extra qualifier, and then
11707 issuing an error about it later if this really is a
11708 class-header. If it turns out just to be an elaborated type
11709 specifier, remain silent. */
11710 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
11711 qualified_p = true;
11713 /* Determine the name of the class. Begin by looking for an
11714 optional nested-name-specifier. */
11715 nested_name_specifier
11716 = cp_parser_nested_name_specifier_opt (parser,
11717 /*typename_keyword_p=*/false,
11718 /*check_dependency_p=*/true,
11720 /* If there was a nested-name-specifier, then there *must* be an
11722 if (nested_name_specifier)
11724 /* Although the grammar says `identifier', it really means
11725 `class-name' or `template-name'. You are only allowed to
11726 define a class that has already been declared with this
11729 The proposed resolution for Core Issue 180 says that whever
11730 you see `class T::X' you should treat `X' as a type-name.
11732 It is OK to define an inaccessible class; for example:
11734 class A { class B; };
11737 So, we ask cp_parser_class_name not to check accessibility.
11739 We do not know if we will see a class-name, or a
11740 template-name. We look for a class-name first, in case the
11741 class-name is a template-id; if we looked for the
11742 template-name first we would stop after the template-name. */
11743 cp_parser_parse_tentatively (parser);
11744 type = cp_parser_class_name (parser,
11745 /*typename_keyword_p=*/false,
11746 /*template_keyword_p=*/false,
11748 /*check_access_p=*/false,
11749 /*check_dependency_p=*/false,
11750 /*class_head_p=*/true);
11751 /* If that didn't work, ignore the nested-name-specifier. */
11752 if (!cp_parser_parse_definitely (parser))
11754 invalid_nested_name_p = true;
11755 id = cp_parser_identifier (parser);
11756 if (id == error_mark_node)
11759 /* If we could not find a corresponding TYPE, treat this
11760 declaration like an unqualified declaration. */
11761 if (type == error_mark_node)
11762 nested_name_specifier = NULL_TREE;
11763 /* Otherwise, count the number of templates used in TYPE and its
11764 containing scopes. */
11769 for (scope = TREE_TYPE (type);
11770 scope && TREE_CODE (scope) != NAMESPACE_DECL;
11771 scope = (TYPE_P (scope)
11772 ? TYPE_CONTEXT (scope)
11773 : DECL_CONTEXT (scope)))
11775 && CLASS_TYPE_P (scope)
11776 && CLASSTYPE_TEMPLATE_INFO (scope)
11777 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
11781 /* Otherwise, the identifier is optional. */
11784 /* We don't know whether what comes next is a template-id,
11785 an identifier, or nothing at all. */
11786 cp_parser_parse_tentatively (parser);
11787 /* Check for a template-id. */
11788 id = cp_parser_template_id (parser,
11789 /*template_keyword_p=*/false,
11790 /*check_dependency_p=*/true);
11791 /* If that didn't work, it could still be an identifier. */
11792 if (!cp_parser_parse_definitely (parser))
11794 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
11795 id = cp_parser_identifier (parser);
11801 template_id_p = true;
11806 /* If it's not a `:' or a `{' then we can't really be looking at a
11807 class-head, since a class-head only appears as part of a
11808 class-specifier. We have to detect this situation before calling
11809 xref_tag, since that has irreversible side-effects. */
11810 if (!cp_parser_next_token_starts_class_definition_p (parser))
11812 cp_parser_error (parser, "expected `{' or `:'");
11813 return error_mark_node;
11816 /* At this point, we're going ahead with the class-specifier, even
11817 if some other problem occurs. */
11818 cp_parser_commit_to_tentative_parse (parser);
11819 /* Issue the error about the overly-qualified name now. */
11821 cp_parser_error (parser,
11822 "global qualification of class name is invalid");
11823 else if (invalid_nested_name_p)
11824 cp_parser_error (parser,
11825 "qualified name does not name a class");
11826 /* Make sure that the right number of template parameters were
11828 if (!cp_parser_check_template_parameters (parser, num_templates))
11829 /* If something went wrong, there is no point in even trying to
11830 process the class-definition. */
11833 /* We do not need to defer access checks for entities declared
11834 within the class. But, we do need to save any access checks that
11835 are currently deferred and restore them later, in case we are in
11836 the middle of something else. */
11837 *deferring_access_checks_p = parser->context->deferring_access_checks_p;
11838 if (*deferring_access_checks_p)
11839 *saved_access_checks = cp_parser_stop_deferring_access_checks (parser);
11841 /* Look up the type. */
11844 type = TREE_TYPE (id);
11845 maybe_process_partial_specialization (type);
11847 else if (!nested_name_specifier)
11849 /* If the class was unnamed, create a dummy name. */
11851 id = make_anon_name ();
11852 type = xref_tag (class_key, id, attributes, /*globalize=*/0);
11861 template <typename T> struct S { struct T };
11862 template <typename T> struct S::T { };
11864 we will get a TYPENAME_TYPE when processing the definition of
11865 `S::T'. We need to resolve it to the actual type before we
11866 try to define it. */
11867 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
11869 type = cp_parser_resolve_typename_type (parser, TREE_TYPE (type));
11870 if (type != error_mark_node)
11871 type = TYPE_NAME (type);
11874 maybe_process_partial_specialization (TREE_TYPE (type));
11875 class_type = current_class_type;
11876 type = TREE_TYPE (handle_class_head (class_key,
11877 nested_name_specifier,
11882 if (type != error_mark_node)
11884 if (!class_type && TYPE_CONTEXT (type))
11885 *nested_name_specifier_p = true;
11886 else if (class_type && !same_type_p (TYPE_CONTEXT (type),
11888 *nested_name_specifier_p = true;
11891 /* Indicate whether this class was declared as a `class' or as a
11893 if (TREE_CODE (type) == RECORD_TYPE)
11894 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
11895 cp_parser_check_class_key (class_key, type);
11897 /* Enter the scope containing the class; the names of base classes
11898 should be looked up in that context. For example, given:
11900 struct A { struct B {}; struct C; };
11901 struct A::C : B {};
11904 if (nested_name_specifier)
11905 push_scope (nested_name_specifier);
11906 /* Now, look for the base-clause. */
11907 token = cp_lexer_peek_token (parser->lexer);
11908 if (token->type == CPP_COLON)
11912 /* Get the list of base-classes. */
11913 bases = cp_parser_base_clause (parser);
11914 /* Process them. */
11915 xref_basetypes (type, bases);
11917 /* Leave the scope given by the nested-name-specifier. We will
11918 enter the class scope itself while processing the members. */
11919 if (nested_name_specifier)
11920 pop_scope (nested_name_specifier);
11925 /* Parse a class-key.
11932 Returns the kind of class-key specified, or none_type to indicate
11935 static enum tag_types
11936 cp_parser_class_key (parser)
11940 enum tag_types tag_type;
11942 /* Look for the class-key. */
11943 token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
11947 /* Check to see if the TOKEN is a class-key. */
11948 tag_type = cp_parser_token_is_class_key (token);
11950 cp_parser_error (parser, "expected class-key");
11954 /* Parse an (optional) member-specification.
11956 member-specification:
11957 member-declaration member-specification [opt]
11958 access-specifier : member-specification [opt] */
11961 cp_parser_member_specification_opt (parser)
11969 /* Peek at the next token. */
11970 token = cp_lexer_peek_token (parser->lexer);
11971 /* If it's a `}', or EOF then we've seen all the members. */
11972 if (token->type == CPP_CLOSE_BRACE || token->type == CPP_EOF)
11975 /* See if this token is a keyword. */
11976 keyword = token->keyword;
11980 case RID_PROTECTED:
11982 /* Consume the access-specifier. */
11983 cp_lexer_consume_token (parser->lexer);
11984 /* Remember which access-specifier is active. */
11985 current_access_specifier = token->value;
11986 /* Look for the `:'. */
11987 cp_parser_require (parser, CPP_COLON, "`:'");
11991 /* Otherwise, the next construction must be a
11992 member-declaration. */
11993 cp_parser_member_declaration (parser);
11994 reset_type_access_control ();
11999 /* Parse a member-declaration.
12001 member-declaration:
12002 decl-specifier-seq [opt] member-declarator-list [opt] ;
12003 function-definition ; [opt]
12004 :: [opt] nested-name-specifier template [opt] unqualified-id ;
12006 template-declaration
12008 member-declarator-list:
12010 member-declarator-list , member-declarator
12013 declarator pure-specifier [opt]
12014 declarator constant-initializer [opt]
12015 identifier [opt] : constant-expression
12019 member-declaration:
12020 __extension__ member-declaration
12023 declarator attributes [opt] pure-specifier [opt]
12024 declarator attributes [opt] constant-initializer [opt]
12025 identifier [opt] attributes [opt] : constant-expression */
12028 cp_parser_member_declaration (parser)
12031 tree decl_specifiers;
12032 tree prefix_attributes;
12034 bool declares_class_or_enum;
12037 int saved_pedantic;
12039 /* Check for the `__extension__' keyword. */
12040 if (cp_parser_extension_opt (parser, &saved_pedantic))
12043 cp_parser_member_declaration (parser);
12044 /* Restore the old value of the PEDANTIC flag. */
12045 pedantic = saved_pedantic;
12050 /* Check for a template-declaration. */
12051 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
12053 /* Parse the template-declaration. */
12054 cp_parser_template_declaration (parser, /*member_p=*/true);
12059 /* Check for a using-declaration. */
12060 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
12062 /* Parse the using-declaration. */
12063 cp_parser_using_declaration (parser);
12068 /* We can't tell whether we're looking at a declaration or a
12069 function-definition. */
12070 cp_parser_parse_tentatively (parser);
12072 /* Parse the decl-specifier-seq. */
12074 = cp_parser_decl_specifier_seq (parser,
12075 CP_PARSER_FLAGS_OPTIONAL,
12076 &prefix_attributes,
12077 &declares_class_or_enum);
12078 /* If there is no declarator, then the decl-specifier-seq should
12080 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12082 /* If there was no decl-specifier-seq, and the next token is a
12083 `;', then we have something like:
12089 Each member-declaration shall declare at least one member
12090 name of the class. */
12091 if (!decl_specifiers)
12094 pedwarn ("extra semicolon");
12100 /* See if this declaration is a friend. */
12101 friend_p = cp_parser_friend_p (decl_specifiers);
12102 /* If there were decl-specifiers, check to see if there was
12103 a class-declaration. */
12104 type = check_tag_decl (decl_specifiers);
12105 /* Nested classes have already been added to the class, but
12106 a `friend' needs to be explicitly registered. */
12109 /* If the `friend' keyword was present, the friend must
12110 be introduced with a class-key. */
12111 if (!declares_class_or_enum)
12112 error ("a class-key must be used when declaring a friend");
12115 template <typename T> struct A {
12116 friend struct A<T>::B;
12119 A<T>::B will be represented by a TYPENAME_TYPE, and
12120 therefore not recognized by check_tag_decl. */
12125 for (specifier = decl_specifiers;
12127 specifier = TREE_CHAIN (specifier))
12129 tree s = TREE_VALUE (specifier);
12131 if (TREE_CODE (s) == IDENTIFIER_NODE
12132 && IDENTIFIER_GLOBAL_VALUE (s))
12133 type = IDENTIFIER_GLOBAL_VALUE (s);
12134 if (TREE_CODE (s) == TYPE_DECL)
12144 error ("friend declaration does not name a class or "
12147 make_friend_class (current_class_type, type);
12149 /* If there is no TYPE, an error message will already have
12153 /* An anonymous aggregate has to be handled specially; such
12154 a declaration really declares a data member (with a
12155 particular type), as opposed to a nested class. */
12156 else if (ANON_AGGR_TYPE_P (type))
12158 /* Remove constructors and such from TYPE, now that we
12159 know it is an anoymous aggregate. */
12160 fixup_anonymous_aggr (type);
12161 /* And make the corresponding data member. */
12162 decl = build_decl (FIELD_DECL, NULL_TREE, type);
12163 /* Add it to the class. */
12164 finish_member_declaration (decl);
12170 /* See if these declarations will be friends. */
12171 friend_p = cp_parser_friend_p (decl_specifiers);
12173 /* Keep going until we hit the `;' at the end of the
12175 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12177 tree attributes = NULL_TREE;
12178 tree first_attribute;
12180 /* Peek at the next token. */
12181 token = cp_lexer_peek_token (parser->lexer);
12183 /* Check for a bitfield declaration. */
12184 if (token->type == CPP_COLON
12185 || (token->type == CPP_NAME
12186 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
12192 /* Get the name of the bitfield. Note that we cannot just
12193 check TOKEN here because it may have been invalidated by
12194 the call to cp_lexer_peek_nth_token above. */
12195 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
12196 identifier = cp_parser_identifier (parser);
12198 identifier = NULL_TREE;
12200 /* Consume the `:' token. */
12201 cp_lexer_consume_token (parser->lexer);
12202 /* Get the width of the bitfield. */
12203 width = cp_parser_constant_expression (parser);
12205 /* Look for attributes that apply to the bitfield. */
12206 attributes = cp_parser_attributes_opt (parser);
12207 /* Remember which attributes are prefix attributes and
12209 first_attribute = attributes;
12210 /* Combine the attributes. */
12211 attributes = chainon (prefix_attributes, attributes);
12213 /* Create the bitfield declaration. */
12214 decl = grokbitfield (identifier,
12217 /* Apply the attributes. */
12218 cplus_decl_attributes (&decl, attributes, /*flags=*/0);
12224 tree asm_specification;
12225 bool ctor_dtor_or_conv_p;
12227 /* Parse the declarator. */
12229 = cp_parser_declarator (parser,
12230 /*abstract_p=*/false,
12231 &ctor_dtor_or_conv_p);
12233 /* If something went wrong parsing the declarator, make sure
12234 that we at least consume some tokens. */
12235 if (declarator == error_mark_node)
12237 /* Skip to the end of the statement. */
12238 cp_parser_skip_to_end_of_statement (parser);
12242 /* Look for an asm-specification. */
12243 asm_specification = cp_parser_asm_specification_opt (parser);
12244 /* Look for attributes that apply to the declaration. */
12245 attributes = cp_parser_attributes_opt (parser);
12246 /* Remember which attributes are prefix attributes and
12248 first_attribute = attributes;
12249 /* Combine the attributes. */
12250 attributes = chainon (prefix_attributes, attributes);
12252 /* If it's an `=', then we have a constant-initializer or a
12253 pure-specifier. It is not correct to parse the
12254 initializer before registering the member declaration
12255 since the member declaration should be in scope while
12256 its initializer is processed. However, the rest of the
12257 front end does not yet provide an interface that allows
12258 us to handle this correctly. */
12259 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12263 A pure-specifier shall be used only in the declaration of
12264 a virtual function.
12266 A member-declarator can contain a constant-initializer
12267 only if it declares a static member of integral or
12270 Therefore, if the DECLARATOR is for a function, we look
12271 for a pure-specifier; otherwise, we look for a
12272 constant-initializer. When we call `grokfield', it will
12273 perform more stringent semantics checks. */
12274 if (TREE_CODE (declarator) == CALL_EXPR)
12275 initializer = cp_parser_pure_specifier (parser);
12278 /* This declaration cannot be a function
12280 cp_parser_commit_to_tentative_parse (parser);
12281 /* Parse the initializer. */
12282 initializer = cp_parser_constant_initializer (parser);
12285 /* Otherwise, there is no initializer. */
12287 initializer = NULL_TREE;
12289 /* See if we are probably looking at a function
12290 definition. We are certainly not looking at at a
12291 member-declarator. Calling `grokfield' has
12292 side-effects, so we must not do it unless we are sure
12293 that we are looking at a member-declarator. */
12294 if (cp_parser_token_starts_function_definition_p
12295 (cp_lexer_peek_token (parser->lexer)))
12296 decl = error_mark_node;
12298 /* Create the declaration. */
12299 decl = grokfield (declarator,
12306 /* Reset PREFIX_ATTRIBUTES. */
12307 while (attributes && TREE_CHAIN (attributes) != first_attribute)
12308 attributes = TREE_CHAIN (attributes);
12310 TREE_CHAIN (attributes) = NULL_TREE;
12312 /* If there is any qualification still in effect, clear it
12313 now; we will be starting fresh with the next declarator. */
12314 parser->scope = NULL_TREE;
12315 parser->qualifying_scope = NULL_TREE;
12316 parser->object_scope = NULL_TREE;
12317 /* If it's a `,', then there are more declarators. */
12318 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
12319 cp_lexer_consume_token (parser->lexer);
12320 /* If the next token isn't a `;', then we have a parse error. */
12321 else if (cp_lexer_next_token_is_not (parser->lexer,
12324 cp_parser_error (parser, "expected `;'");
12325 /* Skip tokens until we find a `;' */
12326 cp_parser_skip_to_end_of_statement (parser);
12333 /* Add DECL to the list of members. */
12335 finish_member_declaration (decl);
12337 /* If DECL is a function, we must return
12338 to parse it later. (Even though there is no definition,
12339 there might be default arguments that need handling.) */
12340 if (TREE_CODE (decl) == FUNCTION_DECL)
12341 TREE_VALUE (parser->unparsed_functions_queues)
12342 = tree_cons (current_class_type, decl,
12343 TREE_VALUE (parser->unparsed_functions_queues));
12348 /* If everything went well, look for the `;'. */
12349 if (cp_parser_parse_definitely (parser))
12351 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
12355 /* Parse the function-definition. */
12356 decl = cp_parser_function_definition (parser, &friend_p);
12357 /* If the member was not a friend, declare it here. */
12359 finish_member_declaration (decl);
12360 /* Peek at the next token. */
12361 token = cp_lexer_peek_token (parser->lexer);
12362 /* If the next token is a semicolon, consume it. */
12363 if (token->type == CPP_SEMICOLON)
12364 cp_lexer_consume_token (parser->lexer);
12367 /* Parse a pure-specifier.
12372 Returns INTEGER_ZERO_NODE if a pure specifier is found.
12373 Otherwiser, ERROR_MARK_NODE is returned. */
12376 cp_parser_pure_specifier (parser)
12381 /* Look for the `=' token. */
12382 if (!cp_parser_require (parser, CPP_EQ, "`='"))
12383 return error_mark_node;
12384 /* Look for the `0' token. */
12385 token = cp_parser_require (parser, CPP_NUMBER, "`0'");
12386 /* Unfortunately, this will accept `0L' and `0x00' as well. We need
12387 to get information from the lexer about how the number was
12388 spelled in order to fix this problem. */
12389 if (!token || !integer_zerop (token->value))
12390 return error_mark_node;
12392 return integer_zero_node;
12395 /* Parse a constant-initializer.
12397 constant-initializer:
12398 = constant-expression
12400 Returns a representation of the constant-expression. */
12403 cp_parser_constant_initializer (parser)
12406 /* Look for the `=' token. */
12407 if (!cp_parser_require (parser, CPP_EQ, "`='"))
12408 return error_mark_node;
12410 /* It is invalid to write:
12412 struct S { static const int i = { 7 }; };
12415 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12417 cp_parser_error (parser,
12418 "a brace-enclosed initializer is not allowed here");
12419 /* Consume the opening brace. */
12420 cp_lexer_consume_token (parser->lexer);
12421 /* Skip the initializer. */
12422 cp_parser_skip_to_closing_brace (parser);
12423 /* Look for the trailing `}'. */
12424 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
12426 return error_mark_node;
12429 return cp_parser_constant_expression (parser);
12432 /* Derived classes [gram.class.derived] */
12434 /* Parse a base-clause.
12437 : base-specifier-list
12439 base-specifier-list:
12441 base-specifier-list , base-specifier
12443 Returns a TREE_LIST representing the base-classes, in the order in
12444 which they were declared. The representation of each node is as
12445 described by cp_parser_base_specifier.
12447 In the case that no bases are specified, this function will return
12448 NULL_TREE, not ERROR_MARK_NODE. */
12451 cp_parser_base_clause (parser)
12454 tree bases = NULL_TREE;
12456 /* Look for the `:' that begins the list. */
12457 cp_parser_require (parser, CPP_COLON, "`:'");
12459 /* Scan the base-specifier-list. */
12465 /* Look for the base-specifier. */
12466 base = cp_parser_base_specifier (parser);
12467 /* Add BASE to the front of the list. */
12468 if (base != error_mark_node)
12470 TREE_CHAIN (base) = bases;
12473 /* Peek at the next token. */
12474 token = cp_lexer_peek_token (parser->lexer);
12475 /* If it's not a comma, then the list is complete. */
12476 if (token->type != CPP_COMMA)
12478 /* Consume the `,'. */
12479 cp_lexer_consume_token (parser->lexer);
12482 /* PARSER->SCOPE may still be non-NULL at this point, if the last
12483 base class had a qualified name. However, the next name that
12484 appears is certainly not qualified. */
12485 parser->scope = NULL_TREE;
12486 parser->qualifying_scope = NULL_TREE;
12487 parser->object_scope = NULL_TREE;
12489 return nreverse (bases);
12492 /* Parse a base-specifier.
12495 :: [opt] nested-name-specifier [opt] class-name
12496 virtual access-specifier [opt] :: [opt] nested-name-specifier
12498 access-specifier virtual [opt] :: [opt] nested-name-specifier
12501 Returns a TREE_LIST. The TREE_PURPOSE will be one of
12502 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
12503 indicate the specifiers provided. The TREE_VALUE will be a TYPE
12504 (or the ERROR_MARK_NODE) indicating the type that was specified. */
12507 cp_parser_base_specifier (parser)
12512 bool virtual_p = false;
12513 bool duplicate_virtual_error_issued_p = false;
12514 bool duplicate_access_error_issued_p = false;
12515 bool class_scope_p;
12516 access_kind access = ak_none;
12520 /* Process the optional `virtual' and `access-specifier'. */
12523 /* Peek at the next token. */
12524 token = cp_lexer_peek_token (parser->lexer);
12525 /* Process `virtual'. */
12526 switch (token->keyword)
12529 /* If `virtual' appears more than once, issue an error. */
12530 if (virtual_p && !duplicate_virtual_error_issued_p)
12532 cp_parser_error (parser,
12533 "`virtual' specified more than once in base-specified");
12534 duplicate_virtual_error_issued_p = true;
12539 /* Consume the `virtual' token. */
12540 cp_lexer_consume_token (parser->lexer);
12545 case RID_PROTECTED:
12547 /* If more than one access specifier appears, issue an
12549 if (access != ak_none && !duplicate_access_error_issued_p)
12551 cp_parser_error (parser,
12552 "more than one access specifier in base-specified");
12553 duplicate_access_error_issued_p = true;
12556 access = ((access_kind)
12557 tree_low_cst (ridpointers[(int) token->keyword],
12560 /* Consume the access-specifier. */
12561 cp_lexer_consume_token (parser->lexer);
12571 /* Map `virtual_p' and `access' onto one of the access
12577 access_node = access_default_node;
12580 access_node = access_public_node;
12583 access_node = access_protected_node;
12586 access_node = access_private_node;
12595 access_node = access_default_virtual_node;
12598 access_node = access_public_virtual_node;
12601 access_node = access_protected_virtual_node;
12604 access_node = access_private_virtual_node;
12610 /* Look for the optional `::' operator. */
12611 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
12612 /* Look for the nested-name-specifier. The simplest way to
12617 The keyword `typename' is not permitted in a base-specifier or
12618 mem-initializer; in these contexts a qualified name that
12619 depends on a template-parameter is implicitly assumed to be a
12622 is to pretend that we have seen the `typename' keyword at this
12624 cp_parser_nested_name_specifier_opt (parser,
12625 /*typename_keyword_p=*/true,
12626 /*check_dependency_p=*/true,
12628 /* If the base class is given by a qualified name, assume that names
12629 we see are type names or templates, as appropriate. */
12630 class_scope_p = (parser->scope && TYPE_P (parser->scope));
12631 /* Finally, look for the class-name. */
12632 type = cp_parser_class_name (parser,
12636 /*check_access=*/true,
12637 /*check_dependency_p=*/true,
12638 /*class_head_p=*/false);
12640 if (type == error_mark_node)
12641 return error_mark_node;
12643 return finish_base_specifier (access_node, TREE_TYPE (type));
12646 /* Exception handling [gram.exception] */
12648 /* Parse an (optional) exception-specification.
12650 exception-specification:
12651 throw ( type-id-list [opt] )
12653 Returns a TREE_LIST representing the exception-specification. The
12654 TREE_VALUE of each node is a type. */
12657 cp_parser_exception_specification_opt (parser)
12663 /* Peek at the next token. */
12664 token = cp_lexer_peek_token (parser->lexer);
12665 /* If it's not `throw', then there's no exception-specification. */
12666 if (!cp_parser_is_keyword (token, RID_THROW))
12669 /* Consume the `throw'. */
12670 cp_lexer_consume_token (parser->lexer);
12672 /* Look for the `('. */
12673 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
12675 /* Peek at the next token. */
12676 token = cp_lexer_peek_token (parser->lexer);
12677 /* If it's not a `)', then there is a type-id-list. */
12678 if (token->type != CPP_CLOSE_PAREN)
12680 const char *saved_message;
12682 /* Types may not be defined in an exception-specification. */
12683 saved_message = parser->type_definition_forbidden_message;
12684 parser->type_definition_forbidden_message
12685 = "types may not be defined in an exception-specification";
12686 /* Parse the type-id-list. */
12687 type_id_list = cp_parser_type_id_list (parser);
12688 /* Restore the saved message. */
12689 parser->type_definition_forbidden_message = saved_message;
12692 type_id_list = empty_except_spec;
12694 /* Look for the `)'. */
12695 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
12697 return type_id_list;
12700 /* Parse an (optional) type-id-list.
12704 type-id-list , type-id
12706 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
12707 in the order that the types were presented. */
12710 cp_parser_type_id_list (parser)
12713 tree types = NULL_TREE;
12720 /* Get the next type-id. */
12721 type = cp_parser_type_id (parser);
12722 /* Add it to the list. */
12723 types = add_exception_specifier (types, type, /*complain=*/1);
12724 /* Peek at the next token. */
12725 token = cp_lexer_peek_token (parser->lexer);
12726 /* If it is not a `,', we are done. */
12727 if (token->type != CPP_COMMA)
12729 /* Consume the `,'. */
12730 cp_lexer_consume_token (parser->lexer);
12733 return nreverse (types);
12736 /* Parse a try-block.
12739 try compound-statement handler-seq */
12742 cp_parser_try_block (parser)
12747 cp_parser_require_keyword (parser, RID_TRY, "`try'");
12748 try_block = begin_try_block ();
12749 cp_parser_compound_statement (parser);
12750 finish_try_block (try_block);
12751 cp_parser_handler_seq (parser);
12752 finish_handler_sequence (try_block);
12757 /* Parse a function-try-block.
12759 function-try-block:
12760 try ctor-initializer [opt] function-body handler-seq */
12763 cp_parser_function_try_block (parser)
12767 bool ctor_initializer_p;
12769 /* Look for the `try' keyword. */
12770 if (!cp_parser_require_keyword (parser, RID_TRY, "`try'"))
12772 /* Let the rest of the front-end know where we are. */
12773 try_block = begin_function_try_block ();
12774 /* Parse the function-body. */
12776 = cp_parser_ctor_initializer_opt_and_function_body (parser);
12777 /* We're done with the `try' part. */
12778 finish_function_try_block (try_block);
12779 /* Parse the handlers. */
12780 cp_parser_handler_seq (parser);
12781 /* We're done with the handlers. */
12782 finish_function_handler_sequence (try_block);
12784 return ctor_initializer_p;
12787 /* Parse a handler-seq.
12790 handler handler-seq [opt] */
12793 cp_parser_handler_seq (parser)
12800 /* Parse the handler. */
12801 cp_parser_handler (parser);
12802 /* Peek at the next token. */
12803 token = cp_lexer_peek_token (parser->lexer);
12804 /* If it's not `catch' then there are no more handlers. */
12805 if (!cp_parser_is_keyword (token, RID_CATCH))
12810 /* Parse a handler.
12813 catch ( exception-declaration ) compound-statement */
12816 cp_parser_handler (parser)
12822 cp_parser_require_keyword (parser, RID_CATCH, "`catch'");
12823 handler = begin_handler ();
12824 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
12825 declaration = cp_parser_exception_declaration (parser);
12826 finish_handler_parms (declaration, handler);
12827 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
12828 cp_parser_compound_statement (parser);
12829 finish_handler (handler);
12832 /* Parse an exception-declaration.
12834 exception-declaration:
12835 type-specifier-seq declarator
12836 type-specifier-seq abstract-declarator
12840 Returns a VAR_DECL for the declaration, or NULL_TREE if the
12841 ellipsis variant is used. */
12844 cp_parser_exception_declaration (parser)
12847 tree type_specifiers;
12849 const char *saved_message;
12851 /* If it's an ellipsis, it's easy to handle. */
12852 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
12854 /* Consume the `...' token. */
12855 cp_lexer_consume_token (parser->lexer);
12859 /* Types may not be defined in exception-declarations. */
12860 saved_message = parser->type_definition_forbidden_message;
12861 parser->type_definition_forbidden_message
12862 = "types may not be defined in exception-declarations";
12864 /* Parse the type-specifier-seq. */
12865 type_specifiers = cp_parser_type_specifier_seq (parser);
12866 /* If it's a `)', then there is no declarator. */
12867 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
12868 declarator = NULL_TREE;
12871 /* Otherwise, we can't be sure whether we are looking at a
12872 direct, or an abstract, declarator. */
12873 cp_parser_parse_tentatively (parser);
12874 /* Try an ordinary declarator. */
12875 declarator = cp_parser_declarator (parser,
12876 /*abstract_p=*/false,
12877 /*ctor_dtor_or_conv_p=*/NULL);
12878 /* If that didn't work, try an abstract declarator. */
12879 if (!cp_parser_parse_definitely (parser))
12880 declarator = cp_parser_declarator (parser,
12881 /*abstract_p=*/true,
12882 /*ctor_dtor_or_conv_p=*/NULL);
12885 /* Restore the saved message. */
12886 parser->type_definition_forbidden_message = saved_message;
12888 return start_handler_parms (type_specifiers, declarator);
12891 /* Parse a throw-expression.
12894 throw assignment-expresion [opt]
12896 Returns a THROW_EXPR representing the throw-expression. */
12899 cp_parser_throw_expression (parser)
12904 cp_parser_require_keyword (parser, RID_THROW, "`throw'");
12905 /* We can't be sure if there is an assignment-expression or not. */
12906 cp_parser_parse_tentatively (parser);
12908 expression = cp_parser_assignment_expression (parser);
12909 /* If it didn't work, this is just a rethrow. */
12910 if (!cp_parser_parse_definitely (parser))
12911 expression = NULL_TREE;
12913 return build_throw (expression);
12916 /* GNU Extensions */
12918 /* Parse an (optional) asm-specification.
12921 asm ( string-literal )
12923 If the asm-specification is present, returns a STRING_CST
12924 corresponding to the string-literal. Otherwise, returns
12928 cp_parser_asm_specification_opt (parser)
12932 tree asm_specification;
12934 /* Peek at the next token. */
12935 token = cp_lexer_peek_token (parser->lexer);
12936 /* If the next token isn't the `asm' keyword, then there's no
12937 asm-specification. */
12938 if (!cp_parser_is_keyword (token, RID_ASM))
12941 /* Consume the `asm' token. */
12942 cp_lexer_consume_token (parser->lexer);
12943 /* Look for the `('. */
12944 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
12946 /* Look for the string-literal. */
12947 token = cp_parser_require (parser, CPP_STRING, "string-literal");
12949 asm_specification = token->value;
12951 asm_specification = NULL_TREE;
12953 /* Look for the `)'. */
12954 cp_parser_require (parser, CPP_CLOSE_PAREN, "`('");
12956 return asm_specification;
12959 /* Parse an asm-operand-list.
12963 asm-operand-list , asm-operand
12966 string-literal ( expression )
12967 [ string-literal ] string-literal ( expression )
12969 Returns a TREE_LIST representing the operands. The TREE_VALUE of
12970 each node is the expression. The TREE_PURPOSE is itself a
12971 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
12972 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
12973 is a STRING_CST for the string literal before the parenthesis. */
12976 cp_parser_asm_operand_list (parser)
12979 tree asm_operands = NULL_TREE;
12983 tree string_literal;
12988 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
12990 /* Consume the `[' token. */
12991 cp_lexer_consume_token (parser->lexer);
12992 /* Read the operand name. */
12993 name = cp_parser_identifier (parser);
12994 if (name != error_mark_node)
12995 name = build_string (IDENTIFIER_LENGTH (name),
12996 IDENTIFIER_POINTER (name));
12997 /* Look for the closing `]'. */
12998 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
13002 /* Look for the string-literal. */
13003 token = cp_parser_require (parser, CPP_STRING, "string-literal");
13004 string_literal = token ? token->value : error_mark_node;
13005 /* Look for the `('. */
13006 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13007 /* Parse the expression. */
13008 expression = cp_parser_expression (parser);
13009 /* Look for the `)'. */
13010 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13011 /* Add this operand to the list. */
13012 asm_operands = tree_cons (build_tree_list (name, string_literal),
13015 /* If the next token is not a `,', there are no more
13017 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13019 /* Consume the `,'. */
13020 cp_lexer_consume_token (parser->lexer);
13023 return nreverse (asm_operands);
13026 /* Parse an asm-clobber-list.
13030 asm-clobber-list , string-literal
13032 Returns a TREE_LIST, indicating the clobbers in the order that they
13033 appeared. The TREE_VALUE of each node is a STRING_CST. */
13036 cp_parser_asm_clobber_list (parser)
13039 tree clobbers = NULL_TREE;
13044 tree string_literal;
13046 /* Look for the string literal. */
13047 token = cp_parser_require (parser, CPP_STRING, "string-literal");
13048 string_literal = token ? token->value : error_mark_node;
13049 /* Add it to the list. */
13050 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
13051 /* If the next token is not a `,', then the list is
13053 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13055 /* Consume the `,' token. */
13056 cp_lexer_consume_token (parser->lexer);
13062 /* Parse an (optional) series of attributes.
13065 attributes attribute
13068 __attribute__ (( attribute-list [opt] ))
13070 The return value is as for cp_parser_attribute_list. */
13073 cp_parser_attributes_opt (parser)
13076 tree attributes = NULL_TREE;
13081 tree attribute_list;
13083 /* Peek at the next token. */
13084 token = cp_lexer_peek_token (parser->lexer);
13085 /* If it's not `__attribute__', then we're done. */
13086 if (token->keyword != RID_ATTRIBUTE)
13089 /* Consume the `__attribute__' keyword. */
13090 cp_lexer_consume_token (parser->lexer);
13091 /* Look for the two `(' tokens. */
13092 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13093 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13095 /* Peek at the next token. */
13096 token = cp_lexer_peek_token (parser->lexer);
13097 if (token->type != CPP_CLOSE_PAREN)
13098 /* Parse the attribute-list. */
13099 attribute_list = cp_parser_attribute_list (parser);
13101 /* If the next token is a `)', then there is no attribute
13103 attribute_list = NULL;
13105 /* Look for the two `)' tokens. */
13106 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13107 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13109 /* Add these new attributes to the list. */
13110 attributes = chainon (attributes, attribute_list);
13116 /* Parse an attribute-list.
13120 attribute-list , attribute
13124 identifier ( identifier )
13125 identifier ( identifier , expression-list )
13126 identifier ( expression-list )
13128 Returns a TREE_LIST. Each node corresponds to an attribute. THe
13129 TREE_PURPOSE of each node is the identifier indicating which
13130 attribute is in use. The TREE_VALUE represents the arguments, if
13134 cp_parser_attribute_list (parser)
13137 tree attribute_list = NULL_TREE;
13145 /* Look for the identifier. We also allow keywords here; for
13146 example `__attribute__ ((const))' is legal. */
13147 token = cp_lexer_peek_token (parser->lexer);
13148 if (token->type != CPP_NAME
13149 && token->type != CPP_KEYWORD)
13150 return error_mark_node;
13151 /* Consume the token. */
13152 token = cp_lexer_consume_token (parser->lexer);
13154 /* Save away the identifier that indicates which attribute this is. */
13155 identifier = token->value;
13156 attribute = build_tree_list (identifier, NULL_TREE);
13158 /* Peek at the next token. */
13159 token = cp_lexer_peek_token (parser->lexer);
13160 /* If it's an `(', then parse the attribute arguments. */
13161 if (token->type == CPP_OPEN_PAREN)
13164 int arguments_allowed_p = 1;
13166 /* Consume the `('. */
13167 cp_lexer_consume_token (parser->lexer);
13168 /* Peek at the next token. */
13169 token = cp_lexer_peek_token (parser->lexer);
13170 /* Check to see if the next token is an identifier. */
13171 if (token->type == CPP_NAME)
13173 /* Save the identifier. */
13174 identifier = token->value;
13175 /* Consume the identifier. */
13176 cp_lexer_consume_token (parser->lexer);
13177 /* Peek at the next token. */
13178 token = cp_lexer_peek_token (parser->lexer);
13179 /* If the next token is a `,', then there are some other
13180 expressions as well. */
13181 if (token->type == CPP_COMMA)
13182 /* Consume the comma. */
13183 cp_lexer_consume_token (parser->lexer);
13185 arguments_allowed_p = 0;
13188 identifier = NULL_TREE;
13190 /* If there are arguments, parse them too. */
13191 if (arguments_allowed_p)
13192 arguments = cp_parser_expression_list (parser);
13194 arguments = NULL_TREE;
13196 /* Combine the identifier and the arguments. */
13198 arguments = tree_cons (NULL_TREE, identifier, arguments);
13200 /* Save the identifier and arguments away. */
13201 TREE_VALUE (attribute) = arguments;
13203 /* Look for the closing `)'. */
13204 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13207 /* Add this attribute to the list. */
13208 TREE_CHAIN (attribute) = attribute_list;
13209 attribute_list = attribute;
13211 /* Now, look for more attributes. */
13212 token = cp_lexer_peek_token (parser->lexer);
13213 /* If the next token isn't a `,', we're done. */
13214 if (token->type != CPP_COMMA)
13217 /* Consume the commma and keep going. */
13218 cp_lexer_consume_token (parser->lexer);
13221 /* We built up the list in reverse order. */
13222 return nreverse (attribute_list);
13225 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
13226 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
13227 current value of the PEDANTIC flag, regardless of whether or not
13228 the `__extension__' keyword is present. The caller is responsible
13229 for restoring the value of the PEDANTIC flag. */
13232 cp_parser_extension_opt (parser, saved_pedantic)
13234 int *saved_pedantic;
13236 /* Save the old value of the PEDANTIC flag. */
13237 *saved_pedantic = pedantic;
13239 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
13241 /* Consume the `__extension__' token. */
13242 cp_lexer_consume_token (parser->lexer);
13243 /* We're not being pedantic while the `__extension__' keyword is
13253 /* Parse a label declaration.
13256 __label__ label-declarator-seq ;
13258 label-declarator-seq:
13259 identifier , label-declarator-seq
13263 cp_parser_label_declaration (parser)
13266 /* Look for the `__label__' keyword. */
13267 cp_parser_require_keyword (parser, RID_LABEL, "`__label__'");
13273 /* Look for an identifier. */
13274 identifier = cp_parser_identifier (parser);
13275 /* Declare it as a lobel. */
13276 finish_label_decl (identifier);
13277 /* If the next token is a `;', stop. */
13278 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13280 /* Look for the `,' separating the label declarations. */
13281 cp_parser_require (parser, CPP_COMMA, "`,'");
13284 /* Look for the final `;'. */
13285 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
13288 /* Support Functions */
13290 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
13291 NAME should have one of the representations used for an
13292 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
13293 is returned. If PARSER->SCOPE is a dependent type, then a
13294 SCOPE_REF is returned.
13296 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
13297 returned; the name was already resolved when the TEMPLATE_ID_EXPR
13298 was formed. Abstractly, such entities should not be passed to this
13299 function, because they do not need to be looked up, but it is
13300 simpler to check for this special case here, rather than at the
13303 In cases not explicitly covered above, this function returns a
13304 DECL, OVERLOAD, or baselink representing the result of the lookup.
13305 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
13308 If CHECK_ACCESS is TRUE, then access control is performed on the
13309 declaration to which the name resolves, and an error message is
13310 issued if the declaration is inaccessible.
13312 If IS_TYPE is TRUE, bindings that do not refer to types are
13315 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
13318 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
13322 cp_parser_lookup_name (cp_parser *parser, tree name, bool check_access,
13323 bool is_type, bool is_namespace, bool check_dependency)
13326 tree object_type = parser->context->object_type;
13328 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
13329 no longer valid. Note that if we are parsing tentatively, and
13330 the parse fails, OBJECT_TYPE will be automatically restored. */
13331 parser->context->object_type = NULL_TREE;
13333 if (name == error_mark_node)
13334 return error_mark_node;
13336 /* A template-id has already been resolved; there is no lookup to
13338 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
13340 if (BASELINK_P (name))
13342 my_friendly_assert ((TREE_CODE (BASELINK_FUNCTIONS (name))
13343 == TEMPLATE_ID_EXPR),
13348 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
13349 it should already have been checked to make sure that the name
13350 used matches the type being destroyed. */
13351 if (TREE_CODE (name) == BIT_NOT_EXPR)
13355 /* Figure out to which type this destructor applies. */
13357 type = parser->scope;
13358 else if (object_type)
13359 type = object_type;
13361 type = current_class_type;
13362 /* If that's not a class type, there is no destructor. */
13363 if (!type || !CLASS_TYPE_P (type))
13364 return error_mark_node;
13365 /* If it was a class type, return the destructor. */
13366 return CLASSTYPE_DESTRUCTORS (type);
13369 /* By this point, the NAME should be an ordinary identifier. If
13370 the id-expression was a qualified name, the qualifying scope is
13371 stored in PARSER->SCOPE at this point. */
13372 my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE,
13375 /* Perform the lookup. */
13378 bool dependent_type_p;
13380 if (parser->scope == error_mark_node)
13381 return error_mark_node;
13383 /* If the SCOPE is dependent, the lookup must be deferred until
13384 the template is instantiated -- unless we are explicitly
13385 looking up names in uninstantiated templates. Even then, we
13386 cannot look up the name if the scope is not a class type; it
13387 might, for example, be a template type parameter. */
13388 dependent_type_p = (TYPE_P (parser->scope)
13389 && !(parser->in_declarator_p
13390 && currently_open_class (parser->scope))
13391 && cp_parser_dependent_type_p (parser->scope));
13392 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
13393 && dependent_type_p)
13396 decl = build_nt (SCOPE_REF, parser->scope, name);
13398 /* The resolution to Core Issue 180 says that `struct A::B'
13399 should be considered a type-name, even if `A' is
13401 decl = TYPE_NAME (make_typename_type (parser->scope,
13407 /* If PARSER->SCOPE is a dependent type, then it must be a
13408 class type, and we must not be checking dependencies;
13409 otherwise, we would have processed this lookup above. So
13410 that PARSER->SCOPE is not considered a dependent base by
13411 lookup_member, we must enter the scope here. */
13412 if (dependent_type_p)
13413 push_scope (parser->scope);
13414 /* If the PARSER->SCOPE is a a template specialization, it
13415 may be instantiated during name lookup. In that case,
13416 errors may be issued. Even if we rollback the current
13417 tentative parse, those errors are valid. */
13418 decl = lookup_qualified_name (parser->scope, name, is_type,
13420 if (dependent_type_p)
13421 pop_scope (parser->scope);
13423 parser->qualifying_scope = parser->scope;
13424 parser->object_scope = NULL_TREE;
13426 else if (object_type)
13428 tree object_decl = NULL_TREE;
13429 /* Look up the name in the scope of the OBJECT_TYPE, unless the
13430 OBJECT_TYPE is not a class. */
13431 if (CLASS_TYPE_P (object_type))
13432 /* If the OBJECT_TYPE is a template specialization, it may
13433 be instantiated during name lookup. In that case, errors
13434 may be issued. Even if we rollback the current tentative
13435 parse, those errors are valid. */
13436 object_decl = lookup_member (object_type,
13438 /*protect=*/0, is_type);
13439 /* Look it up in the enclosing context, too. */
13440 decl = lookup_name_real (name, is_type, /*nonclass=*/0,
13443 parser->object_scope = object_type;
13444 parser->qualifying_scope = NULL_TREE;
13446 decl = object_decl;
13450 decl = lookup_name_real (name, is_type, /*nonclass=*/0,
13453 parser->qualifying_scope = NULL_TREE;
13454 parser->object_scope = NULL_TREE;
13457 /* If the lookup failed, let our caller know. */
13459 || decl == error_mark_node
13460 || (TREE_CODE (decl) == FUNCTION_DECL
13461 && DECL_ANTICIPATED (decl)))
13462 return error_mark_node;
13464 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
13465 if (TREE_CODE (decl) == TREE_LIST)
13467 /* The error message we have to print is too complicated for
13468 cp_parser_error, so we incorporate its actions directly. */
13469 if (!cp_parser_simulate_error (parser))
13471 error ("reference to `%D' is ambiguous", name);
13472 print_candidates (decl);
13474 return error_mark_node;
13477 my_friendly_assert (DECL_P (decl)
13478 || TREE_CODE (decl) == OVERLOAD
13479 || TREE_CODE (decl) == SCOPE_REF
13480 || BASELINK_P (decl),
13483 /* If we have resolved the name of a member declaration, check to
13484 see if the declaration is accessible. When the name resolves to
13485 set of overloaded functions, accesibility is checked when
13486 overload resolution is done.
13488 During an explicit instantiation, access is not checked at all,
13489 as per [temp.explicit]. */
13490 if (check_access && scope_chain->check_access && DECL_P (decl))
13492 tree qualifying_type;
13494 /* Figure out the type through which DECL is being
13497 = cp_parser_scope_through_which_access_occurs (decl,
13500 if (qualifying_type)
13502 /* If we are supposed to defer access checks, just record
13503 the information for later. */
13504 if (parser->context->deferring_access_checks_p)
13505 cp_parser_defer_access_check (parser, qualifying_type, decl);
13506 /* Otherwise, check accessibility now. */
13508 enforce_access (qualifying_type, decl);
13515 /* Like cp_parser_lookup_name, but for use in the typical case where
13516 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, and CHECK_DEPENDENCY is
13520 cp_parser_lookup_name_simple (parser, name)
13524 return cp_parser_lookup_name (parser, name,
13525 /*check_access=*/true,
13527 /*is_namespace=*/false,
13528 /*check_dependency=*/true);
13531 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
13532 TYPENAME_TYPE corresponds. Note that this function peers inside
13533 uninstantiated templates and therefore should be used only in
13534 extremely limited situations. */
13537 cp_parser_resolve_typename_type (parser, type)
13545 my_friendly_assert (TREE_CODE (type) == TYPENAME_TYPE,
13548 scope = TYPE_CONTEXT (type);
13549 name = DECL_NAME (TYPE_NAME (type));
13551 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
13552 it first before we can figure out what NAME refers to. */
13553 if (TREE_CODE (scope) == TYPENAME_TYPE)
13554 scope = cp_parser_resolve_typename_type (parser, scope);
13555 /* If we don't know what SCOPE refers to, then we cannot resolve the
13557 if (scope == error_mark_node)
13558 return error_mark_node;
13559 /* If the SCOPE is a template type parameter, we have no way of
13560 resolving the name. */
13561 if (TREE_CODE (scope) == TEMPLATE_TYPE_PARM)
13563 /* Enter the SCOPE so that name lookup will be resolved as if we
13564 were in the class definition. In particular, SCOPE will no
13565 longer be considered a dependent type. */
13566 push_scope (scope);
13567 /* Look up the declaration. */
13568 decl = lookup_member (scope, name, /*protect=*/0, /*want_type=*/1);
13569 /* If all went well, we got a TYPE_DECL for a non-typename. */
13571 || TREE_CODE (decl) != TYPE_DECL
13572 || TREE_CODE (TREE_TYPE (decl)) == TYPENAME_TYPE)
13574 cp_parser_error (parser, "could not resolve typename type");
13575 type = error_mark_node;
13578 type = TREE_TYPE (decl);
13579 /* Leave the SCOPE. */
13585 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
13586 the current context, return the TYPE_DECL. If TAG_NAME_P is
13587 true, the DECL indicates the class being defined in a class-head,
13588 or declared in an elaborated-type-specifier.
13590 Otherwise, return DECL. */
13593 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
13595 /* If the DECL is a TEMPLATE_DECL for a class type, and we are in
13596 the scope of the class, then treat the TEMPLATE_DECL as a
13597 class-name. For example, in:
13599 template <class T> struct S {
13605 If the TEMPLATE_DECL is being declared as part of a class-head,
13606 the same translation occurs:
13609 template <typename T> struct B;
13612 template <typename T> struct A::B {};
13614 Similarly, in a elaborated-type-specifier:
13616 namespace N { struct X{}; }
13619 template <typename T> friend struct N::X;
13623 if (DECL_CLASS_TEMPLATE_P (decl)
13625 || (current_class_type
13626 && same_type_p (TREE_TYPE (DECL_TEMPLATE_RESULT (decl)),
13627 current_class_type))))
13628 return DECL_TEMPLATE_RESULT (decl);
13633 /* If too many, or too few, template-parameter lists apply to the
13634 declarator, issue an error message. Returns TRUE if all went well,
13635 and FALSE otherwise. */
13638 cp_parser_check_declarator_template_parameters (parser, declarator)
13642 unsigned num_templates;
13644 /* We haven't seen any classes that involve template parameters yet. */
13647 switch (TREE_CODE (declarator))
13654 tree main_declarator = TREE_OPERAND (declarator, 0);
13656 cp_parser_check_declarator_template_parameters (parser,
13665 scope = TREE_OPERAND (declarator, 0);
13666 member = TREE_OPERAND (declarator, 1);
13668 /* If this is a pointer-to-member, then we are not interested
13669 in the SCOPE, because it does not qualify the thing that is
13671 if (TREE_CODE (member) == INDIRECT_REF)
13672 return (cp_parser_check_declarator_template_parameters
13675 while (scope && CLASS_TYPE_P (scope))
13677 /* You're supposed to have one `template <...>'
13678 for every template class, but you don't need one
13679 for a full specialization. For example:
13681 template <class T> struct S{};
13682 template <> struct S<int> { void f(); };
13683 void S<int>::f () {}
13685 is correct; there shouldn't be a `template <>' for
13686 the definition of `S<int>::f'. */
13687 if (CLASSTYPE_TEMPLATE_INFO (scope)
13688 && (CLASSTYPE_TEMPLATE_INSTANTIATION (scope)
13689 || uses_template_parms (CLASSTYPE_TI_ARGS (scope)))
13690 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
13693 scope = TYPE_CONTEXT (scope);
13697 /* Fall through. */
13700 /* If the DECLARATOR has the form `X<y>' then it uses one
13701 additional level of template parameters. */
13702 if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
13705 return cp_parser_check_template_parameters (parser,
13710 /* NUM_TEMPLATES were used in the current declaration. If that is
13711 invalid, return FALSE and issue an error messages. Otherwise,
13715 cp_parser_check_template_parameters (parser, num_templates)
13717 unsigned num_templates;
13719 /* If there are more template classes than parameter lists, we have
13722 template <class T> void S<T>::R<T>::f (); */
13723 if (parser->num_template_parameter_lists < num_templates)
13725 error ("too few template-parameter-lists");
13728 /* If there are the same number of template classes and parameter
13729 lists, that's OK. */
13730 if (parser->num_template_parameter_lists == num_templates)
13732 /* If there are more, but only one more, then we are referring to a
13733 member template. That's OK too. */
13734 if (parser->num_template_parameter_lists == num_templates + 1)
13736 /* Otherwise, there are too many template parameter lists. We have
13739 template <class T> template <class U> void S::f(); */
13740 error ("too many template-parameter-lists");
13744 /* Parse a binary-expression of the general form:
13748 binary-expression <token> <expr>
13750 The TOKEN_TREE_MAP maps <token> types to <expr> codes. FN is used
13751 to parser the <expr>s. If the first production is used, then the
13752 value returned by FN is returned directly. Otherwise, a node with
13753 the indicated EXPR_TYPE is returned, with operands corresponding to
13754 the two sub-expressions. */
13757 cp_parser_binary_expression (parser, token_tree_map, fn)
13759 cp_parser_token_tree_map token_tree_map;
13760 cp_parser_expression_fn fn;
13764 /* Parse the first expression. */
13765 lhs = (*fn) (parser);
13766 /* Now, look for more expressions. */
13770 cp_parser_token_tree_map_node *map_node;
13773 /* Peek at the next token. */
13774 token = cp_lexer_peek_token (parser->lexer);
13775 /* If the token is `>', and that's not an operator at the
13776 moment, then we're done. */
13777 if (token->type == CPP_GREATER
13778 && !parser->greater_than_is_operator_p)
13780 /* If we find one of the tokens we want, build the correspoding
13781 tree representation. */
13782 for (map_node = token_tree_map;
13783 map_node->token_type != CPP_EOF;
13785 if (map_node->token_type == token->type)
13787 /* Consume the operator token. */
13788 cp_lexer_consume_token (parser->lexer);
13789 /* Parse the right-hand side of the expression. */
13790 rhs = (*fn) (parser);
13791 /* Build the binary tree node. */
13792 lhs = build_x_binary_op (map_node->tree_type, lhs, rhs);
13796 /* If the token wasn't one of the ones we want, we're done. */
13797 if (map_node->token_type == CPP_EOF)
13804 /* Parse an optional `::' token indicating that the following name is
13805 from the global namespace. If so, PARSER->SCOPE is set to the
13806 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
13807 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
13808 Returns the new value of PARSER->SCOPE, if the `::' token is
13809 present, and NULL_TREE otherwise. */
13812 cp_parser_global_scope_opt (parser, current_scope_valid_p)
13814 bool current_scope_valid_p;
13818 /* Peek at the next token. */
13819 token = cp_lexer_peek_token (parser->lexer);
13820 /* If we're looking at a `::' token then we're starting from the
13821 global namespace, not our current location. */
13822 if (token->type == CPP_SCOPE)
13824 /* Consume the `::' token. */
13825 cp_lexer_consume_token (parser->lexer);
13826 /* Set the SCOPE so that we know where to start the lookup. */
13827 parser->scope = global_namespace;
13828 parser->qualifying_scope = global_namespace;
13829 parser->object_scope = NULL_TREE;
13831 return parser->scope;
13833 else if (!current_scope_valid_p)
13835 parser->scope = NULL_TREE;
13836 parser->qualifying_scope = NULL_TREE;
13837 parser->object_scope = NULL_TREE;
13843 /* Returns TRUE if the upcoming token sequence is the start of a
13844 constructor declarator. If FRIEND_P is true, the declarator is
13845 preceded by the `friend' specifier. */
13848 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
13850 bool constructor_p;
13851 tree type_decl = NULL_TREE;
13852 bool nested_name_p;
13854 /* Parse tentatively; we are going to roll back all of the tokens
13856 cp_parser_parse_tentatively (parser);
13857 /* Assume that we are looking at a constructor declarator. */
13858 constructor_p = true;
13859 /* Look for the optional `::' operator. */
13860 cp_parser_global_scope_opt (parser,
13861 /*current_scope_valid_p=*/false);
13862 /* Look for the nested-name-specifier. */
13864 = (cp_parser_nested_name_specifier_opt (parser,
13865 /*typename_keyword_p=*/false,
13866 /*check_dependency_p=*/false,
13869 /* Outside of a class-specifier, there must be a
13870 nested-name-specifier. */
13871 if (!nested_name_p &&
13872 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
13874 constructor_p = false;
13875 /* If we still think that this might be a constructor-declarator,
13876 look for a class-name. */
13881 template <typename T> struct S { S(); }
13882 template <typename T> S<T>::S ();
13884 we must recognize that the nested `S' names a class.
13887 template <typename T> S<T>::S<T> ();
13889 we must recognize that the nested `S' names a template. */
13890 type_decl = cp_parser_class_name (parser,
13891 /*typename_keyword_p=*/false,
13892 /*template_keyword_p=*/false,
13894 /*check_access_p=*/false,
13895 /*check_dependency_p=*/false,
13896 /*class_head_p=*/false);
13897 /* If there was no class-name, then this is not a constructor. */
13898 constructor_p = !cp_parser_error_occurred (parser);
13900 /* If we're still considering a constructor, we have to see a `(',
13901 to begin the parameter-declaration-clause, followed by either a
13902 `)', an `...', or a decl-specifier. We need to check for a
13903 type-specifier to avoid being fooled into thinking that:
13907 is a constructor. (It is actually a function named `f' that
13908 takes one parameter (of type `int') and returns a value of type
13911 && cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
13913 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
13914 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
13915 && !cp_parser_storage_class_specifier_opt (parser))
13917 if (current_class_type
13918 && !same_type_p (current_class_type, TREE_TYPE (type_decl)))
13919 /* The constructor for one class cannot be declared inside
13921 constructor_p = false;
13926 /* Names appearing in the type-specifier should be looked up
13927 in the scope of the class. */
13928 if (current_class_type)
13932 type = TREE_TYPE (type_decl);
13933 if (TREE_CODE (type) == TYPENAME_TYPE)
13934 type = cp_parser_resolve_typename_type (parser, type);
13937 /* Look for the type-specifier. */
13938 cp_parser_type_specifier (parser,
13939 CP_PARSER_FLAGS_NONE,
13940 /*is_friend=*/false,
13941 /*is_declarator=*/true,
13942 /*declares_class_or_enum=*/NULL,
13943 /*is_cv_qualifier=*/NULL);
13944 /* Leave the scope of the class. */
13948 constructor_p = !cp_parser_error_occurred (parser);
13953 constructor_p = false;
13954 /* We did not really want to consume any tokens. */
13955 cp_parser_abort_tentative_parse (parser);
13957 return constructor_p;
13960 /* Parse the definition of the function given by the DECL_SPECIFIERS,
13961 ATTRIBUTES, and DECLARATOR. The ACCESS_CHECKS have been deferred;
13962 they must be performed once we are in the scope of the function.
13964 Returns the function defined. */
13967 cp_parser_function_definition_from_specifiers_and_declarator
13968 (parser, decl_specifiers, attributes, declarator, access_checks)
13970 tree decl_specifiers;
13973 tree access_checks;
13978 /* Begin the function-definition. */
13979 success_p = begin_function_definition (decl_specifiers,
13983 /* If there were names looked up in the decl-specifier-seq that we
13984 did not check, check them now. We must wait until we are in the
13985 scope of the function to perform the checks, since the function
13986 might be a friend. */
13987 cp_parser_perform_deferred_access_checks (access_checks);
13991 /* If begin_function_definition didn't like the definition, skip
13992 the entire function. */
13993 error ("invalid function declaration");
13994 cp_parser_skip_to_end_of_block_or_statement (parser);
13995 fn = error_mark_node;
13998 fn = cp_parser_function_definition_after_declarator (parser,
13999 /*inline_p=*/false);
14004 /* Parse the part of a function-definition that follows the
14005 declarator. INLINE_P is TRUE iff this function is an inline
14006 function defined with a class-specifier.
14008 Returns the function defined. */
14011 cp_parser_function_definition_after_declarator (parser,
14017 bool ctor_initializer_p = false;
14018 bool saved_in_unbraced_linkage_specification_p;
14019 unsigned saved_num_template_parameter_lists;
14021 /* If the next token is `return', then the code may be trying to
14022 make use of the "named return value" extension that G++ used to
14024 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
14026 /* Consume the `return' keyword. */
14027 cp_lexer_consume_token (parser->lexer);
14028 /* Look for the identifier that indicates what value is to be
14030 cp_parser_identifier (parser);
14031 /* Issue an error message. */
14032 error ("named return values are no longer supported");
14033 /* Skip tokens until we reach the start of the function body. */
14034 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
14035 cp_lexer_consume_token (parser->lexer);
14037 /* The `extern' in `extern "C" void f () { ... }' does not apply to
14038 anything declared inside `f'. */
14039 saved_in_unbraced_linkage_specification_p
14040 = parser->in_unbraced_linkage_specification_p;
14041 parser->in_unbraced_linkage_specification_p = false;
14042 /* Inside the function, surrounding template-parameter-lists do not
14044 saved_num_template_parameter_lists
14045 = parser->num_template_parameter_lists;
14046 parser->num_template_parameter_lists = 0;
14047 /* If the next token is `try', then we are looking at a
14048 function-try-block. */
14049 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
14050 ctor_initializer_p = cp_parser_function_try_block (parser);
14051 /* A function-try-block includes the function-body, so we only do
14052 this next part if we're not processing a function-try-block. */
14055 = cp_parser_ctor_initializer_opt_and_function_body (parser);
14057 /* Finish the function. */
14058 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
14059 (inline_p ? 2 : 0));
14060 /* Generate code for it, if necessary. */
14062 /* Restore the saved values. */
14063 parser->in_unbraced_linkage_specification_p
14064 = saved_in_unbraced_linkage_specification_p;
14065 parser->num_template_parameter_lists
14066 = saved_num_template_parameter_lists;
14071 /* Parse a template-declaration, assuming that the `export' (and
14072 `extern') keywords, if present, has already been scanned. MEMBER_P
14073 is as for cp_parser_template_declaration. */
14076 cp_parser_template_declaration_after_export (parser, member_p)
14080 tree decl = NULL_TREE;
14081 tree parameter_list;
14082 bool friend_p = false;
14084 /* Look for the `template' keyword. */
14085 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'"))
14089 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
14092 /* Parse the template parameters. */
14093 begin_template_parm_list ();
14094 /* If the next token is `>', then we have an invalid
14095 specialization. Rather than complain about an invalid template
14096 parameter, issue an error message here. */
14097 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
14099 cp_parser_error (parser, "invalid explicit specialization");
14100 parameter_list = NULL_TREE;
14103 parameter_list = cp_parser_template_parameter_list (parser);
14104 parameter_list = end_template_parm_list (parameter_list);
14105 /* Look for the `>'. */
14106 cp_parser_skip_until_found (parser, CPP_GREATER, "`>'");
14107 /* We just processed one more parameter list. */
14108 ++parser->num_template_parameter_lists;
14109 /* If the next token is `template', there are more template
14111 if (cp_lexer_next_token_is_keyword (parser->lexer,
14113 cp_parser_template_declaration_after_export (parser, member_p);
14116 decl = cp_parser_single_declaration (parser,
14120 /* If this is a member template declaration, let the front
14122 if (member_p && !friend_p && decl)
14123 decl = finish_member_template_decl (decl);
14124 else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
14125 make_friend_class (current_class_type, TREE_TYPE (decl));
14127 /* We are done with the current parameter list. */
14128 --parser->num_template_parameter_lists;
14131 finish_template_decl (parameter_list);
14133 /* Register member declarations. */
14134 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
14135 finish_member_declaration (decl);
14137 /* If DECL is a function template, we must return to parse it later.
14138 (Even though there is no definition, there might be default
14139 arguments that need handling.) */
14140 if (member_p && decl
14141 && (TREE_CODE (decl) == FUNCTION_DECL
14142 || DECL_FUNCTION_TEMPLATE_P (decl)))
14143 TREE_VALUE (parser->unparsed_functions_queues)
14144 = tree_cons (current_class_type, decl,
14145 TREE_VALUE (parser->unparsed_functions_queues));
14148 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
14149 `function-definition' sequence. MEMBER_P is true, this declaration
14150 appears in a class scope.
14152 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
14153 *FRIEND_P is set to TRUE iff the declaration is a friend. */
14156 cp_parser_single_declaration (parser,
14163 bool declares_class_or_enum;
14164 tree decl = NULL_TREE;
14165 tree decl_specifiers;
14167 tree access_checks;
14169 /* Parse the dependent declaration. We don't know yet
14170 whether it will be a function-definition. */
14171 cp_parser_parse_tentatively (parser);
14172 /* Defer access checks until we know what is being declared. */
14173 cp_parser_start_deferring_access_checks (parser);
14174 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
14177 = cp_parser_decl_specifier_seq (parser,
14178 CP_PARSER_FLAGS_OPTIONAL,
14180 &declares_class_or_enum);
14181 /* Gather up the access checks that occurred the
14182 decl-specifier-seq. */
14183 access_checks = cp_parser_stop_deferring_access_checks (parser);
14184 /* Check for the declaration of a template class. */
14185 if (declares_class_or_enum)
14187 if (cp_parser_declares_only_class_p (parser))
14189 decl = shadow_tag (decl_specifiers);
14191 decl = TYPE_NAME (decl);
14193 decl = error_mark_node;
14198 /* If it's not a template class, try for a template function. If
14199 the next token is a `;', then this declaration does not declare
14200 anything. But, if there were errors in the decl-specifiers, then
14201 the error might well have come from an attempted class-specifier.
14202 In that case, there's no need to warn about a missing declarator. */
14204 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
14205 || !value_member (error_mark_node, decl_specifiers)))
14206 decl = cp_parser_init_declarator (parser,
14210 /*function_definition_allowed_p=*/false,
14212 /*function_definition_p=*/NULL);
14213 /* Clear any current qualification; whatever comes next is the start
14214 of something new. */
14215 parser->scope = NULL_TREE;
14216 parser->qualifying_scope = NULL_TREE;
14217 parser->object_scope = NULL_TREE;
14218 /* Look for a trailing `;' after the declaration. */
14219 if (!cp_parser_require (parser, CPP_SEMICOLON, "expected `;'")
14220 && cp_parser_committed_to_tentative_parse (parser))
14221 cp_parser_skip_to_end_of_block_or_statement (parser);
14222 /* If it worked, set *FRIEND_P based on the DECL_SPECIFIERS. */
14223 if (cp_parser_parse_definitely (parser))
14226 *friend_p = cp_parser_friend_p (decl_specifiers);
14228 /* Otherwise, try a function-definition. */
14230 decl = cp_parser_function_definition (parser, friend_p);
14235 /* Parse a functional cast to TYPE. Returns an expression
14236 representing the cast. */
14239 cp_parser_functional_cast (parser, type)
14243 tree expression_list;
14245 /* Look for the opening `('. */
14246 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
14247 return error_mark_node;
14248 /* If the next token is not an `)', there are arguments to the
14250 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
14251 expression_list = cp_parser_expression_list (parser);
14253 expression_list = NULL_TREE;
14254 /* Look for the closing `)'. */
14255 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14257 return build_functional_cast (type, expression_list);
14260 /* MEMBER_FUNCTION is a member function, or a friend. If default
14261 arguments, or the body of the function have not yet been parsed,
14265 cp_parser_late_parsing_for_member (parser, member_function)
14267 tree member_function;
14269 cp_lexer *saved_lexer;
14271 /* If this member is a template, get the underlying
14273 if (DECL_FUNCTION_TEMPLATE_P (member_function))
14274 member_function = DECL_TEMPLATE_RESULT (member_function);
14276 /* There should not be any class definitions in progress at this
14277 point; the bodies of members are only parsed outside of all class
14279 my_friendly_assert (parser->num_classes_being_defined == 0, 20010816);
14280 /* While we're parsing the member functions we might encounter more
14281 classes. We want to handle them right away, but we don't want
14282 them getting mixed up with functions that are currently in the
14284 parser->unparsed_functions_queues
14285 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
14287 /* Make sure that any template parameters are in scope. */
14288 maybe_begin_member_template_processing (member_function);
14290 /* If there are default arguments that have not yet been processed,
14291 take care of them now. */
14292 cp_parser_late_parsing_default_args (parser, TREE_TYPE (member_function),
14293 DECL_FUNCTION_MEMBER_P (member_function)
14294 ? DECL_CONTEXT (member_function)
14297 /* If the body of the function has not yet been parsed, parse it
14299 if (DECL_PENDING_INLINE_P (member_function))
14301 tree function_scope;
14302 cp_token_cache *tokens;
14304 /* The function is no longer pending; we are processing it. */
14305 tokens = DECL_PENDING_INLINE_INFO (member_function);
14306 DECL_PENDING_INLINE_INFO (member_function) = NULL;
14307 DECL_PENDING_INLINE_P (member_function) = 0;
14308 /* If this was an inline function in a local class, enter the scope
14309 of the containing function. */
14310 function_scope = decl_function_context (member_function);
14311 if (function_scope)
14312 push_function_context_to (function_scope);
14314 /* Save away the current lexer. */
14315 saved_lexer = parser->lexer;
14316 /* Make a new lexer to feed us the tokens saved for this function. */
14317 parser->lexer = cp_lexer_new_from_tokens (tokens);
14318 parser->lexer->next = saved_lexer;
14320 /* Set the current source position to be the location of the first
14321 token in the saved inline body. */
14322 cp_lexer_set_source_position_from_token
14324 cp_lexer_peek_token (parser->lexer));
14326 /* Let the front end know that we going to be defining this
14328 start_function (NULL_TREE, member_function, NULL_TREE,
14329 SF_PRE_PARSED | SF_INCLASS_INLINE);
14331 /* Now, parse the body of the function. */
14332 cp_parser_function_definition_after_declarator (parser,
14333 /*inline_p=*/true);
14335 /* Leave the scope of the containing function. */
14336 if (function_scope)
14337 pop_function_context_from (function_scope);
14338 /* Restore the lexer. */
14339 parser->lexer = saved_lexer;
14342 /* Remove any template parameters from the symbol table. */
14343 maybe_end_member_template_processing ();
14345 /* Restore the queue. */
14346 parser->unparsed_functions_queues
14347 = TREE_CHAIN (parser->unparsed_functions_queues);
14350 /* TYPE is a FUNCTION_TYPE or METHOD_TYPE which contains a parameter
14351 with an unparsed DEFAULT_ARG. If non-NULL, SCOPE is the class in
14352 whose context name lookups in the default argument should occur.
14353 Parse the default args now. */
14356 cp_parser_late_parsing_default_args (cp_parser *parser, tree type, tree scope)
14358 cp_lexer *saved_lexer;
14359 cp_token_cache *tokens;
14360 bool saved_local_variables_forbidden_p;
14363 for (parameters = TYPE_ARG_TYPES (type);
14365 parameters = TREE_CHAIN (parameters))
14367 if (!TREE_PURPOSE (parameters)
14368 || TREE_CODE (TREE_PURPOSE (parameters)) != DEFAULT_ARG)
14371 /* Save away the current lexer. */
14372 saved_lexer = parser->lexer;
14373 /* Create a new one, using the tokens we have saved. */
14374 tokens = DEFARG_TOKENS (TREE_PURPOSE (parameters));
14375 parser->lexer = cp_lexer_new_from_tokens (tokens);
14377 /* Set the current source position to be the location of the
14378 first token in the default argument. */
14379 cp_lexer_set_source_position_from_token
14380 (parser->lexer, cp_lexer_peek_token (parser->lexer));
14382 /* Local variable names (and the `this' keyword) may not appear
14383 in a default argument. */
14384 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
14385 parser->local_variables_forbidden_p = true;
14386 /* Parse the assignment-expression. */
14388 push_nested_class (scope, 1);
14389 TREE_PURPOSE (parameters) = cp_parser_assignment_expression (parser);
14391 pop_nested_class ();
14393 /* Restore saved state. */
14394 parser->lexer = saved_lexer;
14395 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
14399 /* Parse the operand of `sizeof' (or a similar operator). Returns
14400 either a TYPE or an expression, depending on the form of the
14401 input. The KEYWORD indicates which kind of expression we have
14405 cp_parser_sizeof_operand (parser, keyword)
14409 static const char *format;
14410 tree expr = NULL_TREE;
14411 const char *saved_message;
14412 bool saved_constant_expression_p;
14414 /* Initialize FORMAT the first time we get here. */
14416 format = "types may not be defined in `%s' expressions";
14418 /* Types cannot be defined in a `sizeof' expression. Save away the
14420 saved_message = parser->type_definition_forbidden_message;
14421 /* And create the new one. */
14422 parser->type_definition_forbidden_message
14424 xmalloc (strlen (format)
14425 + strlen (IDENTIFIER_POINTER (ridpointers[keyword]))
14427 sprintf ((char *) parser->type_definition_forbidden_message,
14428 format, IDENTIFIER_POINTER (ridpointers[keyword]));
14430 /* The restrictions on constant-expressions do not apply inside
14431 sizeof expressions. */
14432 saved_constant_expression_p = parser->constant_expression_p;
14433 parser->constant_expression_p = false;
14435 /* Do not actually evaluate the expression. */
14437 /* If it's a `(', then we might be looking at the type-id
14439 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
14443 /* We can't be sure yet whether we're looking at a type-id or an
14445 cp_parser_parse_tentatively (parser);
14446 /* Consume the `('. */
14447 cp_lexer_consume_token (parser->lexer);
14448 /* Parse the type-id. */
14449 type = cp_parser_type_id (parser);
14450 /* Now, look for the trailing `)'. */
14451 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14452 /* If all went well, then we're done. */
14453 if (cp_parser_parse_definitely (parser))
14455 /* Build a list of decl-specifiers; right now, we have only
14456 a single type-specifier. */
14457 type = build_tree_list (NULL_TREE,
14460 /* Call grokdeclarator to figure out what type this is. */
14461 expr = grokdeclarator (NULL_TREE,
14465 /*attrlist=*/NULL);
14469 /* If the type-id production did not work out, then we must be
14470 looking at the unary-expression production. */
14472 expr = cp_parser_unary_expression (parser, /*address_p=*/false);
14473 /* Go back to evaluating expressions. */
14476 /* Free the message we created. */
14477 free ((char *) parser->type_definition_forbidden_message);
14478 /* And restore the old one. */
14479 parser->type_definition_forbidden_message = saved_message;
14480 parser->constant_expression_p = saved_constant_expression_p;
14485 /* If the current declaration has no declarator, return true. */
14488 cp_parser_declares_only_class_p (cp_parser *parser)
14490 /* If the next token is a `;' or a `,' then there is no
14492 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
14493 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
14496 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
14497 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
14500 cp_parser_friend_p (decl_specifiers)
14501 tree decl_specifiers;
14503 while (decl_specifiers)
14505 /* See if this decl-specifier is `friend'. */
14506 if (TREE_CODE (TREE_VALUE (decl_specifiers)) == IDENTIFIER_NODE
14507 && C_RID_CODE (TREE_VALUE (decl_specifiers)) == RID_FRIEND)
14510 /* Go on to the next decl-specifier. */
14511 decl_specifiers = TREE_CHAIN (decl_specifiers);
14517 /* If the next token is of the indicated TYPE, consume it. Otherwise,
14518 issue an error message indicating that TOKEN_DESC was expected.
14520 Returns the token consumed, if the token had the appropriate type.
14521 Otherwise, returns NULL. */
14524 cp_parser_require (parser, type, token_desc)
14526 enum cpp_ttype type;
14527 const char *token_desc;
14529 if (cp_lexer_next_token_is (parser->lexer, type))
14530 return cp_lexer_consume_token (parser->lexer);
14533 /* Output the MESSAGE -- unless we're parsing tentatively. */
14534 if (!cp_parser_simulate_error (parser))
14535 error ("expected %s", token_desc);
14540 /* Like cp_parser_require, except that tokens will be skipped until
14541 the desired token is found. An error message is still produced if
14542 the next token is not as expected. */
14545 cp_parser_skip_until_found (parser, type, token_desc)
14547 enum cpp_ttype type;
14548 const char *token_desc;
14551 unsigned nesting_depth = 0;
14553 if (cp_parser_require (parser, type, token_desc))
14556 /* Skip tokens until the desired token is found. */
14559 /* Peek at the next token. */
14560 token = cp_lexer_peek_token (parser->lexer);
14561 /* If we've reached the token we want, consume it and
14563 if (token->type == type && !nesting_depth)
14565 cp_lexer_consume_token (parser->lexer);
14568 /* If we've run out of tokens, stop. */
14569 if (token->type == CPP_EOF)
14571 if (token->type == CPP_OPEN_BRACE
14572 || token->type == CPP_OPEN_PAREN
14573 || token->type == CPP_OPEN_SQUARE)
14575 else if (token->type == CPP_CLOSE_BRACE
14576 || token->type == CPP_CLOSE_PAREN
14577 || token->type == CPP_CLOSE_SQUARE)
14579 if (nesting_depth-- == 0)
14582 /* Consume this token. */
14583 cp_lexer_consume_token (parser->lexer);
14587 /* If the next token is the indicated keyword, consume it. Otherwise,
14588 issue an error message indicating that TOKEN_DESC was expected.
14590 Returns the token consumed, if the token had the appropriate type.
14591 Otherwise, returns NULL. */
14594 cp_parser_require_keyword (parser, keyword, token_desc)
14597 const char *token_desc;
14599 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
14601 if (token && token->keyword != keyword)
14603 dyn_string_t error_msg;
14605 /* Format the error message. */
14606 error_msg = dyn_string_new (0);
14607 dyn_string_append_cstr (error_msg, "expected ");
14608 dyn_string_append_cstr (error_msg, token_desc);
14609 cp_parser_error (parser, error_msg->s);
14610 dyn_string_delete (error_msg);
14617 /* Returns TRUE iff TOKEN is a token that can begin the body of a
14618 function-definition. */
14621 cp_parser_token_starts_function_definition_p (token)
14624 return (/* An ordinary function-body begins with an `{'. */
14625 token->type == CPP_OPEN_BRACE
14626 /* A ctor-initializer begins with a `:'. */
14627 || token->type == CPP_COLON
14628 /* A function-try-block begins with `try'. */
14629 || token->keyword == RID_TRY
14630 /* The named return value extension begins with `return'. */
14631 || token->keyword == RID_RETURN);
14634 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
14638 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
14642 token = cp_lexer_peek_token (parser->lexer);
14643 return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
14646 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
14647 or none_type otherwise. */
14649 static enum tag_types
14650 cp_parser_token_is_class_key (token)
14653 switch (token->keyword)
14658 return record_type;
14667 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
14670 cp_parser_check_class_key (enum tag_types class_key, tree type)
14672 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
14673 pedwarn ("`%s' tag used in naming `%#T'",
14674 class_key == union_type ? "union"
14675 : class_key == record_type ? "struct" : "class",
14679 /* Look for the `template' keyword, as a syntactic disambiguator.
14680 Return TRUE iff it is present, in which case it will be
14684 cp_parser_optional_template_keyword (cp_parser *parser)
14686 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
14688 /* The `template' keyword can only be used within templates;
14689 outside templates the parser can always figure out what is a
14690 template and what is not. */
14691 if (!processing_template_decl)
14693 error ("`template' (as a disambiguator) is only allowed "
14694 "within templates");
14695 /* If this part of the token stream is rescanned, the same
14696 error message would be generated. So, we purge the token
14697 from the stream. */
14698 cp_lexer_purge_token (parser->lexer);
14703 /* Consume the `template' keyword. */
14704 cp_lexer_consume_token (parser->lexer);
14712 /* Add tokens to CACHE until an non-nested END token appears. */
14715 cp_parser_cache_group (cp_parser *parser,
14716 cp_token_cache *cache,
14717 enum cpp_ttype end,
14724 /* Abort a parenthesized expression if we encounter a brace. */
14725 if ((end == CPP_CLOSE_PAREN || depth == 0)
14726 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
14728 /* Consume the next token. */
14729 token = cp_lexer_consume_token (parser->lexer);
14730 /* If we've reached the end of the file, stop. */
14731 if (token->type == CPP_EOF)
14733 /* Add this token to the tokens we are saving. */
14734 cp_token_cache_push_token (cache, token);
14735 /* See if it starts a new group. */
14736 if (token->type == CPP_OPEN_BRACE)
14738 cp_parser_cache_group (parser, cache, CPP_CLOSE_BRACE, depth + 1);
14742 else if (token->type == CPP_OPEN_PAREN)
14743 cp_parser_cache_group (parser, cache, CPP_CLOSE_PAREN, depth + 1);
14744 else if (token->type == end)
14749 /* Begin parsing tentatively. We always save tokens while parsing
14750 tentatively so that if the tentative parsing fails we can restore the
14754 cp_parser_parse_tentatively (parser)
14757 /* Enter a new parsing context. */
14758 parser->context = cp_parser_context_new (parser->context);
14759 /* Begin saving tokens. */
14760 cp_lexer_save_tokens (parser->lexer);
14761 /* In order to avoid repetitive access control error messages,
14762 access checks are queued up until we are no longer parsing
14764 cp_parser_start_deferring_access_checks (parser);
14767 /* Commit to the currently active tentative parse. */
14770 cp_parser_commit_to_tentative_parse (parser)
14773 cp_parser_context *context;
14776 /* Mark all of the levels as committed. */
14777 lexer = parser->lexer;
14778 for (context = parser->context; context->next; context = context->next)
14780 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
14782 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
14783 while (!cp_lexer_saving_tokens (lexer))
14784 lexer = lexer->next;
14785 cp_lexer_commit_tokens (lexer);
14789 /* Abort the currently active tentative parse. All consumed tokens
14790 will be rolled back, and no diagnostics will be issued. */
14793 cp_parser_abort_tentative_parse (parser)
14796 cp_parser_simulate_error (parser);
14797 /* Now, pretend that we want to see if the construct was
14798 successfully parsed. */
14799 cp_parser_parse_definitely (parser);
14802 /* Stop parsing tentatively. If a parse error has ocurred, restore the
14803 token stream. Otherwise, commit to the tokens we have consumed.
14804 Returns true if no error occurred; false otherwise. */
14807 cp_parser_parse_definitely (parser)
14810 bool error_occurred;
14811 cp_parser_context *context;
14813 /* Remember whether or not an error ocurred, since we are about to
14814 destroy that information. */
14815 error_occurred = cp_parser_error_occurred (parser);
14816 /* Remove the topmost context from the stack. */
14817 context = parser->context;
14818 parser->context = context->next;
14819 /* If no parse errors occurred, commit to the tentative parse. */
14820 if (!error_occurred)
14822 /* Commit to the tokens read tentatively, unless that was
14824 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
14825 cp_lexer_commit_tokens (parser->lexer);
14826 if (!parser->context->deferring_access_checks_p)
14827 /* If in the parent context we are not deferring checks, then
14828 these perform these checks now. */
14829 (cp_parser_perform_deferred_access_checks
14830 (context->deferred_access_checks));
14832 /* Any lookups that were deferred during the tentative parse are
14834 parser->context->deferred_access_checks
14835 = chainon (parser->context->deferred_access_checks,
14836 context->deferred_access_checks);
14838 /* Otherwise, if errors occurred, roll back our state so that things
14839 are just as they were before we began the tentative parse. */
14841 cp_lexer_rollback_tokens (parser->lexer);
14842 /* Add the context to the front of the free list. */
14843 context->next = cp_parser_context_free_list;
14844 cp_parser_context_free_list = context;
14846 return !error_occurred;
14849 /* Returns non-zero if we are parsing tentatively. */
14852 cp_parser_parsing_tentatively (parser)
14855 return parser->context->next != NULL;
14858 /* Returns true if we are parsing tentatively -- but have decided that
14859 we will stick with this tentative parse, even if errors occur. */
14862 cp_parser_committed_to_tentative_parse (parser)
14865 return (cp_parser_parsing_tentatively (parser)
14866 && parser->context->status == CP_PARSER_STATUS_KIND_COMMITTED);
14869 /* Returns non-zero iff an error has occurred during the most recent
14870 tentative parse. */
14873 cp_parser_error_occurred (parser)
14876 return (cp_parser_parsing_tentatively (parser)
14877 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
14880 /* Returns non-zero if GNU extensions are allowed. */
14883 cp_parser_allow_gnu_extensions_p (parser)
14886 return parser->allow_gnu_extensions_p;
14893 static GTY (()) cp_parser *the_parser;
14895 /* External interface. */
14897 /* Parse the entire translation unit. */
14902 bool error_occurred;
14904 the_parser = cp_parser_new ();
14905 error_occurred = cp_parser_translation_unit (the_parser);
14908 return error_occurred;
14911 /* Clean up after parsing the entire translation unit. */
14914 free_parser_stacks ()
14916 /* Nothing to do. */
14919 /* This variable must be provided by every front end. */
14923 #include "gt-cp-parser.h"