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"
44 A cp_lexer represents a stream of cp_tokens. It allows arbitrary
50 We use a circular buffer to store incoming tokens.
52 Some artifacts of the C++ language (such as the
53 expression/declaration ambiguity) require arbitrary look-ahead.
54 The strategy we adopt for dealing with these problems is to attempt
55 to parse one construct (e.g., the declaration) and fall back to the
56 other (e.g., the expression) if that attempt does not succeed.
57 Therefore, we must sometimes store an arbitrary number of tokens.
59 The parser routinely peeks at the next token, and then consumes it
60 later. That also requires a buffer in which to store the tokens.
62 In order to easily permit adding tokens to the end of the buffer,
63 while removing them from the beginning of the buffer, we use a
68 typedef struct cp_token GTY (())
70 /* The kind of token. */
72 /* The value associated with this token, if any. */
74 /* If this token is a keyword, this value indicates which keyword.
75 Otherwise, this value is RID_MAX. */
77 /* The file in which this token was found. */
78 const char *file_name;
79 /* The line at which this token was found. */
83 /* The number of tokens in a single token block. */
85 #define CP_TOKEN_BLOCK_NUM_TOKENS 32
87 /* A group of tokens. These groups are chained together to store
88 large numbers of tokens. (For example, a token block is created
89 when the body of an inline member function is first encountered;
90 the tokens are processed later after the class definition is
93 This somewhat ungainly data structure (as opposed to, say, a
94 variable-length array), is used due to contraints imposed by the
95 current garbage-collection methodology. If it is made more
96 flexible, we could perhaps simplify the data structures involved. */
98 typedef struct cp_token_block GTY (())
101 cp_token tokens[CP_TOKEN_BLOCK_NUM_TOKENS];
102 /* The number of tokens in this block. */
104 /* The next token block in the chain. */
105 struct cp_token_block *next;
106 /* The previous block in the chain. */
107 struct cp_token_block *prev;
110 typedef struct cp_token_cache GTY (())
112 /* The first block in the cache. NULL if there are no tokens in the
114 cp_token_block *first;
115 /* The last block in the cache. NULL If there are no tokens in the
117 cp_token_block *last;
122 static cp_token_cache *cp_token_cache_new
124 static void cp_token_cache_push_token
125 (cp_token_cache *, cp_token *);
127 /* Create a new cp_token_cache. */
129 static cp_token_cache *
130 cp_token_cache_new ()
132 return (cp_token_cache *) ggc_alloc_cleared (sizeof (cp_token_cache));
135 /* Add *TOKEN to *CACHE. */
138 cp_token_cache_push_token (cp_token_cache *cache,
141 cp_token_block *b = cache->last;
143 /* See if we need to allocate a new token block. */
144 if (!b || b->num_tokens == CP_TOKEN_BLOCK_NUM_TOKENS)
146 b = ((cp_token_block *) ggc_alloc_cleared (sizeof (cp_token_block)));
147 b->prev = cache->last;
150 cache->last->next = b;
154 cache->first = cache->last = b;
156 /* Add this token to the current token block. */
157 b->tokens[b->num_tokens++] = *token;
160 /* The cp_lexer structure represents the C++ lexer. It is responsible
161 for managing the token stream from the preprocessor and supplying
164 typedef struct cp_lexer GTY (())
166 /* The memory allocated for the buffer. Never NULL. */
167 cp_token * GTY ((length ("(%h.buffer_end - %h.buffer)"))) buffer;
168 /* A pointer just past the end of the memory allocated for the buffer. */
169 cp_token * GTY ((skip (""))) buffer_end;
170 /* The first valid token in the buffer, or NULL if none. */
171 cp_token * GTY ((skip (""))) first_token;
172 /* The next available token. If NEXT_TOKEN is NULL, then there are
173 no more available tokens. */
174 cp_token * GTY ((skip (""))) next_token;
175 /* A pointer just past the last available token. If FIRST_TOKEN is
176 NULL, however, there are no available tokens, and then this
177 location is simply the place in which the next token read will be
178 placed. If LAST_TOKEN == FIRST_TOKEN, then the buffer is full.
179 When the LAST_TOKEN == BUFFER, then the last token is at the
180 highest memory address in the BUFFER. */
181 cp_token * GTY ((skip (""))) last_token;
183 /* A stack indicating positions at which cp_lexer_save_tokens was
184 called. The top entry is the most recent position at which we
185 began saving tokens. The entries are differences in token
186 position between FIRST_TOKEN and the first saved token.
188 If the stack is non-empty, we are saving tokens. When a token is
189 consumed, the NEXT_TOKEN pointer will move, but the FIRST_TOKEN
190 pointer will not. The token stream will be preserved so that it
191 can be reexamined later.
193 If the stack is empty, then we are not saving tokens. Whenever a
194 token is consumed, the FIRST_TOKEN pointer will be moved, and the
195 consumed token will be gone forever. */
196 varray_type saved_tokens;
198 /* The STRING_CST tokens encountered while processing the current
200 varray_type string_tokens;
202 /* True if we should obtain more tokens from the preprocessor; false
203 if we are processing a saved token cache. */
206 /* True if we should output debugging information. */
209 /* The next lexer in a linked list of lexers. */
210 struct cp_lexer *next;
215 static cp_lexer *cp_lexer_new_main
217 static cp_lexer *cp_lexer_new_from_tokens
218 PARAMS ((struct cp_token_cache *));
219 static int cp_lexer_saving_tokens
220 PARAMS ((const cp_lexer *));
221 static cp_token *cp_lexer_next_token
222 PARAMS ((cp_lexer *, cp_token *));
223 static ptrdiff_t cp_lexer_token_difference
224 PARAMS ((cp_lexer *, cp_token *, cp_token *));
225 static cp_token *cp_lexer_read_token
226 PARAMS ((cp_lexer *));
227 static void cp_lexer_maybe_grow_buffer
228 PARAMS ((cp_lexer *));
229 static void cp_lexer_get_preprocessor_token
230 PARAMS ((cp_lexer *, cp_token *));
231 static cp_token *cp_lexer_peek_token
232 PARAMS ((cp_lexer *));
233 static cp_token *cp_lexer_peek_nth_token
234 PARAMS ((cp_lexer *, size_t));
235 static inline bool cp_lexer_next_token_is
236 PARAMS ((cp_lexer *, enum cpp_ttype));
237 static bool cp_lexer_next_token_is_not
238 PARAMS ((cp_lexer *, enum cpp_ttype));
239 static bool cp_lexer_next_token_is_keyword
240 PARAMS ((cp_lexer *, enum rid));
241 static cp_token *cp_lexer_consume_token
242 PARAMS ((cp_lexer *));
243 static void cp_lexer_purge_token
245 static void cp_lexer_purge_tokens_after
246 (cp_lexer *, cp_token *);
247 static void cp_lexer_save_tokens
248 PARAMS ((cp_lexer *));
249 static void cp_lexer_commit_tokens
250 PARAMS ((cp_lexer *));
251 static void cp_lexer_rollback_tokens
252 PARAMS ((cp_lexer *));
253 static inline void cp_lexer_set_source_position_from_token
254 PARAMS ((cp_lexer *, const cp_token *));
255 static void cp_lexer_print_token
256 PARAMS ((FILE *, cp_token *));
257 static inline bool cp_lexer_debugging_p
258 PARAMS ((cp_lexer *));
259 static void cp_lexer_start_debugging
260 PARAMS ((cp_lexer *)) ATTRIBUTE_UNUSED;
261 static void cp_lexer_stop_debugging
262 PARAMS ((cp_lexer *)) ATTRIBUTE_UNUSED;
264 /* Manifest constants. */
266 #define CP_TOKEN_BUFFER_SIZE 5
267 #define CP_SAVED_TOKENS_SIZE 5
269 /* A token type for keywords, as opposed to ordinary identifiers. */
270 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
272 /* A token type for template-ids. If a template-id is processed while
273 parsing tentatively, it is replaced with a CPP_TEMPLATE_ID token;
274 the value of the CPP_TEMPLATE_ID is whatever was returned by
275 cp_parser_template_id. */
276 #define CPP_TEMPLATE_ID ((enum cpp_ttype) (CPP_KEYWORD + 1))
278 /* A token type for nested-name-specifiers. If a
279 nested-name-specifier is processed while parsing tentatively, it is
280 replaced with a CPP_NESTED_NAME_SPECIFIER token; the value of the
281 CPP_NESTED_NAME_SPECIFIER is whatever was returned by
282 cp_parser_nested_name_specifier_opt. */
283 #define CPP_NESTED_NAME_SPECIFIER ((enum cpp_ttype) (CPP_TEMPLATE_ID + 1))
285 /* A token type for tokens that are not tokens at all; these are used
286 to mark the end of a token block. */
287 #define CPP_NONE (CPP_NESTED_NAME_SPECIFIER + 1)
291 /* The stream to which debugging output should be written. */
292 static FILE *cp_lexer_debug_stream;
294 /* Create a new main C++ lexer, the lexer that gets tokens from the
298 cp_lexer_new_main (void)
301 cp_token first_token;
303 /* It's possible that lexing the first token will load a PCH file,
304 which is a GC collection point. So we have to grab the first
305 token before allocating any memory. */
306 cp_lexer_get_preprocessor_token (NULL, &first_token);
307 cpp_get_callbacks (parse_in)->valid_pch = NULL;
309 /* Allocate the memory. */
310 lexer = (cp_lexer *) ggc_alloc_cleared (sizeof (cp_lexer));
312 /* Create the circular buffer. */
313 lexer->buffer = ((cp_token *)
314 ggc_calloc (CP_TOKEN_BUFFER_SIZE, sizeof (cp_token)));
315 lexer->buffer_end = lexer->buffer + CP_TOKEN_BUFFER_SIZE;
317 /* There is one token in the buffer. */
318 lexer->last_token = lexer->buffer + 1;
319 lexer->first_token = lexer->buffer;
320 lexer->next_token = lexer->buffer;
321 memcpy (lexer->buffer, &first_token, sizeof (cp_token));
323 /* This lexer obtains more tokens by calling c_lex. */
324 lexer->main_lexer_p = true;
326 /* Create the SAVED_TOKENS stack. */
327 VARRAY_INT_INIT (lexer->saved_tokens, CP_SAVED_TOKENS_SIZE, "saved_tokens");
329 /* Create the STRINGS array. */
330 VARRAY_TREE_INIT (lexer->string_tokens, 32, "strings");
332 /* Assume we are not debugging. */
333 lexer->debugging_p = false;
338 /* Create a new lexer whose token stream is primed with the TOKENS.
339 When these tokens are exhausted, no new tokens will be read. */
342 cp_lexer_new_from_tokens (cp_token_cache *tokens)
346 cp_token_block *block;
347 ptrdiff_t num_tokens;
349 /* Allocate the memory. */
350 lexer = (cp_lexer *) ggc_alloc_cleared (sizeof (cp_lexer));
352 /* Create a new buffer, appropriately sized. */
354 for (block = tokens->first; block != NULL; block = block->next)
355 num_tokens += block->num_tokens;
356 lexer->buffer = ((cp_token *) ggc_alloc (num_tokens * sizeof (cp_token)));
357 lexer->buffer_end = lexer->buffer + num_tokens;
359 /* Install the tokens. */
360 token = lexer->buffer;
361 for (block = tokens->first; block != NULL; block = block->next)
363 memcpy (token, block->tokens, block->num_tokens * sizeof (cp_token));
364 token += block->num_tokens;
367 /* The FIRST_TOKEN is the beginning of the buffer. */
368 lexer->first_token = lexer->buffer;
369 /* The next available token is also at the beginning of the buffer. */
370 lexer->next_token = lexer->buffer;
371 /* The buffer is full. */
372 lexer->last_token = lexer->first_token;
374 /* This lexer doesn't obtain more tokens. */
375 lexer->main_lexer_p = false;
377 /* Create the SAVED_TOKENS stack. */
378 VARRAY_INT_INIT (lexer->saved_tokens, CP_SAVED_TOKENS_SIZE, "saved_tokens");
380 /* Create the STRINGS array. */
381 VARRAY_TREE_INIT (lexer->string_tokens, 32, "strings");
383 /* Assume we are not debugging. */
384 lexer->debugging_p = false;
389 /* Returns non-zero if debugging information should be output. */
392 cp_lexer_debugging_p (cp_lexer *lexer)
394 return lexer->debugging_p;
397 /* Set the current source position from the information stored in
401 cp_lexer_set_source_position_from_token (lexer, token)
402 cp_lexer *lexer ATTRIBUTE_UNUSED;
403 const cp_token *token;
405 /* Ideally, the source position information would not be a global
406 variable, but it is. */
408 /* Update the line number. */
409 if (token->type != CPP_EOF)
411 lineno = token->line_number;
412 input_filename = token->file_name;
416 /* TOKEN points into the circular token buffer. Return a pointer to
417 the next token in the buffer. */
419 static inline cp_token *
420 cp_lexer_next_token (lexer, token)
425 if (token == lexer->buffer_end)
426 token = lexer->buffer;
430 /* Non-zero if we are presently saving tokens. */
433 cp_lexer_saving_tokens (lexer)
434 const cp_lexer *lexer;
436 return VARRAY_ACTIVE_SIZE (lexer->saved_tokens) != 0;
439 /* Return a pointer to the token that is N tokens beyond TOKEN in the
443 cp_lexer_advance_token (cp_lexer *lexer, cp_token *token, ptrdiff_t n)
446 if (token >= lexer->buffer_end)
447 token = lexer->buffer + (token - lexer->buffer_end);
451 /* Returns the number of times that START would have to be incremented
452 to reach FINISH. If START and FINISH are the same, returns zero. */
455 cp_lexer_token_difference (lexer, start, finish)
461 return finish - start;
463 return ((lexer->buffer_end - lexer->buffer)
467 /* Obtain another token from the C preprocessor and add it to the
468 token buffer. Returns the newly read token. */
471 cp_lexer_read_token (lexer)
476 /* Make sure there is room in the buffer. */
477 cp_lexer_maybe_grow_buffer (lexer);
479 /* If there weren't any tokens, then this one will be the first. */
480 if (!lexer->first_token)
481 lexer->first_token = lexer->last_token;
482 /* Similarly, if there were no available tokens, there is one now. */
483 if (!lexer->next_token)
484 lexer->next_token = lexer->last_token;
486 /* Figure out where we're going to store the new token. */
487 token = lexer->last_token;
489 /* Get a new token from the preprocessor. */
490 cp_lexer_get_preprocessor_token (lexer, token);
492 /* Increment LAST_TOKEN. */
493 lexer->last_token = cp_lexer_next_token (lexer, token);
495 /* The preprocessor does not yet do translation phase six, i.e., the
496 combination of adjacent string literals. Therefore, we do it
498 if (token->type == CPP_STRING || token->type == CPP_WSTRING)
503 /* When we grow the buffer, we may invalidate TOKEN. So, save
504 the distance from the beginning of the BUFFER so that we can
506 delta = cp_lexer_token_difference (lexer, lexer->buffer, token);
507 /* Make sure there is room in the buffer for another token. */
508 cp_lexer_maybe_grow_buffer (lexer);
510 token = lexer->buffer;
511 for (i = 0; i < delta; ++i)
512 token = cp_lexer_next_token (lexer, token);
514 VARRAY_PUSH_TREE (lexer->string_tokens, token->value);
517 /* Read the token after TOKEN. */
518 cp_lexer_get_preprocessor_token (lexer, lexer->last_token);
519 /* See whether it's another string constant. */
520 if (lexer->last_token->type != token->type)
522 /* If not, then it will be the next real token. */
523 lexer->last_token = cp_lexer_next_token (lexer,
528 /* Chain the strings together. */
529 VARRAY_PUSH_TREE (lexer->string_tokens,
530 lexer->last_token->value);
533 /* Create a single STRING_CST. Curiously we have to call
534 combine_strings even if there is only a single string in
535 order to get the type set correctly. */
536 token->value = combine_strings (lexer->string_tokens);
537 VARRAY_CLEAR (lexer->string_tokens);
538 token->value = fix_string_type (token->value);
539 /* Strings should have type `const char []'. Right now, we will
540 have an ARRAY_TYPE that is constant rather than an array of
541 constant elements. */
542 if (flag_const_strings)
546 /* Get the current type. It will be an ARRAY_TYPE. */
547 type = TREE_TYPE (token->value);
548 /* Use build_cplus_array_type to rebuild the array, thereby
549 getting the right type. */
550 type = build_cplus_array_type (TREE_TYPE (type),
552 /* Reset the type of the token. */
553 TREE_TYPE (token->value) = type;
560 /* If the circular buffer is full, make it bigger. */
563 cp_lexer_maybe_grow_buffer (lexer)
566 /* If the buffer is full, enlarge it. */
567 if (lexer->last_token == lexer->first_token)
569 cp_token *new_buffer;
570 cp_token *old_buffer;
571 cp_token *new_first_token;
572 ptrdiff_t buffer_length;
573 size_t num_tokens_to_copy;
575 /* Remember the current buffer pointer. It will become invalid,
576 but we will need to do pointer arithmetic involving this
578 old_buffer = lexer->buffer;
579 /* Compute the current buffer size. */
580 buffer_length = lexer->buffer_end - lexer->buffer;
581 /* Allocate a buffer twice as big. */
582 new_buffer = ((cp_token *)
583 ggc_realloc (lexer->buffer,
584 2 * buffer_length * sizeof (cp_token)));
586 /* Because the buffer is circular, logically consecutive tokens
587 are not necessarily placed consecutively in memory.
588 Therefore, we must keep move the tokens that were before
589 FIRST_TOKEN to the second half of the newly allocated
591 num_tokens_to_copy = (lexer->first_token - old_buffer);
592 memcpy (new_buffer + buffer_length,
594 num_tokens_to_copy * sizeof (cp_token));
595 /* Clear the rest of the buffer. We never look at this storage,
596 but the garbage collector may. */
597 memset (new_buffer + buffer_length + num_tokens_to_copy, 0,
598 (buffer_length - num_tokens_to_copy) * sizeof (cp_token));
600 /* Now recompute all of the buffer pointers. */
602 = new_buffer + (lexer->first_token - old_buffer);
603 if (lexer->next_token != NULL)
605 ptrdiff_t next_token_delta;
607 if (lexer->next_token > lexer->first_token)
608 next_token_delta = lexer->next_token - lexer->first_token;
611 buffer_length - (lexer->first_token - lexer->next_token);
612 lexer->next_token = new_first_token + next_token_delta;
614 lexer->last_token = new_first_token + buffer_length;
615 lexer->buffer = new_buffer;
616 lexer->buffer_end = new_buffer + buffer_length * 2;
617 lexer->first_token = new_first_token;
621 /* Store the next token from the preprocessor in *TOKEN. */
624 cp_lexer_get_preprocessor_token (lexer, token)
625 cp_lexer *lexer ATTRIBUTE_UNUSED;
630 /* If this not the main lexer, return a terminating CPP_EOF token. */
631 if (lexer != NULL && !lexer->main_lexer_p)
633 token->type = CPP_EOF;
634 token->line_number = 0;
635 token->file_name = NULL;
636 token->value = NULL_TREE;
637 token->keyword = RID_MAX;
643 /* Keep going until we get a token we like. */
646 /* Get a new token from the preprocessor. */
647 token->type = c_lex (&token->value);
648 /* Issue messages about tokens we cannot process. */
654 error ("invalid token");
658 /* These tokens are already warned about by c_lex. */
662 /* This is a good token, so we exit the loop. */
667 /* Now we've got our token. */
668 token->line_number = lineno;
669 token->file_name = input_filename;
671 /* Check to see if this token is a keyword. */
672 if (token->type == CPP_NAME
673 && C_IS_RESERVED_WORD (token->value))
675 /* Mark this token as a keyword. */
676 token->type = CPP_KEYWORD;
677 /* Record which keyword. */
678 token->keyword = C_RID_CODE (token->value);
679 /* Update the value. Some keywords are mapped to particular
680 entities, rather than simply having the value of the
681 corresponding IDENTIFIER_NODE. For example, `__const' is
682 mapped to `const'. */
683 token->value = ridpointers[token->keyword];
686 token->keyword = RID_MAX;
689 /* Return a pointer to the next token in the token stream, but do not
693 cp_lexer_peek_token (lexer)
698 /* If there are no tokens, read one now. */
699 if (!lexer->next_token)
700 cp_lexer_read_token (lexer);
702 /* Provide debugging output. */
703 if (cp_lexer_debugging_p (lexer))
705 fprintf (cp_lexer_debug_stream, "cp_lexer: peeking at token: ");
706 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
707 fprintf (cp_lexer_debug_stream, "\n");
710 token = lexer->next_token;
711 cp_lexer_set_source_position_from_token (lexer, token);
715 /* Return true if the next token has the indicated TYPE. */
718 cp_lexer_next_token_is (lexer, type)
724 /* Peek at the next token. */
725 token = cp_lexer_peek_token (lexer);
726 /* Check to see if it has the indicated TYPE. */
727 return token->type == type;
730 /* Return true if the next token does not have the indicated TYPE. */
733 cp_lexer_next_token_is_not (lexer, type)
737 return !cp_lexer_next_token_is (lexer, type);
740 /* Return true if the next token is the indicated KEYWORD. */
743 cp_lexer_next_token_is_keyword (lexer, keyword)
749 /* Peek at the next token. */
750 token = cp_lexer_peek_token (lexer);
751 /* Check to see if it is the indicated keyword. */
752 return token->keyword == keyword;
755 /* Return a pointer to the Nth token in the token stream. If N is 1,
756 then this is precisely equivalent to cp_lexer_peek_token. */
759 cp_lexer_peek_nth_token (lexer, n)
765 /* N is 1-based, not zero-based. */
766 my_friendly_assert (n > 0, 20000224);
768 /* Skip ahead from NEXT_TOKEN, reading more tokens as necessary. */
769 token = lexer->next_token;
770 /* If there are no tokens in the buffer, get one now. */
773 cp_lexer_read_token (lexer);
774 token = lexer->next_token;
777 /* Now, read tokens until we have enough. */
780 /* Advance to the next token. */
781 token = cp_lexer_next_token (lexer, token);
782 /* If that's all the tokens we have, read a new one. */
783 if (token == lexer->last_token)
784 token = cp_lexer_read_token (lexer);
790 /* Consume the next token. The pointer returned is valid only until
791 another token is read. Callers should preserve copy the token
792 explicitly if they will need its value for a longer period of
796 cp_lexer_consume_token (lexer)
801 /* If there are no tokens, read one now. */
802 if (!lexer->next_token)
803 cp_lexer_read_token (lexer);
805 /* Remember the token we'll be returning. */
806 token = lexer->next_token;
808 /* Increment NEXT_TOKEN. */
809 lexer->next_token = cp_lexer_next_token (lexer,
811 /* Check to see if we're all out of tokens. */
812 if (lexer->next_token == lexer->last_token)
813 lexer->next_token = NULL;
815 /* If we're not saving tokens, then move FIRST_TOKEN too. */
816 if (!cp_lexer_saving_tokens (lexer))
818 /* If there are no tokens available, set FIRST_TOKEN to NULL. */
819 if (!lexer->next_token)
820 lexer->first_token = NULL;
822 lexer->first_token = lexer->next_token;
825 /* Provide debugging output. */
826 if (cp_lexer_debugging_p (lexer))
828 fprintf (cp_lexer_debug_stream, "cp_lexer: consuming token: ");
829 cp_lexer_print_token (cp_lexer_debug_stream, token);
830 fprintf (cp_lexer_debug_stream, "\n");
836 /* Permanently remove the next token from the token stream. There
837 must be a valid next token already; this token never reads
838 additional tokens from the preprocessor. */
841 cp_lexer_purge_token (cp_lexer *lexer)
844 cp_token *next_token;
846 token = lexer->next_token;
849 next_token = cp_lexer_next_token (lexer, token);
850 if (next_token == lexer->last_token)
852 *token = *next_token;
856 lexer->last_token = token;
857 /* The token purged may have been the only token remaining; if so,
859 if (lexer->next_token == token)
860 lexer->next_token = NULL;
863 /* Permanently remove all tokens after TOKEN, up to, but not
864 including, the token that will be returned next by
865 cp_lexer_peek_token. */
868 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *token)
874 if (lexer->next_token)
876 /* Copy the tokens that have not yet been read to the location
877 immediately following TOKEN. */
878 t1 = cp_lexer_next_token (lexer, token);
879 t2 = peek = cp_lexer_peek_token (lexer);
880 /* Move tokens into the vacant area between TOKEN and PEEK. */
881 while (t2 != lexer->last_token)
884 t1 = cp_lexer_next_token (lexer, t1);
885 t2 = cp_lexer_next_token (lexer, t2);
887 /* Now, the next available token is right after TOKEN. */
888 lexer->next_token = cp_lexer_next_token (lexer, token);
889 /* And the last token is wherever we ended up. */
890 lexer->last_token = t1;
894 /* There are no tokens in the buffer, so there is nothing to
895 copy. The last token in the buffer is TOKEN itself. */
896 lexer->last_token = cp_lexer_next_token (lexer, token);
900 /* Begin saving tokens. All tokens consumed after this point will be
904 cp_lexer_save_tokens (lexer)
907 /* Provide debugging output. */
908 if (cp_lexer_debugging_p (lexer))
909 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
911 /* Make sure that LEXER->NEXT_TOKEN is non-NULL so that we can
912 restore the tokens if required. */
913 if (!lexer->next_token)
914 cp_lexer_read_token (lexer);
916 VARRAY_PUSH_INT (lexer->saved_tokens,
917 cp_lexer_token_difference (lexer,
922 /* Commit to the portion of the token stream most recently saved. */
925 cp_lexer_commit_tokens (lexer)
928 /* Provide debugging output. */
929 if (cp_lexer_debugging_p (lexer))
930 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
932 VARRAY_POP (lexer->saved_tokens);
935 /* Return all tokens saved since the last call to cp_lexer_save_tokens
936 to the token stream. Stop saving tokens. */
939 cp_lexer_rollback_tokens (lexer)
944 /* Provide debugging output. */
945 if (cp_lexer_debugging_p (lexer))
946 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
948 /* Find the token that was the NEXT_TOKEN when we started saving
950 delta = VARRAY_TOP_INT(lexer->saved_tokens);
951 /* Make it the next token again now. */
952 lexer->next_token = cp_lexer_advance_token (lexer,
955 /* It might be the case that there wer no tokens when we started
956 saving tokens, but that there are some tokens now. */
957 if (!lexer->next_token && lexer->first_token)
958 lexer->next_token = lexer->first_token;
960 /* Stop saving tokens. */
961 VARRAY_POP (lexer->saved_tokens);
964 /* Print a representation of the TOKEN on the STREAM. */
967 cp_lexer_print_token (stream, token)
971 const char *token_type = NULL;
973 /* Figure out what kind of token this is. */
981 token_type = "COMMA";
985 token_type = "OPEN_PAREN";
988 case CPP_CLOSE_PAREN:
989 token_type = "CLOSE_PAREN";
993 token_type = "OPEN_BRACE";
996 case CPP_CLOSE_BRACE:
997 token_type = "CLOSE_BRACE";
1001 token_type = "SEMICOLON";
1005 token_type = "NAME";
1013 token_type = "keyword";
1016 /* This is not a token that we know how to handle yet. */
1021 /* If we have a name for the token, print it out. Otherwise, we
1022 simply give the numeric code. */
1024 fprintf (stream, "%s", token_type);
1026 fprintf (stream, "%d", token->type);
1027 /* And, for an identifier, print the identifier name. */
1028 if (token->type == CPP_NAME
1029 /* Some keywords have a value that is not an IDENTIFIER_NODE.
1030 For example, `struct' is mapped to an INTEGER_CST. */
1031 || (token->type == CPP_KEYWORD
1032 && TREE_CODE (token->value) == IDENTIFIER_NODE))
1033 fprintf (stream, " %s", IDENTIFIER_POINTER (token->value));
1036 /* Start emitting debugging information. */
1039 cp_lexer_start_debugging (lexer)
1042 ++lexer->debugging_p;
1045 /* Stop emitting debugging information. */
1048 cp_lexer_stop_debugging (lexer)
1051 --lexer->debugging_p;
1060 A cp_parser parses the token stream as specified by the C++
1061 grammar. Its job is purely parsing, not semantic analysis. For
1062 example, the parser breaks the token stream into declarators,
1063 expressions, statements, and other similar syntactic constructs.
1064 It does not check that the types of the expressions on either side
1065 of an assignment-statement are compatible, or that a function is
1066 not declared with a parameter of type `void'.
1068 The parser invokes routines elsewhere in the compiler to perform
1069 semantic analysis and to build up the abstract syntax tree for the
1072 The parser (and the template instantiation code, which is, in a
1073 way, a close relative of parsing) are the only parts of the
1074 compiler that should be calling push_scope and pop_scope, or
1075 related functions. The parser (and template instantiation code)
1076 keeps track of what scope is presently active; everything else
1077 should simply honor that. (The code that generates static
1078 initializers may also need to set the scope, in order to check
1079 access control correctly when emitting the initializers.)
1084 The parser is of the standard recursive-descent variety. Upcoming
1085 tokens in the token stream are examined in order to determine which
1086 production to use when parsing a non-terminal. Some C++ constructs
1087 require arbitrary look ahead to disambiguate. For example, it is
1088 impossible, in the general case, to tell whether a statement is an
1089 expression or declaration without scanning the entire statement.
1090 Therefore, the parser is capable of "parsing tentatively." When the
1091 parser is not sure what construct comes next, it enters this mode.
1092 Then, while we attempt to parse the construct, the parser queues up
1093 error messages, rather than issuing them immediately, and saves the
1094 tokens it consumes. If the construct is parsed successfully, the
1095 parser "commits", i.e., it issues any queued error messages and
1096 the tokens that were being preserved are permanently discarded.
1097 If, however, the construct is not parsed successfully, the parser
1098 rolls back its state completely so that it can resume parsing using
1099 a different alternative.
1104 The performance of the parser could probably be improved
1105 substantially. Some possible improvements include:
1107 - The expression parser recurses through the various levels of
1108 precedence as specified in the grammar, rather than using an
1109 operator-precedence technique. Therefore, parsing a simple
1110 identifier requires multiple recursive calls.
1112 - We could often eliminate the need to parse tentatively by
1113 looking ahead a little bit. In some places, this approach
1114 might not entirely eliminate the need to parse tentatively, but
1115 it might still speed up the average case. */
1117 /* Flags that are passed to some parsing functions. These values can
1118 be bitwise-ored together. */
1120 typedef enum cp_parser_flags
1123 CP_PARSER_FLAGS_NONE = 0x0,
1124 /* The construct is optional. If it is not present, then no error
1125 should be issued. */
1126 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1127 /* When parsing a type-specifier, do not allow user-defined types. */
1128 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2
1131 /* The different kinds of ids that we ecounter. */
1133 typedef enum cp_parser_id_kind
1135 /* Not an id at all. */
1136 CP_PARSER_ID_KIND_NONE,
1137 /* An unqualified-id that is not a template-id. */
1138 CP_PARSER_ID_KIND_UNQUALIFIED,
1139 /* An unqualified template-id. */
1140 CP_PARSER_ID_KIND_TEMPLATE_ID,
1141 /* A qualified-id. */
1142 CP_PARSER_ID_KIND_QUALIFIED
1143 } cp_parser_id_kind;
1145 /* The different kinds of declarators we want to parse. */
1147 typedef enum cp_parser_declarator_kind
1149 /* We want an abstract declartor. */
1150 CP_PARSER_DECLARATOR_ABSTRACT,
1151 /* We want a named declarator. */
1152 CP_PARSER_DECLARATOR_NAMED,
1153 /* We don't mind. */
1154 CP_PARSER_DECLARATOR_EITHER
1155 } cp_parser_declarator_kind;
1157 /* A mapping from a token type to a corresponding tree node type. */
1159 typedef struct cp_parser_token_tree_map_node
1161 /* The token type. */
1162 enum cpp_ttype token_type;
1163 /* The corresponding tree code. */
1164 enum tree_code tree_type;
1165 } cp_parser_token_tree_map_node;
1167 /* A complete map consists of several ordinary entries, followed by a
1168 terminator. The terminating entry has a token_type of CPP_EOF. */
1170 typedef cp_parser_token_tree_map_node cp_parser_token_tree_map[];
1172 /* The status of a tentative parse. */
1174 typedef enum cp_parser_status_kind
1176 /* No errors have occurred. */
1177 CP_PARSER_STATUS_KIND_NO_ERROR,
1178 /* An error has occurred. */
1179 CP_PARSER_STATUS_KIND_ERROR,
1180 /* We are committed to this tentative parse, whether or not an error
1182 CP_PARSER_STATUS_KIND_COMMITTED
1183 } cp_parser_status_kind;
1185 /* Context that is saved and restored when parsing tentatively. */
1187 typedef struct cp_parser_context GTY (())
1189 /* If this is a tentative parsing context, the status of the
1191 enum cp_parser_status_kind status;
1192 /* If non-NULL, we have just seen a `x->' or `x.' expression. Names
1193 that are looked up in this context must be looked up both in the
1194 scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1195 the context of the containing expression. */
1197 /* A TREE_LIST representing name-lookups for which we have deferred
1198 checking access controls. We cannot check the accessibility of
1199 names used in a decl-specifier-seq until we know what is being
1200 declared because code like:
1207 A::B* A::f() { return 0; }
1209 is valid, even though `A::B' is not generally accessible.
1211 The TREE_PURPOSE of each node is the scope used to qualify the
1212 name being looked up; the TREE_VALUE is the DECL to which the
1213 name was resolved. */
1214 tree deferred_access_checks;
1215 /* TRUE iff we are deferring access checks. */
1216 bool deferring_access_checks_p;
1217 /* The next parsing context in the stack. */
1218 struct cp_parser_context *next;
1219 } cp_parser_context;
1223 /* Constructors and destructors. */
1225 static cp_parser_context *cp_parser_context_new
1226 PARAMS ((cp_parser_context *));
1228 /* Class variables. */
1230 static GTY((deletable (""))) cp_parser_context* cp_parser_context_free_list;
1232 /* Constructors and destructors. */
1234 /* Construct a new context. The context below this one on the stack
1235 is given by NEXT. */
1237 static cp_parser_context *
1238 cp_parser_context_new (next)
1239 cp_parser_context *next;
1241 cp_parser_context *context;
1243 /* Allocate the storage. */
1244 if (cp_parser_context_free_list != NULL)
1246 /* Pull the first entry from the free list. */
1247 context = cp_parser_context_free_list;
1248 cp_parser_context_free_list = context->next;
1249 memset ((char *)context, 0, sizeof (*context));
1252 context = ((cp_parser_context *)
1253 ggc_alloc_cleared (sizeof (cp_parser_context)));
1254 /* No errors have occurred yet in this context. */
1255 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1256 /* If this is not the bottomost context, copy information that we
1257 need from the previous context. */
1260 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1261 expression, then we are parsing one in this context, too. */
1262 context->object_type = next->object_type;
1263 /* We are deferring access checks here if we were in the NEXT
1265 context->deferring_access_checks_p
1266 = next->deferring_access_checks_p;
1267 /* Thread the stack. */
1268 context->next = next;
1274 /* The cp_parser structure represents the C++ parser. */
1276 typedef struct cp_parser GTY(())
1278 /* The lexer from which we are obtaining tokens. */
1281 /* The scope in which names should be looked up. If NULL_TREE, then
1282 we look up names in the scope that is currently open in the
1283 source program. If non-NULL, this is either a TYPE or
1284 NAMESPACE_DECL for the scope in which we should look.
1286 This value is not cleared automatically after a name is looked
1287 up, so we must be careful to clear it before starting a new look
1288 up sequence. (If it is not cleared, then `X::Y' followed by `Z'
1289 will look up `Z' in the scope of `X', rather than the current
1290 scope.) Unfortunately, it is difficult to tell when name lookup
1291 is complete, because we sometimes peek at a token, look it up,
1292 and then decide not to consume it. */
1295 /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1296 last lookup took place. OBJECT_SCOPE is used if an expression
1297 like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1298 respectively. QUALIFYING_SCOPE is used for an expression of the
1299 form "X::Y"; it refers to X. */
1301 tree qualifying_scope;
1303 /* A stack of parsing contexts. All but the bottom entry on the
1304 stack will be tentative contexts.
1306 We parse tentatively in order to determine which construct is in
1307 use in some situations. For example, in order to determine
1308 whether a statement is an expression-statement or a
1309 declaration-statement we parse it tentatively as a
1310 declaration-statement. If that fails, we then reparse the same
1311 token stream as an expression-statement. */
1312 cp_parser_context *context;
1314 /* True if we are parsing GNU C++. If this flag is not set, then
1315 GNU extensions are not recognized. */
1316 bool allow_gnu_extensions_p;
1318 /* TRUE if the `>' token should be interpreted as the greater-than
1319 operator. FALSE if it is the end of a template-id or
1320 template-parameter-list. */
1321 bool greater_than_is_operator_p;
1323 /* TRUE if default arguments are allowed within a parameter list
1324 that starts at this point. FALSE if only a gnu extension makes
1325 them permissable. */
1326 bool default_arg_ok_p;
1328 /* TRUE if we are parsing an integral constant-expression. See
1329 [expr.const] for a precise definition. */
1330 /* FIXME: Need to implement code that checks this flag. */
1331 bool constant_expression_p;
1333 /* TRUE if local variable names and `this' are forbidden in the
1335 bool local_variables_forbidden_p;
1337 /* TRUE if the declaration we are parsing is part of a
1338 linkage-specification of the form `extern string-literal
1340 bool in_unbraced_linkage_specification_p;
1342 /* TRUE if we are presently parsing a declarator, after the
1343 direct-declarator. */
1344 bool in_declarator_p;
1346 /* If non-NULL, then we are parsing a construct where new type
1347 definitions are not permitted. The string stored here will be
1348 issued as an error message if a type is defined. */
1349 const char *type_definition_forbidden_message;
1351 /* A TREE_LIST of queues of functions whose bodies have been lexed,
1352 but may not have been parsed. These functions are friends of
1353 members defined within a class-specification; they are not
1354 procssed until the class is complete. The active queue is at the
1357 Within each queue, functions appear in the reverse order that
1358 they appeared in the source. Each TREE_VALUE is a
1359 FUNCTION_DECL of TEMPLATE_DECL corresponding to a member
1361 tree unparsed_functions_queues;
1363 /* The number of classes whose definitions are currently in
1365 unsigned num_classes_being_defined;
1367 /* The number of template parameter lists that apply directly to the
1368 current declaration. */
1369 unsigned num_template_parameter_lists;
1371 /* List of access checks lists, used to prevent GC collection while
1373 tree access_checks_lists;
1376 /* The type of a function that parses some kind of expression */
1377 typedef tree (*cp_parser_expression_fn) PARAMS ((cp_parser *));
1381 /* Constructors and destructors. */
1383 static cp_parser *cp_parser_new
1386 /* Routines to parse various constructs.
1388 Those that return `tree' will return the error_mark_node (rather
1389 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1390 Sometimes, they will return an ordinary node if error-recovery was
1391 attempted, even though a parse error occurrred. So, to check
1392 whether or not a parse error occurred, you should always use
1393 cp_parser_error_occurred. If the construct is optional (indicated
1394 either by an `_opt' in the name of the function that does the
1395 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1396 the construct is not present. */
1398 /* Lexical conventions [gram.lex] */
1400 static tree cp_parser_identifier
1401 PARAMS ((cp_parser *));
1403 /* Basic concepts [gram.basic] */
1405 static bool cp_parser_translation_unit
1406 PARAMS ((cp_parser *));
1408 /* Expressions [gram.expr] */
1410 static tree cp_parser_primary_expression
1411 (cp_parser *, cp_parser_id_kind *, tree *);
1412 static tree cp_parser_id_expression
1413 PARAMS ((cp_parser *, bool, bool, bool *));
1414 static tree cp_parser_unqualified_id
1415 PARAMS ((cp_parser *, bool, bool));
1416 static tree cp_parser_nested_name_specifier_opt
1417 (cp_parser *, bool, bool, bool);
1418 static tree cp_parser_nested_name_specifier
1419 (cp_parser *, bool, bool, bool);
1420 static tree cp_parser_class_or_namespace_name
1421 (cp_parser *, bool, bool, bool, bool);
1422 static tree cp_parser_postfix_expression
1423 (cp_parser *, bool);
1424 static tree cp_parser_expression_list
1425 PARAMS ((cp_parser *));
1426 static void cp_parser_pseudo_destructor_name
1427 PARAMS ((cp_parser *, tree *, tree *));
1428 static tree cp_parser_unary_expression
1429 (cp_parser *, bool);
1430 static enum tree_code cp_parser_unary_operator
1431 PARAMS ((cp_token *));
1432 static tree cp_parser_new_expression
1433 PARAMS ((cp_parser *));
1434 static tree cp_parser_new_placement
1435 PARAMS ((cp_parser *));
1436 static tree cp_parser_new_type_id
1437 PARAMS ((cp_parser *));
1438 static tree cp_parser_new_declarator_opt
1439 PARAMS ((cp_parser *));
1440 static tree cp_parser_direct_new_declarator
1441 PARAMS ((cp_parser *));
1442 static tree cp_parser_new_initializer
1443 PARAMS ((cp_parser *));
1444 static tree cp_parser_delete_expression
1445 PARAMS ((cp_parser *));
1446 static tree cp_parser_cast_expression
1447 (cp_parser *, bool);
1448 static tree cp_parser_pm_expression
1449 PARAMS ((cp_parser *));
1450 static tree cp_parser_multiplicative_expression
1451 PARAMS ((cp_parser *));
1452 static tree cp_parser_additive_expression
1453 PARAMS ((cp_parser *));
1454 static tree cp_parser_shift_expression
1455 PARAMS ((cp_parser *));
1456 static tree cp_parser_relational_expression
1457 PARAMS ((cp_parser *));
1458 static tree cp_parser_equality_expression
1459 PARAMS ((cp_parser *));
1460 static tree cp_parser_and_expression
1461 PARAMS ((cp_parser *));
1462 static tree cp_parser_exclusive_or_expression
1463 PARAMS ((cp_parser *));
1464 static tree cp_parser_inclusive_or_expression
1465 PARAMS ((cp_parser *));
1466 static tree cp_parser_logical_and_expression
1467 PARAMS ((cp_parser *));
1468 static tree cp_parser_logical_or_expression
1469 PARAMS ((cp_parser *));
1470 static tree cp_parser_conditional_expression
1471 PARAMS ((cp_parser *));
1472 static tree cp_parser_question_colon_clause
1473 PARAMS ((cp_parser *, tree));
1474 static tree cp_parser_assignment_expression
1475 PARAMS ((cp_parser *));
1476 static enum tree_code cp_parser_assignment_operator_opt
1477 PARAMS ((cp_parser *));
1478 static tree cp_parser_expression
1479 PARAMS ((cp_parser *));
1480 static tree cp_parser_constant_expression
1481 PARAMS ((cp_parser *));
1483 /* Statements [gram.stmt.stmt] */
1485 static void cp_parser_statement
1486 PARAMS ((cp_parser *));
1487 static tree cp_parser_labeled_statement
1488 PARAMS ((cp_parser *));
1489 static tree cp_parser_expression_statement
1490 PARAMS ((cp_parser *));
1491 static tree cp_parser_compound_statement
1493 static void cp_parser_statement_seq_opt
1494 PARAMS ((cp_parser *));
1495 static tree cp_parser_selection_statement
1496 PARAMS ((cp_parser *));
1497 static tree cp_parser_condition
1498 PARAMS ((cp_parser *));
1499 static tree cp_parser_iteration_statement
1500 PARAMS ((cp_parser *));
1501 static void cp_parser_for_init_statement
1502 PARAMS ((cp_parser *));
1503 static tree cp_parser_jump_statement
1504 PARAMS ((cp_parser *));
1505 static void cp_parser_declaration_statement
1506 PARAMS ((cp_parser *));
1508 static tree cp_parser_implicitly_scoped_statement
1509 PARAMS ((cp_parser *));
1510 static void cp_parser_already_scoped_statement
1511 PARAMS ((cp_parser *));
1513 /* Declarations [gram.dcl.dcl] */
1515 static void cp_parser_declaration_seq_opt
1516 PARAMS ((cp_parser *));
1517 static void cp_parser_declaration
1518 PARAMS ((cp_parser *));
1519 static void cp_parser_block_declaration
1520 PARAMS ((cp_parser *, bool));
1521 static void cp_parser_simple_declaration
1522 PARAMS ((cp_parser *, bool));
1523 static tree cp_parser_decl_specifier_seq
1524 PARAMS ((cp_parser *, cp_parser_flags, tree *, bool *));
1525 static tree cp_parser_storage_class_specifier_opt
1526 PARAMS ((cp_parser *));
1527 static tree cp_parser_function_specifier_opt
1528 PARAMS ((cp_parser *));
1529 static tree cp_parser_type_specifier
1530 (cp_parser *, cp_parser_flags, bool, bool, bool *, bool *);
1531 static tree cp_parser_simple_type_specifier
1532 PARAMS ((cp_parser *, cp_parser_flags));
1533 static tree cp_parser_type_name
1534 PARAMS ((cp_parser *));
1535 static tree cp_parser_elaborated_type_specifier
1536 PARAMS ((cp_parser *, bool, bool));
1537 static tree cp_parser_enum_specifier
1538 PARAMS ((cp_parser *));
1539 static void cp_parser_enumerator_list
1540 PARAMS ((cp_parser *, tree));
1541 static void cp_parser_enumerator_definition
1542 PARAMS ((cp_parser *, tree));
1543 static tree cp_parser_namespace_name
1544 PARAMS ((cp_parser *));
1545 static void cp_parser_namespace_definition
1546 PARAMS ((cp_parser *));
1547 static void cp_parser_namespace_body
1548 PARAMS ((cp_parser *));
1549 static tree cp_parser_qualified_namespace_specifier
1550 PARAMS ((cp_parser *));
1551 static void cp_parser_namespace_alias_definition
1552 PARAMS ((cp_parser *));
1553 static void cp_parser_using_declaration
1554 PARAMS ((cp_parser *));
1555 static void cp_parser_using_directive
1556 PARAMS ((cp_parser *));
1557 static void cp_parser_asm_definition
1558 PARAMS ((cp_parser *));
1559 static void cp_parser_linkage_specification
1560 PARAMS ((cp_parser *));
1562 /* Declarators [gram.dcl.decl] */
1564 static tree cp_parser_init_declarator
1565 PARAMS ((cp_parser *, tree, tree, tree, bool, bool, bool *));
1566 static tree cp_parser_declarator
1567 PARAMS ((cp_parser *, cp_parser_declarator_kind, bool *));
1568 static tree cp_parser_direct_declarator
1569 PARAMS ((cp_parser *, cp_parser_declarator_kind, bool *));
1570 static enum tree_code cp_parser_ptr_operator
1571 PARAMS ((cp_parser *, tree *, tree *));
1572 static tree cp_parser_cv_qualifier_seq_opt
1573 PARAMS ((cp_parser *));
1574 static tree cp_parser_cv_qualifier_opt
1575 PARAMS ((cp_parser *));
1576 static tree cp_parser_declarator_id
1577 PARAMS ((cp_parser *));
1578 static tree cp_parser_type_id
1579 PARAMS ((cp_parser *));
1580 static tree cp_parser_type_specifier_seq
1581 PARAMS ((cp_parser *));
1582 static tree cp_parser_parameter_declaration_clause
1583 PARAMS ((cp_parser *));
1584 static tree cp_parser_parameter_declaration_list
1585 PARAMS ((cp_parser *));
1586 static tree cp_parser_parameter_declaration
1587 PARAMS ((cp_parser *, bool));
1588 static tree cp_parser_function_definition
1589 PARAMS ((cp_parser *, bool *));
1590 static void cp_parser_function_body
1592 static tree cp_parser_initializer
1593 PARAMS ((cp_parser *, bool *));
1594 static tree cp_parser_initializer_clause
1595 PARAMS ((cp_parser *));
1596 static tree cp_parser_initializer_list
1597 PARAMS ((cp_parser *));
1599 static bool cp_parser_ctor_initializer_opt_and_function_body
1602 /* Classes [gram.class] */
1604 static tree cp_parser_class_name
1605 (cp_parser *, bool, bool, bool, bool, bool, bool);
1606 static tree cp_parser_class_specifier
1607 PARAMS ((cp_parser *));
1608 static tree cp_parser_class_head
1609 PARAMS ((cp_parser *, bool *, bool *, tree *));
1610 static enum tag_types cp_parser_class_key
1611 PARAMS ((cp_parser *));
1612 static void cp_parser_member_specification_opt
1613 PARAMS ((cp_parser *));
1614 static void cp_parser_member_declaration
1615 PARAMS ((cp_parser *));
1616 static tree cp_parser_pure_specifier
1617 PARAMS ((cp_parser *));
1618 static tree cp_parser_constant_initializer
1619 PARAMS ((cp_parser *));
1621 /* Derived classes [gram.class.derived] */
1623 static tree cp_parser_base_clause
1624 PARAMS ((cp_parser *));
1625 static tree cp_parser_base_specifier
1626 PARAMS ((cp_parser *));
1628 /* Special member functions [gram.special] */
1630 static tree cp_parser_conversion_function_id
1631 PARAMS ((cp_parser *));
1632 static tree cp_parser_conversion_type_id
1633 PARAMS ((cp_parser *));
1634 static tree cp_parser_conversion_declarator_opt
1635 PARAMS ((cp_parser *));
1636 static bool cp_parser_ctor_initializer_opt
1637 PARAMS ((cp_parser *));
1638 static void cp_parser_mem_initializer_list
1639 PARAMS ((cp_parser *));
1640 static tree cp_parser_mem_initializer
1641 PARAMS ((cp_parser *));
1642 static tree cp_parser_mem_initializer_id
1643 PARAMS ((cp_parser *));
1645 /* Overloading [gram.over] */
1647 static tree cp_parser_operator_function_id
1648 PARAMS ((cp_parser *));
1649 static tree cp_parser_operator
1650 PARAMS ((cp_parser *));
1652 /* Templates [gram.temp] */
1654 static void cp_parser_template_declaration
1655 PARAMS ((cp_parser *, bool));
1656 static tree cp_parser_template_parameter_list
1657 PARAMS ((cp_parser *));
1658 static tree cp_parser_template_parameter
1659 PARAMS ((cp_parser *));
1660 static tree cp_parser_type_parameter
1661 PARAMS ((cp_parser *));
1662 static tree cp_parser_template_id
1663 PARAMS ((cp_parser *, bool, bool));
1664 static tree cp_parser_template_name
1665 PARAMS ((cp_parser *, bool, bool));
1666 static tree cp_parser_template_argument_list
1667 PARAMS ((cp_parser *));
1668 static tree cp_parser_template_argument
1669 PARAMS ((cp_parser *));
1670 static void cp_parser_explicit_instantiation
1671 PARAMS ((cp_parser *));
1672 static void cp_parser_explicit_specialization
1673 PARAMS ((cp_parser *));
1675 /* Exception handling [gram.exception] */
1677 static tree cp_parser_try_block
1678 PARAMS ((cp_parser *));
1679 static bool cp_parser_function_try_block
1680 PARAMS ((cp_parser *));
1681 static void cp_parser_handler_seq
1682 PARAMS ((cp_parser *));
1683 static void cp_parser_handler
1684 PARAMS ((cp_parser *));
1685 static tree cp_parser_exception_declaration
1686 PARAMS ((cp_parser *));
1687 static tree cp_parser_throw_expression
1688 PARAMS ((cp_parser *));
1689 static tree cp_parser_exception_specification_opt
1690 PARAMS ((cp_parser *));
1691 static tree cp_parser_type_id_list
1692 PARAMS ((cp_parser *));
1694 /* GNU Extensions */
1696 static tree cp_parser_asm_specification_opt
1697 PARAMS ((cp_parser *));
1698 static tree cp_parser_asm_operand_list
1699 PARAMS ((cp_parser *));
1700 static tree cp_parser_asm_clobber_list
1701 PARAMS ((cp_parser *));
1702 static tree cp_parser_attributes_opt
1703 PARAMS ((cp_parser *));
1704 static tree cp_parser_attribute_list
1705 PARAMS ((cp_parser *));
1706 static bool cp_parser_extension_opt
1707 PARAMS ((cp_parser *, int *));
1708 static void cp_parser_label_declaration
1709 PARAMS ((cp_parser *));
1711 /* Utility Routines */
1713 static tree cp_parser_lookup_name
1714 PARAMS ((cp_parser *, tree, bool, bool, bool, bool));
1715 static tree cp_parser_lookup_name_simple
1716 PARAMS ((cp_parser *, tree));
1717 static tree cp_parser_resolve_typename_type
1718 PARAMS ((cp_parser *, tree));
1719 static tree cp_parser_maybe_treat_template_as_class
1721 static bool cp_parser_check_declarator_template_parameters
1722 PARAMS ((cp_parser *, tree));
1723 static bool cp_parser_check_template_parameters
1724 PARAMS ((cp_parser *, unsigned));
1725 static tree cp_parser_binary_expression
1726 PARAMS ((cp_parser *,
1727 const cp_parser_token_tree_map,
1728 cp_parser_expression_fn));
1729 static tree cp_parser_global_scope_opt
1730 PARAMS ((cp_parser *, bool));
1731 static bool cp_parser_constructor_declarator_p
1732 (cp_parser *, bool);
1733 static tree cp_parser_function_definition_from_specifiers_and_declarator
1734 PARAMS ((cp_parser *, tree, tree, tree, tree));
1735 static tree cp_parser_function_definition_after_declarator
1736 PARAMS ((cp_parser *, bool));
1737 static void cp_parser_template_declaration_after_export
1738 PARAMS ((cp_parser *, bool));
1739 static tree cp_parser_single_declaration
1740 PARAMS ((cp_parser *, bool, bool *));
1741 static tree cp_parser_functional_cast
1742 PARAMS ((cp_parser *, tree));
1743 static void cp_parser_late_parsing_for_member
1744 PARAMS ((cp_parser *, tree));
1745 static void cp_parser_late_parsing_default_args
1746 (cp_parser *, tree);
1747 static tree cp_parser_sizeof_operand
1748 PARAMS ((cp_parser *, enum rid));
1749 static bool cp_parser_declares_only_class_p
1750 PARAMS ((cp_parser *));
1751 static bool cp_parser_friend_p
1753 static cp_token *cp_parser_require
1754 PARAMS ((cp_parser *, enum cpp_ttype, const char *));
1755 static cp_token *cp_parser_require_keyword
1756 PARAMS ((cp_parser *, enum rid, const char *));
1757 static bool cp_parser_token_starts_function_definition_p
1758 PARAMS ((cp_token *));
1759 static bool cp_parser_next_token_starts_class_definition_p
1761 static enum tag_types cp_parser_token_is_class_key
1762 PARAMS ((cp_token *));
1763 static void cp_parser_check_class_key
1764 (enum tag_types, tree type);
1765 static bool cp_parser_optional_template_keyword
1767 static void cp_parser_pre_parsed_nested_name_specifier
1769 static void cp_parser_cache_group
1770 (cp_parser *, cp_token_cache *, enum cpp_ttype, unsigned);
1771 static void cp_parser_parse_tentatively
1772 PARAMS ((cp_parser *));
1773 static void cp_parser_commit_to_tentative_parse
1774 PARAMS ((cp_parser *));
1775 static void cp_parser_abort_tentative_parse
1776 PARAMS ((cp_parser *));
1777 static bool cp_parser_parse_definitely
1778 PARAMS ((cp_parser *));
1779 static inline bool cp_parser_parsing_tentatively
1780 PARAMS ((cp_parser *));
1781 static bool cp_parser_committed_to_tentative_parse
1782 PARAMS ((cp_parser *));
1783 static void cp_parser_error
1784 PARAMS ((cp_parser *, const char *));
1785 static bool cp_parser_simulate_error
1786 PARAMS ((cp_parser *));
1787 static void cp_parser_check_type_definition
1788 PARAMS ((cp_parser *));
1789 static bool cp_parser_skip_to_closing_parenthesis
1790 PARAMS ((cp_parser *));
1791 static bool cp_parser_skip_to_closing_parenthesis_or_comma
1793 static void cp_parser_skip_to_end_of_statement
1794 PARAMS ((cp_parser *));
1795 static void cp_parser_skip_to_end_of_block_or_statement
1796 PARAMS ((cp_parser *));
1797 static void cp_parser_skip_to_closing_brace
1799 static void cp_parser_skip_until_found
1800 PARAMS ((cp_parser *, enum cpp_ttype, const char *));
1801 static bool cp_parser_error_occurred
1802 PARAMS ((cp_parser *));
1803 static bool cp_parser_allow_gnu_extensions_p
1804 PARAMS ((cp_parser *));
1805 static bool cp_parser_is_string_literal
1806 PARAMS ((cp_token *));
1807 static bool cp_parser_is_keyword
1808 PARAMS ((cp_token *, enum rid));
1809 static bool cp_parser_dependent_type_p
1811 static bool cp_parser_value_dependent_expression_p
1813 static bool cp_parser_type_dependent_expression_p
1815 static bool cp_parser_dependent_template_arg_p
1817 static bool cp_parser_dependent_template_id_p
1819 static bool cp_parser_dependent_template_p
1821 static void cp_parser_defer_access_check
1822 (cp_parser *, tree, tree);
1823 static void cp_parser_start_deferring_access_checks
1825 static tree cp_parser_stop_deferring_access_checks
1826 PARAMS ((cp_parser *));
1827 static void cp_parser_perform_deferred_access_checks
1829 static tree cp_parser_scope_through_which_access_occurs
1832 /* Returns non-zero if we are parsing tentatively. */
1835 cp_parser_parsing_tentatively (parser)
1838 return parser->context->next != NULL;
1841 /* Returns non-zero if TOKEN is a string literal. */
1844 cp_parser_is_string_literal (token)
1847 return (token->type == CPP_STRING || token->type == CPP_WSTRING);
1850 /* Returns non-zero if TOKEN is the indicated KEYWORD. */
1853 cp_parser_is_keyword (token, keyword)
1857 return token->keyword == keyword;
1860 /* Returns TRUE if TYPE is dependent, in the sense of
1864 cp_parser_dependent_type_p (type)
1869 if (!processing_template_decl)
1872 /* If the type is NULL, we have not computed a type for the entity
1873 in question; in that case, the type is dependent. */
1877 /* Erroneous types can be considered non-dependent. */
1878 if (type == error_mark_node)
1883 A type is dependent if it is:
1885 -- a template parameter. */
1886 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
1888 /* -- a qualified-id with a nested-name-specifier which contains a
1889 class-name that names a dependent type or whose unqualified-id
1890 names a dependent type. */
1891 if (TREE_CODE (type) == TYPENAME_TYPE)
1893 /* -- a cv-qualified type where the cv-unqualified type is
1895 type = TYPE_MAIN_VARIANT (type);
1896 /* -- a compound type constructed from any dependent type. */
1897 if (TYPE_PTRMEM_P (type) || TYPE_PTRMEMFUNC_P (type))
1898 return (cp_parser_dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type))
1899 || cp_parser_dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
1901 else if (TREE_CODE (type) == POINTER_TYPE
1902 || TREE_CODE (type) == REFERENCE_TYPE)
1903 return cp_parser_dependent_type_p (TREE_TYPE (type));
1904 else if (TREE_CODE (type) == FUNCTION_TYPE
1905 || TREE_CODE (type) == METHOD_TYPE)
1909 if (cp_parser_dependent_type_p (TREE_TYPE (type)))
1911 for (arg_type = TYPE_ARG_TYPES (type);
1913 arg_type = TREE_CHAIN (arg_type))
1914 if (cp_parser_dependent_type_p (TREE_VALUE (arg_type)))
1918 /* -- an array type constructed from any dependent type or whose
1919 size is specified by a constant expression that is
1921 if (TREE_CODE (type) == ARRAY_TYPE)
1923 if (TYPE_DOMAIN (type)
1924 && ((cp_parser_value_dependent_expression_p
1925 (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
1926 || (cp_parser_type_dependent_expression_p
1927 (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))))
1929 return cp_parser_dependent_type_p (TREE_TYPE (type));
1931 /* -- a template-id in which either the template name is a template
1932 parameter or any of the template arguments is a dependent type or
1933 an expression that is type-dependent or value-dependent.
1935 This language seems somewhat confused; for example, it does not
1936 discuss template template arguments. Therefore, we use the
1937 definition for dependent template arguments in [temp.dep.temp]. */
1938 if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INFO (type)
1939 && (cp_parser_dependent_template_id_p
1940 (CLASSTYPE_TI_TEMPLATE (type),
1941 CLASSTYPE_TI_ARGS (type))))
1943 else if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
1945 /* All TYPEOF_TYPEs are dependent; if the argument of the `typeof'
1946 expression is not type-dependent, then it should already been
1948 if (TREE_CODE (type) == TYPEOF_TYPE)
1950 /* The standard does not specifically mention types that are local
1951 to template functions or local classes, but they should be
1952 considered dependent too. For example:
1954 template <int I> void f() {
1959 The size of `E' cannot be known until the value of `I' has been
1960 determined. Therefore, `E' must be considered dependent. */
1961 scope = TYPE_CONTEXT (type);
1962 if (scope && TYPE_P (scope))
1963 return cp_parser_dependent_type_p (scope);
1964 else if (scope && TREE_CODE (scope) == FUNCTION_DECL)
1965 return cp_parser_type_dependent_expression_p (scope);
1967 /* Other types are non-dependent. */
1971 /* Returns TRUE if the EXPRESSION is value-dependent. */
1974 cp_parser_value_dependent_expression_p (tree expression)
1976 if (!processing_template_decl)
1979 /* A name declared with a dependent type. */
1980 if (DECL_P (expression)
1981 && cp_parser_dependent_type_p (TREE_TYPE (expression)))
1983 /* A non-type template parameter. */
1984 if ((TREE_CODE (expression) == CONST_DECL
1985 && DECL_TEMPLATE_PARM_P (expression))
1986 || TREE_CODE (expression) == TEMPLATE_PARM_INDEX)
1988 /* A constant with integral or enumeration type and is initialized
1989 with an expression that is value-dependent. */
1990 if (TREE_CODE (expression) == VAR_DECL
1991 && DECL_INITIAL (expression)
1992 && (CP_INTEGRAL_TYPE_P (TREE_TYPE (expression))
1993 || TREE_CODE (TREE_TYPE (expression)) == ENUMERAL_TYPE)
1994 && cp_parser_value_dependent_expression_p (DECL_INITIAL (expression)))
1996 /* These expressions are value-dependent if the type to which the
1997 cast occurs is dependent. */
1998 if ((TREE_CODE (expression) == DYNAMIC_CAST_EXPR
1999 || TREE_CODE (expression) == STATIC_CAST_EXPR
2000 || TREE_CODE (expression) == CONST_CAST_EXPR
2001 || TREE_CODE (expression) == REINTERPRET_CAST_EXPR
2002 || TREE_CODE (expression) == CAST_EXPR)
2003 && cp_parser_dependent_type_p (TREE_TYPE (expression)))
2005 /* A `sizeof' expression where the sizeof operand is a type is
2006 value-dependent if the type is dependent. If the type was not
2007 dependent, we would no longer have a SIZEOF_EXPR, so any
2008 SIZEOF_EXPR is dependent. */
2009 if (TREE_CODE (expression) == SIZEOF_EXPR)
2011 /* A constant expression is value-dependent if any subexpression is
2013 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (expression))))
2015 switch (TREE_CODE_CLASS (TREE_CODE (expression)))
2018 return (cp_parser_value_dependent_expression_p
2019 (TREE_OPERAND (expression, 0)));
2022 return ((cp_parser_value_dependent_expression_p
2023 (TREE_OPERAND (expression, 0)))
2024 || (cp_parser_value_dependent_expression_p
2025 (TREE_OPERAND (expression, 1))));
2030 i < TREE_CODE_LENGTH (TREE_CODE (expression));
2032 if (cp_parser_value_dependent_expression_p
2033 (TREE_OPERAND (expression, i)))
2040 /* The expression is not value-dependent. */
2044 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
2048 cp_parser_type_dependent_expression_p (expression)
2051 if (!processing_template_decl)
2054 /* Some expression forms are never type-dependent. */
2055 if (TREE_CODE (expression) == PSEUDO_DTOR_EXPR
2056 || TREE_CODE (expression) == SIZEOF_EXPR
2057 || TREE_CODE (expression) == ALIGNOF_EXPR
2058 || TREE_CODE (expression) == TYPEID_EXPR
2059 || TREE_CODE (expression) == DELETE_EXPR
2060 || TREE_CODE (expression) == VEC_DELETE_EXPR
2061 || TREE_CODE (expression) == THROW_EXPR)
2064 /* The types of these expressions depends only on the type to which
2066 if (TREE_CODE (expression) == DYNAMIC_CAST_EXPR
2067 || TREE_CODE (expression) == STATIC_CAST_EXPR
2068 || TREE_CODE (expression) == CONST_CAST_EXPR
2069 || TREE_CODE (expression) == REINTERPRET_CAST_EXPR
2070 || TREE_CODE (expression) == CAST_EXPR)
2071 return cp_parser_dependent_type_p (TREE_TYPE (expression));
2072 /* The types of these expressions depends only on the type created
2073 by the expression. */
2074 else if (TREE_CODE (expression) == NEW_EXPR
2075 || TREE_CODE (expression) == VEC_NEW_EXPR)
2076 return cp_parser_dependent_type_p (TREE_OPERAND (expression, 1));
2078 if (TREE_CODE (expression) == FUNCTION_DECL
2079 && DECL_LANG_SPECIFIC (expression)
2080 && DECL_TEMPLATE_INFO (expression)
2081 && (cp_parser_dependent_template_id_p
2082 (DECL_TI_TEMPLATE (expression),
2083 INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression)))))
2086 return (cp_parser_dependent_type_p (TREE_TYPE (expression)));
2089 /* Returns TRUE if the ARG (a template argument) is dependent. */
2092 cp_parser_dependent_template_arg_p (tree arg)
2094 if (!processing_template_decl)
2097 if (TREE_CODE (arg) == TEMPLATE_DECL
2098 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
2099 return cp_parser_dependent_template_p (arg);
2100 else if (TYPE_P (arg))
2101 return cp_parser_dependent_type_p (arg);
2103 return (cp_parser_type_dependent_expression_p (arg)
2104 || cp_parser_value_dependent_expression_p (arg));
2107 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
2110 cp_parser_dependent_template_id_p (tree tmpl, tree args)
2114 if (cp_parser_dependent_template_p (tmpl))
2116 for (i = 0; i < TREE_VEC_LENGTH (args); ++i)
2117 if (cp_parser_dependent_template_arg_p (TREE_VEC_ELT (args, i)))
2122 /* Returns TRUE if the template TMPL is dependent. */
2125 cp_parser_dependent_template_p (tree tmpl)
2127 /* Template template parameters are dependent. */
2128 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)
2129 || TREE_CODE (tmpl) == TEMPLATE_TEMPLATE_PARM)
2131 /* So are member templates of dependent classes. */
2132 if (TYPE_P (CP_DECL_CONTEXT (tmpl)))
2133 return cp_parser_dependent_type_p (DECL_CONTEXT (tmpl));
2137 /* Defer checking the accessibility of DECL, when looked up in
2141 cp_parser_defer_access_check (cp_parser *parser,
2147 /* If we are not supposed to defer access checks, just check now. */
2148 if (!parser->context->deferring_access_checks_p)
2150 enforce_access (class_type, decl);
2154 /* See if we are already going to perform this check. */
2155 for (check = parser->context->deferred_access_checks;
2157 check = TREE_CHAIN (check))
2158 if (TREE_VALUE (check) == decl
2159 && same_type_p (TREE_PURPOSE (check), class_type))
2161 /* If not, record the check. */
2162 parser->context->deferred_access_checks
2163 = tree_cons (class_type, decl, parser->context->deferred_access_checks);
2166 /* Start deferring access control checks. */
2169 cp_parser_start_deferring_access_checks (cp_parser *parser)
2171 parser->context->deferring_access_checks_p = true;
2174 /* Stop deferring access control checks. Returns a TREE_LIST
2175 representing the deferred checks. The TREE_PURPOSE of each node is
2176 the type through which the access occurred; the TREE_VALUE is the
2177 declaration named. */
2180 cp_parser_stop_deferring_access_checks (parser)
2185 parser->context->deferring_access_checks_p = false;
2186 access_checks = parser->context->deferred_access_checks;
2187 parser->context->deferred_access_checks = NULL_TREE;
2189 return access_checks;
2192 /* Perform the deferred ACCESS_CHECKS, whose representation is as
2193 documented with cp_parser_stop_deferrring_access_checks. */
2196 cp_parser_perform_deferred_access_checks (access_checks)
2199 tree deferred_check;
2201 /* Look through all the deferred checks. */
2202 for (deferred_check = access_checks;
2204 deferred_check = TREE_CHAIN (deferred_check))
2206 enforce_access (TREE_PURPOSE (deferred_check),
2207 TREE_VALUE (deferred_check));
2210 /* Returns the scope through which DECL is being accessed, or
2211 NULL_TREE if DECL is not a member. If OBJECT_TYPE is non-NULL, we
2212 have just seen `x->' or `x.' and OBJECT_TYPE is the type of `*x',
2213 or `x', respectively. If the DECL was named as `A::B' then
2214 NESTED_NAME_SPECIFIER is `A'. */
2217 cp_parser_scope_through_which_access_occurs (decl,
2219 nested_name_specifier)
2222 tree nested_name_specifier;
2225 tree qualifying_type = NULL_TREE;
2227 /* Determine the SCOPE of DECL. */
2228 scope = context_for_name_lookup (decl);
2229 /* If the SCOPE is not a type, then DECL is not a member. */
2230 if (!TYPE_P (scope))
2232 /* Figure out the type through which DECL is being accessed. */
2233 if (object_type && DERIVED_FROM_P (scope, object_type))
2234 /* If we are processing a `->' or `.' expression, use the type of the
2236 qualifying_type = object_type;
2237 else if (nested_name_specifier)
2239 /* If the reference is to a non-static member of the
2240 current class, treat it as if it were referenced through
2242 if (DECL_NONSTATIC_MEMBER_P (decl)
2243 && current_class_ptr
2244 && DERIVED_FROM_P (scope, current_class_type))
2245 qualifying_type = current_class_type;
2246 /* Otherwise, use the type indicated by the
2247 nested-name-specifier. */
2249 qualifying_type = nested_name_specifier;
2252 /* Otherwise, the name must be from the current class or one of
2254 qualifying_type = currently_open_derived_class (scope);
2256 return qualifying_type;
2259 /* Issue the indicated error MESSAGE. */
2262 cp_parser_error (parser, message)
2264 const char *message;
2266 /* Output the MESSAGE -- unless we're parsing tentatively. */
2267 if (!cp_parser_simulate_error (parser))
2271 /* If we are parsing tentatively, remember that an error has occurred
2272 during this tentative parse. Returns true if the error was
2273 simulated; false if a messgae should be issued by the caller. */
2276 cp_parser_simulate_error (parser)
2279 if (cp_parser_parsing_tentatively (parser)
2280 && !cp_parser_committed_to_tentative_parse (parser))
2282 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2288 /* This function is called when a type is defined. If type
2289 definitions are forbidden at this point, an error message is
2293 cp_parser_check_type_definition (parser)
2296 /* If types are forbidden here, issue a message. */
2297 if (parser->type_definition_forbidden_message)
2298 /* Use `%s' to print the string in case there are any escape
2299 characters in the message. */
2300 error ("%s", parser->type_definition_forbidden_message);
2303 /* Consume tokens up to, and including, the next non-nested closing `)'.
2304 Returns TRUE iff we found a closing `)'. */
2307 cp_parser_skip_to_closing_parenthesis (cp_parser *parser)
2309 unsigned nesting_depth = 0;
2315 /* If we've run out of tokens, then there is no closing `)'. */
2316 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2318 /* Consume the token. */
2319 token = cp_lexer_consume_token (parser->lexer);
2320 /* If it is an `(', we have entered another level of nesting. */
2321 if (token->type == CPP_OPEN_PAREN)
2323 /* If it is a `)', then we might be done. */
2324 else if (token->type == CPP_CLOSE_PAREN && nesting_depth-- == 0)
2329 /* Consume tokens until the next token is a `)', or a `,'. Returns
2330 TRUE if the next token is a `,'. */
2333 cp_parser_skip_to_closing_parenthesis_or_comma (cp_parser *parser)
2335 unsigned nesting_depth = 0;
2339 cp_token *token = cp_lexer_peek_token (parser->lexer);
2341 /* If we've run out of tokens, then there is no closing `)'. */
2342 if (token->type == CPP_EOF)
2344 /* If it is a `,' stop. */
2345 else if (token->type == CPP_COMMA && nesting_depth-- == 0)
2347 /* If it is a `)', stop. */
2348 else if (token->type == CPP_CLOSE_PAREN && nesting_depth-- == 0)
2350 /* If it is an `(', we have entered another level of nesting. */
2351 else if (token->type == CPP_OPEN_PAREN)
2353 /* Consume the token. */
2354 token = cp_lexer_consume_token (parser->lexer);
2358 /* Consume tokens until we reach the end of the current statement.
2359 Normally, that will be just before consuming a `;'. However, if a
2360 non-nested `}' comes first, then we stop before consuming that. */
2363 cp_parser_skip_to_end_of_statement (parser)
2366 unsigned nesting_depth = 0;
2372 /* Peek at the next token. */
2373 token = cp_lexer_peek_token (parser->lexer);
2374 /* If we've run out of tokens, stop. */
2375 if (token->type == CPP_EOF)
2377 /* If the next token is a `;', we have reached the end of the
2379 if (token->type == CPP_SEMICOLON && !nesting_depth)
2381 /* If the next token is a non-nested `}', then we have reached
2382 the end of the current block. */
2383 if (token->type == CPP_CLOSE_BRACE)
2385 /* If this is a non-nested `}', stop before consuming it.
2386 That way, when confronted with something like:
2390 we stop before consuming the closing `}', even though we
2391 have not yet reached a `;'. */
2392 if (nesting_depth == 0)
2394 /* If it is the closing `}' for a block that we have
2395 scanned, stop -- but only after consuming the token.
2401 we will stop after the body of the erroneously declared
2402 function, but before consuming the following `typedef'
2404 if (--nesting_depth == 0)
2406 cp_lexer_consume_token (parser->lexer);
2410 /* If it the next token is a `{', then we are entering a new
2411 block. Consume the entire block. */
2412 else if (token->type == CPP_OPEN_BRACE)
2414 /* Consume the token. */
2415 cp_lexer_consume_token (parser->lexer);
2419 /* Skip tokens until we have consumed an entire block, or until we
2420 have consumed a non-nested `;'. */
2423 cp_parser_skip_to_end_of_block_or_statement (parser)
2426 unsigned nesting_depth = 0;
2432 /* Peek at the next token. */
2433 token = cp_lexer_peek_token (parser->lexer);
2434 /* If we've run out of tokens, stop. */
2435 if (token->type == CPP_EOF)
2437 /* If the next token is a `;', we have reached the end of the
2439 if (token->type == CPP_SEMICOLON && !nesting_depth)
2441 /* Consume the `;'. */
2442 cp_lexer_consume_token (parser->lexer);
2445 /* Consume the token. */
2446 token = cp_lexer_consume_token (parser->lexer);
2447 /* If the next token is a non-nested `}', then we have reached
2448 the end of the current block. */
2449 if (token->type == CPP_CLOSE_BRACE
2450 && (nesting_depth == 0 || --nesting_depth == 0))
2452 /* If it the next token is a `{', then we are entering a new
2453 block. Consume the entire block. */
2454 if (token->type == CPP_OPEN_BRACE)
2459 /* Skip tokens until a non-nested closing curly brace is the next
2463 cp_parser_skip_to_closing_brace (cp_parser *parser)
2465 unsigned nesting_depth = 0;
2471 /* Peek at the next token. */
2472 token = cp_lexer_peek_token (parser->lexer);
2473 /* If we've run out of tokens, stop. */
2474 if (token->type == CPP_EOF)
2476 /* If the next token is a non-nested `}', then we have reached
2477 the end of the current block. */
2478 if (token->type == CPP_CLOSE_BRACE && nesting_depth-- == 0)
2480 /* If it the next token is a `{', then we are entering a new
2481 block. Consume the entire block. */
2482 else if (token->type == CPP_OPEN_BRACE)
2484 /* Consume the token. */
2485 cp_lexer_consume_token (parser->lexer);
2489 /* Create a new C++ parser. */
2497 /* cp_lexer_new_main is called before calling ggc_alloc because
2498 cp_lexer_new_main might load a PCH file. */
2499 lexer = cp_lexer_new_main ();
2501 parser = (cp_parser *) ggc_alloc_cleared (sizeof (cp_parser));
2502 parser->lexer = lexer;
2503 parser->context = cp_parser_context_new (NULL);
2505 /* For now, we always accept GNU extensions. */
2506 parser->allow_gnu_extensions_p = 1;
2508 /* The `>' token is a greater-than operator, not the end of a
2510 parser->greater_than_is_operator_p = true;
2512 parser->default_arg_ok_p = true;
2514 /* We are not parsing a constant-expression. */
2515 parser->constant_expression_p = false;
2517 /* Local variable names are not forbidden. */
2518 parser->local_variables_forbidden_p = false;
2520 /* We are not procesing an `extern "C"' declaration. */
2521 parser->in_unbraced_linkage_specification_p = false;
2523 /* We are not processing a declarator. */
2524 parser->in_declarator_p = false;
2526 /* The unparsed function queue is empty. */
2527 parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2529 /* There are no classes being defined. */
2530 parser->num_classes_being_defined = 0;
2532 /* No template parameters apply. */
2533 parser->num_template_parameter_lists = 0;
2538 /* Lexical conventions [gram.lex] */
2540 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
2544 cp_parser_identifier (parser)
2549 /* Look for the identifier. */
2550 token = cp_parser_require (parser, CPP_NAME, "identifier");
2551 /* Return the value. */
2552 return token ? token->value : error_mark_node;
2555 /* Basic concepts [gram.basic] */
2557 /* Parse a translation-unit.
2560 declaration-seq [opt]
2562 Returns TRUE if all went well. */
2565 cp_parser_translation_unit (parser)
2570 cp_parser_declaration_seq_opt (parser);
2572 /* If there are no tokens left then all went well. */
2573 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2576 /* Otherwise, issue an error message. */
2577 cp_parser_error (parser, "expected declaration");
2581 /* Consume the EOF token. */
2582 cp_parser_require (parser, CPP_EOF, "end-of-file");
2585 finish_translation_unit ();
2587 /* All went well. */
2591 /* Expressions [gram.expr] */
2593 /* Parse a primary-expression.
2604 ( compound-statement )
2605 __builtin_va_arg ( assignment-expression , type-id )
2610 Returns a representation of the expression.
2612 *IDK indicates what kind of id-expression (if any) was present.
2614 *QUALIFYING_CLASS is set to a non-NULL value if the id-expression can be
2615 used as the operand of a pointer-to-member. In that case,
2616 *QUALIFYING_CLASS gives the class that is used as the qualifying
2617 class in the pointer-to-member. */
2620 cp_parser_primary_expression (cp_parser *parser,
2621 cp_parser_id_kind *idk,
2622 tree *qualifying_class)
2626 /* Assume the primary expression is not an id-expression. */
2627 *idk = CP_PARSER_ID_KIND_NONE;
2628 /* And that it cannot be used as pointer-to-member. */
2629 *qualifying_class = NULL_TREE;
2631 /* Peek at the next token. */
2632 token = cp_lexer_peek_token (parser->lexer);
2633 switch (token->type)
2646 token = cp_lexer_consume_token (parser->lexer);
2647 return token->value;
2649 case CPP_OPEN_PAREN:
2652 bool saved_greater_than_is_operator_p;
2654 /* Consume the `('. */
2655 cp_lexer_consume_token (parser->lexer);
2656 /* Within a parenthesized expression, a `>' token is always
2657 the greater-than operator. */
2658 saved_greater_than_is_operator_p
2659 = parser->greater_than_is_operator_p;
2660 parser->greater_than_is_operator_p = true;
2661 /* If we see `( { ' then we are looking at the beginning of
2662 a GNU statement-expression. */
2663 if (cp_parser_allow_gnu_extensions_p (parser)
2664 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
2666 /* Statement-expressions are not allowed by the standard. */
2668 pedwarn ("ISO C++ forbids braced-groups within expressions");
2670 /* And they're not allowed outside of a function-body; you
2671 cannot, for example, write:
2673 int i = ({ int j = 3; j + 1; });
2675 at class or namespace scope. */
2676 if (!at_function_scope_p ())
2677 error ("statement-expressions are allowed only inside functions");
2678 /* Start the statement-expression. */
2679 expr = begin_stmt_expr ();
2680 /* Parse the compound-statement. */
2681 cp_parser_compound_statement (parser);
2683 expr = finish_stmt_expr (expr);
2687 /* Parse the parenthesized expression. */
2688 expr = cp_parser_expression (parser);
2689 /* Let the front end know that this expression was
2690 enclosed in parentheses. This matters in case, for
2691 example, the expression is of the form `A::B', since
2692 `&A::B' might be a pointer-to-member, but `&(A::B)' is
2694 finish_parenthesized_expr (expr);
2696 /* The `>' token might be the end of a template-id or
2697 template-parameter-list now. */
2698 parser->greater_than_is_operator_p
2699 = saved_greater_than_is_operator_p;
2700 /* Consume the `)'. */
2701 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
2702 cp_parser_skip_to_end_of_statement (parser);
2708 switch (token->keyword)
2710 /* These two are the boolean literals. */
2712 cp_lexer_consume_token (parser->lexer);
2713 return boolean_true_node;
2715 cp_lexer_consume_token (parser->lexer);
2716 return boolean_false_node;
2718 /* The `__null' literal. */
2720 cp_lexer_consume_token (parser->lexer);
2723 /* Recognize the `this' keyword. */
2725 cp_lexer_consume_token (parser->lexer);
2726 if (parser->local_variables_forbidden_p)
2728 error ("`this' may not be used in this context");
2729 return error_mark_node;
2731 return finish_this_expr ();
2733 /* The `operator' keyword can be the beginning of an
2738 case RID_FUNCTION_NAME:
2739 case RID_PRETTY_FUNCTION_NAME:
2740 case RID_C99_FUNCTION_NAME:
2741 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
2742 __func__ are the names of variables -- but they are
2743 treated specially. Therefore, they are handled here,
2744 rather than relying on the generic id-expression logic
2745 below. Gramatically, these names are id-expressions.
2747 Consume the token. */
2748 token = cp_lexer_consume_token (parser->lexer);
2749 /* Look up the name. */
2750 return finish_fname (token->value);
2757 /* The `__builtin_va_arg' construct is used to handle
2758 `va_arg'. Consume the `__builtin_va_arg' token. */
2759 cp_lexer_consume_token (parser->lexer);
2760 /* Look for the opening `('. */
2761 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
2762 /* Now, parse the assignment-expression. */
2763 expression = cp_parser_assignment_expression (parser);
2764 /* Look for the `,'. */
2765 cp_parser_require (parser, CPP_COMMA, "`,'");
2766 /* Parse the type-id. */
2767 type = cp_parser_type_id (parser);
2768 /* Look for the closing `)'. */
2769 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
2771 return build_x_va_arg (expression, type);
2775 cp_parser_error (parser, "expected primary-expression");
2776 return error_mark_node;
2780 /* An id-expression can start with either an identifier, a
2781 `::' as the beginning of a qualified-id, or the "operator"
2785 case CPP_TEMPLATE_ID:
2786 case CPP_NESTED_NAME_SPECIFIER:
2792 /* Parse the id-expression. */
2794 = cp_parser_id_expression (parser,
2795 /*template_keyword_p=*/false,
2796 /*check_dependency_p=*/true,
2797 /*template_p=*/NULL);
2798 if (id_expression == error_mark_node)
2799 return error_mark_node;
2800 /* If we have a template-id, then no further lookup is
2801 required. If the template-id was for a template-class, we
2802 will sometimes have a TYPE_DECL at this point. */
2803 else if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
2804 || TREE_CODE (id_expression) == TYPE_DECL)
2805 decl = id_expression;
2806 /* Look up the name. */
2809 decl = cp_parser_lookup_name_simple (parser, id_expression);
2810 /* If name lookup gives us a SCOPE_REF, then the
2811 qualifying scope was dependent. Just propagate the
2813 if (TREE_CODE (decl) == SCOPE_REF)
2815 if (TYPE_P (TREE_OPERAND (decl, 0)))
2816 *qualifying_class = TREE_OPERAND (decl, 0);
2819 /* Check to see if DECL is a local variable in a context
2820 where that is forbidden. */
2821 if (parser->local_variables_forbidden_p
2822 && local_variable_p (decl))
2824 /* It might be that we only found DECL because we are
2825 trying to be generous with pre-ISO scoping rules.
2826 For example, consider:
2830 for (int i = 0; i < 10; ++i) {}
2831 extern void f(int j = i);
2834 Here, name look up will originally find the out
2835 of scope `i'. We need to issue a warning message,
2836 but then use the global `i'. */
2837 decl = check_for_out_of_scope_variable (decl);
2838 if (local_variable_p (decl))
2840 error ("local variable `%D' may not appear in this context",
2842 return error_mark_node;
2846 /* If unqualified name lookup fails while processing a
2847 template, that just means that we need to do name
2848 lookup again when the template is instantiated. */
2850 && decl == error_mark_node
2851 && processing_template_decl)
2853 *idk = CP_PARSER_ID_KIND_UNQUALIFIED;
2854 return build_min_nt (LOOKUP_EXPR, id_expression);
2856 else if (decl == error_mark_node
2857 && !processing_template_decl)
2861 /* It may be resolvable as a koenig lookup function
2863 *idk = CP_PARSER_ID_KIND_UNQUALIFIED;
2864 return id_expression;
2866 else if (TYPE_P (parser->scope)
2867 && !COMPLETE_TYPE_P (parser->scope))
2868 error ("incomplete type `%T' used in nested name specifier",
2870 else if (parser->scope != global_namespace)
2871 error ("`%D' is not a member of `%D'",
2872 id_expression, parser->scope);
2874 error ("`::%D' has not been declared", id_expression);
2876 /* If DECL is a variable would be out of scope under
2877 ANSI/ISO rules, but in scope in the ARM, name lookup
2878 will succeed. Issue a diagnostic here. */
2880 decl = check_for_out_of_scope_variable (decl);
2882 /* Remember that the name was used in the definition of
2883 the current class so that we can check later to see if
2884 the meaning would have been different after the class
2885 was entirely defined. */
2886 if (!parser->scope && decl != error_mark_node)
2887 maybe_note_name_used_in_class (id_expression, decl);
2890 /* If we didn't find anything, or what we found was a type,
2891 then this wasn't really an id-expression. */
2892 if (TREE_CODE (decl) == TYPE_DECL
2893 || TREE_CODE (decl) == NAMESPACE_DECL
2894 || (TREE_CODE (decl) == TEMPLATE_DECL
2895 && !DECL_FUNCTION_TEMPLATE_P (decl)))
2897 cp_parser_error (parser,
2898 "expected primary-expression");
2899 return error_mark_node;
2902 /* If the name resolved to a template parameter, there is no
2903 need to look it up again later. Similarly, we resolve
2904 enumeration constants to their underlying values. */
2905 if (TREE_CODE (decl) == CONST_DECL)
2907 *idk = CP_PARSER_ID_KIND_NONE;
2908 if (DECL_TEMPLATE_PARM_P (decl) || !processing_template_decl)
2909 return DECL_INITIAL (decl);
2916 /* If the declaration was explicitly qualified indicate
2917 that. The semantics of `A::f(3)' are different than
2918 `f(3)' if `f' is virtual. */
2919 *idk = (parser->scope
2920 ? CP_PARSER_ID_KIND_QUALIFIED
2921 : (TREE_CODE (decl) == TEMPLATE_ID_EXPR
2922 ? CP_PARSER_ID_KIND_TEMPLATE_ID
2923 : CP_PARSER_ID_KIND_UNQUALIFIED));
2928 An id-expression is type-dependent if it contains an
2929 identifier that was declared with a dependent type.
2931 As an optimization, we could choose not to create a
2932 LOOKUP_EXPR for a name that resolved to a local
2933 variable in the template function that we are currently
2934 declaring; such a name cannot ever resolve to anything
2935 else. If we did that we would not have to look up
2936 these names at instantiation time.
2938 The standard is not very specific about an
2939 id-expression that names a set of overloaded functions.
2940 What if some of them have dependent types and some of
2941 them do not? Presumably, such a name should be treated
2942 as a dependent name. */
2943 /* Assume the name is not dependent. */
2944 dependent_p = false;
2945 if (!processing_template_decl)
2946 /* No names are dependent outside a template. */
2948 /* A template-id where the name of the template was not
2949 resolved is definitely dependent. */
2950 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
2951 && (TREE_CODE (TREE_OPERAND (decl, 0))
2952 == IDENTIFIER_NODE))
2954 /* For anything except an overloaded function, just check
2956 else if (!is_overloaded_fn (decl))
2958 = cp_parser_dependent_type_p (TREE_TYPE (decl));
2959 /* For a set of overloaded functions, check each of the
2965 if (BASELINK_P (fns))
2966 fns = BASELINK_FUNCTIONS (fns);
2968 /* For a template-id, check to see if the template
2969 arguments are dependent. */
2970 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
2972 tree args = TREE_OPERAND (fns, 1);
2974 if (args && TREE_CODE (args) == TREE_LIST)
2978 if (cp_parser_dependent_template_arg_p
2979 (TREE_VALUE (args)))
2984 args = TREE_CHAIN (args);
2987 else if (args && TREE_CODE (args) == TREE_VEC)
2990 for (i = 0; i < TREE_VEC_LENGTH (args); ++i)
2991 if (cp_parser_dependent_template_arg_p
2992 (TREE_VEC_ELT (args, i)))
2999 /* The functions are those referred to by the
3001 fns = TREE_OPERAND (fns, 0);
3004 /* If there are no dependent template arguments, go
3005 through the overlaoded functions. */
3006 while (fns && !dependent_p)
3008 tree fn = OVL_CURRENT (fns);
3010 /* Member functions of dependent classes are
3012 if (TREE_CODE (fn) == FUNCTION_DECL
3013 && cp_parser_type_dependent_expression_p (fn))
3015 else if (TREE_CODE (fn) == TEMPLATE_DECL
3016 && cp_parser_dependent_template_p (fn))
3019 fns = OVL_NEXT (fns);
3023 /* If the name was dependent on a template parameter,
3024 we will resolve the name at instantiation time. */
3027 /* Create a SCOPE_REF for qualified names. */
3030 if (TYPE_P (parser->scope))
3031 *qualifying_class = parser->scope;
3032 return build_nt (SCOPE_REF,
3036 /* A TEMPLATE_ID already contains all the information
3038 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR)
3039 return id_expression;
3040 /* Create a LOOKUP_EXPR for other unqualified names. */
3041 return build_min_nt (LOOKUP_EXPR, id_expression);
3046 decl = (adjust_result_of_qualified_name_lookup
3047 (decl, parser->scope, current_class_type));
3048 if (TREE_CODE (decl) == FIELD_DECL || BASELINK_P (decl))
3049 *qualifying_class = parser->scope;
3052 /* Transform references to non-static data members into
3054 decl = hack_identifier (decl, id_expression);
3056 /* Resolve references to variables of anonymous unions
3057 into COMPONENT_REFs. */
3058 if (TREE_CODE (decl) == ALIAS_DECL)
3059 decl = DECL_INITIAL (decl);
3062 if (TREE_DEPRECATED (decl))
3063 warn_deprecated_use (decl);
3068 /* Anything else is an error. */
3070 cp_parser_error (parser, "expected primary-expression");
3071 return error_mark_node;
3075 /* Parse an id-expression.
3082 :: [opt] nested-name-specifier template [opt] unqualified-id
3084 :: operator-function-id
3087 Return a representation of the unqualified portion of the
3088 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
3089 a `::' or nested-name-specifier.
3091 Often, if the id-expression was a qualified-id, the caller will
3092 want to make a SCOPE_REF to represent the qualified-id. This
3093 function does not do this in order to avoid wastefully creating
3094 SCOPE_REFs when they are not required.
3096 If TEMPLATE_KEYWORD_P is true, then we have just seen the
3099 If CHECK_DEPENDENCY_P is false, then names are looked up inside
3100 uninstantiated templates.
3102 If *TEMPLATE_KEYWORD_P is non-NULL, it is set to true iff the
3103 `template' keyword is used to explicitly indicate that the entity
3104 named is a template. */
3107 cp_parser_id_expression (cp_parser *parser,
3108 bool template_keyword_p,
3109 bool check_dependency_p,
3112 bool global_scope_p;
3113 bool nested_name_specifier_p;
3115 /* Assume the `template' keyword was not used. */
3117 *template_p = false;
3119 /* Look for the optional `::' operator. */
3121 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
3123 /* Look for the optional nested-name-specifier. */
3124 nested_name_specifier_p
3125 = (cp_parser_nested_name_specifier_opt (parser,
3126 /*typename_keyword_p=*/false,
3130 /* If there is a nested-name-specifier, then we are looking at
3131 the first qualified-id production. */
3132 if (nested_name_specifier_p)
3135 tree saved_object_scope;
3136 tree saved_qualifying_scope;
3137 tree unqualified_id;
3140 /* See if the next token is the `template' keyword. */
3142 template_p = &is_template;
3143 *template_p = cp_parser_optional_template_keyword (parser);
3144 /* Name lookup we do during the processing of the
3145 unqualified-id might obliterate SCOPE. */
3146 saved_scope = parser->scope;
3147 saved_object_scope = parser->object_scope;
3148 saved_qualifying_scope = parser->qualifying_scope;
3149 /* Process the final unqualified-id. */
3150 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
3151 check_dependency_p);
3152 /* Restore the SAVED_SCOPE for our caller. */
3153 parser->scope = saved_scope;
3154 parser->object_scope = saved_object_scope;
3155 parser->qualifying_scope = saved_qualifying_scope;
3157 return unqualified_id;
3159 /* Otherwise, if we are in global scope, then we are looking at one
3160 of the other qualified-id productions. */
3161 else if (global_scope_p)
3166 /* Peek at the next token. */
3167 token = cp_lexer_peek_token (parser->lexer);
3169 /* If it's an identifier, and the next token is not a "<", then
3170 we can avoid the template-id case. This is an optimization
3171 for this common case. */
3172 if (token->type == CPP_NAME
3173 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_LESS)
3174 return cp_parser_identifier (parser);
3176 cp_parser_parse_tentatively (parser);
3177 /* Try a template-id. */
3178 id = cp_parser_template_id (parser,
3179 /*template_keyword_p=*/false,
3180 /*check_dependency_p=*/true);
3181 /* If that worked, we're done. */
3182 if (cp_parser_parse_definitely (parser))
3185 /* Peek at the next token. (Changes in the token buffer may
3186 have invalidated the pointer obtained above.) */
3187 token = cp_lexer_peek_token (parser->lexer);
3189 switch (token->type)
3192 return cp_parser_identifier (parser);
3195 if (token->keyword == RID_OPERATOR)
3196 return cp_parser_operator_function_id (parser);
3200 cp_parser_error (parser, "expected id-expression");
3201 return error_mark_node;
3205 return cp_parser_unqualified_id (parser, template_keyword_p,
3206 /*check_dependency_p=*/true);
3209 /* Parse an unqualified-id.
3213 operator-function-id
3214 conversion-function-id
3218 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3219 keyword, in a construct like `A::template ...'.
3221 Returns a representation of unqualified-id. For the `identifier'
3222 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
3223 production a BIT_NOT_EXPR is returned; the operand of the
3224 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
3225 other productions, see the documentation accompanying the
3226 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
3227 names are looked up in uninstantiated templates. */
3230 cp_parser_unqualified_id (parser, template_keyword_p,
3233 bool template_keyword_p;
3234 bool check_dependency_p;
3238 /* Peek at the next token. */
3239 token = cp_lexer_peek_token (parser->lexer);
3241 switch (token->type)
3247 /* We don't know yet whether or not this will be a
3249 cp_parser_parse_tentatively (parser);
3250 /* Try a template-id. */
3251 id = cp_parser_template_id (parser, template_keyword_p,
3252 check_dependency_p);
3253 /* If it worked, we're done. */
3254 if (cp_parser_parse_definitely (parser))
3256 /* Otherwise, it's an ordinary identifier. */
3257 return cp_parser_identifier (parser);
3260 case CPP_TEMPLATE_ID:
3261 return cp_parser_template_id (parser, template_keyword_p,
3262 check_dependency_p);
3267 tree qualifying_scope;
3271 /* Consume the `~' token. */
3272 cp_lexer_consume_token (parser->lexer);
3273 /* Parse the class-name. The standard, as written, seems to
3276 template <typename T> struct S { ~S (); };
3277 template <typename T> S<T>::~S() {}
3279 is invalid, since `~' must be followed by a class-name, but
3280 `S<T>' is dependent, and so not known to be a class.
3281 That's not right; we need to look in uninstantiated
3282 templates. A further complication arises from:
3284 template <typename T> void f(T t) {
3288 Here, it is not possible to look up `T' in the scope of `T'
3289 itself. We must look in both the current scope, and the
3290 scope of the containing complete expression.
3292 Yet another issue is:
3301 The standard does not seem to say that the `S' in `~S'
3302 should refer to the type `S' and not the data member
3305 /* DR 244 says that we look up the name after the "~" in the
3306 same scope as we looked up the qualifying name. That idea
3307 isn't fully worked out; it's more complicated than that. */
3308 scope = parser->scope;
3309 object_scope = parser->object_scope;
3310 qualifying_scope = parser->qualifying_scope;
3312 /* If the name is of the form "X::~X" it's OK. */
3313 if (scope && TYPE_P (scope)
3314 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
3315 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3317 && (cp_lexer_peek_token (parser->lexer)->value
3318 == TYPE_IDENTIFIER (scope)))
3320 cp_lexer_consume_token (parser->lexer);
3321 return build_nt (BIT_NOT_EXPR, scope);
3324 /* If there was an explicit qualification (S::~T), first look
3325 in the scope given by the qualification (i.e., S). */
3328 cp_parser_parse_tentatively (parser);
3329 type_decl = cp_parser_class_name (parser,
3330 /*typename_keyword_p=*/false,
3331 /*template_keyword_p=*/false,
3333 /*check_access_p=*/true,
3334 /*check_dependency=*/false,
3335 /*class_head_p=*/false);
3336 if (cp_parser_parse_definitely (parser))
3337 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3339 /* In "N::S::~S", look in "N" as well. */
3340 if (scope && qualifying_scope)
3342 cp_parser_parse_tentatively (parser);
3343 parser->scope = qualifying_scope;
3344 parser->object_scope = NULL_TREE;
3345 parser->qualifying_scope = NULL_TREE;
3347 = cp_parser_class_name (parser,
3348 /*typename_keyword_p=*/false,
3349 /*template_keyword_p=*/false,
3351 /*check_access_p=*/true,
3352 /*check_dependency=*/false,
3353 /*class_head_p=*/false);
3354 if (cp_parser_parse_definitely (parser))
3355 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3357 /* In "p->S::~T", look in the scope given by "*p" as well. */
3358 else if (object_scope)
3360 cp_parser_parse_tentatively (parser);
3361 parser->scope = object_scope;
3362 parser->object_scope = NULL_TREE;
3363 parser->qualifying_scope = NULL_TREE;
3365 = cp_parser_class_name (parser,
3366 /*typename_keyword_p=*/false,
3367 /*template_keyword_p=*/false,
3369 /*check_access_p=*/true,
3370 /*check_dependency=*/false,
3371 /*class_head_p=*/false);
3372 if (cp_parser_parse_definitely (parser))
3373 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3375 /* Look in the surrounding context. */
3376 parser->scope = NULL_TREE;
3377 parser->object_scope = NULL_TREE;
3378 parser->qualifying_scope = NULL_TREE;
3380 = cp_parser_class_name (parser,
3381 /*typename_keyword_p=*/false,
3382 /*template_keyword_p=*/false,
3384 /*check_access_p=*/true,
3385 /*check_dependency=*/false,
3386 /*class_head_p=*/false);
3387 /* If an error occurred, assume that the name of the
3388 destructor is the same as the name of the qualifying
3389 class. That allows us to keep parsing after running
3390 into ill-formed destructor names. */
3391 if (type_decl == error_mark_node && scope && TYPE_P (scope))
3392 return build_nt (BIT_NOT_EXPR, scope);
3393 else if (type_decl == error_mark_node)
3394 return error_mark_node;
3396 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3400 if (token->keyword == RID_OPERATOR)
3404 /* This could be a template-id, so we try that first. */
3405 cp_parser_parse_tentatively (parser);
3406 /* Try a template-id. */
3407 id = cp_parser_template_id (parser, template_keyword_p,
3408 /*check_dependency_p=*/true);
3409 /* If that worked, we're done. */
3410 if (cp_parser_parse_definitely (parser))
3412 /* We still don't know whether we're looking at an
3413 operator-function-id or a conversion-function-id. */
3414 cp_parser_parse_tentatively (parser);
3415 /* Try an operator-function-id. */
3416 id = cp_parser_operator_function_id (parser);
3417 /* If that didn't work, try a conversion-function-id. */
3418 if (!cp_parser_parse_definitely (parser))
3419 id = cp_parser_conversion_function_id (parser);
3426 cp_parser_error (parser, "expected unqualified-id");
3427 return error_mark_node;
3431 /* Parse an (optional) nested-name-specifier.
3433 nested-name-specifier:
3434 class-or-namespace-name :: nested-name-specifier [opt]
3435 class-or-namespace-name :: template nested-name-specifier [opt]
3437 PARSER->SCOPE should be set appropriately before this function is
3438 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
3439 effect. TYPE_P is TRUE if we non-type bindings should be ignored
3442 Sets PARSER->SCOPE to the class (TYPE) or namespace
3443 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
3444 it unchanged if there is no nested-name-specifier. Returns the new
3445 scope iff there is a nested-name-specifier, or NULL_TREE otherwise. */
3448 cp_parser_nested_name_specifier_opt (cp_parser *parser,
3449 bool typename_keyword_p,
3450 bool check_dependency_p,
3453 bool success = false;
3454 tree access_check = NULL_TREE;
3458 /* If the next token corresponds to a nested name specifier, there
3459 is no need to reparse it. However, if CHECK_DEPENDENCY_P is
3460 false, it may have been true before, in which case something
3461 like `A<X>::B<Y>::C' may have resulted in a nested-name-specifier
3462 of `A<X>::', where it should now be `A<X>::B<Y>::'. So, when
3463 CHECK_DEPENDENCY_P is false, we have to fall through into the
3465 if (check_dependency_p
3466 && cp_lexer_next_token_is (parser->lexer, CPP_NESTED_NAME_SPECIFIER))
3468 cp_parser_pre_parsed_nested_name_specifier (parser);
3469 return parser->scope;
3472 /* Remember where the nested-name-specifier starts. */
3473 if (cp_parser_parsing_tentatively (parser)
3474 && !cp_parser_committed_to_tentative_parse (parser))
3476 token = cp_lexer_peek_token (parser->lexer);
3477 start = cp_lexer_token_difference (parser->lexer,
3478 parser->lexer->first_token,
3480 access_check = parser->context->deferred_access_checks;
3489 tree saved_qualifying_scope;
3490 bool template_keyword_p;
3492 /* Spot cases that cannot be the beginning of a
3493 nested-name-specifier. */
3494 token = cp_lexer_peek_token (parser->lexer);
3496 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
3497 the already parsed nested-name-specifier. */
3498 if (token->type == CPP_NESTED_NAME_SPECIFIER)
3500 /* Grab the nested-name-specifier and continue the loop. */
3501 cp_parser_pre_parsed_nested_name_specifier (parser);
3506 /* Spot cases that cannot be the beginning of a
3507 nested-name-specifier. On the second and subsequent times
3508 through the loop, we look for the `template' keyword. */
3509 if (success && token->keyword == RID_TEMPLATE)
3511 /* A template-id can start a nested-name-specifier. */
3512 else if (token->type == CPP_TEMPLATE_ID)
3516 /* If the next token is not an identifier, then it is
3517 definitely not a class-or-namespace-name. */
3518 if (token->type != CPP_NAME)
3520 /* If the following token is neither a `<' (to begin a
3521 template-id), nor a `::', then we are not looking at a
3522 nested-name-specifier. */
3523 token = cp_lexer_peek_nth_token (parser->lexer, 2);
3524 if (token->type != CPP_LESS && token->type != CPP_SCOPE)
3528 /* The nested-name-specifier is optional, so we parse
3530 cp_parser_parse_tentatively (parser);
3532 /* Look for the optional `template' keyword, if this isn't the
3533 first time through the loop. */
3535 template_keyword_p = cp_parser_optional_template_keyword (parser);
3537 template_keyword_p = false;
3539 /* Save the old scope since the name lookup we are about to do
3540 might destroy it. */
3541 old_scope = parser->scope;
3542 saved_qualifying_scope = parser->qualifying_scope;
3543 /* Parse the qualifying entity. */
3545 = cp_parser_class_or_namespace_name (parser,
3550 /* Look for the `::' token. */
3551 cp_parser_require (parser, CPP_SCOPE, "`::'");
3553 /* If we found what we wanted, we keep going; otherwise, we're
3555 if (!cp_parser_parse_definitely (parser))
3557 bool error_p = false;
3559 /* Restore the OLD_SCOPE since it was valid before the
3560 failed attempt at finding the last
3561 class-or-namespace-name. */
3562 parser->scope = old_scope;
3563 parser->qualifying_scope = saved_qualifying_scope;
3564 /* If the next token is an identifier, and the one after
3565 that is a `::', then any valid interpretation would have
3566 found a class-or-namespace-name. */
3567 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
3568 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3570 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
3573 token = cp_lexer_consume_token (parser->lexer);
3578 decl = cp_parser_lookup_name_simple (parser, token->value);
3579 if (TREE_CODE (decl) == TEMPLATE_DECL)
3580 error ("`%D' used without template parameters",
3582 else if (parser->scope)
3584 if (TYPE_P (parser->scope))
3585 error ("`%T::%D' is not a class-name or "
3587 parser->scope, token->value);
3589 error ("`%D::%D' is not a class-name or "
3591 parser->scope, token->value);
3594 error ("`%D' is not a class-name or namespace-name",
3596 parser->scope = NULL_TREE;
3598 /* Treat this as a successful nested-name-specifier
3603 If the name found is not a class-name (clause
3604 _class_) or namespace-name (_namespace.def_), the
3605 program is ill-formed. */
3608 cp_lexer_consume_token (parser->lexer);
3613 /* We've found one valid nested-name-specifier. */
3615 /* Make sure we look in the right scope the next time through
3617 parser->scope = (TREE_CODE (new_scope) == TYPE_DECL
3618 ? TREE_TYPE (new_scope)
3620 /* If it is a class scope, try to complete it; we are about to
3621 be looking up names inside the class. */
3622 if (TYPE_P (parser->scope))
3623 complete_type (parser->scope);
3626 /* If parsing tentatively, replace the sequence of tokens that makes
3627 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
3628 token. That way, should we re-parse the token stream, we will
3629 not have to repeat the effort required to do the parse, nor will
3630 we issue duplicate error messages. */
3631 if (success && start >= 0)
3635 /* Find the token that corresponds to the start of the
3637 token = cp_lexer_advance_token (parser->lexer,
3638 parser->lexer->first_token,
3641 /* Remember the access checks associated with this
3642 nested-name-specifier. */
3643 c = parser->context->deferred_access_checks;
3644 if (c == access_check)
3645 access_check = NULL_TREE;
3648 while (TREE_CHAIN (c) != access_check)
3650 access_check = parser->context->deferred_access_checks;
3651 parser->context->deferred_access_checks = TREE_CHAIN (c);
3652 TREE_CHAIN (c) = NULL_TREE;
3655 /* Reset the contents of the START token. */
3656 token->type = CPP_NESTED_NAME_SPECIFIER;
3657 token->value = build_tree_list (access_check, parser->scope);
3658 TREE_TYPE (token->value) = parser->qualifying_scope;
3659 token->keyword = RID_MAX;
3660 /* Purge all subsequent tokens. */
3661 cp_lexer_purge_tokens_after (parser->lexer, token);
3664 return success ? parser->scope : NULL_TREE;
3667 /* Parse a nested-name-specifier. See
3668 cp_parser_nested_name_specifier_opt for details. This function
3669 behaves identically, except that it will an issue an error if no
3670 nested-name-specifier is present, and it will return
3671 ERROR_MARK_NODE, rather than NULL_TREE, if no nested-name-specifier
3675 cp_parser_nested_name_specifier (cp_parser *parser,
3676 bool typename_keyword_p,
3677 bool check_dependency_p,
3682 /* Look for the nested-name-specifier. */
3683 scope = cp_parser_nested_name_specifier_opt (parser,
3687 /* If it was not present, issue an error message. */
3690 cp_parser_error (parser, "expected nested-name-specifier");
3691 return error_mark_node;
3697 /* Parse a class-or-namespace-name.
3699 class-or-namespace-name:
3703 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
3704 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
3705 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
3706 TYPE_P is TRUE iff the next name should be taken as a class-name,
3707 even the same name is declared to be another entity in the same
3710 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
3711 specified by the class-or-namespace-name. If neither is found the
3712 ERROR_MARK_NODE is returned. */
3715 cp_parser_class_or_namespace_name (cp_parser *parser,
3716 bool typename_keyword_p,
3717 bool template_keyword_p,
3718 bool check_dependency_p,
3722 tree saved_qualifying_scope;
3723 tree saved_object_scope;
3727 /* If the next token is the `template' keyword, we know that we are
3728 looking at a class-name. */
3729 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
3730 return cp_parser_class_name (parser,
3734 /*check_access_p=*/true,
3736 /*class_head_p=*/false);
3737 /* Before we try to parse the class-name, we must save away the
3738 current PARSER->SCOPE since cp_parser_class_name will destroy
3740 saved_scope = parser->scope;
3741 saved_qualifying_scope = parser->qualifying_scope;
3742 saved_object_scope = parser->object_scope;
3743 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
3744 there is no need to look for a namespace-name. */
3745 only_class_p = saved_scope && TYPE_P (saved_scope);
3747 cp_parser_parse_tentatively (parser);
3748 scope = cp_parser_class_name (parser,
3752 /*check_access_p=*/true,
3754 /*class_head_p=*/false);
3755 /* If that didn't work, try for a namespace-name. */
3756 if (!only_class_p && !cp_parser_parse_definitely (parser))
3758 /* Restore the saved scope. */
3759 parser->scope = saved_scope;
3760 parser->qualifying_scope = saved_qualifying_scope;
3761 parser->object_scope = saved_object_scope;
3762 /* If we are not looking at an identifier followed by the scope
3763 resolution operator, then this is not part of a
3764 nested-name-specifier. (Note that this function is only used
3765 to parse the components of a nested-name-specifier.) */
3766 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
3767 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
3768 return error_mark_node;
3769 scope = cp_parser_namespace_name (parser);
3775 /* Parse a postfix-expression.
3779 postfix-expression [ expression ]
3780 postfix-expression ( expression-list [opt] )
3781 simple-type-specifier ( expression-list [opt] )
3782 typename :: [opt] nested-name-specifier identifier
3783 ( expression-list [opt] )
3784 typename :: [opt] nested-name-specifier template [opt] template-id
3785 ( expression-list [opt] )
3786 postfix-expression . template [opt] id-expression
3787 postfix-expression -> template [opt] id-expression
3788 postfix-expression . pseudo-destructor-name
3789 postfix-expression -> pseudo-destructor-name
3790 postfix-expression ++
3791 postfix-expression --
3792 dynamic_cast < type-id > ( expression )
3793 static_cast < type-id > ( expression )
3794 reinterpret_cast < type-id > ( expression )
3795 const_cast < type-id > ( expression )
3796 typeid ( expression )
3802 ( type-id ) { initializer-list , [opt] }
3804 This extension is a GNU version of the C99 compound-literal
3805 construct. (The C99 grammar uses `type-name' instead of `type-id',
3806 but they are essentially the same concept.)
3808 If ADDRESS_P is true, the postfix expression is the operand of the
3811 Returns a representation of the expression. */
3814 cp_parser_postfix_expression (cp_parser *parser, bool address_p)
3818 cp_parser_id_kind idk = CP_PARSER_ID_KIND_NONE;
3819 tree postfix_expression = NULL_TREE;
3820 /* Non-NULL only if the current postfix-expression can be used to
3821 form a pointer-to-member. In that case, QUALIFYING_CLASS is the
3822 class used to qualify the member. */
3823 tree qualifying_class = NULL_TREE;
3826 /* Peek at the next token. */
3827 token = cp_lexer_peek_token (parser->lexer);
3828 /* Some of the productions are determined by keywords. */
3829 keyword = token->keyword;
3839 const char *saved_message;
3841 /* All of these can be handled in the same way from the point
3842 of view of parsing. Begin by consuming the token
3843 identifying the cast. */
3844 cp_lexer_consume_token (parser->lexer);
3846 /* New types cannot be defined in the cast. */
3847 saved_message = parser->type_definition_forbidden_message;
3848 parser->type_definition_forbidden_message
3849 = "types may not be defined in casts";
3851 /* Look for the opening `<'. */
3852 cp_parser_require (parser, CPP_LESS, "`<'");
3853 /* Parse the type to which we are casting. */
3854 type = cp_parser_type_id (parser);
3855 /* Look for the closing `>'. */
3856 cp_parser_require (parser, CPP_GREATER, "`>'");
3857 /* Restore the old message. */
3858 parser->type_definition_forbidden_message = saved_message;
3860 /* And the expression which is being cast. */
3861 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3862 expression = cp_parser_expression (parser);
3863 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3869 = build_dynamic_cast (type, expression);
3873 = build_static_cast (type, expression);
3877 = build_reinterpret_cast (type, expression);
3881 = build_const_cast (type, expression);
3892 const char *saved_message;
3894 /* Consume the `typeid' token. */
3895 cp_lexer_consume_token (parser->lexer);
3896 /* Look for the `(' token. */
3897 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3898 /* Types cannot be defined in a `typeid' expression. */
3899 saved_message = parser->type_definition_forbidden_message;
3900 parser->type_definition_forbidden_message
3901 = "types may not be defined in a `typeid\' expression";
3902 /* We can't be sure yet whether we're looking at a type-id or an
3904 cp_parser_parse_tentatively (parser);
3905 /* Try a type-id first. */
3906 type = cp_parser_type_id (parser);
3907 /* Look for the `)' token. Otherwise, we can't be sure that
3908 we're not looking at an expression: consider `typeid (int
3909 (3))', for example. */
3910 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3911 /* If all went well, simply lookup the type-id. */
3912 if (cp_parser_parse_definitely (parser))
3913 postfix_expression = get_typeid (type);
3914 /* Otherwise, fall back to the expression variant. */
3919 /* Look for an expression. */
3920 expression = cp_parser_expression (parser);
3921 /* Compute its typeid. */
3922 postfix_expression = build_typeid (expression);
3923 /* Look for the `)' token. */
3924 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3927 /* Restore the saved message. */
3928 parser->type_definition_forbidden_message = saved_message;
3934 bool template_p = false;
3938 /* Consume the `typename' token. */
3939 cp_lexer_consume_token (parser->lexer);
3940 /* Look for the optional `::' operator. */
3941 cp_parser_global_scope_opt (parser,
3942 /*current_scope_valid_p=*/false);
3943 /* Look for the nested-name-specifier. */
3944 cp_parser_nested_name_specifier (parser,
3945 /*typename_keyword_p=*/true,
3946 /*check_dependency_p=*/true,
3948 /* Look for the optional `template' keyword. */
3949 template_p = cp_parser_optional_template_keyword (parser);
3950 /* We don't know whether we're looking at a template-id or an
3952 cp_parser_parse_tentatively (parser);
3953 /* Try a template-id. */
3954 id = cp_parser_template_id (parser, template_p,
3955 /*check_dependency_p=*/true);
3956 /* If that didn't work, try an identifier. */
3957 if (!cp_parser_parse_definitely (parser))
3958 id = cp_parser_identifier (parser);
3959 /* Create a TYPENAME_TYPE to represent the type to which the
3960 functional cast is being performed. */
3961 type = make_typename_type (parser->scope, id,
3964 postfix_expression = cp_parser_functional_cast (parser, type);
3972 /* If the next thing is a simple-type-specifier, we may be
3973 looking at a functional cast. We could also be looking at
3974 an id-expression. So, we try the functional cast, and if
3975 that doesn't work we fall back to the primary-expression. */
3976 cp_parser_parse_tentatively (parser);
3977 /* Look for the simple-type-specifier. */
3978 type = cp_parser_simple_type_specifier (parser,
3979 CP_PARSER_FLAGS_NONE);
3980 /* Parse the cast itself. */
3981 if (!cp_parser_error_occurred (parser))
3983 = cp_parser_functional_cast (parser, type);
3984 /* If that worked, we're done. */
3985 if (cp_parser_parse_definitely (parser))
3988 /* If the functional-cast didn't work out, try a
3989 compound-literal. */
3990 if (cp_parser_allow_gnu_extensions_p (parser))
3992 tree initializer_list = NULL_TREE;
3994 cp_parser_parse_tentatively (parser);
3995 /* Look for the `('. */
3996 if (cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
3998 type = cp_parser_type_id (parser);
3999 /* Look for the `)'. */
4000 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4001 /* Look for the `{'. */
4002 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
4003 /* If things aren't going well, there's no need to
4005 if (!cp_parser_error_occurred (parser))
4007 /* Parse the initializer-list. */
4009 = cp_parser_initializer_list (parser);
4010 /* Allow a trailing `,'. */
4011 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
4012 cp_lexer_consume_token (parser->lexer);
4013 /* Look for the final `}'. */
4014 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
4017 /* If that worked, we're definitely looking at a
4018 compound-literal expression. */
4019 if (cp_parser_parse_definitely (parser))
4021 /* Warn the user that a compound literal is not
4022 allowed in standard C++. */
4024 pedwarn ("ISO C++ forbids compound-literals");
4025 /* Form the representation of the compound-literal. */
4027 = finish_compound_literal (type, initializer_list);
4032 /* It must be a primary-expression. */
4033 postfix_expression = cp_parser_primary_expression (parser,
4040 /* Peek at the next token. */
4041 token = cp_lexer_peek_token (parser->lexer);
4042 done = (token->type != CPP_OPEN_SQUARE
4043 && token->type != CPP_OPEN_PAREN
4044 && token->type != CPP_DOT
4045 && token->type != CPP_DEREF
4046 && token->type != CPP_PLUS_PLUS
4047 && token->type != CPP_MINUS_MINUS);
4049 /* If the postfix expression is complete, finish up. */
4050 if (address_p && qualifying_class && done)
4052 if (TREE_CODE (postfix_expression) == SCOPE_REF)
4053 postfix_expression = TREE_OPERAND (postfix_expression, 1);
4055 = build_offset_ref (qualifying_class, postfix_expression);
4056 return postfix_expression;
4059 /* Otherwise, if we were avoiding committing until we knew
4060 whether or not we had a pointer-to-member, we now know that
4061 the expression is an ordinary reference to a qualified name. */
4062 if (qualifying_class && !processing_template_decl)
4064 if (TREE_CODE (postfix_expression) == FIELD_DECL)
4066 = finish_non_static_data_member (postfix_expression,
4068 else if (BASELINK_P (postfix_expression))
4073 /* See if any of the functions are non-static members. */
4074 fns = BASELINK_FUNCTIONS (postfix_expression);
4075 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
4076 fns = TREE_OPERAND (fns, 0);
4077 for (fn = fns; fn; fn = OVL_NEXT (fn))
4078 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn))
4080 /* If so, the expression may be relative to the current
4082 if (fn && current_class_type
4083 && DERIVED_FROM_P (qualifying_class, current_class_type))
4085 = (build_class_member_access_expr
4086 (maybe_dummy_object (qualifying_class, NULL),
4088 BASELINK_ACCESS_BINFO (postfix_expression),
4089 /*preserve_reference=*/false));
4091 return build_offset_ref (qualifying_class,
4092 postfix_expression);
4096 /* Remember that there was a reference to this entity. */
4097 if (DECL_P (postfix_expression))
4098 mark_used (postfix_expression);
4100 /* Keep looping until the postfix-expression is complete. */
4103 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE
4104 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
4106 /* It is not a Koenig lookup function call. */
4107 unqualified_name_lookup_error (postfix_expression);
4108 postfix_expression = error_mark_node;
4111 /* Peek at the next token. */
4112 token = cp_lexer_peek_token (parser->lexer);
4114 switch (token->type)
4116 case CPP_OPEN_SQUARE:
4117 /* postfix-expression [ expression ] */
4121 /* Consume the `[' token. */
4122 cp_lexer_consume_token (parser->lexer);
4123 /* Parse the index expression. */
4124 index = cp_parser_expression (parser);
4125 /* Look for the closing `]'. */
4126 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4128 /* Build the ARRAY_REF. */
4130 = grok_array_decl (postfix_expression, index);
4131 idk = CP_PARSER_ID_KIND_NONE;
4135 case CPP_OPEN_PAREN:
4136 /* postfix-expression ( expression-list [opt] ) */
4140 /* Consume the `(' token. */
4141 cp_lexer_consume_token (parser->lexer);
4142 /* If the next token is not a `)', then there are some
4144 if (cp_lexer_next_token_is_not (parser->lexer,
4146 args = cp_parser_expression_list (parser);
4149 /* Look for the closing `)'. */
4150 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4152 if (idk == CP_PARSER_ID_KIND_UNQUALIFIED
4153 && (is_overloaded_fn (postfix_expression)
4154 || DECL_P (postfix_expression)
4155 || TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4159 tree identifier = NULL_TREE;
4160 tree functions = NULL_TREE;
4162 /* Find the name of the overloaded function. */
4163 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4164 identifier = postfix_expression;
4165 else if (is_overloaded_fn (postfix_expression))
4167 functions = postfix_expression;
4168 identifier = DECL_NAME (get_first_fn (functions));
4170 else if (DECL_P (postfix_expression))
4172 functions = postfix_expression;
4173 identifier = DECL_NAME (postfix_expression);
4176 /* A call to a namespace-scope function using an
4179 Do Koenig lookup -- unless any of the arguments are
4181 for (arg = args; arg; arg = TREE_CHAIN (arg))
4182 if (cp_parser_type_dependent_expression_p (TREE_VALUE (arg)))
4187 = lookup_arg_dependent(identifier, functions, args);
4188 if (!postfix_expression)
4190 /* The unqualified name could not be resolved. */
4191 unqualified_name_lookup_error (identifier);
4192 postfix_expression = error_mark_node;
4195 = build_call_from_tree (postfix_expression, args,
4196 /*diallow_virtual=*/false);
4199 postfix_expression = build_min_nt (LOOKUP_EXPR,
4202 else if (idk == CP_PARSER_ID_KIND_UNQUALIFIED
4203 && TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4205 /* The unqualified name could not be resolved. */
4206 unqualified_name_lookup_error (postfix_expression);
4207 postfix_expression = error_mark_node;
4211 /* In the body of a template, no further processing is
4213 if (processing_template_decl)
4215 postfix_expression = build_nt (CALL_EXPR,
4221 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
4223 = (build_new_method_call
4224 (TREE_OPERAND (postfix_expression, 0),
4225 TREE_OPERAND (postfix_expression, 1),
4227 (idk == CP_PARSER_ID_KIND_QUALIFIED
4228 ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL)));
4229 else if (TREE_CODE (postfix_expression) == OFFSET_REF)
4230 postfix_expression = (build_offset_ref_call_from_tree
4231 (postfix_expression, args));
4232 else if (idk == CP_PARSER_ID_KIND_QUALIFIED)
4233 /* A call to a static class member, or a namespace-scope
4236 = finish_call_expr (postfix_expression, args,
4237 /*disallow_virtual=*/true);
4239 /* All other function calls. */
4241 = finish_call_expr (postfix_expression, args,
4242 /*disallow_virtual=*/false);
4244 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
4245 idk = CP_PARSER_ID_KIND_NONE;
4251 /* postfix-expression . template [opt] id-expression
4252 postfix-expression . pseudo-destructor-name
4253 postfix-expression -> template [opt] id-expression
4254 postfix-expression -> pseudo-destructor-name */
4259 tree scope = NULL_TREE;
4261 /* If this is a `->' operator, dereference the pointer. */
4262 if (token->type == CPP_DEREF)
4263 postfix_expression = build_x_arrow (postfix_expression);
4264 /* Check to see whether or not the expression is
4266 dependent_p = (cp_parser_type_dependent_expression_p
4267 (postfix_expression));
4268 /* The identifier following the `->' or `.' is not
4270 parser->scope = NULL_TREE;
4271 parser->qualifying_scope = NULL_TREE;
4272 parser->object_scope = NULL_TREE;
4273 /* Enter the scope corresponding to the type of the object
4274 given by the POSTFIX_EXPRESSION. */
4276 && TREE_TYPE (postfix_expression) != NULL_TREE)
4278 scope = TREE_TYPE (postfix_expression);
4279 /* According to the standard, no expression should
4280 ever have reference type. Unfortunately, we do not
4281 currently match the standard in this respect in
4282 that our internal representation of an expression
4283 may have reference type even when the standard says
4284 it does not. Therefore, we have to manually obtain
4285 the underlying type here. */
4286 if (TREE_CODE (scope) == REFERENCE_TYPE)
4287 scope = TREE_TYPE (scope);
4288 /* If the SCOPE is an OFFSET_TYPE, then we grab the
4289 type of the field. We get an OFFSET_TYPE for
4294 Probably, we should not get an OFFSET_TYPE here;
4295 that transformation should be made only if `&S::T'
4297 if (TREE_CODE (scope) == OFFSET_TYPE)
4298 scope = TREE_TYPE (scope);
4299 /* The type of the POSTFIX_EXPRESSION must be
4301 scope = complete_type_or_else (scope, NULL_TREE);
4302 /* Let the name lookup machinery know that we are
4303 processing a class member access expression. */
4304 parser->context->object_type = scope;
4305 /* If something went wrong, we want to be able to
4306 discern that case, as opposed to the case where
4307 there was no SCOPE due to the type of expression
4310 scope = error_mark_node;
4313 /* Consume the `.' or `->' operator. */
4314 cp_lexer_consume_token (parser->lexer);
4315 /* If the SCOPE is not a scalar type, we are looking at an
4316 ordinary class member access expression, rather than a
4317 pseudo-destructor-name. */
4318 if (!scope || !SCALAR_TYPE_P (scope))
4320 template_p = cp_parser_optional_template_keyword (parser);
4321 /* Parse the id-expression. */
4322 name = cp_parser_id_expression (parser,
4324 /*check_dependency_p=*/true,
4325 /*template_p=*/NULL);
4326 /* In general, build a SCOPE_REF if the member name is
4327 qualified. However, if the name was not dependent
4328 and has already been resolved; there is no need to
4329 build the SCOPE_REF. For example;
4331 struct X { void f(); };
4332 template <typename T> void f(T* t) { t->X::f(); }
4334 Even though "t" is dependent, "X::f" is not and has
4335 except that for a BASELINK there is no need to
4336 include scope information. */
4337 if (name != error_mark_node
4338 && !BASELINK_P (name)
4341 name = build_nt (SCOPE_REF, parser->scope, name);
4342 parser->scope = NULL_TREE;
4343 parser->qualifying_scope = NULL_TREE;
4344 parser->object_scope = NULL_TREE;
4347 = finish_class_member_access_expr (postfix_expression, name);
4349 /* Otherwise, try the pseudo-destructor-name production. */
4355 /* Parse the pseudo-destructor-name. */
4356 cp_parser_pseudo_destructor_name (parser, &s, &type);
4357 /* Form the call. */
4359 = finish_pseudo_destructor_expr (postfix_expression,
4360 s, TREE_TYPE (type));
4363 /* We no longer need to look up names in the scope of the
4364 object on the left-hand side of the `.' or `->'
4366 parser->context->object_type = NULL_TREE;
4367 idk = CP_PARSER_ID_KIND_NONE;
4372 /* postfix-expression ++ */
4373 /* Consume the `++' token. */
4374 cp_lexer_consume_token (parser->lexer);
4375 /* Generate a reprsentation for the complete expression. */
4377 = finish_increment_expr (postfix_expression,
4378 POSTINCREMENT_EXPR);
4379 idk = CP_PARSER_ID_KIND_NONE;
4382 case CPP_MINUS_MINUS:
4383 /* postfix-expression -- */
4384 /* Consume the `--' token. */
4385 cp_lexer_consume_token (parser->lexer);
4386 /* Generate a reprsentation for the complete expression. */
4388 = finish_increment_expr (postfix_expression,
4389 POSTDECREMENT_EXPR);
4390 idk = CP_PARSER_ID_KIND_NONE;
4394 return postfix_expression;
4398 /* We should never get here. */
4400 return error_mark_node;
4403 /* Parse an expression-list.
4406 assignment-expression
4407 expression-list, assignment-expression
4409 Returns a TREE_LIST. The TREE_VALUE of each node is a
4410 representation of an assignment-expression. Note that a TREE_LIST
4411 is returned even if there is only a single expression in the list. */
4414 cp_parser_expression_list (parser)
4417 tree expression_list = NULL_TREE;
4419 /* Consume expressions until there are no more. */
4424 /* Parse the next assignment-expression. */
4425 expr = cp_parser_assignment_expression (parser);
4426 /* Add it to the list. */
4427 expression_list = tree_cons (NULL_TREE, expr, expression_list);
4429 /* If the next token isn't a `,', then we are done. */
4430 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
4432 /* All uses of expression-list in the grammar are followed
4433 by a `)'. Therefore, if the next token is not a `)' an
4434 error will be issued, unless we are parsing tentatively.
4435 Skip ahead to see if there is another `,' before the `)';
4436 if so, we can go there and recover. */
4437 if (cp_parser_parsing_tentatively (parser)
4438 || cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
4439 || !cp_parser_skip_to_closing_parenthesis_or_comma (parser))
4443 /* Otherwise, consume the `,' and keep going. */
4444 cp_lexer_consume_token (parser->lexer);
4447 /* We built up the list in reverse order so we must reverse it now. */
4448 return nreverse (expression_list);
4451 /* Parse a pseudo-destructor-name.
4453 pseudo-destructor-name:
4454 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
4455 :: [opt] nested-name-specifier template template-id :: ~ type-name
4456 :: [opt] nested-name-specifier [opt] ~ type-name
4458 If either of the first two productions is used, sets *SCOPE to the
4459 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
4460 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
4461 or ERROR_MARK_NODE if no type-name is present. */
4464 cp_parser_pseudo_destructor_name (parser, scope, type)
4469 bool nested_name_specifier_p;
4471 /* Look for the optional `::' operator. */
4472 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
4473 /* Look for the optional nested-name-specifier. */
4474 nested_name_specifier_p
4475 = (cp_parser_nested_name_specifier_opt (parser,
4476 /*typename_keyword_p=*/false,
4477 /*check_dependency_p=*/true,
4480 /* Now, if we saw a nested-name-specifier, we might be doing the
4481 second production. */
4482 if (nested_name_specifier_p
4483 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
4485 /* Consume the `template' keyword. */
4486 cp_lexer_consume_token (parser->lexer);
4487 /* Parse the template-id. */
4488 cp_parser_template_id (parser,
4489 /*template_keyword_p=*/true,
4490 /*check_dependency_p=*/false);
4491 /* Look for the `::' token. */
4492 cp_parser_require (parser, CPP_SCOPE, "`::'");
4494 /* If the next token is not a `~', then there might be some
4495 additional qualification. */
4496 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
4498 /* Look for the type-name. */
4499 *scope = TREE_TYPE (cp_parser_type_name (parser));
4500 /* Look for the `::' token. */
4501 cp_parser_require (parser, CPP_SCOPE, "`::'");
4506 /* Look for the `~'. */
4507 cp_parser_require (parser, CPP_COMPL, "`~'");
4508 /* Look for the type-name again. We are not responsible for
4509 checking that it matches the first type-name. */
4510 *type = cp_parser_type_name (parser);
4513 /* Parse a unary-expression.
4519 unary-operator cast-expression
4520 sizeof unary-expression
4528 __extension__ cast-expression
4529 __alignof__ unary-expression
4530 __alignof__ ( type-id )
4531 __real__ cast-expression
4532 __imag__ cast-expression
4535 ADDRESS_P is true iff the unary-expression is appearing as the
4536 operand of the `&' operator.
4538 Returns a representation of the expresion. */
4541 cp_parser_unary_expression (cp_parser *parser, bool address_p)
4544 enum tree_code unary_operator;
4546 /* Peek at the next token. */
4547 token = cp_lexer_peek_token (parser->lexer);
4548 /* Some keywords give away the kind of expression. */
4549 if (token->type == CPP_KEYWORD)
4551 enum rid keyword = token->keyword;
4557 /* Consume the `alignof' token. */
4558 cp_lexer_consume_token (parser->lexer);
4559 /* Parse the operand. */
4560 return finish_alignof (cp_parser_sizeof_operand
4568 /* Consume the `sizeof' token. */
4569 cp_lexer_consume_token (parser->lexer);
4570 /* Parse the operand. */
4571 operand = cp_parser_sizeof_operand (parser, keyword);
4573 /* If the type of the operand cannot be determined build a
4575 if (TYPE_P (operand)
4576 ? cp_parser_dependent_type_p (operand)
4577 : cp_parser_type_dependent_expression_p (operand))
4578 return build_min (SIZEOF_EXPR, size_type_node, operand);
4579 /* Otherwise, compute the constant value. */
4581 return finish_sizeof (operand);
4585 return cp_parser_new_expression (parser);
4588 return cp_parser_delete_expression (parser);
4592 /* The saved value of the PEDANTIC flag. */
4596 /* Save away the PEDANTIC flag. */
4597 cp_parser_extension_opt (parser, &saved_pedantic);
4598 /* Parse the cast-expression. */
4599 expr = cp_parser_cast_expression (parser, /*address_p=*/false);
4600 /* Restore the PEDANTIC flag. */
4601 pedantic = saved_pedantic;
4611 /* Consume the `__real__' or `__imag__' token. */
4612 cp_lexer_consume_token (parser->lexer);
4613 /* Parse the cast-expression. */
4614 expression = cp_parser_cast_expression (parser,
4615 /*address_p=*/false);
4616 /* Create the complete representation. */
4617 return build_x_unary_op ((keyword == RID_REALPART
4618 ? REALPART_EXPR : IMAGPART_EXPR),
4628 /* Look for the `:: new' and `:: delete', which also signal the
4629 beginning of a new-expression, or delete-expression,
4630 respectively. If the next token is `::', then it might be one of
4632 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
4636 /* See if the token after the `::' is one of the keywords in
4637 which we're interested. */
4638 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
4639 /* If it's `new', we have a new-expression. */
4640 if (keyword == RID_NEW)
4641 return cp_parser_new_expression (parser);
4642 /* Similarly, for `delete'. */
4643 else if (keyword == RID_DELETE)
4644 return cp_parser_delete_expression (parser);
4647 /* Look for a unary operator. */
4648 unary_operator = cp_parser_unary_operator (token);
4649 /* The `++' and `--' operators can be handled similarly, even though
4650 they are not technically unary-operators in the grammar. */
4651 if (unary_operator == ERROR_MARK)
4653 if (token->type == CPP_PLUS_PLUS)
4654 unary_operator = PREINCREMENT_EXPR;
4655 else if (token->type == CPP_MINUS_MINUS)
4656 unary_operator = PREDECREMENT_EXPR;
4657 /* Handle the GNU address-of-label extension. */
4658 else if (cp_parser_allow_gnu_extensions_p (parser)
4659 && token->type == CPP_AND_AND)
4663 /* Consume the '&&' token. */
4664 cp_lexer_consume_token (parser->lexer);
4665 /* Look for the identifier. */
4666 identifier = cp_parser_identifier (parser);
4667 /* Create an expression representing the address. */
4668 return finish_label_address_expr (identifier);
4671 if (unary_operator != ERROR_MARK)
4673 tree cast_expression;
4675 /* Consume the operator token. */
4676 token = cp_lexer_consume_token (parser->lexer);
4677 /* Parse the cast-expression. */
4679 = cp_parser_cast_expression (parser, unary_operator == ADDR_EXPR);
4680 /* Now, build an appropriate representation. */
4681 switch (unary_operator)
4684 return build_x_indirect_ref (cast_expression, "unary *");
4687 return build_x_unary_op (ADDR_EXPR, cast_expression);
4691 case TRUTH_NOT_EXPR:
4692 case PREINCREMENT_EXPR:
4693 case PREDECREMENT_EXPR:
4694 return finish_unary_op_expr (unary_operator, cast_expression);
4697 return build_x_unary_op (BIT_NOT_EXPR, cast_expression);
4701 return error_mark_node;
4705 return cp_parser_postfix_expression (parser, address_p);
4708 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
4709 unary-operator, the corresponding tree code is returned. */
4711 static enum tree_code
4712 cp_parser_unary_operator (token)
4715 switch (token->type)
4718 return INDIRECT_REF;
4724 return CONVERT_EXPR;
4730 return TRUTH_NOT_EXPR;
4733 return BIT_NOT_EXPR;
4740 /* Parse a new-expression.
4742 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
4743 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
4745 Returns a representation of the expression. */
4748 cp_parser_new_expression (parser)
4751 bool global_scope_p;
4756 /* Look for the optional `::' operator. */
4758 = (cp_parser_global_scope_opt (parser,
4759 /*current_scope_valid_p=*/false)
4761 /* Look for the `new' operator. */
4762 cp_parser_require_keyword (parser, RID_NEW, "`new'");
4763 /* There's no easy way to tell a new-placement from the
4764 `( type-id )' construct. */
4765 cp_parser_parse_tentatively (parser);
4766 /* Look for a new-placement. */
4767 placement = cp_parser_new_placement (parser);
4768 /* If that didn't work out, there's no new-placement. */
4769 if (!cp_parser_parse_definitely (parser))
4770 placement = NULL_TREE;
4772 /* If the next token is a `(', then we have a parenthesized
4774 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4776 /* Consume the `('. */
4777 cp_lexer_consume_token (parser->lexer);
4778 /* Parse the type-id. */
4779 type = cp_parser_type_id (parser);
4780 /* Look for the closing `)'. */
4781 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4783 /* Otherwise, there must be a new-type-id. */
4785 type = cp_parser_new_type_id (parser);
4787 /* If the next token is a `(', then we have a new-initializer. */
4788 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4789 initializer = cp_parser_new_initializer (parser);
4791 initializer = NULL_TREE;
4793 /* Create a representation of the new-expression. */
4794 return build_new (placement, type, initializer, global_scope_p);
4797 /* Parse a new-placement.
4802 Returns the same representation as for an expression-list. */
4805 cp_parser_new_placement (parser)
4808 tree expression_list;
4810 /* Look for the opening `('. */
4811 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
4812 return error_mark_node;
4813 /* Parse the expression-list. */
4814 expression_list = cp_parser_expression_list (parser);
4815 /* Look for the closing `)'. */
4816 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4818 return expression_list;
4821 /* Parse a new-type-id.
4824 type-specifier-seq new-declarator [opt]
4826 Returns a TREE_LIST whose TREE_PURPOSE is the type-specifier-seq,
4827 and whose TREE_VALUE is the new-declarator. */
4830 cp_parser_new_type_id (parser)
4833 tree type_specifier_seq;
4835 const char *saved_message;
4837 /* The type-specifier sequence must not contain type definitions.
4838 (It cannot contain declarations of new types either, but if they
4839 are not definitions we will catch that because they are not
4841 saved_message = parser->type_definition_forbidden_message;
4842 parser->type_definition_forbidden_message
4843 = "types may not be defined in a new-type-id";
4844 /* Parse the type-specifier-seq. */
4845 type_specifier_seq = cp_parser_type_specifier_seq (parser);
4846 /* Restore the old message. */
4847 parser->type_definition_forbidden_message = saved_message;
4848 /* Parse the new-declarator. */
4849 declarator = cp_parser_new_declarator_opt (parser);
4851 return build_tree_list (type_specifier_seq, declarator);
4854 /* Parse an (optional) new-declarator.
4857 ptr-operator new-declarator [opt]
4858 direct-new-declarator
4860 Returns a representation of the declarator. See
4861 cp_parser_declarator for the representations used. */
4864 cp_parser_new_declarator_opt (parser)
4867 enum tree_code code;
4869 tree cv_qualifier_seq;
4871 /* We don't know if there's a ptr-operator next, or not. */
4872 cp_parser_parse_tentatively (parser);
4873 /* Look for a ptr-operator. */
4874 code = cp_parser_ptr_operator (parser, &type, &cv_qualifier_seq);
4875 /* If that worked, look for more new-declarators. */
4876 if (cp_parser_parse_definitely (parser))
4880 /* Parse another optional declarator. */
4881 declarator = cp_parser_new_declarator_opt (parser);
4883 /* Create the representation of the declarator. */
4884 if (code == INDIRECT_REF)
4885 declarator = make_pointer_declarator (cv_qualifier_seq,
4888 declarator = make_reference_declarator (cv_qualifier_seq,
4891 /* Handle the pointer-to-member case. */
4893 declarator = build_nt (SCOPE_REF, type, declarator);
4898 /* If the next token is a `[', there is a direct-new-declarator. */
4899 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
4900 return cp_parser_direct_new_declarator (parser);
4905 /* Parse a direct-new-declarator.
4907 direct-new-declarator:
4909 direct-new-declarator [constant-expression]
4911 Returns an ARRAY_REF, following the same conventions as are
4912 documented for cp_parser_direct_declarator. */
4915 cp_parser_direct_new_declarator (parser)
4918 tree declarator = NULL_TREE;
4924 /* Look for the opening `['. */
4925 cp_parser_require (parser, CPP_OPEN_SQUARE, "`['");
4926 /* The first expression is not required to be constant. */
4929 expression = cp_parser_expression (parser);
4930 /* The standard requires that the expression have integral
4931 type. DR 74 adds enumeration types. We believe that the
4932 real intent is that these expressions be handled like the
4933 expression in a `switch' condition, which also allows
4934 classes with a single conversion to integral or
4935 enumeration type. */
4936 if (!processing_template_decl)
4939 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
4944 error ("expression in new-declarator must have integral or enumeration type");
4945 expression = error_mark_node;
4949 /* But all the other expressions must be. */
4951 expression = cp_parser_constant_expression (parser);
4952 /* Look for the closing `]'. */
4953 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4955 /* Add this bound to the declarator. */
4956 declarator = build_nt (ARRAY_REF, declarator, expression);
4958 /* If the next token is not a `[', then there are no more
4960 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
4967 /* Parse a new-initializer.
4970 ( expression-list [opt] )
4972 Returns a reprsentation of the expression-list. If there is no
4973 expression-list, VOID_ZERO_NODE is returned. */
4976 cp_parser_new_initializer (parser)
4979 tree expression_list;
4981 /* Look for the opening parenthesis. */
4982 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
4983 /* If the next token is not a `)', then there is an
4985 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
4986 expression_list = cp_parser_expression_list (parser);
4988 expression_list = void_zero_node;
4989 /* Look for the closing parenthesis. */
4990 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4992 return expression_list;
4995 /* Parse a delete-expression.
4998 :: [opt] delete cast-expression
4999 :: [opt] delete [ ] cast-expression
5001 Returns a representation of the expression. */
5004 cp_parser_delete_expression (parser)
5007 bool global_scope_p;
5011 /* Look for the optional `::' operator. */
5013 = (cp_parser_global_scope_opt (parser,
5014 /*current_scope_valid_p=*/false)
5016 /* Look for the `delete' keyword. */
5017 cp_parser_require_keyword (parser, RID_DELETE, "`delete'");
5018 /* See if the array syntax is in use. */
5019 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5021 /* Consume the `[' token. */
5022 cp_lexer_consume_token (parser->lexer);
5023 /* Look for the `]' token. */
5024 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5025 /* Remember that this is the `[]' construct. */
5031 /* Parse the cast-expression. */
5032 expression = cp_parser_cast_expression (parser, /*address_p=*/false);
5034 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
5037 /* Parse a cast-expression.
5041 ( type-id ) cast-expression
5043 Returns a representation of the expression. */
5046 cp_parser_cast_expression (cp_parser *parser, bool address_p)
5048 /* If it's a `(', then we might be looking at a cast. */
5049 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5051 tree type = NULL_TREE;
5052 tree expr = NULL_TREE;
5053 bool compound_literal_p;
5054 const char *saved_message;
5056 /* There's no way to know yet whether or not this is a cast.
5057 For example, `(int (3))' is a unary-expression, while `(int)
5058 3' is a cast. So, we resort to parsing tentatively. */
5059 cp_parser_parse_tentatively (parser);
5060 /* Types may not be defined in a cast. */
5061 saved_message = parser->type_definition_forbidden_message;
5062 parser->type_definition_forbidden_message
5063 = "types may not be defined in casts";
5064 /* Consume the `('. */
5065 cp_lexer_consume_token (parser->lexer);
5066 /* A very tricky bit is that `(struct S) { 3 }' is a
5067 compound-literal (which we permit in C++ as an extension).
5068 But, that construct is not a cast-expression -- it is a
5069 postfix-expression. (The reason is that `(struct S) { 3 }.i'
5070 is legal; if the compound-literal were a cast-expression,
5071 you'd need an extra set of parentheses.) But, if we parse
5072 the type-id, and it happens to be a class-specifier, then we
5073 will commit to the parse at that point, because we cannot
5074 undo the action that is done when creating a new class. So,
5075 then we cannot back up and do a postfix-expression.
5077 Therefore, we scan ahead to the closing `)', and check to see
5078 if the token after the `)' is a `{'. If so, we are not
5079 looking at a cast-expression.
5081 Save tokens so that we can put them back. */
5082 cp_lexer_save_tokens (parser->lexer);
5083 /* Skip tokens until the next token is a closing parenthesis.
5084 If we find the closing `)', and the next token is a `{', then
5085 we are looking at a compound-literal. */
5087 = (cp_parser_skip_to_closing_parenthesis (parser)
5088 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
5089 /* Roll back the tokens we skipped. */
5090 cp_lexer_rollback_tokens (parser->lexer);
5091 /* If we were looking at a compound-literal, simulate an error
5092 so that the call to cp_parser_parse_definitely below will
5094 if (compound_literal_p)
5095 cp_parser_simulate_error (parser);
5098 /* Look for the type-id. */
5099 type = cp_parser_type_id (parser);
5100 /* Look for the closing `)'. */
5101 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5104 /* Restore the saved message. */
5105 parser->type_definition_forbidden_message = saved_message;
5107 /* If all went well, this is a cast. */
5108 if (cp_parser_parse_definitely (parser))
5110 /* Parse the dependent expression. */
5111 expr = cp_parser_cast_expression (parser, /*address_p=*/false);
5112 /* Warn about old-style casts, if so requested. */
5113 if (warn_old_style_cast
5114 && !in_system_header
5115 && !VOID_TYPE_P (type)
5116 && current_lang_name != lang_name_c)
5117 warning ("use of old-style cast");
5118 /* Perform the cast. */
5119 expr = build_c_cast (type, expr);
5126 /* If we get here, then it's not a cast, so it must be a
5127 unary-expression. */
5128 return cp_parser_unary_expression (parser, address_p);
5131 /* Parse a pm-expression.
5135 pm-expression .* cast-expression
5136 pm-expression ->* cast-expression
5138 Returns a representation of the expression. */
5141 cp_parser_pm_expression (parser)
5147 /* Parse the cast-expresion. */
5148 cast_expr = cp_parser_cast_expression (parser, /*address_p=*/false);
5149 pm_expr = cast_expr;
5150 /* Now look for pointer-to-member operators. */
5154 enum cpp_ttype token_type;
5156 /* Peek at the next token. */
5157 token = cp_lexer_peek_token (parser->lexer);
5158 token_type = token->type;
5159 /* If it's not `.*' or `->*' there's no pointer-to-member
5161 if (token_type != CPP_DOT_STAR
5162 && token_type != CPP_DEREF_STAR)
5165 /* Consume the token. */
5166 cp_lexer_consume_token (parser->lexer);
5168 /* Parse another cast-expression. */
5169 cast_expr = cp_parser_cast_expression (parser, /*address_p=*/false);
5171 /* Build the representation of the pointer-to-member
5173 if (token_type == CPP_DEREF_STAR)
5174 pm_expr = build_x_binary_op (MEMBER_REF, pm_expr, cast_expr);
5176 pm_expr = build_m_component_ref (pm_expr, cast_expr);
5182 /* Parse a multiplicative-expression.
5184 mulitplicative-expression:
5186 multiplicative-expression * pm-expression
5187 multiplicative-expression / pm-expression
5188 multiplicative-expression % pm-expression
5190 Returns a representation of the expression. */
5193 cp_parser_multiplicative_expression (parser)
5196 static const cp_parser_token_tree_map map = {
5197 { CPP_MULT, MULT_EXPR },
5198 { CPP_DIV, TRUNC_DIV_EXPR },
5199 { CPP_MOD, TRUNC_MOD_EXPR },
5200 { CPP_EOF, ERROR_MARK }
5203 return cp_parser_binary_expression (parser,
5205 cp_parser_pm_expression);
5208 /* Parse an additive-expression.
5210 additive-expression:
5211 multiplicative-expression
5212 additive-expression + multiplicative-expression
5213 additive-expression - multiplicative-expression
5215 Returns a representation of the expression. */
5218 cp_parser_additive_expression (parser)
5221 static const cp_parser_token_tree_map map = {
5222 { CPP_PLUS, PLUS_EXPR },
5223 { CPP_MINUS, MINUS_EXPR },
5224 { CPP_EOF, ERROR_MARK }
5227 return cp_parser_binary_expression (parser,
5229 cp_parser_multiplicative_expression);
5232 /* Parse a shift-expression.
5236 shift-expression << additive-expression
5237 shift-expression >> additive-expression
5239 Returns a representation of the expression. */
5242 cp_parser_shift_expression (parser)
5245 static const cp_parser_token_tree_map map = {
5246 { CPP_LSHIFT, LSHIFT_EXPR },
5247 { CPP_RSHIFT, RSHIFT_EXPR },
5248 { CPP_EOF, ERROR_MARK }
5251 return cp_parser_binary_expression (parser,
5253 cp_parser_additive_expression);
5256 /* Parse a relational-expression.
5258 relational-expression:
5260 relational-expression < shift-expression
5261 relational-expression > shift-expression
5262 relational-expression <= shift-expression
5263 relational-expression >= shift-expression
5267 relational-expression:
5268 relational-expression <? shift-expression
5269 relational-expression >? shift-expression
5271 Returns a representation of the expression. */
5274 cp_parser_relational_expression (parser)
5277 static const cp_parser_token_tree_map map = {
5278 { CPP_LESS, LT_EXPR },
5279 { CPP_GREATER, GT_EXPR },
5280 { CPP_LESS_EQ, LE_EXPR },
5281 { CPP_GREATER_EQ, GE_EXPR },
5282 { CPP_MIN, MIN_EXPR },
5283 { CPP_MAX, MAX_EXPR },
5284 { CPP_EOF, ERROR_MARK }
5287 return cp_parser_binary_expression (parser,
5289 cp_parser_shift_expression);
5292 /* Parse an equality-expression.
5294 equality-expression:
5295 relational-expression
5296 equality-expression == relational-expression
5297 equality-expression != relational-expression
5299 Returns a representation of the expression. */
5302 cp_parser_equality_expression (parser)
5305 static const cp_parser_token_tree_map map = {
5306 { CPP_EQ_EQ, EQ_EXPR },
5307 { CPP_NOT_EQ, NE_EXPR },
5308 { CPP_EOF, ERROR_MARK }
5311 return cp_parser_binary_expression (parser,
5313 cp_parser_relational_expression);
5316 /* Parse an and-expression.
5320 and-expression & equality-expression
5322 Returns a representation of the expression. */
5325 cp_parser_and_expression (parser)
5328 static const cp_parser_token_tree_map map = {
5329 { CPP_AND, BIT_AND_EXPR },
5330 { CPP_EOF, ERROR_MARK }
5333 return cp_parser_binary_expression (parser,
5335 cp_parser_equality_expression);
5338 /* Parse an exclusive-or-expression.
5340 exclusive-or-expression:
5342 exclusive-or-expression ^ and-expression
5344 Returns a representation of the expression. */
5347 cp_parser_exclusive_or_expression (parser)
5350 static const cp_parser_token_tree_map map = {
5351 { CPP_XOR, BIT_XOR_EXPR },
5352 { CPP_EOF, ERROR_MARK }
5355 return cp_parser_binary_expression (parser,
5357 cp_parser_and_expression);
5361 /* Parse an inclusive-or-expression.
5363 inclusive-or-expression:
5364 exclusive-or-expression
5365 inclusive-or-expression | exclusive-or-expression
5367 Returns a representation of the expression. */
5370 cp_parser_inclusive_or_expression (parser)
5373 static const cp_parser_token_tree_map map = {
5374 { CPP_OR, BIT_IOR_EXPR },
5375 { CPP_EOF, ERROR_MARK }
5378 return cp_parser_binary_expression (parser,
5380 cp_parser_exclusive_or_expression);
5383 /* Parse a logical-and-expression.
5385 logical-and-expression:
5386 inclusive-or-expression
5387 logical-and-expression && inclusive-or-expression
5389 Returns a representation of the expression. */
5392 cp_parser_logical_and_expression (parser)
5395 static const cp_parser_token_tree_map map = {
5396 { CPP_AND_AND, TRUTH_ANDIF_EXPR },
5397 { CPP_EOF, ERROR_MARK }
5400 return cp_parser_binary_expression (parser,
5402 cp_parser_inclusive_or_expression);
5405 /* Parse a logical-or-expression.
5407 logical-or-expression:
5408 logical-and-expresion
5409 logical-or-expression || logical-and-expression
5411 Returns a representation of the expression. */
5414 cp_parser_logical_or_expression (parser)
5417 static const cp_parser_token_tree_map map = {
5418 { CPP_OR_OR, TRUTH_ORIF_EXPR },
5419 { CPP_EOF, ERROR_MARK }
5422 return cp_parser_binary_expression (parser,
5424 cp_parser_logical_and_expression);
5427 /* Parse a conditional-expression.
5429 conditional-expression:
5430 logical-or-expression
5431 logical-or-expression ? expression : assignment-expression
5435 conditional-expression:
5436 logical-or-expression ? : assignment-expression
5438 Returns a representation of the expression. */
5441 cp_parser_conditional_expression (parser)
5444 tree logical_or_expr;
5446 /* Parse the logical-or-expression. */
5447 logical_or_expr = cp_parser_logical_or_expression (parser);
5448 /* If the next token is a `?', then we have a real conditional
5450 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
5451 return cp_parser_question_colon_clause (parser, logical_or_expr);
5452 /* Otherwise, the value is simply the logical-or-expression. */
5454 return logical_or_expr;
5457 /* Parse the `? expression : assignment-expression' part of a
5458 conditional-expression. The LOGICAL_OR_EXPR is the
5459 logical-or-expression that started the conditional-expression.
5460 Returns a representation of the entire conditional-expression.
5462 This routine exists only so that it can be shared between
5463 cp_parser_conditional_expression and
5464 cp_parser_assignment_expression.
5466 ? expression : assignment-expression
5470 ? : assignment-expression */
5473 cp_parser_question_colon_clause (parser, logical_or_expr)
5475 tree logical_or_expr;
5478 tree assignment_expr;
5480 /* Consume the `?' token. */
5481 cp_lexer_consume_token (parser->lexer);
5482 if (cp_parser_allow_gnu_extensions_p (parser)
5483 && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
5484 /* Implicit true clause. */
5487 /* Parse the expression. */
5488 expr = cp_parser_expression (parser);
5490 /* The next token should be a `:'. */
5491 cp_parser_require (parser, CPP_COLON, "`:'");
5492 /* Parse the assignment-expression. */
5493 assignment_expr = cp_parser_assignment_expression (parser);
5495 /* Build the conditional-expression. */
5496 return build_x_conditional_expr (logical_or_expr,
5501 /* Parse an assignment-expression.
5503 assignment-expression:
5504 conditional-expression
5505 logical-or-expression assignment-operator assignment_expression
5508 Returns a representation for the expression. */
5511 cp_parser_assignment_expression (parser)
5516 /* If the next token is the `throw' keyword, then we're looking at
5517 a throw-expression. */
5518 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
5519 expr = cp_parser_throw_expression (parser);
5520 /* Otherwise, it must be that we are looking at a
5521 logical-or-expression. */
5524 /* Parse the logical-or-expression. */
5525 expr = cp_parser_logical_or_expression (parser);
5526 /* If the next token is a `?' then we're actually looking at a
5527 conditional-expression. */
5528 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
5529 return cp_parser_question_colon_clause (parser, expr);
5532 enum tree_code assignment_operator;
5534 /* If it's an assignment-operator, we're using the second
5537 = cp_parser_assignment_operator_opt (parser);
5538 if (assignment_operator != ERROR_MARK)
5542 /* Parse the right-hand side of the assignment. */
5543 rhs = cp_parser_assignment_expression (parser);
5544 /* Build the asignment expression. */
5545 expr = build_x_modify_expr (expr,
5546 assignment_operator,
5555 /* Parse an (optional) assignment-operator.
5557 assignment-operator: one of
5558 = *= /= %= += -= >>= <<= &= ^= |=
5562 assignment-operator: one of
5565 If the next token is an assignment operator, the corresponding tree
5566 code is returned, and the token is consumed. For example, for
5567 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
5568 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
5569 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
5570 operator, ERROR_MARK is returned. */
5572 static enum tree_code
5573 cp_parser_assignment_operator_opt (parser)
5579 /* Peek at the next toen. */
5580 token = cp_lexer_peek_token (parser->lexer);
5582 switch (token->type)
5593 op = TRUNC_DIV_EXPR;
5597 op = TRUNC_MOD_EXPR;
5637 /* Nothing else is an assignment operator. */
5641 /* If it was an assignment operator, consume it. */
5642 if (op != ERROR_MARK)
5643 cp_lexer_consume_token (parser->lexer);
5648 /* Parse an expression.
5651 assignment-expression
5652 expression , assignment-expression
5654 Returns a representation of the expression. */
5657 cp_parser_expression (parser)
5660 tree expression = NULL_TREE;
5661 bool saw_comma_p = false;
5665 tree assignment_expression;
5667 /* Parse the next assignment-expression. */
5668 assignment_expression
5669 = cp_parser_assignment_expression (parser);
5670 /* If this is the first assignment-expression, we can just
5673 expression = assignment_expression;
5674 /* Otherwise, chain the expressions together. It is unclear why
5675 we do not simply build COMPOUND_EXPRs as we go. */
5677 expression = tree_cons (NULL_TREE,
5678 assignment_expression,
5680 /* If the next token is not a comma, then we are done with the
5682 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5684 /* Consume the `,'. */
5685 cp_lexer_consume_token (parser->lexer);
5686 /* The first time we see a `,', we must take special action
5687 because the representation used for a single expression is
5688 different from that used for a list containing the single
5692 /* Remember that this expression has a `,' in it. */
5694 /* Turn the EXPRESSION into a TREE_LIST so that we can link
5695 additional expressions to it. */
5696 expression = build_tree_list (NULL_TREE, expression);
5700 /* Build a COMPOUND_EXPR to represent the entire expression, if
5701 necessary. We built up the list in reverse order, so we must
5702 straighten it out here. */
5704 expression = build_x_compound_expr (nreverse (expression));
5709 /* Parse a constant-expression.
5711 constant-expression:
5712 conditional-expression */
5715 cp_parser_constant_expression (parser)
5718 bool saved_constant_expression_p;
5721 /* It might seem that we could simply parse the
5722 conditional-expression, and then check to see if it were
5723 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
5724 one that the compiler can figure out is constant, possibly after
5725 doing some simplifications or optimizations. The standard has a
5726 precise definition of constant-expression, and we must honor
5727 that, even though it is somewhat more restrictive.
5733 is not a legal declaration, because `(2, 3)' is not a
5734 constant-expression. The `,' operator is forbidden in a
5735 constant-expression. However, GCC's constant-folding machinery
5736 will fold this operation to an INTEGER_CST for `3'. */
5738 /* Save the old setting of CONSTANT_EXPRESSION_P. */
5739 saved_constant_expression_p = parser->constant_expression_p;
5740 /* We are now parsing a constant-expression. */
5741 parser->constant_expression_p = true;
5742 /* Parse the conditional-expression. */
5743 expression = cp_parser_conditional_expression (parser);
5744 /* Restore the old setting of CONSTANT_EXPRESSION_P. */
5745 parser->constant_expression_p = saved_constant_expression_p;
5750 /* Statements [gram.stmt.stmt] */
5752 /* Parse a statement.
5756 expression-statement
5761 declaration-statement
5765 cp_parser_statement (parser)
5770 int statement_line_number;
5772 /* There is no statement yet. */
5773 statement = NULL_TREE;
5774 /* Peek at the next token. */
5775 token = cp_lexer_peek_token (parser->lexer);
5776 /* Remember the line number of the first token in the statement. */
5777 statement_line_number = token->line_number;
5778 /* If this is a keyword, then that will often determine what kind of
5779 statement we have. */
5780 if (token->type == CPP_KEYWORD)
5782 enum rid keyword = token->keyword;
5788 statement = cp_parser_labeled_statement (parser);
5793 statement = cp_parser_selection_statement (parser);
5799 statement = cp_parser_iteration_statement (parser);
5806 statement = cp_parser_jump_statement (parser);
5810 statement = cp_parser_try_block (parser);
5814 /* It might be a keyword like `int' that can start a
5815 declaration-statement. */
5819 else if (token->type == CPP_NAME)
5821 /* If the next token is a `:', then we are looking at a
5822 labeled-statement. */
5823 token = cp_lexer_peek_nth_token (parser->lexer, 2);
5824 if (token->type == CPP_COLON)
5825 statement = cp_parser_labeled_statement (parser);
5827 /* Anything that starts with a `{' must be a compound-statement. */
5828 else if (token->type == CPP_OPEN_BRACE)
5829 statement = cp_parser_compound_statement (parser);
5831 /* Everything else must be a declaration-statement or an
5832 expression-statement. Try for the declaration-statement
5833 first, unless we are looking at a `;', in which case we know that
5834 we have an expression-statement. */
5837 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
5839 cp_parser_parse_tentatively (parser);
5840 /* Try to parse the declaration-statement. */
5841 cp_parser_declaration_statement (parser);
5842 /* If that worked, we're done. */
5843 if (cp_parser_parse_definitely (parser))
5846 /* Look for an expression-statement instead. */
5847 statement = cp_parser_expression_statement (parser);
5850 /* Set the line number for the statement. */
5851 if (statement && statement_code_p (TREE_CODE (statement)))
5852 STMT_LINENO (statement) = statement_line_number;
5855 /* Parse a labeled-statement.
5858 identifier : statement
5859 case constant-expression : statement
5862 Returns the new CASE_LABEL, for a `case' or `default' label. For
5863 an ordinary label, returns a LABEL_STMT. */
5866 cp_parser_labeled_statement (parser)
5870 tree statement = NULL_TREE;
5872 /* The next token should be an identifier. */
5873 token = cp_lexer_peek_token (parser->lexer);
5874 if (token->type != CPP_NAME
5875 && token->type != CPP_KEYWORD)
5877 cp_parser_error (parser, "expected labeled-statement");
5878 return error_mark_node;
5881 switch (token->keyword)
5887 /* Consume the `case' token. */
5888 cp_lexer_consume_token (parser->lexer);
5889 /* Parse the constant-expression. */
5890 expr = cp_parser_constant_expression (parser);
5891 /* Create the label. */
5892 statement = finish_case_label (expr, NULL_TREE);
5897 /* Consume the `default' token. */
5898 cp_lexer_consume_token (parser->lexer);
5899 /* Create the label. */
5900 statement = finish_case_label (NULL_TREE, NULL_TREE);
5904 /* Anything else must be an ordinary label. */
5905 statement = finish_label_stmt (cp_parser_identifier (parser));
5909 /* Require the `:' token. */
5910 cp_parser_require (parser, CPP_COLON, "`:'");
5911 /* Parse the labeled statement. */
5912 cp_parser_statement (parser);
5914 /* Return the label, in the case of a `case' or `default' label. */
5918 /* Parse an expression-statement.
5920 expression-statement:
5923 Returns the new EXPR_STMT -- or NULL_TREE if the expression
5924 statement consists of nothing more than an `;'. */
5927 cp_parser_expression_statement (parser)
5932 /* If the next token is not a `;', then there is an expression to parse. */
5933 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
5934 statement = finish_expr_stmt (cp_parser_expression (parser));
5935 /* Otherwise, we do not even bother to build an EXPR_STMT. */
5939 statement = NULL_TREE;
5941 /* Consume the final `;'. */
5942 if (!cp_parser_require (parser, CPP_SEMICOLON, "`;'"))
5944 /* If there is additional (erroneous) input, skip to the end of
5946 cp_parser_skip_to_end_of_statement (parser);
5947 /* If the next token is now a `;', consume it. */
5948 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
5949 cp_lexer_consume_token (parser->lexer);
5955 /* Parse a compound-statement.
5958 { statement-seq [opt] }
5960 Returns a COMPOUND_STMT representing the statement. */
5963 cp_parser_compound_statement (cp_parser *parser)
5967 /* Consume the `{'. */
5968 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
5969 return error_mark_node;
5970 /* Begin the compound-statement. */
5971 compound_stmt = begin_compound_stmt (/*has_no_scope=*/0);
5972 /* Parse an (optional) statement-seq. */
5973 cp_parser_statement_seq_opt (parser);
5974 /* Finish the compound-statement. */
5975 finish_compound_stmt (/*has_no_scope=*/0, compound_stmt);
5976 /* Consume the `}'. */
5977 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
5979 return compound_stmt;
5982 /* Parse an (optional) statement-seq.
5986 statement-seq [opt] statement */
5989 cp_parser_statement_seq_opt (parser)
5992 /* Scan statements until there aren't any more. */
5995 /* If we're looking at a `}', then we've run out of statements. */
5996 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE)
5997 || cp_lexer_next_token_is (parser->lexer, CPP_EOF))
6000 /* Parse the statement. */
6001 cp_parser_statement (parser);
6005 /* Parse a selection-statement.
6007 selection-statement:
6008 if ( condition ) statement
6009 if ( condition ) statement else statement
6010 switch ( condition ) statement
6012 Returns the new IF_STMT or SWITCH_STMT. */
6015 cp_parser_selection_statement (parser)
6021 /* Peek at the next token. */
6022 token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
6024 /* See what kind of keyword it is. */
6025 keyword = token->keyword;
6034 /* Look for the `('. */
6035 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
6037 cp_parser_skip_to_end_of_statement (parser);
6038 return error_mark_node;
6041 /* Begin the selection-statement. */
6042 if (keyword == RID_IF)
6043 statement = begin_if_stmt ();
6045 statement = begin_switch_stmt ();
6047 /* Parse the condition. */
6048 condition = cp_parser_condition (parser);
6049 /* Look for the `)'. */
6050 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
6051 cp_parser_skip_to_closing_parenthesis (parser);
6053 if (keyword == RID_IF)
6057 /* Add the condition. */
6058 finish_if_stmt_cond (condition, statement);
6060 /* Parse the then-clause. */
6061 then_stmt = cp_parser_implicitly_scoped_statement (parser);
6062 finish_then_clause (statement);
6064 /* If the next token is `else', parse the else-clause. */
6065 if (cp_lexer_next_token_is_keyword (parser->lexer,
6070 /* Consume the `else' keyword. */
6071 cp_lexer_consume_token (parser->lexer);
6072 /* Parse the else-clause. */
6074 = cp_parser_implicitly_scoped_statement (parser);
6075 finish_else_clause (statement);
6078 /* Now we're all done with the if-statement. */
6085 /* Add the condition. */
6086 finish_switch_cond (condition, statement);
6088 /* Parse the body of the switch-statement. */
6089 body = cp_parser_implicitly_scoped_statement (parser);
6091 /* Now we're all done with the switch-statement. */
6092 finish_switch_stmt (statement);
6100 cp_parser_error (parser, "expected selection-statement");
6101 return error_mark_node;
6105 /* Parse a condition.
6109 type-specifier-seq declarator = assignment-expression
6114 type-specifier-seq declarator asm-specification [opt]
6115 attributes [opt] = assignment-expression
6117 Returns the expression that should be tested. */
6120 cp_parser_condition (parser)
6123 tree type_specifiers;
6124 const char *saved_message;
6126 /* Try the declaration first. */
6127 cp_parser_parse_tentatively (parser);
6128 /* New types are not allowed in the type-specifier-seq for a
6130 saved_message = parser->type_definition_forbidden_message;
6131 parser->type_definition_forbidden_message
6132 = "types may not be defined in conditions";
6133 /* Parse the type-specifier-seq. */
6134 type_specifiers = cp_parser_type_specifier_seq (parser);
6135 /* Restore the saved message. */
6136 parser->type_definition_forbidden_message = saved_message;
6137 /* If all is well, we might be looking at a declaration. */
6138 if (!cp_parser_error_occurred (parser))
6141 tree asm_specification;
6144 tree initializer = NULL_TREE;
6146 /* Parse the declarator. */
6147 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
6148 /*ctor_dtor_or_conv_p=*/NULL);
6149 /* Parse the attributes. */
6150 attributes = cp_parser_attributes_opt (parser);
6151 /* Parse the asm-specification. */
6152 asm_specification = cp_parser_asm_specification_opt (parser);
6153 /* If the next token is not an `=', then we might still be
6154 looking at an expression. For example:
6158 looks like a decl-specifier-seq and a declarator -- but then
6159 there is no `=', so this is an expression. */
6160 cp_parser_require (parser, CPP_EQ, "`='");
6161 /* If we did see an `=', then we are looking at a declaration
6163 if (cp_parser_parse_definitely (parser))
6165 /* Create the declaration. */
6166 decl = start_decl (declarator, type_specifiers,
6167 /*initialized_p=*/true,
6168 attributes, /*prefix_attributes=*/NULL_TREE);
6169 /* Parse the assignment-expression. */
6170 initializer = cp_parser_assignment_expression (parser);
6172 /* Process the initializer. */
6173 cp_finish_decl (decl,
6176 LOOKUP_ONLYCONVERTING);
6178 return convert_from_reference (decl);
6181 /* If we didn't even get past the declarator successfully, we are
6182 definitely not looking at a declaration. */
6184 cp_parser_abort_tentative_parse (parser);
6186 /* Otherwise, we are looking at an expression. */
6187 return cp_parser_expression (parser);
6190 /* Parse an iteration-statement.
6192 iteration-statement:
6193 while ( condition ) statement
6194 do statement while ( expression ) ;
6195 for ( for-init-statement condition [opt] ; expression [opt] )
6198 Returns the new WHILE_STMT, DO_STMT, or FOR_STMT. */
6201 cp_parser_iteration_statement (parser)
6208 /* Peek at the next token. */
6209 token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
6211 return error_mark_node;
6213 /* See what kind of keyword it is. */
6214 keyword = token->keyword;
6221 /* Begin the while-statement. */
6222 statement = begin_while_stmt ();
6223 /* Look for the `('. */
6224 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6225 /* Parse the condition. */
6226 condition = cp_parser_condition (parser);
6227 finish_while_stmt_cond (condition, statement);
6228 /* Look for the `)'. */
6229 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6230 /* Parse the dependent statement. */
6231 cp_parser_already_scoped_statement (parser);
6232 /* We're done with the while-statement. */
6233 finish_while_stmt (statement);
6241 /* Begin the do-statement. */
6242 statement = begin_do_stmt ();
6243 /* Parse the body of the do-statement. */
6244 cp_parser_implicitly_scoped_statement (parser);
6245 finish_do_body (statement);
6246 /* Look for the `while' keyword. */
6247 cp_parser_require_keyword (parser, RID_WHILE, "`while'");
6248 /* Look for the `('. */
6249 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6250 /* Parse the expression. */
6251 expression = cp_parser_expression (parser);
6252 /* We're done with the do-statement. */
6253 finish_do_stmt (expression, statement);
6254 /* Look for the `)'. */
6255 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6256 /* Look for the `;'. */
6257 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6263 tree condition = NULL_TREE;
6264 tree expression = NULL_TREE;
6266 /* Begin the for-statement. */
6267 statement = begin_for_stmt ();
6268 /* Look for the `('. */
6269 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6270 /* Parse the initialization. */
6271 cp_parser_for_init_statement (parser);
6272 finish_for_init_stmt (statement);
6274 /* If there's a condition, process it. */
6275 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6276 condition = cp_parser_condition (parser);
6277 finish_for_cond (condition, statement);
6278 /* Look for the `;'. */
6279 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6281 /* If there's an expression, process it. */
6282 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
6283 expression = cp_parser_expression (parser);
6284 finish_for_expr (expression, statement);
6285 /* Look for the `)'. */
6286 cp_parser_require (parser, CPP_CLOSE_PAREN, "`;'");
6288 /* Parse the body of the for-statement. */
6289 cp_parser_already_scoped_statement (parser);
6291 /* We're done with the for-statement. */
6292 finish_for_stmt (statement);
6297 cp_parser_error (parser, "expected iteration-statement");
6298 statement = error_mark_node;
6305 /* Parse a for-init-statement.
6308 expression-statement
6309 simple-declaration */
6312 cp_parser_for_init_statement (parser)
6315 /* If the next token is a `;', then we have an empty
6316 expression-statement. Gramatically, this is also a
6317 simple-declaration, but an invalid one, because it does not
6318 declare anything. Therefore, if we did not handle this case
6319 specially, we would issue an error message about an invalid
6321 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6323 /* We're going to speculatively look for a declaration, falling back
6324 to an expression, if necessary. */
6325 cp_parser_parse_tentatively (parser);
6326 /* Parse the declaration. */
6327 cp_parser_simple_declaration (parser,
6328 /*function_definition_allowed_p=*/false);
6329 /* If the tentative parse failed, then we shall need to look for an
6330 expression-statement. */
6331 if (cp_parser_parse_definitely (parser))
6335 cp_parser_expression_statement (parser);
6338 /* Parse a jump-statement.
6343 return expression [opt] ;
6351 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_STMT, or
6355 cp_parser_jump_statement (parser)
6358 tree statement = error_mark_node;
6362 /* Peek at the next token. */
6363 token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
6365 return error_mark_node;
6367 /* See what kind of keyword it is. */
6368 keyword = token->keyword;
6372 statement = finish_break_stmt ();
6373 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6377 statement = finish_continue_stmt ();
6378 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6385 /* If the next token is a `;', then there is no
6387 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6388 expr = cp_parser_expression (parser);
6391 /* Build the return-statement. */
6392 statement = finish_return_stmt (expr);
6393 /* Look for the final `;'. */
6394 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6399 /* Create the goto-statement. */
6400 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
6402 /* Issue a warning about this use of a GNU extension. */
6404 pedwarn ("ISO C++ forbids computed gotos");
6405 /* Consume the '*' token. */
6406 cp_lexer_consume_token (parser->lexer);
6407 /* Parse the dependent expression. */
6408 finish_goto_stmt (cp_parser_expression (parser));
6411 finish_goto_stmt (cp_parser_identifier (parser));
6412 /* Look for the final `;'. */
6413 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6417 cp_parser_error (parser, "expected jump-statement");
6424 /* Parse a declaration-statement.
6426 declaration-statement:
6427 block-declaration */
6430 cp_parser_declaration_statement (parser)
6433 /* Parse the block-declaration. */
6434 cp_parser_block_declaration (parser, /*statement_p=*/true);
6436 /* Finish off the statement. */
6440 /* Some dependent statements (like `if (cond) statement'), are
6441 implicitly in their own scope. In other words, if the statement is
6442 a single statement (as opposed to a compound-statement), it is
6443 none-the-less treated as if it were enclosed in braces. Any
6444 declarations appearing in the dependent statement are out of scope
6445 after control passes that point. This function parses a statement,
6446 but ensures that is in its own scope, even if it is not a
6449 Returns the new statement. */
6452 cp_parser_implicitly_scoped_statement (parser)
6457 /* If the token is not a `{', then we must take special action. */
6458 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
6460 /* Create a compound-statement. */
6461 statement = begin_compound_stmt (/*has_no_scope=*/0);
6462 /* Parse the dependent-statement. */
6463 cp_parser_statement (parser);
6464 /* Finish the dummy compound-statement. */
6465 finish_compound_stmt (/*has_no_scope=*/0, statement);
6467 /* Otherwise, we simply parse the statement directly. */
6469 statement = cp_parser_compound_statement (parser);
6471 /* Return the statement. */
6475 /* For some dependent statements (like `while (cond) statement'), we
6476 have already created a scope. Therefore, even if the dependent
6477 statement is a compound-statement, we do not want to create another
6481 cp_parser_already_scoped_statement (parser)
6484 /* If the token is not a `{', then we must take special action. */
6485 if (cp_lexer_next_token_is_not(parser->lexer, CPP_OPEN_BRACE))
6489 /* Create a compound-statement. */
6490 statement = begin_compound_stmt (/*has_no_scope=*/1);
6491 /* Parse the dependent-statement. */
6492 cp_parser_statement (parser);
6493 /* Finish the dummy compound-statement. */
6494 finish_compound_stmt (/*has_no_scope=*/1, statement);
6496 /* Otherwise, we simply parse the statement directly. */
6498 cp_parser_statement (parser);
6501 /* Declarations [gram.dcl.dcl] */
6503 /* Parse an optional declaration-sequence.
6507 declaration-seq declaration */
6510 cp_parser_declaration_seq_opt (parser)
6517 token = cp_lexer_peek_token (parser->lexer);
6519 if (token->type == CPP_CLOSE_BRACE
6520 || token->type == CPP_EOF)
6523 if (token->type == CPP_SEMICOLON)
6525 /* A declaration consisting of a single semicolon is
6526 invalid. Allow it unless we're being pedantic. */
6528 pedwarn ("extra `;'");
6529 cp_lexer_consume_token (parser->lexer);
6533 /* The C lexer modifies PENDING_LANG_CHANGE when it wants the
6534 parser to enter or exit implict `extern "C"' blocks. */
6535 while (pending_lang_change > 0)
6537 push_lang_context (lang_name_c);
6538 --pending_lang_change;
6540 while (pending_lang_change < 0)
6542 pop_lang_context ();
6543 ++pending_lang_change;
6546 /* Parse the declaration itself. */
6547 cp_parser_declaration (parser);
6551 /* Parse a declaration.
6556 template-declaration
6557 explicit-instantiation
6558 explicit-specialization
6559 linkage-specification
6560 namespace-definition
6565 __extension__ declaration */
6568 cp_parser_declaration (parser)
6575 /* Check for the `__extension__' keyword. */
6576 if (cp_parser_extension_opt (parser, &saved_pedantic))
6578 /* Parse the qualified declaration. */
6579 cp_parser_declaration (parser);
6580 /* Restore the PEDANTIC flag. */
6581 pedantic = saved_pedantic;
6586 /* Try to figure out what kind of declaration is present. */
6587 token1 = *cp_lexer_peek_token (parser->lexer);
6588 if (token1.type != CPP_EOF)
6589 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
6591 /* If the next token is `extern' and the following token is a string
6592 literal, then we have a linkage specification. */
6593 if (token1.keyword == RID_EXTERN
6594 && cp_parser_is_string_literal (&token2))
6595 cp_parser_linkage_specification (parser);
6596 /* If the next token is `template', then we have either a template
6597 declaration, an explicit instantiation, or an explicit
6599 else if (token1.keyword == RID_TEMPLATE)
6601 /* `template <>' indicates a template specialization. */
6602 if (token2.type == CPP_LESS
6603 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
6604 cp_parser_explicit_specialization (parser);
6605 /* `template <' indicates a template declaration. */
6606 else if (token2.type == CPP_LESS)
6607 cp_parser_template_declaration (parser, /*member_p=*/false);
6608 /* Anything else must be an explicit instantiation. */
6610 cp_parser_explicit_instantiation (parser);
6612 /* If the next token is `export', then we have a template
6614 else if (token1.keyword == RID_EXPORT)
6615 cp_parser_template_declaration (parser, /*member_p=*/false);
6616 /* If the next token is `extern', 'static' or 'inline' and the one
6617 after that is `template', we have a GNU extended explicit
6618 instantiation directive. */
6619 else if (cp_parser_allow_gnu_extensions_p (parser)
6620 && (token1.keyword == RID_EXTERN
6621 || token1.keyword == RID_STATIC
6622 || token1.keyword == RID_INLINE)
6623 && token2.keyword == RID_TEMPLATE)
6624 cp_parser_explicit_instantiation (parser);
6625 /* If the next token is `namespace', check for a named or unnamed
6626 namespace definition. */
6627 else if (token1.keyword == RID_NAMESPACE
6628 && (/* A named namespace definition. */
6629 (token2.type == CPP_NAME
6630 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
6632 /* An unnamed namespace definition. */
6633 || token2.type == CPP_OPEN_BRACE))
6634 cp_parser_namespace_definition (parser);
6635 /* We must have either a block declaration or a function
6638 /* Try to parse a block-declaration, or a function-definition. */
6639 cp_parser_block_declaration (parser, /*statement_p=*/false);
6642 /* Parse a block-declaration.
6647 namespace-alias-definition
6654 __extension__ block-declaration
6657 If STATEMENT_P is TRUE, then this block-declaration is ocurring as
6658 part of a declaration-statement. */
6661 cp_parser_block_declaration (cp_parser *parser,
6667 /* Check for the `__extension__' keyword. */
6668 if (cp_parser_extension_opt (parser, &saved_pedantic))
6670 /* Parse the qualified declaration. */
6671 cp_parser_block_declaration (parser, statement_p);
6672 /* Restore the PEDANTIC flag. */
6673 pedantic = saved_pedantic;
6678 /* Peek at the next token to figure out which kind of declaration is
6680 token1 = cp_lexer_peek_token (parser->lexer);
6682 /* If the next keyword is `asm', we have an asm-definition. */
6683 if (token1->keyword == RID_ASM)
6686 cp_parser_commit_to_tentative_parse (parser);
6687 cp_parser_asm_definition (parser);
6689 /* If the next keyword is `namespace', we have a
6690 namespace-alias-definition. */
6691 else if (token1->keyword == RID_NAMESPACE)
6692 cp_parser_namespace_alias_definition (parser);
6693 /* If the next keyword is `using', we have either a
6694 using-declaration or a using-directive. */
6695 else if (token1->keyword == RID_USING)
6700 cp_parser_commit_to_tentative_parse (parser);
6701 /* If the token after `using' is `namespace', then we have a
6703 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
6704 if (token2->keyword == RID_NAMESPACE)
6705 cp_parser_using_directive (parser);
6706 /* Otherwise, it's a using-declaration. */
6708 cp_parser_using_declaration (parser);
6710 /* If the next keyword is `__label__' we have a label declaration. */
6711 else if (token1->keyword == RID_LABEL)
6714 cp_parser_commit_to_tentative_parse (parser);
6715 cp_parser_label_declaration (parser);
6717 /* Anything else must be a simple-declaration. */
6719 cp_parser_simple_declaration (parser, !statement_p);
6722 /* Parse a simple-declaration.
6725 decl-specifier-seq [opt] init-declarator-list [opt] ;
6727 init-declarator-list:
6729 init-declarator-list , init-declarator
6731 If FUNCTION_DEFINTION_ALLOWED_P is TRUE, then we also recognize a
6732 function-definition as a simple-declaration. */
6735 cp_parser_simple_declaration (parser, function_definition_allowed_p)
6737 bool function_definition_allowed_p;
6739 tree decl_specifiers;
6742 bool declares_class_or_enum;
6743 bool saw_declarator;
6745 /* Defer access checks until we know what is being declared; the
6746 checks for names appearing in the decl-specifier-seq should be
6747 done as if we were in the scope of the thing being declared. */
6748 cp_parser_start_deferring_access_checks (parser);
6749 /* Parse the decl-specifier-seq. We have to keep track of whether
6750 or not the decl-specifier-seq declares a named class or
6751 enumeration type, since that is the only case in which the
6752 init-declarator-list is allowed to be empty.
6756 In a simple-declaration, the optional init-declarator-list can be
6757 omitted only when declaring a class or enumeration, that is when
6758 the decl-specifier-seq contains either a class-specifier, an
6759 elaborated-type-specifier, or an enum-specifier. */
6761 = cp_parser_decl_specifier_seq (parser,
6762 CP_PARSER_FLAGS_OPTIONAL,
6764 &declares_class_or_enum);
6765 /* We no longer need to defer access checks. */
6766 access_checks = cp_parser_stop_deferring_access_checks (parser);
6768 /* Prevent access checks from being reclaimed by GC. */
6769 parser->access_checks_lists = tree_cons (NULL_TREE, access_checks,
6770 parser->access_checks_lists);
6772 /* Keep going until we hit the `;' at the end of the simple
6774 saw_declarator = false;
6775 while (cp_lexer_next_token_is_not (parser->lexer,
6779 bool function_definition_p;
6781 saw_declarator = true;
6782 /* Parse the init-declarator. */
6783 cp_parser_init_declarator (parser, decl_specifiers, attributes,
6785 function_definition_allowed_p,
6787 &function_definition_p);
6788 /* Handle function definitions specially. */
6789 if (function_definition_p)
6791 /* If the next token is a `,', then we are probably
6792 processing something like:
6796 which is erroneous. */
6797 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
6798 error ("mixing declarations and function-definitions is forbidden");
6799 /* Otherwise, we're done with the list of declarators. */
6802 /* Discard access checks no longer in use. */
6803 parser->access_checks_lists
6804 = TREE_CHAIN (parser->access_checks_lists);
6808 /* The next token should be either a `,' or a `;'. */
6809 token = cp_lexer_peek_token (parser->lexer);
6810 /* If it's a `,', there are more declarators to come. */
6811 if (token->type == CPP_COMMA)
6812 cp_lexer_consume_token (parser->lexer);
6813 /* If it's a `;', we are done. */
6814 else if (token->type == CPP_SEMICOLON)
6816 /* Anything else is an error. */
6819 cp_parser_error (parser, "expected `,' or `;'");
6820 /* Skip tokens until we reach the end of the statement. */
6821 cp_parser_skip_to_end_of_statement (parser);
6822 /* Discard access checks no longer in use. */
6823 parser->access_checks_lists
6824 = TREE_CHAIN (parser->access_checks_lists);
6827 /* After the first time around, a function-definition is not
6828 allowed -- even if it was OK at first. For example:
6833 function_definition_allowed_p = false;
6836 /* Issue an error message if no declarators are present, and the
6837 decl-specifier-seq does not itself declare a class or
6839 if (!saw_declarator)
6841 if (cp_parser_declares_only_class_p (parser))
6842 shadow_tag (decl_specifiers);
6843 /* Perform any deferred access checks. */
6844 cp_parser_perform_deferred_access_checks (access_checks);
6847 /* Consume the `;'. */
6848 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6850 /* Mark all the classes that appeared in the decl-specifier-seq as
6851 having received a `;'. */
6852 note_list_got_semicolon (decl_specifiers);
6854 /* Discard access checks no longer in use. */
6855 parser->access_checks_lists = TREE_CHAIN (parser->access_checks_lists);
6858 /* Parse a decl-specifier-seq.
6861 decl-specifier-seq [opt] decl-specifier
6864 storage-class-specifier
6873 decl-specifier-seq [opt] attributes
6875 Returns a TREE_LIST, giving the decl-specifiers in the order they
6876 appear in the source code. The TREE_VALUE of each node is the
6877 decl-specifier. For a keyword (such as `auto' or `friend'), the
6878 TREE_VALUE is simply the correspoding TREE_IDENTIFIER. For the
6879 representation of a type-specifier, see cp_parser_type_specifier.
6881 If there are attributes, they will be stored in *ATTRIBUTES,
6882 represented as described above cp_parser_attributes.
6884 If FRIEND_IS_NOT_CLASS_P is non-NULL, and the `friend' specifier
6885 appears, and the entity that will be a friend is not going to be a
6886 class, then *FRIEND_IS_NOT_CLASS_P will be set to TRUE. Note that
6887 even if *FRIEND_IS_NOT_CLASS_P is FALSE, the entity to which
6888 friendship is granted might not be a class. */
6891 cp_parser_decl_specifier_seq (parser, flags, attributes,
6892 declares_class_or_enum)
6894 cp_parser_flags flags;
6896 bool *declares_class_or_enum;
6898 tree decl_specs = NULL_TREE;
6899 bool friend_p = false;
6900 bool constructor_possible_p = true;
6902 /* Assume no class or enumeration type is declared. */
6903 *declares_class_or_enum = false;
6905 /* Assume there are no attributes. */
6906 *attributes = NULL_TREE;
6908 /* Keep reading specifiers until there are no more to read. */
6911 tree decl_spec = NULL_TREE;
6915 /* Peek at the next token. */
6916 token = cp_lexer_peek_token (parser->lexer);
6917 /* Handle attributes. */
6918 if (token->keyword == RID_ATTRIBUTE)
6920 /* Parse the attributes. */
6921 decl_spec = cp_parser_attributes_opt (parser);
6922 /* Add them to the list. */
6923 *attributes = chainon (*attributes, decl_spec);
6926 /* If the next token is an appropriate keyword, we can simply
6927 add it to the list. */
6928 switch (token->keyword)
6934 /* The representation of the specifier is simply the
6935 appropriate TREE_IDENTIFIER node. */
6936 decl_spec = token->value;
6937 /* Consume the token. */
6938 cp_lexer_consume_token (parser->lexer);
6941 /* function-specifier:
6948 decl_spec = cp_parser_function_specifier_opt (parser);
6954 /* The representation of the specifier is simply the
6955 appropriate TREE_IDENTIFIER node. */
6956 decl_spec = token->value;
6957 /* Consume the token. */
6958 cp_lexer_consume_token (parser->lexer);
6959 /* A constructor declarator cannot appear in a typedef. */
6960 constructor_possible_p = false;
6963 /* storage-class-specifier:
6978 decl_spec = cp_parser_storage_class_specifier_opt (parser);
6985 /* Constructors are a special case. The `S' in `S()' is not a
6986 decl-specifier; it is the beginning of the declarator. */
6987 constructor_p = (!decl_spec
6988 && constructor_possible_p
6989 && cp_parser_constructor_declarator_p (parser,
6992 /* If we don't have a DECL_SPEC yet, then we must be looking at
6993 a type-specifier. */
6994 if (!decl_spec && !constructor_p)
6996 bool decl_spec_declares_class_or_enum;
6997 bool is_cv_qualifier;
7000 = cp_parser_type_specifier (parser, flags,
7002 /*is_declaration=*/true,
7003 &decl_spec_declares_class_or_enum,
7006 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
7008 /* If this type-specifier referenced a user-defined type
7009 (a typedef, class-name, etc.), then we can't allow any
7010 more such type-specifiers henceforth.
7014 The longest sequence of decl-specifiers that could
7015 possibly be a type name is taken as the
7016 decl-specifier-seq of a declaration. The sequence shall
7017 be self-consistent as described below.
7021 As a general rule, at most one type-specifier is allowed
7022 in the complete decl-specifier-seq of a declaration. The
7023 only exceptions are the following:
7025 -- const or volatile can be combined with any other
7028 -- signed or unsigned can be combined with char, long,
7036 void g (const int Pc);
7038 Here, Pc is *not* part of the decl-specifier seq; it's
7039 the declarator. Therefore, once we see a type-specifier
7040 (other than a cv-qualifier), we forbid any additional
7041 user-defined types. We *do* still allow things like `int
7042 int' to be considered a decl-specifier-seq, and issue the
7043 error message later. */
7044 if (decl_spec && !is_cv_qualifier)
7045 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
7046 /* A constructor declarator cannot follow a type-specifier. */
7048 constructor_possible_p = false;
7051 /* If we still do not have a DECL_SPEC, then there are no more
7055 /* Issue an error message, unless the entire construct was
7057 if (!(flags & CP_PARSER_FLAGS_OPTIONAL))
7059 cp_parser_error (parser, "expected decl specifier");
7060 return error_mark_node;
7066 /* Add the DECL_SPEC to the list of specifiers. */
7067 decl_specs = tree_cons (NULL_TREE, decl_spec, decl_specs);
7069 /* After we see one decl-specifier, further decl-specifiers are
7071 flags |= CP_PARSER_FLAGS_OPTIONAL;
7074 /* We have built up the DECL_SPECS in reverse order. Return them in
7075 the correct order. */
7076 return nreverse (decl_specs);
7079 /* Parse an (optional) storage-class-specifier.
7081 storage-class-specifier:
7090 storage-class-specifier:
7093 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
7096 cp_parser_storage_class_specifier_opt (parser)
7099 switch (cp_lexer_peek_token (parser->lexer)->keyword)
7107 /* Consume the token. */
7108 return cp_lexer_consume_token (parser->lexer)->value;
7115 /* Parse an (optional) function-specifier.
7122 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
7125 cp_parser_function_specifier_opt (parser)
7128 switch (cp_lexer_peek_token (parser->lexer)->keyword)
7133 /* Consume the token. */
7134 return cp_lexer_consume_token (parser->lexer)->value;
7141 /* Parse a linkage-specification.
7143 linkage-specification:
7144 extern string-literal { declaration-seq [opt] }
7145 extern string-literal declaration */
7148 cp_parser_linkage_specification (parser)
7154 /* Look for the `extern' keyword. */
7155 cp_parser_require_keyword (parser, RID_EXTERN, "`extern'");
7157 /* Peek at the next token. */
7158 token = cp_lexer_peek_token (parser->lexer);
7159 /* If it's not a string-literal, then there's a problem. */
7160 if (!cp_parser_is_string_literal (token))
7162 cp_parser_error (parser, "expected language-name");
7165 /* Consume the token. */
7166 cp_lexer_consume_token (parser->lexer);
7168 /* Transform the literal into an identifier. If the literal is a
7169 wide-character string, or contains embedded NULs, then we can't
7170 handle it as the user wants. */
7171 if (token->type == CPP_WSTRING
7172 || (strlen (TREE_STRING_POINTER (token->value))
7173 != (size_t) (TREE_STRING_LENGTH (token->value) - 1)))
7175 cp_parser_error (parser, "invalid linkage-specification");
7176 /* Assume C++ linkage. */
7177 linkage = get_identifier ("c++");
7179 /* If it's a simple string constant, things are easier. */
7181 linkage = get_identifier (TREE_STRING_POINTER (token->value));
7183 /* We're now using the new linkage. */
7184 push_lang_context (linkage);
7186 /* If the next token is a `{', then we're using the first
7188 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7190 /* Consume the `{' token. */
7191 cp_lexer_consume_token (parser->lexer);
7192 /* Parse the declarations. */
7193 cp_parser_declaration_seq_opt (parser);
7194 /* Look for the closing `}'. */
7195 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
7197 /* Otherwise, there's just one declaration. */
7200 bool saved_in_unbraced_linkage_specification_p;
7202 saved_in_unbraced_linkage_specification_p
7203 = parser->in_unbraced_linkage_specification_p;
7204 parser->in_unbraced_linkage_specification_p = true;
7205 have_extern_spec = true;
7206 cp_parser_declaration (parser);
7207 have_extern_spec = false;
7208 parser->in_unbraced_linkage_specification_p
7209 = saved_in_unbraced_linkage_specification_p;
7212 /* We're done with the linkage-specification. */
7213 pop_lang_context ();
7216 /* Special member functions [gram.special] */
7218 /* Parse a conversion-function-id.
7220 conversion-function-id:
7221 operator conversion-type-id
7223 Returns an IDENTIFIER_NODE representing the operator. */
7226 cp_parser_conversion_function_id (parser)
7231 tree saved_qualifying_scope;
7232 tree saved_object_scope;
7234 /* Look for the `operator' token. */
7235 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
7236 return error_mark_node;
7237 /* When we parse the conversion-type-id, the current scope will be
7238 reset. However, we need that information in able to look up the
7239 conversion function later, so we save it here. */
7240 saved_scope = parser->scope;
7241 saved_qualifying_scope = parser->qualifying_scope;
7242 saved_object_scope = parser->object_scope;
7243 /* We must enter the scope of the class so that the names of
7244 entities declared within the class are available in the
7245 conversion-type-id. For example, consider:
7252 S::operator I() { ... }
7254 In order to see that `I' is a type-name in the definition, we
7255 must be in the scope of `S'. */
7257 push_scope (saved_scope);
7258 /* Parse the conversion-type-id. */
7259 type = cp_parser_conversion_type_id (parser);
7260 /* Leave the scope of the class, if any. */
7262 pop_scope (saved_scope);
7263 /* Restore the saved scope. */
7264 parser->scope = saved_scope;
7265 parser->qualifying_scope = saved_qualifying_scope;
7266 parser->object_scope = saved_object_scope;
7267 /* If the TYPE is invalid, indicate failure. */
7268 if (type == error_mark_node)
7269 return error_mark_node;
7270 return mangle_conv_op_name_for_type (type);
7273 /* Parse a conversion-type-id:
7276 type-specifier-seq conversion-declarator [opt]
7278 Returns the TYPE specified. */
7281 cp_parser_conversion_type_id (parser)
7285 tree type_specifiers;
7288 /* Parse the attributes. */
7289 attributes = cp_parser_attributes_opt (parser);
7290 /* Parse the type-specifiers. */
7291 type_specifiers = cp_parser_type_specifier_seq (parser);
7292 /* If that didn't work, stop. */
7293 if (type_specifiers == error_mark_node)
7294 return error_mark_node;
7295 /* Parse the conversion-declarator. */
7296 declarator = cp_parser_conversion_declarator_opt (parser);
7298 return grokdeclarator (declarator, type_specifiers, TYPENAME,
7299 /*initialized=*/0, &attributes);
7302 /* Parse an (optional) conversion-declarator.
7304 conversion-declarator:
7305 ptr-operator conversion-declarator [opt]
7307 Returns a representation of the declarator. See
7308 cp_parser_declarator for details. */
7311 cp_parser_conversion_declarator_opt (parser)
7314 enum tree_code code;
7316 tree cv_qualifier_seq;
7318 /* We don't know if there's a ptr-operator next, or not. */
7319 cp_parser_parse_tentatively (parser);
7320 /* Try the ptr-operator. */
7321 code = cp_parser_ptr_operator (parser, &class_type,
7323 /* If it worked, look for more conversion-declarators. */
7324 if (cp_parser_parse_definitely (parser))
7328 /* Parse another optional declarator. */
7329 declarator = cp_parser_conversion_declarator_opt (parser);
7331 /* Create the representation of the declarator. */
7332 if (code == INDIRECT_REF)
7333 declarator = make_pointer_declarator (cv_qualifier_seq,
7336 declarator = make_reference_declarator (cv_qualifier_seq,
7339 /* Handle the pointer-to-member case. */
7341 declarator = build_nt (SCOPE_REF, class_type, declarator);
7349 /* Parse an (optional) ctor-initializer.
7352 : mem-initializer-list
7354 Returns TRUE iff the ctor-initializer was actually present. */
7357 cp_parser_ctor_initializer_opt (parser)
7360 /* If the next token is not a `:', then there is no
7361 ctor-initializer. */
7362 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
7364 /* Do default initialization of any bases and members. */
7365 if (DECL_CONSTRUCTOR_P (current_function_decl))
7366 finish_mem_initializers (NULL_TREE);
7371 /* Consume the `:' token. */
7372 cp_lexer_consume_token (parser->lexer);
7373 /* And the mem-initializer-list. */
7374 cp_parser_mem_initializer_list (parser);
7379 /* Parse a mem-initializer-list.
7381 mem-initializer-list:
7383 mem-initializer , mem-initializer-list */
7386 cp_parser_mem_initializer_list (parser)
7389 tree mem_initializer_list = NULL_TREE;
7391 /* Let the semantic analysis code know that we are starting the
7392 mem-initializer-list. */
7393 begin_mem_initializers ();
7395 /* Loop through the list. */
7398 tree mem_initializer;
7400 /* Parse the mem-initializer. */
7401 mem_initializer = cp_parser_mem_initializer (parser);
7402 /* Add it to the list, unless it was erroneous. */
7403 if (mem_initializer)
7405 TREE_CHAIN (mem_initializer) = mem_initializer_list;
7406 mem_initializer_list = mem_initializer;
7408 /* If the next token is not a `,', we're done. */
7409 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7411 /* Consume the `,' token. */
7412 cp_lexer_consume_token (parser->lexer);
7415 /* Perform semantic analysis. */
7416 finish_mem_initializers (mem_initializer_list);
7419 /* Parse a mem-initializer.
7422 mem-initializer-id ( expression-list [opt] )
7427 ( expresion-list [opt] )
7429 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
7430 class) or FIELD_DECL (for a non-static data member) to initialize;
7431 the TREE_VALUE is the expression-list. */
7434 cp_parser_mem_initializer (parser)
7437 tree mem_initializer_id;
7438 tree expression_list;
7440 /* Find out what is being initialized. */
7441 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7443 pedwarn ("anachronistic old-style base class initializer");
7444 mem_initializer_id = NULL_TREE;
7447 mem_initializer_id = cp_parser_mem_initializer_id (parser);
7448 /* Look for the opening `('. */
7449 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
7450 /* Parse the expression-list. */
7451 if (cp_lexer_next_token_is_not (parser->lexer,
7453 expression_list = cp_parser_expression_list (parser);
7455 expression_list = void_type_node;
7456 /* Look for the closing `)'. */
7457 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
7459 return expand_member_init (mem_initializer_id,
7463 /* Parse a mem-initializer-id.
7466 :: [opt] nested-name-specifier [opt] class-name
7469 Returns a TYPE indicating the class to be initializer for the first
7470 production. Returns an IDENTIFIER_NODE indicating the data member
7471 to be initialized for the second production. */
7474 cp_parser_mem_initializer_id (parser)
7477 bool global_scope_p;
7478 bool nested_name_specifier_p;
7481 /* Look for the optional `::' operator. */
7483 = (cp_parser_global_scope_opt (parser,
7484 /*current_scope_valid_p=*/false)
7486 /* Look for the optional nested-name-specifier. The simplest way to
7491 The keyword `typename' is not permitted in a base-specifier or
7492 mem-initializer; in these contexts a qualified name that
7493 depends on a template-parameter is implicitly assumed to be a
7496 is to assume that we have seen the `typename' keyword at this
7498 nested_name_specifier_p
7499 = (cp_parser_nested_name_specifier_opt (parser,
7500 /*typename_keyword_p=*/true,
7501 /*check_dependency_p=*/true,
7504 /* If there is a `::' operator or a nested-name-specifier, then we
7505 are definitely looking for a class-name. */
7506 if (global_scope_p || nested_name_specifier_p)
7507 return cp_parser_class_name (parser,
7508 /*typename_keyword_p=*/true,
7509 /*template_keyword_p=*/false,
7511 /*check_access_p=*/true,
7512 /*check_dependency_p=*/true,
7513 /*class_head_p=*/false);
7514 /* Otherwise, we could also be looking for an ordinary identifier. */
7515 cp_parser_parse_tentatively (parser);
7516 /* Try a class-name. */
7517 id = cp_parser_class_name (parser,
7518 /*typename_keyword_p=*/true,
7519 /*template_keyword_p=*/false,
7521 /*check_access_p=*/true,
7522 /*check_dependency_p=*/true,
7523 /*class_head_p=*/false);
7524 /* If we found one, we're done. */
7525 if (cp_parser_parse_definitely (parser))
7527 /* Otherwise, look for an ordinary identifier. */
7528 return cp_parser_identifier (parser);
7531 /* Overloading [gram.over] */
7533 /* Parse an operator-function-id.
7535 operator-function-id:
7538 Returns an IDENTIFIER_NODE for the operator which is a
7539 human-readable spelling of the identifier, e.g., `operator +'. */
7542 cp_parser_operator_function_id (parser)
7545 /* Look for the `operator' keyword. */
7546 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
7547 return error_mark_node;
7548 /* And then the name of the operator itself. */
7549 return cp_parser_operator (parser);
7552 /* Parse an operator.
7555 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
7556 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
7557 || ++ -- , ->* -> () []
7564 Returns an IDENTIFIER_NODE for the operator which is a
7565 human-readable spelling of the identifier, e.g., `operator +'. */
7568 cp_parser_operator (parser)
7571 tree id = NULL_TREE;
7574 /* Peek at the next token. */
7575 token = cp_lexer_peek_token (parser->lexer);
7576 /* Figure out which operator we have. */
7577 switch (token->type)
7583 /* The keyword should be either `new' or `delete'. */
7584 if (token->keyword == RID_NEW)
7586 else if (token->keyword == RID_DELETE)
7591 /* Consume the `new' or `delete' token. */
7592 cp_lexer_consume_token (parser->lexer);
7594 /* Peek at the next token. */
7595 token = cp_lexer_peek_token (parser->lexer);
7596 /* If it's a `[' token then this is the array variant of the
7598 if (token->type == CPP_OPEN_SQUARE)
7600 /* Consume the `[' token. */
7601 cp_lexer_consume_token (parser->lexer);
7602 /* Look for the `]' token. */
7603 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
7604 id = ansi_opname (op == NEW_EXPR
7605 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
7607 /* Otherwise, we have the non-array variant. */
7609 id = ansi_opname (op);
7615 id = ansi_opname (PLUS_EXPR);
7619 id = ansi_opname (MINUS_EXPR);
7623 id = ansi_opname (MULT_EXPR);
7627 id = ansi_opname (TRUNC_DIV_EXPR);
7631 id = ansi_opname (TRUNC_MOD_EXPR);
7635 id = ansi_opname (BIT_XOR_EXPR);
7639 id = ansi_opname (BIT_AND_EXPR);
7643 id = ansi_opname (BIT_IOR_EXPR);
7647 id = ansi_opname (BIT_NOT_EXPR);
7651 id = ansi_opname (TRUTH_NOT_EXPR);
7655 id = ansi_assopname (NOP_EXPR);
7659 id = ansi_opname (LT_EXPR);
7663 id = ansi_opname (GT_EXPR);
7667 id = ansi_assopname (PLUS_EXPR);
7671 id = ansi_assopname (MINUS_EXPR);
7675 id = ansi_assopname (MULT_EXPR);
7679 id = ansi_assopname (TRUNC_DIV_EXPR);
7683 id = ansi_assopname (TRUNC_MOD_EXPR);
7687 id = ansi_assopname (BIT_XOR_EXPR);
7691 id = ansi_assopname (BIT_AND_EXPR);
7695 id = ansi_assopname (BIT_IOR_EXPR);
7699 id = ansi_opname (LSHIFT_EXPR);
7703 id = ansi_opname (RSHIFT_EXPR);
7707 id = ansi_assopname (LSHIFT_EXPR);
7711 id = ansi_assopname (RSHIFT_EXPR);
7715 id = ansi_opname (EQ_EXPR);
7719 id = ansi_opname (NE_EXPR);
7723 id = ansi_opname (LE_EXPR);
7726 case CPP_GREATER_EQ:
7727 id = ansi_opname (GE_EXPR);
7731 id = ansi_opname (TRUTH_ANDIF_EXPR);
7735 id = ansi_opname (TRUTH_ORIF_EXPR);
7739 id = ansi_opname (POSTINCREMENT_EXPR);
7742 case CPP_MINUS_MINUS:
7743 id = ansi_opname (PREDECREMENT_EXPR);
7747 id = ansi_opname (COMPOUND_EXPR);
7750 case CPP_DEREF_STAR:
7751 id = ansi_opname (MEMBER_REF);
7755 id = ansi_opname (COMPONENT_REF);
7758 case CPP_OPEN_PAREN:
7759 /* Consume the `('. */
7760 cp_lexer_consume_token (parser->lexer);
7761 /* Look for the matching `)'. */
7762 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
7763 return ansi_opname (CALL_EXPR);
7765 case CPP_OPEN_SQUARE:
7766 /* Consume the `['. */
7767 cp_lexer_consume_token (parser->lexer);
7768 /* Look for the matching `]'. */
7769 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
7770 return ansi_opname (ARRAY_REF);
7774 id = ansi_opname (MIN_EXPR);
7778 id = ansi_opname (MAX_EXPR);
7782 id = ansi_assopname (MIN_EXPR);
7786 id = ansi_assopname (MAX_EXPR);
7790 /* Anything else is an error. */
7794 /* If we have selected an identifier, we need to consume the
7797 cp_lexer_consume_token (parser->lexer);
7798 /* Otherwise, no valid operator name was present. */
7801 cp_parser_error (parser, "expected operator");
7802 id = error_mark_node;
7808 /* Parse a template-declaration.
7810 template-declaration:
7811 export [opt] template < template-parameter-list > declaration
7813 If MEMBER_P is TRUE, this template-declaration occurs within a
7816 The grammar rule given by the standard isn't correct. What
7819 template-declaration:
7820 export [opt] template-parameter-list-seq
7821 decl-specifier-seq [opt] init-declarator [opt] ;
7822 export [opt] template-parameter-list-seq
7825 template-parameter-list-seq:
7826 template-parameter-list-seq [opt]
7827 template < template-parameter-list > */
7830 cp_parser_template_declaration (parser, member_p)
7834 /* Check for `export'. */
7835 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
7837 /* Consume the `export' token. */
7838 cp_lexer_consume_token (parser->lexer);
7839 /* Warn that we do not support `export'. */
7840 warning ("keyword `export' not implemented, and will be ignored");
7843 cp_parser_template_declaration_after_export (parser, member_p);
7846 /* Parse a template-parameter-list.
7848 template-parameter-list:
7850 template-parameter-list , template-parameter
7852 Returns a TREE_LIST. Each node represents a template parameter.
7853 The nodes are connected via their TREE_CHAINs. */
7856 cp_parser_template_parameter_list (parser)
7859 tree parameter_list = NULL_TREE;
7866 /* Parse the template-parameter. */
7867 parameter = cp_parser_template_parameter (parser);
7868 /* Add it to the list. */
7869 parameter_list = process_template_parm (parameter_list,
7872 /* Peek at the next token. */
7873 token = cp_lexer_peek_token (parser->lexer);
7874 /* If it's not a `,', we're done. */
7875 if (token->type != CPP_COMMA)
7877 /* Otherwise, consume the `,' token. */
7878 cp_lexer_consume_token (parser->lexer);
7881 return parameter_list;
7884 /* Parse a template-parameter.
7888 parameter-declaration
7890 Returns a TREE_LIST. The TREE_VALUE represents the parameter. The
7891 TREE_PURPOSE is the default value, if any. */
7894 cp_parser_template_parameter (parser)
7899 /* Peek at the next token. */
7900 token = cp_lexer_peek_token (parser->lexer);
7901 /* If it is `class' or `template', we have a type-parameter. */
7902 if (token->keyword == RID_TEMPLATE)
7903 return cp_parser_type_parameter (parser);
7904 /* If it is `class' or `typename' we do not know yet whether it is a
7905 type parameter or a non-type parameter. Consider:
7907 template <typename T, typename T::X X> ...
7911 template <class C, class D*> ...
7913 Here, the first parameter is a type parameter, and the second is
7914 a non-type parameter. We can tell by looking at the token after
7915 the identifier -- if it is a `,', `=', or `>' then we have a type
7917 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
7919 /* Peek at the token after `class' or `typename'. */
7920 token = cp_lexer_peek_nth_token (parser->lexer, 2);
7921 /* If it's an identifier, skip it. */
7922 if (token->type == CPP_NAME)
7923 token = cp_lexer_peek_nth_token (parser->lexer, 3);
7924 /* Now, see if the token looks like the end of a template
7926 if (token->type == CPP_COMMA
7927 || token->type == CPP_EQ
7928 || token->type == CPP_GREATER)
7929 return cp_parser_type_parameter (parser);
7932 /* Otherwise, it is a non-type parameter.
7936 When parsing a default template-argument for a non-type
7937 template-parameter, the first non-nested `>' is taken as the end
7938 of the template parameter-list rather than a greater-than
7941 cp_parser_parameter_declaration (parser, /*template_parm_p=*/true);
7944 /* Parse a type-parameter.
7947 class identifier [opt]
7948 class identifier [opt] = type-id
7949 typename identifier [opt]
7950 typename identifier [opt] = type-id
7951 template < template-parameter-list > class identifier [opt]
7952 template < template-parameter-list > class identifier [opt]
7955 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
7956 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
7957 the declaration of the parameter. */
7960 cp_parser_type_parameter (parser)
7966 /* Look for a keyword to tell us what kind of parameter this is. */
7967 token = cp_parser_require (parser, CPP_KEYWORD,
7968 "expected `class', `typename', or `template'");
7970 return error_mark_node;
7972 switch (token->keyword)
7978 tree default_argument;
7980 /* If the next token is an identifier, then it names the
7982 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
7983 identifier = cp_parser_identifier (parser);
7985 identifier = NULL_TREE;
7987 /* Create the parameter. */
7988 parameter = finish_template_type_parm (class_type_node, identifier);
7990 /* If the next token is an `=', we have a default argument. */
7991 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7993 /* Consume the `=' token. */
7994 cp_lexer_consume_token (parser->lexer);
7995 /* Parse the default-argumen. */
7996 default_argument = cp_parser_type_id (parser);
7999 default_argument = NULL_TREE;
8001 /* Create the combined representation of the parameter and the
8002 default argument. */
8003 parameter = build_tree_list (default_argument,
8010 tree parameter_list;
8012 tree default_argument;
8014 /* Look for the `<'. */
8015 cp_parser_require (parser, CPP_LESS, "`<'");
8016 /* Parse the template-parameter-list. */
8017 begin_template_parm_list ();
8019 = cp_parser_template_parameter_list (parser);
8020 parameter_list = end_template_parm_list (parameter_list);
8021 /* Look for the `>'. */
8022 cp_parser_require (parser, CPP_GREATER, "`>'");
8023 /* Look for the `class' keyword. */
8024 cp_parser_require_keyword (parser, RID_CLASS, "`class'");
8025 /* If the next token is an `=', then there is a
8026 default-argument. If the next token is a `>', we are at
8027 the end of the parameter-list. If the next token is a `,',
8028 then we are at the end of this parameter. */
8029 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
8030 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
8031 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8032 identifier = cp_parser_identifier (parser);
8034 identifier = NULL_TREE;
8035 /* Create the template parameter. */
8036 parameter = finish_template_template_parm (class_type_node,
8039 /* If the next token is an `=', then there is a
8040 default-argument. */
8041 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8043 /* Consume the `='. */
8044 cp_lexer_consume_token (parser->lexer);
8045 /* Parse the id-expression. */
8047 = cp_parser_id_expression (parser,
8048 /*template_keyword_p=*/false,
8049 /*check_dependency_p=*/true,
8050 /*template_p=*/NULL);
8051 /* Look up the name. */
8053 = cp_parser_lookup_name_simple (parser, default_argument);
8054 /* See if the default argument is valid. */
8056 = check_template_template_default_arg (default_argument);
8059 default_argument = NULL_TREE;
8061 /* Create the combined representation of the parameter and the
8062 default argument. */
8063 parameter = build_tree_list (default_argument,
8069 /* Anything else is an error. */
8070 cp_parser_error (parser,
8071 "expected `class', `typename', or `template'");
8072 parameter = error_mark_node;
8078 /* Parse a template-id.
8081 template-name < template-argument-list [opt] >
8083 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
8084 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
8085 returned. Otherwise, if the template-name names a function, or set
8086 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
8087 names a class, returns a TYPE_DECL for the specialization.
8089 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
8090 uninstantiated templates. */
8093 cp_parser_template_id (cp_parser *parser,
8094 bool template_keyword_p,
8095 bool check_dependency_p)
8100 tree saved_qualifying_scope;
8101 tree saved_object_scope;
8103 bool saved_greater_than_is_operator_p;
8104 ptrdiff_t start_of_id;
8105 tree access_check = NULL_TREE;
8106 cp_token *next_token;
8108 /* If the next token corresponds to a template-id, there is no need
8110 next_token = cp_lexer_peek_token (parser->lexer);
8111 if (next_token->type == CPP_TEMPLATE_ID)
8116 /* Get the stored value. */
8117 value = cp_lexer_consume_token (parser->lexer)->value;
8118 /* Perform any access checks that were deferred. */
8119 for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
8120 cp_parser_defer_access_check (parser,
8121 TREE_PURPOSE (check),
8122 TREE_VALUE (check));
8123 /* Return the stored value. */
8124 return TREE_VALUE (value);
8127 /* Avoid performing name lookup if there is no possibility of
8128 finding a template-id. */
8129 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
8130 || (next_token->type == CPP_NAME
8131 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_LESS))
8133 cp_parser_error (parser, "expected template-id");
8134 return error_mark_node;
8137 /* Remember where the template-id starts. */
8138 if (cp_parser_parsing_tentatively (parser)
8139 && !cp_parser_committed_to_tentative_parse (parser))
8141 next_token = cp_lexer_peek_token (parser->lexer);
8142 start_of_id = cp_lexer_token_difference (parser->lexer,
8143 parser->lexer->first_token,
8145 access_check = parser->context->deferred_access_checks;
8150 /* Parse the template-name. */
8151 template = cp_parser_template_name (parser, template_keyword_p,
8152 check_dependency_p);
8153 if (template == error_mark_node)
8154 return error_mark_node;
8156 /* Look for the `<' that starts the template-argument-list. */
8157 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
8158 return error_mark_node;
8162 When parsing a template-id, the first non-nested `>' is taken as
8163 the end of the template-argument-list rather than a greater-than
8165 saved_greater_than_is_operator_p
8166 = parser->greater_than_is_operator_p;
8167 parser->greater_than_is_operator_p = false;
8168 /* Parsing the argument list may modify SCOPE, so we save it
8170 saved_scope = parser->scope;
8171 saved_qualifying_scope = parser->qualifying_scope;
8172 saved_object_scope = parser->object_scope;
8173 /* Parse the template-argument-list itself. */
8174 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
8175 arguments = NULL_TREE;
8177 arguments = cp_parser_template_argument_list (parser);
8178 /* Look for the `>' that ends the template-argument-list. */
8179 cp_parser_require (parser, CPP_GREATER, "`>'");
8180 /* The `>' token might be a greater-than operator again now. */
8181 parser->greater_than_is_operator_p
8182 = saved_greater_than_is_operator_p;
8183 /* Restore the SAVED_SCOPE. */
8184 parser->scope = saved_scope;
8185 parser->qualifying_scope = saved_qualifying_scope;
8186 parser->object_scope = saved_object_scope;
8188 /* Build a representation of the specialization. */
8189 if (TREE_CODE (template) == IDENTIFIER_NODE)
8190 template_id = build_min_nt (TEMPLATE_ID_EXPR, template, arguments);
8191 else if (DECL_CLASS_TEMPLATE_P (template)
8192 || DECL_TEMPLATE_TEMPLATE_PARM_P (template))
8194 = finish_template_type (template, arguments,
8195 cp_lexer_next_token_is (parser->lexer,
8199 /* If it's not a class-template or a template-template, it should be
8200 a function-template. */
8201 my_friendly_assert ((DECL_FUNCTION_TEMPLATE_P (template)
8202 || TREE_CODE (template) == OVERLOAD
8203 || BASELINK_P (template)),
8206 template_id = lookup_template_function (template, arguments);
8209 /* If parsing tentatively, replace the sequence of tokens that makes
8210 up the template-id with a CPP_TEMPLATE_ID token. That way,
8211 should we re-parse the token stream, we will not have to repeat
8212 the effort required to do the parse, nor will we issue duplicate
8213 error messages about problems during instantiation of the
8215 if (start_of_id >= 0)
8220 /* Find the token that corresponds to the start of the
8222 token = cp_lexer_advance_token (parser->lexer,
8223 parser->lexer->first_token,
8226 /* Remember the access checks associated with this
8227 nested-name-specifier. */
8228 c = parser->context->deferred_access_checks;
8229 if (c == access_check)
8230 access_check = NULL_TREE;
8233 while (TREE_CHAIN (c) != access_check)
8235 access_check = parser->context->deferred_access_checks;
8236 parser->context->deferred_access_checks = TREE_CHAIN (c);
8237 TREE_CHAIN (c) = NULL_TREE;
8240 /* Reset the contents of the START_OF_ID token. */
8241 token->type = CPP_TEMPLATE_ID;
8242 token->value = build_tree_list (access_check, template_id);
8243 token->keyword = RID_MAX;
8244 /* Purge all subsequent tokens. */
8245 cp_lexer_purge_tokens_after (parser->lexer, token);
8251 /* Parse a template-name.
8256 The standard should actually say:
8260 operator-function-id
8261 conversion-function-id
8263 A defect report has been filed about this issue.
8265 If TEMPLATE_KEYWORD_P is true, then we have just seen the
8266 `template' keyword, in a construction like:
8270 In that case `f' is taken to be a template-name, even though there
8271 is no way of knowing for sure.
8273 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
8274 name refers to a set of overloaded functions, at least one of which
8275 is a template, or an IDENTIFIER_NODE with the name of the template,
8276 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
8277 names are looked up inside uninstantiated templates. */
8280 cp_parser_template_name (parser, template_keyword_p, check_dependency_p)
8282 bool template_keyword_p;
8283 bool check_dependency_p;
8289 /* If the next token is `operator', then we have either an
8290 operator-function-id or a conversion-function-id. */
8291 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
8293 /* We don't know whether we're looking at an
8294 operator-function-id or a conversion-function-id. */
8295 cp_parser_parse_tentatively (parser);
8296 /* Try an operator-function-id. */
8297 identifier = cp_parser_operator_function_id (parser);
8298 /* If that didn't work, try a conversion-function-id. */
8299 if (!cp_parser_parse_definitely (parser))
8300 identifier = cp_parser_conversion_function_id (parser);
8302 /* Look for the identifier. */
8304 identifier = cp_parser_identifier (parser);
8306 /* If we didn't find an identifier, we don't have a template-id. */
8307 if (identifier == error_mark_node)
8308 return error_mark_node;
8310 /* If the name immediately followed the `template' keyword, then it
8311 is a template-name. However, if the next token is not `<', then
8312 we do not treat it as a template-name, since it is not being used
8313 as part of a template-id. This enables us to handle constructs
8316 template <typename T> struct S { S(); };
8317 template <typename T> S<T>::S();
8319 correctly. We would treat `S' as a template -- if it were `S<T>'
8320 -- but we do not if there is no `<'. */
8321 if (template_keyword_p && processing_template_decl
8322 && cp_lexer_next_token_is (parser->lexer, CPP_LESS))
8325 /* Look up the name. */
8326 decl = cp_parser_lookup_name (parser, identifier,
8327 /*check_access=*/true,
8329 /*is_namespace=*/false,
8330 check_dependency_p);
8331 decl = maybe_get_template_decl_from_type_decl (decl);
8333 /* If DECL is a template, then the name was a template-name. */
8334 if (TREE_CODE (decl) == TEMPLATE_DECL)
8338 /* The standard does not explicitly indicate whether a name that
8339 names a set of overloaded declarations, some of which are
8340 templates, is a template-name. However, such a name should
8341 be a template-name; otherwise, there is no way to form a
8342 template-id for the overloaded templates. */
8343 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
8344 if (TREE_CODE (fns) == OVERLOAD)
8348 for (fn = fns; fn; fn = OVL_NEXT (fn))
8349 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
8354 /* Otherwise, the name does not name a template. */
8355 cp_parser_error (parser, "expected template-name");
8356 return error_mark_node;
8360 /* If DECL is dependent, and refers to a function, then just return
8361 its name; we will look it up again during template instantiation. */
8362 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
8364 tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
8365 if (TYPE_P (scope) && cp_parser_dependent_type_p (scope))
8372 /* Parse a template-argument-list.
8374 template-argument-list:
8376 template-argument-list , template-argument
8378 Returns a TREE_LIST representing the arguments, in the order they
8379 appeared. The TREE_VALUE of each node is a representation of the
8383 cp_parser_template_argument_list (parser)
8386 tree arguments = NULL_TREE;
8392 /* Parse the template-argument. */
8393 argument = cp_parser_template_argument (parser);
8394 /* Add it to the list. */
8395 arguments = tree_cons (NULL_TREE, argument, arguments);
8396 /* If it is not a `,', then there are no more arguments. */
8397 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8399 /* Otherwise, consume the ','. */
8400 cp_lexer_consume_token (parser->lexer);
8403 /* We built up the arguments in reverse order. */
8404 return nreverse (arguments);
8407 /* Parse a template-argument.
8410 assignment-expression
8414 The representation is that of an assignment-expression, type-id, or
8415 id-expression -- except that the qualified id-expression is
8416 evaluated, so that the value returned is either a DECL or an
8420 cp_parser_template_argument (parser)
8426 /* There's really no way to know what we're looking at, so we just
8427 try each alternative in order.
8431 In a template-argument, an ambiguity between a type-id and an
8432 expression is resolved to a type-id, regardless of the form of
8433 the corresponding template-parameter.
8435 Therefore, we try a type-id first. */
8436 cp_parser_parse_tentatively (parser);
8437 argument = cp_parser_type_id (parser);
8438 /* If the next token isn't a `,' or a `>', then this argument wasn't
8440 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
8441 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER))
8442 cp_parser_error (parser, "expected template-argument");
8443 /* If that worked, we're done. */
8444 if (cp_parser_parse_definitely (parser))
8446 /* We're still not sure what the argument will be. */
8447 cp_parser_parse_tentatively (parser);
8448 /* Try a template. */
8449 argument = cp_parser_id_expression (parser,
8450 /*template_keyword_p=*/false,
8451 /*check_dependency_p=*/true,
8453 /* If the next token isn't a `,' or a `>', then this argument wasn't
8455 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
8456 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER))
8457 cp_parser_error (parser, "expected template-argument");
8458 if (!cp_parser_error_occurred (parser))
8460 /* Figure out what is being referred to. */
8461 argument = cp_parser_lookup_name_simple (parser, argument);
8463 argument = make_unbound_class_template (TREE_OPERAND (argument, 0),
8464 TREE_OPERAND (argument, 1),
8465 tf_error | tf_parsing);
8466 else if (TREE_CODE (argument) != TEMPLATE_DECL)
8467 cp_parser_error (parser, "expected template-name");
8469 if (cp_parser_parse_definitely (parser))
8471 /* It must be an assignment-expression. */
8472 return cp_parser_assignment_expression (parser);
8475 /* Parse an explicit-instantiation.
8477 explicit-instantiation:
8478 template declaration
8480 Although the standard says `declaration', what it really means is:
8482 explicit-instantiation:
8483 template decl-specifier-seq [opt] declarator [opt] ;
8485 Things like `template int S<int>::i = 5, int S<double>::j;' are not
8486 supposed to be allowed. A defect report has been filed about this
8491 explicit-instantiation:
8492 storage-class-specifier template
8493 decl-specifier-seq [opt] declarator [opt] ;
8494 function-specifier template
8495 decl-specifier-seq [opt] declarator [opt] ; */
8498 cp_parser_explicit_instantiation (parser)
8501 bool declares_class_or_enum;
8502 tree decl_specifiers;
8504 tree extension_specifier = NULL_TREE;
8506 /* Look for an (optional) storage-class-specifier or
8507 function-specifier. */
8508 if (cp_parser_allow_gnu_extensions_p (parser))
8511 = cp_parser_storage_class_specifier_opt (parser);
8512 if (!extension_specifier)
8513 extension_specifier = cp_parser_function_specifier_opt (parser);
8516 /* Look for the `template' keyword. */
8517 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
8518 /* Let the front end know that we are processing an explicit
8520 begin_explicit_instantiation ();
8521 /* [temp.explicit] says that we are supposed to ignore access
8522 control while processing explicit instantiation directives. */
8523 scope_chain->check_access = 0;
8524 /* Parse a decl-specifier-seq. */
8526 = cp_parser_decl_specifier_seq (parser,
8527 CP_PARSER_FLAGS_OPTIONAL,
8529 &declares_class_or_enum);
8530 /* If there was exactly one decl-specifier, and it declared a class,
8531 and there's no declarator, then we have an explicit type
8533 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
8537 type = check_tag_decl (decl_specifiers);
8539 do_type_instantiation (type, extension_specifier, /*complain=*/1);
8546 /* Parse the declarator. */
8548 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
8549 /*ctor_dtor_or_conv_p=*/NULL);
8550 decl = grokdeclarator (declarator, decl_specifiers,
8552 /* Do the explicit instantiation. */
8553 do_decl_instantiation (decl, extension_specifier);
8555 /* We're done with the instantiation. */
8556 end_explicit_instantiation ();
8557 /* Trun access control back on. */
8558 scope_chain->check_access = flag_access_control;
8560 /* Look for the trailing `;'. */
8561 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
8564 /* Parse an explicit-specialization.
8566 explicit-specialization:
8567 template < > declaration
8569 Although the standard says `declaration', what it really means is:
8571 explicit-specialization:
8572 template <> decl-specifier [opt] init-declarator [opt] ;
8573 template <> function-definition
8574 template <> explicit-specialization
8575 template <> template-declaration */
8578 cp_parser_explicit_specialization (parser)
8581 /* Look for the `template' keyword. */
8582 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
8583 /* Look for the `<'. */
8584 cp_parser_require (parser, CPP_LESS, "`<'");
8585 /* Look for the `>'. */
8586 cp_parser_require (parser, CPP_GREATER, "`>'");
8587 /* We have processed another parameter list. */
8588 ++parser->num_template_parameter_lists;
8589 /* Let the front end know that we are beginning a specialization. */
8590 begin_specialization ();
8592 /* If the next keyword is `template', we need to figure out whether
8593 or not we're looking a template-declaration. */
8594 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
8596 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
8597 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
8598 cp_parser_template_declaration_after_export (parser,
8599 /*member_p=*/false);
8601 cp_parser_explicit_specialization (parser);
8604 /* Parse the dependent declaration. */
8605 cp_parser_single_declaration (parser,
8609 /* We're done with the specialization. */
8610 end_specialization ();
8611 /* We're done with this parameter list. */
8612 --parser->num_template_parameter_lists;
8615 /* Parse a type-specifier.
8618 simple-type-specifier
8621 elaborated-type-specifier
8629 Returns a representation of the type-specifier. If the
8630 type-specifier is a keyword (like `int' or `const', or
8631 `__complex__') then the correspoding IDENTIFIER_NODE is returned.
8632 For a class-specifier, enum-specifier, or elaborated-type-specifier
8633 a TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
8635 If IS_FRIEND is TRUE then this type-specifier is being declared a
8636 `friend'. If IS_DECLARATION is TRUE, then this type-specifier is
8637 appearing in a decl-specifier-seq.
8639 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
8640 class-specifier, enum-specifier, or elaborated-type-specifier, then
8641 *DECLARES_CLASS_OR_ENUM is set to TRUE. Otherwise, it is set to
8644 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
8645 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
8649 cp_parser_type_specifier (parser,
8653 declares_class_or_enum,
8656 cp_parser_flags flags;
8658 bool is_declaration;
8659 bool *declares_class_or_enum;
8660 bool *is_cv_qualifier;
8662 tree type_spec = NULL_TREE;
8666 /* Assume this type-specifier does not declare a new type. */
8667 if (declares_class_or_enum)
8668 *declares_class_or_enum = false;
8669 /* And that it does not specify a cv-qualifier. */
8670 if (is_cv_qualifier)
8671 *is_cv_qualifier = false;
8672 /* Peek at the next token. */
8673 token = cp_lexer_peek_token (parser->lexer);
8675 /* If we're looking at a keyword, we can use that to guide the
8676 production we choose. */
8677 keyword = token->keyword;
8680 /* Any of these indicate either a class-specifier, or an
8681 elaborated-type-specifier. */
8686 /* Parse tentatively so that we can back up if we don't find a
8687 class-specifier or enum-specifier. */
8688 cp_parser_parse_tentatively (parser);
8689 /* Look for the class-specifier or enum-specifier. */
8690 if (keyword == RID_ENUM)
8691 type_spec = cp_parser_enum_specifier (parser);
8693 type_spec = cp_parser_class_specifier (parser);
8695 /* If that worked, we're done. */
8696 if (cp_parser_parse_definitely (parser))
8698 if (declares_class_or_enum)
8699 *declares_class_or_enum = true;
8706 /* Look for an elaborated-type-specifier. */
8707 type_spec = cp_parser_elaborated_type_specifier (parser,
8710 /* We're declaring a class or enum -- unless we're using
8712 if (declares_class_or_enum && keyword != RID_TYPENAME)
8713 *declares_class_or_enum = true;
8719 type_spec = cp_parser_cv_qualifier_opt (parser);
8720 /* Even though we call a routine that looks for an optional
8721 qualifier, we know that there should be one. */
8722 my_friendly_assert (type_spec != NULL, 20000328);
8723 /* This type-specifier was a cv-qualified. */
8724 if (is_cv_qualifier)
8725 *is_cv_qualifier = true;
8730 /* The `__complex__' keyword is a GNU extension. */
8731 return cp_lexer_consume_token (parser->lexer)->value;
8737 /* If we do not already have a type-specifier, assume we are looking
8738 at a simple-type-specifier. */
8739 type_spec = cp_parser_simple_type_specifier (parser, flags);
8741 /* If we didn't find a type-specifier, and a type-specifier was not
8742 optional in this context, issue an error message. */
8743 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
8745 cp_parser_error (parser, "expected type specifier");
8746 return error_mark_node;
8752 /* Parse a simple-type-specifier.
8754 simple-type-specifier:
8755 :: [opt] nested-name-specifier [opt] type-name
8756 :: [opt] nested-name-specifier template template-id
8771 simple-type-specifier:
8772 __typeof__ unary-expression
8773 __typeof__ ( type-id )
8775 For the various keywords, the value returned is simply the
8776 TREE_IDENTIFIER representing the keyword. For the first two
8777 productions, the value returned is the indicated TYPE_DECL. */
8780 cp_parser_simple_type_specifier (parser, flags)
8782 cp_parser_flags flags;
8784 tree type = NULL_TREE;
8787 /* Peek at the next token. */
8788 token = cp_lexer_peek_token (parser->lexer);
8790 /* If we're looking at a keyword, things are easy. */
8791 switch (token->keyword)
8804 /* Consume the token. */
8805 return cp_lexer_consume_token (parser->lexer)->value;
8811 /* Consume the `typeof' token. */
8812 cp_lexer_consume_token (parser->lexer);
8813 /* Parse the operand to `typeof' */
8814 operand = cp_parser_sizeof_operand (parser, RID_TYPEOF);
8815 /* If it is not already a TYPE, take its type. */
8816 if (!TYPE_P (operand))
8817 operand = finish_typeof (operand);
8826 /* The type-specifier must be a user-defined type. */
8827 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
8829 /* Don't gobble tokens or issue error messages if this is an
8830 optional type-specifier. */
8831 if (flags & CP_PARSER_FLAGS_OPTIONAL)
8832 cp_parser_parse_tentatively (parser);
8834 /* Look for the optional `::' operator. */
8835 cp_parser_global_scope_opt (parser,
8836 /*current_scope_valid_p=*/false);
8837 /* Look for the nested-name specifier. */
8838 cp_parser_nested_name_specifier_opt (parser,
8839 /*typename_keyword_p=*/false,
8840 /*check_dependency_p=*/true,
8842 /* If we have seen a nested-name-specifier, and the next token
8843 is `template', then we are using the template-id production. */
8845 && cp_parser_optional_template_keyword (parser))
8847 /* Look for the template-id. */
8848 type = cp_parser_template_id (parser,
8849 /*template_keyword_p=*/true,
8850 /*check_dependency_p=*/true);
8851 /* If the template-id did not name a type, we are out of
8853 if (TREE_CODE (type) != TYPE_DECL)
8855 cp_parser_error (parser, "expected template-id for type");
8859 /* Otherwise, look for a type-name. */
8862 type = cp_parser_type_name (parser);
8863 if (type == error_mark_node)
8867 /* If it didn't work out, we don't have a TYPE. */
8868 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
8869 && !cp_parser_parse_definitely (parser))
8873 /* If we didn't get a type-name, issue an error message. */
8874 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
8876 cp_parser_error (parser, "expected type-name");
8877 return error_mark_node;
8883 /* Parse a type-name.
8896 Returns a TYPE_DECL for the the type. */
8899 cp_parser_type_name (parser)
8905 /* We can't know yet whether it is a class-name or not. */
8906 cp_parser_parse_tentatively (parser);
8907 /* Try a class-name. */
8908 type_decl = cp_parser_class_name (parser,
8909 /*typename_keyword_p=*/false,
8910 /*template_keyword_p=*/false,
8912 /*check_access_p=*/true,
8913 /*check_dependency_p=*/true,
8914 /*class_head_p=*/false);
8915 /* If it's not a class-name, keep looking. */
8916 if (!cp_parser_parse_definitely (parser))
8918 /* It must be a typedef-name or an enum-name. */
8919 identifier = cp_parser_identifier (parser);
8920 if (identifier == error_mark_node)
8921 return error_mark_node;
8923 /* Look up the type-name. */
8924 type_decl = cp_parser_lookup_name_simple (parser, identifier);
8925 /* Issue an error if we did not find a type-name. */
8926 if (TREE_CODE (type_decl) != TYPE_DECL)
8928 cp_parser_error (parser, "expected type-name");
8929 type_decl = error_mark_node;
8931 /* Remember that the name was used in the definition of the
8932 current class so that we can check later to see if the
8933 meaning would have been different after the class was
8934 entirely defined. */
8935 else if (type_decl != error_mark_node
8937 maybe_note_name_used_in_class (identifier, type_decl);
8944 /* Parse an elaborated-type-specifier. Note that the grammar given
8945 here incorporates the resolution to DR68.
8947 elaborated-type-specifier:
8948 class-key :: [opt] nested-name-specifier [opt] identifier
8949 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
8950 enum :: [opt] nested-name-specifier [opt] identifier
8951 typename :: [opt] nested-name-specifier identifier
8952 typename :: [opt] nested-name-specifier template [opt]
8955 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
8956 declared `friend'. If IS_DECLARATION is TRUE, then this
8957 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
8958 something is being declared.
8960 Returns the TYPE specified. */
8963 cp_parser_elaborated_type_specifier (parser, is_friend, is_declaration)
8966 bool is_declaration;
8968 enum tag_types tag_type;
8970 tree type = NULL_TREE;
8972 /* See if we're looking at the `enum' keyword. */
8973 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
8975 /* Consume the `enum' token. */
8976 cp_lexer_consume_token (parser->lexer);
8977 /* Remember that it's an enumeration type. */
8978 tag_type = enum_type;
8980 /* Or, it might be `typename'. */
8981 else if (cp_lexer_next_token_is_keyword (parser->lexer,
8984 /* Consume the `typename' token. */
8985 cp_lexer_consume_token (parser->lexer);
8986 /* Remember that it's a `typename' type. */
8987 tag_type = typename_type;
8988 /* The `typename' keyword is only allowed in templates. */
8989 if (!processing_template_decl)
8990 pedwarn ("using `typename' outside of template");
8992 /* Otherwise it must be a class-key. */
8995 tag_type = cp_parser_class_key (parser);
8996 if (tag_type == none_type)
8997 return error_mark_node;
9000 /* Look for the `::' operator. */
9001 cp_parser_global_scope_opt (parser,
9002 /*current_scope_valid_p=*/false);
9003 /* Look for the nested-name-specifier. */
9004 if (tag_type == typename_type)
9005 cp_parser_nested_name_specifier (parser,
9006 /*typename_keyword_p=*/true,
9007 /*check_dependency_p=*/true,
9010 /* Even though `typename' is not present, the proposed resolution
9011 to Core Issue 180 says that in `class A<T>::B', `B' should be
9012 considered a type-name, even if `A<T>' is dependent. */
9013 cp_parser_nested_name_specifier_opt (parser,
9014 /*typename_keyword_p=*/true,
9015 /*check_dependency_p=*/true,
9017 /* For everything but enumeration types, consider a template-id. */
9018 if (tag_type != enum_type)
9020 bool template_p = false;
9023 /* Allow the `template' keyword. */
9024 template_p = cp_parser_optional_template_keyword (parser);
9025 /* If we didn't see `template', we don't know if there's a
9026 template-id or not. */
9028 cp_parser_parse_tentatively (parser);
9029 /* Parse the template-id. */
9030 decl = cp_parser_template_id (parser, template_p,
9031 /*check_dependency_p=*/true);
9032 /* If we didn't find a template-id, look for an ordinary
9034 if (!template_p && !cp_parser_parse_definitely (parser))
9036 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
9037 in effect, then we must assume that, upon instantiation, the
9038 template will correspond to a class. */
9039 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
9040 && tag_type == typename_type)
9041 type = make_typename_type (parser->scope, decl,
9044 type = TREE_TYPE (decl);
9047 /* For an enumeration type, consider only a plain identifier. */
9050 identifier = cp_parser_identifier (parser);
9052 if (identifier == error_mark_node)
9053 return error_mark_node;
9055 /* For a `typename', we needn't call xref_tag. */
9056 if (tag_type == typename_type)
9057 return make_typename_type (parser->scope, identifier,
9059 /* Look up a qualified name in the usual way. */
9064 /* In an elaborated-type-specifier, names are assumed to name
9065 types, so we set IS_TYPE to TRUE when calling
9066 cp_parser_lookup_name. */
9067 decl = cp_parser_lookup_name (parser, identifier,
9068 /*check_access=*/true,
9070 /*is_namespace=*/false,
9071 /*check_dependency=*/true);
9072 decl = (cp_parser_maybe_treat_template_as_class
9073 (decl, /*tag_name_p=*/is_friend));
9075 if (TREE_CODE (decl) != TYPE_DECL)
9077 error ("expected type-name");
9078 return error_mark_node;
9080 else if (TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE
9081 && tag_type != enum_type)
9082 error ("`%T' referred to as `%s'", TREE_TYPE (decl),
9083 tag_type == record_type ? "struct" : "class");
9084 else if (TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE
9085 && tag_type == enum_type)
9086 error ("`%T' referred to as enum", TREE_TYPE (decl));
9088 type = TREE_TYPE (decl);
9092 /* An elaborated-type-specifier sometimes introduces a new type and
9093 sometimes names an existing type. Normally, the rule is that it
9094 introduces a new type only if there is not an existing type of
9095 the same name already in scope. For example, given:
9098 void f() { struct S s; }
9100 the `struct S' in the body of `f' is the same `struct S' as in
9101 the global scope; the existing definition is used. However, if
9102 there were no global declaration, this would introduce a new
9103 local class named `S'.
9105 An exception to this rule applies to the following code:
9107 namespace N { struct S; }
9109 Here, the elaborated-type-specifier names a new type
9110 unconditionally; even if there is already an `S' in the
9111 containing scope this declaration names a new type.
9112 This exception only applies if the elaborated-type-specifier
9113 forms the complete declaration:
9117 A declaration consisting solely of `class-key identifier ;' is
9118 either a redeclaration of the name in the current scope or a
9119 forward declaration of the identifier as a class name. It
9120 introduces the name into the current scope.
9122 We are in this situation precisely when the next token is a `;'.
9124 An exception to the exception is that a `friend' declaration does
9125 *not* name a new type; i.e., given:
9127 struct S { friend struct T; };
9129 `T' is not a new type in the scope of `S'.
9131 Also, `new struct S' or `sizeof (struct S)' never results in the
9132 definition of a new type; a new type can only be declared in a
9133 declaration context. */
9135 type = xref_tag (tag_type, identifier,
9136 /*attributes=*/NULL_TREE,
9139 || cp_lexer_next_token_is_not (parser->lexer,
9143 if (tag_type != enum_type)
9144 cp_parser_check_class_key (tag_type, type);
9148 /* Parse an enum-specifier.
9151 enum identifier [opt] { enumerator-list [opt] }
9153 Returns an ENUM_TYPE representing the enumeration. */
9156 cp_parser_enum_specifier (parser)
9160 tree identifier = NULL_TREE;
9163 /* Look for the `enum' keyword. */
9164 if (!cp_parser_require_keyword (parser, RID_ENUM, "`enum'"))
9165 return error_mark_node;
9166 /* Peek at the next token. */
9167 token = cp_lexer_peek_token (parser->lexer);
9169 /* See if it is an identifier. */
9170 if (token->type == CPP_NAME)
9171 identifier = cp_parser_identifier (parser);
9173 /* Look for the `{'. */
9174 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
9175 return error_mark_node;
9177 /* At this point, we're going ahead with the enum-specifier, even
9178 if some other problem occurs. */
9179 cp_parser_commit_to_tentative_parse (parser);
9181 /* Issue an error message if type-definitions are forbidden here. */
9182 cp_parser_check_type_definition (parser);
9184 /* Create the new type. */
9185 type = start_enum (identifier ? identifier : make_anon_name ());
9187 /* Peek at the next token. */
9188 token = cp_lexer_peek_token (parser->lexer);
9189 /* If it's not a `}', then there are some enumerators. */
9190 if (token->type != CPP_CLOSE_BRACE)
9191 cp_parser_enumerator_list (parser, type);
9192 /* Look for the `}'. */
9193 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
9195 /* Finish up the enumeration. */
9201 /* Parse an enumerator-list. The enumerators all have the indicated
9205 enumerator-definition
9206 enumerator-list , enumerator-definition */
9209 cp_parser_enumerator_list (parser, type)
9217 /* Parse an enumerator-definition. */
9218 cp_parser_enumerator_definition (parser, type);
9219 /* Peek at the next token. */
9220 token = cp_lexer_peek_token (parser->lexer);
9221 /* If it's not a `,', then we've reached the end of the
9223 if (token->type != CPP_COMMA)
9225 /* Otherwise, consume the `,' and keep going. */
9226 cp_lexer_consume_token (parser->lexer);
9227 /* If the next token is a `}', there is a trailing comma. */
9228 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
9230 if (pedantic && !in_system_header)
9231 pedwarn ("comma at end of enumerator list");
9237 /* Parse an enumerator-definition. The enumerator has the indicated
9240 enumerator-definition:
9242 enumerator = constant-expression
9248 cp_parser_enumerator_definition (parser, type)
9256 /* Look for the identifier. */
9257 identifier = cp_parser_identifier (parser);
9258 if (identifier == error_mark_node)
9261 /* Peek at the next token. */
9262 token = cp_lexer_peek_token (parser->lexer);
9263 /* If it's an `=', then there's an explicit value. */
9264 if (token->type == CPP_EQ)
9266 /* Consume the `=' token. */
9267 cp_lexer_consume_token (parser->lexer);
9268 /* Parse the value. */
9269 value = cp_parser_constant_expression (parser);
9274 /* Create the enumerator. */
9275 build_enumerator (identifier, value, type);
9278 /* Parse a namespace-name.
9281 original-namespace-name
9284 Returns the NAMESPACE_DECL for the namespace. */
9287 cp_parser_namespace_name (parser)
9291 tree namespace_decl;
9293 /* Get the name of the namespace. */
9294 identifier = cp_parser_identifier (parser);
9295 if (identifier == error_mark_node)
9296 return error_mark_node;
9298 /* Look up the identifier in the currently active scope. Look only
9299 for namespaces, due to:
9303 When looking up a namespace-name in a using-directive or alias
9304 definition, only namespace names are considered.
9310 During the lookup of a name preceding the :: scope resolution
9311 operator, object, function, and enumerator names are ignored.
9313 (Note that cp_parser_class_or_namespace_name only calls this
9314 function if the token after the name is the scope resolution
9316 namespace_decl = cp_parser_lookup_name (parser, identifier,
9317 /*check_access=*/true,
9319 /*is_namespace=*/true,
9320 /*check_dependency=*/true);
9321 /* If it's not a namespace, issue an error. */
9322 if (namespace_decl == error_mark_node
9323 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
9325 cp_parser_error (parser, "expected namespace-name");
9326 namespace_decl = error_mark_node;
9329 return namespace_decl;
9332 /* Parse a namespace-definition.
9334 namespace-definition:
9335 named-namespace-definition
9336 unnamed-namespace-definition
9338 named-namespace-definition:
9339 original-namespace-definition
9340 extension-namespace-definition
9342 original-namespace-definition:
9343 namespace identifier { namespace-body }
9345 extension-namespace-definition:
9346 namespace original-namespace-name { namespace-body }
9348 unnamed-namespace-definition:
9349 namespace { namespace-body } */
9352 cp_parser_namespace_definition (parser)
9357 /* Look for the `namespace' keyword. */
9358 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
9360 /* Get the name of the namespace. We do not attempt to distinguish
9361 between an original-namespace-definition and an
9362 extension-namespace-definition at this point. The semantic
9363 analysis routines are responsible for that. */
9364 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
9365 identifier = cp_parser_identifier (parser);
9367 identifier = NULL_TREE;
9369 /* Look for the `{' to start the namespace. */
9370 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
9371 /* Start the namespace. */
9372 push_namespace (identifier);
9373 /* Parse the body of the namespace. */
9374 cp_parser_namespace_body (parser);
9375 /* Finish the namespace. */
9377 /* Look for the final `}'. */
9378 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
9381 /* Parse a namespace-body.
9384 declaration-seq [opt] */
9387 cp_parser_namespace_body (parser)
9390 cp_parser_declaration_seq_opt (parser);
9393 /* Parse a namespace-alias-definition.
9395 namespace-alias-definition:
9396 namespace identifier = qualified-namespace-specifier ; */
9399 cp_parser_namespace_alias_definition (parser)
9403 tree namespace_specifier;
9405 /* Look for the `namespace' keyword. */
9406 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
9407 /* Look for the identifier. */
9408 identifier = cp_parser_identifier (parser);
9409 if (identifier == error_mark_node)
9411 /* Look for the `=' token. */
9412 cp_parser_require (parser, CPP_EQ, "`='");
9413 /* Look for the qualified-namespace-specifier. */
9415 = cp_parser_qualified_namespace_specifier (parser);
9416 /* Look for the `;' token. */
9417 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
9419 /* Register the alias in the symbol table. */
9420 do_namespace_alias (identifier, namespace_specifier);
9423 /* Parse a qualified-namespace-specifier.
9425 qualified-namespace-specifier:
9426 :: [opt] nested-name-specifier [opt] namespace-name
9428 Returns a NAMESPACE_DECL corresponding to the specified
9432 cp_parser_qualified_namespace_specifier (parser)
9435 /* Look for the optional `::'. */
9436 cp_parser_global_scope_opt (parser,
9437 /*current_scope_valid_p=*/false);
9439 /* Look for the optional nested-name-specifier. */
9440 cp_parser_nested_name_specifier_opt (parser,
9441 /*typename_keyword_p=*/false,
9442 /*check_dependency_p=*/true,
9445 return cp_parser_namespace_name (parser);
9448 /* Parse a using-declaration.
9451 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
9452 using :: unqualified-id ; */
9455 cp_parser_using_declaration (parser)
9459 bool typename_p = false;
9460 bool global_scope_p;
9465 /* Look for the `using' keyword. */
9466 cp_parser_require_keyword (parser, RID_USING, "`using'");
9468 /* Peek at the next token. */
9469 token = cp_lexer_peek_token (parser->lexer);
9470 /* See if it's `typename'. */
9471 if (token->keyword == RID_TYPENAME)
9473 /* Remember that we've seen it. */
9475 /* Consume the `typename' token. */
9476 cp_lexer_consume_token (parser->lexer);
9479 /* Look for the optional global scope qualification. */
9481 = (cp_parser_global_scope_opt (parser,
9482 /*current_scope_valid_p=*/false)
9485 /* If we saw `typename', or didn't see `::', then there must be a
9486 nested-name-specifier present. */
9487 if (typename_p || !global_scope_p)
9488 cp_parser_nested_name_specifier (parser, typename_p,
9489 /*check_dependency_p=*/true,
9491 /* Otherwise, we could be in either of the two productions. In that
9492 case, treat the nested-name-specifier as optional. */
9494 cp_parser_nested_name_specifier_opt (parser,
9495 /*typename_keyword_p=*/false,
9496 /*check_dependency_p=*/true,
9499 /* Parse the unqualified-id. */
9500 identifier = cp_parser_unqualified_id (parser,
9501 /*template_keyword_p=*/false,
9502 /*check_dependency_p=*/true);
9504 /* The function we call to handle a using-declaration is different
9505 depending on what scope we are in. */
9506 scope = current_scope ();
9507 if (scope && TYPE_P (scope))
9509 /* Create the USING_DECL. */
9510 decl = do_class_using_decl (build_nt (SCOPE_REF,
9513 /* Add it to the list of members in this class. */
9514 finish_member_declaration (decl);
9518 decl = cp_parser_lookup_name_simple (parser, identifier);
9520 do_local_using_decl (decl);
9522 do_toplevel_using_decl (decl);
9525 /* Look for the final `;'. */
9526 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
9529 /* Parse a using-directive.
9532 using namespace :: [opt] nested-name-specifier [opt]
9536 cp_parser_using_directive (parser)
9539 tree namespace_decl;
9541 /* Look for the `using' keyword. */
9542 cp_parser_require_keyword (parser, RID_USING, "`using'");
9543 /* And the `namespace' keyword. */
9544 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
9545 /* Look for the optional `::' operator. */
9546 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
9547 /* And the optional nested-name-sepcifier. */
9548 cp_parser_nested_name_specifier_opt (parser,
9549 /*typename_keyword_p=*/false,
9550 /*check_dependency_p=*/true,
9552 /* Get the namespace being used. */
9553 namespace_decl = cp_parser_namespace_name (parser);
9554 /* Update the symbol table. */
9555 do_using_directive (namespace_decl);
9556 /* Look for the final `;'. */
9557 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
9560 /* Parse an asm-definition.
9563 asm ( string-literal ) ;
9568 asm volatile [opt] ( string-literal ) ;
9569 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
9570 asm volatile [opt] ( string-literal : asm-operand-list [opt]
9571 : asm-operand-list [opt] ) ;
9572 asm volatile [opt] ( string-literal : asm-operand-list [opt]
9573 : asm-operand-list [opt]
9574 : asm-operand-list [opt] ) ; */
9577 cp_parser_asm_definition (parser)
9582 tree outputs = NULL_TREE;
9583 tree inputs = NULL_TREE;
9584 tree clobbers = NULL_TREE;
9586 bool volatile_p = false;
9587 bool extended_p = false;
9589 /* Look for the `asm' keyword. */
9590 cp_parser_require_keyword (parser, RID_ASM, "`asm'");
9591 /* See if the next token is `volatile'. */
9592 if (cp_parser_allow_gnu_extensions_p (parser)
9593 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
9595 /* Remember that we saw the `volatile' keyword. */
9597 /* Consume the token. */
9598 cp_lexer_consume_token (parser->lexer);
9600 /* Look for the opening `('. */
9601 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
9602 /* Look for the string. */
9603 token = cp_parser_require (parser, CPP_STRING, "asm body");
9606 string = token->value;
9607 /* If we're allowing GNU extensions, check for the extended assembly
9608 syntax. Unfortunately, the `:' tokens need not be separated by
9609 a space in C, and so, for compatibility, we tolerate that here
9610 too. Doing that means that we have to treat the `::' operator as
9612 if (cp_parser_allow_gnu_extensions_p (parser)
9613 && at_function_scope_p ()
9614 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
9615 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
9617 bool inputs_p = false;
9618 bool clobbers_p = false;
9620 /* The extended syntax was used. */
9623 /* Look for outputs. */
9624 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
9626 /* Consume the `:'. */
9627 cp_lexer_consume_token (parser->lexer);
9628 /* Parse the output-operands. */
9629 if (cp_lexer_next_token_is_not (parser->lexer,
9631 && cp_lexer_next_token_is_not (parser->lexer,
9633 && cp_lexer_next_token_is_not (parser->lexer,
9635 outputs = cp_parser_asm_operand_list (parser);
9637 /* If the next token is `::', there are no outputs, and the
9638 next token is the beginning of the inputs. */
9639 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
9641 /* Consume the `::' token. */
9642 cp_lexer_consume_token (parser->lexer);
9643 /* The inputs are coming next. */
9647 /* Look for inputs. */
9649 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
9652 /* Consume the `:'. */
9653 cp_lexer_consume_token (parser->lexer);
9654 /* Parse the output-operands. */
9655 if (cp_lexer_next_token_is_not (parser->lexer,
9657 && cp_lexer_next_token_is_not (parser->lexer,
9659 && cp_lexer_next_token_is_not (parser->lexer,
9661 inputs = cp_parser_asm_operand_list (parser);
9663 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
9664 /* The clobbers are coming next. */
9667 /* Look for clobbers. */
9669 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
9672 /* Consume the `:'. */
9673 cp_lexer_consume_token (parser->lexer);
9674 /* Parse the clobbers. */
9675 if (cp_lexer_next_token_is_not (parser->lexer,
9677 clobbers = cp_parser_asm_clobber_list (parser);
9680 /* Look for the closing `)'. */
9681 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
9682 cp_parser_skip_to_closing_parenthesis (parser);
9683 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
9685 /* Create the ASM_STMT. */
9686 if (at_function_scope_p ())
9689 finish_asm_stmt (volatile_p
9690 ? ridpointers[(int) RID_VOLATILE] : NULL_TREE,
9691 string, outputs, inputs, clobbers);
9692 /* If the extended syntax was not used, mark the ASM_STMT. */
9694 ASM_INPUT_P (asm_stmt) = 1;
9697 assemble_asm (string);
9700 /* Declarators [gram.dcl.decl] */
9702 /* Parse an init-declarator.
9705 declarator initializer [opt]
9710 declarator asm-specification [opt] attributes [opt] initializer [opt]
9712 The DECL_SPECIFIERS and PREFIX_ATTRIBUTES apply to this declarator.
9713 Returns a reprsentation of the entity declared. The ACCESS_CHECKS
9714 represent deferred access checks from the decl-specifier-seq. If
9715 MEMBER_P is TRUE, then this declarator appears in a class scope.
9716 The new DECL created by this declarator is returned.
9718 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
9719 for a function-definition here as well. If the declarator is a
9720 declarator for a function-definition, *FUNCTION_DEFINITION_P will
9721 be TRUE upon return. By that point, the function-definition will
9722 have been completely parsed.
9724 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
9728 cp_parser_init_declarator (parser,
9732 function_definition_allowed_p,
9734 function_definition_p)
9736 tree decl_specifiers;
9737 tree prefix_attributes;
9739 bool function_definition_allowed_p;
9741 bool *function_definition_p;
9746 tree asm_specification;
9748 tree decl = NULL_TREE;
9750 tree declarator_access_checks;
9751 bool is_initialized;
9752 bool is_parenthesized_init;
9753 bool ctor_dtor_or_conv_p;
9756 /* Assume that this is not the declarator for a function
9758 if (function_definition_p)
9759 *function_definition_p = false;
9761 /* Defer access checks while parsing the declarator; we cannot know
9762 what names are accessible until we know what is being
9764 cp_parser_start_deferring_access_checks (parser);
9765 /* Parse the declarator. */
9767 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
9768 &ctor_dtor_or_conv_p);
9769 /* Gather up the deferred checks. */
9770 declarator_access_checks
9771 = cp_parser_stop_deferring_access_checks (parser);
9773 /* Prevent the access checks from being reclaimed by GC. */
9774 parser->access_checks_lists
9775 = tree_cons (NULL_TREE, declarator_access_checks,
9776 parser->access_checks_lists);
9778 /* If the DECLARATOR was erroneous, there's no need to go
9780 if (declarator == error_mark_node)
9782 /* Discard access checks no longer in use. */
9783 parser->access_checks_lists
9784 = TREE_CHAIN (parser->access_checks_lists);
9785 return error_mark_node;
9788 /* Figure out what scope the entity declared by the DECLARATOR is
9789 located in. `grokdeclarator' sometimes changes the scope, so
9790 we compute it now. */
9791 scope = get_scope_of_declarator (declarator);
9793 /* If we're allowing GNU extensions, look for an asm-specification
9795 if (cp_parser_allow_gnu_extensions_p (parser))
9797 /* Look for an asm-specification. */
9798 asm_specification = cp_parser_asm_specification_opt (parser);
9799 /* And attributes. */
9800 attributes = cp_parser_attributes_opt (parser);
9804 asm_specification = NULL_TREE;
9805 attributes = NULL_TREE;
9808 /* Peek at the next token. */
9809 token = cp_lexer_peek_token (parser->lexer);
9810 /* Check to see if the token indicates the start of a
9811 function-definition. */
9812 if (cp_parser_token_starts_function_definition_p (token))
9814 if (!function_definition_allowed_p)
9816 /* If a function-definition should not appear here, issue an
9818 cp_parser_error (parser,
9819 "a function-definition is not allowed here");
9820 /* Discard access checks no longer in use. */
9821 parser->access_checks_lists
9822 = TREE_CHAIN (parser->access_checks_lists);
9823 return error_mark_node;
9829 /* Neither attributes nor an asm-specification are allowed
9830 on a function-definition. */
9831 if (asm_specification)
9832 error ("an asm-specification is not allowed on a function-definition");
9834 error ("attributes are not allowed on a function-definition");
9835 /* This is a function-definition. */
9836 *function_definition_p = true;
9838 /* Thread the access checks together. */
9839 ac = &access_checks;
9841 ac = &TREE_CHAIN (*ac);
9842 *ac = declarator_access_checks;
9844 /* Parse the function definition. */
9845 decl = (cp_parser_function_definition_from_specifiers_and_declarator
9846 (parser, decl_specifiers, prefix_attributes, declarator,
9849 /* Pull the access-checks apart again. */
9852 /* Discard access checks no longer in use. */
9853 parser->access_checks_lists
9854 = TREE_CHAIN (parser->access_checks_lists);
9862 Only in function declarations for constructors, destructors, and
9863 type conversions can the decl-specifier-seq be omitted.
9865 We explicitly postpone this check past the point where we handle
9866 function-definitions because we tolerate function-definitions
9867 that are missing their return types in some modes. */
9868 if (!decl_specifiers && !ctor_dtor_or_conv_p)
9870 cp_parser_error (parser,
9871 "expected constructor, destructor, or type conversion");
9872 /* Discard access checks no longer in use. */
9873 parser->access_checks_lists
9874 = TREE_CHAIN (parser->access_checks_lists);
9875 return error_mark_node;
9878 /* An `=' or an `(' indicates an initializer. */
9879 is_initialized = (token->type == CPP_EQ
9880 || token->type == CPP_OPEN_PAREN);
9881 /* If the init-declarator isn't initialized and isn't followed by a
9882 `,' or `;', it's not a valid init-declarator. */
9884 && token->type != CPP_COMMA
9885 && token->type != CPP_SEMICOLON)
9887 cp_parser_error (parser, "expected init-declarator");
9888 /* Discard access checks no longer in use. */
9889 parser->access_checks_lists
9890 = TREE_CHAIN (parser->access_checks_lists);
9891 return error_mark_node;
9894 /* Because start_decl has side-effects, we should only call it if we
9895 know we're going ahead. By this point, we know that we cannot
9896 possibly be looking at any other construct. */
9897 cp_parser_commit_to_tentative_parse (parser);
9899 /* Check to see whether or not this declaration is a friend. */
9900 friend_p = cp_parser_friend_p (decl_specifiers);
9902 /* Check that the number of template-parameter-lists is OK. */
9903 if (!cp_parser_check_declarator_template_parameters (parser,
9906 /* Discard access checks no longer in use. */
9907 parser->access_checks_lists
9908 = TREE_CHAIN (parser->access_checks_lists);
9909 return error_mark_node;
9912 /* Enter the newly declared entry in the symbol table. If we're
9913 processing a declaration in a class-specifier, we wait until
9914 after processing the initializer. */
9917 if (parser->in_unbraced_linkage_specification_p)
9919 decl_specifiers = tree_cons (error_mark_node,
9920 get_identifier ("extern"),
9922 have_extern_spec = false;
9924 decl = start_decl (declarator,
9931 /* Enter the SCOPE. That way unqualified names appearing in the
9932 initializer will be looked up in SCOPE. */
9936 /* Perform deferred access control checks, now that we know in which
9937 SCOPE the declared entity resides. */
9938 if (!member_p && decl)
9940 tree saved_current_function_decl = NULL_TREE;
9942 /* If the entity being declared is a function, pretend that we
9943 are in its scope. If it is a `friend', it may have access to
9944 things that would not otherwise be accessible. */
9945 if (TREE_CODE (decl) == FUNCTION_DECL)
9947 saved_current_function_decl = current_function_decl;
9948 current_function_decl = decl;
9951 /* Perform the access control checks for the decl-specifiers. */
9952 cp_parser_perform_deferred_access_checks (access_checks);
9953 /* And for the declarator. */
9954 cp_parser_perform_deferred_access_checks (declarator_access_checks);
9956 /* Restore the saved value. */
9957 if (TREE_CODE (decl) == FUNCTION_DECL)
9958 current_function_decl = saved_current_function_decl;
9961 /* Parse the initializer. */
9963 initializer = cp_parser_initializer (parser,
9964 &is_parenthesized_init);
9967 initializer = NULL_TREE;
9968 is_parenthesized_init = false;
9971 /* The old parser allows attributes to appear after a parenthesized
9972 initializer. Mark Mitchell proposed removing this functionality
9973 on the GCC mailing lists on 2002-08-13. This parser accepts the
9974 attributes -- but ignores them. */
9975 if (cp_parser_allow_gnu_extensions_p (parser) && is_parenthesized_init)
9976 if (cp_parser_attributes_opt (parser))
9977 warning ("attributes after parenthesized initializer ignored");
9979 /* Leave the SCOPE, now that we have processed the initializer. It
9980 is important to do this before calling cp_finish_decl because it
9981 makes decisions about whether to create DECL_STMTs or not based
9982 on the current scope. */
9986 /* For an in-class declaration, use `grokfield' to create the
9989 decl = grokfield (declarator, decl_specifiers,
9990 initializer, /*asmspec=*/NULL_TREE,
9991 /*attributes=*/NULL_TREE);
9993 /* Finish processing the declaration. But, skip friend
9995 if (!friend_p && decl)
9996 cp_finish_decl (decl,
9999 /* If the initializer is in parentheses, then this is
10000 a direct-initialization, which means that an
10001 `explicit' constructor is OK. Otherwise, an
10002 `explicit' constructor cannot be used. */
10003 ((is_parenthesized_init || !is_initialized)
10004 ? 0 : LOOKUP_ONLYCONVERTING));
10006 /* Discard access checks no longer in use. */
10007 parser->access_checks_lists
10008 = TREE_CHAIN (parser->access_checks_lists);
10013 /* Parse a declarator.
10017 ptr-operator declarator
10019 abstract-declarator:
10020 ptr-operator abstract-declarator [opt]
10021 direct-abstract-declarator
10026 attributes [opt] direct-declarator
10027 attributes [opt] ptr-operator declarator
10029 abstract-declarator:
10030 attributes [opt] ptr-operator abstract-declarator [opt]
10031 attributes [opt] direct-abstract-declarator
10033 Returns a representation of the declarator. If the declarator has
10034 the form `* declarator', then an INDIRECT_REF is returned, whose
10035 only operand is the sub-declarator. Analagously, `& declarator' is
10036 represented as an ADDR_EXPR. For `X::* declarator', a SCOPE_REF is
10037 used. The first operand is the TYPE for `X'. The second operand
10038 is an INDIRECT_REF whose operand is the sub-declarator.
10040 Otherwise, the reprsentation is as for a direct-declarator.
10042 (It would be better to define a structure type to represent
10043 declarators, rather than abusing `tree' nodes to represent
10044 declarators. That would be much clearer and save some memory.
10045 There is no reason for declarators to be garbage-collected, for
10046 example; they are created during parser and no longer needed after
10047 `grokdeclarator' has been called.)
10049 For a ptr-operator that has the optional cv-qualifier-seq,
10050 cv-qualifiers will be stored in the TREE_TYPE of the INDIRECT_REF
10053 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is set to
10054 true if this declarator represents a constructor, destructor, or
10055 type conversion operator. Otherwise, it is set to false.
10057 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
10058 a decl-specifier-seq unless it declares a constructor, destructor,
10059 or conversion. It might seem that we could check this condition in
10060 semantic analysis, rather than parsing, but that makes it difficult
10061 to handle something like `f()'. We want to notice that there are
10062 no decl-specifiers, and therefore realize that this is an
10063 expression, not a declaration.) */
10066 cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p)
10068 cp_parser_declarator_kind dcl_kind;
10069 bool *ctor_dtor_or_conv_p;
10073 enum tree_code code;
10074 tree cv_qualifier_seq;
10076 tree attributes = NULL_TREE;
10078 /* Assume this is not a constructor, destructor, or type-conversion
10080 if (ctor_dtor_or_conv_p)
10081 *ctor_dtor_or_conv_p = false;
10083 if (cp_parser_allow_gnu_extensions_p (parser))
10084 attributes = cp_parser_attributes_opt (parser);
10086 /* Peek at the next token. */
10087 token = cp_lexer_peek_token (parser->lexer);
10089 /* Check for the ptr-operator production. */
10090 cp_parser_parse_tentatively (parser);
10091 /* Parse the ptr-operator. */
10092 code = cp_parser_ptr_operator (parser,
10094 &cv_qualifier_seq);
10095 /* If that worked, then we have a ptr-operator. */
10096 if (cp_parser_parse_definitely (parser))
10098 /* The dependent declarator is optional if we are parsing an
10099 abstract-declarator. */
10100 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
10101 cp_parser_parse_tentatively (parser);
10103 /* Parse the dependent declarator. */
10104 declarator = cp_parser_declarator (parser, dcl_kind,
10105 /*ctor_dtor_or_conv_p=*/NULL);
10107 /* If we are parsing an abstract-declarator, we must handle the
10108 case where the dependent declarator is absent. */
10109 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
10110 && !cp_parser_parse_definitely (parser))
10111 declarator = NULL_TREE;
10113 /* Build the representation of the ptr-operator. */
10114 if (code == INDIRECT_REF)
10115 declarator = make_pointer_declarator (cv_qualifier_seq,
10118 declarator = make_reference_declarator (cv_qualifier_seq,
10120 /* Handle the pointer-to-member case. */
10122 declarator = build_nt (SCOPE_REF, class_type, declarator);
10124 /* Everything else is a direct-declarator. */
10126 declarator = cp_parser_direct_declarator (parser,
10128 ctor_dtor_or_conv_p);
10130 if (attributes && declarator != error_mark_node)
10131 declarator = tree_cons (attributes, declarator, NULL_TREE);
10136 /* Parse a direct-declarator or direct-abstract-declarator.
10140 direct-declarator ( parameter-declaration-clause )
10141 cv-qualifier-seq [opt]
10142 exception-specification [opt]
10143 direct-declarator [ constant-expression [opt] ]
10146 direct-abstract-declarator:
10147 direct-abstract-declarator [opt]
10148 ( parameter-declaration-clause )
10149 cv-qualifier-seq [opt]
10150 exception-specification [opt]
10151 direct-abstract-declarator [opt] [ constant-expression [opt] ]
10152 ( abstract-declarator )
10154 Returns a representation of the declarator. DCL_KIND is
10155 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
10156 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
10157 we are parsing a direct-declarator. It is
10158 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
10159 of ambiguity we prefer an abstract declarator, as per
10160 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P is as for
10161 cp_parser_declarator.
10163 For the declarator-id production, the representation is as for an
10164 id-expression, except that a qualified name is represented as a
10165 SCOPE_REF. A function-declarator is represented as a CALL_EXPR;
10166 see the documentation of the FUNCTION_DECLARATOR_* macros for
10167 information about how to find the various declarator components.
10168 An array-declarator is represented as an ARRAY_REF. The
10169 direct-declarator is the first operand; the constant-expression
10170 indicating the size of the array is the second operand. */
10173 cp_parser_direct_declarator (parser, dcl_kind, ctor_dtor_or_conv_p)
10175 cp_parser_declarator_kind dcl_kind;
10176 bool *ctor_dtor_or_conv_p;
10179 tree declarator = NULL_TREE;
10180 tree scope = NULL_TREE;
10181 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
10182 bool saved_in_declarator_p = parser->in_declarator_p;
10187 /* Peek at the next token. */
10188 token = cp_lexer_peek_token (parser->lexer);
10189 if (token->type == CPP_OPEN_PAREN)
10191 /* This is either a parameter-declaration-clause, or a
10192 parenthesized declarator. When we know we are parsing a
10193 named declarator, it must be a paranthesized declarator
10194 if FIRST is true. For instance, `(int)' is a
10195 parameter-declaration-clause, with an omitted
10196 direct-abstract-declarator. But `((*))', is a
10197 parenthesized abstract declarator. Finally, when T is a
10198 template parameter `(T)' is a
10199 paremeter-declaration-clause, and not a parenthesized
10202 We first try and parse a parameter-declaration-clause,
10203 and then try a nested declarator (if FIRST is true).
10205 It is not an error for it not to be a
10206 parameter-declaration-clause, even when FIRST is
10212 The first is the declaration of a function while the
10213 second is a the definition of a variable, including its
10216 Having seen only the parenthesis, we cannot know which of
10217 these two alternatives should be selected. Even more
10218 complex are examples like:
10223 The former is a function-declaration; the latter is a
10224 variable initialization.
10226 Thus again, we try a parameter-declation-clause, and if
10227 that fails, we back out and return. */
10229 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
10233 cp_parser_parse_tentatively (parser);
10235 /* Consume the `('. */
10236 cp_lexer_consume_token (parser->lexer);
10239 /* If this is going to be an abstract declarator, we're
10240 in a declarator and we can't have default args. */
10241 parser->default_arg_ok_p = false;
10242 parser->in_declarator_p = true;
10245 /* Parse the parameter-declaration-clause. */
10246 params = cp_parser_parameter_declaration_clause (parser);
10248 /* If all went well, parse the cv-qualifier-seq and the
10249 exception-specfication. */
10250 if (cp_parser_parse_definitely (parser))
10252 tree cv_qualifiers;
10253 tree exception_specification;
10256 /* Consume the `)'. */
10257 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
10259 /* Parse the cv-qualifier-seq. */
10260 cv_qualifiers = cp_parser_cv_qualifier_seq_opt (parser);
10261 /* And the exception-specification. */
10262 exception_specification
10263 = cp_parser_exception_specification_opt (parser);
10265 /* Create the function-declarator. */
10266 declarator = make_call_declarator (declarator,
10269 exception_specification);
10270 /* Any subsequent parameter lists are to do with
10271 return type, so are not those of the declared
10273 parser->default_arg_ok_p = false;
10275 /* Repeat the main loop. */
10280 /* If this is the first, we can try a parenthesized
10284 parser->default_arg_ok_p = saved_default_arg_ok_p;
10285 parser->in_declarator_p = saved_in_declarator_p;
10287 /* Consume the `('. */
10288 cp_lexer_consume_token (parser->lexer);
10289 /* Parse the nested declarator. */
10291 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p);
10293 /* Expect a `)'. */
10294 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
10295 declarator = error_mark_node;
10296 if (declarator == error_mark_node)
10299 goto handle_declarator;
10301 /* Otherwise, we must be done. */
10305 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
10306 && token->type == CPP_OPEN_SQUARE)
10308 /* Parse an array-declarator. */
10312 parser->default_arg_ok_p = false;
10313 parser->in_declarator_p = true;
10314 /* Consume the `['. */
10315 cp_lexer_consume_token (parser->lexer);
10316 /* Peek at the next token. */
10317 token = cp_lexer_peek_token (parser->lexer);
10318 /* If the next token is `]', then there is no
10319 constant-expression. */
10320 if (token->type != CPP_CLOSE_SQUARE)
10321 bounds = cp_parser_constant_expression (parser);
10323 bounds = NULL_TREE;
10324 /* Look for the closing `]'. */
10325 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'"))
10327 declarator = error_mark_node;
10331 declarator = build_nt (ARRAY_REF, declarator, bounds);
10333 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
10335 /* Parse a declarator_id */
10336 if (dcl_kind == CP_PARSER_DECLARATOR_EITHER)
10337 cp_parser_parse_tentatively (parser);
10338 declarator = cp_parser_declarator_id (parser);
10339 if (dcl_kind == CP_PARSER_DECLARATOR_EITHER
10340 && !cp_parser_parse_definitely (parser))
10341 declarator = error_mark_node;
10342 if (declarator == error_mark_node)
10345 if (TREE_CODE (declarator) == SCOPE_REF)
10347 tree scope = TREE_OPERAND (declarator, 0);
10349 /* In the declaration of a member of a template class
10350 outside of the class itself, the SCOPE will sometimes
10351 be a TYPENAME_TYPE. For example, given:
10353 template <typename T>
10354 int S<T>::R::i = 3;
10356 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
10357 this context, we must resolve S<T>::R to an ordinary
10358 type, rather than a typename type.
10360 The reason we normally avoid resolving TYPENAME_TYPEs
10361 is that a specialization of `S' might render
10362 `S<T>::R' not a type. However, if `S' is
10363 specialized, then this `i' will not be used, so there
10364 is no harm in resolving the types here. */
10365 if (TREE_CODE (scope) == TYPENAME_TYPE)
10367 /* Resolve the TYPENAME_TYPE. */
10368 scope = cp_parser_resolve_typename_type (parser, scope);
10369 /* If that failed, the declarator is invalid. */
10370 if (scope == error_mark_node)
10371 return error_mark_node;
10372 /* Build a new DECLARATOR. */
10373 declarator = build_nt (SCOPE_REF,
10375 TREE_OPERAND (declarator, 1));
10379 /* Check to see whether the declarator-id names a constructor,
10380 destructor, or conversion. */
10381 if (declarator && ctor_dtor_or_conv_p
10382 && ((TREE_CODE (declarator) == SCOPE_REF
10383 && CLASS_TYPE_P (TREE_OPERAND (declarator, 0)))
10384 || (TREE_CODE (declarator) != SCOPE_REF
10385 && at_class_scope_p ())))
10387 tree unqualified_name;
10390 /* Get the unqualified part of the name. */
10391 if (TREE_CODE (declarator) == SCOPE_REF)
10393 class_type = TREE_OPERAND (declarator, 0);
10394 unqualified_name = TREE_OPERAND (declarator, 1);
10398 class_type = current_class_type;
10399 unqualified_name = declarator;
10402 /* See if it names ctor, dtor or conv. */
10403 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR
10404 || IDENTIFIER_TYPENAME_P (unqualified_name)
10405 || constructor_name_p (unqualified_name, class_type))
10406 *ctor_dtor_or_conv_p = true;
10409 handle_declarator:;
10410 scope = get_scope_of_declarator (declarator);
10412 /* Any names that appear after the declarator-id for a member
10413 are looked up in the containing scope. */
10414 push_scope (scope);
10415 parser->in_declarator_p = true;
10416 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
10418 && (TREE_CODE (declarator) == SCOPE_REF
10419 || TREE_CODE (declarator) == IDENTIFIER_NODE)))
10420 /* Default args are only allowed on function
10422 parser->default_arg_ok_p = saved_default_arg_ok_p;
10424 parser->default_arg_ok_p = false;
10433 /* For an abstract declarator, we might wind up with nothing at this
10434 point. That's an error; the declarator is not optional. */
10436 cp_parser_error (parser, "expected declarator");
10438 /* If we entered a scope, we must exit it now. */
10442 parser->default_arg_ok_p = saved_default_arg_ok_p;
10443 parser->in_declarator_p = saved_in_declarator_p;
10448 /* Parse a ptr-operator.
10451 * cv-qualifier-seq [opt]
10453 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
10458 & cv-qualifier-seq [opt]
10460 Returns INDIRECT_REF if a pointer, or pointer-to-member, was
10461 used. Returns ADDR_EXPR if a reference was used. In the
10462 case of a pointer-to-member, *TYPE is filled in with the
10463 TYPE containing the member. *CV_QUALIFIER_SEQ is filled in
10464 with the cv-qualifier-seq, or NULL_TREE, if there are no
10465 cv-qualifiers. Returns ERROR_MARK if an error occurred. */
10467 static enum tree_code
10468 cp_parser_ptr_operator (parser, type, cv_qualifier_seq)
10471 tree *cv_qualifier_seq;
10473 enum tree_code code = ERROR_MARK;
10476 /* Assume that it's not a pointer-to-member. */
10478 /* And that there are no cv-qualifiers. */
10479 *cv_qualifier_seq = NULL_TREE;
10481 /* Peek at the next token. */
10482 token = cp_lexer_peek_token (parser->lexer);
10483 /* If it's a `*' or `&' we have a pointer or reference. */
10484 if (token->type == CPP_MULT || token->type == CPP_AND)
10486 /* Remember which ptr-operator we were processing. */
10487 code = (token->type == CPP_AND ? ADDR_EXPR : INDIRECT_REF);
10489 /* Consume the `*' or `&'. */
10490 cp_lexer_consume_token (parser->lexer);
10492 /* A `*' can be followed by a cv-qualifier-seq, and so can a
10493 `&', if we are allowing GNU extensions. (The only qualifier
10494 that can legally appear after `&' is `restrict', but that is
10495 enforced during semantic analysis. */
10496 if (code == INDIRECT_REF
10497 || cp_parser_allow_gnu_extensions_p (parser))
10498 *cv_qualifier_seq = cp_parser_cv_qualifier_seq_opt (parser);
10502 /* Try the pointer-to-member case. */
10503 cp_parser_parse_tentatively (parser);
10504 /* Look for the optional `::' operator. */
10505 cp_parser_global_scope_opt (parser,
10506 /*current_scope_valid_p=*/false);
10507 /* Look for the nested-name specifier. */
10508 cp_parser_nested_name_specifier (parser,
10509 /*typename_keyword_p=*/false,
10510 /*check_dependency_p=*/true,
10512 /* If we found it, and the next token is a `*', then we are
10513 indeed looking at a pointer-to-member operator. */
10514 if (!cp_parser_error_occurred (parser)
10515 && cp_parser_require (parser, CPP_MULT, "`*'"))
10517 /* The type of which the member is a member is given by the
10519 *type = parser->scope;
10520 /* The next name will not be qualified. */
10521 parser->scope = NULL_TREE;
10522 parser->qualifying_scope = NULL_TREE;
10523 parser->object_scope = NULL_TREE;
10524 /* Indicate that the `*' operator was used. */
10525 code = INDIRECT_REF;
10526 /* Look for the optional cv-qualifier-seq. */
10527 *cv_qualifier_seq = cp_parser_cv_qualifier_seq_opt (parser);
10529 /* If that didn't work we don't have a ptr-operator. */
10530 if (!cp_parser_parse_definitely (parser))
10531 cp_parser_error (parser, "expected ptr-operator");
10537 /* Parse an (optional) cv-qualifier-seq.
10540 cv-qualifier cv-qualifier-seq [opt]
10542 Returns a TREE_LIST. The TREE_VALUE of each node is the
10543 representation of a cv-qualifier. */
10546 cp_parser_cv_qualifier_seq_opt (parser)
10549 tree cv_qualifiers = NULL_TREE;
10555 /* Look for the next cv-qualifier. */
10556 cv_qualifier = cp_parser_cv_qualifier_opt (parser);
10557 /* If we didn't find one, we're done. */
10561 /* Add this cv-qualifier to the list. */
10563 = tree_cons (NULL_TREE, cv_qualifier, cv_qualifiers);
10566 /* We built up the list in reverse order. */
10567 return nreverse (cv_qualifiers);
10570 /* Parse an (optional) cv-qualifier.
10582 cp_parser_cv_qualifier_opt (parser)
10586 tree cv_qualifier = NULL_TREE;
10588 /* Peek at the next token. */
10589 token = cp_lexer_peek_token (parser->lexer);
10590 /* See if it's a cv-qualifier. */
10591 switch (token->keyword)
10596 /* Save the value of the token. */
10597 cv_qualifier = token->value;
10598 /* Consume the token. */
10599 cp_lexer_consume_token (parser->lexer);
10606 return cv_qualifier;
10609 /* Parse a declarator-id.
10613 :: [opt] nested-name-specifier [opt] type-name
10615 In the `id-expression' case, the value returned is as for
10616 cp_parser_id_expression if the id-expression was an unqualified-id.
10617 If the id-expression was a qualified-id, then a SCOPE_REF is
10618 returned. The first operand is the scope (either a NAMESPACE_DECL
10619 or TREE_TYPE), but the second is still just a representation of an
10623 cp_parser_declarator_id (parser)
10626 tree id_expression;
10628 /* The expression must be an id-expression. Assume that qualified
10629 names are the names of types so that:
10632 int S<T>::R::i = 3;
10634 will work; we must treat `S<T>::R' as the name of a type.
10635 Similarly, assume that qualified names are templates, where
10639 int S<T>::R<T>::i = 3;
10642 id_expression = cp_parser_id_expression (parser,
10643 /*template_keyword_p=*/false,
10644 /*check_dependency_p=*/false,
10645 /*template_p=*/NULL);
10646 /* If the name was qualified, create a SCOPE_REF to represent
10649 id_expression = build_nt (SCOPE_REF, parser->scope, id_expression);
10651 return id_expression;
10654 /* Parse a type-id.
10657 type-specifier-seq abstract-declarator [opt]
10659 Returns the TYPE specified. */
10662 cp_parser_type_id (parser)
10665 tree type_specifier_seq;
10666 tree abstract_declarator;
10668 /* Parse the type-specifier-seq. */
10670 = cp_parser_type_specifier_seq (parser);
10671 if (type_specifier_seq == error_mark_node)
10672 return error_mark_node;
10674 /* There might or might not be an abstract declarator. */
10675 cp_parser_parse_tentatively (parser);
10676 /* Look for the declarator. */
10677 abstract_declarator
10678 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL);
10679 /* Check to see if there really was a declarator. */
10680 if (!cp_parser_parse_definitely (parser))
10681 abstract_declarator = NULL_TREE;
10683 return groktypename (build_tree_list (type_specifier_seq,
10684 abstract_declarator));
10687 /* Parse a type-specifier-seq.
10689 type-specifier-seq:
10690 type-specifier type-specifier-seq [opt]
10694 type-specifier-seq:
10695 attributes type-specifier-seq [opt]
10697 Returns a TREE_LIST. Either the TREE_VALUE of each node is a
10698 type-specifier, or the TREE_PURPOSE is a list of attributes. */
10701 cp_parser_type_specifier_seq (parser)
10704 bool seen_type_specifier = false;
10705 tree type_specifier_seq = NULL_TREE;
10707 /* Parse the type-specifiers and attributes. */
10710 tree type_specifier;
10712 /* Check for attributes first. */
10713 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
10715 type_specifier_seq = tree_cons (cp_parser_attributes_opt (parser),
10717 type_specifier_seq);
10721 /* After the first type-specifier, others are optional. */
10722 if (seen_type_specifier)
10723 cp_parser_parse_tentatively (parser);
10724 /* Look for the type-specifier. */
10725 type_specifier = cp_parser_type_specifier (parser,
10726 CP_PARSER_FLAGS_NONE,
10727 /*is_friend=*/false,
10728 /*is_declaration=*/false,
10731 /* If the first type-specifier could not be found, this is not a
10732 type-specifier-seq at all. */
10733 if (!seen_type_specifier && type_specifier == error_mark_node)
10734 return error_mark_node;
10735 /* If subsequent type-specifiers could not be found, the
10736 type-specifier-seq is complete. */
10737 else if (seen_type_specifier && !cp_parser_parse_definitely (parser))
10740 /* Add the new type-specifier to the list. */
10742 = tree_cons (NULL_TREE, type_specifier, type_specifier_seq);
10743 seen_type_specifier = true;
10746 /* We built up the list in reverse order. */
10747 return nreverse (type_specifier_seq);
10750 /* Parse a parameter-declaration-clause.
10752 parameter-declaration-clause:
10753 parameter-declaration-list [opt] ... [opt]
10754 parameter-declaration-list , ...
10756 Returns a representation for the parameter declarations. Each node
10757 is a TREE_LIST. (See cp_parser_parameter_declaration for the exact
10758 representation.) If the parameter-declaration-clause ends with an
10759 ellipsis, PARMLIST_ELLIPSIS_P will hold of the first node in the
10760 list. A return value of NULL_TREE indicates a
10761 parameter-declaration-clause consisting only of an ellipsis. */
10764 cp_parser_parameter_declaration_clause (parser)
10771 /* Peek at the next token. */
10772 token = cp_lexer_peek_token (parser->lexer);
10773 /* Check for trivial parameter-declaration-clauses. */
10774 if (token->type == CPP_ELLIPSIS)
10776 /* Consume the `...' token. */
10777 cp_lexer_consume_token (parser->lexer);
10780 else if (token->type == CPP_CLOSE_PAREN)
10781 /* There are no parameters. */
10783 #ifndef NO_IMPLICIT_EXTERN_C
10784 if (in_system_header && current_class_type == NULL
10785 && current_lang_name == lang_name_c)
10789 return void_list_node;
10791 /* Check for `(void)', too, which is a special case. */
10792 else if (token->keyword == RID_VOID
10793 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
10794 == CPP_CLOSE_PAREN))
10796 /* Consume the `void' token. */
10797 cp_lexer_consume_token (parser->lexer);
10798 /* There are no parameters. */
10799 return void_list_node;
10802 /* Parse the parameter-declaration-list. */
10803 parameters = cp_parser_parameter_declaration_list (parser);
10804 /* If a parse error occurred while parsing the
10805 parameter-declaration-list, then the entire
10806 parameter-declaration-clause is erroneous. */
10807 if (parameters == error_mark_node)
10808 return error_mark_node;
10810 /* Peek at the next token. */
10811 token = cp_lexer_peek_token (parser->lexer);
10812 /* If it's a `,', the clause should terminate with an ellipsis. */
10813 if (token->type == CPP_COMMA)
10815 /* Consume the `,'. */
10816 cp_lexer_consume_token (parser->lexer);
10817 /* Expect an ellipsis. */
10819 = (cp_parser_require (parser, CPP_ELLIPSIS, "`...'") != NULL);
10821 /* It might also be `...' if the optional trailing `,' was
10823 else if (token->type == CPP_ELLIPSIS)
10825 /* Consume the `...' token. */
10826 cp_lexer_consume_token (parser->lexer);
10827 /* And remember that we saw it. */
10831 ellipsis_p = false;
10833 /* Finish the parameter list. */
10834 return finish_parmlist (parameters, ellipsis_p);
10837 /* Parse a parameter-declaration-list.
10839 parameter-declaration-list:
10840 parameter-declaration
10841 parameter-declaration-list , parameter-declaration
10843 Returns a representation of the parameter-declaration-list, as for
10844 cp_parser_parameter_declaration_clause. However, the
10845 `void_list_node' is never appended to the list. */
10848 cp_parser_parameter_declaration_list (parser)
10851 tree parameters = NULL_TREE;
10853 /* Look for more parameters. */
10857 /* Parse the parameter. */
10859 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/false);
10861 /* If a parse error ocurred parsing the parameter declaration,
10862 then the entire parameter-declaration-list is erroneous. */
10863 if (parameter == error_mark_node)
10865 parameters = error_mark_node;
10868 /* Add the new parameter to the list. */
10869 TREE_CHAIN (parameter) = parameters;
10870 parameters = parameter;
10872 /* Peek at the next token. */
10873 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
10874 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10875 /* The parameter-declaration-list is complete. */
10877 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
10881 /* Peek at the next token. */
10882 token = cp_lexer_peek_nth_token (parser->lexer, 2);
10883 /* If it's an ellipsis, then the list is complete. */
10884 if (token->type == CPP_ELLIPSIS)
10886 /* Otherwise, there must be more parameters. Consume the
10888 cp_lexer_consume_token (parser->lexer);
10892 cp_parser_error (parser, "expected `,' or `...'");
10897 /* We built up the list in reverse order; straighten it out now. */
10898 return nreverse (parameters);
10901 /* Parse a parameter declaration.
10903 parameter-declaration:
10904 decl-specifier-seq declarator
10905 decl-specifier-seq declarator = assignment-expression
10906 decl-specifier-seq abstract-declarator [opt]
10907 decl-specifier-seq abstract-declarator [opt] = assignment-expression
10909 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
10910 declares a template parameter. (In that case, a non-nested `>'
10911 token encountered during the parsing of the assignment-expression
10912 is not interpreted as a greater-than operator.)
10914 Returns a TREE_LIST representing the parameter-declaration. The
10915 TREE_VALUE is a representation of the decl-specifier-seq and
10916 declarator. In particular, the TREE_VALUE will be a TREE_LIST
10917 whose TREE_PURPOSE represents the decl-specifier-seq and whose
10918 TREE_VALUE represents the declarator. */
10921 cp_parser_parameter_declaration (cp_parser *parser,
10922 bool template_parm_p)
10924 bool declares_class_or_enum;
10925 bool greater_than_is_operator_p;
10926 tree decl_specifiers;
10929 tree default_argument;
10932 const char *saved_message;
10934 /* In a template parameter, `>' is not an operator.
10938 When parsing a default template-argument for a non-type
10939 template-parameter, the first non-nested `>' is taken as the end
10940 of the template parameter-list rather than a greater-than
10942 greater_than_is_operator_p = !template_parm_p;
10944 /* Type definitions may not appear in parameter types. */
10945 saved_message = parser->type_definition_forbidden_message;
10946 parser->type_definition_forbidden_message
10947 = "types may not be defined in parameter types";
10949 /* Parse the declaration-specifiers. */
10951 = cp_parser_decl_specifier_seq (parser,
10952 CP_PARSER_FLAGS_NONE,
10954 &declares_class_or_enum);
10955 /* If an error occurred, there's no reason to attempt to parse the
10956 rest of the declaration. */
10957 if (cp_parser_error_occurred (parser))
10959 parser->type_definition_forbidden_message = saved_message;
10960 return error_mark_node;
10963 /* Peek at the next token. */
10964 token = cp_lexer_peek_token (parser->lexer);
10965 /* If the next token is a `)', `,', `=', `>', or `...', then there
10966 is no declarator. */
10967 if (token->type == CPP_CLOSE_PAREN
10968 || token->type == CPP_COMMA
10969 || token->type == CPP_EQ
10970 || token->type == CPP_ELLIPSIS
10971 || token->type == CPP_GREATER)
10972 declarator = NULL_TREE;
10973 /* Otherwise, there should be a declarator. */
10976 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
10977 parser->default_arg_ok_p = false;
10979 declarator = cp_parser_declarator (parser,
10980 CP_PARSER_DECLARATOR_EITHER,
10981 /*ctor_dtor_or_conv_p=*/NULL);
10982 parser->default_arg_ok_p = saved_default_arg_ok_p;
10983 /* After the declarator, allow more attributes. */
10984 attributes = chainon (attributes, cp_parser_attributes_opt (parser));
10987 /* The restriction on defining new types applies only to the type
10988 of the parameter, not to the default argument. */
10989 parser->type_definition_forbidden_message = saved_message;
10991 /* If the next token is `=', then process a default argument. */
10992 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10994 bool saved_greater_than_is_operator_p;
10995 /* Consume the `='. */
10996 cp_lexer_consume_token (parser->lexer);
10998 /* If we are defining a class, then the tokens that make up the
10999 default argument must be saved and processed later. */
11000 if (!template_parm_p && at_class_scope_p ()
11001 && TYPE_BEING_DEFINED (current_class_type))
11003 unsigned depth = 0;
11005 /* Create a DEFAULT_ARG to represented the unparsed default
11007 default_argument = make_node (DEFAULT_ARG);
11008 DEFARG_TOKENS (default_argument) = cp_token_cache_new ();
11010 /* Add tokens until we have processed the entire default
11017 /* Peek at the next token. */
11018 token = cp_lexer_peek_token (parser->lexer);
11019 /* What we do depends on what token we have. */
11020 switch (token->type)
11022 /* In valid code, a default argument must be
11023 immediately followed by a `,' `)', or `...'. */
11025 case CPP_CLOSE_PAREN:
11027 /* If we run into a non-nested `;', `}', or `]',
11028 then the code is invalid -- but the default
11029 argument is certainly over. */
11030 case CPP_SEMICOLON:
11031 case CPP_CLOSE_BRACE:
11032 case CPP_CLOSE_SQUARE:
11035 /* Update DEPTH, if necessary. */
11036 else if (token->type == CPP_CLOSE_PAREN
11037 || token->type == CPP_CLOSE_BRACE
11038 || token->type == CPP_CLOSE_SQUARE)
11042 case CPP_OPEN_PAREN:
11043 case CPP_OPEN_SQUARE:
11044 case CPP_OPEN_BRACE:
11049 /* If we see a non-nested `>', and `>' is not an
11050 operator, then it marks the end of the default
11052 if (!depth && !greater_than_is_operator_p)
11056 /* If we run out of tokens, issue an error message. */
11058 error ("file ends in default argument");
11064 /* In these cases, we should look for template-ids.
11065 For example, if the default argument is
11066 `X<int, double>()', we need to do name lookup to
11067 figure out whether or not `X' is a template; if
11068 so, the `,' does not end the deault argument.
11070 That is not yet done. */
11077 /* If we've reached the end, stop. */
11081 /* Add the token to the token block. */
11082 token = cp_lexer_consume_token (parser->lexer);
11083 cp_token_cache_push_token (DEFARG_TOKENS (default_argument),
11087 /* Outside of a class definition, we can just parse the
11088 assignment-expression. */
11091 bool saved_local_variables_forbidden_p;
11093 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
11095 saved_greater_than_is_operator_p
11096 = parser->greater_than_is_operator_p;
11097 parser->greater_than_is_operator_p = greater_than_is_operator_p;
11098 /* Local variable names (and the `this' keyword) may not
11099 appear in a default argument. */
11100 saved_local_variables_forbidden_p
11101 = parser->local_variables_forbidden_p;
11102 parser->local_variables_forbidden_p = true;
11103 /* Parse the assignment-expression. */
11104 default_argument = cp_parser_assignment_expression (parser);
11105 /* Restore saved state. */
11106 parser->greater_than_is_operator_p
11107 = saved_greater_than_is_operator_p;
11108 parser->local_variables_forbidden_p
11109 = saved_local_variables_forbidden_p;
11111 if (!parser->default_arg_ok_p)
11113 pedwarn ("default arguments are only permitted on functions");
11114 if (flag_pedantic_errors)
11115 default_argument = NULL_TREE;
11119 default_argument = NULL_TREE;
11121 /* Create the representation of the parameter. */
11123 decl_specifiers = tree_cons (attributes, NULL_TREE, decl_specifiers);
11124 parameter = build_tree_list (default_argument,
11125 build_tree_list (decl_specifiers,
11131 /* Parse a function-definition.
11133 function-definition:
11134 decl-specifier-seq [opt] declarator ctor-initializer [opt]
11136 decl-specifier-seq [opt] declarator function-try-block
11140 function-definition:
11141 __extension__ function-definition
11143 Returns the FUNCTION_DECL for the function. If FRIEND_P is
11144 non-NULL, *FRIEND_P is set to TRUE iff the function was declared to
11148 cp_parser_function_definition (parser, friend_p)
11152 tree decl_specifiers;
11156 tree access_checks;
11158 bool declares_class_or_enum;
11160 /* The saved value of the PEDANTIC flag. */
11161 int saved_pedantic;
11163 /* Any pending qualification must be cleared by our caller. It is
11164 more robust to force the callers to clear PARSER->SCOPE than to
11165 do it here since if the qualification is in effect here, it might
11166 also end up in effect elsewhere that it is not intended. */
11167 my_friendly_assert (!parser->scope, 20010821);
11169 /* Handle `__extension__'. */
11170 if (cp_parser_extension_opt (parser, &saved_pedantic))
11172 /* Parse the function-definition. */
11173 fn = cp_parser_function_definition (parser, friend_p);
11174 /* Restore the PEDANTIC flag. */
11175 pedantic = saved_pedantic;
11180 /* Check to see if this definition appears in a class-specifier. */
11181 member_p = (at_class_scope_p ()
11182 && TYPE_BEING_DEFINED (current_class_type));
11183 /* Defer access checks in the decl-specifier-seq until we know what
11184 function is being defined. There is no need to do this for the
11185 definition of member functions; we cannot be defining a member
11186 from another class. */
11188 cp_parser_start_deferring_access_checks (parser);
11189 /* Parse the decl-specifier-seq. */
11191 = cp_parser_decl_specifier_seq (parser,
11192 CP_PARSER_FLAGS_OPTIONAL,
11194 &declares_class_or_enum);
11195 /* Figure out whether this declaration is a `friend'. */
11197 *friend_p = cp_parser_friend_p (decl_specifiers);
11199 /* Parse the declarator. */
11200 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
11201 /*ctor_dtor_or_conv_p=*/NULL);
11203 /* Gather up any access checks that occurred. */
11205 access_checks = cp_parser_stop_deferring_access_checks (parser);
11207 access_checks = NULL_TREE;
11209 /* If something has already gone wrong, we may as well stop now. */
11210 if (declarator == error_mark_node)
11212 /* Skip to the end of the function, or if this wasn't anything
11213 like a function-definition, to a `;' in the hopes of finding
11214 a sensible place from which to continue parsing. */
11215 cp_parser_skip_to_end_of_block_or_statement (parser);
11216 return error_mark_node;
11219 /* The next character should be a `{' (for a simple function
11220 definition), a `:' (for a ctor-initializer), or `try' (for a
11221 function-try block). */
11222 token = cp_lexer_peek_token (parser->lexer);
11223 if (!cp_parser_token_starts_function_definition_p (token))
11225 /* Issue the error-message. */
11226 cp_parser_error (parser, "expected function-definition");
11227 /* Skip to the next `;'. */
11228 cp_parser_skip_to_end_of_block_or_statement (parser);
11230 return error_mark_node;
11233 /* If we are in a class scope, then we must handle
11234 function-definitions specially. In particular, we save away the
11235 tokens that make up the function body, and parse them again
11236 later, in order to handle code like:
11239 int f () { return i; }
11243 Here, we cannot parse the body of `f' until after we have seen
11244 the declaration of `i'. */
11247 cp_token_cache *cache;
11249 /* Create the function-declaration. */
11250 fn = start_method (decl_specifiers, declarator, attributes);
11251 /* If something went badly wrong, bail out now. */
11252 if (fn == error_mark_node)
11254 /* If there's a function-body, skip it. */
11255 if (cp_parser_token_starts_function_definition_p
11256 (cp_lexer_peek_token (parser->lexer)))
11257 cp_parser_skip_to_end_of_block_or_statement (parser);
11258 return error_mark_node;
11261 /* Create a token cache. */
11262 cache = cp_token_cache_new ();
11263 /* Save away the tokens that make up the body of the
11265 cp_parser_cache_group (parser, cache, CPP_CLOSE_BRACE, /*depth=*/0);
11266 /* Handle function try blocks. */
11267 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
11268 cp_parser_cache_group (parser, cache, CPP_CLOSE_BRACE, /*depth=*/0);
11270 /* Save away the inline definition; we will process it when the
11271 class is complete. */
11272 DECL_PENDING_INLINE_INFO (fn) = cache;
11273 DECL_PENDING_INLINE_P (fn) = 1;
11275 /* We're done with the inline definition. */
11276 finish_method (fn);
11278 /* Add FN to the queue of functions to be parsed later. */
11279 TREE_VALUE (parser->unparsed_functions_queues)
11280 = tree_cons (NULL_TREE, fn,
11281 TREE_VALUE (parser->unparsed_functions_queues));
11286 /* Check that the number of template-parameter-lists is OK. */
11287 if (!cp_parser_check_declarator_template_parameters (parser,
11290 cp_parser_skip_to_end_of_block_or_statement (parser);
11291 return error_mark_node;
11294 return (cp_parser_function_definition_from_specifiers_and_declarator
11295 (parser, decl_specifiers, attributes, declarator, access_checks));
11298 /* Parse a function-body.
11301 compound_statement */
11304 cp_parser_function_body (cp_parser *parser)
11306 cp_parser_compound_statement (parser);
11309 /* Parse a ctor-initializer-opt followed by a function-body. Return
11310 true if a ctor-initializer was present. */
11313 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
11316 bool ctor_initializer_p;
11318 /* Begin the function body. */
11319 body = begin_function_body ();
11320 /* Parse the optional ctor-initializer. */
11321 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
11322 /* Parse the function-body. */
11323 cp_parser_function_body (parser);
11324 /* Finish the function body. */
11325 finish_function_body (body);
11327 return ctor_initializer_p;
11330 /* Parse an initializer.
11333 = initializer-clause
11334 ( expression-list )
11336 Returns a expression representing the initializer. If no
11337 initializer is present, NULL_TREE is returned.
11339 *IS_PARENTHESIZED_INIT is set to TRUE if the `( expression-list )'
11340 production is used, and zero otherwise. *IS_PARENTHESIZED_INIT is
11341 set to FALSE if there is no initializer present. */
11344 cp_parser_initializer (parser, is_parenthesized_init)
11346 bool *is_parenthesized_init;
11351 /* Peek at the next token. */
11352 token = cp_lexer_peek_token (parser->lexer);
11354 /* Let our caller know whether or not this initializer was
11356 *is_parenthesized_init = (token->type == CPP_OPEN_PAREN);
11358 if (token->type == CPP_EQ)
11360 /* Consume the `='. */
11361 cp_lexer_consume_token (parser->lexer);
11362 /* Parse the initializer-clause. */
11363 init = cp_parser_initializer_clause (parser);
11365 else if (token->type == CPP_OPEN_PAREN)
11367 /* Consume the `('. */
11368 cp_lexer_consume_token (parser->lexer);
11369 /* Parse the expression-list. */
11370 init = cp_parser_expression_list (parser);
11371 /* Consume the `)' token. */
11372 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
11373 cp_parser_skip_to_closing_parenthesis (parser);
11377 /* Anything else is an error. */
11378 cp_parser_error (parser, "expected initializer");
11379 init = error_mark_node;
11385 /* Parse an initializer-clause.
11387 initializer-clause:
11388 assignment-expression
11389 { initializer-list , [opt] }
11392 Returns an expression representing the initializer.
11394 If the `assignment-expression' production is used the value
11395 returned is simply a reprsentation for the expression.
11397 Otherwise, a CONSTRUCTOR is returned. The CONSTRUCTOR_ELTS will be
11398 the elements of the initializer-list (or NULL_TREE, if the last
11399 production is used). The TREE_TYPE for the CONSTRUCTOR will be
11400 NULL_TREE. There is no way to detect whether or not the optional
11401 trailing `,' was provided. */
11404 cp_parser_initializer_clause (parser)
11409 /* If it is not a `{', then we are looking at an
11410 assignment-expression. */
11411 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
11412 initializer = cp_parser_assignment_expression (parser);
11415 /* Consume the `{' token. */
11416 cp_lexer_consume_token (parser->lexer);
11417 /* Create a CONSTRUCTOR to represent the braced-initializer. */
11418 initializer = make_node (CONSTRUCTOR);
11419 /* Mark it with TREE_HAS_CONSTRUCTOR. This should not be
11420 necessary, but check_initializer depends upon it, for
11422 TREE_HAS_CONSTRUCTOR (initializer) = 1;
11423 /* If it's not a `}', then there is a non-trivial initializer. */
11424 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
11426 /* Parse the initializer list. */
11427 CONSTRUCTOR_ELTS (initializer)
11428 = cp_parser_initializer_list (parser);
11429 /* A trailing `,' token is allowed. */
11430 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
11431 cp_lexer_consume_token (parser->lexer);
11434 /* Now, there should be a trailing `}'. */
11435 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
11438 return initializer;
11441 /* Parse an initializer-list.
11445 initializer-list , initializer-clause
11450 identifier : initializer-clause
11451 initializer-list, identifier : initializer-clause
11453 Returns a TREE_LIST. The TREE_VALUE of each node is an expression
11454 for the initializer. If the TREE_PURPOSE is non-NULL, it is the
11455 IDENTIFIER_NODE naming the field to initialize. */
11458 cp_parser_initializer_list (parser)
11461 tree initializers = NULL_TREE;
11463 /* Parse the rest of the list. */
11470 /* If the next token is an identifier and the following one is a
11471 colon, we are looking at the GNU designated-initializer
11473 if (cp_parser_allow_gnu_extensions_p (parser)
11474 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
11475 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
11477 /* Consume the identifier. */
11478 identifier = cp_lexer_consume_token (parser->lexer)->value;
11479 /* Consume the `:'. */
11480 cp_lexer_consume_token (parser->lexer);
11483 identifier = NULL_TREE;
11485 /* Parse the initializer. */
11486 initializer = cp_parser_initializer_clause (parser);
11488 /* Add it to the list. */
11489 initializers = tree_cons (identifier, initializer, initializers);
11491 /* If the next token is not a comma, we have reached the end of
11493 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11496 /* Peek at the next token. */
11497 token = cp_lexer_peek_nth_token (parser->lexer, 2);
11498 /* If the next token is a `}', then we're still done. An
11499 initializer-clause can have a trailing `,' after the
11500 initializer-list and before the closing `}'. */
11501 if (token->type == CPP_CLOSE_BRACE)
11504 /* Consume the `,' token. */
11505 cp_lexer_consume_token (parser->lexer);
11508 /* The initializers were built up in reverse order, so we need to
11509 reverse them now. */
11510 return nreverse (initializers);
11513 /* Classes [gram.class] */
11515 /* Parse a class-name.
11521 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
11522 to indicate that names looked up in dependent types should be
11523 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
11524 keyword has been used to indicate that the name that appears next
11525 is a template. TYPE_P is true iff the next name should be treated
11526 as class-name, even if it is declared to be some other kind of name
11527 as well. The accessibility of the class-name is checked iff
11528 CHECK_ACCESS_P is true. If CHECK_DEPENDENCY_P is FALSE, names are
11529 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
11530 is the class being defined in a class-head.
11532 Returns the TYPE_DECL representing the class. */
11535 cp_parser_class_name (cp_parser *parser,
11536 bool typename_keyword_p,
11537 bool template_keyword_p,
11539 bool check_access_p,
11540 bool check_dependency_p,
11548 /* All class-names start with an identifier. */
11549 token = cp_lexer_peek_token (parser->lexer);
11550 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
11552 cp_parser_error (parser, "expected class-name");
11553 return error_mark_node;
11556 /* PARSER->SCOPE can be cleared when parsing the template-arguments
11557 to a template-id, so we save it here. */
11558 scope = parser->scope;
11559 /* Any name names a type if we're following the `typename' keyword
11560 in a qualified name where the enclosing scope is type-dependent. */
11561 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
11562 && cp_parser_dependent_type_p (scope));
11563 /* Handle the common case (an identifier, but not a template-id)
11565 if (token->type == CPP_NAME
11566 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_LESS)
11570 /* Look for the identifier. */
11571 identifier = cp_parser_identifier (parser);
11572 /* If the next token isn't an identifier, we are certainly not
11573 looking at a class-name. */
11574 if (identifier == error_mark_node)
11575 decl = error_mark_node;
11576 /* If we know this is a type-name, there's no need to look it
11578 else if (typename_p)
11582 /* If the next token is a `::', then the name must be a type
11585 [basic.lookup.qual]
11587 During the lookup for a name preceding the :: scope
11588 resolution operator, object, function, and enumerator
11589 names are ignored. */
11590 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
11592 /* Look up the name. */
11593 decl = cp_parser_lookup_name (parser, identifier,
11596 /*is_namespace=*/false,
11597 check_dependency_p);
11602 /* Try a template-id. */
11603 decl = cp_parser_template_id (parser, template_keyword_p,
11604 check_dependency_p);
11605 if (decl == error_mark_node)
11606 return error_mark_node;
11609 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
11611 /* If this is a typename, create a TYPENAME_TYPE. */
11612 if (typename_p && decl != error_mark_node)
11613 decl = TYPE_NAME (make_typename_type (scope, decl,
11616 /* Check to see that it is really the name of a class. */
11617 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
11618 && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
11619 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
11620 /* Situations like this:
11622 template <typename T> struct A {
11623 typename T::template X<int>::I i;
11626 are problematic. Is `T::template X<int>' a class-name? The
11627 standard does not seem to be definitive, but there is no other
11628 valid interpretation of the following `::'. Therefore, those
11629 names are considered class-names. */
11630 decl = TYPE_NAME (make_typename_type (scope, decl,
11631 tf_error | tf_parsing));
11632 else if (decl == error_mark_node
11633 || TREE_CODE (decl) != TYPE_DECL
11634 || !IS_AGGR_TYPE (TREE_TYPE (decl)))
11636 cp_parser_error (parser, "expected class-name");
11637 return error_mark_node;
11643 /* Parse a class-specifier.
11646 class-head { member-specification [opt] }
11648 Returns the TREE_TYPE representing the class. */
11651 cp_parser_class_specifier (parser)
11656 tree attributes = NULL_TREE;
11657 int has_trailing_semicolon;
11658 bool nested_name_specifier_p;
11659 bool deferring_access_checks_p;
11660 tree saved_access_checks;
11661 unsigned saved_num_template_parameter_lists;
11663 /* Parse the class-head. */
11664 type = cp_parser_class_head (parser,
11665 &nested_name_specifier_p,
11666 &deferring_access_checks_p,
11667 &saved_access_checks);
11668 /* If the class-head was a semantic disaster, skip the entire body
11672 cp_parser_skip_to_end_of_block_or_statement (parser);
11673 return error_mark_node;
11675 /* Look for the `{'. */
11676 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
11677 return error_mark_node;
11678 /* Issue an error message if type-definitions are forbidden here. */
11679 cp_parser_check_type_definition (parser);
11680 /* Remember that we are defining one more class. */
11681 ++parser->num_classes_being_defined;
11682 /* Inside the class, surrounding template-parameter-lists do not
11684 saved_num_template_parameter_lists
11685 = parser->num_template_parameter_lists;
11686 parser->num_template_parameter_lists = 0;
11687 /* Start the class. */
11688 type = begin_class_definition (type);
11689 if (type == error_mark_node)
11690 /* If the type is erroneous, skip the entire body of the class. */
11691 cp_parser_skip_to_closing_brace (parser);
11693 /* Parse the member-specification. */
11694 cp_parser_member_specification_opt (parser);
11695 /* Look for the trailing `}'. */
11696 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
11697 /* We get better error messages by noticing a common problem: a
11698 missing trailing `;'. */
11699 token = cp_lexer_peek_token (parser->lexer);
11700 has_trailing_semicolon = (token->type == CPP_SEMICOLON);
11701 /* Look for attributes to apply to this class. */
11702 if (cp_parser_allow_gnu_extensions_p (parser))
11703 attributes = cp_parser_attributes_opt (parser);
11704 /* Finish the class definition. */
11705 type = finish_class_definition (type,
11707 has_trailing_semicolon,
11708 nested_name_specifier_p);
11709 /* If this class is not itself within the scope of another class,
11710 then we need to parse the bodies of all of the queued function
11711 definitions. Note that the queued functions defined in a class
11712 are not always processed immediately following the
11713 class-specifier for that class. Consider:
11716 struct B { void f() { sizeof (A); } };
11719 If `f' were processed before the processing of `A' were
11720 completed, there would be no way to compute the size of `A'.
11721 Note that the nesting we are interested in here is lexical --
11722 not the semantic nesting given by TYPE_CONTEXT. In particular,
11725 struct A { struct B; };
11726 struct A::B { void f() { } };
11728 there is no need to delay the parsing of `A::B::f'. */
11729 if (--parser->num_classes_being_defined == 0)
11731 tree last_scope = NULL_TREE;
11735 /* Reverse the queue, so that we process it in the order the
11736 functions were declared. */
11737 TREE_VALUE (parser->unparsed_functions_queues)
11738 = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
11739 /* In a first pass, parse default arguments to the functions.
11740 Then, in a second pass, parse the bodies of the functions.
11741 This two-phased approach handles cases like:
11749 for (queue_entry = TREE_VALUE (parser->unparsed_functions_queues);
11751 queue_entry = TREE_CHAIN (queue_entry))
11753 fn = TREE_VALUE (queue_entry);
11754 if (DECL_FUNCTION_TEMPLATE_P (fn))
11755 fn = DECL_TEMPLATE_RESULT (fn);
11756 /* Make sure that any template parameters are in scope. */
11757 maybe_begin_member_template_processing (fn);
11758 /* If there are default arguments that have not yet been processed,
11759 take care of them now. */
11760 cp_parser_late_parsing_default_args (parser, fn);
11761 /* Remove any template parameters from the symbol table. */
11762 maybe_end_member_template_processing ();
11764 /* Now parse the body of the functions. */
11765 while (TREE_VALUE (parser->unparsed_functions_queues))
11768 /* Figure out which function we need to process. */
11769 queue_entry = TREE_VALUE (parser->unparsed_functions_queues);
11770 fn = TREE_VALUE (queue_entry);
11772 /* Parse the function. */
11773 cp_parser_late_parsing_for_member (parser, fn);
11775 TREE_VALUE (parser->unparsed_functions_queues)
11776 = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues));
11779 /* If LAST_SCOPE is non-NULL, then we have pushed scopes one
11780 more time than we have popped, so me must pop here. */
11782 pop_scope (last_scope);
11785 /* Put back any saved access checks. */
11786 if (deferring_access_checks_p)
11788 cp_parser_start_deferring_access_checks (parser);
11789 parser->context->deferred_access_checks = saved_access_checks;
11792 /* Restore the count of active template-parameter-lists. */
11793 parser->num_template_parameter_lists
11794 = saved_num_template_parameter_lists;
11799 /* Parse a class-head.
11802 class-key identifier [opt] base-clause [opt]
11803 class-key nested-name-specifier identifier base-clause [opt]
11804 class-key nested-name-specifier [opt] template-id
11808 class-key attributes identifier [opt] base-clause [opt]
11809 class-key attributes nested-name-specifier identifier base-clause [opt]
11810 class-key attributes nested-name-specifier [opt] template-id
11813 Returns the TYPE of the indicated class. Sets
11814 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
11815 involving a nested-name-specifier was used, and FALSE otherwise.
11816 Sets *DEFERRING_ACCESS_CHECKS_P to TRUE iff we were deferring
11817 access checks before this class-head. In that case,
11818 *SAVED_ACCESS_CHECKS is set to the current list of deferred access
11821 Returns NULL_TREE if the class-head is syntactically valid, but
11822 semantically invalid in a way that means we should skip the entire
11823 body of the class. */
11826 cp_parser_class_head (parser,
11827 nested_name_specifier_p,
11828 deferring_access_checks_p,
11829 saved_access_checks)
11831 bool *nested_name_specifier_p;
11832 bool *deferring_access_checks_p;
11833 tree *saved_access_checks;
11836 tree nested_name_specifier;
11837 enum tag_types class_key;
11838 tree id = NULL_TREE;
11839 tree type = NULL_TREE;
11841 bool template_id_p = false;
11842 bool qualified_p = false;
11843 bool invalid_nested_name_p = false;
11844 unsigned num_templates;
11846 /* Assume no nested-name-specifier will be present. */
11847 *nested_name_specifier_p = false;
11848 /* Assume no template parameter lists will be used in defining the
11852 /* Look for the class-key. */
11853 class_key = cp_parser_class_key (parser);
11854 if (class_key == none_type)
11855 return error_mark_node;
11857 /* Parse the attributes. */
11858 attributes = cp_parser_attributes_opt (parser);
11860 /* If the next token is `::', that is invalid -- but sometimes
11861 people do try to write:
11865 Handle this gracefully by accepting the extra qualifier, and then
11866 issuing an error about it later if this really is a
11867 class-head. If it turns out just to be an elaborated type
11868 specifier, remain silent. */
11869 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
11870 qualified_p = true;
11872 /* Determine the name of the class. Begin by looking for an
11873 optional nested-name-specifier. */
11874 nested_name_specifier
11875 = cp_parser_nested_name_specifier_opt (parser,
11876 /*typename_keyword_p=*/false,
11877 /*check_dependency_p=*/true,
11879 /* If there was a nested-name-specifier, then there *must* be an
11881 if (nested_name_specifier)
11883 /* Although the grammar says `identifier', it really means
11884 `class-name' or `template-name'. You are only allowed to
11885 define a class that has already been declared with this
11888 The proposed resolution for Core Issue 180 says that whever
11889 you see `class T::X' you should treat `X' as a type-name.
11891 It is OK to define an inaccessible class; for example:
11893 class A { class B; };
11896 So, we ask cp_parser_class_name not to check accessibility.
11898 We do not know if we will see a class-name, or a
11899 template-name. We look for a class-name first, in case the
11900 class-name is a template-id; if we looked for the
11901 template-name first we would stop after the template-name. */
11902 cp_parser_parse_tentatively (parser);
11903 type = cp_parser_class_name (parser,
11904 /*typename_keyword_p=*/false,
11905 /*template_keyword_p=*/false,
11907 /*check_access_p=*/false,
11908 /*check_dependency_p=*/false,
11909 /*class_head_p=*/true);
11910 /* If that didn't work, ignore the nested-name-specifier. */
11911 if (!cp_parser_parse_definitely (parser))
11913 invalid_nested_name_p = true;
11914 id = cp_parser_identifier (parser);
11915 if (id == error_mark_node)
11918 /* If we could not find a corresponding TYPE, treat this
11919 declaration like an unqualified declaration. */
11920 if (type == error_mark_node)
11921 nested_name_specifier = NULL_TREE;
11922 /* Otherwise, count the number of templates used in TYPE and its
11923 containing scopes. */
11928 for (scope = TREE_TYPE (type);
11929 scope && TREE_CODE (scope) != NAMESPACE_DECL;
11930 scope = (TYPE_P (scope)
11931 ? TYPE_CONTEXT (scope)
11932 : DECL_CONTEXT (scope)))
11934 && CLASS_TYPE_P (scope)
11935 && CLASSTYPE_TEMPLATE_INFO (scope)
11936 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
11937 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
11941 /* Otherwise, the identifier is optional. */
11944 /* We don't know whether what comes next is a template-id,
11945 an identifier, or nothing at all. */
11946 cp_parser_parse_tentatively (parser);
11947 /* Check for a template-id. */
11948 id = cp_parser_template_id (parser,
11949 /*template_keyword_p=*/false,
11950 /*check_dependency_p=*/true);
11951 /* If that didn't work, it could still be an identifier. */
11952 if (!cp_parser_parse_definitely (parser))
11954 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
11955 id = cp_parser_identifier (parser);
11961 template_id_p = true;
11966 /* If it's not a `:' or a `{' then we can't really be looking at a
11967 class-head, since a class-head only appears as part of a
11968 class-specifier. We have to detect this situation before calling
11969 xref_tag, since that has irreversible side-effects. */
11970 if (!cp_parser_next_token_starts_class_definition_p (parser))
11972 cp_parser_error (parser, "expected `{' or `:'");
11973 return error_mark_node;
11976 /* At this point, we're going ahead with the class-specifier, even
11977 if some other problem occurs. */
11978 cp_parser_commit_to_tentative_parse (parser);
11979 /* Issue the error about the overly-qualified name now. */
11981 cp_parser_error (parser,
11982 "global qualification of class name is invalid");
11983 else if (invalid_nested_name_p)
11984 cp_parser_error (parser,
11985 "qualified name does not name a class");
11986 /* Make sure that the right number of template parameters were
11988 if (!cp_parser_check_template_parameters (parser, num_templates))
11989 /* If something went wrong, there is no point in even trying to
11990 process the class-definition. */
11993 /* We do not need to defer access checks for entities declared
11994 within the class. But, we do need to save any access checks that
11995 are currently deferred and restore them later, in case we are in
11996 the middle of something else. */
11997 *deferring_access_checks_p = parser->context->deferring_access_checks_p;
11998 if (*deferring_access_checks_p)
11999 *saved_access_checks = cp_parser_stop_deferring_access_checks (parser);
12001 /* Look up the type. */
12004 type = TREE_TYPE (id);
12005 maybe_process_partial_specialization (type);
12007 else if (!nested_name_specifier)
12009 /* If the class was unnamed, create a dummy name. */
12011 id = make_anon_name ();
12012 type = xref_tag (class_key, id, attributes, /*globalize=*/0);
12021 template <typename T> struct S { struct T };
12022 template <typename T> struct S::T { };
12024 we will get a TYPENAME_TYPE when processing the definition of
12025 `S::T'. We need to resolve it to the actual type before we
12026 try to define it. */
12027 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
12029 type = cp_parser_resolve_typename_type (parser, TREE_TYPE (type));
12030 if (type != error_mark_node)
12031 type = TYPE_NAME (type);
12034 maybe_process_partial_specialization (TREE_TYPE (type));
12035 class_type = current_class_type;
12036 type = TREE_TYPE (handle_class_head (class_key,
12037 nested_name_specifier,
12042 if (type != error_mark_node)
12044 if (!class_type && TYPE_CONTEXT (type))
12045 *nested_name_specifier_p = true;
12046 else if (class_type && !same_type_p (TYPE_CONTEXT (type),
12048 *nested_name_specifier_p = true;
12051 /* Indicate whether this class was declared as a `class' or as a
12053 if (TREE_CODE (type) == RECORD_TYPE)
12054 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
12055 cp_parser_check_class_key (class_key, type);
12057 /* Enter the scope containing the class; the names of base classes
12058 should be looked up in that context. For example, given:
12060 struct A { struct B {}; struct C; };
12061 struct A::C : B {};
12064 if (nested_name_specifier)
12065 push_scope (nested_name_specifier);
12066 /* Now, look for the base-clause. */
12067 token = cp_lexer_peek_token (parser->lexer);
12068 if (token->type == CPP_COLON)
12072 /* Get the list of base-classes. */
12073 bases = cp_parser_base_clause (parser);
12074 /* Process them. */
12075 xref_basetypes (type, bases);
12077 /* Leave the scope given by the nested-name-specifier. We will
12078 enter the class scope itself while processing the members. */
12079 if (nested_name_specifier)
12080 pop_scope (nested_name_specifier);
12085 /* Parse a class-key.
12092 Returns the kind of class-key specified, or none_type to indicate
12095 static enum tag_types
12096 cp_parser_class_key (parser)
12100 enum tag_types tag_type;
12102 /* Look for the class-key. */
12103 token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
12107 /* Check to see if the TOKEN is a class-key. */
12108 tag_type = cp_parser_token_is_class_key (token);
12110 cp_parser_error (parser, "expected class-key");
12114 /* Parse an (optional) member-specification.
12116 member-specification:
12117 member-declaration member-specification [opt]
12118 access-specifier : member-specification [opt] */
12121 cp_parser_member_specification_opt (parser)
12129 /* Peek at the next token. */
12130 token = cp_lexer_peek_token (parser->lexer);
12131 /* If it's a `}', or EOF then we've seen all the members. */
12132 if (token->type == CPP_CLOSE_BRACE || token->type == CPP_EOF)
12135 /* See if this token is a keyword. */
12136 keyword = token->keyword;
12140 case RID_PROTECTED:
12142 /* Consume the access-specifier. */
12143 cp_lexer_consume_token (parser->lexer);
12144 /* Remember which access-specifier is active. */
12145 current_access_specifier = token->value;
12146 /* Look for the `:'. */
12147 cp_parser_require (parser, CPP_COLON, "`:'");
12151 /* Otherwise, the next construction must be a
12152 member-declaration. */
12153 cp_parser_member_declaration (parser);
12154 reset_type_access_control ();
12159 /* Parse a member-declaration.
12161 member-declaration:
12162 decl-specifier-seq [opt] member-declarator-list [opt] ;
12163 function-definition ; [opt]
12164 :: [opt] nested-name-specifier template [opt] unqualified-id ;
12166 template-declaration
12168 member-declarator-list:
12170 member-declarator-list , member-declarator
12173 declarator pure-specifier [opt]
12174 declarator constant-initializer [opt]
12175 identifier [opt] : constant-expression
12179 member-declaration:
12180 __extension__ member-declaration
12183 declarator attributes [opt] pure-specifier [opt]
12184 declarator attributes [opt] constant-initializer [opt]
12185 identifier [opt] attributes [opt] : constant-expression */
12188 cp_parser_member_declaration (parser)
12191 tree decl_specifiers;
12192 tree prefix_attributes;
12194 bool declares_class_or_enum;
12197 int saved_pedantic;
12199 /* Check for the `__extension__' keyword. */
12200 if (cp_parser_extension_opt (parser, &saved_pedantic))
12203 cp_parser_member_declaration (parser);
12204 /* Restore the old value of the PEDANTIC flag. */
12205 pedantic = saved_pedantic;
12210 /* Check for a template-declaration. */
12211 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
12213 /* Parse the template-declaration. */
12214 cp_parser_template_declaration (parser, /*member_p=*/true);
12219 /* Check for a using-declaration. */
12220 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
12222 /* Parse the using-declaration. */
12223 cp_parser_using_declaration (parser);
12228 /* We can't tell whether we're looking at a declaration or a
12229 function-definition. */
12230 cp_parser_parse_tentatively (parser);
12232 /* Parse the decl-specifier-seq. */
12234 = cp_parser_decl_specifier_seq (parser,
12235 CP_PARSER_FLAGS_OPTIONAL,
12236 &prefix_attributes,
12237 &declares_class_or_enum);
12238 /* If there is no declarator, then the decl-specifier-seq should
12240 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12242 /* If there was no decl-specifier-seq, and the next token is a
12243 `;', then we have something like:
12249 Each member-declaration shall declare at least one member
12250 name of the class. */
12251 if (!decl_specifiers)
12254 pedwarn ("extra semicolon");
12260 /* See if this declaration is a friend. */
12261 friend_p = cp_parser_friend_p (decl_specifiers);
12262 /* If there were decl-specifiers, check to see if there was
12263 a class-declaration. */
12264 type = check_tag_decl (decl_specifiers);
12265 /* Nested classes have already been added to the class, but
12266 a `friend' needs to be explicitly registered. */
12269 /* If the `friend' keyword was present, the friend must
12270 be introduced with a class-key. */
12271 if (!declares_class_or_enum)
12272 error ("a class-key must be used when declaring a friend");
12275 template <typename T> struct A {
12276 friend struct A<T>::B;
12279 A<T>::B will be represented by a TYPENAME_TYPE, and
12280 therefore not recognized by check_tag_decl. */
12285 for (specifier = decl_specifiers;
12287 specifier = TREE_CHAIN (specifier))
12289 tree s = TREE_VALUE (specifier);
12291 if (TREE_CODE (s) == IDENTIFIER_NODE
12292 && IDENTIFIER_GLOBAL_VALUE (s))
12293 type = IDENTIFIER_GLOBAL_VALUE (s);
12294 if (TREE_CODE (s) == TYPE_DECL)
12304 error ("friend declaration does not name a class or "
12307 make_friend_class (current_class_type, type);
12309 /* If there is no TYPE, an error message will already have
12313 /* An anonymous aggregate has to be handled specially; such
12314 a declaration really declares a data member (with a
12315 particular type), as opposed to a nested class. */
12316 else if (ANON_AGGR_TYPE_P (type))
12318 /* Remove constructors and such from TYPE, now that we
12319 know it is an anoymous aggregate. */
12320 fixup_anonymous_aggr (type);
12321 /* And make the corresponding data member. */
12322 decl = build_decl (FIELD_DECL, NULL_TREE, type);
12323 /* Add it to the class. */
12324 finish_member_declaration (decl);
12330 /* See if these declarations will be friends. */
12331 friend_p = cp_parser_friend_p (decl_specifiers);
12333 /* Keep going until we hit the `;' at the end of the
12335 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12337 tree attributes = NULL_TREE;
12338 tree first_attribute;
12340 /* Peek at the next token. */
12341 token = cp_lexer_peek_token (parser->lexer);
12343 /* Check for a bitfield declaration. */
12344 if (token->type == CPP_COLON
12345 || (token->type == CPP_NAME
12346 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
12352 /* Get the name of the bitfield. Note that we cannot just
12353 check TOKEN here because it may have been invalidated by
12354 the call to cp_lexer_peek_nth_token above. */
12355 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
12356 identifier = cp_parser_identifier (parser);
12358 identifier = NULL_TREE;
12360 /* Consume the `:' token. */
12361 cp_lexer_consume_token (parser->lexer);
12362 /* Get the width of the bitfield. */
12363 width = cp_parser_constant_expression (parser);
12365 /* Look for attributes that apply to the bitfield. */
12366 attributes = cp_parser_attributes_opt (parser);
12367 /* Remember which attributes are prefix attributes and
12369 first_attribute = attributes;
12370 /* Combine the attributes. */
12371 attributes = chainon (prefix_attributes, attributes);
12373 /* Create the bitfield declaration. */
12374 decl = grokbitfield (identifier,
12377 /* Apply the attributes. */
12378 cplus_decl_attributes (&decl, attributes, /*flags=*/0);
12384 tree asm_specification;
12385 bool ctor_dtor_or_conv_p;
12387 /* Parse the declarator. */
12389 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
12390 &ctor_dtor_or_conv_p);
12392 /* If something went wrong parsing the declarator, make sure
12393 that we at least consume some tokens. */
12394 if (declarator == error_mark_node)
12396 /* Skip to the end of the statement. */
12397 cp_parser_skip_to_end_of_statement (parser);
12401 /* Look for an asm-specification. */
12402 asm_specification = cp_parser_asm_specification_opt (parser);
12403 /* Look for attributes that apply to the declaration. */
12404 attributes = cp_parser_attributes_opt (parser);
12405 /* Remember which attributes are prefix attributes and
12407 first_attribute = attributes;
12408 /* Combine the attributes. */
12409 attributes = chainon (prefix_attributes, attributes);
12411 /* If it's an `=', then we have a constant-initializer or a
12412 pure-specifier. It is not correct to parse the
12413 initializer before registering the member declaration
12414 since the member declaration should be in scope while
12415 its initializer is processed. However, the rest of the
12416 front end does not yet provide an interface that allows
12417 us to handle this correctly. */
12418 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12422 A pure-specifier shall be used only in the declaration of
12423 a virtual function.
12425 A member-declarator can contain a constant-initializer
12426 only if it declares a static member of integral or
12429 Therefore, if the DECLARATOR is for a function, we look
12430 for a pure-specifier; otherwise, we look for a
12431 constant-initializer. When we call `grokfield', it will
12432 perform more stringent semantics checks. */
12433 if (TREE_CODE (declarator) == CALL_EXPR)
12434 initializer = cp_parser_pure_specifier (parser);
12437 /* This declaration cannot be a function
12439 cp_parser_commit_to_tentative_parse (parser);
12440 /* Parse the initializer. */
12441 initializer = cp_parser_constant_initializer (parser);
12444 /* Otherwise, there is no initializer. */
12446 initializer = NULL_TREE;
12448 /* See if we are probably looking at a function
12449 definition. We are certainly not looking at at a
12450 member-declarator. Calling `grokfield' has
12451 side-effects, so we must not do it unless we are sure
12452 that we are looking at a member-declarator. */
12453 if (cp_parser_token_starts_function_definition_p
12454 (cp_lexer_peek_token (parser->lexer)))
12455 decl = error_mark_node;
12457 /* Create the declaration. */
12458 decl = grokfield (declarator,
12465 /* Reset PREFIX_ATTRIBUTES. */
12466 while (attributes && TREE_CHAIN (attributes) != first_attribute)
12467 attributes = TREE_CHAIN (attributes);
12469 TREE_CHAIN (attributes) = NULL_TREE;
12471 /* If there is any qualification still in effect, clear it
12472 now; we will be starting fresh with the next declarator. */
12473 parser->scope = NULL_TREE;
12474 parser->qualifying_scope = NULL_TREE;
12475 parser->object_scope = NULL_TREE;
12476 /* If it's a `,', then there are more declarators. */
12477 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
12478 cp_lexer_consume_token (parser->lexer);
12479 /* If the next token isn't a `;', then we have a parse error. */
12480 else if (cp_lexer_next_token_is_not (parser->lexer,
12483 cp_parser_error (parser, "expected `;'");
12484 /* Skip tokens until we find a `;' */
12485 cp_parser_skip_to_end_of_statement (parser);
12492 /* Add DECL to the list of members. */
12494 finish_member_declaration (decl);
12496 /* If DECL is a function, we must return
12497 to parse it later. (Even though there is no definition,
12498 there might be default arguments that need handling.) */
12499 if (TREE_CODE (decl) == FUNCTION_DECL)
12500 TREE_VALUE (parser->unparsed_functions_queues)
12501 = tree_cons (NULL_TREE, decl,
12502 TREE_VALUE (parser->unparsed_functions_queues));
12507 /* If everything went well, look for the `;'. */
12508 if (cp_parser_parse_definitely (parser))
12510 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
12514 /* Parse the function-definition. */
12515 decl = cp_parser_function_definition (parser, &friend_p);
12516 /* If the member was not a friend, declare it here. */
12518 finish_member_declaration (decl);
12519 /* Peek at the next token. */
12520 token = cp_lexer_peek_token (parser->lexer);
12521 /* If the next token is a semicolon, consume it. */
12522 if (token->type == CPP_SEMICOLON)
12523 cp_lexer_consume_token (parser->lexer);
12526 /* Parse a pure-specifier.
12531 Returns INTEGER_ZERO_NODE if a pure specifier is found.
12532 Otherwiser, ERROR_MARK_NODE is returned. */
12535 cp_parser_pure_specifier (parser)
12540 /* Look for the `=' token. */
12541 if (!cp_parser_require (parser, CPP_EQ, "`='"))
12542 return error_mark_node;
12543 /* Look for the `0' token. */
12544 token = cp_parser_require (parser, CPP_NUMBER, "`0'");
12545 /* Unfortunately, this will accept `0L' and `0x00' as well. We need
12546 to get information from the lexer about how the number was
12547 spelled in order to fix this problem. */
12548 if (!token || !integer_zerop (token->value))
12549 return error_mark_node;
12551 return integer_zero_node;
12554 /* Parse a constant-initializer.
12556 constant-initializer:
12557 = constant-expression
12559 Returns a representation of the constant-expression. */
12562 cp_parser_constant_initializer (parser)
12565 /* Look for the `=' token. */
12566 if (!cp_parser_require (parser, CPP_EQ, "`='"))
12567 return error_mark_node;
12569 /* It is invalid to write:
12571 struct S { static const int i = { 7 }; };
12574 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12576 cp_parser_error (parser,
12577 "a brace-enclosed initializer is not allowed here");
12578 /* Consume the opening brace. */
12579 cp_lexer_consume_token (parser->lexer);
12580 /* Skip the initializer. */
12581 cp_parser_skip_to_closing_brace (parser);
12582 /* Look for the trailing `}'. */
12583 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
12585 return error_mark_node;
12588 return cp_parser_constant_expression (parser);
12591 /* Derived classes [gram.class.derived] */
12593 /* Parse a base-clause.
12596 : base-specifier-list
12598 base-specifier-list:
12600 base-specifier-list , base-specifier
12602 Returns a TREE_LIST representing the base-classes, in the order in
12603 which they were declared. The representation of each node is as
12604 described by cp_parser_base_specifier.
12606 In the case that no bases are specified, this function will return
12607 NULL_TREE, not ERROR_MARK_NODE. */
12610 cp_parser_base_clause (parser)
12613 tree bases = NULL_TREE;
12615 /* Look for the `:' that begins the list. */
12616 cp_parser_require (parser, CPP_COLON, "`:'");
12618 /* Scan the base-specifier-list. */
12624 /* Look for the base-specifier. */
12625 base = cp_parser_base_specifier (parser);
12626 /* Add BASE to the front of the list. */
12627 if (base != error_mark_node)
12629 TREE_CHAIN (base) = bases;
12632 /* Peek at the next token. */
12633 token = cp_lexer_peek_token (parser->lexer);
12634 /* If it's not a comma, then the list is complete. */
12635 if (token->type != CPP_COMMA)
12637 /* Consume the `,'. */
12638 cp_lexer_consume_token (parser->lexer);
12641 /* PARSER->SCOPE may still be non-NULL at this point, if the last
12642 base class had a qualified name. However, the next name that
12643 appears is certainly not qualified. */
12644 parser->scope = NULL_TREE;
12645 parser->qualifying_scope = NULL_TREE;
12646 parser->object_scope = NULL_TREE;
12648 return nreverse (bases);
12651 /* Parse a base-specifier.
12654 :: [opt] nested-name-specifier [opt] class-name
12655 virtual access-specifier [opt] :: [opt] nested-name-specifier
12657 access-specifier virtual [opt] :: [opt] nested-name-specifier
12660 Returns a TREE_LIST. The TREE_PURPOSE will be one of
12661 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
12662 indicate the specifiers provided. The TREE_VALUE will be a TYPE
12663 (or the ERROR_MARK_NODE) indicating the type that was specified. */
12666 cp_parser_base_specifier (parser)
12671 bool virtual_p = false;
12672 bool duplicate_virtual_error_issued_p = false;
12673 bool duplicate_access_error_issued_p = false;
12674 bool class_scope_p;
12675 access_kind access = ak_none;
12679 /* Process the optional `virtual' and `access-specifier'. */
12682 /* Peek at the next token. */
12683 token = cp_lexer_peek_token (parser->lexer);
12684 /* Process `virtual'. */
12685 switch (token->keyword)
12688 /* If `virtual' appears more than once, issue an error. */
12689 if (virtual_p && !duplicate_virtual_error_issued_p)
12691 cp_parser_error (parser,
12692 "`virtual' specified more than once in base-specified");
12693 duplicate_virtual_error_issued_p = true;
12698 /* Consume the `virtual' token. */
12699 cp_lexer_consume_token (parser->lexer);
12704 case RID_PROTECTED:
12706 /* If more than one access specifier appears, issue an
12708 if (access != ak_none && !duplicate_access_error_issued_p)
12710 cp_parser_error (parser,
12711 "more than one access specifier in base-specified");
12712 duplicate_access_error_issued_p = true;
12715 access = ((access_kind)
12716 tree_low_cst (ridpointers[(int) token->keyword],
12719 /* Consume the access-specifier. */
12720 cp_lexer_consume_token (parser->lexer);
12730 /* Map `virtual_p' and `access' onto one of the access
12736 access_node = access_default_node;
12739 access_node = access_public_node;
12742 access_node = access_protected_node;
12745 access_node = access_private_node;
12754 access_node = access_default_virtual_node;
12757 access_node = access_public_virtual_node;
12760 access_node = access_protected_virtual_node;
12763 access_node = access_private_virtual_node;
12769 /* Look for the optional `::' operator. */
12770 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
12771 /* Look for the nested-name-specifier. The simplest way to
12776 The keyword `typename' is not permitted in a base-specifier or
12777 mem-initializer; in these contexts a qualified name that
12778 depends on a template-parameter is implicitly assumed to be a
12781 is to pretend that we have seen the `typename' keyword at this
12783 cp_parser_nested_name_specifier_opt (parser,
12784 /*typename_keyword_p=*/true,
12785 /*check_dependency_p=*/true,
12787 /* If the base class is given by a qualified name, assume that names
12788 we see are type names or templates, as appropriate. */
12789 class_scope_p = (parser->scope && TYPE_P (parser->scope));
12790 /* Finally, look for the class-name. */
12791 type = cp_parser_class_name (parser,
12795 /*check_access=*/true,
12796 /*check_dependency_p=*/true,
12797 /*class_head_p=*/false);
12799 if (type == error_mark_node)
12800 return error_mark_node;
12802 return finish_base_specifier (access_node, TREE_TYPE (type));
12805 /* Exception handling [gram.exception] */
12807 /* Parse an (optional) exception-specification.
12809 exception-specification:
12810 throw ( type-id-list [opt] )
12812 Returns a TREE_LIST representing the exception-specification. The
12813 TREE_VALUE of each node is a type. */
12816 cp_parser_exception_specification_opt (parser)
12822 /* Peek at the next token. */
12823 token = cp_lexer_peek_token (parser->lexer);
12824 /* If it's not `throw', then there's no exception-specification. */
12825 if (!cp_parser_is_keyword (token, RID_THROW))
12828 /* Consume the `throw'. */
12829 cp_lexer_consume_token (parser->lexer);
12831 /* Look for the `('. */
12832 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
12834 /* Peek at the next token. */
12835 token = cp_lexer_peek_token (parser->lexer);
12836 /* If it's not a `)', then there is a type-id-list. */
12837 if (token->type != CPP_CLOSE_PAREN)
12839 const char *saved_message;
12841 /* Types may not be defined in an exception-specification. */
12842 saved_message = parser->type_definition_forbidden_message;
12843 parser->type_definition_forbidden_message
12844 = "types may not be defined in an exception-specification";
12845 /* Parse the type-id-list. */
12846 type_id_list = cp_parser_type_id_list (parser);
12847 /* Restore the saved message. */
12848 parser->type_definition_forbidden_message = saved_message;
12851 type_id_list = empty_except_spec;
12853 /* Look for the `)'. */
12854 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
12856 return type_id_list;
12859 /* Parse an (optional) type-id-list.
12863 type-id-list , type-id
12865 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
12866 in the order that the types were presented. */
12869 cp_parser_type_id_list (parser)
12872 tree types = NULL_TREE;
12879 /* Get the next type-id. */
12880 type = cp_parser_type_id (parser);
12881 /* Add it to the list. */
12882 types = add_exception_specifier (types, type, /*complain=*/1);
12883 /* Peek at the next token. */
12884 token = cp_lexer_peek_token (parser->lexer);
12885 /* If it is not a `,', we are done. */
12886 if (token->type != CPP_COMMA)
12888 /* Consume the `,'. */
12889 cp_lexer_consume_token (parser->lexer);
12892 return nreverse (types);
12895 /* Parse a try-block.
12898 try compound-statement handler-seq */
12901 cp_parser_try_block (parser)
12906 cp_parser_require_keyword (parser, RID_TRY, "`try'");
12907 try_block = begin_try_block ();
12908 cp_parser_compound_statement (parser);
12909 finish_try_block (try_block);
12910 cp_parser_handler_seq (parser);
12911 finish_handler_sequence (try_block);
12916 /* Parse a function-try-block.
12918 function-try-block:
12919 try ctor-initializer [opt] function-body handler-seq */
12922 cp_parser_function_try_block (parser)
12926 bool ctor_initializer_p;
12928 /* Look for the `try' keyword. */
12929 if (!cp_parser_require_keyword (parser, RID_TRY, "`try'"))
12931 /* Let the rest of the front-end know where we are. */
12932 try_block = begin_function_try_block ();
12933 /* Parse the function-body. */
12935 = cp_parser_ctor_initializer_opt_and_function_body (parser);
12936 /* We're done with the `try' part. */
12937 finish_function_try_block (try_block);
12938 /* Parse the handlers. */
12939 cp_parser_handler_seq (parser);
12940 /* We're done with the handlers. */
12941 finish_function_handler_sequence (try_block);
12943 return ctor_initializer_p;
12946 /* Parse a handler-seq.
12949 handler handler-seq [opt] */
12952 cp_parser_handler_seq (parser)
12959 /* Parse the handler. */
12960 cp_parser_handler (parser);
12961 /* Peek at the next token. */
12962 token = cp_lexer_peek_token (parser->lexer);
12963 /* If it's not `catch' then there are no more handlers. */
12964 if (!cp_parser_is_keyword (token, RID_CATCH))
12969 /* Parse a handler.
12972 catch ( exception-declaration ) compound-statement */
12975 cp_parser_handler (parser)
12981 cp_parser_require_keyword (parser, RID_CATCH, "`catch'");
12982 handler = begin_handler ();
12983 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
12984 declaration = cp_parser_exception_declaration (parser);
12985 finish_handler_parms (declaration, handler);
12986 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
12987 cp_parser_compound_statement (parser);
12988 finish_handler (handler);
12991 /* Parse an exception-declaration.
12993 exception-declaration:
12994 type-specifier-seq declarator
12995 type-specifier-seq abstract-declarator
12999 Returns a VAR_DECL for the declaration, or NULL_TREE if the
13000 ellipsis variant is used. */
13003 cp_parser_exception_declaration (parser)
13006 tree type_specifiers;
13008 const char *saved_message;
13010 /* If it's an ellipsis, it's easy to handle. */
13011 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13013 /* Consume the `...' token. */
13014 cp_lexer_consume_token (parser->lexer);
13018 /* Types may not be defined in exception-declarations. */
13019 saved_message = parser->type_definition_forbidden_message;
13020 parser->type_definition_forbidden_message
13021 = "types may not be defined in exception-declarations";
13023 /* Parse the type-specifier-seq. */
13024 type_specifiers = cp_parser_type_specifier_seq (parser);
13025 /* If it's a `)', then there is no declarator. */
13026 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
13027 declarator = NULL_TREE;
13029 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
13030 /*ctor_dtor_or_conv_p=*/NULL);
13032 /* Restore the saved message. */
13033 parser->type_definition_forbidden_message = saved_message;
13035 return start_handler_parms (type_specifiers, declarator);
13038 /* Parse a throw-expression.
13041 throw assignment-expresion [opt]
13043 Returns a THROW_EXPR representing the throw-expression. */
13046 cp_parser_throw_expression (parser)
13051 cp_parser_require_keyword (parser, RID_THROW, "`throw'");
13052 /* We can't be sure if there is an assignment-expression or not. */
13053 cp_parser_parse_tentatively (parser);
13055 expression = cp_parser_assignment_expression (parser);
13056 /* If it didn't work, this is just a rethrow. */
13057 if (!cp_parser_parse_definitely (parser))
13058 expression = NULL_TREE;
13060 return build_throw (expression);
13063 /* GNU Extensions */
13065 /* Parse an (optional) asm-specification.
13068 asm ( string-literal )
13070 If the asm-specification is present, returns a STRING_CST
13071 corresponding to the string-literal. Otherwise, returns
13075 cp_parser_asm_specification_opt (parser)
13079 tree asm_specification;
13081 /* Peek at the next token. */
13082 token = cp_lexer_peek_token (parser->lexer);
13083 /* If the next token isn't the `asm' keyword, then there's no
13084 asm-specification. */
13085 if (!cp_parser_is_keyword (token, RID_ASM))
13088 /* Consume the `asm' token. */
13089 cp_lexer_consume_token (parser->lexer);
13090 /* Look for the `('. */
13091 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13093 /* Look for the string-literal. */
13094 token = cp_parser_require (parser, CPP_STRING, "string-literal");
13096 asm_specification = token->value;
13098 asm_specification = NULL_TREE;
13100 /* Look for the `)'. */
13101 cp_parser_require (parser, CPP_CLOSE_PAREN, "`('");
13103 return asm_specification;
13106 /* Parse an asm-operand-list.
13110 asm-operand-list , asm-operand
13113 string-literal ( expression )
13114 [ string-literal ] string-literal ( expression )
13116 Returns a TREE_LIST representing the operands. The TREE_VALUE of
13117 each node is the expression. The TREE_PURPOSE is itself a
13118 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
13119 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
13120 is a STRING_CST for the string literal before the parenthesis. */
13123 cp_parser_asm_operand_list (parser)
13126 tree asm_operands = NULL_TREE;
13130 tree string_literal;
13135 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
13137 /* Consume the `[' token. */
13138 cp_lexer_consume_token (parser->lexer);
13139 /* Read the operand name. */
13140 name = cp_parser_identifier (parser);
13141 if (name != error_mark_node)
13142 name = build_string (IDENTIFIER_LENGTH (name),
13143 IDENTIFIER_POINTER (name));
13144 /* Look for the closing `]'. */
13145 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
13149 /* Look for the string-literal. */
13150 token = cp_parser_require (parser, CPP_STRING, "string-literal");
13151 string_literal = token ? token->value : error_mark_node;
13152 /* Look for the `('. */
13153 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13154 /* Parse the expression. */
13155 expression = cp_parser_expression (parser);
13156 /* Look for the `)'. */
13157 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13158 /* Add this operand to the list. */
13159 asm_operands = tree_cons (build_tree_list (name, string_literal),
13162 /* If the next token is not a `,', there are no more
13164 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13166 /* Consume the `,'. */
13167 cp_lexer_consume_token (parser->lexer);
13170 return nreverse (asm_operands);
13173 /* Parse an asm-clobber-list.
13177 asm-clobber-list , string-literal
13179 Returns a TREE_LIST, indicating the clobbers in the order that they
13180 appeared. The TREE_VALUE of each node is a STRING_CST. */
13183 cp_parser_asm_clobber_list (parser)
13186 tree clobbers = NULL_TREE;
13191 tree string_literal;
13193 /* Look for the string literal. */
13194 token = cp_parser_require (parser, CPP_STRING, "string-literal");
13195 string_literal = token ? token->value : error_mark_node;
13196 /* Add it to the list. */
13197 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
13198 /* If the next token is not a `,', then the list is
13200 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13202 /* Consume the `,' token. */
13203 cp_lexer_consume_token (parser->lexer);
13209 /* Parse an (optional) series of attributes.
13212 attributes attribute
13215 __attribute__ (( attribute-list [opt] ))
13217 The return value is as for cp_parser_attribute_list. */
13220 cp_parser_attributes_opt (parser)
13223 tree attributes = NULL_TREE;
13228 tree attribute_list;
13230 /* Peek at the next token. */
13231 token = cp_lexer_peek_token (parser->lexer);
13232 /* If it's not `__attribute__', then we're done. */
13233 if (token->keyword != RID_ATTRIBUTE)
13236 /* Consume the `__attribute__' keyword. */
13237 cp_lexer_consume_token (parser->lexer);
13238 /* Look for the two `(' tokens. */
13239 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13240 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13242 /* Peek at the next token. */
13243 token = cp_lexer_peek_token (parser->lexer);
13244 if (token->type != CPP_CLOSE_PAREN)
13245 /* Parse the attribute-list. */
13246 attribute_list = cp_parser_attribute_list (parser);
13248 /* If the next token is a `)', then there is no attribute
13250 attribute_list = NULL;
13252 /* Look for the two `)' tokens. */
13253 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13254 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13256 /* Add these new attributes to the list. */
13257 attributes = chainon (attributes, attribute_list);
13263 /* Parse an attribute-list.
13267 attribute-list , attribute
13271 identifier ( identifier )
13272 identifier ( identifier , expression-list )
13273 identifier ( expression-list )
13275 Returns a TREE_LIST. Each node corresponds to an attribute. THe
13276 TREE_PURPOSE of each node is the identifier indicating which
13277 attribute is in use. The TREE_VALUE represents the arguments, if
13281 cp_parser_attribute_list (parser)
13284 tree attribute_list = NULL_TREE;
13292 /* Look for the identifier. We also allow keywords here; for
13293 example `__attribute__ ((const))' is legal. */
13294 token = cp_lexer_peek_token (parser->lexer);
13295 if (token->type != CPP_NAME
13296 && token->type != CPP_KEYWORD)
13297 return error_mark_node;
13298 /* Consume the token. */
13299 token = cp_lexer_consume_token (parser->lexer);
13301 /* Save away the identifier that indicates which attribute this is. */
13302 identifier = token->value;
13303 attribute = build_tree_list (identifier, NULL_TREE);
13305 /* Peek at the next token. */
13306 token = cp_lexer_peek_token (parser->lexer);
13307 /* If it's an `(', then parse the attribute arguments. */
13308 if (token->type == CPP_OPEN_PAREN)
13311 int arguments_allowed_p = 1;
13313 /* Consume the `('. */
13314 cp_lexer_consume_token (parser->lexer);
13315 /* Peek at the next token. */
13316 token = cp_lexer_peek_token (parser->lexer);
13317 /* Check to see if the next token is an identifier. */
13318 if (token->type == CPP_NAME)
13320 /* Save the identifier. */
13321 identifier = token->value;
13322 /* Consume the identifier. */
13323 cp_lexer_consume_token (parser->lexer);
13324 /* Peek at the next token. */
13325 token = cp_lexer_peek_token (parser->lexer);
13326 /* If the next token is a `,', then there are some other
13327 expressions as well. */
13328 if (token->type == CPP_COMMA)
13329 /* Consume the comma. */
13330 cp_lexer_consume_token (parser->lexer);
13332 arguments_allowed_p = 0;
13335 identifier = NULL_TREE;
13337 /* If there are arguments, parse them too. */
13338 if (arguments_allowed_p)
13339 arguments = cp_parser_expression_list (parser);
13341 arguments = NULL_TREE;
13343 /* Combine the identifier and the arguments. */
13345 arguments = tree_cons (NULL_TREE, identifier, arguments);
13347 /* Save the identifier and arguments away. */
13348 TREE_VALUE (attribute) = arguments;
13350 /* Look for the closing `)'. */
13351 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13354 /* Add this attribute to the list. */
13355 TREE_CHAIN (attribute) = attribute_list;
13356 attribute_list = attribute;
13358 /* Now, look for more attributes. */
13359 token = cp_lexer_peek_token (parser->lexer);
13360 /* If the next token isn't a `,', we're done. */
13361 if (token->type != CPP_COMMA)
13364 /* Consume the commma and keep going. */
13365 cp_lexer_consume_token (parser->lexer);
13368 /* We built up the list in reverse order. */
13369 return nreverse (attribute_list);
13372 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
13373 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
13374 current value of the PEDANTIC flag, regardless of whether or not
13375 the `__extension__' keyword is present. The caller is responsible
13376 for restoring the value of the PEDANTIC flag. */
13379 cp_parser_extension_opt (parser, saved_pedantic)
13381 int *saved_pedantic;
13383 /* Save the old value of the PEDANTIC flag. */
13384 *saved_pedantic = pedantic;
13386 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
13388 /* Consume the `__extension__' token. */
13389 cp_lexer_consume_token (parser->lexer);
13390 /* We're not being pedantic while the `__extension__' keyword is
13400 /* Parse a label declaration.
13403 __label__ label-declarator-seq ;
13405 label-declarator-seq:
13406 identifier , label-declarator-seq
13410 cp_parser_label_declaration (parser)
13413 /* Look for the `__label__' keyword. */
13414 cp_parser_require_keyword (parser, RID_LABEL, "`__label__'");
13420 /* Look for an identifier. */
13421 identifier = cp_parser_identifier (parser);
13422 /* Declare it as a lobel. */
13423 finish_label_decl (identifier);
13424 /* If the next token is a `;', stop. */
13425 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13427 /* Look for the `,' separating the label declarations. */
13428 cp_parser_require (parser, CPP_COMMA, "`,'");
13431 /* Look for the final `;'. */
13432 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
13435 /* Support Functions */
13437 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
13438 NAME should have one of the representations used for an
13439 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
13440 is returned. If PARSER->SCOPE is a dependent type, then a
13441 SCOPE_REF is returned.
13443 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
13444 returned; the name was already resolved when the TEMPLATE_ID_EXPR
13445 was formed. Abstractly, such entities should not be passed to this
13446 function, because they do not need to be looked up, but it is
13447 simpler to check for this special case here, rather than at the
13450 In cases not explicitly covered above, this function returns a
13451 DECL, OVERLOAD, or baselink representing the result of the lookup.
13452 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
13455 If CHECK_ACCESS is TRUE, then access control is performed on the
13456 declaration to which the name resolves, and an error message is
13457 issued if the declaration is inaccessible.
13459 If IS_TYPE is TRUE, bindings that do not refer to types are
13462 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
13465 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
13469 cp_parser_lookup_name (cp_parser *parser, tree name, bool check_access,
13470 bool is_type, bool is_namespace, bool check_dependency)
13473 tree object_type = parser->context->object_type;
13475 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
13476 no longer valid. Note that if we are parsing tentatively, and
13477 the parse fails, OBJECT_TYPE will be automatically restored. */
13478 parser->context->object_type = NULL_TREE;
13480 if (name == error_mark_node)
13481 return error_mark_node;
13483 /* A template-id has already been resolved; there is no lookup to
13485 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
13487 if (BASELINK_P (name))
13489 my_friendly_assert ((TREE_CODE (BASELINK_FUNCTIONS (name))
13490 == TEMPLATE_ID_EXPR),
13495 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
13496 it should already have been checked to make sure that the name
13497 used matches the type being destroyed. */
13498 if (TREE_CODE (name) == BIT_NOT_EXPR)
13502 /* Figure out to which type this destructor applies. */
13504 type = parser->scope;
13505 else if (object_type)
13506 type = object_type;
13508 type = current_class_type;
13509 /* If that's not a class type, there is no destructor. */
13510 if (!type || !CLASS_TYPE_P (type))
13511 return error_mark_node;
13512 /* If it was a class type, return the destructor. */
13513 return CLASSTYPE_DESTRUCTORS (type);
13516 /* By this point, the NAME should be an ordinary identifier. If
13517 the id-expression was a qualified name, the qualifying scope is
13518 stored in PARSER->SCOPE at this point. */
13519 my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE,
13522 /* Perform the lookup. */
13525 bool dependent_type_p;
13527 if (parser->scope == error_mark_node)
13528 return error_mark_node;
13530 /* If the SCOPE is dependent, the lookup must be deferred until
13531 the template is instantiated -- unless we are explicitly
13532 looking up names in uninstantiated templates. Even then, we
13533 cannot look up the name if the scope is not a class type; it
13534 might, for example, be a template type parameter. */
13535 dependent_type_p = (TYPE_P (parser->scope)
13536 && !(parser->in_declarator_p
13537 && currently_open_class (parser->scope))
13538 && cp_parser_dependent_type_p (parser->scope));
13539 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
13540 && dependent_type_p)
13543 decl = build_nt (SCOPE_REF, parser->scope, name);
13545 /* The resolution to Core Issue 180 says that `struct A::B'
13546 should be considered a type-name, even if `A' is
13548 decl = TYPE_NAME (make_typename_type (parser->scope,
13554 /* If PARSER->SCOPE is a dependent type, then it must be a
13555 class type, and we must not be checking dependencies;
13556 otherwise, we would have processed this lookup above. So
13557 that PARSER->SCOPE is not considered a dependent base by
13558 lookup_member, we must enter the scope here. */
13559 if (dependent_type_p)
13560 push_scope (parser->scope);
13561 /* If the PARSER->SCOPE is a a template specialization, it
13562 may be instantiated during name lookup. In that case,
13563 errors may be issued. Even if we rollback the current
13564 tentative parse, those errors are valid. */
13565 decl = lookup_qualified_name (parser->scope, name, is_type,
13567 if (dependent_type_p)
13568 pop_scope (parser->scope);
13570 parser->qualifying_scope = parser->scope;
13571 parser->object_scope = NULL_TREE;
13573 else if (object_type)
13575 tree object_decl = NULL_TREE;
13576 /* Look up the name in the scope of the OBJECT_TYPE, unless the
13577 OBJECT_TYPE is not a class. */
13578 if (CLASS_TYPE_P (object_type))
13579 /* If the OBJECT_TYPE is a template specialization, it may
13580 be instantiated during name lookup. In that case, errors
13581 may be issued. Even if we rollback the current tentative
13582 parse, those errors are valid. */
13583 object_decl = lookup_member (object_type,
13585 /*protect=*/0, is_type);
13586 /* Look it up in the enclosing context, too. */
13587 decl = lookup_name_real (name, is_type, /*nonclass=*/0,
13590 parser->object_scope = object_type;
13591 parser->qualifying_scope = NULL_TREE;
13593 decl = object_decl;
13597 decl = lookup_name_real (name, is_type, /*nonclass=*/0,
13600 parser->qualifying_scope = NULL_TREE;
13601 parser->object_scope = NULL_TREE;
13604 /* If the lookup failed, let our caller know. */
13606 || decl == error_mark_node
13607 || (TREE_CODE (decl) == FUNCTION_DECL
13608 && DECL_ANTICIPATED (decl)))
13609 return error_mark_node;
13611 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
13612 if (TREE_CODE (decl) == TREE_LIST)
13614 /* The error message we have to print is too complicated for
13615 cp_parser_error, so we incorporate its actions directly. */
13616 if (!cp_parser_simulate_error (parser))
13618 error ("reference to `%D' is ambiguous", name);
13619 print_candidates (decl);
13621 return error_mark_node;
13624 my_friendly_assert (DECL_P (decl)
13625 || TREE_CODE (decl) == OVERLOAD
13626 || TREE_CODE (decl) == SCOPE_REF
13627 || BASELINK_P (decl),
13630 /* If we have resolved the name of a member declaration, check to
13631 see if the declaration is accessible. When the name resolves to
13632 set of overloaded functions, accesibility is checked when
13633 overload resolution is done.
13635 During an explicit instantiation, access is not checked at all,
13636 as per [temp.explicit]. */
13637 if (check_access && scope_chain->check_access && DECL_P (decl))
13639 tree qualifying_type;
13641 /* Figure out the type through which DECL is being
13644 = cp_parser_scope_through_which_access_occurs (decl,
13647 if (qualifying_type)
13649 /* If we are supposed to defer access checks, just record
13650 the information for later. */
13651 if (parser->context->deferring_access_checks_p)
13652 cp_parser_defer_access_check (parser, qualifying_type, decl);
13653 /* Otherwise, check accessibility now. */
13655 enforce_access (qualifying_type, decl);
13662 /* Like cp_parser_lookup_name, but for use in the typical case where
13663 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, and CHECK_DEPENDENCY is
13667 cp_parser_lookup_name_simple (parser, name)
13671 return cp_parser_lookup_name (parser, name,
13672 /*check_access=*/true,
13674 /*is_namespace=*/false,
13675 /*check_dependency=*/true);
13678 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
13679 TYPENAME_TYPE corresponds. Note that this function peers inside
13680 uninstantiated templates and therefore should be used only in
13681 extremely limited situations. */
13684 cp_parser_resolve_typename_type (parser, type)
13692 my_friendly_assert (TREE_CODE (type) == TYPENAME_TYPE,
13695 scope = TYPE_CONTEXT (type);
13696 name = DECL_NAME (TYPE_NAME (type));
13698 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
13699 it first before we can figure out what NAME refers to. */
13700 if (TREE_CODE (scope) == TYPENAME_TYPE)
13701 scope = cp_parser_resolve_typename_type (parser, scope);
13702 /* If we don't know what SCOPE refers to, then we cannot resolve the
13704 if (scope == error_mark_node)
13705 return error_mark_node;
13706 /* If the SCOPE is a template type parameter, we have no way of
13707 resolving the name. */
13708 if (TREE_CODE (scope) == TEMPLATE_TYPE_PARM)
13710 /* Enter the SCOPE so that name lookup will be resolved as if we
13711 were in the class definition. In particular, SCOPE will no
13712 longer be considered a dependent type. */
13713 push_scope (scope);
13714 /* Look up the declaration. */
13715 decl = lookup_member (scope, name, /*protect=*/0, /*want_type=*/1);
13716 /* If all went well, we got a TYPE_DECL for a non-typename. */
13718 || TREE_CODE (decl) != TYPE_DECL
13719 || TREE_CODE (TREE_TYPE (decl)) == TYPENAME_TYPE)
13721 cp_parser_error (parser, "could not resolve typename type");
13722 type = error_mark_node;
13725 type = TREE_TYPE (decl);
13726 /* Leave the SCOPE. */
13732 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
13733 the current context, return the TYPE_DECL. If TAG_NAME_P is
13734 true, the DECL indicates the class being defined in a class-head,
13735 or declared in an elaborated-type-specifier.
13737 Otherwise, return DECL. */
13740 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
13742 /* If the DECL is a TEMPLATE_DECL for a class type, and we are in
13743 the scope of the class, then treat the TEMPLATE_DECL as a
13744 class-name. For example, in:
13746 template <class T> struct S {
13752 If the TEMPLATE_DECL is being declared as part of a class-head,
13753 the same translation occurs:
13756 template <typename T> struct B;
13759 template <typename T> struct A::B {};
13761 Similarly, in a elaborated-type-specifier:
13763 namespace N { struct X{}; }
13766 template <typename T> friend struct N::X;
13770 if (DECL_CLASS_TEMPLATE_P (decl)
13772 || (current_class_type
13773 && same_type_p (TREE_TYPE (DECL_TEMPLATE_RESULT (decl)),
13774 current_class_type))))
13775 return DECL_TEMPLATE_RESULT (decl);
13780 /* If too many, or too few, template-parameter lists apply to the
13781 declarator, issue an error message. Returns TRUE if all went well,
13782 and FALSE otherwise. */
13785 cp_parser_check_declarator_template_parameters (parser, declarator)
13789 unsigned num_templates;
13791 /* We haven't seen any classes that involve template parameters yet. */
13794 switch (TREE_CODE (declarator))
13801 tree main_declarator = TREE_OPERAND (declarator, 0);
13803 cp_parser_check_declarator_template_parameters (parser,
13812 scope = TREE_OPERAND (declarator, 0);
13813 member = TREE_OPERAND (declarator, 1);
13815 /* If this is a pointer-to-member, then we are not interested
13816 in the SCOPE, because it does not qualify the thing that is
13818 if (TREE_CODE (member) == INDIRECT_REF)
13819 return (cp_parser_check_declarator_template_parameters
13822 while (scope && CLASS_TYPE_P (scope))
13824 /* You're supposed to have one `template <...>'
13825 for every template class, but you don't need one
13826 for a full specialization. For example:
13828 template <class T> struct S{};
13829 template <> struct S<int> { void f(); };
13830 void S<int>::f () {}
13832 is correct; there shouldn't be a `template <>' for
13833 the definition of `S<int>::f'. */
13834 if (CLASSTYPE_TEMPLATE_INFO (scope)
13835 && (CLASSTYPE_TEMPLATE_INSTANTIATION (scope)
13836 || uses_template_parms (CLASSTYPE_TI_ARGS (scope)))
13837 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
13840 scope = TYPE_CONTEXT (scope);
13844 /* Fall through. */
13847 /* If the DECLARATOR has the form `X<y>' then it uses one
13848 additional level of template parameters. */
13849 if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
13852 return cp_parser_check_template_parameters (parser,
13857 /* NUM_TEMPLATES were used in the current declaration. If that is
13858 invalid, return FALSE and issue an error messages. Otherwise,
13862 cp_parser_check_template_parameters (parser, num_templates)
13864 unsigned num_templates;
13866 /* If there are more template classes than parameter lists, we have
13869 template <class T> void S<T>::R<T>::f (); */
13870 if (parser->num_template_parameter_lists < num_templates)
13872 error ("too few template-parameter-lists");
13875 /* If there are the same number of template classes and parameter
13876 lists, that's OK. */
13877 if (parser->num_template_parameter_lists == num_templates)
13879 /* If there are more, but only one more, then we are referring to a
13880 member template. That's OK too. */
13881 if (parser->num_template_parameter_lists == num_templates + 1)
13883 /* Otherwise, there are too many template parameter lists. We have
13886 template <class T> template <class U> void S::f(); */
13887 error ("too many template-parameter-lists");
13891 /* Parse a binary-expression of the general form:
13895 binary-expression <token> <expr>
13897 The TOKEN_TREE_MAP maps <token> types to <expr> codes. FN is used
13898 to parser the <expr>s. If the first production is used, then the
13899 value returned by FN is returned directly. Otherwise, a node with
13900 the indicated EXPR_TYPE is returned, with operands corresponding to
13901 the two sub-expressions. */
13904 cp_parser_binary_expression (parser, token_tree_map, fn)
13906 const cp_parser_token_tree_map token_tree_map;
13907 cp_parser_expression_fn fn;
13911 /* Parse the first expression. */
13912 lhs = (*fn) (parser);
13913 /* Now, look for more expressions. */
13917 const cp_parser_token_tree_map_node *map_node;
13920 /* Peek at the next token. */
13921 token = cp_lexer_peek_token (parser->lexer);
13922 /* If the token is `>', and that's not an operator at the
13923 moment, then we're done. */
13924 if (token->type == CPP_GREATER
13925 && !parser->greater_than_is_operator_p)
13927 /* If we find one of the tokens we want, build the correspoding
13928 tree representation. */
13929 for (map_node = token_tree_map;
13930 map_node->token_type != CPP_EOF;
13932 if (map_node->token_type == token->type)
13934 /* Consume the operator token. */
13935 cp_lexer_consume_token (parser->lexer);
13936 /* Parse the right-hand side of the expression. */
13937 rhs = (*fn) (parser);
13938 /* Build the binary tree node. */
13939 lhs = build_x_binary_op (map_node->tree_type, lhs, rhs);
13943 /* If the token wasn't one of the ones we want, we're done. */
13944 if (map_node->token_type == CPP_EOF)
13951 /* Parse an optional `::' token indicating that the following name is
13952 from the global namespace. If so, PARSER->SCOPE is set to the
13953 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
13954 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
13955 Returns the new value of PARSER->SCOPE, if the `::' token is
13956 present, and NULL_TREE otherwise. */
13959 cp_parser_global_scope_opt (parser, current_scope_valid_p)
13961 bool current_scope_valid_p;
13965 /* Peek at the next token. */
13966 token = cp_lexer_peek_token (parser->lexer);
13967 /* If we're looking at a `::' token then we're starting from the
13968 global namespace, not our current location. */
13969 if (token->type == CPP_SCOPE)
13971 /* Consume the `::' token. */
13972 cp_lexer_consume_token (parser->lexer);
13973 /* Set the SCOPE so that we know where to start the lookup. */
13974 parser->scope = global_namespace;
13975 parser->qualifying_scope = global_namespace;
13976 parser->object_scope = NULL_TREE;
13978 return parser->scope;
13980 else if (!current_scope_valid_p)
13982 parser->scope = NULL_TREE;
13983 parser->qualifying_scope = NULL_TREE;
13984 parser->object_scope = NULL_TREE;
13990 /* Returns TRUE if the upcoming token sequence is the start of a
13991 constructor declarator. If FRIEND_P is true, the declarator is
13992 preceded by the `friend' specifier. */
13995 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
13997 bool constructor_p;
13998 tree type_decl = NULL_TREE;
13999 bool nested_name_p;
14000 cp_token *next_token;
14002 /* The common case is that this is not a constructor declarator, so
14003 try to avoid doing lots of work if at all possible. */
14004 next_token = cp_lexer_peek_token (parser->lexer);
14005 if (next_token->type != CPP_NAME
14006 && next_token->type != CPP_SCOPE
14007 && next_token->type != CPP_NESTED_NAME_SPECIFIER
14008 && next_token->type != CPP_TEMPLATE_ID)
14011 /* Parse tentatively; we are going to roll back all of the tokens
14013 cp_parser_parse_tentatively (parser);
14014 /* Assume that we are looking at a constructor declarator. */
14015 constructor_p = true;
14016 /* Look for the optional `::' operator. */
14017 cp_parser_global_scope_opt (parser,
14018 /*current_scope_valid_p=*/false);
14019 /* Look for the nested-name-specifier. */
14021 = (cp_parser_nested_name_specifier_opt (parser,
14022 /*typename_keyword_p=*/false,
14023 /*check_dependency_p=*/false,
14026 /* Outside of a class-specifier, there must be a
14027 nested-name-specifier. */
14028 if (!nested_name_p &&
14029 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
14031 constructor_p = false;
14032 /* If we still think that this might be a constructor-declarator,
14033 look for a class-name. */
14038 template <typename T> struct S { S(); }
14039 template <typename T> S<T>::S ();
14041 we must recognize that the nested `S' names a class.
14044 template <typename T> S<T>::S<T> ();
14046 we must recognize that the nested `S' names a template. */
14047 type_decl = cp_parser_class_name (parser,
14048 /*typename_keyword_p=*/false,
14049 /*template_keyword_p=*/false,
14051 /*check_access_p=*/false,
14052 /*check_dependency_p=*/false,
14053 /*class_head_p=*/false);
14054 /* If there was no class-name, then this is not a constructor. */
14055 constructor_p = !cp_parser_error_occurred (parser);
14057 /* If we're still considering a constructor, we have to see a `(',
14058 to begin the parameter-declaration-clause, followed by either a
14059 `)', an `...', or a decl-specifier. We need to check for a
14060 type-specifier to avoid being fooled into thinking that:
14064 is a constructor. (It is actually a function named `f' that
14065 takes one parameter (of type `int') and returns a value of type
14068 && cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
14070 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
14071 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
14072 && !cp_parser_storage_class_specifier_opt (parser))
14074 if (current_class_type
14075 && !same_type_p (current_class_type, TREE_TYPE (type_decl)))
14076 /* The constructor for one class cannot be declared inside
14078 constructor_p = false;
14083 /* Names appearing in the type-specifier should be looked up
14084 in the scope of the class. */
14085 if (current_class_type)
14089 type = TREE_TYPE (type_decl);
14090 if (TREE_CODE (type) == TYPENAME_TYPE)
14091 type = cp_parser_resolve_typename_type (parser, type);
14094 /* Look for the type-specifier. */
14095 cp_parser_type_specifier (parser,
14096 CP_PARSER_FLAGS_NONE,
14097 /*is_friend=*/false,
14098 /*is_declarator=*/true,
14099 /*declares_class_or_enum=*/NULL,
14100 /*is_cv_qualifier=*/NULL);
14101 /* Leave the scope of the class. */
14105 constructor_p = !cp_parser_error_occurred (parser);
14110 constructor_p = false;
14111 /* We did not really want to consume any tokens. */
14112 cp_parser_abort_tentative_parse (parser);
14114 return constructor_p;
14117 /* Parse the definition of the function given by the DECL_SPECIFIERS,
14118 ATTRIBUTES, and DECLARATOR. The ACCESS_CHECKS have been deferred;
14119 they must be performed once we are in the scope of the function.
14121 Returns the function defined. */
14124 cp_parser_function_definition_from_specifiers_and_declarator
14125 (parser, decl_specifiers, attributes, declarator, access_checks)
14127 tree decl_specifiers;
14130 tree access_checks;
14135 /* Begin the function-definition. */
14136 success_p = begin_function_definition (decl_specifiers,
14140 /* If there were names looked up in the decl-specifier-seq that we
14141 did not check, check them now. We must wait until we are in the
14142 scope of the function to perform the checks, since the function
14143 might be a friend. */
14144 cp_parser_perform_deferred_access_checks (access_checks);
14148 /* If begin_function_definition didn't like the definition, skip
14149 the entire function. */
14150 error ("invalid function declaration");
14151 cp_parser_skip_to_end_of_block_or_statement (parser);
14152 fn = error_mark_node;
14155 fn = cp_parser_function_definition_after_declarator (parser,
14156 /*inline_p=*/false);
14161 /* Parse the part of a function-definition that follows the
14162 declarator. INLINE_P is TRUE iff this function is an inline
14163 function defined with a class-specifier.
14165 Returns the function defined. */
14168 cp_parser_function_definition_after_declarator (parser,
14174 bool ctor_initializer_p = false;
14175 bool saved_in_unbraced_linkage_specification_p;
14176 unsigned saved_num_template_parameter_lists;
14178 /* If the next token is `return', then the code may be trying to
14179 make use of the "named return value" extension that G++ used to
14181 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
14183 /* Consume the `return' keyword. */
14184 cp_lexer_consume_token (parser->lexer);
14185 /* Look for the identifier that indicates what value is to be
14187 cp_parser_identifier (parser);
14188 /* Issue an error message. */
14189 error ("named return values are no longer supported");
14190 /* Skip tokens until we reach the start of the function body. */
14191 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
14192 cp_lexer_consume_token (parser->lexer);
14194 /* The `extern' in `extern "C" void f () { ... }' does not apply to
14195 anything declared inside `f'. */
14196 saved_in_unbraced_linkage_specification_p
14197 = parser->in_unbraced_linkage_specification_p;
14198 parser->in_unbraced_linkage_specification_p = false;
14199 /* Inside the function, surrounding template-parameter-lists do not
14201 saved_num_template_parameter_lists
14202 = parser->num_template_parameter_lists;
14203 parser->num_template_parameter_lists = 0;
14204 /* If the next token is `try', then we are looking at a
14205 function-try-block. */
14206 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
14207 ctor_initializer_p = cp_parser_function_try_block (parser);
14208 /* A function-try-block includes the function-body, so we only do
14209 this next part if we're not processing a function-try-block. */
14212 = cp_parser_ctor_initializer_opt_and_function_body (parser);
14214 /* Finish the function. */
14215 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
14216 (inline_p ? 2 : 0));
14217 /* Generate code for it, if necessary. */
14219 /* Restore the saved values. */
14220 parser->in_unbraced_linkage_specification_p
14221 = saved_in_unbraced_linkage_specification_p;
14222 parser->num_template_parameter_lists
14223 = saved_num_template_parameter_lists;
14228 /* Parse a template-declaration, assuming that the `export' (and
14229 `extern') keywords, if present, has already been scanned. MEMBER_P
14230 is as for cp_parser_template_declaration. */
14233 cp_parser_template_declaration_after_export (parser, member_p)
14237 tree decl = NULL_TREE;
14238 tree parameter_list;
14239 bool friend_p = false;
14241 /* Look for the `template' keyword. */
14242 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'"))
14246 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
14249 /* Parse the template parameters. */
14250 begin_template_parm_list ();
14251 /* If the next token is `>', then we have an invalid
14252 specialization. Rather than complain about an invalid template
14253 parameter, issue an error message here. */
14254 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
14256 cp_parser_error (parser, "invalid explicit specialization");
14257 parameter_list = NULL_TREE;
14260 parameter_list = cp_parser_template_parameter_list (parser);
14261 parameter_list = end_template_parm_list (parameter_list);
14262 /* Look for the `>'. */
14263 cp_parser_skip_until_found (parser, CPP_GREATER, "`>'");
14264 /* We just processed one more parameter list. */
14265 ++parser->num_template_parameter_lists;
14266 /* If the next token is `template', there are more template
14268 if (cp_lexer_next_token_is_keyword (parser->lexer,
14270 cp_parser_template_declaration_after_export (parser, member_p);
14273 decl = cp_parser_single_declaration (parser,
14277 /* If this is a member template declaration, let the front
14279 if (member_p && !friend_p && decl)
14280 decl = finish_member_template_decl (decl);
14281 else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
14282 make_friend_class (current_class_type, TREE_TYPE (decl));
14284 /* We are done with the current parameter list. */
14285 --parser->num_template_parameter_lists;
14288 finish_template_decl (parameter_list);
14290 /* Register member declarations. */
14291 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
14292 finish_member_declaration (decl);
14294 /* If DECL is a function template, we must return to parse it later.
14295 (Even though there is no definition, there might be default
14296 arguments that need handling.) */
14297 if (member_p && decl
14298 && (TREE_CODE (decl) == FUNCTION_DECL
14299 || DECL_FUNCTION_TEMPLATE_P (decl)))
14300 TREE_VALUE (parser->unparsed_functions_queues)
14301 = tree_cons (NULL_TREE, decl,
14302 TREE_VALUE (parser->unparsed_functions_queues));
14305 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
14306 `function-definition' sequence. MEMBER_P is true, this declaration
14307 appears in a class scope.
14309 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
14310 *FRIEND_P is set to TRUE iff the declaration is a friend. */
14313 cp_parser_single_declaration (parser,
14320 bool declares_class_or_enum;
14321 tree decl = NULL_TREE;
14322 tree decl_specifiers;
14324 tree access_checks;
14326 /* Parse the dependent declaration. We don't know yet
14327 whether it will be a function-definition. */
14328 cp_parser_parse_tentatively (parser);
14329 /* Defer access checks until we know what is being declared. */
14330 cp_parser_start_deferring_access_checks (parser);
14331 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
14334 = cp_parser_decl_specifier_seq (parser,
14335 CP_PARSER_FLAGS_OPTIONAL,
14337 &declares_class_or_enum);
14338 /* Gather up the access checks that occurred the
14339 decl-specifier-seq. */
14340 access_checks = cp_parser_stop_deferring_access_checks (parser);
14341 /* Check for the declaration of a template class. */
14342 if (declares_class_or_enum)
14344 if (cp_parser_declares_only_class_p (parser))
14346 decl = shadow_tag (decl_specifiers);
14348 decl = TYPE_NAME (decl);
14350 decl = error_mark_node;
14355 /* If it's not a template class, try for a template function. If
14356 the next token is a `;', then this declaration does not declare
14357 anything. But, if there were errors in the decl-specifiers, then
14358 the error might well have come from an attempted class-specifier.
14359 In that case, there's no need to warn about a missing declarator. */
14361 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
14362 || !value_member (error_mark_node, decl_specifiers)))
14363 decl = cp_parser_init_declarator (parser,
14367 /*function_definition_allowed_p=*/false,
14369 /*function_definition_p=*/NULL);
14370 /* Clear any current qualification; whatever comes next is the start
14371 of something new. */
14372 parser->scope = NULL_TREE;
14373 parser->qualifying_scope = NULL_TREE;
14374 parser->object_scope = NULL_TREE;
14375 /* Look for a trailing `;' after the declaration. */
14376 if (!cp_parser_require (parser, CPP_SEMICOLON, "expected `;'")
14377 && cp_parser_committed_to_tentative_parse (parser))
14378 cp_parser_skip_to_end_of_block_or_statement (parser);
14379 /* If it worked, set *FRIEND_P based on the DECL_SPECIFIERS. */
14380 if (cp_parser_parse_definitely (parser))
14383 *friend_p = cp_parser_friend_p (decl_specifiers);
14385 /* Otherwise, try a function-definition. */
14387 decl = cp_parser_function_definition (parser, friend_p);
14392 /* Parse a functional cast to TYPE. Returns an expression
14393 representing the cast. */
14396 cp_parser_functional_cast (parser, type)
14400 tree expression_list;
14402 /* Look for the opening `('. */
14403 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
14404 return error_mark_node;
14405 /* If the next token is not an `)', there are arguments to the
14407 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
14408 expression_list = cp_parser_expression_list (parser);
14410 expression_list = NULL_TREE;
14411 /* Look for the closing `)'. */
14412 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14414 return build_functional_cast (type, expression_list);
14417 /* MEMBER_FUNCTION is a member function, or a friend. If default
14418 arguments, or the body of the function have not yet been parsed,
14422 cp_parser_late_parsing_for_member (parser, member_function)
14424 tree member_function;
14426 cp_lexer *saved_lexer;
14428 /* If this member is a template, get the underlying
14430 if (DECL_FUNCTION_TEMPLATE_P (member_function))
14431 member_function = DECL_TEMPLATE_RESULT (member_function);
14433 /* There should not be any class definitions in progress at this
14434 point; the bodies of members are only parsed outside of all class
14436 my_friendly_assert (parser->num_classes_being_defined == 0, 20010816);
14437 /* While we're parsing the member functions we might encounter more
14438 classes. We want to handle them right away, but we don't want
14439 them getting mixed up with functions that are currently in the
14441 parser->unparsed_functions_queues
14442 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
14444 /* Make sure that any template parameters are in scope. */
14445 maybe_begin_member_template_processing (member_function);
14447 /* If the body of the function has not yet been parsed, parse it
14449 if (DECL_PENDING_INLINE_P (member_function))
14451 tree function_scope;
14452 cp_token_cache *tokens;
14454 /* The function is no longer pending; we are processing it. */
14455 tokens = DECL_PENDING_INLINE_INFO (member_function);
14456 DECL_PENDING_INLINE_INFO (member_function) = NULL;
14457 DECL_PENDING_INLINE_P (member_function) = 0;
14458 /* If this was an inline function in a local class, enter the scope
14459 of the containing function. */
14460 function_scope = decl_function_context (member_function);
14461 if (function_scope)
14462 push_function_context_to (function_scope);
14464 /* Save away the current lexer. */
14465 saved_lexer = parser->lexer;
14466 /* Make a new lexer to feed us the tokens saved for this function. */
14467 parser->lexer = cp_lexer_new_from_tokens (tokens);
14468 parser->lexer->next = saved_lexer;
14470 /* Set the current source position to be the location of the first
14471 token in the saved inline body. */
14472 cp_lexer_peek_token (parser->lexer);
14474 /* Let the front end know that we going to be defining this
14476 start_function (NULL_TREE, member_function, NULL_TREE,
14477 SF_PRE_PARSED | SF_INCLASS_INLINE);
14479 /* Now, parse the body of the function. */
14480 cp_parser_function_definition_after_declarator (parser,
14481 /*inline_p=*/true);
14483 /* Leave the scope of the containing function. */
14484 if (function_scope)
14485 pop_function_context_from (function_scope);
14486 /* Restore the lexer. */
14487 parser->lexer = saved_lexer;
14490 /* Remove any template parameters from the symbol table. */
14491 maybe_end_member_template_processing ();
14493 /* Restore the queue. */
14494 parser->unparsed_functions_queues
14495 = TREE_CHAIN (parser->unparsed_functions_queues);
14498 /* FN is a FUNCTION_DECL which may contains a parameter with an
14499 unparsed DEFAULT_ARG. Parse the default args now. */
14502 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
14504 cp_lexer *saved_lexer;
14505 cp_token_cache *tokens;
14506 bool saved_local_variables_forbidden_p;
14509 for (parameters = TYPE_ARG_TYPES (TREE_TYPE (fn));
14511 parameters = TREE_CHAIN (parameters))
14513 if (!TREE_PURPOSE (parameters)
14514 || TREE_CODE (TREE_PURPOSE (parameters)) != DEFAULT_ARG)
14517 /* Save away the current lexer. */
14518 saved_lexer = parser->lexer;
14519 /* Create a new one, using the tokens we have saved. */
14520 tokens = DEFARG_TOKENS (TREE_PURPOSE (parameters));
14521 parser->lexer = cp_lexer_new_from_tokens (tokens);
14523 /* Set the current source position to be the location of the
14524 first token in the default argument. */
14525 cp_lexer_peek_token (parser->lexer);
14527 /* Local variable names (and the `this' keyword) may not appear
14528 in a default argument. */
14529 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
14530 parser->local_variables_forbidden_p = true;
14531 /* Parse the assignment-expression. */
14532 if (DECL_CONTEXT (fn))
14533 push_nested_class (DECL_CONTEXT (fn), 1);
14534 TREE_PURPOSE (parameters) = cp_parser_assignment_expression (parser);
14535 if (DECL_CONTEXT (fn))
14536 pop_nested_class ();
14538 /* Restore saved state. */
14539 parser->lexer = saved_lexer;
14540 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
14544 /* Parse the operand of `sizeof' (or a similar operator). Returns
14545 either a TYPE or an expression, depending on the form of the
14546 input. The KEYWORD indicates which kind of expression we have
14550 cp_parser_sizeof_operand (parser, keyword)
14554 static const char *format;
14555 tree expr = NULL_TREE;
14556 const char *saved_message;
14557 bool saved_constant_expression_p;
14559 /* Initialize FORMAT the first time we get here. */
14561 format = "types may not be defined in `%s' expressions";
14563 /* Types cannot be defined in a `sizeof' expression. Save away the
14565 saved_message = parser->type_definition_forbidden_message;
14566 /* And create the new one. */
14567 parser->type_definition_forbidden_message
14569 xmalloc (strlen (format)
14570 + strlen (IDENTIFIER_POINTER (ridpointers[keyword]))
14572 sprintf ((char *) parser->type_definition_forbidden_message,
14573 format, IDENTIFIER_POINTER (ridpointers[keyword]));
14575 /* The restrictions on constant-expressions do not apply inside
14576 sizeof expressions. */
14577 saved_constant_expression_p = parser->constant_expression_p;
14578 parser->constant_expression_p = false;
14580 /* Do not actually evaluate the expression. */
14582 /* If it's a `(', then we might be looking at the type-id
14584 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
14588 /* We can't be sure yet whether we're looking at a type-id or an
14590 cp_parser_parse_tentatively (parser);
14591 /* Consume the `('. */
14592 cp_lexer_consume_token (parser->lexer);
14593 /* Parse the type-id. */
14594 type = cp_parser_type_id (parser);
14595 /* Now, look for the trailing `)'. */
14596 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14597 /* If all went well, then we're done. */
14598 if (cp_parser_parse_definitely (parser))
14600 /* Build a list of decl-specifiers; right now, we have only
14601 a single type-specifier. */
14602 type = build_tree_list (NULL_TREE,
14605 /* Call grokdeclarator to figure out what type this is. */
14606 expr = grokdeclarator (NULL_TREE,
14610 /*attrlist=*/NULL);
14614 /* If the type-id production did not work out, then we must be
14615 looking at the unary-expression production. */
14617 expr = cp_parser_unary_expression (parser, /*address_p=*/false);
14618 /* Go back to evaluating expressions. */
14621 /* Free the message we created. */
14622 free ((char *) parser->type_definition_forbidden_message);
14623 /* And restore the old one. */
14624 parser->type_definition_forbidden_message = saved_message;
14625 parser->constant_expression_p = saved_constant_expression_p;
14630 /* If the current declaration has no declarator, return true. */
14633 cp_parser_declares_only_class_p (cp_parser *parser)
14635 /* If the next token is a `;' or a `,' then there is no
14637 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
14638 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
14641 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
14642 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
14645 cp_parser_friend_p (decl_specifiers)
14646 tree decl_specifiers;
14648 while (decl_specifiers)
14650 /* See if this decl-specifier is `friend'. */
14651 if (TREE_CODE (TREE_VALUE (decl_specifiers)) == IDENTIFIER_NODE
14652 && C_RID_CODE (TREE_VALUE (decl_specifiers)) == RID_FRIEND)
14655 /* Go on to the next decl-specifier. */
14656 decl_specifiers = TREE_CHAIN (decl_specifiers);
14662 /* If the next token is of the indicated TYPE, consume it. Otherwise,
14663 issue an error message indicating that TOKEN_DESC was expected.
14665 Returns the token consumed, if the token had the appropriate type.
14666 Otherwise, returns NULL. */
14669 cp_parser_require (parser, type, token_desc)
14671 enum cpp_ttype type;
14672 const char *token_desc;
14674 if (cp_lexer_next_token_is (parser->lexer, type))
14675 return cp_lexer_consume_token (parser->lexer);
14678 /* Output the MESSAGE -- unless we're parsing tentatively. */
14679 if (!cp_parser_simulate_error (parser))
14680 error ("expected %s", token_desc);
14685 /* Like cp_parser_require, except that tokens will be skipped until
14686 the desired token is found. An error message is still produced if
14687 the next token is not as expected. */
14690 cp_parser_skip_until_found (parser, type, token_desc)
14692 enum cpp_ttype type;
14693 const char *token_desc;
14696 unsigned nesting_depth = 0;
14698 if (cp_parser_require (parser, type, token_desc))
14701 /* Skip tokens until the desired token is found. */
14704 /* Peek at the next token. */
14705 token = cp_lexer_peek_token (parser->lexer);
14706 /* If we've reached the token we want, consume it and
14708 if (token->type == type && !nesting_depth)
14710 cp_lexer_consume_token (parser->lexer);
14713 /* If we've run out of tokens, stop. */
14714 if (token->type == CPP_EOF)
14716 if (token->type == CPP_OPEN_BRACE
14717 || token->type == CPP_OPEN_PAREN
14718 || token->type == CPP_OPEN_SQUARE)
14720 else if (token->type == CPP_CLOSE_BRACE
14721 || token->type == CPP_CLOSE_PAREN
14722 || token->type == CPP_CLOSE_SQUARE)
14724 if (nesting_depth-- == 0)
14727 /* Consume this token. */
14728 cp_lexer_consume_token (parser->lexer);
14732 /* If the next token is the indicated keyword, consume it. Otherwise,
14733 issue an error message indicating that TOKEN_DESC was expected.
14735 Returns the token consumed, if the token had the appropriate type.
14736 Otherwise, returns NULL. */
14739 cp_parser_require_keyword (parser, keyword, token_desc)
14742 const char *token_desc;
14744 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
14746 if (token && token->keyword != keyword)
14748 dyn_string_t error_msg;
14750 /* Format the error message. */
14751 error_msg = dyn_string_new (0);
14752 dyn_string_append_cstr (error_msg, "expected ");
14753 dyn_string_append_cstr (error_msg, token_desc);
14754 cp_parser_error (parser, error_msg->s);
14755 dyn_string_delete (error_msg);
14762 /* Returns TRUE iff TOKEN is a token that can begin the body of a
14763 function-definition. */
14766 cp_parser_token_starts_function_definition_p (token)
14769 return (/* An ordinary function-body begins with an `{'. */
14770 token->type == CPP_OPEN_BRACE
14771 /* A ctor-initializer begins with a `:'. */
14772 || token->type == CPP_COLON
14773 /* A function-try-block begins with `try'. */
14774 || token->keyword == RID_TRY
14775 /* The named return value extension begins with `return'. */
14776 || token->keyword == RID_RETURN);
14779 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
14783 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
14787 token = cp_lexer_peek_token (parser->lexer);
14788 return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
14791 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
14792 or none_type otherwise. */
14794 static enum tag_types
14795 cp_parser_token_is_class_key (token)
14798 switch (token->keyword)
14803 return record_type;
14812 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
14815 cp_parser_check_class_key (enum tag_types class_key, tree type)
14817 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
14818 pedwarn ("`%s' tag used in naming `%#T'",
14819 class_key == union_type ? "union"
14820 : class_key == record_type ? "struct" : "class",
14824 /* Look for the `template' keyword, as a syntactic disambiguator.
14825 Return TRUE iff it is present, in which case it will be
14829 cp_parser_optional_template_keyword (cp_parser *parser)
14831 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
14833 /* The `template' keyword can only be used within templates;
14834 outside templates the parser can always figure out what is a
14835 template and what is not. */
14836 if (!processing_template_decl)
14838 error ("`template' (as a disambiguator) is only allowed "
14839 "within templates");
14840 /* If this part of the token stream is rescanned, the same
14841 error message would be generated. So, we purge the token
14842 from the stream. */
14843 cp_lexer_purge_token (parser->lexer);
14848 /* Consume the `template' keyword. */
14849 cp_lexer_consume_token (parser->lexer);
14857 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
14858 set PARSER->SCOPE, and perform other related actions. */
14861 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
14866 /* Get the stored value. */
14867 value = cp_lexer_consume_token (parser->lexer)->value;
14868 /* Perform any access checks that were deferred. */
14869 for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
14870 cp_parser_defer_access_check (parser,
14871 TREE_PURPOSE (check),
14872 TREE_VALUE (check));
14873 /* Set the scope from the stored value. */
14874 parser->scope = TREE_VALUE (value);
14875 parser->qualifying_scope = TREE_TYPE (value);
14876 parser->object_scope = NULL_TREE;
14879 /* Add tokens to CACHE until an non-nested END token appears. */
14882 cp_parser_cache_group (cp_parser *parser,
14883 cp_token_cache *cache,
14884 enum cpp_ttype end,
14891 /* Abort a parenthesized expression if we encounter a brace. */
14892 if ((end == CPP_CLOSE_PAREN || depth == 0)
14893 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
14895 /* Consume the next token. */
14896 token = cp_lexer_consume_token (parser->lexer);
14897 /* If we've reached the end of the file, stop. */
14898 if (token->type == CPP_EOF)
14900 /* Add this token to the tokens we are saving. */
14901 cp_token_cache_push_token (cache, token);
14902 /* See if it starts a new group. */
14903 if (token->type == CPP_OPEN_BRACE)
14905 cp_parser_cache_group (parser, cache, CPP_CLOSE_BRACE, depth + 1);
14909 else if (token->type == CPP_OPEN_PAREN)
14910 cp_parser_cache_group (parser, cache, CPP_CLOSE_PAREN, depth + 1);
14911 else if (token->type == end)
14916 /* Begin parsing tentatively. We always save tokens while parsing
14917 tentatively so that if the tentative parsing fails we can restore the
14921 cp_parser_parse_tentatively (parser)
14924 /* Enter a new parsing context. */
14925 parser->context = cp_parser_context_new (parser->context);
14926 /* Begin saving tokens. */
14927 cp_lexer_save_tokens (parser->lexer);
14928 /* In order to avoid repetitive access control error messages,
14929 access checks are queued up until we are no longer parsing
14931 cp_parser_start_deferring_access_checks (parser);
14934 /* Commit to the currently active tentative parse. */
14937 cp_parser_commit_to_tentative_parse (parser)
14940 cp_parser_context *context;
14943 /* Mark all of the levels as committed. */
14944 lexer = parser->lexer;
14945 for (context = parser->context; context->next; context = context->next)
14947 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
14949 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
14950 while (!cp_lexer_saving_tokens (lexer))
14951 lexer = lexer->next;
14952 cp_lexer_commit_tokens (lexer);
14956 /* Abort the currently active tentative parse. All consumed tokens
14957 will be rolled back, and no diagnostics will be issued. */
14960 cp_parser_abort_tentative_parse (parser)
14963 cp_parser_simulate_error (parser);
14964 /* Now, pretend that we want to see if the construct was
14965 successfully parsed. */
14966 cp_parser_parse_definitely (parser);
14969 /* Stop parsing tentatively. If a parse error has ocurred, restore the
14970 token stream. Otherwise, commit to the tokens we have consumed.
14971 Returns true if no error occurred; false otherwise. */
14974 cp_parser_parse_definitely (parser)
14977 bool error_occurred;
14978 cp_parser_context *context;
14980 /* Remember whether or not an error ocurred, since we are about to
14981 destroy that information. */
14982 error_occurred = cp_parser_error_occurred (parser);
14983 /* Remove the topmost context from the stack. */
14984 context = parser->context;
14985 parser->context = context->next;
14986 /* If no parse errors occurred, commit to the tentative parse. */
14987 if (!error_occurred)
14989 /* Commit to the tokens read tentatively, unless that was
14991 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
14992 cp_lexer_commit_tokens (parser->lexer);
14993 if (!parser->context->deferring_access_checks_p)
14994 /* If in the parent context we are not deferring checks, then
14995 these perform these checks now. */
14996 (cp_parser_perform_deferred_access_checks
14997 (context->deferred_access_checks));
14999 /* Any lookups that were deferred during the tentative parse are
15001 parser->context->deferred_access_checks
15002 = chainon (parser->context->deferred_access_checks,
15003 context->deferred_access_checks);
15005 /* Otherwise, if errors occurred, roll back our state so that things
15006 are just as they were before we began the tentative parse. */
15008 cp_lexer_rollback_tokens (parser->lexer);
15009 /* Add the context to the front of the free list. */
15010 context->next = cp_parser_context_free_list;
15011 cp_parser_context_free_list = context;
15013 return !error_occurred;
15016 /* Returns true if we are parsing tentatively -- but have decided that
15017 we will stick with this tentative parse, even if errors occur. */
15020 cp_parser_committed_to_tentative_parse (parser)
15023 return (cp_parser_parsing_tentatively (parser)
15024 && parser->context->status == CP_PARSER_STATUS_KIND_COMMITTED);
15027 /* Returns non-zero iff an error has occurred during the most recent
15028 tentative parse. */
15031 cp_parser_error_occurred (parser)
15034 return (cp_parser_parsing_tentatively (parser)
15035 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
15038 /* Returns non-zero if GNU extensions are allowed. */
15041 cp_parser_allow_gnu_extensions_p (parser)
15044 return parser->allow_gnu_extensions_p;
15051 static GTY (()) cp_parser *the_parser;
15053 /* External interface. */
15055 /* Parse the entire translation unit. */
15060 bool error_occurred;
15062 the_parser = cp_parser_new ();
15063 error_occurred = cp_parser_translation_unit (the_parser);
15068 return error_occurred;
15071 /* Clean up after parsing the entire translation unit. */
15074 free_parser_stacks ()
15076 /* Nothing to do. */
15079 /* This variable must be provided by every front end. */
15083 #include "gt-cp-parser.h"