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_cache_group
1768 (cp_parser *, cp_token_cache *, enum cpp_ttype, unsigned);
1769 static void cp_parser_parse_tentatively
1770 PARAMS ((cp_parser *));
1771 static void cp_parser_commit_to_tentative_parse
1772 PARAMS ((cp_parser *));
1773 static void cp_parser_abort_tentative_parse
1774 PARAMS ((cp_parser *));
1775 static bool cp_parser_parse_definitely
1776 PARAMS ((cp_parser *));
1777 static inline bool cp_parser_parsing_tentatively
1778 PARAMS ((cp_parser *));
1779 static bool cp_parser_committed_to_tentative_parse
1780 PARAMS ((cp_parser *));
1781 static void cp_parser_error
1782 PARAMS ((cp_parser *, const char *));
1783 static bool cp_parser_simulate_error
1784 PARAMS ((cp_parser *));
1785 static void cp_parser_check_type_definition
1786 PARAMS ((cp_parser *));
1787 static bool cp_parser_skip_to_closing_parenthesis
1788 PARAMS ((cp_parser *));
1789 static bool cp_parser_skip_to_closing_parenthesis_or_comma
1791 static void cp_parser_skip_to_end_of_statement
1792 PARAMS ((cp_parser *));
1793 static void cp_parser_skip_to_end_of_block_or_statement
1794 PARAMS ((cp_parser *));
1795 static void cp_parser_skip_to_closing_brace
1797 static void cp_parser_skip_until_found
1798 PARAMS ((cp_parser *, enum cpp_ttype, const char *));
1799 static bool cp_parser_error_occurred
1800 PARAMS ((cp_parser *));
1801 static bool cp_parser_allow_gnu_extensions_p
1802 PARAMS ((cp_parser *));
1803 static bool cp_parser_is_string_literal
1804 PARAMS ((cp_token *));
1805 static bool cp_parser_is_keyword
1806 PARAMS ((cp_token *, enum rid));
1807 static bool cp_parser_dependent_type_p
1809 static bool cp_parser_value_dependent_expression_p
1811 static bool cp_parser_type_dependent_expression_p
1813 static bool cp_parser_dependent_template_arg_p
1815 static bool cp_parser_dependent_template_id_p
1817 static bool cp_parser_dependent_template_p
1819 static void cp_parser_defer_access_check
1820 (cp_parser *, tree, tree);
1821 static void cp_parser_start_deferring_access_checks
1823 static tree cp_parser_stop_deferring_access_checks
1824 PARAMS ((cp_parser *));
1825 static void cp_parser_perform_deferred_access_checks
1827 static tree cp_parser_scope_through_which_access_occurs
1830 /* Returns non-zero if we are parsing tentatively. */
1833 cp_parser_parsing_tentatively (parser)
1836 return parser->context->next != NULL;
1839 /* Returns non-zero if TOKEN is a string literal. */
1842 cp_parser_is_string_literal (token)
1845 return (token->type == CPP_STRING || token->type == CPP_WSTRING);
1848 /* Returns non-zero if TOKEN is the indicated KEYWORD. */
1851 cp_parser_is_keyword (token, keyword)
1855 return token->keyword == keyword;
1858 /* Returns TRUE if TYPE is dependent, in the sense of
1862 cp_parser_dependent_type_p (type)
1867 if (!processing_template_decl)
1870 /* If the type is NULL, we have not computed a type for the entity
1871 in question; in that case, the type is dependent. */
1875 /* Erroneous types can be considered non-dependent. */
1876 if (type == error_mark_node)
1881 A type is dependent if it is:
1883 -- a template parameter. */
1884 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
1886 /* -- a qualified-id with a nested-name-specifier which contains a
1887 class-name that names a dependent type or whose unqualified-id
1888 names a dependent type. */
1889 if (TREE_CODE (type) == TYPENAME_TYPE)
1891 /* -- a cv-qualified type where the cv-unqualified type is
1893 type = TYPE_MAIN_VARIANT (type);
1894 /* -- a compound type constructed from any dependent type. */
1895 if (TYPE_PTRMEM_P (type) || TYPE_PTRMEMFUNC_P (type))
1896 return (cp_parser_dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type))
1897 || cp_parser_dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
1899 else if (TREE_CODE (type) == POINTER_TYPE
1900 || TREE_CODE (type) == REFERENCE_TYPE)
1901 return cp_parser_dependent_type_p (TREE_TYPE (type));
1902 else if (TREE_CODE (type) == FUNCTION_TYPE
1903 || TREE_CODE (type) == METHOD_TYPE)
1907 if (cp_parser_dependent_type_p (TREE_TYPE (type)))
1909 for (arg_type = TYPE_ARG_TYPES (type);
1911 arg_type = TREE_CHAIN (arg_type))
1912 if (cp_parser_dependent_type_p (TREE_VALUE (arg_type)))
1916 /* -- an array type constructed from any dependent type or whose
1917 size is specified by a constant expression that is
1919 if (TREE_CODE (type) == ARRAY_TYPE)
1921 if (TYPE_DOMAIN (type)
1922 && ((cp_parser_value_dependent_expression_p
1923 (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
1924 || (cp_parser_type_dependent_expression_p
1925 (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))))
1927 return cp_parser_dependent_type_p (TREE_TYPE (type));
1929 /* -- a template-id in which either the template name is a template
1930 parameter or any of the template arguments is a dependent type or
1931 an expression that is type-dependent or value-dependent.
1933 This language seems somewhat confused; for example, it does not
1934 discuss template template arguments. Therefore, we use the
1935 definition for dependent template arguments in [temp.dep.temp]. */
1936 if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INFO (type)
1937 && (cp_parser_dependent_template_id_p
1938 (CLASSTYPE_TI_TEMPLATE (type),
1939 CLASSTYPE_TI_ARGS (type))))
1941 else if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
1943 /* All TYPEOF_TYPEs are dependent; if the argument of the `typeof'
1944 expression is not type-dependent, then it should already been
1946 if (TREE_CODE (type) == TYPEOF_TYPE)
1948 /* The standard does not specifically mention types that are local
1949 to template functions or local classes, but they should be
1950 considered dependent too. For example:
1952 template <int I> void f() {
1957 The size of `E' cannot be known until the value of `I' has been
1958 determined. Therefore, `E' must be considered dependent. */
1959 scope = TYPE_CONTEXT (type);
1960 if (scope && TYPE_P (scope))
1961 return cp_parser_dependent_type_p (scope);
1962 else if (scope && TREE_CODE (scope) == FUNCTION_DECL)
1963 return cp_parser_type_dependent_expression_p (scope);
1965 /* Other types are non-dependent. */
1969 /* Returns TRUE if the EXPRESSION is value-dependent. */
1972 cp_parser_value_dependent_expression_p (tree expression)
1974 if (!processing_template_decl)
1977 /* A name declared with a dependent type. */
1978 if (DECL_P (expression)
1979 && cp_parser_dependent_type_p (TREE_TYPE (expression)))
1981 /* A non-type template parameter. */
1982 if ((TREE_CODE (expression) == CONST_DECL
1983 && DECL_TEMPLATE_PARM_P (expression))
1984 || TREE_CODE (expression) == TEMPLATE_PARM_INDEX)
1986 /* A constant with integral or enumeration type and is initialized
1987 with an expression that is value-dependent. */
1988 if (TREE_CODE (expression) == VAR_DECL
1989 && DECL_INITIAL (expression)
1990 && (CP_INTEGRAL_TYPE_P (TREE_TYPE (expression))
1991 || TREE_CODE (TREE_TYPE (expression)) == ENUMERAL_TYPE)
1992 && cp_parser_value_dependent_expression_p (DECL_INITIAL (expression)))
1994 /* These expressions are value-dependent if the type to which the
1995 cast occurs is dependent. */
1996 if ((TREE_CODE (expression) == DYNAMIC_CAST_EXPR
1997 || TREE_CODE (expression) == STATIC_CAST_EXPR
1998 || TREE_CODE (expression) == CONST_CAST_EXPR
1999 || TREE_CODE (expression) == REINTERPRET_CAST_EXPR
2000 || TREE_CODE (expression) == CAST_EXPR)
2001 && cp_parser_dependent_type_p (TREE_TYPE (expression)))
2003 /* A `sizeof' expression where the sizeof operand is a type is
2004 value-dependent if the type is dependent. If the type was not
2005 dependent, we would no longer have a SIZEOF_EXPR, so any
2006 SIZEOF_EXPR is dependent. */
2007 if (TREE_CODE (expression) == SIZEOF_EXPR)
2009 /* A constant expression is value-dependent if any subexpression is
2011 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (expression))))
2013 switch (TREE_CODE_CLASS (TREE_CODE (expression)))
2016 return (cp_parser_value_dependent_expression_p
2017 (TREE_OPERAND (expression, 0)));
2020 return ((cp_parser_value_dependent_expression_p
2021 (TREE_OPERAND (expression, 0)))
2022 || (cp_parser_value_dependent_expression_p
2023 (TREE_OPERAND (expression, 1))));
2028 i < TREE_CODE_LENGTH (TREE_CODE (expression));
2030 if (cp_parser_value_dependent_expression_p
2031 (TREE_OPERAND (expression, i)))
2038 /* The expression is not value-dependent. */
2042 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
2046 cp_parser_type_dependent_expression_p (expression)
2049 if (!processing_template_decl)
2052 /* Some expression forms are never type-dependent. */
2053 if (TREE_CODE (expression) == PSEUDO_DTOR_EXPR
2054 || TREE_CODE (expression) == SIZEOF_EXPR
2055 || TREE_CODE (expression) == ALIGNOF_EXPR
2056 || TREE_CODE (expression) == TYPEID_EXPR
2057 || TREE_CODE (expression) == DELETE_EXPR
2058 || TREE_CODE (expression) == VEC_DELETE_EXPR
2059 || TREE_CODE (expression) == THROW_EXPR)
2062 /* The types of these expressions depends only on the type to which
2064 if (TREE_CODE (expression) == DYNAMIC_CAST_EXPR
2065 || TREE_CODE (expression) == STATIC_CAST_EXPR
2066 || TREE_CODE (expression) == CONST_CAST_EXPR
2067 || TREE_CODE (expression) == REINTERPRET_CAST_EXPR
2068 || TREE_CODE (expression) == CAST_EXPR)
2069 return cp_parser_dependent_type_p (TREE_TYPE (expression));
2070 /* The types of these expressions depends only on the type created
2071 by the expression. */
2072 else if (TREE_CODE (expression) == NEW_EXPR
2073 || TREE_CODE (expression) == VEC_NEW_EXPR)
2074 return cp_parser_dependent_type_p (TREE_OPERAND (expression, 1));
2076 if (TREE_CODE (expression) == FUNCTION_DECL
2077 && DECL_LANG_SPECIFIC (expression)
2078 && DECL_TEMPLATE_INFO (expression)
2079 && (cp_parser_dependent_template_id_p
2080 (DECL_TI_TEMPLATE (expression),
2081 INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression)))))
2084 return (cp_parser_dependent_type_p (TREE_TYPE (expression)));
2087 /* Returns TRUE if the ARG (a template argument) is dependent. */
2090 cp_parser_dependent_template_arg_p (tree arg)
2092 if (!processing_template_decl)
2095 if (TREE_CODE (arg) == TEMPLATE_DECL
2096 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
2097 return cp_parser_dependent_template_p (arg);
2098 else if (TYPE_P (arg))
2099 return cp_parser_dependent_type_p (arg);
2101 return (cp_parser_type_dependent_expression_p (arg)
2102 || cp_parser_value_dependent_expression_p (arg));
2105 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
2108 cp_parser_dependent_template_id_p (tree tmpl, tree args)
2112 if (cp_parser_dependent_template_p (tmpl))
2114 for (i = 0; i < TREE_VEC_LENGTH (args); ++i)
2115 if (cp_parser_dependent_template_arg_p (TREE_VEC_ELT (args, i)))
2120 /* Returns TRUE if the template TMPL is dependent. */
2123 cp_parser_dependent_template_p (tree tmpl)
2125 /* Template template parameters are dependent. */
2126 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)
2127 || TREE_CODE (tmpl) == TEMPLATE_TEMPLATE_PARM)
2129 /* So are member templates of dependent classes. */
2130 if (TYPE_P (CP_DECL_CONTEXT (tmpl)))
2131 return cp_parser_dependent_type_p (DECL_CONTEXT (tmpl));
2135 /* Defer checking the accessibility of DECL, when looked up in
2139 cp_parser_defer_access_check (cp_parser *parser,
2145 /* If we are not supposed to defer access checks, just check now. */
2146 if (!parser->context->deferring_access_checks_p)
2148 enforce_access (class_type, decl);
2152 /* See if we are already going to perform this check. */
2153 for (check = parser->context->deferred_access_checks;
2155 check = TREE_CHAIN (check))
2156 if (TREE_VALUE (check) == decl
2157 && same_type_p (TREE_PURPOSE (check), class_type))
2159 /* If not, record the check. */
2160 parser->context->deferred_access_checks
2161 = tree_cons (class_type, decl, parser->context->deferred_access_checks);
2164 /* Start deferring access control checks. */
2167 cp_parser_start_deferring_access_checks (cp_parser *parser)
2169 parser->context->deferring_access_checks_p = true;
2172 /* Stop deferring access control checks. Returns a TREE_LIST
2173 representing the deferred checks. The TREE_PURPOSE of each node is
2174 the type through which the access occurred; the TREE_VALUE is the
2175 declaration named. */
2178 cp_parser_stop_deferring_access_checks (parser)
2183 parser->context->deferring_access_checks_p = false;
2184 access_checks = parser->context->deferred_access_checks;
2185 parser->context->deferred_access_checks = NULL_TREE;
2187 return access_checks;
2190 /* Perform the deferred ACCESS_CHECKS, whose representation is as
2191 documented with cp_parser_stop_deferrring_access_checks. */
2194 cp_parser_perform_deferred_access_checks (access_checks)
2197 tree deferred_check;
2199 /* Look through all the deferred checks. */
2200 for (deferred_check = access_checks;
2202 deferred_check = TREE_CHAIN (deferred_check))
2204 enforce_access (TREE_PURPOSE (deferred_check),
2205 TREE_VALUE (deferred_check));
2208 /* Returns the scope through which DECL is being accessed, or
2209 NULL_TREE if DECL is not a member. If OBJECT_TYPE is non-NULL, we
2210 have just seen `x->' or `x.' and OBJECT_TYPE is the type of `*x',
2211 or `x', respectively. If the DECL was named as `A::B' then
2212 NESTED_NAME_SPECIFIER is `A'. */
2215 cp_parser_scope_through_which_access_occurs (decl,
2217 nested_name_specifier)
2220 tree nested_name_specifier;
2223 tree qualifying_type = NULL_TREE;
2225 /* Determine the SCOPE of DECL. */
2226 scope = context_for_name_lookup (decl);
2227 /* If the SCOPE is not a type, then DECL is not a member. */
2228 if (!TYPE_P (scope))
2230 /* Figure out the type through which DECL is being accessed. */
2231 if (object_type && DERIVED_FROM_P (scope, object_type))
2232 /* If we are processing a `->' or `.' expression, use the type of the
2234 qualifying_type = object_type;
2235 else if (nested_name_specifier)
2237 /* If the reference is to a non-static member of the
2238 current class, treat it as if it were referenced through
2240 if (DECL_NONSTATIC_MEMBER_P (decl)
2241 && current_class_ptr
2242 && DERIVED_FROM_P (scope, current_class_type))
2243 qualifying_type = current_class_type;
2244 /* Otherwise, use the type indicated by the
2245 nested-name-specifier. */
2247 qualifying_type = nested_name_specifier;
2250 /* Otherwise, the name must be from the current class or one of
2252 qualifying_type = currently_open_derived_class (scope);
2254 return qualifying_type;
2257 /* Issue the indicated error MESSAGE. */
2260 cp_parser_error (parser, message)
2262 const char *message;
2264 /* Output the MESSAGE -- unless we're parsing tentatively. */
2265 if (!cp_parser_simulate_error (parser))
2269 /* If we are parsing tentatively, remember that an error has occurred
2270 during this tentative parse. Returns true if the error was
2271 simulated; false if a messgae should be issued by the caller. */
2274 cp_parser_simulate_error (parser)
2277 if (cp_parser_parsing_tentatively (parser)
2278 && !cp_parser_committed_to_tentative_parse (parser))
2280 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2286 /* This function is called when a type is defined. If type
2287 definitions are forbidden at this point, an error message is
2291 cp_parser_check_type_definition (parser)
2294 /* If types are forbidden here, issue a message. */
2295 if (parser->type_definition_forbidden_message)
2296 /* Use `%s' to print the string in case there are any escape
2297 characters in the message. */
2298 error ("%s", parser->type_definition_forbidden_message);
2301 /* Consume tokens up to, and including, the next non-nested closing `)'.
2302 Returns TRUE iff we found a closing `)'. */
2305 cp_parser_skip_to_closing_parenthesis (cp_parser *parser)
2307 unsigned nesting_depth = 0;
2313 /* If we've run out of tokens, then there is no closing `)'. */
2314 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2316 /* Consume the token. */
2317 token = cp_lexer_consume_token (parser->lexer);
2318 /* If it is an `(', we have entered another level of nesting. */
2319 if (token->type == CPP_OPEN_PAREN)
2321 /* If it is a `)', then we might be done. */
2322 else if (token->type == CPP_CLOSE_PAREN && nesting_depth-- == 0)
2327 /* Consume tokens until the next token is a `)', or a `,'. Returns
2328 TRUE if the next token is a `,'. */
2331 cp_parser_skip_to_closing_parenthesis_or_comma (cp_parser *parser)
2333 unsigned nesting_depth = 0;
2337 cp_token *token = cp_lexer_peek_token (parser->lexer);
2339 /* If we've run out of tokens, then there is no closing `)'. */
2340 if (token->type == CPP_EOF)
2342 /* If it is a `,' stop. */
2343 else if (token->type == CPP_COMMA && nesting_depth-- == 0)
2345 /* If it is a `)', stop. */
2346 else if (token->type == CPP_CLOSE_PAREN && nesting_depth-- == 0)
2348 /* If it is an `(', we have entered another level of nesting. */
2349 else if (token->type == CPP_OPEN_PAREN)
2351 /* Consume the token. */
2352 token = cp_lexer_consume_token (parser->lexer);
2356 /* Consume tokens until we reach the end of the current statement.
2357 Normally, that will be just before consuming a `;'. However, if a
2358 non-nested `}' comes first, then we stop before consuming that. */
2361 cp_parser_skip_to_end_of_statement (parser)
2364 unsigned nesting_depth = 0;
2370 /* Peek at the next token. */
2371 token = cp_lexer_peek_token (parser->lexer);
2372 /* If we've run out of tokens, stop. */
2373 if (token->type == CPP_EOF)
2375 /* If the next token is a `;', we have reached the end of the
2377 if (token->type == CPP_SEMICOLON && !nesting_depth)
2379 /* If the next token is a non-nested `}', then we have reached
2380 the end of the current block. */
2381 if (token->type == CPP_CLOSE_BRACE)
2383 /* If this is a non-nested `}', stop before consuming it.
2384 That way, when confronted with something like:
2388 we stop before consuming the closing `}', even though we
2389 have not yet reached a `;'. */
2390 if (nesting_depth == 0)
2392 /* If it is the closing `}' for a block that we have
2393 scanned, stop -- but only after consuming the token.
2399 we will stop after the body of the erroneously declared
2400 function, but before consuming the following `typedef'
2402 if (--nesting_depth == 0)
2404 cp_lexer_consume_token (parser->lexer);
2408 /* If it the next token is a `{', then we are entering a new
2409 block. Consume the entire block. */
2410 else if (token->type == CPP_OPEN_BRACE)
2412 /* Consume the token. */
2413 cp_lexer_consume_token (parser->lexer);
2417 /* Skip tokens until we have consumed an entire block, or until we
2418 have consumed a non-nested `;'. */
2421 cp_parser_skip_to_end_of_block_or_statement (parser)
2424 unsigned nesting_depth = 0;
2430 /* Peek at the next token. */
2431 token = cp_lexer_peek_token (parser->lexer);
2432 /* If we've run out of tokens, stop. */
2433 if (token->type == CPP_EOF)
2435 /* If the next token is a `;', we have reached the end of the
2437 if (token->type == CPP_SEMICOLON && !nesting_depth)
2439 /* Consume the `;'. */
2440 cp_lexer_consume_token (parser->lexer);
2443 /* Consume the token. */
2444 token = cp_lexer_consume_token (parser->lexer);
2445 /* If the next token is a non-nested `}', then we have reached
2446 the end of the current block. */
2447 if (token->type == CPP_CLOSE_BRACE
2448 && (nesting_depth == 0 || --nesting_depth == 0))
2450 /* If it the next token is a `{', then we are entering a new
2451 block. Consume the entire block. */
2452 if (token->type == CPP_OPEN_BRACE)
2457 /* Skip tokens until a non-nested closing curly brace is the next
2461 cp_parser_skip_to_closing_brace (cp_parser *parser)
2463 unsigned nesting_depth = 0;
2469 /* Peek at the next token. */
2470 token = cp_lexer_peek_token (parser->lexer);
2471 /* If we've run out of tokens, stop. */
2472 if (token->type == CPP_EOF)
2474 /* If the next token is a non-nested `}', then we have reached
2475 the end of the current block. */
2476 if (token->type == CPP_CLOSE_BRACE && nesting_depth-- == 0)
2478 /* If it the next token is a `{', then we are entering a new
2479 block. Consume the entire block. */
2480 else if (token->type == CPP_OPEN_BRACE)
2482 /* Consume the token. */
2483 cp_lexer_consume_token (parser->lexer);
2487 /* Create a new C++ parser. */
2495 /* cp_lexer_new_main is called before calling ggc_alloc because
2496 cp_lexer_new_main might load a PCH file. */
2497 lexer = cp_lexer_new_main ();
2499 parser = (cp_parser *) ggc_alloc_cleared (sizeof (cp_parser));
2500 parser->lexer = lexer;
2501 parser->context = cp_parser_context_new (NULL);
2503 /* For now, we always accept GNU extensions. */
2504 parser->allow_gnu_extensions_p = 1;
2506 /* The `>' token is a greater-than operator, not the end of a
2508 parser->greater_than_is_operator_p = true;
2510 parser->default_arg_ok_p = true;
2512 /* We are not parsing a constant-expression. */
2513 parser->constant_expression_p = false;
2515 /* Local variable names are not forbidden. */
2516 parser->local_variables_forbidden_p = false;
2518 /* We are not procesing an `extern "C"' declaration. */
2519 parser->in_unbraced_linkage_specification_p = false;
2521 /* We are not processing a declarator. */
2522 parser->in_declarator_p = false;
2524 /* The unparsed function queue is empty. */
2525 parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2527 /* There are no classes being defined. */
2528 parser->num_classes_being_defined = 0;
2530 /* No template parameters apply. */
2531 parser->num_template_parameter_lists = 0;
2536 /* Lexical conventions [gram.lex] */
2538 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
2542 cp_parser_identifier (parser)
2547 /* Look for the identifier. */
2548 token = cp_parser_require (parser, CPP_NAME, "identifier");
2549 /* Return the value. */
2550 return token ? token->value : error_mark_node;
2553 /* Basic concepts [gram.basic] */
2555 /* Parse a translation-unit.
2558 declaration-seq [opt]
2560 Returns TRUE if all went well. */
2563 cp_parser_translation_unit (parser)
2568 cp_parser_declaration_seq_opt (parser);
2570 /* If there are no tokens left then all went well. */
2571 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2574 /* Otherwise, issue an error message. */
2575 cp_parser_error (parser, "expected declaration");
2579 /* Consume the EOF token. */
2580 cp_parser_require (parser, CPP_EOF, "end-of-file");
2583 finish_translation_unit ();
2585 /* All went well. */
2589 /* Expressions [gram.expr] */
2591 /* Parse a primary-expression.
2602 ( compound-statement )
2603 __builtin_va_arg ( assignment-expression , type-id )
2608 Returns a representation of the expression.
2610 *IDK indicates what kind of id-expression (if any) was present.
2612 *QUALIFYING_CLASS is set to a non-NULL value if the id-expression can be
2613 used as the operand of a pointer-to-member. In that case,
2614 *QUALIFYING_CLASS gives the class that is used as the qualifying
2615 class in the pointer-to-member. */
2618 cp_parser_primary_expression (cp_parser *parser,
2619 cp_parser_id_kind *idk,
2620 tree *qualifying_class)
2624 /* Assume the primary expression is not an id-expression. */
2625 *idk = CP_PARSER_ID_KIND_NONE;
2626 /* And that it cannot be used as pointer-to-member. */
2627 *qualifying_class = NULL_TREE;
2629 /* Peek at the next token. */
2630 token = cp_lexer_peek_token (parser->lexer);
2631 switch (token->type)
2644 token = cp_lexer_consume_token (parser->lexer);
2645 return token->value;
2647 case CPP_OPEN_PAREN:
2650 bool saved_greater_than_is_operator_p;
2652 /* Consume the `('. */
2653 cp_lexer_consume_token (parser->lexer);
2654 /* Within a parenthesized expression, a `>' token is always
2655 the greater-than operator. */
2656 saved_greater_than_is_operator_p
2657 = parser->greater_than_is_operator_p;
2658 parser->greater_than_is_operator_p = true;
2659 /* If we see `( { ' then we are looking at the beginning of
2660 a GNU statement-expression. */
2661 if (cp_parser_allow_gnu_extensions_p (parser)
2662 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
2664 /* Statement-expressions are not allowed by the standard. */
2666 pedwarn ("ISO C++ forbids braced-groups within expressions");
2668 /* And they're not allowed outside of a function-body; you
2669 cannot, for example, write:
2671 int i = ({ int j = 3; j + 1; });
2673 at class or namespace scope. */
2674 if (!at_function_scope_p ())
2675 error ("statement-expressions are allowed only inside functions");
2676 /* Start the statement-expression. */
2677 expr = begin_stmt_expr ();
2678 /* Parse the compound-statement. */
2679 cp_parser_compound_statement (parser);
2681 expr = finish_stmt_expr (expr);
2685 /* Parse the parenthesized expression. */
2686 expr = cp_parser_expression (parser);
2687 /* Let the front end know that this expression was
2688 enclosed in parentheses. This matters in case, for
2689 example, the expression is of the form `A::B', since
2690 `&A::B' might be a pointer-to-member, but `&(A::B)' is
2692 finish_parenthesized_expr (expr);
2694 /* The `>' token might be the end of a template-id or
2695 template-parameter-list now. */
2696 parser->greater_than_is_operator_p
2697 = saved_greater_than_is_operator_p;
2698 /* Consume the `)'. */
2699 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
2700 cp_parser_skip_to_end_of_statement (parser);
2706 switch (token->keyword)
2708 /* These two are the boolean literals. */
2710 cp_lexer_consume_token (parser->lexer);
2711 return boolean_true_node;
2713 cp_lexer_consume_token (parser->lexer);
2714 return boolean_false_node;
2716 /* The `__null' literal. */
2718 cp_lexer_consume_token (parser->lexer);
2721 /* Recognize the `this' keyword. */
2723 cp_lexer_consume_token (parser->lexer);
2724 if (parser->local_variables_forbidden_p)
2726 error ("`this' may not be used in this context");
2727 return error_mark_node;
2729 return finish_this_expr ();
2731 /* The `operator' keyword can be the beginning of an
2736 case RID_FUNCTION_NAME:
2737 case RID_PRETTY_FUNCTION_NAME:
2738 case RID_C99_FUNCTION_NAME:
2739 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
2740 __func__ are the names of variables -- but they are
2741 treated specially. Therefore, they are handled here,
2742 rather than relying on the generic id-expression logic
2743 below. Gramatically, these names are id-expressions.
2745 Consume the token. */
2746 token = cp_lexer_consume_token (parser->lexer);
2747 /* Look up the name. */
2748 return finish_fname (token->value);
2755 /* The `__builtin_va_arg' construct is used to handle
2756 `va_arg'. Consume the `__builtin_va_arg' token. */
2757 cp_lexer_consume_token (parser->lexer);
2758 /* Look for the opening `('. */
2759 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
2760 /* Now, parse the assignment-expression. */
2761 expression = cp_parser_assignment_expression (parser);
2762 /* Look for the `,'. */
2763 cp_parser_require (parser, CPP_COMMA, "`,'");
2764 /* Parse the type-id. */
2765 type = cp_parser_type_id (parser);
2766 /* Look for the closing `)'. */
2767 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
2769 return build_x_va_arg (expression, type);
2773 cp_parser_error (parser, "expected primary-expression");
2774 return error_mark_node;
2778 /* An id-expression can start with either an identifier, a
2779 `::' as the beginning of a qualified-id, or the "operator"
2783 case CPP_TEMPLATE_ID:
2784 case CPP_NESTED_NAME_SPECIFIER:
2790 /* Parse the id-expression. */
2792 = cp_parser_id_expression (parser,
2793 /*template_keyword_p=*/false,
2794 /*check_dependency_p=*/true,
2795 /*template_p=*/NULL);
2796 if (id_expression == error_mark_node)
2797 return error_mark_node;
2798 /* If we have a template-id, then no further lookup is
2799 required. If the template-id was for a template-class, we
2800 will sometimes have a TYPE_DECL at this point. */
2801 else if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
2802 || TREE_CODE (id_expression) == TYPE_DECL)
2803 decl = id_expression;
2804 /* Look up the name. */
2807 decl = cp_parser_lookup_name_simple (parser, id_expression);
2808 /* If name lookup gives us a SCOPE_REF, then the
2809 qualifying scope was dependent. Just propagate the
2811 if (TREE_CODE (decl) == SCOPE_REF)
2813 if (TYPE_P (TREE_OPERAND (decl, 0)))
2814 *qualifying_class = TREE_OPERAND (decl, 0);
2817 /* Check to see if DECL is a local variable in a context
2818 where that is forbidden. */
2819 if (parser->local_variables_forbidden_p
2820 && local_variable_p (decl))
2822 /* It might be that we only found DECL because we are
2823 trying to be generous with pre-ISO scoping rules.
2824 For example, consider:
2828 for (int i = 0; i < 10; ++i) {}
2829 extern void f(int j = i);
2832 Here, name look up will originally find the out
2833 of scope `i'. We need to issue a warning message,
2834 but then use the global `i'. */
2835 decl = check_for_out_of_scope_variable (decl);
2836 if (local_variable_p (decl))
2838 error ("local variable `%D' may not appear in this context",
2840 return error_mark_node;
2844 /* If unqualified name lookup fails while processing a
2845 template, that just means that we need to do name
2846 lookup again when the template is instantiated. */
2848 && decl == error_mark_node
2849 && processing_template_decl)
2851 *idk = CP_PARSER_ID_KIND_UNQUALIFIED;
2852 return build_min_nt (LOOKUP_EXPR, id_expression);
2854 else if (decl == error_mark_node
2855 && !processing_template_decl)
2859 /* It may be resolvable as a koenig lookup function
2861 *idk = CP_PARSER_ID_KIND_UNQUALIFIED;
2862 return id_expression;
2864 else if (TYPE_P (parser->scope)
2865 && !COMPLETE_TYPE_P (parser->scope))
2866 error ("incomplete type `%T' used in nested name specifier",
2868 else if (parser->scope != global_namespace)
2869 error ("`%D' is not a member of `%D'",
2870 id_expression, parser->scope);
2872 error ("`::%D' has not been declared", id_expression);
2874 /* If DECL is a variable would be out of scope under
2875 ANSI/ISO rules, but in scope in the ARM, name lookup
2876 will succeed. Issue a diagnostic here. */
2878 decl = check_for_out_of_scope_variable (decl);
2880 /* Remember that the name was used in the definition of
2881 the current class so that we can check later to see if
2882 the meaning would have been different after the class
2883 was entirely defined. */
2884 if (!parser->scope && decl != error_mark_node)
2885 maybe_note_name_used_in_class (id_expression, decl);
2888 /* If we didn't find anything, or what we found was a type,
2889 then this wasn't really an id-expression. */
2890 if (TREE_CODE (decl) == TYPE_DECL
2891 || TREE_CODE (decl) == NAMESPACE_DECL
2892 || (TREE_CODE (decl) == TEMPLATE_DECL
2893 && !DECL_FUNCTION_TEMPLATE_P (decl)))
2895 cp_parser_error (parser,
2896 "expected primary-expression");
2897 return error_mark_node;
2900 /* If the name resolved to a template parameter, there is no
2901 need to look it up again later. Similarly, we resolve
2902 enumeration constants to their underlying values. */
2903 if (TREE_CODE (decl) == CONST_DECL)
2905 *idk = CP_PARSER_ID_KIND_NONE;
2906 if (DECL_TEMPLATE_PARM_P (decl) || !processing_template_decl)
2907 return DECL_INITIAL (decl);
2914 /* If the declaration was explicitly qualified indicate
2915 that. The semantics of `A::f(3)' are different than
2916 `f(3)' if `f' is virtual. */
2917 *idk = (parser->scope
2918 ? CP_PARSER_ID_KIND_QUALIFIED
2919 : (TREE_CODE (decl) == TEMPLATE_ID_EXPR
2920 ? CP_PARSER_ID_KIND_TEMPLATE_ID
2921 : CP_PARSER_ID_KIND_UNQUALIFIED));
2926 An id-expression is type-dependent if it contains an
2927 identifier that was declared with a dependent type.
2929 As an optimization, we could choose not to create a
2930 LOOKUP_EXPR for a name that resolved to a local
2931 variable in the template function that we are currently
2932 declaring; such a name cannot ever resolve to anything
2933 else. If we did that we would not have to look up
2934 these names at instantiation time.
2936 The standard is not very specific about an
2937 id-expression that names a set of overloaded functions.
2938 What if some of them have dependent types and some of
2939 them do not? Presumably, such a name should be treated
2940 as a dependent name. */
2941 /* Assume the name is not dependent. */
2942 dependent_p = false;
2943 if (!processing_template_decl)
2944 /* No names are dependent outside a template. */
2946 /* A template-id where the name of the template was not
2947 resolved is definitely dependent. */
2948 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
2949 && (TREE_CODE (TREE_OPERAND (decl, 0))
2950 == IDENTIFIER_NODE))
2952 /* For anything except an overloaded function, just check
2954 else if (!is_overloaded_fn (decl))
2956 = cp_parser_dependent_type_p (TREE_TYPE (decl));
2957 /* For a set of overloaded functions, check each of the
2963 if (BASELINK_P (fns))
2964 fns = BASELINK_FUNCTIONS (fns);
2966 /* For a template-id, check to see if the template
2967 arguments are dependent. */
2968 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
2970 tree args = TREE_OPERAND (fns, 1);
2972 if (args && TREE_CODE (args) == TREE_LIST)
2976 if (cp_parser_dependent_template_arg_p
2977 (TREE_VALUE (args)))
2982 args = TREE_CHAIN (args);
2985 else if (args && TREE_CODE (args) == TREE_VEC)
2988 for (i = 0; i < TREE_VEC_LENGTH (args); ++i)
2989 if (cp_parser_dependent_template_arg_p
2990 (TREE_VEC_ELT (args, i)))
2997 /* The functions are those referred to by the
2999 fns = TREE_OPERAND (fns, 0);
3002 /* If there are no dependent template arguments, go
3003 through the overlaoded functions. */
3004 while (fns && !dependent_p)
3006 tree fn = OVL_CURRENT (fns);
3008 /* Member functions of dependent classes are
3010 if (TREE_CODE (fn) == FUNCTION_DECL
3011 && cp_parser_type_dependent_expression_p (fn))
3013 else if (TREE_CODE (fn) == TEMPLATE_DECL
3014 && cp_parser_dependent_template_p (fn))
3017 fns = OVL_NEXT (fns);
3021 /* If the name was dependent on a template parameter,
3022 we will resolve the name at instantiation time. */
3025 /* Create a SCOPE_REF for qualified names. */
3028 if (TYPE_P (parser->scope))
3029 *qualifying_class = parser->scope;
3030 return build_nt (SCOPE_REF,
3034 /* A TEMPLATE_ID already contains all the information
3036 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR)
3037 return id_expression;
3038 /* Create a LOOKUP_EXPR for other unqualified names. */
3039 return build_min_nt (LOOKUP_EXPR, id_expression);
3044 decl = (adjust_result_of_qualified_name_lookup
3045 (decl, parser->scope, current_class_type));
3046 if (TREE_CODE (decl) == FIELD_DECL || BASELINK_P (decl))
3047 *qualifying_class = parser->scope;
3050 /* Transform references to non-static data members into
3052 decl = hack_identifier (decl, id_expression);
3054 /* Resolve references to variables of anonymous unions
3055 into COMPONENT_REFs. */
3056 if (TREE_CODE (decl) == ALIAS_DECL)
3057 decl = DECL_INITIAL (decl);
3060 if (TREE_DEPRECATED (decl))
3061 warn_deprecated_use (decl);
3066 /* Anything else is an error. */
3068 cp_parser_error (parser, "expected primary-expression");
3069 return error_mark_node;
3073 /* Parse an id-expression.
3080 :: [opt] nested-name-specifier template [opt] unqualified-id
3082 :: operator-function-id
3085 Return a representation of the unqualified portion of the
3086 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
3087 a `::' or nested-name-specifier.
3089 Often, if the id-expression was a qualified-id, the caller will
3090 want to make a SCOPE_REF to represent the qualified-id. This
3091 function does not do this in order to avoid wastefully creating
3092 SCOPE_REFs when they are not required.
3094 If ASSUME_TYPENAME_P is true then we assume that qualified names
3095 are typenames. This flag is set when parsing a declarator-id;
3101 we are supposed to assume that `S<T>::R' is a class.
3103 If TEMPLATE_KEYWORD_P is true, then we have just seen the
3106 If CHECK_DEPENDENCY_P is false, then names are looked up inside
3107 uninstantiated templates.
3109 If *TEMPLATE_KEYWORD_P is non-NULL, it is set to true iff the
3110 `template' keyword is used to explicitly indicate that the entity
3111 named is a template. */
3114 cp_parser_id_expression (cp_parser *parser,
3115 bool template_keyword_p,
3116 bool check_dependency_p,
3119 bool global_scope_p;
3120 bool nested_name_specifier_p;
3122 /* Assume the `template' keyword was not used. */
3124 *template_p = false;
3126 /* Look for the optional `::' operator. */
3128 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
3130 /* Look for the optional nested-name-specifier. */
3131 nested_name_specifier_p
3132 = (cp_parser_nested_name_specifier_opt (parser,
3133 /*typename_keyword_p=*/false,
3137 /* If there is a nested-name-specifier, then we are looking at
3138 the first qualified-id production. */
3139 if (nested_name_specifier_p)
3142 tree saved_object_scope;
3143 tree saved_qualifying_scope;
3144 tree unqualified_id;
3147 /* See if the next token is the `template' keyword. */
3149 template_p = &is_template;
3150 *template_p = cp_parser_optional_template_keyword (parser);
3151 /* Name lookup we do during the processing of the
3152 unqualified-id might obliterate SCOPE. */
3153 saved_scope = parser->scope;
3154 saved_object_scope = parser->object_scope;
3155 saved_qualifying_scope = parser->qualifying_scope;
3156 /* Process the final unqualified-id. */
3157 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
3158 check_dependency_p);
3159 /* Restore the SAVED_SCOPE for our caller. */
3160 parser->scope = saved_scope;
3161 parser->object_scope = saved_object_scope;
3162 parser->qualifying_scope = saved_qualifying_scope;
3164 return unqualified_id;
3166 /* Otherwise, if we are in global scope, then we are looking at one
3167 of the other qualified-id productions. */
3168 else if (global_scope_p)
3173 /* Peek at the next token. */
3174 token = cp_lexer_peek_token (parser->lexer);
3176 /* If it's an identifier, and the next token is not a "<", then
3177 we can avoid the template-id case. This is an optimization
3178 for this common case. */
3179 if (token->type == CPP_NAME
3180 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_LESS)
3181 return cp_parser_identifier (parser);
3183 cp_parser_parse_tentatively (parser);
3184 /* Try a template-id. */
3185 id = cp_parser_template_id (parser,
3186 /*template_keyword_p=*/false,
3187 /*check_dependency_p=*/true);
3188 /* If that worked, we're done. */
3189 if (cp_parser_parse_definitely (parser))
3192 /* Peek at the next token. (Changes in the token buffer may
3193 have invalidated the pointer obtained above.) */
3194 token = cp_lexer_peek_token (parser->lexer);
3196 switch (token->type)
3199 return cp_parser_identifier (parser);
3202 if (token->keyword == RID_OPERATOR)
3203 return cp_parser_operator_function_id (parser);
3207 cp_parser_error (parser, "expected id-expression");
3208 return error_mark_node;
3212 return cp_parser_unqualified_id (parser, template_keyword_p,
3213 /*check_dependency_p=*/true);
3216 /* Parse an unqualified-id.
3220 operator-function-id
3221 conversion-function-id
3225 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3226 keyword, in a construct like `A::template ...'.
3228 Returns a representation of unqualified-id. For the `identifier'
3229 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
3230 production a BIT_NOT_EXPR is returned; the operand of the
3231 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
3232 other productions, see the documentation accompanying the
3233 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
3234 names are looked up in uninstantiated templates. */
3237 cp_parser_unqualified_id (parser, template_keyword_p,
3240 bool template_keyword_p;
3241 bool check_dependency_p;
3245 /* Peek at the next token. */
3246 token = cp_lexer_peek_token (parser->lexer);
3248 switch (token->type)
3254 /* We don't know yet whether or not this will be a
3256 cp_parser_parse_tentatively (parser);
3257 /* Try a template-id. */
3258 id = cp_parser_template_id (parser, template_keyword_p,
3259 check_dependency_p);
3260 /* If it worked, we're done. */
3261 if (cp_parser_parse_definitely (parser))
3263 /* Otherwise, it's an ordinary identifier. */
3264 return cp_parser_identifier (parser);
3267 case CPP_TEMPLATE_ID:
3268 return cp_parser_template_id (parser, template_keyword_p,
3269 check_dependency_p);
3274 tree qualifying_scope;
3278 /* Consume the `~' token. */
3279 cp_lexer_consume_token (parser->lexer);
3280 /* Parse the class-name. The standard, as written, seems to
3283 template <typename T> struct S { ~S (); };
3284 template <typename T> S<T>::~S() {}
3286 is invalid, since `~' must be followed by a class-name, but
3287 `S<T>' is dependent, and so not known to be a class.
3288 That's not right; we need to look in uninstantiated
3289 templates. A further complication arises from:
3291 template <typename T> void f(T t) {
3295 Here, it is not possible to look up `T' in the scope of `T'
3296 itself. We must look in both the current scope, and the
3297 scope of the containing complete expression.
3299 Yet another issue is:
3308 The standard does not seem to say that the `S' in `~S'
3309 should refer to the type `S' and not the data member
3312 /* DR 244 says that we look up the name after the "~" in the
3313 same scope as we looked up the qualifying name. That idea
3314 isn't fully worked out; it's more complicated than that. */
3315 scope = parser->scope;
3316 object_scope = parser->object_scope;
3317 qualifying_scope = parser->qualifying_scope;
3319 /* If the name is of the form "X::~X" it's OK. */
3320 if (scope && TYPE_P (scope)
3321 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
3322 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3324 && (cp_lexer_peek_token (parser->lexer)->value
3325 == TYPE_IDENTIFIER (scope)))
3327 cp_lexer_consume_token (parser->lexer);
3328 return build_nt (BIT_NOT_EXPR, scope);
3331 /* If there was an explicit qualification (S::~T), first look
3332 in the scope given by the qualification (i.e., S). */
3335 cp_parser_parse_tentatively (parser);
3336 type_decl = cp_parser_class_name (parser,
3337 /*typename_keyword_p=*/false,
3338 /*template_keyword_p=*/false,
3340 /*check_access_p=*/true,
3341 /*check_dependency=*/false,
3342 /*class_head_p=*/false);
3343 if (cp_parser_parse_definitely (parser))
3344 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3346 /* In "N::S::~S", look in "N" as well. */
3347 if (scope && qualifying_scope)
3349 cp_parser_parse_tentatively (parser);
3350 parser->scope = qualifying_scope;
3351 parser->object_scope = NULL_TREE;
3352 parser->qualifying_scope = NULL_TREE;
3354 = cp_parser_class_name (parser,
3355 /*typename_keyword_p=*/false,
3356 /*template_keyword_p=*/false,
3358 /*check_access_p=*/true,
3359 /*check_dependency=*/false,
3360 /*class_head_p=*/false);
3361 if (cp_parser_parse_definitely (parser))
3362 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3364 /* In "p->S::~T", look in the scope given by "*p" as well. */
3365 else if (object_scope)
3367 cp_parser_parse_tentatively (parser);
3368 parser->scope = object_scope;
3369 parser->object_scope = NULL_TREE;
3370 parser->qualifying_scope = NULL_TREE;
3372 = cp_parser_class_name (parser,
3373 /*typename_keyword_p=*/false,
3374 /*template_keyword_p=*/false,
3376 /*check_access_p=*/true,
3377 /*check_dependency=*/false,
3378 /*class_head_p=*/false);
3379 if (cp_parser_parse_definitely (parser))
3380 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3382 /* Look in the surrounding context. */
3383 parser->scope = NULL_TREE;
3384 parser->object_scope = NULL_TREE;
3385 parser->qualifying_scope = NULL_TREE;
3387 = cp_parser_class_name (parser,
3388 /*typename_keyword_p=*/false,
3389 /*template_keyword_p=*/false,
3391 /*check_access_p=*/true,
3392 /*check_dependency=*/false,
3393 /*class_head_p=*/false);
3394 /* If an error occurred, assume that the name of the
3395 destructor is the same as the name of the qualifying
3396 class. That allows us to keep parsing after running
3397 into ill-formed destructor names. */
3398 if (type_decl == error_mark_node && scope && TYPE_P (scope))
3399 return build_nt (BIT_NOT_EXPR, scope);
3400 else if (type_decl == error_mark_node)
3401 return error_mark_node;
3403 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3407 if (token->keyword == RID_OPERATOR)
3411 /* This could be a template-id, so we try that first. */
3412 cp_parser_parse_tentatively (parser);
3413 /* Try a template-id. */
3414 id = cp_parser_template_id (parser, template_keyword_p,
3415 /*check_dependency_p=*/true);
3416 /* If that worked, we're done. */
3417 if (cp_parser_parse_definitely (parser))
3419 /* We still don't know whether we're looking at an
3420 operator-function-id or a conversion-function-id. */
3421 cp_parser_parse_tentatively (parser);
3422 /* Try an operator-function-id. */
3423 id = cp_parser_operator_function_id (parser);
3424 /* If that didn't work, try a conversion-function-id. */
3425 if (!cp_parser_parse_definitely (parser))
3426 id = cp_parser_conversion_function_id (parser);
3433 cp_parser_error (parser, "expected unqualified-id");
3434 return error_mark_node;
3438 /* Parse an (optional) nested-name-specifier.
3440 nested-name-specifier:
3441 class-or-namespace-name :: nested-name-specifier [opt]
3442 class-or-namespace-name :: template nested-name-specifier [opt]
3444 PARSER->SCOPE should be set appropriately before this function is
3445 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
3446 effect. TYPE_P is TRUE if we non-type bindings should be ignored
3449 Sets PARSER->SCOPE to the class (TYPE) or namespace
3450 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
3451 it unchanged if there is no nested-name-specifier. Returns the new
3452 scope iff there is a nested-name-specifier, or NULL_TREE otherwise. */
3455 cp_parser_nested_name_specifier_opt (cp_parser *parser,
3456 bool typename_keyword_p,
3457 bool check_dependency_p,
3460 bool success = false;
3461 tree access_check = NULL_TREE;
3464 /* If the next token corresponds to a nested name specifier, there
3465 is no need to reparse it. */
3466 if (cp_lexer_next_token_is (parser->lexer, CPP_NESTED_NAME_SPECIFIER))
3471 /* Get the stored value. */
3472 value = cp_lexer_consume_token (parser->lexer)->value;
3473 /* Perform any access checks that were deferred. */
3474 for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
3475 cp_parser_defer_access_check (parser,
3476 TREE_PURPOSE (check),
3477 TREE_VALUE (check));
3478 /* Set the scope from the stored value. */
3479 parser->scope = TREE_VALUE (value);
3480 parser->qualifying_scope = TREE_TYPE (value);
3481 parser->object_scope = NULL_TREE;
3482 return parser->scope;
3485 /* Remember where the nested-name-specifier starts. */
3486 if (cp_parser_parsing_tentatively (parser)
3487 && !cp_parser_committed_to_tentative_parse (parser))
3489 cp_token *next_token = cp_lexer_peek_token (parser->lexer);
3490 start = cp_lexer_token_difference (parser->lexer,
3491 parser->lexer->first_token,
3493 access_check = parser->context->deferred_access_checks;
3502 tree saved_qualifying_scope;
3504 bool template_keyword_p;
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 token = cp_lexer_peek_token (parser->lexer);
3510 if (success && token->keyword == RID_TEMPLATE)
3512 /* A template-id can start a nested-name-specifier. */
3513 else if (token->type == CPP_TEMPLATE_ID)
3517 /* If the next token is not an identifier, then it is
3518 definitely not a class-or-namespace-name. */
3519 if (token->type != CPP_NAME)
3521 /* If the following token is neither a `<' (to begin a
3522 template-id), nor a `::', then we are not looking at a
3523 nested-name-specifier. */
3524 token = cp_lexer_peek_nth_token (parser->lexer, 2);
3525 if (token->type != CPP_LESS && token->type != CPP_SCOPE)
3529 /* The nested-name-specifier is optional, so we parse
3531 cp_parser_parse_tentatively (parser);
3533 /* Look for the optional `template' keyword, if this isn't the
3534 first time through the loop. */
3536 template_keyword_p = cp_parser_optional_template_keyword (parser);
3538 template_keyword_p = false;
3540 /* Save the old scope since the name lookup we are about to do
3541 might destroy it. */
3542 old_scope = parser->scope;
3543 saved_qualifying_scope = parser->qualifying_scope;
3544 /* Parse the qualifying entity. */
3546 = cp_parser_class_or_namespace_name (parser,
3551 /* Look for the `::' token. */
3552 cp_parser_require (parser, CPP_SCOPE, "`::'");
3554 /* If we found what we wanted, we keep going; otherwise, we're
3556 if (!cp_parser_parse_definitely (parser))
3558 bool error_p = false;
3560 /* Restore the OLD_SCOPE since it was valid before the
3561 failed attempt at finding the last
3562 class-or-namespace-name. */
3563 parser->scope = old_scope;
3564 parser->qualifying_scope = saved_qualifying_scope;
3565 /* If the next token is an identifier, and the one after
3566 that is a `::', then any valid interpretation would have
3567 found a class-or-namespace-name. */
3568 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
3569 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3571 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
3574 token = cp_lexer_consume_token (parser->lexer);
3579 decl = cp_parser_lookup_name_simple (parser, token->value);
3580 if (TREE_CODE (decl) == TEMPLATE_DECL)
3581 error ("`%D' used without template parameters",
3583 else if (parser->scope)
3585 if (TYPE_P (parser->scope))
3586 error ("`%T::%D' is not a class-name or "
3588 parser->scope, token->value);
3590 error ("`%D::%D' is not a class-name or "
3592 parser->scope, token->value);
3595 error ("`%D' is not a class-name or namespace-name",
3597 parser->scope = NULL_TREE;
3599 /* Treat this as a successful nested-name-specifier
3604 If the name found is not a class-name (clause
3605 _class_) or namespace-name (_namespace.def_), the
3606 program is ill-formed. */
3609 cp_lexer_consume_token (parser->lexer);
3614 /* We've found one valid nested-name-specifier. */
3616 /* Make sure we look in the right scope the next time through
3618 parser->scope = (TREE_CODE (new_scope) == TYPE_DECL
3619 ? TREE_TYPE (new_scope)
3621 /* If it is a class scope, try to complete it; we are about to
3622 be looking up names inside the class. */
3623 if (TYPE_P (parser->scope))
3624 complete_type (parser->scope);
3627 /* If parsing tentatively, replace the sequence of tokens that makes
3628 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
3629 token. That way, should we re-parse the token stream, we will
3630 not have to repeat the effort required to do the parse, nor will
3631 we issue duplicate error messages. */
3632 if (success && start >= 0)
3637 /* Find the token that corresponds to the start of the
3639 token = cp_lexer_advance_token (parser->lexer,
3640 parser->lexer->first_token,
3643 /* Remember the access checks associated with this
3644 nested-name-specifier. */
3645 c = parser->context->deferred_access_checks;
3646 if (c == access_check)
3647 access_check = NULL_TREE;
3650 while (TREE_CHAIN (c) != access_check)
3652 access_check = parser->context->deferred_access_checks;
3653 parser->context->deferred_access_checks = TREE_CHAIN (c);
3654 TREE_CHAIN (c) = NULL_TREE;
3657 /* Reset the contents of the START token. */
3658 token->type = CPP_NESTED_NAME_SPECIFIER;
3659 token->value = build_tree_list (access_check, parser->scope);
3660 TREE_TYPE (token->value) = parser->qualifying_scope;
3661 token->keyword = RID_MAX;
3662 /* Purge all subsequent tokens. */
3663 cp_lexer_purge_tokens_after (parser->lexer, token);
3666 return success ? parser->scope : NULL_TREE;
3669 /* Parse a nested-name-specifier. See
3670 cp_parser_nested_name_specifier_opt for details. This function
3671 behaves identically, except that it will an issue an error if no
3672 nested-name-specifier is present, and it will return
3673 ERROR_MARK_NODE, rather than NULL_TREE, if no nested-name-specifier
3677 cp_parser_nested_name_specifier (cp_parser *parser,
3678 bool typename_keyword_p,
3679 bool check_dependency_p,
3684 /* Look for the nested-name-specifier. */
3685 scope = cp_parser_nested_name_specifier_opt (parser,
3689 /* If it was not present, issue an error message. */
3692 cp_parser_error (parser, "expected nested-name-specifier");
3693 return error_mark_node;
3699 /* Parse a class-or-namespace-name.
3701 class-or-namespace-name:
3705 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
3706 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
3707 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
3708 TYPE_P is TRUE iff the next name should be taken as a class-name,
3709 even the same name is declared to be another entity in the same
3712 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
3713 specified by the class-or-namespace-name. If neither is found the
3714 ERROR_MARK_NODE is returned. */
3717 cp_parser_class_or_namespace_name (cp_parser *parser,
3718 bool typename_keyword_p,
3719 bool template_keyword_p,
3720 bool check_dependency_p,
3724 tree saved_qualifying_scope;
3725 tree saved_object_scope;
3729 /* If the next token is the `template' keyword, we know that we are
3730 looking at a class-name. */
3731 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
3732 return cp_parser_class_name (parser,
3736 /*check_access_p=*/true,
3738 /*class_head_p=*/false);
3739 /* Before we try to parse the class-name, we must save away the
3740 current PARSER->SCOPE since cp_parser_class_name will destroy
3742 saved_scope = parser->scope;
3743 saved_qualifying_scope = parser->qualifying_scope;
3744 saved_object_scope = parser->object_scope;
3745 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
3746 there is no need to look for a namespace-name. */
3747 only_class_p = saved_scope && TYPE_P (saved_scope);
3749 cp_parser_parse_tentatively (parser);
3750 scope = cp_parser_class_name (parser,
3754 /*check_access_p=*/true,
3756 /*class_head_p=*/false);
3757 /* If that didn't work, try for a namespace-name. */
3758 if (!only_class_p && !cp_parser_parse_definitely (parser))
3760 /* Restore the saved scope. */
3761 parser->scope = saved_scope;
3762 parser->qualifying_scope = saved_qualifying_scope;
3763 parser->object_scope = saved_object_scope;
3764 /* If we are not looking at an identifier followed by the scope
3765 resolution operator, then this is not part of a
3766 nested-name-specifier. (Note that this function is only used
3767 to parse the components of a nested-name-specifier.) */
3768 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
3769 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
3770 return error_mark_node;
3771 scope = cp_parser_namespace_name (parser);
3777 /* Parse a postfix-expression.
3781 postfix-expression [ expression ]
3782 postfix-expression ( expression-list [opt] )
3783 simple-type-specifier ( expression-list [opt] )
3784 typename :: [opt] nested-name-specifier identifier
3785 ( expression-list [opt] )
3786 typename :: [opt] nested-name-specifier template [opt] template-id
3787 ( expression-list [opt] )
3788 postfix-expression . template [opt] id-expression
3789 postfix-expression -> template [opt] id-expression
3790 postfix-expression . pseudo-destructor-name
3791 postfix-expression -> pseudo-destructor-name
3792 postfix-expression ++
3793 postfix-expression --
3794 dynamic_cast < type-id > ( expression )
3795 static_cast < type-id > ( expression )
3796 reinterpret_cast < type-id > ( expression )
3797 const_cast < type-id > ( expression )
3798 typeid ( expression )
3804 ( type-id ) { initializer-list , [opt] }
3806 This extension is a GNU version of the C99 compound-literal
3807 construct. (The C99 grammar uses `type-name' instead of `type-id',
3808 but they are essentially the same concept.)
3810 If ADDRESS_P is true, the postfix expression is the operand of the
3813 Returns a representation of the expression. */
3816 cp_parser_postfix_expression (cp_parser *parser, bool address_p)
3820 cp_parser_id_kind idk = CP_PARSER_ID_KIND_NONE;
3821 tree postfix_expression = NULL_TREE;
3822 /* Non-NULL only if the current postfix-expression can be used to
3823 form a pointer-to-member. In that case, QUALIFYING_CLASS is the
3824 class used to qualify the member. */
3825 tree qualifying_class = NULL_TREE;
3828 /* Peek at the next token. */
3829 token = cp_lexer_peek_token (parser->lexer);
3830 /* Some of the productions are determined by keywords. */
3831 keyword = token->keyword;
3841 const char *saved_message;
3843 /* All of these can be handled in the same way from the point
3844 of view of parsing. Begin by consuming the token
3845 identifying the cast. */
3846 cp_lexer_consume_token (parser->lexer);
3848 /* New types cannot be defined in the cast. */
3849 saved_message = parser->type_definition_forbidden_message;
3850 parser->type_definition_forbidden_message
3851 = "types may not be defined in casts";
3853 /* Look for the opening `<'. */
3854 cp_parser_require (parser, CPP_LESS, "`<'");
3855 /* Parse the type to which we are casting. */
3856 type = cp_parser_type_id (parser);
3857 /* Look for the closing `>'. */
3858 cp_parser_require (parser, CPP_GREATER, "`>'");
3859 /* Restore the old message. */
3860 parser->type_definition_forbidden_message = saved_message;
3862 /* And the expression which is being cast. */
3863 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3864 expression = cp_parser_expression (parser);
3865 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3871 = build_dynamic_cast (type, expression);
3875 = build_static_cast (type, expression);
3879 = build_reinterpret_cast (type, expression);
3883 = build_const_cast (type, expression);
3894 const char *saved_message;
3896 /* Consume the `typeid' token. */
3897 cp_lexer_consume_token (parser->lexer);
3898 /* Look for the `(' token. */
3899 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3900 /* Types cannot be defined in a `typeid' expression. */
3901 saved_message = parser->type_definition_forbidden_message;
3902 parser->type_definition_forbidden_message
3903 = "types may not be defined in a `typeid\' expression";
3904 /* We can't be sure yet whether we're looking at a type-id or an
3906 cp_parser_parse_tentatively (parser);
3907 /* Try a type-id first. */
3908 type = cp_parser_type_id (parser);
3909 /* Look for the `)' token. Otherwise, we can't be sure that
3910 we're not looking at an expression: consider `typeid (int
3911 (3))', for example. */
3912 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3913 /* If all went well, simply lookup the type-id. */
3914 if (cp_parser_parse_definitely (parser))
3915 postfix_expression = get_typeid (type);
3916 /* Otherwise, fall back to the expression variant. */
3921 /* Look for an expression. */
3922 expression = cp_parser_expression (parser);
3923 /* Compute its typeid. */
3924 postfix_expression = build_typeid (expression);
3925 /* Look for the `)' token. */
3926 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3929 /* Restore the saved message. */
3930 parser->type_definition_forbidden_message = saved_message;
3936 bool template_p = false;
3940 /* Consume the `typename' token. */
3941 cp_lexer_consume_token (parser->lexer);
3942 /* Look for the optional `::' operator. */
3943 cp_parser_global_scope_opt (parser,
3944 /*current_scope_valid_p=*/false);
3945 /* Look for the nested-name-specifier. */
3946 cp_parser_nested_name_specifier (parser,
3947 /*typename_keyword_p=*/true,
3948 /*check_dependency_p=*/true,
3950 /* Look for the optional `template' keyword. */
3951 template_p = cp_parser_optional_template_keyword (parser);
3952 /* We don't know whether we're looking at a template-id or an
3954 cp_parser_parse_tentatively (parser);
3955 /* Try a template-id. */
3956 id = cp_parser_template_id (parser, template_p,
3957 /*check_dependency_p=*/true);
3958 /* If that didn't work, try an identifier. */
3959 if (!cp_parser_parse_definitely (parser))
3960 id = cp_parser_identifier (parser);
3961 /* Create a TYPENAME_TYPE to represent the type to which the
3962 functional cast is being performed. */
3963 type = make_typename_type (parser->scope, id,
3966 postfix_expression = cp_parser_functional_cast (parser, type);
3974 /* If the next thing is a simple-type-specifier, we may be
3975 looking at a functional cast. We could also be looking at
3976 an id-expression. So, we try the functional cast, and if
3977 that doesn't work we fall back to the primary-expression. */
3978 cp_parser_parse_tentatively (parser);
3979 /* Look for the simple-type-specifier. */
3980 type = cp_parser_simple_type_specifier (parser,
3981 CP_PARSER_FLAGS_NONE);
3982 /* Parse the cast itself. */
3983 if (!cp_parser_error_occurred (parser))
3985 = cp_parser_functional_cast (parser, type);
3986 /* If that worked, we're done. */
3987 if (cp_parser_parse_definitely (parser))
3990 /* If the functional-cast didn't work out, try a
3991 compound-literal. */
3992 if (cp_parser_allow_gnu_extensions_p (parser))
3994 tree initializer_list = NULL_TREE;
3996 cp_parser_parse_tentatively (parser);
3997 /* Look for the `('. */
3998 if (cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
4000 type = cp_parser_type_id (parser);
4001 /* Look for the `)'. */
4002 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4003 /* Look for the `{'. */
4004 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
4005 /* If things aren't going well, there's no need to
4007 if (!cp_parser_error_occurred (parser))
4009 /* Parse the initializer-list. */
4011 = cp_parser_initializer_list (parser);
4012 /* Allow a trailing `,'. */
4013 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
4014 cp_lexer_consume_token (parser->lexer);
4015 /* Look for the final `}'. */
4016 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
4019 /* If that worked, we're definitely looking at a
4020 compound-literal expression. */
4021 if (cp_parser_parse_definitely (parser))
4023 /* Warn the user that a compound literal is not
4024 allowed in standard C++. */
4026 pedwarn ("ISO C++ forbids compound-literals");
4027 /* Form the representation of the compound-literal. */
4029 = finish_compound_literal (type, initializer_list);
4034 /* It must be a primary-expression. */
4035 postfix_expression = cp_parser_primary_expression (parser,
4042 /* Peek at the next token. */
4043 token = cp_lexer_peek_token (parser->lexer);
4044 done = (token->type != CPP_OPEN_SQUARE
4045 && token->type != CPP_OPEN_PAREN
4046 && token->type != CPP_DOT
4047 && token->type != CPP_DEREF
4048 && token->type != CPP_PLUS_PLUS
4049 && token->type != CPP_MINUS_MINUS);
4051 /* If the postfix expression is complete, finish up. */
4052 if (address_p && qualifying_class && done)
4054 if (TREE_CODE (postfix_expression) == SCOPE_REF)
4055 postfix_expression = TREE_OPERAND (postfix_expression, 1);
4057 = build_offset_ref (qualifying_class, postfix_expression);
4058 return postfix_expression;
4061 /* Otherwise, if we were avoiding committing until we knew
4062 whether or not we had a pointer-to-member, we now know that
4063 the expression is an ordinary reference to a qualified name. */
4064 if (qualifying_class && !processing_template_decl)
4066 if (TREE_CODE (postfix_expression) == FIELD_DECL)
4068 = finish_non_static_data_member (postfix_expression,
4070 else if (BASELINK_P (postfix_expression))
4075 /* See if any of the functions are non-static members. */
4076 fns = BASELINK_FUNCTIONS (postfix_expression);
4077 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
4078 fns = TREE_OPERAND (fns, 0);
4079 for (fn = fns; fn; fn = OVL_NEXT (fn))
4080 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn))
4082 /* If so, the expression may be relative to the current
4084 if (fn && current_class_type
4085 && DERIVED_FROM_P (qualifying_class, current_class_type))
4087 = (build_class_member_access_expr
4088 (maybe_dummy_object (qualifying_class, NULL),
4090 BASELINK_ACCESS_BINFO (postfix_expression),
4091 /*preserve_reference=*/false));
4093 return build_offset_ref (qualifying_class,
4094 postfix_expression);
4098 /* Remember that there was a reference to this entity. */
4099 if (DECL_P (postfix_expression))
4100 mark_used (postfix_expression);
4102 /* Keep looping until the postfix-expression is complete. */
4105 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE
4106 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
4108 /* It is not a Koenig lookup function call. */
4109 unqualified_name_lookup_error (postfix_expression);
4110 postfix_expression = error_mark_node;
4113 /* Peek at the next token. */
4114 token = cp_lexer_peek_token (parser->lexer);
4116 switch (token->type)
4118 case CPP_OPEN_SQUARE:
4119 /* postfix-expression [ expression ] */
4123 /* Consume the `[' token. */
4124 cp_lexer_consume_token (parser->lexer);
4125 /* Parse the index expression. */
4126 index = cp_parser_expression (parser);
4127 /* Look for the closing `]'. */
4128 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4130 /* Build the ARRAY_REF. */
4132 = grok_array_decl (postfix_expression, index);
4133 idk = CP_PARSER_ID_KIND_NONE;
4137 case CPP_OPEN_PAREN:
4138 /* postfix-expression ( expression-list [opt] ) */
4142 /* Consume the `(' token. */
4143 cp_lexer_consume_token (parser->lexer);
4144 /* If the next token is not a `)', then there are some
4146 if (cp_lexer_next_token_is_not (parser->lexer,
4148 args = cp_parser_expression_list (parser);
4151 /* Look for the closing `)'. */
4152 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4154 if (idk == CP_PARSER_ID_KIND_UNQUALIFIED
4155 && (is_overloaded_fn (postfix_expression)
4156 || DECL_P (postfix_expression)
4157 || TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4161 tree identifier = NULL_TREE;
4162 tree functions = NULL_TREE;
4164 /* Find the name of the overloaded function. */
4165 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4166 identifier = postfix_expression;
4167 else if (is_overloaded_fn (postfix_expression))
4169 functions = postfix_expression;
4170 identifier = DECL_NAME (get_first_fn (functions));
4172 else if (DECL_P (postfix_expression))
4174 functions = postfix_expression;
4175 identifier = DECL_NAME (postfix_expression);
4178 /* A call to a namespace-scope function using an
4181 Do Koenig lookup -- unless any of the arguments are
4183 for (arg = args; arg; arg = TREE_CHAIN (arg))
4184 if (cp_parser_type_dependent_expression_p (TREE_VALUE (arg)))
4189 = lookup_arg_dependent(identifier, functions, args);
4190 if (!postfix_expression)
4192 /* The unqualified name could not be resolved. */
4193 unqualified_name_lookup_error (identifier);
4194 postfix_expression = error_mark_node;
4197 = build_call_from_tree (postfix_expression, args,
4198 /*diallow_virtual=*/false);
4201 postfix_expression = build_min_nt (LOOKUP_EXPR,
4204 else if (idk == CP_PARSER_ID_KIND_UNQUALIFIED
4205 && TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4207 /* The unqualified name could not be resolved. */
4208 unqualified_name_lookup_error (postfix_expression);
4209 postfix_expression = error_mark_node;
4213 /* In the body of a template, no further processing is
4215 if (processing_template_decl)
4217 postfix_expression = build_nt (CALL_EXPR,
4223 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
4225 = (build_new_method_call
4226 (TREE_OPERAND (postfix_expression, 0),
4227 TREE_OPERAND (postfix_expression, 1),
4229 (idk == CP_PARSER_ID_KIND_QUALIFIED
4230 ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL)));
4231 else if (TREE_CODE (postfix_expression) == OFFSET_REF)
4232 postfix_expression = (build_offset_ref_call_from_tree
4233 (postfix_expression, args));
4234 else if (idk == CP_PARSER_ID_KIND_QUALIFIED)
4236 /* A call to a static class member, or a
4237 namespace-scope function. */
4239 = finish_call_expr (postfix_expression, args,
4240 /*disallow_virtual=*/true);
4244 /* All other function calls. */
4246 = finish_call_expr (postfix_expression, args,
4247 /*disallow_virtual=*/false);
4250 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
4251 idk = CP_PARSER_ID_KIND_NONE;
4257 /* postfix-expression . template [opt] id-expression
4258 postfix-expression . pseudo-destructor-name
4259 postfix-expression -> template [opt] id-expression
4260 postfix-expression -> pseudo-destructor-name */
4265 tree scope = NULL_TREE;
4267 /* If this is a `->' operator, dereference the pointer. */
4268 if (token->type == CPP_DEREF)
4269 postfix_expression = build_x_arrow (postfix_expression);
4270 /* Check to see whether or not the expression is
4272 dependent_p = (cp_parser_type_dependent_expression_p
4273 (postfix_expression));
4274 /* The identifier following the `->' or `.' is not
4276 parser->scope = NULL_TREE;
4277 parser->qualifying_scope = NULL_TREE;
4278 parser->object_scope = NULL_TREE;
4279 /* Enter the scope corresponding to the type of the object
4280 given by the POSTFIX_EXPRESSION. */
4282 && TREE_TYPE (postfix_expression) != NULL_TREE)
4284 scope = TREE_TYPE (postfix_expression);
4285 /* According to the standard, no expression should
4286 ever have reference type. Unfortunately, we do not
4287 currently match the standard in this respect in
4288 that our internal representation of an expression
4289 may have reference type even when the standard says
4290 it does not. Therefore, we have to manually obtain
4291 the underlying type here. */
4292 if (TREE_CODE (scope) == REFERENCE_TYPE)
4293 scope = TREE_TYPE (scope);
4294 /* If the SCOPE is an OFFSET_TYPE, then we grab the
4295 type of the field. We get an OFFSET_TYPE for
4300 Probably, we should not get an OFFSET_TYPE here;
4301 that transformation should be made only if `&S::T'
4303 if (TREE_CODE (scope) == OFFSET_TYPE)
4304 scope = TREE_TYPE (scope);
4305 /* The type of the POSTFIX_EXPRESSION must be
4307 scope = complete_type_or_else (scope, NULL_TREE);
4308 /* Let the name lookup machinery know that we are
4309 processing a class member access expression. */
4310 parser->context->object_type = scope;
4311 /* If something went wrong, we want to be able to
4312 discern that case, as opposed to the case where
4313 there was no SCOPE due to the type of expression
4316 scope = error_mark_node;
4319 /* Consume the `.' or `->' operator. */
4320 cp_lexer_consume_token (parser->lexer);
4321 /* If the SCOPE is not a scalar type, we are looking at an
4322 ordinary class member access expression, rather than a
4323 pseudo-destructor-name. */
4324 if (!scope || !SCALAR_TYPE_P (scope))
4326 template_p = cp_parser_optional_template_keyword (parser);
4327 /* Parse the id-expression. */
4328 name = cp_parser_id_expression (parser,
4330 /*check_dependency_p=*/true,
4331 /*template_p=*/NULL);
4332 /* In general, build a SCOPE_REF if the member name is
4333 qualified. However, if the name was not dependent
4334 and has already been resolved; there is no need to
4335 build the SCOPE_REF. For example;
4337 struct X { void f(); };
4338 template <typename T> void f(T* t) { t->X::f(); }
4340 Even though "t" is dependent, "X::f" is not and has
4341 except that for a BASELINK there is no need to
4342 include scope information. */
4343 if (name != error_mark_node
4344 && !BASELINK_P (name)
4347 name = build_nt (SCOPE_REF, parser->scope, name);
4348 parser->scope = NULL_TREE;
4349 parser->qualifying_scope = NULL_TREE;
4350 parser->object_scope = NULL_TREE;
4353 = finish_class_member_access_expr (postfix_expression, name);
4355 /* Otherwise, try the pseudo-destructor-name production. */
4361 /* Parse the pseudo-destructor-name. */
4362 cp_parser_pseudo_destructor_name (parser, &s, &type);
4363 /* Form the call. */
4365 = finish_pseudo_destructor_expr (postfix_expression,
4366 s, TREE_TYPE (type));
4369 /* We no longer need to look up names in the scope of the
4370 object on the left-hand side of the `.' or `->'
4372 parser->context->object_type = NULL_TREE;
4373 idk = CP_PARSER_ID_KIND_NONE;
4378 /* postfix-expression ++ */
4379 /* Consume the `++' token. */
4380 cp_lexer_consume_token (parser->lexer);
4381 /* Generate a reprsentation for the complete expression. */
4383 = finish_increment_expr (postfix_expression,
4384 POSTINCREMENT_EXPR);
4385 idk = CP_PARSER_ID_KIND_NONE;
4388 case CPP_MINUS_MINUS:
4389 /* postfix-expression -- */
4390 /* Consume the `--' token. */
4391 cp_lexer_consume_token (parser->lexer);
4392 /* Generate a reprsentation for the complete expression. */
4394 = finish_increment_expr (postfix_expression,
4395 POSTDECREMENT_EXPR);
4396 idk = CP_PARSER_ID_KIND_NONE;
4400 return postfix_expression;
4404 /* We should never get here. */
4406 return error_mark_node;
4409 /* Parse an expression-list.
4412 assignment-expression
4413 expression-list, assignment-expression
4415 Returns a TREE_LIST. The TREE_VALUE of each node is a
4416 representation of an assignment-expression. Note that a TREE_LIST
4417 is returned even if there is only a single expression in the list. */
4420 cp_parser_expression_list (parser)
4423 tree expression_list = NULL_TREE;
4425 /* Consume expressions until there are no more. */
4430 /* Parse the next assignment-expression. */
4431 expr = cp_parser_assignment_expression (parser);
4432 /* Add it to the list. */
4433 expression_list = tree_cons (NULL_TREE, expr, expression_list);
4435 /* If the next token isn't a `,', then we are done. */
4436 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
4438 /* All uses of expression-list in the grammar are followed
4439 by a `)'. Therefore, if the next token is not a `)' an
4440 error will be issued, unless we are parsing tentatively.
4441 Skip ahead to see if there is another `,' before the `)';
4442 if so, we can go there and recover. */
4443 if (cp_parser_parsing_tentatively (parser)
4444 || cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
4445 || !cp_parser_skip_to_closing_parenthesis_or_comma (parser))
4449 /* Otherwise, consume the `,' and keep going. */
4450 cp_lexer_consume_token (parser->lexer);
4453 /* We built up the list in reverse order so we must reverse it now. */
4454 return nreverse (expression_list);
4457 /* Parse a pseudo-destructor-name.
4459 pseudo-destructor-name:
4460 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
4461 :: [opt] nested-name-specifier template template-id :: ~ type-name
4462 :: [opt] nested-name-specifier [opt] ~ type-name
4464 If either of the first two productions is used, sets *SCOPE to the
4465 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
4466 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
4467 or ERROR_MARK_NODE if no type-name is present. */
4470 cp_parser_pseudo_destructor_name (parser, scope, type)
4475 bool nested_name_specifier_p;
4477 /* Look for the optional `::' operator. */
4478 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
4479 /* Look for the optional nested-name-specifier. */
4480 nested_name_specifier_p
4481 = (cp_parser_nested_name_specifier_opt (parser,
4482 /*typename_keyword_p=*/false,
4483 /*check_dependency_p=*/true,
4486 /* Now, if we saw a nested-name-specifier, we might be doing the
4487 second production. */
4488 if (nested_name_specifier_p
4489 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
4491 /* Consume the `template' keyword. */
4492 cp_lexer_consume_token (parser->lexer);
4493 /* Parse the template-id. */
4494 cp_parser_template_id (parser,
4495 /*template_keyword_p=*/true,
4496 /*check_dependency_p=*/false);
4497 /* Look for the `::' token. */
4498 cp_parser_require (parser, CPP_SCOPE, "`::'");
4500 /* If the next token is not a `~', then there might be some
4501 additional qualification. */
4502 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
4504 /* Look for the type-name. */
4505 *scope = TREE_TYPE (cp_parser_type_name (parser));
4506 /* Look for the `::' token. */
4507 cp_parser_require (parser, CPP_SCOPE, "`::'");
4512 /* Look for the `~'. */
4513 cp_parser_require (parser, CPP_COMPL, "`~'");
4514 /* Look for the type-name again. We are not responsible for
4515 checking that it matches the first type-name. */
4516 *type = cp_parser_type_name (parser);
4519 /* Parse a unary-expression.
4525 unary-operator cast-expression
4526 sizeof unary-expression
4534 __extension__ cast-expression
4535 __alignof__ unary-expression
4536 __alignof__ ( type-id )
4537 __real__ cast-expression
4538 __imag__ cast-expression
4541 ADDRESS_P is true iff the unary-expression is appearing as the
4542 operand of the `&' operator.
4544 Returns a representation of the expresion. */
4547 cp_parser_unary_expression (cp_parser *parser, bool address_p)
4550 enum tree_code unary_operator;
4552 /* Peek at the next token. */
4553 token = cp_lexer_peek_token (parser->lexer);
4554 /* Some keywords give away the kind of expression. */
4555 if (token->type == CPP_KEYWORD)
4557 enum rid keyword = token->keyword;
4563 /* Consume the `alignof' token. */
4564 cp_lexer_consume_token (parser->lexer);
4565 /* Parse the operand. */
4566 return finish_alignof (cp_parser_sizeof_operand
4574 /* Consume the `sizeof' token. */
4575 cp_lexer_consume_token (parser->lexer);
4576 /* Parse the operand. */
4577 operand = cp_parser_sizeof_operand (parser, keyword);
4579 /* If the type of the operand cannot be determined build a
4581 if (TYPE_P (operand)
4582 ? cp_parser_dependent_type_p (operand)
4583 : cp_parser_type_dependent_expression_p (operand))
4584 return build_min (SIZEOF_EXPR, size_type_node, operand);
4585 /* Otherwise, compute the constant value. */
4587 return finish_sizeof (operand);
4591 return cp_parser_new_expression (parser);
4594 return cp_parser_delete_expression (parser);
4598 /* The saved value of the PEDANTIC flag. */
4602 /* Save away the PEDANTIC flag. */
4603 cp_parser_extension_opt (parser, &saved_pedantic);
4604 /* Parse the cast-expression. */
4605 expr = cp_parser_cast_expression (parser, /*address_p=*/false);
4606 /* Restore the PEDANTIC flag. */
4607 pedantic = saved_pedantic;
4617 /* Consume the `__real__' or `__imag__' token. */
4618 cp_lexer_consume_token (parser->lexer);
4619 /* Parse the cast-expression. */
4620 expression = cp_parser_cast_expression (parser,
4621 /*address_p=*/false);
4622 /* Create the complete representation. */
4623 return build_x_unary_op ((keyword == RID_REALPART
4624 ? REALPART_EXPR : IMAGPART_EXPR),
4634 /* Look for the `:: new' and `:: delete', which also signal the
4635 beginning of a new-expression, or delete-expression,
4636 respectively. If the next token is `::', then it might be one of
4638 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
4642 /* See if the token after the `::' is one of the keywords in
4643 which we're interested. */
4644 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
4645 /* If it's `new', we have a new-expression. */
4646 if (keyword == RID_NEW)
4647 return cp_parser_new_expression (parser);
4648 /* Similarly, for `delete'. */
4649 else if (keyword == RID_DELETE)
4650 return cp_parser_delete_expression (parser);
4653 /* Look for a unary operator. */
4654 unary_operator = cp_parser_unary_operator (token);
4655 /* The `++' and `--' operators can be handled similarly, even though
4656 they are not technically unary-operators in the grammar. */
4657 if (unary_operator == ERROR_MARK)
4659 if (token->type == CPP_PLUS_PLUS)
4660 unary_operator = PREINCREMENT_EXPR;
4661 else if (token->type == CPP_MINUS_MINUS)
4662 unary_operator = PREDECREMENT_EXPR;
4663 /* Handle the GNU address-of-label extension. */
4664 else if (cp_parser_allow_gnu_extensions_p (parser)
4665 && token->type == CPP_AND_AND)
4669 /* Consume the '&&' token. */
4670 cp_lexer_consume_token (parser->lexer);
4671 /* Look for the identifier. */
4672 identifier = cp_parser_identifier (parser);
4673 /* Create an expression representing the address. */
4674 return finish_label_address_expr (identifier);
4677 if (unary_operator != ERROR_MARK)
4679 tree cast_expression;
4681 /* Consume the operator token. */
4682 token = cp_lexer_consume_token (parser->lexer);
4683 /* Parse the cast-expression. */
4685 = cp_parser_cast_expression (parser, unary_operator == ADDR_EXPR);
4686 /* Now, build an appropriate representation. */
4687 switch (unary_operator)
4690 return build_x_indirect_ref (cast_expression, "unary *");
4693 return build_x_unary_op (ADDR_EXPR, cast_expression);
4697 case TRUTH_NOT_EXPR:
4698 case PREINCREMENT_EXPR:
4699 case PREDECREMENT_EXPR:
4700 return finish_unary_op_expr (unary_operator, cast_expression);
4703 return build_x_unary_op (BIT_NOT_EXPR, cast_expression);
4707 return error_mark_node;
4711 return cp_parser_postfix_expression (parser, address_p);
4714 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
4715 unary-operator, the corresponding tree code is returned. */
4717 static enum tree_code
4718 cp_parser_unary_operator (token)
4721 switch (token->type)
4724 return INDIRECT_REF;
4730 return CONVERT_EXPR;
4736 return TRUTH_NOT_EXPR;
4739 return BIT_NOT_EXPR;
4746 /* Parse a new-expression.
4748 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
4749 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
4751 Returns a representation of the expression. */
4754 cp_parser_new_expression (parser)
4757 bool global_scope_p;
4762 /* Look for the optional `::' operator. */
4764 = (cp_parser_global_scope_opt (parser,
4765 /*current_scope_valid_p=*/false)
4767 /* Look for the `new' operator. */
4768 cp_parser_require_keyword (parser, RID_NEW, "`new'");
4769 /* There's no easy way to tell a new-placement from the
4770 `( type-id )' construct. */
4771 cp_parser_parse_tentatively (parser);
4772 /* Look for a new-placement. */
4773 placement = cp_parser_new_placement (parser);
4774 /* If that didn't work out, there's no new-placement. */
4775 if (!cp_parser_parse_definitely (parser))
4776 placement = NULL_TREE;
4778 /* If the next token is a `(', then we have a parenthesized
4780 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4782 /* Consume the `('. */
4783 cp_lexer_consume_token (parser->lexer);
4784 /* Parse the type-id. */
4785 type = cp_parser_type_id (parser);
4786 /* Look for the closing `)'. */
4787 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4789 /* Otherwise, there must be a new-type-id. */
4791 type = cp_parser_new_type_id (parser);
4793 /* If the next token is a `(', then we have a new-initializer. */
4794 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4795 initializer = cp_parser_new_initializer (parser);
4797 initializer = NULL_TREE;
4799 /* Create a representation of the new-expression. */
4800 return build_new (placement, type, initializer, global_scope_p);
4803 /* Parse a new-placement.
4808 Returns the same representation as for an expression-list. */
4811 cp_parser_new_placement (parser)
4814 tree expression_list;
4816 /* Look for the opening `('. */
4817 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
4818 return error_mark_node;
4819 /* Parse the expression-list. */
4820 expression_list = cp_parser_expression_list (parser);
4821 /* Look for the closing `)'. */
4822 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4824 return expression_list;
4827 /* Parse a new-type-id.
4830 type-specifier-seq new-declarator [opt]
4832 Returns a TREE_LIST whose TREE_PURPOSE is the type-specifier-seq,
4833 and whose TREE_VALUE is the new-declarator. */
4836 cp_parser_new_type_id (parser)
4839 tree type_specifier_seq;
4841 const char *saved_message;
4843 /* The type-specifier sequence must not contain type definitions.
4844 (It cannot contain declarations of new types either, but if they
4845 are not definitions we will catch that because they are not
4847 saved_message = parser->type_definition_forbidden_message;
4848 parser->type_definition_forbidden_message
4849 = "types may not be defined in a new-type-id";
4850 /* Parse the type-specifier-seq. */
4851 type_specifier_seq = cp_parser_type_specifier_seq (parser);
4852 /* Restore the old message. */
4853 parser->type_definition_forbidden_message = saved_message;
4854 /* Parse the new-declarator. */
4855 declarator = cp_parser_new_declarator_opt (parser);
4857 return build_tree_list (type_specifier_seq, declarator);
4860 /* Parse an (optional) new-declarator.
4863 ptr-operator new-declarator [opt]
4864 direct-new-declarator
4866 Returns a representation of the declarator. See
4867 cp_parser_declarator for the representations used. */
4870 cp_parser_new_declarator_opt (parser)
4873 enum tree_code code;
4875 tree cv_qualifier_seq;
4877 /* We don't know if there's a ptr-operator next, or not. */
4878 cp_parser_parse_tentatively (parser);
4879 /* Look for a ptr-operator. */
4880 code = cp_parser_ptr_operator (parser, &type, &cv_qualifier_seq);
4881 /* If that worked, look for more new-declarators. */
4882 if (cp_parser_parse_definitely (parser))
4886 /* Parse another optional declarator. */
4887 declarator = cp_parser_new_declarator_opt (parser);
4889 /* Create the representation of the declarator. */
4890 if (code == INDIRECT_REF)
4891 declarator = make_pointer_declarator (cv_qualifier_seq,
4894 declarator = make_reference_declarator (cv_qualifier_seq,
4897 /* Handle the pointer-to-member case. */
4899 declarator = build_nt (SCOPE_REF, type, declarator);
4904 /* If the next token is a `[', there is a direct-new-declarator. */
4905 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
4906 return cp_parser_direct_new_declarator (parser);
4911 /* Parse a direct-new-declarator.
4913 direct-new-declarator:
4915 direct-new-declarator [constant-expression]
4917 Returns an ARRAY_REF, following the same conventions as are
4918 documented for cp_parser_direct_declarator. */
4921 cp_parser_direct_new_declarator (parser)
4924 tree declarator = NULL_TREE;
4930 /* Look for the opening `['. */
4931 cp_parser_require (parser, CPP_OPEN_SQUARE, "`['");
4932 /* The first expression is not required to be constant. */
4935 expression = cp_parser_expression (parser);
4936 /* The standard requires that the expression have integral
4937 type. DR 74 adds enumeration types. We believe that the
4938 real intent is that these expressions be handled like the
4939 expression in a `switch' condition, which also allows
4940 classes with a single conversion to integral or
4941 enumeration type. */
4942 if (!processing_template_decl)
4945 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
4950 error ("expression in new-declarator must have integral or enumeration type");
4951 expression = error_mark_node;
4955 /* But all the other expressions must be. */
4957 expression = cp_parser_constant_expression (parser);
4958 /* Look for the closing `]'. */
4959 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4961 /* Add this bound to the declarator. */
4962 declarator = build_nt (ARRAY_REF, declarator, expression);
4964 /* If the next token is not a `[', then there are no more
4966 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
4973 /* Parse a new-initializer.
4976 ( expression-list [opt] )
4978 Returns a reprsentation of the expression-list. If there is no
4979 expression-list, VOID_ZERO_NODE is returned. */
4982 cp_parser_new_initializer (parser)
4985 tree expression_list;
4987 /* Look for the opening parenthesis. */
4988 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
4989 /* If the next token is not a `)', then there is an
4991 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
4992 expression_list = cp_parser_expression_list (parser);
4994 expression_list = void_zero_node;
4995 /* Look for the closing parenthesis. */
4996 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4998 return expression_list;
5001 /* Parse a delete-expression.
5004 :: [opt] delete cast-expression
5005 :: [opt] delete [ ] cast-expression
5007 Returns a representation of the expression. */
5010 cp_parser_delete_expression (parser)
5013 bool global_scope_p;
5017 /* Look for the optional `::' operator. */
5019 = (cp_parser_global_scope_opt (parser,
5020 /*current_scope_valid_p=*/false)
5022 /* Look for the `delete' keyword. */
5023 cp_parser_require_keyword (parser, RID_DELETE, "`delete'");
5024 /* See if the array syntax is in use. */
5025 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5027 /* Consume the `[' token. */
5028 cp_lexer_consume_token (parser->lexer);
5029 /* Look for the `]' token. */
5030 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5031 /* Remember that this is the `[]' construct. */
5037 /* Parse the cast-expression. */
5038 expression = cp_parser_cast_expression (parser, /*address_p=*/false);
5040 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
5043 /* Parse a cast-expression.
5047 ( type-id ) cast-expression
5049 Returns a representation of the expression. */
5052 cp_parser_cast_expression (cp_parser *parser, bool address_p)
5054 /* If it's a `(', then we might be looking at a cast. */
5055 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5057 tree type = NULL_TREE;
5058 tree expr = NULL_TREE;
5059 bool compound_literal_p;
5060 const char *saved_message;
5062 /* There's no way to know yet whether or not this is a cast.
5063 For example, `(int (3))' is a unary-expression, while `(int)
5064 3' is a cast. So, we resort to parsing tentatively. */
5065 cp_parser_parse_tentatively (parser);
5066 /* Types may not be defined in a cast. */
5067 saved_message = parser->type_definition_forbidden_message;
5068 parser->type_definition_forbidden_message
5069 = "types may not be defined in casts";
5070 /* Consume the `('. */
5071 cp_lexer_consume_token (parser->lexer);
5072 /* A very tricky bit is that `(struct S) { 3 }' is a
5073 compound-literal (which we permit in C++ as an extension).
5074 But, that construct is not a cast-expression -- it is a
5075 postfix-expression. (The reason is that `(struct S) { 3 }.i'
5076 is legal; if the compound-literal were a cast-expression,
5077 you'd need an extra set of parentheses.) But, if we parse
5078 the type-id, and it happens to be a class-specifier, then we
5079 will commit to the parse at that point, because we cannot
5080 undo the action that is done when creating a new class. So,
5081 then we cannot back up and do a postfix-expression.
5083 Therefore, we scan ahead to the closing `)', and check to see
5084 if the token after the `)' is a `{'. If so, we are not
5085 looking at a cast-expression.
5087 Save tokens so that we can put them back. */
5088 cp_lexer_save_tokens (parser->lexer);
5089 /* Skip tokens until the next token is a closing parenthesis.
5090 If we find the closing `)', and the next token is a `{', then
5091 we are looking at a compound-literal. */
5093 = (cp_parser_skip_to_closing_parenthesis (parser)
5094 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
5095 /* Roll back the tokens we skipped. */
5096 cp_lexer_rollback_tokens (parser->lexer);
5097 /* If we were looking at a compound-literal, simulate an error
5098 so that the call to cp_parser_parse_definitely below will
5100 if (compound_literal_p)
5101 cp_parser_simulate_error (parser);
5104 /* Look for the type-id. */
5105 type = cp_parser_type_id (parser);
5106 /* Look for the closing `)'. */
5107 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5110 /* Restore the saved message. */
5111 parser->type_definition_forbidden_message = saved_message;
5113 /* If all went well, this is a cast. */
5114 if (cp_parser_parse_definitely (parser))
5116 /* Parse the dependent expression. */
5117 expr = cp_parser_cast_expression (parser, /*address_p=*/false);
5118 /* Warn about old-style casts, if so requested. */
5119 if (warn_old_style_cast
5120 && !in_system_header
5121 && !VOID_TYPE_P (type)
5122 && current_lang_name != lang_name_c)
5123 warning ("use of old-style cast");
5124 /* Perform the cast. */
5125 expr = build_c_cast (type, expr);
5132 /* If we get here, then it's not a cast, so it must be a
5133 unary-expression. */
5134 return cp_parser_unary_expression (parser, address_p);
5137 /* Parse a pm-expression.
5141 pm-expression .* cast-expression
5142 pm-expression ->* cast-expression
5144 Returns a representation of the expression. */
5147 cp_parser_pm_expression (parser)
5153 /* Parse the cast-expresion. */
5154 cast_expr = cp_parser_cast_expression (parser, /*address_p=*/false);
5155 pm_expr = cast_expr;
5156 /* Now look for pointer-to-member operators. */
5160 enum cpp_ttype token_type;
5162 /* Peek at the next token. */
5163 token = cp_lexer_peek_token (parser->lexer);
5164 token_type = token->type;
5165 /* If it's not `.*' or `->*' there's no pointer-to-member
5167 if (token_type != CPP_DOT_STAR
5168 && token_type != CPP_DEREF_STAR)
5171 /* Consume the token. */
5172 cp_lexer_consume_token (parser->lexer);
5174 /* Parse another cast-expression. */
5175 cast_expr = cp_parser_cast_expression (parser, /*address_p=*/false);
5177 /* Build the representation of the pointer-to-member
5179 if (token_type == CPP_DEREF_STAR)
5180 pm_expr = build_x_binary_op (MEMBER_REF, pm_expr, cast_expr);
5182 pm_expr = build_m_component_ref (pm_expr, cast_expr);
5188 /* Parse a multiplicative-expression.
5190 mulitplicative-expression:
5192 multiplicative-expression * pm-expression
5193 multiplicative-expression / pm-expression
5194 multiplicative-expression % pm-expression
5196 Returns a representation of the expression. */
5199 cp_parser_multiplicative_expression (parser)
5202 static const cp_parser_token_tree_map map = {
5203 { CPP_MULT, MULT_EXPR },
5204 { CPP_DIV, TRUNC_DIV_EXPR },
5205 { CPP_MOD, TRUNC_MOD_EXPR },
5206 { CPP_EOF, ERROR_MARK }
5209 return cp_parser_binary_expression (parser,
5211 cp_parser_pm_expression);
5214 /* Parse an additive-expression.
5216 additive-expression:
5217 multiplicative-expression
5218 additive-expression + multiplicative-expression
5219 additive-expression - multiplicative-expression
5221 Returns a representation of the expression. */
5224 cp_parser_additive_expression (parser)
5227 static const cp_parser_token_tree_map map = {
5228 { CPP_PLUS, PLUS_EXPR },
5229 { CPP_MINUS, MINUS_EXPR },
5230 { CPP_EOF, ERROR_MARK }
5233 return cp_parser_binary_expression (parser,
5235 cp_parser_multiplicative_expression);
5238 /* Parse a shift-expression.
5242 shift-expression << additive-expression
5243 shift-expression >> additive-expression
5245 Returns a representation of the expression. */
5248 cp_parser_shift_expression (parser)
5251 static const cp_parser_token_tree_map map = {
5252 { CPP_LSHIFT, LSHIFT_EXPR },
5253 { CPP_RSHIFT, RSHIFT_EXPR },
5254 { CPP_EOF, ERROR_MARK }
5257 return cp_parser_binary_expression (parser,
5259 cp_parser_additive_expression);
5262 /* Parse a relational-expression.
5264 relational-expression:
5266 relational-expression < shift-expression
5267 relational-expression > shift-expression
5268 relational-expression <= shift-expression
5269 relational-expression >= shift-expression
5273 relational-expression:
5274 relational-expression <? shift-expression
5275 relational-expression >? shift-expression
5277 Returns a representation of the expression. */
5280 cp_parser_relational_expression (parser)
5283 static const cp_parser_token_tree_map map = {
5284 { CPP_LESS, LT_EXPR },
5285 { CPP_GREATER, GT_EXPR },
5286 { CPP_LESS_EQ, LE_EXPR },
5287 { CPP_GREATER_EQ, GE_EXPR },
5288 { CPP_MIN, MIN_EXPR },
5289 { CPP_MAX, MAX_EXPR },
5290 { CPP_EOF, ERROR_MARK }
5293 return cp_parser_binary_expression (parser,
5295 cp_parser_shift_expression);
5298 /* Parse an equality-expression.
5300 equality-expression:
5301 relational-expression
5302 equality-expression == relational-expression
5303 equality-expression != relational-expression
5305 Returns a representation of the expression. */
5308 cp_parser_equality_expression (parser)
5311 static const cp_parser_token_tree_map map = {
5312 { CPP_EQ_EQ, EQ_EXPR },
5313 { CPP_NOT_EQ, NE_EXPR },
5314 { CPP_EOF, ERROR_MARK }
5317 return cp_parser_binary_expression (parser,
5319 cp_parser_relational_expression);
5322 /* Parse an and-expression.
5326 and-expression & equality-expression
5328 Returns a representation of the expression. */
5331 cp_parser_and_expression (parser)
5334 static const cp_parser_token_tree_map map = {
5335 { CPP_AND, BIT_AND_EXPR },
5336 { CPP_EOF, ERROR_MARK }
5339 return cp_parser_binary_expression (parser,
5341 cp_parser_equality_expression);
5344 /* Parse an exclusive-or-expression.
5346 exclusive-or-expression:
5348 exclusive-or-expression ^ and-expression
5350 Returns a representation of the expression. */
5353 cp_parser_exclusive_or_expression (parser)
5356 static const cp_parser_token_tree_map map = {
5357 { CPP_XOR, BIT_XOR_EXPR },
5358 { CPP_EOF, ERROR_MARK }
5361 return cp_parser_binary_expression (parser,
5363 cp_parser_and_expression);
5367 /* Parse an inclusive-or-expression.
5369 inclusive-or-expression:
5370 exclusive-or-expression
5371 inclusive-or-expression | exclusive-or-expression
5373 Returns a representation of the expression. */
5376 cp_parser_inclusive_or_expression (parser)
5379 static const cp_parser_token_tree_map map = {
5380 { CPP_OR, BIT_IOR_EXPR },
5381 { CPP_EOF, ERROR_MARK }
5384 return cp_parser_binary_expression (parser,
5386 cp_parser_exclusive_or_expression);
5389 /* Parse a logical-and-expression.
5391 logical-and-expression:
5392 inclusive-or-expression
5393 logical-and-expression && inclusive-or-expression
5395 Returns a representation of the expression. */
5398 cp_parser_logical_and_expression (parser)
5401 static const cp_parser_token_tree_map map = {
5402 { CPP_AND_AND, TRUTH_ANDIF_EXPR },
5403 { CPP_EOF, ERROR_MARK }
5406 return cp_parser_binary_expression (parser,
5408 cp_parser_inclusive_or_expression);
5411 /* Parse a logical-or-expression.
5413 logical-or-expression:
5414 logical-and-expresion
5415 logical-or-expression || logical-and-expression
5417 Returns a representation of the expression. */
5420 cp_parser_logical_or_expression (parser)
5423 static const cp_parser_token_tree_map map = {
5424 { CPP_OR_OR, TRUTH_ORIF_EXPR },
5425 { CPP_EOF, ERROR_MARK }
5428 return cp_parser_binary_expression (parser,
5430 cp_parser_logical_and_expression);
5433 /* Parse a conditional-expression.
5435 conditional-expression:
5436 logical-or-expression
5437 logical-or-expression ? expression : assignment-expression
5441 conditional-expression:
5442 logical-or-expression ? : assignment-expression
5444 Returns a representation of the expression. */
5447 cp_parser_conditional_expression (parser)
5450 tree logical_or_expr;
5452 /* Parse the logical-or-expression. */
5453 logical_or_expr = cp_parser_logical_or_expression (parser);
5454 /* If the next token is a `?', then we have a real conditional
5456 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
5457 return cp_parser_question_colon_clause (parser, logical_or_expr);
5458 /* Otherwise, the value is simply the logical-or-expression. */
5460 return logical_or_expr;
5463 /* Parse the `? expression : assignment-expression' part of a
5464 conditional-expression. The LOGICAL_OR_EXPR is the
5465 logical-or-expression that started the conditional-expression.
5466 Returns a representation of the entire conditional-expression.
5468 This routine exists only so that it can be shared between
5469 cp_parser_conditional_expression and
5470 cp_parser_assignment_expression.
5472 ? expression : assignment-expression
5476 ? : assignment-expression */
5479 cp_parser_question_colon_clause (parser, logical_or_expr)
5481 tree logical_or_expr;
5484 tree assignment_expr;
5486 /* Consume the `?' token. */
5487 cp_lexer_consume_token (parser->lexer);
5488 if (cp_parser_allow_gnu_extensions_p (parser)
5489 && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
5490 /* Implicit true clause. */
5493 /* Parse the expression. */
5494 expr = cp_parser_expression (parser);
5496 /* The next token should be a `:'. */
5497 cp_parser_require (parser, CPP_COLON, "`:'");
5498 /* Parse the assignment-expression. */
5499 assignment_expr = cp_parser_assignment_expression (parser);
5501 /* Build the conditional-expression. */
5502 return build_x_conditional_expr (logical_or_expr,
5507 /* Parse an assignment-expression.
5509 assignment-expression:
5510 conditional-expression
5511 logical-or-expression assignment-operator assignment_expression
5514 Returns a representation for the expression. */
5517 cp_parser_assignment_expression (parser)
5522 /* If the next token is the `throw' keyword, then we're looking at
5523 a throw-expression. */
5524 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
5525 expr = cp_parser_throw_expression (parser);
5526 /* Otherwise, it must be that we are looking at a
5527 logical-or-expression. */
5530 /* Parse the logical-or-expression. */
5531 expr = cp_parser_logical_or_expression (parser);
5532 /* If the next token is a `?' then we're actually looking at a
5533 conditional-expression. */
5534 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
5535 return cp_parser_question_colon_clause (parser, expr);
5538 enum tree_code assignment_operator;
5540 /* If it's an assignment-operator, we're using the second
5543 = cp_parser_assignment_operator_opt (parser);
5544 if (assignment_operator != ERROR_MARK)
5548 /* Parse the right-hand side of the assignment. */
5549 rhs = cp_parser_assignment_expression (parser);
5550 /* Build the asignment expression. */
5551 expr = build_x_modify_expr (expr,
5552 assignment_operator,
5561 /* Parse an (optional) assignment-operator.
5563 assignment-operator: one of
5564 = *= /= %= += -= >>= <<= &= ^= |=
5568 assignment-operator: one of
5571 If the next token is an assignment operator, the corresponding tree
5572 code is returned, and the token is consumed. For example, for
5573 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
5574 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
5575 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
5576 operator, ERROR_MARK is returned. */
5578 static enum tree_code
5579 cp_parser_assignment_operator_opt (parser)
5585 /* Peek at the next toen. */
5586 token = cp_lexer_peek_token (parser->lexer);
5588 switch (token->type)
5599 op = TRUNC_DIV_EXPR;
5603 op = TRUNC_MOD_EXPR;
5643 /* Nothing else is an assignment operator. */
5647 /* If it was an assignment operator, consume it. */
5648 if (op != ERROR_MARK)
5649 cp_lexer_consume_token (parser->lexer);
5654 /* Parse an expression.
5657 assignment-expression
5658 expression , assignment-expression
5660 Returns a representation of the expression. */
5663 cp_parser_expression (parser)
5666 tree expression = NULL_TREE;
5667 bool saw_comma_p = false;
5671 tree assignment_expression;
5673 /* Parse the next assignment-expression. */
5674 assignment_expression
5675 = cp_parser_assignment_expression (parser);
5676 /* If this is the first assignment-expression, we can just
5679 expression = assignment_expression;
5680 /* Otherwise, chain the expressions together. It is unclear why
5681 we do not simply build COMPOUND_EXPRs as we go. */
5683 expression = tree_cons (NULL_TREE,
5684 assignment_expression,
5686 /* If the next token is not a comma, then we are done with the
5688 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5690 /* Consume the `,'. */
5691 cp_lexer_consume_token (parser->lexer);
5692 /* The first time we see a `,', we must take special action
5693 because the representation used for a single expression is
5694 different from that used for a list containing the single
5698 /* Remember that this expression has a `,' in it. */
5700 /* Turn the EXPRESSION into a TREE_LIST so that we can link
5701 additional expressions to it. */
5702 expression = build_tree_list (NULL_TREE, expression);
5706 /* Build a COMPOUND_EXPR to represent the entire expression, if
5707 necessary. We built up the list in reverse order, so we must
5708 straighten it out here. */
5710 expression = build_x_compound_expr (nreverse (expression));
5715 /* Parse a constant-expression.
5717 constant-expression:
5718 conditional-expression */
5721 cp_parser_constant_expression (parser)
5724 bool saved_constant_expression_p;
5727 /* It might seem that we could simply parse the
5728 conditional-expression, and then check to see if it were
5729 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
5730 one that the compiler can figure out is constant, possibly after
5731 doing some simplifications or optimizations. The standard has a
5732 precise definition of constant-expression, and we must honor
5733 that, even though it is somewhat more restrictive.
5739 is not a legal declaration, because `(2, 3)' is not a
5740 constant-expression. The `,' operator is forbidden in a
5741 constant-expression. However, GCC's constant-folding machinery
5742 will fold this operation to an INTEGER_CST for `3'. */
5744 /* Save the old setting of CONSTANT_EXPRESSION_P. */
5745 saved_constant_expression_p = parser->constant_expression_p;
5746 /* We are now parsing a constant-expression. */
5747 parser->constant_expression_p = true;
5748 /* Parse the conditional-expression. */
5749 expression = cp_parser_conditional_expression (parser);
5750 /* Restore the old setting of CONSTANT_EXPRESSION_P. */
5751 parser->constant_expression_p = saved_constant_expression_p;
5756 /* Statements [gram.stmt.stmt] */
5758 /* Parse a statement.
5762 expression-statement
5767 declaration-statement
5771 cp_parser_statement (parser)
5776 int statement_line_number;
5778 /* There is no statement yet. */
5779 statement = NULL_TREE;
5780 /* Peek at the next token. */
5781 token = cp_lexer_peek_token (parser->lexer);
5782 /* Remember the line number of the first token in the statement. */
5783 statement_line_number = token->line_number;
5784 /* If this is a keyword, then that will often determine what kind of
5785 statement we have. */
5786 if (token->type == CPP_KEYWORD)
5788 enum rid keyword = token->keyword;
5794 statement = cp_parser_labeled_statement (parser);
5799 statement = cp_parser_selection_statement (parser);
5805 statement = cp_parser_iteration_statement (parser);
5812 statement = cp_parser_jump_statement (parser);
5816 statement = cp_parser_try_block (parser);
5820 /* It might be a keyword like `int' that can start a
5821 declaration-statement. */
5825 else if (token->type == CPP_NAME)
5827 /* If the next token is a `:', then we are looking at a
5828 labeled-statement. */
5829 token = cp_lexer_peek_nth_token (parser->lexer, 2);
5830 if (token->type == CPP_COLON)
5831 statement = cp_parser_labeled_statement (parser);
5833 /* Anything that starts with a `{' must be a compound-statement. */
5834 else if (token->type == CPP_OPEN_BRACE)
5835 statement = cp_parser_compound_statement (parser);
5837 /* Everything else must be a declaration-statement or an
5838 expression-statement. Try for the declaration-statement
5839 first, unless we are looking at a `;', in which case we know that
5840 we have an expression-statement. */
5843 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
5845 cp_parser_parse_tentatively (parser);
5846 /* Try to parse the declaration-statement. */
5847 cp_parser_declaration_statement (parser);
5848 /* If that worked, we're done. */
5849 if (cp_parser_parse_definitely (parser))
5852 /* Look for an expression-statement instead. */
5853 statement = cp_parser_expression_statement (parser);
5856 /* Set the line number for the statement. */
5857 if (statement && statement_code_p (TREE_CODE (statement)))
5858 STMT_LINENO (statement) = statement_line_number;
5861 /* Parse a labeled-statement.
5864 identifier : statement
5865 case constant-expression : statement
5868 Returns the new CASE_LABEL, for a `case' or `default' label. For
5869 an ordinary label, returns a LABEL_STMT. */
5872 cp_parser_labeled_statement (parser)
5876 tree statement = NULL_TREE;
5878 /* The next token should be an identifier. */
5879 token = cp_lexer_peek_token (parser->lexer);
5880 if (token->type != CPP_NAME
5881 && token->type != CPP_KEYWORD)
5883 cp_parser_error (parser, "expected labeled-statement");
5884 return error_mark_node;
5887 switch (token->keyword)
5893 /* Consume the `case' token. */
5894 cp_lexer_consume_token (parser->lexer);
5895 /* Parse the constant-expression. */
5896 expr = cp_parser_constant_expression (parser);
5897 /* Create the label. */
5898 statement = finish_case_label (expr, NULL_TREE);
5903 /* Consume the `default' token. */
5904 cp_lexer_consume_token (parser->lexer);
5905 /* Create the label. */
5906 statement = finish_case_label (NULL_TREE, NULL_TREE);
5910 /* Anything else must be an ordinary label. */
5911 statement = finish_label_stmt (cp_parser_identifier (parser));
5915 /* Require the `:' token. */
5916 cp_parser_require (parser, CPP_COLON, "`:'");
5917 /* Parse the labeled statement. */
5918 cp_parser_statement (parser);
5920 /* Return the label, in the case of a `case' or `default' label. */
5924 /* Parse an expression-statement.
5926 expression-statement:
5929 Returns the new EXPR_STMT -- or NULL_TREE if the expression
5930 statement consists of nothing more than an `;'. */
5933 cp_parser_expression_statement (parser)
5938 /* If the next token is not a `;', then there is an expression to parse. */
5939 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
5940 statement = finish_expr_stmt (cp_parser_expression (parser));
5941 /* Otherwise, we do not even bother to build an EXPR_STMT. */
5945 statement = NULL_TREE;
5947 /* Consume the final `;'. */
5948 if (!cp_parser_require (parser, CPP_SEMICOLON, "`;'"))
5950 /* If there is additional (erroneous) input, skip to the end of
5952 cp_parser_skip_to_end_of_statement (parser);
5953 /* If the next token is now a `;', consume it. */
5954 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
5955 cp_lexer_consume_token (parser->lexer);
5961 /* Parse a compound-statement.
5964 { statement-seq [opt] }
5966 Returns a COMPOUND_STMT representing the statement. */
5969 cp_parser_compound_statement (cp_parser *parser)
5973 /* Consume the `{'. */
5974 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
5975 return error_mark_node;
5976 /* Begin the compound-statement. */
5977 compound_stmt = begin_compound_stmt (/*has_no_scope=*/0);
5978 /* Parse an (optional) statement-seq. */
5979 cp_parser_statement_seq_opt (parser);
5980 /* Finish the compound-statement. */
5981 finish_compound_stmt (/*has_no_scope=*/0, compound_stmt);
5982 /* Consume the `}'. */
5983 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
5985 return compound_stmt;
5988 /* Parse an (optional) statement-seq.
5992 statement-seq [opt] statement */
5995 cp_parser_statement_seq_opt (parser)
5998 /* Scan statements until there aren't any more. */
6001 /* If we're looking at a `}', then we've run out of statements. */
6002 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE)
6003 || cp_lexer_next_token_is (parser->lexer, CPP_EOF))
6006 /* Parse the statement. */
6007 cp_parser_statement (parser);
6011 /* Parse a selection-statement.
6013 selection-statement:
6014 if ( condition ) statement
6015 if ( condition ) statement else statement
6016 switch ( condition ) statement
6018 Returns the new IF_STMT or SWITCH_STMT. */
6021 cp_parser_selection_statement (parser)
6027 /* Peek at the next token. */
6028 token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
6030 /* See what kind of keyword it is. */
6031 keyword = token->keyword;
6040 /* Look for the `('. */
6041 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
6043 cp_parser_skip_to_end_of_statement (parser);
6044 return error_mark_node;
6047 /* Begin the selection-statement. */
6048 if (keyword == RID_IF)
6049 statement = begin_if_stmt ();
6051 statement = begin_switch_stmt ();
6053 /* Parse the condition. */
6054 condition = cp_parser_condition (parser);
6055 /* Look for the `)'. */
6056 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
6057 cp_parser_skip_to_closing_parenthesis (parser);
6059 if (keyword == RID_IF)
6063 /* Add the condition. */
6064 finish_if_stmt_cond (condition, statement);
6066 /* Parse the then-clause. */
6067 then_stmt = cp_parser_implicitly_scoped_statement (parser);
6068 finish_then_clause (statement);
6070 /* If the next token is `else', parse the else-clause. */
6071 if (cp_lexer_next_token_is_keyword (parser->lexer,
6076 /* Consume the `else' keyword. */
6077 cp_lexer_consume_token (parser->lexer);
6078 /* Parse the else-clause. */
6080 = cp_parser_implicitly_scoped_statement (parser);
6081 finish_else_clause (statement);
6084 /* Now we're all done with the if-statement. */
6091 /* Add the condition. */
6092 finish_switch_cond (condition, statement);
6094 /* Parse the body of the switch-statement. */
6095 body = cp_parser_implicitly_scoped_statement (parser);
6097 /* Now we're all done with the switch-statement. */
6098 finish_switch_stmt (statement);
6106 cp_parser_error (parser, "expected selection-statement");
6107 return error_mark_node;
6111 /* Parse a condition.
6115 type-specifier-seq declarator = assignment-expression
6120 type-specifier-seq declarator asm-specification [opt]
6121 attributes [opt] = assignment-expression
6123 Returns the expression that should be tested. */
6126 cp_parser_condition (parser)
6129 tree type_specifiers;
6130 const char *saved_message;
6132 /* Try the declaration first. */
6133 cp_parser_parse_tentatively (parser);
6134 /* New types are not allowed in the type-specifier-seq for a
6136 saved_message = parser->type_definition_forbidden_message;
6137 parser->type_definition_forbidden_message
6138 = "types may not be defined in conditions";
6139 /* Parse the type-specifier-seq. */
6140 type_specifiers = cp_parser_type_specifier_seq (parser);
6141 /* Restore the saved message. */
6142 parser->type_definition_forbidden_message = saved_message;
6143 /* If all is well, we might be looking at a declaration. */
6144 if (!cp_parser_error_occurred (parser))
6147 tree asm_specification;
6150 tree initializer = NULL_TREE;
6152 /* Parse the declarator. */
6153 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
6154 /*ctor_dtor_or_conv_p=*/NULL);
6155 /* Parse the attributes. */
6156 attributes = cp_parser_attributes_opt (parser);
6157 /* Parse the asm-specification. */
6158 asm_specification = cp_parser_asm_specification_opt (parser);
6159 /* If the next token is not an `=', then we might still be
6160 looking at an expression. For example:
6164 looks like a decl-specifier-seq and a declarator -- but then
6165 there is no `=', so this is an expression. */
6166 cp_parser_require (parser, CPP_EQ, "`='");
6167 /* If we did see an `=', then we are looking at a declaration
6169 if (cp_parser_parse_definitely (parser))
6171 /* Create the declaration. */
6172 decl = start_decl (declarator, type_specifiers,
6173 /*initialized_p=*/true,
6174 attributes, /*prefix_attributes=*/NULL_TREE);
6175 /* Parse the assignment-expression. */
6176 initializer = cp_parser_assignment_expression (parser);
6178 /* Process the initializer. */
6179 cp_finish_decl (decl,
6182 LOOKUP_ONLYCONVERTING);
6184 return convert_from_reference (decl);
6187 /* If we didn't even get past the declarator successfully, we are
6188 definitely not looking at a declaration. */
6190 cp_parser_abort_tentative_parse (parser);
6192 /* Otherwise, we are looking at an expression. */
6193 return cp_parser_expression (parser);
6196 /* Parse an iteration-statement.
6198 iteration-statement:
6199 while ( condition ) statement
6200 do statement while ( expression ) ;
6201 for ( for-init-statement condition [opt] ; expression [opt] )
6204 Returns the new WHILE_STMT, DO_STMT, or FOR_STMT. */
6207 cp_parser_iteration_statement (parser)
6214 /* Peek at the next token. */
6215 token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
6217 return error_mark_node;
6219 /* See what kind of keyword it is. */
6220 keyword = token->keyword;
6227 /* Begin the while-statement. */
6228 statement = begin_while_stmt ();
6229 /* Look for the `('. */
6230 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6231 /* Parse the condition. */
6232 condition = cp_parser_condition (parser);
6233 finish_while_stmt_cond (condition, statement);
6234 /* Look for the `)'. */
6235 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6236 /* Parse the dependent statement. */
6237 cp_parser_already_scoped_statement (parser);
6238 /* We're done with the while-statement. */
6239 finish_while_stmt (statement);
6247 /* Begin the do-statement. */
6248 statement = begin_do_stmt ();
6249 /* Parse the body of the do-statement. */
6250 cp_parser_implicitly_scoped_statement (parser);
6251 finish_do_body (statement);
6252 /* Look for the `while' keyword. */
6253 cp_parser_require_keyword (parser, RID_WHILE, "`while'");
6254 /* Look for the `('. */
6255 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6256 /* Parse the expression. */
6257 expression = cp_parser_expression (parser);
6258 /* We're done with the do-statement. */
6259 finish_do_stmt (expression, statement);
6260 /* Look for the `)'. */
6261 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6262 /* Look for the `;'. */
6263 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6269 tree condition = NULL_TREE;
6270 tree expression = NULL_TREE;
6272 /* Begin the for-statement. */
6273 statement = begin_for_stmt ();
6274 /* Look for the `('. */
6275 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6276 /* Parse the initialization. */
6277 cp_parser_for_init_statement (parser);
6278 finish_for_init_stmt (statement);
6280 /* If there's a condition, process it. */
6281 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6282 condition = cp_parser_condition (parser);
6283 finish_for_cond (condition, statement);
6284 /* Look for the `;'. */
6285 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6287 /* If there's an expression, process it. */
6288 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
6289 expression = cp_parser_expression (parser);
6290 finish_for_expr (expression, statement);
6291 /* Look for the `)'. */
6292 cp_parser_require (parser, CPP_CLOSE_PAREN, "`;'");
6294 /* Parse the body of the for-statement. */
6295 cp_parser_already_scoped_statement (parser);
6297 /* We're done with the for-statement. */
6298 finish_for_stmt (statement);
6303 cp_parser_error (parser, "expected iteration-statement");
6304 statement = error_mark_node;
6311 /* Parse a for-init-statement.
6314 expression-statement
6315 simple-declaration */
6318 cp_parser_for_init_statement (parser)
6321 /* If the next token is a `;', then we have an empty
6322 expression-statement. Gramatically, this is also a
6323 simple-declaration, but an invalid one, because it does not
6324 declare anything. Therefore, if we did not handle this case
6325 specially, we would issue an error message about an invalid
6327 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6329 /* We're going to speculatively look for a declaration, falling back
6330 to an expression, if necessary. */
6331 cp_parser_parse_tentatively (parser);
6332 /* Parse the declaration. */
6333 cp_parser_simple_declaration (parser,
6334 /*function_definition_allowed_p=*/false);
6335 /* If the tentative parse failed, then we shall need to look for an
6336 expression-statement. */
6337 if (cp_parser_parse_definitely (parser))
6341 cp_parser_expression_statement (parser);
6344 /* Parse a jump-statement.
6349 return expression [opt] ;
6357 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_STMT, or
6361 cp_parser_jump_statement (parser)
6364 tree statement = error_mark_node;
6368 /* Peek at the next token. */
6369 token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
6371 return error_mark_node;
6373 /* See what kind of keyword it is. */
6374 keyword = token->keyword;
6378 statement = finish_break_stmt ();
6379 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6383 statement = finish_continue_stmt ();
6384 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6391 /* If the next token is a `;', then there is no
6393 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6394 expr = cp_parser_expression (parser);
6397 /* Build the return-statement. */
6398 statement = finish_return_stmt (expr);
6399 /* Look for the final `;'. */
6400 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6405 /* Create the goto-statement. */
6406 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
6408 /* Issue a warning about this use of a GNU extension. */
6410 pedwarn ("ISO C++ forbids computed gotos");
6411 /* Consume the '*' token. */
6412 cp_lexer_consume_token (parser->lexer);
6413 /* Parse the dependent expression. */
6414 finish_goto_stmt (cp_parser_expression (parser));
6417 finish_goto_stmt (cp_parser_identifier (parser));
6418 /* Look for the final `;'. */
6419 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6423 cp_parser_error (parser, "expected jump-statement");
6430 /* Parse a declaration-statement.
6432 declaration-statement:
6433 block-declaration */
6436 cp_parser_declaration_statement (parser)
6439 /* Parse the block-declaration. */
6440 cp_parser_block_declaration (parser, /*statement_p=*/true);
6442 /* Finish off the statement. */
6446 /* Some dependent statements (like `if (cond) statement'), are
6447 implicitly in their own scope. In other words, if the statement is
6448 a single statement (as opposed to a compound-statement), it is
6449 none-the-less treated as if it were enclosed in braces. Any
6450 declarations appearing in the dependent statement are out of scope
6451 after control passes that point. This function parses a statement,
6452 but ensures that is in its own scope, even if it is not a
6455 Returns the new statement. */
6458 cp_parser_implicitly_scoped_statement (parser)
6463 /* If the token is not a `{', then we must take special action. */
6464 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
6466 /* Create a compound-statement. */
6467 statement = begin_compound_stmt (/*has_no_scope=*/0);
6468 /* Parse the dependent-statement. */
6469 cp_parser_statement (parser);
6470 /* Finish the dummy compound-statement. */
6471 finish_compound_stmt (/*has_no_scope=*/0, statement);
6473 /* Otherwise, we simply parse the statement directly. */
6475 statement = cp_parser_compound_statement (parser);
6477 /* Return the statement. */
6481 /* For some dependent statements (like `while (cond) statement'), we
6482 have already created a scope. Therefore, even if the dependent
6483 statement is a compound-statement, we do not want to create another
6487 cp_parser_already_scoped_statement (parser)
6490 /* If the token is not a `{', then we must take special action. */
6491 if (cp_lexer_next_token_is_not(parser->lexer, CPP_OPEN_BRACE))
6495 /* Create a compound-statement. */
6496 statement = begin_compound_stmt (/*has_no_scope=*/1);
6497 /* Parse the dependent-statement. */
6498 cp_parser_statement (parser);
6499 /* Finish the dummy compound-statement. */
6500 finish_compound_stmt (/*has_no_scope=*/1, statement);
6502 /* Otherwise, we simply parse the statement directly. */
6504 cp_parser_statement (parser);
6507 /* Declarations [gram.dcl.dcl] */
6509 /* Parse an optional declaration-sequence.
6513 declaration-seq declaration */
6516 cp_parser_declaration_seq_opt (parser)
6523 token = cp_lexer_peek_token (parser->lexer);
6525 if (token->type == CPP_CLOSE_BRACE
6526 || token->type == CPP_EOF)
6529 if (token->type == CPP_SEMICOLON)
6531 /* A declaration consisting of a single semicolon is
6532 invalid. Allow it unless we're being pedantic. */
6534 pedwarn ("extra `;'");
6535 cp_lexer_consume_token (parser->lexer);
6539 /* The C lexer modifies PENDING_LANG_CHANGE when it wants the
6540 parser to enter or exit implict `extern "C"' blocks. */
6541 while (pending_lang_change > 0)
6543 push_lang_context (lang_name_c);
6544 --pending_lang_change;
6546 while (pending_lang_change < 0)
6548 pop_lang_context ();
6549 ++pending_lang_change;
6552 /* Parse the declaration itself. */
6553 cp_parser_declaration (parser);
6557 /* Parse a declaration.
6562 template-declaration
6563 explicit-instantiation
6564 explicit-specialization
6565 linkage-specification
6566 namespace-definition
6571 __extension__ declaration */
6574 cp_parser_declaration (parser)
6581 /* Check for the `__extension__' keyword. */
6582 if (cp_parser_extension_opt (parser, &saved_pedantic))
6584 /* Parse the qualified declaration. */
6585 cp_parser_declaration (parser);
6586 /* Restore the PEDANTIC flag. */
6587 pedantic = saved_pedantic;
6592 /* Try to figure out what kind of declaration is present. */
6593 token1 = *cp_lexer_peek_token (parser->lexer);
6594 if (token1.type != CPP_EOF)
6595 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
6597 /* If the next token is `extern' and the following token is a string
6598 literal, then we have a linkage specification. */
6599 if (token1.keyword == RID_EXTERN
6600 && cp_parser_is_string_literal (&token2))
6601 cp_parser_linkage_specification (parser);
6602 /* If the next token is `template', then we have either a template
6603 declaration, an explicit instantiation, or an explicit
6605 else if (token1.keyword == RID_TEMPLATE)
6607 /* `template <>' indicates a template specialization. */
6608 if (token2.type == CPP_LESS
6609 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
6610 cp_parser_explicit_specialization (parser);
6611 /* `template <' indicates a template declaration. */
6612 else if (token2.type == CPP_LESS)
6613 cp_parser_template_declaration (parser, /*member_p=*/false);
6614 /* Anything else must be an explicit instantiation. */
6616 cp_parser_explicit_instantiation (parser);
6618 /* If the next token is `export', then we have a template
6620 else if (token1.keyword == RID_EXPORT)
6621 cp_parser_template_declaration (parser, /*member_p=*/false);
6622 /* If the next token is `extern', 'static' or 'inline' and the one
6623 after that is `template', we have a GNU extended explicit
6624 instantiation directive. */
6625 else if (cp_parser_allow_gnu_extensions_p (parser)
6626 && (token1.keyword == RID_EXTERN
6627 || token1.keyword == RID_STATIC
6628 || token1.keyword == RID_INLINE)
6629 && token2.keyword == RID_TEMPLATE)
6630 cp_parser_explicit_instantiation (parser);
6631 /* If the next token is `namespace', check for a named or unnamed
6632 namespace definition. */
6633 else if (token1.keyword == RID_NAMESPACE
6634 && (/* A named namespace definition. */
6635 (token2.type == CPP_NAME
6636 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
6638 /* An unnamed namespace definition. */
6639 || token2.type == CPP_OPEN_BRACE))
6640 cp_parser_namespace_definition (parser);
6641 /* We must have either a block declaration or a function
6644 /* Try to parse a block-declaration, or a function-definition. */
6645 cp_parser_block_declaration (parser, /*statement_p=*/false);
6648 /* Parse a block-declaration.
6653 namespace-alias-definition
6660 __extension__ block-declaration
6663 If STATEMENT_P is TRUE, then this block-declaration is ocurring as
6664 part of a declaration-statement. */
6667 cp_parser_block_declaration (cp_parser *parser,
6673 /* Check for the `__extension__' keyword. */
6674 if (cp_parser_extension_opt (parser, &saved_pedantic))
6676 /* Parse the qualified declaration. */
6677 cp_parser_block_declaration (parser, statement_p);
6678 /* Restore the PEDANTIC flag. */
6679 pedantic = saved_pedantic;
6684 /* Peek at the next token to figure out which kind of declaration is
6686 token1 = cp_lexer_peek_token (parser->lexer);
6688 /* If the next keyword is `asm', we have an asm-definition. */
6689 if (token1->keyword == RID_ASM)
6692 cp_parser_commit_to_tentative_parse (parser);
6693 cp_parser_asm_definition (parser);
6695 /* If the next keyword is `namespace', we have a
6696 namespace-alias-definition. */
6697 else if (token1->keyword == RID_NAMESPACE)
6698 cp_parser_namespace_alias_definition (parser);
6699 /* If the next keyword is `using', we have either a
6700 using-declaration or a using-directive. */
6701 else if (token1->keyword == RID_USING)
6706 cp_parser_commit_to_tentative_parse (parser);
6707 /* If the token after `using' is `namespace', then we have a
6709 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
6710 if (token2->keyword == RID_NAMESPACE)
6711 cp_parser_using_directive (parser);
6712 /* Otherwise, it's a using-declaration. */
6714 cp_parser_using_declaration (parser);
6716 /* If the next keyword is `__label__' we have a label declaration. */
6717 else if (token1->keyword == RID_LABEL)
6720 cp_parser_commit_to_tentative_parse (parser);
6721 cp_parser_label_declaration (parser);
6723 /* Anything else must be a simple-declaration. */
6725 cp_parser_simple_declaration (parser, !statement_p);
6728 /* Parse a simple-declaration.
6731 decl-specifier-seq [opt] init-declarator-list [opt] ;
6733 init-declarator-list:
6735 init-declarator-list , init-declarator
6737 If FUNCTION_DEFINTION_ALLOWED_P is TRUE, then we also recognize a
6738 function-definition as a simple-declaration. */
6741 cp_parser_simple_declaration (parser, function_definition_allowed_p)
6743 bool function_definition_allowed_p;
6745 tree decl_specifiers;
6748 bool declares_class_or_enum;
6749 bool saw_declarator;
6751 /* Defer access checks until we know what is being declared; the
6752 checks for names appearing in the decl-specifier-seq should be
6753 done as if we were in the scope of the thing being declared. */
6754 cp_parser_start_deferring_access_checks (parser);
6755 /* Parse the decl-specifier-seq. We have to keep track of whether
6756 or not the decl-specifier-seq declares a named class or
6757 enumeration type, since that is the only case in which the
6758 init-declarator-list is allowed to be empty.
6762 In a simple-declaration, the optional init-declarator-list can be
6763 omitted only when declaring a class or enumeration, that is when
6764 the decl-specifier-seq contains either a class-specifier, an
6765 elaborated-type-specifier, or an enum-specifier. */
6767 = cp_parser_decl_specifier_seq (parser,
6768 CP_PARSER_FLAGS_OPTIONAL,
6770 &declares_class_or_enum);
6771 /* We no longer need to defer access checks. */
6772 access_checks = cp_parser_stop_deferring_access_checks (parser);
6774 /* Prevent access checks from being reclaimed by GC. */
6775 parser->access_checks_lists = tree_cons (NULL_TREE, access_checks,
6776 parser->access_checks_lists);
6778 /* Keep going until we hit the `;' at the end of the simple
6780 saw_declarator = false;
6781 while (cp_lexer_next_token_is_not (parser->lexer,
6785 bool function_definition_p;
6787 saw_declarator = true;
6788 /* Parse the init-declarator. */
6789 cp_parser_init_declarator (parser, decl_specifiers, attributes,
6791 function_definition_allowed_p,
6793 &function_definition_p);
6794 /* Handle function definitions specially. */
6795 if (function_definition_p)
6797 /* If the next token is a `,', then we are probably
6798 processing something like:
6802 which is erroneous. */
6803 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
6804 error ("mixing declarations and function-definitions is forbidden");
6805 /* Otherwise, we're done with the list of declarators. */
6808 /* Discard access checks no longer in use. */
6809 parser->access_checks_lists
6810 = TREE_CHAIN (parser->access_checks_lists);
6814 /* The next token should be either a `,' or a `;'. */
6815 token = cp_lexer_peek_token (parser->lexer);
6816 /* If it's a `,', there are more declarators to come. */
6817 if (token->type == CPP_COMMA)
6818 cp_lexer_consume_token (parser->lexer);
6819 /* If it's a `;', we are done. */
6820 else if (token->type == CPP_SEMICOLON)
6822 /* Anything else is an error. */
6825 cp_parser_error (parser, "expected `,' or `;'");
6826 /* Skip tokens until we reach the end of the statement. */
6827 cp_parser_skip_to_end_of_statement (parser);
6828 /* Discard access checks no longer in use. */
6829 parser->access_checks_lists
6830 = TREE_CHAIN (parser->access_checks_lists);
6833 /* After the first time around, a function-definition is not
6834 allowed -- even if it was OK at first. For example:
6839 function_definition_allowed_p = false;
6842 /* Issue an error message if no declarators are present, and the
6843 decl-specifier-seq does not itself declare a class or
6845 if (!saw_declarator)
6847 if (cp_parser_declares_only_class_p (parser))
6848 shadow_tag (decl_specifiers);
6849 /* Perform any deferred access checks. */
6850 cp_parser_perform_deferred_access_checks (access_checks);
6853 /* Consume the `;'. */
6854 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6856 /* Mark all the classes that appeared in the decl-specifier-seq as
6857 having received a `;'. */
6858 note_list_got_semicolon (decl_specifiers);
6860 /* Discard access checks no longer in use. */
6861 parser->access_checks_lists = TREE_CHAIN (parser->access_checks_lists);
6864 /* Parse a decl-specifier-seq.
6867 decl-specifier-seq [opt] decl-specifier
6870 storage-class-specifier
6879 decl-specifier-seq [opt] attributes
6881 Returns a TREE_LIST, giving the decl-specifiers in the order they
6882 appear in the source code. The TREE_VALUE of each node is the
6883 decl-specifier. For a keyword (such as `auto' or `friend'), the
6884 TREE_VALUE is simply the correspoding TREE_IDENTIFIER. For the
6885 representation of a type-specifier, see cp_parser_type_specifier.
6887 If there are attributes, they will be stored in *ATTRIBUTES,
6888 represented as described above cp_parser_attributes.
6890 If FRIEND_IS_NOT_CLASS_P is non-NULL, and the `friend' specifier
6891 appears, and the entity that will be a friend is not going to be a
6892 class, then *FRIEND_IS_NOT_CLASS_P will be set to TRUE. Note that
6893 even if *FRIEND_IS_NOT_CLASS_P is FALSE, the entity to which
6894 friendship is granted might not be a class. */
6897 cp_parser_decl_specifier_seq (parser, flags, attributes,
6898 declares_class_or_enum)
6900 cp_parser_flags flags;
6902 bool *declares_class_or_enum;
6904 tree decl_specs = NULL_TREE;
6905 bool friend_p = false;
6907 /* Assume no class or enumeration type is declared. */
6908 *declares_class_or_enum = false;
6910 /* Assume there are no attributes. */
6911 *attributes = NULL_TREE;
6913 /* Keep reading specifiers until there are no more to read. */
6916 tree decl_spec = NULL_TREE;
6920 /* Peek at the next token. */
6921 token = cp_lexer_peek_token (parser->lexer);
6922 /* Handle attributes. */
6923 if (token->keyword == RID_ATTRIBUTE)
6925 /* Parse the attributes. */
6926 decl_spec = cp_parser_attributes_opt (parser);
6927 /* Add them to the list. */
6928 *attributes = chainon (*attributes, decl_spec);
6931 /* If the next token is an appropriate keyword, we can simply
6932 add it to the list. */
6933 switch (token->keyword)
6939 /* The representation of the specifier is simply the
6940 appropriate TREE_IDENTIFIER node. */
6941 decl_spec = token->value;
6942 /* Consume the token. */
6943 cp_lexer_consume_token (parser->lexer);
6946 /* function-specifier:
6953 decl_spec = cp_parser_function_specifier_opt (parser);
6959 /* The representation of the specifier is simply the
6960 appropriate TREE_IDENTIFIER node. */
6961 decl_spec = token->value;
6962 /* Consume the token. */
6963 cp_lexer_consume_token (parser->lexer);
6966 /* storage-class-specifier:
6981 decl_spec = cp_parser_storage_class_specifier_opt (parser);
6988 /* Constructors are a special case. The `S' in `S()' is not a
6989 decl-specifier; it is the beginning of the declarator. */
6990 constructor_p = (!decl_spec
6991 && cp_parser_constructor_declarator_p (parser,
6994 /* If we don't have a DECL_SPEC yet, then we must be looking at
6995 a type-specifier. */
6996 if (!decl_spec && !constructor_p)
6998 bool decl_spec_declares_class_or_enum;
6999 bool is_cv_qualifier;
7002 = cp_parser_type_specifier (parser, flags,
7004 /*is_declaration=*/true,
7005 &decl_spec_declares_class_or_enum,
7008 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
7010 /* If this type-specifier referenced a user-defined type
7011 (a typedef, class-name, etc.), then we can't allow any
7012 more such type-specifiers henceforth.
7016 The longest sequence of decl-specifiers that could
7017 possibly be a type name is taken as the
7018 decl-specifier-seq of a declaration. The sequence shall
7019 be self-consistent as described below.
7023 As a general rule, at most one type-specifier is allowed
7024 in the complete decl-specifier-seq of a declaration. The
7025 only exceptions are the following:
7027 -- const or volatile can be combined with any other
7030 -- signed or unsigned can be combined with char, long,
7038 void g (const int Pc);
7040 Here, Pc is *not* part of the decl-specifier seq; it's
7041 the declarator. Therefore, once we see a type-specifier
7042 (other than a cv-qualifier), we forbid any additional
7043 user-defined types. We *do* still allow things like `int
7044 int' to be considered a decl-specifier-seq, and issue the
7045 error message later. */
7046 if (decl_spec && !is_cv_qualifier)
7047 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
7050 /* If we still do not have a DECL_SPEC, then there are no more
7054 /* Issue an error message, unless the entire construct was
7056 if (!(flags & CP_PARSER_FLAGS_OPTIONAL))
7058 cp_parser_error (parser, "expected decl specifier");
7059 return error_mark_node;
7065 /* Add the DECL_SPEC to the list of specifiers. */
7066 decl_specs = tree_cons (NULL_TREE, decl_spec, decl_specs);
7068 /* After we see one decl-specifier, further decl-specifiers are
7070 flags |= CP_PARSER_FLAGS_OPTIONAL;
7073 /* We have built up the DECL_SPECS in reverse order. Return them in
7074 the correct order. */
7075 return nreverse (decl_specs);
7078 /* Parse an (optional) storage-class-specifier.
7080 storage-class-specifier:
7089 storage-class-specifier:
7092 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
7095 cp_parser_storage_class_specifier_opt (parser)
7098 switch (cp_lexer_peek_token (parser->lexer)->keyword)
7106 /* Consume the token. */
7107 return cp_lexer_consume_token (parser->lexer)->value;
7114 /* Parse an (optional) function-specifier.
7121 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
7124 cp_parser_function_specifier_opt (parser)
7127 switch (cp_lexer_peek_token (parser->lexer)->keyword)
7132 /* Consume the token. */
7133 return cp_lexer_consume_token (parser->lexer)->value;
7140 /* Parse a linkage-specification.
7142 linkage-specification:
7143 extern string-literal { declaration-seq [opt] }
7144 extern string-literal declaration */
7147 cp_parser_linkage_specification (parser)
7153 /* Look for the `extern' keyword. */
7154 cp_parser_require_keyword (parser, RID_EXTERN, "`extern'");
7156 /* Peek at the next token. */
7157 token = cp_lexer_peek_token (parser->lexer);
7158 /* If it's not a string-literal, then there's a problem. */
7159 if (!cp_parser_is_string_literal (token))
7161 cp_parser_error (parser, "expected language-name");
7164 /* Consume the token. */
7165 cp_lexer_consume_token (parser->lexer);
7167 /* Transform the literal into an identifier. If the literal is a
7168 wide-character string, or contains embedded NULs, then we can't
7169 handle it as the user wants. */
7170 if (token->type == CPP_WSTRING
7171 || (strlen (TREE_STRING_POINTER (token->value))
7172 != (size_t) (TREE_STRING_LENGTH (token->value) - 1)))
7174 cp_parser_error (parser, "invalid linkage-specification");
7175 /* Assume C++ linkage. */
7176 linkage = get_identifier ("c++");
7178 /* If it's a simple string constant, things are easier. */
7180 linkage = get_identifier (TREE_STRING_POINTER (token->value));
7182 /* We're now using the new linkage. */
7183 push_lang_context (linkage);
7185 /* If the next token is a `{', then we're using the first
7187 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7189 /* Consume the `{' token. */
7190 cp_lexer_consume_token (parser->lexer);
7191 /* Parse the declarations. */
7192 cp_parser_declaration_seq_opt (parser);
7193 /* Look for the closing `}'. */
7194 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
7196 /* Otherwise, there's just one declaration. */
7199 bool saved_in_unbraced_linkage_specification_p;
7201 saved_in_unbraced_linkage_specification_p
7202 = parser->in_unbraced_linkage_specification_p;
7203 parser->in_unbraced_linkage_specification_p = true;
7204 have_extern_spec = true;
7205 cp_parser_declaration (parser);
7206 have_extern_spec = false;
7207 parser->in_unbraced_linkage_specification_p
7208 = saved_in_unbraced_linkage_specification_p;
7211 /* We're done with the linkage-specification. */
7212 pop_lang_context ();
7215 /* Special member functions [gram.special] */
7217 /* Parse a conversion-function-id.
7219 conversion-function-id:
7220 operator conversion-type-id
7222 Returns an IDENTIFIER_NODE representing the operator. */
7225 cp_parser_conversion_function_id (parser)
7230 tree saved_qualifying_scope;
7231 tree saved_object_scope;
7233 /* Look for the `operator' token. */
7234 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
7235 return error_mark_node;
7236 /* When we parse the conversion-type-id, the current scope will be
7237 reset. However, we need that information in able to look up the
7238 conversion function later, so we save it here. */
7239 saved_scope = parser->scope;
7240 saved_qualifying_scope = parser->qualifying_scope;
7241 saved_object_scope = parser->object_scope;
7242 /* We must enter the scope of the class so that the names of
7243 entities declared within the class are available in the
7244 conversion-type-id. For example, consider:
7251 S::operator I() { ... }
7253 In order to see that `I' is a type-name in the definition, we
7254 must be in the scope of `S'. */
7256 push_scope (saved_scope);
7257 /* Parse the conversion-type-id. */
7258 type = cp_parser_conversion_type_id (parser);
7259 /* Leave the scope of the class, if any. */
7261 pop_scope (saved_scope);
7262 /* Restore the saved scope. */
7263 parser->scope = saved_scope;
7264 parser->qualifying_scope = saved_qualifying_scope;
7265 parser->object_scope = saved_object_scope;
7266 /* If the TYPE is invalid, indicate failure. */
7267 if (type == error_mark_node)
7268 return error_mark_node;
7269 return mangle_conv_op_name_for_type (type);
7272 /* Parse a conversion-type-id:
7275 type-specifier-seq conversion-declarator [opt]
7277 Returns the TYPE specified. */
7280 cp_parser_conversion_type_id (parser)
7284 tree type_specifiers;
7287 /* Parse the attributes. */
7288 attributes = cp_parser_attributes_opt (parser);
7289 /* Parse the type-specifiers. */
7290 type_specifiers = cp_parser_type_specifier_seq (parser);
7291 /* If that didn't work, stop. */
7292 if (type_specifiers == error_mark_node)
7293 return error_mark_node;
7294 /* Parse the conversion-declarator. */
7295 declarator = cp_parser_conversion_declarator_opt (parser);
7297 return grokdeclarator (declarator, type_specifiers, TYPENAME,
7298 /*initialized=*/0, &attributes);
7301 /* Parse an (optional) conversion-declarator.
7303 conversion-declarator:
7304 ptr-operator conversion-declarator [opt]
7306 Returns a representation of the declarator. See
7307 cp_parser_declarator for details. */
7310 cp_parser_conversion_declarator_opt (parser)
7313 enum tree_code code;
7315 tree cv_qualifier_seq;
7317 /* We don't know if there's a ptr-operator next, or not. */
7318 cp_parser_parse_tentatively (parser);
7319 /* Try the ptr-operator. */
7320 code = cp_parser_ptr_operator (parser, &class_type,
7322 /* If it worked, look for more conversion-declarators. */
7323 if (cp_parser_parse_definitely (parser))
7327 /* Parse another optional declarator. */
7328 declarator = cp_parser_conversion_declarator_opt (parser);
7330 /* Create the representation of the declarator. */
7331 if (code == INDIRECT_REF)
7332 declarator = make_pointer_declarator (cv_qualifier_seq,
7335 declarator = make_reference_declarator (cv_qualifier_seq,
7338 /* Handle the pointer-to-member case. */
7340 declarator = build_nt (SCOPE_REF, class_type, declarator);
7348 /* Parse an (optional) ctor-initializer.
7351 : mem-initializer-list
7353 Returns TRUE iff the ctor-initializer was actually present. */
7356 cp_parser_ctor_initializer_opt (parser)
7359 /* If the next token is not a `:', then there is no
7360 ctor-initializer. */
7361 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
7363 /* Do default initialization of any bases and members. */
7364 if (DECL_CONSTRUCTOR_P (current_function_decl))
7365 finish_mem_initializers (NULL_TREE);
7370 /* Consume the `:' token. */
7371 cp_lexer_consume_token (parser->lexer);
7372 /* And the mem-initializer-list. */
7373 cp_parser_mem_initializer_list (parser);
7378 /* Parse a mem-initializer-list.
7380 mem-initializer-list:
7382 mem-initializer , mem-initializer-list */
7385 cp_parser_mem_initializer_list (parser)
7388 tree mem_initializer_list = NULL_TREE;
7390 /* Let the semantic analysis code know that we are starting the
7391 mem-initializer-list. */
7392 begin_mem_initializers ();
7394 /* Loop through the list. */
7397 tree mem_initializer;
7399 /* Parse the mem-initializer. */
7400 mem_initializer = cp_parser_mem_initializer (parser);
7401 /* Add it to the list, unless it was erroneous. */
7402 if (mem_initializer)
7404 TREE_CHAIN (mem_initializer) = mem_initializer_list;
7405 mem_initializer_list = mem_initializer;
7407 /* If the next token is not a `,', we're done. */
7408 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7410 /* Consume the `,' token. */
7411 cp_lexer_consume_token (parser->lexer);
7414 /* Perform semantic analysis. */
7415 finish_mem_initializers (mem_initializer_list);
7418 /* Parse a mem-initializer.
7421 mem-initializer-id ( expression-list [opt] )
7426 ( expresion-list [opt] )
7428 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
7429 class) or FIELD_DECL (for a non-static data member) to initialize;
7430 the TREE_VALUE is the expression-list. */
7433 cp_parser_mem_initializer (parser)
7436 tree mem_initializer_id;
7437 tree expression_list;
7439 /* Find out what is being initialized. */
7440 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7442 pedwarn ("anachronistic old-style base class initializer");
7443 mem_initializer_id = NULL_TREE;
7446 mem_initializer_id = cp_parser_mem_initializer_id (parser);
7447 /* Look for the opening `('. */
7448 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
7449 /* Parse the expression-list. */
7450 if (cp_lexer_next_token_is_not (parser->lexer,
7452 expression_list = cp_parser_expression_list (parser);
7454 expression_list = void_type_node;
7455 /* Look for the closing `)'. */
7456 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
7458 return expand_member_init (mem_initializer_id,
7462 /* Parse a mem-initializer-id.
7465 :: [opt] nested-name-specifier [opt] class-name
7468 Returns a TYPE indicating the class to be initializer for the first
7469 production. Returns an IDENTIFIER_NODE indicating the data member
7470 to be initialized for the second production. */
7473 cp_parser_mem_initializer_id (parser)
7476 bool global_scope_p;
7477 bool nested_name_specifier_p;
7480 /* Look for the optional `::' operator. */
7482 = (cp_parser_global_scope_opt (parser,
7483 /*current_scope_valid_p=*/false)
7485 /* Look for the optional nested-name-specifier. The simplest way to
7490 The keyword `typename' is not permitted in a base-specifier or
7491 mem-initializer; in these contexts a qualified name that
7492 depends on a template-parameter is implicitly assumed to be a
7495 is to assume that we have seen the `typename' keyword at this
7497 nested_name_specifier_p
7498 = (cp_parser_nested_name_specifier_opt (parser,
7499 /*typename_keyword_p=*/true,
7500 /*check_dependency_p=*/true,
7503 /* If there is a `::' operator or a nested-name-specifier, then we
7504 are definitely looking for a class-name. */
7505 if (global_scope_p || nested_name_specifier_p)
7506 return cp_parser_class_name (parser,
7507 /*typename_keyword_p=*/true,
7508 /*template_keyword_p=*/false,
7510 /*check_access_p=*/true,
7511 /*check_dependency_p=*/true,
7512 /*class_head_p=*/false);
7513 /* Otherwise, we could also be looking for an ordinary identifier. */
7514 cp_parser_parse_tentatively (parser);
7515 /* Try a class-name. */
7516 id = cp_parser_class_name (parser,
7517 /*typename_keyword_p=*/true,
7518 /*template_keyword_p=*/false,
7520 /*check_access_p=*/true,
7521 /*check_dependency_p=*/true,
7522 /*class_head_p=*/false);
7523 /* If we found one, we're done. */
7524 if (cp_parser_parse_definitely (parser))
7526 /* Otherwise, look for an ordinary identifier. */
7527 return cp_parser_identifier (parser);
7530 /* Overloading [gram.over] */
7532 /* Parse an operator-function-id.
7534 operator-function-id:
7537 Returns an IDENTIFIER_NODE for the operator which is a
7538 human-readable spelling of the identifier, e.g., `operator +'. */
7541 cp_parser_operator_function_id (parser)
7544 /* Look for the `operator' keyword. */
7545 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
7546 return error_mark_node;
7547 /* And then the name of the operator itself. */
7548 return cp_parser_operator (parser);
7551 /* Parse an operator.
7554 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
7555 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
7556 || ++ -- , ->* -> () []
7563 Returns an IDENTIFIER_NODE for the operator which is a
7564 human-readable spelling of the identifier, e.g., `operator +'. */
7567 cp_parser_operator (parser)
7570 tree id = NULL_TREE;
7573 /* Peek at the next token. */
7574 token = cp_lexer_peek_token (parser->lexer);
7575 /* Figure out which operator we have. */
7576 switch (token->type)
7582 /* The keyword should be either `new' or `delete'. */
7583 if (token->keyword == RID_NEW)
7585 else if (token->keyword == RID_DELETE)
7590 /* Consume the `new' or `delete' token. */
7591 cp_lexer_consume_token (parser->lexer);
7593 /* Peek at the next token. */
7594 token = cp_lexer_peek_token (parser->lexer);
7595 /* If it's a `[' token then this is the array variant of the
7597 if (token->type == CPP_OPEN_SQUARE)
7599 /* Consume the `[' token. */
7600 cp_lexer_consume_token (parser->lexer);
7601 /* Look for the `]' token. */
7602 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
7603 id = ansi_opname (op == NEW_EXPR
7604 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
7606 /* Otherwise, we have the non-array variant. */
7608 id = ansi_opname (op);
7614 id = ansi_opname (PLUS_EXPR);
7618 id = ansi_opname (MINUS_EXPR);
7622 id = ansi_opname (MULT_EXPR);
7626 id = ansi_opname (TRUNC_DIV_EXPR);
7630 id = ansi_opname (TRUNC_MOD_EXPR);
7634 id = ansi_opname (BIT_XOR_EXPR);
7638 id = ansi_opname (BIT_AND_EXPR);
7642 id = ansi_opname (BIT_IOR_EXPR);
7646 id = ansi_opname (BIT_NOT_EXPR);
7650 id = ansi_opname (TRUTH_NOT_EXPR);
7654 id = ansi_assopname (NOP_EXPR);
7658 id = ansi_opname (LT_EXPR);
7662 id = ansi_opname (GT_EXPR);
7666 id = ansi_assopname (PLUS_EXPR);
7670 id = ansi_assopname (MINUS_EXPR);
7674 id = ansi_assopname (MULT_EXPR);
7678 id = ansi_assopname (TRUNC_DIV_EXPR);
7682 id = ansi_assopname (TRUNC_MOD_EXPR);
7686 id = ansi_assopname (BIT_XOR_EXPR);
7690 id = ansi_assopname (BIT_AND_EXPR);
7694 id = ansi_assopname (BIT_IOR_EXPR);
7698 id = ansi_opname (LSHIFT_EXPR);
7702 id = ansi_opname (RSHIFT_EXPR);
7706 id = ansi_assopname (LSHIFT_EXPR);
7710 id = ansi_assopname (RSHIFT_EXPR);
7714 id = ansi_opname (EQ_EXPR);
7718 id = ansi_opname (NE_EXPR);
7722 id = ansi_opname (LE_EXPR);
7725 case CPP_GREATER_EQ:
7726 id = ansi_opname (GE_EXPR);
7730 id = ansi_opname (TRUTH_ANDIF_EXPR);
7734 id = ansi_opname (TRUTH_ORIF_EXPR);
7738 id = ansi_opname (POSTINCREMENT_EXPR);
7741 case CPP_MINUS_MINUS:
7742 id = ansi_opname (PREDECREMENT_EXPR);
7746 id = ansi_opname (COMPOUND_EXPR);
7749 case CPP_DEREF_STAR:
7750 id = ansi_opname (MEMBER_REF);
7754 id = ansi_opname (COMPONENT_REF);
7757 case CPP_OPEN_PAREN:
7758 /* Consume the `('. */
7759 cp_lexer_consume_token (parser->lexer);
7760 /* Look for the matching `)'. */
7761 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
7762 return ansi_opname (CALL_EXPR);
7764 case CPP_OPEN_SQUARE:
7765 /* Consume the `['. */
7766 cp_lexer_consume_token (parser->lexer);
7767 /* Look for the matching `]'. */
7768 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
7769 return ansi_opname (ARRAY_REF);
7773 id = ansi_opname (MIN_EXPR);
7777 id = ansi_opname (MAX_EXPR);
7781 id = ansi_assopname (MIN_EXPR);
7785 id = ansi_assopname (MAX_EXPR);
7789 /* Anything else is an error. */
7793 /* If we have selected an identifier, we need to consume the
7796 cp_lexer_consume_token (parser->lexer);
7797 /* Otherwise, no valid operator name was present. */
7800 cp_parser_error (parser, "expected operator");
7801 id = error_mark_node;
7807 /* Parse a template-declaration.
7809 template-declaration:
7810 export [opt] template < template-parameter-list > declaration
7812 If MEMBER_P is TRUE, this template-declaration occurs within a
7815 The grammar rule given by the standard isn't correct. What
7818 template-declaration:
7819 export [opt] template-parameter-list-seq
7820 decl-specifier-seq [opt] init-declarator [opt] ;
7821 export [opt] template-parameter-list-seq
7824 template-parameter-list-seq:
7825 template-parameter-list-seq [opt]
7826 template < template-parameter-list > */
7829 cp_parser_template_declaration (parser, member_p)
7833 /* Check for `export'. */
7834 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
7836 /* Consume the `export' token. */
7837 cp_lexer_consume_token (parser->lexer);
7838 /* Warn that we do not support `export'. */
7839 warning ("keyword `export' not implemented, and will be ignored");
7842 cp_parser_template_declaration_after_export (parser, member_p);
7845 /* Parse a template-parameter-list.
7847 template-parameter-list:
7849 template-parameter-list , template-parameter
7851 Returns a TREE_LIST. Each node represents a template parameter.
7852 The nodes are connected via their TREE_CHAINs. */
7855 cp_parser_template_parameter_list (parser)
7858 tree parameter_list = NULL_TREE;
7865 /* Parse the template-parameter. */
7866 parameter = cp_parser_template_parameter (parser);
7867 /* Add it to the list. */
7868 parameter_list = process_template_parm (parameter_list,
7871 /* Peek at the next token. */
7872 token = cp_lexer_peek_token (parser->lexer);
7873 /* If it's not a `,', we're done. */
7874 if (token->type != CPP_COMMA)
7876 /* Otherwise, consume the `,' token. */
7877 cp_lexer_consume_token (parser->lexer);
7880 return parameter_list;
7883 /* Parse a template-parameter.
7887 parameter-declaration
7889 Returns a TREE_LIST. The TREE_VALUE represents the parameter. The
7890 TREE_PURPOSE is the default value, if any. */
7893 cp_parser_template_parameter (parser)
7898 /* Peek at the next token. */
7899 token = cp_lexer_peek_token (parser->lexer);
7900 /* If it is `class' or `template', we have a type-parameter. */
7901 if (token->keyword == RID_TEMPLATE)
7902 return cp_parser_type_parameter (parser);
7903 /* If it is `class' or `typename' we do not know yet whether it is a
7904 type parameter or a non-type parameter. Consider:
7906 template <typename T, typename T::X X> ...
7910 template <class C, class D*> ...
7912 Here, the first parameter is a type parameter, and the second is
7913 a non-type parameter. We can tell by looking at the token after
7914 the identifier -- if it is a `,', `=', or `>' then we have a type
7916 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
7918 /* Peek at the token after `class' or `typename'. */
7919 token = cp_lexer_peek_nth_token (parser->lexer, 2);
7920 /* If it's an identifier, skip it. */
7921 if (token->type == CPP_NAME)
7922 token = cp_lexer_peek_nth_token (parser->lexer, 3);
7923 /* Now, see if the token looks like the end of a template
7925 if (token->type == CPP_COMMA
7926 || token->type == CPP_EQ
7927 || token->type == CPP_GREATER)
7928 return cp_parser_type_parameter (parser);
7931 /* Otherwise, it is a non-type parameter.
7935 When parsing a default template-argument for a non-type
7936 template-parameter, the first non-nested `>' is taken as the end
7937 of the template parameter-list rather than a greater-than
7940 cp_parser_parameter_declaration (parser, /*template_parm_p=*/true);
7943 /* Parse a type-parameter.
7946 class identifier [opt]
7947 class identifier [opt] = type-id
7948 typename identifier [opt]
7949 typename identifier [opt] = type-id
7950 template < template-parameter-list > class identifier [opt]
7951 template < template-parameter-list > class identifier [opt]
7954 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
7955 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
7956 the declaration of the parameter. */
7959 cp_parser_type_parameter (parser)
7965 /* Look for a keyword to tell us what kind of parameter this is. */
7966 token = cp_parser_require (parser, CPP_KEYWORD,
7967 "expected `class', `typename', or `template'");
7969 return error_mark_node;
7971 switch (token->keyword)
7977 tree default_argument;
7979 /* If the next token is an identifier, then it names the
7981 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
7982 identifier = cp_parser_identifier (parser);
7984 identifier = NULL_TREE;
7986 /* Create the parameter. */
7987 parameter = finish_template_type_parm (class_type_node, identifier);
7989 /* If the next token is an `=', we have a default argument. */
7990 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7992 /* Consume the `=' token. */
7993 cp_lexer_consume_token (parser->lexer);
7994 /* Parse the default-argumen. */
7995 default_argument = cp_parser_type_id (parser);
7998 default_argument = NULL_TREE;
8000 /* Create the combined representation of the parameter and the
8001 default argument. */
8002 parameter = build_tree_list (default_argument,
8009 tree parameter_list;
8011 tree default_argument;
8013 /* Look for the `<'. */
8014 cp_parser_require (parser, CPP_LESS, "`<'");
8015 /* Parse the template-parameter-list. */
8016 begin_template_parm_list ();
8018 = cp_parser_template_parameter_list (parser);
8019 parameter_list = end_template_parm_list (parameter_list);
8020 /* Look for the `>'. */
8021 cp_parser_require (parser, CPP_GREATER, "`>'");
8022 /* Look for the `class' keyword. */
8023 cp_parser_require_keyword (parser, RID_CLASS, "`class'");
8024 /* If the next token is an `=', then there is a
8025 default-argument. If the next token is a `>', we are at
8026 the end of the parameter-list. If the next token is a `,',
8027 then we are at the end of this parameter. */
8028 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
8029 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
8030 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8031 identifier = cp_parser_identifier (parser);
8033 identifier = NULL_TREE;
8034 /* Create the template parameter. */
8035 parameter = finish_template_template_parm (class_type_node,
8038 /* If the next token is an `=', then there is a
8039 default-argument. */
8040 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8042 /* Consume the `='. */
8043 cp_lexer_consume_token (parser->lexer);
8044 /* Parse the id-expression. */
8046 = cp_parser_id_expression (parser,
8047 /*template_keyword_p=*/false,
8048 /*check_dependency_p=*/true,
8049 /*template_p=*/NULL);
8050 /* Look up the name. */
8052 = cp_parser_lookup_name_simple (parser, default_argument);
8053 /* See if the default argument is valid. */
8055 = check_template_template_default_arg (default_argument);
8058 default_argument = NULL_TREE;
8060 /* Create the combined representation of the parameter and the
8061 default argument. */
8062 parameter = build_tree_list (default_argument,
8068 /* Anything else is an error. */
8069 cp_parser_error (parser,
8070 "expected `class', `typename', or `template'");
8071 parameter = error_mark_node;
8077 /* Parse a template-id.
8080 template-name < template-argument-list [opt] >
8082 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
8083 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
8084 returned. Otherwise, if the template-name names a function, or set
8085 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
8086 names a class, returns a TYPE_DECL for the specialization.
8088 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
8089 uninstantiated templates. */
8092 cp_parser_template_id (cp_parser *parser,
8093 bool template_keyword_p,
8094 bool check_dependency_p)
8099 tree saved_qualifying_scope;
8100 tree saved_object_scope;
8102 bool saved_greater_than_is_operator_p;
8103 ptrdiff_t start_of_id;
8104 tree access_check = NULL_TREE;
8106 /* If the next token corresponds to a template-id, there is no need
8108 if (cp_lexer_next_token_is (parser->lexer, CPP_TEMPLATE_ID))
8113 /* Get the stored value. */
8114 value = cp_lexer_consume_token (parser->lexer)->value;
8115 /* Perform any access checks that were deferred. */
8116 for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
8117 cp_parser_defer_access_check (parser,
8118 TREE_PURPOSE (check),
8119 TREE_VALUE (check));
8120 /* Return the stored value. */
8121 return TREE_VALUE (value);
8124 /* Remember where the template-id starts. */
8125 if (cp_parser_parsing_tentatively (parser)
8126 && !cp_parser_committed_to_tentative_parse (parser))
8128 cp_token *next_token = cp_lexer_peek_token (parser->lexer);
8129 start_of_id = cp_lexer_token_difference (parser->lexer,
8130 parser->lexer->first_token,
8132 access_check = parser->context->deferred_access_checks;
8137 /* Parse the template-name. */
8138 template = cp_parser_template_name (parser, template_keyword_p,
8139 check_dependency_p);
8140 if (template == error_mark_node)
8141 return error_mark_node;
8143 /* Look for the `<' that starts the template-argument-list. */
8144 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
8145 return error_mark_node;
8149 When parsing a template-id, the first non-nested `>' is taken as
8150 the end of the template-argument-list rather than a greater-than
8152 saved_greater_than_is_operator_p
8153 = parser->greater_than_is_operator_p;
8154 parser->greater_than_is_operator_p = false;
8155 /* Parsing the argument list may modify SCOPE, so we save it
8157 saved_scope = parser->scope;
8158 saved_qualifying_scope = parser->qualifying_scope;
8159 saved_object_scope = parser->object_scope;
8160 /* Parse the template-argument-list itself. */
8161 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
8162 arguments = NULL_TREE;
8164 arguments = cp_parser_template_argument_list (parser);
8165 /* Look for the `>' that ends the template-argument-list. */
8166 cp_parser_require (parser, CPP_GREATER, "`>'");
8167 /* The `>' token might be a greater-than operator again now. */
8168 parser->greater_than_is_operator_p
8169 = saved_greater_than_is_operator_p;
8170 /* Restore the SAVED_SCOPE. */
8171 parser->scope = saved_scope;
8172 parser->qualifying_scope = saved_qualifying_scope;
8173 parser->object_scope = saved_object_scope;
8175 /* Build a representation of the specialization. */
8176 if (TREE_CODE (template) == IDENTIFIER_NODE)
8177 template_id = build_min_nt (TEMPLATE_ID_EXPR, template, arguments);
8178 else if (DECL_CLASS_TEMPLATE_P (template)
8179 || DECL_TEMPLATE_TEMPLATE_PARM_P (template))
8181 = finish_template_type (template, arguments,
8182 cp_lexer_next_token_is (parser->lexer,
8186 /* If it's not a class-template or a template-template, it should be
8187 a function-template. */
8188 my_friendly_assert ((DECL_FUNCTION_TEMPLATE_P (template)
8189 || TREE_CODE (template) == OVERLOAD
8190 || BASELINK_P (template)),
8193 template_id = lookup_template_function (template, arguments);
8196 /* If parsing tentatively, replace the sequence of tokens that makes
8197 up the template-id with a CPP_TEMPLATE_ID token. That way,
8198 should we re-parse the token stream, we will not have to repeat
8199 the effort required to do the parse, nor will we issue duplicate
8200 error messages about problems during instantiation of the
8202 if (start_of_id >= 0)
8207 /* Find the token that corresponds to the start of the
8209 token = cp_lexer_advance_token (parser->lexer,
8210 parser->lexer->first_token,
8213 /* Remember the access checks associated with this
8214 nested-name-specifier. */
8215 c = parser->context->deferred_access_checks;
8216 if (c == access_check)
8217 access_check = NULL_TREE;
8220 while (TREE_CHAIN (c) != access_check)
8222 access_check = parser->context->deferred_access_checks;
8223 parser->context->deferred_access_checks = TREE_CHAIN (c);
8224 TREE_CHAIN (c) = NULL_TREE;
8227 /* Reset the contents of the START_OF_ID token. */
8228 token->type = CPP_TEMPLATE_ID;
8229 token->value = build_tree_list (access_check, template_id);
8230 token->keyword = RID_MAX;
8231 /* Purge all subsequent tokens. */
8232 cp_lexer_purge_tokens_after (parser->lexer, token);
8238 /* Parse a template-name.
8243 The standard should actually say:
8247 operator-function-id
8248 conversion-function-id
8250 A defect report has been filed about this issue.
8252 If TEMPLATE_KEYWORD_P is true, then we have just seen the
8253 `template' keyword, in a construction like:
8257 In that case `f' is taken to be a template-name, even though there
8258 is no way of knowing for sure.
8260 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
8261 name refers to a set of overloaded functions, at least one of which
8262 is a template, or an IDENTIFIER_NODE with the name of the template,
8263 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
8264 names are looked up inside uninstantiated templates. */
8267 cp_parser_template_name (parser, template_keyword_p, check_dependency_p)
8269 bool template_keyword_p;
8270 bool check_dependency_p;
8276 /* If the next token is `operator', then we have either an
8277 operator-function-id or a conversion-function-id. */
8278 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
8280 /* We don't know whether we're looking at an
8281 operator-function-id or a conversion-function-id. */
8282 cp_parser_parse_tentatively (parser);
8283 /* Try an operator-function-id. */
8284 identifier = cp_parser_operator_function_id (parser);
8285 /* If that didn't work, try a conversion-function-id. */
8286 if (!cp_parser_parse_definitely (parser))
8287 identifier = cp_parser_conversion_function_id (parser);
8289 /* Look for the identifier. */
8291 identifier = cp_parser_identifier (parser);
8293 /* If we didn't find an identifier, we don't have a template-id. */
8294 if (identifier == error_mark_node)
8295 return error_mark_node;
8297 /* If the name immediately followed the `template' keyword, then it
8298 is a template-name. However, if the next token is not `<', then
8299 we do not treat it as a template-name, since it is not being used
8300 as part of a template-id. This enables us to handle constructs
8303 template <typename T> struct S { S(); };
8304 template <typename T> S<T>::S();
8306 correctly. We would treat `S' as a template -- if it were `S<T>'
8307 -- but we do not if there is no `<'. */
8308 if (template_keyword_p && processing_template_decl
8309 && cp_lexer_next_token_is (parser->lexer, CPP_LESS))
8312 /* Look up the name. */
8313 decl = cp_parser_lookup_name (parser, identifier,
8314 /*check_access=*/true,
8316 /*is_namespace=*/false,
8317 check_dependency_p);
8318 decl = maybe_get_template_decl_from_type_decl (decl);
8320 /* If DECL is a template, then the name was a template-name. */
8321 if (TREE_CODE (decl) == TEMPLATE_DECL)
8325 /* The standard does not explicitly indicate whether a name that
8326 names a set of overloaded declarations, some of which are
8327 templates, is a template-name. However, such a name should
8328 be a template-name; otherwise, there is no way to form a
8329 template-id for the overloaded templates. */
8330 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
8331 if (TREE_CODE (fns) == OVERLOAD)
8335 for (fn = fns; fn; fn = OVL_NEXT (fn))
8336 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
8341 /* Otherwise, the name does not name a template. */
8342 cp_parser_error (parser, "expected template-name");
8343 return error_mark_node;
8347 /* If DECL is dependent, and refers to a function, then just return
8348 its name; we will look it up again during template instantiation. */
8349 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
8351 tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
8352 if (TYPE_P (scope) && cp_parser_dependent_type_p (scope))
8359 /* Parse a template-argument-list.
8361 template-argument-list:
8363 template-argument-list , template-argument
8365 Returns a TREE_LIST representing the arguments, in the order they
8366 appeared. The TREE_VALUE of each node is a representation of the
8370 cp_parser_template_argument_list (parser)
8373 tree arguments = NULL_TREE;
8379 /* Parse the template-argument. */
8380 argument = cp_parser_template_argument (parser);
8381 /* Add it to the list. */
8382 arguments = tree_cons (NULL_TREE, argument, arguments);
8383 /* If it is not a `,', then there are no more arguments. */
8384 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8386 /* Otherwise, consume the ','. */
8387 cp_lexer_consume_token (parser->lexer);
8390 /* We built up the arguments in reverse order. */
8391 return nreverse (arguments);
8394 /* Parse a template-argument.
8397 assignment-expression
8401 The representation is that of an assignment-expression, type-id, or
8402 id-expression -- except that the qualified id-expression is
8403 evaluated, so that the value returned is either a DECL or an
8407 cp_parser_template_argument (parser)
8413 /* There's really no way to know what we're looking at, so we just
8414 try each alternative in order.
8418 In a template-argument, an ambiguity between a type-id and an
8419 expression is resolved to a type-id, regardless of the form of
8420 the corresponding template-parameter.
8422 Therefore, we try a type-id first. */
8423 cp_parser_parse_tentatively (parser);
8424 argument = cp_parser_type_id (parser);
8425 /* If the next token isn't a `,' or a `>', then this argument wasn't
8427 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
8428 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER))
8429 cp_parser_error (parser, "expected template-argument");
8430 /* If that worked, we're done. */
8431 if (cp_parser_parse_definitely (parser))
8433 /* We're still not sure what the argument will be. */
8434 cp_parser_parse_tentatively (parser);
8435 /* Try a template. */
8436 argument = cp_parser_id_expression (parser,
8437 /*template_keyword_p=*/false,
8438 /*check_dependency_p=*/true,
8440 /* If the next token isn't a `,' or a `>', then this argument wasn't
8442 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
8443 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER))
8444 cp_parser_error (parser, "expected template-argument");
8445 if (!cp_parser_error_occurred (parser))
8447 /* Figure out what is being referred to. */
8448 argument = cp_parser_lookup_name_simple (parser, argument);
8450 argument = make_unbound_class_template (TREE_OPERAND (argument, 0),
8451 TREE_OPERAND (argument, 1),
8452 tf_error | tf_parsing);
8453 else if (TREE_CODE (argument) != TEMPLATE_DECL)
8454 cp_parser_error (parser, "expected template-name");
8456 if (cp_parser_parse_definitely (parser))
8458 /* It must be an assignment-expression. */
8459 return cp_parser_assignment_expression (parser);
8462 /* Parse an explicit-instantiation.
8464 explicit-instantiation:
8465 template declaration
8467 Although the standard says `declaration', what it really means is:
8469 explicit-instantiation:
8470 template decl-specifier-seq [opt] declarator [opt] ;
8472 Things like `template int S<int>::i = 5, int S<double>::j;' are not
8473 supposed to be allowed. A defect report has been filed about this
8478 explicit-instantiation:
8479 storage-class-specifier template
8480 decl-specifier-seq [opt] declarator [opt] ;
8481 function-specifier template
8482 decl-specifier-seq [opt] declarator [opt] ; */
8485 cp_parser_explicit_instantiation (parser)
8488 bool declares_class_or_enum;
8489 tree decl_specifiers;
8491 tree extension_specifier = NULL_TREE;
8493 /* Look for an (optional) storage-class-specifier or
8494 function-specifier. */
8495 if (cp_parser_allow_gnu_extensions_p (parser))
8498 = cp_parser_storage_class_specifier_opt (parser);
8499 if (!extension_specifier)
8500 extension_specifier = cp_parser_function_specifier_opt (parser);
8503 /* Look for the `template' keyword. */
8504 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
8505 /* Let the front end know that we are processing an explicit
8507 begin_explicit_instantiation ();
8508 /* [temp.explicit] says that we are supposed to ignore access
8509 control while processing explicit instantiation directives. */
8510 scope_chain->check_access = 0;
8511 /* Parse a decl-specifier-seq. */
8513 = cp_parser_decl_specifier_seq (parser,
8514 CP_PARSER_FLAGS_OPTIONAL,
8516 &declares_class_or_enum);
8517 /* If there was exactly one decl-specifier, and it declared a class,
8518 and there's no declarator, then we have an explicit type
8520 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
8524 type = check_tag_decl (decl_specifiers);
8526 do_type_instantiation (type, extension_specifier, /*complain=*/1);
8533 /* Parse the declarator. */
8535 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
8536 /*ctor_dtor_or_conv_p=*/NULL);
8537 decl = grokdeclarator (declarator, decl_specifiers,
8539 /* Do the explicit instantiation. */
8540 do_decl_instantiation (decl, extension_specifier);
8542 /* We're done with the instantiation. */
8543 end_explicit_instantiation ();
8544 /* Trun access control back on. */
8545 scope_chain->check_access = flag_access_control;
8547 /* Look for the trailing `;'. */
8548 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
8551 /* Parse an explicit-specialization.
8553 explicit-specialization:
8554 template < > declaration
8556 Although the standard says `declaration', what it really means is:
8558 explicit-specialization:
8559 template <> decl-specifier [opt] init-declarator [opt] ;
8560 template <> function-definition
8561 template <> explicit-specialization
8562 template <> template-declaration */
8565 cp_parser_explicit_specialization (parser)
8568 /* Look for the `template' keyword. */
8569 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
8570 /* Look for the `<'. */
8571 cp_parser_require (parser, CPP_LESS, "`<'");
8572 /* Look for the `>'. */
8573 cp_parser_require (parser, CPP_GREATER, "`>'");
8574 /* We have processed another parameter list. */
8575 ++parser->num_template_parameter_lists;
8576 /* Let the front end know that we are beginning a specialization. */
8577 begin_specialization ();
8579 /* If the next keyword is `template', we need to figure out whether
8580 or not we're looking a template-declaration. */
8581 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
8583 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
8584 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
8585 cp_parser_template_declaration_after_export (parser,
8586 /*member_p=*/false);
8588 cp_parser_explicit_specialization (parser);
8591 /* Parse the dependent declaration. */
8592 cp_parser_single_declaration (parser,
8596 /* We're done with the specialization. */
8597 end_specialization ();
8598 /* We're done with this parameter list. */
8599 --parser->num_template_parameter_lists;
8602 /* Parse a type-specifier.
8605 simple-type-specifier
8608 elaborated-type-specifier
8616 Returns a representation of the type-specifier. If the
8617 type-specifier is a keyword (like `int' or `const', or
8618 `__complex__') then the correspoding IDENTIFIER_NODE is returned.
8619 For a class-specifier, enum-specifier, or elaborated-type-specifier
8620 a TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
8622 If IS_FRIEND is TRUE then this type-specifier is being declared a
8623 `friend'. If IS_DECLARATION is TRUE, then this type-specifier is
8624 appearing in a decl-specifier-seq.
8626 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
8627 class-specifier, enum-specifier, or elaborated-type-specifier, then
8628 *DECLARES_CLASS_OR_ENUM is set to TRUE. Otherwise, it is set to
8631 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
8632 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
8636 cp_parser_type_specifier (parser,
8640 declares_class_or_enum,
8643 cp_parser_flags flags;
8645 bool is_declaration;
8646 bool *declares_class_or_enum;
8647 bool *is_cv_qualifier;
8649 tree type_spec = NULL_TREE;
8653 /* Assume this type-specifier does not declare a new type. */
8654 if (declares_class_or_enum)
8655 *declares_class_or_enum = false;
8656 /* And that it does not specify a cv-qualifier. */
8657 if (is_cv_qualifier)
8658 *is_cv_qualifier = false;
8659 /* Peek at the next token. */
8660 token = cp_lexer_peek_token (parser->lexer);
8662 /* If we're looking at a keyword, we can use that to guide the
8663 production we choose. */
8664 keyword = token->keyword;
8667 /* Any of these indicate either a class-specifier, or an
8668 elaborated-type-specifier. */
8673 /* Parse tentatively so that we can back up if we don't find a
8674 class-specifier or enum-specifier. */
8675 cp_parser_parse_tentatively (parser);
8676 /* Look for the class-specifier or enum-specifier. */
8677 if (keyword == RID_ENUM)
8678 type_spec = cp_parser_enum_specifier (parser);
8680 type_spec = cp_parser_class_specifier (parser);
8682 /* If that worked, we're done. */
8683 if (cp_parser_parse_definitely (parser))
8685 if (declares_class_or_enum)
8686 *declares_class_or_enum = true;
8693 /* Look for an elaborated-type-specifier. */
8694 type_spec = cp_parser_elaborated_type_specifier (parser,
8697 /* We're declaring a class or enum -- unless we're using
8699 if (declares_class_or_enum && keyword != RID_TYPENAME)
8700 *declares_class_or_enum = true;
8706 type_spec = cp_parser_cv_qualifier_opt (parser);
8707 /* Even though we call a routine that looks for an optional
8708 qualifier, we know that there should be one. */
8709 my_friendly_assert (type_spec != NULL, 20000328);
8710 /* This type-specifier was a cv-qualified. */
8711 if (is_cv_qualifier)
8712 *is_cv_qualifier = true;
8717 /* The `__complex__' keyword is a GNU extension. */
8718 return cp_lexer_consume_token (parser->lexer)->value;
8724 /* If we do not already have a type-specifier, assume we are looking
8725 at a simple-type-specifier. */
8726 type_spec = cp_parser_simple_type_specifier (parser, flags);
8728 /* If we didn't find a type-specifier, and a type-specifier was not
8729 optional in this context, issue an error message. */
8730 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
8732 cp_parser_error (parser, "expected type specifier");
8733 return error_mark_node;
8739 /* Parse a simple-type-specifier.
8741 simple-type-specifier:
8742 :: [opt] nested-name-specifier [opt] type-name
8743 :: [opt] nested-name-specifier template template-id
8758 simple-type-specifier:
8759 __typeof__ unary-expression
8760 __typeof__ ( type-id )
8762 For the various keywords, the value returned is simply the
8763 TREE_IDENTIFIER representing the keyword. For the first two
8764 productions, the value returned is the indicated TYPE_DECL. */
8767 cp_parser_simple_type_specifier (parser, flags)
8769 cp_parser_flags flags;
8771 tree type = NULL_TREE;
8774 /* Peek at the next token. */
8775 token = cp_lexer_peek_token (parser->lexer);
8777 /* If we're looking at a keyword, things are easy. */
8778 switch (token->keyword)
8791 /* Consume the token. */
8792 return cp_lexer_consume_token (parser->lexer)->value;
8798 /* Consume the `typeof' token. */
8799 cp_lexer_consume_token (parser->lexer);
8800 /* Parse the operand to `typeof' */
8801 operand = cp_parser_sizeof_operand (parser, RID_TYPEOF);
8802 /* If it is not already a TYPE, take its type. */
8803 if (!TYPE_P (operand))
8804 operand = finish_typeof (operand);
8813 /* The type-specifier must be a user-defined type. */
8814 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
8816 /* Don't gobble tokens or issue error messages if this is an
8817 optional type-specifier. */
8818 if (flags & CP_PARSER_FLAGS_OPTIONAL)
8819 cp_parser_parse_tentatively (parser);
8821 /* Look for the optional `::' operator. */
8822 cp_parser_global_scope_opt (parser,
8823 /*current_scope_valid_p=*/false);
8824 /* Look for the nested-name specifier. */
8825 cp_parser_nested_name_specifier_opt (parser,
8826 /*typename_keyword_p=*/false,
8827 /*check_dependency_p=*/true,
8829 /* If we have seen a nested-name-specifier, and the next token
8830 is `template', then we are using the template-id production. */
8832 && cp_parser_optional_template_keyword (parser))
8834 /* Look for the template-id. */
8835 type = cp_parser_template_id (parser,
8836 /*template_keyword_p=*/true,
8837 /*check_dependency_p=*/true);
8838 /* If the template-id did not name a type, we are out of
8840 if (TREE_CODE (type) != TYPE_DECL)
8842 cp_parser_error (parser, "expected template-id for type");
8846 /* Otherwise, look for a type-name. */
8849 type = cp_parser_type_name (parser);
8850 if (type == error_mark_node)
8854 /* If it didn't work out, we don't have a TYPE. */
8855 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
8856 && !cp_parser_parse_definitely (parser))
8860 /* If we didn't get a type-name, issue an error message. */
8861 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
8863 cp_parser_error (parser, "expected type-name");
8864 return error_mark_node;
8870 /* Parse a type-name.
8883 Returns a TYPE_DECL for the the type. */
8886 cp_parser_type_name (parser)
8892 /* We can't know yet whether it is a class-name or not. */
8893 cp_parser_parse_tentatively (parser);
8894 /* Try a class-name. */
8895 type_decl = cp_parser_class_name (parser,
8896 /*typename_keyword_p=*/false,
8897 /*template_keyword_p=*/false,
8899 /*check_access_p=*/true,
8900 /*check_dependency_p=*/true,
8901 /*class_head_p=*/false);
8902 /* If it's not a class-name, keep looking. */
8903 if (!cp_parser_parse_definitely (parser))
8905 /* It must be a typedef-name or an enum-name. */
8906 identifier = cp_parser_identifier (parser);
8907 if (identifier == error_mark_node)
8908 return error_mark_node;
8910 /* Look up the type-name. */
8911 type_decl = cp_parser_lookup_name_simple (parser, identifier);
8912 /* Issue an error if we did not find a type-name. */
8913 if (TREE_CODE (type_decl) != TYPE_DECL)
8915 cp_parser_error (parser, "expected type-name");
8916 type_decl = error_mark_node;
8918 /* Remember that the name was used in the definition of the
8919 current class so that we can check later to see if the
8920 meaning would have been different after the class was
8921 entirely defined. */
8922 else if (type_decl != error_mark_node
8924 maybe_note_name_used_in_class (identifier, type_decl);
8931 /* Parse an elaborated-type-specifier. Note that the grammar given
8932 here incorporates the resolution to DR68.
8934 elaborated-type-specifier:
8935 class-key :: [opt] nested-name-specifier [opt] identifier
8936 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
8937 enum :: [opt] nested-name-specifier [opt] identifier
8938 typename :: [opt] nested-name-specifier identifier
8939 typename :: [opt] nested-name-specifier template [opt]
8942 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
8943 declared `friend'. If IS_DECLARATION is TRUE, then this
8944 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
8945 something is being declared.
8947 Returns the TYPE specified. */
8950 cp_parser_elaborated_type_specifier (parser, is_friend, is_declaration)
8953 bool is_declaration;
8955 enum tag_types tag_type;
8957 tree type = NULL_TREE;
8959 /* See if we're looking at the `enum' keyword. */
8960 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
8962 /* Consume the `enum' token. */
8963 cp_lexer_consume_token (parser->lexer);
8964 /* Remember that it's an enumeration type. */
8965 tag_type = enum_type;
8967 /* Or, it might be `typename'. */
8968 else if (cp_lexer_next_token_is_keyword (parser->lexer,
8971 /* Consume the `typename' token. */
8972 cp_lexer_consume_token (parser->lexer);
8973 /* Remember that it's a `typename' type. */
8974 tag_type = typename_type;
8975 /* The `typename' keyword is only allowed in templates. */
8976 if (!processing_template_decl)
8977 pedwarn ("using `typename' outside of template");
8979 /* Otherwise it must be a class-key. */
8982 tag_type = cp_parser_class_key (parser);
8983 if (tag_type == none_type)
8984 return error_mark_node;
8987 /* Look for the `::' operator. */
8988 cp_parser_global_scope_opt (parser,
8989 /*current_scope_valid_p=*/false);
8990 /* Look for the nested-name-specifier. */
8991 if (tag_type == typename_type)
8992 cp_parser_nested_name_specifier (parser,
8993 /*typename_keyword_p=*/true,
8994 /*check_dependency_p=*/true,
8997 /* Even though `typename' is not present, the proposed resolution
8998 to Core Issue 180 says that in `class A<T>::B', `B' should be
8999 considered a type-name, even if `A<T>' is dependent. */
9000 cp_parser_nested_name_specifier_opt (parser,
9001 /*typename_keyword_p=*/true,
9002 /*check_dependency_p=*/true,
9004 /* For everything but enumeration types, consider a template-id. */
9005 if (tag_type != enum_type)
9007 bool template_p = false;
9010 /* Allow the `template' keyword. */
9011 template_p = cp_parser_optional_template_keyword (parser);
9012 /* If we didn't see `template', we don't know if there's a
9013 template-id or not. */
9015 cp_parser_parse_tentatively (parser);
9016 /* Parse the template-id. */
9017 decl = cp_parser_template_id (parser, template_p,
9018 /*check_dependency_p=*/true);
9019 /* If we didn't find a template-id, look for an ordinary
9021 if (!template_p && !cp_parser_parse_definitely (parser))
9023 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
9024 in effect, then we must assume that, upon instantiation, the
9025 template will correspond to a class. */
9026 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
9027 && tag_type == typename_type)
9028 type = make_typename_type (parser->scope, decl,
9031 type = TREE_TYPE (decl);
9034 /* For an enumeration type, consider only a plain identifier. */
9037 identifier = cp_parser_identifier (parser);
9039 if (identifier == error_mark_node)
9040 return error_mark_node;
9042 /* For a `typename', we needn't call xref_tag. */
9043 if (tag_type == typename_type)
9044 return make_typename_type (parser->scope, identifier,
9046 /* Look up a qualified name in the usual way. */
9051 /* In an elaborated-type-specifier, names are assumed to name
9052 types, so we set IS_TYPE to TRUE when calling
9053 cp_parser_lookup_name. */
9054 decl = cp_parser_lookup_name (parser, identifier,
9055 /*check_access=*/true,
9057 /*is_namespace=*/false,
9058 /*check_dependency=*/true);
9059 decl = (cp_parser_maybe_treat_template_as_class
9060 (decl, /*tag_name_p=*/is_friend));
9062 if (TREE_CODE (decl) != TYPE_DECL)
9064 error ("expected type-name");
9065 return error_mark_node;
9067 else if (TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE
9068 && tag_type != enum_type)
9069 error ("`%T' referred to as `%s'", TREE_TYPE (decl),
9070 tag_type == record_type ? "struct" : "class");
9071 else if (TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE
9072 && tag_type == enum_type)
9073 error ("`%T' referred to as enum", TREE_TYPE (decl));
9075 type = TREE_TYPE (decl);
9079 /* An elaborated-type-specifier sometimes introduces a new type and
9080 sometimes names an existing type. Normally, the rule is that it
9081 introduces a new type only if there is not an existing type of
9082 the same name already in scope. For example, given:
9085 void f() { struct S s; }
9087 the `struct S' in the body of `f' is the same `struct S' as in
9088 the global scope; the existing definition is used. However, if
9089 there were no global declaration, this would introduce a new
9090 local class named `S'.
9092 An exception to this rule applies to the following code:
9094 namespace N { struct S; }
9096 Here, the elaborated-type-specifier names a new type
9097 unconditionally; even if there is already an `S' in the
9098 containing scope this declaration names a new type.
9099 This exception only applies if the elaborated-type-specifier
9100 forms the complete declaration:
9104 A declaration consisting solely of `class-key identifier ;' is
9105 either a redeclaration of the name in the current scope or a
9106 forward declaration of the identifier as a class name. It
9107 introduces the name into the current scope.
9109 We are in this situation precisely when the next token is a `;'.
9111 An exception to the exception is that a `friend' declaration does
9112 *not* name a new type; i.e., given:
9114 struct S { friend struct T; };
9116 `T' is not a new type in the scope of `S'.
9118 Also, `new struct S' or `sizeof (struct S)' never results in the
9119 definition of a new type; a new type can only be declared in a
9120 declaration context. */
9122 type = xref_tag (tag_type, identifier,
9123 /*attributes=*/NULL_TREE,
9126 || cp_lexer_next_token_is_not (parser->lexer,
9130 if (tag_type != enum_type)
9131 cp_parser_check_class_key (tag_type, type);
9135 /* Parse an enum-specifier.
9138 enum identifier [opt] { enumerator-list [opt] }
9140 Returns an ENUM_TYPE representing the enumeration. */
9143 cp_parser_enum_specifier (parser)
9147 tree identifier = NULL_TREE;
9150 /* Look for the `enum' keyword. */
9151 if (!cp_parser_require_keyword (parser, RID_ENUM, "`enum'"))
9152 return error_mark_node;
9153 /* Peek at the next token. */
9154 token = cp_lexer_peek_token (parser->lexer);
9156 /* See if it is an identifier. */
9157 if (token->type == CPP_NAME)
9158 identifier = cp_parser_identifier (parser);
9160 /* Look for the `{'. */
9161 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
9162 return error_mark_node;
9164 /* At this point, we're going ahead with the enum-specifier, even
9165 if some other problem occurs. */
9166 cp_parser_commit_to_tentative_parse (parser);
9168 /* Issue an error message if type-definitions are forbidden here. */
9169 cp_parser_check_type_definition (parser);
9171 /* Create the new type. */
9172 type = start_enum (identifier ? identifier : make_anon_name ());
9174 /* Peek at the next token. */
9175 token = cp_lexer_peek_token (parser->lexer);
9176 /* If it's not a `}', then there are some enumerators. */
9177 if (token->type != CPP_CLOSE_BRACE)
9178 cp_parser_enumerator_list (parser, type);
9179 /* Look for the `}'. */
9180 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
9182 /* Finish up the enumeration. */
9188 /* Parse an enumerator-list. The enumerators all have the indicated
9192 enumerator-definition
9193 enumerator-list , enumerator-definition */
9196 cp_parser_enumerator_list (parser, type)
9204 /* Parse an enumerator-definition. */
9205 cp_parser_enumerator_definition (parser, type);
9206 /* Peek at the next token. */
9207 token = cp_lexer_peek_token (parser->lexer);
9208 /* If it's not a `,', then we've reached the end of the
9210 if (token->type != CPP_COMMA)
9212 /* Otherwise, consume the `,' and keep going. */
9213 cp_lexer_consume_token (parser->lexer);
9214 /* If the next token is a `}', there is a trailing comma. */
9215 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
9217 if (pedantic && !in_system_header)
9218 pedwarn ("comma at end of enumerator list");
9224 /* Parse an enumerator-definition. The enumerator has the indicated
9227 enumerator-definition:
9229 enumerator = constant-expression
9235 cp_parser_enumerator_definition (parser, type)
9243 /* Look for the identifier. */
9244 identifier = cp_parser_identifier (parser);
9245 if (identifier == error_mark_node)
9248 /* Peek at the next token. */
9249 token = cp_lexer_peek_token (parser->lexer);
9250 /* If it's an `=', then there's an explicit value. */
9251 if (token->type == CPP_EQ)
9253 /* Consume the `=' token. */
9254 cp_lexer_consume_token (parser->lexer);
9255 /* Parse the value. */
9256 value = cp_parser_constant_expression (parser);
9261 /* Create the enumerator. */
9262 build_enumerator (identifier, value, type);
9265 /* Parse a namespace-name.
9268 original-namespace-name
9271 Returns the NAMESPACE_DECL for the namespace. */
9274 cp_parser_namespace_name (parser)
9278 tree namespace_decl;
9280 /* Get the name of the namespace. */
9281 identifier = cp_parser_identifier (parser);
9282 if (identifier == error_mark_node)
9283 return error_mark_node;
9285 /* Look up the identifier in the currently active scope. Look only
9286 for namespaces, due to:
9290 When looking up a namespace-name in a using-directive or alias
9291 definition, only namespace names are considered.
9297 During the lookup of a name preceding the :: scope resolution
9298 operator, object, function, and enumerator names are ignored.
9300 (Note that cp_parser_class_or_namespace_name only calls this
9301 function if the token after the name is the scope resolution
9303 namespace_decl = cp_parser_lookup_name (parser, identifier,
9304 /*check_access=*/true,
9306 /*is_namespace=*/true,
9307 /*check_dependency=*/true);
9308 /* If it's not a namespace, issue an error. */
9309 if (namespace_decl == error_mark_node
9310 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
9312 cp_parser_error (parser, "expected namespace-name");
9313 namespace_decl = error_mark_node;
9316 return namespace_decl;
9319 /* Parse a namespace-definition.
9321 namespace-definition:
9322 named-namespace-definition
9323 unnamed-namespace-definition
9325 named-namespace-definition:
9326 original-namespace-definition
9327 extension-namespace-definition
9329 original-namespace-definition:
9330 namespace identifier { namespace-body }
9332 extension-namespace-definition:
9333 namespace original-namespace-name { namespace-body }
9335 unnamed-namespace-definition:
9336 namespace { namespace-body } */
9339 cp_parser_namespace_definition (parser)
9344 /* Look for the `namespace' keyword. */
9345 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
9347 /* Get the name of the namespace. We do not attempt to distinguish
9348 between an original-namespace-definition and an
9349 extension-namespace-definition at this point. The semantic
9350 analysis routines are responsible for that. */
9351 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
9352 identifier = cp_parser_identifier (parser);
9354 identifier = NULL_TREE;
9356 /* Look for the `{' to start the namespace. */
9357 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
9358 /* Start the namespace. */
9359 push_namespace (identifier);
9360 /* Parse the body of the namespace. */
9361 cp_parser_namespace_body (parser);
9362 /* Finish the namespace. */
9364 /* Look for the final `}'. */
9365 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
9368 /* Parse a namespace-body.
9371 declaration-seq [opt] */
9374 cp_parser_namespace_body (parser)
9377 cp_parser_declaration_seq_opt (parser);
9380 /* Parse a namespace-alias-definition.
9382 namespace-alias-definition:
9383 namespace identifier = qualified-namespace-specifier ; */
9386 cp_parser_namespace_alias_definition (parser)
9390 tree namespace_specifier;
9392 /* Look for the `namespace' keyword. */
9393 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
9394 /* Look for the identifier. */
9395 identifier = cp_parser_identifier (parser);
9396 if (identifier == error_mark_node)
9398 /* Look for the `=' token. */
9399 cp_parser_require (parser, CPP_EQ, "`='");
9400 /* Look for the qualified-namespace-specifier. */
9402 = cp_parser_qualified_namespace_specifier (parser);
9403 /* Look for the `;' token. */
9404 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
9406 /* Register the alias in the symbol table. */
9407 do_namespace_alias (identifier, namespace_specifier);
9410 /* Parse a qualified-namespace-specifier.
9412 qualified-namespace-specifier:
9413 :: [opt] nested-name-specifier [opt] namespace-name
9415 Returns a NAMESPACE_DECL corresponding to the specified
9419 cp_parser_qualified_namespace_specifier (parser)
9422 /* Look for the optional `::'. */
9423 cp_parser_global_scope_opt (parser,
9424 /*current_scope_valid_p=*/false);
9426 /* Look for the optional nested-name-specifier. */
9427 cp_parser_nested_name_specifier_opt (parser,
9428 /*typename_keyword_p=*/false,
9429 /*check_dependency_p=*/true,
9432 return cp_parser_namespace_name (parser);
9435 /* Parse a using-declaration.
9438 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
9439 using :: unqualified-id ; */
9442 cp_parser_using_declaration (parser)
9446 bool typename_p = false;
9447 bool global_scope_p;
9452 /* Look for the `using' keyword. */
9453 cp_parser_require_keyword (parser, RID_USING, "`using'");
9455 /* Peek at the next token. */
9456 token = cp_lexer_peek_token (parser->lexer);
9457 /* See if it's `typename'. */
9458 if (token->keyword == RID_TYPENAME)
9460 /* Remember that we've seen it. */
9462 /* Consume the `typename' token. */
9463 cp_lexer_consume_token (parser->lexer);
9466 /* Look for the optional global scope qualification. */
9468 = (cp_parser_global_scope_opt (parser,
9469 /*current_scope_valid_p=*/false)
9472 /* If we saw `typename', or didn't see `::', then there must be a
9473 nested-name-specifier present. */
9474 if (typename_p || !global_scope_p)
9475 cp_parser_nested_name_specifier (parser, typename_p,
9476 /*check_dependency_p=*/true,
9478 /* Otherwise, we could be in either of the two productions. In that
9479 case, treat the nested-name-specifier as optional. */
9481 cp_parser_nested_name_specifier_opt (parser,
9482 /*typename_keyword_p=*/false,
9483 /*check_dependency_p=*/true,
9486 /* Parse the unqualified-id. */
9487 identifier = cp_parser_unqualified_id (parser,
9488 /*template_keyword_p=*/false,
9489 /*check_dependency_p=*/true);
9491 /* The function we call to handle a using-declaration is different
9492 depending on what scope we are in. */
9493 scope = current_scope ();
9494 if (scope && TYPE_P (scope))
9496 /* Create the USING_DECL. */
9497 decl = do_class_using_decl (build_nt (SCOPE_REF,
9500 /* Add it to the list of members in this class. */
9501 finish_member_declaration (decl);
9505 decl = cp_parser_lookup_name_simple (parser, identifier);
9507 do_local_using_decl (decl);
9509 do_toplevel_using_decl (decl);
9512 /* Look for the final `;'. */
9513 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
9516 /* Parse a using-directive.
9519 using namespace :: [opt] nested-name-specifier [opt]
9523 cp_parser_using_directive (parser)
9526 tree namespace_decl;
9528 /* Look for the `using' keyword. */
9529 cp_parser_require_keyword (parser, RID_USING, "`using'");
9530 /* And the `namespace' keyword. */
9531 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
9532 /* Look for the optional `::' operator. */
9533 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
9534 /* And the optional nested-name-sepcifier. */
9535 cp_parser_nested_name_specifier_opt (parser,
9536 /*typename_keyword_p=*/false,
9537 /*check_dependency_p=*/true,
9539 /* Get the namespace being used. */
9540 namespace_decl = cp_parser_namespace_name (parser);
9541 /* Update the symbol table. */
9542 do_using_directive (namespace_decl);
9543 /* Look for the final `;'. */
9544 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
9547 /* Parse an asm-definition.
9550 asm ( string-literal ) ;
9555 asm volatile [opt] ( string-literal ) ;
9556 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
9557 asm volatile [opt] ( string-literal : asm-operand-list [opt]
9558 : asm-operand-list [opt] ) ;
9559 asm volatile [opt] ( string-literal : asm-operand-list [opt]
9560 : asm-operand-list [opt]
9561 : asm-operand-list [opt] ) ; */
9564 cp_parser_asm_definition (parser)
9569 tree outputs = NULL_TREE;
9570 tree inputs = NULL_TREE;
9571 tree clobbers = NULL_TREE;
9573 bool volatile_p = false;
9574 bool extended_p = false;
9576 /* Look for the `asm' keyword. */
9577 cp_parser_require_keyword (parser, RID_ASM, "`asm'");
9578 /* See if the next token is `volatile'. */
9579 if (cp_parser_allow_gnu_extensions_p (parser)
9580 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
9582 /* Remember that we saw the `volatile' keyword. */
9584 /* Consume the token. */
9585 cp_lexer_consume_token (parser->lexer);
9587 /* Look for the opening `('. */
9588 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
9589 /* Look for the string. */
9590 token = cp_parser_require (parser, CPP_STRING, "asm body");
9593 string = token->value;
9594 /* If we're allowing GNU extensions, check for the extended assembly
9595 syntax. Unfortunately, the `:' tokens need not be separated by
9596 a space in C, and so, for compatibility, we tolerate that here
9597 too. Doing that means that we have to treat the `::' operator as
9599 if (cp_parser_allow_gnu_extensions_p (parser)
9600 && at_function_scope_p ()
9601 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
9602 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
9604 bool inputs_p = false;
9605 bool clobbers_p = false;
9607 /* The extended syntax was used. */
9610 /* Look for outputs. */
9611 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
9613 /* Consume the `:'. */
9614 cp_lexer_consume_token (parser->lexer);
9615 /* Parse the output-operands. */
9616 if (cp_lexer_next_token_is_not (parser->lexer,
9618 && cp_lexer_next_token_is_not (parser->lexer,
9620 && cp_lexer_next_token_is_not (parser->lexer,
9622 outputs = cp_parser_asm_operand_list (parser);
9624 /* If the next token is `::', there are no outputs, and the
9625 next token is the beginning of the inputs. */
9626 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
9628 /* Consume the `::' token. */
9629 cp_lexer_consume_token (parser->lexer);
9630 /* The inputs are coming next. */
9634 /* Look for inputs. */
9636 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
9639 /* Consume the `:'. */
9640 cp_lexer_consume_token (parser->lexer);
9641 /* Parse the output-operands. */
9642 if (cp_lexer_next_token_is_not (parser->lexer,
9644 && cp_lexer_next_token_is_not (parser->lexer,
9646 && cp_lexer_next_token_is_not (parser->lexer,
9648 inputs = cp_parser_asm_operand_list (parser);
9650 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
9651 /* The clobbers are coming next. */
9654 /* Look for clobbers. */
9656 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
9659 /* Consume the `:'. */
9660 cp_lexer_consume_token (parser->lexer);
9661 /* Parse the clobbers. */
9662 if (cp_lexer_next_token_is_not (parser->lexer,
9664 clobbers = cp_parser_asm_clobber_list (parser);
9667 /* Look for the closing `)'. */
9668 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
9669 cp_parser_skip_to_closing_parenthesis (parser);
9670 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
9672 /* Create the ASM_STMT. */
9673 if (at_function_scope_p ())
9676 finish_asm_stmt (volatile_p
9677 ? ridpointers[(int) RID_VOLATILE] : NULL_TREE,
9678 string, outputs, inputs, clobbers);
9679 /* If the extended syntax was not used, mark the ASM_STMT. */
9681 ASM_INPUT_P (asm_stmt) = 1;
9684 assemble_asm (string);
9687 /* Declarators [gram.dcl.decl] */
9689 /* Parse an init-declarator.
9692 declarator initializer [opt]
9697 declarator asm-specification [opt] attributes [opt] initializer [opt]
9699 The DECL_SPECIFIERS and PREFIX_ATTRIBUTES apply to this declarator.
9700 Returns a reprsentation of the entity declared. The ACCESS_CHECKS
9701 represent deferred access checks from the decl-specifier-seq. If
9702 MEMBER_P is TRUE, then this declarator appears in a class scope.
9703 The new DECL created by this declarator is returned.
9705 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
9706 for a function-definition here as well. If the declarator is a
9707 declarator for a function-definition, *FUNCTION_DEFINITION_P will
9708 be TRUE upon return. By that point, the function-definition will
9709 have been completely parsed.
9711 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
9715 cp_parser_init_declarator (parser,
9719 function_definition_allowed_p,
9721 function_definition_p)
9723 tree decl_specifiers;
9724 tree prefix_attributes;
9726 bool function_definition_allowed_p;
9728 bool *function_definition_p;
9733 tree asm_specification;
9735 tree decl = NULL_TREE;
9737 tree declarator_access_checks;
9738 bool is_initialized;
9739 bool is_parenthesized_init;
9740 bool ctor_dtor_or_conv_p;
9743 /* Assume that this is not the declarator for a function
9745 if (function_definition_p)
9746 *function_definition_p = false;
9748 /* Defer access checks while parsing the declarator; we cannot know
9749 what names are accessible until we know what is being
9751 cp_parser_start_deferring_access_checks (parser);
9752 /* Parse the declarator. */
9754 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
9755 &ctor_dtor_or_conv_p);
9756 /* Gather up the deferred checks. */
9757 declarator_access_checks
9758 = cp_parser_stop_deferring_access_checks (parser);
9760 /* Prevent the access checks from being reclaimed by GC. */
9761 parser->access_checks_lists
9762 = tree_cons (NULL_TREE, declarator_access_checks,
9763 parser->access_checks_lists);
9765 /* If the DECLARATOR was erroneous, there's no need to go
9767 if (declarator == error_mark_node)
9769 /* Discard access checks no longer in use. */
9770 parser->access_checks_lists
9771 = TREE_CHAIN (parser->access_checks_lists);
9772 return error_mark_node;
9775 /* Figure out what scope the entity declared by the DECLARATOR is
9776 located in. `grokdeclarator' sometimes changes the scope, so
9777 we compute it now. */
9778 scope = get_scope_of_declarator (declarator);
9780 /* If we're allowing GNU extensions, look for an asm-specification
9782 if (cp_parser_allow_gnu_extensions_p (parser))
9784 /* Look for an asm-specification. */
9785 asm_specification = cp_parser_asm_specification_opt (parser);
9786 /* And attributes. */
9787 attributes = cp_parser_attributes_opt (parser);
9791 asm_specification = NULL_TREE;
9792 attributes = NULL_TREE;
9795 /* Peek at the next token. */
9796 token = cp_lexer_peek_token (parser->lexer);
9797 /* Check to see if the token indicates the start of a
9798 function-definition. */
9799 if (cp_parser_token_starts_function_definition_p (token))
9801 if (!function_definition_allowed_p)
9803 /* If a function-definition should not appear here, issue an
9805 cp_parser_error (parser,
9806 "a function-definition is not allowed here");
9807 /* Discard access checks no longer in use. */
9808 parser->access_checks_lists
9809 = TREE_CHAIN (parser->access_checks_lists);
9810 return error_mark_node;
9816 /* Neither attributes nor an asm-specification are allowed
9817 on a function-definition. */
9818 if (asm_specification)
9819 error ("an asm-specification is not allowed on a function-definition");
9821 error ("attributes are not allowed on a function-definition");
9822 /* This is a function-definition. */
9823 *function_definition_p = true;
9825 /* Thread the access checks together. */
9826 ac = &access_checks;
9828 ac = &TREE_CHAIN (*ac);
9829 *ac = declarator_access_checks;
9831 /* Parse the function definition. */
9832 decl = (cp_parser_function_definition_from_specifiers_and_declarator
9833 (parser, decl_specifiers, prefix_attributes, declarator,
9836 /* Pull the access-checks apart again. */
9839 /* Discard access checks no longer in use. */
9840 parser->access_checks_lists
9841 = TREE_CHAIN (parser->access_checks_lists);
9849 Only in function declarations for constructors, destructors, and
9850 type conversions can the decl-specifier-seq be omitted.
9852 We explicitly postpone this check past the point where we handle
9853 function-definitions because we tolerate function-definitions
9854 that are missing their return types in some modes. */
9855 if (!decl_specifiers && !ctor_dtor_or_conv_p)
9857 cp_parser_error (parser,
9858 "expected constructor, destructor, or type conversion");
9859 /* Discard access checks no longer in use. */
9860 parser->access_checks_lists
9861 = TREE_CHAIN (parser->access_checks_lists);
9862 return error_mark_node;
9865 /* An `=' or an `(' indicates an initializer. */
9866 is_initialized = (token->type == CPP_EQ
9867 || token->type == CPP_OPEN_PAREN);
9868 /* If the init-declarator isn't initialized and isn't followed by a
9869 `,' or `;', it's not a valid init-declarator. */
9871 && token->type != CPP_COMMA
9872 && token->type != CPP_SEMICOLON)
9874 cp_parser_error (parser, "expected init-declarator");
9875 /* Discard access checks no longer in use. */
9876 parser->access_checks_lists
9877 = TREE_CHAIN (parser->access_checks_lists);
9878 return error_mark_node;
9881 /* Because start_decl has side-effects, we should only call it if we
9882 know we're going ahead. By this point, we know that we cannot
9883 possibly be looking at any other construct. */
9884 cp_parser_commit_to_tentative_parse (parser);
9886 /* Check to see whether or not this declaration is a friend. */
9887 friend_p = cp_parser_friend_p (decl_specifiers);
9889 /* Check that the number of template-parameter-lists is OK. */
9890 if (!cp_parser_check_declarator_template_parameters (parser,
9893 /* Discard access checks no longer in use. */
9894 parser->access_checks_lists
9895 = TREE_CHAIN (parser->access_checks_lists);
9896 return error_mark_node;
9899 /* Enter the newly declared entry in the symbol table. If we're
9900 processing a declaration in a class-specifier, we wait until
9901 after processing the initializer. */
9904 if (parser->in_unbraced_linkage_specification_p)
9906 decl_specifiers = tree_cons (error_mark_node,
9907 get_identifier ("extern"),
9909 have_extern_spec = false;
9911 decl = start_decl (declarator,
9918 /* Enter the SCOPE. That way unqualified names appearing in the
9919 initializer will be looked up in SCOPE. */
9923 /* Perform deferred access control checks, now that we know in which
9924 SCOPE the declared entity resides. */
9925 if (!member_p && decl)
9927 tree saved_current_function_decl = NULL_TREE;
9929 /* If the entity being declared is a function, pretend that we
9930 are in its scope. If it is a `friend', it may have access to
9931 things that would not otherwise be accessible. */
9932 if (TREE_CODE (decl) == FUNCTION_DECL)
9934 saved_current_function_decl = current_function_decl;
9935 current_function_decl = decl;
9938 /* Perform the access control checks for the decl-specifiers. */
9939 cp_parser_perform_deferred_access_checks (access_checks);
9940 /* And for the declarator. */
9941 cp_parser_perform_deferred_access_checks (declarator_access_checks);
9943 /* Restore the saved value. */
9944 if (TREE_CODE (decl) == FUNCTION_DECL)
9945 current_function_decl = saved_current_function_decl;
9948 /* Parse the initializer. */
9950 initializer = cp_parser_initializer (parser,
9951 &is_parenthesized_init);
9954 initializer = NULL_TREE;
9955 is_parenthesized_init = false;
9958 /* The old parser allows attributes to appear after a parenthesized
9959 initializer. Mark Mitchell proposed removing this functionality
9960 on the GCC mailing lists on 2002-08-13. This parser accepts the
9961 attributes -- but ignores them. */
9962 if (cp_parser_allow_gnu_extensions_p (parser) && is_parenthesized_init)
9963 if (cp_parser_attributes_opt (parser))
9964 warning ("attributes after parenthesized initializer ignored");
9966 /* Leave the SCOPE, now that we have processed the initializer. It
9967 is important to do this before calling cp_finish_decl because it
9968 makes decisions about whether to create DECL_STMTs or not based
9969 on the current scope. */
9973 /* For an in-class declaration, use `grokfield' to create the
9976 decl = grokfield (declarator, decl_specifiers,
9977 initializer, /*asmspec=*/NULL_TREE,
9978 /*attributes=*/NULL_TREE);
9980 /* Finish processing the declaration. But, skip friend
9982 if (!friend_p && decl)
9983 cp_finish_decl (decl,
9986 /* If the initializer is in parentheses, then this is
9987 a direct-initialization, which means that an
9988 `explicit' constructor is OK. Otherwise, an
9989 `explicit' constructor cannot be used. */
9990 ((is_parenthesized_init || !is_initialized)
9991 ? 0 : LOOKUP_ONLYCONVERTING));
9993 /* Discard access checks no longer in use. */
9994 parser->access_checks_lists
9995 = TREE_CHAIN (parser->access_checks_lists);
10000 /* Parse a declarator.
10004 ptr-operator declarator
10006 abstract-declarator:
10007 ptr-operator abstract-declarator [opt]
10008 direct-abstract-declarator
10013 attributes [opt] direct-declarator
10014 attributes [opt] ptr-operator declarator
10016 abstract-declarator:
10017 attributes [opt] ptr-operator abstract-declarator [opt]
10018 attributes [opt] direct-abstract-declarator
10020 Returns a representation of the declarator. If the declarator has
10021 the form `* declarator', then an INDIRECT_REF is returned, whose
10022 only operand is the sub-declarator. Analagously, `& declarator' is
10023 represented as an ADDR_EXPR. For `X::* declarator', a SCOPE_REF is
10024 used. The first operand is the TYPE for `X'. The second operand
10025 is an INDIRECT_REF whose operand is the sub-declarator.
10027 Otherwise, the reprsentation is as for a direct-declarator.
10029 (It would be better to define a structure type to represent
10030 declarators, rather than abusing `tree' nodes to represent
10031 declarators. That would be much clearer and save some memory.
10032 There is no reason for declarators to be garbage-collected, for
10033 example; they are created during parser and no longer needed after
10034 `grokdeclarator' has been called.)
10036 For a ptr-operator that has the optional cv-qualifier-seq,
10037 cv-qualifiers will be stored in the TREE_TYPE of the INDIRECT_REF
10040 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is set to
10041 true if this declarator represents a constructor, destructor, or
10042 type conversion operator. Otherwise, it is set to false.
10044 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
10045 a decl-specifier-seq unless it declares a constructor, destructor,
10046 or conversion. It might seem that we could check this condition in
10047 semantic analysis, rather than parsing, but that makes it difficult
10048 to handle something like `f()'. We want to notice that there are
10049 no decl-specifiers, and therefore realize that this is an
10050 expression, not a declaration.) */
10053 cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p)
10055 cp_parser_declarator_kind dcl_kind;
10056 bool *ctor_dtor_or_conv_p;
10060 enum tree_code code;
10061 tree cv_qualifier_seq;
10063 tree attributes = NULL_TREE;
10065 /* Assume this is not a constructor, destructor, or type-conversion
10067 if (ctor_dtor_or_conv_p)
10068 *ctor_dtor_or_conv_p = false;
10070 if (cp_parser_allow_gnu_extensions_p (parser))
10071 attributes = cp_parser_attributes_opt (parser);
10073 /* Peek at the next token. */
10074 token = cp_lexer_peek_token (parser->lexer);
10076 /* Check for the ptr-operator production. */
10077 cp_parser_parse_tentatively (parser);
10078 /* Parse the ptr-operator. */
10079 code = cp_parser_ptr_operator (parser,
10081 &cv_qualifier_seq);
10082 /* If that worked, then we have a ptr-operator. */
10083 if (cp_parser_parse_definitely (parser))
10085 /* The dependent declarator is optional if we are parsing an
10086 abstract-declarator. */
10087 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
10088 cp_parser_parse_tentatively (parser);
10090 /* Parse the dependent declarator. */
10091 declarator = cp_parser_declarator (parser, dcl_kind,
10092 /*ctor_dtor_or_conv_p=*/NULL);
10094 /* If we are parsing an abstract-declarator, we must handle the
10095 case where the dependent declarator is absent. */
10096 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
10097 && !cp_parser_parse_definitely (parser))
10098 declarator = NULL_TREE;
10100 /* Build the representation of the ptr-operator. */
10101 if (code == INDIRECT_REF)
10102 declarator = make_pointer_declarator (cv_qualifier_seq,
10105 declarator = make_reference_declarator (cv_qualifier_seq,
10107 /* Handle the pointer-to-member case. */
10109 declarator = build_nt (SCOPE_REF, class_type, declarator);
10111 /* Everything else is a direct-declarator. */
10113 declarator = cp_parser_direct_declarator (parser,
10115 ctor_dtor_or_conv_p);
10117 if (attributes && declarator != error_mark_node)
10118 declarator = tree_cons (attributes, declarator, NULL_TREE);
10123 /* Parse a direct-declarator or direct-abstract-declarator.
10127 direct-declarator ( parameter-declaration-clause )
10128 cv-qualifier-seq [opt]
10129 exception-specification [opt]
10130 direct-declarator [ constant-expression [opt] ]
10133 direct-abstract-declarator:
10134 direct-abstract-declarator [opt]
10135 ( parameter-declaration-clause )
10136 cv-qualifier-seq [opt]
10137 exception-specification [opt]
10138 direct-abstract-declarator [opt] [ constant-expression [opt] ]
10139 ( abstract-declarator )
10141 Returns a representation of the declarator. DCL_KIND is
10142 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
10143 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
10144 we are parsing a direct-declarator. It is
10145 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
10146 of ambiguity we prefer an abstract declarator, as per
10147 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P is as for
10148 cp_parser_declarator.
10150 For the declarator-id production, the representation is as for an
10151 id-expression, except that a qualified name is represented as a
10152 SCOPE_REF. A function-declarator is represented as a CALL_EXPR;
10153 see the documentation of the FUNCTION_DECLARATOR_* macros for
10154 information about how to find the various declarator components.
10155 An array-declarator is represented as an ARRAY_REF. The
10156 direct-declarator is the first operand; the constant-expression
10157 indicating the size of the array is the second operand. */
10160 cp_parser_direct_declarator (parser, dcl_kind, ctor_dtor_or_conv_p)
10162 cp_parser_declarator_kind dcl_kind;
10163 bool *ctor_dtor_or_conv_p;
10166 tree declarator = NULL_TREE;
10167 tree scope = NULL_TREE;
10168 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
10169 bool saved_in_declarator_p = parser->in_declarator_p;
10174 /* Peek at the next token. */
10175 token = cp_lexer_peek_token (parser->lexer);
10176 if (token->type == CPP_OPEN_PAREN)
10178 /* This is either a parameter-declaration-clause, or a
10179 parenthesized declarator. When we know we are parsing a
10180 named declaratory, it must be a paranthesized declarator
10181 if FIRST is true. For instance, `(int)' is a
10182 parameter-declaration-clause, with an omitted
10183 direct-abstract-declarator. But `((*))', is a
10184 parenthesized abstract declarator. Finally, when T is a
10185 template parameter `(T)' is a
10186 paremeter-declaration-clause, and not a parenthesized
10189 We first try and parse a parameter-declaration-clause,
10190 and then try a nested declarator (if FIRST is true).
10192 It is not an error for it not to be a
10193 parameter-declaration-clause, even when FIRST is
10199 The first is the declaration of a function while the
10200 second is a the definition of a variable, including its
10203 Having seen only the parenthesis, we cannot know which of
10204 these two alternatives should be selected. Even more
10205 complex are examples like:
10210 The former is a function-declaration; the latter is a
10211 variable initialization.
10213 Thus again, we try a parameter-declation-clause, and if
10214 that fails, we back out and return. */
10216 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
10220 cp_parser_parse_tentatively (parser);
10222 /* Consume the `('. */
10223 cp_lexer_consume_token (parser->lexer);
10226 /* If this is going to be an abstract declarator, we're
10227 in a declarator and we can't have default args. */
10228 parser->default_arg_ok_p = false;
10229 parser->in_declarator_p = true;
10232 /* Parse the parameter-declaration-clause. */
10233 params = cp_parser_parameter_declaration_clause (parser);
10235 /* If all went well, parse the cv-qualifier-seq and the
10236 exception-specfication. */
10237 if (cp_parser_parse_definitely (parser))
10239 tree cv_qualifiers;
10240 tree exception_specification;
10243 /* Consume the `)'. */
10244 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
10246 /* Parse the cv-qualifier-seq. */
10247 cv_qualifiers = cp_parser_cv_qualifier_seq_opt (parser);
10248 /* And the exception-specification. */
10249 exception_specification
10250 = cp_parser_exception_specification_opt (parser);
10252 /* Create the function-declarator. */
10253 declarator = make_call_declarator (declarator,
10256 exception_specification);
10257 /* Any subsequent parameter lists are to do with
10258 return type, so are not those of the declared
10260 parser->default_arg_ok_p = false;
10262 /* Repeat the main loop. */
10267 /* If this is the first, we can try a parenthesized
10271 parser->default_arg_ok_p = saved_default_arg_ok_p;
10272 parser->in_declarator_p = saved_in_declarator_p;
10274 /* Consume the `('. */
10275 cp_lexer_consume_token (parser->lexer);
10276 /* Parse the nested declarator. */
10278 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p);
10280 /* Expect a `)'. */
10281 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
10282 declarator = error_mark_node;
10283 if (declarator == error_mark_node)
10286 goto handle_declarator;
10288 /* Otherwise, we must be done. */
10292 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
10293 && token->type == CPP_OPEN_SQUARE)
10295 /* Parse an array-declarator. */
10299 parser->default_arg_ok_p = false;
10300 parser->in_declarator_p = true;
10301 /* Consume the `['. */
10302 cp_lexer_consume_token (parser->lexer);
10303 /* Peek at the next token. */
10304 token = cp_lexer_peek_token (parser->lexer);
10305 /* If the next token is `]', then there is no
10306 constant-expression. */
10307 if (token->type != CPP_CLOSE_SQUARE)
10308 bounds = cp_parser_constant_expression (parser);
10310 bounds = NULL_TREE;
10311 /* Look for the closing `]'. */
10312 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'"))
10314 declarator = error_mark_node;
10318 declarator = build_nt (ARRAY_REF, declarator, bounds);
10320 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
10322 /* Parse a declarator_id */
10323 if (dcl_kind == CP_PARSER_DECLARATOR_EITHER)
10324 cp_parser_parse_tentatively (parser);
10325 declarator = cp_parser_declarator_id (parser);
10326 if (dcl_kind == CP_PARSER_DECLARATOR_EITHER
10327 && !cp_parser_parse_definitely (parser))
10328 declarator = error_mark_node;
10329 if (declarator == error_mark_node)
10332 if (TREE_CODE (declarator) == SCOPE_REF)
10334 tree scope = TREE_OPERAND (declarator, 0);
10336 /* In the declaration of a member of a template class
10337 outside of the class itself, the SCOPE will sometimes
10338 be a TYPENAME_TYPE. For example, given:
10340 template <typename T>
10341 int S<T>::R::i = 3;
10343 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
10344 this context, we must resolve S<T>::R to an ordinary
10345 type, rather than a typename type.
10347 The reason we normally avoid resolving TYPENAME_TYPEs
10348 is that a specialization of `S' might render
10349 `S<T>::R' not a type. However, if `S' is
10350 specialized, then this `i' will not be used, so there
10351 is no harm in resolving the types here. */
10352 if (TREE_CODE (scope) == TYPENAME_TYPE)
10354 /* Resolve the TYPENAME_TYPE. */
10355 scope = cp_parser_resolve_typename_type (parser, scope);
10356 /* If that failed, the declarator is invalid. */
10357 if (scope == error_mark_node)
10358 return error_mark_node;
10359 /* Build a new DECLARATOR. */
10360 declarator = build_nt (SCOPE_REF,
10362 TREE_OPERAND (declarator, 1));
10366 /* Check to see whether the declarator-id names a constructor,
10367 destructor, or conversion. */
10368 if (declarator && ctor_dtor_or_conv_p
10369 && ((TREE_CODE (declarator) == SCOPE_REF
10370 && CLASS_TYPE_P (TREE_OPERAND (declarator, 0)))
10371 || (TREE_CODE (declarator) != SCOPE_REF
10372 && at_class_scope_p ())))
10374 tree unqualified_name;
10377 /* Get the unqualified part of the name. */
10378 if (TREE_CODE (declarator) == SCOPE_REF)
10380 class_type = TREE_OPERAND (declarator, 0);
10381 unqualified_name = TREE_OPERAND (declarator, 1);
10385 class_type = current_class_type;
10386 unqualified_name = declarator;
10389 /* See if it names ctor, dtor or conv. */
10390 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR
10391 || IDENTIFIER_TYPENAME_P (unqualified_name)
10392 || constructor_name_p (unqualified_name, class_type))
10393 *ctor_dtor_or_conv_p = true;
10396 handle_declarator:;
10397 scope = get_scope_of_declarator (declarator);
10399 /* Any names that appear after the declarator-id for a member
10400 are looked up in the containing scope. */
10401 push_scope (scope);
10402 parser->in_declarator_p = true;
10403 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
10405 && (TREE_CODE (declarator) == SCOPE_REF
10406 || TREE_CODE (declarator) == IDENTIFIER_NODE)))
10407 /* Default args are only allowed on function
10409 parser->default_arg_ok_p = saved_default_arg_ok_p;
10411 parser->default_arg_ok_p = false;
10420 /* For an abstract declarator, we might wind up with nothing at this
10421 point. That's an error; the declarator is not optional. */
10423 cp_parser_error (parser, "expected declarator");
10425 /* If we entered a scope, we must exit it now. */
10429 parser->default_arg_ok_p = saved_default_arg_ok_p;
10430 parser->in_declarator_p = saved_in_declarator_p;
10435 /* Parse a ptr-operator.
10438 * cv-qualifier-seq [opt]
10440 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
10445 & cv-qualifier-seq [opt]
10447 Returns INDIRECT_REF if a pointer, or pointer-to-member, was
10448 used. Returns ADDR_EXPR if a reference was used. In the
10449 case of a pointer-to-member, *TYPE is filled in with the
10450 TYPE containing the member. *CV_QUALIFIER_SEQ is filled in
10451 with the cv-qualifier-seq, or NULL_TREE, if there are no
10452 cv-qualifiers. Returns ERROR_MARK if an error occurred. */
10454 static enum tree_code
10455 cp_parser_ptr_operator (parser, type, cv_qualifier_seq)
10458 tree *cv_qualifier_seq;
10460 enum tree_code code = ERROR_MARK;
10463 /* Assume that it's not a pointer-to-member. */
10465 /* And that there are no cv-qualifiers. */
10466 *cv_qualifier_seq = NULL_TREE;
10468 /* Peek at the next token. */
10469 token = cp_lexer_peek_token (parser->lexer);
10470 /* If it's a `*' or `&' we have a pointer or reference. */
10471 if (token->type == CPP_MULT || token->type == CPP_AND)
10473 /* Remember which ptr-operator we were processing. */
10474 code = (token->type == CPP_AND ? ADDR_EXPR : INDIRECT_REF);
10476 /* Consume the `*' or `&'. */
10477 cp_lexer_consume_token (parser->lexer);
10479 /* A `*' can be followed by a cv-qualifier-seq, and so can a
10480 `&', if we are allowing GNU extensions. (The only qualifier
10481 that can legally appear after `&' is `restrict', but that is
10482 enforced during semantic analysis. */
10483 if (code == INDIRECT_REF
10484 || cp_parser_allow_gnu_extensions_p (parser))
10485 *cv_qualifier_seq = cp_parser_cv_qualifier_seq_opt (parser);
10489 /* Try the pointer-to-member case. */
10490 cp_parser_parse_tentatively (parser);
10491 /* Look for the optional `::' operator. */
10492 cp_parser_global_scope_opt (parser,
10493 /*current_scope_valid_p=*/false);
10494 /* Look for the nested-name specifier. */
10495 cp_parser_nested_name_specifier (parser,
10496 /*typename_keyword_p=*/false,
10497 /*check_dependency_p=*/true,
10499 /* If we found it, and the next token is a `*', then we are
10500 indeed looking at a pointer-to-member operator. */
10501 if (!cp_parser_error_occurred (parser)
10502 && cp_parser_require (parser, CPP_MULT, "`*'"))
10504 /* The type of which the member is a member is given by the
10506 *type = parser->scope;
10507 /* The next name will not be qualified. */
10508 parser->scope = NULL_TREE;
10509 parser->qualifying_scope = NULL_TREE;
10510 parser->object_scope = NULL_TREE;
10511 /* Indicate that the `*' operator was used. */
10512 code = INDIRECT_REF;
10513 /* Look for the optional cv-qualifier-seq. */
10514 *cv_qualifier_seq = cp_parser_cv_qualifier_seq_opt (parser);
10516 /* If that didn't work we don't have a ptr-operator. */
10517 if (!cp_parser_parse_definitely (parser))
10518 cp_parser_error (parser, "expected ptr-operator");
10524 /* Parse an (optional) cv-qualifier-seq.
10527 cv-qualifier cv-qualifier-seq [opt]
10529 Returns a TREE_LIST. The TREE_VALUE of each node is the
10530 representation of a cv-qualifier. */
10533 cp_parser_cv_qualifier_seq_opt (parser)
10536 tree cv_qualifiers = NULL_TREE;
10542 /* Look for the next cv-qualifier. */
10543 cv_qualifier = cp_parser_cv_qualifier_opt (parser);
10544 /* If we didn't find one, we're done. */
10548 /* Add this cv-qualifier to the list. */
10550 = tree_cons (NULL_TREE, cv_qualifier, cv_qualifiers);
10553 /* We built up the list in reverse order. */
10554 return nreverse (cv_qualifiers);
10557 /* Parse an (optional) cv-qualifier.
10569 cp_parser_cv_qualifier_opt (parser)
10573 tree cv_qualifier = NULL_TREE;
10575 /* Peek at the next token. */
10576 token = cp_lexer_peek_token (parser->lexer);
10577 /* See if it's a cv-qualifier. */
10578 switch (token->keyword)
10583 /* Save the value of the token. */
10584 cv_qualifier = token->value;
10585 /* Consume the token. */
10586 cp_lexer_consume_token (parser->lexer);
10593 return cv_qualifier;
10596 /* Parse a declarator-id.
10600 :: [opt] nested-name-specifier [opt] type-name
10602 In the `id-expression' case, the value returned is as for
10603 cp_parser_id_expression if the id-expression was an unqualified-id.
10604 If the id-expression was a qualified-id, then a SCOPE_REF is
10605 returned. The first operand is the scope (either a NAMESPACE_DECL
10606 or TREE_TYPE), but the second is still just a representation of an
10610 cp_parser_declarator_id (parser)
10613 tree id_expression;
10615 /* The expression must be an id-expression. Assume that qualified
10616 names are the names of types so that:
10619 int S<T>::R::i = 3;
10621 will work; we must treat `S<T>::R' as the name of a type.
10622 Similarly, assume that qualified names are templates, where
10626 int S<T>::R<T>::i = 3;
10629 id_expression = cp_parser_id_expression (parser,
10630 /*template_keyword_p=*/false,
10631 /*check_dependency_p=*/false,
10632 /*template_p=*/NULL);
10633 /* If the name was qualified, create a SCOPE_REF to represent
10636 id_expression = build_nt (SCOPE_REF, parser->scope, id_expression);
10638 return id_expression;
10641 /* Parse a type-id.
10644 type-specifier-seq abstract-declarator [opt]
10646 Returns the TYPE specified. */
10649 cp_parser_type_id (parser)
10652 tree type_specifier_seq;
10653 tree abstract_declarator;
10655 /* Parse the type-specifier-seq. */
10657 = cp_parser_type_specifier_seq (parser);
10658 if (type_specifier_seq == error_mark_node)
10659 return error_mark_node;
10661 /* There might or might not be an abstract declarator. */
10662 cp_parser_parse_tentatively (parser);
10663 /* Look for the declarator. */
10664 abstract_declarator
10665 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL);
10666 /* Check to see if there really was a declarator. */
10667 if (!cp_parser_parse_definitely (parser))
10668 abstract_declarator = NULL_TREE;
10670 return groktypename (build_tree_list (type_specifier_seq,
10671 abstract_declarator));
10674 /* Parse a type-specifier-seq.
10676 type-specifier-seq:
10677 type-specifier type-specifier-seq [opt]
10681 type-specifier-seq:
10682 attributes type-specifier-seq [opt]
10684 Returns a TREE_LIST. Either the TREE_VALUE of each node is a
10685 type-specifier, or the TREE_PURPOSE is a list of attributes. */
10688 cp_parser_type_specifier_seq (parser)
10691 bool seen_type_specifier = false;
10692 tree type_specifier_seq = NULL_TREE;
10694 /* Parse the type-specifiers and attributes. */
10697 tree type_specifier;
10699 /* Check for attributes first. */
10700 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
10702 type_specifier_seq = tree_cons (cp_parser_attributes_opt (parser),
10704 type_specifier_seq);
10708 /* After the first type-specifier, others are optional. */
10709 if (seen_type_specifier)
10710 cp_parser_parse_tentatively (parser);
10711 /* Look for the type-specifier. */
10712 type_specifier = cp_parser_type_specifier (parser,
10713 CP_PARSER_FLAGS_NONE,
10714 /*is_friend=*/false,
10715 /*is_declaration=*/false,
10718 /* If the first type-specifier could not be found, this is not a
10719 type-specifier-seq at all. */
10720 if (!seen_type_specifier && type_specifier == error_mark_node)
10721 return error_mark_node;
10722 /* If subsequent type-specifiers could not be found, the
10723 type-specifier-seq is complete. */
10724 else if (seen_type_specifier && !cp_parser_parse_definitely (parser))
10727 /* Add the new type-specifier to the list. */
10729 = tree_cons (NULL_TREE, type_specifier, type_specifier_seq);
10730 seen_type_specifier = true;
10733 /* We built up the list in reverse order. */
10734 return nreverse (type_specifier_seq);
10737 /* Parse a parameter-declaration-clause.
10739 parameter-declaration-clause:
10740 parameter-declaration-list [opt] ... [opt]
10741 parameter-declaration-list , ...
10743 Returns a representation for the parameter declarations. Each node
10744 is a TREE_LIST. (See cp_parser_parameter_declaration for the exact
10745 representation.) If the parameter-declaration-clause ends with an
10746 ellipsis, PARMLIST_ELLIPSIS_P will hold of the first node in the
10747 list. A return value of NULL_TREE indicates a
10748 parameter-declaration-clause consisting only of an ellipsis. */
10751 cp_parser_parameter_declaration_clause (parser)
10758 /* Peek at the next token. */
10759 token = cp_lexer_peek_token (parser->lexer);
10760 /* Check for trivial parameter-declaration-clauses. */
10761 if (token->type == CPP_ELLIPSIS)
10763 /* Consume the `...' token. */
10764 cp_lexer_consume_token (parser->lexer);
10767 else if (token->type == CPP_CLOSE_PAREN)
10768 /* There are no parameters. */
10770 #ifndef NO_IMPLICIT_EXTERN_C
10771 if (in_system_header && current_class_type == NULL
10772 && current_lang_name == lang_name_c)
10776 return void_list_node;
10778 /* Check for `(void)', too, which is a special case. */
10779 else if (token->keyword == RID_VOID
10780 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
10781 == CPP_CLOSE_PAREN))
10783 /* Consume the `void' token. */
10784 cp_lexer_consume_token (parser->lexer);
10785 /* There are no parameters. */
10786 return void_list_node;
10789 /* Parse the parameter-declaration-list. */
10790 parameters = cp_parser_parameter_declaration_list (parser);
10791 /* If a parse error occurred while parsing the
10792 parameter-declaration-list, then the entire
10793 parameter-declaration-clause is erroneous. */
10794 if (parameters == error_mark_node)
10795 return error_mark_node;
10797 /* Peek at the next token. */
10798 token = cp_lexer_peek_token (parser->lexer);
10799 /* If it's a `,', the clause should terminate with an ellipsis. */
10800 if (token->type == CPP_COMMA)
10802 /* Consume the `,'. */
10803 cp_lexer_consume_token (parser->lexer);
10804 /* Expect an ellipsis. */
10806 = (cp_parser_require (parser, CPP_ELLIPSIS, "`...'") != NULL);
10808 /* It might also be `...' if the optional trailing `,' was
10810 else if (token->type == CPP_ELLIPSIS)
10812 /* Consume the `...' token. */
10813 cp_lexer_consume_token (parser->lexer);
10814 /* And remember that we saw it. */
10818 ellipsis_p = false;
10820 /* Finish the parameter list. */
10821 return finish_parmlist (parameters, ellipsis_p);
10824 /* Parse a parameter-declaration-list.
10826 parameter-declaration-list:
10827 parameter-declaration
10828 parameter-declaration-list , parameter-declaration
10830 Returns a representation of the parameter-declaration-list, as for
10831 cp_parser_parameter_declaration_clause. However, the
10832 `void_list_node' is never appended to the list. */
10835 cp_parser_parameter_declaration_list (parser)
10838 tree parameters = NULL_TREE;
10840 /* Look for more parameters. */
10844 /* Parse the parameter. */
10846 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/false);
10848 /* If a parse error ocurred parsing the parameter declaration,
10849 then the entire parameter-declaration-list is erroneous. */
10850 if (parameter == error_mark_node)
10852 parameters = error_mark_node;
10855 /* Add the new parameter to the list. */
10856 TREE_CHAIN (parameter) = parameters;
10857 parameters = parameter;
10859 /* Peek at the next token. */
10860 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
10861 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10862 /* The parameter-declaration-list is complete. */
10864 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
10868 /* Peek at the next token. */
10869 token = cp_lexer_peek_nth_token (parser->lexer, 2);
10870 /* If it's an ellipsis, then the list is complete. */
10871 if (token->type == CPP_ELLIPSIS)
10873 /* Otherwise, there must be more parameters. Consume the
10875 cp_lexer_consume_token (parser->lexer);
10879 cp_parser_error (parser, "expected `,' or `...'");
10884 /* We built up the list in reverse order; straighten it out now. */
10885 return nreverse (parameters);
10888 /* Parse a parameter declaration.
10890 parameter-declaration:
10891 decl-specifier-seq declarator
10892 decl-specifier-seq declarator = assignment-expression
10893 decl-specifier-seq abstract-declarator [opt]
10894 decl-specifier-seq abstract-declarator [opt] = assignment-expression
10896 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
10897 declares a template parameter. (In that case, a non-nested `>'
10898 token encountered during the parsing of the assignment-expression
10899 is not interpreted as a greater-than operator.)
10901 Returns a TREE_LIST representing the parameter-declaration. The
10902 TREE_VALUE is a representation of the decl-specifier-seq and
10903 declarator. In particular, the TREE_VALUE will be a TREE_LIST
10904 whose TREE_PURPOSE represents the decl-specifier-seq and whose
10905 TREE_VALUE represents the declarator. */
10908 cp_parser_parameter_declaration (cp_parser *parser,
10909 bool template_parm_p)
10911 bool declares_class_or_enum;
10912 bool greater_than_is_operator_p;
10913 tree decl_specifiers;
10916 tree default_argument;
10919 const char *saved_message;
10921 /* In a template parameter, `>' is not an operator.
10925 When parsing a default template-argument for a non-type
10926 template-parameter, the first non-nested `>' is taken as the end
10927 of the template parameter-list rather than a greater-than
10929 greater_than_is_operator_p = !template_parm_p;
10931 /* Type definitions may not appear in parameter types. */
10932 saved_message = parser->type_definition_forbidden_message;
10933 parser->type_definition_forbidden_message
10934 = "types may not be defined in parameter types";
10936 /* Parse the declaration-specifiers. */
10938 = cp_parser_decl_specifier_seq (parser,
10939 CP_PARSER_FLAGS_NONE,
10941 &declares_class_or_enum);
10942 /* If an error occurred, there's no reason to attempt to parse the
10943 rest of the declaration. */
10944 if (cp_parser_error_occurred (parser))
10946 parser->type_definition_forbidden_message = saved_message;
10947 return error_mark_node;
10950 /* Peek at the next token. */
10951 token = cp_lexer_peek_token (parser->lexer);
10952 /* If the next token is a `)', `,', `=', `>', or `...', then there
10953 is no declarator. */
10954 if (token->type == CPP_CLOSE_PAREN
10955 || token->type == CPP_COMMA
10956 || token->type == CPP_EQ
10957 || token->type == CPP_ELLIPSIS
10958 || token->type == CPP_GREATER)
10959 declarator = NULL_TREE;
10960 /* Otherwise, there should be a declarator. */
10963 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
10964 parser->default_arg_ok_p = false;
10966 declarator = cp_parser_declarator (parser,
10967 CP_PARSER_DECLARATOR_EITHER,
10968 /*ctor_dtor_or_conv_p=*/NULL);
10969 parser->default_arg_ok_p = saved_default_arg_ok_p;
10970 /* After the declarator, allow more attributes. */
10971 attributes = chainon (attributes, cp_parser_attributes_opt (parser));
10974 /* The restriction on defining new types applies only to the type
10975 of the parameter, not to the default argument. */
10976 parser->type_definition_forbidden_message = saved_message;
10978 /* If the next token is `=', then process a default argument. */
10979 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10981 bool saved_greater_than_is_operator_p;
10982 /* Consume the `='. */
10983 cp_lexer_consume_token (parser->lexer);
10985 /* If we are defining a class, then the tokens that make up the
10986 default argument must be saved and processed later. */
10987 if (!template_parm_p && at_class_scope_p ()
10988 && TYPE_BEING_DEFINED (current_class_type))
10990 unsigned depth = 0;
10992 /* Create a DEFAULT_ARG to represented the unparsed default
10994 default_argument = make_node (DEFAULT_ARG);
10995 DEFARG_TOKENS (default_argument) = cp_token_cache_new ();
10997 /* Add tokens until we have processed the entire default
11004 /* Peek at the next token. */
11005 token = cp_lexer_peek_token (parser->lexer);
11006 /* What we do depends on what token we have. */
11007 switch (token->type)
11009 /* In valid code, a default argument must be
11010 immediately followed by a `,' `)', or `...'. */
11012 case CPP_CLOSE_PAREN:
11014 /* If we run into a non-nested `;', `}', or `]',
11015 then the code is invalid -- but the default
11016 argument is certainly over. */
11017 case CPP_SEMICOLON:
11018 case CPP_CLOSE_BRACE:
11019 case CPP_CLOSE_SQUARE:
11022 /* Update DEPTH, if necessary. */
11023 else if (token->type == CPP_CLOSE_PAREN
11024 || token->type == CPP_CLOSE_BRACE
11025 || token->type == CPP_CLOSE_SQUARE)
11029 case CPP_OPEN_PAREN:
11030 case CPP_OPEN_SQUARE:
11031 case CPP_OPEN_BRACE:
11036 /* If we see a non-nested `>', and `>' is not an
11037 operator, then it marks the end of the default
11039 if (!depth && !greater_than_is_operator_p)
11043 /* If we run out of tokens, issue an error message. */
11045 error ("file ends in default argument");
11051 /* In these cases, we should look for template-ids.
11052 For example, if the default argument is
11053 `X<int, double>()', we need to do name lookup to
11054 figure out whether or not `X' is a template; if
11055 so, the `,' does not end the deault argument.
11057 That is not yet done. */
11064 /* If we've reached the end, stop. */
11068 /* Add the token to the token block. */
11069 token = cp_lexer_consume_token (parser->lexer);
11070 cp_token_cache_push_token (DEFARG_TOKENS (default_argument),
11074 /* Outside of a class definition, we can just parse the
11075 assignment-expression. */
11078 bool saved_local_variables_forbidden_p;
11080 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
11082 saved_greater_than_is_operator_p
11083 = parser->greater_than_is_operator_p;
11084 parser->greater_than_is_operator_p = greater_than_is_operator_p;
11085 /* Local variable names (and the `this' keyword) may not
11086 appear in a default argument. */
11087 saved_local_variables_forbidden_p
11088 = parser->local_variables_forbidden_p;
11089 parser->local_variables_forbidden_p = true;
11090 /* Parse the assignment-expression. */
11091 default_argument = cp_parser_assignment_expression (parser);
11092 /* Restore saved state. */
11093 parser->greater_than_is_operator_p
11094 = saved_greater_than_is_operator_p;
11095 parser->local_variables_forbidden_p
11096 = saved_local_variables_forbidden_p;
11098 if (!parser->default_arg_ok_p)
11100 pedwarn ("default arguments are only permitted on functions");
11101 if (flag_pedantic_errors)
11102 default_argument = NULL_TREE;
11106 default_argument = NULL_TREE;
11108 /* Create the representation of the parameter. */
11110 decl_specifiers = tree_cons (attributes, NULL_TREE, decl_specifiers);
11111 parameter = build_tree_list (default_argument,
11112 build_tree_list (decl_specifiers,
11118 /* Parse a function-definition.
11120 function-definition:
11121 decl-specifier-seq [opt] declarator ctor-initializer [opt]
11123 decl-specifier-seq [opt] declarator function-try-block
11127 function-definition:
11128 __extension__ function-definition
11130 Returns the FUNCTION_DECL for the function. If FRIEND_P is
11131 non-NULL, *FRIEND_P is set to TRUE iff the function was declared to
11135 cp_parser_function_definition (parser, friend_p)
11139 tree decl_specifiers;
11143 tree access_checks;
11145 bool declares_class_or_enum;
11147 /* The saved value of the PEDANTIC flag. */
11148 int saved_pedantic;
11150 /* Any pending qualification must be cleared by our caller. It is
11151 more robust to force the callers to clear PARSER->SCOPE than to
11152 do it here since if the qualification is in effect here, it might
11153 also end up in effect elsewhere that it is not intended. */
11154 my_friendly_assert (!parser->scope, 20010821);
11156 /* Handle `__extension__'. */
11157 if (cp_parser_extension_opt (parser, &saved_pedantic))
11159 /* Parse the function-definition. */
11160 fn = cp_parser_function_definition (parser, friend_p);
11161 /* Restore the PEDANTIC flag. */
11162 pedantic = saved_pedantic;
11167 /* Check to see if this definition appears in a class-specifier. */
11168 member_p = (at_class_scope_p ()
11169 && TYPE_BEING_DEFINED (current_class_type));
11170 /* Defer access checks in the decl-specifier-seq until we know what
11171 function is being defined. There is no need to do this for the
11172 definition of member functions; we cannot be defining a member
11173 from another class. */
11175 cp_parser_start_deferring_access_checks (parser);
11176 /* Parse the decl-specifier-seq. */
11178 = cp_parser_decl_specifier_seq (parser,
11179 CP_PARSER_FLAGS_OPTIONAL,
11181 &declares_class_or_enum);
11182 /* Figure out whether this declaration is a `friend'. */
11184 *friend_p = cp_parser_friend_p (decl_specifiers);
11186 /* Parse the declarator. */
11187 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
11188 /*ctor_dtor_or_conv_p=*/NULL);
11190 /* Gather up any access checks that occurred. */
11192 access_checks = cp_parser_stop_deferring_access_checks (parser);
11194 access_checks = NULL_TREE;
11196 /* If something has already gone wrong, we may as well stop now. */
11197 if (declarator == error_mark_node)
11199 /* Skip to the end of the function, or if this wasn't anything
11200 like a function-definition, to a `;' in the hopes of finding
11201 a sensible place from which to continue parsing. */
11202 cp_parser_skip_to_end_of_block_or_statement (parser);
11203 return error_mark_node;
11206 /* The next character should be a `{' (for a simple function
11207 definition), a `:' (for a ctor-initializer), or `try' (for a
11208 function-try block). */
11209 token = cp_lexer_peek_token (parser->lexer);
11210 if (!cp_parser_token_starts_function_definition_p (token))
11212 /* Issue the error-message. */
11213 cp_parser_error (parser, "expected function-definition");
11214 /* Skip to the next `;'. */
11215 cp_parser_skip_to_end_of_block_or_statement (parser);
11217 return error_mark_node;
11220 /* If we are in a class scope, then we must handle
11221 function-definitions specially. In particular, we save away the
11222 tokens that make up the function body, and parse them again
11223 later, in order to handle code like:
11226 int f () { return i; }
11230 Here, we cannot parse the body of `f' until after we have seen
11231 the declaration of `i'. */
11234 cp_token_cache *cache;
11236 /* Create the function-declaration. */
11237 fn = start_method (decl_specifiers, declarator, attributes);
11238 /* If something went badly wrong, bail out now. */
11239 if (fn == error_mark_node)
11241 /* If there's a function-body, skip it. */
11242 if (cp_parser_token_starts_function_definition_p
11243 (cp_lexer_peek_token (parser->lexer)))
11244 cp_parser_skip_to_end_of_block_or_statement (parser);
11245 return error_mark_node;
11248 /* Create a token cache. */
11249 cache = cp_token_cache_new ();
11250 /* Save away the tokens that make up the body of the
11252 cp_parser_cache_group (parser, cache, CPP_CLOSE_BRACE, /*depth=*/0);
11253 /* Handle function try blocks. */
11254 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
11255 cp_parser_cache_group (parser, cache, CPP_CLOSE_BRACE, /*depth=*/0);
11257 /* Save away the inline definition; we will process it when the
11258 class is complete. */
11259 DECL_PENDING_INLINE_INFO (fn) = cache;
11260 DECL_PENDING_INLINE_P (fn) = 1;
11262 /* We're done with the inline definition. */
11263 finish_method (fn);
11265 /* Add FN to the queue of functions to be parsed later. */
11266 TREE_VALUE (parser->unparsed_functions_queues)
11267 = tree_cons (NULL_TREE, fn,
11268 TREE_VALUE (parser->unparsed_functions_queues));
11273 /* Check that the number of template-parameter-lists is OK. */
11274 if (!cp_parser_check_declarator_template_parameters (parser,
11277 cp_parser_skip_to_end_of_block_or_statement (parser);
11278 return error_mark_node;
11281 return (cp_parser_function_definition_from_specifiers_and_declarator
11282 (parser, decl_specifiers, attributes, declarator, access_checks));
11285 /* Parse a function-body.
11288 compound_statement */
11291 cp_parser_function_body (cp_parser *parser)
11293 cp_parser_compound_statement (parser);
11296 /* Parse a ctor-initializer-opt followed by a function-body. Return
11297 true if a ctor-initializer was present. */
11300 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
11303 bool ctor_initializer_p;
11305 /* Begin the function body. */
11306 body = begin_function_body ();
11307 /* Parse the optional ctor-initializer. */
11308 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
11309 /* Parse the function-body. */
11310 cp_parser_function_body (parser);
11311 /* Finish the function body. */
11312 finish_function_body (body);
11314 return ctor_initializer_p;
11317 /* Parse an initializer.
11320 = initializer-clause
11321 ( expression-list )
11323 Returns a expression representing the initializer. If no
11324 initializer is present, NULL_TREE is returned.
11326 *IS_PARENTHESIZED_INIT is set to TRUE if the `( expression-list )'
11327 production is used, and zero otherwise. *IS_PARENTHESIZED_INIT is
11328 set to FALSE if there is no initializer present. */
11331 cp_parser_initializer (parser, is_parenthesized_init)
11333 bool *is_parenthesized_init;
11338 /* Peek at the next token. */
11339 token = cp_lexer_peek_token (parser->lexer);
11341 /* Let our caller know whether or not this initializer was
11343 *is_parenthesized_init = (token->type == CPP_OPEN_PAREN);
11345 if (token->type == CPP_EQ)
11347 /* Consume the `='. */
11348 cp_lexer_consume_token (parser->lexer);
11349 /* Parse the initializer-clause. */
11350 init = cp_parser_initializer_clause (parser);
11352 else if (token->type == CPP_OPEN_PAREN)
11354 /* Consume the `('. */
11355 cp_lexer_consume_token (parser->lexer);
11356 /* Parse the expression-list. */
11357 init = cp_parser_expression_list (parser);
11358 /* Consume the `)' token. */
11359 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
11360 cp_parser_skip_to_closing_parenthesis (parser);
11364 /* Anything else is an error. */
11365 cp_parser_error (parser, "expected initializer");
11366 init = error_mark_node;
11372 /* Parse an initializer-clause.
11374 initializer-clause:
11375 assignment-expression
11376 { initializer-list , [opt] }
11379 Returns an expression representing the initializer.
11381 If the `assignment-expression' production is used the value
11382 returned is simply a reprsentation for the expression.
11384 Otherwise, a CONSTRUCTOR is returned. The CONSTRUCTOR_ELTS will be
11385 the elements of the initializer-list (or NULL_TREE, if the last
11386 production is used). The TREE_TYPE for the CONSTRUCTOR will be
11387 NULL_TREE. There is no way to detect whether or not the optional
11388 trailing `,' was provided. */
11391 cp_parser_initializer_clause (parser)
11396 /* If it is not a `{', then we are looking at an
11397 assignment-expression. */
11398 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
11399 initializer = cp_parser_assignment_expression (parser);
11402 /* Consume the `{' token. */
11403 cp_lexer_consume_token (parser->lexer);
11404 /* Create a CONSTRUCTOR to represent the braced-initializer. */
11405 initializer = make_node (CONSTRUCTOR);
11406 /* Mark it with TREE_HAS_CONSTRUCTOR. This should not be
11407 necessary, but check_initializer depends upon it, for
11409 TREE_HAS_CONSTRUCTOR (initializer) = 1;
11410 /* If it's not a `}', then there is a non-trivial initializer. */
11411 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
11413 /* Parse the initializer list. */
11414 CONSTRUCTOR_ELTS (initializer)
11415 = cp_parser_initializer_list (parser);
11416 /* A trailing `,' token is allowed. */
11417 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
11418 cp_lexer_consume_token (parser->lexer);
11421 /* Now, there should be a trailing `}'. */
11422 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
11425 return initializer;
11428 /* Parse an initializer-list.
11432 initializer-list , initializer-clause
11437 identifier : initializer-clause
11438 initializer-list, identifier : initializer-clause
11440 Returns a TREE_LIST. The TREE_VALUE of each node is an expression
11441 for the initializer. If the TREE_PURPOSE is non-NULL, it is the
11442 IDENTIFIER_NODE naming the field to initialize. */
11445 cp_parser_initializer_list (parser)
11448 tree initializers = NULL_TREE;
11450 /* Parse the rest of the list. */
11457 /* If the next token is an identifier and the following one is a
11458 colon, we are looking at the GNU designated-initializer
11460 if (cp_parser_allow_gnu_extensions_p (parser)
11461 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
11462 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
11464 /* Consume the identifier. */
11465 identifier = cp_lexer_consume_token (parser->lexer)->value;
11466 /* Consume the `:'. */
11467 cp_lexer_consume_token (parser->lexer);
11470 identifier = NULL_TREE;
11472 /* Parse the initializer. */
11473 initializer = cp_parser_initializer_clause (parser);
11475 /* Add it to the list. */
11476 initializers = tree_cons (identifier, initializer, initializers);
11478 /* If the next token is not a comma, we have reached the end of
11480 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11483 /* Peek at the next token. */
11484 token = cp_lexer_peek_nth_token (parser->lexer, 2);
11485 /* If the next token is a `}', then we're still done. An
11486 initializer-clause can have a trailing `,' after the
11487 initializer-list and before the closing `}'. */
11488 if (token->type == CPP_CLOSE_BRACE)
11491 /* Consume the `,' token. */
11492 cp_lexer_consume_token (parser->lexer);
11495 /* The initializers were built up in reverse order, so we need to
11496 reverse them now. */
11497 return nreverse (initializers);
11500 /* Classes [gram.class] */
11502 /* Parse a class-name.
11508 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
11509 to indicate that names looked up in dependent types should be
11510 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
11511 keyword has been used to indicate that the name that appears next
11512 is a template. TYPE_P is true iff the next name should be treated
11513 as class-name, even if it is declared to be some other kind of name
11514 as well. The accessibility of the class-name is checked iff
11515 CHECK_ACCESS_P is true. If CHECK_DEPENDENCY_P is FALSE, names are
11516 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
11517 is the class being defined in a class-head.
11519 Returns the TYPE_DECL representing the class. */
11522 cp_parser_class_name (cp_parser *parser,
11523 bool typename_keyword_p,
11524 bool template_keyword_p,
11526 bool check_access_p,
11527 bool check_dependency_p,
11535 /* All class-names start with an identifier. */
11536 token = cp_lexer_peek_token (parser->lexer);
11537 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
11539 cp_parser_error (parser, "expected class-name");
11540 return error_mark_node;
11543 /* PARSER->SCOPE can be cleared when parsing the template-arguments
11544 to a template-id, so we save it here. */
11545 scope = parser->scope;
11546 /* Any name names a type if we're following the `typename' keyword
11547 in a qualified name where the enclosing scope is type-dependent. */
11548 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
11549 && cp_parser_dependent_type_p (scope));
11550 /* Handle the common case (an identifier, but not a template-id)
11552 if (token->type == CPP_NAME
11553 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_LESS)
11557 /* Look for the identifier. */
11558 identifier = cp_parser_identifier (parser);
11559 /* If the next token isn't an identifier, we are certainly not
11560 looking at a class-name. */
11561 if (identifier == error_mark_node)
11562 decl = error_mark_node;
11563 /* If we know this is a type-name, there's no need to look it
11565 else if (typename_p)
11569 /* If the next token is a `::', then the name must be a type
11572 [basic.lookup.qual]
11574 During the lookup for a name preceding the :: scope
11575 resolution operator, object, function, and enumerator
11576 names are ignored. */
11577 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
11579 /* Look up the name. */
11580 decl = cp_parser_lookup_name (parser, identifier,
11583 /*is_namespace=*/false,
11584 check_dependency_p);
11589 /* Try a template-id. */
11590 decl = cp_parser_template_id (parser, template_keyword_p,
11591 check_dependency_p);
11592 if (decl == error_mark_node)
11593 return error_mark_node;
11596 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
11598 /* If this is a typename, create a TYPENAME_TYPE. */
11599 if (typename_p && decl != error_mark_node)
11600 decl = TYPE_NAME (make_typename_type (scope, decl,
11603 /* Check to see that it is really the name of a class. */
11604 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
11605 && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
11606 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
11607 /* Situations like this:
11609 template <typename T> struct A {
11610 typename T::template X<int>::I i;
11613 are problematic. Is `T::template X<int>' a class-name? The
11614 standard does not seem to be definitive, but there is no other
11615 valid interpretation of the following `::'. Therefore, those
11616 names are considered class-names. */
11617 decl = TYPE_NAME (make_typename_type (scope, decl,
11618 tf_error | tf_parsing));
11619 else if (decl == error_mark_node
11620 || TREE_CODE (decl) != TYPE_DECL
11621 || !IS_AGGR_TYPE (TREE_TYPE (decl)))
11623 cp_parser_error (parser, "expected class-name");
11624 return error_mark_node;
11630 /* Parse a class-specifier.
11633 class-head { member-specification [opt] }
11635 Returns the TREE_TYPE representing the class. */
11638 cp_parser_class_specifier (parser)
11643 tree attributes = NULL_TREE;
11644 int has_trailing_semicolon;
11645 bool nested_name_specifier_p;
11646 bool deferring_access_checks_p;
11647 tree saved_access_checks;
11648 unsigned saved_num_template_parameter_lists;
11650 /* Parse the class-head. */
11651 type = cp_parser_class_head (parser,
11652 &nested_name_specifier_p,
11653 &deferring_access_checks_p,
11654 &saved_access_checks);
11655 /* If the class-head was a semantic disaster, skip the entire body
11659 cp_parser_skip_to_end_of_block_or_statement (parser);
11660 return error_mark_node;
11662 /* Look for the `{'. */
11663 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
11664 return error_mark_node;
11665 /* Issue an error message if type-definitions are forbidden here. */
11666 cp_parser_check_type_definition (parser);
11667 /* Remember that we are defining one more class. */
11668 ++parser->num_classes_being_defined;
11669 /* Inside the class, surrounding template-parameter-lists do not
11671 saved_num_template_parameter_lists
11672 = parser->num_template_parameter_lists;
11673 parser->num_template_parameter_lists = 0;
11674 /* Start the class. */
11675 type = begin_class_definition (type);
11676 if (type == error_mark_node)
11677 /* If the type is erroneous, skip the entire body of the class. */
11678 cp_parser_skip_to_closing_brace (parser);
11680 /* Parse the member-specification. */
11681 cp_parser_member_specification_opt (parser);
11682 /* Look for the trailing `}'. */
11683 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
11684 /* We get better error messages by noticing a common problem: a
11685 missing trailing `;'. */
11686 token = cp_lexer_peek_token (parser->lexer);
11687 has_trailing_semicolon = (token->type == CPP_SEMICOLON);
11688 /* Look for attributes to apply to this class. */
11689 if (cp_parser_allow_gnu_extensions_p (parser))
11690 attributes = cp_parser_attributes_opt (parser);
11691 /* Finish the class definition. */
11692 type = finish_class_definition (type,
11694 has_trailing_semicolon,
11695 nested_name_specifier_p);
11696 /* If this class is not itself within the scope of another class,
11697 then we need to parse the bodies of all of the queued function
11698 definitions. Note that the queued functions defined in a class
11699 are not always processed immediately following the
11700 class-specifier for that class. Consider:
11703 struct B { void f() { sizeof (A); } };
11706 If `f' were processed before the processing of `A' were
11707 completed, there would be no way to compute the size of `A'.
11708 Note that the nesting we are interested in here is lexical --
11709 not the semantic nesting given by TYPE_CONTEXT. In particular,
11712 struct A { struct B; };
11713 struct A::B { void f() { } };
11715 there is no need to delay the parsing of `A::B::f'. */
11716 if (--parser->num_classes_being_defined == 0)
11718 tree last_scope = NULL_TREE;
11722 /* Reverse the queue, so that we process it in the order the
11723 functions were declared. */
11724 TREE_VALUE (parser->unparsed_functions_queues)
11725 = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
11726 /* In a first pass, parse default arguments to the functions.
11727 Then, in a second pass, parse the bodies of the functions.
11728 This two-phased approach handles cases like:
11736 for (queue_entry = TREE_VALUE (parser->unparsed_functions_queues);
11738 queue_entry = TREE_CHAIN (queue_entry))
11740 fn = TREE_VALUE (queue_entry);
11741 if (DECL_FUNCTION_TEMPLATE_P (fn))
11742 fn = DECL_TEMPLATE_RESULT (fn);
11743 /* Make sure that any template parameters are in scope. */
11744 maybe_begin_member_template_processing (fn);
11745 /* If there are default arguments that have not yet been processed,
11746 take care of them now. */
11747 cp_parser_late_parsing_default_args (parser, fn);
11748 /* Remove any template parameters from the symbol table. */
11749 maybe_end_member_template_processing ();
11751 /* Now parse the body of the functions. */
11752 while (TREE_VALUE (parser->unparsed_functions_queues))
11755 /* Figure out which function we need to process. */
11756 queue_entry = TREE_VALUE (parser->unparsed_functions_queues);
11757 fn = TREE_VALUE (queue_entry);
11759 /* Parse the function. */
11760 cp_parser_late_parsing_for_member (parser, fn);
11762 TREE_VALUE (parser->unparsed_functions_queues)
11763 = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues));
11766 /* If LAST_SCOPE is non-NULL, then we have pushed scopes one
11767 more time than we have popped, so me must pop here. */
11769 pop_scope (last_scope);
11772 /* Put back any saved access checks. */
11773 if (deferring_access_checks_p)
11775 cp_parser_start_deferring_access_checks (parser);
11776 parser->context->deferred_access_checks = saved_access_checks;
11779 /* Restore the count of active template-parameter-lists. */
11780 parser->num_template_parameter_lists
11781 = saved_num_template_parameter_lists;
11786 /* Parse a class-head.
11789 class-key identifier [opt] base-clause [opt]
11790 class-key nested-name-specifier identifier base-clause [opt]
11791 class-key nested-name-specifier [opt] template-id
11795 class-key attributes identifier [opt] base-clause [opt]
11796 class-key attributes nested-name-specifier identifier base-clause [opt]
11797 class-key attributes nested-name-specifier [opt] template-id
11800 Returns the TYPE of the indicated class. Sets
11801 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
11802 involving a nested-name-specifier was used, and FALSE otherwise.
11803 Sets *DEFERRING_ACCESS_CHECKS_P to TRUE iff we were deferring
11804 access checks before this class-head. In that case,
11805 *SAVED_ACCESS_CHECKS is set to the current list of deferred access
11808 Returns NULL_TREE if the class-head is syntactically valid, but
11809 semantically invalid in a way that means we should skip the entire
11810 body of the class. */
11813 cp_parser_class_head (parser,
11814 nested_name_specifier_p,
11815 deferring_access_checks_p,
11816 saved_access_checks)
11818 bool *nested_name_specifier_p;
11819 bool *deferring_access_checks_p;
11820 tree *saved_access_checks;
11823 tree nested_name_specifier;
11824 enum tag_types class_key;
11825 tree id = NULL_TREE;
11826 tree type = NULL_TREE;
11828 bool template_id_p = false;
11829 bool qualified_p = false;
11830 bool invalid_nested_name_p = false;
11831 unsigned num_templates;
11833 /* Assume no nested-name-specifier will be present. */
11834 *nested_name_specifier_p = false;
11835 /* Assume no template parameter lists will be used in defining the
11839 /* Look for the class-key. */
11840 class_key = cp_parser_class_key (parser);
11841 if (class_key == none_type)
11842 return error_mark_node;
11844 /* Parse the attributes. */
11845 attributes = cp_parser_attributes_opt (parser);
11847 /* If the next token is `::', that is invalid -- but sometimes
11848 people do try to write:
11852 Handle this gracefully by accepting the extra qualifier, and then
11853 issuing an error about it later if this really is a
11854 class-header. If it turns out just to be an elaborated type
11855 specifier, remain silent. */
11856 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
11857 qualified_p = true;
11859 /* Determine the name of the class. Begin by looking for an
11860 optional nested-name-specifier. */
11861 nested_name_specifier
11862 = cp_parser_nested_name_specifier_opt (parser,
11863 /*typename_keyword_p=*/false,
11864 /*check_dependency_p=*/true,
11866 /* If there was a nested-name-specifier, then there *must* be an
11868 if (nested_name_specifier)
11870 /* Although the grammar says `identifier', it really means
11871 `class-name' or `template-name'. You are only allowed to
11872 define a class that has already been declared with this
11875 The proposed resolution for Core Issue 180 says that whever
11876 you see `class T::X' you should treat `X' as a type-name.
11878 It is OK to define an inaccessible class; for example:
11880 class A { class B; };
11883 So, we ask cp_parser_class_name not to check accessibility.
11885 We do not know if we will see a class-name, or a
11886 template-name. We look for a class-name first, in case the
11887 class-name is a template-id; if we looked for the
11888 template-name first we would stop after the template-name. */
11889 cp_parser_parse_tentatively (parser);
11890 type = cp_parser_class_name (parser,
11891 /*typename_keyword_p=*/false,
11892 /*template_keyword_p=*/false,
11894 /*check_access_p=*/false,
11895 /*check_dependency_p=*/false,
11896 /*class_head_p=*/true);
11897 /* If that didn't work, ignore the nested-name-specifier. */
11898 if (!cp_parser_parse_definitely (parser))
11900 invalid_nested_name_p = true;
11901 id = cp_parser_identifier (parser);
11902 if (id == error_mark_node)
11905 /* If we could not find a corresponding TYPE, treat this
11906 declaration like an unqualified declaration. */
11907 if (type == error_mark_node)
11908 nested_name_specifier = NULL_TREE;
11909 /* Otherwise, count the number of templates used in TYPE and its
11910 containing scopes. */
11915 for (scope = TREE_TYPE (type);
11916 scope && TREE_CODE (scope) != NAMESPACE_DECL;
11917 scope = (TYPE_P (scope)
11918 ? TYPE_CONTEXT (scope)
11919 : DECL_CONTEXT (scope)))
11921 && CLASS_TYPE_P (scope)
11922 && CLASSTYPE_TEMPLATE_INFO (scope)
11923 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
11927 /* Otherwise, the identifier is optional. */
11930 /* We don't know whether what comes next is a template-id,
11931 an identifier, or nothing at all. */
11932 cp_parser_parse_tentatively (parser);
11933 /* Check for a template-id. */
11934 id = cp_parser_template_id (parser,
11935 /*template_keyword_p=*/false,
11936 /*check_dependency_p=*/true);
11937 /* If that didn't work, it could still be an identifier. */
11938 if (!cp_parser_parse_definitely (parser))
11940 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
11941 id = cp_parser_identifier (parser);
11947 template_id_p = true;
11952 /* If it's not a `:' or a `{' then we can't really be looking at a
11953 class-head, since a class-head only appears as part of a
11954 class-specifier. We have to detect this situation before calling
11955 xref_tag, since that has irreversible side-effects. */
11956 if (!cp_parser_next_token_starts_class_definition_p (parser))
11958 cp_parser_error (parser, "expected `{' or `:'");
11959 return error_mark_node;
11962 /* At this point, we're going ahead with the class-specifier, even
11963 if some other problem occurs. */
11964 cp_parser_commit_to_tentative_parse (parser);
11965 /* Issue the error about the overly-qualified name now. */
11967 cp_parser_error (parser,
11968 "global qualification of class name is invalid");
11969 else if (invalid_nested_name_p)
11970 cp_parser_error (parser,
11971 "qualified name does not name a class");
11972 /* Make sure that the right number of template parameters were
11974 if (!cp_parser_check_template_parameters (parser, num_templates))
11975 /* If something went wrong, there is no point in even trying to
11976 process the class-definition. */
11979 /* We do not need to defer access checks for entities declared
11980 within the class. But, we do need to save any access checks that
11981 are currently deferred and restore them later, in case we are in
11982 the middle of something else. */
11983 *deferring_access_checks_p = parser->context->deferring_access_checks_p;
11984 if (*deferring_access_checks_p)
11985 *saved_access_checks = cp_parser_stop_deferring_access_checks (parser);
11987 /* Look up the type. */
11990 type = TREE_TYPE (id);
11991 maybe_process_partial_specialization (type);
11993 else if (!nested_name_specifier)
11995 /* If the class was unnamed, create a dummy name. */
11997 id = make_anon_name ();
11998 type = xref_tag (class_key, id, attributes, /*globalize=*/0);
12007 template <typename T> struct S { struct T };
12008 template <typename T> struct S::T { };
12010 we will get a TYPENAME_TYPE when processing the definition of
12011 `S::T'. We need to resolve it to the actual type before we
12012 try to define it. */
12013 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
12015 type = cp_parser_resolve_typename_type (parser, TREE_TYPE (type));
12016 if (type != error_mark_node)
12017 type = TYPE_NAME (type);
12020 maybe_process_partial_specialization (TREE_TYPE (type));
12021 class_type = current_class_type;
12022 type = TREE_TYPE (handle_class_head (class_key,
12023 nested_name_specifier,
12028 if (type != error_mark_node)
12030 if (!class_type && TYPE_CONTEXT (type))
12031 *nested_name_specifier_p = true;
12032 else if (class_type && !same_type_p (TYPE_CONTEXT (type),
12034 *nested_name_specifier_p = true;
12037 /* Indicate whether this class was declared as a `class' or as a
12039 if (TREE_CODE (type) == RECORD_TYPE)
12040 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
12041 cp_parser_check_class_key (class_key, type);
12043 /* Enter the scope containing the class; the names of base classes
12044 should be looked up in that context. For example, given:
12046 struct A { struct B {}; struct C; };
12047 struct A::C : B {};
12050 if (nested_name_specifier)
12051 push_scope (nested_name_specifier);
12052 /* Now, look for the base-clause. */
12053 token = cp_lexer_peek_token (parser->lexer);
12054 if (token->type == CPP_COLON)
12058 /* Get the list of base-classes. */
12059 bases = cp_parser_base_clause (parser);
12060 /* Process them. */
12061 xref_basetypes (type, bases);
12063 /* Leave the scope given by the nested-name-specifier. We will
12064 enter the class scope itself while processing the members. */
12065 if (nested_name_specifier)
12066 pop_scope (nested_name_specifier);
12071 /* Parse a class-key.
12078 Returns the kind of class-key specified, or none_type to indicate
12081 static enum tag_types
12082 cp_parser_class_key (parser)
12086 enum tag_types tag_type;
12088 /* Look for the class-key. */
12089 token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
12093 /* Check to see if the TOKEN is a class-key. */
12094 tag_type = cp_parser_token_is_class_key (token);
12096 cp_parser_error (parser, "expected class-key");
12100 /* Parse an (optional) member-specification.
12102 member-specification:
12103 member-declaration member-specification [opt]
12104 access-specifier : member-specification [opt] */
12107 cp_parser_member_specification_opt (parser)
12115 /* Peek at the next token. */
12116 token = cp_lexer_peek_token (parser->lexer);
12117 /* If it's a `}', or EOF then we've seen all the members. */
12118 if (token->type == CPP_CLOSE_BRACE || token->type == CPP_EOF)
12121 /* See if this token is a keyword. */
12122 keyword = token->keyword;
12126 case RID_PROTECTED:
12128 /* Consume the access-specifier. */
12129 cp_lexer_consume_token (parser->lexer);
12130 /* Remember which access-specifier is active. */
12131 current_access_specifier = token->value;
12132 /* Look for the `:'. */
12133 cp_parser_require (parser, CPP_COLON, "`:'");
12137 /* Otherwise, the next construction must be a
12138 member-declaration. */
12139 cp_parser_member_declaration (parser);
12140 reset_type_access_control ();
12145 /* Parse a member-declaration.
12147 member-declaration:
12148 decl-specifier-seq [opt] member-declarator-list [opt] ;
12149 function-definition ; [opt]
12150 :: [opt] nested-name-specifier template [opt] unqualified-id ;
12152 template-declaration
12154 member-declarator-list:
12156 member-declarator-list , member-declarator
12159 declarator pure-specifier [opt]
12160 declarator constant-initializer [opt]
12161 identifier [opt] : constant-expression
12165 member-declaration:
12166 __extension__ member-declaration
12169 declarator attributes [opt] pure-specifier [opt]
12170 declarator attributes [opt] constant-initializer [opt]
12171 identifier [opt] attributes [opt] : constant-expression */
12174 cp_parser_member_declaration (parser)
12177 tree decl_specifiers;
12178 tree prefix_attributes;
12180 bool declares_class_or_enum;
12183 int saved_pedantic;
12185 /* Check for the `__extension__' keyword. */
12186 if (cp_parser_extension_opt (parser, &saved_pedantic))
12189 cp_parser_member_declaration (parser);
12190 /* Restore the old value of the PEDANTIC flag. */
12191 pedantic = saved_pedantic;
12196 /* Check for a template-declaration. */
12197 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
12199 /* Parse the template-declaration. */
12200 cp_parser_template_declaration (parser, /*member_p=*/true);
12205 /* Check for a using-declaration. */
12206 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
12208 /* Parse the using-declaration. */
12209 cp_parser_using_declaration (parser);
12214 /* We can't tell whether we're looking at a declaration or a
12215 function-definition. */
12216 cp_parser_parse_tentatively (parser);
12218 /* Parse the decl-specifier-seq. */
12220 = cp_parser_decl_specifier_seq (parser,
12221 CP_PARSER_FLAGS_OPTIONAL,
12222 &prefix_attributes,
12223 &declares_class_or_enum);
12224 /* If there is no declarator, then the decl-specifier-seq should
12226 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12228 /* If there was no decl-specifier-seq, and the next token is a
12229 `;', then we have something like:
12235 Each member-declaration shall declare at least one member
12236 name of the class. */
12237 if (!decl_specifiers)
12240 pedwarn ("extra semicolon");
12246 /* See if this declaration is a friend. */
12247 friend_p = cp_parser_friend_p (decl_specifiers);
12248 /* If there were decl-specifiers, check to see if there was
12249 a class-declaration. */
12250 type = check_tag_decl (decl_specifiers);
12251 /* Nested classes have already been added to the class, but
12252 a `friend' needs to be explicitly registered. */
12255 /* If the `friend' keyword was present, the friend must
12256 be introduced with a class-key. */
12257 if (!declares_class_or_enum)
12258 error ("a class-key must be used when declaring a friend");
12261 template <typename T> struct A {
12262 friend struct A<T>::B;
12265 A<T>::B will be represented by a TYPENAME_TYPE, and
12266 therefore not recognized by check_tag_decl. */
12271 for (specifier = decl_specifiers;
12273 specifier = TREE_CHAIN (specifier))
12275 tree s = TREE_VALUE (specifier);
12277 if (TREE_CODE (s) == IDENTIFIER_NODE
12278 && IDENTIFIER_GLOBAL_VALUE (s))
12279 type = IDENTIFIER_GLOBAL_VALUE (s);
12280 if (TREE_CODE (s) == TYPE_DECL)
12290 error ("friend declaration does not name a class or "
12293 make_friend_class (current_class_type, type);
12295 /* If there is no TYPE, an error message will already have
12299 /* An anonymous aggregate has to be handled specially; such
12300 a declaration really declares a data member (with a
12301 particular type), as opposed to a nested class. */
12302 else if (ANON_AGGR_TYPE_P (type))
12304 /* Remove constructors and such from TYPE, now that we
12305 know it is an anoymous aggregate. */
12306 fixup_anonymous_aggr (type);
12307 /* And make the corresponding data member. */
12308 decl = build_decl (FIELD_DECL, NULL_TREE, type);
12309 /* Add it to the class. */
12310 finish_member_declaration (decl);
12316 /* See if these declarations will be friends. */
12317 friend_p = cp_parser_friend_p (decl_specifiers);
12319 /* Keep going until we hit the `;' at the end of the
12321 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12323 tree attributes = NULL_TREE;
12324 tree first_attribute;
12326 /* Peek at the next token. */
12327 token = cp_lexer_peek_token (parser->lexer);
12329 /* Check for a bitfield declaration. */
12330 if (token->type == CPP_COLON
12331 || (token->type == CPP_NAME
12332 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
12338 /* Get the name of the bitfield. Note that we cannot just
12339 check TOKEN here because it may have been invalidated by
12340 the call to cp_lexer_peek_nth_token above. */
12341 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
12342 identifier = cp_parser_identifier (parser);
12344 identifier = NULL_TREE;
12346 /* Consume the `:' token. */
12347 cp_lexer_consume_token (parser->lexer);
12348 /* Get the width of the bitfield. */
12349 width = cp_parser_constant_expression (parser);
12351 /* Look for attributes that apply to the bitfield. */
12352 attributes = cp_parser_attributes_opt (parser);
12353 /* Remember which attributes are prefix attributes and
12355 first_attribute = attributes;
12356 /* Combine the attributes. */
12357 attributes = chainon (prefix_attributes, attributes);
12359 /* Create the bitfield declaration. */
12360 decl = grokbitfield (identifier,
12363 /* Apply the attributes. */
12364 cplus_decl_attributes (&decl, attributes, /*flags=*/0);
12370 tree asm_specification;
12371 bool ctor_dtor_or_conv_p;
12373 /* Parse the declarator. */
12375 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
12376 &ctor_dtor_or_conv_p);
12378 /* If something went wrong parsing the declarator, make sure
12379 that we at least consume some tokens. */
12380 if (declarator == error_mark_node)
12382 /* Skip to the end of the statement. */
12383 cp_parser_skip_to_end_of_statement (parser);
12387 /* Look for an asm-specification. */
12388 asm_specification = cp_parser_asm_specification_opt (parser);
12389 /* Look for attributes that apply to the declaration. */
12390 attributes = cp_parser_attributes_opt (parser);
12391 /* Remember which attributes are prefix attributes and
12393 first_attribute = attributes;
12394 /* Combine the attributes. */
12395 attributes = chainon (prefix_attributes, attributes);
12397 /* If it's an `=', then we have a constant-initializer or a
12398 pure-specifier. It is not correct to parse the
12399 initializer before registering the member declaration
12400 since the member declaration should be in scope while
12401 its initializer is processed. However, the rest of the
12402 front end does not yet provide an interface that allows
12403 us to handle this correctly. */
12404 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12408 A pure-specifier shall be used only in the declaration of
12409 a virtual function.
12411 A member-declarator can contain a constant-initializer
12412 only if it declares a static member of integral or
12415 Therefore, if the DECLARATOR is for a function, we look
12416 for a pure-specifier; otherwise, we look for a
12417 constant-initializer. When we call `grokfield', it will
12418 perform more stringent semantics checks. */
12419 if (TREE_CODE (declarator) == CALL_EXPR)
12420 initializer = cp_parser_pure_specifier (parser);
12423 /* This declaration cannot be a function
12425 cp_parser_commit_to_tentative_parse (parser);
12426 /* Parse the initializer. */
12427 initializer = cp_parser_constant_initializer (parser);
12430 /* Otherwise, there is no initializer. */
12432 initializer = NULL_TREE;
12434 /* See if we are probably looking at a function
12435 definition. We are certainly not looking at at a
12436 member-declarator. Calling `grokfield' has
12437 side-effects, so we must not do it unless we are sure
12438 that we are looking at a member-declarator. */
12439 if (cp_parser_token_starts_function_definition_p
12440 (cp_lexer_peek_token (parser->lexer)))
12441 decl = error_mark_node;
12443 /* Create the declaration. */
12444 decl = grokfield (declarator,
12451 /* Reset PREFIX_ATTRIBUTES. */
12452 while (attributes && TREE_CHAIN (attributes) != first_attribute)
12453 attributes = TREE_CHAIN (attributes);
12455 TREE_CHAIN (attributes) = NULL_TREE;
12457 /* If there is any qualification still in effect, clear it
12458 now; we will be starting fresh with the next declarator. */
12459 parser->scope = NULL_TREE;
12460 parser->qualifying_scope = NULL_TREE;
12461 parser->object_scope = NULL_TREE;
12462 /* If it's a `,', then there are more declarators. */
12463 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
12464 cp_lexer_consume_token (parser->lexer);
12465 /* If the next token isn't a `;', then we have a parse error. */
12466 else if (cp_lexer_next_token_is_not (parser->lexer,
12469 cp_parser_error (parser, "expected `;'");
12470 /* Skip tokens until we find a `;' */
12471 cp_parser_skip_to_end_of_statement (parser);
12478 /* Add DECL to the list of members. */
12480 finish_member_declaration (decl);
12482 /* If DECL is a function, we must return
12483 to parse it later. (Even though there is no definition,
12484 there might be default arguments that need handling.) */
12485 if (TREE_CODE (decl) == FUNCTION_DECL)
12486 TREE_VALUE (parser->unparsed_functions_queues)
12487 = tree_cons (NULL_TREE, decl,
12488 TREE_VALUE (parser->unparsed_functions_queues));
12493 /* If everything went well, look for the `;'. */
12494 if (cp_parser_parse_definitely (parser))
12496 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
12500 /* Parse the function-definition. */
12501 decl = cp_parser_function_definition (parser, &friend_p);
12502 /* If the member was not a friend, declare it here. */
12504 finish_member_declaration (decl);
12505 /* Peek at the next token. */
12506 token = cp_lexer_peek_token (parser->lexer);
12507 /* If the next token is a semicolon, consume it. */
12508 if (token->type == CPP_SEMICOLON)
12509 cp_lexer_consume_token (parser->lexer);
12512 /* Parse a pure-specifier.
12517 Returns INTEGER_ZERO_NODE if a pure specifier is found.
12518 Otherwiser, ERROR_MARK_NODE is returned. */
12521 cp_parser_pure_specifier (parser)
12526 /* Look for the `=' token. */
12527 if (!cp_parser_require (parser, CPP_EQ, "`='"))
12528 return error_mark_node;
12529 /* Look for the `0' token. */
12530 token = cp_parser_require (parser, CPP_NUMBER, "`0'");
12531 /* Unfortunately, this will accept `0L' and `0x00' as well. We need
12532 to get information from the lexer about how the number was
12533 spelled in order to fix this problem. */
12534 if (!token || !integer_zerop (token->value))
12535 return error_mark_node;
12537 return integer_zero_node;
12540 /* Parse a constant-initializer.
12542 constant-initializer:
12543 = constant-expression
12545 Returns a representation of the constant-expression. */
12548 cp_parser_constant_initializer (parser)
12551 /* Look for the `=' token. */
12552 if (!cp_parser_require (parser, CPP_EQ, "`='"))
12553 return error_mark_node;
12555 /* It is invalid to write:
12557 struct S { static const int i = { 7 }; };
12560 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12562 cp_parser_error (parser,
12563 "a brace-enclosed initializer is not allowed here");
12564 /* Consume the opening brace. */
12565 cp_lexer_consume_token (parser->lexer);
12566 /* Skip the initializer. */
12567 cp_parser_skip_to_closing_brace (parser);
12568 /* Look for the trailing `}'. */
12569 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
12571 return error_mark_node;
12574 return cp_parser_constant_expression (parser);
12577 /* Derived classes [gram.class.derived] */
12579 /* Parse a base-clause.
12582 : base-specifier-list
12584 base-specifier-list:
12586 base-specifier-list , base-specifier
12588 Returns a TREE_LIST representing the base-classes, in the order in
12589 which they were declared. The representation of each node is as
12590 described by cp_parser_base_specifier.
12592 In the case that no bases are specified, this function will return
12593 NULL_TREE, not ERROR_MARK_NODE. */
12596 cp_parser_base_clause (parser)
12599 tree bases = NULL_TREE;
12601 /* Look for the `:' that begins the list. */
12602 cp_parser_require (parser, CPP_COLON, "`:'");
12604 /* Scan the base-specifier-list. */
12610 /* Look for the base-specifier. */
12611 base = cp_parser_base_specifier (parser);
12612 /* Add BASE to the front of the list. */
12613 if (base != error_mark_node)
12615 TREE_CHAIN (base) = bases;
12618 /* Peek at the next token. */
12619 token = cp_lexer_peek_token (parser->lexer);
12620 /* If it's not a comma, then the list is complete. */
12621 if (token->type != CPP_COMMA)
12623 /* Consume the `,'. */
12624 cp_lexer_consume_token (parser->lexer);
12627 /* PARSER->SCOPE may still be non-NULL at this point, if the last
12628 base class had a qualified name. However, the next name that
12629 appears is certainly not qualified. */
12630 parser->scope = NULL_TREE;
12631 parser->qualifying_scope = NULL_TREE;
12632 parser->object_scope = NULL_TREE;
12634 return nreverse (bases);
12637 /* Parse a base-specifier.
12640 :: [opt] nested-name-specifier [opt] class-name
12641 virtual access-specifier [opt] :: [opt] nested-name-specifier
12643 access-specifier virtual [opt] :: [opt] nested-name-specifier
12646 Returns a TREE_LIST. The TREE_PURPOSE will be one of
12647 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
12648 indicate the specifiers provided. The TREE_VALUE will be a TYPE
12649 (or the ERROR_MARK_NODE) indicating the type that was specified. */
12652 cp_parser_base_specifier (parser)
12657 bool virtual_p = false;
12658 bool duplicate_virtual_error_issued_p = false;
12659 bool duplicate_access_error_issued_p = false;
12660 bool class_scope_p;
12661 access_kind access = ak_none;
12665 /* Process the optional `virtual' and `access-specifier'. */
12668 /* Peek at the next token. */
12669 token = cp_lexer_peek_token (parser->lexer);
12670 /* Process `virtual'. */
12671 switch (token->keyword)
12674 /* If `virtual' appears more than once, issue an error. */
12675 if (virtual_p && !duplicate_virtual_error_issued_p)
12677 cp_parser_error (parser,
12678 "`virtual' specified more than once in base-specified");
12679 duplicate_virtual_error_issued_p = true;
12684 /* Consume the `virtual' token. */
12685 cp_lexer_consume_token (parser->lexer);
12690 case RID_PROTECTED:
12692 /* If more than one access specifier appears, issue an
12694 if (access != ak_none && !duplicate_access_error_issued_p)
12696 cp_parser_error (parser,
12697 "more than one access specifier in base-specified");
12698 duplicate_access_error_issued_p = true;
12701 access = ((access_kind)
12702 tree_low_cst (ridpointers[(int) token->keyword],
12705 /* Consume the access-specifier. */
12706 cp_lexer_consume_token (parser->lexer);
12716 /* Map `virtual_p' and `access' onto one of the access
12722 access_node = access_default_node;
12725 access_node = access_public_node;
12728 access_node = access_protected_node;
12731 access_node = access_private_node;
12740 access_node = access_default_virtual_node;
12743 access_node = access_public_virtual_node;
12746 access_node = access_protected_virtual_node;
12749 access_node = access_private_virtual_node;
12755 /* Look for the optional `::' operator. */
12756 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
12757 /* Look for the nested-name-specifier. The simplest way to
12762 The keyword `typename' is not permitted in a base-specifier or
12763 mem-initializer; in these contexts a qualified name that
12764 depends on a template-parameter is implicitly assumed to be a
12767 is to pretend that we have seen the `typename' keyword at this
12769 cp_parser_nested_name_specifier_opt (parser,
12770 /*typename_keyword_p=*/true,
12771 /*check_dependency_p=*/true,
12773 /* If the base class is given by a qualified name, assume that names
12774 we see are type names or templates, as appropriate. */
12775 class_scope_p = (parser->scope && TYPE_P (parser->scope));
12776 /* Finally, look for the class-name. */
12777 type = cp_parser_class_name (parser,
12781 /*check_access=*/true,
12782 /*check_dependency_p=*/true,
12783 /*class_head_p=*/false);
12785 if (type == error_mark_node)
12786 return error_mark_node;
12788 return finish_base_specifier (access_node, TREE_TYPE (type));
12791 /* Exception handling [gram.exception] */
12793 /* Parse an (optional) exception-specification.
12795 exception-specification:
12796 throw ( type-id-list [opt] )
12798 Returns a TREE_LIST representing the exception-specification. The
12799 TREE_VALUE of each node is a type. */
12802 cp_parser_exception_specification_opt (parser)
12808 /* Peek at the next token. */
12809 token = cp_lexer_peek_token (parser->lexer);
12810 /* If it's not `throw', then there's no exception-specification. */
12811 if (!cp_parser_is_keyword (token, RID_THROW))
12814 /* Consume the `throw'. */
12815 cp_lexer_consume_token (parser->lexer);
12817 /* Look for the `('. */
12818 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
12820 /* Peek at the next token. */
12821 token = cp_lexer_peek_token (parser->lexer);
12822 /* If it's not a `)', then there is a type-id-list. */
12823 if (token->type != CPP_CLOSE_PAREN)
12825 const char *saved_message;
12827 /* Types may not be defined in an exception-specification. */
12828 saved_message = parser->type_definition_forbidden_message;
12829 parser->type_definition_forbidden_message
12830 = "types may not be defined in an exception-specification";
12831 /* Parse the type-id-list. */
12832 type_id_list = cp_parser_type_id_list (parser);
12833 /* Restore the saved message. */
12834 parser->type_definition_forbidden_message = saved_message;
12837 type_id_list = empty_except_spec;
12839 /* Look for the `)'. */
12840 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
12842 return type_id_list;
12845 /* Parse an (optional) type-id-list.
12849 type-id-list , type-id
12851 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
12852 in the order that the types were presented. */
12855 cp_parser_type_id_list (parser)
12858 tree types = NULL_TREE;
12865 /* Get the next type-id. */
12866 type = cp_parser_type_id (parser);
12867 /* Add it to the list. */
12868 types = add_exception_specifier (types, type, /*complain=*/1);
12869 /* Peek at the next token. */
12870 token = cp_lexer_peek_token (parser->lexer);
12871 /* If it is not a `,', we are done. */
12872 if (token->type != CPP_COMMA)
12874 /* Consume the `,'. */
12875 cp_lexer_consume_token (parser->lexer);
12878 return nreverse (types);
12881 /* Parse a try-block.
12884 try compound-statement handler-seq */
12887 cp_parser_try_block (parser)
12892 cp_parser_require_keyword (parser, RID_TRY, "`try'");
12893 try_block = begin_try_block ();
12894 cp_parser_compound_statement (parser);
12895 finish_try_block (try_block);
12896 cp_parser_handler_seq (parser);
12897 finish_handler_sequence (try_block);
12902 /* Parse a function-try-block.
12904 function-try-block:
12905 try ctor-initializer [opt] function-body handler-seq */
12908 cp_parser_function_try_block (parser)
12912 bool ctor_initializer_p;
12914 /* Look for the `try' keyword. */
12915 if (!cp_parser_require_keyword (parser, RID_TRY, "`try'"))
12917 /* Let the rest of the front-end know where we are. */
12918 try_block = begin_function_try_block ();
12919 /* Parse the function-body. */
12921 = cp_parser_ctor_initializer_opt_and_function_body (parser);
12922 /* We're done with the `try' part. */
12923 finish_function_try_block (try_block);
12924 /* Parse the handlers. */
12925 cp_parser_handler_seq (parser);
12926 /* We're done with the handlers. */
12927 finish_function_handler_sequence (try_block);
12929 return ctor_initializer_p;
12932 /* Parse a handler-seq.
12935 handler handler-seq [opt] */
12938 cp_parser_handler_seq (parser)
12945 /* Parse the handler. */
12946 cp_parser_handler (parser);
12947 /* Peek at the next token. */
12948 token = cp_lexer_peek_token (parser->lexer);
12949 /* If it's not `catch' then there are no more handlers. */
12950 if (!cp_parser_is_keyword (token, RID_CATCH))
12955 /* Parse a handler.
12958 catch ( exception-declaration ) compound-statement */
12961 cp_parser_handler (parser)
12967 cp_parser_require_keyword (parser, RID_CATCH, "`catch'");
12968 handler = begin_handler ();
12969 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
12970 declaration = cp_parser_exception_declaration (parser);
12971 finish_handler_parms (declaration, handler);
12972 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
12973 cp_parser_compound_statement (parser);
12974 finish_handler (handler);
12977 /* Parse an exception-declaration.
12979 exception-declaration:
12980 type-specifier-seq declarator
12981 type-specifier-seq abstract-declarator
12985 Returns a VAR_DECL for the declaration, or NULL_TREE if the
12986 ellipsis variant is used. */
12989 cp_parser_exception_declaration (parser)
12992 tree type_specifiers;
12994 const char *saved_message;
12996 /* If it's an ellipsis, it's easy to handle. */
12997 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
12999 /* Consume the `...' token. */
13000 cp_lexer_consume_token (parser->lexer);
13004 /* Types may not be defined in exception-declarations. */
13005 saved_message = parser->type_definition_forbidden_message;
13006 parser->type_definition_forbidden_message
13007 = "types may not be defined in exception-declarations";
13009 /* Parse the type-specifier-seq. */
13010 type_specifiers = cp_parser_type_specifier_seq (parser);
13011 /* If it's a `)', then there is no declarator. */
13012 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
13013 declarator = NULL_TREE;
13015 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
13016 /*ctor_dtor_or_conv_p=*/NULL);
13018 /* Restore the saved message. */
13019 parser->type_definition_forbidden_message = saved_message;
13021 return start_handler_parms (type_specifiers, declarator);
13024 /* Parse a throw-expression.
13027 throw assignment-expresion [opt]
13029 Returns a THROW_EXPR representing the throw-expression. */
13032 cp_parser_throw_expression (parser)
13037 cp_parser_require_keyword (parser, RID_THROW, "`throw'");
13038 /* We can't be sure if there is an assignment-expression or not. */
13039 cp_parser_parse_tentatively (parser);
13041 expression = cp_parser_assignment_expression (parser);
13042 /* If it didn't work, this is just a rethrow. */
13043 if (!cp_parser_parse_definitely (parser))
13044 expression = NULL_TREE;
13046 return build_throw (expression);
13049 /* GNU Extensions */
13051 /* Parse an (optional) asm-specification.
13054 asm ( string-literal )
13056 If the asm-specification is present, returns a STRING_CST
13057 corresponding to the string-literal. Otherwise, returns
13061 cp_parser_asm_specification_opt (parser)
13065 tree asm_specification;
13067 /* Peek at the next token. */
13068 token = cp_lexer_peek_token (parser->lexer);
13069 /* If the next token isn't the `asm' keyword, then there's no
13070 asm-specification. */
13071 if (!cp_parser_is_keyword (token, RID_ASM))
13074 /* Consume the `asm' token. */
13075 cp_lexer_consume_token (parser->lexer);
13076 /* Look for the `('. */
13077 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13079 /* Look for the string-literal. */
13080 token = cp_parser_require (parser, CPP_STRING, "string-literal");
13082 asm_specification = token->value;
13084 asm_specification = NULL_TREE;
13086 /* Look for the `)'. */
13087 cp_parser_require (parser, CPP_CLOSE_PAREN, "`('");
13089 return asm_specification;
13092 /* Parse an asm-operand-list.
13096 asm-operand-list , asm-operand
13099 string-literal ( expression )
13100 [ string-literal ] string-literal ( expression )
13102 Returns a TREE_LIST representing the operands. The TREE_VALUE of
13103 each node is the expression. The TREE_PURPOSE is itself a
13104 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
13105 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
13106 is a STRING_CST for the string literal before the parenthesis. */
13109 cp_parser_asm_operand_list (parser)
13112 tree asm_operands = NULL_TREE;
13116 tree string_literal;
13121 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
13123 /* Consume the `[' token. */
13124 cp_lexer_consume_token (parser->lexer);
13125 /* Read the operand name. */
13126 name = cp_parser_identifier (parser);
13127 if (name != error_mark_node)
13128 name = build_string (IDENTIFIER_LENGTH (name),
13129 IDENTIFIER_POINTER (name));
13130 /* Look for the closing `]'. */
13131 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
13135 /* Look for the string-literal. */
13136 token = cp_parser_require (parser, CPP_STRING, "string-literal");
13137 string_literal = token ? token->value : error_mark_node;
13138 /* Look for the `('. */
13139 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13140 /* Parse the expression. */
13141 expression = cp_parser_expression (parser);
13142 /* Look for the `)'. */
13143 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13144 /* Add this operand to the list. */
13145 asm_operands = tree_cons (build_tree_list (name, string_literal),
13148 /* If the next token is not a `,', there are no more
13150 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13152 /* Consume the `,'. */
13153 cp_lexer_consume_token (parser->lexer);
13156 return nreverse (asm_operands);
13159 /* Parse an asm-clobber-list.
13163 asm-clobber-list , string-literal
13165 Returns a TREE_LIST, indicating the clobbers in the order that they
13166 appeared. The TREE_VALUE of each node is a STRING_CST. */
13169 cp_parser_asm_clobber_list (parser)
13172 tree clobbers = NULL_TREE;
13177 tree string_literal;
13179 /* Look for the string literal. */
13180 token = cp_parser_require (parser, CPP_STRING, "string-literal");
13181 string_literal = token ? token->value : error_mark_node;
13182 /* Add it to the list. */
13183 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
13184 /* If the next token is not a `,', then the list is
13186 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13188 /* Consume the `,' token. */
13189 cp_lexer_consume_token (parser->lexer);
13195 /* Parse an (optional) series of attributes.
13198 attributes attribute
13201 __attribute__ (( attribute-list [opt] ))
13203 The return value is as for cp_parser_attribute_list. */
13206 cp_parser_attributes_opt (parser)
13209 tree attributes = NULL_TREE;
13214 tree attribute_list;
13216 /* Peek at the next token. */
13217 token = cp_lexer_peek_token (parser->lexer);
13218 /* If it's not `__attribute__', then we're done. */
13219 if (token->keyword != RID_ATTRIBUTE)
13222 /* Consume the `__attribute__' keyword. */
13223 cp_lexer_consume_token (parser->lexer);
13224 /* Look for the two `(' tokens. */
13225 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13226 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13228 /* Peek at the next token. */
13229 token = cp_lexer_peek_token (parser->lexer);
13230 if (token->type != CPP_CLOSE_PAREN)
13231 /* Parse the attribute-list. */
13232 attribute_list = cp_parser_attribute_list (parser);
13234 /* If the next token is a `)', then there is no attribute
13236 attribute_list = NULL;
13238 /* Look for the two `)' tokens. */
13239 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13240 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13242 /* Add these new attributes to the list. */
13243 attributes = chainon (attributes, attribute_list);
13249 /* Parse an attribute-list.
13253 attribute-list , attribute
13257 identifier ( identifier )
13258 identifier ( identifier , expression-list )
13259 identifier ( expression-list )
13261 Returns a TREE_LIST. Each node corresponds to an attribute. THe
13262 TREE_PURPOSE of each node is the identifier indicating which
13263 attribute is in use. The TREE_VALUE represents the arguments, if
13267 cp_parser_attribute_list (parser)
13270 tree attribute_list = NULL_TREE;
13278 /* Look for the identifier. We also allow keywords here; for
13279 example `__attribute__ ((const))' is legal. */
13280 token = cp_lexer_peek_token (parser->lexer);
13281 if (token->type != CPP_NAME
13282 && token->type != CPP_KEYWORD)
13283 return error_mark_node;
13284 /* Consume the token. */
13285 token = cp_lexer_consume_token (parser->lexer);
13287 /* Save away the identifier that indicates which attribute this is. */
13288 identifier = token->value;
13289 attribute = build_tree_list (identifier, NULL_TREE);
13291 /* Peek at the next token. */
13292 token = cp_lexer_peek_token (parser->lexer);
13293 /* If it's an `(', then parse the attribute arguments. */
13294 if (token->type == CPP_OPEN_PAREN)
13297 int arguments_allowed_p = 1;
13299 /* Consume the `('. */
13300 cp_lexer_consume_token (parser->lexer);
13301 /* Peek at the next token. */
13302 token = cp_lexer_peek_token (parser->lexer);
13303 /* Check to see if the next token is an identifier. */
13304 if (token->type == CPP_NAME)
13306 /* Save the identifier. */
13307 identifier = token->value;
13308 /* Consume the identifier. */
13309 cp_lexer_consume_token (parser->lexer);
13310 /* Peek at the next token. */
13311 token = cp_lexer_peek_token (parser->lexer);
13312 /* If the next token is a `,', then there are some other
13313 expressions as well. */
13314 if (token->type == CPP_COMMA)
13315 /* Consume the comma. */
13316 cp_lexer_consume_token (parser->lexer);
13318 arguments_allowed_p = 0;
13321 identifier = NULL_TREE;
13323 /* If there are arguments, parse them too. */
13324 if (arguments_allowed_p)
13325 arguments = cp_parser_expression_list (parser);
13327 arguments = NULL_TREE;
13329 /* Combine the identifier and the arguments. */
13331 arguments = tree_cons (NULL_TREE, identifier, arguments);
13333 /* Save the identifier and arguments away. */
13334 TREE_VALUE (attribute) = arguments;
13336 /* Look for the closing `)'. */
13337 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13340 /* Add this attribute to the list. */
13341 TREE_CHAIN (attribute) = attribute_list;
13342 attribute_list = attribute;
13344 /* Now, look for more attributes. */
13345 token = cp_lexer_peek_token (parser->lexer);
13346 /* If the next token isn't a `,', we're done. */
13347 if (token->type != CPP_COMMA)
13350 /* Consume the commma and keep going. */
13351 cp_lexer_consume_token (parser->lexer);
13354 /* We built up the list in reverse order. */
13355 return nreverse (attribute_list);
13358 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
13359 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
13360 current value of the PEDANTIC flag, regardless of whether or not
13361 the `__extension__' keyword is present. The caller is responsible
13362 for restoring the value of the PEDANTIC flag. */
13365 cp_parser_extension_opt (parser, saved_pedantic)
13367 int *saved_pedantic;
13369 /* Save the old value of the PEDANTIC flag. */
13370 *saved_pedantic = pedantic;
13372 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
13374 /* Consume the `__extension__' token. */
13375 cp_lexer_consume_token (parser->lexer);
13376 /* We're not being pedantic while the `__extension__' keyword is
13386 /* Parse a label declaration.
13389 __label__ label-declarator-seq ;
13391 label-declarator-seq:
13392 identifier , label-declarator-seq
13396 cp_parser_label_declaration (parser)
13399 /* Look for the `__label__' keyword. */
13400 cp_parser_require_keyword (parser, RID_LABEL, "`__label__'");
13406 /* Look for an identifier. */
13407 identifier = cp_parser_identifier (parser);
13408 /* Declare it as a lobel. */
13409 finish_label_decl (identifier);
13410 /* If the next token is a `;', stop. */
13411 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13413 /* Look for the `,' separating the label declarations. */
13414 cp_parser_require (parser, CPP_COMMA, "`,'");
13417 /* Look for the final `;'. */
13418 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
13421 /* Support Functions */
13423 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
13424 NAME should have one of the representations used for an
13425 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
13426 is returned. If PARSER->SCOPE is a dependent type, then a
13427 SCOPE_REF is returned.
13429 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
13430 returned; the name was already resolved when the TEMPLATE_ID_EXPR
13431 was formed. Abstractly, such entities should not be passed to this
13432 function, because they do not need to be looked up, but it is
13433 simpler to check for this special case here, rather than at the
13436 In cases not explicitly covered above, this function returns a
13437 DECL, OVERLOAD, or baselink representing the result of the lookup.
13438 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
13441 If CHECK_ACCESS is TRUE, then access control is performed on the
13442 declaration to which the name resolves, and an error message is
13443 issued if the declaration is inaccessible.
13445 If IS_TYPE is TRUE, bindings that do not refer to types are
13448 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
13451 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
13455 cp_parser_lookup_name (cp_parser *parser, tree name, bool check_access,
13456 bool is_type, bool is_namespace, bool check_dependency)
13459 tree object_type = parser->context->object_type;
13461 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
13462 no longer valid. Note that if we are parsing tentatively, and
13463 the parse fails, OBJECT_TYPE will be automatically restored. */
13464 parser->context->object_type = NULL_TREE;
13466 if (name == error_mark_node)
13467 return error_mark_node;
13469 /* A template-id has already been resolved; there is no lookup to
13471 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
13473 if (BASELINK_P (name))
13475 my_friendly_assert ((TREE_CODE (BASELINK_FUNCTIONS (name))
13476 == TEMPLATE_ID_EXPR),
13481 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
13482 it should already have been checked to make sure that the name
13483 used matches the type being destroyed. */
13484 if (TREE_CODE (name) == BIT_NOT_EXPR)
13488 /* Figure out to which type this destructor applies. */
13490 type = parser->scope;
13491 else if (object_type)
13492 type = object_type;
13494 type = current_class_type;
13495 /* If that's not a class type, there is no destructor. */
13496 if (!type || !CLASS_TYPE_P (type))
13497 return error_mark_node;
13498 /* If it was a class type, return the destructor. */
13499 return CLASSTYPE_DESTRUCTORS (type);
13502 /* By this point, the NAME should be an ordinary identifier. If
13503 the id-expression was a qualified name, the qualifying scope is
13504 stored in PARSER->SCOPE at this point. */
13505 my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE,
13508 /* Perform the lookup. */
13511 bool dependent_type_p;
13513 if (parser->scope == error_mark_node)
13514 return error_mark_node;
13516 /* If the SCOPE is dependent, the lookup must be deferred until
13517 the template is instantiated -- unless we are explicitly
13518 looking up names in uninstantiated templates. Even then, we
13519 cannot look up the name if the scope is not a class type; it
13520 might, for example, be a template type parameter. */
13521 dependent_type_p = (TYPE_P (parser->scope)
13522 && !(parser->in_declarator_p
13523 && currently_open_class (parser->scope))
13524 && cp_parser_dependent_type_p (parser->scope));
13525 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
13526 && dependent_type_p)
13529 decl = build_nt (SCOPE_REF, parser->scope, name);
13531 /* The resolution to Core Issue 180 says that `struct A::B'
13532 should be considered a type-name, even if `A' is
13534 decl = TYPE_NAME (make_typename_type (parser->scope,
13540 /* If PARSER->SCOPE is a dependent type, then it must be a
13541 class type, and we must not be checking dependencies;
13542 otherwise, we would have processed this lookup above. So
13543 that PARSER->SCOPE is not considered a dependent base by
13544 lookup_member, we must enter the scope here. */
13545 if (dependent_type_p)
13546 push_scope (parser->scope);
13547 /* If the PARSER->SCOPE is a a template specialization, it
13548 may be instantiated during name lookup. In that case,
13549 errors may be issued. Even if we rollback the current
13550 tentative parse, those errors are valid. */
13551 decl = lookup_qualified_name (parser->scope, name, is_type,
13553 if (dependent_type_p)
13554 pop_scope (parser->scope);
13556 parser->qualifying_scope = parser->scope;
13557 parser->object_scope = NULL_TREE;
13559 else if (object_type)
13561 tree object_decl = NULL_TREE;
13562 /* Look up the name in the scope of the OBJECT_TYPE, unless the
13563 OBJECT_TYPE is not a class. */
13564 if (CLASS_TYPE_P (object_type))
13565 /* If the OBJECT_TYPE is a template specialization, it may
13566 be instantiated during name lookup. In that case, errors
13567 may be issued. Even if we rollback the current tentative
13568 parse, those errors are valid. */
13569 object_decl = lookup_member (object_type,
13571 /*protect=*/0, is_type);
13572 /* Look it up in the enclosing context, too. */
13573 decl = lookup_name_real (name, is_type, /*nonclass=*/0,
13576 parser->object_scope = object_type;
13577 parser->qualifying_scope = NULL_TREE;
13579 decl = object_decl;
13583 decl = lookup_name_real (name, is_type, /*nonclass=*/0,
13586 parser->qualifying_scope = NULL_TREE;
13587 parser->object_scope = NULL_TREE;
13590 /* If the lookup failed, let our caller know. */
13592 || decl == error_mark_node
13593 || (TREE_CODE (decl) == FUNCTION_DECL
13594 && DECL_ANTICIPATED (decl)))
13595 return error_mark_node;
13597 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
13598 if (TREE_CODE (decl) == TREE_LIST)
13600 /* The error message we have to print is too complicated for
13601 cp_parser_error, so we incorporate its actions directly. */
13602 if (!cp_parser_simulate_error (parser))
13604 error ("reference to `%D' is ambiguous", name);
13605 print_candidates (decl);
13607 return error_mark_node;
13610 my_friendly_assert (DECL_P (decl)
13611 || TREE_CODE (decl) == OVERLOAD
13612 || TREE_CODE (decl) == SCOPE_REF
13613 || BASELINK_P (decl),
13616 /* If we have resolved the name of a member declaration, check to
13617 see if the declaration is accessible. When the name resolves to
13618 set of overloaded functions, accesibility is checked when
13619 overload resolution is done.
13621 During an explicit instantiation, access is not checked at all,
13622 as per [temp.explicit]. */
13623 if (check_access && scope_chain->check_access && DECL_P (decl))
13625 tree qualifying_type;
13627 /* Figure out the type through which DECL is being
13630 = cp_parser_scope_through_which_access_occurs (decl,
13633 if (qualifying_type)
13635 /* If we are supposed to defer access checks, just record
13636 the information for later. */
13637 if (parser->context->deferring_access_checks_p)
13638 cp_parser_defer_access_check (parser, qualifying_type, decl);
13639 /* Otherwise, check accessibility now. */
13641 enforce_access (qualifying_type, decl);
13648 /* Like cp_parser_lookup_name, but for use in the typical case where
13649 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, and CHECK_DEPENDENCY is
13653 cp_parser_lookup_name_simple (parser, name)
13657 return cp_parser_lookup_name (parser, name,
13658 /*check_access=*/true,
13660 /*is_namespace=*/false,
13661 /*check_dependency=*/true);
13664 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
13665 TYPENAME_TYPE corresponds. Note that this function peers inside
13666 uninstantiated templates and therefore should be used only in
13667 extremely limited situations. */
13670 cp_parser_resolve_typename_type (parser, type)
13678 my_friendly_assert (TREE_CODE (type) == TYPENAME_TYPE,
13681 scope = TYPE_CONTEXT (type);
13682 name = DECL_NAME (TYPE_NAME (type));
13684 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
13685 it first before we can figure out what NAME refers to. */
13686 if (TREE_CODE (scope) == TYPENAME_TYPE)
13687 scope = cp_parser_resolve_typename_type (parser, scope);
13688 /* If we don't know what SCOPE refers to, then we cannot resolve the
13690 if (scope == error_mark_node)
13691 return error_mark_node;
13692 /* If the SCOPE is a template type parameter, we have no way of
13693 resolving the name. */
13694 if (TREE_CODE (scope) == TEMPLATE_TYPE_PARM)
13696 /* Enter the SCOPE so that name lookup will be resolved as if we
13697 were in the class definition. In particular, SCOPE will no
13698 longer be considered a dependent type. */
13699 push_scope (scope);
13700 /* Look up the declaration. */
13701 decl = lookup_member (scope, name, /*protect=*/0, /*want_type=*/1);
13702 /* If all went well, we got a TYPE_DECL for a non-typename. */
13704 || TREE_CODE (decl) != TYPE_DECL
13705 || TREE_CODE (TREE_TYPE (decl)) == TYPENAME_TYPE)
13707 cp_parser_error (parser, "could not resolve typename type");
13708 type = error_mark_node;
13711 type = TREE_TYPE (decl);
13712 /* Leave the SCOPE. */
13718 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
13719 the current context, return the TYPE_DECL. If TAG_NAME_P is
13720 true, the DECL indicates the class being defined in a class-head,
13721 or declared in an elaborated-type-specifier.
13723 Otherwise, return DECL. */
13726 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
13728 /* If the DECL is a TEMPLATE_DECL for a class type, and we are in
13729 the scope of the class, then treat the TEMPLATE_DECL as a
13730 class-name. For example, in:
13732 template <class T> struct S {
13738 If the TEMPLATE_DECL is being declared as part of a class-head,
13739 the same translation occurs:
13742 template <typename T> struct B;
13745 template <typename T> struct A::B {};
13747 Similarly, in a elaborated-type-specifier:
13749 namespace N { struct X{}; }
13752 template <typename T> friend struct N::X;
13756 if (DECL_CLASS_TEMPLATE_P (decl)
13758 || (current_class_type
13759 && same_type_p (TREE_TYPE (DECL_TEMPLATE_RESULT (decl)),
13760 current_class_type))))
13761 return DECL_TEMPLATE_RESULT (decl);
13766 /* If too many, or too few, template-parameter lists apply to the
13767 declarator, issue an error message. Returns TRUE if all went well,
13768 and FALSE otherwise. */
13771 cp_parser_check_declarator_template_parameters (parser, declarator)
13775 unsigned num_templates;
13777 /* We haven't seen any classes that involve template parameters yet. */
13780 switch (TREE_CODE (declarator))
13787 tree main_declarator = TREE_OPERAND (declarator, 0);
13789 cp_parser_check_declarator_template_parameters (parser,
13798 scope = TREE_OPERAND (declarator, 0);
13799 member = TREE_OPERAND (declarator, 1);
13801 /* If this is a pointer-to-member, then we are not interested
13802 in the SCOPE, because it does not qualify the thing that is
13804 if (TREE_CODE (member) == INDIRECT_REF)
13805 return (cp_parser_check_declarator_template_parameters
13808 while (scope && CLASS_TYPE_P (scope))
13810 /* You're supposed to have one `template <...>'
13811 for every template class, but you don't need one
13812 for a full specialization. For example:
13814 template <class T> struct S{};
13815 template <> struct S<int> { void f(); };
13816 void S<int>::f () {}
13818 is correct; there shouldn't be a `template <>' for
13819 the definition of `S<int>::f'. */
13820 if (CLASSTYPE_TEMPLATE_INFO (scope)
13821 && (CLASSTYPE_TEMPLATE_INSTANTIATION (scope)
13822 || uses_template_parms (CLASSTYPE_TI_ARGS (scope)))
13823 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
13826 scope = TYPE_CONTEXT (scope);
13830 /* Fall through. */
13833 /* If the DECLARATOR has the form `X<y>' then it uses one
13834 additional level of template parameters. */
13835 if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
13838 return cp_parser_check_template_parameters (parser,
13843 /* NUM_TEMPLATES were used in the current declaration. If that is
13844 invalid, return FALSE and issue an error messages. Otherwise,
13848 cp_parser_check_template_parameters (parser, num_templates)
13850 unsigned num_templates;
13852 /* If there are more template classes than parameter lists, we have
13855 template <class T> void S<T>::R<T>::f (); */
13856 if (parser->num_template_parameter_lists < num_templates)
13858 error ("too few template-parameter-lists");
13861 /* If there are the same number of template classes and parameter
13862 lists, that's OK. */
13863 if (parser->num_template_parameter_lists == num_templates)
13865 /* If there are more, but only one more, then we are referring to a
13866 member template. That's OK too. */
13867 if (parser->num_template_parameter_lists == num_templates + 1)
13869 /* Otherwise, there are too many template parameter lists. We have
13872 template <class T> template <class U> void S::f(); */
13873 error ("too many template-parameter-lists");
13877 /* Parse a binary-expression of the general form:
13881 binary-expression <token> <expr>
13883 The TOKEN_TREE_MAP maps <token> types to <expr> codes. FN is used
13884 to parser the <expr>s. If the first production is used, then the
13885 value returned by FN is returned directly. Otherwise, a node with
13886 the indicated EXPR_TYPE is returned, with operands corresponding to
13887 the two sub-expressions. */
13890 cp_parser_binary_expression (parser, token_tree_map, fn)
13892 const cp_parser_token_tree_map token_tree_map;
13893 cp_parser_expression_fn fn;
13897 /* Parse the first expression. */
13898 lhs = (*fn) (parser);
13899 /* Now, look for more expressions. */
13903 const cp_parser_token_tree_map_node *map_node;
13906 /* Peek at the next token. */
13907 token = cp_lexer_peek_token (parser->lexer);
13908 /* If the token is `>', and that's not an operator at the
13909 moment, then we're done. */
13910 if (token->type == CPP_GREATER
13911 && !parser->greater_than_is_operator_p)
13913 /* If we find one of the tokens we want, build the correspoding
13914 tree representation. */
13915 for (map_node = token_tree_map;
13916 map_node->token_type != CPP_EOF;
13918 if (map_node->token_type == token->type)
13920 /* Consume the operator token. */
13921 cp_lexer_consume_token (parser->lexer);
13922 /* Parse the right-hand side of the expression. */
13923 rhs = (*fn) (parser);
13924 /* Build the binary tree node. */
13925 lhs = build_x_binary_op (map_node->tree_type, lhs, rhs);
13929 /* If the token wasn't one of the ones we want, we're done. */
13930 if (map_node->token_type == CPP_EOF)
13937 /* Parse an optional `::' token indicating that the following name is
13938 from the global namespace. If so, PARSER->SCOPE is set to the
13939 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
13940 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
13941 Returns the new value of PARSER->SCOPE, if the `::' token is
13942 present, and NULL_TREE otherwise. */
13945 cp_parser_global_scope_opt (parser, current_scope_valid_p)
13947 bool current_scope_valid_p;
13951 /* Peek at the next token. */
13952 token = cp_lexer_peek_token (parser->lexer);
13953 /* If we're looking at a `::' token then we're starting from the
13954 global namespace, not our current location. */
13955 if (token->type == CPP_SCOPE)
13957 /* Consume the `::' token. */
13958 cp_lexer_consume_token (parser->lexer);
13959 /* Set the SCOPE so that we know where to start the lookup. */
13960 parser->scope = global_namespace;
13961 parser->qualifying_scope = global_namespace;
13962 parser->object_scope = NULL_TREE;
13964 return parser->scope;
13966 else if (!current_scope_valid_p)
13968 parser->scope = NULL_TREE;
13969 parser->qualifying_scope = NULL_TREE;
13970 parser->object_scope = NULL_TREE;
13976 /* Returns TRUE if the upcoming token sequence is the start of a
13977 constructor declarator. If FRIEND_P is true, the declarator is
13978 preceded by the `friend' specifier. */
13981 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
13983 bool constructor_p;
13984 tree type_decl = NULL_TREE;
13985 bool nested_name_p;
13987 /* Parse tentatively; we are going to roll back all of the tokens
13989 cp_parser_parse_tentatively (parser);
13990 /* Assume that we are looking at a constructor declarator. */
13991 constructor_p = true;
13992 /* Look for the optional `::' operator. */
13993 cp_parser_global_scope_opt (parser,
13994 /*current_scope_valid_p=*/false);
13995 /* Look for the nested-name-specifier. */
13997 = (cp_parser_nested_name_specifier_opt (parser,
13998 /*typename_keyword_p=*/false,
13999 /*check_dependency_p=*/false,
14002 /* Outside of a class-specifier, there must be a
14003 nested-name-specifier. */
14004 if (!nested_name_p &&
14005 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
14007 constructor_p = false;
14008 /* If we still think that this might be a constructor-declarator,
14009 look for a class-name. */
14014 template <typename T> struct S { S(); }
14015 template <typename T> S<T>::S ();
14017 we must recognize that the nested `S' names a class.
14020 template <typename T> S<T>::S<T> ();
14022 we must recognize that the nested `S' names a template. */
14023 type_decl = cp_parser_class_name (parser,
14024 /*typename_keyword_p=*/false,
14025 /*template_keyword_p=*/false,
14027 /*check_access_p=*/false,
14028 /*check_dependency_p=*/false,
14029 /*class_head_p=*/false);
14030 /* If there was no class-name, then this is not a constructor. */
14031 constructor_p = !cp_parser_error_occurred (parser);
14033 /* If we're still considering a constructor, we have to see a `(',
14034 to begin the parameter-declaration-clause, followed by either a
14035 `)', an `...', or a decl-specifier. We need to check for a
14036 type-specifier to avoid being fooled into thinking that:
14040 is a constructor. (It is actually a function named `f' that
14041 takes one parameter (of type `int') and returns a value of type
14044 && cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
14046 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
14047 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
14048 && !cp_parser_storage_class_specifier_opt (parser))
14050 if (current_class_type
14051 && !same_type_p (current_class_type, TREE_TYPE (type_decl)))
14052 /* The constructor for one class cannot be declared inside
14054 constructor_p = false;
14059 /* Names appearing in the type-specifier should be looked up
14060 in the scope of the class. */
14061 if (current_class_type)
14065 type = TREE_TYPE (type_decl);
14066 if (TREE_CODE (type) == TYPENAME_TYPE)
14067 type = cp_parser_resolve_typename_type (parser, type);
14070 /* Look for the type-specifier. */
14071 cp_parser_type_specifier (parser,
14072 CP_PARSER_FLAGS_NONE,
14073 /*is_friend=*/false,
14074 /*is_declarator=*/true,
14075 /*declares_class_or_enum=*/NULL,
14076 /*is_cv_qualifier=*/NULL);
14077 /* Leave the scope of the class. */
14081 constructor_p = !cp_parser_error_occurred (parser);
14086 constructor_p = false;
14087 /* We did not really want to consume any tokens. */
14088 cp_parser_abort_tentative_parse (parser);
14090 return constructor_p;
14093 /* Parse the definition of the function given by the DECL_SPECIFIERS,
14094 ATTRIBUTES, and DECLARATOR. The ACCESS_CHECKS have been deferred;
14095 they must be performed once we are in the scope of the function.
14097 Returns the function defined. */
14100 cp_parser_function_definition_from_specifiers_and_declarator
14101 (parser, decl_specifiers, attributes, declarator, access_checks)
14103 tree decl_specifiers;
14106 tree access_checks;
14111 /* Begin the function-definition. */
14112 success_p = begin_function_definition (decl_specifiers,
14116 /* If there were names looked up in the decl-specifier-seq that we
14117 did not check, check them now. We must wait until we are in the
14118 scope of the function to perform the checks, since the function
14119 might be a friend. */
14120 cp_parser_perform_deferred_access_checks (access_checks);
14124 /* If begin_function_definition didn't like the definition, skip
14125 the entire function. */
14126 error ("invalid function declaration");
14127 cp_parser_skip_to_end_of_block_or_statement (parser);
14128 fn = error_mark_node;
14131 fn = cp_parser_function_definition_after_declarator (parser,
14132 /*inline_p=*/false);
14137 /* Parse the part of a function-definition that follows the
14138 declarator. INLINE_P is TRUE iff this function is an inline
14139 function defined with a class-specifier.
14141 Returns the function defined. */
14144 cp_parser_function_definition_after_declarator (parser,
14150 bool ctor_initializer_p = false;
14151 bool saved_in_unbraced_linkage_specification_p;
14152 unsigned saved_num_template_parameter_lists;
14154 /* If the next token is `return', then the code may be trying to
14155 make use of the "named return value" extension that G++ used to
14157 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
14159 /* Consume the `return' keyword. */
14160 cp_lexer_consume_token (parser->lexer);
14161 /* Look for the identifier that indicates what value is to be
14163 cp_parser_identifier (parser);
14164 /* Issue an error message. */
14165 error ("named return values are no longer supported");
14166 /* Skip tokens until we reach the start of the function body. */
14167 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
14168 cp_lexer_consume_token (parser->lexer);
14170 /* The `extern' in `extern "C" void f () { ... }' does not apply to
14171 anything declared inside `f'. */
14172 saved_in_unbraced_linkage_specification_p
14173 = parser->in_unbraced_linkage_specification_p;
14174 parser->in_unbraced_linkage_specification_p = false;
14175 /* Inside the function, surrounding template-parameter-lists do not
14177 saved_num_template_parameter_lists
14178 = parser->num_template_parameter_lists;
14179 parser->num_template_parameter_lists = 0;
14180 /* If the next token is `try', then we are looking at a
14181 function-try-block. */
14182 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
14183 ctor_initializer_p = cp_parser_function_try_block (parser);
14184 /* A function-try-block includes the function-body, so we only do
14185 this next part if we're not processing a function-try-block. */
14188 = cp_parser_ctor_initializer_opt_and_function_body (parser);
14190 /* Finish the function. */
14191 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
14192 (inline_p ? 2 : 0));
14193 /* Generate code for it, if necessary. */
14195 /* Restore the saved values. */
14196 parser->in_unbraced_linkage_specification_p
14197 = saved_in_unbraced_linkage_specification_p;
14198 parser->num_template_parameter_lists
14199 = saved_num_template_parameter_lists;
14204 /* Parse a template-declaration, assuming that the `export' (and
14205 `extern') keywords, if present, has already been scanned. MEMBER_P
14206 is as for cp_parser_template_declaration. */
14209 cp_parser_template_declaration_after_export (parser, member_p)
14213 tree decl = NULL_TREE;
14214 tree parameter_list;
14215 bool friend_p = false;
14217 /* Look for the `template' keyword. */
14218 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'"))
14222 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
14225 /* Parse the template parameters. */
14226 begin_template_parm_list ();
14227 /* If the next token is `>', then we have an invalid
14228 specialization. Rather than complain about an invalid template
14229 parameter, issue an error message here. */
14230 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
14232 cp_parser_error (parser, "invalid explicit specialization");
14233 parameter_list = NULL_TREE;
14236 parameter_list = cp_parser_template_parameter_list (parser);
14237 parameter_list = end_template_parm_list (parameter_list);
14238 /* Look for the `>'. */
14239 cp_parser_skip_until_found (parser, CPP_GREATER, "`>'");
14240 /* We just processed one more parameter list. */
14241 ++parser->num_template_parameter_lists;
14242 /* If the next token is `template', there are more template
14244 if (cp_lexer_next_token_is_keyword (parser->lexer,
14246 cp_parser_template_declaration_after_export (parser, member_p);
14249 decl = cp_parser_single_declaration (parser,
14253 /* If this is a member template declaration, let the front
14255 if (member_p && !friend_p && decl)
14256 decl = finish_member_template_decl (decl);
14257 else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
14258 make_friend_class (current_class_type, TREE_TYPE (decl));
14260 /* We are done with the current parameter list. */
14261 --parser->num_template_parameter_lists;
14264 finish_template_decl (parameter_list);
14266 /* Register member declarations. */
14267 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
14268 finish_member_declaration (decl);
14270 /* If DECL is a function template, we must return to parse it later.
14271 (Even though there is no definition, there might be default
14272 arguments that need handling.) */
14273 if (member_p && decl
14274 && (TREE_CODE (decl) == FUNCTION_DECL
14275 || DECL_FUNCTION_TEMPLATE_P (decl)))
14276 TREE_VALUE (parser->unparsed_functions_queues)
14277 = tree_cons (NULL_TREE, decl,
14278 TREE_VALUE (parser->unparsed_functions_queues));
14281 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
14282 `function-definition' sequence. MEMBER_P is true, this declaration
14283 appears in a class scope.
14285 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
14286 *FRIEND_P is set to TRUE iff the declaration is a friend. */
14289 cp_parser_single_declaration (parser,
14296 bool declares_class_or_enum;
14297 tree decl = NULL_TREE;
14298 tree decl_specifiers;
14300 tree access_checks;
14302 /* Parse the dependent declaration. We don't know yet
14303 whether it will be a function-definition. */
14304 cp_parser_parse_tentatively (parser);
14305 /* Defer access checks until we know what is being declared. */
14306 cp_parser_start_deferring_access_checks (parser);
14307 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
14310 = cp_parser_decl_specifier_seq (parser,
14311 CP_PARSER_FLAGS_OPTIONAL,
14313 &declares_class_or_enum);
14314 /* Gather up the access checks that occurred the
14315 decl-specifier-seq. */
14316 access_checks = cp_parser_stop_deferring_access_checks (parser);
14317 /* Check for the declaration of a template class. */
14318 if (declares_class_or_enum)
14320 if (cp_parser_declares_only_class_p (parser))
14322 decl = shadow_tag (decl_specifiers);
14324 decl = TYPE_NAME (decl);
14326 decl = error_mark_node;
14331 /* If it's not a template class, try for a template function. If
14332 the next token is a `;', then this declaration does not declare
14333 anything. But, if there were errors in the decl-specifiers, then
14334 the error might well have come from an attempted class-specifier.
14335 In that case, there's no need to warn about a missing declarator. */
14337 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
14338 || !value_member (error_mark_node, decl_specifiers)))
14339 decl = cp_parser_init_declarator (parser,
14343 /*function_definition_allowed_p=*/false,
14345 /*function_definition_p=*/NULL);
14346 /* Clear any current qualification; whatever comes next is the start
14347 of something new. */
14348 parser->scope = NULL_TREE;
14349 parser->qualifying_scope = NULL_TREE;
14350 parser->object_scope = NULL_TREE;
14351 /* Look for a trailing `;' after the declaration. */
14352 if (!cp_parser_require (parser, CPP_SEMICOLON, "expected `;'")
14353 && cp_parser_committed_to_tentative_parse (parser))
14354 cp_parser_skip_to_end_of_block_or_statement (parser);
14355 /* If it worked, set *FRIEND_P based on the DECL_SPECIFIERS. */
14356 if (cp_parser_parse_definitely (parser))
14359 *friend_p = cp_parser_friend_p (decl_specifiers);
14361 /* Otherwise, try a function-definition. */
14363 decl = cp_parser_function_definition (parser, friend_p);
14368 /* Parse a functional cast to TYPE. Returns an expression
14369 representing the cast. */
14372 cp_parser_functional_cast (parser, type)
14376 tree expression_list;
14378 /* Look for the opening `('. */
14379 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
14380 return error_mark_node;
14381 /* If the next token is not an `)', there are arguments to the
14383 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
14384 expression_list = cp_parser_expression_list (parser);
14386 expression_list = NULL_TREE;
14387 /* Look for the closing `)'. */
14388 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14390 return build_functional_cast (type, expression_list);
14393 /* MEMBER_FUNCTION is a member function, or a friend. If default
14394 arguments, or the body of the function have not yet been parsed,
14398 cp_parser_late_parsing_for_member (parser, member_function)
14400 tree member_function;
14402 cp_lexer *saved_lexer;
14404 /* If this member is a template, get the underlying
14406 if (DECL_FUNCTION_TEMPLATE_P (member_function))
14407 member_function = DECL_TEMPLATE_RESULT (member_function);
14409 /* There should not be any class definitions in progress at this
14410 point; the bodies of members are only parsed outside of all class
14412 my_friendly_assert (parser->num_classes_being_defined == 0, 20010816);
14413 /* While we're parsing the member functions we might encounter more
14414 classes. We want to handle them right away, but we don't want
14415 them getting mixed up with functions that are currently in the
14417 parser->unparsed_functions_queues
14418 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
14420 /* Make sure that any template parameters are in scope. */
14421 maybe_begin_member_template_processing (member_function);
14423 /* If the body of the function has not yet been parsed, parse it
14425 if (DECL_PENDING_INLINE_P (member_function))
14427 tree function_scope;
14428 cp_token_cache *tokens;
14430 /* The function is no longer pending; we are processing it. */
14431 tokens = DECL_PENDING_INLINE_INFO (member_function);
14432 DECL_PENDING_INLINE_INFO (member_function) = NULL;
14433 DECL_PENDING_INLINE_P (member_function) = 0;
14434 /* If this was an inline function in a local class, enter the scope
14435 of the containing function. */
14436 function_scope = decl_function_context (member_function);
14437 if (function_scope)
14438 push_function_context_to (function_scope);
14440 /* Save away the current lexer. */
14441 saved_lexer = parser->lexer;
14442 /* Make a new lexer to feed us the tokens saved for this function. */
14443 parser->lexer = cp_lexer_new_from_tokens (tokens);
14444 parser->lexer->next = saved_lexer;
14446 /* Set the current source position to be the location of the first
14447 token in the saved inline body. */
14448 (void) cp_lexer_peek_token (parser->lexer);
14450 /* Let the front end know that we going to be defining this
14452 start_function (NULL_TREE, member_function, NULL_TREE,
14453 SF_PRE_PARSED | SF_INCLASS_INLINE);
14455 /* Now, parse the body of the function. */
14456 cp_parser_function_definition_after_declarator (parser,
14457 /*inline_p=*/true);
14459 /* Leave the scope of the containing function. */
14460 if (function_scope)
14461 pop_function_context_from (function_scope);
14462 /* Restore the lexer. */
14463 parser->lexer = saved_lexer;
14466 /* Remove any template parameters from the symbol table. */
14467 maybe_end_member_template_processing ();
14469 /* Restore the queue. */
14470 parser->unparsed_functions_queues
14471 = TREE_CHAIN (parser->unparsed_functions_queues);
14474 /* FN is a FUNCTION_DECL which may contains a parameter with an
14475 unparsed DEFAULT_ARG. Parse the default args now. */
14478 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
14480 cp_lexer *saved_lexer;
14481 cp_token_cache *tokens;
14482 bool saved_local_variables_forbidden_p;
14485 for (parameters = TYPE_ARG_TYPES (TREE_TYPE (fn));
14487 parameters = TREE_CHAIN (parameters))
14489 if (!TREE_PURPOSE (parameters)
14490 || TREE_CODE (TREE_PURPOSE (parameters)) != DEFAULT_ARG)
14493 /* Save away the current lexer. */
14494 saved_lexer = parser->lexer;
14495 /* Create a new one, using the tokens we have saved. */
14496 tokens = DEFARG_TOKENS (TREE_PURPOSE (parameters));
14497 parser->lexer = cp_lexer_new_from_tokens (tokens);
14499 /* Set the current source position to be the location of the
14500 first token in the default argument. */
14501 (void) cp_lexer_peek_token (parser->lexer);
14503 /* Local variable names (and the `this' keyword) may not appear
14504 in a default argument. */
14505 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
14506 parser->local_variables_forbidden_p = true;
14507 /* Parse the assignment-expression. */
14508 if (DECL_CONTEXT (fn))
14509 push_nested_class (DECL_CONTEXT (fn), 1);
14510 TREE_PURPOSE (parameters) = cp_parser_assignment_expression (parser);
14511 if (DECL_CONTEXT (fn))
14512 pop_nested_class ();
14514 /* Restore saved state. */
14515 parser->lexer = saved_lexer;
14516 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
14520 /* Parse the operand of `sizeof' (or a similar operator). Returns
14521 either a TYPE or an expression, depending on the form of the
14522 input. The KEYWORD indicates which kind of expression we have
14526 cp_parser_sizeof_operand (parser, keyword)
14530 static const char *format;
14531 tree expr = NULL_TREE;
14532 const char *saved_message;
14533 bool saved_constant_expression_p;
14535 /* Initialize FORMAT the first time we get here. */
14537 format = "types may not be defined in `%s' expressions";
14539 /* Types cannot be defined in a `sizeof' expression. Save away the
14541 saved_message = parser->type_definition_forbidden_message;
14542 /* And create the new one. */
14543 parser->type_definition_forbidden_message
14545 xmalloc (strlen (format)
14546 + strlen (IDENTIFIER_POINTER (ridpointers[keyword]))
14548 sprintf ((char *) parser->type_definition_forbidden_message,
14549 format, IDENTIFIER_POINTER (ridpointers[keyword]));
14551 /* The restrictions on constant-expressions do not apply inside
14552 sizeof expressions. */
14553 saved_constant_expression_p = parser->constant_expression_p;
14554 parser->constant_expression_p = false;
14556 /* Do not actually evaluate the expression. */
14558 /* If it's a `(', then we might be looking at the type-id
14560 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
14564 /* We can't be sure yet whether we're looking at a type-id or an
14566 cp_parser_parse_tentatively (parser);
14567 /* Consume the `('. */
14568 cp_lexer_consume_token (parser->lexer);
14569 /* Parse the type-id. */
14570 type = cp_parser_type_id (parser);
14571 /* Now, look for the trailing `)'. */
14572 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14573 /* If all went well, then we're done. */
14574 if (cp_parser_parse_definitely (parser))
14576 /* Build a list of decl-specifiers; right now, we have only
14577 a single type-specifier. */
14578 type = build_tree_list (NULL_TREE,
14581 /* Call grokdeclarator to figure out what type this is. */
14582 expr = grokdeclarator (NULL_TREE,
14586 /*attrlist=*/NULL);
14590 /* If the type-id production did not work out, then we must be
14591 looking at the unary-expression production. */
14593 expr = cp_parser_unary_expression (parser, /*address_p=*/false);
14594 /* Go back to evaluating expressions. */
14597 /* Free the message we created. */
14598 free ((char *) parser->type_definition_forbidden_message);
14599 /* And restore the old one. */
14600 parser->type_definition_forbidden_message = saved_message;
14601 parser->constant_expression_p = saved_constant_expression_p;
14606 /* If the current declaration has no declarator, return true. */
14609 cp_parser_declares_only_class_p (cp_parser *parser)
14611 /* If the next token is a `;' or a `,' then there is no
14613 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
14614 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
14617 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
14618 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
14621 cp_parser_friend_p (decl_specifiers)
14622 tree decl_specifiers;
14624 while (decl_specifiers)
14626 /* See if this decl-specifier is `friend'. */
14627 if (TREE_CODE (TREE_VALUE (decl_specifiers)) == IDENTIFIER_NODE
14628 && C_RID_CODE (TREE_VALUE (decl_specifiers)) == RID_FRIEND)
14631 /* Go on to the next decl-specifier. */
14632 decl_specifiers = TREE_CHAIN (decl_specifiers);
14638 /* If the next token is of the indicated TYPE, consume it. Otherwise,
14639 issue an error message indicating that TOKEN_DESC was expected.
14641 Returns the token consumed, if the token had the appropriate type.
14642 Otherwise, returns NULL. */
14645 cp_parser_require (parser, type, token_desc)
14647 enum cpp_ttype type;
14648 const char *token_desc;
14650 if (cp_lexer_next_token_is (parser->lexer, type))
14651 return cp_lexer_consume_token (parser->lexer);
14654 /* Output the MESSAGE -- unless we're parsing tentatively. */
14655 if (!cp_parser_simulate_error (parser))
14656 error ("expected %s", token_desc);
14661 /* Like cp_parser_require, except that tokens will be skipped until
14662 the desired token is found. An error message is still produced if
14663 the next token is not as expected. */
14666 cp_parser_skip_until_found (parser, type, token_desc)
14668 enum cpp_ttype type;
14669 const char *token_desc;
14672 unsigned nesting_depth = 0;
14674 if (cp_parser_require (parser, type, token_desc))
14677 /* Skip tokens until the desired token is found. */
14680 /* Peek at the next token. */
14681 token = cp_lexer_peek_token (parser->lexer);
14682 /* If we've reached the token we want, consume it and
14684 if (token->type == type && !nesting_depth)
14686 cp_lexer_consume_token (parser->lexer);
14689 /* If we've run out of tokens, stop. */
14690 if (token->type == CPP_EOF)
14692 if (token->type == CPP_OPEN_BRACE
14693 || token->type == CPP_OPEN_PAREN
14694 || token->type == CPP_OPEN_SQUARE)
14696 else if (token->type == CPP_CLOSE_BRACE
14697 || token->type == CPP_CLOSE_PAREN
14698 || token->type == CPP_CLOSE_SQUARE)
14700 if (nesting_depth-- == 0)
14703 /* Consume this token. */
14704 cp_lexer_consume_token (parser->lexer);
14708 /* If the next token is the indicated keyword, consume it. Otherwise,
14709 issue an error message indicating that TOKEN_DESC was expected.
14711 Returns the token consumed, if the token had the appropriate type.
14712 Otherwise, returns NULL. */
14715 cp_parser_require_keyword (parser, keyword, token_desc)
14718 const char *token_desc;
14720 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
14722 if (token && token->keyword != keyword)
14724 dyn_string_t error_msg;
14726 /* Format the error message. */
14727 error_msg = dyn_string_new (0);
14728 dyn_string_append_cstr (error_msg, "expected ");
14729 dyn_string_append_cstr (error_msg, token_desc);
14730 cp_parser_error (parser, error_msg->s);
14731 dyn_string_delete (error_msg);
14738 /* Returns TRUE iff TOKEN is a token that can begin the body of a
14739 function-definition. */
14742 cp_parser_token_starts_function_definition_p (token)
14745 return (/* An ordinary function-body begins with an `{'. */
14746 token->type == CPP_OPEN_BRACE
14747 /* A ctor-initializer begins with a `:'. */
14748 || token->type == CPP_COLON
14749 /* A function-try-block begins with `try'. */
14750 || token->keyword == RID_TRY
14751 /* The named return value extension begins with `return'. */
14752 || token->keyword == RID_RETURN);
14755 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
14759 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
14763 token = cp_lexer_peek_token (parser->lexer);
14764 return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
14767 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
14768 or none_type otherwise. */
14770 static enum tag_types
14771 cp_parser_token_is_class_key (token)
14774 switch (token->keyword)
14779 return record_type;
14788 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
14791 cp_parser_check_class_key (enum tag_types class_key, tree type)
14793 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
14794 pedwarn ("`%s' tag used in naming `%#T'",
14795 class_key == union_type ? "union"
14796 : class_key == record_type ? "struct" : "class",
14800 /* Look for the `template' keyword, as a syntactic disambiguator.
14801 Return TRUE iff it is present, in which case it will be
14805 cp_parser_optional_template_keyword (cp_parser *parser)
14807 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
14809 /* The `template' keyword can only be used within templates;
14810 outside templates the parser can always figure out what is a
14811 template and what is not. */
14812 if (!processing_template_decl)
14814 error ("`template' (as a disambiguator) is only allowed "
14815 "within templates");
14816 /* If this part of the token stream is rescanned, the same
14817 error message would be generated. So, we purge the token
14818 from the stream. */
14819 cp_lexer_purge_token (parser->lexer);
14824 /* Consume the `template' keyword. */
14825 cp_lexer_consume_token (parser->lexer);
14833 /* Add tokens to CACHE until an non-nested END token appears. */
14836 cp_parser_cache_group (cp_parser *parser,
14837 cp_token_cache *cache,
14838 enum cpp_ttype end,
14845 /* Abort a parenthesized expression if we encounter a brace. */
14846 if ((end == CPP_CLOSE_PAREN || depth == 0)
14847 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
14849 /* Consume the next token. */
14850 token = cp_lexer_consume_token (parser->lexer);
14851 /* If we've reached the end of the file, stop. */
14852 if (token->type == CPP_EOF)
14854 /* Add this token to the tokens we are saving. */
14855 cp_token_cache_push_token (cache, token);
14856 /* See if it starts a new group. */
14857 if (token->type == CPP_OPEN_BRACE)
14859 cp_parser_cache_group (parser, cache, CPP_CLOSE_BRACE, depth + 1);
14863 else if (token->type == CPP_OPEN_PAREN)
14864 cp_parser_cache_group (parser, cache, CPP_CLOSE_PAREN, depth + 1);
14865 else if (token->type == end)
14870 /* Begin parsing tentatively. We always save tokens while parsing
14871 tentatively so that if the tentative parsing fails we can restore the
14875 cp_parser_parse_tentatively (parser)
14878 /* Enter a new parsing context. */
14879 parser->context = cp_parser_context_new (parser->context);
14880 /* Begin saving tokens. */
14881 cp_lexer_save_tokens (parser->lexer);
14882 /* In order to avoid repetitive access control error messages,
14883 access checks are queued up until we are no longer parsing
14885 cp_parser_start_deferring_access_checks (parser);
14888 /* Commit to the currently active tentative parse. */
14891 cp_parser_commit_to_tentative_parse (parser)
14894 cp_parser_context *context;
14897 /* Mark all of the levels as committed. */
14898 lexer = parser->lexer;
14899 for (context = parser->context; context->next; context = context->next)
14901 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
14903 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
14904 while (!cp_lexer_saving_tokens (lexer))
14905 lexer = lexer->next;
14906 cp_lexer_commit_tokens (lexer);
14910 /* Abort the currently active tentative parse. All consumed tokens
14911 will be rolled back, and no diagnostics will be issued. */
14914 cp_parser_abort_tentative_parse (parser)
14917 cp_parser_simulate_error (parser);
14918 /* Now, pretend that we want to see if the construct was
14919 successfully parsed. */
14920 cp_parser_parse_definitely (parser);
14923 /* Stop parsing tentatively. If a parse error has ocurred, restore the
14924 token stream. Otherwise, commit to the tokens we have consumed.
14925 Returns true if no error occurred; false otherwise. */
14928 cp_parser_parse_definitely (parser)
14931 bool error_occurred;
14932 cp_parser_context *context;
14934 /* Remember whether or not an error ocurred, since we are about to
14935 destroy that information. */
14936 error_occurred = cp_parser_error_occurred (parser);
14937 /* Remove the topmost context from the stack. */
14938 context = parser->context;
14939 parser->context = context->next;
14940 /* If no parse errors occurred, commit to the tentative parse. */
14941 if (!error_occurred)
14943 /* Commit to the tokens read tentatively, unless that was
14945 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
14946 cp_lexer_commit_tokens (parser->lexer);
14947 if (!parser->context->deferring_access_checks_p)
14948 /* If in the parent context we are not deferring checks, then
14949 these perform these checks now. */
14950 (cp_parser_perform_deferred_access_checks
14951 (context->deferred_access_checks));
14953 /* Any lookups that were deferred during the tentative parse are
14955 parser->context->deferred_access_checks
14956 = chainon (parser->context->deferred_access_checks,
14957 context->deferred_access_checks);
14959 /* Otherwise, if errors occurred, roll back our state so that things
14960 are just as they were before we began the tentative parse. */
14962 cp_lexer_rollback_tokens (parser->lexer);
14963 /* Add the context to the front of the free list. */
14964 context->next = cp_parser_context_free_list;
14965 cp_parser_context_free_list = context;
14967 return !error_occurred;
14970 /* Returns true if we are parsing tentatively -- but have decided that
14971 we will stick with this tentative parse, even if errors occur. */
14974 cp_parser_committed_to_tentative_parse (parser)
14977 return (cp_parser_parsing_tentatively (parser)
14978 && parser->context->status == CP_PARSER_STATUS_KIND_COMMITTED);
14981 /* Returns non-zero iff an error has occurred during the most recent
14982 tentative parse. */
14985 cp_parser_error_occurred (parser)
14988 return (cp_parser_parsing_tentatively (parser)
14989 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
14992 /* Returns non-zero if GNU extensions are allowed. */
14995 cp_parser_allow_gnu_extensions_p (parser)
14998 return parser->allow_gnu_extensions_p;
15005 static GTY (()) cp_parser *the_parser;
15007 /* External interface. */
15009 /* Parse the entire translation unit. */
15014 bool error_occurred;
15016 the_parser = cp_parser_new ();
15017 error_occurred = cp_parser_translation_unit (the_parser);
15022 return error_occurred;
15025 /* Clean up after parsing the entire translation unit. */
15028 free_parser_stacks ()
15030 /* Nothing to do. */
15033 /* This variable must be provided by every front end. */
15037 #include "gt-cp-parser.h"